Merge tag '5.11-rc-smb3-part2' of git://git.samba.org/sfrench/cifs-2.6
[linux-2.6-microblaze.git] / fs / btrfs / discard.c
index 741c7e1..1db966b 100644 (file)
@@ -355,7 +355,7 @@ void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl,
 
        block_group = find_next_block_group(discard_ctl, now);
        if (block_group) {
-               unsigned long delay = discard_ctl->delay;
+               u64 delay = discard_ctl->delay_ms * NSEC_PER_MSEC;
                u32 kbps_limit = READ_ONCE(discard_ctl->kbps_limit);
 
                /*
@@ -366,9 +366,9 @@ void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl,
                if (kbps_limit && discard_ctl->prev_discard) {
                        u64 bps_limit = ((u64)kbps_limit) * SZ_1K;
                        u64 bps_delay = div64_u64(discard_ctl->prev_discard *
-                                                 MSEC_PER_SEC, bps_limit);
+                                                 NSEC_PER_SEC, bps_limit);
 
-                       delay = max(delay, msecs_to_jiffies(bps_delay));
+                       delay = max(delay, bps_delay);
                }
 
                /*
@@ -378,11 +378,20 @@ void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl,
                if (now < block_group->discard_eligible_time) {
                        u64 bg_timeout = block_group->discard_eligible_time - now;
 
-                       delay = max(delay, nsecs_to_jiffies(bg_timeout));
+                       delay = max(delay, bg_timeout);
+               }
+
+               if (override && discard_ctl->prev_discard) {
+                       u64 elapsed = now - discard_ctl->prev_discard_time;
+
+                       if (delay > elapsed)
+                               delay -= elapsed;
+                       else
+                               delay = 0;
                }
 
                mod_delayed_work(discard_ctl->discard_workers,
-                                &discard_ctl->work, delay);
+                                &discard_ctl->work, nsecs_to_jiffies(delay));
        }
 out:
        spin_unlock(&discard_ctl->lock);
@@ -465,7 +474,12 @@ static void btrfs_discard_workfn(struct work_struct *work)
                discard_ctl->discard_extent_bytes += trimmed;
        }
 
+       /*
+        * Updated without locks as this is inside the workfn and nothing else
+        * is reading the values
+        */
        discard_ctl->prev_discard = trimmed;
+       discard_ctl->prev_discard_time = ktime_get_ns();
 
        /* Determine next steps for a block_group */
        if (block_group->discard_cursor >= btrfs_block_group_end(block_group)) {
@@ -519,7 +533,6 @@ void btrfs_discard_calc_delay(struct btrfs_discard_ctl *discard_ctl)
        s64 discardable_bytes;
        u32 iops_limit;
        unsigned long delay;
-       unsigned long lower_limit = BTRFS_DISCARD_MIN_DELAY_MSEC;
 
        discardable_extents = atomic_read(&discard_ctl->discardable_extents);
        if (!discardable_extents)
@@ -550,12 +563,13 @@ void btrfs_discard_calc_delay(struct btrfs_discard_ctl *discard_ctl)
 
        iops_limit = READ_ONCE(discard_ctl->iops_limit);
        if (iops_limit)
-               lower_limit = max_t(unsigned long, lower_limit,
-                                   MSEC_PER_SEC / iops_limit);
+               delay = MSEC_PER_SEC / iops_limit;
+       else
+               delay = BTRFS_DISCARD_TARGET_MSEC / discardable_extents;
 
-       delay = BTRFS_DISCARD_TARGET_MSEC / discardable_extents;
-       delay = clamp(delay, lower_limit, BTRFS_DISCARD_MAX_DELAY_MSEC);
-       discard_ctl->delay = msecs_to_jiffies(delay);
+       delay = clamp(delay, BTRFS_DISCARD_MIN_DELAY_MSEC,
+                     BTRFS_DISCARD_MAX_DELAY_MSEC);
+       discard_ctl->delay_ms = delay;
 
        spin_unlock(&discard_ctl->lock);
 }
@@ -563,15 +577,14 @@ void btrfs_discard_calc_delay(struct btrfs_discard_ctl *discard_ctl)
 /**
  * btrfs_discard_update_discardable - propagate discard counters
  * @block_group: block_group of interest
- * @ctl: free_space_ctl of @block_group
  *
  * This propagates deltas of counters up to the discard_ctl.  It maintains a
  * current counter and a previous counter passing the delta up to the global
  * stat.  Then the current counter value becomes the previous counter value.
  */
-void btrfs_discard_update_discardable(struct btrfs_block_group *block_group,
-                                     struct btrfs_free_space_ctl *ctl)
+void btrfs_discard_update_discardable(struct btrfs_block_group *block_group)
 {
+       struct btrfs_free_space_ctl *ctl;
        struct btrfs_discard_ctl *discard_ctl;
        s32 extents_delta;
        s64 bytes_delta;
@@ -581,8 +594,10 @@ void btrfs_discard_update_discardable(struct btrfs_block_group *block_group,
            !btrfs_is_block_group_data_only(block_group))
                return;
 
+       ctl = block_group->free_space_ctl;
        discard_ctl = &block_group->fs_info->discard_ctl;
 
+       lockdep_assert_held(&ctl->tree_lock);
        extents_delta = ctl->discardable_extents[BTRFS_STAT_CURR] -
                        ctl->discardable_extents[BTRFS_STAT_PREV];
        if (extents_delta) {
@@ -684,10 +699,11 @@ void btrfs_discard_init(struct btrfs_fs_info *fs_info)
                INIT_LIST_HEAD(&discard_ctl->discard_list[i]);
 
        discard_ctl->prev_discard = 0;
+       discard_ctl->prev_discard_time = 0;
        atomic_set(&discard_ctl->discardable_extents, 0);
        atomic64_set(&discard_ctl->discardable_bytes, 0);
        discard_ctl->max_discard_size = BTRFS_ASYNC_DISCARD_DEFAULT_MAX_SIZE;
-       discard_ctl->delay = BTRFS_DISCARD_MAX_DELAY_MSEC;
+       discard_ctl->delay_ms = BTRFS_DISCARD_MAX_DELAY_MSEC;
        discard_ctl->iops_limit = BTRFS_DISCARD_MAX_IOPS;
        discard_ctl->kbps_limit = 0;
        discard_ctl->discard_extent_bytes = 0;