]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: count and log ordered extents that end with sectors unwritten
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Fri, 9 Oct 2026 23:13:11 +0000 (19:13 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:30 +0000 (21:16 -0500)
When an ordered extent reports to its stripe run, sectors it allocated
that no write reached are marked written so nothing waits for them (a
failed write, truncated folios, a short direct write, error teardown).
Whether any of these paths can still leave a waiter stuck in practice is
not known: the log settle that motivated the marking seals and frees a
shared LOG run before it would wait.

Count such extents and their unwritten bytes in stripe_run_stats
(unwritten_on_end, unwritten_on_end_bytes) and log each one, rate
limited, with the extent's identity, truncated length and flags, to see
which paths produce them.

One class is harmless and is counted but not logged: an extent marked
TRUNCATED, with no IOERR and not compressed (disk_num_bytes == num_bytes),
whose unwritten sectors are exactly the part truncation cut off,
[disk_bytenr + ALIGN(truncated_len, sectorsize), disk_bytenr +
disk_num_bytes).  It comes from a truncate or O_TRUNC racing writeback and
from a direct write whose bounce-buffer copy faults before copying
anything, and it would bury the lines the message is there to find.
btrfs_open_stripe_write_done_run() returns the lowest unwritten sector so
that the tail can be told from merely as many sectors.  Those extents go to
unwritten_on_end_truncated and unwritten_on_end_truncated_bytes;
unwritten_on_end and unwritten_on_end_bytes count only the logged ones, so
any increase there is worth a look.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/fs.h
fs/btrfs/ordered-data.c

index e59e3826d9bb855814c880a03135cb37c9af9128..27b7fb36361fa304c28321a6d8997e854547556d 100644 (file)
@@ -2692,26 +2692,45 @@ struct btrfs_open_stripe_run *btrfs_get_open_stripe_run(
  * write, error teardown) will not be written now.  Marking it keeps a log
  * settle from waiting for it forever, and a written sector is never padded,
  * so a later partial write to the stripe reads it instead of zeroing it.
+ *
+ * Returns the number of allocated sectors that had not been written, and
+ * in @first_unwritten the logical address of the lowest of them (U64_MAX if
+ * there are none).
  */
-void btrfs_open_stripe_write_done_run(struct btrfs_open_stripe_run *run,
-                                     u64 start, u64 num_bytes)
+u32 btrfs_open_stripe_write_done_run(struct btrfs_open_stripe_run *run,
+                                    u64 start, u64 num_bytes,
+                                    u64 *first_unwritten)
 {
        struct btrfs_block_group *bg = run->bg;
        const u32 bits = bg->fs_info->sectorsize_bits;
        unsigned long flags;
        u64 s = start;
        u64 l = num_bytes;
+       u32 unwritten = 0;
 
+       *first_unwritten = U64_MAX;
        spin_lock_irqsave(&bg->stripe_run_lock, flags);
        ASSERT(run->inflight_bytes >= num_bytes);
        run->inflight_bytes -= num_bytes;
-       if (run_written_range(run, &s, &l))
-               bitmap_set(run->written, (s - run->start) >> bits, l >> bits);
+       if (run_written_range(run, &s, &l)) {
+               const u32 first = (s - run->start) >> bits;
+               const u32 last = first + (l >> bits);
+
+               for (u32 i = first; i < last; i++) {
+                       if (!test_bit(i, run->live) || test_bit(i, run->written))
+                               continue;
+                       if (!unwritten)
+                               *first_unwritten = run->start + ((u64)i << bits);
+                       unwritten++;
+               }
+               bitmap_set(run->written, first, l >> bits);
+       }
        /* Under the lock: the run and the group may be gone after it. */
        wake_up_var(&bg->open_stripe_runs);
        if (open_stripe_run_drained(run) && !run->retained)
                free_open_stripe_run(bg, run);
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       return unwritten;
 }
 
 /*
index 75775cb52ae8ab005ddb7e6cb82a28755f9050ee..211dd5feaa27ecb86135a8b1825cf8fdf639fcaa 100644 (file)
@@ -500,8 +500,9 @@ void btrfs_open_stripe_write_done(struct btrfs_block_group *bg, u64 start,
                                  u64 num_bytes);
 void btrfs_open_stripe_write_abandoned(struct btrfs_block_group *bg, u64 start,
                                       u64 num_bytes);
-void btrfs_open_stripe_write_done_run(struct btrfs_open_stripe_run *run,
-                                     u64 start, u64 num_bytes);
+u32 btrfs_open_stripe_write_done_run(struct btrfs_open_stripe_run *run,
+                                    u64 start, u64 num_bytes,
+                                    u64 *first_unwritten);
 void btrfs_open_stripe_write_done_bytenr(struct btrfs_fs_info *fs_info,
                                         u64 start, u64 num_bytes);
 struct btrfs_open_stripe_run *btrfs_get_open_stripe_run(
index 69d33accd21ee34e2a687b9b38eb0db98acdd9bc..7186c62e8120cd17b160147a908f7ad2e368b360 100644 (file)
@@ -977,6 +977,15 @@ struct btrfs_fs_info {
                /* shared runs: frontier stripes sealed at log commit, bytes skipped */
                atomic64_t log_seals;
                atomic64_t log_seal_bytes;
+               /*
+                * ordered extents that ended with allocated sectors unwritten,
+                * other than exactly their truncated tail (these are logged)
+                */
+               atomic64_t unwritten_on_end;
+               atomic64_t unwritten_on_end_bytes;
+               /* ... whose unwritten sectors were exactly their truncated tail */
+               atomic64_t unwritten_on_end_truncated;
+               atomic64_t unwritten_on_end_truncated_bytes;
        } stripe_run_stats;
        /* BTRFS_STRIPE_RMW_* masks; see btrfs_stripe_allow_rmw(). */
        u32 stripe_rmw_prop;
