With discard flag in btrfs_device, we will only push trim request to the
devices support that.
Now we don't return EOPNOTSUPP to the caller, so we won't trigger BUG_ONs
in the walk_log_tree functions if we mount a drive without DISCARD
using -o discard, but it is still possible if we get errors from
blkdev_issue_discard.
This won't affect the return value of fstrim on the drives without DISCARD,
because we've already checked that in btrfs_ioctl_fitrim, Thanks

Changes v2:
  better name for the discard flag

Signed-off-by: Li Dongyang <lidongy...@novell.com>
---
 fs/btrfs/extent-tree.c |   24 ++++++++++++------------
 fs/btrfs/volumes.c     |    4 ++++
 fs/btrfs/volumes.h     |    1 +
 3 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 1f61bf5..81ccae2 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -1774,7 +1774,6 @@ static int btrfs_discard_extent(struct btrfs_root *root, 
u64 bytenr,
        u64 discarded_bytes = 0;
        struct btrfs_multi_bio *multi = NULL;
 
-
        /* Tell the block device(s) that the sectors can be discarded */
        ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
                              bytenr, &num_bytes, &multi, 0);
@@ -1782,25 +1781,26 @@ static int btrfs_discard_extent(struct btrfs_root 
*root, u64 bytenr,
                struct btrfs_bio_stripe *stripe = multi->stripes;
                int i;
 
-
                for (i = 0; i < multi->num_stripes; i++, stripe++) {
-                       ret = btrfs_issue_discard(stripe->dev->bdev,
-                                                 stripe->physical,
-                                                 stripe->length);
-                       if (!ret)
-                               discarded_bytes += stripe->length;
-                       else if (ret != -EOPNOTSUPP)
-                               break;
+                       if (stripe->dev->has_trim) {
+                               ret = btrfs_issue_discard(stripe->dev->bdev,
+                                                         stripe->physical,
+                                                         stripe->length);
+                               if (!ret)
+                                       discarded_bytes += stripe->length;
+                               else if (ret == -EOPNOTSUPP) {
+                                       stripe->dev->has_trim = 0;
+                                       ret = 0;
+                               } else
+                                       break;
+                       }
                }
                kfree(multi);
        }
-       if (discarded_bytes && ret == -EOPNOTSUPP)
-               ret = 0;
 
        if (actual_bytes)
                *actual_bytes = discarded_bytes;
 
-
        return ret;
 }
 
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 1efa56e..3d86b35 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -341,6 +341,7 @@ static noinline int device_list_add(const char *path,
                device->work.func = pending_bios_fn;
                memcpy(device->uuid, disk_super->dev_item.uuid,
                       BTRFS_UUID_SIZE);
+               device->has_trim = 1;
                spin_lock_init(&device->io_lock);
                device->name = kstrdup(path, GFP_NOFS);
                if (!device->name) {
@@ -408,6 +409,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct 
btrfs_fs_devices *orig)
                device->devid = orig_dev->devid;
                device->work.func = pending_bios_fn;
                memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
+               device->has_trim = 1;
                spin_lock_init(&device->io_lock);
                INIT_LIST_HEAD(&device->dev_list);
                INIT_LIST_HEAD(&device->dev_alloc_list);
@@ -1612,6 +1614,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char 
*device_path)
        lock_chunks(root);
 
        device->writeable = 1;
+       device->has_trim = 1;
        device->work.func = pending_bios_fn;
        generate_random_uuid(device->uuid);
        spin_lock_init(&device->io_lock);
@@ -3342,6 +3345,7 @@ static struct btrfs_device *add_missing_dev(struct 
btrfs_root *root,
                return NULL;
        list_add(&device->dev_list,
                 &fs_devices->devices);
+       device->has_trim = 1;
        device->dev_root = root->fs_info->dev_root;
        device->devid = devid;
        device->work.func = pending_bios_fn;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 7c12d61..376d71c 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -45,6 +45,7 @@ struct btrfs_device {
        int running_pending;
        u64 generation;
 
+       int has_trim;
        int writeable;
        int in_fs_metadata;
        int missing;
-- 
1.7.5.4

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to