Commit bbac586
btrfs: remove device item and update super block in the same transaction
[BUG]
There is a report that a btrfs has a bad super block num devices.
This makes btrfs to reject the fs completely.
BTRFS error (device sdd3): super_num_devices 3 mismatch with num_devices 2 found here
BTRFS error (device sdd3): failed to read chunk tree: -22
BTRFS error (device sdd3): open_ctree failed
[CAUSE]
During btrfs device removal, chunk tree and super block num devs are
updated in two different transactions:
btrfs_rm_device()
|- btrfs_rm_dev_item(device)
| |- trans = btrfs_start_transaction()
| | Now we got transaction X
| |
| |- btrfs_del_item()
| | Now device item is removed from chunk tree
| |
| |- btrfs_commit_transaction()
| Transaction X got committed, super num devs untouched,
| but device item removed from chunk tree.
| (AKA, super num devs is already incorrect)
|
|- cur_devices->num_devices--;
|- cur_devices->total_devices--;
|- btrfs_set_super_num_devices()
All those operations are not in transaction X, thus it will
only be written back to disk in next transaction.
So after the transaction X in btrfs_rm_dev_item() committed, but before
transaction X+1 (which can be minutes away), a power loss happen, then
we got the super num mismatch.
[FIX]
Instead of starting and committing a transaction inside
btrfs_rm_dev_item(), start a transaction in side btrfs_rm_device() and
pass it to btrfs_rm_dev_item().
And only commit the transaction after everything is done.
Reported-by: Luca Béla Palkovics <luca.bela.palkovics@gmail.com>
Link: https://lore.kernel.org/linux-btrfs/CA+8xDSpvdm_U0QLBAnrH=zqDq_cWCOH5TiV46CKmp3igr44okQ@mail.gmail.com/
CC: stable@vger.kernel.org # 4.14+
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>1 parent b642b52 commit bbac586
1 file changed
Lines changed: 28 additions & 37 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1896 | 1896 | | |
1897 | 1897 | | |
1898 | 1898 | | |
1899 | | - | |
| 1899 | + | |
| 1900 | + | |
1900 | 1901 | | |
1901 | 1902 | | |
1902 | 1903 | | |
1903 | 1904 | | |
1904 | 1905 | | |
1905 | | - | |
1906 | 1906 | | |
1907 | 1907 | | |
1908 | 1908 | | |
1909 | 1909 | | |
1910 | 1910 | | |
1911 | | - | |
1912 | | - | |
1913 | | - | |
1914 | | - | |
1915 | | - | |
1916 | 1911 | | |
1917 | 1912 | | |
1918 | 1913 | | |
| |||
1923 | 1918 | | |
1924 | 1919 | | |
1925 | 1920 | | |
1926 | | - | |
1927 | | - | |
1928 | 1921 | | |
1929 | 1922 | | |
1930 | 1923 | | |
1931 | 1924 | | |
1932 | | - | |
1933 | | - | |
1934 | | - | |
1935 | | - | |
1936 | | - | |
1937 | 1925 | | |
1938 | 1926 | | |
1939 | | - | |
1940 | | - | |
1941 | 1927 | | |
1942 | 1928 | | |
1943 | 1929 | | |
| |||
2078 | 2064 | | |
2079 | 2065 | | |
2080 | 2066 | | |
| 2067 | + | |
2081 | 2068 | | |
2082 | 2069 | | |
2083 | 2070 | | |
| |||
2098 | 2085 | | |
2099 | 2086 | | |
2100 | 2087 | | |
2101 | | - | |
| 2088 | + | |
2102 | 2089 | | |
2103 | 2090 | | |
2104 | 2091 | | |
2105 | 2092 | | |
2106 | 2093 | | |
2107 | 2094 | | |
2108 | 2095 | | |
2109 | | - | |
| 2096 | + | |
2110 | 2097 | | |
2111 | 2098 | | |
2112 | 2099 | | |
2113 | 2100 | | |
2114 | 2101 | | |
2115 | 2102 | | |
2116 | | - | |
2117 | | - | |
| 2103 | + | |
2118 | 2104 | | |
2119 | 2105 | | |
2120 | | - | |
2121 | | - | |
2122 | | - | |
2123 | | - | |
| 2106 | + | |
| 2107 | + | |
2124 | 2108 | | |
2125 | 2109 | | |
2126 | | - | |
2127 | | - | |
2128 | | - | |
2129 | | - | |
| 2110 | + | |
| 2111 | + | |
2130 | 2112 | | |
2131 | 2113 | | |
2132 | 2114 | | |
| |||
2139 | 2121 | | |
2140 | 2122 | | |
2141 | 2123 | | |
2142 | | - | |
2143 | | - | |
2144 | | - | |
2145 | | - | |
2146 | | - | |
2147 | | - | |
2148 | | - | |
| 2124 | + | |
| 2125 | + | |
| 2126 | + | |
2149 | 2127 | | |
| 2128 | + | |
| 2129 | + | |
| 2130 | + | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
| 2135 | + | |
| 2136 | + | |
| 2137 | + | |
| 2138 | + | |
| 2139 | + | |
2150 | 2140 | | |
2151 | 2141 | | |
2152 | 2142 | | |
| |||
2229 | 2219 | | |
2230 | 2220 | | |
2231 | 2221 | | |
2232 | | - | |
| 2222 | + | |
| 2223 | + | |
2233 | 2224 | | |
2234 | 2225 | | |
2235 | 2226 | | |
| |||
2240 | 2231 | | |
2241 | 2232 | | |
2242 | 2233 | | |
2243 | | - | |
| 2234 | + | |
2244 | 2235 | | |
2245 | 2236 | | |
2246 | 2237 | | |
| |||
0 commit comments