]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/journal_reclaim.c
Update bcachefs sources to 259ff91605 bcachefs: Don't keep around btree_paths unneces...
[bcachefs-tools-debian] / libbcachefs / journal_reclaim.c
index c468d597d4276c414803ee3891407fd45e941484..ec565edbbfc566d4f411c37b263363d8b982a5f2 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;
@@ -214,23 +209,17 @@ 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);
 
                bch2_fatal_error(c);
                ret = cur_entry_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;
 
        if ((j->space[journal_space_clean_ondisk].next_entry <
             j->space[journal_space_clean_ondisk].total) &&
@@ -284,7 +273,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 +361,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 +474,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) {
@@ -635,7 +619,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,6 +628,9 @@ static int __bch2_journal_reclaim(struct journal *j, bool direct)
                if (fifo_free(&j->pin) <= 32)
                        min_nr = 1;
 
+               if (atomic_read(&c->btree_cache.dirty) * 2 > c->btree_cache.used)
+                       min_nr = 1;
+
                trace_journal_reclaim_start(c,
                                min_nr,
                                j->prereserved.reserved,
@@ -666,7 +653,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);
 
@@ -681,13 +668,12 @@ int bch2_journal_reclaim(struct journal *j)
 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;
        int ret = 0;
 
        set_freezable();
 
-       kthread_wait_freezable(test_bit(JOURNAL_RECLAIM_STARTED, &j->flags));
-
        j->last_flushed = jiffies;
 
        while (!ret && !kthread_should_stop()) {
@@ -698,7 +684,7 @@ static int bch2_journal_reclaim_thread(void *arg)
                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))
@@ -765,7 +751,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 +761,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);