* 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;
}
/*
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
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,