]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: carry an inode's log tail forward at fsync
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:37 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:16:29 +0000 (21:16 -0500)
Closing a log-active inode's private stripe run at each log commit
traps the run's final partial stripe tail every time the inode fsyncs:
a steady fsync stream burns one stripe per log commit until the stripes
free or balance runs.

Reclaim the tails by carrying the partial stripe's live data forward.
At each log commit, the settling walk now computes the closed run's
final partial stripe, and the logging paths record the file ranges of
the logged extents that live inside it on a small per-inode table
(bounded; overflow just means an extent is not carried).  At the
inode's next fsync, before its delalloc flush, each recorded range that
still maps to the recorded disk bytenr is re-dirtied, defrag style
(reserve, reset delalloc state, mark the folios dirty; absent folios
are read back, which is a plain read of settled data).  The flush then
COWs the carried ranges together with the new data into the inode's
current private run, and the old partial stripe empties and frees
whole.

Everything downstream is the ordinary COW pipeline: new extent maps,
ordered extents, checksums, file extent items and delayed refs, and
the log's modified-extents snapshot -- taken inside btrfs_log_inode
after the flush -- picks up the new addresses by itself.  Reflinked
ranges need no special handling: foreign referents keep the old extent
alive in its closed (never again written) stripe, and only the space
reclaim degrades.  A range that was rewritten, truncated, punched or
compressed simply fails the mapping check and stays put.

Crash safety does not regress.  A carried extent's old copy is dropped
through the normal paths: if it was committed, the free pins until the
transaction commits; if it was logged but never committed, its ADD and
DROP delayed refs cancel in cleanup_ref_head(), which also pins
must_insert_reserved heads -- either way the old blocks cannot be
reallocated before the log that references them is superseded.

The settling walk also learned to report the partial stripe through a
widened btrfs_log_settle_stripes() signature (inode and file range
instead of fs_info); compressed extents pass a zero file length to opt
out of carrying while still settling.

The re-dirty reserves with NO_FLUSH.  carry_one_range() takes the folio
locks and the extent range lock before reserving, and a flushing
(ticketed) reservation can sleep in wait_reserve_ticket() until the
flusher makes progress -- but every way forward needs the locks we
hold: FLUSH_DELALLOC has to write back the carried range, which blocks
in __folio_lock() on our locked folio, and a transaction commit (with
flushoncommit) waits for the same writeback.  The whole filesystem then
wedges behind the stuck commit.  Carrying is best effort by design and
the carried range's data is already durable in its old stripe, so on
ENOSPC just skip the carry and let the old stripe free the slow way.

The carry's reservation does not flush at all, including the qgroup
reserve: btrfs_check_data_free_space_noflush() reserves data space and
qgroup space without flushing, since try_flush_qgroup() would write back
the folios the carry holds locked.  On EDQUOT, as on ENOSPC, the range is
just not carried.  The carry also clears a stale EXTENT_NORESERVE (left by
a NOCOW write) with the other delalloc bits, so the data space it reserved
is released when the range is later written NOCOW.

Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-opus-5-5
Assisted-by: Claude:claude-opus-5
12 files changed:
fs/btrfs/block-group.c
fs/btrfs/block-group.h
fs/btrfs/btrfs_inode.h
fs/btrfs/defrag.c
fs/btrfs/defrag.h
fs/btrfs/delalloc-space.c
fs/btrfs/delalloc-space.h
fs/btrfs/file.c
fs/btrfs/inode.c
fs/btrfs/qgroup.c
fs/btrfs/qgroup.h
fs/btrfs/tree-log.c

index 1eabdc2653b26088fafc17558375d134d7c0495d..e58d3e96bcc0a58034b06db4ecb92abdcee5cbdd 100644 (file)
@@ -543,6 +543,11 @@ static u64 stripe_round_down(u64 val, u64 fsl)
        return div64_u64(val, fsl) * fsl;
 }
 
