diff options
author | Stefan Behrens <sbehrens@giantdisaster.de> | 2012-11-05 11:33:06 -0500 |
---|---|---|
committer | Josef Bacik <jbacik@fusionio.com> | 2012-12-12 17:15:41 -0500 |
commit | e93c89c1aaaaaec3487c4c18dd02360371790722 (patch) | |
tree | 359c310c736d0db5cfd724c628fdb9f2f680a3d3 /fs/btrfs | |
parent | ff023aac31198e88507d626825379b28ea481d4d (diff) |
Btrfs: add new sources for device replace code
This adds a new file to the sources together with the header file
and the changes to ioctl.h and ctree.h that are required by the
new C source file. Additionally, 4 new functions are added to
volume.c that deal with device creation and destruction.
Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
Diffstat (limited to 'fs/btrfs')
-rw-r--r-- | fs/btrfs/Makefile | 2 | ||||
-rw-r--r-- | fs/btrfs/ctree.h | 2 | ||||
-rw-r--r-- | fs/btrfs/dev-replace.c | 856 | ||||
-rw-r--r-- | fs/btrfs/dev-replace.h | 18 | ||||
-rw-r--r-- | fs/btrfs/ioctl.h | 45 | ||||
-rw-r--r-- | fs/btrfs/volumes.c | 139 | ||||
-rw-r--r-- | fs/btrfs/volumes.h | 8 |
7 files changed, 1069 insertions, 1 deletions
diff --git a/fs/btrfs/Makefile b/fs/btrfs/Makefile index d7fcdba141a2..7df3e0f0ee51 100644 --- a/fs/btrfs/Makefile +++ b/fs/btrfs/Makefile | |||
@@ -8,7 +8,7 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ | |||
8 | extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \ | 8 | extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \ |
9 | export.o tree-log.o free-space-cache.o zlib.o lzo.o \ | 9 | export.o tree-log.o free-space-cache.o zlib.o lzo.o \ |
10 | compression.o delayed-ref.o relocation.o delayed-inode.o scrub.o \ | 10 | compression.o delayed-ref.o relocation.o delayed-inode.o scrub.o \ |
11 | reada.o backref.o ulist.o qgroup.o send.o | 11 | reada.o backref.o ulist.o qgroup.o send.o dev-replace.o |
12 | 12 | ||
13 | btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o | 13 | btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o |
14 | btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o | 14 | btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o |
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index ded7caa0d304..45e7f752b64a 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h | |||
@@ -142,6 +142,8 @@ struct btrfs_ordered_sum; | |||
142 | 142 | ||
143 | #define BTRFS_EMPTY_SUBVOL_DIR_OBJECTID 2 | 143 | #define BTRFS_EMPTY_SUBVOL_DIR_OBJECTID 2 |
144 | 144 | ||
145 | #define BTRFS_DEV_REPLACE_DEVID 0 | ||
146 | |||
145 | /* | 147 | /* |
146 | * the max metadata block size. This limit is somewhat artificial, | 148 | * the max metadata block size. This limit is somewhat artificial, |
147 | * but the memmove costs go through the roof for larger blocks. | 149 | * but the memmove costs go through the roof for larger blocks. |
diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c new file mode 100644 index 000000000000..66dbc8dbddf7 --- /dev/null +++ b/fs/btrfs/dev-replace.c | |||
@@ -0,0 +1,856 @@ | |||
1 | /* | ||
2 | * Copyright (C) STRATO AG 2012. All rights reserved. | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU General Public | ||
6 | * License v2 as published by the Free Software Foundation. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
11 | * General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public | ||
14 | * License along with this program; if not, write to the | ||
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
16 | * Boston, MA 021110-1307, USA. | ||
17 | */ | ||
18 | #include <linux/sched.h> | ||
19 | #include <linux/bio.h> | ||
20 | #include <linux/slab.h> | ||
21 | #include <linux/buffer_head.h> | ||
22 | #include <linux/blkdev.h> | ||
23 | #include <linux/random.h> | ||
24 | #include <linux/iocontext.h> | ||
25 | #include <linux/capability.h> | ||
26 | #include <linux/kthread.h> | ||
27 | #include <linux/math64.h> | ||
28 | #include <asm/div64.h> | ||
29 | #include "compat.h" | ||
30 | #include "ctree.h" | ||
31 | #include "extent_map.h" | ||
32 | #include "disk-io.h" | ||
33 | #include "transaction.h" | ||
34 | #include "print-tree.h" | ||
35 | #include "volumes.h" | ||
36 | #include "async-thread.h" | ||
37 | #include "check-integrity.h" | ||
38 | #include "rcu-string.h" | ||
39 | #include "dev-replace.h" | ||
40 | |||
41 | static u64 btrfs_get_seconds_since_1970(void); | ||
42 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, | ||
43 | int scrub_ret); | ||
44 | static void btrfs_dev_replace_update_device_in_mapping_tree( | ||
45 | struct btrfs_fs_info *fs_info, | ||
46 | struct btrfs_device *srcdev, | ||
47 | struct btrfs_device *tgtdev); | ||
48 | static int btrfs_dev_replace_find_srcdev(struct btrfs_root *root, u64 srcdevid, | ||
49 | char *srcdev_name, | ||
50 | struct btrfs_device **device); | ||
51 | static u64 __btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info); | ||
52 | static int btrfs_dev_replace_kthread(void *data); | ||
53 | static int btrfs_dev_replace_continue_on_mount(struct btrfs_fs_info *fs_info); | ||
54 | |||
55 | |||
56 | int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info) | ||
57 | { | ||
58 | struct btrfs_key key; | ||
59 | struct btrfs_root *dev_root = fs_info->dev_root; | ||
60 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
61 | struct extent_buffer *eb; | ||
62 | int slot; | ||
63 | int ret = 0; | ||
64 | struct btrfs_path *path = NULL; | ||
65 | int item_size; | ||
66 | struct btrfs_dev_replace_item *ptr; | ||
67 | u64 src_devid; | ||
68 | |||
69 | path = btrfs_alloc_path(); | ||
70 | if (!path) { | ||
71 | ret = -ENOMEM; | ||
72 | goto out; | ||
73 | } | ||
74 | |||
75 | key.objectid = 0; | ||
76 | key.type = BTRFS_DEV_REPLACE_KEY; | ||
77 | key.offset = 0; | ||
78 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); | ||
79 | if (ret) { | ||
80 | no_valid_dev_replace_entry_found: | ||
81 | ret = 0; | ||
82 | dev_replace->replace_state = | ||
83 | BTRFS_DEV_REPLACE_ITEM_STATE_NEVER_STARTED; | ||
84 | dev_replace->cont_reading_from_srcdev_mode = | ||
85 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS; | ||
86 | dev_replace->replace_state = 0; | ||
87 | dev_replace->time_started = 0; | ||
88 | dev_replace->time_stopped = 0; | ||
89 | atomic64_set(&dev_replace->num_write_errors, 0); | ||
90 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0); | ||
91 | dev_replace->cursor_left = 0; | ||
92 | dev_replace->committed_cursor_left = 0; | ||
93 | dev_replace->cursor_left_last_write_of_item = 0; | ||
94 | dev_replace->cursor_right = 0; | ||
95 | dev_replace->srcdev = NULL; | ||
96 | dev_replace->tgtdev = NULL; | ||
97 | dev_replace->is_valid = 0; | ||
98 | dev_replace->item_needs_writeback = 0; | ||
99 | goto out; | ||
100 | } | ||
101 | slot = path->slots[0]; | ||
102 | eb = path->nodes[0]; | ||
103 | item_size = btrfs_item_size_nr(eb, slot); | ||
104 | ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item); | ||
105 | |||
106 | if (item_size != sizeof(struct btrfs_dev_replace_item)) { | ||
107 | pr_warn("btrfs: dev_replace entry found has unexpected size, ignore entry\n"); | ||
108 | goto no_valid_dev_replace_entry_found; | ||
109 | } | ||
110 | |||
111 | src_devid = btrfs_dev_replace_src_devid(eb, ptr); | ||
112 | dev_replace->cont_reading_from_srcdev_mode = | ||
113 | btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr); | ||
114 | dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr); | ||
115 | dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr); | ||
116 | dev_replace->time_stopped = | ||
117 | btrfs_dev_replace_time_stopped(eb, ptr); | ||
118 | atomic64_set(&dev_replace->num_write_errors, | ||
119 | btrfs_dev_replace_num_write_errors(eb, ptr)); | ||
120 | atomic64_set(&dev_replace->num_uncorrectable_read_errors, | ||
121 | btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr)); | ||
122 | dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr); | ||
123 | dev_replace->committed_cursor_left = dev_replace->cursor_left; | ||
124 | dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left; | ||
125 | dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr); | ||
126 | dev_replace->is_valid = 1; | ||
127 | |||
128 | dev_replace->item_needs_writeback = 0; | ||
129 | switch (dev_replace->replace_state) { | ||
130 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: | ||
131 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: | ||
132 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: | ||
133 | dev_replace->srcdev = NULL; | ||
134 | dev_replace->tgtdev = NULL; | ||
135 | break; | ||
136 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: | ||
137 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: | ||
138 | dev_replace->srcdev = btrfs_find_device(fs_info, src_devid, | ||
139 | NULL, NULL); | ||
140 | dev_replace->tgtdev = btrfs_find_device(fs_info, | ||
141 | BTRFS_DEV_REPLACE_DEVID, | ||
142 | NULL, NULL); | ||
143 | /* | ||
144 | * allow 'btrfs dev replace_cancel' if src/tgt device is | ||
145 | * missing | ||
146 | */ | ||
147 | if (!dev_replace->srcdev && | ||
148 | !btrfs_test_opt(dev_root, DEGRADED)) { | ||
149 | ret = -EIO; | ||
150 | pr_warn("btrfs: cannot mount because device replace operation is ongoing and\n" "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?\n", | ||
151 | (unsigned long long)src_devid); | ||
152 | } | ||
153 | if (!dev_replace->tgtdev && | ||
154 | !btrfs_test_opt(dev_root, DEGRADED)) { | ||
155 | ret = -EIO; | ||
156 | pr_warn("btrfs: cannot mount because device replace operation is ongoing and\n" "tgtdev (devid %llu) is missing, need to run btrfs dev scan?\n", | ||
157 | (unsigned long long)BTRFS_DEV_REPLACE_DEVID); | ||
158 | } | ||
159 | if (dev_replace->tgtdev) { | ||
160 | if (dev_replace->srcdev) { | ||
161 | dev_replace->tgtdev->total_bytes = | ||
162 | dev_replace->srcdev->total_bytes; | ||
163 | dev_replace->tgtdev->disk_total_bytes = | ||
164 | dev_replace->srcdev->disk_total_bytes; | ||
165 | dev_replace->tgtdev->bytes_used = | ||
166 | dev_replace->srcdev->bytes_used; | ||
167 | } | ||
168 | dev_replace->tgtdev->is_tgtdev_for_dev_replace = 1; | ||
169 | btrfs_init_dev_replace_tgtdev_for_resume(fs_info, | ||
170 | dev_replace->tgtdev); | ||
171 | } | ||
172 | break; | ||
173 | } | ||
174 | |||
175 | out: | ||
176 | if (path) | ||
177 | btrfs_free_path(path); | ||
178 | return ret; | ||
179 | } | ||
180 | |||
181 | /* | ||
182 | * called from commit_transaction. Writes changed device replace state to | ||
183 | * disk. | ||
184 | */ | ||
185 | int btrfs_run_dev_replace(struct btrfs_trans_handle *trans, | ||
186 | struct btrfs_fs_info *fs_info) | ||
187 | { | ||
188 | int ret; | ||
189 | struct btrfs_root *dev_root = fs_info->dev_root; | ||
190 | struct btrfs_path *path; | ||
191 | struct btrfs_key key; | ||
192 | struct extent_buffer *eb; | ||
193 | struct btrfs_dev_replace_item *ptr; | ||
194 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
195 | |||
196 | btrfs_dev_replace_lock(dev_replace); | ||
197 | if (!dev_replace->is_valid || | ||
198 | !dev_replace->item_needs_writeback) { | ||
199 | btrfs_dev_replace_unlock(dev_replace); | ||
200 | return 0; | ||
201 | } | ||
202 | btrfs_dev_replace_unlock(dev_replace); | ||
203 | |||
204 | key.objectid = 0; | ||
205 | key.type = BTRFS_DEV_REPLACE_KEY; | ||
206 | key.offset = 0; | ||
207 | |||
208 | path = btrfs_alloc_path(); | ||
209 | if (!path) { | ||
210 | ret = -ENOMEM; | ||
211 | goto out; | ||
212 | } | ||
213 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); | ||
214 | if (ret < 0) { | ||
215 | pr_warn("btrfs: error %d while searching for dev_replace item!\n", | ||
216 | ret); | ||
217 | goto out; | ||
218 | } | ||
219 | |||
220 | if (ret == 0 && | ||
221 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { | ||
222 | /* | ||
223 | * need to delete old one and insert a new one. | ||
224 | * Since no attempt is made to recover any old state, if the | ||
225 | * dev_replace state is 'running', the data on the target | ||
226 | * drive is lost. | ||
227 | * It would be possible to recover the state: just make sure | ||
228 | * that the beginning of the item is never changed and always | ||
229 | * contains all the essential information. Then read this | ||
230 | * minimal set of information and use it as a base for the | ||
231 | * new state. | ||
232 | */ | ||
233 | ret = btrfs_del_item(trans, dev_root, path); | ||
234 | if (ret != 0) { | ||
235 | pr_warn("btrfs: delete too small dev_replace item failed %d!\n", | ||
236 | ret); | ||
237 | goto out; | ||
238 | } | ||
239 | ret = 1; | ||
240 | } | ||
241 | |||
242 | if (ret == 1) { | ||
243 | /* need to insert a new item */ | ||
244 | btrfs_release_path(path); | ||
245 | ret = btrfs_insert_empty_item(trans, dev_root, path, | ||
246 | &key, sizeof(*ptr)); | ||
247 | if (ret < 0) { | ||
248 | pr_warn("btrfs: insert dev_replace item failed %d!\n", | ||
249 | ret); | ||
250 | goto out; | ||
251 | } | ||
252 | } | ||
253 | |||
254 | eb = path->nodes[0]; | ||
255 | ptr = btrfs_item_ptr(eb, path->slots[0], | ||
256 | struct btrfs_dev_replace_item); | ||
257 | |||
258 | btrfs_dev_replace_lock(dev_replace); | ||
259 | if (dev_replace->srcdev) | ||
260 | btrfs_set_dev_replace_src_devid(eb, ptr, | ||
261 | dev_replace->srcdev->devid); | ||
262 | else | ||
263 | btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1); | ||
264 | btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr, | ||
265 | dev_replace->cont_reading_from_srcdev_mode); | ||
266 | btrfs_set_dev_replace_replace_state(eb, ptr, | ||
267 | dev_replace->replace_state); | ||
268 | btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started); | ||
269 | btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped); | ||
270 | btrfs_set_dev_replace_num_write_errors(eb, ptr, | ||
271 | atomic64_read(&dev_replace->num_write_errors)); | ||
272 | btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr, | ||
273 | atomic64_read(&dev_replace->num_uncorrectable_read_errors)); | ||
274 | dev_replace->cursor_left_last_write_of_item = | ||
275 | dev_replace->cursor_left; | ||
276 | btrfs_set_dev_replace_cursor_left(eb, ptr, | ||
277 | dev_replace->cursor_left_last_write_of_item); | ||
278 | btrfs_set_dev_replace_cursor_right(eb, ptr, | ||
279 | dev_replace->cursor_right); | ||
280 | dev_replace->item_needs_writeback = 0; | ||
281 | btrfs_dev_replace_unlock(dev_replace); | ||
282 | |||
283 | btrfs_mark_buffer_dirty(eb); | ||
284 | |||
285 | out: | ||
286 | btrfs_free_path(path); | ||
287 | |||
288 | return ret; | ||
289 | } | ||
290 | |||
291 | void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info) | ||
292 | { | ||
293 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
294 | |||
295 | dev_replace->committed_cursor_left = | ||
296 | dev_replace->cursor_left_last_write_of_item; | ||
297 | } | ||
298 | |||
299 | static u64 btrfs_get_seconds_since_1970(void) | ||
300 | { | ||
301 | struct timespec t = CURRENT_TIME_SEC; | ||
302 | |||
303 | return t.tv_sec; | ||
304 | } | ||
305 | |||
306 | int btrfs_dev_replace_start(struct btrfs_root *root, | ||
307 | struct btrfs_ioctl_dev_replace_args *args) | ||
308 | { | ||
309 | struct btrfs_trans_handle *trans; | ||
310 | struct btrfs_fs_info *fs_info = root->fs_info; | ||
311 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
312 | int ret; | ||
313 | struct btrfs_device *tgt_device = NULL; | ||
314 | struct btrfs_device *src_device = NULL; | ||
315 | |||
316 | switch (args->start.cont_reading_from_srcdev_mode) { | ||
317 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS: | ||
318 | case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID: | ||
319 | break; | ||
320 | default: | ||
321 | return -EINVAL; | ||
322 | } | ||
323 | |||
324 | if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') || | ||
325 | args->start.tgtdev_name[0] == '\0') | ||
326 | return -EINVAL; | ||
327 | |||
328 | mutex_lock(&fs_info->volume_mutex); | ||
329 | ret = btrfs_init_dev_replace_tgtdev(root, args->start.tgtdev_name, | ||
330 | &tgt_device); | ||
331 | if (ret) { | ||
332 | pr_err("btrfs: target device %s is invalid!\n", | ||
333 | args->start.tgtdev_name); | ||
334 | mutex_unlock(&fs_info->volume_mutex); | ||
335 | return -EINVAL; | ||
336 | } | ||
337 | |||
338 | ret = btrfs_dev_replace_find_srcdev(root, args->start.srcdevid, | ||
339 | args->start.srcdev_name, | ||
340 | &src_device); | ||
341 | mutex_unlock(&fs_info->volume_mutex); | ||
342 | if (ret) { | ||
343 | ret = -EINVAL; | ||
344 | goto leave_no_lock; | ||
345 | } | ||
346 | |||
347 | if (tgt_device->total_bytes < src_device->total_bytes) { | ||
348 | pr_err("btrfs: target device is smaller than source device!\n"); | ||
349 | ret = -EINVAL; | ||
350 | goto leave_no_lock; | ||
351 | } | ||
352 | |||
353 | btrfs_dev_replace_lock(dev_replace); | ||
354 | switch (dev_replace->replace_state) { | ||
355 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: | ||
356 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: | ||
357 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: | ||
358 | break; | ||
359 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: | ||
360 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: | ||
361 | args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED; | ||
362 | goto leave; | ||
363 | } | ||
364 | |||
365 | dev_replace->cont_reading_from_srcdev_mode = | ||
366 | args->start.cont_reading_from_srcdev_mode; | ||
367 | WARN_ON(!src_device); | ||
368 | dev_replace->srcdev = src_device; | ||
369 | WARN_ON(!tgt_device); | ||
370 | dev_replace->tgtdev = tgt_device; | ||
371 | |||
372 | printk_in_rcu(KERN_INFO | ||
373 | "btrfs: dev_replace from %s (devid %llu) to %s) started\n", | ||
374 | src_device->missing ? "<missing disk>" : | ||
375 | rcu_str_deref(src_device->name), | ||
376 | src_device->devid, | ||
377 | rcu_str_deref(tgt_device->name)); | ||
378 | |||
379 | tgt_device->total_bytes = src_device->total_bytes; | ||
380 | tgt_device->disk_total_bytes = src_device->disk_total_bytes; | ||
381 | tgt_device->bytes_used = src_device->bytes_used; | ||
382 | |||
383 | /* | ||
384 | * from now on, the writes to the srcdev are all duplicated to | ||
385 | * go to the tgtdev as well (refer to btrfs_map_block()). | ||
386 | */ | ||
387 | dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED; | ||
388 | dev_replace->time_started = btrfs_get_seconds_since_1970(); | ||
389 | dev_replace->cursor_left = 0; | ||
390 | dev_replace->committed_cursor_left = 0; | ||
391 | dev_replace->cursor_left_last_write_of_item = 0; | ||
392 | dev_replace->cursor_right = 0; | ||
393 | dev_replace->is_valid = 1; | ||
394 | dev_replace->item_needs_writeback = 1; | ||
395 | args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; | ||
396 | btrfs_dev_replace_unlock(dev_replace); | ||
397 | |||
398 | btrfs_wait_ordered_extents(root, 0); | ||
399 | |||
400 | /* force writing the updated state information to disk */ | ||
401 | trans = btrfs_start_transaction(root, 0); | ||
402 | if (IS_ERR(trans)) { | ||
403 | ret = PTR_ERR(trans); | ||
404 | btrfs_dev_replace_lock(dev_replace); | ||
405 | goto leave; | ||
406 | } | ||
407 | |||
408 | ret = btrfs_commit_transaction(trans, root); | ||
409 | WARN_ON(ret); | ||
410 | |||
411 | /* the disk copy procedure reuses the scrub code */ | ||
412 | ret = btrfs_scrub_dev(fs_info, src_device->devid, 0, | ||
413 | src_device->total_bytes, | ||
414 | &dev_replace->scrub_progress, 0, 1); | ||
415 | |||
416 | ret = btrfs_dev_replace_finishing(root->fs_info, ret); | ||
417 | WARN_ON(ret); | ||
418 | |||
419 | return 0; | ||
420 | |||
421 | leave: | ||
422 | dev_replace->srcdev = NULL; | ||
423 | dev_replace->tgtdev = NULL; | ||
424 | btrfs_dev_replace_unlock(dev_replace); | ||
425 | leave_no_lock: | ||
426 | if (tgt_device) | ||
427 | btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device); | ||
428 | return ret; | ||
429 | } | ||
430 | |||
431 | static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info, | ||
432 | int scrub_ret) | ||
433 | { | ||
434 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
435 | struct btrfs_device *tgt_device; | ||
436 | struct btrfs_device *src_device; | ||
437 | struct btrfs_root *root = fs_info->tree_root; | ||
438 | u8 uuid_tmp[BTRFS_UUID_SIZE]; | ||
439 | struct btrfs_trans_handle *trans; | ||
440 | int ret = 0; | ||
441 | |||
442 | /* don't allow cancel or unmount to disturb the finishing procedure */ | ||
443 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); | ||
444 | |||
445 | btrfs_dev_replace_lock(dev_replace); | ||
446 | /* was the operation canceled, or is it finished? */ | ||
447 | if (dev_replace->replace_state != | ||
448 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) { | ||
449 | btrfs_dev_replace_unlock(dev_replace); | ||
450 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); | ||
451 | return 0; | ||
452 | } | ||
453 | |||
454 | tgt_device = dev_replace->tgtdev; | ||
455 | src_device = dev_replace->srcdev; | ||
456 | btrfs_dev_replace_unlock(dev_replace); | ||
457 | |||
458 | /* replace old device with new one in mapping tree */ | ||
459 | if (!scrub_ret) | ||
460 | btrfs_dev_replace_update_device_in_mapping_tree(fs_info, | ||
461 | src_device, | ||
462 | tgt_device); | ||
463 | |||
464 | /* | ||
465 | * flush all outstanding I/O and inode extent mappings before the | ||
466 | * copy operation is declared as being finished | ||
467 | */ | ||
468 | btrfs_start_delalloc_inodes(root, 0); | ||
469 | btrfs_wait_ordered_extents(root, 0); | ||
470 | |||
471 | trans = btrfs_start_transaction(root, 0); | ||
472 | if (IS_ERR(trans)) { | ||
473 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); | ||
474 | return PTR_ERR(trans); | ||
475 | } | ||
476 | ret = btrfs_commit_transaction(trans, root); | ||
477 | WARN_ON(ret); | ||
478 | |||
479 | /* keep away write_all_supers() during the finishing procedure */ | ||
480 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); | ||
481 | btrfs_dev_replace_lock(dev_replace); | ||
482 | dev_replace->replace_state = | ||
483 | scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED | ||
484 | : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED; | ||
485 | dev_replace->tgtdev = NULL; | ||
486 | dev_replace->srcdev = NULL; | ||
487 | dev_replace->time_stopped = btrfs_get_seconds_since_1970(); | ||
488 | dev_replace->item_needs_writeback = 1; | ||
489 | |||
490 | if (scrub_ret) { | ||
491 | printk_in_rcu(KERN_ERR | ||
492 | "btrfs: btrfs_scrub_dev(%s, %llu, %s) failed %d\n", | ||
493 | src_device->missing ? "<missing disk>" : | ||
494 | rcu_str_deref(src_device->name), | ||
495 | src_device->devid, | ||
496 | rcu_str_deref(tgt_device->name), scrub_ret); | ||
497 | btrfs_dev_replace_unlock(dev_replace); | ||
498 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); | ||
499 | if (tgt_device) | ||
500 | btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device); | ||
501 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); | ||
502 | |||
503 | return 0; | ||
504 | } | ||
505 | |||
506 | printk_in_rcu(KERN_INFO | ||
507 | "btrfs: dev_replace from %s (devid %llu) to %s) finished\n", | ||
508 | src_device->missing ? "<missing disk>" : | ||
509 | rcu_str_deref(src_device->name), | ||
510 | src_device->devid, | ||
511 | rcu_str_deref(tgt_device->name)); | ||
512 | tgt_device->is_tgtdev_for_dev_replace = 0; | ||
513 | tgt_device->devid = src_device->devid; | ||
514 | src_device->devid = BTRFS_DEV_REPLACE_DEVID; | ||
515 | tgt_device->bytes_used = src_device->bytes_used; | ||
516 | memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp)); | ||
517 | memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid)); | ||
518 | memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid)); | ||
519 | tgt_device->total_bytes = src_device->total_bytes; | ||
520 | tgt_device->disk_total_bytes = src_device->disk_total_bytes; | ||
521 | tgt_device->bytes_used = src_device->bytes_used; | ||
522 | if (fs_info->sb->s_bdev == src_device->bdev) | ||
523 | fs_info->sb->s_bdev = tgt_device->bdev; | ||
524 | if (fs_info->fs_devices->latest_bdev == src_device->bdev) | ||
525 | fs_info->fs_devices->latest_bdev = tgt_device->bdev; | ||
526 | list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list); | ||
527 | |||
528 | btrfs_rm_dev_replace_srcdev(fs_info, src_device); | ||
529 | if (src_device->bdev) { | ||
530 | /* zero out the old super */ | ||
531 | btrfs_scratch_superblock(src_device); | ||
532 | } | ||
533 | /* | ||
534 | * this is again a consistent state where no dev_replace procedure | ||
535 | * is running, the target device is part of the filesystem, the | ||
536 | * source device is not part of the filesystem anymore and its 1st | ||
537 | * superblock is scratched out so that it is no longer marked to | ||
538 | * belong to this filesystem. | ||
539 | */ | ||
540 | btrfs_dev_replace_unlock(dev_replace); | ||
541 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); | ||
542 | |||
543 | /* write back the superblocks */ | ||
544 | trans = btrfs_start_transaction(root, 0); | ||
545 | if (!IS_ERR(trans)) | ||
546 | btrfs_commit_transaction(trans, root); | ||
547 | |||
548 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); | ||
549 | |||
550 | return 0; | ||
551 | } | ||
552 | |||
553 | static void btrfs_dev_replace_update_device_in_mapping_tree( | ||
554 | struct btrfs_fs_info *fs_info, | ||
555 | struct btrfs_device *srcdev, | ||
556 | struct btrfs_device *tgtdev) | ||
557 | { | ||
558 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; | ||
559 | struct extent_map *em; | ||
560 | struct map_lookup *map; | ||
561 | u64 start = 0; | ||
562 | int i; | ||
563 | |||
564 | write_lock(&em_tree->lock); | ||
565 | do { | ||
566 | em = lookup_extent_mapping(em_tree, start, (u64)-1); | ||
567 | if (!em) | ||
568 | break; | ||
569 | map = (struct map_lookup *)em->bdev; | ||
570 | for (i = 0; i < map->num_stripes; i++) | ||
571 | if (srcdev == map->stripes[i].dev) | ||
572 | map->stripes[i].dev = tgtdev; | ||
573 | start = em->start + em->len; | ||
574 | free_extent_map(em); | ||
575 | } while (start); | ||
576 | write_unlock(&em_tree->lock); | ||
577 | } | ||
578 | |||
579 | static int btrfs_dev_replace_find_srcdev(struct btrfs_root *root, u64 srcdevid, | ||
580 | char *srcdev_name, | ||
581 | struct btrfs_device **device) | ||
582 | { | ||
583 | int ret; | ||
584 | |||
585 | if (srcdevid) { | ||
586 | ret = 0; | ||
587 | *device = btrfs_find_device(root->fs_info, srcdevid, NULL, | ||
588 | NULL); | ||
589 | if (!*device) | ||
590 | ret = -ENOENT; | ||
591 | } else { | ||
592 | ret = btrfs_find_device_missing_or_by_path(root, srcdev_name, | ||
593 | device); | ||
594 | } | ||
595 | return ret; | ||
596 | } | ||
597 | |||
598 | void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info, | ||
599 | struct btrfs_ioctl_dev_replace_args *args) | ||
600 | { | ||
601 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
602 | |||
603 | btrfs_dev_replace_lock(dev_replace); | ||
604 | /* even if !dev_replace_is_valid, the values are good enough for | ||
605 | * the replace_status ioctl */ | ||
606 | args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; | ||
607 | args->status.replace_state = dev_replace->replace_state; | ||
608 | args->status.time_started = dev_replace->time_started; | ||
609 | args->status.time_stopped = dev_replace->time_stopped; | ||
610 | args->status.num_write_errors = | ||
611 | atomic64_read(&dev_replace->num_write_errors); | ||
612 | args->status.num_uncorrectable_read_errors = | ||
613 | atomic64_read(&dev_replace->num_uncorrectable_read_errors); | ||
614 | switch (dev_replace->replace_state) { | ||
615 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: | ||
616 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: | ||
617 | args->status.progress_1000 = 0; | ||
618 | break; | ||
619 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: | ||
620 | args->status.progress_1000 = 1000; | ||
621 | break; | ||
622 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: | ||
623 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: | ||
624 | args->status.progress_1000 = div64_u64(dev_replace->cursor_left, | ||
625 | div64_u64(dev_replace->srcdev->total_bytes, 1000)); | ||
626 | break; | ||
627 | } | ||
628 | btrfs_dev_replace_unlock(dev_replace); | ||
629 | } | ||
630 | |||
631 | int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info, | ||
632 | struct btrfs_ioctl_dev_replace_args *args) | ||
633 | { | ||
634 | args->result = __btrfs_dev_replace_cancel(fs_info); | ||
635 | return 0; | ||
636 | } | ||
637 | |||
638 | static u64 __btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info) | ||
639 | { | ||
640 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
641 | struct btrfs_device *tgt_device = NULL; | ||
642 | struct btrfs_trans_handle *trans; | ||
643 | struct btrfs_root *root = fs_info->tree_root; | ||
644 | u64 result; | ||
645 | int ret; | ||
646 | |||
647 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); | ||
648 | btrfs_dev_replace_lock(dev_replace); | ||
649 | switch (dev_replace->replace_state) { | ||
650 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: | ||
651 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: | ||
652 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: | ||
653 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED; | ||
654 | btrfs_dev_replace_unlock(dev_replace); | ||
655 | goto leave; | ||
656 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: | ||
657 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: | ||
658 | result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; | ||
659 | tgt_device = dev_replace->tgtdev; | ||
660 | dev_replace->tgtdev = NULL; | ||
661 | dev_replace->srcdev = NULL; | ||
662 | break; | ||
663 | } | ||
664 | dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED; | ||
665 | dev_replace->time_stopped = btrfs_get_seconds_since_1970(); | ||
666 | dev_replace->item_needs_writeback = 1; | ||
667 | btrfs_dev_replace_unlock(dev_replace); | ||
668 | btrfs_scrub_cancel(fs_info); | ||
669 | |||
670 | trans = btrfs_start_transaction(root, 0); | ||
671 | if (IS_ERR(trans)) { | ||
672 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); | ||
673 | return PTR_ERR(trans); | ||
674 | } | ||
675 | ret = btrfs_commit_transaction(trans, root); | ||
676 | WARN_ON(ret); | ||
677 | if (tgt_device) | ||
678 | btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device); | ||
679 | |||
680 | leave: | ||
681 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); | ||
682 | return result; | ||
683 | } | ||
684 | |||
685 | void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info) | ||
686 | { | ||
687 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
688 | |||
689 | mutex_lock(&dev_replace->lock_finishing_cancel_unmount); | ||
690 | btrfs_dev_replace_lock(dev_replace); | ||
691 | switch (dev_replace->replace_state) { | ||
692 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: | ||
693 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: | ||
694 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: | ||
695 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: | ||
696 | break; | ||
697 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: | ||
698 | dev_replace->replace_state = | ||
699 | BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED; | ||
700 | dev_replace->time_stopped = btrfs_get_seconds_since_1970(); | ||
701 | dev_replace->item_needs_writeback = 1; | ||
702 | pr_info("btrfs: suspending dev_replace for unmount\n"); | ||
703 | break; | ||
704 | } | ||
705 | |||
706 | btrfs_dev_replace_unlock(dev_replace); | ||
707 | mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); | ||
708 | } | ||
709 | |||
710 | /* resume dev_replace procedure that was interrupted by unmount */ | ||
711 | int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info) | ||
712 | { | ||
713 | struct task_struct *task; | ||
714 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
715 | |||
716 | btrfs_dev_replace_lock(dev_replace); | ||
717 | switch (dev_replace->replace_state) { | ||
718 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: | ||
719 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: | ||
720 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: | ||
721 | btrfs_dev_replace_unlock(dev_replace); | ||
722 | return 0; | ||
723 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: | ||
724 | break; | ||
725 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: | ||
726 | dev_replace->replace_state = | ||
727 | BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED; | ||
728 | break; | ||
729 | } | ||
730 | if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) { | ||
731 | pr_info("btrfs: cannot continue dev_replace, tgtdev is missing\n" | ||
732 | "btrfs: you may cancel the operation after 'mount -o degraded'\n"); | ||
733 | btrfs_dev_replace_unlock(dev_replace); | ||
734 | return 0; | ||
735 | } | ||
736 | btrfs_dev_replace_unlock(dev_replace); | ||
737 | |||
738 | WARN_ON(atomic_xchg( | ||
739 | &fs_info->mutually_exclusive_operation_running, 1)); | ||
740 | task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl"); | ||
741 | return PTR_RET(task); | ||
742 | } | ||
743 | |||
744 | static int btrfs_dev_replace_kthread(void *data) | ||
745 | { | ||
746 | struct btrfs_fs_info *fs_info = data; | ||
747 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
748 | struct btrfs_ioctl_dev_replace_args *status_args; | ||
749 | u64 progress; | ||
750 | |||
751 | status_args = kzalloc(sizeof(*status_args), GFP_NOFS); | ||
752 | if (status_args) { | ||
753 | btrfs_dev_replace_status(fs_info, status_args); | ||
754 | progress = status_args->status.progress_1000; | ||
755 | kfree(status_args); | ||
756 | do_div(progress, 10); | ||
757 | printk_in_rcu(KERN_INFO | ||
758 | "btrfs: continuing dev_replace from %s (devid %llu) to %s @%u%%\n", | ||
759 | dev_replace->srcdev->missing ? "<missing disk>" : | ||
760 | rcu_str_deref(dev_replace->srcdev->name), | ||
761 | dev_replace->srcdev->devid, | ||
762 | dev_replace->tgtdev ? | ||
763 | rcu_str_deref(dev_replace->tgtdev->name) : | ||
764 | "<missing target disk>", | ||
765 | (unsigned int)progress); | ||
766 | } | ||
767 | btrfs_dev_replace_continue_on_mount(fs_info); | ||
768 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); | ||
769 | |||
770 | return 0; | ||
771 | } | ||
772 | |||
773 | static int btrfs_dev_replace_continue_on_mount(struct btrfs_fs_info *fs_info) | ||
774 | { | ||
775 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; | ||
776 | int ret; | ||
777 | |||
778 | ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid, | ||
779 | dev_replace->committed_cursor_left, | ||
780 | dev_replace->srcdev->total_bytes, | ||
781 | &dev_replace->scrub_progress, 0, 1); | ||
782 | ret = btrfs_dev_replace_finishing(fs_info, ret); | ||
783 | WARN_ON(ret); | ||
784 | return 0; | ||
785 | } | ||
786 | |||
787 | int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace) | ||
788 | { | ||
789 | if (!dev_replace->is_valid) | ||
790 | return 0; | ||
791 | |||
792 | switch (dev_replace->replace_state) { | ||
793 | case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: | ||
794 | case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED: | ||
795 | case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED: | ||
796 | return 0; | ||
797 | case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED: | ||
798 | case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED: | ||
799 | /* | ||
800 | * return true even if tgtdev is missing (this is | ||
801 | * something that can happen if the dev_replace | ||
802 | * procedure is suspended by an umount and then | ||
803 | * the tgtdev is missing (or "btrfs dev scan") was | ||
804 | * not called and the the filesystem is remounted | ||
805 | * in degraded state. This does not stop the | ||
806 | * dev_replace procedure. It needs to be canceled | ||
807 | * manually if the cancelation is wanted. | ||
808 | */ | ||
809 | break; | ||
810 | } | ||
811 | return 1; | ||
812 | } | ||
813 | |||
814 | void btrfs_dev_replace_lock(struct btrfs_dev_replace *dev_replace) | ||
815 | { | ||
816 | /* the beginning is just an optimization for the typical case */ | ||
817 | if (atomic_read(&dev_replace->nesting_level) == 0) { | ||
818 | acquire_lock: | ||
819 | /* this is not a nested case where the same thread | ||
820 | * is trying to acqurire the same lock twice */ | ||
821 | mutex_lock(&dev_replace->lock); | ||
822 | mutex_lock(&dev_replace->lock_management_lock); | ||
823 | dev_replace->lock_owner = current->pid; | ||
824 | atomic_inc(&dev_replace->nesting_level); | ||
825 | mutex_unlock(&dev_replace->lock_management_lock); | ||
826 | return; | ||
827 | } | ||
828 | |||
829 | mutex_lock(&dev_replace->lock_management_lock); | ||
830 | if (atomic_read(&dev_replace->nesting_level) > 0 && | ||
831 | dev_replace->lock_owner == current->pid) { | ||
832 | WARN_ON(!mutex_is_locked(&dev_replace->lock)); | ||
833 | atomic_inc(&dev_replace->nesting_level); | ||
834 | mutex_unlock(&dev_replace->lock_management_lock); | ||
835 | return; | ||
836 | } | ||
837 | |||
838 | mutex_unlock(&dev_replace->lock_management_lock); | ||
839 | goto acquire_lock; | ||
840 | } | ||
841 | |||
842 | void btrfs_dev_replace_unlock(struct btrfs_dev_replace *dev_replace) | ||
843 | { | ||
844 | WARN_ON(!mutex_is_locked(&dev_replace->lock)); | ||
845 | mutex_lock(&dev_replace->lock_management_lock); | ||
846 | WARN_ON(atomic_read(&dev_replace->nesting_level) < 1); | ||
847 | WARN_ON(dev_replace->lock_owner != current->pid); | ||
848 | atomic_dec(&dev_replace->nesting_level); | ||
849 | if (atomic_read(&dev_replace->nesting_level) == 0) { | ||
850 | dev_replace->lock_owner = 0; | ||
851 | mutex_unlock(&dev_replace->lock_management_lock); | ||
852 | mutex_unlock(&dev_replace->lock); | ||
853 | } else { | ||
854 | mutex_unlock(&dev_replace->lock_management_lock); | ||
855 | } | ||
856 | } | ||
diff --git a/fs/btrfs/dev-replace.h b/fs/btrfs/dev-replace.h index 1fb5c89037ee..20035cbbf021 100644 --- a/fs/btrfs/dev-replace.h +++ b/fs/btrfs/dev-replace.h | |||
@@ -19,6 +19,24 @@ | |||
19 | #if !defined(__BTRFS_DEV_REPLACE__) | 19 | #if !defined(__BTRFS_DEV_REPLACE__) |
20 | #define __BTRFS_DEV_REPLACE__ | 20 | #define __BTRFS_DEV_REPLACE__ |
21 | 21 | ||
22 | struct btrfs_ioctl_dev_replace_args; | ||
23 | |||
24 | int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info); | ||
25 | int btrfs_run_dev_replace(struct btrfs_trans_handle *trans, | ||
26 | struct btrfs_fs_info *fs_info); | ||
27 | void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info); | ||
28 | int btrfs_dev_replace_start(struct btrfs_root *root, | ||
29 | struct btrfs_ioctl_dev_replace_args *args); | ||
30 | void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info, | ||
31 | struct btrfs_ioctl_dev_replace_args *args); | ||
32 | int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info, | ||
33 | struct btrfs_ioctl_dev_replace_args *args); | ||
34 | void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info); | ||
35 | int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info); | ||
36 | int btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace); | ||
37 | void btrfs_dev_replace_lock(struct btrfs_dev_replace *dev_replace); | ||
38 | void btrfs_dev_replace_unlock(struct btrfs_dev_replace *dev_replace); | ||
39 | |||
22 | static inline void btrfs_dev_replace_stats_inc(atomic64_t *stat_value) | 40 | static inline void btrfs_dev_replace_stats_inc(atomic64_t *stat_value) |
23 | { | 41 | { |
24 | atomic64_inc(stat_value); | 42 | atomic64_inc(stat_value); |
diff --git a/fs/btrfs/ioctl.h b/fs/btrfs/ioctl.h index 731e2875ab93..62006ba02719 100644 --- a/fs/btrfs/ioctl.h +++ b/fs/btrfs/ioctl.h | |||
@@ -123,6 +123,48 @@ struct btrfs_ioctl_scrub_args { | |||
123 | __u64 unused[(1024-32-sizeof(struct btrfs_scrub_progress))/8]; | 123 | __u64 unused[(1024-32-sizeof(struct btrfs_scrub_progress))/8]; |
124 | }; | 124 | }; |
125 | 125 | ||
126 | #define BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS 0 | ||
127 | #define BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID 1 | ||
128 | struct btrfs_ioctl_dev_replace_start_params { | ||
129 | __u64 srcdevid; /* in, if 0, use srcdev_name instead */ | ||
130 | __u8 srcdev_name[BTRFS_PATH_NAME_MAX + 1]; /* in */ | ||
131 | __u8 tgtdev_name[BTRFS_PATH_NAME_MAX + 1]; /* in */ | ||
132 | __u64 cont_reading_from_srcdev_mode; /* in, see #define | ||
133 | * above */ | ||
134 | }; | ||
135 | |||
136 | #define BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED 0 | ||
137 | #define BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED 1 | ||
138 | #define BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED 2 | ||
139 | #define BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED 3 | ||
140 | #define BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED 4 | ||
141 | struct btrfs_ioctl_dev_replace_status_params { | ||
142 | __u64 replace_state; /* out, see #define above */ | ||
143 | __u64 progress_1000; /* out, 0 <= x <= 1000 */ | ||
144 | __u64 time_started; /* out, seconds since 1-Jan-1970 */ | ||
145 | __u64 time_stopped; /* out, seconds since 1-Jan-1970 */ | ||
146 | __u64 num_write_errors; /* out */ | ||
147 | __u64 num_uncorrectable_read_errors; /* out */ | ||
148 | }; | ||
149 | |||
150 | #define BTRFS_IOCTL_DEV_REPLACE_CMD_START 0 | ||
151 | #define BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS 1 | ||
152 | #define BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL 2 | ||
153 | #define BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR 0 | ||
154 | #define BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED 1 | ||
155 | #define BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED 2 | ||
156 | struct btrfs_ioctl_dev_replace_args { | ||
157 | __u64 cmd; /* in */ | ||
158 | __u64 result; /* out */ | ||
159 | |||
160 | union { | ||
161 | struct btrfs_ioctl_dev_replace_start_params start; | ||
162 | struct btrfs_ioctl_dev_replace_status_params status; | ||
163 | }; /* in/out */ | ||
164 | |||
165 | __u64 spare[64]; | ||
166 | }; | ||
167 | |||
126 | #define BTRFS_DEVICE_PATH_NAME_MAX 1024 | 168 | #define BTRFS_DEVICE_PATH_NAME_MAX 1024 |
127 | struct btrfs_ioctl_dev_info_args { | 169 | struct btrfs_ioctl_dev_info_args { |
128 | __u64 devid; /* in/out */ | 170 | __u64 devid; /* in/out */ |
@@ -453,4 +495,7 @@ struct btrfs_ioctl_send_args { | |||
453 | struct btrfs_ioctl_qgroup_limit_args) | 495 | struct btrfs_ioctl_qgroup_limit_args) |
454 | #define BTRFS_IOC_GET_DEV_STATS _IOWR(BTRFS_IOCTL_MAGIC, 52, \ | 496 | #define BTRFS_IOC_GET_DEV_STATS _IOWR(BTRFS_IOCTL_MAGIC, 52, \ |
455 | struct btrfs_ioctl_get_dev_stats) | 497 | struct btrfs_ioctl_get_dev_stats) |
498 | #define BTRFS_IOC_DEV_REPLACE _IOWR(BTRFS_IOCTL_MAGIC, 53, \ | ||
499 | struct btrfs_ioctl_dev_replace_args) | ||
500 | |||
456 | #endif | 501 | #endif |
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 415862885b67..5777e6a9aab1 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c | |||
@@ -1537,6 +1537,53 @@ error_undo: | |||
1537 | goto error_brelse; | 1537 | goto error_brelse; |
1538 | } | 1538 | } |
1539 | 1539 | ||
1540 | void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info, | ||
1541 | struct btrfs_device *srcdev) | ||
1542 | { | ||
1543 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); | ||
1544 | list_del_rcu(&srcdev->dev_list); | ||
1545 | list_del_rcu(&srcdev->dev_alloc_list); | ||
1546 | fs_info->fs_devices->num_devices--; | ||
1547 | if (srcdev->missing) { | ||
1548 | fs_info->fs_devices->missing_devices--; | ||
1549 | fs_info->fs_devices->rw_devices++; | ||
1550 | } | ||
1551 | if (srcdev->can_discard) | ||
1552 | fs_info->fs_devices->num_can_discard--; | ||
1553 | if (srcdev->bdev) | ||
1554 | fs_info->fs_devices->open_devices--; | ||
1555 | |||
1556 | call_rcu(&srcdev->rcu, free_device); | ||
1557 | } | ||
1558 | |||
1559 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, | ||
1560 | struct btrfs_device *tgtdev) | ||
1561 | { | ||
1562 | struct btrfs_device *next_device; | ||
1563 | |||
1564 | WARN_ON(!tgtdev); | ||
1565 | mutex_lock(&fs_info->fs_devices->device_list_mutex); | ||
1566 | if (tgtdev->bdev) { | ||
1567 | btrfs_scratch_superblock(tgtdev); | ||
1568 | fs_info->fs_devices->open_devices--; | ||
1569 | } | ||
1570 | fs_info->fs_devices->num_devices--; | ||
1571 | if (tgtdev->can_discard) | ||
1572 | fs_info->fs_devices->num_can_discard++; | ||
1573 | |||
1574 | next_device = list_entry(fs_info->fs_devices->devices.next, | ||
1575 | struct btrfs_device, dev_list); | ||
1576 | if (tgtdev->bdev == fs_info->sb->s_bdev) | ||
1577 | fs_info->sb->s_bdev = next_device->bdev; | ||
1578 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) | ||
1579 | fs_info->fs_devices->latest_bdev = next_device->bdev; | ||
1580 | list_del_rcu(&tgtdev->dev_list); | ||
1581 | |||
1582 | call_rcu(&tgtdev->rcu, free_device); | ||
1583 | |||
1584 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | ||
1585 | } | ||
1586 | |||
1540 | int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, | 1587 | int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
1541 | struct btrfs_device **device) | 1588 | struct btrfs_device **device) |
1542 | { | 1589 | { |
@@ -1931,6 +1978,98 @@ error: | |||
1931 | return ret; | 1978 | return ret; |
1932 | } | 1979 | } |
1933 | 1980 | ||
1981 | int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, | ||
1982 | struct btrfs_device **device_out) | ||
1983 | { | ||
1984 | struct request_queue *q; | ||
1985 | struct btrfs_device *device; | ||
1986 | struct block_device *bdev; | ||
1987 | struct btrfs_fs_info *fs_info = root->fs_info; | ||
1988 | struct list_head *devices; | ||
1989 | struct rcu_string *name; | ||
1990 | int ret = 0; | ||
1991 | |||
1992 | *device_out = NULL; | ||
1993 | if (fs_info->fs_devices->seeding) | ||
1994 | return -EINVAL; | ||
1995 | |||
1996 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, | ||
1997 | fs_info->bdev_holder); | ||
1998 | if (IS_ERR(bdev)) | ||
1999 | return PTR_ERR(bdev); | ||
2000 | |||
2001 | filemap_write_and_wait(bdev->bd_inode->i_mapping); | ||
2002 | |||
2003 | devices = &fs_info->fs_devices->devices; | ||
2004 | list_for_each_entry(device, devices, dev_list) { | ||
2005 | if (device->bdev == bdev) { | ||
2006 | ret = -EEXIST; | ||
2007 | goto error; | ||
2008 | } | ||
2009 | } | ||
2010 | |||
2011 | device = kzalloc(sizeof(*device), GFP_NOFS); | ||
2012 | if (!device) { | ||
2013 | ret = -ENOMEM; | ||
2014 | goto error; | ||
2015 | } | ||
2016 | |||
2017 | name = rcu_string_strdup(device_path, GFP_NOFS); | ||
2018 | if (!name) { | ||
2019 | kfree(device); | ||
2020 | ret = -ENOMEM; | ||
2021 | goto error; | ||
2022 | } | ||
2023 | rcu_assign_pointer(device->name, name); | ||
2024 | |||
2025 | q = bdev_get_queue(bdev); | ||
2026 | if (blk_queue_discard(q)) | ||
2027 | device->can_discard = 1; | ||
2028 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); | ||
2029 | device->writeable = 1; | ||
2030 | device->work.func = pending_bios_fn; | ||
2031 | generate_random_uuid(device->uuid); | ||
2032 | device->devid = BTRFS_DEV_REPLACE_DEVID; | ||
2033 | spin_lock_init(&device->io_lock); | ||
2034 | device->generation = 0; | ||
2035 | device->io_width = root->sectorsize; | ||
2036 | device->io_align = root->sectorsize; | ||
2037 | device->sector_size = root->sectorsize; | ||
2038 | device->total_bytes = i_size_read(bdev->bd_inode); | ||
2039 | device->disk_total_bytes = device->total_bytes; | ||
2040 | device->dev_root = fs_info->dev_root; | ||
2041 | device->bdev = bdev; | ||
2042 | device->in_fs_metadata = 1; | ||
2043 | device->is_tgtdev_for_dev_replace = 1; | ||
2044 | device->mode = FMODE_EXCL; | ||
2045 | set_blocksize(device->bdev, 4096); | ||
2046 | device->fs_devices = fs_info->fs_devices; | ||
2047 | list_add(&device->dev_list, &fs_info->fs_devices->devices); | ||
2048 | fs_info->fs_devices->num_devices++; | ||
2049 | fs_info->fs_devices->open_devices++; | ||
2050 | if (device->can_discard) | ||
2051 | fs_info->fs_devices->num_can_discard++; | ||
2052 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); | ||
2053 | |||
2054 | *device_out = device; | ||
2055 | return ret; | ||
2056 | |||
2057 | error: | ||
2058 | blkdev_put(bdev, FMODE_EXCL); | ||
2059 | return ret; | ||
2060 | } | ||
2061 | |||
2062 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, | ||
2063 | struct btrfs_device *tgtdev) | ||
2064 | { | ||
2065 | WARN_ON(fs_info->fs_devices->rw_devices == 0); | ||
2066 | tgtdev->io_width = fs_info->dev_root->sectorsize; | ||
2067 | tgtdev->io_align = fs_info->dev_root->sectorsize; | ||
2068 | tgtdev->sector_size = fs_info->dev_root->sectorsize; | ||
2069 | tgtdev->dev_root = fs_info->dev_root; | ||
2070 | tgtdev->in_fs_metadata = 1; | ||
2071 | } | ||
2072 | |||
1934 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, | 2073 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
1935 | struct btrfs_device *device) | 2074 | struct btrfs_device *device) |
1936 | { | 2075 | { |
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index 8fd5a4d8acc8..58d79375deaf 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h | |||
@@ -286,6 +286,8 @@ struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid, | |||
286 | u8 *uuid, u8 *fsid); | 286 | u8 *uuid, u8 *fsid); |
287 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size); | 287 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size); |
288 | int btrfs_init_new_device(struct btrfs_root *root, char *path); | 288 | int btrfs_init_new_device(struct btrfs_root *root, char *path); |
289 | int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, | ||
290 | struct btrfs_device **device_out); | ||
289 | int btrfs_balance(struct btrfs_balance_control *bctl, | 291 | int btrfs_balance(struct btrfs_balance_control *bctl, |
290 | struct btrfs_ioctl_balance_args *bargs); | 292 | struct btrfs_ioctl_balance_args *bargs); |
291 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info); | 293 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info); |
@@ -302,6 +304,12 @@ int btrfs_get_dev_stats(struct btrfs_root *root, | |||
302 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info); | 304 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info); |
303 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, | 305 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
304 | struct btrfs_fs_info *fs_info); | 306 | struct btrfs_fs_info *fs_info); |
307 | void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info, | ||
308 | struct btrfs_device *srcdev); | ||
309 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, | ||
310 | struct btrfs_device *tgtdev); | ||
311 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, | ||
312 | struct btrfs_device *tgtdev); | ||
305 | int btrfs_scratch_superblock(struct btrfs_device *device); | 313 | int btrfs_scratch_superblock(struct btrfs_device *device); |
306 | 314 | ||
307 | static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, | 315 | static inline void btrfs_dev_stat_inc(struct btrfs_device *dev, |