xref: /openbmc/linux/fs/btrfs/dev-replace.c (revision 1888709d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STRATO AG 2012.  All rights reserved.
4  */
5 
6 #include <linux/sched.h>
7 #include <linux/bio.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/kthread.h>
11 #include <linux/math64.h>
12 #include "misc.h"
13 #include "ctree.h"
14 #include "extent_map.h"
15 #include "disk-io.h"
16 #include "transaction.h"
17 #include "print-tree.h"
18 #include "volumes.h"
19 #include "async-thread.h"
20 #include "check-integrity.h"
21 #include "rcu-string.h"
22 #include "dev-replace.h"
23 #include "sysfs.h"
24 
25 /*
26  * Device replace overview
27  *
28  * [Objective]
29  * To copy all extents (both new and on-disk) from source device to target
30  * device, while still keeping the filesystem read-write.
31  *
32  * [Method]
33  * There are two main methods involved:
34  *
35  * - Write duplication
36  *
37  *   All new writes will be written to both target and source devices, so even
38  *   if replace gets canceled, sources device still contans up-to-date data.
39  *
40  *   Location:		handle_ops_on_dev_replace() from __btrfs_map_block()
41  *   Start:		btrfs_dev_replace_start()
42  *   End:		btrfs_dev_replace_finishing()
43  *   Content:		Latest data/metadata
44  *
45  * - Copy existing extents
46  *
47  *   This happens by re-using scrub facility, as scrub also iterates through
48  *   existing extents from commit root.
49  *
50  *   Location:		scrub_write_block_to_dev_replace() from
51  *   			scrub_block_complete()
52  *   Content:		Data/meta from commit root.
53  *
54  * Due to the content difference, we need to avoid nocow write when dev-replace
55  * is happening.  This is done by marking the block group read-only and waiting
56  * for NOCOW writes.
57  *
58  * After replace is done, the finishing part is done by swapping the target and
59  * source devices.
60  *
61  *   Location:		btrfs_dev_replace_update_device_in_mapping_tree() from
62  *   			btrfs_dev_replace_finishing()
63  */
64 
65 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
66 				       int scrub_ret);
67 static void btrfs_dev_replace_update_device_in_mapping_tree(
68 						struct btrfs_fs_info *fs_info,
69 						struct btrfs_device *srcdev,
70 						struct btrfs_device *tgtdev);
71 static int btrfs_dev_replace_kthread(void *data);
72 
73 int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
74 {
75 	struct btrfs_key key;
76 	struct btrfs_root *dev_root = fs_info->dev_root;
77 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
78 	struct extent_buffer *eb;
79 	int slot;
80 	int ret = 0;
81 	struct btrfs_path *path = NULL;
82 	int item_size;
83 	struct btrfs_dev_replace_item *ptr;
84 	u64 src_devid;
85 
86 	path = btrfs_alloc_path();
87 	if (!path) {
88 		ret = -ENOMEM;
89 		goto out;
90 	}
91 
92 	key.objectid = 0;
93 	key.type = BTRFS_DEV_REPLACE_KEY;
94 	key.offset = 0;
95 	ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
96 	if (ret) {
97 no_valid_dev_replace_entry_found:
98 		ret = 0;
99 		dev_replace->replace_state =
100 			BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
101 		dev_replace->cont_reading_from_srcdev_mode =
102 		    BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
103 		dev_replace->time_started = 0;
104 		dev_replace->time_stopped = 0;
105 		atomic64_set(&dev_replace->num_write_errors, 0);
106 		atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
107 		dev_replace->cursor_left = 0;
108 		dev_replace->committed_cursor_left = 0;
109 		dev_replace->cursor_left_last_write_of_item = 0;
110 		dev_replace->cursor_right = 0;
111 		dev_replace->srcdev = NULL;
112 		dev_replace->tgtdev = NULL;
113 		dev_replace->is_valid = 0;
114 		dev_replace->item_needs_writeback = 0;
115 		goto out;
116 	}
117 	slot = path->slots[0];
118 	eb = path->nodes[0];
119 	item_size = btrfs_item_size_nr(eb, slot);
120 	ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_replace_item);
121 
122 	if (item_size != sizeof(struct btrfs_dev_replace_item)) {
123 		btrfs_warn(fs_info,
124 			"dev_replace entry found has unexpected size, ignore entry");
125 		goto no_valid_dev_replace_entry_found;
126 	}
127 
128 	src_devid = btrfs_dev_replace_src_devid(eb, ptr);
129 	dev_replace->cont_reading_from_srcdev_mode =
130 		btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
131 	dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
132 	dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
133 	dev_replace->time_stopped =
134 		btrfs_dev_replace_time_stopped(eb, ptr);
135 	atomic64_set(&dev_replace->num_write_errors,
136 		     btrfs_dev_replace_num_write_errors(eb, ptr));
137 	atomic64_set(&dev_replace->num_uncorrectable_read_errors,
138 		     btrfs_dev_replace_num_uncorrectable_read_errors(eb, ptr));
139 	dev_replace->cursor_left = btrfs_dev_replace_cursor_left(eb, ptr);
140 	dev_replace->committed_cursor_left = dev_replace->cursor_left;
141 	dev_replace->cursor_left_last_write_of_item = dev_replace->cursor_left;
142 	dev_replace->cursor_right = btrfs_dev_replace_cursor_right(eb, ptr);
143 	dev_replace->is_valid = 1;
144 
145 	dev_replace->item_needs_writeback = 0;
146 	switch (dev_replace->replace_state) {
147 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
148 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
149 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
150 		dev_replace->srcdev = NULL;
151 		dev_replace->tgtdev = NULL;
152 		break;
153 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
154 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
155 		dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
156 						src_devid, NULL, NULL, true);
157 		dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
158 							BTRFS_DEV_REPLACE_DEVID,
159 							NULL, NULL, true);
160 		/*
161 		 * allow 'btrfs dev replace_cancel' if src/tgt device is
162 		 * missing
163 		 */
164 		if (!dev_replace->srcdev &&
165 		    !btrfs_test_opt(fs_info, DEGRADED)) {
166 			ret = -EIO;
167 			btrfs_warn(fs_info,
168 			   "cannot mount because device replace operation is ongoing and");
169 			btrfs_warn(fs_info,
170 			   "srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
171 			   src_devid);
172 		}
173 		if (!dev_replace->tgtdev &&
174 		    !btrfs_test_opt(fs_info, DEGRADED)) {
175 			ret = -EIO;
176 			btrfs_warn(fs_info,
177 			   "cannot mount because device replace operation is ongoing and");
178 			btrfs_warn(fs_info,
179 			   "tgtdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
180 				BTRFS_DEV_REPLACE_DEVID);
181 		}
182 		if (dev_replace->tgtdev) {
183 			if (dev_replace->srcdev) {
184 				dev_replace->tgtdev->total_bytes =
185 					dev_replace->srcdev->total_bytes;
186 				dev_replace->tgtdev->disk_total_bytes =
187 					dev_replace->srcdev->disk_total_bytes;
188 				dev_replace->tgtdev->commit_total_bytes =
189 					dev_replace->srcdev->commit_total_bytes;
190 				dev_replace->tgtdev->bytes_used =
191 					dev_replace->srcdev->bytes_used;
192 				dev_replace->tgtdev->commit_bytes_used =
193 					dev_replace->srcdev->commit_bytes_used;
194 			}
195 			set_bit(BTRFS_DEV_STATE_REPLACE_TGT,
196 				&dev_replace->tgtdev->dev_state);
197 
198 			WARN_ON(fs_info->fs_devices->rw_devices == 0);
199 			dev_replace->tgtdev->io_width = fs_info->sectorsize;
200 			dev_replace->tgtdev->io_align = fs_info->sectorsize;
201 			dev_replace->tgtdev->sector_size = fs_info->sectorsize;
202 			dev_replace->tgtdev->fs_info = fs_info;
203 			set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
204 				&dev_replace->tgtdev->dev_state);
205 		}
206 		break;
207 	}
208 
209 out:
210 	btrfs_free_path(path);
211 	return ret;
212 }
213 
214 /*
215  * Initialize a new device for device replace target from a given source dev
216  * and path.
217  *
218  * Return 0 and new device in @device_out, otherwise return < 0
219  */
220 static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
221 				  const char *device_path,
222 				  struct btrfs_device *srcdev,
223 				  struct btrfs_device **device_out)
224 {
225 	struct btrfs_device *device;
226 	struct block_device *bdev;
227 	struct rcu_string *name;
228 	u64 devid = BTRFS_DEV_REPLACE_DEVID;
229 	int ret = 0;
230 
231 	*device_out = NULL;
232 	if (srcdev->fs_devices->seeding) {
233 		btrfs_err(fs_info, "the filesystem is a seed filesystem!");
234 		return -EINVAL;
235 	}
236 
237 	bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
238 				  fs_info->bdev_holder);
239 	if (IS_ERR(bdev)) {
240 		btrfs_err(fs_info, "target device %s is invalid!", device_path);
241 		return PTR_ERR(bdev);
242 	}
243 
244 	sync_blockdev(bdev);
245 
246 	list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
247 		if (device->bdev == bdev) {
248 			btrfs_err(fs_info,
249 				  "target device is in the filesystem!");
250 			ret = -EEXIST;
251 			goto error;
252 		}
253 	}
254 
255 
256 	if (i_size_read(bdev->bd_inode) <
257 	    btrfs_device_get_total_bytes(srcdev)) {
258 		btrfs_err(fs_info,
259 			  "target device is smaller than source device!");
260 		ret = -EINVAL;
261 		goto error;
262 	}
263 
264 
265 	device = btrfs_alloc_device(NULL, &devid, NULL);
266 	if (IS_ERR(device)) {
267 		ret = PTR_ERR(device);
268 		goto error;
269 	}
270 
271 	name = rcu_string_strdup(device_path, GFP_KERNEL);
272 	if (!name) {
273 		btrfs_free_device(device);
274 		ret = -ENOMEM;
275 		goto error;
276 	}
277 	rcu_assign_pointer(device->name, name);
278 
279 	set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
280 	device->generation = 0;
281 	device->io_width = fs_info->sectorsize;
282 	device->io_align = fs_info->sectorsize;
283 	device->sector_size = fs_info->sectorsize;
284 	device->total_bytes = btrfs_device_get_total_bytes(srcdev);
285 	device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
286 	device->bytes_used = btrfs_device_get_bytes_used(srcdev);
287 	device->commit_total_bytes = srcdev->commit_total_bytes;
288 	device->commit_bytes_used = device->bytes_used;
289 	device->fs_info = fs_info;
290 	device->bdev = bdev;
291 	set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
292 	set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
293 	device->mode = FMODE_EXCL;
294 	device->dev_stats_valid = 1;
295 	set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
296 	device->fs_devices = fs_info->fs_devices;
297 
298 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
299 	list_add(&device->dev_list, &fs_info->fs_devices->devices);
300 	fs_info->fs_devices->num_devices++;
301 	fs_info->fs_devices->open_devices++;
302 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
303 
304 	*device_out = device;
305 	return 0;
306 
307 error:
308 	blkdev_put(bdev, FMODE_EXCL);
309 	return ret;
310 }
311 
312 /*
313  * called from commit_transaction. Writes changed device replace state to
314  * disk.
315  */
316 int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
317 {
318 	struct btrfs_fs_info *fs_info = trans->fs_info;
319 	int ret;
320 	struct btrfs_root *dev_root = fs_info->dev_root;
321 	struct btrfs_path *path;
322 	struct btrfs_key key;
323 	struct extent_buffer *eb;
324 	struct btrfs_dev_replace_item *ptr;
325 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
326 
327 	down_read(&dev_replace->rwsem);
328 	if (!dev_replace->is_valid ||
329 	    !dev_replace->item_needs_writeback) {
330 		up_read(&dev_replace->rwsem);
331 		return 0;
332 	}
333 	up_read(&dev_replace->rwsem);
334 
335 	key.objectid = 0;
336 	key.type = BTRFS_DEV_REPLACE_KEY;
337 	key.offset = 0;
338 
339 	path = btrfs_alloc_path();
340 	if (!path) {
341 		ret = -ENOMEM;
342 		goto out;
343 	}
344 	ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
345 	if (ret < 0) {
346 		btrfs_warn(fs_info,
347 			   "error %d while searching for dev_replace item!",
348 			   ret);
349 		goto out;
350 	}
351 
352 	if (ret == 0 &&
353 	    btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
354 		/*
355 		 * need to delete old one and insert a new one.
356 		 * Since no attempt is made to recover any old state, if the
357 		 * dev_replace state is 'running', the data on the target
358 		 * drive is lost.
359 		 * It would be possible to recover the state: just make sure
360 		 * that the beginning of the item is never changed and always
361 		 * contains all the essential information. Then read this
362 		 * minimal set of information and use it as a base for the
363 		 * new state.
364 		 */
365 		ret = btrfs_del_item(trans, dev_root, path);
366 		if (ret != 0) {
367 			btrfs_warn(fs_info,
368 				   "delete too small dev_replace item failed %d!",
369 				   ret);
370 			goto out;
371 		}
372 		ret = 1;
373 	}
374 
375 	if (ret == 1) {
376 		/* need to insert a new item */
377 		btrfs_release_path(path);
378 		ret = btrfs_insert_empty_item(trans, dev_root, path,
379 					      &key, sizeof(*ptr));
380 		if (ret < 0) {
381 			btrfs_warn(fs_info,
382 				   "insert dev_replace item failed %d!", ret);
383 			goto out;
384 		}
385 	}
386 
387 	eb = path->nodes[0];
388 	ptr = btrfs_item_ptr(eb, path->slots[0],
389 			     struct btrfs_dev_replace_item);
390 
391 	down_write(&dev_replace->rwsem);
392 	if (dev_replace->srcdev)
393 		btrfs_set_dev_replace_src_devid(eb, ptr,
394 			dev_replace->srcdev->devid);
395 	else
396 		btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
397 	btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
398 		dev_replace->cont_reading_from_srcdev_mode);
399 	btrfs_set_dev_replace_replace_state(eb, ptr,
400 		dev_replace->replace_state);
401 	btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
402 	btrfs_set_dev_replace_time_stopped(eb, ptr, dev_replace->time_stopped);
403 	btrfs_set_dev_replace_num_write_errors(eb, ptr,
404 		atomic64_read(&dev_replace->num_write_errors));
405 	btrfs_set_dev_replace_num_uncorrectable_read_errors(eb, ptr,
406 		atomic64_read(&dev_replace->num_uncorrectable_read_errors));
407 	dev_replace->cursor_left_last_write_of_item =
408 		dev_replace->cursor_left;
409 	btrfs_set_dev_replace_cursor_left(eb, ptr,
410 		dev_replace->cursor_left_last_write_of_item);
411 	btrfs_set_dev_replace_cursor_right(eb, ptr,
412 		dev_replace->cursor_right);
413 	dev_replace->item_needs_writeback = 0;
414 	up_write(&dev_replace->rwsem);
415 
416 	btrfs_mark_buffer_dirty(eb);
417 
418 out:
419 	btrfs_free_path(path);
420 
421 	return ret;
422 }
423 
424 static char* btrfs_dev_name(struct btrfs_device *device)
425 {
426 	if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
427 		return "<missing disk>";
428 	else
429 		return rcu_str_deref(device->name);
430 }
431 
432 static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
433 		const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
434 		int read_src)
435 {
436 	struct btrfs_root *root = fs_info->dev_root;
437 	struct btrfs_trans_handle *trans;
438 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
439 	int ret;
440 	struct btrfs_device *tgt_device = NULL;
441 	struct btrfs_device *src_device = NULL;
442 
443 	src_device = btrfs_find_device_by_devspec(fs_info, srcdevid,
444 						  srcdev_name);
445 	if (IS_ERR(src_device))
446 		return PTR_ERR(src_device);
447 
448 	if (btrfs_pinned_by_swapfile(fs_info, src_device)) {
449 		btrfs_warn_in_rcu(fs_info,
450 	  "cannot replace device %s (devid %llu) due to active swapfile",
451 			btrfs_dev_name(src_device), src_device->devid);
452 		return -ETXTBSY;
453 	}
454 
455 	/*
456 	 * Here we commit the transaction to make sure commit_total_bytes
457 	 * of all the devices are updated.
458 	 */
459 	trans = btrfs_attach_transaction(root);
460 	if (!IS_ERR(trans)) {
461 		ret = btrfs_commit_transaction(trans);
462 		if (ret)
463 			return ret;
464 	} else if (PTR_ERR(trans) != -ENOENT) {
465 		return PTR_ERR(trans);
466 	}
467 
468 	ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
469 					    src_device, &tgt_device);
470 	if (ret)
471 		return ret;
472 
473 	down_write(&dev_replace->rwsem);
474 	switch (dev_replace->replace_state) {
475 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
476 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
477 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
478 		break;
479 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
480 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
481 		ASSERT(0);
482 		ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_ALREADY_STARTED;
483 		up_write(&dev_replace->rwsem);
484 		goto leave;
485 	}
486 
487 	dev_replace->cont_reading_from_srcdev_mode = read_src;
488 	dev_replace->srcdev = src_device;
489 	dev_replace->tgtdev = tgt_device;
490 
491 	btrfs_info_in_rcu(fs_info,
492 		      "dev_replace from %s (devid %llu) to %s started",
493 		      btrfs_dev_name(src_device),
494 		      src_device->devid,
495 		      rcu_str_deref(tgt_device->name));
496 
497 	/*
498 	 * from now on, the writes to the srcdev are all duplicated to
499 	 * go to the tgtdev as well (refer to btrfs_map_block()).
500 	 */
501 	dev_replace->replace_state = BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
502 	dev_replace->time_started = ktime_get_real_seconds();
503 	dev_replace->cursor_left = 0;
504 	dev_replace->committed_cursor_left = 0;
505 	dev_replace->cursor_left_last_write_of_item = 0;
506 	dev_replace->cursor_right = 0;
507 	dev_replace->is_valid = 1;
508 	dev_replace->item_needs_writeback = 1;
509 	atomic64_set(&dev_replace->num_write_errors, 0);
510 	atomic64_set(&dev_replace->num_uncorrectable_read_errors, 0);
511 	up_write(&dev_replace->rwsem);
512 
513 	ret = btrfs_sysfs_add_device(tgt_device);
514 	if (ret)
515 		btrfs_err(fs_info, "kobj add dev failed %d", ret);
516 
517 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
518 
519 	/* Commit dev_replace state and reserve 1 item for it. */
520 	trans = btrfs_start_transaction(root, 1);
521 	if (IS_ERR(trans)) {
522 		ret = PTR_ERR(trans);
523 		down_write(&dev_replace->rwsem);
524 		dev_replace->replace_state =
525 			BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
526 		dev_replace->srcdev = NULL;
527 		dev_replace->tgtdev = NULL;
528 		up_write(&dev_replace->rwsem);
529 		goto leave;
530 	}
531 
532 	ret = btrfs_commit_transaction(trans);
533 	WARN_ON(ret);
534 
535 	/* the disk copy procedure reuses the scrub code */
536 	ret = btrfs_scrub_dev(fs_info, src_device->devid, 0,
537 			      btrfs_device_get_total_bytes(src_device),
538 			      &dev_replace->scrub_progress, 0, 1);
539 
540 	ret = btrfs_dev_replace_finishing(fs_info, ret);
541 	if (ret == -EINPROGRESS)
542 		ret = BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS;
543 
544 	return ret;
545 
546 leave:
547 	btrfs_destroy_dev_replace_tgtdev(tgt_device);
548 	return ret;
549 }
550 
551 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
552 			    struct btrfs_ioctl_dev_replace_args *args)
553 {
554 	int ret;
555 
556 	switch (args->start.cont_reading_from_srcdev_mode) {
557 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
558 	case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
559 		break;
560 	default:
561 		return -EINVAL;
562 	}
563 
564 	if ((args->start.srcdevid == 0 && args->start.srcdev_name[0] == '\0') ||
565 	    args->start.tgtdev_name[0] == '\0')
566 		return -EINVAL;
567 
568 	ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
569 					args->start.srcdevid,
570 					args->start.srcdev_name,
571 					args->start.cont_reading_from_srcdev_mode);
572 	args->result = ret;
573 	/* don't warn if EINPROGRESS, someone else might be running scrub */
574 	if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
575 	    ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR)
576 		return 0;
577 
578 	return ret;
579 }
580 
581 /*
582  * blocked until all in-flight bios operations are finished.
583  */
584 static void btrfs_rm_dev_replace_blocked(struct btrfs_fs_info *fs_info)
585 {
586 	set_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
587 	wait_event(fs_info->dev_replace.replace_wait, !percpu_counter_sum(
588 		   &fs_info->dev_replace.bio_counter));
589 }
590 
591 /*
592  * we have removed target device, it is safe to allow new bios request.
593  */
594 static void btrfs_rm_dev_replace_unblocked(struct btrfs_fs_info *fs_info)
595 {
596 	clear_bit(BTRFS_FS_STATE_DEV_REPLACING, &fs_info->fs_state);
597 	wake_up(&fs_info->dev_replace.replace_wait);
598 }
599 
600 /*
601  * When finishing the device replace, before swapping the source device with the
602  * target device we must update the chunk allocation state in the target device,
603  * as it is empty because replace works by directly copying the chunks and not
604  * through the normal chunk allocation path.
605  */
606 static int btrfs_set_target_alloc_state(struct btrfs_device *srcdev,
607 					struct btrfs_device *tgtdev)
608 {
609 	struct extent_state *cached_state = NULL;
610 	u64 start = 0;
611 	u64 found_start;
612 	u64 found_end;
613 	int ret = 0;
614 
615 	lockdep_assert_held(&srcdev->fs_info->chunk_mutex);
616 
617 	while (!find_first_extent_bit(&srcdev->alloc_state, start,
618 				      &found_start, &found_end,
619 				      CHUNK_ALLOCATED, &cached_state)) {
620 		ret = set_extent_bits(&tgtdev->alloc_state, found_start,
621 				      found_end, CHUNK_ALLOCATED);
622 		if (ret)
623 			break;
624 		start = found_end + 1;
625 	}
626 
627 	free_extent_state(cached_state);
628 	return ret;
629 }
630 
631 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
632 				       int scrub_ret)
633 {
634 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
635 	struct btrfs_device *tgt_device;
636 	struct btrfs_device *src_device;
637 	struct btrfs_root *root = fs_info->tree_root;
638 	u8 uuid_tmp[BTRFS_UUID_SIZE];
639 	struct btrfs_trans_handle *trans;
640 	int ret = 0;
641 
642 	/* don't allow cancel or unmount to disturb the finishing procedure */
643 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
644 
645 	down_read(&dev_replace->rwsem);
646 	/* was the operation canceled, or is it finished? */
647 	if (dev_replace->replace_state !=
648 	    BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED) {
649 		up_read(&dev_replace->rwsem);
650 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
651 		return 0;
652 	}
653 
654 	tgt_device = dev_replace->tgtdev;
655 	src_device = dev_replace->srcdev;
656 	up_read(&dev_replace->rwsem);
657 
658 	/*
659 	 * flush all outstanding I/O and inode extent mappings before the
660 	 * copy operation is declared as being finished
661 	 */
662 	ret = btrfs_start_delalloc_roots(fs_info, U64_MAX);
663 	if (ret) {
664 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
665 		return ret;
666 	}
667 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
668 
669 	/*
670 	 * We have to use this loop approach because at this point src_device
671 	 * has to be available for transaction commit to complete, yet new
672 	 * chunks shouldn't be allocated on the device.
673 	 */
674 	while (1) {
675 		trans = btrfs_start_transaction(root, 0);
676 		if (IS_ERR(trans)) {
677 			mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
678 			return PTR_ERR(trans);
679 		}
680 		ret = btrfs_commit_transaction(trans);
681 		WARN_ON(ret);
682 
683 		/* Prevent write_all_supers() during the finishing procedure */
684 		mutex_lock(&fs_info->fs_devices->device_list_mutex);
685 		/* Prevent new chunks being allocated on the source device */
686 		mutex_lock(&fs_info->chunk_mutex);
687 
688 		if (!list_empty(&src_device->post_commit_list)) {
689 			mutex_unlock(&fs_info->fs_devices->device_list_mutex);
690 			mutex_unlock(&fs_info->chunk_mutex);
691 		} else {
692 			break;
693 		}
694 	}
695 
696 	down_write(&dev_replace->rwsem);
697 	dev_replace->replace_state =
698 		scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
699 			  : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
700 	dev_replace->tgtdev = NULL;
701 	dev_replace->srcdev = NULL;
702 	dev_replace->time_stopped = ktime_get_real_seconds();
703 	dev_replace->item_needs_writeback = 1;
704 
705 	/*
706 	 * Update allocation state in the new device and replace the old device
707 	 * with the new one in the mapping tree.
708 	 */
709 	if (!scrub_ret) {
710 		scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device);
711 		if (scrub_ret)
712 			goto error;
713 		btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
714 								src_device,
715 								tgt_device);
716 	} else {
717 		if (scrub_ret != -ECANCELED)
718 			btrfs_err_in_rcu(fs_info,
719 				 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
720 				 btrfs_dev_name(src_device),
721 				 src_device->devid,
722 				 rcu_str_deref(tgt_device->name), scrub_ret);
723 error:
724 		up_write(&dev_replace->rwsem);
725 		mutex_unlock(&fs_info->chunk_mutex);
726 		mutex_unlock(&fs_info->fs_devices->device_list_mutex);
727 		btrfs_rm_dev_replace_blocked(fs_info);
728 		if (tgt_device)
729 			btrfs_destroy_dev_replace_tgtdev(tgt_device);
730 		btrfs_rm_dev_replace_unblocked(fs_info);
731 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
732 
733 		return scrub_ret;
734 	}
735 
736 	btrfs_info_in_rcu(fs_info,
737 			  "dev_replace from %s (devid %llu) to %s finished",
738 			  btrfs_dev_name(src_device),
739 			  src_device->devid,
740 			  rcu_str_deref(tgt_device->name));
741 	clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
742 	tgt_device->devid = src_device->devid;
743 	src_device->devid = BTRFS_DEV_REPLACE_DEVID;
744 	memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
745 	memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
746 	memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
747 	btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
748 	btrfs_device_set_disk_total_bytes(tgt_device,
749 					  src_device->disk_total_bytes);
750 	btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
751 	tgt_device->commit_bytes_used = src_device->bytes_used;
752 
753 	btrfs_assign_next_active_device(src_device, tgt_device);
754 
755 	list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
756 	fs_info->fs_devices->rw_devices++;
757 
758 	up_write(&dev_replace->rwsem);
759 	btrfs_rm_dev_replace_blocked(fs_info);
760 
761 	btrfs_rm_dev_replace_remove_srcdev(src_device);
762 
763 	btrfs_rm_dev_replace_unblocked(fs_info);
764 
765 	/*
766 	 * Increment dev_stats_ccnt so that btrfs_run_dev_stats() will
767 	 * update on-disk dev stats value during commit transaction
768 	 */
769 	atomic_inc(&tgt_device->dev_stats_ccnt);
770 
771 	/*
772 	 * this is again a consistent state where no dev_replace procedure
773 	 * is running, the target device is part of the filesystem, the
774 	 * source device is not part of the filesystem anymore and its 1st
775 	 * superblock is scratched out so that it is no longer marked to
776 	 * belong to this filesystem.
777 	 */
778 	mutex_unlock(&fs_info->chunk_mutex);
779 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
780 
781 	/* replace the sysfs entry */
782 	btrfs_sysfs_remove_device(src_device);
783 	btrfs_sysfs_update_devid(tgt_device);
784 	if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &src_device->dev_state))
785 		btrfs_scratch_superblocks(fs_info, src_device->bdev,
786 					  src_device->name->str);
787 
788 	/* write back the superblocks */
789 	trans = btrfs_start_transaction(root, 0);
790 	if (!IS_ERR(trans))
791 		btrfs_commit_transaction(trans);
792 
793 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
794 
795 	btrfs_rm_dev_replace_free_srcdev(src_device);
796 
797 	return 0;
798 }
799 
800 static void btrfs_dev_replace_update_device_in_mapping_tree(
801 						struct btrfs_fs_info *fs_info,
802 						struct btrfs_device *srcdev,
803 						struct btrfs_device *tgtdev)
804 {
805 	struct extent_map_tree *em_tree = &fs_info->mapping_tree;
806 	struct extent_map *em;
807 	struct map_lookup *map;
808 	u64 start = 0;
809 	int i;
810 
811 	write_lock(&em_tree->lock);
812 	do {
813 		em = lookup_extent_mapping(em_tree, start, (u64)-1);
814 		if (!em)
815 			break;
816 		map = em->map_lookup;
817 		for (i = 0; i < map->num_stripes; i++)
818 			if (srcdev == map->stripes[i].dev)
819 				map->stripes[i].dev = tgtdev;
820 		start = em->start + em->len;
821 		free_extent_map(em);
822 	} while (start);
823 	write_unlock(&em_tree->lock);
824 }
825 
826 /*
827  * Read progress of device replace status according to the state and last
828  * stored position. The value format is the same as for
829  * btrfs_dev_replace::progress_1000
830  */
831 static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
832 {
833 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
834 	u64 ret = 0;
835 
836 	switch (dev_replace->replace_state) {
837 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
838 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
839 		ret = 0;
840 		break;
841 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
842 		ret = 1000;
843 		break;
844 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
845 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
846 		ret = div64_u64(dev_replace->cursor_left,
847 				div_u64(btrfs_device_get_total_bytes(
848 						dev_replace->srcdev), 1000));
849 		break;
850 	}
851 
852 	return ret;
853 }
854 
855 void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
856 			      struct btrfs_ioctl_dev_replace_args *args)
857 {
858 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
859 
860 	down_read(&dev_replace->rwsem);
861 	/* even if !dev_replace_is_valid, the values are good enough for
862 	 * the replace_status ioctl */
863 	args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
864 	args->status.replace_state = dev_replace->replace_state;
865 	args->status.time_started = dev_replace->time_started;
866 	args->status.time_stopped = dev_replace->time_stopped;
867 	args->status.num_write_errors =
868 		atomic64_read(&dev_replace->num_write_errors);
869 	args->status.num_uncorrectable_read_errors =
870 		atomic64_read(&dev_replace->num_uncorrectable_read_errors);
871 	args->status.progress_1000 = btrfs_dev_replace_progress(fs_info);
872 	up_read(&dev_replace->rwsem);
873 }
874 
875 int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
876 {
877 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
878 	struct btrfs_device *tgt_device = NULL;
879 	struct btrfs_device *src_device = NULL;
880 	struct btrfs_trans_handle *trans;
881 	struct btrfs_root *root = fs_info->tree_root;
882 	int result;
883 	int ret;
884 
885 	if (sb_rdonly(fs_info->sb))
886 		return -EROFS;
887 
888 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
889 	down_write(&dev_replace->rwsem);
890 	switch (dev_replace->replace_state) {
891 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
892 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
893 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
894 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
895 		up_write(&dev_replace->rwsem);
896 		break;
897 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
898 		tgt_device = dev_replace->tgtdev;
899 		src_device = dev_replace->srcdev;
900 		up_write(&dev_replace->rwsem);
901 		ret = btrfs_scrub_cancel(fs_info);
902 		if (ret < 0) {
903 			result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NOT_STARTED;
904 		} else {
905 			result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
906 			/*
907 			 * btrfs_dev_replace_finishing() will handle the
908 			 * cleanup part
909 			 */
910 			btrfs_info_in_rcu(fs_info,
911 				"dev_replace from %s (devid %llu) to %s canceled",
912 				btrfs_dev_name(src_device), src_device->devid,
913 				btrfs_dev_name(tgt_device));
914 		}
915 		break;
916 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
917 		/*
918 		 * Scrub doing the replace isn't running so we need to do the
919 		 * cleanup step of btrfs_dev_replace_finishing() here
920 		 */
921 		result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
922 		tgt_device = dev_replace->tgtdev;
923 		src_device = dev_replace->srcdev;
924 		dev_replace->tgtdev = NULL;
925 		dev_replace->srcdev = NULL;
926 		dev_replace->replace_state =
927 				BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED;
928 		dev_replace->time_stopped = ktime_get_real_seconds();
929 		dev_replace->item_needs_writeback = 1;
930 
931 		up_write(&dev_replace->rwsem);
932 
933 		/* Scrub for replace must not be running in suspended state */
934 		ret = btrfs_scrub_cancel(fs_info);
935 		ASSERT(ret != -ENOTCONN);
936 
937 		trans = btrfs_start_transaction(root, 0);
938 		if (IS_ERR(trans)) {
939 			mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
940 			return PTR_ERR(trans);
941 		}
942 		ret = btrfs_commit_transaction(trans);
943 		WARN_ON(ret);
944 
945 		btrfs_info_in_rcu(fs_info,
946 		"suspended dev_replace from %s (devid %llu) to %s canceled",
947 			btrfs_dev_name(src_device), src_device->devid,
948 			btrfs_dev_name(tgt_device));
949 
950 		if (tgt_device)
951 			btrfs_destroy_dev_replace_tgtdev(tgt_device);
952 		break;
953 	default:
954 		up_write(&dev_replace->rwsem);
955 		result = -EINVAL;
956 	}
957 
958 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
959 	return result;
960 }
961 
962 void btrfs_dev_replace_suspend_for_unmount(struct btrfs_fs_info *fs_info)
963 {
964 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
965 
966 	mutex_lock(&dev_replace->lock_finishing_cancel_unmount);
967 	down_write(&dev_replace->rwsem);
968 
969 	switch (dev_replace->replace_state) {
970 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
971 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
972 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
973 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
974 		break;
975 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
976 		dev_replace->replace_state =
977 			BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
978 		dev_replace->time_stopped = ktime_get_real_seconds();
979 		dev_replace->item_needs_writeback = 1;
980 		btrfs_info(fs_info, "suspending dev_replace for unmount");
981 		break;
982 	}
983 
984 	up_write(&dev_replace->rwsem);
985 	mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
986 }
987 
988 /* resume dev_replace procedure that was interrupted by unmount */
989 int btrfs_resume_dev_replace_async(struct btrfs_fs_info *fs_info)
990 {
991 	struct task_struct *task;
992 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
993 
994 	down_write(&dev_replace->rwsem);
995 
996 	switch (dev_replace->replace_state) {
997 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
998 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
999 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1000 		up_write(&dev_replace->rwsem);
1001 		return 0;
1002 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1003 		break;
1004 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1005 		dev_replace->replace_state =
1006 			BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED;
1007 		break;
1008 	}
1009 	if (!dev_replace->tgtdev || !dev_replace->tgtdev->bdev) {
1010 		btrfs_info(fs_info,
1011 			   "cannot continue dev_replace, tgtdev is missing");
1012 		btrfs_info(fs_info,
1013 			   "you may cancel the operation after 'mount -o degraded'");
1014 		dev_replace->replace_state =
1015 					BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1016 		up_write(&dev_replace->rwsem);
1017 		return 0;
1018 	}
1019 	up_write(&dev_replace->rwsem);
1020 
1021 	/*
1022 	 * This could collide with a paused balance, but the exclusive op logic
1023 	 * should never allow both to start and pause. We don't want to allow
1024 	 * dev-replace to start anyway.
1025 	 */
1026 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
1027 		down_write(&dev_replace->rwsem);
1028 		dev_replace->replace_state =
1029 					BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED;
1030 		up_write(&dev_replace->rwsem);
1031 		btrfs_info(fs_info,
1032 		"cannot resume dev-replace, other exclusive operation running");
1033 		return 0;
1034 	}
1035 
1036 	task = kthread_run(btrfs_dev_replace_kthread, fs_info, "btrfs-devrepl");
1037 	return PTR_ERR_OR_ZERO(task);
1038 }
1039 
1040 static int btrfs_dev_replace_kthread(void *data)
1041 {
1042 	struct btrfs_fs_info *fs_info = data;
1043 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1044 	u64 progress;
1045 	int ret;
1046 
1047 	progress = btrfs_dev_replace_progress(fs_info);
1048 	progress = div_u64(progress, 10);
1049 	btrfs_info_in_rcu(fs_info,
1050 		"continuing dev_replace from %s (devid %llu) to target %s @%u%%",
1051 		btrfs_dev_name(dev_replace->srcdev),
1052 		dev_replace->srcdev->devid,
1053 		btrfs_dev_name(dev_replace->tgtdev),
1054 		(unsigned int)progress);
1055 
1056 	ret = btrfs_scrub_dev(fs_info, dev_replace->srcdev->devid,
1057 			      dev_replace->committed_cursor_left,
1058 			      btrfs_device_get_total_bytes(dev_replace->srcdev),
1059 			      &dev_replace->scrub_progress, 0, 1);
1060 	ret = btrfs_dev_replace_finishing(fs_info, ret);
1061 	WARN_ON(ret && ret != -ECANCELED);
1062 
1063 	btrfs_exclop_finish(fs_info);
1064 	return 0;
1065 }
1066 
1067 int __pure btrfs_dev_replace_is_ongoing(struct btrfs_dev_replace *dev_replace)
1068 {
1069 	if (!dev_replace->is_valid)
1070 		return 0;
1071 
1072 	switch (dev_replace->replace_state) {
1073 	case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
1074 	case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
1075 	case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
1076 		return 0;
1077 	case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
1078 	case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
1079 		/*
1080 		 * return true even if tgtdev is missing (this is
1081 		 * something that can happen if the dev_replace
1082 		 * procedure is suspended by an umount and then
1083 		 * the tgtdev is missing (or "btrfs dev scan") was
1084 		 * not called and the filesystem is remounted
1085 		 * in degraded state. This does not stop the
1086 		 * dev_replace procedure. It needs to be canceled
1087 		 * manually if the cancellation is wanted.
1088 		 */
1089 		break;
1090 	}
1091 	return 1;
1092 }
1093 
1094 void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info)
1095 {
1096 	percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1097 }
1098 
1099 void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount)
1100 {
1101 	percpu_counter_sub(&fs_info->dev_replace.bio_counter, amount);
1102 	cond_wake_up_nomb(&fs_info->dev_replace.replace_wait);
1103 }
1104 
1105 void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info)
1106 {
1107 	while (1) {
1108 		percpu_counter_inc(&fs_info->dev_replace.bio_counter);
1109 		if (likely(!test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1110 				     &fs_info->fs_state)))
1111 			break;
1112 
1113 		btrfs_bio_counter_dec(fs_info);
1114 		wait_event(fs_info->dev_replace.replace_wait,
1115 			   !test_bit(BTRFS_FS_STATE_DEV_REPLACING,
1116 				     &fs_info->fs_state));
1117 	}
1118 }
1119