Skip to contentSkip to conversation

btrfs: make code easy to read in btrfs_open_one_device()

PATCH Superseded Anand Jain 2 participants 7 messages Dec 15, 2017 – Jan 22, 2018 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
author
#1 lore raw

PATCH 2/3 btrfs: optimize move uuid_mutex closer to the critical section

In reply to Anand Jain

Move uuid_mutex closer to the exclusion section.

---
 fs/btrfs/volumes.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
fs/btrfs/volumes.c +8 -9
index a81574dba124..a7eb9b42cd92 100644
@@ -1187,13 +1187,10 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 	 */
 	bytenr = btrfs_sb_offset(0);
 	flags |= FMODE_EXCL;
-	mutex_lock(&uuid_mutex);
 
 	bdev = blkdev_get_by_path(path, flags, holder);
-	if (IS_ERR(bdev)) {
-		ret = PTR_ERR(bdev);
-		goto error;
-	}
+	if (IS_ERR(bdev))
+		return PTR_ERR(bdev);
 
 	if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super))
 		goto error_bdev_put;
@@ -1202,7 +1199,12 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 	transid = btrfs_super_generation(disk_super);
 	total_devices = btrfs_super_num_devices(disk_super);
 
+	mutex_lock(&uuid_mutex);
 	ret = device_list_add(path, disk_super, devid, fs_devices_ret);
+	if (ret >= 0 && fs_devices_ret)
+		(*fs_devices_ret)->total_devices = total_devices;
+	mutex_unlock(&uuid_mutex);
+
 	if (ret > 0) {
 		if (disk_super->label[0]) {
 			pr_info("BTRFS: device label %s ", disk_super->label);
@@ -1213,15 +1215,12 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 		pr_cont("devid %llu transid %llu %s\n", devid, transid, path);
 		ret = 0;
 	}
-	if (!ret && fs_devices_ret)
-		(*fs_devices_ret)->total_devices = total_devices;
 
 	btrfs_release_disk_super(page);
 
 error_bdev_put:
 	blkdev_put(bdev, flags);
-error:
-	mutex_unlock(&uuid_mutex);
+
 	return ret;
 }
 
Signature · 1 line
2.7.0
author
#3 lore raw

PATCH 3/3 btrfs: misc cleanup btrfs_scan_one_device()

In reply to Anand Jain

Assign ret = -EINVAL where actually its required.
Remove { } around single line if else code.

---
 fs/btrfs/volumes.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
fs/btrfs/volumes.c +6 -5
index a7eb9b42cd92..ba06a4ccd5a6 100644
@@ -1173,7 +1173,7 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 	struct btrfs_super_block *disk_super;
 	struct block_device *bdev;
 	struct page *page;
-	int ret = -EINVAL;
+	int ret;
 	u64 devid;
 	u64 transid;
 	u64 total_devices;
@@ -1192,8 +1192,10 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 	if (IS_ERR(bdev))
 		return PTR_ERR(bdev);
 
-	if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super))
+	if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
+		ret = -EINVAL;
 		goto error_bdev_put;
+	}
 
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	transid = btrfs_super_generation(disk_super);
@@ -1206,11 +1208,10 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 	mutex_unlock(&uuid_mutex);
 
 	if (ret > 0) {
-		if (disk_super->label[0]) {
+		if (disk_super->label[0])
 			pr_info("BTRFS: device label %s ", disk_super->label);
-		} else {
+		else
 			pr_info("BTRFS: device fsid %pU ", disk_super->fsid);
-		}
 
 		pr_cont("devid %llu transid %llu %s\n", devid, transid, path);
 		ret = 0;
Signature · 1 line
2.7.0

Anand Jain

author
#0 lore raw

PATCH 1/3 btrfs: make code easy to read in btrfs_open_one_device()

No functional change. First set the usual case, writeable then check for any special config.

---
 fs/btrfs/volumes.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
fs/btrfs/volumes.c +3 -5
index 5a4c30451c7f..a81574dba124 100644
@@ -676,14 +676,12 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 
 	device->generation = btrfs_super_generation(disk_super);
 
+	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
 		clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 		fs_devices->seeding = 1;
-	} else {
-		if (bdev_read_only(bdev))
-			clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
-		else
-			set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
+	} else if (bdev_read_only(bdev)) {
+		clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 	}
 
 	q = bdev_get_queue(bdev);
Signature · 1 line
2.7.0
#5 lore raw
Quoted 13 lines · Anand Jain

No functional change. First set the usual case, writeable then check for any special config.

---
 fs/btrfs/volumes.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
fs/btrfs/volumes.c +0 -0
index 5a4c30451c7f..a81574dba124 100644

fs/btrfs/volumes.c static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, · line 679

Quoted 4 lines
fs/btrfs/volumes.c +1 -0
@@ -676,14 +676,12 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 
 	device->generation = btrfs_super_generation(disk_super);
 
+	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);

I would not say there's no functional change. This line will unconditionally set the writeable flag, but this was not the case before.

Sure it's dropped a few lines below, but this would need some checking that it's not a problem. btrfs_open_one_device is indirectly called from mount so it should be safe (we can't use one device twice), but this needs to be documented.

Quoted 20 lines
fs/btrfs/volumes.c +2 -5
 	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
 		clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 		fs_devices->seeding = 1;
-	} else {
-		if (bdev_read_only(bdev))
-			clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
-		else
-			set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
+	} else if (bdev_read_only(bdev)) {
+		clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
 	}
 
 	q = bdev_get_queue(bdev);
Signature · 6 lines
2.7.0

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] btrfs: optimize move uuid_mutex closer to the critical section

In reply to Anand Jain
Quoted 1 line · Anand Jain

Move uuid_mutex closer to the exclusion section.

Looks good, there's really something unrelated inside the critical section so this could potentially speed up scanning devices.

author
#6 lore raw
In reply to David Sterba
Quoted 15 lines · David Sterba
Quoted 14 lines · Anand Jain

No functional change. First set the usual case, writeable then check for any special config.

---
  fs/btrfs/volumes.c | 8 +++-----
  1 file changed, 3 insertions(+), 5 deletions(-)
fs/btrfs/volumes.c +0 -0
index 5a4c30451c7f..a81574dba124 100644
@@ -676,14 +676,12 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
 
Quoted 7 lines

fs/btrfs/volumes.c static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, · line 679

Quoted 3 lines
fs/btrfs/volumes.c +1 -0
  	device->generation = btrfs_super_generation(disk_super);
 
+	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);

I would not say there's no functional change. This line will unconditionally set the writeable flag, but this was not the case before.

(Sorry for the delay, got distracted by other patches).

agreed here.

Quoted 4 lines

Sure it's dropped a few lines below, but this would need some checking that it's not a problem. btrfs_open_one_device is indirectly called from mount so it should be safe (we can't use one device twice), but this needs to be documented.

Sure will do.

Thanks, Anand

Quoted 20 lines
Quoted 20 lines
fs/btrfs/volumes.c +2 -5
  	if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
  		clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
  		fs_devices->seeding = 1;
-	} else {
-		if (bdev_read_only(bdev))
-			clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
-		else
-			set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
+	} else if (bdev_read_only(bdev)) {
+		clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
  	}
 
  	q = bdev_get_queue(bdev);
Signature · 6 lines
2.7.0

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