Linux-BTRFS Archive on lore.kernel.org
 help / Atom feed
* [PATCH 0/6] btrfs: find_device cleanups
@ 2019-01-17 15:32 Anand Jain
  2019-01-17 15:32 ` [PATCH 1/6] btrfs: merge btrfs_find_device_missing_or_by_path() into parent Anand Jain
                   ` (6 more replies)
  0 siblings, 7 replies; 22+ messages in thread
From: Anand Jain @ 2019-01-17 15:32 UTC (permalink / raw)
  To: linux-btrfs

find_device and its helper functions are as below

 btrfs_find_device_by_devspec()
 btrfs_find_device_missing_or_by_path()
 btrfs_find_device_by_path()
 btrfs_find_device()
 find_device()

Its quite confusing and too fragmented.

In this patch-set..
 1/6 and 2/6 -- btrfs_find_device_missing_or_by_path() is collapsed into
 btrfs_find_device_by_devspec() and cleanup the surviving function.

 3/6 -- rename btrfs_find_device_by_path() to find_device_by_superblock().

 4/6 and 5/6 -- collapses find_device() into btrfs_find_device().

 6/6 refactors btrfs_find_device() to return standard error code.

Resulting in..

btrfs_find_device():
 Mainly used to get struct btrfs_device internally for a given devid
 and or uuid and also helper function for btrfs_find_device_by_devspec().

btrfs_find_device_by_devspec() and a helper function find_device_by_superblock():
 Is mainly to retrieve the struct btrfs_device of a userland given device_path.

Anand Jain (6):
  btrfs: merge btrfs_find_device_missing_or_by_path() into parent
  btrfs: cleanup btrfs_find_device_by_devspec()
  btrfs: rename btrfs_find_device_by_path()
  btrfs: refactor btrfs_find_device() take fs_devices as argument
  btrfs: merge btrfs_find_device() and find_device()
  btrfs: refactor btrfs_find_device() return error code

 fs/btrfs/dev-replace.c |  12 ++--
 fs/btrfs/ioctl.c       |   9 +--
 fs/btrfs/scrub.c       |  16 +++--
 fs/btrfs/volumes.c     | 160 +++++++++++++++++++++----------------------------
 fs/btrfs/volumes.h     |   5 +-
 5 files changed, 93 insertions(+), 109 deletions(-)

-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 1/6] btrfs: merge btrfs_find_device_missing_or_by_path() into parent
  2019-01-17 15:32 [PATCH 0/6] btrfs: find_device cleanups Anand Jain
@ 2019-01-17 15:32 ` Anand Jain
  2019-01-17 15:57   ` David Sterba
  2019-01-17 15:32 ` [PATCH 2/6] btrfs: cleanup btrfs_find_device_by_devspec() Anand Jain
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 22+ messages in thread
From: Anand Jain @ 2019-01-17 15:32 UTC (permalink / raw)
  To: linux-btrfs

btrfs_find_device_missing_or_by_path() is relatively small function, and
its only parent btrfs_find_device_by_devspec() is small as well. Besides
there are a number of find_device functions. Merge
btrfs_find_device_missing_or_by_path() into its parent.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/volumes.c | 40 +++++++++++++---------------------------
 1 file changed, 13 insertions(+), 27 deletions(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 24ce4ef41139..e92404081e25 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -2431,32 +2431,6 @@ static struct btrfs_device *btrfs_find_device_by_path(
 	return device;
 }
 
-static struct btrfs_device *btrfs_find_device_missing_or_by_path(
-		struct btrfs_fs_info *fs_info, const char *device_path)
-{
-	struct btrfs_device *device = NULL;
-	if (strcmp(device_path, "missing") == 0) {
-		struct list_head *devices;
-		struct btrfs_device *tmp;
-
-		devices = &fs_info->fs_devices->devices;
-		list_for_each_entry(tmp, devices, dev_list) {
-			if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
-					&tmp->dev_state) && !tmp->bdev) {
-				device = tmp;
-				break;
-			}
-		}
-
-		if (!device)
-			return ERR_PTR(-ENOENT);
-	} else {
-		device = btrfs_find_device_by_path(fs_info, device_path);
-	}
-
-	return device;
-}
-
 /*
  * Lookup a device given by device id, or the path if the id is 0.
  */
@@ -2472,7 +2446,19 @@ struct btrfs_device *btrfs_find_device_by_devspec(
 	} else {
 		if (!devpath || !devpath[0])
 			return ERR_PTR(-EINVAL);
-		device = btrfs_find_device_missing_or_by_path(fs_info, devpath);
+
+		if (strcmp(devpath, "missing") == 0) {
+			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);
+		} else {
+			device = btrfs_find_device_by_path(fs_info, devpath);
+		}
 	}
 	return device;
 }
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 2/6] btrfs: cleanup btrfs_find_device_by_devspec()
  2019-01-17 15:32 [PATCH 0/6] btrfs: find_device cleanups Anand Jain
  2019-01-17 15:32 ` [PATCH 1/6] btrfs: merge btrfs_find_device_missing_or_by_path() into parent Anand Jain
@ 2019-01-17 15:32 ` Anand Jain
  2019-01-17 15:57   ` David Sterba
  2019-01-17 15:32 ` [PATCH 3/6] btrfs: rename btrfs_find_device_by_path() Anand Jain
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 22+ messages in thread
From: Anand Jain @ 2019-01-17 15:32 UTC (permalink / raw)
  To: linux-btrfs

btrfs_find_device_by_devspec() finds the device by @devid or by
@device_path. This patch makes code flow easy to read by open
coding the else part and renames devpath to device_path.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
If the else is preferred, I am ok to keep them. Nikolay?

 fs/btrfs/volumes.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index e92404081e25..8b5201c734b9 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -2435,7 +2435,8 @@ static struct btrfs_device *btrfs_find_device_by_path(
  * Lookup a device given by device id, or the path if the id is 0.
  */
 struct btrfs_device *btrfs_find_device_by_devspec(
-		struct btrfs_fs_info *fs_info, u64 devid, const char *devpath)
+		struct btrfs_fs_info *fs_info, u64 devid,
+		const char *device_path)
 {
 	struct btrfs_device *device;
 
@@ -2443,24 +2444,23 @@ struct btrfs_device *btrfs_find_device_by_devspec(
 		device = btrfs_find_device(fs_info, devid, NULL, NULL);
 		if (!device)
 			return ERR_PTR(-ENOENT);
-	} else {
-		if (!devpath || !devpath[0])
-			return ERR_PTR(-EINVAL);
-
-		if (strcmp(devpath, "missing") == 0) {
-			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);
-		} else {
-			device = btrfs_find_device_by_path(fs_info, devpath);
+		return device;
+	}
+
+	if (!device_path || !device_path[0])
+		return ERR_PTR(-EINVAL);
+
+	if (strcmp(device_path, "missing") == 0) {
+		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);
 	}
-	return device;
+
+	return btrfs_find_device_by_path(fs_info, device_path);
 }
 
 /*
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 3/6] btrfs: rename btrfs_find_device_by_path()
  2019-01-17 15:32 [PATCH 0/6] btrfs: find_device cleanups Anand Jain
  2019-01-17 15:32 ` [PATCH 1/6] btrfs: merge btrfs_find_device_missing_or_by_path() into parent Anand Jain
  2019-01-17 15:32 ` [PATCH 2/6] btrfs: cleanup btrfs_find_device_by_devspec() Anand Jain
@ 2019-01-17 15:32 ` Anand Jain
  2019-01-17 15:54   ` David Sterba
  2019-01-17 15:32 ` [PATCH 4/6] btrfs: refactor btrfs_find_device() take fs_devices as argument Anand Jain
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 22+ messages in thread
From: Anand Jain @ 2019-01-17 15:32 UTC (permalink / raw)
  To: linux-btrfs

btrfs_find_device_by_path() is a helper function, drop the btrfs prefix
and the suffix _path is too generic, in fact as it reads superblock to
find the btrfs_device, so rename it to find_device_by_superblock()

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/volumes.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 8b5201c734b9..a307d76c8926 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -2399,7 +2399,7 @@ void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
 	call_rcu(&tgtdev->rcu, free_device_rcu);
 }
 
