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
48 48
49 If you don't know what Access Control Lists are, say N49 If you don't know what Access Control Lists are, say N
50 50
51config 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
72config BTRFS_FS_RUN_SANITY_TESTS51config BTRFS_FS_RUN_SANITY_TESTS
73 bool "Btrfs will run sanity tests upon loading"52 bool "Btrfs will run sanity tests upon loading"
74 depends on BTRFS_FS53 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
33 uuid-tree.o props.o free-space-tree.o tree-checker.o space-info.o \33 uuid-tree.o props.o free-space-tree.o tree-checker.o space-info.o \
34 block-rsv.o delalloc-space.o block-group.o discard.o reflink.o \34 block-rsv.o delalloc-space.o block-group.o discard.o reflink.o \
35 subpage.o tree-mod-log.o extent-io-tree.o fs.o messages.o bio.o \35 subpage.o tree-mod-log.o extent-io-tree.o fs.o messages.o bio.o \
36 lru_cache.o36 lru_cache.o raid-stripe-tree.o
37 37
38btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o38btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o
39btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o
40btrfs-$(CONFIG_BTRFS_FS_REF_VERIFY) += ref-verify.o39btrfs-$(CONFIG_BTRFS_FS_REF_VERIFY) += ref-verify.o
41btrfs-$(CONFIG_BLK_DEV_ZONED) += zoned.o40btrfs-$(CONFIG_BLK_DEV_ZONED) += zoned.o
42btrfs-$(CONFIG_FS_VERITY) += verity.o41btrfs-$(CONFIG_FS_VERITY) += verity.o
fs/btrfs/accessors.h+16−0
@@ -4,6 +4,7 @@ · 3 unmodified lines
4#define BTRFS_ACCESSORS_H4#define BTRFS_ACCESSORS_H
5 5
6#include <linux/stddef.h>6#include <linux/stddef.h>
7#include <asm/unaligned.h>
7 8
8struct btrfs_map_token {9struct btrfs_map_token {
9 struct extent_buffer *eb;10 struct extent_buffer *eb;
@@ -305,6 +306,14 @@ BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32); · 295 unmodified lines
305BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);306BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
306BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);307BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
307 308
309BTRFS_SETGET_FUNCS(stripe_extent_encoding, struct btrfs_stripe_extent, encoding, 8);
310BTRFS_SETGET_FUNCS(raid_stride_devid, struct btrfs_raid_stride, devid, 64);
311BTRFS_SETGET_FUNCS(raid_stride_physical, struct btrfs_raid_stride, physical, 64);
312BTRFS_SETGET_STACK_FUNCS(stack_stripe_extent_encoding,
313 struct btrfs_stripe_extent, encoding, 8);
314BTRFS_SETGET_STACK_FUNCS(stack_raid_stride_devid, struct btrfs_raid_stride, devid, 64);
315BTRFS_SETGET_STACK_FUNCS(stack_raid_stride_physical, struct btrfs_raid_stride, physical, 64);
316
308/* struct btrfs_dev_extent */317/* struct btrfs_dev_extent */
309BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent, chunk_tree, 64);318BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent, chunk_tree, 64);
310BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,319BTRFS_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
349 358
350BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref, count, 32);359BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref, count, 32);
351 360
361BTRFS_SETGET_FUNCS(extent_owner_ref_root_id, struct btrfs_extent_owner_ref,
362 root_id, 64);
363
352BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,364BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
353 type, 8);365 type, 8);
354BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,366BTRFS_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
365 if (type == BTRFS_EXTENT_DATA_REF_KEY)377 if (type == BTRFS_EXTENT_DATA_REF_KEY)
366 return sizeof(struct btrfs_extent_data_ref) +378 return sizeof(struct btrfs_extent_data_ref) +
367 offsetof(struct btrfs_extent_inline_ref, offset);379 offsetof(struct btrfs_extent_inline_ref, offset);
380 if (type == BTRFS_EXTENT_OWNER_REF_KEY)
381 return sizeof(struct btrfs_extent_inline_ref);
368 return 0;382 return 0;
369}383}
370 384
@@ -966,6 +980,8 @@ BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item, · 595 unmodified lines
966 flags, 64);980 flags, 64);
967BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,981BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
968 rescan, 64);982 rescan, 64);
983BTRFS_SETGET_FUNCS(qgroup_status_enable_gen, struct btrfs_qgroup_status_item,
984 enable_gen, 64);
969 985
970/* btrfs_qgroup_info_item */986/* btrfs_qgroup_info_item */
971BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,987BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
fs/btrfs/async-thread.c+6−6
@@ -9,6 +9,7 @@ · 8 unmodified lines
9#include <linux/list.h>9#include <linux/list.h>
10#include <linux/spinlock.h>10#include <linux/spinlock.h>
11#include <linux/freezer.h>11#include <linux/freezer.h>
12#include <trace/events/btrfs.h>
12#include "async-thread.h"13#include "async-thread.h"
13#include "ctree.h"14#include "ctree.h"
14 15
@@ -242,7 +243,7 @@ static void run_ordered_work(struct btrfs_workqueue *wq, · 227 unmodified lines
242 break;243 break;
243 trace_btrfs_ordered_sched(work);244 trace_btrfs_ordered_sched(work);
244 spin_unlock_irqrestore(lock, flags);245 spin_unlock_irqrestore(lock, flags);
245 work->ordered_func(work);246 work->ordered_func(work, false);
246 247
247 /* now take the lock again and drop our item from the list */248 /* now take the lock again and drop our item from the list */
248 spin_lock_irqsave(lock, flags);249 spin_lock_irqsave(lock, flags);
@@ -277,7 +278,7 @@ static void run_ordered_work(struct btrfs_workqueue *wq, · 28 unmodified lines
277 * We don't want to call the ordered free functions with278 * We don't want to call the ordered free functions with
278 * the lock held.279 * the lock held.
279 */280 */
280 work->ordered_free(work);281 work->ordered_func(work, true);
281 /* NB: work must not be dereferenced past this point. */282 /* NB: work must not be dereferenced past this point. */
282 trace_btrfs_all_work_done(wq->fs_info, work);283 trace_btrfs_all_work_done(wq->fs_info, work);
283 }284 }
@@ -285,7 +286,7 @@ static void run_ordered_work(struct btrfs_workqueue *wq, · 1 unmodified line
285 spin_unlock_irqrestore(lock, flags);286 spin_unlock_irqrestore(lock, flags);
286 287
287 if (free_self) {288 if (free_self) {
288 self->ordered_free(self);289 self->ordered_func(self, true);
289 /* NB: self must not be dereferenced past this point. */290 /* NB: self must not be dereferenced past this point. */
290 trace_btrfs_all_work_done(wq->fs_info, self);291 trace_btrfs_all_work_done(wq->fs_info, self);
291 }292 }
@@ -300,7 +301,7 @@ static void btrfs_work_helper(struct work_struct *normal_work) · 8 unmodified lines
300 301
301 /*302 /*
302 * We should not touch things inside work in the following cases:303 * We should not touch things inside work in the following cases:
303 * 1) after work->func() if it has no ordered_free304 * 1) after work->func() if it has no ordered_func(..., true) to free
304 * Since the struct is freed in work->func().305 * Since the struct is freed in work->func().
305 * 2) after setting WORK_DONE_BIT306 * 2) after setting WORK_DONE_BIT
306 * The work may be freed in other threads almost instantly.307 * 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
329}330}
330 331
331void btrfs_init_work(struct btrfs_work *work, btrfs_func_t func,332void 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)
333{334{
334 work->func = func;335 work->func = func;
335 work->ordered_func = ordered_func;336 work->ordered_func = ordered_func;
336 work->ordered_free = ordered_free;
337 INIT_WORK(&work->normal_work, btrfs_work_helper);337 INIT_WORK(&work->normal_work, btrfs_work_helper);
338 INIT_LIST_HEAD(&work->ordered_list);338 INIT_LIST_HEAD(&work->ordered_list);
339 work->flags = 0;339 work->flags = 0;
fs/btrfs/async-thread.h+3−3
@@ -13,11 +13,11 @@ struct btrfs_fs_info; · 12 unmodified lines
13struct btrfs_workqueue;13struct btrfs_workqueue;
14struct btrfs_work;14struct btrfs_work;
15typedef void (*btrfs_func_t)(struct btrfs_work *arg);15typedef void (*btrfs_func_t)(struct btrfs_work *arg);
16typedef void (*btrfs_ordered_func_t)(struct btrfs_work *arg, bool);
16 17
17struct btrfs_work {18struct btrfs_work {
18 btrfs_func_t func;19 btrfs_func_t func;
19 btrfs_func_t ordered_func;20 btrfs_ordered_func_t ordered_func;
20 btrfs_func_t ordered_free;
21 21
22 /* Don't touch things below */22 /* Don't touch things below */
23 struct work_struct normal_work;23 struct work_struct normal_work;
@@ -35,7 +35,7 @@ struct btrfs_workqueue *btrfs_alloc_ordered_workqueue( · 11 unmodified lines
35 struct btrfs_fs_info *fs_info, const char *name,35 struct btrfs_fs_info *fs_info, const char *name,
36 unsigned int flags);36 unsigned int flags);
37void btrfs_init_work(struct btrfs_work *work, btrfs_func_t func,37void 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);
39void btrfs_queue_work(struct btrfs_workqueue *wq,39void btrfs_queue_work(struct btrfs_workqueue *wq,
40 struct btrfs_work *work);40 struct btrfs_work *work);
41void btrfs_destroy_workqueue(struct btrfs_workqueue *wq);41void 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
1129 count, sc, GFP_NOFS);1129 count, sc, GFP_NOFS);
1130 break;1130 break;
1131 }1131 }
1132 case BTRFS_EXTENT_OWNER_REF_KEY:
1133 ASSERT(btrfs_fs_incompat(ctx->fs_info, SIMPLE_QUOTA));
1134 break;
1132 default:1135 default:
1133 WARN_ON(1);1136 WARN_ON(1);
1134 }1137 }
@@ -2998,7 +3001,7 @@ int btrfs_backref_iter_next(struct btrfs_backref_iter *iter) · 1863 unmodified lines
2998}3001}
2999 3002
3000void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,3003void 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)
3002{3005{
3003 int i;3006 int i;
3004 3007
fs/btrfs/backref.h+5−5
@@ -247,7 +247,7 @@ struct prelim_ref { · 246 unmodified lines
247 struct rb_node rbnode;247 struct rb_node rbnode;
248 u64 root_id;248 u64 root_id;
249 struct btrfs_key key_for_search;249 struct btrfs_key key_for_search;
250 int level;250 u8 level;
251 int count;251 int count;
252 struct extent_inode_elem *inode_list;252 struct extent_inode_elem *inode_list;
253 u64 parent;253 u64 parent;
@@ -440,11 +440,11 @@ struct btrfs_backref_cache { · 186 unmodified lines
440 * Reloction backref cache require more info for reloc root compared440 * Reloction backref cache require more info for reloc root compared
441 * to generic backref cache.441 * to generic backref cache.
442 */442 */
443 unsigned int is_reloc;443 bool is_reloc;
444};444};
445 445
446void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,446void 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);
448struct btrfs_backref_node *btrfs_backref_alloc_node(448struct btrfs_backref_node *btrfs_backref_alloc_node(
449 struct btrfs_backref_cache *cache, u64 bytenr, int level);449 struct btrfs_backref_cache *cache, u64 bytenr, int level);
450struct btrfs_backref_edge *btrfs_backref_alloc_edge(450struct btrfs_backref_edge *btrfs_backref_alloc_edge(
@@ -533,9 +533,9 @@ void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, · 82 unmodified lines
533void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);533void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
534 534
535static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,535static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
536 u64 bytenr, int errno)536 u64 bytenr, int error)
537{537{
538 btrfs_panic(fs_info, errno,538 btrfs_panic(fs_info, error,
539 "Inconsistency in backref cache found at offset %llu",539 "Inconsistency in backref cache found at offset %llu",
540 bytenr);540 bytenr);
541}541}
fs/btrfs/bio.c+33−14
@@ -10,11 +10,11 @@ · 9 unmodified lines
10#include "volumes.h"10#include "volumes.h"
11#include "raid56.h"11#include "raid56.h"
12#include "async-thread.h"12#include "async-thread.h"
13#include "check-integrity.h"
14#include "dev-replace.h"13#include "dev-replace.h"
15#include "rcu-string.h"14#include "rcu-string.h"
16#include "zoned.h"15#include "zoned.h"
17#include "file-item.h"16#include "file-item.h"
17#include "raid-stripe-tree.h"
18 18
19static struct bio_set btrfs_bioset;19static struct bio_set btrfs_bioset;
20static struct bio_set btrfs_clone_bioset;20static struct bio_set btrfs_clone_bioset;
@@ -416,6 +416,9 @@ static void btrfs_orig_write_end_io(struct bio *bio) · 395 unmodified lines
416 else416 else
417 bio->bi_status = BLK_STS_OK;417 bio->bi_status = BLK_STS_OK;
418 418
419 if (bio_op(bio) == REQ_OP_ZONE_APPEND && !bio->bi_status)
420 stripe->physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
421
419 btrfs_orig_bbio_end_io(bbio);422 btrfs_orig_bbio_end_io(bbio);
420 btrfs_put_bioc(bioc);423 btrfs_put_bioc(bioc);
421}424}
@@ -427,6 +430,8 @@ static void btrfs_clone_write_end_io(struct bio *bio) · 5 unmodified lines
427 if (bio->bi_status) {430 if (bio->bi_status) {
428 atomic_inc(&stripe->bioc->error);431 atomic_inc(&stripe->bioc->error);
429 btrfs_log_dev_io_error(bio, stripe->dev);432 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;
430 }435 }
431 436
432 /* Pass on control to the original bio this one was cloned from */437 /* 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
463 (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev),468 (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev),
464 dev->devid, bio->bi_iter.bi_size);469 dev->devid, bio->bi_iter.bi_size);
465 470
466 btrfsic_check_bio(bio);
467
468 if (bio->bi_opf & REQ_BTRFS_CGROUP_PUNT)471 if (bio->bi_opf & REQ_BTRFS_CGROUP_PUNT)
469 blkcg_punt_bio_submit(bio);472 blkcg_punt_bio_submit(bio);
470 else473 else
@@ -490,6 +493,7 @@ static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr) · 19 unmodified lines
490 bio->bi_private = &bioc->stripes[dev_nr];493 bio->bi_private = &bioc->stripes[dev_nr];
491 bio->bi_iter.bi_sector = bioc->stripes[dev_nr].physical >> SECTOR_SHIFT;494 bio->bi_iter.bi_sector = bioc->stripes[dev_nr].physical >> SECTOR_SHIFT;
492 bioc->stripes[dev_nr].bioc = bioc;495 bioc->stripes[dev_nr].bioc = bioc;
496 bioc->size = bio->bi_iter.bi_size;
493 btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio);497 btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio);
494}498}
495 499
@@ -499,6 +503,8 @@ static void __btrfs_submit_bio(struct bio *bio, struct btrfs_io_context *bioc, · 3 unmodified lines
499 if (!bioc) {503 if (!bioc) {
500 /* Single mirror read/write fast path. */504 /* Single mirror read/write fast path. */
501 btrfs_bio(bio)->mirror_num = mirror_num;505 btrfs_bio(bio)->mirror_num = mirror_num;
506 if (bio_op(bio) != REQ_OP_READ)
507 btrfs_bio(bio)->orig_physical = smap->physical;
502 bio->bi_iter.bi_sector = smap->physical >> SECTOR_SHIFT;508 bio->bi_iter.bi_sector = smap->physical >> SECTOR_SHIFT;
503 if (bio_op(bio) != REQ_OP_READ)509 if (bio_op(bio) != REQ_OP_READ)
504 btrfs_bio(bio)->orig_physical = smap->physical;510 btrfs_bio(bio)->orig_physical = smap->physical;
@@ -568,13 +574,20 @@ static void run_one_async_start(struct btrfs_work *work) · 63 unmodified lines
568 *574 *
569 * At IO completion time the csums attached on the ordered extent record are575 * At IO completion time the csums attached on the ordered extent record are
570 * inserted into the tree.576 * inserted into the tree.
577 *
578 * If called with @do_free == true, then it will free the work struct.
571 */579 */
572static void run_one_async_done(struct btrfs_work *work)580static void run_one_async_done(struct btrfs_work *work, bool do_free)
573{581{
574 struct async_submit_bio *async =582 struct async_submit_bio *async =
575 container_of(work, struct async_submit_bio, work);583 container_of(work, struct async_submit_bio, work);
576 struct bio *bio = &async->bbio->bio;584 struct bio *bio = &async->bbio->bio;
577 585
586 if (do_free) {
587 kfree(container_of(work, struct async_submit_bio, work));
588 return;
589 }
590
578 /* If an error occurred we just want to clean up the bio and move on. */591 /* If an error occurred we just want to clean up the bio and move on. */
579 if (bio->bi_status) {592 if (bio->bi_status) {
580 btrfs_orig_bbio_end_io(async->bbio);593 btrfs_orig_bbio_end_io(async->bbio);
@@ -590,11 +603,6 @@ static void run_one_async_done(struct btrfs_work *work) · 9 unmodified lines
590 __btrfs_submit_bio(bio, async->bioc, &async->smap, async->mirror_num);603 __btrfs_submit_bio(bio, async->bioc, &async->smap, async->mirror_num);
591}604}
592 605
593static void run_one_async_free(struct btrfs_work *work)
594{
595 kfree(container_of(work, struct async_submit_bio, work));
596}
597
598static bool should_async_write(struct btrfs_bio *bbio)606static bool should_async_write(struct btrfs_bio *bbio)
599{607{
600 /* Submit synchronously if the checksum implementation is fast. */608 /* 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
636 async->smap = *smap;644 async->smap = *smap;
637 async->mirror_num = mirror_num;645 async->mirror_num = mirror_num;
638 646
639 btrfs_init_work(&async->work, run_one_async_start, run_one_async_done,647 btrfs_init_work(&async->work, run_one_async_start, run_one_async_done);
640 run_one_async_free);
641 btrfs_queue_work(fs_info->workers, &async->work);648 btrfs_queue_work(fs_info->workers, &async->work);
642 return true;649 return true;
643}650}
@@ -657,9 +664,11 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) · 13 unmodified lines
657 blk_status_t ret;664 blk_status_t ret;
658 int error;665 int error;
659 666
667 smap.is_scrub = !bbio->inode;
668
660 btrfs_bio_counter_inc_blocked(fs_info);669 btrfs_bio_counter_inc_blocked(fs_info);
661 error = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,670 error = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
662 &bioc, &smap, &mirror_num, 1);671 &bioc, &smap, &mirror_num);
663 if (error) {672 if (error) {
664 ret = errno_to_blk_status(error);673 ret = errno_to_blk_status(error);
665 goto fail;674 goto fail;
@@ -691,6 +700,18 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) · 25 unmodified lines
691 bio->bi_opf |= REQ_OP_ZONE_APPEND;700 bio->bi_opf |= REQ_OP_ZONE_APPEND;
692 }701 }
693 702
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
694 /*715 /*
695 * Csum items for reloc roots have already been cloned at this716 * Csum items for reloc roots have already been cloned at this
696 * point, so they are handled as part of the no-checksum case.717 * 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
779 bio_init(&bio, smap.dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);800 bio_init(&bio, smap.dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);
780 bio.bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT;801 bio.bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT;
781 __bio_add_page(&bio, page, length, pg_offset);802 __bio_add_page(&bio, page, length, pg_offset);
782
783 btrfsic_check_bio(&bio);
784 ret = submit_bio_wait(&bio);803 ret = submit_bio_wait(&bio);
785 if (ret) {804 if (ret) {
786 /* try to remap that extent elsewhere? */805 /* 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
935 caching_ctl->block_group = cache;935 caching_ctl->block_group = cache;
936 refcount_set(&caching_ctl->count, 2);936 refcount_set(&caching_ctl->count, 2);
937 atomic_set(&caching_ctl->progress, 0);937 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);
939 939
940 spin_lock(&cache->lock);940 spin_lock(&cache->lock);
941 if (cache->cached != BTRFS_CACHE_NO) {941 if (cache->cached != BTRFS_CACHE_NO) {
@@ -1286,7 +1286,7 @@ out: · 344 unmodified lines
1286 /* Once for the lookup reference */1286 /* Once for the lookup reference */
1287 btrfs_put_block_group(block_group);1287 btrfs_put_block_group(block_group);
1288 if (remove_rsv)1288 if (remove_rsv)
1289 btrfs_delayed_refs_rsv_release(fs_info, 1);1289 btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
1290 btrfs_free_path(path);1290 btrfs_free_path(path);
1291 return ret;1291 return ret;
1292}1292}
@@ -2601,7 +2601,7 @@ static int insert_dev_extent(struct btrfs_trans_handle *trans, · 1308 unmodified lines
2601 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);2601 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
2602 2602
2603 btrfs_set_dev_extent_length(leaf, extent, num_bytes);2603 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
2604 btrfs_mark_buffer_dirty(leaf);2604 btrfs_mark_buffer_dirty(trans, leaf);
2605out:2605out:
2606 btrfs_free_path(path);2606 btrfs_free_path(path);
2607 return ret;2607 return ret;
@@ -2709,7 +2709,7 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) · 101 unmodified lines
2709 2709
2710 /* Already aborted the transaction if it failed. */2710 /* Already aborted the transaction if it failed. */
2711next:2711next:
2712 btrfs_delayed_refs_rsv_release(fs_info, 1);2712 btrfs_dec_delayed_refs_rsv_bg_inserts(fs_info);
2713 list_del_init(&block_group->bg_list);2713 list_del_init(&block_group->bg_list);
2714 clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags);2714 clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags);
2715 }2715 }
@@ -2819,8 +2819,7 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *tran · 103 unmodified lines
2819#endif2819#endif
2820 2820
2821 list_add_tail(&cache->bg_list, &trans->new_bgs);2821 list_add_tail(&cache->bg_list, &trans->new_bgs);
2822 trans->delayed_ref_updates++;2822 btrfs_inc_delayed_refs_rsv_bg_inserts(fs_info);
2823 btrfs_update_delayed_refs_rsv(trans);
2824 2823
2825 set_avail_alloc_bits(fs_info, type);2824 set_avail_alloc_bits(fs_info, type);
2826 return cache;2825 return cache;
@@ -3025,7 +3024,7 @@ static int update_block_group_item(struct btrfs_trans_handle *trans, · 198 unmodified lines
3025 cache->global_root_id);3024 cache->global_root_id);
3026 btrfs_set_stack_block_group_flags(&bgi, cache->flags);3025 btrfs_set_stack_block_group_flags(&bgi, cache->flags);
3027 write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));3026 write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
3028 btrfs_mark_buffer_dirty(leaf);3027 btrfs_mark_buffer_dirty(trans, leaf);
3029fail:3028fail:
3030 btrfs_release_path(path);3029 btrfs_release_path(path);
3031 /*3030 /*
@@ -3051,7 +3050,6 @@ static int cache_save_setup(struct btrfs_block_group *block_group, · 19 unmodified lines
3051 struct btrfs_path *path)3050 struct btrfs_path *path)
3052{3051{
3053 struct btrfs_fs_info *fs_info = block_group->fs_info;3052 struct btrfs_fs_info *fs_info = block_group->fs_info;
3054 struct btrfs_root *root = fs_info->tree_root;
3055 struct inode *inode = NULL;3053 struct inode *inode = NULL;
3056 struct extent_changeset *data_reserved = NULL;3054 struct extent_changeset *data_reserved = NULL;
3057 u64 alloc_hint = 0;3055 u64 alloc_hint = 0;
@@ -3103,7 +3101,7 @@ again: · 45 unmodified lines
3103 * time.3101 * time.
3104 */3102 */
3105 BTRFS_I(inode)->generation = 0;3103 BTRFS_I(inode)->generation = 0;
3106 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));3104 ret = btrfs_update_inode(trans, BTRFS_I(inode));
3107 if (ret) {3105 if (ret) {
3108 /*3106 /*
3109 * So theoretically we could recover from this, simply set the3107 * So theoretically we could recover from this, simply set the
@@ -3370,7 +3368,7 @@ again: · 260 unmodified lines
3370 if (should_put)3368 if (should_put)
3371 btrfs_put_block_group(cache);3369 btrfs_put_block_group(cache);
3372 if (drop_reserve)3370 if (drop_reserve)
3373 btrfs_delayed_refs_rsv_release(fs_info, 1);3371 btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
3374 /*3372 /*
3375 * Avoid blocking other tasks for too long. It might even save3373 * Avoid blocking other tasks for too long. It might even save
3376 * us from writing caches for block groups that are going to be3374 * 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
3474 cache_save_setup(cache, trans, path);3472 cache_save_setup(cache, trans, path);
3475 3473
3476 if (!ret)3474 if (!ret)
3477 ret = btrfs_run_delayed_refs(trans,3475 ret = btrfs_run_delayed_refs(trans, U64_MAX);
3478 (unsigned long) -1);
3479 3476
3480 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {3477 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3481 cache->io_ctl.inode = NULL;3478 cache->io_ctl.inode = NULL;
@@ -3518,7 +3515,7 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) · 36 unmodified lines
3518 /* If its not on the io list, we need to put the block group */3515 /* If its not on the io list, we need to put the block group */
3519 if (should_put)3516 if (should_put)
3520 btrfs_put_block_group(cache);3517 btrfs_put_block_group(cache);
3521 btrfs_delayed_refs_rsv_release(fs_info, 1);3518 btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
3522 spin_lock(&cur_trans->dirty_bgs_lock);3519 spin_lock(&cur_trans->dirty_bgs_lock);
3523 }3520 }
3524 spin_unlock(&cur_trans->dirty_bgs_lock);3521 spin_unlock(&cur_trans->dirty_bgs_lock);
@@ -3543,12 +3540,12 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, · 18 unmodified lines
3543 u64 bytenr, u64 num_bytes, bool alloc)3540 u64 bytenr, u64 num_bytes, bool alloc)
3544{3541{
3545 struct btrfs_fs_info *info = trans->fs_info;3542 struct btrfs_fs_info *info = trans->fs_info;
3546 struct btrfs_block_group *cache = NULL;3543 struct btrfs_space_info *space_info;
3547 u64 total = num_bytes;3544 struct btrfs_block_group *cache;
3548 u64 old_val;3545 u64 old_val;
3549 u64 byte_in_group;3546 bool reclaim = false;
3547 bool bg_already_dirty = true;
3550 int factor;3548 int factor;
3551 int ret = 0;
3552 3549
3553 /* Block accounting for super block */3550 /* Block accounting for super block */
3554 spin_lock(&info->delalloc_root_lock);3551 spin_lock(&info->delalloc_root_lock);
@@ -3560,97 +3557,86 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, · 5 unmodified lines
3560 btrfs_set_super_bytes_used(info->super_copy, old_val);3557 btrfs_set_super_bytes_used(info->super_copy, old_val);
3561 spin_unlock(&info->delalloc_root_lock);3558 spin_unlock(&info->delalloc_root_lock);
3562 3559
3563 while (total) {3560 cache = btrfs_lookup_block_group(info, bytenr);
3564 struct btrfs_space_info *space_info;3561 if (!cache)
3565 bool reclaim = false;3562 return -ENOENT;
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);
3574 3563
3575 /*3564 /* An extent can not span multiple block groups. */
3576 * If this block group has free space cache written out, we3565 ASSERT(bytenr + num_bytes <= cache->start + cache->length);
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);
3583 3566
3584 byte_in_group = bytenr - cache->start;3567 space_info = cache->space_info;
3585 WARN_ON(byte_in_group > cache->length);3568 factor = btrfs_bg_type_to_factor(cache->flags);
3586 3569
3587 spin_lock(&space_info->lock);3570 /*
3588 spin_lock(&cache->lock);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);
3589 3578
3590 if (btrfs_test_opt(info, SPACE_CACHE) &&3579 spin_lock(&space_info->lock);
3591 cache->disk_cache_state < BTRFS_DC_CLEAR)3580 spin_lock(&cache->lock);
3592 cache->disk_cache_state = BTRFS_DC_CLEAR;
3593 3581
3594 old_val = cache->used;3582 if (btrfs_test_opt(info, SPACE_CACHE) &&
3595 num_bytes = min(total, cache->length - byte_in_group);3583 cache->disk_cache_state < BTRFS_DC_CLEAR)
3596 if (alloc) {3584 cache->disk_cache_state = BTRFS_DC_CLEAR;
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;
3613 3585
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;
3615 3603
3616 spin_unlock(&cache->lock);3604 reclaim = should_reclaim_block_group(cache, num_bytes);
3617 spin_unlock(&space_info->lock);
3618 3605
3619 set_extent_bit(&trans->transaction->pinned_extents,3606 spin_unlock(&cache->lock);
3620 bytenr, bytenr + num_bytes - 1,3607 spin_unlock(&space_info->lock);
3621 EXTENT_DIRTY, NULL);
3622 }
3623 3608
3624 spin_lock(&trans->transaction->dirty_bgs_lock);3609 set_extent_bit(&trans->transaction->pinned_extents, bytenr,
3625 if (list_empty(&cache->dirty_list)) {3610 bytenr + num_bytes - 1, EXTENT_DIRTY, NULL);
3626 list_add_tail(&cache->dirty_list,3611 }
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);
3632 3612
3633 /*3613 spin_lock(&trans->transaction->dirty_bgs_lock);
3634 * No longer have used bytes in this block group, queue it for3614 if (list_empty(&cache->dirty_list)) {
3635 * deletion. We do this after adding the block group to the3615 list_add_tail(&cache->dirty_list, &trans->transaction->dirty_bgs);
3636 * dirty list to avoid races between cleaner kthread and space3616 bg_already_dirty = false;
3637 * cache writeout.3617 btrfs_get_block_group(cache);
3638 */3618 }
3639 if (!alloc && old_val == 0) {3619 spin_unlock(&trans->transaction->dirty_bgs_lock);
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 }
3645 3620
3646 btrfs_put_block_group(cache);3621 /*
3647 total -= num_bytes;3622 * No longer have used bytes in this block group, queue it for deletion.
3648 bytenr += num_bytes;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);
3649 }3631 }
3650 3632
3633 btrfs_put_block_group(cache);
3634
3651 /* Modified block groups are accounted for in the delayed_refs_rsv. */3635 /* Modified block groups are accounted for in the delayed_refs_rsv. */
3652 btrfs_update_delayed_refs_rsv(trans);3636 if (!bg_already_dirty)
3653 return ret;3637 btrfs_inc_delayed_refs_rsv_bg_updates(info);
3638
3639 return 0;
3654}3640}
3655 3641
3656/*3642/*
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
221 if (num_bytes == 0)221 if (num_bytes == 0)
222 return 0;222 return 0;
223 223
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);
225 if (!ret)226 if (!ret)
226 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, true);227 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, true);
227 228
@@ -261,7 +262,8 @@ int btrfs_block_rsv_refill(struct btrfs_fs_info *fs_info, · 33 unmodified lines
261 if (!ret)262 if (!ret)
262 return 0;263 return 0;
263 264
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);
265 if (!ret) {267 if (!ret) {
266 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, false);268 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, false);
267 return 0;269 return 0;
@@ -279,10 +281,10 @@ u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info, · 11 unmodified lines
279 struct btrfs_block_rsv *target = NULL;281 struct btrfs_block_rsv *target = NULL;
280 282
281 /*283 /*
282 * If we are the delayed_rsv then push to the global rsv, otherwise dump284 * If we are a delayed block reserve then push to the global rsv,
283 * into the delayed rsv if it is not full.285 * otherwise dump into the global delayed reserve if it is not full.
284 */286 */
285 if (block_rsv == delayed_rsv)287 if (block_rsv->type == BTRFS_BLOCK_RSV_DELOPS)
286 target = global_rsv;288 target = global_rsv;
287 else if (block_rsv != global_rsv && !btrfs_block_rsv_full(delayed_rsv))289 else if (block_rsv != global_rsv && !btrfs_block_rsv_full(delayed_rsv))
288 target = delayed_rsv;290 target = delayed_rsv;
@@ -354,6 +356,11 @@ void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info) · 65 unmodified lines
354 min_items++;356 min_items++;
355 }357 }
356 358
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
357 /*364 /*
358 * But we also want to reserve enough space so we can do the fallback365 * But we also want to reserve enough space so we can do the fallback
359 * global reserve for an unlink, which is an additional366 * 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
405 case BTRFS_EXTENT_TREE_OBJECTID:412 case BTRFS_EXTENT_TREE_OBJECTID:
406 case BTRFS_FREE_SPACE_TREE_OBJECTID:413 case BTRFS_FREE_SPACE_TREE_OBJECTID:
407 case BTRFS_BLOCK_GROUP_TREE_OBJECTID:414 case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
415 case BTRFS_RAID_STRIPE_TREE_OBJECTID:
408 root->block_rsv = &fs_info->delayed_refs_rsv;416 root->block_rsv = &fs_info->delayed_refs_rsv;
409 break;417 break;
410 case BTRFS_ROOT_TREE_OBJECTID:418 case BTRFS_ROOT_TREE_OBJECTID:
@@ -517,8 +525,8 @@ again: · 106 unmodified lines
517 block_rsv->type, ret);525 block_rsv->type, ret);
518 }526 }
519try_reserve:527try_reserve:
520 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,528 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info,
521 BTRFS_RESERVE_NO_FLUSH);529 blocksize, BTRFS_RESERVE_NO_FLUSH);
522 if (!ret)530 if (!ret)
523 return block_rsv;531 return block_rsv;
524 /*532 /*
@@ -539,7 +547,7 @@ try_reserve: · 14 unmodified lines
539 * one last time to force a reservation if there's enough actual space547 * one last time to force a reservation if there's enough actual space
540 * on disk to make the reservation.548 * on disk to make the reservation.
541 */549 */
542 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,550 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv->space_info, blocksize,
543 BTRFS_RESERVE_FLUSH_EMERGENCY);551 BTRFS_RESERVE_FLUSH_EMERGENCY);
544 if (!ret)552 if (!ret)
545 return block_rsv;553 return block_rsv;
fs/btrfs/btrfs_inode.h+44−36
@@ -8,6 +8,8 @@ · 7 unmodified lines
8 8
9#include <linux/hash.h>9#include <linux/hash.h>
10#include <linux/refcount.h>10#include <linux/refcount.h>
11#include <linux/fscrypt.h>
12#include <trace/events/btrfs.h>
11#include "extent_map.h"13#include "extent_map.h"
12#include "extent_io.h"14#include "extent_io.h"
13#include "ordered-data.h"15#include "ordered-data.h"
@@ -79,11 +81,21 @@ struct btrfs_inode { · 65 unmodified lines
79 */81 */
80 struct btrfs_key location;82 struct btrfs_key location;
81 83
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
82 /*93 /*
83 * Lock for counters and all fields used to determine if the inode is in94 * Lock for counters and all fields used to determine if the inode is in
84 * the log or not (last_trans, last_sub_trans, last_log_commit,95 * the log or not (last_trans, last_sub_trans, last_log_commit,
85 * logged_trans), to access/update new_delalloc_bytes and to update the96 * logged_trans), to access/update delalloc_bytes, new_delalloc_bytes,
86 * VFS' inode number of bytes used.97 * defrag_bytes, disk_i_size, outstanding_extents, csum_bytes and to
98 * update the VFS' inode number of bytes used.
87 */99 */
88 spinlock_t lock;100 spinlock_t lock;
89 101
@@ -102,8 +114,18 @@ struct btrfs_inode { · 12 unmodified lines
102 /* held while logging the inode in tree-log.c */114 /* held while logging the inode in tree-log.c */
103 struct mutex log_mutex;115 struct mutex log_mutex;
104 116
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
105 /* used to order data wrt metadata */125 /* 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;
107 129
108 /* list of all the delalloc inodes in the FS. There are times we need130 /* list of all the delalloc inodes in the FS. There are times we need
109 * to write all the delalloc pages to disk, and this list is used131 * to write all the delalloc pages to disk, and this list is used
@@ -122,28 +144,31 @@ struct btrfs_inode { · 12 unmodified lines
122 u64 generation;144 u64 generation;
123 145
124 /*146 /*
125 * transid of the trans_handle that last modified this inode147 * ID of the transaction handle that last modified this inode.
148 * Protected by 'lock'.
126 */149 */
127 u64 last_trans;150 u64 last_trans;
128 151
129 /*152 /*
130 * transid that last logged this inode153 * ID of the transaction that last logged this inode.
154 * Protected by 'lock'.
131 */155 */
132 u64 logged_trans;156 u64 logged_trans;
133 157
134 /*158 /*
135 * log transid when this inode was last modified159 * Log transaction ID when this inode was last modified.
160 * Protected by 'lock'.
136 */161 */
137 int last_sub_trans;162 int last_sub_trans;
138 163
139 /* a local copy of root's last_log_commit */164 /* A local copy of root's last_log_commit. Protected by 'lock'. */
140 int last_log_commit;165 int last_log_commit;
141 166
142 union {167 union {
143 /*168 /*
144 * Total number of bytes pending delalloc, used by stat to169 * Total number of bytes pending delalloc, used by stat to
145 * calculate the real block usage of the file. This is used170 * calculate the real block usage of the file. This is used
146 * only for files.171 * only for files. Protected by 'lock'.
147 */172 */
148 u64 delalloc_bytes;173 u64 delalloc_bytes;
149 /*174 /*
@@ -161,7 +186,7 @@ struct btrfs_inode { · 11 unmodified lines
161 * Total number of bytes pending delalloc that fall within a file186 * Total number of bytes pending delalloc that fall within a file
162 * range that is either a hole or beyond EOF (and no prealloc extent187 * range that is either a hole or beyond EOF (and no prealloc extent
163 * exists in the range). This is always <= delalloc_bytes and this188 * 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'.
165 */190 */
166 u64 new_delalloc_bytes;191 u64 new_delalloc_bytes;
167 /*192 /*
@@ -172,15 +197,15 @@ struct btrfs_inode { · 4 unmodified lines
172 };197 };
173 198
174 /*199 /*
175 * total number of bytes pending defrag, used by stat to check whether200 * Total number of bytes pending defrag, used by stat to check whether
176 * it needs COW.201 * it needs COW. Protected by 'lock'.
177 */202 */
178 u64 defrag_bytes;203 u64 defrag_bytes;
179 204
180 /*205 /*
181 * the size of the file stored in the metadata on disk. data=ordered206 * The size of the file stored in the metadata on disk. data=ordered
182 * means the in-memory i_size might be larger than the size on disk207 * 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'.
184 */209 */
185 u64 disk_i_size;210 u64 disk_i_size;
186 211
@@ -214,7 +239,7 @@ struct btrfs_inode { · 27 unmodified lines
214 239
215 /*240 /*
216 * Number of bytes outstanding that are going to need csums. This is241 * 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'.
218 */243 */
219 u64 csum_bytes;244 u64 csum_bytes;
220 245
@@ -223,30 +248,13 @@ struct btrfs_inode { · 2 unmodified lines
223 /* Read-only compatibility flags, upper half of inode_item::flags */248 /* Read-only compatibility flags, upper half of inode_item::flags */
224 u32 ro_flags;249 u32 ro_flags;
225 250
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
234 struct btrfs_block_rsv block_rsv;251 struct btrfs_block_rsv block_rsv;
235 252
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
246 struct btrfs_delayed_node *delayed_node;253 struct btrfs_delayed_node *delayed_node;
247 254
248 /* File creation time. */255 /* File creation time. */
249 struct timespec64 i_otime;256 u64 i_otime_sec;
257 u32 i_otime_nsec;
250 258
251 /* Hook into fs_info->delayed_iputs */259 /* Hook into fs_info->delayed_iputs */
252 struct list_head delayed_iput;260 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
387 spin_lock(&inode->lock);395 spin_lock(&inode->lock);
388 if (inode->logged_trans == generation &&396 if (inode->logged_trans == generation &&
389 inode->last_sub_trans <= inode->last_log_commit &&397 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))
391 ret = true;399 ret = true;
392 spin_unlock(&inode->lock);400 spin_unlock(&inode->lock);
393 return ret;401 return ret;
@@ -481,9 +489,9 @@ struct extent_map *btrfs_get_extent(struct btrfs_inode *inode, · 87 unmodified lines
481 struct page *page, size_t pg_offset,489 struct page *page, size_t pg_offset,
482 u64 start, u64 end);490 u64 start, u64 end);
483int btrfs_update_inode(struct btrfs_trans_handle *trans,491int btrfs_update_inode(struct btrfs_trans_handle *trans,
484 struct btrfs_root *root, struct btrfs_inode *inode);492 struct btrfs_inode *inode);
485int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,493int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
486 struct btrfs_root *root, struct btrfs_inode *inode);494 struct btrfs_inode *inode);
487int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct btrfs_inode *inode);495int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct btrfs_inode *inode);
488int btrfs_orphan_cleanup(struct btrfs_root *root);496int btrfs_orphan_cleanup(struct btrfs_root *root);
489int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size);497int 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
128struct btrfsic_dev_state;
129struct btrfsic_state;
130
131struct 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 */
172struct 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
183struct 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
192struct btrfsic_block_hashtable {
193 struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
194};
195
196struct btrfsic_block_link_hashtable {
197 struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
198};
199
200struct btrfsic_dev_state_hashtable {
201 struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
202};
203
204struct 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() */
216struct 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 */
233struct 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
246static 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);
250static void btrfsic_read_from_block_data(
251 struct btrfsic_block_data_ctx *block_ctx,
252 void *dst, u32 offset, size_t len);
253static 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);
265static 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);
269static 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);
272static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx);
273static int btrfsic_read_block(struct btrfsic_state *state,
274 struct btrfsic_block_data_ctx *block_ctx);
275static int btrfsic_process_written_superblock(
276 struct btrfsic_state *state,
277 struct btrfsic_block *const block,
278 struct btrfs_super_block *const super_hdr);
279static void btrfsic_bio_end_io(struct bio *bp);
280static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
281 const struct btrfsic_block *block,
282 int recursion_level);
283static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
284 struct btrfsic_block *const block,
285 int recursion_level);
286static void btrfsic_print_add_link(const struct btrfsic_state *state,
287 const struct btrfsic_block_link *l);
288static void btrfsic_print_rem_link(const struct btrfsic_state *state,
289 const struct btrfsic_block_link *l);
290static char btrfsic_get_block_type(const struct btrfsic_state *state,
291 const struct btrfsic_block *block);
292static void btrfsic_dump_tree(const struct btrfsic_state *state);
293static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
294 const struct btrfsic_block *block,
295 int indent_level);
296static 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);
302static 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);
311static 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);
318static struct btrfsic_dev_state *btrfsic_dev_state_lookup(dev_t dev);
319static 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
324static struct mutex btrfsic_mutex;
325static int btrfsic_is_initialized;
326static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
327
328
329static 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
356static 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
367static 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
373static 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
384static 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
395static 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
401static 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
413static 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
424static 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
431static 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
439static 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
450static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
451{
452 list_del(&b->collision_resolving_node);
453}
454
455static 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
474static 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
483static 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
499static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
500{
501 list_del(&l->collision_resolving_node);
502}
503
504static 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
532static 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
541static 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
552static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
553{
554 list_del(&ds->collision_resolving_node);
555}
556
557static 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
572static 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
698static 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
858out:
859 put_page(page);
860 return ret;
861}
862
863static 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
873static 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
880static 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
903continue_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
921continue_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) {
939leaf_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
1046continue_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
1115one_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
1139static 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
1166static 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
1310static 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
1455static 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
1509static 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
1536static 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
1601static 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 */
1652static 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
1687static 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
1704again:
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
2030continue_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
2038static 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
2084static 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
2219static 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
2329static 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
2378static 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
2393static 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
2408static 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
2423static void btrfsic_dump_tree(const struct btrfsic_state *state)
2424{
2425 btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
2426}
2427
2428static 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
2491static 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
2533static 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
2586static 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
2636static 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
2642static 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
2680static 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
2709void 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
2731int 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
2804void 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
10void btrfsic_check_bio(struct bio *bio);
11#else
12static inline void btrfsic_check_bio(struct bio *bio) { }
13#endif
14
15int btrfsic_mount(struct btrfs_fs_info *fs_info,
16 struct btrfs_fs_devices *fs_devices,
17 int including_extent_data, u32 print_mask);
18void 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
193 unsigned long index = cb->start >> PAGE_SHIFT;193 unsigned long index = cb->start >> PAGE_SHIFT;
194 unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT;194 unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT;
195 struct folio_batch fbatch;195 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);
197 int i;197 int i;
198 int ret;198 int ret;
199 199
200 if (errno)200 if (error)
201 mapping_set_error(inode->i_mapping, errno);201 mapping_set_error(inode->i_mapping, error);
202 202
203 folio_batch_init(&fbatch);203 folio_batch_init(&fbatch);
204 while (index <= end_index) {204 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
230 * cause could be a bug, eg. due to ENOSPC, and not for common errors that are230 * cause could be a bug, eg. due to ENOSPC, and not for common errors that are
231 * caused by external factors.231 * caused by external factors.
232 */232 */
233bool __cold abort_should_print_stack(int errno)233bool __cold abort_should_print_stack(int error)
234{234{
235 switch (errno) {235 switch (error) {
236 case -EIO:236 case -EIO:
237 case -EROFS:237 case -EROFS:
238 case -ENOMEM:238 case -ENOMEM:
@@ -316,6 +316,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, · 77 unmodified lines
316 int ret = 0;316 int ret = 0;
317 int level;317 int level;
318 struct btrfs_disk_key disk_key;318 struct btrfs_disk_key disk_key;
319 u64 reloc_src_root = 0;
319 320
320 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&321 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
321 trans->transid != fs_info->running_transaction->transid);322 trans->transid != fs_info->running_transaction->transid);
@@ -328,9 +329,11 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, · 6 unmodified lines
328 else329 else
329 btrfs_node_key(buf, &disk_key, 0);330 btrfs_node_key(buf, &disk_key, 0);
330 331
332 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
333 reloc_src_root = btrfs_header_owner(buf);
331 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,334 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
332 &disk_key, level, buf->start, 0,335 &disk_key, level, buf->start, 0,
333 BTRFS_NESTING_NEW_ROOT);336 reloc_src_root, BTRFS_NESTING_NEW_ROOT);
334 if (IS_ERR(cow))337 if (IS_ERR(cow))
335 return PTR_ERR(cow);338 return PTR_ERR(cow);
336 339
@@ -359,7 +362,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans, · 22 unmodified lines
359 return ret;362 return ret;
360 }363 }
361 364
362 btrfs_mark_buffer_dirty(cow);365 btrfs_mark_buffer_dirty(trans, cow);
363 *cow_ret = cow;366 *cow_ret = cow;
364 return 0;367 return 0;
365}368}
@@ -518,13 +521,13 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, · 152 unmodified lines
518 * bytes the allocator should try to find free next to the block it returns.521 * bytes the allocator should try to find free next to the block it returns.
519 * This is just a hint and may be ignored by the allocator.522 * This is just a hint and may be ignored by the allocator.
520 */523 */
521static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,524int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
522 struct btrfs_root *root,525 struct btrfs_root *root,
523 struct extent_buffer *buf,526 struct extent_buffer *buf,
524 struct extent_buffer *parent, int parent_slot,527 struct extent_buffer *parent, int parent_slot,
525 struct extent_buffer **cow_ret,528 struct extent_buffer **cow_ret,
526 u64 search_start, u64 empty_size,529 u64 search_start, u64 empty_size,
527 enum btrfs_lock_nesting nest)530 enum btrfs_lock_nesting nest)
528{531{
529 struct btrfs_fs_info *fs_info = root->fs_info;532 struct btrfs_fs_info *fs_info = root->fs_info;
530 struct btrfs_disk_key disk_key;533 struct btrfs_disk_key disk_key;
@@ -533,6 +536,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 2 unmodified lines
533 int last_ref = 0;536 int last_ref = 0;
534 int unlock_orig = 0;537 int unlock_orig = 0;
535 u64 parent_start = 0;538 u64 parent_start = 0;
539 u64 reloc_src_root = 0;
536 540
537 if (*cow_ret == buf)541 if (*cow_ret == buf)
538 unlock_orig = 1;542 unlock_orig = 1;
@@ -551,12 +555,14 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 12 unmodified lines
551 else555 else
552 btrfs_node_key(buf, &disk_key, 0);556 btrfs_node_key(buf, &disk_key, 0);
553 557
554 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)558 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
555 parent_start = parent->start;559 if (parent)
556 560 parent_start = parent->start;
561 reloc_src_root = btrfs_header_owner(buf);
562 }
557 cow = btrfs_alloc_tree_block(trans, root, parent_start,563 cow = btrfs_alloc_tree_block(trans, root, parent_start,
558 root->root_key.objectid, &disk_key, level,564 root->root_key.objectid, &disk_key, level,
559 search_start, empty_size, nest);565 search_start, empty_size, reloc_src_root, nest);
560 if (IS_ERR(cow))566 if (IS_ERR(cow))
561 return PTR_ERR(cow);567 return PTR_ERR(cow);
562 568
@@ -627,7 +633,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 64 unmodified lines
627 cow->start);633 cow->start);
628 btrfs_set_node_ptr_generation(parent, parent_slot,634 btrfs_set_node_ptr_generation(parent, parent_slot,
629 trans->transid);635 trans->transid);
630 btrfs_mark_buffer_dirty(parent);636 btrfs_mark_buffer_dirty(trans, parent);
631 if (last_ref) {637 if (last_ref) {
632 ret = btrfs_tree_mod_log_free_eb(buf);638 ret = btrfs_tree_mod_log_free_eb(buf);
633 if (ret) {639 if (ret) {
@@ -643,7 +649,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, · 9 unmodified lines
643 if (unlock_orig)649 if (unlock_orig)
644 btrfs_tree_unlock(buf);650 btrfs_tree_unlock(buf);
645 free_extent_buffer_stale(buf);651 free_extent_buffer_stale(buf);
646 btrfs_mark_buffer_dirty(cow);652 btrfs_mark_buffer_dirty(trans, cow);
647 *cow_ret = cow;653 *cow_ret = cow;
648 return 0;654 return 0;
649}655}
@@ -679,11 +685,11 @@ static inline int should_cow_block(struct btrfs_trans_handle *trans, · 29 unmodified lines
679}685}
680 686
681/*687/*
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.
683 * This version of it has extra checks so that a block isn't COWed more than689 * This version of it has extra checks so that a block isn't COWed more than
684 * once per transaction, as long as it hasn't been written yet690 * once per transaction, as long as it hasn't been written yet
685 */691 */
686noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,692int btrfs_cow_block(struct btrfs_trans_handle *trans,
687 struct btrfs_root *root, struct extent_buffer *buf,693 struct btrfs_root *root, struct extent_buffer *buf,
688 struct extent_buffer *parent, int parent_slot,694 struct extent_buffer *parent, int parent_slot,
689 struct extent_buffer **cow_ret,695 struct extent_buffer **cow_ret,
@@ -723,7 +729,7 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, · 33 unmodified lines
723 return 0;729 return 0;
724 }730 }
725 731
726 search_start = buf->start & ~((u64)SZ_1G - 1);732 search_start = round_down(buf->start, SZ_1G);
727 733
728 /*734 /*
729 * Before CoWing this block for later modification, check if it's735 * 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
732 * Also We don't care about the error, as it's handled internally.738 * Also We don't care about the error, as it's handled internally.
733 */739 */
734 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);740 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
735 ret = __btrfs_cow_block(trans, root, buf, parent,741 ret = btrfs_force_cow_block(trans, root, buf, parent, parent_slot,
736 parent_slot, cow_ret, search_start, 0, nest);742 cow_ret, search_start, 0, nest);
737 743
738 trace_btrfs_cow_block(root, buf, *cow_ret);744 trace_btrfs_cow_block(root, buf, *cow_ret);
739 745
@@ -741,49 +747,6 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, · 1 unmodified line
741}747}
742ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);748ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);
743 749
744/*
745 * helper function for defrag to decide if two blocks pointed to by a
746 * node are actually close by
747 */
748static 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 */
763static 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 */
776static 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
787/*750/*
788 * same as comp_keys only with two btrfs_key's751 * same as comp_keys only with two btrfs_key's
789 */752 */
@@ -804,105 +767,6 @@ int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_ke · 14 unmodified lines
804 return 0;767 return 0;
805}768}
806 769
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 */
812int 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
906/*770/*
907 * Search for a key in the given extent_buffer.771 * Search for a key in the given extent_buffer.
908 *772 *
@@ -969,7 +833,7 @@ int btrfs_bin_search(struct extent_buffer *eb, int first_slot, · 60 unmodified lines
969 tmp = &unaligned;833 tmp = &unaligned;
970 }834 }
971 835
972 ret = comp_keys(tmp, key);836 ret = btrfs_comp_keys(tmp, key);
973 837
974 if (ret < 0)838 if (ret < 0)
975 low = mid + 1;839 low = mid + 1;
@@ -984,19 +848,19 @@ int btrfs_bin_search(struct extent_buffer *eb, int first_slot, · 8 unmodified lines
984 return 1;848 return 1;
985}849}
986 850
987static void root_add_used(struct btrfs_root *root, u32 size)851static void root_add_used_bytes(struct btrfs_root *root)
988{852{
989 spin_lock(&root->accounting_lock);853 spin_lock(&root->accounting_lock);
990 btrfs_set_root_used(&root->root_item,854 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);
992 spin_unlock(&root->accounting_lock);856 spin_unlock(&root->accounting_lock);
993}857}
994 858
995static void root_sub_used(struct btrfs_root *root, u32 size)859static void root_sub_used_bytes(struct btrfs_root *root)
996{860{
997 spin_lock(&root->accounting_lock);861 spin_lock(&root->accounting_lock);
998 btrfs_set_root_used(&root->root_item,862 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);
1000 spin_unlock(&root->accounting_lock);864 spin_unlock(&root->accounting_lock);
1001}865}
1002 866
@@ -1112,7 +976,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 109 unmodified lines
1112 /* once for the path */976 /* once for the path */
1113 free_extent_buffer(mid);977 free_extent_buffer(mid);
1114 978
1115 root_sub_used(root, mid->len);979 root_sub_used_bytes(root);
1116 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);980 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
1117 /* once for the root ptr */981 /* once for the root ptr */
1118 free_extent_buffer_stale(mid);982 free_extent_buffer_stale(mid);
@@ -1182,7 +1046,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 63 unmodified lines
1182 right = NULL;1046 right = NULL;
1183 goto out;1047 goto out;
1184 }1048 }
1185 root_sub_used(root, right->len);1049 root_sub_used_bytes(root);
1186 btrfs_free_tree_block(trans, btrfs_root_id(root), right,1050 btrfs_free_tree_block(trans, btrfs_root_id(root), right,
1187 0, 1);1051 0, 1);
1188 free_extent_buffer_stale(right);1052 free_extent_buffer_stale(right);
@@ -1197,7 +1061,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 8 unmodified lines
1197 goto out;1061 goto out;
1198 }1062 }
1199 btrfs_set_node_key(parent, &right_key, pslot + 1);1063 btrfs_set_node_key(parent, &right_key, pslot + 1);
1200 btrfs_mark_buffer_dirty(parent);1064 btrfs_mark_buffer_dirty(trans, parent);
1201 }1065 }
1202 }1066 }
1203 if (btrfs_header_nritems(mid) == 1) {1067 if (btrfs_header_nritems(mid) == 1) {
@@ -1240,7 +1104,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 36 unmodified lines
1240 mid = NULL;1104 mid = NULL;
1241 goto out;1105 goto out;
1242 }1106 }
1243 root_sub_used(root, mid->len);1107 root_sub_used_bytes(root);
1244 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);1108 btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
1245 free_extent_buffer_stale(mid);1109 free_extent_buffer_stale(mid);
1246 mid = NULL;1110 mid = NULL;
@@ -1255,7 +1119,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, · 8 unmodified lines
1255 goto out;1119 goto out;
1256 }1120 }
1257 btrfs_set_node_key(parent, &mid_key, pslot);1121 btrfs_set_node_key(parent, &mid_key, pslot);
1258 btrfs_mark_buffer_dirty(parent);1122 btrfs_mark_buffer_dirty(trans, parent);
1259 }1123 }
1260 1124
1261 /* update the path */1125 /* update the path */
@@ -1362,7 +1226,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, · 100 unmodified lines
1362 return ret;1226 return ret;
1363 }1227 }
1364 btrfs_set_node_key(parent, &disk_key, pslot);1228 btrfs_set_node_key(parent, &disk_key, pslot);
1365 btrfs_mark_buffer_dirty(parent);1229 btrfs_mark_buffer_dirty(trans, parent);
1366 if (btrfs_header_nritems(left) > orig_slot) {1230 if (btrfs_header_nritems(left) > orig_slot) {
1367 path->nodes[level] = left;1231 path->nodes[level] = left;
1368 path->slots[level + 1] -= 1;1232 path->slots[level + 1] -= 1;
@@ -1422,7 +1286,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, · 53 unmodified lines
1422 return ret;1286 return ret;
1423 }1287 }
1424 btrfs_set_node_key(parent, &disk_key, pslot + 1);1288 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1425 btrfs_mark_buffer_dirty(parent);1289 btrfs_mark_buffer_dirty(trans, parent);
1426 1290
1427 if (btrfs_header_nritems(mid) <= orig_slot) {1291 if (btrfs_header_nritems(mid) <= orig_slot) {
1428 path->nodes[level] = right;1292 path->nodes[level] = right;
@@ -2006,7 +1870,7 @@ static int search_leaf(struct btrfs_trans_handle *trans, · 577 unmodified lines
2006 * the extent buffer's header and we have recently accessed1870 * the extent buffer's header and we have recently accessed
2007 * the header's level field.1871 * the header's level field.
2008 */1872 */
2009 ret = comp_keys(&first_key, key);1873 ret = btrfs_comp_keys(&first_key, key);
2010 if (ret < 0) {1874 if (ret < 0) {
2011 /*1875 /*
2012 * The first key is smaller than the key we want1876 * 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
2091}1955}
2092 1956
2093/*1957/*
2094 * btrfs_search_slot - look for a key in a tree and perform necessary1958 * Look for a key in a tree and perform necessary modifications to preserve
2095 * modifications to preserve tree invariants.1959 * tree invariants.
2096 *1960 *
2097 * @trans: Handle of transaction, used when modifying the tree1961 * @trans: Handle of transaction, used when modifying the tree
2098 * @p: Holds all btree nodes along the search path1962 * @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
2515 */2379 */
2516 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {2380 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
2517 btrfs_item_key(path->nodes[0], &found_key, path->slots[0]);2381 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);
2519 if (ret == 0) {2383 if (ret == 0) {
2520 if (path->slots[0] > 0) {2384 if (path->slots[0] > 0) {
2521 path->slots[0]--;2385 path->slots[0]--;
@@ -2530,7 +2394,7 @@ static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) · 8 unmodified lines
2530 }2394 }
2531 2395
2532 btrfs_item_key(path->nodes[0], &found_key, 0);2396 btrfs_item_key(path->nodes[0], &found_key, 0);
2533 ret = comp_keys(&found_key, &key);2397 ret = btrfs_comp_keys(&found_key, &key);
2534 /*2398 /*
2535 * We might have had an item with the previous key in the tree right2399 * We might have had an item with the previous key in the tree right
2536 * before we released our path. And after we released our path, that2400 * 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
2678 * higher levels2542 * higher levels
2679 *2543 *
2680 */2544 */
2681static void fixup_low_keys(struct btrfs_path *path,2545static void fixup_low_keys(struct btrfs_trans_handle *trans,
2546 struct btrfs_path *path,
2682 struct btrfs_disk_key *key, int level)2547 struct btrfs_disk_key *key, int level)
2683{2548{
2684 int i;2549 int i;
@@ -2695,7 +2560,7 @@ static void fixup_low_keys(struct btrfs_path *path, · 10 unmodified lines
2695 BTRFS_MOD_LOG_KEY_REPLACE);2560 BTRFS_MOD_LOG_KEY_REPLACE);
2696 BUG_ON(ret < 0);2561 BUG_ON(ret < 0);
2697 btrfs_set_node_key(t, key, tslot);2562 btrfs_set_node_key(t, key, tslot);
2698 btrfs_mark_buffer_dirty(path->nodes[i]);2563 btrfs_mark_buffer_dirty(trans, path->nodes[i]);
2699 if (tslot != 0)2564 if (tslot != 0)
2700 break;2565 break;
2701 }2566 }
@@ -2707,10 +2572,11 @@ static void fixup_low_keys(struct btrfs_path *path, · 5 unmodified lines
2707 * This function isn't completely safe. It's the caller's responsibility2572 * This function isn't completely safe. It's the caller's responsibility
2708 * that the new key won't break the order2573 * that the new key won't break the order
2709 */2574 */
2710void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,2575void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2711 struct btrfs_path *path,2576 struct btrfs_path *path,
2712 const struct btrfs_key *new_key)2577 const struct btrfs_key *new_key)
2713{2578{
2579 struct btrfs_fs_info *fs_info = trans->fs_info;
2714 struct btrfs_disk_key disk_key;2580 struct btrfs_disk_key disk_key;
2715 struct extent_buffer *eb;2581 struct extent_buffer *eb;
2716 int slot;2582 int slot;
@@ -2719,7 +2585,7 @@ void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, · 2 unmodified lines
2719 slot = path->slots[0];2585 slot = path->slots[0];
2720 if (slot > 0) {2586 if (slot > 0) {
2721 btrfs_item_key(eb, &disk_key, slot - 1);2587 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)) {
2723 btrfs_print_leaf(eb);2589 btrfs_print_leaf(eb);
2724 btrfs_crit(fs_info,2590 btrfs_crit(fs_info,
2725 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",2591 "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
2733 }2599 }
2734 if (slot < btrfs_header_nritems(eb) - 1) {2600 if (slot < btrfs_header_nritems(eb) - 1) {
2735 btrfs_item_key(eb, &disk_key, slot + 1);2601 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)) {
2737 btrfs_print_leaf(eb);2603 btrfs_print_leaf(eb);
2738 btrfs_crit(fs_info,2604 btrfs_crit(fs_info,
2739 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",2605 "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
2748 2614
2749 btrfs_cpu_key_to_disk(&disk_key, new_key);2615 btrfs_cpu_key_to_disk(&disk_key, new_key);
2750 btrfs_set_item_key(eb, &disk_key, slot);2616 btrfs_set_item_key(eb, &disk_key, slot);
2751 btrfs_mark_buffer_dirty(eb);2617 btrfs_mark_buffer_dirty(trans, eb);
2752 if (slot == 0)2618 if (slot == 0)
2753 fixup_low_keys(path, &disk_key, 1);2619 fixup_low_keys(trans, path, &disk_key, 1);
2754}2620}
2755 2621
2756/*2622/*
@@ -2881,8 +2747,8 @@ static int push_node_left(struct btrfs_trans_handle *trans, · 124 unmodified lines
2881 }2747 }
2882 btrfs_set_header_nritems(src, src_nritems - push_items);2748 btrfs_set_header_nritems(src, src_nritems - push_items);
2883 btrfs_set_header_nritems(dst, dst_nritems + push_items);2749 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2884 btrfs_mark_buffer_dirty(src);2750 btrfs_mark_buffer_dirty(trans, src);
2885 btrfs_mark_buffer_dirty(dst);2751 btrfs_mark_buffer_dirty(trans, dst);
2886 2752
2887 return ret;2753 return ret;
2888}2754}
@@ -2957,8 +2823,8 @@ static int balance_node_right(struct btrfs_trans_handle *trans, · 68 unmodified lines
2957 btrfs_set_header_nritems(src, src_nritems - push_items);2823 btrfs_set_header_nritems(src, src_nritems - push_items);
2958 btrfs_set_header_nritems(dst, dst_nritems + push_items);2824 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2959 2825
2960 btrfs_mark_buffer_dirty(src);2826 btrfs_mark_buffer_dirty(trans, src);
2961 btrfs_mark_buffer_dirty(dst);2827 btrfs_mark_buffer_dirty(trans, dst);
2962 2828
2963 return ret;2829 return ret;
2964}2830}
@@ -2974,7 +2840,6 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans, · 9 unmodified lines
2974 struct btrfs_root *root,2840 struct btrfs_root *root,
2975 struct btrfs_path *path, int level)2841 struct btrfs_path *path, int level)
2976{2842{
2977 struct btrfs_fs_info *fs_info = root->fs_info;
2978 u64 lower_gen;2843 u64 lower_gen;
2979 struct extent_buffer *lower;2844 struct extent_buffer *lower;
2980 struct extent_buffer *c;2845 struct extent_buffer *c;
@@ -2993,11 +2858,11 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans, · 12 unmodified lines
2993 2858
2994 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,2859 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
2995 &lower_key, level, root->node->start, 0,2860 &lower_key, level, root->node->start, 0,
2996 BTRFS_NESTING_NEW_ROOT);2861 0, BTRFS_NESTING_NEW_ROOT);
2997 if (IS_ERR(c))2862 if (IS_ERR(c))
2998 return PTR_ERR(c);2863 return PTR_ERR(c);
2999 2864
3000 root_add_used(root, fs_info->nodesize);2865 root_add_used_bytes(root);
3001 2866
3002 btrfs_set_header_nritems(c, 1);2867 btrfs_set_header_nritems(c, 1);
3003 btrfs_set_node_key(c, &lower_key, 0);2868 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
3007 2872
3008 btrfs_set_node_ptr_generation(c, 0, lower_gen);2873 btrfs_set_node_ptr_generation(c, 0, lower_gen);
3009 2874
3010 btrfs_mark_buffer_dirty(c);2875 btrfs_mark_buffer_dirty(trans, c);
3011 2876
3012 old = root->node;2877 old = root->node;
3013 ret = btrfs_tree_mod_log_insert_root(root->node, c, false);2878 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
3079 WARN_ON(trans->transid == 0);2944 WARN_ON(trans->transid == 0);
3080 btrfs_set_node_ptr_generation(lower, slot, trans->transid);2945 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3081 btrfs_set_header_nritems(lower, nritems + 1);2946 btrfs_set_header_nritems(lower, nritems + 1);
3082 btrfs_mark_buffer_dirty(lower);2947 btrfs_mark_buffer_dirty(trans, lower);
3083 2948
3084 return 0;2949 return 0;
3085}2950}
@@ -3137,11 +3002,11 @@ static noinline int split_node(struct btrfs_trans_handle *trans, · 51 unmodified lines
3137 3002
3138 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,3003 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3139 &disk_key, level, c->start, 0,3004 &disk_key, level, c->start, 0,
3140 BTRFS_NESTING_SPLIT);3005 0, BTRFS_NESTING_SPLIT);
3141 if (IS_ERR(split))3006 if (IS_ERR(split))
3142 return PTR_ERR(split);3007 return PTR_ERR(split);
3143 3008
3144 root_add_used(root, fs_info->nodesize);3009 root_add_used_bytes(root);
3145 ASSERT(btrfs_header_level(c) == level);3010 ASSERT(btrfs_header_level(c) == level);
3146 3011
3147 ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);3012 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
3158 btrfs_set_header_nritems(split, c_nritems - mid);3023 btrfs_set_header_nritems(split, c_nritems - mid);
3159 btrfs_set_header_nritems(c, mid);3024 btrfs_set_header_nritems(c, mid);
3160 3025
3161 btrfs_mark_buffer_dirty(c);3026 btrfs_mark_buffer_dirty(trans, c);
3162 btrfs_mark_buffer_dirty(split);3027 btrfs_mark_buffer_dirty(trans, split);
3163 3028
3164 ret = insert_ptr(trans, path, &disk_key, split->start,3029 ret = insert_ptr(trans, path, &disk_key, split->start,
3165 path->slots[level + 1] + 1, level + 1);3030 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
3325 btrfs_set_header_nritems(left, left_nritems);3190 btrfs_set_header_nritems(left, left_nritems);
3326 3191
3327 if (left_nritems)3192 if (left_nritems)
3328 btrfs_mark_buffer_dirty(left);3193 btrfs_mark_buffer_dirty(trans, left);
3329 else3194 else
3330 btrfs_clear_buffer_dirty(trans, left);3195 btrfs_clear_buffer_dirty(trans, left);
3331 3196
3332 btrfs_mark_buffer_dirty(right);3197 btrfs_mark_buffer_dirty(trans, right);
3333 3198
3334 btrfs_item_key(right, &disk_key, 0);3199 btrfs_item_key(right, &disk_key, 0);
3335 btrfs_set_node_key(upper, &disk_key, slot + 1);3200 btrfs_set_node_key(upper, &disk_key, slot + 1);
3336 btrfs_mark_buffer_dirty(upper);3201 btrfs_mark_buffer_dirty(trans, upper);
3337 3202
3338 /* then fixup the leaf pointer in the path */3203 /* then fixup the leaf pointer in the path */
3339 if (path->slots[0] >= left_nritems) {3204 if (path->slots[0] >= left_nritems) {
@@ -3545,14 +3410,14 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, · 205 unmodified lines
3545 btrfs_set_token_item_offset(&token, i, push_space);3410 btrfs_set_token_item_offset(&token, i, push_space);
3546 }3411 }
3547 3412
3548 btrfs_mark_buffer_dirty(left);3413 btrfs_mark_buffer_dirty(trans, left);
3549 if (right_nritems)3414 if (right_nritems)
3550 btrfs_mark_buffer_dirty(right);3415 btrfs_mark_buffer_dirty(trans, right);
3551 else3416 else
3552 btrfs_clear_buffer_dirty(trans, right);3417 btrfs_clear_buffer_dirty(trans, right);
3553 3418
3554 btrfs_item_key(right, &disk_key, 0);3419 btrfs_item_key(right, &disk_key, 0);
3555 fixup_low_keys(path, &disk_key, 1);3420 fixup_low_keys(trans, path, &disk_key, 1);
3556 3421
3557 /* then fixup the leaf pointer in the path */3422 /* then fixup the leaf pointer in the path */
3558 if (path->slots[0] < push_items) {3423 if (path->slots[0] < push_items) {
@@ -3683,8 +3548,8 @@ static noinline int copy_for_split(struct btrfs_trans_handle *trans, · 124 unmodified lines
3683 if (ret < 0)3548 if (ret < 0)
3684 return ret;3549 return ret;
3685 3550
3686 btrfs_mark_buffer_dirty(right);3551 btrfs_mark_buffer_dirty(trans, right);
3687 btrfs_mark_buffer_dirty(l);3552 btrfs_mark_buffer_dirty(trans, l);
3688 BUG_ON(path->slots[0] != slot);3553 BUG_ON(path->slots[0] != slot);
3689 3554
3690 if (mid <= slot) {3555 if (mid <= slot) {
@@ -3888,13 +3753,13 @@ again: · 197 unmodified lines
3888 * use BTRFS_NESTING_NEW_ROOT.3753 * use BTRFS_NESTING_NEW_ROOT.
3889 */3754 */
3890 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,3755 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,
3892 num_doubles ? BTRFS_NESTING_NEW_ROOT :3757 num_doubles ? BTRFS_NESTING_NEW_ROOT :
3893 BTRFS_NESTING_SPLIT);3758 BTRFS_NESTING_SPLIT);
3894 if (IS_ERR(right))3759 if (IS_ERR(right))
3895 return PTR_ERR(right);3760 return PTR_ERR(right);
3896 3761
3897 root_add_used(root, fs_info->nodesize);3762 root_add_used_bytes(root);
3898 3763
3899 if (split == 0) {3764 if (split == 0) {
3900 if (mid <= slot) {3765 if (mid <= slot) {
@@ -3925,7 +3790,7 @@ again: · 24 unmodified lines
3925 path->nodes[0] = right;3790 path->nodes[0] = right;
3926 path->slots[0] = 0;3791 path->slots[0] = 0;
3927 if (path->slots[1] == 0)3792 if (path->slots[1] == 0)
3928 fixup_low_keys(path, &disk_key, 1);3793 fixup_low_keys(trans, path, &disk_key, 1);
3929 }3794 }
3930 /*3795 /*
3931 * We create a new leaf 'right' for the required ins_len and3796 * We create a new leaf 'right' for the required ins_len and
@@ -4024,7 +3889,8 @@ err: · 92 unmodified lines
4024 return ret;3889 return ret;
4025}3890}
4026 3891
4027static noinline int split_item(struct btrfs_path *path,3892static noinline int split_item(struct btrfs_trans_handle *trans,
3893 struct btrfs_path *path,
4028 const struct btrfs_key *new_key,3894 const struct btrfs_key *new_key,
4029 unsigned long split_offset)3895 unsigned long split_offset)
4030{3896{
@@ -4083,7 +3949,7 @@ static noinline int split_item(struct btrfs_path *path, · 52 unmodified lines
4083 write_extent_buffer(leaf, buf + split_offset,3949 write_extent_buffer(leaf, buf + split_offset,
4084 btrfs_item_ptr_offset(leaf, slot),3950 btrfs_item_ptr_offset(leaf, slot),
4085 item_size - split_offset);3951 item_size - split_offset);
4086 btrfs_mark_buffer_dirty(leaf);3952 btrfs_mark_buffer_dirty(trans, leaf);
4087 3953
4088 BUG_ON(btrfs_leaf_free_space(leaf) < 0);3954 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
4089 kfree(buf);3955 kfree(buf);
@@ -4117,7 +3983,7 @@ int btrfs_split_item(struct btrfs_trans_handle *trans, · 27 unmodified lines
4117 if (ret)3983 if (ret)
4118 return ret;3984 return ret;
4119 3985
4120 ret = split_item(path, new_key, split_offset);3986 ret = split_item(trans, path, new_key, split_offset);
4121 return ret;3987 return ret;
4122}3988}
4123 3989
@@ -4127,7 +3993,8 @@ int btrfs_split_item(struct btrfs_trans_handle *trans, · 3 unmodified lines
4127 * off the end of the item or if we shift the item to chop bytes off3993 * off the end of the item or if we shift the item to chop bytes off
4128 * the front.3994 * the front.
4129 */3995 */
4130void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)3996void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3997 struct btrfs_path *path, u32 new_size, int from_end)
4131{3998{
4132 int slot;3999 int slot;
4133 struct extent_buffer *leaf;4000 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
4203 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);4070 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4204 btrfs_set_item_key(leaf, &disk_key, slot);4071 btrfs_set_item_key(leaf, &disk_key, slot);
4205 if (slot == 0)4072 if (slot == 0)
4206 fixup_low_keys(path, &disk_key, 1);4073 fixup_low_keys(trans, path, &disk_key, 1);
4207 }4074 }
4208 4075
4209 btrfs_set_item_size(leaf, slot, new_size);4076 btrfs_set_item_size(leaf, slot, new_size);
4210 btrfs_mark_buffer_dirty(leaf);4077 btrfs_mark_buffer_dirty(trans, leaf);
4211 4078
4212 if (btrfs_leaf_free_space(leaf) < 0) {4079 if (btrfs_leaf_free_space(leaf) < 0) {
4213 btrfs_print_leaf(leaf);4080 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
4218/*4085/*
4219 * make the item pointed to by the path bigger, data_size is the added size.4086 * make the item pointed to by the path bigger, data_size is the added size.
4220 */4087 */
4221void btrfs_extend_item(struct btrfs_path *path, u32 data_size)4088void btrfs_extend_item(struct btrfs_trans_handle *trans,
4089 struct btrfs_path *path, u32 data_size)
4222{4090{
4223 int slot;4091 int slot;
4224 struct extent_buffer *leaf;4092 struct extent_buffer *leaf;
@@ -4268,7 +4136,7 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size) · 43 unmodified lines
4268 data_end = old_data;4136 data_end = old_data;
4269 old_size = btrfs_item_size(leaf, slot);4137 old_size = btrfs_item_size(leaf, slot);
4270 btrfs_set_item_size(leaf, slot, old_size + data_size);4138 btrfs_set_item_size(leaf, slot, old_size + data_size);
4271 btrfs_mark_buffer_dirty(leaf);4139 btrfs_mark_buffer_dirty(trans, leaf);
4272 4140
4273 if (btrfs_leaf_free_space(leaf) < 0) {4141 if (btrfs_leaf_free_space(leaf) < 0) {
4274 btrfs_print_leaf(leaf);4142 btrfs_print_leaf(leaf);
@@ -4279,6 +4147,7 @@ void btrfs_extend_item(struct btrfs_path *path, u32 data_size) · 4 unmodified lines
4279/*4147/*
4280 * Make space in the node before inserting one or more items.4148 * Make space in the node before inserting one or more items.
4281 *4149 *
4150 * @trans: transaction handle
4282 * @root: root we are inserting items to4151 * @root: root we are inserting items to
4283 * @path: points to the leaf/slot where we are going to insert new items4152 * @path: points to the leaf/slot where we are going to insert new items
4284 * @batch: information about the batch of items to insert4153 * @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
4286 * Main purpose is to save stack depth by doing the bulk of the work in a4155 * Main purpose is to save stack depth by doing the bulk of the work in a
4287 * function that doesn't call btrfs_search_slot4156 * function that doesn't call btrfs_search_slot
4288 */4157 */
4289static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,4158static void setup_items_for_insert(struct btrfs_trans_handle *trans,
4159 struct btrfs_root *root, struct btrfs_path *path,
4290 const struct btrfs_item_batch *batch)4160 const struct btrfs_item_batch *batch)
4291{4161{
4292 struct btrfs_fs_info *fs_info = root->fs_info;4162 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
4306 */4176 */
4307 if (path->slots[0] == 0) {4177 if (path->slots[0] == 0) {
4308 btrfs_cpu_key_to_disk(&disk_key, &batch->keys[0]);4178 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);
4310 }4180 }
4311 btrfs_unlock_up_safe(path, 1);4181 btrfs_unlock_up_safe(path, 1);
4312 4182
@@ -4365,7 +4235,7 @@ static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *p · 52 unmodified lines
4365 }4235 }
4366 4236
4367 btrfs_set_header_nritems(leaf, nritems + batch->nr);4237 btrfs_set_header_nritems(leaf, nritems + batch->nr);
4368 btrfs_mark_buffer_dirty(leaf);4238 btrfs_mark_buffer_dirty(trans, leaf);
4369 4239
4370 if (btrfs_leaf_free_space(leaf) < 0) {4240 if (btrfs_leaf_free_space(leaf) < 0) {
4371 btrfs_print_leaf(leaf);4241 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
4376/*4246/*
4377 * Insert a new item into a leaf.4247 * Insert a new item into a leaf.
4378 *4248 *
4249 * @trans: Transaction handle.
4379 * @root: The root of the btree.4250 * @root: The root of the btree.
4380 * @path: A path pointing to the target leaf and slot.4251 * @path: A path pointing to the target leaf and slot.
4381 * @key: The key of the new item.4252 * @key: The key of the new item.
4382 * @data_size: The size of the data associated with the new key.4253 * @data_size: The size of the data associated with the new key.
4383 */4254 */
4384void btrfs_setup_item_for_insert(struct btrfs_root *root,4255void btrfs_setup_item_for_insert(struct btrfs_trans_handle *trans,
4256 struct btrfs_root *root,
4385 struct btrfs_path *path,4257 struct btrfs_path *path,
4386 const struct btrfs_key *key,4258 const struct btrfs_key *key,
4387 u32 data_size)4259 u32 data_size)
@@ -4393,7 +4265,7 @@ void btrfs_setup_item_for_insert(struct btrfs_root *root, · 5 unmodified lines
4393 batch.total_data_size = data_size;4265 batch.total_data_size = data_size;
4394 batch.nr = 1;4266 batch.nr = 1;
4395 4267
4396 setup_items_for_insert(root, path, &batch);4268 setup_items_for_insert(trans, root, path, &batch);
4397}4269}
4398 4270
4399/*4271/*
@@ -4419,7 +4291,7 @@ int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, · 19 unmodified lines
4419 slot = path->slots[0];4291 slot = path->slots[0];
4420 BUG_ON(slot < 0);4292 BUG_ON(slot < 0);
4421 4293
4422 setup_items_for_insert(root, path, batch);4294 setup_items_for_insert(trans, root, path, batch);
4423 return 0;4295 return 0;
4424}4296}
4425 4297
@@ -4444,7 +4316,7 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 18 unmodified lines
4444 leaf = path->nodes[0];4316 leaf = path->nodes[0];
4445 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);4317 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4446 write_extent_buffer(leaf, data, ptr, data_size);4318 write_extent_buffer(leaf, data, ptr, data_size);
4447 btrfs_mark_buffer_dirty(leaf);4319 btrfs_mark_buffer_dirty(trans, leaf);
4448 }4320 }
4449 btrfs_free_path(path);4321 btrfs_free_path(path);
4450 return ret;4322 return ret;
@@ -4475,7 +4347,7 @@ int btrfs_duplicate_item(struct btrfs_trans_handle *trans, · 24 unmodified lines
4475 return ret;4347 return ret;
4476 4348
4477 path->slots[0]++;4349 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);
4479 leaf = path->nodes[0];4351 leaf = path->nodes[0];
4480 memcpy_extent_buffer(leaf,4352 memcpy_extent_buffer(leaf,
4481 btrfs_item_ptr_offset(leaf, path->slots[0]),4353 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
4533 struct btrfs_disk_key disk_key;4405 struct btrfs_disk_key disk_key;
4534 4406
4535 btrfs_node_key(parent, &disk_key, 0);4407 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);
4537 }4409 }
4538 btrfs_mark_buffer_dirty(parent);4410 btrfs_mark_buffer_dirty(trans, parent);
4539 return 0;4411 return 0;
4540}4412}
4541 4413
@@ -4567,7 +4439,7 @@ static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans, · 25 unmodified lines
4567 */4439 */
4568 btrfs_unlock_up_safe(path, 0);4440 btrfs_unlock_up_safe(path, 0);
4569 4441
4570 root_sub_used(root, leaf->len);4442 root_sub_used_bytes(root);
4571 4443
4572 atomic_inc(&leaf->refs);4444 atomic_inc(&leaf->refs);
4573 btrfs_free_tree_block(trans, btrfs_root_id(root), leaf, 0, 1);4445 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
4632 struct btrfs_disk_key disk_key;4504 struct btrfs_disk_key disk_key;
4633 4505
4634 btrfs_item_key(leaf, &disk_key, 0);4506 btrfs_item_key(leaf, &disk_key, 0);
4635 fixup_low_keys(path, &disk_key, 1);4507 fixup_low_keys(trans, path, &disk_key, 1);
4636 }4508 }
4637 4509
4638 /*4510 /*
@@ -4697,11 +4569,11 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, · 58 unmodified lines
4697 * dirtied this buffer4569 * dirtied this buffer
4698 */4570 */
4699 if (path->nodes[0] == leaf)4571 if (path->nodes[0] == leaf)
4700 btrfs_mark_buffer_dirty(leaf);4572 btrfs_mark_buffer_dirty(trans, leaf);
4701 free_extent_buffer(leaf);4573 free_extent_buffer(leaf);
4702 }4574 }
4703 } else {4575 } else {
4704 btrfs_mark_buffer_dirty(leaf);4576 btrfs_mark_buffer_dirty(trans, leaf);
4705 }4577 }
4706 }4578 }
4707 return ret;4579 return ret;
fs/btrfs/ctree.h+81−61
@@ -6,37 +6,10 @@ · 5 unmodified lines
6#ifndef BTRFS_CTREE_H6#ifndef BTRFS_CTREE_H
7#define BTRFS_CTREE_H7#define BTRFS_CTREE_H
8 8
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>
21#include <linux/pagemap.h>9#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"
37#include "locking.h"10#include "locking.h"
38#include "misc.h"
39#include "fs.h"11#include "fs.h"
12#include "accessors.h"
40 13
41struct btrfs_trans_handle;14struct btrfs_trans_handle;
42struct btrfs_transaction;15struct btrfs_transaction;
@@ -218,10 +191,22 @@ struct btrfs_root { · 175 unmodified lines
218 atomic_t log_commit[2];191 atomic_t log_commit[2];
219 /* Used only for log trees of subvolumes, not for the log root tree */192 /* Used only for log trees of subvolumes, not for the log root tree */
220 atomic_t log_batch;193 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 */
221 int log_transid;202 int log_transid;
222 /* No matter the commit succeeds or not*/203 /* No matter the commit succeeds or not*/
223 int log_transid_committed;204 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 */
225 int last_log_commit;210 int last_log_commit;
226 pid_t log_start_pid;211 pid_t log_start_pid;
227 212
@@ -326,6 +311,9 @@ struct btrfs_root { · 98 unmodified lines
326 /* Used only by log trees, when logging csum items */311 /* Used only by log trees, when logging csum items */
327 struct extent_io_tree log_csum_range;312 struct extent_io_tree log_csum_range;
328 313
314 /* Used in simple quotas, track root during relocation. */
315 u64 relocation_src_root;
316
329#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS317#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
330 u64 alloc_bytenr;318 u64 alloc_bytenr;
331#endif319#endif
@@ -352,6 +340,26 @@ static inline u64 btrfs_root_id(const struct btrfs_root *root) · 20 unmodified lines
352 return root->root_key.objectid;340 return root->root_key.objectid;
353}341}
354 342
343static inline int btrfs_get_root_log_transid(const struct btrfs_root *root)
344{
345 return READ_ONCE(root->log_transid);
346}
347
348static 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
353static inline int btrfs_get_root_last_log_commit(const struct btrfs_root *root)
354{
355 return READ_ONCE(root->last_log_commit);
356}
357
358static 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
355/*363/*
356 * Structure that conveys information about an extent that is going to replace364 * Structure that conveys information about an extent that is going to replace
357 * all the extents in a file range.365 * 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
470#define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \478#define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
471 ((bytes) >> (fs_info)->sectorsize_bits)479 ((bytes) >> (fs_info)->sectorsize_bits)
472 480
473static inline u32 btrfs_crc32c(u32 crc, const void *address, unsigned length)
474{
475 return crc32c(crc, address, length);
476}
477
478static inline void btrfs_crc32c_final(u32 crc, u8 *result)
479{
480 put_unaligned_le32(~crc, result);
481}
482
483static 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 */
491static 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
497static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)481static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
498{482{
499 return mapping_gfp_constraint(mapping, ~__GFP_FS);483 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
513 const struct btrfs_key *key, int *slot);497 const struct btrfs_key *key, int *slot);
514 498
515int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);499int __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 */
507static 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. */
518static 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
516int btrfs_previous_item(struct btrfs_root *root,530int btrfs_previous_item(struct btrfs_root *root,
517 struct btrfs_path *path, u64 min_objectid,531 struct btrfs_path *path, u64 min_objectid,
518 int type);532 int type);
519int btrfs_previous_extent_item(struct btrfs_root *root,533int btrfs_previous_extent_item(struct btrfs_root *root,
520 struct btrfs_path *path, u64 min_objectid);534 struct btrfs_path *path, u64 min_objectid);
521void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,535void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
522 struct btrfs_path *path,536 struct btrfs_path *path,
523 const struct btrfs_key *new_key);537 const struct btrfs_key *new_key);
524struct extent_buffer *btrfs_root_node(struct btrfs_root *root);538struct 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
536 struct extent_buffer *parent, int parent_slot,550 struct extent_buffer *parent, int parent_slot,
537 struct extent_buffer **cow_ret,551 struct extent_buffer **cow_ret,
538 enum btrfs_lock_nesting nest);552 enum btrfs_lock_nesting nest);
553int 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);
539int btrfs_copy_root(struct btrfs_trans_handle *trans,560int btrfs_copy_root(struct btrfs_trans_handle *trans,
540 struct btrfs_root *root,561 struct btrfs_root *root,
541 struct extent_buffer *buf,562 struct extent_buffer *buf,
@@ -545,8 +566,10 @@ int btrfs_block_can_be_shared(struct btrfs_trans_handle *trans, · 3 unmodified lines
545 struct extent_buffer *buf);566 struct extent_buffer *buf);
546int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,567int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
547 struct btrfs_path *path, int level, int slot);568 struct btrfs_path *path, int level, int slot);
548void btrfs_extend_item(struct btrfs_path *path, u32 data_size);569void btrfs_extend_item(struct btrfs_trans_handle *trans,
549void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end);570 struct btrfs_path *path, u32 data_size);
571void btrfs_truncate_item(struct btrfs_trans_handle *trans,
572 struct btrfs_path *path, u32 new_size, int from_end);
550int btrfs_split_item(struct btrfs_trans_handle *trans,573int btrfs_split_item(struct btrfs_trans_handle *trans,
551 struct btrfs_root *root,574 struct btrfs_root *root,
552 struct btrfs_path *path,575 struct btrfs_path *path,
@@ -567,10 +590,6 @@ int btrfs_search_slot_for_read(struct btrfs_root *root, · 14 unmodified lines
567 const struct btrfs_key *key,590 const struct btrfs_key *key,
568 struct btrfs_path *p, int find_higher,591 struct btrfs_path *p, int find_higher,
569 int return_any);592 int return_any);
570int 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);
574void btrfs_release_path(struct btrfs_path *p);593void btrfs_release_path(struct btrfs_path *p);
575struct btrfs_path *btrfs_alloc_path(void);594struct btrfs_path *btrfs_alloc_path(void);
576void btrfs_free_path(struct btrfs_path *p);595void btrfs_free_path(struct btrfs_path *p);
@@ -610,7 +629,8 @@ struct btrfs_item_batch { · 33 unmodified lines
610 int nr;629 int nr;
611};630};
612 631
613void btrfs_setup_item_for_insert(struct btrfs_root *root,632void btrfs_setup_item_for_insert(struct btrfs_trans_handle *trans,
633 struct btrfs_root *root,
614 struct btrfs_path *path,634 struct btrfs_path *path,
615 const struct btrfs_key *key,635 const struct btrfs_key *key,
616 u32 data_size);636 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
337 return 0;337 return 0;
338}338}
339 339
340/*
341 * Check if two blocks addresses are close, used by defrag.
342 */
343static 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 */
356static 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
340/*445/*
341 * Defrag all the leaves in a given btree.446 * Defrag all the leaves in a given btree.
342 * Read all the leaves and try to get key order to447 * Read all the leaves and try to get key order to
343 * better reflect disk order448 * better reflect disk order
344 */449 */
345 450
346int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,451static int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
347 struct btrfs_root *root)452 struct btrfs_root *root)
348{453{
349 struct btrfs_path *path = NULL;454 struct btrfs_path *path = NULL;
350 struct btrfs_key key;455 struct btrfs_key key;
@@ -460,6 +565,45 @@ done: · 109 unmodified lines
460 return ret;565 return ret;
461}566}
462 567
568/*
569 * Defrag a given btree. Every leaf in the btree is read and defragmented.
570 */
571int 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
463/*607/*
464 * Defrag specific helper to get an extent map.608 * Defrag specific helper to get an extent map.
465 *609 *
@@ -891,8 +1035,8 @@ static int defrag_collect_targets(struct btrfs_inode *inode, · 425 unmodified lines
891 * very likely resulting in a larger extent after writeback is1035 * very likely resulting in a larger extent after writeback is
892 * triggered (except in a case of free space fragmentation).1036 * triggered (except in a case of free space fragmentation).
893 */1037 */
894 if (test_range_bit(&inode->io_tree, cur, cur + range_len - 1,1038 if (test_range_bit_exists(&inode->io_tree, cur, cur + range_len - 1,
895 EXTENT_DELALLOC, 0, NULL))1039 EXTENT_DELALLOC))
896 goto next;1040 goto next;
897 1041
898 /*1042 /*
fs/btrfs/defrag.h+1−1
@@ -12,7 +12,7 @@ int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans, · 11 unmodified lines
12 struct btrfs_inode *inode, u32 extent_thresh);12 struct btrfs_inode *inode, u32 extent_thresh);
13int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info);13int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info);
14void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info);14void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info);
15int btrfs_defrag_leaves(struct btrfs_trans_handle *trans, struct btrfs_root *root);15int btrfs_defrag_root(struct btrfs_root *root);
16 16
17static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)17static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
18{18{
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
322 } else {322 } else {
323 if (current->journal_info)323 if (current->journal_info)
324 flush = BTRFS_RESERVE_FLUSH_LIMIT;324 flush = BTRFS_RESERVE_FLUSH_LIMIT;
325
326 if (btrfs_transaction_in_commit(fs_info))
327 schedule_timeout(1);
328 }325 }
329 326
330 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);327 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
346 noflush);343 noflush);
347 if (ret)344 if (ret)
348 return ret;345 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);
350 if (ret) {348 if (ret) {
351 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);349 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
352 return ret;350 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
328}328}
329 329
330/*330/*
331 * __btrfs_lookup_delayed_item - look up the delayed item by key331 * Look up the delayed item by key.
332 *
332 * @delayed_node: pointer to the delayed node333 * @delayed_node: pointer to the delayed node
333 * @index: the dir index value to lookup (offset of a dir index key)334 * @index: the dir index value to lookup (offset of a dir index key)
334 *335 *
@@ -517,7 +518,7 @@ static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans, · 182 unmodified lines
517 /*518 /*
518 * For insertions we track reserved metadata space by accounting519 * For insertions we track reserved metadata space by accounting
519 * for the number of leaves that will be used, based on the delayed520 * 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.
521 */522 */
522 if (item->type == BTRFS_DELAYED_DELETION_ITEM)523 if (item->type == BTRFS_DELAYED_DELETION_ITEM)
523 item->bytes_reserved = num_bytes;524 item->bytes_reserved = num_bytes;
@@ -1030,7 +1031,7 @@ static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, · 506 unmodified lines
1030 struct btrfs_inode_item);1031 struct btrfs_inode_item);
1031 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,1032 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
1032 sizeof(struct btrfs_inode_item));1033 sizeof(struct btrfs_inode_item));
1033 btrfs_mark_buffer_dirty(leaf);1034 btrfs_mark_buffer_dirty(trans, leaf);
1034 1035
1035 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))1036 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1036 goto out;1037 goto out;
@@ -1378,8 +1379,7 @@ static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root, · 341 unmodified lines
1378 return -ENOMEM;1379 return -ENOMEM;
1379 1380
1380 async_work->delayed_root = delayed_root;1381 async_work->delayed_root = delayed_root;
1381 btrfs_init_work(&async_work->work, btrfs_async_run_delayed_root, NULL,1382 btrfs_init_work(&async_work->work, btrfs_async_run_delayed_root, NULL);
1382 NULL);
1383 async_work->nr = nr;1383 async_work->nr = nr;
1384 1384
1385 btrfs_queue_work(fs_info->delayed_workers, &async_work->work);1385 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
1760}1760}
1761 1761
1762/*1762/*
1763 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree1763 * Read dir info stored in the delayed tree.
1764 *
1765 */1764 */
1766int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,1765int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
1767 struct list_head *ins_list)1766 struct list_head *ins_list)
@@ -1848,10 +1847,8 @@ static void fill_stack_inode_item(struct btrfs_trans_handle *trans, · 80 unmodified lines
1848 btrfs_set_stack_timespec_nsec(&inode_item->ctime,1847 btrfs_set_stack_timespec_nsec(&inode_item->ctime,
1849 inode_get_ctime_nsec(inode));1848 inode_get_ctime_nsec(inode));
1850 1849
1851 btrfs_set_stack_timespec_sec(&inode_item->otime,1850 btrfs_set_stack_timespec_sec(&inode_item->otime, BTRFS_I(inode)->i_otime_sec);
1852 BTRFS_I(inode)->i_otime.tv_sec);1851 btrfs_set_stack_timespec_nsec(&inode_item->otime, BTRFS_I(inode)->i_otime_nsec);
1853 btrfs_set_stack_timespec_nsec(&inode_item->otime,
1854 BTRFS_I(inode)->i_otime.tv_nsec);
1855}1852}
1856 1853
1857int btrfs_fill_inode(struct inode *inode, u32 *rdev)1854int btrfs_fill_inode(struct inode *inode, u32 *rdev)
@@ -1900,10 +1897,8 @@ int btrfs_fill_inode(struct inode *inode, u32 *rdev) · 42 unmodified lines
1900 inode_set_ctime(inode, btrfs_stack_timespec_sec(&inode_item->ctime),1897 inode_set_ctime(inode, btrfs_stack_timespec_sec(&inode_item->ctime),
1901 btrfs_stack_timespec_nsec(&inode_item->ctime));1898 btrfs_stack_timespec_nsec(&inode_item->ctime));
1902 1899
1903 BTRFS_I(inode)->i_otime.tv_sec =1900 BTRFS_I(inode)->i_otime_sec = btrfs_stack_timespec_sec(&inode_item->otime);
1904 btrfs_stack_timespec_sec(&inode_item->otime);1901 BTRFS_I(inode)->i_otime_nsec = btrfs_stack_timespec_nsec(&inode_item->otime);
1905 BTRFS_I(inode)->i_otime.tv_nsec =
1906 btrfs_stack_timespec_nsec(&inode_item->otime);
1907 1902
1908 inode->i_generation = BTRFS_I(inode)->generation;1903 inode->i_generation = BTRFS_I(inode)->generation;
1909 BTRFS_I(inode)->index_cnt = (u64)-1;1904 BTRFS_I(inode)->index_cnt = (u64)-1;
@@ -1914,9 +1909,9 @@ int btrfs_fill_inode(struct inode *inode, u32 *rdev) · 4 unmodified lines
1914}1909}
1915 1910
1916int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,1911int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1917 struct btrfs_root *root,
1918 struct btrfs_inode *inode)1912 struct btrfs_inode *inode)
1919{1913{
1914 struct btrfs_root *root = inode->root;
1920 struct btrfs_delayed_node *delayed_node;1915 struct btrfs_delayed_node *delayed_node;
1921 int ret = 0;1916 int ret = 0;
1922 1917
fs/btrfs/delayed-inode.h+0−1
@@ -135,7 +135,6 @@ int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode); · 134 unmodified lines
135 135
136 136
137int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,137int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root,
139 struct btrfs_inode *inode);138 struct btrfs_inode *inode);
140int btrfs_fill_inode(struct inode *inode, u32 *rdev);139int btrfs_fill_inode(struct inode *inode, u32 *rdev);
141int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode);140int 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
57 * Release a ref head's reservation.57 * Release a ref head's reservation.
58 *58 *
59 * @fs_info: the filesystem59 * @fs_info: the filesystem
60 * @nr: number of items to drop60 * @nr_refs: number of delayed refs to drop
61 * @nr_csums: number of csum items to drop
61 *62 *
62 * Drops the delayed ref head's count from the delayed refs rsv and free any63 * Drops the delayed ref head's count from the delayed refs rsv and free any
63 * excess reservation we had.64 * excess reservation we had.
64 */65 */
65void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr)66void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr_refs, int nr_csums)
66{67{
67 struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;68 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 num_bytes;
69 u64 released = 0;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);
70 74
71 released = btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL);75 released = btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL);
72 if (released)76 if (released)
@@ -77,26 +81,118 @@ void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr) · 4 unmodified lines
77/*81/*
78 * Adjust the size of the delayed refs rsv.82 * Adjust the size of the delayed refs rsv.
79 *83 *
80 * This is to be called anytime we may have adjusted trans->delayed_ref_updates,84 * 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.85 * or trans->delayed_ref_csum_deletions, it'll calculate the additional size and
86 * add it to the delayed_refs_rsv.
82 */87 */
83void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans)88void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans)
84{89{
85 struct btrfs_fs_info *fs_info = trans->fs_info;90 struct btrfs_fs_info *fs_info = trans->fs_info;
86 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;91 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
92 struct btrfs_block_rsv *local_rsv = &trans->delayed_rsv;
87 u64 num_bytes;93 u64 num_bytes;
94 u64 reserved_bytes;
88 95
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)
90 return;101 return;
91 102
92 num_bytes = btrfs_calc_delayed_ref_bytes(fs_info,103 /*
93 trans->delayed_ref_updates);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);
94 118
95 spin_lock(&delayed_rsv->lock);119 spin_lock(&delayed_rsv->lock);
96 delayed_rsv->size += num_bytes;120 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);
98 spin_unlock(&delayed_rsv->lock);123 spin_unlock(&delayed_rsv->lock);
99 trans->delayed_ref_updates = 0;124 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 */
132void 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 */
151void 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 */
167void 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 */
186void 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);
100}196}
101 197
102/*198/*
@@ -154,6 +250,7 @@ int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info, · 51 unmodified lines
154 enum btrfs_reserve_flush_enum flush)250 enum btrfs_reserve_flush_enum flush)
155{251{
156 struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;252 struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
253 struct btrfs_space_info *space_info = block_rsv->space_info;
157 u64 limit = btrfs_calc_delayed_ref_bytes(fs_info, 1);254 u64 limit = btrfs_calc_delayed_ref_bytes(fs_info, 1);
158 u64 num_bytes = 0;255 u64 num_bytes = 0;
159 u64 refilled_bytes;256 u64 refilled_bytes;
@@ -170,7 +267,7 @@ int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info, · 10 unmodified lines
170 if (!num_bytes)267 if (!num_bytes)
171 return 0;268 return 0;
172 269
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);
174 if (ret)271 if (ret)
175 return ret;272 return ret;
176 273
@@ -199,8 +296,7 @@ int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info, · 22 unmodified lines
199 spin_unlock(&block_rsv->lock);296 spin_unlock(&block_rsv->lock);
200 297
201 if (to_free > 0)298 if (to_free > 0)
202 btrfs_space_info_free_bytes_may_use(fs_info, block_rsv->space_info,299 btrfs_space_info_free_bytes_may_use(fs_info, space_info, to_free);
203 to_free);
204 300
205 if (refilled_bytes > 0)301 if (refilled_bytes > 0)
206 trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv", 0,302 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
422 return 0;518 return 0;
423}519}
424 520
425static inline void drop_delayed_ref(struct btrfs_delayed_ref_root *delayed_refs,521static inline void drop_delayed_ref(struct btrfs_fs_info *fs_info,
522 struct btrfs_delayed_ref_root *delayed_refs,
426 struct btrfs_delayed_ref_head *head,523 struct btrfs_delayed_ref_head *head,
427 struct btrfs_delayed_ref_node *ref)524 struct btrfs_delayed_ref_node *ref)
428{525{
@@ -433,9 +530,11 @@ static inline void drop_delayed_ref(struct btrfs_delayed_ref_root *delayed_refs, · 4 unmodified lines
433 list_del(&ref->add_list);530 list_del(&ref->add_list);
434 btrfs_put_delayed_ref(ref);531 btrfs_put_delayed_ref(ref);
435 atomic_dec(&delayed_refs->num_entries);532 atomic_dec(&delayed_refs->num_entries);
533 btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
436}534}
437 535
438static bool merge_ref(struct btrfs_delayed_ref_root *delayed_refs,536static bool merge_ref(struct btrfs_fs_info *fs_info,
537 struct btrfs_delayed_ref_root *delayed_refs,
439 struct btrfs_delayed_ref_head *head,538 struct btrfs_delayed_ref_head *head,
440 struct btrfs_delayed_ref_node *ref,539 struct btrfs_delayed_ref_node *ref,
441 u64 seq)540 u64 seq)
@@ -464,10 +563,10 @@ static bool merge_ref(struct btrfs_delayed_ref_root *delayed_refs, · 22 unmodified lines
464 mod = -next->ref_mod;563 mod = -next->ref_mod;
465 }564 }
466 565
467 drop_delayed_ref(delayed_refs, head, next);566 drop_delayed_ref(fs_info, delayed_refs, head, next);
468 ref->ref_mod += mod;567 ref->ref_mod += mod;
469 if (ref->ref_mod == 0) {568 if (ref->ref_mod == 0) {
470 drop_delayed_ref(delayed_refs, head, ref);569 drop_delayed_ref(fs_info, delayed_refs, head, ref);
471 done = true;570 done = true;
472 } else {571 } else {
473 /*572 /*
@@ -505,7 +604,7 @@ again: · 31 unmodified lines
505 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);604 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
506 if (seq && ref->seq >= seq)605 if (seq && ref->seq >= seq)
507 continue;606 continue;
508 if (merge_ref(delayed_refs, head, ref, seq))607 if (merge_ref(fs_info, delayed_refs, head, ref, seq))
509 goto again;608 goto again;
510 }609 }
511}610}
@@ -584,10 +683,11 @@ void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs, · 72 unmodified lines
584 * Return true if the ref was merged into an existing one (and therefore can be683 * Return true if the ref was merged into an existing one (and therefore can be
585 * freed by the caller).684 * freed by the caller).
586 */685 */
587static bool insert_delayed_ref(struct btrfs_delayed_ref_root *root,686static bool insert_delayed_ref(struct btrfs_trans_handle *trans,
588 struct btrfs_delayed_ref_head *href,687 struct btrfs_delayed_ref_head *href,
589 struct btrfs_delayed_ref_node *ref)688 struct btrfs_delayed_ref_node *ref)
590{689{
690 struct btrfs_delayed_ref_root *root = &trans->transaction->delayed_refs;
591 struct btrfs_delayed_ref_node *exist;691 struct btrfs_delayed_ref_node *exist;
592 int mod;692 int mod;
593 693
@@ -598,6 +698,7 @@ static bool insert_delayed_ref(struct btrfs_delayed_ref_root *root, · 4 unmodified lines
598 list_add_tail(&ref->add_list, &href->ref_add_list);698 list_add_tail(&ref->add_list, &href->ref_add_list);
599 atomic_inc(&root->num_entries);699 atomic_inc(&root->num_entries);
600 spin_unlock(&href->lock);700 spin_unlock(&href->lock);
701 trans->delayed_ref_updates++;
601 return false;702 return false;
602 }703 }
603 704
@@ -626,7 +727,7 @@ static bool insert_delayed_ref(struct btrfs_delayed_ref_root *root, · 22 unmodified lines
626 727
627 /* remove existing tail if its ref_mod is zero */728 /* remove existing tail if its ref_mod is zero */
628 if (exist->ref_mod == 0)729 if (exist->ref_mod == 0)
629 drop_delayed_ref(root, href, exist);730 drop_delayed_ref(trans->fs_info, root, href, exist);
630 spin_unlock(&href->lock);731 spin_unlock(&href->lock);
631 return true;732 return true;
632}733}
@@ -647,6 +748,15 @@ static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans, · 14 unmodified lines
647 BUG_ON(existing->is_data != update->is_data);748 BUG_ON(existing->is_data != update->is_data);
648 749
649 spin_lock(&existing->lock);750 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
650 if (update->must_insert_reserved) {760 if (update->must_insert_reserved) {
651 /* if the extent was freed and then761 /* if the extent was freed and then
652 * reallocated before the delayed ref762 * reallocated before the delayed ref
@@ -656,6 +766,7 @@ static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans, · 3 unmodified lines
656 * Set it again here766 * Set it again here
657 */767 */
658 existing->must_insert_reserved = update->must_insert_reserved;768 existing->must_insert_reserved = update->must_insert_reserved;
769 existing->owning_root = update->owning_root;
659 770
660 /*771 /*
661 * update the num_bytes so we make sure the accounting772 * 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
695 /*806 /*
696 * If we are going to from a positive ref mod to a negative or vice807 * If we are going to from a positive ref mod to a negative or vice
697 * versa we need to make sure to adjust pending_csums accordingly.808 * 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.
698 */811 */
699 if (existing->is_data) {812 if (existing->is_data) {
700 u64 csum_leaves =813 u64 csum_leaves =
@@ -703,11 +816,11 @@ static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans, · 2 unmodified lines
703 816
704 if (existing->total_ref_mod >= 0 && old_ref_mod < 0) {817 if (existing->total_ref_mod >= 0 && old_ref_mod < 0) {
705 delayed_refs->pending_csums -= existing->num_bytes;818 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);
707 }820 }
708 if (existing->total_ref_mod < 0 && old_ref_mod >= 0) {821 if (existing->total_ref_mod < 0 && old_ref_mod >= 0) {
709 delayed_refs->pending_csums += existing->num_bytes;822 delayed_refs->pending_csums += existing->num_bytes;
710 trans->delayed_ref_updates += csum_leaves;823 trans->delayed_ref_csum_deletions += csum_leaves;
711 }824 }
712 }825 }
713 826
@@ -718,7 +831,7 @@ static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref, · 4 unmodified lines
718 struct btrfs_qgroup_extent_record *qrecord,831 struct btrfs_qgroup_extent_record *qrecord,
719 u64 bytenr, u64 num_bytes, u64 ref_root,832 u64 bytenr, u64 num_bytes, u64 ref_root,
720 u64 reserved, int action, bool is_data,833 u64 reserved, int action, bool is_data,
721 bool is_system)834 bool is_system, u64 owning_root)
722{835{
723 int count_mod = 1;836 int count_mod = 1;
724 bool must_insert_reserved = false;837 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
758 head_ref->bytenr = bytenr;871 head_ref->bytenr = bytenr;
759 head_ref->num_bytes = num_bytes;872 head_ref->num_bytes = num_bytes;
760 head_ref->ref_mod = count_mod;873 head_ref->ref_mod = count_mod;
874 head_ref->reserved_bytes = reserved;
761 head_ref->must_insert_reserved = must_insert_reserved;875 head_ref->must_insert_reserved = must_insert_reserved;
876 head_ref->owning_root = owning_root;
762 head_ref->is_data = is_data;877 head_ref->is_data = is_data;
763 head_ref->is_system = is_system;878 head_ref->is_system = is_system;
764 head_ref->ref_tree = RB_ROOT_CACHED;879 head_ref->ref_tree = RB_ROOT_CACHED;
@@ -819,16 +934,21 @@ add_delayed_ref_head(struct btrfs_trans_handle *trans, · 54 unmodified lines
819 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);934 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
820 head_ref = existing;935 head_ref = existing;
821 } else {936 } 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 */
822 if (head_ref->is_data && head_ref->ref_mod < 0) {943 if (head_ref->is_data && head_ref->ref_mod < 0) {
823 delayed_refs->pending_csums += head_ref->num_bytes;944 delayed_refs->pending_csums += head_ref->num_bytes;
824 trans->delayed_ref_updates +=945 trans->delayed_ref_csum_deletions +=
825 btrfs_csum_bytes_to_leaves(trans->fs_info,946 btrfs_csum_bytes_to_leaves(trans->fs_info,
826 head_ref->num_bytes);947 head_ref->num_bytes);
827 }948 }
828 delayed_refs->num_heads++;949 delayed_refs->num_heads++;
829 delayed_refs->num_heads_ready++;950 delayed_refs->num_heads_ready++;
830 atomic_inc(&delayed_refs->num_entries);951 atomic_inc(&delayed_refs->num_entries);
831 trans->delayed_ref_updates++;
832 }952 }
833 if (qrecord_inserted_ret)953 if (qrecord_inserted_ret)
834 *qrecord_inserted_ret = qrecord_inserted;954 *qrecord_inserted_ret = qrecord_inserted;
@@ -837,8 +957,7 @@ add_delayed_ref_head(struct btrfs_trans_handle *trans, · 2 unmodified lines
837}957}
838 958
839/*959/*
840 * init_delayed_ref_common - Initialize the structure which represents a960 * Initialize the structure which represents a modification to a an extent.
841 * modification to a an extent.
842 *961 *
843 * @fs_info: Internal to the mounted filesystem mount structure.962 * @fs_info: Internal to the mounted filesystem mount structure.
844 *963 *
@@ -909,7 +1028,7 @@ int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans, · 64 unmodified lines
909 u64 parent = generic_ref->parent;1028 u64 parent = generic_ref->parent;
910 u8 ref_type;1029 u8 ref_type;
911 1030
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);
913 1032
914 ASSERT(generic_ref->type == BTRFS_REF_METADATA && generic_ref->action);1033 ASSERT(generic_ref->type == BTRFS_REF_METADATA && generic_ref->action);
915 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);1034 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
922 return -ENOMEM;1041 return -ENOMEM;
923 }1042 }
924 1043
925 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&1044 if (btrfs_qgroup_enabled(fs_info) && !generic_ref->skip_qgroup) {
926 !generic_ref->skip_qgroup) {
927 record = kzalloc(sizeof(*record), GFP_NOFS);1045 record = kzalloc(sizeof(*record), GFP_NOFS);
928 if (!record) {1046 if (!record) {
929 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);1047 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
938 ref_type = BTRFS_TREE_BLOCK_REF_KEY;1056 ref_type = BTRFS_TREE_BLOCK_REF_KEY;
939 1057
940 init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,1058 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,
942 ref_type);1060 ref_type);
943 ref->root = generic_ref->tree_ref.owning_root;1061 ref->root = generic_ref->tree_ref.ref_root;
944 ref->parent = parent;1062 ref->parent = parent;
945 ref->level = level;1063 ref->level = level;
946 1064
947 init_delayed_ref_head(head_ref, record, bytenr, num_bytes,1065 init_delayed_ref_head(head_ref, record, bytenr, num_bytes,
948 generic_ref->tree_ref.owning_root, 0, action,1066 generic_ref->tree_ref.ref_root, 0, action,
949 false, is_system);1067 false, is_system, generic_ref->owning_root);
950 head_ref->extent_op = extent_op;1068 head_ref->extent_op = extent_op;
951 1069
952 delayed_refs = &trans->transaction->delayed_refs;1070 delayed_refs = &trans->transaction->delayed_refs;
@@ -959,7 +1077,7 @@ int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans, · 6 unmodified lines
959 head_ref = add_delayed_ref_head(trans, head_ref, record,1077 head_ref = add_delayed_ref_head(trans, head_ref, record,
960 action, &qrecord_inserted);1078 action, &qrecord_inserted);
961 1079
962 merged = insert_delayed_ref(delayed_refs, head_ref, &ref->node);1080 merged = insert_delayed_ref(trans, head_ref, &ref->node);
963 spin_unlock(&delayed_refs->lock);1081 spin_unlock(&delayed_refs->lock);
964 1082
965 /*1083 /*
@@ -998,7 +1116,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans, · 32 unmodified lines
998 u64 bytenr = generic_ref->bytenr;1116 u64 bytenr = generic_ref->bytenr;
999 u64 num_bytes = generic_ref->len;1117 u64 num_bytes = generic_ref->len;
1000 u64 parent = generic_ref->parent;1118 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;
1002 u64 owner = generic_ref->data_ref.ino;1120 u64 owner = generic_ref->data_ref.ino;
1003 u64 offset = generic_ref->data_ref.offset;1121 u64 offset = generic_ref->data_ref.offset;
1004 u8 ref_type;1122 u8 ref_type;
@@ -1026,8 +1144,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans, · 21 unmodified lines
1026 return -ENOMEM;1144 return -ENOMEM;
1027 }1145 }
1028 1146
1029 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&1147 if (btrfs_qgroup_enabled(fs_info) && !generic_ref->skip_qgroup) {
1030 !generic_ref->skip_qgroup) {
1031 record = kzalloc(sizeof(*record), GFP_NOFS);1148 record = kzalloc(sizeof(*record), GFP_NOFS);
1032 if (!record) {1149 if (!record) {
1033 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);1150 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
1038 }1155 }
1039 1156
1040 init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root,1157 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);
1042 head_ref->extent_op = NULL;1159 head_ref->extent_op = NULL;
1043 1160
1044 delayed_refs = &trans->transaction->delayed_refs;1161 delayed_refs = &trans->transaction->delayed_refs;
@@ -1051,7 +1168,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans, · 6 unmodified lines
1051 head_ref = add_delayed_ref_head(trans, head_ref, record,1168 head_ref = add_delayed_ref_head(trans, head_ref, record,
1052 action, &qrecord_inserted);1169 action, &qrecord_inserted);
1053 1170
1054 merged = insert_delayed_ref(delayed_refs, head_ref, &ref->node);1171 merged = insert_delayed_ref(trans, head_ref, &ref->node);
1055 spin_unlock(&delayed_refs->lock);1172 spin_unlock(&delayed_refs->lock);
1056 1173
1057 /*1174 /*
@@ -1084,7 +1201,7 @@ int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans, · 26 unmodified lines
1084 return -ENOMEM;1201 return -ENOMEM;
1085 1202
1086 init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0,1203 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);
1088 head_ref->extent_op = extent_op;1205 head_ref->extent_op = extent_op;
1089 1206
1090 delayed_refs = &trans->transaction->delayed_refs;1207 delayed_refs = &trans->transaction->delayed_refs;
fs/btrfs/delayed-ref.h+53−17
@@ -9,10 +9,16 @@ · 8 unmodified lines
9#include <linux/refcount.h>9#include <linux/refcount.h>
10 10
11/* these are the possible values of struct btrfs_delayed_ref_node->action */11/* 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 */12enum btrfs_delayed_ref_action {
13#define BTRFS_DROP_DELAYED_REF 2 /* delete one backref from the tree */13 /* Add one backref to the tree */
14#define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */14 BTRFS_ADD_DELAYED_REF = 1,
15#define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */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;
16 22
17struct btrfs_delayed_ref_node {23struct btrfs_delayed_ref_node {
18 struct rb_node ref_node;24 struct rb_node ref_node;
@@ -104,6 +110,18 @@ struct btrfs_delayed_ref_head { · 85 unmodified lines
104 */110 */
105 int ref_mod;111 int ref_mod;
106 112
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
107 /*125 /*
108 * when a new extent is allocated, it is just reserved in memory126 * when a new extent is allocated, it is just reserved in memory
109 * The actual extent isn't inserted into the extent allocation tree127 * The actual extent isn't inserted into the extent allocation tree
@@ -117,6 +135,7 @@ struct btrfs_delayed_ref_head { · 7 unmodified lines
117 * the free has happened.135 * the free has happened.
118 */136 */
119 bool must_insert_reserved;137 bool must_insert_reserved;
138
120 bool is_data;139 bool is_data;
121 bool is_system;140 bool is_system;
122 bool processing;141 bool processing;
@@ -183,13 +202,13 @@ enum btrfs_ref_type { · 60 unmodified lines
183 BTRFS_REF_DATA,202 BTRFS_REF_DATA,
184 BTRFS_REF_METADATA,203 BTRFS_REF_METADATA,
185 BTRFS_REF_LAST,204 BTRFS_REF_LAST,
186};205} __packed;
187 206
188struct btrfs_data_ref {207struct btrfs_data_ref {
189 /* For EXTENT_DATA_REF */208 /* For EXTENT_DATA_REF */
190 209
191 /* Original root this data extent belongs to */210 /* Root which owns this data reference. */
192 u64 owning_root;211 u64 ref_root;
193 212
194 /* Inode which refers to this data extent */213 /* Inode which refers to this data extent */
195 u64 ino;214 u64 ino;
@@ -212,18 +231,18 @@ struct btrfs_tree_ref { · 16 unmodified lines
212 int level;231 int level;
213 232
214 /*233 /*
215 * Root which owns this tree block.234 * Root which owns this tree block reference.
216 *235 *
217 * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)236 * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
218 */237 */
219 u64 owning_root;238 u64 ref_root;
220 239
221 /* For non-skinny metadata, no special member needed */240 /* For non-skinny metadata, no special member needed */
222};241};
223 242
224struct btrfs_ref {243struct btrfs_ref {
225 enum btrfs_ref_type type;244 enum btrfs_ref_type type;
226 int action;245 enum btrfs_delayed_ref_action action;
227 246
228 /*247 /*
229 * Whether this extent should go through qgroup record.248 * Whether this extent should go through qgroup record.
@@ -239,6 +258,7 @@ struct btrfs_ref { · 9 unmodified lines
239#endif258#endif
240 u64 bytenr;259 u64 bytenr;
241 u64 len;260 u64 len;
261 u64 owning_root;
242 262
243 /* Bytenr of the parent tree block */263 /* Bytenr of the parent tree block */
244 u64 parent;264 u64 parent;
@@ -277,24 +297,37 @@ static inline u64 btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info *fs_in · 32 unmodified lines
277 return num_bytes;297 return num_bytes;
278}298}
279 299
300static 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
280static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,311static 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)
282{314{
283 generic_ref->action = action;315 generic_ref->action = action;
284 generic_ref->bytenr = bytenr;316 generic_ref->bytenr = bytenr;
285 generic_ref->len = len;317 generic_ref->len = len;
286 generic_ref->parent = parent;318 generic_ref->parent = parent;
319 generic_ref->owning_root = owning_root;
287}320}
288 321
289static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,322static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref, int level,
290 int level, u64 root, u64 mod_root, bool skip_qgroup)323 u64 root, u64 mod_root, bool skip_qgroup)
291{324{
292#ifdef CONFIG_BTRFS_FS_REF_VERIFY325#ifdef CONFIG_BTRFS_FS_REF_VERIFY
293 /* If @real_root not set, use @root as fallback */326 /* If @real_root not set, use @root as fallback */
294 generic_ref->real_root = mod_root ?: root;327 generic_ref->real_root = mod_root ?: root;
295#endif328#endif
296 generic_ref->tree_ref.level = level;329 generic_ref->tree_ref.level = level;
297 generic_ref->tree_ref.owning_root = root;330 generic_ref->tree_ref.ref_root = root;
298 generic_ref->type = BTRFS_REF_METADATA;331 generic_ref->type = BTRFS_REF_METADATA;
299 if (skip_qgroup || !(is_fstree(root) &&332 if (skip_qgroup || !(is_fstree(root) &&
300 (!mod_root || is_fstree(mod_root))))333 (!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
312 /* If @real_root not set, use @root as fallback */345 /* If @real_root not set, use @root as fallback */
313 generic_ref->real_root = mod_root ?: ref_root;346 generic_ref->real_root = mod_root ?: ref_root;
314#endif347#endif
315 generic_ref->data_ref.owning_root = ref_root;348 generic_ref->data_ref.ref_root = ref_root;
316 generic_ref->data_ref.ino = ino;349 generic_ref->data_ref.ino = ino;
317 generic_ref->data_ref.offset = offset;350 generic_ref->data_ref.offset = offset;
318 generic_ref->type = BTRFS_REF_DATA;351 generic_ref->type = BTRFS_REF_DATA;
@@ -338,7 +371,6 @@ btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op) · 19 unmodified lines
338 371
339static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)372static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
340{373{
341 WARN_ON(refcount_read(&ref->refs) == 0);
342 if (refcount_dec_and_test(&ref->refs)) {374 if (refcount_dec_and_test(&ref->refs)) {
343 WARN_ON(!RB_EMPTY_NODE(&ref->ref_node));375 WARN_ON(!RB_EMPTY_NODE(&ref->ref_node));
344 switch (ref->type) {376 switch (ref->type) {
@@ -402,8 +434,12 @@ struct btrfs_delayed_ref_head *btrfs_select_ref_head( · 57 unmodified lines
402 434
403int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);435int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
404 436
405void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);437void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr_refs, int nr_csums);
406void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);438void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
439void btrfs_inc_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);
440void btrfs_dec_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);
441void btrfs_inc_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);
442void btrfs_dec_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);
407int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,443int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
408 enum btrfs_reserve_flush_enum flush);444 enum btrfs_reserve_flush_enum flush);
409void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,445void 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
17#include "print-tree.h"17#include "print-tree.h"
18#include "volumes.h"18#include "volumes.h"
19#include "async-thread.h"19#include "async-thread.h"
20#include "check-integrity.h"
21#include "dev-replace.h"20#include "dev-replace.h"
22#include "sysfs.h"21#include "sysfs.h"
23#include "zoned.h"22#include "zoned.h"
@@ -444,7 +443,7 @@ int btrfs_run_dev_replace(struct btrfs_trans_handle *trans) · 420 unmodified lines
444 dev_replace->item_needs_writeback = 0;443 dev_replace->item_needs_writeback = 0;
445 up_write(&dev_replace->rwsem);444 up_write(&dev_replace->rwsem);
446 445
447 btrfs_mark_buffer_dirty(eb);446 btrfs_mark_buffer_dirty(trans, eb);
448 447
449out:448out:
450 btrfs_free_path(path);449 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
38 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);38 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
39 if (di)39 if (di)
40 return ERR_PTR(-EEXIST);40 return ERR_PTR(-EEXIST);
41 btrfs_extend_item(path, data_size);41 btrfs_extend_item(trans, path, data_size);
42 } else if (ret < 0)42 } else if (ret < 0)
43 return ERR_PTR(ret);43 return ERR_PTR(ret);
44 WARN_ON(ret > 0);44 WARN_ON(ret > 0);
@@ -93,7 +93,7 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, · 48 unmodified lines
93 93
94 write_extent_buffer(leaf, name, name_ptr, name_len);94 write_extent_buffer(leaf, name, name_ptr, name_len);
95 write_extent_buffer(leaf, data, data_ptr, data_len);95 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]);
97 97
98 return ret;98 return ret;
99}99}
@@ -153,7 +153,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, · 53 unmodified lines
153 name_ptr = (unsigned long)(dir_item + 1);153 name_ptr = (unsigned long)(dir_item + 1);
154 154
155 write_extent_buffer(leaf, name->name, name_ptr, name->len);155 write_extent_buffer(leaf, name->name, name_ptr, name->len);
156 btrfs_mark_buffer_dirty(leaf);156 btrfs_mark_buffer_dirty(trans, leaf);
157 157
158second_insert:158second_insert:
159 /* FIXME, use some real flag for selecting the extra index */159 /* 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
439 start = btrfs_item_ptr_offset(leaf, path->slots[0]);439 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
440 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,440 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
441 item_len - (ptr + sub_item_len - start));441 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);
443 }443 }
444 return ret;444 return ret;
445}445}
fs/btrfs/dir-item.h+9−0
@@ -3,6 +3,10 @@ · 2 unmodified lines
3#ifndef BTRFS_DIR_ITEM_H3#ifndef BTRFS_DIR_ITEM_H
4#define BTRFS_DIR_ITEM_H4#define BTRFS_DIR_ITEM_H
5 5
6#include <linux/crc32c.h>
7
8struct fscrypt_str;
9
6int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,10int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
7 const struct fscrypt_str *name);11 const struct fscrypt_str *name);
8int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,12int 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
39 const char *name,43 const char *name,
40 int name_len);44 int name_len);
41 45
46static inline u64 btrfs_name_hash(const char *name, int len)
47{
48 return crc32c((u32)~1, name, len);
49}
50
42#endif51#endif
fs/btrfs/disk-io.c+55−87
@@ -29,7 +29,6 @@ · 28 unmodified lines
29#include "tree-log.h"29#include "tree-log.h"
30#include "free-space-cache.h"30#include "free-space-cache.h"
31#include "free-space-tree.h"31#include "free-space-tree.h"
32#include "check-integrity.h"
33#include "rcu-string.h"32#include "rcu-string.h"
34#include "dev-replace.h"33#include "dev-replace.h"
35#include "raid56.h"34#include "raid56.h"
@@ -245,6 +244,7 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio) · 209 unmodified lines
245 struct extent_buffer *eb = bbio->private;244 struct extent_buffer *eb = bbio->private;
246 struct btrfs_fs_info *fs_info = eb->fs_info;245 struct btrfs_fs_info *fs_info = eb->fs_info;
247 u64 found_start = btrfs_header_bytenr(eb);246 u64 found_start = btrfs_header_bytenr(eb);
247 u64 last_trans;
248 u8 result[BTRFS_CSUM_SIZE];248 u8 result[BTRFS_CSUM_SIZE];
249 int ret;249 int ret;
250 250
@@ -282,12 +282,12 @@ blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio) · 31 unmodified lines
282 * Also check the generation, the eb reached here must be newer than282 * Also check the generation, the eb reached here must be newer than
283 * last committed. Or something seriously wrong happened.283 * last committed. Or something seriously wrong happened.
284 */284 */
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)) {
286 ret = -EUCLEAN;287 ret = -EUCLEAN;
287 btrfs_err(fs_info,288 btrfs_err(fs_info,
288 "block=%llu bad generation, have %llu expect > %llu",289 "block=%llu bad generation, have %llu expect > %llu",
289 eb->start, btrfs_header_generation(eb),290 eb->start, btrfs_header_generation(eb), last_trans);
290 fs_info->last_trans_committed);
291 goto error;291 goto error;
292 }292 }
293 write_extent_buffer(eb, result, 0, fs_info->csum_size);293 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
318 BTRFS_FSID_SIZE);318 BTRFS_FSID_SIZE);
319 319
320 /*320 /*
321 * alloc_fs_devices() copies the fsid into metadata_uuid if the321 * alloc_fsid_devices() copies the fsid into fs_devices::metadata_uuid.
322 * metadata_uuid is unset in the superblock, including for a seed device.322 * This is then overwritten by metadata_uuid if it is present in the
323 * So, we can use fs_devices->metadata_uuid.323 * device_list_add(). The same true for a seed device as well. So use of
324 * fs_devices::metadata_uuid is appropriate here.
324 */325 */
325 if (memcmp(fsid, fs_info->fs_devices->metadata_uuid, BTRFS_FSID_SIZE) == 0)326 if (memcmp(fsid, fs_info->fs_devices->metadata_uuid, BTRFS_FSID_SIZE) == 0)
326 return false;327 return false;
@@ -675,9 +676,9 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, · 348 unmodified lines
675 refcount_set(&root->refs, 1);676 refcount_set(&root->refs, 1);
676 atomic_set(&root->snapshot_force_cow, 0);677 atomic_set(&root->snapshot_force_cow, 0);
677 atomic_set(&root->nr_swapfiles, 0);678 atomic_set(&root->nr_swapfiles, 0);
678 root->log_transid = 0;679 btrfs_set_root_log_transid(root, 0);
679 root->log_transid_committed = -1;680 root->log_transid_committed = -1;
680 root->last_log_commit = 0;681 btrfs_set_root_last_log_commit(root, 0);
681 root->anon_dev = 0;682 root->anon_dev = 0;
682 if (!dummy) {683 if (!dummy) {
683 extent_io_tree_init(fs_info, &root->dirty_log_pages,684 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
859 root->root_key.offset = 0;860 root->root_key.offset = 0;
860 861
861 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,862 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
862 BTRFS_NESTING_NORMAL);863 0, BTRFS_NESTING_NORMAL);
863 if (IS_ERR(leaf)) {864 if (IS_ERR(leaf)) {
864 ret = PTR_ERR(leaf);865 ret = PTR_ERR(leaf);
865 leaf = NULL;866 leaf = NULL;
@@ -867,7 +868,7 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, · 1 unmodified line
867 }868 }
868 869
869 root->node = leaf;870 root->node = leaf;
870 btrfs_mark_buffer_dirty(leaf);871 btrfs_mark_buffer_dirty(trans, leaf);
871 872
872 root->commit_root = btrfs_root_node(root);873 root->commit_root = btrfs_root_node(root);
873 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);874 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
936 */937 */
937 938
938 leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,939 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);
940 if (IS_ERR(leaf))941 if (IS_ERR(leaf))
941 return PTR_ERR(leaf);942 return PTR_ERR(leaf);
942 943
943 root->node = leaf;944 root->node = leaf;
944 945
945 btrfs_mark_buffer_dirty(root->node);946 btrfs_mark_buffer_dirty(trans, root->node);
946 btrfs_tree_unlock(root->node);947 btrfs_tree_unlock(root->node);
947 948
948 return 0;949 return 0;
@@ -1004,9 +1005,9 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans, · 55 unmodified lines
1004 1005
1005 WARN_ON(root->log_root);1006 WARN_ON(root->log_root);
1006 root->log_root = log_root;1007 root->log_root = log_root;
1007 root->log_transid = 0;1008 btrfs_set_root_log_transid(root, 0);
1008 root->log_transid_committed = -1;1009 root->log_transid_committed = -1;
1009 root->last_log_commit = 0;1010 btrfs_set_root_last_log_commit(root, 0);
1010 return 0;1011 return 0;
1011}1012}
1012 1013
@@ -1179,6 +1180,8 @@ static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info, · 166 unmodified lines
1179 return btrfs_grab_root(fs_info->block_group_root);1180 return btrfs_grab_root(fs_info->block_group_root);
1180 case BTRFS_FREE_SPACE_TREE_OBJECTID:1181 case BTRFS_FREE_SPACE_TREE_OBJECTID:
1181 return btrfs_grab_root(btrfs_global_root(fs_info, &key));1182 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);
1182 default:1185 default:
1183 return NULL;1186 return NULL;
1184 }1187 }
@@ -1259,6 +1262,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info) · 74 unmodified lines
1259 btrfs_put_root(fs_info->fs_root);1262 btrfs_put_root(fs_info->fs_root);
1260 btrfs_put_root(fs_info->data_reloc_root);1263 btrfs_put_root(fs_info->data_reloc_root);
1261 btrfs_put_root(fs_info->block_group_root);1264 btrfs_put_root(fs_info->block_group_root);
1265 btrfs_put_root(fs_info->stripe_root);
1262 btrfs_check_leaked_roots(fs_info);1266 btrfs_check_leaked_roots(fs_info);
1263 btrfs_extent_buffer_leak_debug_check(fs_info);1267 btrfs_extent_buffer_leak_debug_check(fs_info);
1264 kfree(fs_info->super_copy);1268 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
1402}1406}
1403 1407
1404/*1408/*
1405 * btrfs_get_fs_root_commit_root - return a root for the given objectid1409 * Return a root for the given objectid.
1410 *
1406 * @fs_info: the fs_info1411 * @fs_info: the fs_info
1407 * @objectid: the objectid we need to lookup1412 * @objectid: the objectid we need to lookup
1408 *1413 *
@@ -1699,11 +1704,11 @@ static void backup_super_roots(struct btrfs_fs_info *info) · 290 unmodified lines
1699}1704}
1700 1705
1701/*1706/*
1702 * read_backup_root - Reads a backup root based on the passed priority. Prio 01707 * Reads a backup root based on the passed priority. Prio 0 is the newest, prio
1703 * is the newest, prio 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots1708 * 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots
1704 *1709 *
1705 * fs_info - filesystem whose backup roots need to be read1710 * @fs_info: filesystem whose backup roots need to be read
1706 * priority - priority of backup root required1711 * @priority: priority of backup root required
1707 *1712 *
1708 * Returns backup root index on success and -EINVAL otherwise.1713 * Returns backup root index on success and -EINVAL otherwise.
1709 */1714 */
@@ -1803,6 +1808,7 @@ static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root) · 93 unmodified lines
1803 free_root_extent_buffers(info->fs_root);1808 free_root_extent_buffers(info->fs_root);
1804 free_root_extent_buffers(info->data_reloc_root);1809 free_root_extent_buffers(info->data_reloc_root);
1805 free_root_extent_buffers(info->block_group_root);1810 free_root_extent_buffers(info->block_group_root);
1811 free_root_extent_buffers(info->stripe_root);
1806 if (free_chunk_root)1812 if (free_chunk_root)
1807 free_root_extent_buffers(info->chunk_root);1813 free_root_extent_buffers(info->chunk_root);
1808}1814}
@@ -2262,7 +2268,6 @@ static int btrfs_read_roots(struct btrfs_fs_info *fs_info) · 453 unmodified lines
2262 root = btrfs_read_tree_root(tree_root, &location);2268 root = btrfs_read_tree_root(tree_root, &location);
2263 if (!IS_ERR(root)) {2269 if (!IS_ERR(root)) {
2264 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);2270 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2265 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
2266 fs_info->quota_root = root;2271 fs_info->quota_root = root;
2267 }2272 }
2268 2273
@@ -2279,6 +2284,20 @@ static int btrfs_read_roots(struct btrfs_fs_info *fs_info) · 10 unmodified lines
2279 fs_info->uuid_root = root;2284 fs_info->uuid_root = root;
2280 }2285 }
2281 2286
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
2282 return 0;2301 return 0;
2283out:2302out:
2284 btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",2303 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
2381 ret = -EINVAL;2400 ret = -EINVAL;
2382 }2401 }
2383 2402
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) {
2385 btrfs_err(fs_info,2405 btrfs_err(fs_info,
2386 "superblock fsid doesn't match fsid of fs_devices: %pU != %pU",2406 "superblock fsid doesn't match fsid of fs_devices: %pU != %pU",
2387 sb->fsid, fs_info->fs_devices->fsid);2407 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
2634 2654
2635 /* All successful */2655 /* All successful */
2636 fs_info->generation = btrfs_header_generation(tree_root->node);2656 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);
2638 fs_info->last_reloc_trans = 0;2658 fs_info->last_reloc_trans = 0;
2639 2659
2640 /* Always begin writing backup roots after the one being used */2660 /* 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
2735 spin_lock_init(&fs_info->ordered_root_lock);2755 spin_lock_init(&fs_info->ordered_root_lock);
2736 2756
2737 btrfs_init_scrub(fs_info);2757 btrfs_init_scrub(fs_info);
2738#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2739 fs_info->check_integrity_print_mask = 0;
2740#endif
2741 btrfs_init_balance(fs_info);2758 btrfs_init_balance(fs_info);
2742 btrfs_init_async_reclaim_work(fs_info);2759 btrfs_init_async_reclaim_work(fs_info);
2743 2760
@@ -3157,7 +3174,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 413 unmodified lines
3157 u32 nodesize;3174 u32 nodesize;
3158 u32 stripesize;3175 u32 stripesize;
3159 u64 generation;3176 u64 generation;
3160 u64 features;
3161 u16 csum_type;3177 u16 csum_type;
3162 struct btrfs_super_block *disk_super;3178 struct btrfs_super_block *disk_super;
3163 struct btrfs_fs_info *fs_info = btrfs_sb(sb);3179 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
3239 3255
3240 disk_super = fs_info->super_copy;3256 disk_super = fs_info->super_copy;
3241 3257
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
3251 memcpy(fs_info->super_for_commit, fs_info->super_copy,3258 memcpy(fs_info->super_for_commit, fs_info->super_copy,
3252 sizeof(*fs_info->super_for_commit));3259 sizeof(*fs_info->super_for_commit));
3253 3260
@@ -3509,18 +3516,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device · 255 unmodified lines
3509 "auto enabling async discard");3516 "auto enabling async discard");
3510 }3517 }
3511 3518
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
3524 ret = btrfs_read_qgroup_config(fs_info);3519 ret = btrfs_read_qgroup_config(fs_info);
3525 if (ret)3520 if (ret)
3526 goto fail_trans_kthread;3521 goto fail_trans_kthread;
@@ -3820,8 +3815,6 @@ static int write_dev_supers(struct btrfs_device *device, · 293 unmodified lines
3820 */3815 */
3821 if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))3816 if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
3822 bio->bi_opf |= REQ_FUA;3817 bio->bi_opf |= REQ_FUA;
3823
3824 btrfsic_check_bio(bio);
3825 submit_bio(bio);3818 submit_bio(bio);
3826 3819
3827 if (btrfs_advance_sb_log(device, i))3820 if (btrfs_advance_sb_log(device, i))
@@ -3917,28 +3910,11 @@ static void write_dev_flush(struct btrfs_device *device) · 89 unmodified lines
3917 3910
3918 device->last_flush_error = BLK_STS_OK;3911 device->last_flush_error = BLK_STS_OK;
3919 3912
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
3935 bio_init(bio, device->bdev, NULL, 0,3913 bio_init(bio, device->bdev, NULL, 0,
3936 REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH);3914 REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH);
3937 bio->bi_end_io = btrfs_end_empty_barrier;3915 bio->bi_end_io = btrfs_end_empty_barrier;
3938 init_completion(&device->flush_wait);3916 init_completion(&device->flush_wait);
3939 bio->bi_private = &device->flush_wait;3917 bio->bi_private = &device->flush_wait;
3940
3941 btrfsic_check_bio(bio);
3942 submit_bio(bio);3918 submit_bio(bio);
3943 set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);3919 set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
3944}3920}
@@ -4414,16 +4390,12 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info) · 469 unmodified lines
4414 4390
4415 iput(fs_info->btree_inode);4391 iput(fs_info->btree_inode);
4416 4392
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
4422 btrfs_mapping_tree_free(&fs_info->mapping_tree);4393 btrfs_mapping_tree_free(&fs_info->mapping_tree);
4423 btrfs_close_devices(fs_info->fs_devices);4394 btrfs_close_devices(fs_info->fs_devices);
4424}4395}
4425 4396
4426void btrfs_mark_buffer_dirty(struct extent_buffer *buf)4397void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
4398 struct extent_buffer *buf)
4427{4399{
4428 struct btrfs_fs_info *fs_info = buf->fs_info;4400 struct btrfs_fs_info *fs_info = buf->fs_info;
4429 u64 transid = btrfs_header_generation(buf);4401 u64 transid = btrfs_header_generation(buf);
@@ -4437,21 +4409,16 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf) · 7 unmodified lines
4437 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))4409 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
4438 return;4410 return;
4439#endif4411#endif
4412 /* This is an active transaction (its state < TRANS_STATE_UNBLOCKED). */
4413 ASSERT(trans->transid == fs_info->generation);
4440 btrfs_assert_tree_write_locked(buf);4414 btrfs_assert_tree_write_locked(buf);
4441 if (transid != fs_info->generation)4415 if (unlikely(transid != fs_info->generation)) {
4442 WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",4416 btrfs_abort_transaction(trans, -EUCLEAN);
4443 buf->start, transid, fs_info->generation);4417 btrfs_crit(fs_info,
4444 set_extent_buffer_dirty(buf);4418"dirty buffer transid mismatch, logical %llu found transid %llu running transid %llu",
4445#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY4419 buf->start, transid, fs_info->generation);
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);
4453 }4420 }
4454#endif4421 set_extent_buffer_dirty(buf);
4455}4422}
4456 4423
4457static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,4424static 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
4611 list_del(&ref->add_list);4578 list_del(&ref->add_list);
4612 atomic_dec(&delayed_refs->num_entries);4579 atomic_dec(&delayed_refs->num_entries);
4613 btrfs_put_delayed_ref(ref);4580 btrfs_put_delayed_ref(ref);
4581 btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
4614 }4582 }
4615 if (head->must_insert_reserved)4583 if (head->must_insert_reserved)
4616 pin_bytes = true;4584 pin_bytes = true;
@@ -4808,7 +4776,7 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, · 191 unmodified lines
4808 4776
4809 spin_unlock(&cur_trans->dirty_bgs_lock);4777 spin_unlock(&cur_trans->dirty_bgs_lock);
4810 btrfs_put_block_group(cache);4778 btrfs_put_block_group(cache);
4811 btrfs_delayed_refs_rsv_release(fs_info, 1);4779 btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
4812 spin_lock(&cur_trans->dirty_bgs_lock);4780 spin_lock(&cur_trans->dirty_bgs_lock);
4813 }4781 }
4814 spin_unlock(&cur_trans->dirty_bgs_lock);4782 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
104}104}
105 105
106void btrfs_put_root(struct btrfs_root *root);106void btrfs_put_root(struct btrfs_root *root);
107void btrfs_mark_buffer_dirty(struct extent_buffer *buf);107void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
108 struct extent_buffer *buf);
108int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,109int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
109 int atomic);110 int atomic);
110int btrfs_read_extent_buffer(struct extent_buffer *buf,111int 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
105 lockdep_set_class(&tree->lock, &file_extent_tree_class);105 lockdep_set_class(&tree->lock, &file_extent_tree_class);
106}106}
107 107
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 */
108void extent_io_tree_release(struct extent_io_tree *tree)115void extent_io_tree_release(struct extent_io_tree *tree)
109{116{
117 struct rb_root root;
118 struct extent_state *state;
119 struct extent_state *tmp;
120
110 spin_lock(&tree->lock);121 spin_lock(&tree->lock);
111 /*122 root = tree->state;
112 * Do a single barrier for the waitqueue_active check here, the state123 tree->state = RB_ROOT;
113 * of the waitqueue should not change once extent_io_tree_release is124 rbtree_postorder_for_each_entry_safe(state, tmp, &root, rb_node) {
114 * called.125 /* Clear node to keep free_extent_state() happy. */
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);
124 RB_CLEAR_NODE(&state->rb_node);126 RB_CLEAR_NODE(&state->rb_node);
127 ASSERT(!(state->state & EXTENT_LOCKED));
125 /*128 /*
126 * btree io trees aren't supposed to have tasks waiting for129 * No need for a memory barrier here, as we are holding the tree
127 * changes in the flags of extent states ever.130 * lock and we only change the waitqueue while holding that lock
131 * (see wait_extent_bit()).
128 */132 */
129 ASSERT(!waitqueue_active(&state->wq));133 ASSERT(!waitqueue_active(&state->wq));
130 free_extent_state(state);134 free_extent_state(state);
131
132 cond_resched_lock(&tree->lock);135 cond_resched_lock(&tree->lock);
133 }136 }
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));
134 spin_unlock(&tree->lock);142 spin_unlock(&tree->lock);
135}143}
136 144
@@ -327,6 +335,36 @@ static void extent_io_tree_panic(struct extent_io_tree *tree, int err) · 190 unmodified lines
327 "locking error: extent tree was modified by another thread while locked");335 "locking error: extent tree was modified by another thread while locked");
328}336}
329 337
338static 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
353static 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
330/*368/*
331 * Utility function to look for merge candidates inside a given range. Any369 * Utility function to look for merge candidates inside a given range. Any
332 * extents with matching state are merged together into a single extent in the370 * 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
338 */376 */
339static void merge_state(struct extent_io_tree *tree, struct extent_state *state)377static void merge_state(struct extent_io_tree *tree, struct extent_state *state)
340{378{
341 struct extent_state *other;
342
343 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))379 if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY))
344 return;380 return;
345 381
346 other = prev_state(state);382 merge_prev_state(tree, state);
347 if (other && other->end == state->start - 1 &&383 merge_next_state(tree, state);
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 }
366}384}
367 385
368static void set_state_bits(struct extent_io_tree *tree,386static 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
384 * Insert an extent_state struct into the tree. 'bits' are set on the402 * Insert an extent_state struct into the tree. 'bits' are set on the
385 * struct before it is inserted.403 * struct before it is inserted.
386 *404 *
387 * This may return -EEXIST if the extent is already there, in which case the405 * Returns a pointer to the struct extent_state record containing the range
388 * state struct is freed.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.
389 *412 *
390 * The tree lock is not taken internally. This is a utility function and413 * The tree lock is not taken internally. This is a utility function and
391 * probably isn't what you want to call (see set/clear_extent_bit).414 * probably isn't what you want to call (see set/clear_extent_bit).
392 */415 */
393static int insert_state(struct extent_io_tree *tree,416static struct extent_state *insert_state(struct extent_io_tree *tree,
394 struct extent_state *state,417 struct extent_state *state,
395 u32 bits, struct extent_changeset *changeset)418 u32 bits,
419 struct extent_changeset *changeset)
396{420{
397 struct rb_node **node;421 struct rb_node **node;
398 struct rb_node *parent = NULL;422 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));
400 426
401 set_state_bits(tree, state, bits, changeset);427 set_state_bits(tree, state, bits, changeset);
402 428
@@ -407,23 +433,42 @@ static int insert_state(struct extent_io_tree *tree, · 4 unmodified lines
407 parent = *node;433 parent = *node;
408 entry = rb_entry(parent, struct extent_state, rb_node);434 entry = rb_entry(parent, struct extent_state, rb_node);
409 435
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 }
411 node = &(*node)->rb_left;447 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 }
413 node = &(*node)->rb_right;459 node = &(*node)->rb_right;
414 } else {460 } else {
415 btrfs_err(tree->fs_info,461 btrfs_err(tree->fs_info,
416 "found node %llu %llu on insert of %llu %llu",462 "found node %llu %llu on insert of %llu %llu",
417 entry->start, entry->end, state->start, end);463 entry->start, entry->end, state->start, state->end);
418 return -EEXIST;464 return ERR_PTR(-EEXIST);
419 }465 }
420 }466 }
421 467
422 rb_link_node(&state->rb_node, parent, node);468 rb_link_node(&state->rb_node, parent, node);
423 rb_insert_color(&state->rb_node, &tree->state);469 rb_insert_color(&state->rb_node, &tree->state);
424 470
425 merge_state(tree, state);471 return state;
426 return 0;
427}472}
428 473
429/*474/*
@@ -708,26 +753,13 @@ out: · 278 unmodified lines
708 753
709}754}
710 755
711static 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
724/*756/*
725 * Wait for one or more bits to clear on a range in the state tree.757 * Wait for one or more bits to clear on a range in the state tree.
726 * The range [start, end] is inclusive.758 * The range [start, end] is inclusive.
727 * The tree lock is taken by this function759 * The tree lock is taken by this function
728 */760 */
729void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,761static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
730 struct extent_state **cached_state)762 u32 bits, struct extent_state **cached_state)
731{763{
732 struct extent_state *state;764 struct extent_state *state;
733 765
@@ -758,9 +790,15 @@ process_node: · 24 unmodified lines
758 goto out;790 goto out;
759 791
760 if (state->state & bits) {792 if (state->state & bits) {
793 DEFINE_WAIT(wait);
794
761 start = state->start;795 start = state->start;
762 refcount_inc(&state->refs);796 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);
764 free_extent_state(state);802 free_extent_state(state);
765 goto again;803 goto again;
766 }804 }
@@ -847,10 +885,19 @@ bool find_first_extent_bit(struct extent_io_tree *tree, u64 start, · 80 unmodified lines
847 if (state->end == start - 1 && extent_state_in_tree(state)) {885 if (state->end == start - 1 && extent_state_in_tree(state)) {
848 while ((state = next_state(state)) != NULL) {886 while ((state = next_state(state)) != NULL) {
849 if (state->state & bits)887 if (state->state & bits)
850 goto got_it;888 break;
851 }889 }
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 */
852 free_extent_state(*cached_state);897 free_extent_state(*cached_state);
853 *cached_state = NULL;898 *cached_state = NULL;
899 if (state)
900 goto got_it;
854 goto out;901 goto out;
855 }902 }
856 free_extent_state(*cached_state);903 free_extent_state(*cached_state);
@@ -1133,6 +1180,8 @@ hit_next: · 276 unmodified lines
1133 */1180 */
1134 if (state->start > start) {1181 if (state->start > start) {
1135 u64 this_end;1182 u64 this_end;
1183 struct extent_state *inserted_state;
1184
1136 if (end < last_start)1185 if (end < last_start)
1137 this_end = end;1186 this_end = end;
1138 else1187 else
@@ -1148,12 +1197,15 @@ hit_next: · 9 unmodified lines
1148 */1197 */
1149 prealloc->start = start;1198 prealloc->start = start;
1150 prealloc->end = this_end;1199 prealloc->end = this_end;
1151 err = insert_state(tree, prealloc, bits, changeset);1200 inserted_state = insert_state(tree, prealloc, bits, changeset);
1152 if (err)1201 if (IS_ERR(inserted_state)) {
1202 err = PTR_ERR(inserted_state);
1153 extent_io_tree_panic(tree, err);1203 extent_io_tree_panic(tree, err);
1204 }
1154 1205
1155 cache_state(prealloc, cached_state);1206 cache_state(inserted_state, cached_state);
1156 prealloc = NULL;1207 if (inserted_state == prealloc)
1208 prealloc = NULL;
1157 start = this_end + 1;1209 start = this_end + 1;
1158 goto search_again;1210 goto search_again;
1159 }1211 }
@@ -1356,6 +1408,8 @@ hit_next: · 196 unmodified lines
1356 */1408 */
1357 if (state->start > start) {1409 if (state->start > start) {
1358 u64 this_end;1410 u64 this_end;
1411 struct extent_state *inserted_state;
1412
1359 if (end < last_start)1413 if (end < last_start)
1360 this_end = end;1414 this_end = end;
1361 else1415 else
@@ -1373,11 +1427,14 @@ hit_next: · 11 unmodified lines
1373 */1427 */
1374 prealloc->start = start;1428 prealloc->start = start;
1375 prealloc->end = this_end;1429 prealloc->end = this_end;
1376 err = insert_state(tree, prealloc, bits, NULL);1430 inserted_state = insert_state(tree, prealloc, bits, NULL);
1377 if (err)1431 if (IS_ERR(inserted_state)) {
1432 err = PTR_ERR(inserted_state);
1378 extent_io_tree_panic(tree, err);1433 extent_io_tree_panic(tree, err);
1379 cache_state(prealloc, cached_state);1434 }
1380 prealloc = NULL;1435 cache_state(inserted_state, cached_state);
1436 if (inserted_state == prealloc)
1437 prealloc = NULL;
1381 start = this_end + 1;1438 start = this_end + 1;
1382 goto search_again;1439 goto search_again;
1383 }1440 }
@@ -1640,15 +1697,46 @@ search: · 256 unmodified lines
1640}1697}
1641 1698
1642/*1699/*
1643 * Search a range in the state tree for a given mask. If 'filled' == 1, this1700 * Check if the single @bit exists in the given range.
1644 * returns 1 only if every extent in the tree has the bits set. Otherwise, 11701 */
1645 * is returned if any bit in the range is found set.1702bool 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.
1646 */1732 */
1647int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,1733bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
1648 u32 bits, int filled, struct extent_state *cached)1734 struct extent_state *cached)
1649{1735{
1650 struct extent_state *state = NULL;1736 struct extent_state *state = NULL;
1651 int bitset = 0;1737 bool bitset = true;
1738
1739 ASSERT(is_power_of_2(bit));
1652 1740
1653 spin_lock(&tree->lock);1741 spin_lock(&tree->lock);
1654 if (cached && extent_state_in_tree(cached) && cached->start <= start &&1742 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
1657 else1745 else
1658 state = tree_search(tree, start);1746 state = tree_search(tree, start);
1659 while (state && start <= end) {1747 while (state && start <= end) {
1660 if (filled && state->start > start) {1748 if (state->start > start) {
1661 bitset = 0;1749 bitset = false;
1662 break;1750 break;
1663 }1751 }
1664 1752
1665 if (state->start > end)1753 if (state->start > end)
1666 break;1754 break;
1667 1755
1668 if (state->state & bits) {1756 if ((state->state & bit) == 0) {
1669 bitset = 1;1757 bitset = false;
1670 if (!filled)
1671 break;
1672 } else if (filled) {
1673 bitset = 0;
1674 break;1758 break;
1675 }1759 }
1676 1760
1677 if (state->end == (u64)-1)1761 if (state->end == (u64)-1)
1678 break;1762 break;
1679 1763
1764 /*
1765 * Last entry (if state->end is (u64)-1 and overflow happens),
1766 * or next entry starts after the range.
1767 */
1680 start = state->end + 1;1768 start = state->end + 1;
1681 if (start > end)1769 if (start > end || start == 0)
1682 break;1770 break;
1683 state = next_state(state);1771 state = next_state(state);
1684 }1772 }
1685 1773
1686 /* We ran out of states and were still inside of our range. */1774 /* We ran out of states and were still inside of our range. */
1687 if (filled && !state)1775 if (!state)
1688 bitset = 0;1776 bitset = false;
1689 spin_unlock(&tree->lock);1777 spin_unlock(&tree->lock);
1690 return bitset;1778 return bitset;
1691}1779}
fs/btrfs/extent-io-tree.h+3−4
@@ -131,8 +131,9 @@ u64 count_range_bits(struct extent_io_tree *tree, · 130 unmodified lines
131 struct extent_state **cached_state);131 struct extent_state **cached_state);
132 132
133void free_extent_state(struct extent_state *state);133void free_extent_state(struct extent_state *state);
134int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,134bool test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
135 u32 bits, int filled, struct extent_state *cached_state);135 struct extent_state *cached_state);
136bool test_range_bit_exists(struct extent_io_tree *tree, u64 start, u64 end, u32 bit);
136int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,137int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
137 u32 bits, struct extent_changeset *changeset);138 u32 bits, struct extent_changeset *changeset);
138int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,139int __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
192bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,193bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
193 u64 *end, u64 max_bytes,194 u64 *end, u64 max_bytes,
194 struct extent_state **cached_state);195 struct extent_state **cached_state);
195void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
196 struct extent_state **cached_state);
197 196
198#endif /* BTRFS_EXTENT_IO_TREE_H */197#endif /* BTRFS_EXTENT_IO_TREE_H */
fs/btrfs/extent-tree.c+360−176
@@ -42,14 +42,16 @@ · 41 unmodified lines
42#include "file-item.h"42#include "file-item.h"
43#include "orphan.h"43#include "orphan.h"
44#include "tree-checker.h"44#include "tree-checker.h"
45#include "raid-stripe-tree.h"
45 46
46#undef SCRAMBLE_DELAYED_REFS47#undef SCRAMBLE_DELAYED_REFS
47 48
48 49
49static int __btrfs_free_extent(struct btrfs_trans_handle *trans,50static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
51 struct btrfs_delayed_ref_head *href,
50 struct btrfs_delayed_ref_node *node, u64 parent,52 struct btrfs_delayed_ref_node *node, u64 parent,
51 u64 root_objectid, u64 owner_objectid,53 u64 root_objectid, u64 owner_objectid,
52 u64 owner_offset, int refs_to_drop,54 u64 owner_offset,
53 struct btrfs_delayed_extent_op *extra_op);55 struct btrfs_delayed_extent_op *extra_op);
54static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,56static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
55 struct extent_buffer *leaf,57 struct extent_buffer *leaf,
@@ -57,7 +59,7 @@ static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, · 1 unmodified line
57static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,59static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
58 u64 parent, u64 root_objectid,60 u64 parent, u64 root_objectid,
59 u64 flags, u64 owner, u64 offset,61 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);
61static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,63static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
62 struct btrfs_delayed_ref_node *node,64 struct btrfs_delayed_ref_node *node,
63 struct btrfs_delayed_extent_op *extent_op);65 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
344 struct btrfs_extent_inline_ref *iref,346 struct btrfs_extent_inline_ref *iref,
345 enum btrfs_inline_ref_type is_data)347 enum btrfs_inline_ref_type is_data)
346{348{
349 struct btrfs_fs_info *fs_info = eb->fs_info;
347 int type = btrfs_extent_inline_ref_type(eb, iref);350 int type = btrfs_extent_inline_ref_type(eb, iref);
348 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);351 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
349 352
353 if (type == BTRFS_EXTENT_OWNER_REF_KEY) {
354 ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
355 return type;
356 }
357
350 if (type == BTRFS_TREE_BLOCK_REF_KEY ||358 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
351 type == BTRFS_SHARED_BLOCK_REF_KEY ||359 type == BTRFS_SHARED_BLOCK_REF_KEY ||
352 type == BTRFS_SHARED_DATA_REF_KEY ||360 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
355 if (type == BTRFS_TREE_BLOCK_REF_KEY)363 if (type == BTRFS_TREE_BLOCK_REF_KEY)
356 return type;364 return type;
357 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {365 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
358 ASSERT(eb->fs_info);366 ASSERT(fs_info);
359 /*367 /*
360 * Every shared one has parent tree block,368 * Every shared one has parent tree block,
361 * which must be aligned to sector size.369 * which must be aligned to sector size.
362 */370 */
363 if (offset &&371 if (offset && IS_ALIGNED(offset, fs_info->sectorsize))
364 IS_ALIGNED(offset, eb->fs_info->sectorsize))
365 return type;372 return type;
366 }373 }
367 } else if (is_data == BTRFS_REF_TYPE_DATA) {374 } else if (is_data == BTRFS_REF_TYPE_DATA) {
368 if (type == BTRFS_EXTENT_DATA_REF_KEY)375 if (type == BTRFS_EXTENT_DATA_REF_KEY)
369 return type;376 return type;
370 if (type == BTRFS_SHARED_DATA_REF_KEY) {377 if (type == BTRFS_SHARED_DATA_REF_KEY) {
371 ASSERT(eb->fs_info);378 ASSERT(fs_info);
372 /*379 /*
373 * Every shared one has parent tree block,380 * Every shared one has parent tree block,
374 * which must be aligned to sector size.381 * which must be aligned to sector size.
375 */382 */
376 if (offset &&383 if (offset &&
377 IS_ALIGNED(offset, eb->fs_info->sectorsize))384 IS_ALIGNED(offset, fs_info->sectorsize))
378 return type;385 return type;
379 }386 }
380 } else {387 } else {
@@ -385,7 +392,7 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, · 4 unmodified lines
385 392
386 WARN_ON(1);393 WARN_ON(1);
387 btrfs_print_leaf(eb);394 btrfs_print_leaf(eb);
388 btrfs_err(eb->fs_info,395 btrfs_err(fs_info,
389 "eb %llu iref 0x%lx invalid extent inline ref type %d",396 "eb %llu iref 0x%lx invalid extent inline ref type %d",
390 eb->start, (unsigned long)iref, type);397 eb->start, (unsigned long)iref, type);
391 398
@@ -399,11 +406,11 @@ u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) · 7 unmodified lines
399 __le64 lenum;406 __le64 lenum;
400 407
401 lenum = cpu_to_le64(root_objectid);408 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));
403 lenum = cpu_to_le64(owner);410 lenum = cpu_to_le64(owner);
404 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));411 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
405 lenum = cpu_to_le64(offset);412 lenum = cpu_to_le64(offset);
406 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));413 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
407 414
408 return ((u64)high_crc << 31) ^ (u64)low_crc;415 return ((u64)high_crc << 31) ^ (u64)low_crc;
409}416}
@@ -575,7 +582,7 @@ static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, · 165 unmodified lines
575 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);582 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
576 }583 }
577 }584 }
578 btrfs_mark_buffer_dirty(leaf);585 btrfs_mark_buffer_dirty(trans, leaf);
579 ret = 0;586 ret = 0;
580fail:587fail:
581 btrfs_release_path(path);588 btrfs_release_path(path);
@@ -623,7 +630,7 @@ static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, · 41 unmodified lines
623 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);630 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
624 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)631 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
625 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);632 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
626 btrfs_mark_buffer_dirty(leaf);633 btrfs_mark_buffer_dirty(trans, leaf);
627 }634 }
628 return ret;635 return ret;
629}636}
@@ -789,7 +796,6 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, · 159 unmodified lines
789 int type;796 int type;
790 int want;797 int want;
791 int ret;798 int ret;
792 int err = 0;
793 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);799 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
794 int needed;800 int needed;
795 801
@@ -816,10 +822,8 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, · 20 unmodified lines
816 822
817again:823again:
818 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);824 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
819 if (ret < 0) {825 if (ret < 0)
820 err = ret;
821 goto out;826 goto out;
822 }
823 827
824 /*828 /*
825 * We may be a newly converted file system which still has the old fat829 * We may be a newly converted file system which still has the old fat
@@ -846,7 +850,7 @@ again: · 20 unmodified lines
846 }850 }
847 851
848 if (ret && !insert) {852 if (ret && !insert) {
849 err = -ENOENT;853 ret = -ENOENT;
850 goto out;854 goto out;
851 } else if (WARN_ON(ret)) {855 } else if (WARN_ON(ret)) {
852 btrfs_print_leaf(path->nodes[0]);856 btrfs_print_leaf(path->nodes[0]);
@@ -854,18 +858,18 @@ again: · 1 unmodified line
854"extent item not found for insert, bytenr %llu num_bytes %llu parent %llu root_objectid %llu owner %llu offset %llu",858"extent item not found for insert, bytenr %llu num_bytes %llu parent %llu root_objectid %llu owner %llu offset %llu",
855 bytenr, num_bytes, parent, root_objectid, owner,859 bytenr, num_bytes, parent, root_objectid, owner,
856 offset);860 offset);
857 err = -EIO;861 ret = -EUCLEAN;
858 goto out;862 goto out;
859 }863 }
860 864
861 leaf = path->nodes[0];865 leaf = path->nodes[0];
862 item_size = btrfs_item_size(leaf, path->slots[0]);866 item_size = btrfs_item_size(leaf, path->slots[0]);
863 if (unlikely(item_size < sizeof(*ei))) {867 if (unlikely(item_size < sizeof(*ei))) {
864 err = -EUCLEAN;868 ret = -EUCLEAN;
865 btrfs_err(fs_info,869 btrfs_err(fs_info,
866 "unexpected extent item size, has %llu expect >= %zu",870 "unexpected extent item size, has %llu expect >= %zu",
867 item_size, sizeof(*ei));871 item_size, sizeof(*ei));
868 btrfs_abort_transaction(trans, err);872 btrfs_abort_transaction(trans, ret);
869 goto out;873 goto out;
870 }874 }
871 875
@@ -885,22 +889,17 @@ again: · 13 unmodified lines
885 else889 else
886 needed = BTRFS_REF_TYPE_BLOCK;890 needed = BTRFS_REF_TYPE_BLOCK;
887 891
888 err = -ENOENT;892 ret = -ENOENT;
889 while (1) {893 while (ptr < end) {
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 }
900 iref = (struct btrfs_extent_inline_ref *)ptr;894 iref = (struct btrfs_extent_inline_ref *)ptr;
901 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);895 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 }
902 if (type == BTRFS_REF_TYPE_INVALID) {901 if (type == BTRFS_REF_TYPE_INVALID) {
903 err = -EUCLEAN;902 ret = -EUCLEAN;
904 goto out;903 goto out;
905 }904 }
906 905
@@ -916,7 +915,7 @@ again: · 9 unmodified lines
916 dref = (struct btrfs_extent_data_ref *)(&iref->offset);915 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
917 if (match_extent_data_ref(leaf, dref, root_objectid,916 if (match_extent_data_ref(leaf, dref, root_objectid,
918 owner, offset)) {917 owner, offset)) {
919 err = 0;918 ret = 0;
920 break;919 break;
921 }920 }
922 if (hash_extent_data_ref_item(leaf, dref) <921 if (hash_extent_data_ref_item(leaf, dref) <
@@ -927,14 +926,14 @@ again: · 4 unmodified lines
927 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);926 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
928 if (parent > 0) {927 if (parent > 0) {
929 if (parent == ref_offset) {928 if (parent == ref_offset) {
930 err = 0;929 ret = 0;
931 break;930 break;
932 }931 }
933 if (ref_offset < parent)932 if (ref_offset < parent)
934 break;933 break;
935 } else {934 } else {
936 if (root_objectid == ref_offset) {935 if (root_objectid == ref_offset) {
937 err = 0;936 ret = 0;
938 break;937 break;
939 }938 }
940 if (ref_offset < root_objectid)939 if (ref_offset < root_objectid)
@@ -943,10 +942,20 @@ again: · 2 unmodified lines
943 }942 }
944 ptr += btrfs_extent_inline_ref_size(type);943 ptr += btrfs_extent_inline_ref_size(type);
945 }944 }
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) {
947 if (item_size + extra_size >=956 if (item_size + extra_size >=
948 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {957 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
949 err = -EAGAIN;958 ret = -EAGAIN;
950 goto out;959 goto out;
951 }960 }
952 /*961 /*
@@ -958,7 +967,7 @@ again: · 5 unmodified lines
958 if (find_next_key(path, 0, &key) == 0 &&967 if (find_next_key(path, 0, &key) == 0 &&
959 key.objectid == bytenr &&968 key.objectid == bytenr &&
960 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {969 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
961 err = -EAGAIN;970 ret = -EAGAIN;
962 goto out;971 goto out;
963 }972 }
964 }973 }
@@ -969,14 +978,14 @@ out: · 4 unmodified lines
969 path->search_for_extension = 0;978 path->search_for_extension = 0;
970 btrfs_unlock_up_safe(path, 1);979 btrfs_unlock_up_safe(path, 1);
971 }980 }
972 return err;981 return ret;
973}982}
974 983
975/*984/*
976 * helper to add new inline back ref985 * helper to add new inline back ref
977 */986 */
978static noinline_for_stack987static noinline_for_stack
979void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,988void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
980 struct btrfs_path *path,989 struct btrfs_path *path,
981 struct btrfs_extent_inline_ref *iref,990 struct btrfs_extent_inline_ref *iref,
982 u64 parent, u64 root_objectid,991 u64 parent, u64 root_objectid,
@@ -999,7 +1008,7 @@ void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, · 16 unmodified lines
999 type = extent_ref_type(parent, owner);1008 type = extent_ref_type(parent, owner);
1000 size = btrfs_extent_inline_ref_size(type);1009 size = btrfs_extent_inline_ref_size(type);
1001 1010
1002 btrfs_extend_item(path, size);1011 btrfs_extend_item(trans, path, size);
1003 1012
1004 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);1013 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1005 refs = btrfs_extent_refs(leaf, ei);1014 refs = btrfs_extent_refs(leaf, ei);
@@ -1033,7 +1042,7 @@ void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, · 27 unmodified lines
1033 } else {1042 } else {
1034 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);1043 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1035 }1044 }
1036 btrfs_mark_buffer_dirty(leaf);1045 btrfs_mark_buffer_dirty(trans, leaf);
1037}1046}
1038 1047
1039static int lookup_extent_backref(struct btrfs_trans_handle *trans,1048static 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
1066/*1075/*
1067 * helper to update/remove inline back ref1076 * helper to update/remove inline back ref
1068 */1077 */
1069static noinline_for_stack int update_inline_extent_backref(struct btrfs_path *path,1078static noinline_for_stack int update_inline_extent_backref(
1079 struct btrfs_trans_handle *trans,
1080 struct btrfs_path *path,
1070 struct btrfs_extent_inline_ref *iref,1081 struct btrfs_extent_inline_ref *iref,
1071 int refs_to_mod,1082 int refs_to_mod,
1072 struct btrfs_delayed_extent_op *extent_op)1083 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
1174 memmove_extent_buffer(leaf, ptr, ptr + size,1185 memmove_extent_buffer(leaf, ptr, ptr + size,
1175 end - ptr - size);1186 end - ptr - size);
1176 item_size -= size;1187 item_size -= size;
1177 btrfs_truncate_item(path, item_size, 1);1188 btrfs_truncate_item(trans, path, item_size, 1);
1178 }1189 }
1179 btrfs_mark_buffer_dirty(leaf);1190 btrfs_mark_buffer_dirty(trans, leaf);
1180 return 0;1191 return 0;
1181}1192}
1182 1193
@@ -1206,9 +1217,10 @@ int insert_inline_extent_backref(struct btrfs_trans_handle *trans, · 23 unmodified lines
1206 bytenr, num_bytes, root_objectid, path->slots[0]);1217 bytenr, num_bytes, root_objectid, path->slots[0]);
1207 return -EUCLEAN;1218 return -EUCLEAN;
1208 }1219 }
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);
1210 } else if (ret == -ENOENT) {1222 } else if (ret == -ENOENT) {
1211 setup_inline_extent_backref(trans->fs_info, path, iref, parent,1223 setup_inline_extent_backref(trans, path, iref, parent,
1212 root_objectid, owner, offset,1224 root_objectid, owner, offset,
1213 refs_to_add, extent_op);1225 refs_to_add, extent_op);
1214 ret = 0;1226 ret = 0;
@@ -1226,7 +1238,8 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans, · 11 unmodified lines
1226 1238
1227 BUG_ON(!is_data && refs_to_drop != 1);1239 BUG_ON(!is_data && refs_to_drop != 1);
1228 if (iref)1240 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);
1230 else if (is_data)1243 else if (is_data)
1231 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);1244 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1232 else1245 else
@@ -1422,7 +1435,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 189 unmodified lines
1422 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&1435 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
1423 generic_ref->action);1436 generic_ref->action);
1424 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&1437 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);
1426 1439
1427 if (generic_ref->type == BTRFS_REF_METADATA)1440 if (generic_ref->type == BTRFS_REF_METADATA)
1428 ret = btrfs_add_delayed_tree_ref(trans, generic_ref, NULL);1441 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
1435}1448}
1436 1449
1437/*1450/*
1438 * __btrfs_inc_extent_ref - insert backreference for a given extent1451 * Insert backreference for a given extent.
1439 *1452 *
1440 * The counterpart is in __btrfs_free_extent(), with examples and more details1453 * The counterpart is in __btrfs_free_extent(), with examples and more details
1441 * how it works.1454 * how it works.
@@ -1465,8 +1478,6 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 23 unmodified lines
1465 * always passed as 0. For data extents it is the fileoffset1478 * always passed as 0. For data extents it is the fileoffset
1466 * this extent belongs to.1479 * this extent belongs to.
1467 *1480 *
1468 * @refs_to_add Number of references to add
1469 *
1470 * @extent_op Pointer to a structure, holding information necessary when1481 * @extent_op Pointer to a structure, holding information necessary when
1471 * updating a tree block's flags1482 * updating a tree block's flags
1472 *1483 *
@@ -1474,7 +1485,7 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 1 unmodified line
1474static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,1485static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1475 struct btrfs_delayed_ref_node *node,1486 struct btrfs_delayed_ref_node *node,
1476 u64 parent, u64 root_objectid,1487 u64 parent, u64 root_objectid,
1477 u64 owner, u64 offset, int refs_to_add,1488 u64 owner, u64 offset,
1478 struct btrfs_delayed_extent_op *extent_op)1489 struct btrfs_delayed_extent_op *extent_op)
1479{1490{
1480 struct btrfs_path *path;1491 struct btrfs_path *path;
@@ -1484,6 +1495,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 3 unmodified lines
1484 u64 bytenr = node->bytenr;1495 u64 bytenr = node->bytenr;
1485 u64 num_bytes = node->num_bytes;1496 u64 num_bytes = node->num_bytes;
1486 u64 refs;1497 u64 refs;
1498 int refs_to_add = node->ref_mod;
1487 int ret;1499 int ret;
1488 1500
1489 path = btrfs_alloc_path();1501 path = btrfs_alloc_path();
@@ -1510,7 +1522,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, · 20 unmodified lines
1510 if (extent_op)1522 if (extent_op)
1511 __run_delayed_extent_op(extent_op, leaf, item);1523 __run_delayed_extent_op(extent_op, leaf, item);
1512 1524
1513 btrfs_mark_buffer_dirty(leaf);1525 btrfs_mark_buffer_dirty(trans, leaf);
1514 btrfs_release_path(path);1526 btrfs_release_path(path);
1515 1527
1516 /* now insert the actual backref */1528 /* now insert the actual backref */
@@ -1530,44 +1542,57 @@ out: · 13 unmodified lines
1530}1542}
1531 1543
1532static int run_delayed_data_ref(struct btrfs_trans_handle *trans,1544static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1545 struct btrfs_delayed_ref_head *href,
1533 struct btrfs_delayed_ref_node *node,1546 struct btrfs_delayed_ref_node *node,
1534 struct btrfs_delayed_extent_op *extent_op,1547 struct btrfs_delayed_extent_op *extent_op,
1535 bool insert_reserved)1548 bool insert_reserved)
1536{1549{
1537 int ret = 0;1550 int ret = 0;
1538 struct btrfs_delayed_data_ref *ref;1551 struct btrfs_delayed_data_ref *ref;
1539 struct btrfs_key ins;
1540 u64 parent = 0;1552 u64 parent = 0;
1541 u64 ref_root = 0;
1542 u64 flags = 0;1553 u64 flags = 0;
1543 1554
1544 ins.objectid = node->bytenr;
1545 ins.offset = node->num_bytes;
1546 ins.type = BTRFS_EXTENT_ITEM_KEY;
1547
1548 ref = btrfs_delayed_node_to_data_ref(node);1555 ref = btrfs_delayed_node_to_data_ref(node);
1549 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);1556 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
1550 1557
1551 if (node->type == BTRFS_SHARED_DATA_REF_KEY)1558 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1552 parent = ref->parent;1559 parent = ref->parent;
1553 ref_root = ref->root;
1554 1560
1555 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {1561 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
1556 if (extent_op)1572 if (extent_op)
1557 flags |= extent_op->flags_to_set;1573 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,
1559 flags, ref->objectid,1580 flags, ref->objectid,
1560 ref->offset, &ins,1581 ref->offset, &key,
1561 node->ref_mod);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);
1562 } else if (node->action == BTRFS_ADD_DELAYED_REF) {1588 } 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,
1564 ref->objectid, ref->offset,1590 ref->objectid, ref->offset,
1565 node->ref_mod, extent_op);1591 extent_op);
1566 } else if (node->action == BTRFS_DROP_DELAYED_REF) {1592 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1567 ret = __btrfs_free_extent(trans, node, parent,1593 ret = __btrfs_free_extent(trans, href, node, parent,
1568 ref_root, ref->objectid,1594 ref->root, ref->objectid,
1569 ref->offset, node->ref_mod,1595 ref->offset, extent_op);
1570 extent_op);
1571 } else {1596 } else {
1572 BUG();1597 BUG();
1573 }1598 }
@@ -1604,7 +1629,6 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans, · 30 unmodified lines
1604 struct extent_buffer *leaf;1629 struct extent_buffer *leaf;
1605 u32 item_size;1630 u32 item_size;
1606 int ret;1631 int ret;
1607 int err = 0;
1608 int metadata = 1;1632 int metadata = 1;
1609 1633
1610 if (TRANS_ABORTED(trans))1634 if (TRANS_ABORTED(trans))
@@ -1631,10 +1655,8 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans, · 20 unmodified lines
1631again:1655again:
1632 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);1656 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1633 if (ret < 0) {1657 if (ret < 0) {
1634 err = ret;
1635 goto out;1658 goto out;
1636 }1659 } else if (ret > 0) {
1637 if (ret > 0) {
1638 if (metadata) {1660 if (metadata) {
1639 if (path->slots[0] > 0) {1661 if (path->slots[0] > 0) {
1640 path->slots[0]--;1662 path->slots[0]--;
@@ -1655,7 +1677,7 @@ again: · 14 unmodified lines
1655 goto again;1677 goto again;
1656 }1678 }
1657 } else {1679 } else {
1658 err = -EUCLEAN;1680 ret = -EUCLEAN;
1659 btrfs_err(fs_info,1681 btrfs_err(fs_info,
1660 "missing extent item for extent %llu num_bytes %llu level %d",1682 "missing extent item for extent %llu num_bytes %llu level %d",
1661 head->bytenr, head->num_bytes, extent_op->level);1683 head->bytenr, head->num_bytes, extent_op->level);
@@ -1667,29 +1689,31 @@ again: · 5 unmodified lines
1667 item_size = btrfs_item_size(leaf, path->slots[0]);1689 item_size = btrfs_item_size(leaf, path->slots[0]);
1668 1690
1669 if (unlikely(item_size < sizeof(*ei))) {1691 if (unlikely(item_size < sizeof(*ei))) {
1670 err = -EUCLEAN;1692 ret = -EUCLEAN;
1671 btrfs_err(fs_info,1693 btrfs_err(fs_info,
1672 "unexpected extent item size, has %u expect >= %zu",1694 "unexpected extent item size, has %u expect >= %zu",
1673 item_size, sizeof(*ei));1695 item_size, sizeof(*ei));
1674 btrfs_abort_transaction(trans, err);1696 btrfs_abort_transaction(trans, ret);
1675 goto out;1697 goto out;
1676 }1698 }
1677 1699
1678 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);1700 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1679 __run_delayed_extent_op(extent_op, leaf, ei);1701 __run_delayed_extent_op(extent_op, leaf, ei);
1680 1702
1681 btrfs_mark_buffer_dirty(leaf);1703 btrfs_mark_buffer_dirty(trans, leaf);
1682out:1704out:
1683 btrfs_free_path(path);1705 btrfs_free_path(path);
1684 return err;1706 return ret;
1685}1707}
1686 1708
1687static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,1709static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1710 struct btrfs_delayed_ref_head *href,
1688 struct btrfs_delayed_ref_node *node,1711 struct btrfs_delayed_ref_node *node,
1689 struct btrfs_delayed_extent_op *extent_op,1712 struct btrfs_delayed_extent_op *extent_op,
1690 bool insert_reserved)1713 bool insert_reserved)
1691{1714{
1692 int ret = 0;1715 int ret = 0;
1716 struct btrfs_fs_info *fs_info = trans->fs_info;
1693 struct btrfs_delayed_tree_ref *ref;1717 struct btrfs_delayed_tree_ref *ref;
1694 u64 parent = 0;1718 u64 parent = 0;
1695 u64 ref_root = 0;1719 u64 ref_root = 0;
@@ -1709,14 +1733,25 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, · 13 unmodified lines
1709 return -EUCLEAN;1733 return -EUCLEAN;
1710 }1734 }
1711 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {1735 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
1712 BUG_ON(!extent_op || !extent_op->update_flags);1745 BUG_ON(!extent_op || !extent_op->update_flags);
1713 ret = alloc_reserved_tree_block(trans, node, extent_op);1746 ret = alloc_reserved_tree_block(trans, node, extent_op);
1747 if (!ret)
1748 btrfs_record_squota_delta(fs_info, &delta);
1714 } else if (node->action == BTRFS_ADD_DELAYED_REF) {1749 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1715 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,1750 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1716 ref->level, 0, 1, extent_op);1751 ref->level, 0, extent_op);
1717 } else if (node->action == BTRFS_DROP_DELAYED_REF) {1752 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1718 ret = __btrfs_free_extent(trans, node, parent, ref_root,1753 ret = __btrfs_free_extent(trans, href, node, parent, ref_root,
1719 ref->level, 0, 1, extent_op);1754 ref->level, 0, extent_op);
1720 } else {1755 } else {
1721 BUG();1756 BUG();
1722 }1757 }
@@ -1725,6 +1760,7 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, · 2 unmodified lines
1725 1760
1726/* helper function to actually process a single delayed ref entry */1761/* helper function to actually process a single delayed ref entry */
1727static int run_one_delayed_ref(struct btrfs_trans_handle *trans,1762static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1763 struct btrfs_delayed_ref_head *href,
1728 struct btrfs_delayed_ref_node *node,1764 struct btrfs_delayed_ref_node *node,
1729 struct btrfs_delayed_extent_op *extent_op,1765 struct btrfs_delayed_extent_op *extent_op,
1730 bool insert_reserved)1766 bool insert_reserved)
@@ -1739,12 +1775,14 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans, · 8 unmodified lines
1739 1775
1740 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||1776 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1741 node->type == BTRFS_SHARED_BLOCK_REF_KEY)1777 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,
1743 insert_reserved);1779 insert_reserved);
1744 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||1780 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1745 node->type == BTRFS_SHARED_DATA_REF_KEY)1781 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,
1747 insert_reserved);1783 insert_reserved);
1784 else if (node->type == BTRFS_EXTENT_OWNER_REF_KEY)
1785 ret = 0;
1748 else1786 else
1749 BUG();1787 BUG();
1750 if (ret && insert_reserved)1788 if (ret && insert_reserved)
@@ -1823,28 +1861,37 @@ static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans, · 72 unmodified lines
1823 return ret ? ret : 1;1861 return ret ? ret : 1;
1824}1862}
1825 1863
1826void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,1864u64 btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1827 struct btrfs_delayed_ref_root *delayed_refs,1865 struct btrfs_delayed_ref_root *delayed_refs,
1828 struct btrfs_delayed_ref_head *head)1866 struct btrfs_delayed_ref_head *head)
1829{1867{
1830 int nr_items = 1; /* Dropping this ref head update. */
1831
1832 /*1868 /*
1833 * We had csum deletions accounted for in our delayed refs rsv, we need1869 * We had csum deletions accounted for in our delayed refs rsv, we need
1834 * to drop the csum leaves for this update from our delayed_refs_rsv.1870 * to drop the csum leaves for this update from our delayed_refs_rsv.
1835 */1871 */
1836 if (head->total_ref_mod < 0 && head->is_data) {1872 if (head->total_ref_mod < 0 && head->is_data) {
1873 int nr_csums;
1874
1837 spin_lock(&delayed_refs->lock);1875 spin_lock(&delayed_refs->lock);
1838 delayed_refs->pending_csums -= head->num_bytes;1876 delayed_refs->pending_csums -= head->num_bytes;
1839 spin_unlock(&delayed_refs->lock);1877 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);
1841 }1883 }
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);
1842 1888
1843 btrfs_delayed_refs_rsv_release(fs_info, nr_items);1889 return 0;
1844}1890}
1845 1891
1846static int cleanup_ref_head(struct btrfs_trans_handle *trans,1892static 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)
1848{1895{
1849 1896
1850 struct btrfs_fs_info *fs_info = trans->fs_info;1897 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
1889 }1936 }
1890 }1937 }
1891 1938
1892 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);1939 *bytes_released += btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
1893 1940
1894 trace_run_delayed_ref_head(fs_info, head, 0);1941 trace_run_delayed_ref_head(fs_info, head, 0);
1895 btrfs_delayed_ref_unlock(head);1942 btrfs_delayed_ref_unlock(head);
@@ -1931,7 +1978,8 @@ static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head( · 35 unmodified lines
1931}1978}
1932 1979
1933static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,1980static 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)
1935{1983{
1936 struct btrfs_fs_info *fs_info = trans->fs_info;1984 struct btrfs_fs_info *fs_info = trans->fs_info;
1937 struct btrfs_delayed_ref_root *delayed_refs;1985 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
1985 locked_ref->extent_op = NULL;2033 locked_ref->extent_op = NULL;
1986 spin_unlock(&locked_ref->lock);2034 spin_unlock(&locked_ref->lock);
1987 2035
1988 ret = run_one_delayed_ref(trans, ref, extent_op,2036 ret = run_one_delayed_ref(trans, locked_ref, ref, extent_op,
1989 must_insert_reserved);2037 must_insert_reserved);
2038 btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
2039 *bytes_released += btrfs_calc_delayed_ref_bytes(fs_info, 1);
1990 2040
1991 btrfs_free_delayed_extent_op(extent_op);2041 btrfs_free_delayed_extent_op(extent_op);
1992 if (ret) {2042 if (ret) {
@@ -2010,15 +2060,22 @@ static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans, · 17 unmodified lines
2010 * Returns -ENOMEM or -EIO on failure and will abort the transaction.2060 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2011 */2061 */
2012static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,2062static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2013 unsigned long nr)2063 u64 min_bytes)
2014{2064{
2015 struct btrfs_fs_info *fs_info = trans->fs_info;2065 struct btrfs_fs_info *fs_info = trans->fs_info;
2016 struct btrfs_delayed_ref_root *delayed_refs;2066 struct btrfs_delayed_ref_root *delayed_refs;
2017 struct btrfs_delayed_ref_head *locked_ref = NULL;2067 struct btrfs_delayed_ref_head *locked_ref = NULL;
2018 int ret;2068 int ret;
2019 unsigned long count = 0;2069 unsigned long count = 0;
2070 unsigned long max_count = 0;
2071 u64 bytes_processed = 0;
2020 2072
2021 delayed_refs = &trans->transaction->delayed_refs;2073 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
2022 do {2079 do {
2023 if (!locked_ref) {2080 if (!locked_ref) {
2024 locked_ref = btrfs_obtain_ref_head(trans);2081 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
2046 spin_lock(&locked_ref->lock);2103 spin_lock(&locked_ref->lock);
2047 btrfs_merge_delayed_refs(fs_info, delayed_refs, locked_ref);2104 btrfs_merge_delayed_refs(fs_info, delayed_refs, locked_ref);
2048 2105
2049 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref);2106 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref, &bytes_processed);
2050 if (ret < 0 && ret != -EAGAIN) {2107 if (ret < 0 && ret != -EAGAIN) {
2051 /*2108 /*
2052 * Error, btrfs_run_delayed_refs_for_head already2109 * 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
2058 * Success, perform the usual cleanup of a processed2115 * Success, perform the usual cleanup of a processed
2059 * head2116 * head
2060 */2117 */
2061 ret = cleanup_ref_head(trans, locked_ref);2118 ret = cleanup_ref_head(trans, locked_ref, &bytes_processed);
2062 if (ret > 0 ) {2119 if (ret > 0 ) {
2063 /* We dropped our lock, we need to loop. */2120 /* We dropped our lock, we need to loop. */
2064 ret = 0;2121 ret = 0;
@@ -2075,7 +2132,9 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, · 10 unmodified lines
2075 2132
2076 locked_ref = NULL;2133 locked_ref = NULL;
2077 cond_resched();2134 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);
2079 2138
2080 return 0;2139 return 0;
2081}2140}
@@ -2124,24 +2183,25 @@ static u64 find_middle(struct rb_root *root) · 42 unmodified lines
2124#endif2183#endif
2125 2184
2126/*2185/*
2127 * this starts processing the delayed reference count updates and2186 * Start processing the delayed reference count updates and extent insertions
2128 * extent insertions we have queued up so far. count can be2187 * we have queued up so far.
2129 * 0, which means to process everything in the tree at the start2188 *
2130 * of the run (but not newly added entries), or it can be some target2189 * @trans: Transaction handle.
2131 * number you'd like to process.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.
2132 *2196 *
2133 * Returns 0 on success or if called with an aborted transaction2197 * Returns 0 on success or if called with an aborted transaction
2134 * Returns <0 on error and aborts the transaction2198 * Returns <0 on error and aborts the transaction
2135 */2199 */
2136int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,2200int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, u64 min_bytes)
2137 unsigned long count)
2138{2201{
2139 struct btrfs_fs_info *fs_info = trans->fs_info;2202 struct btrfs_fs_info *fs_info = trans->fs_info;
2140 struct rb_node *node;
2141 struct btrfs_delayed_ref_root *delayed_refs;2203 struct btrfs_delayed_ref_root *delayed_refs;
2142 struct btrfs_delayed_ref_head *head;
2143 int ret;2204 int ret;
2144 int run_all = count == (unsigned long)-1;
2145 2205
2146 /* We'll clean this up in btrfs_cleanup_transaction */2206 /* We'll clean this up in btrfs_cleanup_transaction */
2147 if (TRANS_ABORTED(trans))2207 if (TRANS_ABORTED(trans))
@@ -2151,42 +2211,30 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, · 3 unmodified lines
2151 return 0;2211 return 0;
2152 2212
2153 delayed_refs = &trans->transaction->delayed_refs;2213 delayed_refs = &trans->transaction->delayed_refs;
2154 if (count == 0)
2155 count = delayed_refs->num_heads_ready;
2156
2157again:2214again:
2158#ifdef SCRAMBLE_DELAYED_REFS2215#ifdef SCRAMBLE_DELAYED_REFS
2159 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);2216 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2160#endif2217#endif
2161 ret = __btrfs_run_delayed_refs(trans, count);2218 ret = __btrfs_run_delayed_refs(trans, min_bytes);
2162 if (ret < 0) {2219 if (ret < 0) {
2163 btrfs_abort_transaction(trans, ret);2220 btrfs_abort_transaction(trans, ret);
2164 return ret;2221 return ret;
2165 }2222 }
2166 2223
2167 if (run_all) {2224 if (min_bytes == U64_MAX) {
2168 btrfs_create_pending_block_groups(trans);2225 btrfs_create_pending_block_groups(trans);
2169 2226
2170 spin_lock(&delayed_refs->lock);2227 spin_lock(&delayed_refs->lock);
2171 node = rb_first_cached(&delayed_refs->href_root);2228 if (RB_EMPTY_ROOT(&delayed_refs->href_root.rb_root)) {
2172 if (!node) {
2173 spin_unlock(&delayed_refs->lock);2229 spin_unlock(&delayed_refs->lock);
2174 goto out;2230 return 0;
2175 }2231 }
2176 head = rb_entry(node, struct btrfs_delayed_ref_head,
2177 href_node);
2178 refcount_inc(&head->refs);
2179 spin_unlock(&delayed_refs->lock);2232 spin_unlock(&delayed_refs->lock);
2180 2233
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);
2186 cond_resched();2234 cond_resched();
2187 goto again;2235 goto again;
2188 }2236 }
2189out:2237
2190 return 0;2238 return 0;
2191}2239}
2192 2240
@@ -2311,6 +2359,7 @@ static noinline int check_committed_ref(struct btrfs_root *root, · 118 unmodified lines
2311 struct btrfs_extent_item *ei;2359 struct btrfs_extent_item *ei;
2312 struct btrfs_key key;2360 struct btrfs_key key;
2313 u32 item_size;2361 u32 item_size;
2362 u32 expected_size;
2314 int type;2363 int type;
2315 int ret;2364 int ret;
2316 2365
@@ -2337,10 +2386,22 @@ static noinline int check_committed_ref(struct btrfs_root *root, · 20 unmodified lines
2337 ret = 1;2386 ret = 1;
2338 item_size = btrfs_item_size(leaf, path->slots[0]);2387 item_size = btrfs_item_size(leaf, path->slots[0]);
2339 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);2388 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 }
2340 2402
2341 /* If extent item has more than 1 inline ref then it's shared */2403 /* If extent item has more than 1 inline ref then it's shared */
2342 if (item_size != sizeof(*ei) +2404 if (item_size != expected_size)
2343 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2344 goto out;2405 goto out;
2345 2406
2346 /*2407 /*
@@ -2352,8 +2413,6 @@ static noinline int check_committed_ref(struct btrfs_root *root, · 5 unmodified lines
2352 btrfs_root_last_snapshot(&root->root_item)))2413 btrfs_root_last_snapshot(&root->root_item)))
2353 goto out;2414 goto out;
2354 2415
2355 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2356
2357 /* If this extent has SHARED_DATA_REF then it's shared */2416 /* If this extent has SHARED_DATA_REF then it's shared */
2358 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);2417 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2359 if (type != BTRFS_EXTENT_DATA_REF_KEY)2418 if (type != BTRFS_EXTENT_DATA_REF_KEY)
@@ -2450,7 +2509,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, · 90 unmodified lines
2450 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);2509 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2451 key.offset -= btrfs_file_extent_offset(buf, fi);2510 key.offset -= btrfs_file_extent_offset(buf, fi);
2452 btrfs_init_generic_ref(&generic_ref, action, bytenr,2511 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2453 num_bytes, parent);2512 num_bytes, parent, ref_root);
2454 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,2513 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
2455 key.offset, root->root_key.objectid,2514 key.offset, root->root_key.objectid,
2456 for_reloc);2515 for_reloc);
@@ -2463,8 +2522,9 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, · 6 unmodified lines
2463 } else {2522 } else {
2464 bytenr = btrfs_node_blockptr(buf, i);2523 bytenr = btrfs_node_blockptr(buf, i);
2465 num_bytes = fs_info->nodesize;2524 num_bytes = fs_info->nodesize;
2525 /* We don't know the owning_root, use 0. */
2466 btrfs_init_generic_ref(&generic_ref, action, bytenr,2526 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2467 num_bytes, parent);2527 num_bytes, parent, 0);
2468 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root,2528 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root,
2469 root->root_key.objectid, for_reloc);2529 root->root_key.objectid, for_reloc);
2470 if (inc)2530 if (inc)
@@ -2565,16 +2625,13 @@ int btrfs_pin_extent(struct btrfs_trans_handle *trans, · 94 unmodified lines
2565 return 0;2625 return 0;
2566}2626}
2567 2627
2568/*
2569 * this function must be called within transaction
2570 */
2571int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,2628int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
2572 u64 bytenr, u64 num_bytes)2629 const struct extent_buffer *eb)
2573{2630{
2574 struct btrfs_block_group *cache;2631 struct btrfs_block_group *cache;
2575 int ret;2632 int ret;
2576 2633
2577 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);2634 cache = btrfs_lookup_block_group(trans->fs_info, eb->start);
2578 if (!cache)2635 if (!cache)
2579 return -EINVAL;2636 return -EINVAL;
2580 2637
@@ -2586,10 +2643,10 @@ int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, · 5 unmodified lines
2586 if (ret)2643 if (ret)
2587 goto out;2644 goto out;
2588 2645
2589 pin_down_extent(trans, cache, bytenr, num_bytes, 0);2646 pin_down_extent(trans, cache, eb->start, eb->len, 0);
2590 2647
2591 /* remove us from the free space cache (if we're there at all) */2648 /* 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);
2593out:2650out:
2594 btrfs_put_block_group(cache);2651 btrfs_put_block_group(cache);
2595 return ret;2652 return ret;
@@ -2844,12 +2901,61 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) · 248 unmodified lines
2844 return 0;2901 return 0;
2845}2902}
2846 2903
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 */
2918u64 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
2847static int do_free_extent_accounting(struct btrfs_trans_handle *trans,2952static 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)
2849{2954{
2850 int ret;2955 int ret;
2956 u64 num_bytes = delta->num_bytes;
2851 2957
2852 if (is_data) {2958 if (delta->is_data) {
2853 struct btrfs_root *csum_root;2959 struct btrfs_root *csum_root;
2854 2960
2855 csum_root = btrfs_csum_root(trans->fs_info, bytenr);2961 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
2858 btrfs_abort_transaction(trans, ret);2964 btrfs_abort_transaction(trans, ret);
2859 return ret;2965 return ret;
2860 }2966 }
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;
2861 }2979 }
2862 2980
2863 ret = add_to_free_space_tree(trans, bytenr, num_bytes);2981 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
2940 * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed.3058 * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed.
2941 */3059 */
2942static int __btrfs_free_extent(struct btrfs_trans_handle *trans,3060static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3061 struct btrfs_delayed_ref_head *href,
2943 struct btrfs_delayed_ref_node *node, u64 parent,3062 struct btrfs_delayed_ref_node *node, u64 parent,
2944 u64 root_objectid, u64 owner_objectid,3063 u64 root_objectid, u64 owner_objectid,
2945 u64 owner_offset, int refs_to_drop,3064 u64 owner_offset,
2946 struct btrfs_delayed_extent_op *extent_op)3065 struct btrfs_delayed_extent_op *extent_op)
2947{3066{
2948 struct btrfs_fs_info *info = trans->fs_info;3067 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
2957 int extent_slot = 0;3076 int extent_slot = 0;
2958 int found_extent = 0;3077 int found_extent = 0;
2959 int num_to_del = 1;3078 int num_to_del = 1;
3079 int refs_to_drop = node->ref_mod;
2960 u32 item_size;3080 u32 item_size;
2961 u64 refs;3081 u64 refs;
2962 u64 bytenr = node->bytenr;3082 u64 bytenr = node->bytenr;
2963 u64 num_bytes = node->num_bytes;3083 u64 num_bytes = node->num_bytes;
2964 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);3084 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
3085 u64 delayed_ref_root = href->owning_root;
2965 3086
2966 extent_root = btrfs_extent_root(info, bytenr);3087 extent_root = btrfs_extent_root(info, bytenr);
2967 ASSERT(extent_root);3088 ASSERT(extent_root);
@@ -3151,7 +3272,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, · 183 unmodified lines
3151 }3272 }
3152 } else {3273 } else {
3153 btrfs_set_extent_refs(leaf, ei, refs);3274 btrfs_set_extent_refs(leaf, ei, refs);
3154 btrfs_mark_buffer_dirty(leaf);3275 btrfs_mark_buffer_dirty(trans, leaf);
3155 }3276 }
3156 if (found_extent) {3277 if (found_extent) {
3157 ret = remove_extent_backref(trans, extent_root, path,3278 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
3162 }3283 }
3163 }3284 }
3164 } else {3285 } 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
3165 /* In this branch refs == 1 */3295 /* In this branch refs == 1 */
3166 if (found_extent) {3296 if (found_extent) {
3167 if (is_data && refs_to_drop !=3297 if (is_data && refs_to_drop !=
@@ -3200,6 +3330,16 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, · 32 unmodified lines
3200 num_to_del = 2;3330 num_to_del = 2;
3201 }3331 }
3202 }3332 }
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);
3203 3343
3204 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],3344 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3205 num_to_del);3345 num_to_del);
@@ -3209,7 +3349,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, · 3 unmodified lines
3209 }3349 }
3210 btrfs_release_path(path);3350 btrfs_release_path(path);
3211 3351
3212 ret = do_free_extent_accounting(trans, bytenr, num_bytes, is_data);3352 ret = do_free_extent_accounting(trans, bytenr, &delta);
3213 }3353 }
3214 btrfs_release_path(path);3354 btrfs_release_path(path);
3215 3355
@@ -3283,7 +3423,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans, · 67 unmodified lines
3283 int ret;3423 int ret;
3284 3424
3285 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,3425 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));
3287 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),3427 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
3288 root_id, 0, false);3428 root_id, 0, false);
3289 3429
@@ -3370,10 +3510,9 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref) · 80 unmodified lines
3370 * tree, just update pinning info and exit early.3510 * tree, just update pinning info and exit early.
3371 */3511 */
3372 if ((ref->type == BTRFS_REF_METADATA &&3512 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) ||
3374 (ref->type == BTRFS_REF_DATA &&3514 (ref->type == BTRFS_REF_DATA &&
3375 ref->data_ref.owning_root == BTRFS_TREE_LOG_OBJECTID)) {3515 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
3376 /* unlocks the pinned mutex */
3377 btrfs_pin_extent(trans, ref->bytenr, ref->len, 1);3516 btrfs_pin_extent(trans, ref->bytenr, ref->len, 1);
3378 ret = 0;3517 ret = 0;
3379 } else if (ref->type == BTRFS_REF_METADATA) {3518 } 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
3383 }3522 }
3384 3523
3385 if (!((ref->type == BTRFS_REF_METADATA &&3524 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) ||
3387 (ref->type == BTRFS_REF_DATA &&3526 (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)))
3389 btrfs_ref_tree_mod(fs_info, ref);3528 btrfs_ref_tree_mod(fs_info, ref);
3390 3529
3391 return ret;3530 return ret;
@@ -4442,8 +4581,8 @@ loop: · 1050 unmodified lines
4442}4581}
4443 4582
4444/*4583/*
4445 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a4584 * Entry point to the extent allocator. Tries to find a hole that is at least
4446 * hole that is at least as big as @num_bytes.4585 * as big as @num_bytes.
4447 *4586 *
4448 * @root - The root that will contain this extent4587 * @root - The root that will contain this extent
4449 *4588 *
@@ -4562,20 +4701,20 @@ int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, · 112 unmodified lines
4562 return 0;4701 return 0;
4563}4702}
4564 4703
4565int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,4704int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans,
4566 u64 len)4705 const struct extent_buffer *eb)
4567{4706{
4568 struct btrfs_block_group *cache;4707 struct btrfs_block_group *cache;
4569 int ret = 0;4708 int ret = 0;
4570 4709
4571 cache = btrfs_lookup_block_group(trans->fs_info, start);4710 cache = btrfs_lookup_block_group(trans->fs_info, eb->start);
4572 if (!cache) {4711 if (!cache) {
4573 btrfs_err(trans->fs_info, "unable to find block group for %llu",4712 btrfs_err(trans->fs_info, "unable to find block group for %llu",
4574 start);4713 eb->start);
4575 return -ENOSPC;4714 return -ENOSPC;
4576 }4715 }
4577 4716
4578 ret = pin_down_extent(trans, cache, start, len, 1);4717 ret = pin_down_extent(trans, cache, eb->start, eb->len, 1);
4579 btrfs_put_block_group(cache);4718 btrfs_put_block_group(cache);
4580 return ret;4719 return ret;
4581}4720}
@@ -4605,24 +4744,29 @@ static int alloc_reserved_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 23 unmodified lines
4605static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,4744static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4606 u64 parent, u64 root_objectid,4745 u64 parent, u64 root_objectid,
4607 u64 flags, u64 owner, u64 offset,4746 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)
4609{4748{
4610 struct btrfs_fs_info *fs_info = trans->fs_info;4749 struct btrfs_fs_info *fs_info = trans->fs_info;
4611 struct btrfs_root *extent_root;4750 struct btrfs_root *extent_root;
4612 int ret;4751 int ret;
4613 struct btrfs_extent_item *extent_item;4752 struct btrfs_extent_item *extent_item;
4753 struct btrfs_extent_owner_ref *oref;
4614 struct btrfs_extent_inline_ref *iref;4754 struct btrfs_extent_inline_ref *iref;
4615 struct btrfs_path *path;4755 struct btrfs_path *path;
4616 struct extent_buffer *leaf;4756 struct extent_buffer *leaf;
4617 int type;4757 int type;
4618 u32 size;4758 u32 size;
4759 const bool simple_quota = (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE);
4619 4760
4620 if (parent > 0)4761 if (parent > 0)
4621 type = BTRFS_SHARED_DATA_REF_KEY;4762 type = BTRFS_SHARED_DATA_REF_KEY;
4622 else4763 else
4623 type = BTRFS_EXTENT_DATA_REF_KEY;4764 type = BTRFS_EXTENT_DATA_REF_KEY;
4624 4765
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);
4626 4770
4627 path = btrfs_alloc_path();4771 path = btrfs_alloc_path();
4628 if (!path)4772 if (!path)
@@ -4644,7 +4788,14 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, · 15 unmodified lines
4644 flags | BTRFS_EXTENT_FLAG_DATA);4788 flags | BTRFS_EXTENT_FLAG_DATA);
4645 4789
4646 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);4790 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 }
4647 btrfs_set_extent_inline_ref_type(leaf, iref, type);4797 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4798
4648 if (parent > 0) {4799 if (parent > 0) {
4649 struct btrfs_shared_data_ref *ref;4800 struct btrfs_shared_data_ref *ref;
4650 ref = (struct btrfs_shared_data_ref *)(iref + 1);4801 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
4659 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);4810 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4660 }4811 }
4661 4812
4662 btrfs_mark_buffer_dirty(path->nodes[0]);4813 btrfs_mark_buffer_dirty(trans, path->nodes[0]);
4663 btrfs_free_path(path);4814 btrfs_free_path(path);
4664 4815
4665 return alloc_reserved_extent(trans, ins->objectid, ins->offset);4816 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
4734 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);4885 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
4735 }4886 }
4736 4887
4737 btrfs_mark_buffer_dirty(leaf);4888 btrfs_mark_buffer_dirty(trans, leaf);
4738 btrfs_free_path(path);4889 btrfs_free_path(path);
4739 4890
4740 return alloc_reserved_extent(trans, node->bytenr, fs_info->nodesize);4891 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
4746 struct btrfs_key *ins)4897 struct btrfs_key *ins)
4747{4898{
4748 struct btrfs_ref generic_ref = { 0 };4899 struct btrfs_ref generic_ref = { 0 };
4900 u64 root_objectid = root->root_key.objectid;
4901 u64 owning_root = root_objectid;
4749 4902
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;
4751 4907
4752 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,4908 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4753 ins->objectid, ins->offset, 0);4909 ins->objectid, ins->offset, 0, owning_root);
4754 btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner,4910 btrfs_init_data_ref(&generic_ref, root_objectid, owner,
4755 offset, 0, false);4911 offset, 0, false);
4756 btrfs_ref_tree_mod(root->fs_info, &generic_ref);4912 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
4757 4913
@@ -4771,6 +4927,14 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, · 13 unmodified lines
4771 int ret;4927 int ret;
4772 struct btrfs_block_group *block_group;4928 struct btrfs_block_group *block_group;
4773 struct btrfs_space_info *space_info;4929 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 };
4774 4938
4775 /*4939 /*
4776 * Mixed block groups will exclude before processing the log so we only4940 * 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
4796 spin_unlock(&space_info->lock);4960 spin_unlock(&space_info->lock);
4797 4961
4798 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,4962 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4799 offset, ins, 1);4963 offset, ins, 1, root_objectid);
4800 if (ret)4964 if (ret)
4801 btrfs_pin_extent(trans, ins->objectid, ins->offset, 1);4965 btrfs_pin_extent(trans, ins->objectid, ins->offset, 1);
4966 ret = btrfs_record_squota_delta(fs_info, &delta);
4802 btrfs_put_block_group(block_group);4967 btrfs_put_block_group(block_group);
4803 return ret;4968 return ret;
4804}4969}
4805 4970
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 */
4976static 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
4987static bool check_eb_lock_owner(struct extent_buffer *eb)
4988{
4989 return false;
4990}
4991#endif
4992
4806static struct extent_buffer *4993static struct extent_buffer *
4807btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,4994btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4808 u64 bytenr, int level, u64 owner,4995 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
4816 if (IS_ERR(buf))5003 if (IS_ERR(buf))
4817 return buf;5004 return buf;
4818 5005
4819 /*5006 if (check_eb_lock_owner(buf)) {
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);
4828 free_extent_buffer(buf);5007 free_extent_buffer(buf);
4829 return ERR_PTR(-EUCLEAN);5008 return ERR_PTR(-EUCLEAN);
4830 }5009 }
@@ -4901,6 +5080,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, · 70 unmodified lines
4901 const struct btrfs_disk_key *key,5080 const struct btrfs_disk_key *key,
4902 int level, u64 hint,5081 int level, u64 hint,
4903 u64 empty_size,5082 u64 empty_size,
5083 u64 reloc_src_root,
4904 enum btrfs_lock_nesting nest)5084 enum btrfs_lock_nesting nest)
4905{5085{
4906 struct btrfs_fs_info *fs_info = root->fs_info;5086 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
4913 int ret;5093 int ret;
4914 u32 blocksize = fs_info->nodesize;5094 u32 blocksize = fs_info->nodesize;
4915 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);5095 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
5096 u64 owning_root;
4916 5097
4917#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS5098#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4918 if (btrfs_is_testing(fs_info)) {5099 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
4939 ret = PTR_ERR(buf);5120 ret = PTR_ERR(buf);
4940 goto out_free_reserved;5121 goto out_free_reserved;
4941 }5122 }
5123 owning_root = btrfs_header_owner(buf);
4942 5124
4943 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {5125 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4944 if (parent == 0)5126 if (parent == 0)
4945 parent = ins.objectid;5127 parent = ins.objectid;
4946 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;5128 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5129 owning_root = reloc_src_root;
4947 } else5130 } else
4948 BUG_ON(parent > 0);5131 BUG_ON(parent > 0);
4949 5132
@@ -4963,7 +5146,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, · 13 unmodified lines
4963 extent_op->level = level;5146 extent_op->level = level;
4964 5147
4965 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,5148 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4966 ins.objectid, ins.offset, parent);5149 ins.objectid, ins.offset, parent, owning_root);
4967 btrfs_init_tree_ref(&generic_ref, level, root_objectid,5150 btrfs_init_tree_ref(&generic_ref, level, root_objectid,
4968 root->root_key.objectid, false);5151 root->root_key.objectid, false);
4969 btrfs_ref_tree_mod(fs_info, &generic_ref);5152 btrfs_ref_tree_mod(fs_info, &generic_ref);
@@ -5384,7 +5567,8 @@ skip: · 414 unmodified lines
5384 find_next_key(path, level, &wc->drop_progress);5567 find_next_key(path, level, &wc->drop_progress);
5385 5568
5386 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,5569 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));
5388 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid,5572 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid,
5389 0, false);5573 0, false);
5390 ret = btrfs_free_extent(trans, &ref);5574 ret = btrfs_free_extent(trans, &ref);
fs/btrfs/extent-tree.h+10−5
@@ -7,6 +7,7 @@ · 6 unmodified lines
7#include "block-group.h"7#include "block-group.h"
8 8
9struct btrfs_free_cluster;9struct btrfs_free_cluster;
10struct btrfs_delayed_ref_head;
10 11
11enum btrfs_extent_allocation_policy {12enum btrfs_extent_allocation_policy {
12 BTRFS_EXTENT_ALLOC_CLUSTERED,13 BTRFS_EXTENT_ALLOC_CLUSTERED,
@@ -91,8 +92,8 @@ int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, · 78 unmodified lines
91 enum btrfs_inline_ref_type is_data);92 enum btrfs_inline_ref_type is_data);
92u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);93u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);
93 94
94int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, unsigned long count);95int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, u64 min_bytes);
95void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,96u64 btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
96 struct btrfs_delayed_ref_root *delayed_refs,97 struct btrfs_delayed_ref_root *delayed_refs,
97 struct btrfs_delayed_ref_head *head);98 struct btrfs_delayed_ref_head *head);
98int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);99int 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
102int btrfs_pin_extent(struct btrfs_trans_handle *trans, u64 bytenr, u64 num,103int btrfs_pin_extent(struct btrfs_trans_handle *trans, u64 bytenr, u64 num,
103 int reserved);104 int reserved);
104int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,105int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
105 u64 bytenr, u64 num_bytes);106 const struct extent_buffer *eb);
106int btrfs_exclude_logged_extents(struct extent_buffer *eb);107int btrfs_exclude_logged_extents(struct extent_buffer *eb);
107int btrfs_cross_ref_exist(struct btrfs_root *root,108int btrfs_cross_ref_exist(struct btrfs_root *root,
108 u64 objectid, u64 offset, u64 bytenr, bool strict,109 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
113 const struct btrfs_disk_key *key,114 const struct btrfs_disk_key *key,
114 int level, u64 hint,115 int level, u64 hint,
115 u64 empty_size,116 u64 empty_size,
117 u64 reloc_src_root,
116 enum btrfs_lock_nesting nest);118 enum btrfs_lock_nesting nest);
117void btrfs_free_tree_block(struct btrfs_trans_handle *trans,119void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
118 u64 root_id,120 u64 root_id,
@@ -136,12 +138,15 @@ int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, · 17 unmodified lines
136 struct extent_buffer *eb, u64 flags);138 struct extent_buffer *eb, u64 flags);
137int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);139int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
138 140
141u64 btrfs_get_extent_owner_root(struct btrfs_fs_info *fs_info,
142 struct extent_buffer *leaf, int slot);
139int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,143int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
140 u64 start, u64 len, int delalloc);144 u64 start, u64 len, int delalloc);
141int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start, u64 len);145int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans,
146 const struct extent_buffer *eb);
142int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);147int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);
143int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, struct btrfs_ref *generic_ref);148int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, struct btrfs_ref *generic_ref);
144int __must_check btrfs_drop_snapshot(struct btrfs_root *root, int update_ref,149int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref,
145 int for_reloc);150 int for_reloc);
146int btrfs_drop_subtree(struct btrfs_trans_handle *trans,151int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
147 struct btrfs_root *root,152 struct btrfs_root *root,
fs/btrfs/extent_io.c+23−16
@@ -21,7 +21,6 @@ · 20 unmodified lines
21#include "ctree.h"21#include "ctree.h"
22#include "btrfs_inode.h"22#include "btrfs_inode.h"
23#include "bio.h"23#include "bio.h"
24#include "check-integrity.h"
25#include "locking.h"24#include "locking.h"
26#include "rcu-string.h"25#include "rcu-string.h"
27#include "backref.h"26#include "backref.h"
@@ -395,7 +394,7 @@ again: · 367 unmodified lines
395 394
396 /* then test to make sure it is all still delalloc */395 /* then test to make sure it is all still delalloc */
397 ret = test_range_bit(tree, delalloc_start, delalloc_end,396 ret = test_range_bit(tree, delalloc_start, delalloc_end,
398 EXTENT_DELALLOC, 1, cached_state);397 EXTENT_DELALLOC, cached_state);
399 if (!ret) {398 if (!ret) {
400 unlock_extent(tree, delalloc_start, delalloc_end,399 unlock_extent(tree, delalloc_start, delalloc_end,
401 &cached_state);400 &cached_state);
@@ -2294,7 +2293,7 @@ static int try_release_extent_state(struct extent_io_tree *tree, · 1892 unmodified lines
2294 u64 end = start + PAGE_SIZE - 1;2293 u64 end = start + PAGE_SIZE - 1;
2295 int ret = 1;2294 int ret = 1;
2296 2295
2297 if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) {2296 if (test_range_bit_exists(tree, start, end, EXTENT_LOCKED)) {
2298 ret = 0;2297 ret = 0;
2299 } else {2298 } else {
2300 u32 clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM |2299 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
2353 free_extent_map(em);2352 free_extent_map(em);
2354 break;2353 break;
2355 }2354 }
2356 if (test_range_bit(tree, em->start,2355 if (test_range_bit_exists(tree, em->start,
2357 extent_map_end(em) - 1,2356 extent_map_end(em) - 1,
2358 EXTENT_LOCKED, 0, NULL))2357 EXTENT_LOCKED))
2359 goto next;2358 goto next;
2360 /*2359 /*
2361 * If it's not in the list of modified extents, used2360 * 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
3455 start, fs_info->nodesize);3454 start, fs_info->nodesize);
3456 return -EINVAL;3455 return -EINVAL;
3457 }3456 }
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 }
3458 return 0;3463 return 0;
3459}3464}
3460 3465
@@ -4248,14 +4253,14 @@ void copy_extent_buffer(const struct extent_buffer *dst, · 787 unmodified lines
4248}4253}
4249 4254
4250/*4255/*
4251 * eb_bitmap_offset() - calculate the page and offset of the byte containing the4256 * Calculate the page and offset of the byte containing the given bit number.
4252 * given bit number4257 *
4253 * @eb: the extent buffer4258 * @eb: the extent buffer
4254 * @start: offset of the bitmap item in the extent buffer4259 * @start: offset of the bitmap item in the extent buffer
4255 * @nr: bit number4260 * @nr: bit number
4256 * @page_index: return index of the page in the extent buffer that contains the4261 * @page_index: return index of the page in the extent buffer that contains
4257 * given bit number4262 * the given bit number
4258 * @page_offset: return offset into the page given by page_index4263 * @page_offset: return offset into the page given by page_index
4259 *4264 *
4260 * This helper hides the ugliness of finding the byte in an extent buffer which4265 * This helper hides the ugliness of finding the byte in an extent buffer which
4261 * contains a given bit.4266 * contains a given bit.
@@ -4614,7 +4619,8 @@ int try_release_extent_buffer(struct page *page) · 352 unmodified lines
4614}4619}
4615 4620
4616/*4621/*
4617 * btrfs_readahead_tree_block - attempt to readahead a child block4622 * Attempt to readahead a child block.
4623 *
4618 * @fs_info: the fs_info4624 * @fs_info: the fs_info
4619 * @bytenr: bytenr to read4625 * @bytenr: bytenr to read
4620 * @owner_root: objectid of the root that owns this eb4626 * @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
4653}4659}
4654 4660
4655/*4661/*
4656 * btrfs_readahead_node_child - readahead a node's child block4662 * Readahead a node's child block.
4663 *
4657 * @node: parent node we're reading from4664 * @node: parent node we're reading from
4658 * @slot: slot in the parent node for the child we want to read4665 * @slot: slot in the parent node for the child we want to read
4659 *4666 *
fs/btrfs/extent_io.h+2−2
@@ -80,16 +80,16 @@ struct extent_buffer { · 79 unmodified lines
80 spinlock_t refs_lock;80 spinlock_t refs_lock;
81 atomic_t refs;81 atomic_t refs;
82 int read_mirror;82 int read_mirror;
83 struct rcu_head rcu_head;
84 pid_t lock_owner;
85 /* >= 0 if eb belongs to a log tree, -1 otherwise */83 /* >= 0 if eb belongs to a log tree, -1 otherwise */
86 s8 log_index;84 s8 log_index;
85 struct rcu_head rcu_head;
87 86
88 struct rw_semaphore lock;87 struct rw_semaphore lock;
89 88
90 struct page *pages[INLINE_EXTENT_BUFFER_PAGES];89 struct page *pages[INLINE_EXTENT_BUFFER_PAGES];
91#ifdef CONFIG_BTRFS_DEBUG90#ifdef CONFIG_BTRFS_DEBUG
92 struct list_head leak_list;91 struct list_head leak_list;
92 pid_t lock_owner;
93#endif93#endif
94};94};
95 95
fs/btrfs/file-item.c+9−8
@@ -194,7 +194,7 @@ int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans, · 193 unmodified lines
194 btrfs_set_file_extent_encryption(leaf, item, 0);194 btrfs_set_file_extent_encryption(leaf, item, 0);
195 btrfs_set_file_extent_other_encoding(leaf, item, 0);195 btrfs_set_file_extent_other_encoding(leaf, item, 0);
196 196
197 btrfs_mark_buffer_dirty(leaf);197 btrfs_mark_buffer_dirty(trans, leaf);
198out:198out:
199 btrfs_free_path(path);199 btrfs_free_path(path);
200 return ret;200 return ret;
@@ -811,11 +811,12 @@ blk_status_t btrfs_alloc_dummy_sum(struct btrfs_bio *bbio) · 610 unmodified lines
811 * This calls btrfs_truncate_item with the correct args based on the overlap,811 * This calls btrfs_truncate_item with the correct args based on the overlap,
812 * and fixes up the key as required.812 * and fixes up the key as required.
813 */813 */
814static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info,814static noinline void truncate_one_csum(struct btrfs_trans_handle *trans,
815 struct btrfs_path *path,815 struct btrfs_path *path,
816 struct btrfs_key *key,816 struct btrfs_key *key,
817 u64 bytenr, u64 len)817 u64 bytenr, u64 len)
818{818{
819 struct btrfs_fs_info *fs_info = trans->fs_info;
819 struct extent_buffer *leaf;820 struct extent_buffer *leaf;
820 const u32 csum_size = fs_info->csum_size;821 const u32 csum_size = fs_info->csum_size;
821 u64 csum_end;822 u64 csum_end;
@@ -836,7 +837,7 @@ static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, · 14 unmodified lines
836 */837 */
837 u32 new_size = (bytenr - key->offset) >> blocksize_bits;838 u32 new_size = (bytenr - key->offset) >> blocksize_bits;
838 new_size *= csum_size;839 new_size *= csum_size;
839 btrfs_truncate_item(path, new_size, 1);840 btrfs_truncate_item(trans, path, new_size, 1);
840 } else if (key->offset >= bytenr && csum_end > end_byte &&841 } else if (key->offset >= bytenr && csum_end > end_byte &&
841 end_byte > key->offset) {842 end_byte > key->offset) {
842 /*843 /*
@@ -848,10 +849,10 @@ static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, · 5 unmodified lines
848 u32 new_size = (csum_end - end_byte) >> blocksize_bits;849 u32 new_size = (csum_end - end_byte) >> blocksize_bits;
849 new_size *= csum_size;850 new_size *= csum_size;
850 851
851 btrfs_truncate_item(path, new_size, 0);852 btrfs_truncate_item(trans, path, new_size, 0);
852 853
853 key->offset = end_byte;854 key->offset = end_byte;
854 btrfs_set_item_key_safe(fs_info, path, key);855 btrfs_set_item_key_safe(trans, path, key);
855 } else {856 } else {
856 BUG();857 BUG();
857 }858 }
@@ -994,7 +995,7 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, · 136 unmodified lines
994 995
995 key.offset = end_byte - 1;996 key.offset = end_byte - 1;
996 } else {997 } else {
997 truncate_one_csum(fs_info, path, &key, bytenr, len);998 truncate_one_csum(trans, path, &key, bytenr, len);
998 if (key.offset < bytenr)999 if (key.offset < bytenr)
999 break;1000 break;
1000 }1001 }
@@ -1202,7 +1203,7 @@ extend_csum: · 201 unmodified lines
1202 diff /= csum_size;1203 diff /= csum_size;
1203 diff *= csum_size;1204 diff *= csum_size;
1204 1205
1205 btrfs_extend_item(path, diff);1206 btrfs_extend_item(trans, path, diff);
1206 ret = 0;1207 ret = 0;
1207 goto csum;1208 goto csum;
1208 }1209 }
@@ -1249,7 +1250,7 @@ found: · 40 unmodified lines
1249 ins_size /= csum_size;1250 ins_size /= csum_size;
1250 total_bytes += ins_size * fs_info->sectorsize;1251 total_bytes += ins_size * fs_info->sectorsize;
1251 1252
1252 btrfs_mark_buffer_dirty(path->nodes[0]);1253 btrfs_mark_buffer_dirty(trans, path->nodes[0]);
1253 if (total_bytes < sums->len) {1254 if (total_bytes < sums->len) {
1254 btrfs_release_path(path);1255 btrfs_release_path(path);
1255 cond_resched();1256 cond_resched();
fs/btrfs/file.c+32−29
@@ -17,6 +17,7 @@ · 16 unmodified lines
17#include <linux/uio.h>17#include <linux/uio.h>
18#include <linux/iversion.h>18#include <linux/iversion.h>
19#include <linux/fsverity.h>19#include <linux/fsverity.h>
20#include <linux/iomap.h>
20#include "ctree.h"21#include "ctree.h"
21#include "disk-io.h"22#include "disk-io.h"
22#include "transaction.h"23#include "transaction.h"
@@ -368,12 +369,13 @@ next_slot: · 345 unmodified lines
368 btrfs_set_file_extent_offset(leaf, fi, extent_offset);369 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
369 btrfs_set_file_extent_num_bytes(leaf, fi,370 btrfs_set_file_extent_num_bytes(leaf, fi,
370 extent_end - args->start);371 extent_end - args->start);
371 btrfs_mark_buffer_dirty(leaf);372 btrfs_mark_buffer_dirty(trans, leaf);
372 373
373 if (update_refs && disk_bytenr > 0) {374 if (update_refs && disk_bytenr > 0) {
374 btrfs_init_generic_ref(&ref,375 btrfs_init_generic_ref(&ref,
375 BTRFS_ADD_DELAYED_REF,376 BTRFS_ADD_DELAYED_REF,
376 disk_bytenr, num_bytes, 0);377 disk_bytenr, num_bytes, 0,
378 root->root_key.objectid);
377 btrfs_init_data_ref(&ref,379 btrfs_init_data_ref(&ref,
378 root->root_key.objectid,380 root->root_key.objectid,
379 new_key.objectid,381 new_key.objectid,
@@ -405,13 +407,13 @@ next_slot: · 25 unmodified lines
405 407
406 memcpy(&new_key, &key, sizeof(new_key));408 memcpy(&new_key, &key, sizeof(new_key));
407 new_key.offset = args->end;409 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);
409 411
410 extent_offset += args->end - key.offset;412 extent_offset += args->end - key.offset;
411 btrfs_set_file_extent_offset(leaf, fi, extent_offset);413 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
412 btrfs_set_file_extent_num_bytes(leaf, fi,414 btrfs_set_file_extent_num_bytes(leaf, fi,
413 extent_end - args->end);415 extent_end - args->end);
414 btrfs_mark_buffer_dirty(leaf);416 btrfs_mark_buffer_dirty(trans, leaf);
415 if (update_refs && disk_bytenr > 0)417 if (update_refs && disk_bytenr > 0)
416 args->bytes_found += args->end - key.offset;418 args->bytes_found += args->end - key.offset;
417 break;419 break;
@@ -431,7 +433,7 @@ next_slot: · 13 unmodified lines
431 433
432 btrfs_set_file_extent_num_bytes(leaf, fi,434 btrfs_set_file_extent_num_bytes(leaf, fi,
433 args->start - key.offset);435 args->start - key.offset);
434 btrfs_mark_buffer_dirty(leaf);436 btrfs_mark_buffer_dirty(trans, leaf);
435 if (update_refs && disk_bytenr > 0)437 if (update_refs && disk_bytenr > 0)
436 args->bytes_found += extent_end - args->start;438 args->bytes_found += extent_end - args->start;
437 if (args->end == extent_end)439 if (args->end == extent_end)
@@ -463,7 +465,8 @@ delete_extent_item: · 25 unmodified lines
463 } else if (update_refs && disk_bytenr > 0) {465 } else if (update_refs && disk_bytenr > 0) {
464 btrfs_init_generic_ref(&ref,466 btrfs_init_generic_ref(&ref,
465 BTRFS_DROP_DELAYED_REF,467 BTRFS_DROP_DELAYED_REF,
466 disk_bytenr, num_bytes, 0);468 disk_bytenr, num_bytes, 0,
469 root->root_key.objectid);
467 btrfs_init_data_ref(&ref,470 btrfs_init_data_ref(&ref,
468 root->root_key.objectid,471 root->root_key.objectid,
469 key.objectid,472 key.objectid,
@@ -536,7 +539,8 @@ delete_extent_item: · 66 unmodified lines
536 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)539 if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
537 path->slots[0]++;540 path->slots[0]++;
538 }541 }
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);
540 args->extent_inserted = true;544 args->extent_inserted = true;
541 }545 }
542 546
@@ -593,7 +597,6 @@ static int extent_mergeable(struct extent_buffer *leaf, int slot, · 50 unmodified lines
593int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,597int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
594 struct btrfs_inode *inode, u64 start, u64 end)598 struct btrfs_inode *inode, u64 start, u64 end)
595{599{
596 struct btrfs_fs_info *fs_info = trans->fs_info;
597 struct btrfs_root *root = inode->root;600 struct btrfs_root *root = inode->root;
598 struct extent_buffer *leaf;601 struct extent_buffer *leaf;
599 struct btrfs_path *path;602 struct btrfs_path *path;
@@ -664,7 +667,7 @@ again: · 64 unmodified lines
664 ino, bytenr, orig_offset,667 ino, bytenr, orig_offset,
665 &other_start, &other_end)) {668 &other_start, &other_end)) {
666 new_key.offset = end;669 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);
668 fi = btrfs_item_ptr(leaf, path->slots[0],671 fi = btrfs_item_ptr(leaf, path->slots[0],
669 struct btrfs_file_extent_item);672 struct btrfs_file_extent_item);
670 btrfs_set_file_extent_generation(leaf, fi,673 btrfs_set_file_extent_generation(leaf, fi,
@@ -679,7 +682,7 @@ again: · 8 unmodified lines
679 trans->transid);682 trans->transid);
680 btrfs_set_file_extent_num_bytes(leaf, fi,683 btrfs_set_file_extent_num_bytes(leaf, fi,
681 end - other_start);684 end - other_start);
682 btrfs_mark_buffer_dirty(leaf);685 btrfs_mark_buffer_dirty(trans, leaf);
683 goto out;686 goto out;
684 }687 }
685 }688 }
@@ -698,7 +701,7 @@ again: · 12 unmodified lines
698 trans->transid);701 trans->transid);
699 path->slots[0]++;702 path->slots[0]++;
700 new_key.offset = start;703 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);
702 705
703 fi = btrfs_item_ptr(leaf, path->slots[0],706 fi = btrfs_item_ptr(leaf, path->slots[0],
704 struct btrfs_file_extent_item);707 struct btrfs_file_extent_item);
@@ -708,7 +711,7 @@ again: · 3 unmodified lines
708 other_end - start);711 other_end - start);
709 btrfs_set_file_extent_offset(leaf, fi,712 btrfs_set_file_extent_offset(leaf, fi,
710 start - orig_offset);713 start - orig_offset);
711 btrfs_mark_buffer_dirty(leaf);714 btrfs_mark_buffer_dirty(trans, leaf);
712 goto out;715 goto out;
713 }716 }
714 }717 }
@@ -742,10 +745,10 @@ again: · 27 unmodified lines
742 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);745 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
743 btrfs_set_file_extent_num_bytes(leaf, fi,746 btrfs_set_file_extent_num_bytes(leaf, fi,
744 extent_end - split);747 extent_end - split);
745 btrfs_mark_buffer_dirty(leaf);748 btrfs_mark_buffer_dirty(trans, leaf);
746 749
747 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, bytenr,750 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, bytenr,
748 num_bytes, 0);751 num_bytes, 0, root->root_key.objectid);
749 btrfs_init_data_ref(&ref, root->root_key.objectid, ino,752 btrfs_init_data_ref(&ref, root->root_key.objectid, ino,
750 orig_offset, 0, false);753 orig_offset, 0, false);
751 ret = btrfs_inc_extent_ref(trans, &ref);754 ret = btrfs_inc_extent_ref(trans, &ref);
@@ -771,7 +774,7 @@ again: · 19 unmodified lines
771 other_start = end;774 other_start = end;
772 other_end = 0;775 other_end = 0;
773 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,776 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
774 num_bytes, 0);777 num_bytes, 0, root->root_key.objectid);
775 btrfs_init_data_ref(&ref, root->root_key.objectid, ino, orig_offset,778 btrfs_init_data_ref(&ref, root->root_key.objectid, ino, orig_offset,
776 0, false);779 0, false);
777 if (extent_mergeable(leaf, path->slots[0] + 1,780 if (extent_mergeable(leaf, path->slots[0] + 1,
@@ -814,7 +817,7 @@ again: · 36 unmodified lines
814 btrfs_set_file_extent_type(leaf, fi,817 btrfs_set_file_extent_type(leaf, fi,
815 BTRFS_FILE_EXTENT_REG);818 BTRFS_FILE_EXTENT_REG);
816 btrfs_set_file_extent_generation(leaf, fi, trans->transid);819 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
817 btrfs_mark_buffer_dirty(leaf);820 btrfs_mark_buffer_dirty(trans, leaf);
818 } else {821 } else {
819 fi = btrfs_item_ptr(leaf, del_slot - 1,822 fi = btrfs_item_ptr(leaf, del_slot - 1,
820 struct btrfs_file_extent_item);823 struct btrfs_file_extent_item);
@@ -823,7 +826,7 @@ again: · 2 unmodified lines
823 btrfs_set_file_extent_generation(leaf, fi, trans->transid);826 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
824 btrfs_set_file_extent_num_bytes(leaf, fi,827 btrfs_set_file_extent_num_bytes(leaf, fi,
825 extent_end - key.offset);828 extent_end - key.offset);
826 btrfs_mark_buffer_dirty(leaf);829 btrfs_mark_buffer_dirty(trans, leaf);
827 830
828 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);831 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
829 if (ret < 0) {832 if (ret < 0) {
@@ -1747,7 +1750,7 @@ static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx) · 917 unmodified lines
1747 struct btrfs_inode *inode = BTRFS_I(ctx->inode);1750 struct btrfs_inode *inode = BTRFS_I(ctx->inode);
1748 struct btrfs_fs_info *fs_info = inode->root->fs_info;1751 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1749 1752
1750 if (btrfs_inode_in_log(inode, fs_info->generation) &&1753 if (btrfs_inode_in_log(inode, btrfs_get_fs_generation(fs_info)) &&
1751 list_empty(&ctx->ordered_extents))1754 list_empty(&ctx->ordered_extents))
1752 return true;1755 return true;
1753 1756
@@ -1758,7 +1761,7 @@ static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx) · 4 unmodified lines
1758 * and for a fast fsync we don't wait for that, we only wait for the1761 * and for a fast fsync we don't wait for that, we only wait for the
1759 * writeback to complete.1762 * writeback to complete.
1760 */1763 */
1761 if (inode->last_trans <= fs_info->last_trans_committed &&1764 if (inode->last_trans <= btrfs_get_last_trans_committed(fs_info) &&
1762 (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||1765 (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
1763 list_empty(&ctx->ordered_extents)))1766 list_empty(&ctx->ordered_extents)))
1764 return true;1767 return true;
@@ -1887,7 +1890,6 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) · 122 unmodified lines
1887 1890
1888 atomic_inc(&root->log_batch);1891 atomic_inc(&root->log_batch);
1889 1892
1890 smp_mb();
1891 if (skip_inode_logging(&ctx)) {1893 if (skip_inode_logging(&ctx)) {
1892 /*1894 /*
1893 * We've had everything committed since the last time we were1895 * 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
2105 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);2107 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2106 btrfs_set_file_extent_offset(leaf, fi, 0);2108 btrfs_set_file_extent_offset(leaf, fi, 0);
2107 btrfs_set_file_extent_generation(leaf, fi, trans->transid);2109 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2108 btrfs_mark_buffer_dirty(leaf);2110 btrfs_mark_buffer_dirty(trans, leaf);
2109 goto out;2111 goto out;
2110 }2112 }
2111 2113
@@ -2113,7 +2115,7 @@ static int fill_holes(struct btrfs_trans_handle *trans, · 1 unmodified line
2113 u64 num_bytes;2115 u64 num_bytes;
2114 2116
2115 key.offset = offset;2117 key.offset = offset;
2116 btrfs_set_item_key_safe(fs_info, path, &key);2118 btrfs_set_item_key_safe(trans, path, &key);
2117 fi = btrfs_item_ptr(leaf, path->slots[0],2119 fi = btrfs_item_ptr(leaf, path->slots[0],
2118 struct btrfs_file_extent_item);2120 struct btrfs_file_extent_item);
2119 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -2121 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
2122 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);2124 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
2123 btrfs_set_file_extent_offset(leaf, fi, 0);2125 btrfs_set_file_extent_offset(leaf, fi, 0);
2124 btrfs_set_file_extent_generation(leaf, fi, trans->transid);2126 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2125 btrfs_mark_buffer_dirty(leaf);2127 btrfs_mark_buffer_dirty(trans, leaf);
2126 goto out;2128 goto out;
2127 }2129 }
2128 btrfs_release_path(path);2130 btrfs_release_path(path);
@@ -2274,7 +2276,7 @@ static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans, · 145 unmodified lines
2274 btrfs_set_file_extent_num_bytes(leaf, extent, replace_len);2276 btrfs_set_file_extent_num_bytes(leaf, extent, replace_len);
2275 if (extent_info->is_new_extent)2277 if (extent_info->is_new_extent)
2276 btrfs_set_file_extent_generation(leaf, extent, trans->transid);2278 btrfs_set_file_extent_generation(leaf, extent, trans->transid);
2277 btrfs_mark_buffer_dirty(leaf);2279 btrfs_mark_buffer_dirty(trans, leaf);
2278 btrfs_release_path(path);2280 btrfs_release_path(path);
2279 2281
2280 ret = btrfs_inode_set_file_extent_range(inode, extent_info->file_offset,2282 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
2304 2306
2305 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,2307 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
2306 extent_info->disk_offset,2308 extent_info->disk_offset,
2307 extent_info->disk_len, 0);2309 extent_info->disk_len, 0,
2310 root->root_key.objectid);
2308 ref_offset = extent_info->file_offset - extent_info->data_offset;2311 ref_offset = extent_info->file_offset - extent_info->data_offset;
2309 btrfs_init_data_ref(&ref, root->root_key.objectid,2312 btrfs_init_data_ref(&ref, root->root_key.objectid,
2310 btrfs_ino(inode), ref_offset, 0, false);2313 btrfs_ino(inode), ref_offset, 0, false);
@@ -2477,7 +2480,7 @@ int btrfs_replace_file_extents(struct btrfs_inode *inode, · 166 unmodified lines
2477 inode_set_mtime_to_ts(&inode->vfs_inode,2480 inode_set_mtime_to_ts(&inode->vfs_inode,
2478 inode_set_ctime_current(&inode->vfs_inode));2481 inode_set_ctime_current(&inode->vfs_inode));
2479 2482
2480 ret = btrfs_update_inode(trans, root, inode);2483 ret = btrfs_update_inode(trans, inode);
2481 if (ret)2484 if (ret)
2482 break;2485 break;
2483 2486
@@ -2717,7 +2720,7 @@ static int btrfs_punch_hole(struct file *file, loff_t offset, loff_t len) · 233 unmodified lines
2717 ASSERT(trans != NULL);2720 ASSERT(trans != NULL);
2718 inode_inc_iversion(inode);2721 inode_inc_iversion(inode);
2719 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));2722 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));
2721 updated_inode = true;2724 updated_inode = true;
2722 btrfs_end_transaction(trans);2725 btrfs_end_transaction(trans);
2723 btrfs_btree_balance_dirty(fs_info);2726 btrfs_btree_balance_dirty(fs_info);
@@ -2743,7 +2746,7 @@ out_only_mutex: · 19 unmodified lines
2743 } else {2746 } else {
2744 int ret2;2747 int ret2;
2745 2748
2746 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));2749 ret = btrfs_update_inode(trans, BTRFS_I(inode));
2747 ret2 = btrfs_end_transaction(trans);2750 ret2 = btrfs_end_transaction(trans);
2748 if (!ret)2751 if (!ret)
2749 ret = ret2;2752 ret = ret2;
@@ -2810,7 +2813,7 @@ static int btrfs_fallocate_update_isize(struct inode *inode, · 60 unmodified lines
2810 inode_set_ctime_current(inode);2813 inode_set_ctime_current(inode);
2811 i_size_write(inode, end);2814 i_size_write(inode, end);
2812 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);2815 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));
2814 ret2 = btrfs_end_transaction(trans);2817 ret2 = btrfs_end_transaction(trans);
2815 2818
2816 return ret ? ret : ret2;2819 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
57 struct btrfs_free_space *info, u64 offset,57 struct btrfs_free_space *info, u64 offset,
58 u64 bytes, bool update_stats);58 u64 bytes, bool update_stats);
59 59
60static void btrfs_crc32c_final(u32 crc, u8 *result)
61{
62 put_unaligned_le32(~crc, result);
63}
64
60static void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)65static void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
61{66{
62 struct btrfs_free_space *info;67 struct btrfs_free_space *info;
@@ -195,7 +200,7 @@ static int __create_free_space_inode(struct btrfs_root *root, · 132 unmodified lines
195 btrfs_set_inode_nlink(leaf, inode_item, 1);200 btrfs_set_inode_nlink(leaf, inode_item, 1);
196 btrfs_set_inode_transid(leaf, inode_item, trans->transid);201 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
197 btrfs_set_inode_block_group(leaf, inode_item, offset);202 btrfs_set_inode_block_group(leaf, inode_item, offset);
198 btrfs_mark_buffer_dirty(leaf);203 btrfs_mark_buffer_dirty(trans, leaf);
199 btrfs_release_path(path);204 btrfs_release_path(path);
200 205
201 key.objectid = BTRFS_FREE_SPACE_OBJECTID;206 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
@@ -213,7 +218,7 @@ static int __create_free_space_inode(struct btrfs_root *root, · 11 unmodified lines
213 struct btrfs_free_space_header);218 struct btrfs_free_space_header);
214 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));219 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
215 btrfs_set_free_space_key(leaf, header, &disk_key);220 btrfs_set_free_space_key(leaf, header, &disk_key);
216 btrfs_mark_buffer_dirty(leaf);221 btrfs_mark_buffer_dirty(trans, leaf);
217 btrfs_release_path(path);222 btrfs_release_path(path);
218 223
219 return 0;224 return 0;
@@ -354,7 +359,7 @@ int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans, · 134 unmodified lines
354 if (ret)359 if (ret)
355 goto fail;360 goto fail;
356 361
357 ret = btrfs_update_inode(trans, root, inode);362 ret = btrfs_update_inode(trans, inode);
358 363
359fail:364fail:
360 if (locked)365 if (locked)
@@ -540,7 +545,7 @@ static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index) · 179 unmodified lines
540 if (index == 0)545 if (index == 0)
541 offset = sizeof(u32) * io_ctl->num_pages;546 offset = sizeof(u32) * io_ctl->num_pages;
542 547
543 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);548 crc = crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
544 btrfs_crc32c_final(crc, (u8 *)&crc);549 btrfs_crc32c_final(crc, (u8 *)&crc);
545 io_ctl_unmap_page(io_ctl);550 io_ctl_unmap_page(io_ctl);
546 tmp = page_address(io_ctl->pages[0]);551 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
562 val = *tmp;567 val = *tmp;
563 568
564 io_ctl_map_page(io_ctl, 0);569 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);
566 btrfs_crc32c_final(crc, (u8 *)&crc);571 btrfs_crc32c_final(crc, (u8 *)&crc);
567 if (val != crc) {572 if (val != crc) {
568 btrfs_err_rl(io_ctl->fs_info,573 btrfs_err_rl(io_ctl->fs_info,
@@ -1185,7 +1190,7 @@ update_cache_item(struct btrfs_trans_handle *trans, · 616 unmodified lines
1185 btrfs_set_free_space_entries(leaf, header, entries);1190 btrfs_set_free_space_entries(leaf, header, entries);
1186 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);1191 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1187 btrfs_set_free_space_generation(leaf, header, trans->transid);1192 btrfs_set_free_space_generation(leaf, header, trans->transid);
1188 btrfs_mark_buffer_dirty(leaf);1193 btrfs_mark_buffer_dirty(trans, leaf);
1189 btrfs_release_path(path);1194 btrfs_release_path(path);
1190 1195
1191 return 0;1196 return 0;
@@ -1321,7 +1326,7 @@ out: · 129 unmodified lines
1321 "failed to write free space cache for block group %llu error %d",1326 "failed to write free space cache for block group %llu error %d",
1322 block_group->start, ret);1327 block_group->start, ret);
1323 }1328 }
1324 btrfs_update_inode(trans, root, BTRFS_I(inode));1329 btrfs_update_inode(trans, BTRFS_I(inode));
1325 1330
1326 if (block_group) {1331 if (block_group) {
1327 /* the dirty list is protected by the dirty_bgs_lock */1332 /* 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
1362/*1367/*
1363 * Write out cached info to an inode.1368 * Write out cached info to an inode.
1364 *1369 *
1365 * @root: root the inode belongs to
1366 * @inode: freespace inode we are writing out1370 * @inode: freespace inode we are writing out
1367 * @ctl: free space cache we are going to write out1371 * @ctl: free space cache we are going to write out
1368 * @block_group: block_group for this cache if it belongs to a block_group1372 * @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
1373 * on mount. This will return 0 if it was successful in writing the cache out,1377 * on mount. This will return 0 if it was successful in writing the cache out,
1374 * or an errno if it was not.1378 * or an errno if it was not.
1375 */1379 */
1376static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,1380static int __btrfs_write_out_cache(struct inode *inode,
1377 struct btrfs_free_space_ctl *ctl,1381 struct btrfs_free_space_ctl *ctl,
1378 struct btrfs_block_group *block_group,1382 struct btrfs_block_group *block_group,
1379 struct btrfs_io_ctl *io_ctl,1383 struct btrfs_io_ctl *io_ctl,
@@ -1506,7 +1510,7 @@ out: · 126 unmodified lines
1506 invalidate_inode_pages2(inode->i_mapping);1510 invalidate_inode_pages2(inode->i_mapping);
1507 BTRFS_I(inode)->generation = 0;1511 BTRFS_I(inode)->generation = 0;
1508 }1512 }
1509 btrfs_update_inode(trans, root, BTRFS_I(inode));1513 btrfs_update_inode(trans, BTRFS_I(inode));
1510 if (must_iput)1514 if (must_iput)
1511 iput(inode);1515 iput(inode);
1512 return ret;1516 return ret;
@@ -1532,8 +1536,8 @@ int btrfs_write_out_cache(struct btrfs_trans_handle *trans, · 19 unmodified lines
1532 if (IS_ERR(inode))1536 if (IS_ERR(inode))
1533 return 0;1537 return 0;
1534 1538
1535 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,1539 ret = __btrfs_write_out_cache(inode, ctl, block_group,
1536 block_group, &block_group->io_ctl, trans);1540 &block_group->io_ctl, trans);
1537 if (ret) {1541 if (ret) {
1538 btrfs_debug(fs_info,1542 btrfs_debug(fs_info,
1539 "failed to write free space cache for block group %llu error %d",1543 "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
89 struct btrfs_free_space_info);89 struct btrfs_free_space_info);
90 btrfs_set_free_space_extent_count(leaf, info, 0);90 btrfs_set_free_space_extent_count(leaf, info, 0);
91 btrfs_set_free_space_flags(leaf, info, 0);91 btrfs_set_free_space_flags(leaf, info, 0);
92 btrfs_mark_buffer_dirty(leaf);92 btrfs_mark_buffer_dirty(trans, leaf);
93 93
94 ret = 0;94 ret = 0;
95out:95out:
@@ -287,7 +287,7 @@ int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans, · 191 unmodified lines
287 flags |= BTRFS_FREE_SPACE_USING_BITMAPS;287 flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
288 btrfs_set_free_space_flags(leaf, info, flags);288 btrfs_set_free_space_flags(leaf, info, flags);
289 expected_extent_count = btrfs_free_space_extent_count(leaf, info);289 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
290 btrfs_mark_buffer_dirty(leaf);290 btrfs_mark_buffer_dirty(trans, leaf);
291 btrfs_release_path(path);291 btrfs_release_path(path);
292 292
293 if (extent_count != expected_extent_count) {293 if (extent_count != expected_extent_count) {
@@ -324,7 +324,7 @@ int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans, · 30 unmodified lines
324 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);324 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
325 write_extent_buffer(leaf, bitmap_cursor, ptr,325 write_extent_buffer(leaf, bitmap_cursor, ptr,
326 data_size);326 data_size);
327 btrfs_mark_buffer_dirty(leaf);327 btrfs_mark_buffer_dirty(trans, leaf);
328 btrfs_release_path(path);328 btrfs_release_path(path);
329 329
330 i += extent_size;330 i += extent_size;
@@ -430,7 +430,7 @@ int convert_free_space_to_extents(struct btrfs_trans_handle *trans, · 99 unmodified lines
430 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;430 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
431 btrfs_set_free_space_flags(leaf, info, flags);431 btrfs_set_free_space_flags(leaf, info, flags);
432 expected_extent_count = btrfs_free_space_extent_count(leaf, info);432 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
433 btrfs_mark_buffer_dirty(leaf);433 btrfs_mark_buffer_dirty(trans, leaf);
434 btrfs_release_path(path);434 btrfs_release_path(path);
435 435
436 nrbits = block_group->length >> block_group->fs_info->sectorsize_bits;436 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
495 495
496 extent_count += new_extents;496 extent_count += new_extents;
497 btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);497 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]);
499 btrfs_release_path(path);499 btrfs_release_path(path);
500 500
501 if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&501 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
533 return !!extent_buffer_test_bit(leaf, ptr, i);533 return !!extent_buffer_test_bit(leaf, ptr, i);
534}534}
535 535
536static void free_space_set_bits(struct btrfs_block_group *block_group,536static void free_space_set_bits(struct btrfs_trans_handle *trans,
537 struct btrfs_block_group *block_group,
537 struct btrfs_path *path, u64 *start, u64 *size,538 struct btrfs_path *path, u64 *start, u64 *size,
538 int bit)539 int bit)
539{540{
@@ -563,7 +564,7 @@ static void free_space_set_bits(struct btrfs_block_group *block_group, · 23 unmodified lines
563 extent_buffer_bitmap_set(leaf, ptr, first, last - first);564 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
564 else565 else
565 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);566 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
566 btrfs_mark_buffer_dirty(leaf);567 btrfs_mark_buffer_dirty(trans, leaf);
567 568
568 *size -= end - *start;569 *size -= end - *start;
569 *start = end;570 *start = end;
@@ -656,7 +657,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans, · 86 unmodified lines
656 cur_start = start;657 cur_start = start;
657 cur_size = size;658 cur_size = size;
658 while (1) {659 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,
660 !remove);661 !remove);
661 if (cur_size == 0)662 if (cur_size == 0)
662 break;663 break;
fs/btrfs/fs.h+52−17
@@ -139,6 +139,12 @@ enum { · 138 unmodified lines
139 */139 */
140 BTRFS_FS_FEATURE_CHANGED,140 BTRFS_FS_FEATURE_CHANGED,
141 141
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
142#if BITS_PER_LONG == 32148#if BITS_PER_LONG == 32
143 /* Indicate if we have error/warn message printed on 32bit systems */149 /* Indicate if we have error/warn message printed on 32bit systems */
144 BTRFS_FS_32BIT_ERROR,150 BTRFS_FS_32BIT_ERROR,
@@ -171,19 +177,17 @@ enum { · 26 unmodified lines
171 BTRFS_MOUNT_AUTO_DEFRAG = (1UL << 16),177 BTRFS_MOUNT_AUTO_DEFRAG = (1UL << 16),
172 BTRFS_MOUNT_USEBACKUPROOT = (1UL << 17),178 BTRFS_MOUNT_USEBACKUPROOT = (1UL << 17),
173 BTRFS_MOUNT_SKIP_BALANCE = (1UL << 18),179 BTRFS_MOUNT_SKIP_BALANCE = (1UL << 18),
174 BTRFS_MOUNT_CHECK_INTEGRITY = (1UL << 19),180 BTRFS_MOUNT_PANIC_ON_FATAL_ERROR = (1UL << 19),
175 BTRFS_MOUNT_CHECK_INTEGRITY_DATA = (1UL << 20),181 BTRFS_MOUNT_RESCAN_UUID_TREE = (1UL << 20),
176 BTRFS_MOUNT_PANIC_ON_FATAL_ERROR = (1UL << 21),182 BTRFS_MOUNT_FRAGMENT_DATA = (1UL << 21),
177 BTRFS_MOUNT_RESCAN_UUID_TREE = (1UL << 22),183 BTRFS_MOUNT_FRAGMENT_METADATA = (1UL << 22),
178 BTRFS_MOUNT_FRAGMENT_DATA = (1UL << 23),184 BTRFS_MOUNT_FREE_SPACE_TREE = (1UL << 23),
179 BTRFS_MOUNT_FRAGMENT_METADATA = (1UL << 24),185 BTRFS_MOUNT_NOLOGREPLAY = (1UL << 24),
180 BTRFS_MOUNT_FREE_SPACE_TREE = (1UL << 25),186 BTRFS_MOUNT_REF_VERIFY = (1UL << 25),
181 BTRFS_MOUNT_NOLOGREPLAY = (1UL << 26),187 BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 26),
182 BTRFS_MOUNT_REF_VERIFY = (1UL << 27),188 BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 27),
183 BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 28),189 BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 28),
184 BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 29),190 BTRFS_MOUNT_NODISCARD = (1UL << 29),
185 BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 30),
186 BTRFS_MOUNT_NODISCARD = (1UL << 31),
187};191};
188 192
189/*193/*
@@ -216,7 +220,8 @@ enum { · 26 unmodified lines
216 BTRFS_FEATURE_INCOMPAT_NO_HOLES | \220 BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
217 BTRFS_FEATURE_INCOMPAT_METADATA_UUID | \221 BTRFS_FEATURE_INCOMPAT_METADATA_UUID | \
218 BTRFS_FEATURE_INCOMPAT_RAID1C34 | \222 BTRFS_FEATURE_INCOMPAT_RAID1C34 | \
219 BTRFS_FEATURE_INCOMPAT_ZONED)223 BTRFS_FEATURE_INCOMPAT_ZONED | \
224 BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA)
220 225
221#ifdef CONFIG_BTRFS_DEBUG226#ifdef CONFIG_BTRFS_DEBUG
222 /*227 /*
@@ -225,6 +230,7 @@ enum { · 2 unmodified lines
225 */230 */
226#define BTRFS_FEATURE_INCOMPAT_SUPP \231#define BTRFS_FEATURE_INCOMPAT_SUPP \
227 (BTRFS_FEATURE_INCOMPAT_SUPP_STABLE | \232 (BTRFS_FEATURE_INCOMPAT_SUPP_STABLE | \
233 BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE | \
228 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2)234 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2)
229 235
230#else236#else
@@ -369,6 +375,7 @@ struct btrfs_fs_info { · 138 unmodified lines
369 struct btrfs_root *uuid_root;375 struct btrfs_root *uuid_root;
370 struct btrfs_root *data_reloc_root;376 struct btrfs_root *data_reloc_root;
371 struct btrfs_root *block_group_root;377 struct btrfs_root *block_group_root;
378 struct btrfs_root *stripe_root;
372 379
373 /* The log root tree is a directory of all the other log roots */380 /* The log root tree is a directory of all the other log roots */
374 struct btrfs_root *log_root_tree;381 struct btrfs_root *log_root_tree;
@@ -409,7 +416,17 @@ struct btrfs_fs_info { · 34 unmodified lines
409 416
410 struct btrfs_block_rsv empty_block_rsv;417 struct btrfs_block_rsv empty_block_rsv;
411 418
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 */
412 u64 generation;425 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 */
413 u64 last_trans_committed;430 u64 last_trans_committed;
414 /*431 /*
415 * Generation of the last transaction used for block group relocation432 * Generation of the last transaction used for block group relocation
@@ -645,9 +662,6 @@ struct btrfs_fs_info { · 229 unmodified lines
645 662
646 struct btrfs_discard_ctl discard_ctl;663 struct btrfs_discard_ctl discard_ctl;
647 664
648#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
649 u32 check_integrity_print_mask;
650#endif
651 /* Is qgroup tracking in a consistent state? */665 /* Is qgroup tracking in a consistent state? */
652 u64 qgroup_flags;666 u64 qgroup_flags;
653 667
@@ -683,6 +697,7 @@ struct btrfs_fs_info { · 29 unmodified lines
683 /* Protected by qgroup_rescan_lock */697 /* Protected by qgroup_rescan_lock */
684 bool qgroup_rescan_running;698 bool qgroup_rescan_running;
685 u8 qgroup_drop_subtree_thres;699 u8 qgroup_drop_subtree_thres;
700 u64 qgroup_enable_gen;
686 701
687 /*702 /*
688 * If this is not 0, then it indicates a serious filesystem error has703 * If this is not 0, then it indicates a serious filesystem error has
@@ -812,6 +827,26 @@ struct btrfs_fs_info { · 123 unmodified lines
812#endif827#endif
813};828};
814 829
830static inline u64 btrfs_get_fs_generation(const struct btrfs_fs_info *fs_info)
831{
832 return READ_ONCE(fs_info->generation);
833}
834
835static inline void btrfs_set_fs_generation(struct btrfs_fs_info *fs_info, u64 gen)
836{
837 WRITE_ONCE(fs_info->generation, gen);
838}
839
840static 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
845static 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
815static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,850static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,
816 u64 gen)851 u64 gen)
817{852{
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
167 memmove_extent_buffer(leaf, ptr, ptr + del_len,167 memmove_extent_buffer(leaf, ptr, ptr + del_len,
168 item_size - (ptr + del_len - item_start));168 item_size - (ptr + del_len - item_start));
169 169
170 btrfs_truncate_item(path, item_size - del_len, 1);170 btrfs_truncate_item(trans, path, item_size - del_len, 1);
171 171
172out:172out:
173 btrfs_free_path(path);173 btrfs_free_path(path);
@@ -229,7 +229,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, · 55 unmodified lines
229 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);229 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
230 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,230 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
231 item_size - (ptr + sub_item_len - item_start));231 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);
233out:233out:
234 btrfs_free_path(path);234 btrfs_free_path(path);
235 235
@@ -247,7 +247,7 @@ out: · 11 unmodified lines
247}247}
248 248
249/*249/*
250 * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree.250 * Insert an extended inode ref into a tree.
251 *251 *
252 * The caller must have checked against BTRFS_LINK_MAX already.252 * The caller must have checked against BTRFS_LINK_MAX already.
253 */253 */
@@ -282,7 +282,7 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans, · 28 unmodified lines
282 name))282 name))
283 goto out;283 goto out;
284 284
285 btrfs_extend_item(path, ins_len);285 btrfs_extend_item(trans, path, ins_len);
286 ret = 0;286 ret = 0;
287 }287 }
288 if (ret < 0)288 if (ret < 0)
@@ -299,7 +299,7 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans, · 10 unmodified lines
299 299
300 ptr = (unsigned long)&extref->name;300 ptr = (unsigned long)&extref->name;
301 write_extent_buffer(path->nodes[0], name->name, ptr, name->len);301 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]);
303 303
304out:304out:
305 btrfs_free_path(path);305 btrfs_free_path(path);
@@ -338,7 +338,7 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, · 32 unmodified lines
338 goto out;338 goto out;
339 339
340 old_size = btrfs_item_size(path->nodes[0], path->slots[0]);340 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);
342 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],342 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
343 struct btrfs_inode_ref);343 struct btrfs_inode_ref);
344 ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);344 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
364 ptr = (unsigned long)(ref + 1);364 ptr = (unsigned long)(ref + 1);
365 }365 }
366 write_extent_buffer(path->nodes[0], name->name, ptr, name->len);366 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]);
368 368
369out:369out:
370 btrfs_free_path(path);370 btrfs_free_path(path);
@@ -591,7 +591,7 @@ search_again: · 220 unmodified lines
591 num_dec = (orig_num_bytes - extent_num_bytes);591 num_dec = (orig_num_bytes - extent_num_bytes);
592 if (extent_start != 0)592 if (extent_start != 0)
593 control->sub_bytes += num_dec;593 control->sub_bytes += num_dec;
594 btrfs_mark_buffer_dirty(leaf);594 btrfs_mark_buffer_dirty(trans, leaf);
595 } else {595 } else {
596 extent_num_bytes =596 extent_num_bytes =
597 btrfs_file_extent_disk_num_bytes(leaf, fi);597 btrfs_file_extent_disk_num_bytes(leaf, fi);
@@ -617,7 +617,7 @@ search_again: · 19 unmodified lines
617 617
618 btrfs_set_file_extent_ram_bytes(leaf, fi, size);618 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
619 size = btrfs_file_extent_calc_inline_size(size);619 size = btrfs_file_extent_calc_inline_size(size);
620 btrfs_truncate_item(path, size, 1);620 btrfs_truncate_item(trans, path, size, 1);
621 } else if (!del_item) {621 } else if (!del_item) {
622 /*622 /*
623 * We have to bail so the last_size is set to623 * We have to bail so the last_size is set to
@@ -676,7 +676,8 @@ delete: · 52 unmodified lines
676 bytes_deleted += extent_num_bytes;676 bytes_deleted += extent_num_bytes;
677 677
678 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,678 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);
680 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),681 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
681 control->ino, extent_offset,682 control->ino, extent_offset,
682 root->root_key.objectid, false);683 root->root_key.objectid, false);
fs/btrfs/inode-item.h+8−0
@@ -4,6 +4,7 @@ · 3 unmodified lines
4#define BTRFS_INODE_ITEM_H4#define BTRFS_INODE_ITEM_H
5 5
6#include <linux/types.h>6#include <linux/types.h>
7#include <linux/crc32c.h>
7 8
8struct btrfs_trans_handle;9struct btrfs_trans_handle;
9struct btrfs_root;10struct btrfs_root;
@@ -12,6 +13,7 @@ struct btrfs_key; · 2 unmodified lines
12struct btrfs_inode_extref;13struct btrfs_inode_extref;
13struct btrfs_inode;14struct btrfs_inode;
14struct extent_buffer;15struct extent_buffer;
16struct fscrypt_str;
15 17
16/*18/*
17 * Return this if we need to call truncate_block for the last bit of the19 * 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
76 *ro_flags = (u32)(inode_item_flags >> 32);78 *ro_flags = (u32)(inode_item_flags >> 32);
77}79}
78 80
81/* Figure the key offset of an extended inode ref. */
82static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name, int len)
83{
84 return (u64)crc32c(parent_objectid, name, len);
85}
86
79int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,87int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,88 struct btrfs_root *root,
81 struct btrfs_truncate_control *control);89 struct btrfs_truncate_control *control);
fs/btrfs/inode.c+101−90
@@ -71,6 +71,7 @@ · 70 unmodified lines
71#include "super.h"71#include "super.h"
72#include "orphan.h"72#include "orphan.h"
73#include "backref.h"73#include "backref.h"
74#include "raid-stripe-tree.h"
74 75
75struct btrfs_iget_args {76struct btrfs_iget_args {
76 u64 ino;77 u64 ino;
@@ -348,7 +349,7 @@ static void __cold btrfs_print_data_csum_error(struct btrfs_inode *inode, · 271 unmodified lines
348}349}
349 350
350/*351/*
351 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed352 * Lock inode i_rwsem based on arguments passed.
352 *353 *
353 * ilock_flags can have the following bit set:354 * ilock_flags can have the following bit set:
354 *355 *
@@ -382,7 +383,7 @@ int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags) · 27 unmodified lines
382}383}
383 384
384/*385/*
385 * btrfs_inode_unlock - unock inode i_rwsem386 * Unock inode i_rwsem.
386 *387 *
387 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()388 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
388 * to decide whether the lock acquired is shared or exclusive.389 * 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
573 kunmap_local(kaddr);574 kunmap_local(kaddr);
574 put_page(page);575 put_page(page);
575 }576 }
576 btrfs_mark_buffer_dirty(leaf);577 btrfs_mark_buffer_dirty(trans, leaf);
577 btrfs_release_path(path);578 btrfs_release_path(path);
578 579
579 /*580 /*
@@ -670,7 +671,7 @@ static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size, · 90 unmodified lines
670 }671 }
671 672
672 btrfs_update_inode_bytes(inode, size, drop_args.bytes_found);673 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);
674 if (ret && ret != -ENOSPC) {675 if (ret && ret != -ENOSPC) {
675 btrfs_abort_transaction(trans, ret);676 btrfs_abort_transaction(trans, ret);
676 goto out;677 goto out;
@@ -1565,8 +1566,11 @@ out_unlock: · 888 unmodified lines
1565 * Phase two of compressed writeback. This is the ordered portion of the code,1566 * Phase two of compressed writeback. This is the ordered portion of the code,
1566 * which only gets called in the order the work was queued. We walk all the1567 * which only gets called in the order the work was queued. We walk all the
1567 * async extents created by compress_file_range and send them down to the disk.1568 * 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.
1568 */1572 */
1569static noinline void submit_compressed_extents(struct btrfs_work *work)1573static noinline void submit_compressed_extents(struct btrfs_work *work, bool do_free)
1570{1574{
1571 struct async_chunk *async_chunk = container_of(work, struct async_chunk,1575 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1572 work);1576 work);
@@ -1575,6 +1579,21 @@ static noinline void submit_compressed_extents(struct btrfs_work *work) · 2 unmodified lines
1575 unsigned long nr_pages;1579 unsigned long nr_pages;
1576 u64 alloc_hint = 0;1580 u64 alloc_hint = 0;
1577 1581
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
1578 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>1597 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
1579 PAGE_SHIFT;1598 PAGE_SHIFT;
1580 1599
@@ -1591,21 +1610,6 @@ static noinline void submit_compressed_extents(struct btrfs_work *work) · 10 unmodified lines
1591 cond_wake_up_nomb(&fs_info->async_submit_wait);1610 cond_wake_up_nomb(&fs_info->async_submit_wait);
1592}1611}
1593 1612
1594static 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
1609static bool run_delalloc_compressed(struct btrfs_inode *inode,1613static bool run_delalloc_compressed(struct btrfs_inode *inode,
1610 struct page *locked_page, u64 start,1614 struct page *locked_page, u64 start,
1611 u64 end, struct writeback_control *wbc)1615 u64 end, struct writeback_control *wbc)
@@ -1683,7 +1687,7 @@ static bool run_delalloc_compressed(struct btrfs_inode *inode, · 71 unmodified lines
1683 }1687 }
1684 1688
1685 btrfs_init_work(&async_chunk[i].work, compress_file_range,1689 btrfs_init_work(&async_chunk[i].work, compress_file_range,
1686 submit_compressed_extents, async_cow_free);1690 submit_compressed_extents);
1687 1691
1688 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);1692 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
1689 atomic_add(nr_pages, &fs_info->async_delalloc_pages);1693 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
2235{2239{
2236 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {2240 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
2237 if (inode->defrag_bytes &&2241 if (inode->defrag_bytes &&
2238 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,2242 test_range_bit_exists(&inode->io_tree, start, end, EXTENT_DEFRAG))
2239 0, NULL))
2240 return false;2243 return false;
2241 return true;2244 return true;
2242 }2245 }
@@ -2847,7 +2850,7 @@ int btrfs_writepage_cow_fixup(struct page *page) · 604 unmodified lines
2847 ihold(inode);2850 ihold(inode);
2848 btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE);2851 btrfs_page_set_checked(fs_info, page, page_offset(page), PAGE_SIZE);
2849 get_page(page);2852 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);
2851 fixup->page = page;2854 fixup->page = page;
2852 fixup->inode = BTRFS_I(inode);2855 fixup->inode = BTRFS_I(inode);
2853 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);2856 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
2912 btrfs_item_ptr_offset(leaf, path->slots[0]),2915 btrfs_item_ptr_offset(leaf, path->slots[0]),
2913 sizeof(struct btrfs_file_extent_item));2916 sizeof(struct btrfs_file_extent_item));
2914 2917
2915 btrfs_mark_buffer_dirty(leaf);2918 btrfs_mark_buffer_dirty(trans, leaf);
2916 btrfs_release_path(path);2919 btrfs_release_path(path);
2917 2920
2918 /*2921 /*
@@ -3070,7 +3073,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) · 151 unmodified lines
3070 goto out;3073 goto out;
3071 }3074 }
3072 trans->block_rsv = &inode->block_rsv;3075 trans->block_rsv = &inode->block_rsv;
3073 ret = btrfs_update_inode_fallback(trans, root, inode);3076 ret = btrfs_update_inode_fallback(trans, inode);
3074 if (ret) /* -ENOMEM or corruption */3077 if (ret) /* -ENOMEM or corruption */
3075 btrfs_abort_transaction(trans, ret);3078 btrfs_abort_transaction(trans, ret);
3076 goto out;3079 goto out;
@@ -3091,6 +3094,10 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) · 14 unmodified lines
3091 3094
3092 trans->block_rsv = &inode->block_rsv;3095 trans->block_rsv = &inode->block_rsv;
3093 3096
3097 ret = btrfs_insert_raid_extent(trans, ordered_extent);
3098 if (ret)
3099 goto out;
3100
3094 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))3101 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
3095 compress_type = ordered_extent->compress_type;3102 compress_type = ordered_extent->compress_type;
3096 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {3103 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
3136 &cached_state);3143 &cached_state);
3137 3144
3138 btrfs_inode_safe_disk_i_size_write(inode, 0);3145 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);
3140 if (ret) { /* -ENOMEM or corruption */3147 if (ret) { /* -ENOMEM or corruption */
3141 btrfs_abort_transaction(trans, ret);3148 btrfs_abort_transaction(trans, ret);
3142 goto out;3149 goto out;
@@ -3224,7 +3231,8 @@ out: · 81 unmodified lines
3224int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered)3231int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered)
3225{3232{
3226 if (btrfs_is_zoned(btrfs_sb(ordered->inode->i_sb)) &&3233 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))
3228 btrfs_finish_ordered_zoned(ordered);3236 btrfs_finish_ordered_zoned(ordered);
3229 return btrfs_finish_one_ordered(ordered);3237 return btrfs_finish_one_ordered(ordered);
3230}3238}
@@ -3282,7 +3290,7 @@ bool btrfs_data_csum_ok(struct btrfs_bio *bbio, struct btrfs_device *dev, · 51 unmodified lines
3282 3290
3283 if (btrfs_is_data_reloc_root(inode->root) &&3291 if (btrfs_is_data_reloc_root(inode->root) &&
3284 test_range_bit(&inode->io_tree, file_offset, end, EXTENT_NODATASUM,3292 test_range_bit(&inode->io_tree, file_offset, end, EXTENT_NODATASUM,
3285 1, NULL)) {3293 NULL)) {
3286 /* Skip the range without csum for data reloc inode */3294 /* Skip the range without csum for data reloc inode */
3287 clear_extent_bits(&inode->io_tree, file_offset, end,3295 clear_extent_bits(&inode->io_tree, file_offset, end,
3288 EXTENT_NODATASUM);3296 EXTENT_NODATASUM);
@@ -3306,7 +3314,7 @@ zeroit: · 17 unmodified lines
3306}3314}
3307 3315
3308/*3316/*
3309 * btrfs_add_delayed_iput - perform a delayed iput on @inode3317 * Perform a delayed iput on @inode.
3310 *3318 *
3311 * @inode: The inode we want to perform iput on3319 * @inode: The inode we want to perform iput on
3312 *3320 *
@@ -3763,10 +3771,8 @@ static int btrfs_read_locked_inode(struct inode *inode, · 450 unmodified lines
3763 inode_set_ctime(inode, btrfs_timespec_sec(leaf, &inode_item->ctime),3771 inode_set_ctime(inode, btrfs_timespec_sec(leaf, &inode_item->ctime),
3764 btrfs_timespec_nsec(leaf, &inode_item->ctime));3772 btrfs_timespec_nsec(leaf, &inode_item->ctime));
3765 3773
3766 BTRFS_I(inode)->i_otime.tv_sec =3774 BTRFS_I(inode)->i_otime_sec = btrfs_timespec_sec(leaf, &inode_item->otime);
3767 btrfs_timespec_sec(leaf, &inode_item->otime);3775 BTRFS_I(inode)->i_otime_nsec = btrfs_timespec_nsec(leaf, &inode_item->otime);
3768 BTRFS_I(inode)->i_otime.tv_nsec =
3769 btrfs_timespec_nsec(leaf, &inode_item->otime);
3770 3776
3771 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));3777 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3772 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);3778 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
@@ -3792,7 +3798,7 @@ cache_index: · 19 unmodified lines
3792 * This is required for both inode re-read from disk and delayed inode3798 * This is required for both inode re-read from disk and delayed inode
3793 * in delayed_nodes_tree.3799 * in delayed_nodes_tree.
3794 */3800 */
3795 if (BTRFS_I(inode)->last_trans == fs_info->generation)3801 if (BTRFS_I(inode)->last_trans == btrfs_get_fs_generation(fs_info))
3796 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,3802 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3797 &BTRFS_I(inode)->runtime_flags);3803 &BTRFS_I(inode)->runtime_flags);
3798 3804
@@ -3936,10 +3942,8 @@ static void fill_inode_item(struct btrfs_trans_handle *trans, · 137 unmodified lines
3936 btrfs_set_token_timespec_nsec(&token, &item->ctime,3942 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3937 inode_get_ctime_nsec(inode));3943 inode_get_ctime_nsec(inode));
3938 3944
3939 btrfs_set_token_timespec_sec(&token, &item->otime,3945 btrfs_set_token_timespec_sec(&token, &item->otime, BTRFS_I(inode)->i_otime_sec);
3940 BTRFS_I(inode)->i_otime.tv_sec);3946 btrfs_set_token_timespec_nsec(&token, &item->otime, BTRFS_I(inode)->i_otime_nsec);
3941 btrfs_set_token_timespec_nsec(&token, &item->otime,
3942 BTRFS_I(inode)->i_otime.tv_nsec);
3943 3947
3944 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));3948 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3945 btrfs_set_token_inode_generation(&token, item,3949 btrfs_set_token_inode_generation(&token, item,
@@ -3957,8 +3961,7 @@ static void fill_inode_item(struct btrfs_trans_handle *trans, · 11 unmodified lines
3957 * copy everything in the in-memory inode into the btree.3961 * copy everything in the in-memory inode into the btree.
3958 */3962 */
3959static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,3963static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3960 struct btrfs_root *root,3964 struct btrfs_inode *inode)
3961 struct btrfs_inode *inode)
3962{3965{
3963 struct btrfs_inode_item *inode_item;3966 struct btrfs_inode_item *inode_item;
3964 struct btrfs_path *path;3967 struct btrfs_path *path;
@@ -3969,7 +3972,7 @@ static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, · 4 unmodified lines
3969 if (!path)3972 if (!path)
3970 return -ENOMEM;3973 return -ENOMEM;
3971 3974
3972 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);3975 ret = btrfs_lookup_inode(trans, inode->root, path, &inode->location, 1);
3973 if (ret) {3976 if (ret) {
3974 if (ret > 0)3977 if (ret > 0)
3975 ret = -ENOENT;3978 ret = -ENOENT;
@@ -3981,7 +3984,7 @@ static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, · 5 unmodified lines
3981 struct btrfs_inode_item);3984 struct btrfs_inode_item);
3982 3985
3983 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);3986 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
3984 btrfs_mark_buffer_dirty(leaf);3987 btrfs_mark_buffer_dirty(trans, leaf);
3985 btrfs_set_inode_last_trans(trans, inode);3988 btrfs_set_inode_last_trans(trans, inode);
3986 ret = 0;3989 ret = 0;
3987failed:3990failed:
@@ -3992,10 +3995,10 @@ failed: · 4 unmodified lines
3992/*3995/*
3993 * copy everything in the in-memory inode into the btree.3996 * copy everything in the in-memory inode into the btree.
3994 */3997 */
3995noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,3998int btrfs_update_inode(struct btrfs_trans_handle *trans,
3996 struct btrfs_root *root,3999 struct btrfs_inode *inode)
3997 struct btrfs_inode *inode)
3998{4000{
4001 struct btrfs_root *root = inode->root;
3999 struct btrfs_fs_info *fs_info = root->fs_info;4002 struct btrfs_fs_info *fs_info = root->fs_info;
4000 int ret;4003 int ret;
4001 4004
@@ -4011,23 +4014,23 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, · 9 unmodified lines
4011 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {4014 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
4012 btrfs_update_root_times(trans, root);4015 btrfs_update_root_times(trans, root);
4013 4016
4014 ret = btrfs_delayed_update_inode(trans, root, inode);4017 ret = btrfs_delayed_update_inode(trans, inode);
4015 if (!ret)4018 if (!ret)
4016 btrfs_set_inode_last_trans(trans, inode);4019 btrfs_set_inode_last_trans(trans, inode);
4017 return ret;4020 return ret;
4018 }4021 }
4019 4022
4020 return btrfs_update_inode_item(trans, root, inode);4023 return btrfs_update_inode_item(trans, inode);
4021}4024}
4022 4025
4023int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,4026int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4024 struct btrfs_root *root, struct btrfs_inode *inode)4027 struct btrfs_inode *inode)
4025{4028{
4026 int ret;4029 int ret;
4027 4030
4028 ret = btrfs_update_inode(trans, root, inode);4031 ret = btrfs_update_inode(trans, inode);
4029 if (ret == -ENOSPC)4032 if (ret == -ENOSPC)
4030 return btrfs_update_inode_item(trans, root, inode);4033 return btrfs_update_inode_item(trans, inode);
4031 return ret;4034 return ret;
4032}4035}
4033 4036
@@ -4133,7 +4136,7 @@ err: · 99 unmodified lines
4133 inode_inc_iversion(&inode->vfs_inode);4136 inode_inc_iversion(&inode->vfs_inode);
4134 inode_inc_iversion(&dir->vfs_inode);4137 inode_inc_iversion(&dir->vfs_inode);
4135 inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode));4138 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);
4137out:4140out:
4138 return ret;4141 return ret;
4139}4142}
@@ -4147,7 +4150,7 @@ int btrfs_unlink_inode(struct btrfs_trans_handle *trans, · 7 unmodified lines
4147 ret = __btrfs_unlink_inode(trans, dir, inode, name, NULL);4150 ret = __btrfs_unlink_inode(trans, dir, inode, name, NULL);
4148 if (!ret) {4151 if (!ret) {
4149 drop_nlink(&inode->vfs_inode);4152 drop_nlink(&inode->vfs_inode);
4150 ret = btrfs_update_inode(trans, inode->root, inode);4153 ret = btrfs_update_inode(trans, inode);
4151 }4154 }
4152 return ret;4155 return ret;
4153}4156}
@@ -4306,7 +4309,7 @@ static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, · 152 unmodified lines
4306 btrfs_i_size_write(dir, dir->vfs_inode.i_size - fname.disk_name.len * 2);4309 btrfs_i_size_write(dir, dir->vfs_inode.i_size - fname.disk_name.len * 2);
4307 inode_inc_iversion(&dir->vfs_inode);4310 inode_inc_iversion(&dir->vfs_inode);
4308 inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode));4311 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);
4310 if (ret)4313 if (ret)
4311 btrfs_abort_transaction(trans, ret);4314 btrfs_abort_transaction(trans, ret);
4312out:4315out:
@@ -4640,7 +4643,8 @@ out_notrans: · 327 unmodified lines
4640}4643}
4641 4644
4642/*4645/*
4643 * btrfs_truncate_block - read, zero a chunk and write a block4646 * Read, zero a chunk and write a block.
4647 *
4644 * @inode - inode that we're zeroing4648 * @inode - inode that we're zeroing
4645 * @from - the offset to start zeroing4649 * @from - the offset to start zeroing
4646 * @len - the length to zero, 0 to zero the entire range respective to the4650 * @len - the length to zero, 0 to zero the entire range respective to the
@@ -4790,9 +4794,9 @@ out: · 143 unmodified lines
4790 return ret;4794 return ret;
4791}4795}
4792 4796
4793static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,4797static int maybe_insert_hole(struct btrfs_inode *inode, u64 offset, u64 len)
4794 u64 offset, u64 len)
4795{4798{
4799 struct btrfs_root *root = inode->root;
4796 struct btrfs_fs_info *fs_info = root->fs_info;4800 struct btrfs_fs_info *fs_info = root->fs_info;
4797 struct btrfs_trans_handle *trans;4801 struct btrfs_trans_handle *trans;
4798 struct btrfs_drop_extents_args drop_args = { 0 };4802 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
4832 btrfs_abort_transaction(trans, ret);4836 btrfs_abort_transaction(trans, ret);
4833 } else {4837 } else {
4834 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);4838 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4835 btrfs_update_inode(trans, root, inode);4839 btrfs_update_inode(trans, inode);
4836 }4840 }
4837 btrfs_end_transaction(trans);4841 btrfs_end_transaction(trans);
4838 return ret;4842 return ret;
@@ -4888,8 +4892,7 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) · 49 unmodified lines
4888 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {4892 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4889 struct extent_map *hole_em;4893 struct extent_map *hole_em;
4890 4894
4891 err = maybe_insert_hole(root, inode, cur_offset,4895 err = maybe_insert_hole(inode, cur_offset, hole_size);
4892 hole_size);
4893 if (err)4896 if (err)
4894 break;4897 break;
4895 4898
@@ -4915,7 +4918,7 @@ int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size) · 19 unmodified lines
4915 hole_em->orig_block_len = 0;4918 hole_em->orig_block_len = 0;
4916 hole_em->ram_bytes = hole_size;4919 hole_em->ram_bytes = hole_size;
4917 hole_em->compress_type = BTRFS_COMPRESS_NONE;4920 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4918 hole_em->generation = fs_info->generation;4921 hole_em->generation = btrfs_get_fs_generation(fs_info);
4919 4922
4920 err = btrfs_replace_extent_map_range(inode, hole_em, true);4923 err = btrfs_replace_extent_map_range(inode, hole_em, true);
4921 free_extent_map(hole_em);4924 free_extent_map(hole_em);
@@ -4984,7 +4987,7 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr) · 62 unmodified lines
4984 i_size_write(inode, newsize);4987 i_size_write(inode, newsize);
4985 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);4988 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
4986 pagecache_isize_extended(inode, oldsize, newsize);4989 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));
4988 btrfs_drew_write_unlock(&root->snapshot_lock);4991 btrfs_drew_write_unlock(&root->snapshot_lock);
4989 btrfs_end_transaction(trans);4992 btrfs_end_transaction(trans);
4990 } else {4993 } else {
@@ -5582,6 +5585,7 @@ static struct inode *new_simple_dir(struct inode *dir, · 591 unmodified lines
5582 struct btrfs_key *key,5585 struct btrfs_key *key,
5583 struct btrfs_root *root)5586 struct btrfs_root *root)
5584{5587{
5588 struct timespec64 ts;
5585 struct inode *inode = new_inode(dir->i_sb);5589 struct inode *inode = new_inode(dir->i_sb);
5586 5590
5587 if (!inode)5591 if (!inode)
@@ -5600,9 +5604,13 @@ static struct inode *new_simple_dir(struct inode *dir, · 12 unmodified lines
5600 inode->i_opflags &= ~IOP_XATTR;5604 inode->i_opflags &= ~IOP_XATTR;
5601 inode->i_fop = &simple_dir_operations;5605 inode->i_fop = &simple_dir_operations;
5602 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;5606 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);
5604 inode_set_atime_to_ts(inode, inode_get_atime(dir));5610 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
5606 inode->i_uid = dir->i_uid;5614 inode->i_uid = dir->i_uid;
5607 inode->i_gid = dir->i_gid;5615 inode->i_gid = dir->i_gid;
5608 5616
@@ -6000,15 +6008,15 @@ static int btrfs_dirty_inode(struct btrfs_inode *inode) · 391 unmodified lines
6000 if (IS_ERR(trans))6008 if (IS_ERR(trans))
6001 return PTR_ERR(trans);6009 return PTR_ERR(trans);
6002 6010
6003 ret = btrfs_update_inode(trans, root, inode);6011 ret = btrfs_update_inode(trans, inode);
6004 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {6012 if (ret == -ENOSPC || ret == -EDQUOT) {
6005 /* whoops, lets try again with the full transaction */6013 /* whoops, lets try again with the full transaction */
6006 btrfs_end_transaction(trans);6014 btrfs_end_transaction(trans);
6007 trans = btrfs_start_transaction(root, 1);6015 trans = btrfs_start_transaction(root, 1);
6008 if (IS_ERR(trans))6016 if (IS_ERR(trans))
6009 return PTR_ERR(trans);6017 return PTR_ERR(trans);
6010 6018
6011 ret = btrfs_update_inode(trans, root, inode);6019 ret = btrfs_update_inode(trans, inode);
6012 }6020 }
6013 btrfs_end_transaction(trans);6021 btrfs_end_transaction(trans);
6014 if (inode->delayed_node)6022 if (inode->delayed_node)
@@ -6024,7 +6032,7 @@ static int btrfs_dirty_inode(struct btrfs_inode *inode) · 9 unmodified lines
6024static int btrfs_update_time(struct inode *inode, int flags)6032static int btrfs_update_time(struct inode *inode, int flags)
6025{6033{
6026 struct btrfs_root *root = BTRFS_I(inode)->root;6034 struct btrfs_root *root = BTRFS_I(inode)->root;
6027 bool dirty = flags & ~S_VERSION;6035 bool dirty;
6028 6036
6029 if (btrfs_root_readonly(root))6037 if (btrfs_root_readonly(root))
6030 return -EROFS;6038 return -EROFS;
@@ -6160,6 +6168,7 @@ static void btrfs_inherit_iflags(struct btrfs_inode *inode, struct btrfs_inode * · 129 unmodified lines
6160int btrfs_create_new_inode(struct btrfs_trans_handle *trans,6168int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
6161 struct btrfs_new_inode_args *args)6169 struct btrfs_new_inode_args *args)
6162{6170{
6171 struct timespec64 ts;
6163 struct inode *dir = args->dir;6172 struct inode *dir = args->dir;
6164 struct inode *inode = args->inode;6173 struct inode *inode = args->inode;
6165 const struct fscrypt_str *name = args->orphan ? NULL : &args->fname.disk_name;6174 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
6277 goto discard;6286 goto discard;
6278 }6287 }
6279 6288
6280 simple_inode_init_ts(inode);6289 ts = simple_inode_init_ts(inode);
6281 BTRFS_I(inode)->i_otime = inode_get_mtime(inode);6290 BTRFS_I(inode)->i_otime_sec = ts.tv_sec;
6291 BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec;
6282 6292
6283 /*6293 /*
6284 * We're going to fill the inode item now, so at this point the inode6294 * 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
6309 }6319 }
6310 }6320 }
6311 6321
6312 btrfs_mark_buffer_dirty(path->nodes[0]);6322 btrfs_mark_buffer_dirty(trans, path->nodes[0]);
6313 /*6323 /*
6314 * We don't need the path anymore, plus inheriting properties, adding6324 * We don't need the path anymore, plus inheriting properties, adding
6315 * ACLs, security xattrs, orphan item or adding the link, will result in6325 * 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
6446 inode_set_mtime_to_ts(&parent_inode->vfs_inode,6456 inode_set_mtime_to_ts(&parent_inode->vfs_inode,
6447 inode_set_ctime_current(&parent_inode->vfs_inode));6457 inode_set_ctime_current(&parent_inode->vfs_inode));
6448 6458
6449 ret = btrfs_update_inode(trans, root, parent_inode);6459 ret = btrfs_update_inode(trans, parent_inode);
6450 if (ret)6460 if (ret)
6451 btrfs_abort_transaction(trans, ret);6461 btrfs_abort_transaction(trans, ret);
6452 return ret;6462 return ret;
@@ -6597,7 +6607,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir, · 144 unmodified lines
6597 } else {6607 } else {
6598 struct dentry *parent = dentry->d_parent;6608 struct dentry *parent = dentry->d_parent;
6599 6609
6600 err = btrfs_update_inode(trans, root, BTRFS_I(inode));6610 err = btrfs_update_inode(trans, BTRFS_I(inode));
6601 if (err)6611 if (err)
6602 goto fail;6612 goto fail;
6603 if (inode->i_nlink == 1) {6613 if (inode->i_nlink == 1) {
@@ -7102,8 +7112,7 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, · 498 unmodified lines
7102 7112
7103 range_end = round_up(offset + nocow_args.num_bytes,7113 range_end = round_up(offset + nocow_args.num_bytes,
7104 root->fs_info->sectorsize) - 1;7114 root->fs_info->sectorsize) - 1;
7105 ret = test_range_bit(io_tree, offset, range_end,7115 ret = test_range_bit_exists(io_tree, offset, range_end, EXTENT_DELALLOC);
7106 EXTENT_DELALLOC, 0, NULL);
7107 if (ret) {7116 if (ret) {
7108 ret = -EAGAIN;7117 ret = -EAGAIN;
7109 goto out;7118 goto out;
@@ -8004,11 +8013,11 @@ static void btrfs_invalidate_folio(struct folio *folio, size_t offset, · 894 unmodified lines
8004 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |8013 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8005 EXTENT_DEFRAG, &cached_state);8014 EXTENT_DEFRAG, &cached_state);
8006 8015
8007 spin_lock_irq(&inode->ordered_tree.lock);8016 spin_lock_irq(&inode->ordered_tree_lock);
8008 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);8017 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8009 ordered->truncated_len = min(ordered->truncated_len,8018 ordered->truncated_len = min(ordered->truncated_len,
8010 cur - ordered->file_offset);8019 cur - ordered->file_offset);
8011 spin_unlock_irq(&inode->ordered_tree.lock);8020 spin_unlock_irq(&inode->ordered_tree_lock);
8012 8021
8013 /*8022 /*
8014 * If the ordered extent has finished, we're safe to delete all8023 * 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
8338 if (ret != -ENOSPC && ret != -EAGAIN)8347 if (ret != -ENOSPC && ret != -EAGAIN)
8339 break;8348 break;
8340 8349
8341 ret = btrfs_update_inode(trans, root, inode);8350 ret = btrfs_update_inode(trans, inode);
8342 if (ret)8351 if (ret)
8343 break;8352 break;
8344 8353
@@ -8391,7 +8400,7 @@ static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback) · 46 unmodified lines
8391 int ret2;8400 int ret2;
8392 8401
8393 trans->block_rsv = &fs_info->trans_block_rsv;8402 trans->block_rsv = &fs_info->trans_block_rsv;
8394 ret2 = btrfs_update_inode(trans, root, inode);8403 ret2 = btrfs_update_inode(trans, inode);
8395 if (ret2 && !ret)8404 if (ret2 && !ret)
8396 ret = ret2;8405 ret = ret2;
8397 8406
@@ -8480,8 +8489,8 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 82 unmodified lines
8480 8489
8481 ei->delayed_node = NULL;8490 ei->delayed_node = NULL;
8482 8491
8483 ei->i_otime.tv_sec = 0;8492 ei->i_otime_sec = 0;
8484 ei->i_otime.tv_nsec = 0;8493 ei->i_otime_nsec = 0;
8485 8494
8486 inode = &ei->vfs_inode;8495 inode = &ei->vfs_inode;
8487 extent_map_tree_init(&ei->extent_tree);8496 extent_map_tree_init(&ei->extent_tree);
@@ -8490,7 +8499,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb) · 2 unmodified lines
8490 extent_io_tree_init(fs_info, &ei->file_extent_tree,8499 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8491 IO_TREE_INODE_FILE_EXTENT);8500 IO_TREE_INODE_FILE_EXTENT);
8492 mutex_init(&ei->log_mutex);8501 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;
8494 INIT_LIST_HEAD(&ei->delalloc_inodes);8505 INIT_LIST_HEAD(&ei->delalloc_inodes);
8495 INIT_LIST_HEAD(&ei->delayed_iput);8506 INIT_LIST_HEAD(&ei->delayed_iput);
8496 RB_CLEAR_NODE(&ei->rb_node);8507 RB_CLEAR_NODE(&ei->rb_node);
@@ -8633,8 +8644,8 @@ static int btrfs_getattr(struct mnt_idmap *idmap, · 136 unmodified lines
8633 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;8644 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
8634 8645
8635 stat->result_mask |= STATX_BTIME;8646 stat->result_mask |= STATX_BTIME;
8636 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;8647 stat->btime.tv_sec = BTRFS_I(inode)->i_otime_sec;
8637 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;8648 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime_nsec;
8638 if (bi_flags & BTRFS_INODE_APPEND)8649 if (bi_flags & BTRFS_INODE_APPEND)
8639 stat->attributes |= STATX_ATTR_APPEND;8650 stat->attributes |= STATX_ATTR_APPEND;
8640 if (bi_flags & BTRFS_INODE_COMPRESS)8651 if (bi_flags & BTRFS_INODE_COMPRESS)
@@ -8822,7 +8833,7 @@ static int btrfs_rename_exchange(struct inode *old_dir, · 181 unmodified lines
8822 BTRFS_I(old_dentry->d_inode),8833 BTRFS_I(old_dentry->d_inode),
8823 old_name, &old_rename_ctx);8834 old_name, &old_rename_ctx);
8824 if (!ret)8835 if (!ret)
8825 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));8836 ret = btrfs_update_inode(trans, BTRFS_I(old_inode));
8826 }8837 }
8827 if (ret) {8838 if (ret) {
8828 btrfs_abort_transaction(trans, ret);8839 btrfs_abort_transaction(trans, ret);
@@ -8837,7 +8848,7 @@ static int btrfs_rename_exchange(struct inode *old_dir, · 8 unmodified lines
8837 BTRFS_I(new_dentry->d_inode),8848 BTRFS_I(new_dentry->d_inode),
8838 new_name, &new_rename_ctx);8849 new_name, &new_rename_ctx);
8839 if (!ret)8850 if (!ret)
8840 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));8851 ret = btrfs_update_inode(trans, BTRFS_I(new_inode));
8841 }8852 }
8842 if (ret) {8853 if (ret) {
8843 btrfs_abort_transaction(trans, ret);8854 btrfs_abort_transaction(trans, ret);
@@ -9082,7 +9093,7 @@ static int btrfs_rename(struct mnt_idmap *idmap, · 238 unmodified lines
9082 BTRFS_I(d_inode(old_dentry)),9093 BTRFS_I(d_inode(old_dentry)),
9083 &old_fname.disk_name, &rename_ctx);9094 &old_fname.disk_name, &rename_ctx);
9084 if (!ret)9095 if (!ret)
9085 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));9096 ret = btrfs_update_inode(trans, BTRFS_I(old_inode));
9086 }9097 }
9087 if (ret) {9098 if (ret) {
9088 btrfs_abort_transaction(trans, ret);9099 btrfs_abort_transaction(trans, ret);
@@ -9207,7 +9218,7 @@ static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode · 118 unmodified lines
9207 init_completion(&work->completion);9218 init_completion(&work->completion);
9208 INIT_LIST_HEAD(&work->list);9219 INIT_LIST_HEAD(&work->list);
9209 work->inode = inode;9220 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);
9211 9222
9212 return work;9223 return work;
9213}9224}
@@ -9445,7 +9456,7 @@ static int btrfs_symlink(struct mnt_idmap *idmap, struct inode *dir, · 231 unmodified lines
9445 9456
9446 ptr = btrfs_file_extent_inline_start(ei);9457 ptr = btrfs_file_extent_inline_start(ei);
9447 write_extent_buffer(leaf, symname, ptr, name_len);9458 write_extent_buffer(leaf, symname, ptr, name_len);
9448 btrfs_mark_buffer_dirty(leaf);9459 btrfs_mark_buffer_dirty(trans, leaf);
9449 btrfs_free_path(path);9460 btrfs_free_path(path);
9450 9461
9451 d_instantiate_new(dentry, inode);9462 d_instantiate_new(dentry, inode);
@@ -9638,7 +9649,7 @@ next: · 186 unmodified lines
9638 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);9649 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
9639 }9650 }
9640 9651
9641 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));9652 ret = btrfs_update_inode(trans, BTRFS_I(inode));
9642 9653
9643 if (ret) {9654 if (ret) {
9644 btrfs_abort_transaction(trans, ret);9655 btrfs_abort_transaction(trans, ret);
fs/btrfs/ioctl.c+15−8
@@ -385,7 +385,7 @@ update_flags: · 384 unmodified lines
385 btrfs_sync_inode_flags_to_i_flags(inode);385 btrfs_sync_inode_flags_to_i_flags(inode);
386 inode_inc_iversion(inode);386 inode_inc_iversion(inode);
387 inode_set_ctime_current(inode);387 inode_set_ctime_current(inode);
388 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));388 ret = btrfs_update_inode(trans, BTRFS_I(inode));
389 389
390 out_end_trans:390 out_end_trans:
391 btrfs_end_transaction(trans);391 btrfs_end_transaction(trans);
@@ -652,18 +652,18 @@ static noinline int create_subvol(struct mnt_idmap *idmap, · 260 unmodified lines
652 /* Tree log can't currently deal with an inode which is a new root. */652 /* Tree log can't currently deal with an inode which is a new root. */
653 btrfs_set_log_full_commit(trans);653 btrfs_set_log_full_commit(trans);
654 654
655 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);655 ret = btrfs_qgroup_inherit(trans, 0, objectid, root->root_key.objectid, inherit);
656 if (ret)656 if (ret)
657 goto out;657 goto out;
658 658
659 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,659 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
660 BTRFS_NESTING_NORMAL);660 0, BTRFS_NESTING_NORMAL);
661 if (IS_ERR(leaf)) {661 if (IS_ERR(leaf)) {
662 ret = PTR_ERR(leaf);662 ret = PTR_ERR(leaf);
663 goto out;663 goto out;
664 }664 }
665 665
666 btrfs_mark_buffer_dirty(leaf);666 btrfs_mark_buffer_dirty(trans, leaf);
667 667
668 inode_item = &root_item->inode;668 inode_item = &root_item->inode;
669 btrfs_set_stack_inode_generation(inode_item, 1);669 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
2635 return -EINVAL;2635 return -EINVAL;
2636 }2636 }
2637 2637
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
2638 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) {2644 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) {
2639 if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD))2645 if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD))
2640 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;2646 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
2820 }2826 }
2821 2827
2822 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {2828 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);
2824 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;2830 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
2825 }2831 }
2826 2832
@@ -2945,7 +2951,7 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) · 118 unmodified lines
2945 2951
2946 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);2952 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2947 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);2953 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]);
2949 btrfs_release_path(path);2955 btrfs_release_path(path);
2950 2956
2951 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);2957 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
3129 return PTR_ERR(trans);3135 return PTR_ERR(trans);
3130 3136
3131 /* No running transaction, don't bother */3137 /* No running transaction, don't bother */
3132 transid = root->fs_info->last_trans_committed;3138 transid = btrfs_get_last_trans_committed(root->fs_info);
3133 goto out;3139 goto out;
3134 }3140 }
3135 transid = trans->transid;3141 transid = trans->transid;
@@ -3695,7 +3701,8 @@ static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) · 559 unmodified lines
3695 3701
3696 switch (sa->cmd) {3702 switch (sa->cmd) {
3697 case BTRFS_QUOTA_CTL_ENABLE:3703 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);
3699 break;3706 break;
3700 case BTRFS_QUOTA_CTL_DISABLE:3707 case BTRFS_QUOTA_CTL_DISABLE:
3701 ret = btrfs_quota_disable(fs_info);3708 ret = btrfs_quota_disable(fs_info);
fs/btrfs/locking.c+16−4
@@ -8,6 +8,7 @@ · 7 unmodified lines
8#include <linux/spinlock.h>8#include <linux/spinlock.h>
9#include <linux/page-flags.h>9#include <linux/page-flags.h>
10#include <asm/bug.h>10#include <asm/bug.h>
11#include <trace/events/btrfs.h>
11#include "misc.h"12#include "misc.h"
12#include "ctree.h"13#include "ctree.h"
13#include "extent_io.h"14#include "extent_io.h"
@@ -73,6 +74,7 @@ static struct btrfs_lockdep_keyset { · 59 unmodified lines
73 { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") },74 { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") },
74 { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") },75 { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") },
75 { .id = BTRFS_BLOCK_GROUP_TREE_OBJECTID, DEFINE_NAME("block-group") },76 { .id = BTRFS_BLOCK_GROUP_TREE_OBJECTID, DEFINE_NAME("block-group") },
77 { .id = BTRFS_RAID_STRIPE_TREE_OBJECTID, DEFINE_NAME("raid-stripe") },
76 { .id = 0, DEFINE_NAME("tree") },78 { .id = 0, DEFINE_NAME("tree") },
77};79};
78 80
@@ -102,6 +104,15 @@ void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root, struct extent_buff · 23 unmodified lines
102 104
103#endif105#endif
104 106
107#ifdef CONFIG_BTRFS_DEBUG
108static void btrfs_set_eb_lock_owner(struct extent_buffer *eb, pid_t owner)
109{
110 eb->lock_owner = owner;
111}
112#else
113static void btrfs_set_eb_lock_owner(struct extent_buffer *eb, pid_t owner) { }
114#endif
115
105/*116/*
106 * Extent buffer locking117 * Extent buffer locking
107 * =====================118 * =====================
@@ -164,7 +175,7 @@ int btrfs_try_tree_read_lock(struct extent_buffer *eb) · 56 unmodified lines
164int btrfs_try_tree_write_lock(struct extent_buffer *eb)175int btrfs_try_tree_write_lock(struct extent_buffer *eb)
165{176{
166 if (down_write_trylock(&eb->lock)) {177 if (down_write_trylock(&eb->lock)) {
167 eb->lock_owner = current->pid;178 btrfs_set_eb_lock_owner(eb, current->pid);
168 trace_btrfs_try_tree_write_lock(eb);179 trace_btrfs_try_tree_write_lock(eb);
169 return 1;180 return 1;
170 }181 }
@@ -181,7 +192,8 @@ void btrfs_tree_read_unlock(struct extent_buffer *eb) · 10 unmodified lines
181}192}
182 193
183/*194/*
184 * __btrfs_tree_lock - lock eb for write195 * Lock eb for write.
196 *
185 * @eb: the eb to lock197 * @eb: the eb to lock
186 * @nest: the nesting to use for the lock198 * @nest: the nesting to use for the lock
187 *199 *
@@ -196,7 +208,7 @@ void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest) · 8 unmodified lines
196 start_ns = ktime_get_ns();208 start_ns = ktime_get_ns();
197 209
198 down_write_nested(&eb->lock, nest);210 down_write_nested(&eb->lock, nest);
199 eb->lock_owner = current->pid;211 btrfs_set_eb_lock_owner(eb, current->pid);
200 trace_btrfs_tree_lock(eb, start_ns);212 trace_btrfs_tree_lock(eb, start_ns);
201}213}
202 214
@@ -211,7 +223,7 @@ void btrfs_tree_lock(struct extent_buffer *eb) · 8 unmodified lines
211void btrfs_tree_unlock(struct extent_buffer *eb)223void btrfs_tree_unlock(struct extent_buffer *eb)
212{224{
213 trace_btrfs_tree_unlock(eb);225 trace_btrfs_tree_unlock(eb);
214 eb->lock_owner = 0;226 btrfs_set_eb_lock_owner(eb, 0);
215 up_write(&eb->lock);227 up_write(&eb->lock);
216}228}
217 229
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
72 * over the error. Each subsequent error that doesn't have any context72 * over the error. Each subsequent error that doesn't have any context
73 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.73 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
74 */74 */
75const char * __attribute_const__ btrfs_decode_error(int errno)75const char * __attribute_const__ btrfs_decode_error(int error)
76{76{
77 char *errstr = "unknown";77 char *errstr = "unknown";
78 78
79 switch (errno) {79 switch (error) {
80 case -ENOENT: /* -2 */80 case -ENOENT: /* -2 */
81 errstr = "No such entry";81 errstr = "No such entry";
82 break;82 break;
@@ -110,12 +110,12 @@ const char * __attribute_const__ btrfs_decode_error(int errno) · 27 unmodified lines
110}110}
111 111
112/*112/*
113 * __btrfs_handle_fs_error decodes expected errors from the caller and113 * Decodes expected errors from the caller and invokes the appropriate error
114 * invokes the appropriate error response.114 * response.
115 */115 */
116__cold116__cold
117void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,117void __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, ...)
119{119{
120 struct super_block *sb = fs_info->sb;120 struct super_block *sb = fs_info->sb;
121#ifdef CONFIG_PRINTK121#ifdef CONFIG_PRINTK
@@ -132,11 +132,11 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function · 10 unmodified lines
132 * Special case: if the error is EROFS, and we're already under132 * Special case: if the error is EROFS, and we're already under
133 * SB_RDONLY, then it is safe here.133 * SB_RDONLY, then it is safe here.
134 */134 */
135 if (errno == -EROFS && sb_rdonly(sb))135 if (error == -EROFS && sb_rdonly(sb))
136 return;136 return;
137 137
138#ifdef CONFIG_PRINTK138#ifdef CONFIG_PRINTK
139 errstr = btrfs_decode_error(errno);139 errstr = btrfs_decode_error(error);
140 btrfs_state_to_string(fs_info, statestr);140 btrfs_state_to_string(fs_info, statestr);
141 if (fmt) {141 if (fmt) {
142 struct va_format vaf;142 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
147 vaf.va = &args;147 vaf.va = &args;
148 148
149 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n",149 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);
151 va_end(args);151 va_end(args);
152 } else {152 } else {
153 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",153 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);
155 }155 }
156#endif156#endif
157 157
@@ -159,7 +159,7 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function · 1 unmodified line
159 * Today we only save the error info to memory. Long term we'll also159 * Today we only save the error info to memory. Long term we'll also
160 * send it down to the disk.160 * send it down to the disk.
161 */161 */
162 WRITE_ONCE(fs_info->fs_error, errno);162 WRITE_ONCE(fs_info->fs_error, error);
163 163
164 /* Don't go through full error handling during mount. */164 /* Don't go through full error handling during mount. */
165 if (!(sb->s_flags & SB_BORN))165 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
283#endif283#endif
284 284
285/*285/*
286 * __btrfs_panic decodes unexpected, fatal errors from the caller, issues an286 * Decode unexpected, fatal errors from the caller, issue an alert, and either
287 * alert, and either panics or BUGs, depending on mount options.287 * panic or BUGs, depending on mount options.
288 */288 */
289__cold289__cold
290void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,290void __btrfs_panic(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, ...)
292{292{
293 char *s_id = "<unknown>";293 char *s_id = "<unknown>";
294 const char *errstr;294 const char *errstr;
@@ -301,13 +301,13 @@ void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, · 6 unmodified lines
301 va_start(args, fmt);301 va_start(args, fmt);
302 vaf.va = &args;302 vaf.va = &args;
303 303
304 errstr = btrfs_decode_error(errno);304 errstr = btrfs_decode_error(error);
305 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))305 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
306 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",306 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);
308 308
309 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",309 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
310 function, line, &vaf, errno, errstr);310 function, line, &vaf, error, errstr);
311 va_end(args);311 va_end(args);
312 /* Caller calls BUG() */312 /* Caller calls BUG() */
313}313}
fs/btrfs/messages.h+7−7
@@ -184,25 +184,25 @@ do { \ · 183 unmodified lines
184__printf(5, 6)184__printf(5, 6)
185__cold185__cold
186void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,186void __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, ...);
188 188
189const char * __attribute_const__ btrfs_decode_error(int errno);189const char * __attribute_const__ btrfs_decode_error(int error);
190 190
191#define btrfs_handle_fs_error(fs_info, errno, fmt, args...) \191#define btrfs_handle_fs_error(fs_info, error, fmt, args...) \
192 __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \192 __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \
193 (errno), fmt, ##args)193 (error), fmt, ##args)
194 194
195__printf(5, 6)195__printf(5, 6)
196__cold196__cold
197void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,197void __btrfs_panic(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, ...);
199/*199/*
200 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic200 * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
201 * will panic(). Otherwise we BUG() here.201 * will panic(). Otherwise we BUG() here.
202 */202 */
203#define btrfs_panic(fs_info, errno, fmt, args...) \203#define btrfs_panic(fs_info, error, fmt, args...) \
204do { \204do { \
205 __btrfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args); \205 __btrfs_panic(fs_info, __func__, __LINE__, error, fmt, ##args); \
206 BUG(); \206 BUG(); \
207} while (0)207} while (0)
208 208
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
124 * look find the first ordered struct that has this offset, otherwise124 * look find the first ordered struct that has this offset, otherwise
125 * the first one less than this offset125 * the first one less than this offset
126 */126 */
127static inline struct rb_node *tree_search(struct btrfs_ordered_inode_tree *tree,127static inline struct rb_node *ordered_tree_search(struct btrfs_inode *inode,
128 u64 file_offset)128 u64 file_offset)
129{129{
130 struct rb_root *root = &tree->tree;
131 struct rb_node *prev = NULL;130 struct rb_node *prev = NULL;
132 struct rb_node *ret;131 struct rb_node *ret;
133 struct btrfs_ordered_extent *entry;132 struct btrfs_ordered_extent *entry;
134 133
135 if (tree->last) {134 if (inode->ordered_tree_last) {
136 entry = rb_entry(tree->last, struct btrfs_ordered_extent,135 entry = rb_entry(inode->ordered_tree_last, struct btrfs_ordered_extent,
137 rb_node);136 rb_node);
138 if (in_range(file_offset, entry->file_offset, entry->num_bytes))137 if (in_range(file_offset, entry->file_offset, entry->num_bytes))
139 return tree->last;138 return inode->ordered_tree_last;
140 }139 }
141 ret = __tree_search(root, file_offset, &prev);140 ret = __tree_search(&inode->ordered_tree, file_offset, &prev);
142 if (!ret)141 if (!ret)
143 ret = prev;142 ret = prev;
144 if (ret)143 if (ret)
145 tree->last = ret;144 inode->ordered_tree_last = ret;
146 return ret;145 return ret;
147}146}
148 147
@@ -191,6 +190,7 @@ static struct btrfs_ordered_extent *alloc_ordered_extent( · 42 unmodified lines
191 INIT_LIST_HEAD(&entry->log_list);190 INIT_LIST_HEAD(&entry->log_list);
192 INIT_LIST_HEAD(&entry->root_extent_list);191 INIT_LIST_HEAD(&entry->root_extent_list);
193 INIT_LIST_HEAD(&entry->work_list);192 INIT_LIST_HEAD(&entry->work_list);
193 INIT_LIST_HEAD(&entry->bioc_list);
194 init_completion(&entry->completion);194 init_completion(&entry->completion);
195 195
196 /*196 /*
@@ -208,7 +208,6 @@ static struct btrfs_ordered_extent *alloc_ordered_extent( · 11 unmodified lines
208static void insert_ordered_extent(struct btrfs_ordered_extent *entry)208static void insert_ordered_extent(struct btrfs_ordered_extent *entry)
209{209{
210 struct btrfs_inode *inode = BTRFS_I(entry->inode);210 struct btrfs_inode *inode = BTRFS_I(entry->inode);
211 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
212 struct btrfs_root *root = inode->root;211 struct btrfs_root *root = inode->root;
213 struct btrfs_fs_info *fs_info = root->fs_info;212 struct btrfs_fs_info *fs_info = root->fs_info;
214 struct rb_node *node;213 struct rb_node *node;
@@ -221,13 +220,14 @@ static void insert_ordered_extent(struct btrfs_ordered_extent *entry) · 6 unmodified lines
221 /* One ref for the tree. */220 /* One ref for the tree. */
222 refcount_inc(&entry->refs);221 refcount_inc(&entry->refs);
223 222
224 spin_lock_irq(&tree->lock);223 spin_lock_irq(&inode->ordered_tree_lock);
225 node = tree_insert(&tree->tree, entry->file_offset, &entry->rb_node);224 node = tree_insert(&inode->ordered_tree, entry->file_offset,
225 &entry->rb_node);
226 if (node)226 if (node)
227 btrfs_panic(fs_info, -EEXIST,227 btrfs_panic(fs_info, -EEXIST,
228 "inconsistency in ordered tree at offset %llu",228 "inconsistency in ordered tree at offset %llu",
229 entry->file_offset);229 entry->file_offset);
230 spin_unlock_irq(&tree->lock);230 spin_unlock_irq(&inode->ordered_tree_lock);
231 231
232 spin_lock(&root->ordered_extent_lock);232 spin_lock(&root->ordered_extent_lock);
233 list_add_tail(&entry->root_extent_list,233 list_add_tail(&entry->root_extent_list,
@@ -287,12 +287,11 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( · 53 unmodified lines
287void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,287void btrfs_add_ordered_sum(struct btrfs_ordered_extent *entry,
288 struct btrfs_ordered_sum *sum)288 struct btrfs_ordered_sum *sum)
289{289{
290 struct btrfs_ordered_inode_tree *tree;290 struct btrfs_inode *inode = BTRFS_I(entry->inode);
291 291
292 tree = &BTRFS_I(entry->inode)->ordered_tree;292 spin_lock_irq(&inode->ordered_tree_lock);
293 spin_lock_irq(&tree->lock);
294 list_add_tail(&sum->list, &entry->list);293 list_add_tail(&sum->list, &entry->list);
295 spin_unlock_irq(&tree->lock);294 spin_unlock_irq(&inode->ordered_tree_lock);
296}295}
297 296
298static void finish_ordered_fn(struct btrfs_work *work)297static 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
310 struct btrfs_inode *inode = BTRFS_I(ordered->inode);309 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
311 struct btrfs_fs_info *fs_info = inode->root->fs_info;310 struct btrfs_fs_info *fs_info = inode->root->fs_info;
312 311
313 lockdep_assert_held(&inode->ordered_tree.lock);312 lockdep_assert_held(&inode->ordered_tree_lock);
314 313
315 if (page) {314 if (page) {
316 ASSERT(page->mapping);315 ASSERT(page->mapping);
@@ -364,7 +363,7 @@ static void btrfs_queue_ordered_fn(struct btrfs_ordered_extent *ordered) · 47 unmodified lines
364 struct btrfs_workqueue *wq = btrfs_is_free_space_inode(inode) ?363 struct btrfs_workqueue *wq = btrfs_is_free_space_inode(inode) ?
365 fs_info->endio_freespace_worker : fs_info->endio_write_workers;364 fs_info->endio_freespace_worker : fs_info->endio_write_workers;
366 365
367 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL, NULL);366 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL);
368 btrfs_queue_work(wq, &ordered->work);367 btrfs_queue_work(wq, &ordered->work);
369}368}
370 369
@@ -378,9 +377,9 @@ bool btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered, · 7 unmodified lines
378 377
379 trace_btrfs_finish_ordered_extent(inode, file_offset, len, uptodate);378 trace_btrfs_finish_ordered_extent(inode, file_offset, len, uptodate);
380 379
381 spin_lock_irqsave(&inode->ordered_tree.lock, flags);380 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
382 ret = can_finish_ordered_extent(ordered, page, file_offset, len, uptodate);381 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);
384 383
385 if (ret)384 if (ret)
386 btrfs_queue_ordered_fn(ordered);385 btrfs_queue_ordered_fn(ordered);
@@ -404,7 +403,6 @@ void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, · 17 unmodified lines
404 struct page *page, u64 file_offset,403 struct page *page, u64 file_offset,
405 u64 num_bytes, bool uptodate)404 u64 num_bytes, bool uptodate)
406{405{
407 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
408 struct rb_node *node;406 struct rb_node *node;
409 struct btrfs_ordered_extent *entry = NULL;407 struct btrfs_ordered_extent *entry = NULL;
410 unsigned long flags;408 unsigned long flags;
@@ -414,13 +412,13 @@ void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, · 3 unmodified lines
414 file_offset + num_bytes - 1,412 file_offset + num_bytes - 1,
415 uptodate);413 uptodate);
416 414
417 spin_lock_irqsave(&tree->lock, flags);415 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
418 while (cur < file_offset + num_bytes) {416 while (cur < file_offset + num_bytes) {
419 u64 entry_end;417 u64 entry_end;
420 u64 end;418 u64 end;
421 u32 len;419 u32 len;
422 420
423 node = tree_search(tree, cur);421 node = ordered_tree_search(inode, cur);
424 /* No ordered extents at all */422 /* No ordered extents at all */
425 if (!node)423 if (!node)
426 break;424 break;
@@ -467,13 +465,13 @@ void btrfs_mark_ordered_io_finished(struct btrfs_inode *inode, · 40 unmodified lines
467 len = end + 1 - cur;465 len = end + 1 - cur;
468 466
469 if (can_finish_ordered_extent(entry, page, cur, len, uptodate)) {467 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);
471 btrfs_queue_ordered_fn(entry);469 btrfs_queue_ordered_fn(entry);
472 spin_lock_irqsave(&tree->lock, flags);470 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
473 }471 }
474 cur += len;472 cur += len;
475 }473 }
476 spin_unlock_irqrestore(&tree->lock, flags);474 spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
477}475}
478 476
479/*477/*
@@ -497,19 +495,18 @@ bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode, · 17 unmodified lines
497 struct btrfs_ordered_extent **cached,495 struct btrfs_ordered_extent **cached,
498 u64 file_offset, u64 io_size)496 u64 file_offset, u64 io_size)
499{497{
500 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
501 struct rb_node *node;498 struct rb_node *node;
502 struct btrfs_ordered_extent *entry = NULL;499 struct btrfs_ordered_extent *entry = NULL;
503 unsigned long flags;500 unsigned long flags;
504 bool finished = false;501 bool finished = false;
505 502
506 spin_lock_irqsave(&tree->lock, flags);503 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
507 if (cached && *cached) {504 if (cached && *cached) {
508 entry = *cached;505 entry = *cached;
509 goto have_entry;506 goto have_entry;
510 }507 }
511 508
512 node = tree_search(tree, file_offset);509 node = ordered_tree_search(inode, file_offset);
513 if (!node)510 if (!node)
514 goto out;511 goto out;
515 512
@@ -540,7 +537,7 @@ out: · 24 unmodified lines
540 refcount_inc(&entry->refs);537 refcount_inc(&entry->refs);
541 trace_btrfs_ordered_extent_dec_test_pending(inode, entry);538 trace_btrfs_ordered_extent_dec_test_pending(inode, entry);
542 }539 }
543 spin_unlock_irqrestore(&tree->lock, flags);540 spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
544 return finished;541 return finished;
545}542}
546 543
@@ -578,7 +575,6 @@ void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry) · 31 unmodified lines
578void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,575void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
579 struct btrfs_ordered_extent *entry)576 struct btrfs_ordered_extent *entry)
580{577{
581 struct btrfs_ordered_inode_tree *tree;
582 struct btrfs_root *root = btrfs_inode->root;578 struct btrfs_root *root = btrfs_inode->root;
583 struct btrfs_fs_info *fs_info = root->fs_info;579 struct btrfs_fs_info *fs_info = root->fs_info;
584 struct rb_node *node;580 struct rb_node *node;
@@ -609,16 +605,15 @@ void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode, · 24 unmodified lines
609 percpu_counter_add_batch(&fs_info->ordered_bytes, -entry->num_bytes,605 percpu_counter_add_batch(&fs_info->ordered_bytes, -entry->num_bytes,
610 fs_info->delalloc_batch);606 fs_info->delalloc_batch);
611 607
612 tree = &btrfs_inode->ordered_tree;608 spin_lock_irq(&btrfs_inode->ordered_tree_lock);
613 spin_lock_irq(&tree->lock);
614 node = &entry->rb_node;609 node = &entry->rb_node;
615 rb_erase(node, &tree->tree);610 rb_erase(node, &btrfs_inode->ordered_tree);
616 RB_CLEAR_NODE(node);611 RB_CLEAR_NODE(node);
617 if (tree->last == node)612 if (btrfs_inode->ordered_tree_last == node)
618 tree->last = NULL;613 btrfs_inode->ordered_tree_last = NULL;
619 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);614 set_bit(BTRFS_ORDERED_COMPLETE, &entry->flags);
620 pending = test_and_clear_bit(BTRFS_ORDERED_PENDING, &entry->flags);615 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);
622 617
623 /*618 /*
624 * The current running transaction is waiting on us, we need to let it619 * 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
711 spin_unlock(&root->ordered_extent_lock);706 spin_unlock(&root->ordered_extent_lock);
712 707
713 btrfs_init_work(&ordered->flush_work,708 btrfs_init_work(&ordered->flush_work,
714 btrfs_run_ordered_extent_work, NULL, NULL);709 btrfs_run_ordered_extent_work, NULL);
715 list_add_tail(&ordered->work_list, &works);710 list_add_tail(&ordered->work_list, &works);
716 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);711 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
717 712
@@ -875,14 +870,12 @@ int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len) · 157 unmodified lines
875struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,870struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *inode,
876 u64 file_offset)871 u64 file_offset)
877{872{
878 struct btrfs_ordered_inode_tree *tree;
879 struct rb_node *node;873 struct rb_node *node;
880 struct btrfs_ordered_extent *entry = NULL;874 struct btrfs_ordered_extent *entry = NULL;
881 unsigned long flags;875 unsigned long flags;
882 876
883 tree = &inode->ordered_tree;877 spin_lock_irqsave(&inode->ordered_tree_lock, flags);
884 spin_lock_irqsave(&tree->lock, flags);878 node = ordered_tree_search(inode, file_offset);
885 node = tree_search(tree, file_offset);
886 if (!node)879 if (!node)
887 goto out;880 goto out;
888 881
@@ -894,7 +887,7 @@ struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct btrfs_inode *ino · 5 unmodified lines
894 trace_btrfs_ordered_extent_lookup(inode, entry);887 trace_btrfs_ordered_extent_lookup(inode, entry);
895 }888 }
896out:889out:
897 spin_unlock_irqrestore(&tree->lock, flags);890 spin_unlock_irqrestore(&inode->ordered_tree_lock, flags);
898 return entry;891 return entry;
899}892}
900 893
@@ -904,15 +897,13 @@ out: · 3 unmodified lines
904struct btrfs_ordered_extent *btrfs_lookup_ordered_range(897struct btrfs_ordered_extent *btrfs_lookup_ordered_range(
905 struct btrfs_inode *inode, u64 file_offset, u64 len)898 struct btrfs_inode *inode, u64 file_offset, u64 len)
906{899{
907 struct btrfs_ordered_inode_tree *tree;
908 struct rb_node *node;900 struct rb_node *node;
909 struct btrfs_ordered_extent *entry = NULL;901 struct btrfs_ordered_extent *entry = NULL;
910 902
911 tree = &inode->ordered_tree;903 spin_lock_irq(&inode->ordered_tree_lock);
912 spin_lock_irq(&tree->lock);904 node = ordered_tree_search(inode, file_offset);
913 node = tree_search(tree, file_offset);
914 if (!node) {905 if (!node) {
915 node = tree_search(tree, file_offset + len);906 node = ordered_tree_search(inode, file_offset + len);
916 if (!node)907 if (!node)
917 goto out;908 goto out;
918 }909 }
@@ -936,7 +927,7 @@ out: · 17 unmodified lines
936 refcount_inc(&entry->refs);927 refcount_inc(&entry->refs);
937 trace_btrfs_ordered_extent_lookup_range(inode, entry);928 trace_btrfs_ordered_extent_lookup_range(inode, entry);
938 }929 }
939 spin_unlock_irq(&tree->lock);930 spin_unlock_irq(&inode->ordered_tree_lock);
940 return entry;931 return entry;
941}932}
942 933
@@ -947,13 +938,12 @@ out: · 4 unmodified lines
947void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,938void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
948 struct list_head *list)939 struct list_head *list)
949{940{
950 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
951 struct rb_node *n;941 struct rb_node *n;
952 942
953 ASSERT(inode_is_locked(&inode->vfs_inode));943 ASSERT(inode_is_locked(&inode->vfs_inode));
954 944
955 spin_lock_irq(&tree->lock);945 spin_lock_irq(&inode->ordered_tree_lock);
956 for (n = rb_first(&tree->tree); n; n = rb_next(n)) {946 for (n = rb_first(&inode->ordered_tree); n; n = rb_next(n)) {
957 struct btrfs_ordered_extent *ordered;947 struct btrfs_ordered_extent *ordered;
958 948
959 ordered = rb_entry(n, struct btrfs_ordered_extent, rb_node);949 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
966 refcount_inc(&ordered->refs);956 refcount_inc(&ordered->refs);
967 trace_btrfs_ordered_extent_lookup_for_logging(inode, ordered);957 trace_btrfs_ordered_extent_lookup_for_logging(inode, ordered);
968 }958 }
969 spin_unlock_irq(&tree->lock);959 spin_unlock_irq(&inode->ordered_tree_lock);
970}960}
971 961
972/*962/*
@@ -976,13 +966,11 @@ void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode, · 3 unmodified lines
976struct btrfs_ordered_extent *966struct btrfs_ordered_extent *
977btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset)967btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset)
978{968{
979 struct btrfs_ordered_inode_tree *tree;
980 struct rb_node *node;969 struct rb_node *node;
981 struct btrfs_ordered_extent *entry = NULL;970 struct btrfs_ordered_extent *entry = NULL;
982 971
983 tree = &inode->ordered_tree;972 spin_lock_irq(&inode->ordered_tree_lock);
984 spin_lock_irq(&tree->lock);973 node = ordered_tree_search(inode, file_offset);
985 node = tree_search(tree, file_offset);
986 if (!node)974 if (!node)
987 goto out;975 goto out;
988 976
@@ -990,7 +978,7 @@ btrfs_lookup_first_ordered_extent(struct btrfs_inode *inode, u64 file_offset) · 1 unmodified line
990 refcount_inc(&entry->refs);978 refcount_inc(&entry->refs);
991 trace_btrfs_ordered_extent_lookup_first(inode, entry);979 trace_btrfs_ordered_extent_lookup_first(inode, entry);
992out:980out:
993 spin_unlock_irq(&tree->lock);981 spin_unlock_irq(&inode->ordered_tree_lock);
994 return entry;982 return entry;
995}983}
996 984
@@ -1006,15 +994,14 @@ out: · 9 unmodified lines
1006struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(994struct btrfs_ordered_extent *btrfs_lookup_first_ordered_range(
1007 struct btrfs_inode *inode, u64 file_offset, u64 len)995 struct btrfs_inode *inode, u64 file_offset, u64 len)
1008{996{
1009 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
1010 struct rb_node *node;997 struct rb_node *node;
1011 struct rb_node *cur;998 struct rb_node *cur;
1012 struct rb_node *prev;999 struct rb_node *prev;
1013 struct rb_node *next;1000 struct rb_node *next;
1014 struct btrfs_ordered_extent *entry = NULL;1001 struct btrfs_ordered_extent *entry = NULL;
1015 1002
1016 spin_lock_irq(&tree->lock);1003 spin_lock_irq(&inode->ordered_tree_lock);
1017 node = tree->tree.rb_node;1004 node = inode->ordered_tree.rb_node;
1018 /*1005 /*
1019 * Here we don't want to use tree_search() which will use tree->last1006 * Here we don't want to use tree_search() which will use tree->last
1020 * and screw up the search order.1007 * and screw up the search order.
@@ -1068,7 +1055,7 @@ out: · 47 unmodified lines
1068 trace_btrfs_ordered_extent_lookup_first_range(inode, entry);1055 trace_btrfs_ordered_extent_lookup_first_range(inode, entry);
1069 }1056 }
1070 1057
1071 spin_unlock_irq(&tree->lock);1058 spin_unlock_irq(&inode->ordered_tree_lock);
1072 return entry;1059 return entry;
1073}1060}
1074 1061
@@ -1147,7 +1134,6 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent( · 72 unmodified lines
1147 struct btrfs_ordered_extent *ordered, u64 len)1134 struct btrfs_ordered_extent *ordered, u64 len)
1148{1135{
1149 struct btrfs_inode *inode = BTRFS_I(ordered->inode);1136 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1150 struct btrfs_ordered_inode_tree *tree = &inode->ordered_tree;
1151 struct btrfs_root *root = inode->root;1137 struct btrfs_root *root = inode->root;
1152 struct btrfs_fs_info *fs_info = root->fs_info;1138 struct btrfs_fs_info *fs_info = root->fs_info;
1153 u64 file_offset = ordered->file_offset;1139 u64 file_offset = ordered->file_offset;
@@ -1187,13 +1173,13 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent( · 33 unmodified lines
1187 refcount_inc(&new->refs);1173 refcount_inc(&new->refs);
1188 1174
1189 spin_lock_irq(&root->ordered_extent_lock);1175 spin_lock_irq(&root->ordered_extent_lock);
1190 spin_lock(&tree->lock);1176 spin_lock(&inode->ordered_tree_lock);
1191 /* Remove from tree once */1177 /* Remove from tree once */
1192 node = &ordered->rb_node;1178 node = &ordered->rb_node;
1193 rb_erase(node, &tree->tree);1179 rb_erase(node, &inode->ordered_tree);
1194 RB_CLEAR_NODE(node);1180 RB_CLEAR_NODE(node);
1195 if (tree->last == node)1181 if (inode->ordered_tree_last == node)
1196 tree->last = NULL;1182 inode->ordered_tree_last = NULL;
1197 1183
1198 ordered->file_offset += len;1184 ordered->file_offset += len;
1199 ordered->disk_bytenr += len;1185 ordered->disk_bytenr += len;
@@ -1224,18 +1210,19 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent( · 24 unmodified lines
1224 }1210 }
1225 1211
1226 /* Re-insert the node */1212 /* 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);
1228 if (node)1215 if (node)
1229 btrfs_panic(fs_info, -EEXIST,1216 btrfs_panic(fs_info, -EEXIST,
1230 "zoned: inconsistency in ordered tree at offset %llu",1217 "zoned: inconsistency in ordered tree at offset %llu",
1231 ordered->file_offset);1218 ordered->file_offset);
1232 1219
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);
1234 if (node)1221 if (node)
1235 btrfs_panic(fs_info, -EEXIST,1222 btrfs_panic(fs_info, -EEXIST,
1236 "zoned: inconsistency in ordered tree at offset %llu",1223 "zoned: inconsistency in ordered tree at offset %llu",
1237 new->file_offset);1224 new->file_offset);
1238 spin_unlock(&tree->lock);1225 spin_unlock(&inode->ordered_tree_lock);
1239 1226
1240 list_add_tail(&new->root_extent_list, &root->ordered_extents);1227 list_add_tail(&new->root_extent_list, &root->ordered_extents);
1241 root->nr_ordered_extents++;1228 root->nr_ordered_extents++;
fs/btrfs/ordered-data.h+2−15
@@ -6,13 +6,6 @@ · 5 unmodified lines
6#ifndef BTRFS_ORDERED_DATA_H6#ifndef BTRFS_ORDERED_DATA_H
7#define BTRFS_ORDERED_DATA_H7#define BTRFS_ORDERED_DATA_H
8 8
9/* one of these per inode */
10struct btrfs_ordered_inode_tree {
11 spinlock_t lock;
12 struct rb_root tree;
13 struct rb_node *last;
14};
15
16struct btrfs_ordered_sum {9struct btrfs_ordered_sum {
17 /*10 /*
18 * Logical start address and length for of the blocks covered by11 * Logical start address and length for of the blocks covered by
@@ -151,15 +144,9 @@ struct btrfs_ordered_extent { · 132 unmodified lines
151 struct completion completion;144 struct completion completion;
152 struct btrfs_work flush_work;145 struct btrfs_work flush_work;
153 struct list_head work_list;146 struct list_head work_list;
154};
155 147
156static inline void148 struct list_head bioc_list;
157btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)149};
158{
159 spin_lock_init(&t->lock);
160 t->tree = RB_ROOT;
161 t->last = NULL;
162}
163 150
164int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent);151int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent);
165int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);152int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
fs/btrfs/print-tree.c+35−0
@@ -9,6 +9,8 @@ · 8 unmodified lines
9#include "print-tree.h"9#include "print-tree.h"
10#include "accessors.h"10#include "accessors.h"
11#include "tree-checker.h"11#include "tree-checker.h"
12#include "volumes.h"
13#include "raid-stripe-tree.h"
12 14
13struct root_name_map {15struct root_name_map {
14 u64 id;16 u64 id;
@@ -28,6 +30,7 @@ static const struct root_name_map root_map[] = { · 13 unmodified lines
28 { BTRFS_FREE_SPACE_TREE_OBJECTID, "FREE_SPACE_TREE" },30 { BTRFS_FREE_SPACE_TREE_OBJECTID, "FREE_SPACE_TREE" },
29 { BTRFS_BLOCK_GROUP_TREE_OBJECTID, "BLOCK_GROUP_TREE" },31 { BTRFS_BLOCK_GROUP_TREE_OBJECTID, "BLOCK_GROUP_TREE" },
30 { BTRFS_DATA_RELOC_TREE_OBJECTID, "DATA_RELOC_TREE" },32 { BTRFS_DATA_RELOC_TREE_OBJECTID, "DATA_RELOC_TREE" },
33 { BTRFS_RAID_STRIPE_TREE_OBJECTID, "RAID_STRIPE_TREE" },
31};34};
32 35
33const char *btrfs_root_name(const struct btrfs_key *key, char *buf)36const 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
80 btrfs_extent_data_ref_count(eb, ref));83 btrfs_extent_data_ref_count(eb, ref));
81}84}
82 85
86static 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
83static void print_extent_item(const struct extent_buffer *eb, int slot, int type)93static void print_extent_item(const struct extent_buffer *eb, int slot, int type)
84{94{
85 struct btrfs_extent_item *ei;95 struct btrfs_extent_item *ei;
86 struct btrfs_extent_inline_ref *iref;96 struct btrfs_extent_inline_ref *iref;
87 struct btrfs_extent_data_ref *dref;97 struct btrfs_extent_data_ref *dref;
88 struct btrfs_shared_data_ref *sref;98 struct btrfs_shared_data_ref *sref;
99 struct btrfs_extent_owner_ref *oref;
89 struct btrfs_disk_key key;100 struct btrfs_disk_key key;
90 unsigned long end;101 unsigned long end;
91 unsigned long ptr;102 unsigned long ptr;
@@ -161,6 +172,10 @@ static void print_extent_item(const struct extent_buffer *eb, int slot, int type · 69 unmodified lines
161 "\t\t\t(parent %llu not aligned to sectorsize %u)\n",172 "\t\t\t(parent %llu not aligned to sectorsize %u)\n",
162 offset, eb->fs_info->sectorsize);173 offset, eb->fs_info->sectorsize);
163 break;174 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;
164 default:179 default:
165 pr_cont("(extent %llu has INVALID ref type %d)\n",180 pr_cont("(extent %llu has INVALID ref type %d)\n",
166 eb->start, type);181 eb->start, type);
@@ -189,6 +204,22 @@ static void print_uuid_item(const struct extent_buffer *l, unsigned long offset, · 22 unmodified lines
189 }204 }
190}205}
191 206
207static 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
192/*223/*
193 * Helper to output refs and locking status of extent buffer. Useful to debug224 * Helper to output refs and locking status of extent buffer. Useful to debug
194 * race condition related problems.225 * race condition related problems.
@@ -349,6 +380,10 @@ void btrfs_print_leaf(const struct extent_buffer *l) · 154 unmodified lines
349 print_uuid_item(l, btrfs_item_ptr_offset(l, i),380 print_uuid_item(l, btrfs_item_ptr_offset(l, i),
350 btrfs_item_size(l, i));381 btrfs_item_size(l, i));
351 break;382 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;
352 }387 }
353 }388 }
354}389}
fs/btrfs/props.c+1−0
@@ -15,6 +15,7 @@ · 14 unmodified lines
15#include "fs.h"15#include "fs.h"
16#include "accessors.h"16#include "accessors.h"
17#include "super.h"17#include "super.h"
18#include "dir-item.h"
18 19
19#define BTRFS_PROP_HANDLERS_HT_BITS 820#define BTRFS_PROP_HANDLERS_HT_BITS 8
20static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);21static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);
fs/btrfs/qgroup.c+560−312
@@ -30,6 +30,25 @@ · 29 unmodified lines
30#include "root-tree.h"30#include "root-tree.h"
31#include "tree-checker.h"31#include "tree-checker.h"
32 32
33enum 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
42bool btrfs_qgroup_enabled(struct btrfs_fs_info *fs_info)
43{
44 return btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_DISABLED;
45}
46
47bool btrfs_qgroup_full_accounting(struct btrfs_fs_info *fs_info)
48{
49 return btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL;
50}
51
33/*52/*
34 * Helpers to access qgroup reservation53 * Helpers to access qgroup reservation
35 *54 *
@@ -146,16 +165,6 @@ struct btrfs_qgroup_list { · 110 unmodified lines
146 struct btrfs_qgroup *member;165 struct btrfs_qgroup *member;
147};166};
148 167
149static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
150{
151 return (u64)(uintptr_t)qg;
152}
153
154static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
155{
156 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
157}
158
159static int168static int
160qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,169qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
161 int init_flags);170 int init_flags);
@@ -180,34 +189,46 @@ static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info, · 18 unmodified lines
180 return NULL;189 return NULL;
181}190}
182 191
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 */
184static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,200static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
201 struct btrfs_qgroup *prealloc,
185 u64 qgroupid)202 u64 qgroupid)
186{203{
187 struct rb_node **p = &fs_info->qgroup_tree.rb_node;204 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
188 struct rb_node *parent = NULL;205 struct rb_node *parent = NULL;
189 struct btrfs_qgroup *qgroup;206 struct btrfs_qgroup *qgroup;
190 207
208 /* Caller must have pre-allocated @prealloc. */
209 ASSERT(prealloc);
210
191 while (*p) {211 while (*p) {
192 parent = *p;212 parent = *p;
193 qgroup = rb_entry(parent, struct btrfs_qgroup, node);213 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
194 214
195 if (qgroup->qgroupid < qgroupid)215 if (qgroup->qgroupid < qgroupid) {
196 p = &(*p)->rb_left;216 p = &(*p)->rb_left;
197 else if (qgroup->qgroupid > qgroupid)217 } else if (qgroup->qgroupid > qgroupid) {
198 p = &(*p)->rb_right;218 p = &(*p)->rb_right;
199 else219 } else {
220 kfree(prealloc);
200 return qgroup;221 return qgroup;
222 }
201 }223 }
202 224
203 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);225 qgroup = prealloc;
204 if (!qgroup)
205 return ERR_PTR(-ENOMEM);
206
207 qgroup->qgroupid = qgroupid;226 qgroup->qgroupid = qgroupid;
208 INIT_LIST_HEAD(&qgroup->groups);227 INIT_LIST_HEAD(&qgroup->groups);
209 INIT_LIST_HEAD(&qgroup->members);228 INIT_LIST_HEAD(&qgroup->members);
210 INIT_LIST_HEAD(&qgroup->dirty);229 INIT_LIST_HEAD(&qgroup->dirty);
230 INIT_LIST_HEAD(&qgroup->iterator);
231 INIT_LIST_HEAD(&qgroup->nested_iterator);
211 232
212 rb_link_node(&qgroup->node, parent, p);233 rb_link_node(&qgroup->node, parent, p);
213 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);234 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
254/*275/*
255 * Add relation specified by two qgroups.276 * Add relation specified by two qgroups.
256 *277 *
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.
258 *280 *
259 * Return: 0 on success281 * Return: 0 on success
260 * -ENOENT if one of the qgroups is NULL282 * -ENOENT if one of the qgroups is NULL
261 * <0 other errors283 * <0 other errors
262 */284 */
263static int __add_relation_rb(struct btrfs_qgroup *member, struct btrfs_qgroup *parent)285static int __add_relation_rb(struct btrfs_qgroup_list *prealloc,
286 struct btrfs_qgroup *member,
287 struct btrfs_qgroup *parent)
264{288{
265 struct btrfs_qgroup_list *list;289 if (!member || !parent) {
266 290 kfree(prealloc);
267 if (!member || !parent)
268 return -ENOENT;291 return -ENOENT;
292 }
269 293
270 list = kzalloc(sizeof(*list), GFP_ATOMIC);294 prealloc->group = parent;
271 if (!list)295 prealloc->member = member;
272 return -ENOMEM;296 list_add_tail(&prealloc->next_group, &member->groups);
273 297 list_add_tail(&prealloc->next_member, &parent->members);
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);
278 298
279 return 0;299 return 0;
280}300}
@@ -288,7 +308,9 @@ static int __add_relation_rb(struct btrfs_qgroup *member, struct btrfs_qgroup *p · 7 unmodified lines
288 * -ENOENT if one of the ids does not exist308 * -ENOENT if one of the ids does not exist
289 * <0 other errors309 * <0 other errors
290 */310 */
291static int add_relation_rb(struct btrfs_fs_info *fs_info, u64 memberid, u64 parentid)311static int add_relation_rb(struct btrfs_fs_info *fs_info,
312 struct btrfs_qgroup_list *prealloc,
313 u64 memberid, u64 parentid)
292{314{
293 struct btrfs_qgroup *member;315 struct btrfs_qgroup *member;
294 struct btrfs_qgroup *parent;316 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
296 member = find_qgroup_rb(fs_info, memberid);318 member = find_qgroup_rb(fs_info, memberid);
297 parent = find_qgroup_rb(fs_info, parentid);319 parent = find_qgroup_rb(fs_info, parentid);
298 320
299 return __add_relation_rb(member, parent);321 return __add_relation_rb(prealloc, member, parent);
300}322}
301 323
302/* Must be called with qgroup_lock held */324/* 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
340 362
341static void qgroup_mark_inconsistent(struct btrfs_fs_info *fs_info)363static void qgroup_mark_inconsistent(struct btrfs_fs_info *fs_info)
342{364{
365 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
366 return;
343 fs_info->qgroup_flags |= (BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT |367 fs_info->qgroup_flags |= (BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT |
344 BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN |368 BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN |
345 BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING);369 BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING);
346}370}
347 371
372static 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
348/*381/*
349 * The full config is read in one go, only called from open_ctree()382 * The full config is read in one go, only called from open_ctree()
350 * It doesn't use any locking, as at this point we're still single-threaded383 * 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
361 u64 flags = 0;394 u64 flags = 0;
362 u64 rescan_progress = 0;395 u64 rescan_progress = 0;
363 396
364 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))397 if (!fs_info->quota_root)
365 return 0;398 return 0;
366 399
367 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);400 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
411 "old qgroup version, quota disabled");444 "old qgroup version, quota disabled");
412 goto out;445 goto out;
413 }446 }
414 if (btrfs_qgroup_status_generation(l, ptr) !=447 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l, ptr);
415 fs_info->generation) {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) {
416 qgroup_mark_inconsistent(fs_info);451 qgroup_mark_inconsistent(fs_info);
417 btrfs_err(fs_info,452 btrfs_err(fs_info,
418 "qgroup generation mismatch, marked as inconsistent");453 "qgroup generation mismatch, marked as inconsistent");
419 }454 }
420 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
421 ptr);
422 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);455 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
423 goto next1;456 goto next1;
424 }457 }
@@ -434,11 +467,14 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) · 9 unmodified lines
434 qgroup_mark_inconsistent(fs_info);467 qgroup_mark_inconsistent(fs_info);
435 }468 }
436 if (!qgroup) {469 if (!qgroup) {
437 qgroup = add_qgroup_rb(fs_info, found_key.offset);470 struct btrfs_qgroup *prealloc;
438 if (IS_ERR(qgroup)) {471
439 ret = PTR_ERR(qgroup);472 prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL);
473 if (!prealloc) {
474 ret = -ENOMEM;
440 goto out;475 goto out;
441 }476 }
477 qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset);
442 }478 }
443 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);479 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
444 if (ret < 0)480 if (ret < 0)
@@ -489,6 +525,8 @@ next1: · 44 unmodified lines
489 if (ret)525 if (ret)
490 goto out;526 goto out;
491 while (1) {527 while (1) {
528 struct btrfs_qgroup_list *list = NULL;
529
492 slot = path->slots[0];530 slot = path->slots[0];
493 l = path->nodes[0];531 l = path->nodes[0];
494 btrfs_item_key_to_cpu(l, &found_key, slot);532 btrfs_item_key_to_cpu(l, &found_key, slot);
@@ -502,8 +540,14 @@ next1: · 7 unmodified lines
502 goto next2;540 goto next2;
503 }541 }
504 542
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,
506 found_key.offset);549 found_key.offset);
550 list = NULL;
507 if (ret == -ENOENT) {551 if (ret == -ENOENT) {
508 btrfs_warn(fs_info,552 btrfs_warn(fs_info,
509 "orphan qgroup relation 0x%llx->0x%llx",553 "orphan qgroup relation 0x%llx->0x%llx",
@@ -522,13 +566,12 @@ next2: · 12 unmodified lines
522out:566out:
523 btrfs_free_path(path);567 btrfs_free_path(path);
524 fs_info->qgroup_flags |= flags;568 fs_info->qgroup_flags |= flags;
525 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))569 if (ret >= 0) {
526 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);570 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)
527 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&571 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
528 ret >= 0)572 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
529 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);573 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
530 574 } else {
531 if (ret < 0) {
532 ulist_free(fs_info->qgroup_ulist);575 ulist_free(fs_info->qgroup_ulist);
533 fs_info->qgroup_ulist = NULL;576 fs_info->qgroup_ulist = NULL;
534 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;577 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
550 struct rb_node *node;593 struct rb_node *node;
551 bool ret = false;594 bool ret = false;
552 595
553 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))596 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
554 return ret;597 return ret;
555 /*598 /*
556 * Since we're unmounting, there is no race and no need to grab qgroup599 * 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
622 665
623 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);666 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
624 667
625 btrfs_mark_buffer_dirty(path->nodes[0]);668 btrfs_mark_buffer_dirty(trans, path->nodes[0]);
626 669
627 btrfs_free_path(path);670 btrfs_free_path(path);
628 return ret;671 return ret;
@@ -700,7 +743,7 @@ static int add_qgroup_item(struct btrfs_trans_handle *trans, · 71 unmodified lines
700 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);743 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
701 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);744 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
702 745
703 btrfs_mark_buffer_dirty(leaf);746 btrfs_mark_buffer_dirty(trans, leaf);
704 747
705 btrfs_release_path(path);748 btrfs_release_path(path);
706 749
@@ -719,7 +762,7 @@ static int add_qgroup_item(struct btrfs_trans_handle *trans, · 12 unmodified lines
719 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);762 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
720 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);763 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
721 764
722 btrfs_mark_buffer_dirty(leaf);765 btrfs_mark_buffer_dirty(trans, leaf);
723 766
724 ret = 0;767 ret = 0;
725out:768out:
@@ -808,7 +851,7 @@ static int update_qgroup_limit_item(struct btrfs_trans_handle *trans, · 82 unmodified lines
808 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);851 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
809 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);852 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
810 853
811 btrfs_mark_buffer_dirty(l);854 btrfs_mark_buffer_dirty(trans, l);
812 855
813out:856out:
814 btrfs_free_path(path);857 btrfs_free_path(path);
@@ -854,7 +897,7 @@ static int update_qgroup_info_item(struct btrfs_trans_handle *trans, · 39 unmodified lines
854 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);897 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
855 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);898 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
856 899
857 btrfs_mark_buffer_dirty(l);900 btrfs_mark_buffer_dirty(trans, l);
858 901
859out:902out:
860 btrfs_free_path(path);903 btrfs_free_path(path);
@@ -896,7 +939,7 @@ static int update_qgroup_status_item(struct btrfs_trans_handle *trans) · 35 unmodified lines
896 btrfs_set_qgroup_status_rescan(l, ptr,939 btrfs_set_qgroup_status_rescan(l, ptr,
897 fs_info->qgroup_rescan_progress.objectid);940 fs_info->qgroup_rescan_progress.objectid);
898 941
899 btrfs_mark_buffer_dirty(l);942 btrfs_mark_buffer_dirty(trans, l);
900 943
901out:944out:
902 btrfs_free_path(path);945 btrfs_free_path(path);
@@ -949,7 +992,8 @@ out: · 46 unmodified lines
949 return ret;992 return ret;
950}993}
951 994
952int btrfs_quota_enable(struct btrfs_fs_info *fs_info)995int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
996 struct btrfs_ioctl_quota_ctl_args *quota_ctl_args)
953{997{
954 struct btrfs_root *quota_root;998 struct btrfs_root *quota_root;
955 struct btrfs_root *tree_root = fs_info->tree_root;999 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
959 struct btrfs_key key;1003 struct btrfs_key key;
960 struct btrfs_key found_key;1004 struct btrfs_key found_key;
961 struct btrfs_qgroup *qgroup = NULL;1005 struct btrfs_qgroup *qgroup = NULL;
1006 struct btrfs_qgroup *prealloc = NULL;
962 struct btrfs_trans_handle *trans = NULL;1007 struct btrfs_trans_handle *trans = NULL;
963 struct ulist *ulist = NULL;1008 struct ulist *ulist = NULL;
1009 const bool simple = (quota_ctl_args->cmd == BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA);
964 int ret = 0;1010 int ret = 0;
965 int slot;1011 int slot;
966 1012
@@ -1063,13 +1109,18 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info) · 96 unmodified lines
1063 struct btrfs_qgroup_status_item);1109 struct btrfs_qgroup_status_item);
1064 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);1110 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
1065 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);1111 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
1066 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |1112 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON;
1067 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;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 }
1068 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags &1119 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags &
1069 BTRFS_QGROUP_STATUS_FLAGS_MASK);1120 BTRFS_QGROUP_STATUS_FLAGS_MASK);
1070 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);1121 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
1071 1122
1072 btrfs_mark_buffer_dirty(leaf);1123 btrfs_mark_buffer_dirty(trans, leaf);
1073 1124
1074 key.objectid = 0;1125 key.objectid = 0;
1075 key.type = BTRFS_ROOT_REF_KEY;1126 key.type = BTRFS_ROOT_REF_KEY;
@@ -1094,6 +1145,15 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info) · 18 unmodified lines
1094 /* Release locks on tree_root before we access quota_root */1145 /* Release locks on tree_root before we access quota_root */
1095 btrfs_release_path(path);1146 btrfs_release_path(path);
1096 1147
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
1097 ret = add_qgroup_item(trans, quota_root,1157 ret = add_qgroup_item(trans, quota_root,
1098 found_key.offset);1158 found_key.offset);
1099 if (ret) {1159 if (ret) {
@@ -1101,7 +1161,8 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info) · 1 unmodified line
1101 goto out_free_path;1161 goto out_free_path;
1102 }1162 }
1103 1163
1104 qgroup = add_qgroup_rb(fs_info, found_key.offset);1164 qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset);
1165 prealloc = NULL;
1105 if (IS_ERR(qgroup)) {1166 if (IS_ERR(qgroup)) {
1106 ret = PTR_ERR(qgroup);1167 ret = PTR_ERR(qgroup);
1107 btrfs_abort_transaction(trans, ret);1168 btrfs_abort_transaction(trans, ret);
@@ -1144,18 +1205,22 @@ out_add_root: · 36 unmodified lines
1144 goto out_free_path;1205 goto out_free_path;
1145 }1206 }
1146 1207
1147 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);1208 ASSERT(prealloc == NULL);
1148 if (IS_ERR(qgroup)) {1209 prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
1149 ret = PTR_ERR(qgroup);1210 if (!prealloc) {
1150 btrfs_abort_transaction(trans, ret);1211 ret = -ENOMEM;
1151 goto out_free_path;1212 goto out_free_path;
1152 }1213 }
1214 qgroup = add_qgroup_rb(fs_info, prealloc, BTRFS_FS_TREE_OBJECTID);
1215 prealloc = NULL;
1153 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);1216 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1154 if (ret < 0) {1217 if (ret < 0) {
1155 btrfs_abort_transaction(trans, ret);1218 btrfs_abort_transaction(trans, ret);
1156 goto out_free_path;1219 goto out_free_path;
1157 }1220 }
1158 1221
1222 fs_info->qgroup_enable_gen = trans->transid;
1223
1159 mutex_unlock(&fs_info->qgroup_ioctl_lock);1224 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1160 /*1225 /*
1161 * Commit the transaction while not holding qgroup_ioctl_lock, to avoid1226 * Commit the transaction while not holding qgroup_ioctl_lock, to avoid
@@ -1180,8 +1245,14 @@ out_add_root: · 18 unmodified lines
1180 spin_lock(&fs_info->qgroup_lock);1245 spin_lock(&fs_info->qgroup_lock);
1181 fs_info->quota_root = quota_root;1246 fs_info->quota_root = quota_root;
1182 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);1247 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1248 if (simple)
1249 btrfs_set_fs_incompat(fs_info, SIMPLE_QUOTA);
1183 spin_unlock(&fs_info->qgroup_lock);1250 spin_unlock(&fs_info->qgroup_lock);
1184 1251
1252 /* Skip rescan for simple qgroups. */
1253 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
1254 goto out_free_path;
1255
1185 ret = qgroup_rescan_init(fs_info, 0, 1);1256 ret = qgroup_rescan_init(fs_info, 0, 1);
1186 if (!ret) {1257 if (!ret) {
1187 qgroup_rescan_zero_tracking(fs_info);1258 qgroup_rescan_zero_tracking(fs_info);
@@ -1222,6 +1293,39 @@ out: · 34 unmodified lines
1222 else if (trans)1293 else if (trans)
1223 ret = btrfs_end_transaction(trans);1294 ret = btrfs_end_transaction(trans);
1224 ulist_free(ulist);1295 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 */
1315static 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
1225 return ret;1329 return ret;
1226}1330}
1227 1331
@@ -1269,6 +1373,10 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info) · 41 unmodified lines
1269 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);1373 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1270 btrfs_qgroup_wait_for_completion(fs_info, false);1374 btrfs_qgroup_wait_for_completion(fs_info, false);
1271 1375
1376 ret = flush_reservations(fs_info);
1377 if (ret)
1378 goto out_unlock_cleaner;
1379
1272 /*1380 /*
1273 * 1 For the root item1381 * 1 For the root item
1274 *1382 *
@@ -1295,6 +1403,7 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info) · 20 unmodified lines
1295 quota_root = fs_info->quota_root;1403 quota_root = fs_info->quota_root;
1296 fs_info->quota_root = NULL;1404 fs_info->quota_root = NULL;
1297 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;1405 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1406 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
1298 fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;1407 fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;
1299 spin_unlock(&fs_info->qgroup_lock);1408 spin_unlock(&fs_info->qgroup_lock);
1300 1409
@@ -1329,7 +1438,8 @@ out: · 28 unmodified lines
1329 if (ret && trans)1438 if (ret && trans)
1330 btrfs_end_transaction(trans);1439 btrfs_end_transaction(trans);
1331 else if (trans)1440 else if (trans)
1332 ret = btrfs_end_transaction(trans);1441 ret = btrfs_commit_transaction(trans);
1442out_unlock_cleaner:
1333 mutex_unlock(&fs_info->cleaner_mutex);1443 mutex_unlock(&fs_info->cleaner_mutex);
1334 1444
1335 return ret;1445 return ret;
@@ -1342,6 +1452,24 @@ static void qgroup_dirty(struct btrfs_fs_info *fs_info, · 6 unmodified lines
1342 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);1452 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1343}1453}
1344 1454
1455static 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
1463static 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
1345/*1473/*
1346 * The easy accounting, we're updating qgroup relationship whose child qgroup1474 * The easy accounting, we're updating qgroup relationship whose child qgroup
1347 * only has exclusive extents.1475 * only has exclusive extents.
@@ -1356,14 +1484,12 @@ static void qgroup_dirty(struct btrfs_fs_info *fs_info, · 8 unmodified lines
1356 *1484 *
1357 * Caller should hold fs_info->qgroup_lock.1485 * Caller should hold fs_info->qgroup_lock.
1358 */1486 */
1359static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,1487static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info, u64 ref_root,
1360 struct ulist *tmp, u64 ref_root,
1361 struct btrfs_qgroup *src, int sign)1488 struct btrfs_qgroup *src, int sign)
1362{1489{
1363 struct btrfs_qgroup *qgroup;1490 struct btrfs_qgroup *qgroup;
1364 struct btrfs_qgroup_list *glist;1491 struct btrfs_qgroup *cur;
1365 struct ulist_node *unode;1492 LIST_HEAD(qgroup_list);
1366 struct ulist_iterator uiter;
1367 u64 num_bytes = src->excl;1493 u64 num_bytes = src->excl;
1368 int ret = 0;1494 int ret = 0;
1369 1495
@@ -1371,53 +1497,30 @@ static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info, · 1 unmodified line
1371 if (!qgroup)1497 if (!qgroup)
1372 goto out;1498 goto out;
1373 1499
1374 qgroup->rfer += sign * num_bytes;1500 qgroup_iterator_add(&qgroup_list, qgroup);
1375 qgroup->rfer_cmpr += sign * num_bytes;1501 list_for_each_entry(cur, &qgroup_list, iterator) {
1376 1502 struct btrfs_qgroup_list *glist;
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 }
1395 1503
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);
1400 qgroup->rfer += sign * num_bytes;1504 qgroup->rfer += sign * num_bytes;
1401 qgroup->rfer_cmpr += sign * num_bytes;1505 qgroup->rfer_cmpr += sign * num_bytes;
1506
1402 WARN_ON(sign < 0 && qgroup->excl < num_bytes);1507 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1403 qgroup->excl += sign * num_bytes;1508 qgroup->excl += sign * num_bytes;
1509 qgroup->excl_cmpr += sign * num_bytes;
1510
1404 if (sign > 0)1511 if (sign > 0)
1405 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);1512 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1406 else1513 else
1407 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);1514 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1408 qgroup->excl_cmpr += sign * num_bytes;
1409 qgroup_dirty(fs_info, qgroup);1515 qgroup_dirty(fs_info, qgroup);
1410 1516
1411 /* Add any parents of the parents */1517 /* Append parent qgroups to @qgroup_list. */
1412 list_for_each_entry(glist, &qgroup->groups, next_group) {1518 list_for_each_entry(glist, &qgroup->groups, next_group)
1413 ret = ulist_add(tmp, glist->group->qgroupid,1519 qgroup_iterator_add(&qgroup_list, glist->group);
1414 qgroup_to_aux(glist->group), GFP_ATOMIC);
1415 if (ret < 0)
1416 goto out;
1417 }
1418 }1520 }
1419 ret = 0;1521 ret = 0;
1420out:1522out:
1523 qgroup_iterator_clean(&qgroup_list);
1421 return ret;1524 return ret;
1422}1525}
1423 1526
@@ -1434,8 +1537,7 @@ out: · 10 unmodified lines
1434 * Return < 0 for other error.1537 * Return < 0 for other error.
1435 */1538 */
1436static int quick_update_accounting(struct btrfs_fs_info *fs_info,1539static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1437 struct ulist *tmp, u64 src, u64 dst,1540 u64 src, u64 dst, int sign)
1438 int sign)
1439{1541{
1440 struct btrfs_qgroup *qgroup;1542 struct btrfs_qgroup *qgroup;
1441 int ret = 1;1543 int ret = 1;
@@ -1446,8 +1548,7 @@ static int quick_update_accounting(struct btrfs_fs_info *fs_info, · 4 unmodified lines
1446 goto out;1548 goto out;
1447 if (qgroup->excl == qgroup->rfer) {1549 if (qgroup->excl == qgroup->rfer) {
1448 ret = 0;1550 ret = 0;
1449 err = __qgroup_excl_accounting(fs_info, tmp, dst,1551 err = __qgroup_excl_accounting(fs_info, dst, qgroup, sign);
1450 qgroup, sign);
1451 if (err < 0) {1552 if (err < 0) {
1452 ret = err;1553 ret = err;
1453 goto out;1554 goto out;
@@ -1459,28 +1560,19 @@ out: · 5 unmodified lines
1459 return ret;1560 return ret;
1460}1561}
1461 1562
1462int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,1563int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst)
1463 u64 dst)
1464{1564{
1465 struct btrfs_fs_info *fs_info = trans->fs_info;1565 struct btrfs_fs_info *fs_info = trans->fs_info;
1466 struct btrfs_qgroup *parent;1566 struct btrfs_qgroup *parent;
1467 struct btrfs_qgroup *member;1567 struct btrfs_qgroup *member;
1468 struct btrfs_qgroup_list *list;1568 struct btrfs_qgroup_list *list;
1469 struct ulist *tmp;1569 struct btrfs_qgroup_list *prealloc = NULL;
1470 unsigned int nofs_flag;
1471 int ret = 0;1570 int ret = 0;
1472 1571
1473 /* Check the level of src and dst first */1572 /* Check the level of src and dst first */
1474 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))1573 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1475 return -EINVAL;1574 return -EINVAL;
1476 1575
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
1484 mutex_lock(&fs_info->qgroup_ioctl_lock);1576 mutex_lock(&fs_info->qgroup_ioctl_lock);
1485 if (!fs_info->quota_root) {1577 if (!fs_info->quota_root) {
1486 ret = -ENOTCONN;1578 ret = -ENOTCONN;
@@ -1501,6 +1593,11 @@ int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, · 14 unmodified lines
1501 }1593 }
1502 }1594 }
1503 1595
1596 prealloc = kzalloc(sizeof(*list), GFP_NOFS);
1597 if (!prealloc) {
1598 ret = -ENOMEM;
1599 goto out;
1600 }
1504 ret = add_qgroup_relation_item(trans, src, dst);1601 ret = add_qgroup_relation_item(trans, src, dst);
1505 if (ret)1602 if (ret)
1506 goto out;1603 goto out;
@@ -1512,16 +1609,17 @@ int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, · 5 unmodified lines
1512 }1609 }
1513 1610
1514 spin_lock(&fs_info->qgroup_lock);1611 spin_lock(&fs_info->qgroup_lock);
1515 ret = __add_relation_rb(member, parent);1612 ret = __add_relation_rb(prealloc, member, parent);
1613 prealloc = NULL;
1516 if (ret < 0) {1614 if (ret < 0) {
1517 spin_unlock(&fs_info->qgroup_lock);1615 spin_unlock(&fs_info->qgroup_lock);
1518 goto out;1616 goto out;
1519 }1617 }
1520 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);1618 ret = quick_update_accounting(fs_info, src, dst, 1);
1521 spin_unlock(&fs_info->qgroup_lock);1619 spin_unlock(&fs_info->qgroup_lock);
1522out:1620out:
1621 kfree(prealloc);
1523 mutex_unlock(&fs_info->qgroup_ioctl_lock);1622 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1524 ulist_free(tmp);
1525 return ret;1623 return ret;
1526}1624}
1527 1625
@@ -1532,19 +1630,10 @@ static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, · 4 unmodified lines
1532 struct btrfs_qgroup *parent;1630 struct btrfs_qgroup *parent;
1533 struct btrfs_qgroup *member;1631 struct btrfs_qgroup *member;
1534 struct btrfs_qgroup_list *list;1632 struct btrfs_qgroup_list *list;
1535 struct ulist *tmp;
1536 bool found = false;1633 bool found = false;
1537 unsigned int nofs_flag;
1538 int ret = 0;1634 int ret = 0;
1539 int ret2;1635 int ret2;
1540 1636
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
1548 if (!fs_info->quota_root) {1637 if (!fs_info->quota_root) {
1549 ret = -ENOTCONN;1638 ret = -ENOTCONN;
1550 goto out;1639 goto out;
@@ -1582,11 +1671,10 @@ delete_item: · 31 unmodified lines
1582 if (found) {1671 if (found) {
1583 spin_lock(&fs_info->qgroup_lock);1672 spin_lock(&fs_info->qgroup_lock);
1584 del_relation_rb(fs_info, src, dst);1673 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);
1586 spin_unlock(&fs_info->qgroup_lock);1675 spin_unlock(&fs_info->qgroup_lock);
1587 }1676 }
1588out:1677out:
1589 ulist_free(tmp);
1590 return ret;1678 return ret;
1591}1679}
1592 1680
@@ -1608,8 +1696,12 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid) · 15 unmodified lines
1608 struct btrfs_fs_info *fs_info = trans->fs_info;1696 struct btrfs_fs_info *fs_info = trans->fs_info;
1609 struct btrfs_root *quota_root;1697 struct btrfs_root *quota_root;
1610 struct btrfs_qgroup *qgroup;1698 struct btrfs_qgroup *qgroup;
1699 struct btrfs_qgroup *prealloc = NULL;
1611 int ret = 0;1700 int ret = 0;
1612 1701
1702 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
1703 return 0;
1704
1613 mutex_lock(&fs_info->qgroup_ioctl_lock);1705 mutex_lock(&fs_info->qgroup_ioctl_lock);
1614 if (!fs_info->quota_root) {1706 if (!fs_info->quota_root) {
1615 ret = -ENOTCONN;1707 ret = -ENOTCONN;
@@ -1622,21 +1714,25 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid) · 6 unmodified lines
1622 goto out;1714 goto out;
1623 }1715 }
1624 1716
1717 prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
1718 if (!prealloc) {
1719 ret = -ENOMEM;
1720 goto out;
1721 }
1722
1625 ret = add_qgroup_item(trans, quota_root, qgroupid);1723 ret = add_qgroup_item(trans, quota_root, qgroupid);
1626 if (ret)1724 if (ret)
1627 goto out;1725 goto out;
1628 1726
1629 spin_lock(&fs_info->qgroup_lock);1727 spin_lock(&fs_info->qgroup_lock);
1630 qgroup = add_qgroup_rb(fs_info, qgroupid);1728 qgroup = add_qgroup_rb(fs_info, prealloc, qgroupid);
1631 spin_unlock(&fs_info->qgroup_lock);1729 spin_unlock(&fs_info->qgroup_lock);
1730 prealloc = NULL;
1632 1731
1633 if (IS_ERR(qgroup)) {
1634 ret = PTR_ERR(qgroup);
1635 goto out;
1636 }
1637 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);1732 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1638out:1733out:
1639 mutex_unlock(&fs_info->qgroup_ioctl_lock);1734 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1735 kfree(prealloc);
1640 return ret;1736 return ret;
1641}1737}
1642 1738
@@ -1771,6 +1867,17 @@ out: · 128 unmodified lines
1771 return ret;1867 return ret;
1772}1868}
1773 1869
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 */
1774int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,1881int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
1775 struct btrfs_delayed_ref_root *delayed_refs,1882 struct btrfs_delayed_ref_root *delayed_refs,
1776 struct btrfs_qgroup_extent_record *record)1883 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
1780 struct btrfs_qgroup_extent_record *entry;1887 struct btrfs_qgroup_extent_record *entry;
1781 u64 bytenr = record->bytenr;1888 u64 bytenr = record->bytenr;
1782 1889
1890 if (!btrfs_qgroup_full_accounting(fs_info))
1891 return 0;
1892
1783 lockdep_assert_held(&delayed_refs->lock);1893 lockdep_assert_held(&delayed_refs->lock);
1784 trace_btrfs_qgroup_trace_extent(fs_info, record);1894 trace_btrfs_qgroup_trace_extent(fs_info, record);
1785 1895
@@ -1806,12 +1916,35 @@ int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info, · 20 unmodified lines
1806 return 0;1916 return 0;
1807}1917}
1808 1918
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 */
1809int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,1940int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,
1810 struct btrfs_qgroup_extent_record *qrecord)1941 struct btrfs_qgroup_extent_record *qrecord)
1811{1942{
1812 struct btrfs_backref_walk_ctx ctx = { 0 };1943 struct btrfs_backref_walk_ctx ctx = { 0 };
1813 int ret;1944 int ret;
1814 1945
1946 if (!btrfs_qgroup_full_accounting(trans->fs_info))
1947 return 0;
1815 /*1948 /*
1816 * We are always called in a context where we are already holding a1949 * We are always called in a context where we are already holding a
1817 * transaction handle. Often we are called when adding a data delayed1950 * 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
1859 return 0;1992 return 0;
1860}1993}
1861 1994
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 */
1862int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,2008int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
1863 u64 num_bytes)2009 u64 num_bytes)
1864{2010{
@@ -1867,8 +2013,7 @@ int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 2 unmodified lines
1867 struct btrfs_delayed_ref_root *delayed_refs;2013 struct btrfs_delayed_ref_root *delayed_refs;
1868 int ret;2014 int ret;
1869 2015
1870 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)2016 if (!btrfs_qgroup_full_accounting(fs_info) || bytenr == 0 || num_bytes == 0)
1871 || bytenr == 0 || num_bytes == 0)
1872 return 0;2017 return 0;
1873 record = kzalloc(sizeof(*record), GFP_NOFS);2018 record = kzalloc(sizeof(*record), GFP_NOFS);
1874 if (!record)2019 if (!record)
@@ -1889,6 +2034,12 @@ int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 14 unmodified lines
1889 return btrfs_qgroup_trace_extent_post(trans, record);2034 return btrfs_qgroup_trace_extent_post(trans, record);
1890}2035}
1891 2036
2037/*
2038 * Inform qgroup to trace all leaf items of data
2039 *
2040 * Return 0 for success
2041 * Return <0 for error(ENOMEM)
2042 */
1892int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,2043int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1893 struct extent_buffer *eb)2044 struct extent_buffer *eb)
1894{2045{
@@ -1900,7 +2051,7 @@ int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans, · 5 unmodified lines
1900 u64 bytenr, num_bytes;2051 u64 bytenr, num_bytes;
1901 2052
1902 /* We can be called directly from walk_up_proc() */2053 /* 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))
1904 return 0;2055 return 0;
1905 2056
1906 for (i = 0; i < nr; i++) {2057 for (i = 0; i < nr; i++) {
@@ -2276,7 +2427,7 @@ static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans, · 369 unmodified lines
2276 int level;2427 int level;
2277 int ret;2428 int ret;
2278 2429
2279 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))2430 if (!btrfs_qgroup_full_accounting(fs_info))
2280 return 0;2431 return 0;
2281 2432
2282 /* Wrong parameter order */2433 /* Wrong parameter order */
@@ -2319,6 +2470,16 @@ out: · 36 unmodified lines
2319 return ret;2470 return ret;
2320}2471}
2321 2472
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 */
2322int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,2483int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
2323 struct extent_buffer *root_eb,2484 struct extent_buffer *root_eb,
2324 u64 root_gen, int root_level)2485 u64 root_gen, int root_level)
@@ -2333,7 +2494,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans, · 8 unmodified lines
2333 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);2494 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
2334 BUG_ON(root_eb == NULL);2495 BUG_ON(root_eb == NULL);
2335 2496
2336 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))2497 if (!btrfs_qgroup_full_accounting(fs_info))
2337 return 0;2498 return 0;
2338 2499
2339 spin_lock(&fs_info->qgroup_lock);2500 spin_lock(&fs_info->qgroup_lock);
@@ -2445,62 +2606,64 @@ out: · 105 unmodified lines
2445 return ret;2606 return ret;
2446}2607}
2447 2608
2609static 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
2617static 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
2448#define UPDATE_NEW 02627#define UPDATE_NEW 0
2449#define UPDATE_OLD 12628#define UPDATE_OLD 1
2450/*2629/*
2451 * Walk all of the roots that points to the bytenr and adjust their refcnts.2630 * Walk all of the roots that points to the bytenr and adjust their refcnts.
2452 */2631 */
2453static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,2632static void qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
2454 struct ulist *roots, struct ulist *tmp,2633 struct ulist *roots, struct list_head *qgroups,
2455 struct ulist *qgroups, u64 seq, int update_old)2634 u64 seq, int update_old)
2456{2635{
2457 struct ulist_node *unode;2636 struct ulist_node *unode;
2458 struct ulist_iterator uiter;2637 struct ulist_iterator uiter;
2459 struct ulist_node *tmp_unode;
2460 struct ulist_iterator tmp_uiter;
2461 struct btrfs_qgroup *qg;2638 struct btrfs_qgroup *qg;
2462 int ret = 0;
2463 2639
2464 if (!roots)2640 if (!roots)
2465 return 0;2641 return;
2466 ULIST_ITER_INIT(&uiter);2642 ULIST_ITER_INIT(&uiter);
2467 while ((unode = ulist_next(roots, &uiter))) {2643 while ((unode = ulist_next(roots, &uiter))) {
2644 LIST_HEAD(tmp);
2645
2468 qg = find_qgroup_rb(fs_info, unode->val);2646 qg = find_qgroup_rb(fs_info, unode->val);
2469 if (!qg)2647 if (!qg)
2470 continue;2648 continue;
2471 2649
2472 ulist_reinit(tmp);2650 qgroup_iterator_nested_add(qgroups, qg);
2473 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),2651 qgroup_iterator_add(&tmp, qg);
2474 GFP_ATOMIC);2652 list_for_each_entry(qg, &tmp, iterator) {
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))) {
2482 struct btrfs_qgroup_list *glist;2653 struct btrfs_qgroup_list *glist;
2483 2654
2484 qg = unode_aux_to_qgroup(tmp_unode);
2485 if (update_old)2655 if (update_old)
2486 btrfs_qgroup_update_old_refcnt(qg, seq, 1);2656 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
2487 else2657 else
2488 btrfs_qgroup_update_new_refcnt(qg, seq, 1);2658 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
2659
2489 list_for_each_entry(glist, &qg->groups, next_group) {2660 list_for_each_entry(glist, &qg->groups, next_group) {
2490 ret = ulist_add(qgroups, glist->group->qgroupid,2661 qgroup_iterator_nested_add(qgroups, glist->group);
2491 qgroup_to_aux(glist->group),2662 qgroup_iterator_add(&tmp, 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;
2500 }2663 }
2501 }2664 }
2665 qgroup_iterator_clean(&tmp);
2502 }2666 }
2503 return 0;
2504}2667}
2505 2668
2506/*2669/*
@@ -2539,22 +2702,16 @@ static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info, · 32 unmodified lines
2539 * But this time we don't need to consider other things, the codes and logic2702 * But this time we don't need to consider other things, the codes and logic
2540 * is easy to understand now.2703 * is easy to understand now.
2541 */2704 */
2542static int qgroup_update_counters(struct btrfs_fs_info *fs_info,2705static void qgroup_update_counters(struct btrfs_fs_info *fs_info,
2543 struct ulist *qgroups,2706 struct list_head *qgroups, u64 nr_old_roots,
2544 u64 nr_old_roots,2707 u64 nr_new_roots, u64 num_bytes, u64 seq)
2545 u64 nr_new_roots,
2546 u64 num_bytes, u64 seq)
2547{2708{
2548 struct ulist_node *unode;
2549 struct ulist_iterator uiter;
2550 struct btrfs_qgroup *qg;2709 struct btrfs_qgroup *qg;
2551 u64 cur_new_count, cur_old_count;
2552 2710
2553 ULIST_ITER_INIT(&uiter);2711 list_for_each_entry(qg, qgroups, nested_iterator) {
2554 while ((unode = ulist_next(qgroups, &uiter))) {2712 u64 cur_new_count, cur_old_count;
2555 bool dirty = false;2713 bool dirty = false;
2556 2714
2557 qg = unode_aux_to_qgroup(unode);
2558 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);2715 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
2559 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);2716 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
2560 2717
@@ -2625,7 +2782,6 @@ static int qgroup_update_counters(struct btrfs_fs_info *fs_info, · 64 unmodified lines
2625 if (dirty)2782 if (dirty)
2626 qgroup_dirty(fs_info, qg);2783 qgroup_dirty(fs_info, qg);
2627 }2784 }
2628 return 0;
2629}2785}
2630 2786
2631/*2787/*
@@ -2662,8 +2818,7 @@ int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 30 unmodified lines
2662 struct ulist *new_roots)2818 struct ulist *new_roots)
2663{2819{
2664 struct btrfs_fs_info *fs_info = trans->fs_info;2820 struct btrfs_fs_info *fs_info = trans->fs_info;
2665 struct ulist *qgroups = NULL;2821 LIST_HEAD(qgroups);
2666 struct ulist *tmp = NULL;
2667 u64 seq;2822 u64 seq;
2668 u64 nr_new_roots = 0;2823 u64 nr_new_roots = 0;
2669 u64 nr_old_roots = 0;2824 u64 nr_old_roots = 0;
@@ -2673,7 +2828,7 @@ int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 3 unmodified lines
2673 * If quotas get disabled meanwhile, the resources need to be freed and2828 * If quotas get disabled meanwhile, the resources need to be freed and
2674 * we can't just exit here.2829 * we can't just exit here.
2675 */2830 */
2676 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||2831 if (!btrfs_qgroup_full_accounting(fs_info) ||
2677 fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING)2832 fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING)
2678 goto out_free;2833 goto out_free;
2679 2834
@@ -2697,17 +2852,6 @@ int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr, · 17 unmodified lines
2697 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,2852 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
2698 num_bytes, nr_old_roots, nr_new_roots);2853 num_bytes, nr_old_roots, nr_new_roots);
2699 2854
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
2711 mutex_lock(&fs_info->qgroup_rescan_lock);2855 mutex_lock(&fs_info->qgroup_rescan_lock);
2712 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {2856 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2713 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {2857 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
2722 seq = fs_info->qgroup_seq;2866 seq = fs_info->qgroup_seq;
2723 2867
2724 /* Update old refcnts using old_roots */2868 /* Update old refcnts using old_roots */
2725 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,2869 qgroup_update_refcnt(fs_info, old_roots, &qgroups, seq, UPDATE_OLD);
2726 UPDATE_OLD);
2727 if (ret < 0)
2728 goto out;
2729 2870
2730 /* Update new refcnts using new_roots */2871 /* Update new refcnts using new_roots */
2731 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,2872 qgroup_update_refcnt(fs_info, new_roots, &qgroups, seq, UPDATE_NEW);
2732 UPDATE_NEW);
2733 if (ret < 0)
2734 goto out;
2735 2873
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,
2737 num_bytes, seq);2875 num_bytes, seq);
2738 2876
2739 /*2877 /*
2740 * Bump qgroup_seq to avoid seq overlap2878 * Bump qgroup_seq to avoid seq overlap
2741 */2879 */
2742 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;2880 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2743out:
2744 spin_unlock(&fs_info->qgroup_lock);2881 spin_unlock(&fs_info->qgroup_lock);
2745out_free:2882out_free:
2746 ulist_free(tmp);2883 qgroup_iterator_nested_clean(&qgroups);
2747 ulist_free(qgroups);
2748 ulist_free(old_roots);2884 ulist_free(old_roots);
2749 ulist_free(new_roots);2885 ulist_free(new_roots);
2750 return ret;2886 return ret;
@@ -2761,6 +2897,9 @@ int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans) · 10 unmodified lines
2761 u64 qgroup_to_skip;2897 u64 qgroup_to_skip;
2762 int ret = 0;2898 int ret = 0;
2763 2899
2900 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
2901 return 0;
2902
2764 delayed_refs = &trans->transaction->delayed_refs;2903 delayed_refs = &trans->transaction->delayed_refs;
2765 qgroup_to_skip = delayed_refs->qgroup_to_skip;2904 qgroup_to_skip = delayed_refs->qgroup_to_skip;
2766 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {2905 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
2876 qgroup_mark_inconsistent(fs_info);3015 qgroup_mark_inconsistent(fs_info);
2877 spin_lock(&fs_info->qgroup_lock);3016 spin_lock(&fs_info->qgroup_lock);
2878 }3017 }
2879 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))3018 if (btrfs_qgroup_enabled(fs_info))
2880 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;3019 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2881 else3020 else
2882 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;3021 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
2889 return ret;3028 return ret;
2890}3029}
2891 3030
3031static 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
2892/*3072/*
2893 * Copy the accounting information between qgroups. This is necessary3073 * Copy the accounting information between qgroups. This is necessary
2894 * when a snapshot or a subvolume is created. Throwing an error will3074 * 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
2896 * when a readonly fs is a reasonable outcome.3076 * when a readonly fs is a reasonable outcome.
2897 */3077 */
2898int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,3078int 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)
2900{3081{
2901 int ret = 0;3082 int ret = 0;
2902 int i;3083 int i;
@@ -2906,10 +3087,17 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 3 unmodified lines
2906 struct btrfs_root *quota_root;3087 struct btrfs_root *quota_root;
2907 struct btrfs_qgroup *srcgroup;3088 struct btrfs_qgroup *srcgroup;
2908 struct btrfs_qgroup *dstgroup;3089 struct btrfs_qgroup *dstgroup;
3090 struct btrfs_qgroup *prealloc;
3091 struct btrfs_qgroup_list **qlist_prealloc = NULL;
3092 bool free_inherit = false;
2909 bool need_rescan = false;3093 bool need_rescan = false;
2910 u32 level_size = 0;3094 u32 level_size = 0;
2911 u64 nums;3095 u64 nums;
2912 3096
3097 prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
3098 if (!prealloc)
3099 return -ENOMEM;
3100
2913 /*3101 /*
2914 * There are only two callers of this function.3102 * There are only two callers of this function.
2915 *3103 *
@@ -2929,7 +3117,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 13 unmodified lines
2929 3117
2930 if (!committing)3118 if (!committing)
2931 mutex_lock(&fs_info->qgroup_ioctl_lock);3119 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))
2933 goto out;3121 goto out;
2934 3122
2935 quota_root = fs_info->quota_root;3123 quota_root = fs_info->quota_root;
@@ -2938,6 +3126,13 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 2 unmodified lines
2938 goto out;3126 goto out;
2939 }3127 }
2940 3128
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
2941 if (inherit) {3136 if (inherit) {
2942 i_qgroups = (u64 *)(inherit + 1);3137 i_qgroups = (u64 *)(inherit + 1);
2943 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +3138 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
2982 goto out;3177 goto out;
2983 }3178 }
2984 ret = 0;3179 ret = 0;
2985 }
2986 3180
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 }
2987 3197
2988 spin_lock(&fs_info->qgroup_lock);3198 spin_lock(&fs_info->qgroup_lock);
2989 3199
2990 dstgroup = add_qgroup_rb(fs_info, objectid);3200 dstgroup = add_qgroup_rb(fs_info, prealloc, objectid);
2991 if (IS_ERR(dstgroup)) {3201 prealloc = NULL;
2992 ret = PTR_ERR(dstgroup);
2993 goto unlock;
2994 }
2995 3202
2996 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {3203 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2997 dstgroup->lim_flags = inherit->lim.flags;3204 dstgroup->lim_flags = inherit->lim.flags;
@@ -3003,7 +3210,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 5 unmodified lines
3003 qgroup_dirty(fs_info, dstgroup);3210 qgroup_dirty(fs_info, dstgroup);
3004 }3211 }
3005 3212
3006 if (srcid) {3213 if (srcid && btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL) {
3007 srcgroup = find_qgroup_rb(fs_info, srcid);3214 srcgroup = find_qgroup_rb(fs_info, srcid);
3008 if (!srcgroup)3215 if (!srcgroup)
3009 goto unlock;3216 goto unlock;
@@ -3038,7 +3245,9 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · 28 unmodified lines
3038 i_qgroups = (u64 *)(inherit + 1);3245 i_qgroups = (u64 *)(inherit + 1);
3039 for (i = 0; i < inherit->num_qgroups; ++i) {3246 for (i = 0; i < inherit->num_qgroups; ++i) {
3040 if (*i_qgroups) {3247 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;
3042 if (ret)3251 if (ret)
3043 goto unlock;3252 goto unlock;
3044 }3253 }
@@ -3102,6 +3311,14 @@ out: · 57 unmodified lines
3102 mutex_unlock(&fs_info->qgroup_ioctl_lock);3311 mutex_unlock(&fs_info->qgroup_ioctl_lock);
3103 if (need_rescan)3312 if (need_rescan)
3104 qgroup_mark_inconsistent(fs_info);3313 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);
3105 return ret;3322 return ret;
3106}3323}
3107 3324
@@ -3125,8 +3342,7 @@ static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce, · 17 unmodified lines
3125 struct btrfs_fs_info *fs_info = root->fs_info;3342 struct btrfs_fs_info *fs_info = root->fs_info;
3126 u64 ref_root = root->root_key.objectid;3343 u64 ref_root = root->root_key.objectid;
3127 int ret = 0;3344 int ret = 0;
3128 struct ulist_node *unode;3345 LIST_HEAD(qgroup_list);
3129 struct ulist_iterator uiter;
3130 3346
3131 if (!is_fstree(ref_root))3347 if (!is_fstree(ref_root))
3132 return 0;3348 return 0;
@@ -3146,49 +3362,28 @@ static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce, · 13 unmodified lines
3146 if (!qgroup)3362 if (!qgroup)
3147 goto out;3363 goto out;
3148 3364
3149 /*3365 qgroup_iterator_add(&qgroup_list, qgroup);
3150 * in a first step, we check all affected qgroups if any limits would3366 list_for_each_entry(qgroup, &qgroup_list, iterator) {
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;
3161 struct btrfs_qgroup_list *glist;3367 struct btrfs_qgroup_list *glist;
3162 3368
3163 qg = unode_aux_to_qgroup(unode);3369 if (enforce && !qgroup_check_limits(qgroup, num_bytes)) {
3164
3165 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
3166 ret = -EDQUOT;3370 ret = -EDQUOT;
3167 goto out;3371 goto out;
3168 }3372 }
3169 3373
3170 list_for_each_entry(glist, &qg->groups, next_group) {3374 list_for_each_entry(glist, &qgroup->groups, next_group)
3171 ret = ulist_add(fs_info->qgroup_ulist,3375 qgroup_iterator_add(&qgroup_list, glist->group);
3172 glist->group->qgroupid,
3173 qgroup_to_aux(glist->group), GFP_ATOMIC);
3174 if (ret < 0)
3175 goto out;
3176 }
3177 }3376 }
3377
3178 ret = 0;3378 ret = 0;
3179 /*3379 /*
3180 * no limits exceeded, now record the reservation into all qgroups3380 * no limits exceeded, now record the reservation into all qgroups
3181 */3381 */
3182 ULIST_ITER_INIT(&uiter);3382 list_for_each_entry(qgroup, &qgroup_list, iterator)
3183 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {3383 qgroup_rsv_add(fs_info, qgroup, num_bytes, type);
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 }
3190 3384
3191out:3385out:
3386 qgroup_iterator_clean(&qgroup_list);
3192 spin_unlock(&fs_info->qgroup_lock);3387 spin_unlock(&fs_info->qgroup_lock);
3193 return ret;3388 return ret;
3194}3389}
@@ -3207,9 +3402,7 @@ void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info, · 12 unmodified lines
3207 enum btrfs_qgroup_rsv_type type)3402 enum btrfs_qgroup_rsv_type type)
3208{3403{
3209 struct btrfs_qgroup *qgroup;3404 struct btrfs_qgroup *qgroup;
3210 struct ulist_node *unode;3405 LIST_HEAD(qgroup_list);
3211 struct ulist_iterator uiter;
3212 int ret = 0;
3213 3406
3214 if (!is_fstree(ref_root))3407 if (!is_fstree(ref_root))
3215 return;3408 return;
@@ -3237,30 +3430,17 @@ void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info, · 21 unmodified lines
3237 */3430 */
3238 num_bytes = qgroup->rsv.values[type];3431 num_bytes = qgroup->rsv.values[type];
3239 3432
3240 ulist_reinit(fs_info->qgroup_ulist);3433 qgroup_iterator_add(&qgroup_list, qgroup);
3241 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,3434 list_for_each_entry(qgroup, &qgroup_list, iterator) {
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;
3248 struct btrfs_qgroup_list *glist;3435 struct btrfs_qgroup_list *glist;
3249 3436
3250 qg = unode_aux_to_qgroup(unode);3437 qgroup_rsv_release(fs_info, qgroup, num_bytes, type);
3251 3438 list_for_each_entry(glist, &qgroup->groups, next_group) {
3252 qgroup_rsv_release(fs_info, qg, num_bytes, type);3439 qgroup_iterator_add(&qgroup_list, glist->group);
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;
3260 }3440 }
3261 }3441 }
3262
3263out:3442out:
3443 qgroup_iterator_clean(&qgroup_list);
3264 spin_unlock(&fs_info->qgroup_lock);3444 spin_unlock(&fs_info->qgroup_lock);
3265}3445}
3266 3446
@@ -3295,6 +3475,9 @@ static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans, · 28 unmodified lines
3295 int slot;3475 int slot;
3296 int ret;3476 int ret;
3297 3477
3478 if (!btrfs_qgroup_full_accounting(fs_info))
3479 return 1;
3480
3298 mutex_lock(&fs_info->qgroup_rescan_lock);3481 mutex_lock(&fs_info->qgroup_rescan_lock);
3299 extent_root = btrfs_extent_root(fs_info,3482 extent_root = btrfs_extent_root(fs_info,
3300 fs_info->qgroup_rescan_progress.objectid);3483 fs_info->qgroup_rescan_progress.objectid);
@@ -3375,10 +3558,15 @@ out: · 74 unmodified lines
3375 3558
3376static bool rescan_should_stop(struct btrfs_fs_info *fs_info)3559static bool rescan_should_stop(struct btrfs_fs_info *fs_info)
3377{3560{
3378 return btrfs_fs_closing(fs_info) ||3561 if (btrfs_fs_closing(fs_info))
3379 test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state) ||3562 return true;
3380 !test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||3563 if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state))
3381 fs_info->qgroup_flags & BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN;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;
3382}3570}
3383 3571
3384static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)3572static 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
3392 bool stopped = false;3580 bool stopped = false;
3393 bool did_leaf_rescans = false;3581 bool did_leaf_rescans = false;
3394 3582
3583 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
3584 return;
3585
3395 path = btrfs_alloc_path();3586 path = btrfs_alloc_path();
3396 if (!path)3587 if (!path)
3397 goto out;3588 goto out;
@@ -3495,6 +3686,11 @@ qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, · 97 unmodified lines
3495{3686{
3496 int ret = 0;3687 int ret = 0;
3497 3688
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
3498 if (!init_flags) {3694 if (!init_flags) {
3499 /* we're resuming qgroup rescan at mount time */3695 /* we're resuming qgroup rescan at mount time */
3500 if (!(fs_info->qgroup_flags &3696 if (!(fs_info->qgroup_flags &
@@ -3525,7 +3721,7 @@ qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, · 24 unmodified lines
3525 btrfs_warn(fs_info,3721 btrfs_warn(fs_info,
3526 "qgroup rescan init failed, qgroup is not enabled");3722 "qgroup rescan init failed, qgroup is not enabled");
3527 ret = -EINVAL;3723 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) {
3529 /* Quota disable is in progress */3725 /* Quota disable is in progress */
3530 ret = -EBUSY;3726 ret = -EBUSY;
3531 }3727 }
@@ -3546,7 +3742,7 @@ qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, · 14 unmodified lines
3546 mutex_unlock(&fs_info->qgroup_rescan_lock);3742 mutex_unlock(&fs_info->qgroup_rescan_lock);
3547 3743
3548 btrfs_init_work(&fs_info->qgroup_rescan_work,3744 btrfs_init_work(&fs_info->qgroup_rescan_work,
3549 btrfs_qgroup_rescan_worker, NULL, NULL);3745 btrfs_qgroup_rescan_worker, NULL);
3550 return 0;3746 return 0;
3551}3747}
3552 3748
@@ -3784,7 +3980,7 @@ static int qgroup_reserve_data(struct btrfs_inode *inode, · 231 unmodified lines
3784 u64 to_reserve;3980 u64 to_reserve;
3785 int ret;3981 int ret;
3786 3982
3787 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||3983 if (btrfs_qgroup_mode(root->fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
3788 !is_fstree(root->root_key.objectid) || len == 0)3984 !is_fstree(root->root_key.objectid) || len == 0)
3789 return 0;3985 return 0;
3790 3986
@@ -3916,8 +4112,12 @@ static int __btrfs_qgroup_release_data(struct btrfs_inode *inode, · 125 unmodified lines
3916 int trace_op = QGROUP_RELEASE;4112 int trace_op = QGROUP_RELEASE;
3917 int ret;4113 int ret;
3918 4114
3919 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &inode->root->fs_info->flags))4115 if (btrfs_qgroup_mode(inode->root->fs_info) == BTRFS_QGROUP_MODE_DISABLED) {
3920 return 0;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 }
3921 4121
3922 /* In release case, we shouldn't have @reserved */4122 /* In release case, we shouldn't have @reserved */
3923 WARN_ON(!free && reserved);4123 WARN_ON(!free && reserved);
@@ -4027,7 +4227,7 @@ int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes, · 103 unmodified lines
4027 struct btrfs_fs_info *fs_info = root->fs_info;4227 struct btrfs_fs_info *fs_info = root->fs_info;
4028 int ret;4228 int ret;
4029 4229
4030 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||4230 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
4031 !is_fstree(root->root_key.objectid) || num_bytes == 0)4231 !is_fstree(root->root_key.objectid) || num_bytes == 0)
4032 return 0;4232 return 0;
4033 4233
@@ -4064,11 +4264,15 @@ int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes, · 30 unmodified lines
4064 return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);4264 return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
4065}4265}
4066 4266
4267/*
4268 * Per-transaction meta reservation should be all freed at transaction commit
4269 * time
4270 */
4067void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)4271void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
4068{4272{
4069 struct btrfs_fs_info *fs_info = root->fs_info;4273 struct btrfs_fs_info *fs_info = root->fs_info;
4070 4274
4071 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||4275 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
4072 !is_fstree(root->root_key.objectid))4276 !is_fstree(root->root_key.objectid))
4073 return;4277 return;
4074 4278
@@ -4084,7 +4288,7 @@ void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes, · 9 unmodified lines
4084{4288{
4085 struct btrfs_fs_info *fs_info = root->fs_info;4289 struct btrfs_fs_info *fs_info = root->fs_info;
4086 4290
4087 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||4291 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
4088 !is_fstree(root->root_key.objectid))4292 !is_fstree(root->root_key.objectid))
4089 return;4293 return;
4090 4294
@@ -4104,9 +4308,7 @@ static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root, · 13 unmodified lines
4104 int num_bytes)4308 int num_bytes)
4105{4309{
4106 struct btrfs_qgroup *qgroup;4310 struct btrfs_qgroup *qgroup;
4107 struct ulist_node *unode;4311 LIST_HEAD(qgroup_list);
4108 struct ulist_iterator uiter;
4109 int ret = 0;
4110 4312
4111 if (num_bytes == 0)4313 if (num_bytes == 0)
4112 return;4314 return;
@@ -4117,39 +4319,35 @@ static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root, · 4 unmodified lines
4117 qgroup = find_qgroup_rb(fs_info, ref_root);4319 qgroup = find_qgroup_rb(fs_info, ref_root);
4118 if (!qgroup)4320 if (!qgroup)
4119 goto out;4321 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;
4129 4322
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;
4131 4326
4132 qgroup_rsv_release(fs_info, qg, num_bytes,4327 qgroup_rsv_release(fs_info, qgroup, num_bytes,
4133 BTRFS_QGROUP_RSV_META_PREALLOC);4328 BTRFS_QGROUP_RSV_META_PREALLOC);
4134 qgroup_rsv_add(fs_info, qg, num_bytes,4329 qgroup_rsv_add(fs_info, qgroup, num_bytes,
4135 BTRFS_QGROUP_RSV_META_PERTRANS);4330 BTRFS_QGROUP_RSV_META_PERTRANS);
4136 list_for_each_entry(glist, &qg->groups, next_group) {4331
4137 ret = ulist_add(fs_info->qgroup_ulist,4332 list_for_each_entry(glist, &qgroup->groups, next_group)
4138 glist->group->qgroupid,4333 qgroup_iterator_add(&qgroup_list, glist->group);
4139 qgroup_to_aux(glist->group), GFP_ATOMIC);
4140 if (ret < 0)
4141 goto out;
4142 }
4143 }4334 }
4144out:4335out:
4336 qgroup_iterator_clean(&qgroup_list);
4145 spin_unlock(&fs_info->qgroup_lock);4337 spin_unlock(&fs_info->qgroup_lock);
4146}4338}
4147 4339
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 */
4148void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)4346void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
4149{4347{
4150 struct btrfs_fs_info *fs_info = root->fs_info;4348 struct btrfs_fs_info *fs_info = root->fs_info;
4151 4349
4152 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||4350 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED ||
4153 !is_fstree(root->root_key.objectid))4351 !is_fstree(root->root_key.objectid))
4154 return;4352 return;
4155 /* Same as btrfs_qgroup_free_meta_prealloc() */4353 /* 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
4257 int level = btrfs_header_level(subvol_parent) - 1;4455 int level = btrfs_header_level(subvol_parent) - 1;
4258 int ret = 0;4456 int ret = 0;
4259 4457
4260 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))4458 if (!btrfs_qgroup_full_accounting(fs_info))
4261 return 0;4459 return 0;
4262 4460
4263 if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) >4461 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
4367 int ret = 0;4565 int ret = 0;
4368 int i;4566 int i;
4369 4567
4370 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))4568 if (!btrfs_qgroup_full_accounting(fs_info))
4371 return 0;4569 return 0;
4372 if (!is_fstree(root->root_key.objectid) || !root->reloc_root)4570 if (!is_fstree(root->root_key.objectid) || !root->reloc_root)
4373 return 0;4571 return 0;
@@ -4450,3 +4648,53 @@ void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans) · 76 unmodified lines
4450 }4648 }
4451 *root = RB_ROOT;4649 *root = RB_ROOT;
4452}4650}
4651
4652int 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
4694out:
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
101 * subtree rescan for them.101 * subtree rescan for them.
102 */102 */
103 103
104#define BTRFS_QGROUP_RUNTIME_FLAG_CANCEL_RESCAN (1UL << 3)104/*
105#define BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING (1UL << 4)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)
106 113
107/*114/*
108 * Record a dirty extent, and info qgroup to update quota on it115 * Record a dirty extent, and info qgroup to update quota on it
@@ -220,6 +227,33 @@ struct btrfs_qgroup { · 111 unmodified lines
220 struct list_head groups; /* groups this group is member of */227 struct list_head groups; /* groups this group is member of */
221 struct list_head members; /* groups that are members of this group */228 struct list_head members; /* groups that are members of this group */
222 struct list_head dirty; /* dirty groups */229 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;
223 struct rb_node node; /* tree of qgroups */257 struct rb_node node; /* tree of qgroups */
224 258
225 /*259 /*
@@ -235,6 +269,21 @@ struct btrfs_qgroup { · 9 unmodified lines
235 struct kobject kobj;269 struct kobject kobj;
236};270};
237 271
272struct 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
238static inline u64 btrfs_qgroup_subvolid(u64 qgroupid)287static inline u64 btrfs_qgroup_subvolid(u64 qgroupid)
239{288{
240 return (qgroupid & ((1ULL << BTRFS_QGROUP_LEVEL_SHIFT) - 1));289 return (qgroupid & ((1ULL << BTRFS_QGROUP_LEVEL_SHIFT) - 1));
@@ -249,14 +298,23 @@ enum { · 8 unmodified lines
249 ENUM_BIT(QGROUP_FREE),298 ENUM_BIT(QGROUP_FREE),
250};299};
251 300
252int btrfs_quota_enable(struct btrfs_fs_info *fs_info);301enum btrfs_qgroup_mode {
302 BTRFS_QGROUP_MODE_DISABLED,
303 BTRFS_QGROUP_MODE_FULL,
304 BTRFS_QGROUP_MODE_SIMPLE
305};
306
307enum btrfs_qgroup_mode btrfs_qgroup_mode(struct btrfs_fs_info *fs_info);
308bool btrfs_qgroup_enabled(struct btrfs_fs_info *fs_info);
309bool btrfs_qgroup_full_accounting(struct btrfs_fs_info *fs_info);
310int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
311 struct btrfs_ioctl_quota_ctl_args *quota_ctl_args);
253int btrfs_quota_disable(struct btrfs_fs_info *fs_info);312int btrfs_quota_disable(struct btrfs_fs_info *fs_info);
254int btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info);313int btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info);
255void btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info);314void btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info);
256int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,315int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
257 bool interruptible);316 bool interruptible);
258int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,317int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, u64 dst);
259 u64 dst);
260int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,318int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
261 u64 dst);319 u64 dst);
262int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid);320int 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
267void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info);325void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info);
268struct btrfs_delayed_extent_op;326struct btrfs_delayed_extent_op;
269 327
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 */
281int btrfs_qgroup_trace_extent_nolock(328int btrfs_qgroup_trace_extent_nolock(
282 struct btrfs_fs_info *fs_info,329 struct btrfs_fs_info *fs_info,
283 struct btrfs_delayed_ref_root *delayed_refs,330 struct btrfs_delayed_ref_root *delayed_refs,
284 struct btrfs_qgroup_extent_record *record);331 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 */
307int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,332int btrfs_qgroup_trace_extent_post(struct btrfs_trans_handle *trans,
308 struct btrfs_qgroup_extent_record *qrecord);333 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 */
323int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,334int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
324 u64 num_bytes);335 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 */
332int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,336int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
333 struct extent_buffer *eb);337 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 */
344int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,338int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
345 struct extent_buffer *root_eb,339 struct extent_buffer *root_eb,
346 u64 root_gen, int root_level);340 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
350int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans);344int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans);
351int btrfs_run_qgroups(struct btrfs_trans_handle *trans);345int btrfs_run_qgroups(struct btrfs_trans_handle *trans);
352int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,346int 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);
354void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,349void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
355 u64 ref_root, u64 num_bytes,350 u64 ref_root, u64 num_bytes,
356 enum btrfs_qgroup_rsv_type type);351 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
408 BTRFS_QGROUP_RSV_META_PREALLOC);403 BTRFS_QGROUP_RSV_META_PREALLOC);
409}404}
410 405
411/*
412 * Per-transaction meta reservation should be all freed at transaction commit
413 * time
414 */
415void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root);406void 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 */
423void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes);407void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes);
424
425void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode);408void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode);
426 409
427/* btrfs_qgroup_swapped_blocks related functions */410/* 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
439 struct btrfs_root *root, struct extent_buffer *eb);422 struct btrfs_root *root, struct extent_buffer *eb);
440void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans);423void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans);
441bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info);424bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info);
425int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info,
426 struct btrfs_squota_delta *delta);
442 427
443#endif428#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
17int 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
77static 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
126int 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
151int 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;
259out:
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 }
270free_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
14struct btrfs_io_context;
15struct btrfs_io_stripe;
16struct btrfs_ordered_extent;
17struct btrfs_trans_handle;
18
19int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length);
20int 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);
23int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
24 struct btrfs_ordered_extent *ordered_extent);
25
26static 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
44static 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
485 ret = add_shared_data_ref(fs_info, offset, count,485 ret = add_shared_data_ref(fs_info, offset, count,
486 key->objectid, key->offset);486 key->objectid, key->offset);
487 break;487 break;
488 case BTRFS_EXTENT_OWNER_REF_KEY:
489 WARN_ON(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
490 break;
488 default:491 default:
489 btrfs_err(fs_info, "invalid key type in iref");492 btrfs_err(fs_info, "invalid key type in iref");
490 ret = -EINVAL;493 ret = -EINVAL;
@@ -652,7 +655,7 @@ static void dump_block_entry(struct btrfs_fs_info *fs_info, · 161 unmodified lines
652}655}
653 656
654/*657/*
655 * btrfs_ref_tree_mod: called when we modify a ref for a bytenr658 * Called when we modify a ref for a bytenr.
656 *659 *
657 * This will add an action item to the given bytenr and do sanity checks to make660 * This will add an action item to the given bytenr and do sanity checks to make
658 * sure we haven't messed something up. If we are making a new allocation and661 * 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
681 684
682 if (generic_ref->type == BTRFS_REF_METADATA) {685 if (generic_ref->type == BTRFS_REF_METADATA) {
683 if (!parent)686 if (!parent)
684 ref_root = generic_ref->tree_ref.owning_root;687 ref_root = generic_ref->tree_ref.ref_root;
685 owner = generic_ref->tree_ref.level;688 owner = generic_ref->tree_ref.level;
686 } else if (!parent) {689 } else if (!parent) {
687 ref_root = generic_ref->data_ref.owning_root;690 ref_root = generic_ref->data_ref.ref_root;
688 owner = generic_ref->data_ref.ino;691 owner = generic_ref->data_ref.ino;
689 offset = generic_ref->data_ref.offset;692 offset = generic_ref->data_ref.offset;
690 }693 }
fs/btrfs/reflink.c+1−2
@@ -25,7 +25,6 @@ static int clone_finish_inode_update(struct btrfs_trans_handle *trans, · 24 unmodified lines
25 const u64 olen,25 const u64 olen,
26 int no_time_update)26 int no_time_update)
27{27{
28 struct btrfs_root *root = BTRFS_I(inode)->root;
29 int ret;28 int ret;
30 29
31 inode_inc_iversion(inode);30 inode_inc_iversion(inode);
@@ -43,7 +42,7 @@ static int clone_finish_inode_update(struct btrfs_trans_handle *trans, · 11 unmodified lines
43 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);42 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
44 }43 }
45 44
46 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));45 ret = btrfs_update_inode(trans, BTRFS_I(inode));
47 if (ret) {46 if (ret) {
48 btrfs_abort_transaction(trans, ret);47 btrfs_abort_transaction(trans, ret);
49 btrfs_end_transaction(trans);48 btrfs_end_transaction(trans);
fs/btrfs/relocation.c+127−81
@@ -111,8 +111,8 @@ struct tree_block { · 110 unmodified lines
111 }; /* Use rb_simple_node for search/insert */111 }; /* Use rb_simple_node for search/insert */
112 u64 owner;112 u64 owner;
113 struct btrfs_key key;113 struct btrfs_key key;
114 unsigned int level:8;114 u8 level;
115 unsigned int key_ready:1;115 bool key_ready;
116};116};
117 117
118#define MAX_EXTENTS 128118#define MAX_EXTENTS 128
@@ -122,6 +122,13 @@ struct file_extent_cluster { · 3 unmodified lines
122 u64 end;122 u64 end;
123 u64 boundary[MAX_EXTENTS];123 u64 boundary[MAX_EXTENTS];
124 unsigned int nr;124 unsigned int nr;
125 u64 owning_root;
126};
127
128/* Stages of data relocation. */
129enum reloc_stage {
130 MOVE_DATA_EXTENTS,
131 UPDATE_DATA_PTRS
125};132};
126 133
127struct reloc_control {134struct reloc_control {
@@ -155,16 +162,12 @@ struct reloc_control { · 27 unmodified lines
155 u64 search_start;162 u64 search_start;
156 u64 extents_found;163 u64 extents_found;
157 164
158 unsigned int stage:8;165 enum reloc_stage stage;
159 unsigned int create_reloc_tree:1;166 bool create_reloc_tree;
160 unsigned int merge_reloc_tree:1;167 bool merge_reloc_tree;
161 unsigned int found_file_extent:1;168 bool found_file_extent;
162};169};
163 170
164/* stages of data relocation */
165#define MOVE_DATA_EXTENTS 0
166#define UPDATE_DATA_PTRS 1
167
168static void mark_block_processed(struct reloc_control *rc,171static void mark_block_processed(struct reloc_control *rc,
169 struct btrfs_backref_node *node)172 struct btrfs_backref_node *node)
170{173{
@@ -180,13 +183,6 @@ static void mark_block_processed(struct reloc_control *rc, · 9 unmodified lines
180 node->processed = 1;183 node->processed = 1;
181}184}
182 185
183
184static void mapping_tree_init(struct mapping_tree *tree)
185{
186 tree->rb_root = RB_ROOT;
187 spin_lock_init(&tree->lock);
188}
189
190/*186/*
191 * walk up backref nodes until reach node presents tree root187 * walk up backref nodes until reach node presents tree root
192 */188 */
@@ -299,7 +295,7 @@ static int update_backref_cache(struct btrfs_trans_handle *trans, · 106 unmodified lines
299 return 1;295 return 1;
300}296}
301 297
302static bool reloc_root_is_dead(struct btrfs_root *root)298static bool reloc_root_is_dead(const struct btrfs_root *root)
303{299{
304 /*300 /*
305 * Pair with set_bit/clear_bit in clean_dirty_subvols and301 * 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
320 * from no reloc root. But btrfs_should_ignore_reloc_root() below is a316 * from no reloc root. But btrfs_should_ignore_reloc_root() below is a
321 * special case.317 * special case.
322 */318 */
323static bool have_reloc_root(struct btrfs_root *root)319static bool have_reloc_root(const struct btrfs_root *root)
324{320{
325 if (reloc_root_is_dead(root))321 if (reloc_root_is_dead(root))
326 return false;322 return false;
@@ -329,31 +325,30 @@ static bool have_reloc_root(struct btrfs_root *root) · 2 unmodified lines
329 return true;325 return true;
330}326}
331 327
332int btrfs_should_ignore_reloc_root(struct btrfs_root *root)328bool btrfs_should_ignore_reloc_root(const struct btrfs_root *root)
333{329{
334 struct btrfs_root *reloc_root;330 struct btrfs_root *reloc_root;
335 331
336 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))332 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
337 return 0;333 return false;
338 334
339 /* This root has been merged with its reloc tree, we can ignore it */335 /* This root has been merged with its reloc tree, we can ignore it */
340 if (reloc_root_is_dead(root))336 if (reloc_root_is_dead(root))
341 return 1;337 return true;
342 338
343 reloc_root = root->reloc_root;339 reloc_root = root->reloc_root;
344 if (!reloc_root)340 if (!reloc_root)
345 return 0;341 return false;
346 342
347 if (btrfs_header_generation(reloc_root->commit_root) ==343 if (btrfs_header_generation(reloc_root->commit_root) ==
348 root->fs_info->running_transaction->transid)344 root->fs_info->running_transaction->transid)
349 return 0;345 return false;
350 /*346 /*
351 * if there is reloc tree and it was created in previous347 * If there is reloc tree and it was created in previous transaction
352 * transaction backref lookup can find the reloc tree,348 * backref lookup can find the reloc tree, so backref node for the fs
353 * so backref node for the fs tree root is useless for349 * tree root is useless for relocation.
354 * relocation.
355 */350 */
356 return 1;351 return true;
357}352}
358 353
359/*354/*
@@ -547,7 +542,7 @@ out: · 187 unmodified lines
547 */542 */
548static int clone_backref_node(struct btrfs_trans_handle *trans,543static int clone_backref_node(struct btrfs_trans_handle *trans,
549 struct reloc_control *rc,544 struct reloc_control *rc,
550 struct btrfs_root *src,545 const struct btrfs_root *src,
551 struct btrfs_root *dest)546 struct btrfs_root *dest)
552{547{
553 struct btrfs_root *reloc_root = src->reloc_root;548 struct btrfs_root *reloc_root = src->reloc_root;
@@ -632,7 +627,7 @@ fail: · 78 unmodified lines
632/*627/*
633 * helper to add 'address of tree root -> reloc tree' mapping628 * helper to add 'address of tree root -> reloc tree' mapping
634 */629 */
635static int __must_check __add_reloc_root(struct btrfs_root *root)630static int __add_reloc_root(struct btrfs_root *root)
636{631{
637 struct btrfs_fs_info *fs_info = root->fs_info;632 struct btrfs_fs_info *fs_info = root->fs_info;
638 struct rb_node *rb_node;633 struct rb_node *rb_node;
@@ -1159,7 +1154,7 @@ int replace_file_extents(struct btrfs_trans_handle *trans, · 520 unmodified lines
1159 1154
1160 key.offset -= btrfs_file_extent_offset(leaf, fi);1155 key.offset -= btrfs_file_extent_offset(leaf, fi);
1161 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,1156 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
1162 num_bytes, parent);1157 num_bytes, parent, root->root_key.objectid);
1163 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),1158 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1164 key.objectid, key.offset,1159 key.objectid, key.offset,
1165 root->root_key.objectid, false);1160 root->root_key.objectid, false);
@@ -1170,7 +1165,7 @@ int replace_file_extents(struct btrfs_trans_handle *trans, · 4 unmodified lines
1170 }1165 }
1171 1166
1172 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,1167 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1173 num_bytes, parent);1168 num_bytes, parent, root->root_key.objectid);
1174 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),1169 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1175 key.objectid, key.offset,1170 key.objectid, key.offset,
1176 root->root_key.objectid, false);1171 root->root_key.objectid, false);
@@ -1181,15 +1176,15 @@ int replace_file_extents(struct btrfs_trans_handle *trans, · 4 unmodified lines
1181 }1176 }
1182 }1177 }
1183 if (dirty)1178 if (dirty)
1184 btrfs_mark_buffer_dirty(leaf);1179 btrfs_mark_buffer_dirty(trans, leaf);
1185 if (inode)1180 if (inode)
1186 btrfs_add_delayed_iput(BTRFS_I(inode));1181 btrfs_add_delayed_iput(BTRFS_I(inode));
1187 return ret;1182 return ret;
1188}1183}
1189 1184
1190static noinline_for_stack1185static noinline_for_stack int memcmp_node_keys(const struct extent_buffer *eb,
1191int memcmp_node_keys(struct extent_buffer *eb, int slot,1186 int slot, const struct btrfs_path *path,
1192 struct btrfs_path *path, int level)1187 int level)
1193{1188{
1194 struct btrfs_disk_key key1;1189 struct btrfs_disk_key key1;
1195 struct btrfs_disk_key key2;1190 struct btrfs_disk_key key2;
@@ -1374,16 +1369,17 @@ again: · 178 unmodified lines
1374 */1369 */
1375 btrfs_set_node_blockptr(parent, slot, new_bytenr);1370 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1376 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);1371 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1377 btrfs_mark_buffer_dirty(parent);1372 btrfs_mark_buffer_dirty(trans, parent);
1378 1373
1379 btrfs_set_node_blockptr(path->nodes[level],1374 btrfs_set_node_blockptr(path->nodes[level],
1380 path->slots[level], old_bytenr);1375 path->slots[level], old_bytenr);
1381 btrfs_set_node_ptr_generation(path->nodes[level],1376 btrfs_set_node_ptr_generation(path->nodes[level],
1382 path->slots[level], old_ptr_gen);1377 path->slots[level], old_ptr_gen);
1383 btrfs_mark_buffer_dirty(path->nodes[level]);1378 btrfs_mark_buffer_dirty(trans, path->nodes[level]);
1384 1379
1385 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,1380 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);
1387 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,1383 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
1388 0, true);1384 0, true);
1389 ret = btrfs_inc_extent_ref(trans, &ref);1385 ret = btrfs_inc_extent_ref(trans, &ref);
@@ -1392,7 +1388,7 @@ again: · 2 unmodified lines
1392 break;1388 break;
1393 }1389 }
1394 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,1390 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
1395 blocksize, 0);1391 blocksize, 0, dest->root_key.objectid);
1396 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid, 0,1392 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid, 0,
1397 true);1393 true);
1398 ret = btrfs_inc_extent_ref(trans, &ref);1394 ret = btrfs_inc_extent_ref(trans, &ref);
@@ -1401,8 +1397,9 @@ again: · 2 unmodified lines
1401 break;1397 break;
1402 }1398 }
1403 1399
1400 /* We don't know the real owning_root, use 0. */
1404 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,1401 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
1405 blocksize, path->nodes[level]->start);1402 blocksize, path->nodes[level]->start, 0);
1406 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,1403 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
1407 0, true);1404 0, true);
1408 ret = btrfs_free_extent(trans, &ref);1405 ret = btrfs_free_extent(trans, &ref);
@@ -1411,8 +1408,9 @@ again: · 2 unmodified lines
1411 break;1408 break;
1412 }1409 }
1413 1410
1411 /* We don't know the real owning_root, use 0. */
1414 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,1412 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
1415 blocksize, 0);1413 blocksize, 0, 0);
1416 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid,1414 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid,
1417 0, true);1415 0, true);
1418 ret = btrfs_free_extent(trans, &ref);1416 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
1518 * [min_key, max_key)1516 * [min_key, max_key)
1519 */1517 */
1520static int invalidate_extent_cache(struct btrfs_root *root,1518static int invalidate_extent_cache(struct btrfs_root *root,
1521 struct btrfs_key *min_key,1519 const struct btrfs_key *min_key,
1522 struct btrfs_key *max_key)1520 const struct btrfs_key *max_key)
1523{1521{
1524 struct btrfs_fs_info *fs_info = root->fs_info;1522 struct btrfs_fs_info *fs_info = root->fs_info;
1525 struct inode *inode = NULL;1523 struct inode *inode = NULL;
@@ -1897,7 +1895,7 @@ again: · 371 unmodified lines
1897 }1895 }
1898 }1896 }
1899 1897
1900 rc->merge_reloc_tree = 1;1898 rc->merge_reloc_tree = true;
1901 1899
1902 while (!list_empty(&rc->reloc_roots)) {1900 while (!list_empty(&rc->reloc_roots)) {
1903 reloc_root = list_entry(rc->reloc_roots.next,1901 reloc_root = list_entry(rc->reloc_roots.next,
@@ -2517,11 +2515,12 @@ static int do_relocation(struct btrfs_trans_handle *trans, · 613 unmodified lines
2517 node->eb->start);2515 node->eb->start);
2518 btrfs_set_node_ptr_generation(upper->eb, slot,2516 btrfs_set_node_ptr_generation(upper->eb, slot,
2519 trans->transid);2517 trans->transid);
2520 btrfs_mark_buffer_dirty(upper->eb);2518 btrfs_mark_buffer_dirty(trans, upper->eb);
2521 2519
2522 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,2520 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
2523 node->eb->start, blocksize,2521 node->eb->start, blocksize,
2524 upper->eb->start);2522 upper->eb->start,
2523 btrfs_header_owner(upper->eb));
2525 btrfs_init_tree_ref(&ref, node->level,2524 btrfs_init_tree_ref(&ref, node->level,
2526 btrfs_header_owner(upper->eb),2525 btrfs_header_owner(upper->eb),
2527 root->root_key.objectid, false);2526 root->root_key.objectid, false);
@@ -2633,7 +2632,7 @@ static int tree_block_processed(u64 bytenr, struct reloc_control *rc) · 105 unmodified lines
2633 u32 blocksize = rc->extent_root->fs_info->nodesize;2632 u32 blocksize = rc->extent_root->fs_info->nodesize;
2634 2633
2635 if (test_range_bit(&rc->processed_blocks, bytenr,2634 if (test_range_bit(&rc->processed_blocks, bytenr,
2636 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))2635 bytenr + blocksize - 1, EXTENT_DIRTY, NULL))
2637 return 1;2636 return 1;
2638 return 0;2637 return 0;
2639}2638}
@@ -2660,7 +2659,7 @@ static int get_tree_block_key(struct btrfs_fs_info *fs_info, · 20 unmodified lines
2660 else2659 else
2661 btrfs_node_key_to_cpu(eb, &block->key, 0);2660 btrfs_node_key_to_cpu(eb, &block->key, 0);
2662 free_extent_buffer(eb);2661 free_extent_buffer(eb);
2663 block->key_ready = 1;2662 block->key_ready = true;
2664 return 0;2663 return 0;
2665}2664}
2666 2665
@@ -2830,7 +2829,7 @@ out_free_blocks: · 163 unmodified lines
2830 2829
2831static noinline_for_stack int prealloc_file_extent_cluster(2830static noinline_for_stack int prealloc_file_extent_cluster(
2832 struct btrfs_inode *inode,2831 struct btrfs_inode *inode,
2833 struct file_extent_cluster *cluster)2832 const struct file_extent_cluster *cluster)
2834{2833{
2835 u64 alloc_hint = 0;2834 u64 alloc_hint = 0;
2836 u64 start;2835 u64 start;
@@ -2965,7 +2964,7 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod · 128 unmodified lines
2965/*2964/*
2966 * Allow error injection to test balance/relocation cancellation2965 * Allow error injection to test balance/relocation cancellation
2967 */2966 */
2968noinline int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)2967noinline int btrfs_should_cancel_balance(const struct btrfs_fs_info *fs_info)
2969{2968{
2970 return atomic_read(&fs_info->balance_cancel_req) ||2969 return atomic_read(&fs_info->balance_cancel_req) ||
2971 atomic_read(&fs_info->reloc_cancel_req) ||2970 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
2973}2972}
2974ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);2973ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
2975 2974
2976static u64 get_cluster_boundary_end(struct file_extent_cluster *cluster,2975static u64 get_cluster_boundary_end(const struct file_extent_cluster *cluster,
2977 int cluster_nr)2976 int cluster_nr)
2978{2977{
2979 /* Last extent, use cluster end directly */2978 /* Last extent, use cluster end directly */
@@ -2985,7 +2984,7 @@ static u64 get_cluster_boundary_end(struct file_extent_cluster *cluster, · 5 unmodified lines
2985}2984}
2986 2985
2987static int relocate_one_page(struct inode *inode, struct file_ra_state *ra,2986static 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,
2989 int *cluster_nr, unsigned long page_index)2988 int *cluster_nr, unsigned long page_index)
2990{2989{
2991 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);2990 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
@@ -3120,7 +3119,7 @@ release_page: · 128 unmodified lines
3120}3119}
3121 3120
3122static int relocate_file_extent_cluster(struct inode *inode,3121static int relocate_file_extent_cluster(struct inode *inode,
3123 struct file_extent_cluster *cluster)3122 const struct file_extent_cluster *cluster)
3124{3123{
3125 u64 offset = BTRFS_I(inode)->index_cnt;3124 u64 offset = BTRFS_I(inode)->index_cnt;
3126 unsigned long index;3125 unsigned long index;
@@ -3158,11 +3157,12 @@ out: · 31 unmodified lines
3158 return ret;3157 return ret;
3159}3158}
3160 3159
3161static noinline_for_stack3160static noinline_for_stack int relocate_data_extent(struct inode *inode,
3162int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,3161 const struct btrfs_key *extent_key,
3163 struct file_extent_cluster *cluster)3162 struct file_extent_cluster *cluster)
3164{3163{
3165 int ret;3164 int ret;
3165 struct btrfs_root *root = BTRFS_I(inode)->root;
3166 3166
3167 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {3167 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3168 ret = relocate_file_extent_cluster(inode, cluster);3168 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
3171 cluster->nr = 0;3171 cluster->nr = 0;
3172 }3172 }
3173 3173
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) {
3175 cluster->start = extent_key->objectid;3203 cluster->start = extent_key->objectid;
3204 cluster->owning_root = root->relocation_src_root;
3205 }
3176 else3206 else
3177 BUG_ON(cluster->nr >= MAX_EXTENTS);3207 BUG_ON(cluster->nr >= MAX_EXTENTS);
3178 cluster->end = extent_key->objectid + extent_key->offset - 1;3208 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
3193 * the major work is getting the generation and level of the block3223 * the major work is getting the generation and level of the block
3194 */3224 */
3195static int add_tree_block(struct reloc_control *rc,3225static int add_tree_block(struct reloc_control *rc,
3196 struct btrfs_key *extent_key,3226 const struct btrfs_key *extent_key,
3197 struct btrfs_path *path,3227 struct btrfs_path *path,
3198 struct rb_root *blocks)3228 struct rb_root *blocks)
3199{3229{
@@ -3278,7 +3308,7 @@ static int add_tree_block(struct reloc_control *rc, · 78 unmodified lines
3278 block->key.objectid = rc->extent_root->fs_info->nodesize;3308 block->key.objectid = rc->extent_root->fs_info->nodesize;
3279 block->key.offset = generation;3309 block->key.offset = generation;
3280 block->level = level;3310 block->level = level;
3281 block->key_ready = 0;3311 block->key_ready = false;
3282 block->owner = owner;3312 block->owner = owner;
3283 3313
3284 rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);3314 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
3444/*3474/*
3445 * helper to find all tree blocks that reference a given data extent3475 * helper to find all tree blocks that reference a given data extent
3446 */3476 */
3447static noinline_for_stack3477static noinline_for_stack int add_data_references(struct reloc_control *rc,
3448int add_data_references(struct reloc_control *rc,3478 const struct btrfs_key *extent_key,
3449 struct btrfs_key *extent_key,3479 struct btrfs_path *path,
3450 struct btrfs_path *path,3480 struct rb_root *blocks)
3451 struct rb_root *blocks)
3452{3481{
3453 struct btrfs_backref_walk_ctx ctx = { 0 };3482 struct btrfs_backref_walk_ctx ctx = { 0 };
3454 struct ulist_iterator leaf_uiter;3483 struct ulist_iterator leaf_uiter;
@@ -3622,7 +3651,7 @@ int prepare_to_relocate(struct reloc_control *rc) · 167 unmodified lines
3622 if (ret)3651 if (ret)
3623 return ret;3652 return ret;
3624 3653
3625 rc->create_reloc_tree = 1;3654 rc->create_reloc_tree = true;
3626 set_reloc_control(rc);3655 set_reloc_control(rc);
3627 3656
3628 trans = btrfs_join_transaction(rc->extent_root);3657 trans = btrfs_join_transaction(rc->extent_root);
@@ -3702,6 +3731,21 @@ restart: · 73 unmodified lines
3702 struct btrfs_extent_item);3731 struct btrfs_extent_item);
3703 flags = btrfs_extent_flags(path->nodes[0], ei);3732 flags = btrfs_extent_flags(path->nodes[0], ei);
3704 3733
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
3705 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {3749 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
3706 ret = add_tree_block(rc, &key, path, &blocks);3750 ret = add_tree_block(rc, &key, path, &blocks);
3707 } else if (rc->stage == UPDATE_DATA_PTRS &&3751 } else if (rc->stage == UPDATE_DATA_PTRS &&
@@ -3734,7 +3778,7 @@ restart: · 26 unmodified lines
3734 3778
3735 if (rc->stage == MOVE_DATA_EXTENTS &&3779 if (rc->stage == MOVE_DATA_EXTENTS &&
3736 (flags & BTRFS_EXTENT_FLAG_DATA)) {3780 (flags & BTRFS_EXTENT_FLAG_DATA)) {
3737 rc->found_file_extent = 1;3781 rc->found_file_extent = true;
3738 ret = relocate_data_extent(rc->data_inode,3782 ret = relocate_data_extent(rc->data_inode,
3739 &key, &rc->cluster);3783 &key, &rc->cluster);
3740 if (ret < 0) {3784 if (ret < 0) {
@@ -3771,7 +3815,7 @@ restart: · 30 unmodified lines
3771 err = ret;3815 err = ret;
3772 }3816 }
3773 3817
3774 rc->create_reloc_tree = 0;3818 rc->create_reloc_tree = false;
3775 set_reloc_control(rc);3819 set_reloc_control(rc);
3776 3820
3777 btrfs_backref_release_cache(&rc->backref_cache);3821 btrfs_backref_release_cache(&rc->backref_cache);
@@ -3789,7 +3833,7 @@ restart: · 11 unmodified lines
3789 3833
3790 merge_reloc_roots(rc);3834 merge_reloc_roots(rc);
3791 3835
3792 rc->merge_reloc_tree = 0;3836 rc->merge_reloc_tree = false;
3793 unset_reloc_control(rc);3837 unset_reloc_control(rc);
3794 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);3838 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
3795 3839
@@ -3835,7 +3879,7 @@ static int __insert_orphan_inode(struct btrfs_trans_handle *trans, · 39 unmodified lines
3835 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);3879 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
3836 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |3880 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
3837 BTRFS_INODE_PREALLOC);3881 BTRFS_INODE_PREALLOC);
3838 btrfs_mark_buffer_dirty(leaf);3882 btrfs_mark_buffer_dirty(trans, leaf);
3839out:3883out:
3840 btrfs_free_path(path);3884 btrfs_free_path(path);
3841 return ret;3885 return ret;
@@ -3874,9 +3918,9 @@ out: · 32 unmodified lines
3874 * helper to create inode for data relocation.3918 * helper to create inode for data relocation.
3875 * the inode is in data relocation tree and its link count is 03919 * the inode is in data relocation tree and its link count is 0
3876 */3920 */
3877static noinline_for_stack3921static noinline_for_stack struct inode *create_reloc_inode(
3878struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,3922 struct btrfs_fs_info *fs_info,
3879 struct btrfs_block_group *group)3923 const struct btrfs_block_group *group)
3880{3924{
3881 struct inode *inode = NULL;3925 struct inode *inode = NULL;
3882 struct btrfs_trans_handle *trans;3926 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
3971 4015
3972 INIT_LIST_HEAD(&rc->reloc_roots);4016 INIT_LIST_HEAD(&rc->reloc_roots);
3973 INIT_LIST_HEAD(&rc->dirty_subvol_roots);4017 INIT_LIST_HEAD(&rc->dirty_subvol_roots);
3974 btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);4018 btrfs_backref_init_cache(fs_info, &rc->backref_cache, true);
3975 mapping_tree_init(&rc->reloc_root_tree);4019 rc->reloc_root_tree.rb_root = RB_ROOT;
4020 spin_lock_init(&rc->reloc_root_tree.lock);
3976 extent_io_tree_init(fs_info, &rc->processed_blocks, IO_TREE_RELOC_BLOCKS);4021 extent_io_tree_init(fs_info, &rc->processed_blocks, IO_TREE_RELOC_BLOCKS);
3977 return rc;4022 return rc;
3978}4023}
@@ -4004,7 +4049,7 @@ static void describe_relocation(struct btrfs_fs_info *fs_info, · 25 unmodified lines
4004 block_group->start, buf);4049 block_group->start, buf);
4005}4050}
4006 4051
4007static const char *stage_to_string(int stage)4052static const char *stage_to_string(enum reloc_stage stage)
4008{4053{
4009 if (stage == MOVE_DATA_EXTENTS)4054 if (stage == MOVE_DATA_EXTENTS)
4010 return "move data extents";4055 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
4120 WARN_ON(ret && ret != -EAGAIN);4165 WARN_ON(ret && ret != -EAGAIN);
4121 4166
4122 while (1) {4167 while (1) {
4123 int finishes_stage;4168 enum reloc_stage finishes_stage;
4124 4169
4125 mutex_lock(&fs_info->cleaner_mutex);4170 mutex_lock(&fs_info->cleaner_mutex);
4126 ret = relocate_block_group(rc);4171 ret = relocate_block_group(rc);
@@ -4303,7 +4348,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) · 176 unmodified lines
4303 goto out_unset;4348 goto out_unset;
4304 }4349 }
4305 4350
4306 rc->merge_reloc_tree = 1;4351 rc->merge_reloc_tree = true;
4307 4352
4308 while (!list_empty(&reloc_roots)) {4353 while (!list_empty(&reloc_roots)) {
4309 reloc_root = list_entry(reloc_roots.next,4354 reloc_root = list_entry(reloc_roots.next,
@@ -4422,7 +4467,8 @@ int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered) · 112 unmodified lines
4422}4467}
4423 4468
4424int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,4469int 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,
4426 struct extent_buffer *cow)4472 struct extent_buffer *cow)
4427{4473{
4428 struct btrfs_fs_info *fs_info = root->fs_info;4474 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
4561 *4607 *
4562 * Return U64_MAX if no running relocation.4608 * Return U64_MAX if no running relocation.
4563 */4609 */
4564u64 btrfs_get_reloc_bg_bytenr(struct btrfs_fs_info *fs_info)4610u64 btrfs_get_reloc_bg_bytenr(const struct btrfs_fs_info *fs_info)
4565{4611{
4566 u64 logical = U64_MAX;4612 u64 logical = U64_MAX;
4567 4613
fs/btrfs/relocation.h+5−4
@@ -10,15 +10,16 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, · 9 unmodified lines
10int btrfs_recover_relocation(struct btrfs_fs_info *fs_info);10int btrfs_recover_relocation(struct btrfs_fs_info *fs_info);
11int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered);11int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered);
12int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,12int 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,
14 struct extent_buffer *cow);15 struct extent_buffer *cow);
15void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,16void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
16 u64 *bytes_to_reserve);17 u64 *bytes_to_reserve);
17int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,18int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
18 struct btrfs_pending_snapshot *pending);19 struct btrfs_pending_snapshot *pending);
19int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info);20int btrfs_should_cancel_balance(const struct btrfs_fs_info *fs_info);
20struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr);21struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr);
21int btrfs_should_ignore_reloc_root(struct btrfs_root *root);22bool btrfs_should_ignore_reloc_root(const struct btrfs_root *root);
22u64 btrfs_get_reloc_bg_bytenr(struct btrfs_fs_info *fs_info);23u64 btrfs_get_reloc_bg_bytenr(const struct btrfs_fs_info *fs_info);
23 24
24#endif25#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
51}51}
52 52
53/*53/*
54 * btrfs_find_root - lookup the root by the key.54 * Lookup the root by the key.
55 *
55 * root: the root of the root tree56 * root: the root of the root tree
56 * search_key: the key to search57 * search_key: the key to search
57 * path: the path we search58 * path: the path we search
@@ -191,7 +192,7 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root · 133 unmodified lines
191 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));192 btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
192 193
193 write_extent_buffer(l, item, ptr, sizeof(*item));194 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]);
195out:196out:
196 btrfs_free_path(path);197 btrfs_free_path(path);
197 return ret;198 return ret;
@@ -438,7 +439,7 @@ again: · 240 unmodified lines
438 btrfs_set_root_ref_name_len(leaf, ref, name->len);439 btrfs_set_root_ref_name_len(leaf, ref, name->len);
439 ptr = (unsigned long)(ref + 1);440 ptr = (unsigned long)(ref + 1);
440 write_extent_buffer(leaf, name->name, ptr, name->len);441 write_extent_buffer(leaf, name->name, ptr, name->len);
441 btrfs_mark_buffer_dirty(leaf);442 btrfs_mark_buffer_dirty(trans, leaf);
442 443
443 if (key.type == BTRFS_ROOT_BACKREF_KEY) {444 if (key.type == BTRFS_ROOT_BACKREF_KEY) {
444 btrfs_release_path(path);445 btrfs_release_path(path);
@@ -485,7 +486,8 @@ void btrfs_update_root_times(struct btrfs_trans_handle *trans, · 40 unmodified lines
485}486}
486 487
487/*488/*
488 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation489 * Reserve space for subvolume operation.
490 *
489 * root: the root of the parent directory491 * root: the root of the parent directory
490 * rsv: block reservation492 * rsv: block reservation
491 * items: the number of items that we need do reservation493 * 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
508 struct btrfs_fs_info *fs_info = root->fs_info;510 struct btrfs_fs_info *fs_info = root->fs_info;
509 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;511 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
510 512
511 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {513 if (btrfs_qgroup_enabled(fs_info)) {
512 /* One for parent inode, two for dir entries */514 /* One for parent inode, two for dir entries */
513 qgroup_num_bytes = 3 * fs_info->nodesize;515 qgroup_num_bytes = 3 * fs_info->nodesize;
514 ret = btrfs_qgroup_reserve_meta_prealloc(root,516 ret = btrfs_qgroup_reserve_meta_prealloc(root,
fs/btrfs/root-tree.h+4−4
@@ -3,6 +3,8 @@ · 2 unmodified lines
3#ifndef BTRFS_ROOT_TREE_H3#ifndef BTRFS_ROOT_TREE_H
4#define BTRFS_ROOT_TREE_H4#define BTRFS_ROOT_TREE_H
5 5
6struct fscrypt_str;
7
6int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,8int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
7 struct btrfs_block_rsv *rsv,9 struct btrfs_block_rsv *rsv,
8 int nitems, bool use_global_rsv);10 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
18int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,20int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
19 const struct btrfs_key *key,21 const struct btrfs_key *key,
20 struct btrfs_root_item *item);22 struct btrfs_root_item *item);
21int __must_check btrfs_update_root(struct btrfs_trans_handle *trans,23int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
22 struct btrfs_root *root,24 struct btrfs_key *key, struct btrfs_root_item *item);
23 struct btrfs_key *key,
24 struct btrfs_root_item *item);
25int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,25int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
26 struct btrfs_path *path, struct btrfs_root_item *root_item,26 struct btrfs_path *path, struct btrfs_root_item *root_item,
27 struct btrfs_key *root_key);27 struct btrfs_key *root_key);
fs/btrfs/scrub.c+74−4
@@ -16,7 +16,6 @@ · 15 unmodified lines
16#include "backref.h"16#include "backref.h"
17#include "extent_io.h"17#include "extent_io.h"
18#include "dev-replace.h"18#include "dev-replace.h"
19#include "check-integrity.h"
20#include "raid56.h"19#include "raid56.h"
21#include "block-group.h"20#include "block-group.h"
22#include "zoned.h"21#include "zoned.h"
@@ -24,6 +23,7 @@ · 1 unmodified line
24#include "accessors.h"23#include "accessors.h"
25#include "file-item.h"24#include "file-item.h"
26#include "scrub.h"25#include "scrub.h"
26#include "raid-stripe-tree.h"
27 27
28/*28/*
29 * This is only the first step towards a full-features scrub. It reads all29 * 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
897 ASSERT(stripe->mirror_num >= 1);897 ASSERT(stripe->mirror_num >= 1);
898 ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,898 ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
899 stripe->logical, &mapped_len, &bioc,899 stripe->logical, &mapped_len, &bioc,
900 NULL, NULL, 1);900 NULL, NULL);
901 /*901 /*
902 * If we failed, dev will be NULL, and later detailed reports902 * If we failed, dev will be NULL, and later detailed reports
903 * will just be skipped.903 * will just be skipped.
@@ -1635,6 +1635,71 @@ static void scrub_reset_stripe(struct scrub_stripe *stripe) · 731 unmodified lines
1635 }1635 }
1636}1636}
1637 1637
1638static 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
1638static void scrub_submit_initial_read(struct scrub_ctx *sctx,1703static void scrub_submit_initial_read(struct scrub_ctx *sctx,
1639 struct scrub_stripe *stripe)1704 struct scrub_stripe *stripe)
1640{1705{
@@ -1646,6 +1711,11 @@ static void scrub_submit_initial_read(struct scrub_ctx *sctx, · 5 unmodified lines
1646 ASSERT(stripe->mirror_num > 0);1711 ASSERT(stripe->mirror_num > 0);
1647 ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));1712 ASSERT(test_bit(SCRUB_STRIPE_FLAG_INITIALIZED, &stripe->state));
1648 1713
1714 if (btrfs_need_stripe_tree_update(fs_info, stripe->bg->flags)) {
1715 scrub_submit_extent_sector_read(sctx, stripe);
1716 return;
1717 }
1718
1649 bbio = btrfs_bio_alloc(SCRUB_STRIPE_PAGES, REQ_OP_READ, fs_info,1719 bbio = btrfs_bio_alloc(SCRUB_STRIPE_PAGES, REQ_OP_READ, fs_info,
1650 scrub_read_endio, stripe);1720 scrub_read_endio, stripe);
1651 1721
@@ -1952,7 +2022,7 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx, · 300 unmodified lines
1952 2022
1953 btrfs_bio_counter_inc_blocked(fs_info);2023 btrfs_bio_counter_inc_blocked(fs_info);
1954 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,2024 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,
1955 &length, &bioc, NULL, NULL, 1);2025 &length, &bioc, NULL, NULL);
1956 if (ret < 0) {2026 if (ret < 0) {
1957 btrfs_put_bioc(bioc);2027 btrfs_put_bioc(bioc);
1958 btrfs_bio_counter_dec(fs_info);2028 btrfs_bio_counter_dec(fs_info);
@@ -2717,7 +2787,7 @@ static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx, · 758 unmodified lines
2717 if (scrub_dev->fs_devices != fs_info->fs_devices)2787 if (scrub_dev->fs_devices != fs_info->fs_devices)
2718 gen = scrub_dev->generation;2788 gen = scrub_dev->generation;
2719 else2789 else
2720 gen = fs_info->last_trans_committed;2790 gen = btrfs_get_last_trans_committed(fs_info);
2721 2791
2722 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {2792 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2723 bytenr = btrfs_sb_offset(i);2793 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
796 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);796 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
797 put_unaligned_le32(0, &hdr->crc);797 put_unaligned_le32(0, &hdr->crc);
798 798
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);
800 put_unaligned_le32(crc, &hdr->crc);800 put_unaligned_le32(crc, &hdr->crc);
801 801
802 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,802 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
5669 hdr = (struct btrfs_cmd_header *)sctx->send_buf;5669 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
5670 hdr->len = cpu_to_le32(sctx->send_size + disk_num_bytes - sizeof(*hdr));5670 hdr->len = cpu_to_le32(sctx->send_size + disk_num_bytes - sizeof(*hdr));
5671 hdr->crc = 0;5671 hdr->crc = 0;
5672 crc = btrfs_crc32c(0, sctx->send_buf, sctx->send_size);5672 crc = crc32c(0, sctx->send_buf, sctx->send_size);
5673 crc = btrfs_crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes);5673 crc = crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes);
5674 hdr->crc = cpu_to_le32(crc);5674 hdr->crc = cpu_to_le32(crc);
5675 5675
5676 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,5676 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
345 struct btrfs_space_info *space_info,345 struct btrfs_space_info *space_info,
346 enum btrfs_reserve_flush_enum flush)346 enum btrfs_reserve_flush_enum flush)
347{347{
348 struct btrfs_space_info *data_sinfo;
348 u64 profile;349 u64 profile;
349 u64 avail;350 u64 avail;
351 u64 data_chunk_size;
350 int factor;352 int factor;
351 353
352 if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)354 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
364 */366 */
365 factor = btrfs_bg_type_to_factor(profile);367 factor = btrfs_bg_type_to_factor(profile);
366 avail = div_u64(avail, factor);368 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;
367 399
368 /*400 /*
369 * If we aren't flushing all things, let us overcommit up to401 * 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
556 return nr;588 return nr;
557}589}
558 590
559static 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
571#define EXTENT_SIZE_PER_ITEM SZ_256K591#define EXTENT_SIZE_PER_ITEM SZ_256K
572 592
573/*593/*
@@ -749,10 +769,9 @@ static void flush_space(struct btrfs_fs_info *fs_info, · 175 unmodified lines
749 break;769 break;
750 }770 }
751 if (state == FLUSH_DELAYED_REFS_NR)771 if (state == FLUSH_DELAYED_REFS_NR)
752 nr = calc_delayed_refs_nr(fs_info, num_bytes);772 btrfs_run_delayed_refs(trans, num_bytes);
753 else773 else
754 nr = 0;774 btrfs_run_delayed_refs(trans, 0);
755 btrfs_run_delayed_refs(trans, nr);
756 btrfs_end_transaction(trans);775 btrfs_end_transaction(trans);
757 break;776 break;
758 case ALLOC_CHUNK:777 case ALLOC_CHUNK:
@@ -978,7 +997,8 @@ static bool steal_from_global_rsv(struct btrfs_fs_info *fs_info, · 219 unmodified lines
978}997}
979 998
980/*999/*
981 * maybe_fail_all_tickets - we've exhausted our flushing, start failing tickets1000 * We've exhausted our flushing, start failing tickets.
1001 *
982 * @fs_info - fs_info for this fs1002 * @fs_info - fs_info for this fs
983 * @space_info - the space info we were flushing1003 * @space_info - the space info we were flushing
984 *1004 *
@@ -1742,7 +1762,7 @@ static int __reserve_bytes(struct btrfs_fs_info *fs_info, · 757 unmodified lines
1742 * Try to reserve metadata bytes from the block_rsv's space.1762 * Try to reserve metadata bytes from the block_rsv's space.
1743 *1763 *
1744 * @fs_info: the filesystem1764 * @fs_info: the filesystem
1745 * @block_rsv: block_rsv we're allocating for1765 * @space_info: the space_info we're allocating for
1746 * @orig_bytes: number of bytes we want1766 * @orig_bytes: number of bytes we want
1747 * @flush: whether or not we can flush to make our reservation1767 * @flush: whether or not we can flush to make our reservation
1748 *1768 *
@@ -1754,21 +1774,19 @@ static int __reserve_bytes(struct btrfs_fs_info *fs_info, · 5 unmodified lines
1754 * space already.1774 * space already.
1755 */1775 */
1756int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,1776int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
1757 struct btrfs_block_rsv *block_rsv,1777 struct btrfs_space_info *space_info,
1758 u64 orig_bytes,1778 u64 orig_bytes,
1759 enum btrfs_reserve_flush_enum flush)1779 enum btrfs_reserve_flush_enum flush)
1760{1780{
1761 int ret;1781 int ret;
1762 1782
1763 ret = __reserve_bytes(fs_info, block_rsv->space_info, orig_bytes, flush);1783 ret = __reserve_bytes(fs_info, space_info, orig_bytes, flush);
1764 if (ret == -ENOSPC) {1784 if (ret == -ENOSPC) {
1765 trace_btrfs_space_reservation(fs_info, "space_info:enospc",1785 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
1766 block_rsv->space_info->flags,1786 space_info->flags, orig_bytes, 1);
1767 orig_bytes, 1);
1768 1787
1769 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))1788 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
1770 btrfs_dump_space_info(fs_info, block_rsv->space_info,1789 btrfs_dump_space_info(fs_info, space_info, orig_bytes, 0);
1771 orig_bytes, 0);
1772 }1790 }
1773 return ret;1791 return ret;
1774}1792}
fs/btrfs/space-info.h+2−1
@@ -3,6 +3,7 @@ · 2 unmodified lines
3#ifndef BTRFS_SPACE_INFO_H3#ifndef BTRFS_SPACE_INFO_H
4#define BTRFS_SPACE_INFO_H4#define BTRFS_SPACE_INFO_H
5 5
6#include <trace/events/btrfs.h>
6#include "volumes.h"7#include "volumes.h"
7 8
8/*9/*
@@ -212,7 +213,7 @@ void btrfs_dump_space_info(struct btrfs_fs_info *fs_info, · 203 unmodified lines
212 struct btrfs_space_info *info, u64 bytes,213 struct btrfs_space_info *info, u64 bytes,
213 int dump_block_groups);214 int dump_block_groups);
214int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,215int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
215 struct btrfs_block_rsv *block_rsv,216 struct btrfs_space_info *space_info,
216 u64 orig_bytes,217 u64 orig_bytes,
217 enum btrfs_reserve_flush_enum flush);218 enum btrfs_reserve_flush_enum flush);
218void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,219void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
fs/btrfs/super.c+23−64
@@ -26,6 +26,7 @@ · 25 unmodified lines
26#include <linux/ratelimit.h>26#include <linux/ratelimit.h>
27#include <linux/crc32c.h>27#include <linux/crc32c.h>
28#include <linux/btrfs.h>28#include <linux/btrfs.h>
29#include <linux/security.h>
29#include "messages.h"30#include "messages.h"
30#include "delayed-inode.h"31#include "delayed-inode.h"
31#include "ctree.h"32#include "ctree.h"
@@ -129,9 +130,6 @@ enum { · 97 unmodified lines
129 Opt_inode_cache, Opt_noinode_cache,130 Opt_inode_cache, Opt_noinode_cache,
130 131
131 /* Debugging options */132 /* Debugging options */
132 Opt_check_integrity,
133 Opt_check_integrity_including_extent_data,
134 Opt_check_integrity_print_mask,
135 Opt_enospc_debug, Opt_noenospc_debug,133 Opt_enospc_debug, Opt_noenospc_debug,
136#ifdef CONFIG_BTRFS_DEBUG134#ifdef CONFIG_BTRFS_DEBUG
137 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,135 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
@@ -200,9 +198,6 @@ static const match_table_t tokens = { · 62 unmodified lines
200 {Opt_recovery, "recovery"},198 {Opt_recovery, "recovery"},
201 199
202 /* Debugging options */200 /* 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"},
206 {Opt_enospc_debug, "enospc_debug"},201 {Opt_enospc_debug, "enospc_debug"},
207 {Opt_noenospc_debug, "noenospc_debug"},202 {Opt_noenospc_debug, "noenospc_debug"},
208#ifdef CONFIG_BTRFS_DEBUG203#ifdef CONFIG_BTRFS_DEBUG
@@ -707,44 +702,6 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, · 498 unmodified lines
707 case Opt_skip_balance:702 case Opt_skip_balance:
708 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);703 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
709 break;704 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
748 case Opt_fatal_errors:705 case Opt_fatal_errors:
749 if (strcmp(args[0].from, "panic") == 0) {706 if (strcmp(args[0].from, "panic") == 0) {
750 btrfs_set_opt(info->mount_opt,707 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
889 error = -ENOMEM;846 error = -ENOMEM;
890 goto out;847 goto out;
891 }848 }
892 device = btrfs_scan_one_device(device_name, flags);849 device = btrfs_scan_one_device(device_name, flags, false);
893 kfree(device_name);850 kfree(device_name);
894 if (IS_ERR(device)) {851 if (IS_ERR(device)) {
895 error = PTR_ERR(device);852 error = PTR_ERR(device);
@@ -1305,15 +1262,6 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) · 409 unmodified lines
1305 seq_puts(seq, ",autodefrag");1262 seq_puts(seq, ",autodefrag");
1306 if (btrfs_test_opt(info, SKIP_BALANCE))1263 if (btrfs_test_opt(info, SKIP_BALANCE))
1307 seq_puts(seq, ",skip_balance");1264 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
1317 if (info->metadata_ratio)1265 if (info->metadata_ratio)
1318 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);1266 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
1319 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))1267 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
1484 goto error_fs_info;1432 goto error_fs_info;
1485 }1433 }
1486 1434
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);
1488 if (IS_ERR(device)) {1441 if (IS_ERR(device)) {
1489 mutex_unlock(&uuid_mutex);1442 mutex_unlock(&uuid_mutex);
1490 error = PTR_ERR(device);1443 error = PTR_ERR(device);
@@ -2196,7 +2149,11 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd, · 705 unmodified lines
2196 switch (cmd) {2149 switch (cmd) {
2197 case BTRFS_IOC_SCAN_DEV:2150 case BTRFS_IOC_SCAN_DEV:
2198 mutex_lock(&uuid_mutex);2151 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);
2200 ret = PTR_ERR_OR_ZERO(device);2157 ret = PTR_ERR_OR_ZERO(device);
2201 mutex_unlock(&uuid_mutex);2158 mutex_unlock(&uuid_mutex);
2202 break;2159 break;
@@ -2210,8 +2167,12 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd, · 7 unmodified lines
2210 break;2167 break;
2211 case BTRFS_IOC_DEVICES_READY:2168 case BTRFS_IOC_DEVICES_READY:
2212 mutex_lock(&uuid_mutex);2169 mutex_lock(&uuid_mutex);
2213 device = btrfs_scan_one_device(vol->name, BLK_OPEN_READ);2170 /*
2214 if (IS_ERR(device)) {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)) {
2215 mutex_unlock(&uuid_mutex);2176 mutex_unlock(&uuid_mutex);
2216 ret = PTR_ERR(device);2177 ret = PTR_ERR(device);
2217 break;2178 break;
@@ -2256,6 +2217,7 @@ static int check_dev_super(struct btrfs_device *dev) · 38 unmodified lines
2256{2217{
2257 struct btrfs_fs_info *fs_info = dev->fs_info;2218 struct btrfs_fs_info *fs_info = dev->fs_info;
2258 struct btrfs_super_block *sb;2219 struct btrfs_super_block *sb;
2220 u64 last_trans;
2259 u16 csum_type;2221 u16 csum_type;
2260 int ret = 0;2222 int ret = 0;
2261 2223
@@ -2291,10 +2253,10 @@ static int check_dev_super(struct btrfs_device *dev) · 29 unmodified lines
2291 if (ret < 0)2253 if (ret < 0)
2292 goto out;2254 goto out;
2293 2255
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) {
2295 btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",2258 btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
2296 btrfs_super_generation(sb),2259 btrfs_super_generation(sb), last_trans);
2297 fs_info->last_trans_committed);
2298 ret = -EUCLEAN;2260 ret = -EUCLEAN;
2299 goto out;2261 goto out;
2300 }2262 }
@@ -2404,9 +2366,6 @@ static int __init btrfs_print_mod_info(void) · 103 unmodified lines
2404#ifdef CONFIG_BTRFS_ASSERT2366#ifdef CONFIG_BTRFS_ASSERT
2405 ", assert=on"2367 ", assert=on"
2406#endif2368#endif
2407#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2408 ", integrity-checker=on"
2409#endif
2410#ifdef CONFIG_BTRFS_FS_REF_VERIFY2369#ifdef CONFIG_BTRFS_FS_REF_VERIFY
2411 ", ref-verify=on"2370 ", ref-verify=on"
2412#endif2371#endif
fs/btrfs/sysfs.c+52−1
@@ -291,12 +291,15 @@ BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); · 290 unmodified lines
291BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);291BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
292BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);292BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
293BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);293BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
294BTRFS_FEAT_ATTR_INCOMPAT(simple_quota, SIMPLE_QUOTA);
294#ifdef CONFIG_BLK_DEV_ZONED295#ifdef CONFIG_BLK_DEV_ZONED
295BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);296BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
296#endif297#endif
297#ifdef CONFIG_BTRFS_DEBUG298#ifdef CONFIG_BTRFS_DEBUG
298/* Remove once support for extent tree v2 is feature complete */299/* Remove once support for extent tree v2 is feature complete */
299BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);300BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
301/* Remove once support for raid stripe tree is feature complete. */
302BTRFS_FEAT_ATTR_INCOMPAT(raid_stripe_tree, RAID_STRIPE_TREE);
300#endif303#endif
301#ifdef CONFIG_FS_VERITY304#ifdef CONFIG_FS_VERITY
302BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);305BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
@@ -322,11 +325,13 @@ static struct attribute *btrfs_supported_feature_attrs[] = { · 19 unmodified lines
322 BTRFS_FEAT_ATTR_PTR(free_space_tree),325 BTRFS_FEAT_ATTR_PTR(free_space_tree),
323 BTRFS_FEAT_ATTR_PTR(raid1c34),326 BTRFS_FEAT_ATTR_PTR(raid1c34),
324 BTRFS_FEAT_ATTR_PTR(block_group_tree),327 BTRFS_FEAT_ATTR_PTR(block_group_tree),
328 BTRFS_FEAT_ATTR_PTR(simple_quota),
325#ifdef CONFIG_BLK_DEV_ZONED329#ifdef CONFIG_BLK_DEV_ZONED
326 BTRFS_FEAT_ATTR_PTR(zoned),330 BTRFS_FEAT_ATTR_PTR(zoned),
327#endif331#endif
328#ifdef CONFIG_BTRFS_DEBUG332#ifdef CONFIG_BTRFS_DEBUG
329 BTRFS_FEAT_ATTR_PTR(extent_tree_v2),333 BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
334 BTRFS_FEAT_ATTR_PTR(raid_stripe_tree),
330#endif335#endif
331#ifdef CONFIG_FS_VERITY336#ifdef CONFIG_FS_VERITY
332 BTRFS_FEAT_ATTR_PTR(verity),337 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
420}425}
421BTRFS_ATTR(static_feature, acl, acl_show);426BTRFS_ATTR(static_feature, acl, acl_show);
422 427
428static 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}
433BTRFS_ATTR(static_feature, temp_fsid, temp_fsid_supported_show);
434
423/*435/*
424 * Features which only depend on kernel version.436 * Features which only depend on kernel version.
425 *437 *
@@ -433,6 +445,7 @@ static struct attribute *btrfs_supported_static_feature_attrs[] = { · 7 unmodified lines
433 BTRFS_ATTR_PTR(static_feature, send_stream_version),445 BTRFS_ATTR_PTR(static_feature, send_stream_version),
434 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),446 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
435 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),447 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
448 BTRFS_ATTR_PTR(static_feature, temp_fsid),
436 NULL449 NULL
437};450};
438 451
@@ -1196,10 +1209,19 @@ static ssize_t btrfs_generation_show(struct kobject *kobj, · 757 unmodified lines
1196{1209{
1197 struct btrfs_fs_info *fs_info = to_fs_info(kobj);1210 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1198 1211
1199 return sysfs_emit(buf, "%llu\n", fs_info->generation);1212 return sysfs_emit(buf, "%llu\n", btrfs_get_fs_generation(fs_info));
1200}1213}
1201BTRFS_ATTR(, generation, btrfs_generation_show);1214BTRFS_ATTR(, generation, btrfs_generation_show);
1202 1215
1216static 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}
1223BTRFS_ATTR(, temp_fsid, btrfs_temp_fsid_show);
1224
1203static const char * const btrfs_read_policy_name[] = { "pid" };1225static const char * const btrfs_read_policy_name[] = { "pid" };
1204 1226
1205static ssize_t btrfs_read_policy_show(struct kobject *kobj,1227static ssize_t btrfs_read_policy_show(struct kobject *kobj,
@@ -1302,6 +1324,7 @@ static const struct attribute *btrfs_attrs[] = { · 96 unmodified lines
1302 BTRFS_ATTR_PTR(, read_policy),1324 BTRFS_ATTR_PTR(, read_policy),
1303 BTRFS_ATTR_PTR(, bg_reclaim_threshold),1325 BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1304 BTRFS_ATTR_PTR(, commit_stats),1326 BTRFS_ATTR_PTR(, commit_stats),
1327 BTRFS_ATTR_PTR(, temp_fsid),
1305 NULL,1328 NULL,
1306};1329};
1307 1330
@@ -2086,6 +2109,33 @@ static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj, · 778 unmodified lines
2086}2109}
2087BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);2110BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);
2088 2111
2112static 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}
2137BTRFS_ATTR(qgroups, mode, qgroup_mode_show);
2138
2089static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,2139static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
2090 struct kobj_attribute *a,2140 struct kobj_attribute *a,
2091 char *buf)2141 char *buf)
@@ -2148,6 +2198,7 @@ static struct attribute *qgroups_attrs[] = { · 56 unmodified lines
2148 BTRFS_ATTR_PTR(qgroups, enabled),2198 BTRFS_ATTR_PTR(qgroups, enabled),
2149 BTRFS_ATTR_PTR(qgroups, inconsistent),2199 BTRFS_ATTR_PTR(qgroups, inconsistent),
2150 BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),2200 BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
2201 BTRFS_ATTR_PTR(qgroups, mode),
2151 NULL2202 NULL
2152};2203};
2153ATTRIBUTE_GROUPS(qgroups);2204ATTRIBUTE_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
61 key.type = BTRFS_EXTENT_CSUM_KEY;61 key.type = BTRFS_EXTENT_CSUM_KEY;
62 key.offset = 0;62 key.offset = 0;
63 63
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);
65 write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),69 write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0),
66 value_len);70 value_len);
67 71
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
34 key.type = BTRFS_EXTENT_DATA_KEY;34 key.type = BTRFS_EXTENT_DATA_KEY;
35 key.offset = start;35 key.offset = start;
36 36
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);
38 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);42 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
39 btrfs_set_file_extent_generation(leaf, fi, 1);43 btrfs_set_file_extent_generation(leaf, fi, 1);
40 btrfs_set_file_extent_type(leaf, fi, type);44 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
64 key.type = BTRFS_INODE_ITEM_KEY;68 key.type = BTRFS_INODE_ITEM_KEY;
65 key.offset = 0;69 key.offset = 0;
66 70
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);
68}76}
69 77
70/*78/*
fs/btrfs/transaction.c+140−89
@@ -386,7 +386,7 @@ loop: · 385 unmodified lines
386 IO_TREE_TRANS_DIRTY_PAGES);386 IO_TREE_TRANS_DIRTY_PAGES);
387 extent_io_tree_init(fs_info, &cur_trans->pinned_extents,387 extent_io_tree_init(fs_info, &cur_trans->pinned_extents,
388 IO_TREE_FS_PINNED_EXTENTS);388 IO_TREE_FS_PINNED_EXTENTS);
389 fs_info->generation++;389 btrfs_set_fs_generation(fs_info, fs_info->generation + 1);
390 cur_trans->transid = fs_info->generation;390 cur_trans->transid = fs_info->generation;
391 fs_info->running_transaction = cur_trans;391 fs_info->running_transaction = cur_trans;
392 cur_trans->aborted = 0;392 cur_trans->aborted = 0;
@@ -561,6 +561,69 @@ static inline bool need_reserve_reloc_root(struct btrfs_root *root) · 168 unmodified lines
561 return true;561 return true;
562}562}
563 563
564static 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
564static struct btrfs_trans_handle *627static struct btrfs_trans_handle *
565start_transaction(struct btrfs_root *root, unsigned int num_items,628start_transaction(struct btrfs_root *root, unsigned int num_items,
566 unsigned int type, enum btrfs_reserve_flush_enum flush,629 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
568{631{
569 struct btrfs_fs_info *fs_info = root->fs_info;632 struct btrfs_fs_info *fs_info = root->fs_info;
570 struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;633 struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
634 struct btrfs_block_rsv *trans_rsv = &fs_info->trans_block_rsv;
571 struct btrfs_trans_handle *h;635 struct btrfs_trans_handle *h;
572 struct btrfs_transaction *cur_trans;636 struct btrfs_transaction *cur_trans;
573 u64 num_bytes = 0;637 u64 num_bytes = 0;
574 u64 qgroup_reserved = 0;638 u64 qgroup_reserved = 0;
639 u64 delayed_refs_bytes = 0;
575 bool reloc_reserved = false;640 bool reloc_reserved = false;
576 bool do_chunk_alloc = false;641 bool do_chunk_alloc = false;
577 int ret;642 int ret;
@@ -594,9 +659,6 @@ start_transaction(struct btrfs_root *root, unsigned int num_items, · 16 unmodified lines
594 * the appropriate flushing if need be.659 * the appropriate flushing if need be.
595 */660 */
596 if (num_items && root != fs_info->chunk_root) {661 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
600 qgroup_reserved = num_items * fs_info->nodesize;662 qgroup_reserved = num_items * fs_info->nodesize;
601 /*663 /*
602 * Use prealloc for now, as there might be a currently running664 * 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
608 if (ret)670 if (ret)
609 return ERR_PTR(ret);671 return ERR_PTR(ret);
610 672
673 num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);
611 /*674 /*
612 * We want to reserve all the bytes we may need all at once, so675 * If we plan to insert/update/delete "num_items" from a btree,
613 * we only do 1 enospc flushing cycle per transaction start. We676 * we will also generate delayed refs for extent buffers in the
614 * accomplish this by simply assuming we'll do num_items worth677 * respective btree paths, so reserve space for the delayed refs
615 * of delayed refs updates in this trans handle, and refill that678 * that will be generated by the caller as it modifies btrees.
616 * amount for whatever is missing in the reserve.679 * Try to reserve them to avoid excessive use of the global
680 * block reserve.
617 */681 */
618 num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_items);682 delayed_refs_bytes = btrfs_calc_delayed_ref_bytes(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 }
625 683
626 /*684 /*
627 * Do the reservation for the relocation root creation685 * 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
631 reloc_reserved = true;689 reloc_reserved = true;
632 }690 }
633 691
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);
635 if (ret)694 if (ret)
636 goto reserve_fail;695 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);
642 696
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)
644 do_chunk_alloc = true;700 do_chunk_alloc = true;
645 } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&701 } else if (num_items == 0 && flush == BTRFS_RESERVE_FLUSH_ALL &&
646 !btrfs_block_rsv_full(delayed_refs_rsv)) {702 !btrfs_block_rsv_full(delayed_refs_rsv)) {
@@ -700,6 +756,7 @@ again: · 53 unmodified lines
700 756
701 h->type = type;757 h->type = type;
702 INIT_LIST_HEAD(&h->new_bgs);758 INIT_LIST_HEAD(&h->new_bgs);
759 btrfs_init_metadata_block_rsv(fs_info, &h->delayed_rsv, BTRFS_BLOCK_RSV_DELOPS);
703 760
704 smp_mb();761 smp_mb();
705 if (cur_trans->state >= TRANS_STATE_COMMIT_START &&762 if (cur_trans->state >= TRANS_STATE_COMMIT_START &&
@@ -712,8 +769,17 @@ again: · 6 unmodified lines
712 if (num_bytes) {769 if (num_bytes) {
713 trace_btrfs_space_reservation(fs_info, "transaction",770 trace_btrfs_space_reservation(fs_info, "transaction",
714 h->transid, num_bytes, 1);771 h->transid, num_bytes, 1);
715 h->block_rsv = &fs_info->trans_block_rsv;772 h->block_rsv = trans_rsv;
716 h->bytes_reserved = num_bytes;773 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 }
717 h->reloc_reserved = reloc_reserved;783 h->reloc_reserved = reloc_reserved;
718 }784 }
719 785
@@ -769,8 +835,10 @@ join_fail: · 49 unmodified lines
769 kmem_cache_free(btrfs_trans_handle_cachep, h);835 kmem_cache_free(btrfs_trans_handle_cachep, h);
770alloc_fail:836alloc_fail:
771 if (num_bytes)837 if (num_bytes)
772 btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,838 btrfs_block_rsv_release(fs_info, trans_rsv, num_bytes, NULL);
773 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);
774reserve_fail:842reserve_fail:
775 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);843 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
776 return ERR_PTR(ret);844 return ERR_PTR(ret);
@@ -817,7 +885,7 @@ struct btrfs_trans_handle *btrfs_join_transaction_nostart(struct btrfs_root *roo · 40 unmodified lines
817}885}
818 886
819/*887/*
820 * btrfs_attach_transaction() - catch the running transaction888 * Catch the running transaction.
821 *889 *
822 * It is used when we want to commit the current the transaction, but890 * It is used when we want to commit the current the transaction, but
823 * don't want to start a new one.891 * 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
836}904}
837 905
838/*906/*
839 * btrfs_attach_transaction_barrier() - catch the running transaction907 * Catch the running transaction.
840 *908 *
841 * It is similar to the above function, the difference is this one909 * It is similar to the above function, the difference is this one
842 * will wait for all the inactive transactions until they fully910 * 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
912 int ret = 0;980 int ret = 0;
913 981
914 if (transid) {982 if (transid) {
915 if (transid <= fs_info->last_trans_committed)983 if (transid <= btrfs_get_last_trans_committed(fs_info))
916 goto out;984 goto out;
917 985
918 /* find specified transaction */986 /* find specified transaction */
@@ -936,7 +1004,7 @@ int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid) · 17 unmodified lines
936 * raced with btrfs_commit_transaction1004 * raced with btrfs_commit_transaction
937 */1005 */
938 if (!cur_trans) {1006 if (!cur_trans) {
939 if (transid > fs_info->last_trans_committed)1007 if (transid > btrfs_get_last_trans_committed(fs_info))
940 ret = -EINVAL;1008 ret = -EINVAL;
941 goto out;1009 goto out;
942 }1010 }
@@ -991,11 +1059,14 @@ static void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans) · 48 unmodified lines
991 1059
992 if (!trans->block_rsv) {1060 if (!trans->block_rsv) {
993 ASSERT(!trans->bytes_reserved);1061 ASSERT(!trans->bytes_reserved);
1062 ASSERT(!trans->delayed_refs_bytes_reserved);
994 return;1063 return;
995 }1064 }
996 1065
997 if (!trans->bytes_reserved)1066 if (!trans->bytes_reserved) {
1067 ASSERT(!trans->delayed_refs_bytes_reserved);
998 return;1068 return;
1069 }
999 1070
1000 ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);1071 ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
1001 trace_btrfs_space_reservation(fs_info, "transaction",1072 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
1003 btrfs_block_rsv_release(fs_info, trans->block_rsv,1074 btrfs_block_rsv_release(fs_info, trans->block_rsv,
1004 trans->bytes_reserved, NULL);1075 trans->bytes_reserved, NULL);
1005 trans->bytes_reserved = 0;1076 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;
1006}1087}
1007 1088
1008static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,1089static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
@@ -1334,7 +1415,7 @@ again: · 325 unmodified lines
1334 }1415 }
1335 1416
1336 /* Now flush any delayed refs generated by updating all of the roots */1417 /* 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);
1338 if (ret)1419 if (ret)
1339 return ret;1420 return ret;
1340 1421
@@ -1349,7 +1430,7 @@ again: · 8 unmodified lines
1349 * so we want to keep this flushing in this loop to make sure1430 * so we want to keep this flushing in this loop to make sure
1350 * everything gets run.1431 * everything gets run.
1351 */1432 */
1352 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);1433 ret = btrfs_run_delayed_refs(trans, U64_MAX);
1353 if (ret)1434 if (ret)
1354 return ret;1435 return ret;
1355 }1436 }
@@ -1483,45 +1564,6 @@ static noinline int commit_fs_roots(struct btrfs_trans_handle *trans) · 127 unmodified lines
1483 return 0;1564 return 0;
1484}1565}
1485 1566
1486/*
1487 * defrag a given btree.
1488 * Every leaf in the btree is read and defragged.
1489 */
1490int 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
1525/*1567/*
1526 * Do all special snapshot related qgroup dirty hack.1568 * Do all special snapshot related qgroup dirty hack.
1527 *1569 *
@@ -1539,11 +1581,10 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, · 11 unmodified lines
1539 int ret;1581 int ret;
1540 1582
1541 /*1583 /*
1542 * Save some performance in the case that qgroups are not1584 * Save some performance in the case that qgroups are not enabled. If
1543 * enabled. If this check races with the ioctl, rescan will1585 * this check races with the ioctl, rescan will kick in anyway.
1544 * kick in anyway.
1545 */1586 */
1546 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))1587 if (!btrfs_qgroup_full_accounting(fs_info))
1547 return 0;1588 return 0;
1548 1589
1549 /*1590 /*
@@ -1567,7 +1608,7 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, · 17 unmodified lines
1567 * for now flush the delayed refs to narrow the race window where the1608 * for now flush the delayed refs to narrow the race window where the
1568 * qgroup counters could end up wrong.1609 * qgroup counters could end up wrong.
1569 */1610 */
1570 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);1611 ret = btrfs_run_delayed_refs(trans, U64_MAX);
1571 if (ret) {1612 if (ret) {
1572 btrfs_abort_transaction(trans, ret);1613 btrfs_abort_transaction(trans, ret);
1573 return ret;1614 return ret;
@@ -1582,7 +1623,7 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans, · 8 unmodified lines
1582 1623
1583 /* Now qgroup are all updated, we can inherit it to new qgroups */1624 /* Now qgroup are all updated, we can inherit it to new qgroups */
1584 ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,1625 ret = btrfs_qgroup_inherit(trans, src->root_key.objectid, dst_objectid,
1585 inherit);1626 parent->root_key.objectid, inherit);
1586 if (ret < 0)1627 if (ret < 0)
1587 goto out;1628 goto out;
1588 1629
@@ -1732,6 +1773,12 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, · 143 unmodified lines
1732 }1773 }
1733 btrfs_release_path(path);1774 btrfs_release_path(path);
1734 1775
1776 ret = btrfs_create_qgroup(trans, objectid);
1777 if (ret) {
1778 btrfs_abort_transaction(trans, ret);
1779 goto fail;
1780 }
1781
1735 /*1782 /*
1736 * pull in the delayed directory update1783 * pull in the delayed directory update
1737 * and the delayed inode item1784 * and the delayed inode item
@@ -1843,8 +1890,12 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, · 105 unmodified lines
1843 * To co-operate with that hack, we do hack again.1890 * To co-operate with that hack, we do hack again.
1844 * Or snapshot will be greatly slowed down by a subtree qgroup rescan1891 * Or snapshot will be greatly slowed down by a subtree qgroup rescan
1845 */1892 */
1846 ret = qgroup_account_snapshot(trans, root, parent_root,1893 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL)
1847 pending->inherit, objectid);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);
1848 if (ret < 0)1899 if (ret < 0)
1849 goto fail;1900 goto fail;
1850 1901
@@ -1862,7 +1913,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, · 11 unmodified lines
1862 fname.disk_name.len * 2);1913 fname.disk_name.len * 2);
1863 inode_set_mtime_to_ts(parent_inode,1914 inode_set_mtime_to_ts(parent_inode,
1864 inode_set_ctime_current(parent_inode));1915 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));
1866 if (ret) {1917 if (ret) {
1867 btrfs_abort_transaction(trans, ret);1918 btrfs_abort_transaction(trans, ret);
1868 goto fail;1919 goto fail;
@@ -2085,7 +2136,7 @@ static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans) · 216 unmodified lines
2085 struct btrfs_block_group *block_group, *tmp;2136 struct btrfs_block_group *block_group, *tmp;
2086 2137
2087 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {2138 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);
2089 list_del_init(&block_group->bg_list);2140 list_del_init(&block_group->bg_list);
2090 }2141 }
2091}2142}
@@ -2404,7 +2455,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) · 312 unmodified lines
2404 if (ret)2455 if (ret)
2405 goto unlock_reloc;2456 goto unlock_reloc;
2406 2457
2407 ret = btrfs_run_delayed_refs(trans, (unsigned long)-1);2458 ret = btrfs_run_delayed_refs(trans, U64_MAX);
2408 if (ret)2459 if (ret)
2409 goto unlock_reloc;2460 goto unlock_reloc;
2410 2461
@@ -2537,7 +2588,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans) · 126 unmodified lines
2537 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))2588 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
2538 btrfs_clear_space_info_full(fs_info);2589 btrfs_clear_space_info_full(fs_info);
2539 2590
2540 fs_info->last_trans_committed = cur_trans->transid;2591 btrfs_set_last_trans_committed(fs_info, cur_trans->transid);
2541 /*2592 /*
2542 * We needn't acquire the lock here because there is no other task2593 * We needn't acquire the lock here because there is no other task
2543 * which can change it.2594 * which can change it.
@@ -2655,18 +2706,18 @@ int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info) · 111 unmodified lines
2655 */2706 */
2656void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,2707void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
2657 const char *function,2708 const char *function,
2658 unsigned int line, int errno, bool first_hit)2709 unsigned int line, int error, bool first_hit)
2659{2710{
2660 struct btrfs_fs_info *fs_info = trans->fs_info;2711 struct btrfs_fs_info *fs_info = trans->fs_info;
2661 2712
2662 WRITE_ONCE(trans->aborted, errno);2713 WRITE_ONCE(trans->aborted, error);
2663 WRITE_ONCE(trans->transaction->aborted, errno);2714 WRITE_ONCE(trans->transaction->aborted, error);
2664 if (first_hit && errno == -ENOSPC)2715 if (first_hit && error == -ENOSPC)
2665 btrfs_dump_space_info_for_trans_abort(fs_info);2716 btrfs_dump_space_info_for_trans_abort(fs_info);
2666 /* Wake up anybody who may be waiting on this transaction */2717 /* Wake up anybody who may be waiting on this transaction */
2667 wake_up(&fs_info->transaction_wait);2718 wake_up(&fs_info->transaction_wait);
2668 wake_up(&fs_info->transaction_blocked_wait);2719 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);
2670}2721}
2671 2722
2672int __init btrfs_transaction_init(void)2723int __init btrfs_transaction_init(void)
fs/btrfs/transaction.h+11−9
@@ -118,8 +118,10 @@ enum { · 117 unmodified lines
118struct btrfs_trans_handle {118struct btrfs_trans_handle {
119 u64 transid;119 u64 transid;
120 u64 bytes_reserved;120 u64 bytes_reserved;
121 u64 delayed_refs_bytes_reserved;
121 u64 chunk_bytes_reserved;122 u64 chunk_bytes_reserved;
122 unsigned long delayed_ref_updates;123 unsigned long delayed_ref_updates;
124 unsigned long delayed_ref_csum_deletions;
123 struct btrfs_transaction *transaction;125 struct btrfs_transaction *transaction;
124 struct btrfs_block_rsv *block_rsv;126 struct btrfs_block_rsv *block_rsv;
125 struct btrfs_block_rsv *orig_rsv;127 struct btrfs_block_rsv *orig_rsv;
@@ -139,6 +141,7 @@ struct btrfs_trans_handle { · 13 unmodified lines
139 bool in_fsync;141 bool in_fsync;
140 struct btrfs_fs_info *fs_info;142 struct btrfs_fs_info *fs_info;
141 struct list_head new_bgs;143 struct list_head new_bgs;
144 struct btrfs_block_rsv delayed_rsv;
142};145};
143 146
144/*147/*
@@ -172,7 +175,7 @@ static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans, · 27 unmodified lines
172{175{
173 spin_lock(&inode->lock);176 spin_lock(&inode->lock);
174 inode->last_trans = trans->transaction->transid;177 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);
176 inode->last_log_commit = inode->last_sub_trans - 1;179 inode->last_log_commit = inode->last_sub_trans - 1;
177 spin_unlock(&inode->lock);180 spin_unlock(&inode->lock);
178}181}
@@ -200,32 +203,32 @@ static inline void btrfs_clear_skip_qgroup(struct btrfs_trans_handle *trans) · 21 unmodified lines
200 delayed_refs->qgroup_to_skip = 0;203 delayed_refs->qgroup_to_skip = 0;
201}204}
202 205
203bool __cold abort_should_print_stack(int errno);206bool __cold abort_should_print_stack(int error);
204 207
205/*208/*
206 * Call btrfs_abort_transaction as early as possible when an error condition is209 * Call btrfs_abort_transaction as early as possible when an error condition is
207 * detected, that way the exact stack trace is reported for some errors.210 * detected, that way the exact stack trace is reported for some errors.
208 */211 */
209#define btrfs_abort_transaction(trans, errno) \212#define btrfs_abort_transaction(trans, error) \
210do { \213do { \
211 bool first = false; \214 bool first = false; \
212 /* Report first abort since mount */ \215 /* Report first abort since mount */ \
213 if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \216 if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \
214 &((trans)->fs_info->fs_state))) { \217 &((trans)->fs_info->fs_state))) { \
215 first = true; \218 first = true; \
216 if (WARN(abort_should_print_stack(errno), \219 if (WARN(abort_should_print_stack(error), \
217 KERN_ERR \220 KERN_ERR \
218 "BTRFS: Transaction aborted (error %d)\n", \221 "BTRFS: Transaction aborted (error %d)\n", \
219 (errno))) { \222 (error))) { \
220 /* Stack trace printed. */ \223 /* Stack trace printed. */ \
221 } else { \224 } else { \
222 btrfs_err((trans)->fs_info, \225 btrfs_err((trans)->fs_info, \
223 "Transaction aborted (error %d)", \226 "Transaction aborted (error %d)", \
224 (errno)); \227 (error)); \
225 } \228 } \
226 } \229 } \
227 __btrfs_abort_transaction((trans), __func__, \230 __btrfs_abort_transaction((trans), __func__, \
228 __LINE__, (errno), first); \231 __LINE__, (error), first); \
229} while (0)232} while (0)
230 233
231int btrfs_end_transaction(struct btrfs_trans_handle *trans);234int btrfs_end_transaction(struct btrfs_trans_handle *trans);
@@ -243,7 +246,6 @@ struct btrfs_trans_handle *btrfs_attach_transaction_barrier( · 11 unmodified lines
243int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);246int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
244 247
245void btrfs_add_dead_root(struct btrfs_root *root);248void btrfs_add_dead_root(struct btrfs_root *root);
246int btrfs_defrag_root(struct btrfs_root *root);
247void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);249void btrfs_maybe_wake_unfinished_drop(struct btrfs_fs_info *fs_info);
248int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);250int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
249int btrfs_commit_transaction(struct btrfs_trans_handle *trans);251int 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
264void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);266void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
265void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,267void __cold __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
266 const char *function,268 const char *function,
267 unsigned int line, int errno, bool first_hit);269 unsigned int line, int error, bool first_hit);
268 270
269int __init btrfs_transaction_init(void);271int __init btrfs_transaction_init(void);
270void __cold btrfs_transaction_exit(void);272void __cold btrfs_transaction_exit(void);
fs/btrfs/tree-checker.c+47−1
@@ -29,6 +29,8 @@ · 28 unmodified lines
29#include "accessors.h"29#include "accessors.h"
30#include "file-item.h"30#include "file-item.h"
31#include "inode-item.h"31#include "inode-item.h"
32#include "dir-item.h"
33#include "raid-stripe-tree.h"
32 34
33/*35/*
34 * Error message should follow the following format:36 * Error message should follow the following format:
@@ -1465,6 +1467,9 @@ static int check_extent_item(struct extent_buffer *leaf, · 1430 unmodified lines
1465 }1467 }
1466 inline_refs += btrfs_shared_data_ref_count(leaf, sref);1468 inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1467 break;1469 break;
1470 case BTRFS_EXTENT_OWNER_REF_KEY:
1471 WARN_ON(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
1472 break;
1468 default:1473 default:
1469 extent_err(leaf, slot, "unknown inline ref type: %u",1474 extent_err(leaf, slot, "unknown inline ref type: %u",
1470 inline_type);1475 inline_type);
@@ -1631,6 +1636,44 @@ static int check_inode_ref(struct extent_buffer *leaf, · 160 unmodified lines
1631 return 0;1636 return 0;
1632}1637}
1633 1638
1639static 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
1634/*1677/*
1635 * Common point to switch the item-specific validation.1678 * Common point to switch the item-specific validation.
1636 */1679 */
@@ -1685,6 +1728,9 @@ static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf, · 48 unmodified lines
1685 case BTRFS_EXTENT_DATA_REF_KEY:1728 case BTRFS_EXTENT_DATA_REF_KEY:
1686 ret = check_extent_data_ref(leaf, key, slot);1729 ret = check_extent_data_ref(leaf, key, slot);
1687 break;1730 break;
1731 case BTRFS_RAID_STRIPE_KEY:
1732 ret = check_raid_stripe_extent(leaf, key, slot);
1733 break;
1688 }1734 }
1689 1735
1690 if (ret)1736 if (ret)
@@ -2005,7 +2051,7 @@ int btrfs_verify_level_key(struct extent_buffer *eb, int level, · 314 unmodified lines
2005 * So we only checks tree blocks which is read from disk, whose2051 * So we only checks tree blocks which is read from disk, whose
2006 * generation <= fs_info->last_trans_committed.2052 * generation <= fs_info->last_trans_committed.
2007 */2053 */
2008 if (btrfs_header_generation(eb) > fs_info->last_trans_committed)2054 if (btrfs_header_generation(eb) > btrfs_get_last_trans_committed(fs_info))
2009 return 0;2055 return 0;
2010 2056
2011 /* We have @first_key, so this @eb must have at least one item */2057 /* 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
347 }347 }
348 348
349 if (wc->pin) {349 if (wc->pin) {
350 ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start,350 ret = btrfs_pin_extent_for_log_replay(wc->trans, eb);
351 eb->len);
352 if (ret)351 if (ret)
353 return ret;352 return ret;
354 353
@@ -504,9 +503,9 @@ insert: · 149 unmodified lines
504 found_size = btrfs_item_size(path->nodes[0],503 found_size = btrfs_item_size(path->nodes[0],
505 path->slots[0]);504 path->slots[0]);
506 if (found_size > item_size)505 if (found_size > item_size)
507 btrfs_truncate_item(path, item_size, 1);506 btrfs_truncate_item(trans, path, item_size, 1);
508 else if (found_size < item_size)507 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);
510 } else if (ret) {509 } else if (ret) {
511 return ret;510 return ret;
512 }511 }
@@ -574,7 +573,7 @@ insert: · 61 unmodified lines
574 }573 }
575 }574 }
576no_copy:575no_copy:
577 btrfs_mark_buffer_dirty(path->nodes[0]);576 btrfs_mark_buffer_dirty(trans, path->nodes[0]);
578 btrfs_release_path(path);577 btrfs_release_path(path);
579 return 0;578 return 0;
580}579}
@@ -767,7 +766,8 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, · 186 unmodified lines
767 } else if (ret == 0) {766 } else if (ret == 0) {
768 btrfs_init_generic_ref(&ref,767 btrfs_init_generic_ref(&ref,
769 BTRFS_ADD_DELAYED_REF,768 BTRFS_ADD_DELAYED_REF,
770 ins.objectid, ins.offset, 0);769 ins.objectid, ins.offset, 0,
770 root->root_key.objectid);
771 btrfs_init_data_ref(&ref,771 btrfs_init_data_ref(&ref,
772 root->root_key.objectid,772 root->root_key.objectid,
773 key->objectid, offset, 0, false);773 key->objectid, offset, 0, false);
@@ -890,7 +890,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, · 116 unmodified lines
890 890
891update_inode:891update_inode:
892 btrfs_update_inode_bytes(BTRFS_I(inode), nbytes, drop_args.bytes_found);892 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));
894out:894out:
895 iput(inode);895 iput(inode);
896 return ret;896 return ret;
@@ -1445,7 +1445,7 @@ static noinline int add_inode_ref(struct btrfs_trans_handle *trans, · 548 unmodified lines
1445 if (ret)1445 if (ret)
1446 goto out;1446 goto out;
1447 1447
1448 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));1448 ret = btrfs_update_inode(trans, BTRFS_I(inode));
1449 if (ret)1449 if (ret)
1450 goto out;1450 goto out;
1451 }1451 }
@@ -1483,8 +1483,7 @@ out: · 31 unmodified lines
1483 return ret;1483 return ret;
1484}1484}
1485 1485
1486static int count_inode_extrefs(struct btrfs_root *root,1486static int count_inode_extrefs(struct btrfs_inode *inode, struct btrfs_path *path)
1487 struct btrfs_inode *inode, struct btrfs_path *path)
1488{1487{
1489 int ret = 0;1488 int ret = 0;
1490 int name_len;1489 int name_len;
@@ -1498,8 +1497,8 @@ static int count_inode_extrefs(struct btrfs_root *root, · 7 unmodified lines
1498 struct extent_buffer *leaf;1497 struct extent_buffer *leaf;
1499 1498
1500 while (1) {1499 while (1) {
1501 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,1500 ret = btrfs_find_one_extref(inode->root, inode_objectid, offset,
1502 &extref, &offset);1501 path, &extref, &offset);
1503 if (ret)1502 if (ret)
1504 break;1503 break;
1505 1504
@@ -1527,8 +1526,7 @@ static int count_inode_extrefs(struct btrfs_root *root, · 21 unmodified lines
1527 return nlink;1526 return nlink;
1528}1527}
1529 1528
1530static int count_inode_refs(struct btrfs_root *root,1529static int count_inode_refs(struct btrfs_inode *inode, struct btrfs_path *path)
1531 struct btrfs_inode *inode, struct btrfs_path *path)
1532{1530{
1533 int ret;1531 int ret;
1534 struct btrfs_key key;1532 struct btrfs_key key;
@@ -1543,7 +1541,7 @@ static int count_inode_refs(struct btrfs_root *root, · 8 unmodified lines
1543 key.offset = (u64)-1;1541 key.offset = (u64)-1;
1544 1542
1545 while (1) {1543 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);
1547 if (ret < 0)1545 if (ret < 0)
1548 break;1546 break;
1549 if (ret > 0) {1547 if (ret > 0) {
@@ -1595,9 +1593,9 @@ process_slot: · 45 unmodified lines
1595 * will free the inode.1593 * will free the inode.
1596 */1594 */
1597static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,1595static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1598 struct btrfs_root *root,
1599 struct inode *inode)1596 struct inode *inode)
1600{1597{
1598 struct btrfs_root *root = BTRFS_I(inode)->root;
1601 struct btrfs_path *path;1599 struct btrfs_path *path;
1602 int ret;1600 int ret;
1603 u64 nlink = 0;1601 u64 nlink = 0;
@@ -1607,13 +1605,13 @@ static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, · 3 unmodified lines
1607 if (!path)1605 if (!path)
1608 return -ENOMEM;1606 return -ENOMEM;
1609 1607
1610 ret = count_inode_refs(root, BTRFS_I(inode), path);1608 ret = count_inode_refs(BTRFS_I(inode), path);
1611 if (ret < 0)1609 if (ret < 0)
1612 goto out;1610 goto out;
1613 1611
1614 nlink = ret;1612 nlink = ret;
1615 1613
1616 ret = count_inode_extrefs(root, BTRFS_I(inode), path);1614 ret = count_inode_extrefs(BTRFS_I(inode), path);
1617 if (ret < 0)1615 if (ret < 0)
1618 goto out;1616 goto out;
1619 1617
@@ -1623,7 +1621,7 @@ static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans, · 3 unmodified lines
1623 1621
1624 if (nlink != inode->i_nlink) {1622 if (nlink != inode->i_nlink) {
1625 set_nlink(inode, nlink);1623 set_nlink(inode, nlink);
1626 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));1624 ret = btrfs_update_inode(trans, BTRFS_I(inode));
1627 if (ret)1625 if (ret)
1628 goto out;1626 goto out;
1629 }1627 }
@@ -1685,7 +1683,7 @@ static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, · 55 unmodified lines
1685 break;1683 break;
1686 }1684 }
1687 1685
1688 ret = fixup_inode_link_count(trans, root, inode);1686 ret = fixup_inode_link_count(trans, inode);
1689 iput(inode);1687 iput(inode);
1690 if (ret)1688 if (ret)
1691 break;1689 break;
@@ -1732,7 +1730,7 @@ static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, · 40 unmodified lines
1732 set_nlink(inode, 1);1730 set_nlink(inode, 1);
1733 else1731 else
1734 inc_nlink(inode);1732 inc_nlink(inode);
1735 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));1733 ret = btrfs_update_inode(trans, BTRFS_I(inode));
1736 } else if (ret == -EEXIST) {1734 } else if (ret == -EEXIST) {
1737 ret = 0;1735 ret = 0;
1738 }1736 }
@@ -1939,7 +1937,7 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans, · 200 unmodified lines
1939out:1937out:
1940 if (!ret && update_size) {1938 if (!ret && update_size) {
1941 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name.len * 2);1939 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));
1943 }1941 }
1944 kfree(name.name);1942 kfree(name.name);
1945 iput(dir);1943 iput(dir);
@@ -2483,7 +2481,7 @@ static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb, · 537 unmodified lines
2483 drop_args.bytes_found);2481 drop_args.bytes_found);
2484 /* Update the inode's nbytes. */2482 /* Update the inode's nbytes. */
2485 ret = btrfs_update_inode(wc->trans,2483 ret = btrfs_update_inode(wc->trans,
2486 root, BTRFS_I(inode));2484 BTRFS_I(inode));
2487 }2485 }
2488 iput(inode);2486 iput(inode);
2489 if (ret)2487 if (ret)
@@ -2574,7 +2572,7 @@ static int clean_log_buffer(struct btrfs_trans_handle *trans, · 84 unmodified lines
2574 btrfs_tree_unlock(eb);2572 btrfs_tree_unlock(eb);
2575 2573
2576 if (trans) {2574 if (trans) {
2577 ret = btrfs_pin_reserved_extent(trans, eb->start, eb->len);2575 ret = btrfs_pin_reserved_extent(trans, eb);
2578 if (ret)2576 if (ret)
2579 return ret;2577 return ret;
2580 btrfs_redirty_list_add(trans->transaction, eb);2578 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
2848}2846}
2849 2847
2850/*2848/*
2851 * btrfs_sync_log does sends a given tree log down to the disk and2849 * Sends a given tree log down to the disk and updates the super blocks to
2852 * updates the super blocks to record it. When this call is done,2850 * record it. When this call is done, you know that any inodes previously
2853 * you know that any inodes previously logged are safely on disk only2851 * logged are safely on disk only if it returns 0.
2854 * if it returns 0.
2855 *2852 *
2856 * Any other return value means you need to call btrfs_commit_transaction.2853 * Any other return value means you need to call btrfs_commit_transaction.
2857 * Some of the edge cases for fsyncing directories that have had unlinks2854 * 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
2961 btrfs_set_root_node(&log->root_item, log->node);2958 btrfs_set_root_node(&log->root_item, log->node);
2962 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));2959 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
2963 2960
2964 root->log_transid++;2961 btrfs_set_root_log_transid(root, root->log_transid + 1);
2965 log->log_transid = root->log_transid;2962 log->log_transid = root->log_transid;
2966 root->log_start_pid = 0;2963 root->log_start_pid = 0;
2967 /*2964 /*
@@ -2999,9 +2996,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans, · 31 unmodified lines
2999 */2996 */
3000 ret = update_log_root(trans, log, &new_root_item);2997 ret = update_log_root(trans, log, &new_root_item);
3001 if (ret) {2998 if (ret) {
3002 if (!list_empty(&root_log_ctx.list))2999 list_del_init(&root_log_ctx.list);
3003 list_del_init(&root_log_ctx.list);
3004
3005 blk_finish_plug(&plug);3000 blk_finish_plug(&plug);
3006 btrfs_set_log_full_commit(trans);3001 btrfs_set_log_full_commit(trans);
3007 if (ret != -ENOSPC)3002 if (ret != -ENOSPC)
@@ -3021,7 +3016,6 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans, · 13 unmodified lines
3021 goto out;3016 goto out;
3022 }3017 }
3023 3018
3024 index2 = root_log_ctx.log_transid % 2;
3025 if (atomic_read(&log_root_tree->log_commit[index2])) {3019 if (atomic_read(&log_root_tree->log_commit[index2])) {
3026 blk_finish_plug(&plug);3020 blk_finish_plug(&plug);
3027 ret = btrfs_wait_tree_log_extents(log, mark);3021 ret = btrfs_wait_tree_log_extents(log, mark);
@@ -3136,8 +3130,8 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans, · 108 unmodified lines
3136 * someone else already started it. We use <= and not < because the3130 * someone else already started it. We use <= and not < because the
3137 * first log transaction has an ID of 0.3131 * first log transaction has an ID of 0.
3138 */3132 */
3139 ASSERT(root->last_log_commit <= log_transid);3133 ASSERT(btrfs_get_root_last_log_commit(root) <= log_transid);
3140 root->last_log_commit = log_transid;3134 btrfs_set_root_last_log_commit(root, log_transid);
3141 3135
3142out_wake_log_root:3136out_wake_log_root:
3143 mutex_lock(&log_root_tree->log_mutex);3137 mutex_lock(&log_root_tree->log_mutex);
@@ -3211,8 +3205,7 @@ static void free_log_tree(struct btrfs_trans_handle *trans, · 67 unmodified lines
3211 }3205 }
3212 }3206 }
3213 3207
3214 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,3208 extent_io_tree_release(&log->dirty_log_pages);
3215 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
3216 extent_io_tree_release(&log->log_csum_range);3209 extent_io_tree_release(&log->log_csum_range);
3217 3210
3218 btrfs_put_root(log);3211 btrfs_put_root(log);
@@ -3530,7 +3523,7 @@ static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans, · 311 unmodified lines
3530 last_offset = max(last_offset, curr_end);3523 last_offset = max(last_offset, curr_end);
3531 }3524 }
3532 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);3525 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]);
3534 btrfs_release_path(path);3527 btrfs_release_path(path);
3535 return 0;3528 return 0;
3536}3529}
@@ -4488,7 +4481,7 @@ copy_item: · 951 unmodified lines
4488 dst_index++;4481 dst_index++;
4489 }4482 }
4490 4483
4491 btrfs_mark_buffer_dirty(dst_path->nodes[0]);4484 btrfs_mark_buffer_dirty(trans, dst_path->nodes[0]);
4492 btrfs_release_path(dst_path);4485 btrfs_release_path(dst_path);
4493out:4486out:
4494 kfree(ins_data);4487 kfree(ins_data);
@@ -4693,7 +4686,7 @@ static int log_one_extent(struct btrfs_trans_handle *trans, · 198 unmodified lines
4693 write_extent_buffer(leaf, &fi,4686 write_extent_buffer(leaf, &fi,
4694 btrfs_item_ptr_offset(leaf, path->slots[0]),4687 btrfs_item_ptr_offset(leaf, path->slots[0]),
4695 sizeof(fi));4688 sizeof(fi));
4696 btrfs_mark_buffer_dirty(leaf);4689 btrfs_mark_buffer_dirty(trans, leaf);
4697 4690
4698 btrfs_release_path(path);4691 btrfs_release_path(path);
4699 4692
@@ -4921,12 +4914,12 @@ process: · 221 unmodified lines
4921 set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);4914 set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
4922 4915
4923 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {4916 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4924 spin_lock_irq(&inode->ordered_tree.lock);4917 spin_lock_irq(&inode->ordered_tree_lock);
4925 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {4918 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4926 set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);4919 set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
4927 atomic_inc(&trans->transaction->pending_ordered);4920 atomic_inc(&trans->transaction->pending_ordered);
4928 }4921 }
4929 spin_unlock_irq(&inode->ordered_tree.lock);4922 spin_unlock_irq(&inode->ordered_tree_lock);
4930 }4923 }
4931 btrfs_put_ordered_extent(ordered);4924 btrfs_put_ordered_extent(ordered);
4932 }4925 }
@@ -7204,9 +7197,7 @@ again: · 2271 unmodified lines
7204 * each subsequent pass.7197 * each subsequent pass.
7205 */7198 */
7206 if (ret == -ENOENT)7199 if (ret == -ENOENT)
7207 ret = btrfs_pin_extent_for_log_replay(trans,7200 ret = btrfs_pin_extent_for_log_replay(trans, log->node);
7208 log->node->start,
7209 log->node->len);
7210 btrfs_put_root(log);7201 btrfs_put_root(log);
7211 7202
7212 if (!ret)7203 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
223}223}
224 224
225/*225/*
226 * ulist_del - delete one node from ulist226 * Delete one node from ulist.
227 *
227 * @ulist: ulist to remove node from228 * @ulist: ulist to remove node from
228 * @val: value to delete229 * @val: value to delete
229 * @aux: aux to delete230 * @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
124 * An item with that type already exists.124 * An item with that type already exists.
125 * Extend the item and store the new subid at the end.125 * Extend the item and store the new subid at the end.
126 */126 */
127 btrfs_extend_item(path, sizeof(subid_le));127 btrfs_extend_item(trans, path, sizeof(subid_le));
128 eb = path->nodes[0];128 eb = path->nodes[0];
129 slot = path->slots[0];129 slot = path->slots[0];
130 offset = btrfs_item_ptr_offset(eb, slot);130 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
139 ret = 0;139 ret = 0;
140 subid_le = cpu_to_le64(subid_cpu);140 subid_le = cpu_to_le64(subid_cpu);
141 write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));141 write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
142 btrfs_mark_buffer_dirty(eb);142 btrfs_mark_buffer_dirty(trans, eb);
143 143
144out:144out:
145 btrfs_free_path(path);145 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
221 move_src = offset + sizeof(subid);221 move_src = offset + sizeof(subid);
222 move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));222 move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
223 memmove_extent_buffer(eb, move_dst, move_src, move_len);223 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);
225 225
226out:226out:
227 btrfs_free_path(path);227 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
487 }487 }
488 inode->ro_flags &= ~BTRFS_INODE_RO_VERITY;488 inode->ro_flags &= ~BTRFS_INODE_RO_VERITY;
489 btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode);489 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);
491 if (ret) {491 if (ret) {
492 btrfs_abort_transaction(trans, ret);492 btrfs_abort_transaction(trans, ret);
493 goto out;493 goto out;
@@ -554,7 +554,7 @@ static int finish_verity(struct btrfs_inode *inode, const void *desc, · 60 unmodified lines
554 }554 }
555 inode->ro_flags |= BTRFS_INODE_RO_VERITY;555 inode->ro_flags |= BTRFS_INODE_RO_VERITY;
556 btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode);556 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);
558 if (ret)558 if (ret)
559 goto end_trans;559 goto end_trans;
560 ret = del_orphan(trans, inode);560 ret = del_orphan(trans, inode);
fs/btrfs/volumes.c+203−214
@@ -35,6 +35,7 @@ · 34 unmodified lines
35#include "relocation.h"35#include "relocation.h"
36#include "scrub.h"36#include "scrub.h"
37#include "super.h"37#include "super.h"
38#include "raid-stripe-tree.h"
38 39
39#define BTRFS_BLOCK_GROUP_STRIPE_MASK (BTRFS_BLOCK_GROUP_RAID0 | \40#define BTRFS_BLOCK_GROUP_STRIPE_MASK (BTRFS_BLOCK_GROUP_RAID0 | \
40 BTRFS_BLOCK_GROUP_RAID10 | \41 BTRFS_BLOCK_GROUP_RAID10 | \
@@ -357,21 +358,19 @@ struct list_head * __attribute_const__ btrfs_get_fs_uuids(void) · 316 unmodified lines
357}358}
358 359
359/*360/*
360 * alloc_fs_devices - allocate struct btrfs_fs_devices361 * Allocate new btrfs_fs_devices structure identified by a fsid.
361 * @fsid: if not NULL, copy the UUID to fs_devices::fsid362 *
362 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid363 * @fsid: if not NULL, copy the UUID to fs_devices::fsid and to
364 * fs_devices::metadata_fsid
363 *365 *
364 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().366 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
365 * The returned struct is not linked onto any lists and can be destroyed with367 * The returned struct is not linked onto any lists and can be destroyed with
366 * kfree() right away.368 * kfree() right away.
367 */369 */
368static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,370static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
369 const u8 *metadata_fsid)
370{371{
371 struct btrfs_fs_devices *fs_devs;372 struct btrfs_fs_devices *fs_devs;
372 373
373 ASSERT(fsid || !metadata_fsid);
374
375 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);374 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
376 if (!fs_devs)375 if (!fs_devs)
377 return ERR_PTR(-ENOMEM);376 return ERR_PTR(-ENOMEM);
@@ -385,8 +384,7 @@ static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, · 7 unmodified lines
385 384
386 if (fsid) {385 if (fsid) {
387 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);386 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
388 memcpy(fs_devs->metadata_uuid,387 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
389 metadata_fsid ?: fsid, BTRFS_FSID_SIZE);
390 }388 }
391 389
392 return fs_devs;390 return fs_devs;
@@ -457,58 +455,6 @@ static noinline struct btrfs_fs_devices *find_fsid( · 64 unmodified lines
457 return NULL;455 return NULL;
458}456}
459 457
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 */
465static 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
473static 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
512static int458static int
513btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder,459btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder,
514 int flush, struct bdev_handle **bdev_handle,460 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
564{510{
565 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;511 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
566 struct btrfs_device *device, *tmp_device;512 struct btrfs_device *device, *tmp_device;
567 int ret = 0;513 int ret;
514 bool freed = false;
568 515
569 lockdep_assert_held(&uuid_mutex);516 lockdep_assert_held(&uuid_mutex);
570 517
571 if (devt)518 /* Return good status if there is no instance of devt. */
572 ret = -ENOENT;519 ret = 0;
573
574 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {520 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
575 521
576 mutex_lock(&fs_devices->device_list_mutex);522 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
581 if (devt && devt != device->devt)527 if (devt && devt != device->devt)
582 continue;528 continue;
583 if (fs_devices->opened) {529 if (fs_devices->opened) {
584 /* for an already deleted device return 0 */530 if (devt)
585 if (devt && ret != 0)
586 ret = -EBUSY;531 ret = -EBUSY;
587 break;532 break;
588 }533 }
@@ -592,7 +537,7 @@ static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device · 3 unmodified lines
592 list_del(&device->dev_list);537 list_del(&device->dev_list);
593 btrfs_free_device(device);538 btrfs_free_device(device);
594 539
595 ret = 0;540 freed = true;
596 }541 }
597 mutex_unlock(&fs_devices->device_list_mutex);542 mutex_unlock(&fs_devices->device_list_mutex);
598 543
@@ -603,9 +548,81 @@ static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device · 4 unmodified lines
603 }548 }
604 }549 }
605 550
551 /* If there is at least one freed device return 0. */
552 if (freed)
553 return 0;
554
606 return ret;555 return ret;
607}556}
608 557
558static 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
609/*626/*
610 * This is only used on mount, and we are protected from competing things627 * This is only used on mount, and we are protected from competing things
611 * messing with our fs_devices by the uuid_mutex, thus we do not need the628 * 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
691 return has_metadata_uuid ? sb->metadata_uuid : sb->fsid;708 return has_metadata_uuid ? sb->metadata_uuid : sb->fsid;
692}709}
693 710
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 */
700static 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
716static 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
748static 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}
772/*711/*
773 * Add new device to list of registered devices712 * Add new device to list of registered devices
774 *713 *
@@ -787,10 +726,16 @@ static noinline struct btrfs_device *device_list_add(const char *path, · 12 unmodified lines
787 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);726 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
788 dev_t path_devt;727 dev_t path_devt;
789 int error;728 int error;
729 bool same_fsid_diff_dev = false;
790 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &730 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
791 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);731 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
792 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &732
793 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);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 }
794 739
795 error = lookup_bdev(path, &path_devt);740 error = lookup_bdev(path, &path_devt);
796 if (error) {741 if (error) {
@@ -799,27 +744,23 @@ static noinline struct btrfs_device *device_list_add(const char *path, · 2 unmodified lines
799 return ERR_PTR(error);744 return ERR_PTR(error);
800 }745 }
801 746
802 if (fsid_change_in_progress) {747 fs_devices = find_fsid_by_device(disk_super, path_devt, &same_fsid_diff_dev);
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
815 748
816 if (!fs_devices) {749 if (!fs_devices) {
817 fs_devices = alloc_fs_devices(disk_super->fsid,750 fs_devices = alloc_fs_devices(disk_super->fsid);
818 has_metadata_uuid ? disk_super->metadata_uuid : NULL);751 if (has_metadata_uuid)
752 memcpy(fs_devices->metadata_uuid,
753 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
754
819 if (IS_ERR(fs_devices))755 if (IS_ERR(fs_devices))
820 return ERR_CAST(fs_devices);756 return ERR_CAST(fs_devices);
821 757
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 }
823 764
824 mutex_lock(&fs_devices->device_list_mutex);765 mutex_lock(&fs_devices->device_list_mutex);
825 list_add(&fs_devices->fs_list, &fs_uuids);766 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
834 mutex_lock(&fs_devices->device_list_mutex);775 mutex_lock(&fs_devices->device_list_mutex);
835 device = btrfs_find_device(fs_devices, &args);776 device = btrfs_find_device(fs_devices, &args);
836 777
837 /*778 if (found_transid > fs_devices->latest_generation) {
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) {
844 memcpy(fs_devices->fsid, disk_super->fsid,779 memcpy(fs_devices->fsid, disk_super->fsid,
845 BTRFS_FSID_SIZE);780 BTRFS_FSID_SIZE);
846 memcpy(fs_devices->metadata_uuid,781 memcpy(fs_devices->metadata_uuid,
847 btrfs_sb_fsid_ptr(disk_super), BTRFS_FSID_SIZE);782 btrfs_sb_fsid_ptr(disk_super), BTRFS_FSID_SIZE);
848 fs_devices->fsid_change = false;
849 }783 }
850 }784 }
851 785
@@ -999,7 +933,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) · 147 unmodified lines
999 933
1000 lockdep_assert_held(&uuid_mutex);934 lockdep_assert_held(&uuid_mutex);
1001 935
1002 fs_devices = alloc_fs_devices(orig->fsid, NULL);936 fs_devices = alloc_fs_devices(orig->fsid);
1003 if (IS_ERR(fs_devices))937 if (IS_ERR(fs_devices))
1004 return fs_devices;938 return fs_devices;
1005 939
@@ -1359,9 +1293,14 @@ int btrfs_forget_devices(dev_t devt) · 353 unmodified lines
1359/*1293/*
1360 * Look for a btrfs signature on a device. This may be called out of the mount path1294 * Look for a btrfs signature on a device. This may be called out of the mount path
1361 * and we are not allowed to call set_blocksize during the scan. The superblock1295 * and we are not allowed to call set_blocksize during the scan. The superblock
1362 * is read via pagecache1296 * 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.
1363 */1301 */
1364struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags)1302struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags,
1303 bool mount_arg_dev)
1365{1304{
1366 struct btrfs_super_block *disk_super;1305 struct btrfs_super_block *disk_super;
1367 bool new_device_added = false;1306 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
1407 goto error_bdev_put;1346 goto error_bdev_put;
1408 }1347 }
1409 1348
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
1410 device = device_list_add(path, disk_super, &new_device_added);1365 device = device_list_add(path, disk_super, &new_device_added);
1411 if (!IS_ERR(device) && new_device_added)1366 if (!IS_ERR(device) && new_device_added)
1412 btrfs_free_stale_devices(device->devt, device);1367 btrfs_free_stale_devices(device->devt, device);
1413 1368
1369free_disk_super:
1414 btrfs_release_disk_super(disk_super);1370 btrfs_release_disk_super(disk_super);
1415 1371
1416error_bdev_put:1372error_bdev_put:
@@ -1898,7 +1854,7 @@ static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, · 481 unmodified lines
1898 ptr = btrfs_device_fsid(dev_item);1854 ptr = btrfs_device_fsid(dev_item);
1899 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,1855 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1900 ptr, BTRFS_FSID_SIZE);1856 ptr, BTRFS_FSID_SIZE);
1901 btrfs_mark_buffer_dirty(leaf);1857 btrfs_mark_buffer_dirty(trans, leaf);
1902 1858
1903 ret = 0;1859 ret = 0;
1904out:1860out:
@@ -2454,7 +2410,7 @@ static struct btrfs_fs_devices *btrfs_init_sprout(struct btrfs_fs_info *fs_info) · 549 unmodified lines
2454 * Private copy of the seed devices, anchored at2410 * Private copy of the seed devices, anchored at
2455 * fs_info->fs_devices->seed_list2411 * fs_info->fs_devices->seed_list
2456 */2412 */
2457 seed_devices = alloc_fs_devices(NULL, NULL);2413 seed_devices = alloc_fs_devices(NULL);
2458 if (IS_ERR(seed_devices))2414 if (IS_ERR(seed_devices))
2459 return seed_devices;2415 return seed_devices;
2460 2416
@@ -2600,7 +2556,7 @@ next_slot: · 139 unmodified lines
2600 if (device->fs_devices->seeding) {2556 if (device->fs_devices->seeding) {
2601 btrfs_set_device_generation(leaf, dev_item,2557 btrfs_set_device_generation(leaf, dev_item,
2602 device->generation);2558 device->generation);
2603 btrfs_mark_buffer_dirty(leaf);2559 btrfs_mark_buffer_dirty(trans, leaf);
2604 }2560 }
2605 2561
2606 path->slots[0]++;2562 path->slots[0]++;
@@ -2898,7 +2854,7 @@ static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, · 291 unmodified lines
2898 btrfs_device_get_disk_total_bytes(device));2854 btrfs_device_get_disk_total_bytes(device));
2899 btrfs_set_device_bytes_used(leaf, dev_item,2855 btrfs_set_device_bytes_used(leaf, dev_item,
2900 btrfs_device_get_bytes_used(device));2856 btrfs_device_get_bytes_used(device));
2901 btrfs_mark_buffer_dirty(leaf);2857 btrfs_mark_buffer_dirty(trans, leaf);
2902 2858
2903out:2859out:
2904 btrfs_free_path(path);2860 btrfs_free_path(path);
@@ -2932,6 +2888,7 @@ int btrfs_grow_device(struct btrfs_trans_handle *trans, · 27 unmodified lines
2932 btrfs_set_super_total_bytes(super_copy,2888 btrfs_set_super_total_bytes(super_copy,
2933 round_down(old_total + diff, fs_info->sectorsize));2889 round_down(old_total + diff, fs_info->sectorsize));
2934 device->fs_devices->total_rw_bytes += diff;2890 device->fs_devices->total_rw_bytes += diff;
2891 atomic64_add(diff, &fs_info->free_chunk_space);
2935 2892
2936 btrfs_device_set_total_bytes(device, new_size);2893 btrfs_device_set_total_bytes(device, new_size);
2937 btrfs_device_set_disk_total_bytes(device, new_size);2894 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
3030}2987}
3031 2988
3032/*2989/*
3033 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.2990 * Find the mapping containing the given logical extent.
2991 *
3034 * @logical: Logical block offset in bytes.2992 * @logical: Logical block offset in bytes.
3035 * @length: Length of extent in bytes.2993 * @length: Length of extent in bytes.
3036 *2994 *
@@ -3486,7 +3444,7 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info, · 449 unmodified lines
3486 3444
3487 btrfs_set_balance_flags(leaf, item, bctl->flags);3445 btrfs_set_balance_flags(leaf, item, bctl->flags);
3488 3446
3489 btrfs_mark_buffer_dirty(leaf);3447 btrfs_mark_buffer_dirty(trans, leaf);
3490out:3448out:
3491 btrfs_free_path(path);3449 btrfs_free_path(path);
3492 err = btrfs_commit_transaction(trans);3450 err = btrfs_commit_transaction(trans);
@@ -4841,6 +4799,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) · 1348 unmodified lines
4841 u64 old_size = btrfs_device_get_total_bytes(device);4799 u64 old_size = btrfs_device_get_total_bytes(device);
4842 u64 diff;4800 u64 diff;
4843 u64 start;4801 u64 start;
4802 u64 free_diff = 0;
4844 4803
4845 new_size = round_down(new_size, fs_info->sectorsize);4804 new_size = round_down(new_size, fs_info->sectorsize);
4846 start = new_size;4805 start = new_size;
@@ -4866,7 +4825,19 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) · 19 unmodified lines
4866 btrfs_device_set_total_bytes(device, new_size);4825 btrfs_device_set_total_bytes(device, new_size);
4867 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {4826 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
4868 device->fs_devices->total_rw_bytes -= diff;4827 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);
4870 }4841 }
4871 4842
4872 /*4843 /*
@@ -5001,9 +4972,10 @@ done: · 128 unmodified lines
5001 if (ret) {4972 if (ret) {
5002 mutex_lock(&fs_info->chunk_mutex);4973 mutex_lock(&fs_info->chunk_mutex);
5003 btrfs_device_set_total_bytes(device, old_size);4974 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)) {
5005 device->fs_devices->total_rw_bytes += diff;4976 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 }
5007 mutex_unlock(&fs_info->chunk_mutex);4979 mutex_unlock(&fs_info->chunk_mutex);
5008 }4980 }
5009 return ret;4981 return ret;
@@ -5883,6 +5855,7 @@ static int find_live_mirror(struct btrfs_fs_info *fs_info, · 873 unmodified lines
5883}5855}
5884 5856
5885static struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_info,5857static struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_info,
5858 u64 logical,
5886 u16 total_stripes)5859 u16 total_stripes)
5887{5860{
5888 struct btrfs_io_context *bioc;5861 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
5902 bioc->fs_info = fs_info;5875 bioc->fs_info = fs_info;
5903 bioc->replace_stripe_src = -1;5876 bioc->replace_stripe_src = -1;
5904 bioc->full_stripe_logical = (u64)-1;5877 bioc->full_stripe_logical = (u64)-1;
5878 bioc->logical = logical;
5905 5879
5906 return bioc;5880 return bioc;
5907}5881}
@@ -6206,12 +6180,20 @@ static u64 btrfs_max_io_len(struct map_lookup *map, enum btrfs_map_op op, · 298 unmodified lines
6206 return U64_MAX;6180 return U64_MAX;
6207}6181}
6208 6182
6209static void set_io_stripe(struct btrfs_io_stripe *dst, const struct map_lookup *map,6183static int set_io_stripe(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6210 u32 stripe_index, u64 stripe_offset, u32 stripe_nr)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)
6211{6187{
6212 dst->dev = map->stripes[stripe_index].dev;6188 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
6213 dst->physical = map->stripes[stripe_index].physical +6194 dst->physical = map->stripes[stripe_index].physical +
6214 stripe_offset + btrfs_stripe_nr_to_offset(stripe_nr);6195 stripe_offset + btrfs_stripe_nr_to_offset(stripe_nr);
6196 return 0;
6215}6197}
6216 6198
6217/*6199/*
@@ -6248,16 +6230,11 @@ static void set_io_stripe(struct btrfs_io_stripe *dst, const struct map_lookup * · 30 unmodified lines
6248 * For RAID6 profile, mirror > 2 means mark another6230 * For RAID6 profile, mirror > 2 means mark another
6249 * data/P stripe error and rebuild from the remaining6231 * data/P stripe error and rebuild from the remaining
6250 * stripes..6232 * 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.
6255 */6233 */
6256int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,6234int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6257 u64 logical, u64 *length,6235 u64 logical, u64 *length,
6258 struct btrfs_io_context **bioc_ret,6236 struct btrfs_io_context **bioc_ret,
6259 struct btrfs_io_stripe *smap, int *mirror_num_ret,6237 struct btrfs_io_stripe *smap, int *mirror_num_ret)
6260 int need_raid_map)
6261{6238{
6262 struct extent_map *em;6239 struct extent_map *em;
6263 struct map_lookup *map;6240 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
6352 }6329 }
6353 6330
6354 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {6331 } 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) {
6356 /*6333 /*
6334 * Needs full stripe mapping.
6335 *
6357 * Push stripe_nr back to the start of the full stripe6336 * Push stripe_nr back to the start of the full stripe
6358 * For those cases needing a full stripe, @stripe_nr6337 * For those cases needing a full stripe, @stripe_nr
6359 * is the full stripe number.6338 * 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
6376 stripe_index = 0;6355 stripe_index = 0;
6377 stripe_offset = 0;6356 stripe_offset = 0;
6378 } else {6357 } else {
6379 /*6358 ASSERT(mirror_num <= 1);
6380 * Mirror #0 or #1 means the original data block.6359 /* Just grab the data stripe directly. */
6381 * Mirror #2 is RAID5 parity block.
6382 * Mirror #3 is RAID6 Q block.
6383 */
6384 stripe_index = stripe_nr % data_stripes;6360 stripe_index = stripe_nr % data_stripes;
6385 stripe_nr /= data_stripes;6361 stripe_nr /= data_stripes;
6386 if (mirror_num > 1)
6387 stripe_index = data_stripes + mirror_num - 2;
6388 6362
6389 /* We distribute the parity blocks across stripes */6363 /* We distribute the parity blocks across stripes */
6390 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;6364 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)
6392 mirror_num = 1;6366 mirror_num = 1;
6393 }6367 }
6394 } else {6368 } else {
@@ -6427,16 +6401,18 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 32 unmodified lines
6427 * I/O context structure.6401 * I/O context structure.
6428 */6402 */
6429 if (smap && num_alloc_stripes == 1 &&6403 if (smap && num_alloc_stripes == 1 &&
6404 !(btrfs_need_stripe_tree_update(fs_info, map->type) &&
6405 op != BTRFS_MAP_READ) &&
6430 !((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && mirror_num > 1)) {6406 !((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);
6432 if (mirror_num_ret)6409 if (mirror_num_ret)
6433 *mirror_num_ret = mirror_num;6410 *mirror_num_ret = mirror_num;
6434 *bioc_ret = NULL;6411 *bioc_ret = NULL;
6435 ret = 0;
6436 goto out;6412 goto out;
6437 }6413 }
6438 6414
6439 bioc = alloc_btrfs_io_context(fs_info, num_alloc_stripes);6415 bioc = alloc_btrfs_io_context(fs_info, logical, num_alloc_stripes);
6440 if (!bioc) {6416 if (!bioc) {
6441 ret = -ENOMEM;6417 ret = -ENOMEM;
6442 goto out;6418 goto out;
@@ -6450,7 +6426,7 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, · 7 unmodified lines
6450 *6426 *
6451 * It's still mostly the same as other profiles, just with extra rotation.6427 * It's still mostly the same as other profiles, just with extra rotation.
6452 */6428 */
6453 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&6429 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
6454 (op != BTRFS_MAP_READ || mirror_num > 1)) {6430 (op != BTRFS_MAP_READ || mirror_num > 1)) {
6455 /*6431 /*
6456 * For RAID56 @stripe_nr is already the number of full stripes6432 * 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
6462 */6438 */
6463 bioc->full_stripe_logical = em->start +6439 bioc->full_stripe_logical = em->start +
6464 btrfs_stripe_nr_to_offset(stripe_nr * data_stripes);6440 btrfs_stripe_nr_to_offset(stripe_nr * data_stripes);
6465 for (i = 0; i < num_stripes; i++)6441 for (int i = 0; i < num_stripes; i++) {
6466 set_io_stripe(&bioc->stripes[i], map,6442 ret = set_io_stripe(fs_info, op, logical, length,
6467 (i + stripe_nr) % num_stripes,6443 &bioc->stripes[i], map,
6468 stripe_offset, stripe_nr);6444 (i + stripe_nr) % num_stripes,
6445 stripe_offset, stripe_nr);
6446 if (ret < 0)
6447 break;
6448 }
6469 } else {6449 } else {
6470 /*6450 /*
6471 * For all other non-RAID56 profiles, just copy the target6451 * For all other non-RAID56 profiles, just copy the target
6472 * stripe into the bioc.6452 * stripe into the bioc.
6473 */6453 */
6474 for (i = 0; i < num_stripes; i++) {6454 for (i = 0; i < num_stripes; i++) {
6475 set_io_stripe(&bioc->stripes[i], map, stripe_index,6455 ret = set_io_stripe(fs_info, op, logical, length,
6476 stripe_offset, stripe_nr);6456 &bioc->stripes[i], map, stripe_index,
6457 stripe_offset, stripe_nr);
6458 if (ret < 0)
6459 break;
6477 stripe_index++;6460 stripe_index++;
6478 }6461 }
6479 }6462 }
6480 6463
6464 if (ret) {
6465 *bioc_ret = NULL;
6466 btrfs_put_bioc(bioc);
6467 goto out;
6468 }
6469
6481 if (op != BTRFS_MAP_READ)6470 if (op != BTRFS_MAP_READ)
6482 max_errors = btrfs_chunk_max_errors(map);6471 max_errors = btrfs_chunk_max_errors(map);
6483 6472
@@ -6904,7 +6893,7 @@ static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info, · 420 unmodified lines
6904 if (!btrfs_test_opt(fs_info, DEGRADED))6893 if (!btrfs_test_opt(fs_info, DEGRADED))
6905 return ERR_PTR(-ENOENT);6894 return ERR_PTR(-ENOENT);
6906 6895
6907 fs_devices = alloc_fs_devices(fsid, NULL);6896 fs_devices = alloc_fs_devices(fsid);
6908 if (IS_ERR(fs_devices))6897 if (IS_ERR(fs_devices))
6909 return fs_devices;6898 return fs_devices;
6910 6899
@@ -7537,7 +7526,7 @@ static int update_dev_stat_item(struct btrfs_trans_handle *trans, · 626 unmodified lines
7537 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)7526 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7538 btrfs_set_dev_stats_value(eb, ptr, i,7527 btrfs_set_dev_stats_value(eb, ptr, i,
7539 btrfs_dev_stat_read(device, i));7528 btrfs_dev_stat_read(device, i));
7540 btrfs_mark_buffer_dirty(eb);7529 btrfs_mark_buffer_dirty(trans, eb);
7541 7530
7542out:7531out:
7543 btrfs_free_path(path);7532 btrfs_free_path(path);
@@ -8079,7 +8068,7 @@ int btrfs_map_repair_block(struct btrfs_fs_info *fs_info, · 535 unmodified lines
8079 ASSERT(mirror_num > 0);8068 ASSERT(mirror_num > 0);
8080 8069
8081 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, &map_length,8070 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, &map_length,
8082 &bioc, smap, &mirror_ret, true);8071 &bioc, smap, &mirror_ret);
8083 if (ret < 0)8072 if (ret < 0)
8084 return ret;8073 return ret;
8085 8074
fs/btrfs/volumes.h+29−10
@@ -288,6 +288,19 @@ struct btrfs_fs_devices { · 287 unmodified lines
288 * - Following shall be true at all times:288 * - Following shall be true at all times:
289 * - metadata_uuid == btrfs_header::fsid289 * - metadata_uuid == btrfs_header::fsid
290 * - metadata_uuid == btrfs_dev_item::fsid290 * - 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
291 */304 */
292 u8 metadata_uuid[BTRFS_FSID_SIZE];305 u8 metadata_uuid[BTRFS_FSID_SIZE];
293 306
@@ -351,9 +364,10 @@ struct btrfs_fs_devices { · 57 unmodified lines
351 bool rotating;364 bool rotating;
352 /* Devices support TRIM/discard commands. */365 /* Devices support TRIM/discard commands. */
353 bool discardable;366 bool discardable;
354 bool fsid_change;
355 /* The filesystem is a seed filesystem. */367 /* The filesystem is a seed filesystem. */
356 bool seeding;368 bool seeding;
369 /* The mount needs to use a randomly generated fsid. */
370 bool temp_fsid;
357 371
358 struct btrfs_fs_info *fs_info;372 struct btrfs_fs_info *fs_info;
359 /* sysfs kobjects */373 /* sysfs kobjects */
@@ -379,12 +393,12 @@ struct btrfs_fs_devices { · 19 unmodified lines
379 393
380struct btrfs_io_stripe {394struct btrfs_io_stripe {
381 struct btrfs_device *dev;395 struct btrfs_device *dev;
382 union {396 /* Block mapping. */
383 /* Block mapping */397 u64 physical;
384 u64 physical;398 u64 length;
385 /* For the endio handler */399 bool is_scrub;
386 struct btrfs_io_context *bioc;400 /* For the endio handler. */
387 };401 struct btrfs_io_context *bioc;
388};402};
389 403
390struct btrfs_discard_stripe {404struct btrfs_discard_stripe {
@@ -417,6 +431,11 @@ struct btrfs_io_context { · 26 unmodified lines
417 atomic_t error;431 atomic_t error;
418 u16 max_errors;432 u16 max_errors;
419 433
434 u64 logical;
435 u64 size;
436 /* Raid stripe tree ordered entry. */
437 struct list_head rst_ordered_entry;
438
420 /*439 /*
421 * The total number of stripes, including the extra duplicated440 * The total number of stripes, including the extra duplicated
422 * stripe for replace.441 * stripe for replace.
@@ -594,8 +613,7 @@ void btrfs_put_bioc(struct btrfs_io_context *bioc); · 171 unmodified lines
594int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,613int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
595 u64 logical, u64 *length,614 u64 logical, u64 *length,
596 struct btrfs_io_context **bioc_ret,615 struct btrfs_io_context **bioc_ret,
597 struct btrfs_io_stripe *smap, int *mirror_num_ret,616 struct btrfs_io_stripe *smap, int *mirror_num_ret);
598 int need_raid_map);
599int btrfs_map_repair_block(struct btrfs_fs_info *fs_info,617int btrfs_map_repair_block(struct btrfs_fs_info *fs_info,
600 struct btrfs_io_stripe *smap, u64 logical,618 struct btrfs_io_stripe *smap, u64 logical,
601 u32 length, int mirror_num);619 u32 length, int mirror_num);
@@ -609,7 +627,8 @@ struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans, · 7 unmodified lines
609void btrfs_mapping_tree_free(struct extent_map_tree *tree);627void btrfs_mapping_tree_free(struct extent_map_tree *tree);
610int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,628int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
611 blk_mode_t flags, void *holder);629 blk_mode_t flags, void *holder);
612struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags);630struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags,
631 bool mount_arg_dev);
613int btrfs_forget_devices(dev_t devt);632int btrfs_forget_devices(dev_t devt);
614void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);633void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
615void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices);634void 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
188 if (old_data_len + name_len + sizeof(*di) == item_size) {188 if (old_data_len + name_len + sizeof(*di) == item_size) {
189 /* No other xattrs packed in the same leaf item. */189 /* No other xattrs packed in the same leaf item. */
190 if (size > old_data_len)190 if (size > old_data_len)
191 btrfs_extend_item(path, size - old_data_len);191 btrfs_extend_item(trans, path, size - old_data_len);
192 else if (size < old_data_len)192 else if (size < old_data_len)
193 btrfs_truncate_item(path, data_size, 1);193 btrfs_truncate_item(trans, path, data_size, 1);
194 } else {194 } else {
195 /* There are other xattrs packed in the same item. */195 /* There are other xattrs packed in the same item. */
196 ret = btrfs_delete_one_dir_name(trans, root, path, di);196 ret = btrfs_delete_one_dir_name(trans, root, path, di);
197 if (ret)197 if (ret)
198 goto out;198 goto out;
199 btrfs_extend_item(path, data_size);199 btrfs_extend_item(trans, path, data_size);
200 }200 }
201 201
202 ptr = btrfs_item_ptr(leaf, slot, char);202 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
205 btrfs_set_dir_data_len(leaf, di, size);205 btrfs_set_dir_data_len(leaf, di, size);
206 data_ptr = ((unsigned long)(di + 1)) + name_len;206 data_ptr = ((unsigned long)(di + 1)) + name_len;
207 write_extent_buffer(leaf, value, data_ptr, size);207 write_extent_buffer(leaf, value, data_ptr, size);
208 btrfs_mark_buffer_dirty(leaf);208 btrfs_mark_buffer_dirty(trans, leaf);
209 } else {209 } else {
210 /*210 /*
211 * Insert, and we had space for the xattr, so path->slots[0] is211 * 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
265 265
266 inode_inc_iversion(inode);266 inode_inc_iversion(inode);
267 inode_set_ctime_current(inode);267 inode_set_ctime_current(inode);
268 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));268 ret = btrfs_update_inode(trans, BTRFS_I(inode));
269 if (ret)269 if (ret)
270 btrfs_abort_transaction(trans, ret);270 btrfs_abort_transaction(trans, ret);
271out:271out:
@@ -408,7 +408,7 @@ static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, · 136 unmodified lines
408 if (!ret) {408 if (!ret) {
409 inode_inc_iversion(inode);409 inode_inc_iversion(inode);
410 inode_set_ctime_current(inode);410 inode_set_ctime_current(inode);
411 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));411 ret = btrfs_update_inode(trans, BTRFS_I(inode));
412 if (ret)412 if (ret)
413 btrfs_abort_transaction(trans, ret);413 btrfs_abort_transaction(trans, ret);
414 }414 }
fs/btrfs/zoned.c+289−163
@@ -1282,21 +1282,284 @@ out: · 1281 unmodified lines
1282 return ret;1282 return ret;
1283}1283}
1284 1284
1285struct zone_info {
1286 u64 physical;
1287 u64 capacity;
1288 u64 alloc_offset;
1289};
1290
1291static 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
1377static 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
1395static 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
1437static 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
1485static 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
1517static 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
1285int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)1552int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1286{1553{
1287 struct btrfs_fs_info *fs_info = cache->fs_info;1554 struct btrfs_fs_info *fs_info = cache->fs_info;
1288 struct extent_map_tree *em_tree = &fs_info->mapping_tree;1555 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1289 struct extent_map *em;1556 struct extent_map *em;
1290 struct map_lookup *map;1557 struct map_lookup *map;
1291 struct btrfs_device *device;
1292 u64 logical = cache->start;1558 u64 logical = cache->start;
1293 u64 length = cache->length;1559 u64 length = cache->length;
1560 struct zone_info *zone_info = NULL;
1294 int ret;1561 int ret;
1295 int i;1562 int i;
1296 unsigned int nofs_flag;
1297 u64 *alloc_offsets = NULL;
1298 u64 *caps = NULL;
1299 u64 *physical = NULL;
1300 unsigned long *active = NULL;1563 unsigned long *active = NULL;
1301 u64 last_alloc = 0;1564 u64 last_alloc = 0;
1302 u32 num_sequential = 0, num_conventional = 0;1565 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
1328 goto out;1591 goto out;
1329 }1592 }
1330 1593
1331 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);1594 zone_info = kcalloc(map->num_stripes, sizeof(*zone_info), GFP_NOFS);
1332 if (!alloc_offsets) {1595 if (!zone_info) {
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) {
1345 ret = -ENOMEM;1596 ret = -ENOMEM;
1346 goto out;1597 goto out;
1347 }1598 }
@@ -1353,98 +1604,14 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) · 5 unmodified lines
1353 }1604 }
1354 1605
1355 for (i = 0; i < map->num_stripes; i++) {1606 for (i = 0; i < map->num_stripes; i++) {
1356 bool is_sequential;1607 ret = btrfs_load_zone_info(fs_info, i, &zone_info[i], active, map);
1357 struct blk_zone zone;1608 if (ret)
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;
1421 goto out;1609 goto out;
1422 }
1423 1610
1424 caps[i] = (zone.capacity << SECTOR_SHIFT);1611 if (zone_info[i].alloc_offset == WP_CONVENTIONAL)
1425 1612 num_conventional++;
1426 switch (zone.cond) {1613 else
1427 case BLK_ZONE_COND_OFFLINE:1614 num_sequential++;
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 }
1448 }1615 }
1449 1616
1450 if (num_sequential > 0)1617 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
1468 1635
1469 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {1636 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1470 case 0: /* single */1637 case 0: /* single */
1471 if (alloc_offsets[0] == WP_MISSING_DEV) {1638 ret = btrfs_load_block_group_single(cache, &zone_info[0], active);
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);
1482 break;1639 break;
1483 case BTRFS_BLOCK_GROUP_DUP:1640 case BTRFS_BLOCK_GROUP_DUP:
1484 if (map->type & BTRFS_BLOCK_GROUP_DATA) {1641 ret = btrfs_load_block_group_dup(cache, map, zone_info, active);
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]);
1521 break;1642 break;
1522 case BTRFS_BLOCK_GROUP_RAID1:1643 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;
1523 case BTRFS_BLOCK_GROUP_RAID0:1648 case BTRFS_BLOCK_GROUP_RAID0:
1649 ret = btrfs_load_block_group_raid0(cache, map, zone_info, active);
1650 break;
1524 case BTRFS_BLOCK_GROUP_RAID10:1651 case BTRFS_BLOCK_GROUP_RAID10:
1652 ret = btrfs_load_block_group_raid10(cache, map, zone_info, active);
1653 break;
1525 case BTRFS_BLOCK_GROUP_RAID5:1654 case BTRFS_BLOCK_GROUP_RAID5:
1526 case BTRFS_BLOCK_GROUP_RAID6:1655 case BTRFS_BLOCK_GROUP_RAID6:
1527 /* non-single profiles are not supported yet */
1528 default:1656 default:
1529 btrfs_err(fs_info, "zoned: profile %s not yet supported",1657 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1530 btrfs_bg_type_to_raid_name(map->type));1658 btrfs_bg_type_to_raid_name(map->type));
@@ -1570,9 +1698,7 @@ out: · 39 unmodified lines
1570 cache->physical_map = NULL;1698 cache->physical_map = NULL;
1571 }1699 }
1572 bitmap_free(active);1700 bitmap_free(active);
1573 kfree(physical);1701 kfree(zone_info);
1574 kfree(caps);
1575 kfree(alloc_offsets);
1576 free_extent_map(em);1702 free_extent_map(em);
1577 1703
1578 return ret;1704 return ret;
@@ -1609,7 +1735,7 @@ void btrfs_redirty_list_add(struct btrfs_transaction *trans, · 30 unmodified lines
1609 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);1735 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1610 set_extent_buffer_dirty(eb);1736 set_extent_buffer_dirty(eb);
1611 set_extent_bit(&trans->dirty_pages, eb->start, eb->start + eb->len - 1,1737 set_extent_bit(&trans->dirty_pages, eb->start, eb->start + eb->len - 1,
1612 EXTENT_DIRTY | EXTENT_NOWAIT, NULL);1738 EXTENT_DIRTY, NULL);
1613}1739}
1614 1740
1615bool btrfs_use_zone_append(struct btrfs_bio *bbio)1741bool 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
1887 int i, ret;2013 int i, ret;
1888 2014
1889 ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,2015 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);
1891 if (ret || !bioc || mapped_length < PAGE_SIZE) {2017 if (ret || !bioc || mapped_length < PAGE_SIZE) {
1892 ret = -EIO;2018 ret = -EIO;
1893 goto out_put_bioc;2019 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
145}145}
146 146
147/*147/*
148 * zstd_calc_ws_mem_sizes - calculate monotonic memory bounds148 * Calculate monotonic memory bounds.
149 *149 *
150 * It is possible based on the level configurations that a higher level150 * It is possible based on the level configurations that a higher level
151 * workspace uses less memory than a lower level workspace. In order to reuse151 * 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
218}218}
219 219
220/*220/*
221 * zstd_find_workspace - find workspace221 * Find workspace for given level.
222 *
222 * @level: compression level223 * @level: compression level
223 *224 *
224 * This iterates over the set bits in the active_map beginning at the requested225 * 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
256}257}
257 258
258/*259/*
259 * zstd_get_workspace - zstd's get_workspace260 * Zstd get_workspace for level.
261 *
260 * @level: compression level262 * @level: compression level
261 *263 *
262 * If @level is 0, then any compression level can be used. Therefore, we begin264 * If @level is 0, then any compression level can be used. Therefore, we begin
@@ -296,7 +298,8 @@ again: · 33 unmodified lines
296}298}
297 299
298/*300/*
299 * zstd_put_workspace - zstd put_workspace301 * Zstd put_workspace.
302 *
300 * @ws: list_head for the workspace303 * @ws: list_head for the workspace
301 *304 *
302 * When putting back a workspace, we only need to update the LRU if we are of305 * 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
1561 __field( const void *, wq )1561 __field( const void *, wq )
1562 __field( const void *, func )1562 __field( const void *, func )
1563 __field( const void *, ordered_func )1563 __field( const void *, ordered_func )
1564 __field( const void *, ordered_free )
1565 __field( const void *, normal_work )1564 __field( const void *, normal_work )
1566 ),1565 ),
1567 1566
@@ -1570,14 +1569,12 @@ DECLARE_EVENT_CLASS(btrfs__work, · 2 unmodified lines
1570 __entry->wq = work->wq;1569 __entry->wq = work->wq;
1571 __entry->func = work->func;1570 __entry->func = work->func;
1572 __entry->ordered_func = work->ordered_func;1571 __entry->ordered_func = work->ordered_func;
1573 __entry->ordered_free = work->ordered_free;
1574 __entry->normal_work = &work->normal_work;1572 __entry->normal_work = &work->normal_work;
1575 ),1573 ),
1576 1574
1577 TP_printk_btrfs("work=%p (normal_work=%p) wq=%p func=%ps ordered_func=%p "1575 TP_printk_btrfs("work=%p (normal_work=%p) wq=%p func=%ps ordered_func=%p",
1578 "ordered_free=%p",
1579 __entry->work, __entry->normal_work, __entry->wq,1576 __entry->work, __entry->normal_work, __entry->wq,
1580 __entry->func, __entry->ordered_func, __entry->ordered_free)1577 __entry->func, __entry->ordered_func)
1581);1578);
1582 1579
1583/*1580/*
@@ -2497,6 +2494,82 @@ DEFINE_EVENT(btrfs_raid56_bio, raid56_write, · 913 unmodified lines
2497 TP_ARGS(rbio, bio, trace_info)2494 TP_ARGS(rbio, bio, trace_info)
2498);2495);
2499 2496
2497TRACE_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
2521TRACE_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
2547TRACE_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
2500#endif /* _TRACE_BTRFS_H */2573#endif /* _TRACE_BTRFS_H */
2501 2574
2502/* This part must be outside protection */2575/* 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
333#define BTRFS_FEATURE_INCOMPAT_RAID1C34 (1ULL << 11)333#define BTRFS_FEATURE_INCOMPAT_RAID1C34 (1ULL << 11)
334#define BTRFS_FEATURE_INCOMPAT_ZONED (1ULL << 12)334#define BTRFS_FEATURE_INCOMPAT_ZONED (1ULL << 12)
335#define BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2 (1ULL << 13)335#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)
336 338
337struct btrfs_ioctl_feature_flags {339struct btrfs_ioctl_feature_flags {
338 __u64 compat_flags;340 __u64 compat_flags;
@@ -753,6 +755,7 @@ struct btrfs_ioctl_get_dev_stats { · 414 unmodified lines
753#define BTRFS_QUOTA_CTL_ENABLE 1755#define BTRFS_QUOTA_CTL_ENABLE 1
754#define BTRFS_QUOTA_CTL_DISABLE 2756#define BTRFS_QUOTA_CTL_DISABLE 2
755#define BTRFS_QUOTA_CTL_RESCAN__NOTUSED 3757#define BTRFS_QUOTA_CTL_RESCAN__NOTUSED 3
758#define BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA 4
756struct btrfs_ioctl_quota_ctl_args {759struct btrfs_ioctl_quota_ctl_args {
757 __u64 cmd;760 __u64 cmd;
758 __u64 status;761 __u64 status;
include/uapi/linux/btrfs_tree.h+59−1
@@ -73,6 +73,9 @@ · 72 unmodified lines
73/* Holds the block group items for extent tree v2. */73/* Holds the block group items for extent tree v2. */
74#define BTRFS_BLOCK_GROUP_TREE_OBJECTID 11ULL74#define BTRFS_BLOCK_GROUP_TREE_OBJECTID 11ULL
75 75
76/* Tracks RAID stripes in block groups. */
77#define BTRFS_RAID_STRIPE_TREE_OBJECTID 12ULL
78
76/* device stats in the device tree */79/* device stats in the device tree */
77#define BTRFS_DEV_STATS_OBJECTID 0ULL80#define BTRFS_DEV_STATS_OBJECTID 0ULL
78 81
@@ -230,6 +233,14 @@ · 151 unmodified lines
230 233
231#define BTRFS_SHARED_DATA_REF_KEY 184234#define BTRFS_SHARED_DATA_REF_KEY 184
232 235
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
233/*244/*
234 * block groups give us hints into the extent allocation trees. Which245 * block groups give us hints into the extent allocation trees. Which
235 * blocks are free etc etc246 * blocks are free etc etc
@@ -261,6 +272,8 @@ · 25 unmodified lines
261#define BTRFS_DEV_ITEM_KEY 216272#define BTRFS_DEV_ITEM_KEY 216
262#define BTRFS_CHUNK_ITEM_KEY 228273#define BTRFS_CHUNK_ITEM_KEY 228
263 274
275#define BTRFS_RAID_STRIPE_KEY 230
276
264/*277/*
265 * Records the overall state of the qgroups.278 * Records the overall state of the qgroups.
266 * There's only one instance of this key present,279 * There's only one instance of this key present,
@@ -719,6 +732,30 @@ struct btrfs_free_space_header { · 452 unmodified lines
719 __le64 num_bitmaps;732 __le64 num_bitmaps;
720} __attribute__ ((__packed__));733} __attribute__ ((__packed__));
721 734
735struct 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
752struct 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
722#define BTRFS_HEADER_FLAG_WRITTEN (1ULL << 0)759#define BTRFS_HEADER_FLAG_WRITTEN (1ULL << 0)
723#define BTRFS_HEADER_FLAG_RELOC (1ULL << 1)760#define BTRFS_HEADER_FLAG_RELOC (1ULL << 1)
724 761
@@ -787,6 +824,10 @@ struct btrfs_shared_data_ref { · 62 unmodified lines
787 __le32 count;824 __le32 count;
788} __attribute__ ((__packed__));825} __attribute__ ((__packed__));
789 826
827struct btrfs_extent_owner_ref {
828 __le64 root_id;
829} __attribute__ ((__packed__));
830
790struct btrfs_extent_inline_ref {831struct btrfs_extent_inline_ref {
791 __u8 type;832 __u8 type;
792 __le64 offset;833 __le64 offset;
@@ -1204,9 +1245,17 @@ static inline __u16 btrfs_qgroup_level(__u64 qgroupid) · 411 unmodified lines
1204 */1245 */
1205#define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT (1ULL << 2)1246#define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT (1ULL << 2)
1206 1247
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
1207#define BTRFS_QGROUP_STATUS_FLAGS_MASK (BTRFS_QGROUP_STATUS_FLAG_ON | \1255#define BTRFS_QGROUP_STATUS_FLAGS_MASK (BTRFS_QGROUP_STATUS_FLAG_ON | \
1208 BTRFS_QGROUP_STATUS_FLAG_RESCAN | \1256 BTRFS_QGROUP_STATUS_FLAG_RESCAN | \
1209 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)1257 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT | \
1258 BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE)
1210 1259
1211#define BTRFS_QGROUP_STATUS_VERSION 11260#define BTRFS_QGROUP_STATUS_VERSION 1
1212 1261
@@ -1228,6 +1277,15 @@ struct btrfs_qgroup_status_item { · 15 unmodified lines
1228 * of the scan. It contains a logical address1277 * of the scan. It contains a logical address
1229 */1278 */
1230 __le64 rescan;1279 __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;
1231} __attribute__ ((__packed__));1289} __attribute__ ((__packed__));
1232 1290
1233struct btrfs_qgroup_info_item {1291struct 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