+static bool stripe_is_aligned(u64 val, u64 fsl)
+{
+       return stripe_round_down(val, fsl) == val;
+}
+
 /*
  * Band for a run with @remaining bytes of unallocated space: the power-of-two
  * bucket of its remaining sectors (band k == remaining in [2^k, 2^(k+1))
@@ -1749,6 +1754,63 @@ static bool stripe_log_range_settled(struct btrfs_block_group *bg, u64 bytenr)
        return settled;
 }
 
+/*
+ * Remember, on the logging inode, a logged extent that lives in the final
+ * partial stripe of one of the inode's recently closed runs, so that the
+ * next fsync can re-dirty its file range and carry the data forward into
+ * the then-current run -- letting the old partial stripe free whole.  A
+ * zero @file_len means the caller does not want this extent carried
+ * (e.g. compressed extents, which would recompress).  @ps_len is nonzero
+ * when the settling walk could see the run and computed its final partial
+ * stripe.  Every table here is small and bounded; on overflow the extent
+ * simply is not carried and its stripe frees the slow way.  Caller holds
+ * the inode's log_mutex, which serializes against the fsync-entry consumer.
+ */
+static void log_carry_record(struct btrfs_inode *inode, u64 file_start,
+                            u64 file_len, u64 bytenr, u64 num_bytes,
+                            u64 ps_start, u64 ps_len)
+{
+       struct btrfs_log_carry *carry = inode->log_carry;
+       int n, i;
+
+       if (!carry) {
+               carry = kzalloc(sizeof(*carry), GFP_NOFS);
+               if (!carry)
+                       return;
+               inode->log_carry = carry;
+       }
+       if (ps_len) {
+               n = min_t(int, carry->nr_ps, BTRFS_LOG_CARRY_NR_PS);
+               for (i = 0; i < n; i++)
+                       if (carry->ps_start[i] == ps_start)
+                               break;
+               if (i == n) {
+                       int slot = carry->nr_ps % BTRFS_LOG_CARRY_NR_PS;
+
+                       carry->ps_start[slot] = ps_start;
+                       carry->ps_len[slot] = ps_len;
+                       carry->nr_ps++;
+               }
+       }
+       if (!file_len || carry->nr_ranges >= BTRFS_LOG_CARRY_NR_RANGES)
+               return;
+       n = min_t(int, carry->nr_ps, BTRFS_LOG_CARRY_NR_PS);
+       for (i = 0; i < n; i++)
+               if (bytenr < carry->ps_start[i] + carry->ps_len[i] &&
+                   bytenr + num_bytes > carry->ps_start[i])
+                       break;
+       if (i == n)
+               return;
+       for (i = 0; i < carry->nr_ranges; i++)
+               if (carry->range[i].start == file_start &&
+                   carry->range[i].len == file_len)
+                       return;
+       carry->range[carry->nr_ranges].start = file_start;
+       carry->range[carry->nr_ranges].len = file_len;
+       carry->range[carry->nr_ranges].disk_bytenr = bytenr;
+       carry->nr_ranges++;
+}
+
 /*
  * Disk end of @inode's last regular file extent, or 0: the re-adoption
  * candidate for a nocow allocation arriving with no allocation hint --
@@ -1880,10 +1942,15 @@ restart:
  * the wait is bio flight time plus the parked-write deadline that the flush
  * below short-circuits.  It is a pure data-IO wait (no transaction joins),
  * safe under the inode log mutex and a running transaction handle.
+ *
+ * [file_start, file_start + file_len) is the extent's file range, recorded
+ * for carry-forward when the extent sits in the run's final partial stripe;
+ * file_len == 0 opts the extent out of carrying (settling still happens).
  */