-static struct btrfs_device *btrfs_find_device_by_path(
+static struct btrfs_device *find_device_by_superblock(
 		struct btrfs_fs_info *fs_info, const char *device_path)
 {
 	int ret = 0;
@@ -2460,7 +2460,7 @@ struct btrfs_device *btrfs_find_device_by_devspec(
 		return ERR_PTR(-ENOENT);
 	}
 
-	return btrfs_find_device_by_path(fs_info, device_path);
+	return find_device_by_superblock(fs_info, device_path);
 }
 
 /*
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 4/6] btrfs: refactor btrfs_find_device() take fs_devices as argument
  2019-01-17 15:32 [PATCH 0/6] btrfs: find_device cleanups Anand Jain
                   ` (2 preceding siblings ...)
  2019-01-17 15:32 ` [PATCH 3/6] btrfs: rename btrfs_find_device_by_path() Anand Jain
@ 2019-01-17 15:32 ` Anand Jain
  2019-01-17 15:58   ` David Sterba
  2019-01-17 15:32 ` [PATCH 5/6] btrfs: merge btrfs_find_device() and find_device() Anand Jain
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 22+ messages in thread
From: Anand Jain @ 2019-01-17 15:32 UTC (permalink / raw)
  To: linux-btrfs

btrfs_find_device() accepts fs_info as an argument and retrieves
fs_devices from fs_info.

Instead send fs_devices, so that this function can be used in non
mounted (scanned) context as well.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/dev-replace.c |  6 +++---
 fs/btrfs/ioctl.c       |  5 +++--
 fs/btrfs/scrub.c       |  4 ++--
 fs/btrfs/volumes.c     | 39 ++++++++++++++++++++-------------------
 fs/btrfs/volumes.h     |  4 ++--
 5 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index 8750c835f535..6f0fe3623381 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -111,9 +111,9 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 		break;
 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
-		dev_replace->srcdev = btrfs_find_device(fs_info, src_devid,
-							NULL, NULL);
-		dev_replace->tgtdev = btrfs_find_device(fs_info,
+		dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
+							src_devid, NULL, NULL);
+		dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
 							BTRFS_DEV_REPLACE_DEVID,
 							NULL, NULL);
 		/*
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 72b2b2de6293..fd5f97aeb35c 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1642,7 +1642,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
 		btrfs_info(fs_info, "resizing devid %llu", devid);
 	}
 
-	device = btrfs_find_device(fs_info, devid, NULL, NULL);
+	device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
 	if (!device) {
 		btrfs_info(fs_info, "resizer unable to find device %llu",
 			   devid);
@@ -3178,7 +3178,8 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
 		s_uuid = di_args->uuid;
 
 	rcu_read_lock();
-	dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
+				NULL);
 
 	if (!dev) {
 		ret = -ENODEV;
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 6dcd36d7b849..72044efc610a 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -3835,7 +3835,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 		return PTR_ERR(sctx);
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
-	dev = btrfs_find_device(fs_info, devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
 	if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
 		     !is_dev_replace)) {
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
@@ -4012,7 +4012,7 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
 	struct scrub_ctx *sctx = NULL;
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
-	dev = btrfs_find_device(fs_info, devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
 	if (dev)
 		sctx = dev->scrub_ctx;
 	if (sctx)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index a307d76c8926..be473a493a63 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -2418,11 +2418,11 @@ static struct btrfs_device *find_device_by_superblock(
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	dev_uuid = disk_super->dev_item.uuid;
 	if (btrfs_fs_incompat(fs_info, METADATA_UUID))
-		device = btrfs_find_device(fs_info, devid, dev_uuid,
-				disk_super->metadata_uuid);
+		device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
+					   disk_super->metadata_uuid);
 	else
-		device = btrfs_find_device(fs_info, devid,
-				dev_uuid, disk_super->fsid);
+		device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
+					   disk_super->fsid);
 
 	brelse(bh);
 	if (!device)
@@ -2441,7 +2441,8 @@ struct btrfs_device *btrfs_find_device_by_devspec(
 	struct btrfs_device *device;
 
 	if (devid) {
-		device = btrfs_find_device(fs_info, devid, NULL, NULL);
+		device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
+					   NULL);
 		if (!device)
 			return ERR_PTR(-ENOENT);
 		return device;
@@ -2582,7 +2583,8 @@ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
 				   BTRFS_UUID_SIZE);
 		read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
 				   BTRFS_FSID_SIZE);
-		device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
+		device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
+					   fs_uuid);
 		BUG_ON(!device); /* Logic error */
 
 		if (device->fs_devices->seeding) {
@@ -6635,21 +6637,19 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
 	return BLK_STS_OK;
 }
 
-struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
-				       u8 *uuid, u8 *fsid)
+struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
+				       u64 devid, u8 *uuid, u8 *fsid)
 {
 	struct btrfs_device *device;
-	struct btrfs_fs_devices *cur_devices;
 
-	cur_devices = fs_info->fs_devices;
-	while (cur_devices) {
+	while (fs_devices) {
 		if (!fsid ||
-		    !memcmp(cur_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
-			device = find_device(cur_devices, devid, uuid);
+		    !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
+			device = find_device(fs_devices, devid, uuid);
 			if (device)
 				return device;
 		}
-		cur_devices = cur_devices->seed;
+		fs_devices = fs_devices->seed;
 	}
 	return NULL;
 }
@@ -6894,8 +6894,8 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
 		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, devid,
-							uuid, NULL);
+		map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
+							devid, uuid, NULL);
 		if (!map->stripes[i].dev &&
 		    !btrfs_test_opt(fs_info, DEGRADED)) {
 			free_extent_map(em);
@@ -7034,7 +7034,8 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 			return PTR_ERR(fs_devices);
 	}
 
