device = NULL;
        } else {
+               struct btrfs_dev_lookup_args args = {
+                       .devid = devid,
+                       .uuid = disk_super->dev_item.uuid,
+               };
+
                mutex_lock(&fs_devices->device_list_mutex);
-               device = btrfs_find_device(fs_devices, devid,
-                               disk_super->dev_item.uuid, NULL);
+               device = btrfs_find_device(fs_devices, &args);
 
                /*
                 * If this disk has been pulled into an fs devices created by
 static struct btrfs_device *btrfs_find_device_by_path(
                struct btrfs_fs_info *fs_info, const char *device_path)
 {
+       BTRFS_DEV_LOOKUP_ARGS(args);
        int ret = 0;
        struct btrfs_super_block *disk_super;
-       u64 devid;
-       u8 *dev_uuid;
        struct block_device *bdev;
        struct btrfs_device *device;
 
        if (ret)
                return ERR_PTR(ret);
 
-       devid = btrfs_stack_device_id(&disk_super->dev_item);
-       dev_uuid = disk_super->dev_item.uuid;
+       args.devid = btrfs_stack_device_id(&disk_super->dev_item);
+       args.uuid = disk_super->dev_item.uuid;
        if (btrfs_fs_incompat(fs_info, METADATA_UUID))
-               device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
-                                          disk_super->metadata_uuid);
+               args.fsid = disk_super->metadata_uuid;
        else
-               device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
-                                          disk_super->fsid);
+               args.fsid = disk_super->fsid;
+
+       device = btrfs_find_device(fs_info->fs_devices, &args);
 
        btrfs_release_disk_super(disk_super);
        if (!device)
                struct btrfs_fs_info *fs_info, u64 devid,
                const char *device_path)
 {
+       BTRFS_DEV_LOOKUP_ARGS(args);
        struct btrfs_device *device;
 
        if (devid) {
-               device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
-                                          NULL);
+               args.devid = devid;
+               device = btrfs_find_device(fs_info->fs_devices, &args);
                if (!device)
                        return ERR_PTR(-ENOENT);
                return device;
                return ERR_PTR(-EINVAL);
 
        if (strcmp(device_path, "missing") == 0) {
-               /* Find first missing device */
-               list_for_each_entry(device, &fs_info->fs_devices->devices,
-                                   dev_list) {
-                       if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
-                                    &device->dev_state) && !device->bdev)
-                               return device;
-               }
-               return ERR_PTR(-ENOENT);
+               args.missing = true;
+               device = btrfs_find_device(fs_info->fs_devices, &args);
+               if (!device)
+                       return ERR_PTR(-ENOENT);
+               return device;
        }
 
        return btrfs_find_device_by_path(fs_info, device_path);
  */
 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
 {
+       BTRFS_DEV_LOOKUP_ARGS(args);
        struct btrfs_fs_info *fs_info = trans->fs_info;
        struct btrfs_root *root = fs_info->chunk_root;
        struct btrfs_path *path;
        struct btrfs_key key;
        u8 fs_uuid[BTRFS_FSID_SIZE];
        u8 dev_uuid[BTRFS_UUID_SIZE];
-       u64 devid;
        int ret;
 
        path = btrfs_alloc_path();
 
                dev_item = btrfs_item_ptr(leaf, path->slots[0],
                                          struct btrfs_dev_item);
-               devid = btrfs_device_id(leaf, dev_item);
+               args.devid = btrfs_device_id(leaf, dev_item);
                read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
                                   BTRFS_UUID_SIZE);
                read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
                                   BTRFS_FSID_SIZE);
-               device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
-                                          fs_uuid);
+               args.uuid = dev_uuid;
+               args.fsid = fs_uuid;
+               device = btrfs_find_device(fs_info->fs_devices, &args);
                BUG_ON(!device); /* Logic error */
 
                if (device->fs_devices->seeding) {
        return BLK_STS_OK;
 }
 
+static bool dev_args_match_fs_devices(const struct btrfs_dev_lookup_args *args,
+                                     const struct btrfs_fs_devices *fs_devices)
+{
+       if (args->fsid == NULL)
+               return true;
+       if (memcmp(fs_devices->metadata_uuid, args->fsid, BTRFS_FSID_SIZE) == 0)
+               return true;
+       return false;
+}
+
+static bool dev_args_match_device(const struct btrfs_dev_lookup_args *args,
+                                 const struct btrfs_device *device)
+{
+       ASSERT((args->devid != (u64)-1) || args->missing);
+
+       if ((args->devid != (u64)-1) && device->devid != args->devid)
+               return false;
+       if (args->uuid && memcmp(device->uuid, args->uuid, BTRFS_UUID_SIZE) != 0)
+               return false;
+       if (!args->missing)
+               return true;
+       if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state) &&
+           !device->bdev)
+               return true;
+       return false;
+}
+
 /*
  * Find a device specified by @devid or @uuid in the list of @fs_devices, or
  * return NULL.
  * If devid and uuid are both specified, the match must be exact, otherwise
  * only devid is used.
  */
-struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
-                                      u64 devid, u8 *uuid, u8 *fsid)
+struct btrfs_device *btrfs_find_device(const struct btrfs_fs_devices *fs_devices,
+                                      const struct btrfs_dev_lookup_args *args)
 {
        struct btrfs_device *device;
        struct btrfs_fs_devices *seed_devs;
 
-       if (!fsid || !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
+       if (dev_args_match_fs_devices(args, fs_devices)) {
                list_for_each_entry(device, &fs_devices->devices, dev_list) {
-                       if (device->devid == devid &&
-                           (!uuid || memcmp(device->uuid, uuid,
-                                            BTRFS_UUID_SIZE) == 0))
+                       if (dev_args_match_device(args, device))
                                return device;
                }
        }
 
        list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
-               if (!fsid ||
-                   !memcmp(seed_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
-                       list_for_each_entry(device, &seed_devs->devices,
-                                           dev_list) {
-                               if (device->devid == devid &&
-                                   (!uuid || memcmp(device->uuid, uuid,
-                                                    BTRFS_UUID_SIZE) == 0))
-                                       return device;
-                       }
+               if (!dev_args_match_fs_devices(args, seed_devs))
+                       continue;
+               list_for_each_entry(device, &seed_devs->devices, dev_list) {
+                       if (dev_args_match_device(args, device))
+                               return device;
                }
        }
 
 static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
                          struct btrfs_chunk *chunk)
 {
+       BTRFS_DEV_LOOKUP_ARGS(args);
        struct btrfs_fs_info *fs_info = leaf->fs_info;
        struct extent_map_tree *map_tree = &fs_info->mapping_tree;
        struct map_lookup *map;
                map->stripes[i].physical =
                        btrfs_stripe_offset_nr(leaf, chunk, i);
                devid = btrfs_stripe_devid_nr(leaf, chunk, i);
+               args.devid = devid;
                read_extent_buffer(leaf, uuid, (unsigned long)
                                   btrfs_stripe_dev_uuid_nr(chunk, i),
                                   BTRFS_UUID_SIZE);
-               map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
-                                                       devid, uuid, NULL);
+               args.uuid = uuid;
+               map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, &args);
                if (!map->stripes[i].dev &&
                    !btrfs_test_opt(fs_info, DEGRADED)) {
                        free_extent_map(em);
 static int read_one_dev(struct extent_buffer *leaf,
                        struct btrfs_dev_item *dev_item)
 {
+       BTRFS_DEV_LOOKUP_ARGS(args);
        struct btrfs_fs_info *fs_info = leaf->fs_info;
        struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
        struct btrfs_device *device;
        u8 fs_uuid[BTRFS_FSID_SIZE];
        u8 dev_uuid[BTRFS_UUID_SIZE];
 
-       devid = btrfs_device_id(leaf, dev_item);
+       devid = args.devid = btrfs_device_id(leaf, dev_item);
        read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
                           BTRFS_UUID_SIZE);
        read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
                           BTRFS_FSID_SIZE);
+       args.uuid = dev_uuid;
+       args.fsid = fs_uuid;
 
        if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
                fs_devices = open_seed_devices(fs_info, fs_uuid);
                        return PTR_ERR(fs_devices);
        }
 
-       device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
-                                  fs_uuid);
+       device = btrfs_find_device(fs_info->fs_devices, &args);
        if (!device) {
                if (!btrfs_test_opt(fs_info, DEGRADED)) {
                        btrfs_report_missing_device(fs_info, devid,
 int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
                        struct btrfs_ioctl_get_dev_stats *stats)
 {
+       BTRFS_DEV_LOOKUP_ARGS(args);
        struct btrfs_device *dev;
        struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
        int i;
 
        mutex_lock(&fs_devices->device_list_mutex);
-       dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL);
+       args.devid = stats->devid;
+       dev = btrfs_find_device(fs_info->fs_devices, &args);
        mutex_unlock(&fs_devices->device_list_mutex);
 
        if (!dev) {
                                 u64 chunk_offset, u64 devid,
                                 u64 physical_offset, u64 physical_len)
 {
+       struct btrfs_dev_lookup_args args = { .devid = devid };
        struct extent_map_tree *em_tree = &fs_info->mapping_tree;
        struct extent_map *em;
        struct map_lookup *map;
        }
 
        /* Make sure no dev extent is beyond device boundary */
-       dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
+       dev = btrfs_find_device(fs_info->fs_devices, &args);
        if (!dev) {
                btrfs_err(fs_info, "failed to find devid %llu", devid);
                ret = -EUCLEAN;