linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Replace obscure constructs in fs/block_dev.c
@ 2007-06-15 13:46 Johannes Weiner
  2007-06-17  9:38 ` Björn Steinbrink
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Weiner @ 2007-06-15 13:46 UTC (permalink / raw)
  To: Linux Kernel Mailing List; +Cc: Andrew Morton

[-- Attachment #1: Type: text/plain, Size: 270 bytes --]

Hi,

Andrew, I promised it [1], here goes. Patched against Linus' git-tree.

[1] http://lkml.org/lkml/2007/3/22/226

This patch replaces some obscure code-paths in fs/block_dev.c with more
readable versions.

Signed-off-by: Johannes Weiner <hannes-kernel@saeurebad.de>


[-- Attachment #2: block_dev-anti-obscurity.patch --]
[-- Type: text/plain, Size: 3065 bytes --]

diff --git a/fs/block_dev.c b/fs/block_dev.c
index ea1480a..1ffbb6d 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -660,26 +660,16 @@ void bd_forget(struct inode *inode)
 
 int bd_claim(struct block_device *bdev, void *holder)
 {
-	int res;
+	int res = 0;
 	spin_lock(&bdev_lock);
 
-	/* first decide result */
-	if (bdev->bd_holder == holder)
-		res = 0;	 /* already a holder */
-	else if (bdev->bd_holder != NULL)
-		res = -EBUSY; 	 /* held by someone else */
-	else if (bdev->bd_contains == bdev)
-		res = 0;  	 /* is a whole device which isn't held */
-
-	else if (bdev->bd_contains->bd_holder == bd_claim)
-		res = 0; 	 /* is a partition of a device that is being partitioned */
-	else if (bdev->bd_contains->bd_holder != NULL)
-		res = -EBUSY;	 /* is a partition of a held device */
-	else
-		res = 0;	 /* is a partition of an un-held device */
+	/* If already held by someone else or we have a
+	 * partition of a held device, we do nothing. */
 
-	/* now impose change */
-	if (res==0) {
+	if (bdev->bd_holder || bdev->bd_contains->bd_holder)
+		res = -EBUSY;
+
+	if (!res) {
 		/* note that for a whole device bd_holders
 		 * will be incremented twice, and bd_holder will
 		 * be set to bd_claim before being set to holder
@@ -874,7 +864,7 @@ static struct bd_holder *find_bd_holder(struct block_device *bdev,
  */
 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
 {
-	int ret;
+	int err;
 
 	if (!bo)
 		return -EINVAL;
@@ -882,15 +872,18 @@ static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
 	if (!bd_holder_grab_dirs(bdev, bo))
 		return -EBUSY;
 
-	ret = add_symlink(bo->sdir, bo->sdev);
-	if (ret == 0) {
-		ret = add_symlink(bo->hdir, bo->hdev);
-		if (ret)
-			del_symlink(bo->sdir, bo->sdev);
+	err = add_symlink(bo->sdir, bo->sdev);
+	if (err)
+		return err;
+
+	err = add_symlink(bo->hdir, bo->hdev);
+	if (err) {
+		del_symlink(bo->sdir, bo->sdev);
+		return err;
 	}
-	if (ret == 0)
-		list_add_tail(&bo->list, &bdev->bd_holder_list);
-	return ret;
+		
+	list_add_tail(&bo->list, &bdev->bd_holder_list);
+	return 0;
 }
 
 /**
@@ -948,7 +941,7 @@ static struct bd_holder *del_bd_holder(struct block_device *bdev,
 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
 				struct kobject *kobj)
 {
-	int res;
+	int err;
 	struct bd_holder *bo, *found;
 
 	if (!kobj)
@@ -959,21 +952,25 @@ static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
 		return -ENOMEM;
 
 	mutex_lock(&bdev->bd_mutex);
-	res = bd_claim(bdev, holder);
-	if (res == 0) {
-		found = find_bd_holder(bdev, bo);
-		if (found == NULL) {
-			res = add_bd_holder(bdev, bo);
-			if (res)
-				bd_release(bdev);
-		}
-	}
 
-	if (res || found)
+	err = bd_claim(bdev, holder);
+	if (err)
+		goto out;
+
+	found = find_bd_holder(bdev, bo);
+	if (found)
+		goto out;
+
+	err = add_bd_holder(bdev, bo);
+	if (err)
+		bd_release(bdev);
+
+out:
+	if (err || found)
 		free_bd_holder(bo);
-	mutex_unlock(&bdev->bd_mutex);
 
-	return res;
+	mutex_unlock(&bdev->bd_mutex);
+	return err;
 }
 
 /**

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

* Re: [PATCH] Replace obscure constructs in fs/block_dev.c
  2007-06-15 13:46 [PATCH] Replace obscure constructs in fs/block_dev.c Johannes Weiner
@ 2007-06-17  9:38 ` Björn Steinbrink
  2007-06-17 12:52   ` Johannes Weiner
  2007-06-17 15:42   ` Johannes Weiner
  0 siblings, 2 replies; 4+ messages in thread