-	device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
+	device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
+				   fs_uuid);
 	if (!device) {
 		if (!btrfs_test_opt(fs_info, DEGRADED)) {
 			btrfs_report_missing_device(fs_info, devid,
@@ -7624,7 +7625,7 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
 	int i;
 
 	mutex_lock(&fs_devices->device_list_mutex);
-	dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL);
 	mutex_unlock(&fs_devices->device_list_mutex);
 
 	if (!dev) {
@@ -7838,7 +7839,7 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
 	}
 
 	/* Make sure no dev extent is beyond device bondary */
-	dev = btrfs_find_device(fs_info, devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
 	if (!dev) {
 		btrfs_err(fs_info, "failed to find devid %llu", devid);
 		ret = -EUCLEAN;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 1e73257e55fb..46ee8eafdc92 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -434,8 +434,8 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info,
 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
 int btrfs_grow_device(struct btrfs_trans_handle *trans,
 		      struct btrfs_device *device, u64 new_size);
-struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
-				       u8 *uuid, u8 *fsid);
+struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
+				       u64 devid, u8 *uuid, u8 *fsid);
 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
 int btrfs_balance(struct btrfs_fs_info *fs_info,
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 5/6] btrfs: merge btrfs_find_device() and find_device()
  2019-01-17 15:32 [PATCH 0/6] btrfs: find_device cleanups Anand Jain
                   ` (3 preceding siblings ...)
  2019-01-17 15:32 ` [PATCH 4/6] btrfs: refactor btrfs_find_device() take fs_devices as argument Anand Jain
@ 2019-01-17 15:32 ` Anand Jain
  2019-01-17 15:51   ` David Sterba
  2019-01-19  6:48   ` [PATCH 5/6 v2] " Anand Jain
  2019-01-17 15:32 ` [PATCH 6/6] btrfs: refactor btrfs_find_device() return error code Anand Jain
  2019-01-18 17:33 ` [PATCH 0/6] btrfs: find_device cleanups David Sterba
  6 siblings, 2 replies; 22+ messages in thread
From: Anand Jain @ 2019-01-17 15:32 UTC (permalink / raw)
  To: linux-btrfs

Both btrfs_find_device() and find_device() does the same things expect
that latter function is not keen in seed device in the scan-context. So
merge them.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/dev-replace.c |  4 +--
 fs/btrfs/ioctl.c       |  4 +--
 fs/btrfs/scrub.c       |  4 +--
 fs/btrfs/volumes.c     | 69 ++++++++++++++++++++++++--------------------------
 fs/btrfs/volumes.h     |  3 ++-
 5 files changed, 41 insertions(+), 43 deletions(-)

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index 6f0fe3623381..b5897becd542 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -112,10 +112,10 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
 		dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
-							src_devid, NULL, NULL);
+							src_devid, NULL, NULL, 0);
 		dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
 							BTRFS_DEV_REPLACE_DEVID,
-							NULL, NULL);
+							NULL, NULL, 0);
 		/*
 		 * allow 'btrfs dev replace_cancel' if src/tgt device is
 		 * missing
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index fd5f97aeb35c..66f2f9e229d5 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1642,7 +1642,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
 		btrfs_info(fs_info, "resizing devid %llu", devid);
 	}
 
-	device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
+	device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, 0);
 	if (!device) {
 		btrfs_info(fs_info, "resizer unable to find device %llu",
 			   devid);
@@ -3179,7 +3179,7 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
 
 	rcu_read_lock();
 	dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
-				NULL);
+				NULL, 0);
 
 	if (!dev) {
 		ret = -ENODEV;
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 72044efc610a..477c585927e4 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -3835,7 +3835,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 		return PTR_ERR(sctx);
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
-	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, 0);
 	if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
 		     !is_dev_replace)) {
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
@@ -4012,7 +4012,7 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
 	struct scrub_ctx *sctx = NULL;
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
-	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, 0);
 	if (dev)
 		sctx = dev->scrub_ctx;
 	if (sctx)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index be473a493a63..7f89c8b1cab1 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -415,27 +415,6 @@ static struct btrfs_device *__alloc_device(void)
 	return dev;
 }
 
-/*
- * 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.
- */
-static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
-		u64 devid, const u8 *uuid)
-{
-	struct btrfs_device *dev;
-
-	list_for_each_entry(dev, &fs_devices->devices, dev_list) {
-		if (dev->devid == devid &&
-		    (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
-			return dev;
-		}
-	}
-	return NULL;
-}
-
 static noinline struct btrfs_fs_devices *find_fsid(
 		const u8 *fsid, const u8 *metadata_fsid)
 {
@@ -990,8 +969,8 @@ static noinline struct btrfs_device *device_list_add(const char *path,
 		device = NULL;
 	} else {
 		mutex_lock(&fs_devices->device_list_mutex);
-		device = find_device(fs_devices, devid,
-				disk_super->dev_item.uuid);
+		device = btrfs_find_device(fs_devices, devid,
+					   disk_super->dev_item.uuid, NULL, 1);
 
 		/*
 		 * If this disk has been pulled into an fs devices created by
@@ -2419,10 +2398,10 @@ static struct btrfs_device *find_device_by_superblock(
 	dev_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);
+					   disk_super->metadata_uuid, 0);
 	else
 		device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
-					   disk_super->fsid);
+					   disk_super->fsid, 0);
 
 	brelse(bh);
 	if (!device)
@@ -2442,7 +2421,7 @@ struct btrfs_device *btrfs_find_device_by_devspec(
 
 	if (devid) {
 		device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
-					   NULL);
+					   NULL, 0);
 		if (!device)
 			return ERR_PTR(-ENOENT);
 		return device;
@@ -2584,7 +2563,7 @@ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
 		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);
+					   fs_uuid, 0);
 		BUG_ON(!device); /* Logic error */
 
 		if (device->fs_devices->seeding) {
@@ -6637,19 +6616,36 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
 	return BLK_STS_OK;
 }
 
+/*
+ * 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.
+ *
+ * By default will check seed devices as well, unless @no_seed is 1.
+ */
 struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
-				       u64 devid, u8 *uuid, u8 *fsid)
+				       u64 devid, u8 *uuid, u8 *fsid,
+				       int no_seed)
 {
 	struct btrfs_device *device;
 
 	while (fs_devices) {
 		if (!fsid ||
 		    !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
-			device = find_device(fs_devices, devid, uuid);
-			if (device)
-				return device;
+			list_for_each_entry(device, &fs_devices->devices,
+					    dev_list) {
+				if (device->devid == devid && (!uuid ||
+				    !memcmp(device->uuid, uuid,
+				    BTRFS_UUID_SIZE)))
+					return device;
+			}
 		}
-		fs_devices = fs_devices->seed;
+		if (no_seed)
+			return NULL;
+		else
+			fs_devices = fs_devices->seed;
 	}
 	return NULL;
 }
@@ -6895,7 +6891,7 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
 				   btrfs_stripe_dev_uuid_nr(chunk, i),
 				   BTRFS_UUID_SIZE);
 		map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
-							devid, uuid, NULL);
+							devid, uuid, NULL, 0);
 		if (!map->stripes[i].dev &&
 		    !btrfs_test_opt(fs_info, DEGRADED)) {
 			free_extent_map(em);
@@ -7035,7 +7031,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 	}
 
 	device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
-				   fs_uuid);
+				   fs_uuid, 0);
 	if (!device) {
 		if (!btrfs_test_opt(fs_info, DEGRADED)) {
 			btrfs_report_missing_device(fs_info, devid,
@@ -7625,7 +7621,8 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
 	int i;
 
 	mutex_lock(&fs_devices->device_list_mutex);
-	dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
+				0);
 	mutex_unlock(&fs_devices->device_list_mutex);
 
 	if (!dev) {
@@ -7839,7 +7836,7 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
 	}
 
 	/* Make sure no dev extent is beyond device bondary */
-	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, 0);
 	if (!dev) {
 		btrfs_err(fs_info, "failed to find devid %llu", devid);
 		ret = -EUCLEAN;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 46ee8eafdc92..8f71cc724015 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -435,7 +435,8 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info,
 int btrfs_grow_device(struct btrfs_trans_handle *trans,
 		      struct btrfs_device *device, u64 new_size);
 struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
-				       u64 devid, u8 *uuid, u8 *fsid);
+				       u64 devid, u8 *uuid, u8 *fsid,
+				       int no_seed);
 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
 int btrfs_balance(struct btrfs_fs_info *fs_info,
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 6/6] btrfs: refactor btrfs_find_device() return error code
  2019-01-17 15:32 [PATCH 0/6] btrfs: find_device cleanups Anand Jain
                   ` (4 preceding siblings ...)
  2019-01-17 15:32 ` [PATCH 5/6] btrfs: merge btrfs_find_device() and find_device() Anand Jain
@ 2019-01-17 15:32 ` Anand Jain
  2019-01-17 15:49   ` David Sterba
  2019-01-18 17:33 ` [PATCH 0/6] btrfs: find_device cleanups David Sterba
  6 siblings, 1 reply; 22+ messages in thread
From: Anand Jain @ 2019-01-17 15:32 UTC (permalink / raw)
  To: linux-btrfs

Refactor btrfs_find_device() to return standard error code.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 fs/btrfs/dev-replace.c |  4 ++--
 fs/btrfs/ioctl.c       |  4 ++--
 fs/btrfs/scrub.c       | 12 ++++++++----
 fs/btrfs/volumes.c     | 32 +++++++++++++-------------------
 4 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index b5897becd542..23cef72a5a87 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -120,7 +120,7 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 		 * allow 'btrfs dev replace_cancel' if src/tgt device is
 		 * missing
 		 */
-		if (!dev_replace->srcdev &&
+		if (PTR_ERR(dev_replace->srcdev) == -ENOENT &&
 		    !btrfs_test_opt(fs_info, DEGRADED)) {
 			ret = -EIO;
 			btrfs_warn(fs_info,
@@ -129,7 +129,7 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 			   "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
 			   src_devid);
 		}
-		if (!dev_replace->tgtdev &&
+		if (PTR_ERR(dev_replace->tgtdev) == -ENOENT &&
 		    !btrfs_test_opt(fs_info, DEGRADED)) {
 			ret = -EIO;
 			btrfs_warn(fs_info,
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 66f2f9e229d5..71243025b324 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1643,7 +1643,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
 	}
 
 	device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, 0);
-	if (!device) {
+	if (PTR_ERR(device) == -ENOENT) {
 		btrfs_info(fs_info, "resizer unable to find device %llu",
 			   devid);
 		ret = -ENODEV;
@@ -3181,7 +3181,7 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
 	dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
 				NULL, 0);
 
-	if (!dev) {
+	if (PTR_ERR(dev) == -ENOENT) {
 		ret = -ENODEV;
 		goto out;
 	}
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 477c585927e4..c8029628282a 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -3836,8 +3836,9 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, 0);
-	if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
-		     !is_dev_replace)) {
+	if (PTR_ERR(dev) == -ENOENT ||
+	    (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
+	    !is_dev_replace)) {
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
 		ret = -ENODEV;
 		goto out_free_ctx;
@@ -4013,13 +4014,16 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
 	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, 0);
-	if (dev)
+	if (PTR_ERR(dev) != -ENOENT)
 		sctx = dev->scrub_ctx;
 	if (sctx)
 		memcpy(progress, &sctx->stat, sizeof(*progress));
 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
 
-	return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
+	if (PTR_ERR(dev) == -ENOENT)
+		return -ENODEV;
+	else
+		return sctx ? 0 : -ENOTCONN;
 }
 
 static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 7f89c8b1cab1..5a3846daabcf 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -966,7 +966,7 @@ static noinline struct btrfs_device *device_list_add(const char *path,
 		mutex_lock(&fs_devices->device_list_mutex);
 		list_add(&fs_devices->fs_list, &fs_uuids);
 
-		device = NULL;
+		device = ERR_PTR(-ENOENT);
 	} else {
 		mutex_lock(&fs_devices->device_list_mutex);
 		device = btrfs_find_device(fs_devices, devid,
@@ -988,7 +988,7 @@ static noinline struct btrfs_device *device_list_add(const char *path,
 		}
 	}
 
-	if (!device) {
+	if (PTR_ERR(device) == -ENOENT) {
 		if (fs_devices->opened) {
 			mutex_unlock(&fs_devices->device_list_mutex);
 			return ERR_PTR(-EBUSY);
@@ -2404,8 +2404,6 @@ static struct btrfs_device *find_device_by_superblock(
 					   disk_super->fsid, 0);
 
 	brelse(bh);
-	if (!device)
-		device = ERR_PTR(-ENOENT);
 	blkdev_put(bdev, FMODE_READ);
 	return device;
 }
@@ -2419,13 +2417,9 @@ struct btrfs_device *btrfs_find_device_by_devspec(
 {
 	struct btrfs_device *device;
 
-	if (devid) {
-		device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
-					   NULL, 0);
-		if (!device)
-			return ERR_PTR(-ENOENT);
-		return device;
-	}
+	if (devid)
+		return btrfs_find_device(fs_info->fs_devices, devid, NULL,
+					 NULL, 0);
 
 	if (!device_path || !device_path[0])
 		return ERR_PTR(-EINVAL);
@@ -2564,7 +2558,7 @@ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
 				   BTRFS_FSID_SIZE);
 		device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
 					   fs_uuid, 0);
-		BUG_ON(!device); /* Logic error */
+		BUG_ON(PTR_ERR(device) == -ENOENT); /* Logic error */
 
 		if (device->fs_devices->seeding) {
 			btrfs_set_device_generation(leaf, dev_item,
@@ -6643,11 +6637,11 @@ struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
 			}
 		}
 		if (no_seed)
-			return NULL;
+			return ERR_PTR(-ENOENT);
 		else
 			fs_devices = fs_devices->seed;
 	}
-	return NULL;
+	return ERR_PTR(-ENOENT);
 }
 
 static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
@@ -6892,13 +6886,13 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
 				   BTRFS_UUID_SIZE);
 		map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
 							devid, uuid, NULL, 0);
-		if (!map->stripes[i].dev &&
+		if (PTR_ERR(map->stripes[i].dev) == -ENOENT &&
 		    !btrfs_test_opt(fs_info, DEGRADED)) {
 			free_extent_map(em);
 			btrfs_report_missing_device(fs_info, devid, uuid, true);
 			return -ENOENT;
 		}
-		if (!map->stripes[i].dev) {
+		if (PTR_ERR(map->stripes[i].dev) == -ENOENT) {
 			map->stripes[i].dev =
 				add_missing_dev(fs_info->fs_devices, devid,
 						uuid);
@@ -7032,7 +7026,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 
 	device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
 				   fs_uuid, 0);
-	if (!device) {
+	if (PTR_ERR(device) == -ENOENT) {
 		if (!btrfs_test_opt(fs_info, DEGRADED)) {
 			btrfs_report_missing_device(fs_info, devid,
 							dev_uuid, true);
@@ -7625,7 +7619,7 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
 				0);
 	mutex_unlock(&fs_devices->device_list_mutex);
 
-	if (!dev) {
+	if (PTR_ERR(dev) == -ENOENT) {
 		btrfs_warn(fs_info, "get dev_stats failed, device not found");
 		return -ENODEV;
 	} else if (!dev->dev_stats_valid) {
@@ -7837,7 +7831,7 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
 
 	/* Make sure no dev extent is beyond device bondary */
 	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, 0);
-	if (!dev) {
+	if (PTR_ERR(dev) == -ENOENT) {
 		btrfs_err(fs_info, "failed to find devid %llu", devid);
 		ret = -EUCLEAN;
 		goto out;
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 6/6] btrfs: refactor btrfs_find_device() return error code
  2019-01-17 15:32 ` [PATCH 6/6] btrfs: refactor btrfs_find_device() return error code Anand Jain
@ 2019-01-17 15:49   ` David Sterba
  2019-01-18  6:13     ` Anand Jain
  0 siblings, 1 reply; 22+ messages in thread
From: David Sterba @ 2019-01-17 15:49 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Thu, Jan 17, 2019 at 11:32:33PM +0800, Anand Jain wrote:
> Refactor btrfs_find_device() to return standard error code.

Do you intend to add more error codes? A NULL is valid in case it's
clear that it always means 'device not found'.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 5/6] btrfs: merge btrfs_find_device() and find_device()
  2019-01-17 15:32 ` [PATCH 5/6] btrfs: merge btrfs_find_device() and find_device() Anand Jain
@ 2019-01-17 15:51   ` David Sterba
  2019-01-19  6:48   ` [PATCH 5/6 v2] " Anand Jain
  1 sibling, 0 replies; 22+ messages in thread
From: David Sterba @ 2019-01-17 15:51 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Thu, Jan 17, 2019 at 11:32:32PM +0800, Anand Jain wrote:
> @@ -6637,19 +6616,36 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
>  	return BLK_STS_OK;
>  }
>  
> +/*
> + * 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.
> + *
> + * By default will check seed devices as well, unless @no_seed is 1.
> + */
>  struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
> -				       u64 devid, u8 *uuid, u8 *fsid)
> +				       u64 devid, u8 *uuid, u8 *fsid,
> +				       int no_seed)

Please use bool here and a positive meaning of the variable.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 3/6] btrfs: rename btrfs_find_device_by_path()
  2019-01-17 15:32 ` [PATCH 3/6] btrfs: rename btrfs_find_device_by_path() Anand Jain
@ 2019-01-17 15:54   ` David Sterba
  2019-01-18  6:13     ` Anand Jain
  0 siblings, 1 reply; 22+ messages in thread
From: David Sterba @ 2019-01-17 15:54 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Thu, Jan 17, 2019 at 11:32:30PM +0800, Anand Jain wrote:
> btrfs_find_device_by_path() is a helper function, drop the btrfs prefix
> and the suffix _path is too generic, in fact as it reads superblock to
> find the btrfs_device, so rename it to find_device_by_superblock()

The function takes a path so it's search by path, as the name says, I
don't think that needs to change. And even this is a helper, the
btrfs_ prefix is useful in case there's something stuck inside the
block layer functions that get called later so it's obvious on the
stack.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 1/6] btrfs: merge btrfs_find_device_missing_or_by_path() into parent
  2019-01-17 15:32 ` [PATCH 1/6] btrfs: merge btrfs_find_device_missing_or_by_path() into parent Anand Jain
@ 2019-01-17 15:57   ` David Sterba
  0 siblings, 0 replies; 22+ messages in thread
From: David Sterba @ 2019-01-17 15:57 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Thu, Jan 17, 2019 at 11:32:28PM +0800, Anand Jain wrote:
> btrfs_find_device_missing_or_by_path() is relatively small function, and
> its only parent btrfs_find_device_by_devspec() is small as well. Besides
> there are a number of find_device functions. Merge
> btrfs_find_device_missing_or_by_path() into its parent.
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>

OK,

Reviewed-by: David Sterba <dsterba@suse.com>

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 2/6] btrfs: cleanup btrfs_find_device_by_devspec()
  2019-01-17 15:32 ` [PATCH 2/6] btrfs: cleanup btrfs_find_device_by_devspec() Anand Jain
@ 2019-01-17 15:57   ` David Sterba
  0 siblings, 0 replies; 22+ messages in thread
From: David Sterba @ 2019-01-17 15:57 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Thu, Jan 17, 2019 at 11:32:29PM +0800, Anand Jain wrote:
> btrfs_find_device_by_devspec() finds the device by @devid or by
> @device_path. This patch makes code flow easy to read by open
> coding the else part and renames devpath to device_path.
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>
> ---
> If the else is preferred, I am ok to keep them. Nikolay?

It looks better to me the way you implement it, a series of ifs, each
does a return, the function is short and easy to follow.

Reviewed-by: David Sterba <dsterba@suse.com>

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 4/6] btrfs: refactor btrfs_find_device() take fs_devices as argument
  2019-01-17 15:32 ` [PATCH 4/6] btrfs: refactor btrfs_find_device() take fs_devices as argument Anand Jain
@ 2019-01-17 15:58   ` David Sterba
  0 siblings, 0 replies; 22+ messages in thread
From: David Sterba @ 2019-01-17 15:58 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Thu, Jan 17, 2019 at 11:32:31PM +0800, Anand Jain wrote:
> btrfs_find_device() accepts fs_info as an argument and retrieves
> fs_devices from fs_info.
> 
> Instead send fs_devices, so that this function can be used in non
> mounted (scanned) context as well.
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>

Reviewed-by: David Sterba <dsterba@suse.com>

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 3/6] btrfs: rename btrfs_find_device_by_path()
  2019-01-17 15:54   ` David Sterba
@ 2019-01-18  6:13     ` Anand Jain
  2019-01-18 17:05       ` David Sterba
  0 siblings, 1 reply; 22+ messages in thread
From: Anand Jain @ 2019-01-18  6:13 UTC (permalink / raw)
  To: dsterba, linux-btrfs



On 01/17/2019 11:54 PM, David Sterba wrote:
> On Thu, Jan 17, 2019 at 11:32:30PM +0800, Anand Jain wrote:
>> btrfs_find_device_by_path() is a helper function, drop the btrfs prefix
>> and the suffix _path is too generic, in fact as it reads superblock to
>> find the btrfs_device, so rename it to find_device_by_superblock()
> 
> The function takes a path so it's search by path, as the name says, I
> don't think that needs to change.

  Sorry I didn't mention about my motivation to rename..
  find device by matching the device_path (without reading its
  superblock) is done by btrfs_free_stale_devices() and its helper
  device_path_matched() in the patch [1] which is in next-dev.

  [1]
  btrfs: refactor btrfs_free_stale_devices() to get return value

  So a reader can think why not use btrfs_find_device_by_path() here,
  unless fn is read to find out it matches the device by taking
  the devid from the superblock.

  Its not a big deal though.. I am ok to keep this as it is.. or... more
  below..

> And even this is a helper, the
> btrfs_ prefix is useful in case there's something stuck inside the
> block layer functions that get called later so it's obvious on the
> stack.

  Makes sense.

  Or.
  I think its a good idea to collapse this relatively small function into
  its only parent, btrfs_find_device_by_devspec().

Thanks.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 6/6] btrfs: refactor btrfs_find_device() return error code
  2019-01-17 15:49   ` David Sterba
@ 2019-01-18  6:13     ` Anand Jain
  2019-01-18 17:20       ` David Sterba
  0 siblings, 1 reply; 22+ messages in thread
From: Anand Jain @ 2019-01-18  6:13 UTC (permalink / raw)
  To: dsterba, linux-btrfs



On 01/17/2019 11:49 PM, David Sterba wrote:
> On Thu, Jan 17, 2019 at 11:32:33PM +0800, Anand Jain wrote:
>> Refactor btrfs_find_device() to return standard error code.
> 
> Do you intend to add more error codes?

  No actually.

  code like this ...
     if (PTR_ERR(device) == -ENOENT)
  can be
     if (IS_ERR(device))


> A NULL is valid in case it's
> clear that it always means 'device not found'.
> 

  Oh. I am not too particular, I am ok to drop this patch.

  The thought process was.. having btrfs_find_device() to return standard
  err code made code at @@ -2404,8 +2404,6 @@; @@ -2419,13 +2417,9 @@;
  simpler.
  And as ioctls already return -ENODEV (though it might not be correct
  one), so if btrfs_find_device() could return -ENODEV (instead of
  -ENOENT) then there are few more places where code can be just ..
     if (IS_ERR(device))
	return PTR_ERR(device);

  What do you think?

Thanks, Anand

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 3/6] btrfs: rename btrfs_find_device_by_path()
  2019-01-18  6:13     ` Anand Jain
@ 2019-01-18 17:05       ` David Sterba
  0 siblings, 0 replies; 22+ messages in thread
From: David Sterba @ 2019-01-18 17:05 UTC (permalink / raw)
  To: Anand Jain; +Cc: dsterba, linux-btrfs

On Fri, Jan 18, 2019 at 02:13:19PM +0800, Anand Jain wrote:
> On 01/17/2019 11:54 PM, David Sterba wrote:
> > On Thu, Jan 17, 2019 at 11:32:30PM +0800, Anand Jain wrote:
> >> btrfs_find_device_by_path() is a helper function, drop the btrfs prefix
> >> and the suffix _path is too generic, in fact as it reads superblock to
> >> find the btrfs_device, so rename it to find_device_by_superblock()
> > 
> > The function takes a path so it's search by path, as the name says, I
> > don't think that needs to change.
> 
>   Sorry I didn't mention about my motivation to rename..
>   find device by matching the device_path (without reading its
>   superblock) is done by btrfs_free_stale_devices() and its helper
>   device_path_matched() in the patch [1] which is in next-dev.
> 
>   [1]
>   btrfs: refactor btrfs_free_stale_devices() to get return value
> 
>   So a reader can think why not use btrfs_find_device_by_path() here,
>   unless fn is read to find out it matches the device by taking
>   the devid from the superblock.

That would be really unfortunate to conflate a simple short helper that
really wraps strcmp with a function doing a lot of other things. With
source code at hand we don't have to speculate from a function name what
it does, we go and read it if not sure.

>   Its not a big deal though.. I am ok to keep this as it is.. or... more
>   below..
> 
> > And even this is a helper, the
> > btrfs_ prefix is useful in case there's something stuck inside the
> > block layer functions that get called later so it's obvious on the
> > stack.
> 
>   Makes sense.
> 
>   Or.
>   I think its a good idea to collapse this relatively small function into
>   its only parent, btrfs_find_device_by_devspec().

Devspec is either device id or the path, so the _devspec is a switch to
two ways how to find the device and merging the the helper code does not
seem to improve code.

Sorry I really fail to see the point of this patch and the code issues
it's supposed to resolve.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 6/6] btrfs: refactor btrfs_find_device() return error code
  2019-01-18  6:13     ` Anand Jain
@ 2019-01-18 17:20       ` David Sterba
  0 siblings, 0 replies; 22+ messages in thread
From: David Sterba @ 2019-01-18 17:20 UTC (permalink / raw)
  To: Anand Jain; +Cc: dsterba, linux-btrfs

On Fri, Jan 18, 2019 at 02:13:26PM +0800, Anand Jain wrote:
> 
> 
> On 01/17/2019 11:49 PM, David Sterba wrote:
> > On Thu, Jan 17, 2019 at 11:32:33PM +0800, Anand Jain wrote:
> >> Refactor btrfs_find_device() to return standard error code.
> > 
> > Do you intend to add more error codes?
> 
>   No actually.
> 
>   code like this ...
>      if (PTR_ERR(device) == -ENOENT)
>   can be
>      if (IS_ERR(device))
> 
> 
> > A NULL is valid in case it's
> > clear that it always means 'device not found'.
> > 
> 
>   Oh. I am not too particular, I am ok to drop this patch.
> 
>   The thought process was.. having btrfs_find_device() to return standard
>   err code made code at @@ -2404,8 +2404,6 @@; @@ -2419,13 +2417,9 @@;
>   simpler.

Well, that piece of code is simpler but when compared to the amount of
other changes in the rest it's not making it simpler.

>   And as ioctls already return -ENODEV (though it might not be correct
>   one),

Unrelated to this patch, but I tend to agree that ENODEV is wrong.
However it's been in use for a long time so we can't change easily.

>   so if btrfs_find_device() could return -ENODEV (instead of
>   -ENOENT) then there are few more places where code can be just ..
>      if (IS_ERR(device))
> 	return PTR_ERR(device);

I've seen a lot return values of btrfs_find_device are for internal
purposes so the translation from NULL pointer to ENODEV/ENOENT does not
happen that often. In some contexts it's even something different like
EUCLEAN, but I maybe see the motivation to change it to ERR_PTR, as
btrfs_find_device_by_devspec and btrfs_find_device_missing_or_by_path
return that - btrfs_find_device is different here.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 0/6] btrfs: find_device cleanups
  2019-01-17 15:32 [PATCH 0/6] btrfs: find_device cleanups Anand Jain
                   ` (5 preceding siblings ...)
  2019-01-17 15:32 ` [PATCH 6/6] btrfs: refactor btrfs_find_device() return error code Anand Jain
@ 2019-01-18 17:33 ` David Sterba
  2019-01-19  6:54   ` Anand Jain
  6 siblings, 1 reply; 22+ messages in thread
From: David Sterba @ 2019-01-18 17:33 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Thu, Jan 17, 2019 at 11:32:27PM +0800, Anand Jain wrote:
> find_device and its helper functions are as below
> 
>  btrfs_find_device_by_devspec()
>  btrfs_find_device_missing_or_by_path()
>  btrfs_find_device_by_path()
>  btrfs_find_device()
>  find_device()
> 
> Its quite confusing and too fragmented.
> 
> In this patch-set..
>  1/6 and 2/6 -- btrfs_find_device_missing_or_by_path() is collapsed into
>  btrfs_find_device_by_devspec() and cleanup the surviving function.
> 
>  3/6 -- rename btrfs_find_device_by_path() to find_device_by_superblock().
> 
>  4/6 and 5/6 -- collapses find_device() into btrfs_find_device().
> 
>  6/6 refactors btrfs_find_device() to return standard error code.
> 
> Resulting in..
> 
> btrfs_find_device():
>  Mainly used to get struct btrfs_device internally for a given devid
>  and or uuid and also helper function for btrfs_find_device_by_devspec().
> 
> btrfs_find_device_by_devspec() and a helper function find_device_by_superblock():
>  Is mainly to retrieve the struct btrfs_device of a userland given device_path.
> 
> Anand Jain (6):
>   btrfs: merge btrfs_find_device_missing_or_by_path() into parent
>   btrfs: cleanup btrfs_find_device_by_devspec()
>   btrfs: rename btrfs_find_device_by_path()
>   btrfs: refactor btrfs_find_device() take fs_devices as argument
>   btrfs: merge btrfs_find_device() and find_device()
>   btrfs: refactor btrfs_find_device() return error code

1,2,4, applied, please update 5 and resend. Thanks.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 5/6 v2] btrfs: merge btrfs_find_device() and find_device()
  2019-01-17 15:32 ` [PATCH 5/6] btrfs: merge btrfs_find_device() and find_device() Anand Jain
  2019-01-17 15:51   ` David Sterba
@ 2019-01-19  6:48   ` " Anand Jain
  2019-01-23  5:28     ` Anand Jain
  2019-01-28 18:44     ` David Sterba
  1 sibling, 2 replies; 22+ messages in thread
From: Anand Jain @ 2019-01-19  6:48 UTC (permalink / raw)
  To: linux-btrfs

Both btrfs_find_device() and find_device() does the same things expect
that latter function is not keen in seed device in the scan-context. So
merge them.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
v2: use bool instead of int.
    use positive meaning instead of negative.
    conflict fix: due to 1b3922a8bc7 in misc-next.

	if (dev->disk_total_bytes == 0) {
-		dev = find_device(fs_info->fs_devices->seed, devid, NULL);
+		dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
+					NULL, false);
 fs/btrfs/dev-replace.c |  4 +--
 fs/btrfs/ioctl.c       |  4 +--
 fs/btrfs/scrub.c       |  4 +--
 fs/btrfs/volumes.c     | 73 +++++++++++++++++++++++++-------------------------
 fs/btrfs/volumes.h     |  3 ++-
 5 files changed, 44 insertions(+), 44 deletions(-)

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index 6f0fe3623381..975cbf011593 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -112,10 +112,10 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
 		dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
-							src_devid, NULL, NULL);
+							src_devid, NULL, NULL, true);
 		dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
 							BTRFS_DEV_REPLACE_DEVID,
