]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/journal_reclaim.c
Update bcachefs sources to bdf6d7c135 fixup! bcachefs: Kill journal buf bloom filter
[bcachefs-tools-debian] / libbcachefs / journal_reclaim.c
index 7a0ae5d3431c0aa29e7e075e6628b64365a4916f..fdc94e831a86d63b6d0c07fcc3b0eee8e72fe288 100644 (file)
@@ -59,25 +59,13 @@ static void journal_set_remaining(struct journal *j, unsigned u64s_remaining)
                                       old.v, new.v)) != old.v);
 }
 
-static inline unsigned get_unwritten_sectors(struct journal *j, unsigned *idx)
-{
-       unsigned sectors = 0;
-
-       while (!sectors && *idx != j->reservations.idx) {
-               sectors = j->buf[*idx].sectors;
-
-               *idx = (*idx + 1) & JOURNAL_BUF_MASK;
-       }
-
-       return sectors;
-}
-
 static struct journal_space
 journal_dev_space_available(struct journal *j, struct bch_dev *ca,
                            enum journal_space_from from)
 {
        struct journal_device *ja = &ca->journal;
-       unsigned sectors, buckets, unwritten, idx = j->reservations.unwritten_idx;
+       unsigned sectors, buckets, unwritten;
+       u64 seq;
 
        if (from == journal_space_total)
                return (struct journal_space) {
@@ -92,7 +80,14 @@ journal_dev_space_available(struct journal *j, struct bch_dev *ca,
         * We that we don't allocate the space for a journal entry
         * until we write it out - thus, account for it here:
         */
-       while ((unwritten = get_unwritten_sectors(j, &idx))) {
+       for (seq = journal_last_unwritten_seq(j);
+            seq <= journal_cur_seq(j);
+            seq++) {
+               unwritten = j->buf[seq & JOURNAL_BUF_MASK].sectors;
+
+               if (!unwritten)
+                       continue;
+
                /* entry won't fit on this device, skip: */
                if (unwritten > ca->mi.bucket_size)
                        continue;
@@ -200,7 +195,7 @@ void bch2_journal_space_available(struct journal *j)
        j->can_discard = can_discard;
 
        if (nr_online < c->opts.metadata_replicas_required) {
-               ret = cur_entry_insufficient_devices;
+               ret = JOURNAL_ERR_insufficient_devices;
                goto out;
        }
 
@@ -214,23 +209,24 @@ void bch2_journal_space_available(struct journal *j)
        total           = j->space[journal_space_total].total;
 
        if (!clean_ondisk &&
-           j->reservations.idx ==
-           j->reservations.unwritten_idx) {
-               char *buf = kmalloc(4096, GFP_ATOMIC);
-
-               bch_err(c, "journal stuck");
-               if (buf) {
-                       __bch2_journal_debug_to_text(&_PBUF(buf, 4096), j);
-                       pr_err("\n%s", buf);
-                       kfree(buf);
-               }
+           journal_cur_seq(j) == j->seq_ondisk) {
+               struct printbuf buf = PRINTBUF;
+
+               __bch2_journal_debug_to_text(&buf, j);
+               bch_err(c, "journal stuck\n%s", buf.buf);
+               printbuf_exit(&buf);
 
+               /*
+                * Hack: bch2_fatal_error() calls bch2_journal_halt() which
+                * takes journal lock:
+                */
+               spin_unlock(&j->lock);
                bch2_fatal_error(c);
-               ret = cur_entry_journal_stuck;
+               spin_lock(&j->lock);
+
+               ret = JOURNAL_ERR_journal_stuck;
        } else if (!j->space[journal_space_discarded].next_entry)
-               ret = cur_entry_journal_full;
-       else if (!fifo_free(&j->pin))
-               ret = cur_entry_journal_pin_full;
+               ret = JOURNAL_ERR_journal_full;
 
        if ((j->space[journal_space_clean_ondisk].next_entry <
             j->space[journal_space_clean_ondisk].total) &&
@@ -249,7 +245,7 @@ out:
        j->cur_entry_sectors    = !ret ? j->space[journal_space_discarded].next_entry : 0;
        j->cur_entry_error      = ret;
        journal_set_remaining(j, u64s_remaining);
-       journal_check_may_get_unreserved(j);
+       journal_set_watermark(j);
 
        if (!ret)
                journal_wake(j);
@@ -284,7 +280,8 @@ void bch2_journal_do_discards(struct journal *j)
                struct journal_device *ja = &ca->journal;
 
                while (should_discard_bucket(j, ja)) {
-                       if (ca->mi.discard &&
+                       if (!c->opts.nochanges &&
+                           ca->mi.discard &&
                            blk_queue_discard(bdev_get_queue(ca->disk_sb.bdev)))
                                blkdev_issue_discard(ca->disk_sb.bdev,
                                        bucket_to_sector(ca,
@@ -371,9 +368,6 @@ static inline void __journal_pin_drop(struct journal *j,
        if (atomic_dec_and_test(&pin_list->count) &&
            pin_list == &fifo_peek_front(&j->pin))
                bch2_journal_reclaim_fast(j);
-       else if (fifo_used(&j->pin) == 1 &&
-                atomic_read(&pin_list->count) == 1)
-               journal_wake(j);
 }
 
 void bch2_journal_pin_drop(struct journal *j,
@@ -487,9 +481,6 @@ static size_t journal_flush_pins(struct journal *j, u64 seq_to_flush,
        u64 seq;
        int err;
 
-       if (!test_bit(JOURNAL_RECLAIM_STARTED, &j->flags))
-               return 0;
-
        lockdep_assert_held(&j->reclaim_lock);
 
        while (1) {
@@ -598,7 +589,7 @@ static u64 journal_seq_to_flush(struct journal *j)
  * 512 journal entries or 25% of all journal buckets, then
  * journal_next_bucket() should not stall.
  */
-static int __bch2_journal_reclaim(struct journal *j, bool direct)
+static int __bch2_journal_reclaim(struct journal *j, bool direct, bool kicked)
 {
        struct bch_fs *c = container_of(j, struct bch_fs, journal);
        bool kthread = (current->flags & PF_KTHREAD) != 0;
@@ -635,7 +626,7 @@ static int __bch2_journal_reclaim(struct journal *j, bool direct)
                 * make sure to flush at least one journal pin:
                 */
                if (time_after(jiffies, j->last_flushed +
-                              msecs_to_jiffies(j->reclaim_delay_ms)))
+                              msecs_to_jiffies(c->opts.journal_reclaim_delay)))
                        min_nr = 1;
 
                if (j->prereserved.reserved * 4 > j->prereserved.remaining)
@@ -644,8 +635,13 @@ static int __bch2_journal_reclaim(struct journal *j, bool direct)
                if (fifo_free(&j->pin) <= 32)
                        min_nr = 1;
 
-               trace_journal_reclaim_start(c,
-                               min_nr,
+               if (atomic_read(&c->btree_cache.dirty) * 2 > c->btree_cache.used)
+                       min_nr = 1;
+
+               min_key_cache = min(bch2_nr_btree_keys_need_flush(c), (size_t) 128);
+
+               trace_journal_reclaim_start(c, direct, kicked,
+                               min_nr, min_key_cache,
                                j->prereserved.reserved,
                                j->prereserved.remaining,
                                atomic_read(&c->btree_cache.dirty),
@@ -653,8 +649,6 @@ static int __bch2_journal_reclaim(struct journal *j, bool direct)
                                atomic_long_read(&c->btree_key_cache.nr_dirty),
                                atomic_long_read(&c->btree_key_cache.nr_keys));
 
-               min_key_cache = min(bch2_nr_btree_keys_need_flush(c), 128UL);
-
                nr_flushed = journal_flush_pins(j, seq_to_flush,
                                                min_nr, min_key_cache);
 
@@ -666,7 +660,7 @@ static int __bch2_journal_reclaim(struct journal *j, bool direct)
 
                if (nr_flushed)
                        wake_up(&j->reclaim_wait);
-       } while ((min_nr || min_key_cache) && !direct);
+       } while ((min_nr || min_key_cache) && nr_flushed && !direct);
 
        memalloc_noreclaim_restore(flags);
 
@@ -675,30 +669,32 @@ static int __bch2_journal_reclaim(struct journal *j, bool direct)
 
 int bch2_journal_reclaim(struct journal *j)
 {
-       return __bch2_journal_reclaim(j, true);
+       return __bch2_journal_reclaim(j, true, true);
 }
 
 static int bch2_journal_reclaim_thread(void *arg)
 {
        struct journal *j = arg;
+       struct bch_fs *c = container_of(j, struct bch_fs, journal);
        unsigned long delay, now;
+       bool journal_empty;
        int ret = 0;
 
        set_freezable();
 
-       kthread_wait_freezable(test_bit(JOURNAL_RECLAIM_STARTED, &j->flags));
-
        j->last_flushed = jiffies;
 
        while (!ret && !kthread_should_stop()) {
+               bool kicked = j->reclaim_kicked;
+
                j->reclaim_kicked = false;
 
                mutex_lock(&j->reclaim_lock);
-               ret = __bch2_journal_reclaim(j, false);
+               ret = __bch2_journal_reclaim(j, false, kicked);
                mutex_unlock(&j->reclaim_lock);
 
                now = jiffies;
-               delay = msecs_to_jiffies(j->reclaim_delay_ms);
+               delay = msecs_to_jiffies(c->opts.journal_reclaim_delay);
                j->next_reclaim = j->last_flushed + delay;
 
                if (!time_in_range(j->next_reclaim, now, now + delay))
@@ -710,10 +706,17 @@ static int bch2_journal_reclaim_thread(void *arg)
                                break;
                        if (j->reclaim_kicked)
                                break;
-                       if (time_after_eq(jiffies, j->next_reclaim))
-                               break;
-                       freezable_schedule_timeout(j->next_reclaim - jiffies);
 
+                       spin_lock(&j->lock);
+                       journal_empty = fifo_empty(&j->pin);
+                       spin_unlock(&j->lock);
+
+                       if (journal_empty)
+                               freezable_schedule();
+                       else if (time_after(j->next_reclaim, jiffies))
+                               freezable_schedule_timeout(j->next_reclaim - jiffies);
+                       else
+                               break;
                }
                __set_current_state(TASK_RUNNING);
        }
@@ -765,7 +768,8 @@ static int journal_flush_done(struct journal *j, u64 seq_to_flush,
 
        mutex_lock(&j->reclaim_lock);
 
-       *did_work = journal_flush_pins(j, seq_to_flush, 0, 0) != 0;
+       if (journal_flush_pins(j, seq_to_flush, 0, 0))
+               *did_work = true;
 
        spin_lock(&j->lock);
        /*
@@ -774,8 +778,7 @@ static int journal_flush_done(struct journal *j, u64 seq_to_flush,
         */
        ret = !test_bit(JOURNAL_REPLAY_DONE, &j->flags) ||
                journal_last_seq(j) > seq_to_flush ||
-               (fifo_used(&j->pin) == 1 &&
-                atomic_read(&fifo_peek_front(&j->pin).count) == 1);
+               !fifo_used(&j->pin);
 
        spin_unlock(&j->lock);
        mutex_unlock(&j->reclaim_lock);
@@ -823,10 +826,12 @@ int bch2_journal_flush_device_pins(struct journal *j, int dev_idx)
        seq = 0;
 
        spin_lock(&j->lock);
-       while (!ret && seq < j->pin.back) {
+       while (!ret) {
                struct bch_replicas_padded replicas;
 
                seq = max(seq, journal_last_seq(j));
+               if (seq >= j->pin.back)
+                       break;
                bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
                                         journal_seq_pin(j, seq)->devs);
                seq++;