From: Björn Steinbrink @ 2007-06-17  9:38 UTC (permalink / raw)
  To: Linux Kernel Mailing List, Andrew Morton

On 2007.06.15 15:46:32 +0200, Johannes Weiner wrote:
> Hi,
> 
> Andrew, I promised it [1], here goes. Patched against Linus' git-tree.
> 
> [1] http://lkml.org/lkml/2007/3/22/226
> 
> This patch replaces some obscure code-paths in fs/block_dev.c with more
> readable versions.
> 
> Signed-off-by: Johannes Weiner <hannes-kernel@saeurebad.de>
> 

> diff --git a/fs/block_dev.c b/fs/block_dev.c
> index ea1480a..1ffbb6d 100644
> --- a/fs/block_dev.c
> +++ b/fs/block_dev.c
> @@ -660,26 +660,16 @@ void bd_forget(struct inode *inode)
>  
>  int bd_claim(struct block_device *bdev, void *holder)
>  {
> -	int res;
> +	int res = 0;
>  	spin_lock(&bdev_lock);
>  
> -	/* first decide result */
> -	if (bdev->bd_holder == holder)
> -		res = 0;	 /* already a holder */
> -	else if (bdev->bd_holder != NULL)
> -		res = -EBUSY; 	 /* held by someone else */
> -	else if (bdev->bd_contains == bdev)
> -		res = 0;  	 /* is a whole device which isn't held */
> -
> -	else if (bdev->bd_contains->bd_holder == bd_claim)
> -		res = 0; 	 /* is a partition of a device that is being partitioned */
> -	else if (bdev->bd_contains->bd_holder != NULL)
> -		res = -EBUSY;	 /* is a partition of a held device */
> -	else
> -		res = 0;	 /* is a partition of an un-held device */
> +	/* If already held by someone else or we have a
> +	 * partition of a held device, we do nothing. */
>  
> -	/* now impose change */
> -	if (res==0) {
> +	if (bdev->bd_holder || bdev->bd_contains->bd_holder)
> +		res = -EBUSY;

Hm, that actually ignores the "someone else" part of the comment and
introduces a semantic change. No idea, if it breaks anything though.

Björn

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

* Re: [PATCH] Replace obscure constructs in fs/block_dev.c
  2007-06-17  9:38 ` Björn Steinbrink
@ 2007-06-17 12:52   ` Johannes Weiner
  2007-06-17 15:42   ` Johannes Weiner
  1 sibling, 0 replies; 4+ messages in thread
From: Johannes Weiner @ 2007-06-17 12:52 UTC (permalink / raw)
  To: Linux Kernel Mailing List; +Cc: Bj?rn Steinbrink, Andrew Morton

[-- Attachment #1: Type: text/plain, Size: 1426 bytes --]

Hi,

On Sun, Jun 17, 2007 at 11:38:55AM +0200, Bjoern Steinbrink wrote:
[SNIP]
> > -	/* first decide result */
> > -	if (bdev->bd_holder == holder)
> > -		res = 0;	 /* already a holder */
> > -	else if (bdev->bd_holder != NULL)
> > -		res = -EBUSY; 	 /* held by someone else */
> > -	else if (bdev->bd_contains == bdev)
> > -		res = 0;  	 /* is a whole device which isn't held */
> > -
> > -	else if (bdev->bd_contains->bd_holder == bd_claim)
> > -		res = 0; 	 /* is a partition of a device that is being partitioned */
> > -	else if (bdev->bd_contains->bd_holder != NULL)
> > -		res = -EBUSY;	 /* is a partition of a held device */
> > -	else
> > -		res = 0;	 /* is a partition of an un-held device */
> > +	/* If already held by someone else or we have a
> > +	 * partition of a held device, we do nothing. */
> >  
> > -	/* now impose change */
> > -	if (res==0) {
> > +	if (bdev->bd_holder || bdev->bd_contains->bd_holder)
> > +		res = -EBUSY;
[SNAP]

> Hm, that actually ignores the "someone else" part of the comment and
> introduces a semantic change. No idea, if it breaks anything though.

Whoops, yes. ->bd_holder can be actually !NULL when it is == holder.
Same for ->bd_contains->bd_holder. Also the `is a whole unheld device'
was ignored - sorry. Second try:


This patch replaces some obscure code-paths in fs/block_dev.c with more
readable versions.

Signed-off-by: Johannes Weiner <hannes-kernel@saeurebad.de>

[-- Attachment #2: block_dev-anti-obscurity-r1.patch --]
[-- Type: text/plain, Size: 2710 bytes --]

diff --git a/fs/block_dev.c b/fs/block_dev.c
index ea1480a..3a49adb 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -660,37 +660,32 @@ void bd_forget(struct inode *inode)
 
 int bd_claim(struct block_device *bdev, void *holder)
 {
-	int res;
+	void *bd_holder;
+	struct block_device *bd_contains;
+
 	spin_lock(&bdev_lock);
 
-	/* first decide result */
-	if (bdev->bd_holder == holder)
-		res = 0;	 /* already a holder */
-	else if (bdev->bd_holder != NULL)
-		res = -EBUSY; 	 /* held by someone else */
-	else if (bdev->bd_contains == bdev)
-		res = 0;  	 /* is a whole device which isn't held */
-
-	else if (bdev->bd_contains->bd_holder == bd_claim)
-		res = 0; 	 /* is a partition of a device that is being partitioned */
-	else if (bdev->bd_contains->bd_holder != NULL)
-		res = -EBUSY;	 /* is a partition of a held device */
-	else
-		res = 0;	 /* is a partition of an un-held device */
+	bd_holder = bdev->bd_holder;
+	bd_contains = bdev->bd_contains;
 
-	/* now impose change */
-	if (res==0) {
-		/* note that for a whole device bd_holders
-		 * will be incremented twice, and bd_holder will
-		 * be set to bd_claim before being set to holder
-		 */
-		bdev->bd_contains->bd_holders ++;
-		bdev->bd_contains->bd_holder = bd_claim;
-		bdev->bd_holders++;
-		bdev->bd_holder = holder;
+	if ((bd_holder && bd_holder != holder) ||
+	    (bd_contains != bdev &&
+	     bd_contains->bd_holder && bd_contains->bd_holder != bd_claim)) {
+		spin_unlock(&bdev_lock);
+		return -EBUSY;
 	}
+
+	/* note that for a whole device bd_holders
+	 * will be incremented twice, and bd_holder will
+	 * be set to bd_claim before being set to holder
+	 */
+	bdev->bd_contains->bd_holders ++;
+	bdev->bd_contains->bd_holder = bd_claim;
+	bdev->bd_holders++;
+	bdev->bd_holder = holder;
+
 	spin_unlock(&bdev_lock);
-	return res;
+	return 0;
 }
 
 EXPORT_SYMBOL(bd_claim);
@@ -874,7 +869,7 @@ static struct bd_holder *find_bd_holder(struct block_device *bdev,
  */
 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
 {
-	int ret;
+	int err;
 
 	if (!bo)
 		return -EINVAL;
@@ -882,14 +877,17 @@ static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
 	if (!bd_holder_grab_dirs(bdev, bo))
 		return -EBUSY;
 
-	ret = add_symlink(bo->sdir, bo->sdev);
-	if (ret == 0) {
-		ret = add_symlink(bo->hdir, bo->hdev);
-		if (ret)
-			del_symlink(bo->sdir, bo->sdev);
+	err = add_symlink(bo->sdir, bo->sdev);
+	if (err)
+		return err;
+
+	err = add_symlink(bo->hdir, bo->hdev);
+	if (err) {
+		del_symlink(bo->sdir, bo->sdev);
+		return err;
 	}
-	if (ret == 0)
-		list_add_tail(&bo->list, &bdev->bd_holder_list);
+
+	list_add_tail(&bo->list, &bdev->bd_holder_list);
 	return ret;
 }
 

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

* Re: [PATCH] Replace obscure constructs in fs/block_dev.c
  2007-06-17  9:38 ` Björn Steinbrink
  2007-06-17 12:52   ` Johannes Weiner
@ 2007-06-17 15:42   ` Johannes Weiner
  1 sibling, 0 replies; 4+ messages in thread
From: Johannes Weiner @ 2007-06-17 15:42 UTC (permalink / raw)
  To: Linux Kernel Mailing List; +Cc: Björn Steinbrink, Andrew Morton

[-- Attachment #1: Type: text/plain, Size: 385 bytes --]

Hi,

please ignore the last revision of this patch. There were some places where
semantics where changed instead of just presentation.  Also the bd_claim()
'fixup' was reverted, due to a misunderstanding of the code.


This patch replaces some funky codepaths in fs/block_dev.c with cleaner
versions of the affected places.

Signed-off-by: Johannes Weiner <hannes-kernel@saeurebad.de>

[-- Attachment #2: block_dev-anti-obscurity-r2.patch --]
[-- Type: text/x-diff, Size: 1910 bytes --]

diff --git a/fs/block_dev.c b/fs/block_dev.c
index ea1480a..710de2f 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -874,7 +874,7 @@ static struct bd_holder *find_bd_holder(struct block_device *bdev,
  */
 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
 {
-	int ret;
+	int err;
 
 	if (!bo)
 		return -EINVAL;
@@ -882,15 +882,18 @@ static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
 	if (!bd_holder_grab_dirs(bdev, bo))
 		return -EBUSY;
 
-	ret = add_symlink(bo->sdir, bo->sdev);
-	if (ret == 0) {
-		ret = add_symlink(bo->hdir, bo->hdev);
-		if (ret)
-			del_symlink(bo->sdir, bo->sdev);
+	err = add_symlink(bo->sdir, bo->sdev);
+	if (err)
+		return err;
+
+	err = add_symlink(bo->hdir, bo->hdev);
+	if (err) {
+		del_symlink(bo->sdir, bo->sdev);
+		return err;
 	}
-	if (ret == 0)
-		list_add_tail(&bo->list, &bdev->bd_holder_list);
-	return ret;
+
+	list_add_tail(&bo->list, &bdev->bd_holder_list);
+	return err;
 }
 
 /**
@@ -948,7 +951,7 @@ static struct bd_holder *del_bd_holder(struct block_device *bdev,
 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
 				struct kobject *kobj)
 {
-	int res;
+	int err;
 	struct bd_holder *bo, *found;
 
 	if (!kobj)
@@ -959,21 +962,24 @@ static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
 		return -ENOMEM;
 
 	mutex_lock(&bdev->bd_mutex);
-	res = bd_claim(bdev, holder);
-	if (res == 0) {
-		found = find_bd_holder(bdev, bo);
-		if (found == NULL) {
-			res = add_bd_holder(bdev, bo);
-			if (res)
-				bd_release(bdev);
-		}
-	}
 
-	if (res || found)
+	err = bd_claim(bdev, holder);
+	if (err)
+		goto out;
+
+	found = find_bd_holder(bdev, bo);
+	if (found)
+		goto out;
+
+	err = add_bd_holder(bdev, bo);
+	if (err)
+		bd_release(bdev);
+
+out:
+	if (err || found)
 		free_bd_holder(bo);
 	mutex_unlock(&bdev->bd_mutex);
-
-	return res;
+	return err;
 }
 
 /**

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

end of thread, other threads:[~2007-06-17 15:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-15 13:46 [PATCH] Replace obscure constructs in fs/block_dev.c Johannes Weiner
2007-06-17  9:38 ` Björn Steinbrink
2007-06-17 12:52   ` Johannes Weiner
2007-06-17 15:42   ` Johannes Weiner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).