-							NULL, NULL);
+							NULL, NULL, true);
 		/*
 		 * allow 'btrfs dev replace_cancel' if src/tgt device is
 		 * missing
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index fd5f97aeb35c..3f9d7be30bf4 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1642,7 +1642,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
 		btrfs_info(fs_info, "resizing devid %llu", devid);
 	}
 
-	device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
+	device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
 	if (!device) {
 		btrfs_info(fs_info, "resizer unable to find device %llu",
 			   devid);
@@ -3179,7 +3179,7 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
 
 	rcu_read_lock();
 	dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
-				NULL);
+				NULL, true);
 
 	if (!dev) {
 		ret = -ENODEV;
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 72044efc610a..b320910c6740 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -3835,7 +3835,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
 		return PTR_ERR(sctx);
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
-	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
 	if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
 		     !is_dev_replace)) {
 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
@@ -4012,7 +4012,7 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
 	struct scrub_ctx *sctx = NULL;
 
 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
-	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
 	if (dev)
 		sctx = dev->scrub_ctx;
 	if (sctx)
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 5b37044503c0..df1a077aeb80 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -415,27 +415,6 @@ static struct btrfs_device *__alloc_device(void)
 	return dev;
 }
 
-/*
- * 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.
- */
-static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
-		u64 devid, const u8 *uuid)
-{
-	struct btrfs_device *dev;
-
-	list_for_each_entry(dev, &fs_devices->devices, dev_list) {
-		if (dev->devid == devid &&
-		    (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
-			return dev;
-		}
-	}
-	return NULL;
-}
-
 static noinline struct btrfs_fs_devices *find_fsid(
 		const u8 *fsid, const u8 *metadata_fsid)
 {
@@ -990,8 +969,9 @@ static noinline struct btrfs_device *device_list_add(const char *path,
 		device = NULL;
 	} else {
 		mutex_lock(&fs_devices->device_list_mutex);
-		device = find_device(fs_devices, devid,
-				disk_super->dev_item.uuid);
+		device = btrfs_find_device(fs_devices, devid,
+					   disk_super->dev_item.uuid, NULL,
+					   false);
 
 		/*
 		 * If this disk has been pulled into an fs devices created by
@@ -2419,10 +2399,10 @@ static struct btrfs_device *btrfs_find_device_by_path(
 	dev_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);
+					   disk_super->metadata_uuid, true);
 	else
 		device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
-					   disk_super->fsid);
+					   disk_super->fsid, true);
 
 	brelse(bh);
 	if (!device)
@@ -2442,7 +2422,7 @@ struct btrfs_device *btrfs_find_device_by_devspec(
 
 	if (devid) {
 		device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
-					   NULL);
+					   NULL, true);
 		if (!device)
 			return ERR_PTR(-ENOENT);
 		return device;
@@ -2584,7 +2564,7 @@ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
 		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);
+					   fs_uuid, true);
 		BUG_ON(!device); /* Logic error */
 
 		if (device->fs_devices->seeding) {
@@ -6637,19 +6617,36 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
 	return BLK_STS_OK;
 }
 
+/*
+ * 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.
+ *
+ * If seed is true, traverse through the seed devices.
+ */
 struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
-				       u64 devid, u8 *uuid, u8 *fsid)
+				       u64 devid, u8 *uuid, u8 *fsid,
+				       bool seed)
 {
 	struct btrfs_device *device;
 
 	while (fs_devices) {
 		if (!fsid ||
 		    !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
-			device = find_device(fs_devices, devid, uuid);
-			if (device)
-				return device;
+			list_for_each_entry(device, &fs_devices->devices,
+					    dev_list) {
+				if (device->devid == devid && (!uuid ||
+				    !memcmp(device->uuid, uuid,
+				    BTRFS_UUID_SIZE)))
+					return device;
+			}
 		}
-		fs_devices = fs_devices->seed;
+		if (seed)
+			fs_devices = fs_devices->seed;
+		else
+			return NULL;
 	}
 	return NULL;
 }
@@ -6895,7 +6892,7 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
 				   btrfs_stripe_dev_uuid_nr(chunk, i),
 				   BTRFS_UUID_SIZE);
 		map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