index 38d1ee713d77ccd7e089caa4121dcb62c5c22e5d..e65b719d38e7e0625ac0c490a6d635d293175efc 100644 (file)
@@ -401,6 +401,34 @@ static void finish_ordered_fn(struct btrfs_work *work)
        btrfs_finish_ordered_io(ordered_extent);
 }
 
+/*
+ * Whether the @unwritten sectors from @first_unwritten up are exactly the
+ * tail that truncation cut off @ordered, [disk_bytenr + truncated_len,
+ * disk_bytenr + disk_num_bytes): truncated folios that were never submitted,
+ * or a direct write that ended short.  Only for an extent without a write
+ * error whose disk extent maps its file range one to one (not compressed).
+ */
+static bool ordered_unwritten_is_truncated_tail(
+                               const struct btrfs_ordered_extent *ordered,
+                               u32 unwritten, u64 first_unwritten)
+{
+       const struct btrfs_fs_info *fs_info = ordered->inode->root->fs_info;
+       u64 tail;
+
+       if (!test_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags) ||
+           test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) ||
+           test_bit(BTRFS_ORDERED_COMPRESSED, &ordered->flags))
+               return false;
+       if (ordered->disk_num_bytes != ordered->num_bytes ||
+           ordered->truncated_len > ordered->num_bytes)
+               return false;
+       /* The sector holding the last byte kept was written with it. */
+       tail = ALIGN(ordered->truncated_len, fs_info->sectorsize);
+       return first_unwritten == ordered->disk_bytenr + tail &&
+              unwritten == (ordered->disk_num_bytes - tail) >>
+                           fs_info->sectorsize_bits;
+}
+
 /*
  * Report this ordered extent's data IO to its open stripe run, exactly
  * once.  Called at IO completion, and as a catch-all when an ordered
@@ -410,12 +438,46 @@ static void finish_ordered_fn(struct btrfs_work *work)
 static void btrfs_ordered_stripe_write_done(struct btrfs_ordered_extent *ordered)
 {
        struct btrfs_open_stripe_run *run = ordered->stripe_run;
+       struct btrfs_fs_info *fs_info = ordered->inode->root->fs_info;
+       u64 first_unwritten;
+       u32 unwritten;
 
        if (!run)
                return;
        ordered->stripe_run = NULL;
-       btrfs_open_stripe_write_done_run(run, ordered->disk_bytenr,
-                                        ordered->disk_num_bytes);
+       unwritten = btrfs_open_stripe_write_done_run(run, ordered->disk_bytenr,
+                                                    ordered->disk_num_bytes,
+                                                    &first_unwritten);
+       if (likely(!unwritten))
+               return;
+       /*
+        * Sectors this extent allocated that no write reached: a failed write,
+        * truncated folios, a short direct write or error teardown.  They are
+        * now marked written so nothing waits for them.
+        *
+        * When they are exactly the truncated tail (a truncate or O_TRUNC
+        * racing writeback, or a direct write that ended short), nothing went
+        * wrong: count them apart and do not log them.  Every other case is
+        * counted in unwritten_on_end and logged, to find out which paths
+        * produce it in practice.
+        */
+       if (ordered_unwritten_is_truncated_tail(ordered, unwritten,
+                                               first_unwritten)) {
+               atomic64_inc(&fs_info->stripe_run_stats.unwritten_on_end_truncated);
+               atomic64_add((u64)unwritten << fs_info->sectorsize_bits,
+                            &fs_info->stripe_run_stats.unwritten_on_end_truncated_bytes);
+               return;
+       }
+       atomic64_inc(&fs_info->stripe_run_stats.unwritten_on_end);
+       atomic64_add((u64)unwritten << fs_info->sectorsize_bits,
+                    &fs_info->stripe_run_stats.unwritten_on_end_bytes);
+       btrfs_info_rl(fs_info,
+"stripe_alloc: ordered extent ended with %u unwritten sectors: root %llu ino %llu file_offset %llu len %llu disk_bytenr %llu disk_num_bytes %llu truncated_len %lld flags 0x%lx",
+                     unwritten, btrfs_root_id(ordered->inode->root),
+                     btrfs_ino(ordered->inode), ordered->file_offset,
+                     ordered->num_bytes, ordered->disk_bytenr,
+                     ordered->disk_num_bytes, (s64)ordered->truncated_len,
+                     ordered->flags);
 }
 
 static bool can_finish_ordered_extent(struct btrfs_ordered_extent *ordered,