-void btrfs_log_settle_stripes(struct btrfs_fs_info *fs_info, u64 bytenr,
-                             u64 num_bytes)
+void btrfs_log_settle_stripes(struct btrfs_inode *inode, u64 file_start,
+                             u64 file_len, u64 bytenr, u64 num_bytes)
 {
+       struct btrfs_fs_info *fs_info = inode->root->fs_info;
        struct btrfs_block_group *bg;
        struct btrfs_open_stripe_run *run;
        unsigned long flags;
@@ -1891,6 +1958,8 @@ void btrfs_log_settle_stripes(struct btrfs_fs_info *fs_info, u64 bytenr,
        u64 tail_len = 0;
        u64 flush_start = 0;
        u64 flush_len = 0;
+       u64 ps_start = 0;
+       u64 ps_len = 0;
 
        /* stripe_alloc_allow_rmw=fsync waives the log-commit guarantee. */
        if (btrfs_stripe_allow_rmw(fs_info) & BTRFS_STRIPE_RMW_FSYNC)
@@ -1903,6 +1972,8 @@ void btrfs_log_settle_stripes(struct btrfs_fs_info *fs_info, u64 bytenr,
 
        spin_lock_irqsave(&bg->stripe_run_lock, flags);
        list_for_each_entry(run, &bg->open_stripe_runs, list) {
+               u64 filled;
+
                if (bytenr < run->start || bytenr >= run->offset)
                        continue;
                /*
@@ -1915,6 +1986,18 @@ void btrfs_log_settle_stripes(struct btrfs_fs_info *fs_info, u64 bytenr,
                        break;
                flush_start = run->start;
                flush_len = run->end - run->start;
+               /*
+                * The run's final partial stripe, whether this call closes
+                * the run or an earlier settle already did (offset froze at
+                * close).  Runs start stripe-aligned, so alignment relative
+                * to run->start is stripe alignment.
+                */
+               filled = run->offset - run->start;
+               if (!stripe_is_aligned(filled, bg->full_stripe_len)) {
+                       ps_start = run->start +
+                               stripe_round_down(filled, bg->full_stripe_len);
+                       ps_len = bg->full_stripe_len;
+               }
                if (run->open) {
                        int class;
                        int band;
@@ -1929,6 +2012,9 @@ void btrfs_log_settle_stripes(struct btrfs_fs_info *fs_info, u64 bytenr,
        }
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
 
+       log_carry_record(inode, file_start, file_len, bytenr, num_bytes,
+                        ps_start, ps_len);
+
        if (tail_len) {
                btrfs_add_free_space(bg, tail_start, tail_len);
                stripe_open_remainder_sync(bg);
index 32bbbad20a817e3fb503104abc9f08d1b96d3e01..d6b2c12c7230c09b6348221abb922ea97e7d4156 100644 (file)
@@ -544,8 +544,8 @@ bool btrfs_stripe_run_pad_mask(struct btrfs_fs_info *fs_info, u64 stripe_start,
 bool btrfs_stripe_nocow_writable(struct btrfs_inode *inode, u64 start,
                                 u64 num_bytes, u32 which, bool nowait);
 int btrfs_parse_stripe_rmw(const char *value, size_t len, u32 *mask);
-void btrfs_log_settle_stripes(struct btrfs_fs_info *fs_info, u64 bytenr,
-                             u64 num_bytes);
+void btrfs_log_settle_stripes(struct btrfs_inode *inode, u64 file_start,
+                             u64 file_len, u64 bytenr, u64 num_bytes);
 int btrfs_stripe_alloc_check_support(struct btrfs_fs_info *fs_info);
 int btrfs_enable_stripe_alloc(struct btrfs_fs_info *fs_info);
 void btrfs_disable_stripe_alloc(struct btrfs_fs_info *fs_info);
index e139777ecc0895b226794a95fecb2455895f857b..c00fd1ebd40464b9c5b056bc119623e73c59ab9c 100644 (file)
@@ -130,6 +130,31 @@ enum {
        BTRFS_INODE_ROOT_STUB,
 };
 
+/*
+ * Carry-forward bookkeeping for a log-active inode whose LOG runs are
+ * private (stripe_log_data_shared clear).  ps_start/ps_len remember the
+ * final partial stripes of the inode's recently closed private runs (a
+ * small ring; older entries are overwritten).  range[] records the file
+ * ranges of logged extents living in those partial stripes, for the next
+ * fsync to re-dirty; disk_bytenr lets the consumer verify the range still
+ * maps there before carrying.  Bounded loss: when the ring or the range
+ * table overflows, the extra extents simply stay put and their stripes free
+ * the slow way.
+ */
+#define BTRFS_LOG_CARRY_NR_PS 4
+#define BTRFS_LOG_CARRY_NR_RANGES 48
+struct btrfs_log_carry {
+       int nr_ps;
+       u64 ps_start[BTRFS_LOG_CARRY_NR_PS];
+       u64 ps_len[BTRFS_LOG_CARRY_NR_PS];
+       int nr_ranges;
+       struct {
+               u64 start;
+               u64 len;
+               u64 disk_bytenr;
+       } range[BTRFS_LOG_CARRY_NR_RANGES];
+};
+
 /* in memory btrfs inode */
 struct btrfs_inode {
        /* which subvolume this inode belongs to */
@@ -319,6 +344,12 @@ struct btrfs_inode {
                u64 ref_root_id;
        };
 
+       /*
+        * Carry-forward state (struct btrfs_log_carry), lazily allocated;
+        * recorded and consumed under log_mutex.
+        */
+       struct btrfs_log_carry *log_carry;
+
        /*
         * Start of the last extent allocated from one of this inode's private
         * stripe runs (LOG or NOCOW class).  Only a search hint, so
index cbc532b49c67d7eefea3439314096c6d379ee401..1e3bc1051b9d223b18f95b102c68986a102c9f4a 100644 (file)
@@ -1263,6 +1263,171 @@ free_folios:
        return ret;
 }
 
+/*
+ * Carry-forward for log-active inodes with private LOG runs (see struct
+ * btrfs_log_carry).  Re-dirty a file range recorded at the last log commit
+ * so the coming writeback COWs it into the inode's current private stripe
+ * run and the old partial stripe can free whole.  The range is carried only
+ * while it still fully maps to the recorded disk bytenr; a range that was
+ * rewritten, truncated, hole-punched or already carried fails that check
+ * and is skipped -- skipping only costs the space optimization, never
+ * correctness.  Modeled on defrag_one_range().
+ */
+static int carry_one_range(struct btrfs_inode *inode, u64 start, u64 len,
+                          u64 disk_bytenr)
+{
+       struct btrfs_fs_info *fs_info = inode->root->fs_info;
+       struct extent_changeset *data_reserved = NULL;
+       struct extent_state *cached_state = NULL;
+       struct extent_map *em;
+       struct folio **folios;
+       const unsigned int nr_pages = ((start + len - 1) >> PAGE_SHIFT) -
+                                     (start >> PAGE_SHIFT) + 1;
+       u64 cur;
+       int ret = 0;
+
+       ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
+              IS_ALIGNED(len, fs_info->sectorsize));
+
+       folios = kzalloc_objs(struct folio *, nr_pages, GFP_NOFS);
+       if (!folios)
+               return -ENOMEM;
+
+       cur = start;
+       for (int i = 0; cur < start + len && i < nr_pages; i++) {
+               folios[i] = defrag_prepare_one_folio(inode, cur >> PAGE_SHIFT);
+               if (IS_ERR(folios[i])) {
+                       ret = PTR_ERR(folios[i]);
+                       folios[i] = NULL;
+                       goto free_folios;
+               }
+               cur = folio_next_pos(folios[i]);
+       }
+       for (int i = 0; i < nr_pages && folios[i]; i++)
+               folio_wait_writeback(folios[i]);
+
+       ASSERT(folios[0]);
+       btrfs_lock_extent(&inode->io_tree, folio_pos(folios[0]), cur - 1,
+                         &cached_state);
+       /* Still fully mapped to the recorded location? */
+       em = btrfs_get_extent(inode, NULL, start, len);
+       if (IS_ERR(em)) {
+               ret = PTR_ERR(em);
+               goto unlock_extent;
+       }
+       if (em->disk_bytenr >= EXTENT_MAP_LAST_BYTE ||
+           (em->flags & EXTENT_FLAG_PREALLOC) ||
+           btrfs_extent_map_compression(em) != BTRFS_COMPRESS_NONE ||
+           em->start > start ||
+           em->start + em->len < start + len ||
+           btrfs_extent_map_block_start(em) + (start - em->start) !=
+               disk_bytenr) {
+               btrfs_free_extent_map(em);
+               goto unlock_extent;
+       }
+       btrfs_free_extent_map(em);
+
+       /*
+        * We hold the folio locks and the extent lock, so a flushing
+        * reservation can deadlock: satisfying a space ticket needs delalloc
+        * writeback -- or a transaction commit, which with flushoncommit waits
+        * for that writeback -- and a qgroup flush (try_flush_qgroup() on
+        * EDQUOT) writes back the root's delalloc and waits for its ordered
+        * extents, or waits for another task doing so.  Writeback of this
+        * range blocks in __folio_lock on the folios held here, and the
+        * folios are often dirty: an fsync'ed append dirties the folio holding
+        * the old tail.  Reserve space and qgroup space without flushing;
+        * carrying is optional, so on ENOSPC or EDQUOT the tail just stays in
+        * its old stripe (see btrfs_carry_log_tail).
+        */
+       ret = btrfs_check_data_free_space_noflush(inode, &data_reserved, start,
+                                                 len);
+       if (ret < 0)
+               goto unlock_extent;
+       ret = btrfs_delalloc_reserve_metadata(inode, len, len, true);
+       if (ret < 0) {
+               btrfs_free_reserved_data_space(inode, data_reserved, start,
+                                              len);
+               extent_changeset_free(data_reserved);
+               goto unlock_extent;
+       }
+       /*
+        * Part of the range may already be delalloc (an fsync'ed append
+        * dirties the old tail block).  As in btrfs_reset_extent_delalloc(),
+        * clearing it with EXTENT_DO_ACCOUNTING releases that delalloc's own
+        * reservations, and the ones made above cover the whole range.  Clear
+        * EXTENT_NORESERVE too: a NOCOW write that reserved no data space must
+        * not leave the flag on a range whose data space is now reserved, or
+        * that reservation is never released if the range is written NOCOW
+        * (the clear hook skips the release for EXTENT_NORESERVE, and checks
+        * the flag before clearing it, so the old state releases nothing).
+        */
+       btrfs_clear_extent_bit(&inode->io_tree, start, start + len - 1,
+                              EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
+                              EXTENT_DEFRAG | EXTENT_NORESERVE, &cached_state);
+       btrfs_set_extent_bit(&inode->io_tree, start, start + len - 1,
+                            EXTENT_DELALLOC, &cached_state);
+       for (int i = 0; i < nr_pages && folios[i]; i++) {
+               struct folio *folio = folios[i];
+
+               if (start >= folio_next_pos(folio) ||
+                   start + len <= folio_pos(folio))
+                       continue;
+               btrfs_folio_clamp_set_dirty(fs_info, folio, start, len);
+       }
+       btrfs_delalloc_release_extents(inode, len);
+       extent_changeset_free(data_reserved);
+unlock_extent:
+       btrfs_unlock_extent(&inode->io_tree, folio_pos(folios[0]), cur - 1,
+                           &cached_state);
+free_folios:
+       for (int i = 0; i < nr_pages; i++) {
+               if (!folios[i])
+                       break;
+               folio_unlock(folios[i]);
+               folio_put(folios[i]);
+       }
+       kfree(folios);
+       return ret;
+}
+
+/*
+ * Consume the inode's carry table (see struct btrfs_log_carry): re-dirty
+ * every recorded range so the caller's upcoming writeback carries them
+ * into the inode's current private stripe run.  Does nothing with shared
+ * LOG runs (stripe_log_data_shared).  Called at fsync entry with the inode
+ * locked; takes log_mutex only to detach the table, so the logging side
+ * never records into a half-consumed list.  All failures are soft: an
+ * uncarried range just leaves its old stripe to free the slow way.
+ */
+void btrfs_carry_log_tail(struct btrfs_inode *inode)
+{
+       struct btrfs_log_carry *copy;
+
+       if (!btrfs_test_opt(inode->root->fs_info, STRIPE_ALLOC))
+               return;
+       if (!READ_ONCE(inode->log_carry))
+               return;
+       copy = kmalloc(sizeof(*copy), GFP_NOFS);
+       if (!copy)
+               return;
+       mutex_lock(&inode->log_mutex);
+       if (!inode->log_carry || !inode->log_carry->nr_ranges) {
+               mutex_unlock(&inode->log_mutex);
+               kfree(copy);
+               return;
+       }
+       memcpy(copy, inode->log_carry, sizeof(*copy));
+       inode->log_carry->nr_ranges = 0;
+       mutex_unlock(&inode->log_mutex);
+
+       for (int i = 0; i < copy->nr_ranges; i++)
+               carry_one_range(inode, copy->range[i].start,
+                               copy->range[i].len,
+                               copy->range[i].disk_bytenr);
+       kfree(copy);
+}
+
 static int defrag_one_cluster(struct btrfs_inode *inode,
                              struct file_ra_state *ra,
                              u64 start, u32 len, u32 extent_thresh,
index a7f917a38dbfb75ed9d5452d95dbcbd1be3c0d9f..0b2a1241518931854d29664bba5ffc7e57f284a8 100644 (file)
@@ -16,6 +16,7 @@ struct btrfs_ioctl_defrag_range_args;
 int btrfs_defrag_file(struct btrfs_inode *inode, struct file_ra_state *ra,
                      struct btrfs_ioctl_defrag_range_args *range,
                      u64 newer_than, unsigned long max_to_defrag);
+void btrfs_carry_log_tail(struct btrfs_inode *inode);
 int __init btrfs_auto_defrag_init(void);
 void __cold btrfs_auto_defrag_exit(void);
 void btrfs_add_inode_defrag(struct btrfs_inode *inode, u32 extent_thresh);
index d09bf6aa4335284f7680afb235930bb7511ce8be..bfb11760b9eaee605e0a5b6561cf94e633ff21a9 100644 (file)
@@ -154,9 +154,10 @@ int btrfs_alloc_data_chunk_ondemand_held(const struct btrfs_inode *inode,
                                             flush, stripe_held);
 }
 
-int btrfs_check_data_free_space(struct btrfs_inode *inode,
-                               struct extent_changeset **reserved, u64 start,
-                               u64 len, bool noflush, u64 *stripe_held)
+static int check_data_free_space(struct btrfs_inode *inode,
+                                struct extent_changeset **reserved, u64 start,
+                                u64 len, bool noflush, bool qgroup_noflush,
+                                u64 *stripe_held)
 {
        struct btrfs_fs_info *fs_info = inode->root->fs_info;
        enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
@@ -201,7 +202,11 @@ int btrfs_check_data_free_space(struct btrfs_inode *inode,
                *stripe_held = stripe_margin;
 
        /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
-       ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
+       if (qgroup_noflush)
+               ret = btrfs_qgroup_reserve_data_noflush(inode, reserved, start,
+                                                       len);
+       else
+               ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
        if (ret < 0) {
                if (stripe_held) {
                        btrfs_release_data_margin_probe(inode, stripe_margin);
@@ -216,6 +221,27 @@ int btrfs_check_data_free_space(struct btrfs_inode *inode,
        return ret;
 }
 
+int btrfs_check_data_free_space(struct btrfs_inode *inode,
+                               struct extent_changeset **reserved, u64 start,
+                               u64 len, bool noflush, u64 *stripe_held)
+{
+       return check_data_free_space(inode, reserved, start, len, noflush,
+                                    false, stripe_held);
+}
+
+/*
+ * Reserve data space and qgroup space for [@start, @start + @len) without
+ * flushing either, for callers holding folio or extent locks of @inode:
+ * both flushes write back delalloc, the inode's own included.  (The noflush
+ * of btrfs_check_data_free_space() covers only the space reservation.)
+ */
+int btrfs_check_data_free_space_noflush(struct btrfs_inode *inode,
+                                       struct extent_changeset **reserved,
+                                       u64 start, u64 len)
+{
+       return check_data_free_space(inode, reserved, start, len, true, true,
+                                    NULL);
+}
 
 /* Release the probe margin btrfs_check_data_free_space() handed back in @stripe_held. */
 void btrfs_release_data_margin_probe(struct btrfs_inode *inode, u64 held)
@@ -223,6 +249,7 @@ void btrfs_release_data_margin_probe(struct btrfs_inode *inode, u64 held)
        if (held)
                btrfs_space_info_free_bytes_may_use(data_sinfo_for_inode(inode), held);
 }
+
 /*
  * Called if we need to clear a data reservation for this inode
  * Normally in a error case.
index 37860ced7a72372b36a85f50c21f9b8b38e35571..3c8dded8cdd43d26e646dafb5452802565a64e82 100644 (file)
@@ -15,6 +15,9 @@ int btrfs_alloc_data_chunk_ondemand_held(const struct btrfs_inode *inode,
 int btrfs_check_data_free_space(struct btrfs_inode *inode,
                        struct extent_changeset **reserved, u64 start, u64 len,
                        bool noflush, u64 *stripe_held);
+int btrfs_check_data_free_space_noflush(struct btrfs_inode *inode,
+                                       struct extent_changeset **reserved,
+                                       u64 start, u64 len);
 void btrfs_release_data_margin_probe(struct btrfs_inode *inode, u64 held);
 void btrfs_free_reserved_data_space(struct btrfs_inode *inode,
                        struct extent_changeset *reserved, u64 start, u64 len);
index d85ed2976ced382070ee17298cd86568b9829f77..04ccf839acfa3f78dc04586735af376845b97525 100644 (file)
@@ -35,6 +35,7 @@
 #include "extent-tree.h"
 #include "file-item.h"
 #include "ioctl.h"
+#include "defrag.h"
 #include "raid56.h"
 #include "file.h"
 #include "super.h"
@@ -1627,6 +1628,16 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
        else
                btrfs_inode_lock(inode, BTRFS_ILOCK_MMAP);
 
+       /*
+        * With private LOG runs (stripe_log_data_shared clear), re-dirty the
+        * tail ranges recorded at the last log commit so the writeback below
+        * carries them into the inode's current private stripe run, letting
+        * the old partial stripe free whole (see struct btrfs_log_carry).
+        * Purely a space optimization: a range that cannot be carried is
+        * simply skipped.
+        */
+       btrfs_carry_log_tail(inode);
+
        /*
         * Before we acquired the inode's lock and the mmap lock, someone may
         * have dirtied more pages in the target range. We need to make sure
index cc1dabf910591cea33c6342739eb4c1557643d7f..55f452b4aa4a300b82bd80ec0a344a2f9419cdb6 100644 (file)
@@ -8144,6 +8144,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
        ei->last_reflink_trans = 0;
        ei->last_log_commit = 0;
        ei->log_run_hint = 0;
+       ei->log_carry = NULL;
 
        spin_lock_init(&ei->lock);
        ei->outstanding_extents = 0;
@@ -8183,6 +8184,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 void btrfs_test_destroy_inode(struct inode *inode)
 {
        btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
+       kfree(BTRFS_I(inode)->log_carry);
        kfree(BTRFS_I(inode)->file_extent_tree);
        kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
 }
@@ -8190,6 +8192,7 @@ void btrfs_test_destroy_inode(struct inode *inode)
 
 void btrfs_free_inode(struct inode *inode)
 {
+       kfree(BTRFS_I(inode)->log_carry);
        kfree(BTRFS_I(inode)->file_extent_tree);
        kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
 }
index baef546fa213e3f9745cedd729939865bc1976ea..e7dbf9666c2b9a5c83f0b6438a99a2798636b7cc 100644 (file)
@@ -4372,6 +4372,18 @@ int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
        return qgroup_reserve_data(inode, reserved_ret, start, len);
 }
 
+/*
+ * Like btrfs_qgroup_reserve_data(), but return -EDQUOT instead of flushing,
+ * for callers holding folio or extent locks of @inode: the flush writes back
+ * delalloc (the inode's own included) and waits for ordered extents.
+ */
+int btrfs_qgroup_reserve_data_noflush(struct btrfs_inode *inode,
+                                     struct extent_changeset **reserved_ret,
+                                     u64 start, u64 len)
+{
+       return qgroup_reserve_data(inode, reserved_ret, start, len);
+}
+
 /* Free ranges specified by @reserved, normally in error path */
 static int qgroup_free_reserved_data(struct btrfs_inode *inode,
                                     struct extent_changeset *reserved,
index a29b4e216f25d47f7c144e02763734d4316345de..b37de68f87dc0c597bf3ea4e252739c8a3e63f83 100644 (file)
@@ -400,6 +400,9 @@ int btrfs_verify_qgroup_counts(const struct btrfs_fs_info *fs_info, u64 qgroupid
 /* New io_tree based accurate qgroup reserve API */
 int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
                        struct extent_changeset **reserved, u64 start, u64 len);
+int btrfs_qgroup_reserve_data_noflush(struct btrfs_inode *inode,
+                                     struct extent_changeset **reserved,
+                                     u64 start, u64 len);
 int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len, u64 *released);
 int btrfs_qgroup_free_data(struct btrfs_inode *inode,
                           struct extent_changeset *reserved, u64 start,
index 8992a40bbd28661acb546c4b6de990a6cb838659..ee00af739267044ced6de72be0f8fd68d34a6dcc 100644 (file)
@@ -4903,9 +4903,12 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
                        disk_num_bytes =
                                btrfs_file_extent_disk_num_bytes(src, extent);
                        if (disk_bytenr)
-                               btrfs_log_settle_stripes(trans->fs_info,
-                                                        disk_bytenr,
-                                                        disk_num_bytes);
+                               btrfs_log_settle_stripes(inode,
+                                       ins_keys[dst_index].offset,
+                                       btrfs_file_extent_compression(src, extent) ?
+                                               0 :
+                                               btrfs_file_extent_num_bytes(src, extent),
+                                       disk_bytenr, disk_num_bytes);
                }
 
                if (skip_csum)
@@ -5205,8 +5208,10 @@ static int log_one_extent(struct btrfs_trans_handle *trans,
         */
        if (!(em->flags & EXTENT_FLAG_PREALLOC) &&
            em->disk_bytenr < EXTENT_MAP_LAST_BYTE)
-               btrfs_log_settle_stripes(inode->root->fs_info, em->disk_bytenr,
-                                        em->disk_num_bytes);
+               btrfs_log_settle_stripes(inode, em->start,
+                                        compress_type == BTRFS_COMPRESS_NONE ?
+                                               em->len : 0,
+                                        em->disk_bytenr, em->disk_num_bytes);
 
        if (compress_type != BTRFS_COMPRESS_NONE) {
                btrfs_set_stack_file_extent_disk_bytenr(&fi, block_start);