]> Pileus Git - ~andy/linux/blobdiff - fs/ext4/extents_status.c
Linux 3.14
[~andy/linux] / fs / ext4 / extents_status.c
index 91cb110da1b4fe6fe9e334073ddae337507cd5d6..3981ff7839503df43a56ff282cd570d522bf764e 100644 (file)
@@ -13,7 +13,6 @@
 #include <linux/list_sort.h>
 #include "ext4.h"
 #include "extents_status.h"
-#include "ext4_extents.h"
 
 #include <trace/events/ext4.h>
 
@@ -263,7 +262,7 @@ void ext4_es_find_delayed_extent_range(struct inode *inode,
        if (tree->cache_es) {
                es1 = tree->cache_es;
                if (in_range(lblk, es1->es_lblk, es1->es_len)) {
-                       es_debug("%u cached by [%u/%u) %llu %llx\n",
+                       es_debug("%u cached by [%u/%u) %llu %x\n",
                                 lblk, es1->es_lblk, es1->es_len,
                                 ext4_es_pblock(es1), ext4_es_status(es1));
                        goto out;
@@ -409,6 +408,8 @@ ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
 }
 
 #ifdef ES_AGGRESSIVE_TEST
+#include "ext4_extents.h"      /* Needed when ES_AGGRESSIVE_TEST is defined */
+
 static void ext4_es_insert_extent_ext_check(struct inode *inode,
                                            struct extent_status *es)
 {
@@ -419,7 +420,7 @@ static void ext4_es_insert_extent_ext_check(struct inode *inode,
        unsigned short ee_len;
        int depth, ee_status, es_status;
 
-       path = ext4_ext_find_extent(inode, es->es_lblk, NULL);
+       path = ext4_ext_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
        if (IS_ERR(path))
                return;
 
@@ -641,13 +642,13 @@ out:
  */
 int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
                          ext4_lblk_t len, ext4_fsblk_t pblk,
-                         unsigned long long status)
+                         unsigned int status)
 {
        struct extent_status newes;
        ext4_lblk_t end = lblk + len - 1;
        int err = 0;
 
-       es_debug("add [%u/%u) %llu %llx to extent status tree of inode %lu\n",
+       es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
                 lblk, len, pblk, status, inode->i_ino);
 
        if (!len)
@@ -683,6 +684,38 @@ error:
        return err;
 }
 
+/*
+ * ext4_es_cache_extent() inserts information into the extent status
+ * tree if and only if there isn't information about the range in
+ * question already.
+ */
+void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
+                         ext4_lblk_t len, ext4_fsblk_t pblk,
+                         unsigned int status)
+{
+       struct extent_status *es;
+       struct extent_status newes;
+       ext4_lblk_t end = lblk + len - 1;
+
+       newes.es_lblk = lblk;
+       newes.es_len = len;
+       ext4_es_store_pblock(&newes, pblk);
+       ext4_es_store_status(&newes, status);
+       trace_ext4_es_cache_extent(inode, &newes);
+
+       if (!len)
+               return;
+
+       BUG_ON(end < lblk);
+
+       write_lock(&EXT4_I(inode)->i_es_lock);
+
+       es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
+       if (!es || es->es_lblk > end)
+               __es_insert_extent(inode, &newes);
+       write_unlock(&EXT4_I(inode)->i_es_lock);
+}
+
 /*
  * ext4_es_lookup_extent() looks up an extent in extent status tree.
  *
@@ -871,23 +904,6 @@ int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
        return err;
 }
 
-int ext4_es_zeroout(struct inode *inode, struct ext4_extent *ex)
-{
-       ext4_lblk_t  ee_block;
-       ext4_fsblk_t ee_pblock;
-       unsigned int ee_len;
-
-       ee_block  = le32_to_cpu(ex->ee_block);
-       ee_len    = ext4_ext_get_actual_len(ex);
-       ee_pblock = ext4_ext_pblock(ex);
-
-       if (ee_len == 0)
-               return 0;
-
-       return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
-                                    EXTENT_STATUS_WRITTEN);
-}
-
 static int ext4_inode_touch_time_cmp(void *priv, struct list_head *a,
                                     struct list_head *b)
 {
@@ -895,6 +911,12 @@ static int ext4_inode_touch_time_cmp(void *priv, struct list_head *a,
        eia = list_entry(a, struct ext4_inode_info, i_es_lru);
        eib = list_entry(b, struct ext4_inode_info, i_es_lru);
 
+       if (ext4_test_inode_state(&eia->vfs_inode, EXT4_STATE_EXT_PRECACHED) &&
+           !ext4_test_inode_state(&eib->vfs_inode, EXT4_STATE_EXT_PRECACHED))
+               return 1;
+       if (!ext4_test_inode_state(&eia->vfs_inode, EXT4_STATE_EXT_PRECACHED) &&
+           ext4_test_inode_state(&eib->vfs_inode, EXT4_STATE_EXT_PRECACHED))
+               return -1;
        if (eia->i_touch_when == eib->i_touch_when)
                return 0;
        if (time_after(eia->i_touch_when, eib->i_touch_when))
@@ -908,22 +930,16 @@ static int __ext4_es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
 {
        struct ext4_inode_info *ei;
        struct list_head *cur, *tmp;
-       LIST_HEAD(skiped);
-       int ret, nr_shrunk = 0;
+       LIST_HEAD(skipped);
+       int nr_shrunk = 0;
+       int retried = 0, skip_precached = 1, nr_skipped = 0;
 
        spin_lock(&sbi->s_es_lru_lock);
 
-       /*
-        * If the inode that is at the head of LRU list is newer than
-        * last_sorted time, that means that we need to sort this list.
-        */
-       ei = list_first_entry(&sbi->s_es_lru, struct ext4_inode_info, i_es_lru);
-       if (sbi->s_es_last_sorted < ei->i_touch_when) {
-               list_sort(NULL, &sbi->s_es_lru, ext4_inode_touch_time_cmp);
-               sbi->s_es_last_sorted = jiffies;
-       }
-
+retry:
        list_for_each_safe(cur, tmp, &sbi->s_es_lru) {
+               int shrunk;
+
                /*
                 * If we have already reclaimed all extents from extent
                 * status tree, just stop the loop immediately.
@@ -933,9 +949,16 @@ static int __ext4_es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
 
                ei = list_entry(cur, struct ext4_inode_info, i_es_lru);
 
-               /* Skip the inode that is newer than the last_sorted time */
-               if (sbi->s_es_last_sorted < ei->i_touch_when) {
-                       list_move_tail(cur, &skiped);
+               /*
+                * Skip the inode that is newer than the last_sorted
+                * time.  Normally we try hard to avoid shrinking
+                * precached inodes, but we will as a last resort.
+                */
+               if ((sbi->s_es_last_sorted < ei->i_touch_when) ||
+                   (skip_precached && ext4_test_inode_state(&ei->vfs_inode,
+                                               EXT4_STATE_EXT_PRECACHED))) {
+                       nr_skipped++;
+                       list_move_tail(cur, &skipped);
                        continue;
                }
 
@@ -943,28 +966,63 @@ static int __ext4_es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
                        continue;
 
                write_lock(&ei->i_es_lock);
-               ret = __es_try_to_reclaim_extents(ei, nr_to_scan);
+               shrunk = __es_try_to_reclaim_extents(ei, nr_to_scan);
                if (ei->i_es_lru_nr == 0)
                        list_del_init(&ei->i_es_lru);
                write_unlock(&ei->i_es_lock);
 
-               nr_shrunk += ret;
-               nr_to_scan -= ret;
+               nr_shrunk += shrunk;
+               nr_to_scan -= shrunk;
                if (nr_to_scan == 0)
                        break;
        }
 
        /* Move the newer inodes into the tail of the LRU list. */
-       list_splice_tail(&skiped, &sbi->s_es_lru);
+       list_splice_tail(&skipped, &sbi->s_es_lru);
+       INIT_LIST_HEAD(&skipped);
+
+       /*
+        * If we skipped any inodes, and we weren't able to make any
+        * forward progress, sort the list and try again.
+        */
+       if ((nr_shrunk == 0) && nr_skipped && !retried) {
+               retried++;
+               list_sort(NULL, &sbi->s_es_lru, ext4_inode_touch_time_cmp);
+               sbi->s_es_last_sorted = jiffies;
+               ei = list_first_entry(&sbi->s_es_lru, struct ext4_inode_info,
+                                     i_es_lru);
+               /*
+                * If there are no non-precached inodes left on the
+                * list, start releasing precached extents.
+                */
+               if (ext4_test_inode_state(&ei->vfs_inode,
+                                         EXT4_STATE_EXT_PRECACHED))
+                       skip_precached = 0;
+               goto retry;
+       }
+
        spin_unlock(&sbi->s_es_lru_lock);
 
        if (locked_ei && nr_shrunk == 0)
-               nr_shrunk = __es_try_to_reclaim_extents(ei, nr_to_scan);
+               nr_shrunk = __es_try_to_reclaim_extents(locked_ei, nr_to_scan);
 
        return nr_shrunk;
 }
 
-static int ext4_es_shrink(struct shrinker *shrink, struct shrink_control *sc)
+static unsigned long ext4_es_count(struct shrinker *shrink,
+                                  struct shrink_control *sc)
+{
+       unsigned long nr;
+       struct ext4_sb_info *sbi;
+
+       sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
+       nr = percpu_counter_read_positive(&sbi->s_extent_cache_cnt);
+       trace_ext4_es_shrink_enter(sbi->s_sb, sc->nr_to_scan, nr);
+       return nr;
+}
+
+static unsigned long ext4_es_scan(struct shrinker *shrink,
+                                 struct shrink_control *sc)
 {
        struct ext4_sb_info *sbi = container_of(shrink,
                                        struct ext4_sb_info, s_es_shrinker);
@@ -979,9 +1037,8 @@ static int ext4_es_shrink(struct shrinker *shrink, struct shrink_control *sc)
 
        nr_shrunk = __ext4_es_shrink(sbi, nr_to_scan, NULL);
 
-       ret = percpu_counter_read_positive(&sbi->s_extent_cache_cnt);
        trace_ext4_es_shrink_exit(sbi->s_sb, nr_shrunk, ret);
-       return ret;
+       return nr_shrunk;
 }
 
 void ext4_es_register_shrinker(struct ext4_sb_info *sbi)
@@ -989,7 +1046,8 @@ void ext4_es_register_shrinker(struct ext4_sb_info *sbi)
        INIT_LIST_HEAD(&sbi->s_es_lru);
        spin_lock_init(&sbi->s_es_lru_lock);
        sbi->s_es_last_sorted = 0;
-       sbi->s_es_shrinker.shrink = ext4_es_shrink;
+       sbi->s_es_shrinker.scan_objects = ext4_es_scan;
+       sbi->s_es_shrinker.count_objects = ext4_es_count;
        sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
        register_shrinker(&sbi->s_es_shrinker);
 }
@@ -1033,11 +1091,17 @@ static int __es_try_to_reclaim_extents(struct ext4_inode_info *ei,
        struct ext4_es_tree *tree = &ei->i_es_tree;
        struct rb_node *node;
        struct extent_status *es;
-       int nr_shrunk = 0;
+       unsigned long nr_shrunk = 0;
+       static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
+                                     DEFAULT_RATELIMIT_BURST);
 
        if (ei->i_es_lru_nr == 0)
                return 0;
 
+       if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
+           __ratelimit(&_rs))
+               ext4_warning(inode->i_sb, "forced shrink of precached extents");
+
        node = rb_first(&tree->root);
        while (node != NULL) {
                es = rb_entry(node, struct extent_status, rb_node);