Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

[PATCH] clean up add_bd_holder()

add_bd_holder() is called from bd_claim_by_kobject to put a given struct
bd_holder in the list if there is no matching entry.

There are 3 possible results of add_bd_holder():
1. there is no matching entry and add the given one to the list
2. there is matching entry, so just increment reference count of
the existing one
3. something failed during its course

1 and 2 are successful cases. But for case 2, someone has to free the
unused struct bd_holder.

The current code frees it inside of add_bd_holder and returns same value
0 for both cases 1 and 2. However, it's natural and less error-prone if
caller frees it since it's allocated by the caller.

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by

Jun'ichi Nomura and committed by
Linus Torvalds
df6c0cd9 bcb55165

+35 -18
+35 -18
fs/block_dev.c
··· 642 642 } 643 643 644 644 /** 645 + * find_bd_holder - find matching struct bd_holder from the block device 646 + * 647 + * @bdev: struct block device to be searched 648 + * @bo: target struct bd_holder 649 + * 650 + * Returns matching entry with @bo in @bdev->bd_holder_list. 651 + * If found, increment the reference count and return the pointer. 652 + * If not found, returns NULL. 653 + */ 654 + static int find_bd_holder(struct block_device *bdev, struct bd_holder *bo) 655 + { 656 + struct bd_holder *tmp; 657 + 658 + list_for_each_entry(tmp, &bdev->bd_holder_list, list) 659 + if (tmp->sdir == bo->sdir) { 660 + tmp->count++; 661 + return tmp; 662 + } 663 + 664 + return NULL; 665 + } 666 + 667 + /** 645 668 * add_bd_holder - create sysfs symlinks for bd_claim() relationship 646 669 * 647 670 * @bdev: block device to be bd_claimed 648 671 * @bo: preallocated and initialized by alloc_bd_holder() 649 672 * 650 - * If there is no matching entry with @bo in @bdev->bd_holder_list, 651 - * add @bo to the list, create symlinks. 673 + * Add @bo to @bdev->bd_holder_list, create symlinks. 652 674 * 653 - * Returns 0 if symlinks are created or already there. 654 - * Returns -ve if something fails and @bo can be freed. 675 + * Returns 0 if symlinks are created. 676 + * Returns -ve if something fails. 655 677 */ 656 678 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo) 657 679 { ··· 682 660 683 661 if (!bo) 684 662 return -EINVAL; 685 - 686 - list_for_each_entry(tmp, &bdev->bd_holder_list, list) { 687 - if (tmp->sdir == bo->sdir) { 688 - tmp->count++; 689 - /* We've already done what we need to do here. */ 690 - free_bd_holder(bo); 691 - return 0; 692 - } 693 - } 694 663 695 664 if (!bd_holder_grab_dirs(bdev, bo)) 696 665 return -EBUSY; ··· 753 740 struct kobject *kobj) 754 741 { 755 742 int res; 756 - struct bd_holder *bo; 743 + struct bd_holder *bo, *found; 757 744 758 745 if (!kobj) 759 746 return -EINVAL; ··· 765 752 mutex_lock_nested(&bdev->bd_mutex, BD_MUTEX_PARTITION); 766 753 res = bd_claim(bdev, holder); 767 754 if (res == 0) { 768 - res = add_bd_holder(bdev, bo); 769 - if (res) 770 - bd_release(bdev); 755 + found = find_bd_holder(bdev, bo); 756 + if (found == NULL) { 757 + res = add_bd_holder(bdev, bo); 758 + if (res) 759 + bd_release(bdev); 760 + } 771 761 } 772 - if (res) 762 + 763 + if (res || found) 773 764 free_bd_holder(bo); 774 765 mutex_unlock(&bdev->bd_mutex); 775 766