-							devid, uuid, NULL);
+							devid, uuid, NULL, true);
 		if (!map->stripes[i].dev &&
 		    !btrfs_test_opt(fs_info, DEGRADED)) {
 			free_extent_map(em);
@@ -7035,7 +7032,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
 	}
 
 	device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
-				   fs_uuid);
+				   fs_uuid, true);
 	if (!device) {
 		if (!btrfs_test_opt(fs_info, DEGRADED)) {
 			btrfs_report_missing_device(fs_info, devid,
@@ -7625,7 +7622,8 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
 	int i;
 
 	mutex_lock(&fs_devices->device_list_mutex);
-	dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
+				true);
 	mutex_unlock(&fs_devices->device_list_mutex);
 
 	if (!dev) {
@@ -7839,7 +7837,7 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
 	}
 
 	/* Make sure no dev extent is beyond device bondary */
-	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
+	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
 	if (!dev) {
 		btrfs_err(fs_info, "failed to find devid %llu", devid);
 		ret = -EUCLEAN;
@@ -7848,7 +7846,8 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
 
 	/* It's possible this device is a dummy for seed device */
 	if (dev->disk_total_bytes == 0) {
-		dev = find_device(fs_info->fs_devices->seed, devid, NULL);
+		dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
+					NULL, false);
 		if (!dev) {
 			btrfs_err(fs_info, "failed to find seed devid %llu",
 				  devid);
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 46ee8eafdc92..c8ddcf4ae1f2 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -435,7 +435,8 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info,
 int btrfs_grow_device(struct btrfs_trans_handle *trans,
 		      struct btrfs_device *device, u64 new_size);
 struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
-				       u64 devid, u8 *uuid, u8 *fsid);
+				       u64 devid, u8 *uuid, u8 *fsid,
+				       bool seed);
 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
 int btrfs_balance(struct btrfs_fs_info *fs_info,
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 0/6] btrfs: find_device cleanups
  2019-01-18 17:33 ` [PATCH 0/6] btrfs: find_device cleanups David Sterba
@ 2019-01-19  6:54   ` Anand Jain
  0 siblings, 0 replies; 22+ messages in thread
From: Anand Jain @ 2019-01-19  6:54 UTC (permalink / raw)
  To: dsterba; +Cc: linux-btrfs



On 01/19/2019 01:33 AM, David Sterba wrote:
> On Thu, Jan 17, 2019 at 11:32:27PM +0800, Anand Jain wrote:
>> find_device and its helper functions are as below
>>
>>   btrfs_find_device_by_devspec()
>>   btrfs_find_device_missing_or_by_path()
>>   btrfs_find_device_by_path()
>>   btrfs_find_device()
>>   find_device()
>>
>> Its quite confusing and too fragmented.
>>
>> In this patch-set..
>>   1/6 and 2/6 -- btrfs_find_device_missing_or_by_path() is collapsed into
>>   btrfs_find_device_by_devspec() and cleanup the surviving function.
>>
>>   3/6 -- rename btrfs_find_device_by_path() to find_device_by_superblock().
>>
>>   4/6 and 5/6 -- collapses find_device() into btrfs_find_device().
>>
>>   6/6 refactors btrfs_find_device() to return standard error code.
>>
>> Resulting in..
>>
>> btrfs_find_device():
>>   Mainly used to get struct btrfs_device internally for a given devid
>>   and or uuid and also helper function for btrfs_find_device_by_devspec().
>>
>> btrfs_find_device_by_devspec() and a helper function find_device_by_superblock():
>>   Is mainly to retrieve the struct btrfs_device of a userland given device_path.
>>
>> Anand Jain (6):
>>    btrfs: merge btrfs_find_device_missing_or_by_path() into parent
>>    btrfs: cleanup btrfs_find_device_by_devspec()
>>    btrfs: rename btrfs_find_device_by_path()
>>    btrfs: refactor btrfs_find_device() take fs_devices as argument
>>    btrfs: merge btrfs_find_device() and find_device()
>>    btrfs: refactor btrfs_find_device() return error code
> 
> 1,2,4, applied,

  Thanks

> please update 5 and resend. Thanks.

  comments were fixed and v2 is in the ML.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 5/6 v2] btrfs: merge btrfs_find_device() and find_device()
  2019-01-19  6:48   ` [PATCH 5/6 v2] " Anand Jain
@ 2019-01-23  5:28     ` Anand Jain
  2019-01-28 18:44     ` David Sterba
  1 sibling, 0 replies; 22+ messages in thread
From: Anand Jain @ 2019-01-23  5:28 UTC (permalink / raw)
  To: linux-btrfs, dsterba



On 01/19/2019 02:48 PM, Anand Jain wrote:
> Both btrfs_find_device() and find_device() does the same things expect
> that latter function is not keen in seed device in the scan-context. So
> merge them.
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>

    A gentle ping.

Thanks, Anand

> ---
> v2: use bool instead of int.
>      use positive meaning instead of negative.
>      conflict fix: due to 1b3922a8bc7 in misc-next.
> 
> 	if (dev->disk_total_bytes == 0) {
> -		dev = find_device(fs_info->fs_devices->seed, devid, NULL);
> +		dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
> +					NULL, false);
>   fs/btrfs/dev-replace.c |  4 +--
>   fs/btrfs/ioctl.c       |  4 +--
>   fs/btrfs/scrub.c       |  4 +--
>   fs/btrfs/volumes.c     | 73 +++++++++++++++++++++++++-------------------------
>   fs/btrfs/volumes.h     |  3 ++-
>   5 files changed, 44 insertions(+), 44 deletions(-)
> 
> diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
> index 6f0fe3623381..975cbf011593 100644
> --- a/fs/btrfs/dev-replace.c
> +++ b/fs/btrfs/dev-replace.c
> @@ -112,10 +112,10 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
>   	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
>   	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
>   		dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
> -							src_devid, NULL, NULL);
> +							src_devid, NULL, NULL, true);
>   		dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
>   							BTRFS_DEV_REPLACE_DEVID,
> -							NULL, NULL);
> +							NULL, NULL, true);
>   		/*
>   		 * allow 'btrfs dev replace_cancel' if src/tgt device is
>   		 * missing
> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
> index fd5f97aeb35c..3f9d7be30bf4 100644
> --- a/fs/btrfs/ioctl.c
> +++ b/fs/btrfs/ioctl.c
> @@ -1642,7 +1642,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
>   		btrfs_info(fs_info, "resizing devid %llu", devid);
>   	}
>   
> -	device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
> +	device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
>   	if (!device) {
>   		btrfs_info(fs_info, "resizer unable to find device %llu",
>   			   devid);
> @@ -3179,7 +3179,7 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
>   
>   	rcu_read_lock();
>   	dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
> -				NULL);
> +				NULL, true);
>   
>   	if (!dev) {
>   		ret = -ENODEV;
> diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
> index 72044efc610a..b320910c6740 100644
> --- a/fs/btrfs/scrub.c
> +++ b/fs/btrfs/scrub.c
> @@ -3835,7 +3835,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
>   		return PTR_ERR(sctx);
>   
>   	mutex_lock(&fs_info->fs_devices->device_list_mutex);
> -	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
> +	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
>   	if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
>   		     !is_dev_replace)) {
>   		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
> @@ -4012,7 +4012,7 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
>   	struct scrub_ctx *sctx = NULL;
>   
>   	mutex_lock(&fs_info->fs_devices->device_list_mutex);
> -	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
> +	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
>   	if (dev)
>   		sctx = dev->scrub_ctx;
>   	if (sctx)
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index 5b37044503c0..df1a077aeb80 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -415,27 +415,6 @@ static struct btrfs_device *__alloc_device(void)
>   	return dev;
>   }
>   
> -/*
> - * 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.
> - */
> -static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
> -		u64 devid, const u8 *uuid)
> -{
> -	struct btrfs_device *dev;
> -
> -	list_for_each_entry(dev, &fs_devices->devices, dev_list) {
> -		if (dev->devid == devid &&
> -		    (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
> -			return dev;
> -		}
> -	}
> -	return NULL;
> -}
> -
>   static noinline struct btrfs_fs_devices *find_fsid(
>   		const u8 *fsid, const u8 *metadata_fsid)
>   {
> @@ -990,8 +969,9 @@ static noinline struct btrfs_device *device_list_add(const char *path,
>   		device = NULL;
>   	} else {
>   		mutex_lock(&fs_devices->device_list_mutex);
> -		device = find_device(fs_devices, devid,
> -				disk_super->dev_item.uuid);
> +		device = btrfs_find_device(fs_devices, devid,
> +					   disk_super->dev_item.uuid, NULL,
> +					   false);
>   
>   		/*
>   		 * If this disk has been pulled into an fs devices created by
> @@ -2419,10 +2399,10 @@ static struct btrfs_device *btrfs_find_device_by_path(
>   	dev_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);
> +					   disk_super->metadata_uuid, true);
>   	else
>   		device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
> -					   disk_super->fsid);
> +					   disk_super->fsid, true);
>   
>   	brelse(bh);
>   	if (!device)
> @@ -2442,7 +2422,7 @@ struct btrfs_device *btrfs_find_device_by_devspec(
>   
>   	if (devid) {
>   		device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
> -					   NULL);
> +					   NULL, true);
>   		if (!device)
>   			return ERR_PTR(-ENOENT);
>   		return device;
> @@ -2584,7 +2564,7 @@ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
>   		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);
> +					   fs_uuid, true);
>   		BUG_ON(!device); /* Logic error */
>   
>   		if (device->fs_devices->seeding) {
> @@ -6637,19 +6617,36 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
>   	return BLK_STS_OK;
>   }
>   
> +/*
> + * 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.
> + *
> + * If seed is true, traverse through the seed devices.
> + */
>   struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
> -				       u64 devid, u8 *uuid, u8 *fsid)
> +				       u64 devid, u8 *uuid, u8 *fsid,
> +				       bool seed)
>   {
>   	struct btrfs_device *device;
>   
>   	while (fs_devices) {
>   		if (!fsid ||
>   		    !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
> -			device = find_device(fs_devices, devid, uuid);
> -			if (device)
> -				return device;
> +			list_for_each_entry(device, &fs_devices->devices,
> +					    dev_list) {
> +				if (device->devid == devid && (!uuid ||
> +				    !memcmp(device->uuid, uuid,
> +				    BTRFS_UUID_SIZE)))
> +					return device;
> +			}
>   		}
> -		fs_devices = fs_devices->seed;
> +		if (seed)
> +			fs_devices = fs_devices->seed;
> +		else
> +			return NULL;
>   	}
>   	return NULL;
>   }
> @@ -6895,7 +6892,7 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
>   				   btrfs_stripe_dev_uuid_nr(chunk, i),
>   				   BTRFS_UUID_SIZE);
>   		map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
> -							devid, uuid, NULL);
> +							devid, uuid, NULL, true);
>   		if (!map->stripes[i].dev &&
>   		    !btrfs_test_opt(fs_info, DEGRADED)) {
>   			free_extent_map(em);
> @@ -7035,7 +7032,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
>   	}
>   
>   	device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
> -				   fs_uuid);
> +				   fs_uuid, true);
>   	if (!device) {
>   		if (!btrfs_test_opt(fs_info, DEGRADED)) {
>   			btrfs_report_missing_device(fs_info, devid,
> @@ -7625,7 +7622,8 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
>   	int i;
>   
>   	mutex_lock(&fs_devices->device_list_mutex);
> -	dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL);
> +	dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
> +				true);
>   	mutex_unlock(&fs_devices->device_list_mutex);
>   
>   	if (!dev) {
> @@ -7839,7 +7837,7 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
>   	}
>   
>   	/* Make sure no dev extent is beyond device bondary */
> -	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
> +	dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
>   	if (!dev) {
>   		btrfs_err(fs_info, "failed to find devid %llu", devid);
>   		ret = -EUCLEAN;
> @@ -7848,7 +7846,8 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
>   
>   	/* It's possible this device is a dummy for seed device */
>   	if (dev->disk_total_bytes == 0) {
> -		dev = find_device(fs_info->fs_devices->seed, devid, NULL);
> +		dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
> +					NULL, false);
>   		if (!dev) {
>   			btrfs_err(fs_info, "failed to find seed devid %llu",
>   				  devid);
> diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
> index 46ee8eafdc92..c8ddcf4ae1f2 100644
> --- a/fs/btrfs/volumes.h
> +++ b/fs/btrfs/volumes.h
> @@ -435,7 +435,8 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info,
>   int btrfs_grow_device(struct btrfs_trans_handle *trans,
>   		      struct btrfs_device *device, u64 new_size);
>   struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
> -				       u64 devid, u8 *uuid, u8 *fsid);
> +				       u64 devid, u8 *uuid, u8 *fsid,
> +				       bool seed);
>   int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
>   int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
>   int btrfs_balance(struct btrfs_fs_info *fs_info,
> 

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 5/6 v2] btrfs: merge btrfs_find_device() and find_device()
  2019-01-19  6:48   ` [PATCH 5/6 v2] " Anand Jain
  2019-01-23  5:28     ` Anand Jain
@ 2019-01-28 18:44     ` David Sterba
  1 sibling, 0 replies; 22+ messages in thread
From: David Sterba @ 2019-01-28 18:44 UTC (permalink / raw)
  To: Anand Jain; +Cc: linux-btrfs

On Sat, Jan 19, 2019 at 02:48:55PM +0800, Anand Jain wrote:
> Both btrfs_find_device() and find_device() does the same things expect
> that latter function is not keen in seed device in the scan-context. So
> merge them.
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>

I can't find a reply that this has been merged, but it is, in misc-next,
thanks.

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, back to index

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-17 15:32 [PATCH 0/6] btrfs: find_device cleanups Anand Jain
2019-01-17 15:32 ` [PATCH 1/6] btrfs: merge btrfs_find_device_missing_or_by_path() into parent Anand Jain
2019-01-17 15:57   ` David Sterba
2019-01-17 15:32 ` [PATCH 2/6] btrfs: cleanup btrfs_find_device_by_devspec() Anand Jain
2019-01-17 15:57   ` David Sterba
2019-01-17 15:32 ` [PATCH 3/6] btrfs: rename btrfs_find_device_by_path() Anand Jain
2019-01-17 15:54   ` David Sterba
2019-01-18  6:13     ` Anand Jain
2019-01-18 17:05       ` David Sterba
2019-01-17 15:32 ` [PATCH 4/6] btrfs: refactor btrfs_find_device() take fs_devices as argument Anand Jain
2019-01-17 15:58   ` David Sterba
2019-01-17 15:32 ` [PATCH 5/6] btrfs: merge btrfs_find_device() and find_device() Anand Jain
2019-01-17 15:51   ` David Sterba
2019-01-19  6:48   ` [PATCH 5/6 v2] " Anand Jain
2019-01-23  5:28     ` Anand Jain
2019-01-28 18:44     ` David Sterba
2019-01-17 15:32 ` [PATCH 6/6] btrfs: refactor btrfs_find_device() return error code Anand Jain
2019-01-17 15:49   ` David Sterba
2019-01-18  6:13     ` Anand Jain
2019-01-18 17:20       ` David Sterba
2019-01-18 17:33 ` [PATCH 0/6] btrfs: find_device cleanups David Sterba
2019-01-19  6:54   ` Anand Jain

Linux-BTRFS Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-btrfs/0 linux-btrfs/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-btrfs linux-btrfs/ https://lore.kernel.org/linux-btrfs \
		linux-btrfs@vger.kernel.org linux-btrfs@archiver.kernel.org
	public-inbox-index linux-btrfs


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-btrfs


AGPL code for this site: git clone https://public-inbox.org/ public-inbox