@@ -1752,13 +1752,11 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
17521752{
17531753 struct btrfs_device * device ;
17541754 struct btrfs_device * next_device ;
1755- struct block_device * bdev ;
1755+ struct block_device * bdev = NULL ;
17561756 struct buffer_head * bh = NULL ;
1757- struct btrfs_super_block * disk_super ;
1757+ struct btrfs_super_block * disk_super = NULL ;
17581758 struct btrfs_fs_devices * cur_devices ;
1759- u64 devid ;
17601759 u64 num_devices ;
1761- u8 * dev_uuid ;
17621760 int ret = 0 ;
17631761 bool clear_super = false;
17641762
@@ -1768,57 +1766,19 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
17681766 if (ret )
17691767 goto out ;
17701768
1771- if (strcmp (device_path , "missing" ) == 0 ) {
1772- struct list_head * devices ;
1773- struct btrfs_device * tmp ;
1774-
1775- device = NULL ;
1776- devices = & root -> fs_info -> fs_devices -> devices ;
1777- /*
1778- * It is safe to read the devices since the volume_mutex
1779- * is held.
1780- */
1781- list_for_each_entry (tmp , devices , dev_list ) {
1782- if (tmp -> in_fs_metadata &&
1783- !tmp -> is_tgtdev_for_dev_replace &&
1784- !tmp -> bdev ) {
1785- device = tmp ;
1786- break ;
1787- }
1788- }
1789- bdev = NULL ;
1790- bh = NULL ;
1791- disk_super = NULL ;
1792- if (!device ) {
1793- ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND ;
1794- goto out ;
1795- }
1796- } else {
1797- ret = btrfs_get_bdev_and_sb (device_path ,
1798- FMODE_WRITE | FMODE_EXCL ,
1799- root -> fs_info -> bdev_holder , 0 ,
1800- & bdev , & bh );
1801- if (ret )
1802- goto out ;
1803- disk_super = (struct btrfs_super_block * )bh -> b_data ;
1804- devid = btrfs_stack_device_id (& disk_super -> dev_item );
1805- dev_uuid = disk_super -> dev_item .uuid ;
1806- device = btrfs_find_device (root -> fs_info , devid , dev_uuid ,
1807- disk_super -> fsid );
1808- if (!device ) {
1809- ret = - ENOENT ;
1810- goto error_brelse ;
1811- }
1812- }
1769+ ret = btrfs_find_device_by_user_input (root , 0 , device_path ,
1770+ & device );
1771+ if (ret )
1772+ goto out ;
18131773
18141774 if (device -> is_tgtdev_for_dev_replace ) {
18151775 ret = BTRFS_ERROR_DEV_TGT_REPLACE ;
1816- goto error_brelse ;
1776+ goto out ;
18171777 }
18181778
18191779 if (device -> writeable && root -> fs_info -> fs_devices -> rw_devices == 1 ) {
18201780 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE ;
1821- goto error_brelse ;
1781+ goto out ;
18221782 }
18231783
18241784 if (device -> writeable ) {
@@ -1908,16 +1868,33 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
19081868 * at this point, the device is zero sized. We want to
19091869 * remove it from the devices list and zero out the old super
19101870 */
1911- if (clear_super && disk_super ) {
1871+ if (clear_super ) {
19121872 u64 bytenr ;
19131873 int i ;
19141874
1875+ if (!disk_super ) {
1876+ ret = btrfs_get_bdev_and_sb (device_path ,
1877+ FMODE_WRITE | FMODE_EXCL ,
1878+ root -> fs_info -> bdev_holder , 0 ,
1879+ & bdev , & bh );
1880+ if (ret ) {
1881+ /*
1882+ * It could be a failed device ok for clear_super
1883+ * to fail. So return success
1884+ */
1885+ ret = 0 ;
1886+ goto out ;
1887+ }
1888+
1889+ disk_super = (struct btrfs_super_block * )bh -> b_data ;
1890+ }
19151891 /* make sure this device isn't detected as part of
19161892 * the FS anymore
19171893 */
19181894 memset (& disk_super -> magic , 0 , sizeof (disk_super -> magic ));
19191895 set_buffer_dirty (bh );
19201896 sync_dirty_buffer (bh );
1897+ brelse (bh );
19211898
19221899 /* clear the mirror copies of super block on the disk
19231900 * being removed, 0th copy is been taken care above and
@@ -1929,7 +1906,6 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
19291906 i_size_read (bdev -> bd_inode ))
19301907 break ;
19311908
1932- brelse (bh );
19331909 bh = __bread (bdev , bytenr / 4096 ,
19341910 BTRFS_SUPER_INFO_SIZE );
19351911 if (!bh )
@@ -1939,32 +1915,30 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
19391915
19401916 if (btrfs_super_bytenr (disk_super ) != bytenr ||
19411917 btrfs_super_magic (disk_super ) != BTRFS_MAGIC ) {
1918+ brelse (bh );
19421919 continue ;
19431920 }
19441921 memset (& disk_super -> magic , 0 ,
19451922 sizeof (disk_super -> magic ));
19461923 set_buffer_dirty (bh );
19471924 sync_dirty_buffer (bh );
1925+ brelse (bh );
19481926 }
1949- }
19501927
1951- ret = 0 ;
1952-
1953- if (bdev ) {
1954- /* Notify udev that device has changed */
1955- btrfs_kobject_uevent (bdev , KOBJ_CHANGE );
1928+ if (bdev ) {
1929+ /* Notify udev that device has changed */
1930+ btrfs_kobject_uevent (bdev , KOBJ_CHANGE );
19561931
1957- /* Update ctime/mtime for device path for libblkid */
1958- update_dev_time (device_path );
1932+ /* Update ctime/mtime for device path for libblkid */
1933+ update_dev_time (device_path );
1934+ blkdev_put (bdev , FMODE_READ | FMODE_EXCL );
1935+ }
19591936 }
19601937
1961- error_brelse :
1962- brelse (bh );
1963- if (bdev )
1964- blkdev_put (bdev , FMODE_READ | FMODE_EXCL );
19651938out :
19661939 mutex_unlock (& uuid_mutex );
19671940 return ret ;
1941+
19681942error_undo :
19691943 if (device -> writeable ) {
19701944 lock_chunks (root );
@@ -1973,7 +1947,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
19731947 device -> fs_devices -> rw_devices ++ ;
19741948 unlock_chunks (root );
19751949 }
1976- goto error_brelse ;
1950+ goto out ;
19771951}
19781952
19791953void btrfs_rm_dev_replace_remove_srcdev (struct btrfs_fs_info * fs_info ,
0 commit comments