return ret;
 }
 
-static bool device_path_matched(const char *path, struct btrfs_device *device)
+/*
+ * Check if the device in the path matches the device in the given struct device.
+ *
+ * Returns:
+ *   true  If it is the same device.
+ *   false If it is not the same device or on error.
+ */
+static bool device_matched(const struct btrfs_device *device, const char *path)
 {
-       int found;
+       char *device_name;
+       dev_t dev_old;
+       dev_t dev_new;
+       int ret;
+
+       /*
+        * If we are looking for a device with the matching dev_t, then skip
+        * device without a name (a missing device).
+        */
+       if (!device->name)
+               return false;
+
+       device_name = kzalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
+       if (!device_name)
+               return false;
 
        rcu_read_lock();
-       found = strcmp(rcu_str_deref(device->name), path);
+       scnprintf(device_name, BTRFS_PATH_NAME_MAX, "%s", rcu_str_deref(device->name));
        rcu_read_unlock();
 
-       return found == 0;
+       ret = lookup_bdev(device_name, &dev_old);
+       kfree(device_name);
+       if (ret)
+               return false;
+
+       ret = lookup_bdev(path, &dev_new);
+       if (ret)
+               return false;
+
+       if (dev_old == dev_new)
+               return true;
+
+       return false;
 }
 
 /*
                                         &fs_devices->devices, dev_list) {
                        if (skip_device && skip_device == device)
                                continue;
-                       if (path && !device->name)
-                               continue;
-                       if (path && !device_path_matched(path, device))
+                       if (path && !device_matched(device, path))
                                continue;
                        if (fs_devices->opened) {
                                /* for an already deleted device return 0 */