]>
Commit | Line | Data |
---|---|---|
0b86a832 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. 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> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
8a4b83cc | 21 | #include <linux/buffer_head.h> |
f2d8d74d | 22 | #include <linux/blkdev.h> |
b765ead5 | 23 | #include <linux/iocontext.h> |
6f88a440 | 24 | #include <linux/capability.h> |
442a4f63 | 25 | #include <linux/ratelimit.h> |
59641015 | 26 | #include <linux/kthread.h> |
53b381b3 | 27 | #include <linux/raid/pq.h> |
803b2f54 | 28 | #include <linux/semaphore.h> |
8da4b8c4 | 29 | #include <linux/uuid.h> |
53b381b3 | 30 | #include <asm/div64.h> |
0b86a832 CM |
31 | #include "ctree.h" |
32 | #include "extent_map.h" | |
33 | #include "disk-io.h" | |
34 | #include "transaction.h" | |
35 | #include "print-tree.h" | |
36 | #include "volumes.h" | |
53b381b3 | 37 | #include "raid56.h" |
8b712842 | 38 | #include "async-thread.h" |
21adbd5c | 39 | #include "check-integrity.h" |
606686ee | 40 | #include "rcu-string.h" |
3fed40cc | 41 | #include "math.h" |
8dabb742 | 42 | #include "dev-replace.h" |
99994cde | 43 | #include "sysfs.h" |
0b86a832 | 44 | |
af902047 ZL |
45 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
46 | [BTRFS_RAID_RAID10] = { | |
47 | .sub_stripes = 2, | |
48 | .dev_stripes = 1, | |
49 | .devs_max = 0, /* 0 == as many as possible */ | |
50 | .devs_min = 4, | |
8789f4fe | 51 | .tolerated_failures = 1, |
af902047 ZL |
52 | .devs_increment = 2, |
53 | .ncopies = 2, | |
54 | }, | |
55 | [BTRFS_RAID_RAID1] = { | |
56 | .sub_stripes = 1, | |
57 | .dev_stripes = 1, | |
58 | .devs_max = 2, | |
59 | .devs_min = 2, | |
8789f4fe | 60 | .tolerated_failures = 1, |
af902047 ZL |
61 | .devs_increment = 2, |
62 | .ncopies = 2, | |
63 | }, | |
64 | [BTRFS_RAID_DUP] = { | |
65 | .sub_stripes = 1, | |
66 | .dev_stripes = 2, | |
67 | .devs_max = 1, | |
68 | .devs_min = 1, | |
8789f4fe | 69 | .tolerated_failures = 0, |
af902047 ZL |
70 | .devs_increment = 1, |
71 | .ncopies = 2, | |
72 | }, | |
73 | [BTRFS_RAID_RAID0] = { | |
74 | .sub_stripes = 1, | |
75 | .dev_stripes = 1, | |
76 | .devs_max = 0, | |
77 | .devs_min = 2, | |
8789f4fe | 78 | .tolerated_failures = 0, |
af902047 ZL |
79 | .devs_increment = 1, |
80 | .ncopies = 1, | |
81 | }, | |
82 | [BTRFS_RAID_SINGLE] = { | |
83 | .sub_stripes = 1, | |
84 | .dev_stripes = 1, | |
85 | .devs_max = 1, | |
86 | .devs_min = 1, | |
8789f4fe | 87 | .tolerated_failures = 0, |
af902047 ZL |
88 | .devs_increment = 1, |
89 | .ncopies = 1, | |
90 | }, | |
91 | [BTRFS_RAID_RAID5] = { | |
92 | .sub_stripes = 1, | |
93 | .dev_stripes = 1, | |
94 | .devs_max = 0, | |
95 | .devs_min = 2, | |
8789f4fe | 96 | .tolerated_failures = 1, |
af902047 | 97 | .devs_increment = 1, |
18d3abde | 98 | .ncopies = 1, |
af902047 ZL |
99 | }, |
100 | [BTRFS_RAID_RAID6] = { | |
101 | .sub_stripes = 1, | |
102 | .dev_stripes = 1, | |
103 | .devs_max = 0, | |
104 | .devs_min = 3, | |
8789f4fe | 105 | .tolerated_failures = 2, |
af902047 | 106 | .devs_increment = 1, |
18d3abde | 107 | .ncopies = 1, |
af902047 ZL |
108 | }, |
109 | }; | |
110 | ||
fb75d857 | 111 | const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = { |
af902047 ZL |
112 | [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10, |
113 | [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1, | |
114 | [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP, | |
115 | [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0, | |
116 | [BTRFS_RAID_SINGLE] = 0, | |
117 | [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5, | |
118 | [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6, | |
119 | }; | |
120 | ||
621292ba DS |
121 | /* |
122 | * Table to convert BTRFS_RAID_* to the error code if minimum number of devices | |
123 | * condition is not met. Zero means there's no corresponding | |
124 | * BTRFS_ERROR_DEV_*_NOT_MET value. | |
125 | */ | |
126 | const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = { | |
127 | [BTRFS_RAID_RAID10] = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, | |
128 | [BTRFS_RAID_RAID1] = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, | |
129 | [BTRFS_RAID_DUP] = 0, | |
130 | [BTRFS_RAID_RAID0] = 0, | |
131 | [BTRFS_RAID_SINGLE] = 0, | |
132 | [BTRFS_RAID_RAID5] = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, | |
133 | [BTRFS_RAID_RAID6] = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, | |
134 | }; | |
135 | ||
2b82032c | 136 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
e4a4dce7 | 137 | struct btrfs_fs_info *fs_info); |
2ff7e61e | 138 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
733f4fbb | 139 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
48a3b636 | 140 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
733f4fbb | 141 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
5ab56090 LB |
142 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
143 | enum btrfs_map_op op, | |
144 | u64 logical, u64 *length, | |
145 | struct btrfs_bio **bbio_ret, | |
146 | int mirror_num, int need_raid_map); | |
2b82032c | 147 | |
67a2c45e | 148 | DEFINE_MUTEX(uuid_mutex); |
8a4b83cc | 149 | static LIST_HEAD(fs_uuids); |
c73eccf7 AJ |
150 | struct list_head *btrfs_get_fs_uuids(void) |
151 | { | |
152 | return &fs_uuids; | |
153 | } | |
8a4b83cc | 154 | |
2dfeca9b DS |
155 | /* |
156 | * alloc_fs_devices - allocate struct btrfs_fs_devices | |
157 | * @fsid: if not NULL, copy the uuid to fs_devices::fsid | |
158 | * | |
159 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). | |
160 | * The returned struct is not linked onto any lists and can be destroyed with | |
161 | * kfree() right away. | |
162 | */ | |
163 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) | |
2208a378 ID |
164 | { |
165 | struct btrfs_fs_devices *fs_devs; | |
166 | ||
78f2c9e6 | 167 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
2208a378 ID |
168 | if (!fs_devs) |
169 | return ERR_PTR(-ENOMEM); | |
170 | ||
171 | mutex_init(&fs_devs->device_list_mutex); | |
172 | ||
173 | INIT_LIST_HEAD(&fs_devs->devices); | |
935e5cc9 | 174 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
2208a378 ID |
175 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
176 | INIT_LIST_HEAD(&fs_devs->list); | |
2208a378 ID |
177 | if (fsid) |
178 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); | |
2208a378 ID |
179 | |
180 | return fs_devs; | |
181 | } | |
182 | ||
e4404d6e YZ |
183 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
184 | { | |
185 | struct btrfs_device *device; | |
186 | WARN_ON(fs_devices->opened); | |
187 | while (!list_empty(&fs_devices->devices)) { | |
188 | device = list_entry(fs_devices->devices.next, | |
189 | struct btrfs_device, dev_list); | |
190 | list_del(&device->dev_list); | |
606686ee | 191 | rcu_string_free(device->name); |
3065ae5b | 192 | bio_put(device->flush_bio); |
e4404d6e YZ |
193 | kfree(device); |
194 | } | |
195 | kfree(fs_devices); | |
196 | } | |
197 | ||
b8b8ff59 LC |
198 | static void btrfs_kobject_uevent(struct block_device *bdev, |
199 | enum kobject_action action) | |
200 | { | |
201 | int ret; | |
202 | ||
203 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); | |
204 | if (ret) | |
efe120a0 | 205 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
b8b8ff59 LC |
206 | action, |
207 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), | |
208 | &disk_to_dev(bdev->bd_disk)->kobj); | |
209 | } | |
210 | ||
143bede5 | 211 | void btrfs_cleanup_fs_uuids(void) |
8a4b83cc CM |
212 | { |
213 | struct btrfs_fs_devices *fs_devices; | |
8a4b83cc | 214 | |
2b82032c YZ |
215 | while (!list_empty(&fs_uuids)) { |
216 | fs_devices = list_entry(fs_uuids.next, | |
217 | struct btrfs_fs_devices, list); | |
218 | list_del(&fs_devices->list); | |
e4404d6e | 219 | free_fs_devices(fs_devices); |
8a4b83cc | 220 | } |
8a4b83cc CM |
221 | } |
222 | ||
12bd2fc0 ID |
223 | static struct btrfs_device *__alloc_device(void) |
224 | { | |
225 | struct btrfs_device *dev; | |
226 | ||
78f2c9e6 | 227 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
12bd2fc0 ID |
228 | if (!dev) |
229 | return ERR_PTR(-ENOMEM); | |
230 | ||
e0ae9994 DS |
231 | /* |
232 | * Preallocate a bio that's always going to be used for flushing device | |
233 | * barriers and matches the device lifespan | |
234 | */ | |
235 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); | |
236 | if (!dev->flush_bio) { | |
237 | kfree(dev); | |
238 | return ERR_PTR(-ENOMEM); | |
239 | } | |
e0ae9994 | 240 | |
12bd2fc0 ID |
241 | INIT_LIST_HEAD(&dev->dev_list); |
242 | INIT_LIST_HEAD(&dev->dev_alloc_list); | |
935e5cc9 | 243 | INIT_LIST_HEAD(&dev->resized_list); |
12bd2fc0 ID |
244 | |
245 | spin_lock_init(&dev->io_lock); | |
246 | ||
247 | spin_lock_init(&dev->reada_lock); | |
248 | atomic_set(&dev->reada_in_flight, 0); | |
addc3fa7 | 249 | atomic_set(&dev->dev_stats_ccnt, 0); |
546bed63 | 250 | btrfs_device_data_ordered_init(dev); |
9bcaaea7 | 251 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
d0164adc | 252 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
12bd2fc0 ID |
253 | |
254 | return dev; | |
255 | } | |
256 | ||
a1b32a59 | 257 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
8a4b83cc | 258 | { |
8a4b83cc CM |
259 | struct btrfs_fs_devices *fs_devices; |
260 | ||
c6e30871 | 261 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
8a4b83cc CM |
262 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
263 | return fs_devices; | |
264 | } | |
265 | return NULL; | |
266 | } | |
267 | ||
beaf8ab3 SB |
268 | static int |
269 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, | |
270 | int flush, struct block_device **bdev, | |
271 | struct buffer_head **bh) | |
272 | { | |
273 | int ret; | |
274 | ||
275 | *bdev = blkdev_get_by_path(device_path, flags, holder); | |
276 | ||
277 | if (IS_ERR(*bdev)) { | |
278 | ret = PTR_ERR(*bdev); | |
beaf8ab3 SB |
279 | goto error; |
280 | } | |
281 | ||
282 | if (flush) | |
283 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); | |
9f6d2510 | 284 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
beaf8ab3 SB |
285 | if (ret) { |
286 | blkdev_put(*bdev, flags); | |
287 | goto error; | |
288 | } | |
289 | invalidate_bdev(*bdev); | |
290 | *bh = btrfs_read_dev_super(*bdev); | |
92fc03fb AJ |
291 | if (IS_ERR(*bh)) { |
292 | ret = PTR_ERR(*bh); | |
beaf8ab3 SB |
293 | blkdev_put(*bdev, flags); |
294 | goto error; | |
295 | } | |
296 | ||
297 | return 0; | |
298 | ||
299 | error: | |
300 | *bdev = NULL; | |
301 | *bh = NULL; | |
302 | return ret; | |
303 | } | |
304 | ||
ffbd517d CM |
305 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
306 | struct bio *head, struct bio *tail) | |
307 | { | |
308 | ||
309 | struct bio *old_head; | |
310 | ||
311 | old_head = pending_bios->head; | |
312 | pending_bios->head = head; | |
313 | if (pending_bios->tail) | |
314 | tail->bi_next = old_head; | |
315 | else | |
316 | pending_bios->tail = tail; | |
317 | } | |
318 | ||
8b712842 CM |
319 | /* |
320 | * we try to collect pending bios for a device so we don't get a large | |
321 | * number of procs sending bios down to the same device. This greatly | |
322 | * improves the schedulers ability to collect and merge the bios. | |
323 | * | |
324 | * But, it also turns into a long list of bios to process and that is sure | |
325 | * to eventually make the worker thread block. The solution here is to | |
326 | * make some progress and then put this work struct back at the end of | |
327 | * the list if the block device is congested. This way, multiple devices | |
328 | * can make progress from a single worker thread. | |
329 | */ | |
143bede5 | 330 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
8b712842 | 331 | { |
0b246afa | 332 | struct btrfs_fs_info *fs_info = device->fs_info; |
8b712842 CM |
333 | struct bio *pending; |
334 | struct backing_dev_info *bdi; | |
ffbd517d | 335 | struct btrfs_pending_bios *pending_bios; |
8b712842 CM |
336 | struct bio *tail; |
337 | struct bio *cur; | |
338 | int again = 0; | |
ffbd517d | 339 | unsigned long num_run; |
d644d8a1 | 340 | unsigned long batch_run = 0; |
b765ead5 | 341 | unsigned long last_waited = 0; |
d84275c9 | 342 | int force_reg = 0; |
0e588859 | 343 | int sync_pending = 0; |
211588ad CM |
344 | struct blk_plug plug; |
345 | ||
346 | /* | |
347 | * this function runs all the bios we've collected for | |
348 | * a particular device. We don't want to wander off to | |
349 | * another device without first sending all of these down. | |
350 | * So, setup a plug here and finish it off before we return | |
351 | */ | |
352 | blk_start_plug(&plug); | |
8b712842 | 353 | |
efa7c9f9 | 354 | bdi = device->bdev->bd_bdi; |
b64a2851 | 355 | |
8b712842 CM |
356 | loop: |
357 | spin_lock(&device->io_lock); | |
358 | ||
a6837051 | 359 | loop_lock: |
d84275c9 | 360 | num_run = 0; |
ffbd517d | 361 | |
8b712842 CM |
362 | /* take all the bios off the list at once and process them |
363 | * later on (without the lock held). But, remember the | |
364 | * tail and other pointers so the bios can be properly reinserted | |
365 | * into the list if we hit congestion | |
366 | */ | |
d84275c9 | 367 | if (!force_reg && device->pending_sync_bios.head) { |
ffbd517d | 368 | pending_bios = &device->pending_sync_bios; |
d84275c9 CM |
369 | force_reg = 1; |
370 | } else { | |
ffbd517d | 371 | pending_bios = &device->pending_bios; |
d84275c9 CM |
372 | force_reg = 0; |
373 | } | |
ffbd517d CM |
374 | |
375 | pending = pending_bios->head; | |
376 | tail = pending_bios->tail; | |
8b712842 | 377 | WARN_ON(pending && !tail); |
8b712842 CM |
378 | |
379 | /* | |
380 | * if pending was null this time around, no bios need processing | |
381 | * at all and we can stop. Otherwise it'll loop back up again | |
382 | * and do an additional check so no bios are missed. | |
383 | * | |
384 | * device->running_pending is used to synchronize with the | |
385 | * schedule_bio code. | |
386 | */ | |
ffbd517d CM |
387 | if (device->pending_sync_bios.head == NULL && |
388 | device->pending_bios.head == NULL) { | |
8b712842 CM |
389 | again = 0; |
390 | device->running_pending = 0; | |
ffbd517d CM |
391 | } else { |
392 | again = 1; | |
393 | device->running_pending = 1; | |
8b712842 | 394 | } |
ffbd517d CM |
395 | |
396 | pending_bios->head = NULL; | |
397 | pending_bios->tail = NULL; | |
398 | ||
8b712842 CM |
399 | spin_unlock(&device->io_lock); |
400 | ||
d397712b | 401 | while (pending) { |
ffbd517d CM |
402 | |
403 | rmb(); | |
d84275c9 CM |
404 | /* we want to work on both lists, but do more bios on the |
405 | * sync list than the regular list | |
406 | */ | |
407 | if ((num_run > 32 && | |
408 | pending_bios != &device->pending_sync_bios && | |
409 | device->pending_sync_bios.head) || | |
410 | (num_run > 64 && pending_bios == &device->pending_sync_bios && | |
411 | device->pending_bios.head)) { | |
ffbd517d CM |
412 | spin_lock(&device->io_lock); |
413 | requeue_list(pending_bios, pending, tail); | |
414 | goto loop_lock; | |
415 | } | |
416 | ||
8b712842 CM |
417 | cur = pending; |
418 | pending = pending->bi_next; | |
419 | cur->bi_next = NULL; | |
b64a2851 | 420 | |
dac56212 | 421 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
d644d8a1 | 422 | |
2ab1ba68 CM |
423 | /* |
424 | * if we're doing the sync list, record that our | |
425 | * plug has some sync requests on it | |
426 | * | |
427 | * If we're doing the regular list and there are | |
428 | * sync requests sitting around, unplug before | |
429 | * we add more | |
430 | */ | |
431 | if (pending_bios == &device->pending_sync_bios) { | |
432 | sync_pending = 1; | |
433 | } else if (sync_pending) { | |
434 | blk_finish_plug(&plug); | |
435 | blk_start_plug(&plug); | |
436 | sync_pending = 0; | |
437 | } | |
438 | ||
4e49ea4a | 439 | btrfsic_submit_bio(cur); |
5ff7ba3a CM |
440 | num_run++; |
441 | batch_run++; | |
853d8ec4 DS |
442 | |
443 | cond_resched(); | |
8b712842 CM |
444 | |
445 | /* | |
446 | * we made progress, there is more work to do and the bdi | |
447 | * is now congested. Back off and let other work structs | |
448 | * run instead | |
449 | */ | |
57fd5a5f | 450 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
5f2cc086 | 451 | fs_info->fs_devices->open_devices > 1) { |
b765ead5 | 452 | struct io_context *ioc; |
8b712842 | 453 | |
b765ead5 CM |
454 | ioc = current->io_context; |
455 | ||
456 | /* | |
457 | * the main goal here is that we don't want to | |
458 | * block if we're going to be able to submit | |
459 | * more requests without blocking. | |
460 | * | |
461 | * This code does two great things, it pokes into | |
462 | * the elevator code from a filesystem _and_ | |
463 | * it makes assumptions about how batching works. | |
464 | */ | |
465 | if (ioc && ioc->nr_batch_requests > 0 && | |
466 | time_before(jiffies, ioc->last_waited + HZ/50UL) && | |
467 | (last_waited == 0 || | |
468 | ioc->last_waited == last_waited)) { | |
469 | /* | |
470 | * we want to go through our batch of | |
471 | * requests and stop. So, we copy out | |
472 | * the ioc->last_waited time and test | |
473 | * against it before looping | |
474 | */ | |
475 | last_waited = ioc->last_waited; | |
853d8ec4 | 476 | cond_resched(); |
b765ead5 CM |
477 | continue; |
478 | } | |
8b712842 | 479 | spin_lock(&device->io_lock); |
ffbd517d | 480 | requeue_list(pending_bios, pending, tail); |
a6837051 | 481 | device->running_pending = 1; |
8b712842 CM |
482 | |
483 | spin_unlock(&device->io_lock); | |
a8c93d4e QW |
484 | btrfs_queue_work(fs_info->submit_workers, |
485 | &device->work); | |
8b712842 CM |
486 | goto done; |
487 | } | |
488 | } | |
ffbd517d | 489 | |
51684082 CM |
490 | cond_resched(); |
491 | if (again) | |
492 | goto loop; | |
493 | ||
494 | spin_lock(&device->io_lock); | |
495 | if (device->pending_bios.head || device->pending_sync_bios.head) | |
496 | goto loop_lock; | |
497 | spin_unlock(&device->io_lock); | |
498 | ||
8b712842 | 499 | done: |
211588ad | 500 | blk_finish_plug(&plug); |
8b712842 CM |
501 | } |
502 | ||
b2950863 | 503 | static void pending_bios_fn(struct btrfs_work *work) |
8b712842 CM |
504 | { |
505 | struct btrfs_device *device; | |
506 | ||
507 | device = container_of(work, struct btrfs_device, work); | |
508 | run_scheduled_bios(device); | |
509 | } | |
510 | ||
4fde46f0 | 511 | |
c9162bdf | 512 | static void btrfs_free_stale_device(struct btrfs_device *cur_dev) |
4fde46f0 AJ |
513 | { |
514 | struct btrfs_fs_devices *fs_devs; | |
515 | struct btrfs_device *dev; | |
516 | ||
517 | if (!cur_dev->name) | |
518 | return; | |
519 | ||
520 | list_for_each_entry(fs_devs, &fs_uuids, list) { | |
521 | int del = 1; | |
522 | ||
523 | if (fs_devs->opened) | |
524 | continue; | |
525 | if (fs_devs->seeding) | |
526 | continue; | |
527 | ||
528 | list_for_each_entry(dev, &fs_devs->devices, dev_list) { | |
529 | ||
530 | if (dev == cur_dev) | |
531 | continue; | |
532 | if (!dev->name) | |
533 | continue; | |
534 | ||
535 | /* | |
536 | * Todo: This won't be enough. What if the same device | |
537 | * comes back (with new uuid and) with its mapper path? | |
538 | * But for now, this does help as mostly an admin will | |
539 | * either use mapper or non mapper path throughout. | |
540 | */ | |
541 | rcu_read_lock(); | |
542 | del = strcmp(rcu_str_deref(dev->name), | |
543 | rcu_str_deref(cur_dev->name)); | |
544 | rcu_read_unlock(); | |
545 | if (!del) | |
546 | break; | |
547 | } | |
548 | ||
549 | if (!del) { | |
550 | /* delete the stale device */ | |
551 | if (fs_devs->num_devices == 1) { | |
552 | btrfs_sysfs_remove_fsid(fs_devs); | |
553 | list_del(&fs_devs->list); | |
554 | free_fs_devices(fs_devs); | |
6b2f4cac | 555 | break; |
4fde46f0 AJ |
556 | } else { |
557 | fs_devs->num_devices--; | |
558 | list_del(&dev->dev_list); | |
559 | rcu_string_free(dev->name); | |
3065ae5b | 560 | bio_put(dev->flush_bio); |
4fde46f0 AJ |
561 | kfree(dev); |
562 | } | |
563 | break; | |
564 | } | |
565 | } | |
566 | } | |
567 | ||
60999ca4 DS |
568 | /* |
569 | * Add new device to list of registered devices | |
570 | * | |
571 | * Returns: | |
572 | * 1 - first time device is seen | |
573 | * 0 - device already known | |
574 | * < 0 - error | |
575 | */ | |
a1b32a59 | 576 | static noinline int device_list_add(const char *path, |
8a4b83cc CM |
577 | struct btrfs_super_block *disk_super, |
578 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) | |
579 | { | |
580 | struct btrfs_device *device; | |
581 | struct btrfs_fs_devices *fs_devices; | |
606686ee | 582 | struct rcu_string *name; |
60999ca4 | 583 | int ret = 0; |
8a4b83cc CM |
584 | u64 found_transid = btrfs_super_generation(disk_super); |
585 | ||
586 | fs_devices = find_fsid(disk_super->fsid); | |
587 | if (!fs_devices) { | |
2208a378 ID |
588 | fs_devices = alloc_fs_devices(disk_super->fsid); |
589 | if (IS_ERR(fs_devices)) | |
590 | return PTR_ERR(fs_devices); | |
591 | ||
8a4b83cc | 592 | list_add(&fs_devices->list, &fs_uuids); |
2208a378 | 593 | |
8a4b83cc CM |
594 | device = NULL; |
595 | } else { | |
34857e15 AJ |
596 | device = btrfs_find_device(fs_devices, devid, |
597 | disk_super->dev_item.uuid, NULL, false); | |
8a4b83cc | 598 | } |
443f24fe | 599 | |
8a4b83cc | 600 | if (!device) { |
2b82032c YZ |
601 | if (fs_devices->opened) |
602 | return -EBUSY; | |
603 | ||
12bd2fc0 ID |
604 | device = btrfs_alloc_device(NULL, &devid, |
605 | disk_super->dev_item.uuid); | |
606 | if (IS_ERR(device)) { | |
8a4b83cc | 607 | /* we can safely leave the fs_devices entry around */ |
12bd2fc0 | 608 | return PTR_ERR(device); |
8a4b83cc | 609 | } |
606686ee JB |
610 | |
611 | name = rcu_string_strdup(path, GFP_NOFS); | |
612 | if (!name) { | |
3065ae5b | 613 | bio_put(device->flush_bio); |
8a4b83cc CM |
614 | kfree(device); |
615 | return -ENOMEM; | |
616 | } | |
606686ee | 617 | rcu_assign_pointer(device->name, name); |
90519d66 | 618 | |
e5e9a520 | 619 | mutex_lock(&fs_devices->device_list_mutex); |
1f78160c | 620 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
f7171750 | 621 | fs_devices->num_devices++; |
e5e9a520 CM |
622 | mutex_unlock(&fs_devices->device_list_mutex); |
623 | ||
60999ca4 | 624 | ret = 1; |
2b82032c | 625 | device->fs_devices = fs_devices; |
606686ee | 626 | } else if (!device->name || strcmp(device->name->str, path)) { |
b96de000 AJ |
627 | /* |
628 | * When FS is already mounted. | |
629 | * 1. If you are here and if the device->name is NULL that | |
630 | * means this device was missing at time of FS mount. | |
631 | * 2. If you are here and if the device->name is different | |
632 | * from 'path' that means either | |
633 | * a. The same device disappeared and reappeared with | |
634 | * different name. or | |
635 | * b. The missing-disk-which-was-replaced, has | |
636 | * reappeared now. | |
637 | * | |
638 | * We must allow 1 and 2a above. But 2b would be a spurious | |
639 | * and unintentional. | |
640 | * | |
641 | * Further in case of 1 and 2a above, the disk at 'path' | |
642 | * would have missed some transaction when it was away and | |
643 | * in case of 2a the stale bdev has to be updated as well. | |
644 | * 2b must not be allowed at all time. | |
645 | */ | |
646 | ||
647 | /* | |
0f23ae74 CM |
648 | * For now, we do allow update to btrfs_fs_device through the |
649 | * btrfs dev scan cli after FS has been mounted. We're still | |
650 | * tracking a problem where systems fail mount by subvolume id | |
651 | * when we reject replacement on a mounted FS. | |
b96de000 | 652 | */ |
0f23ae74 | 653 | if (!fs_devices->opened && found_transid < device->generation) { |
77bdae4d AJ |
654 | /* |
655 | * That is if the FS is _not_ mounted and if you | |
656 | * are here, that means there is more than one | |
657 | * disk with same uuid and devid.We keep the one | |
658 | * with larger generation number or the last-in if | |
659 | * generation are equal. | |
660 | */ | |
0f23ae74 | 661 | return -EEXIST; |
77bdae4d | 662 | } |
b96de000 | 663 | |
606686ee | 664 | name = rcu_string_strdup(path, GFP_NOFS); |
3a0524dc TH |
665 | if (!name) |
666 | return -ENOMEM; | |
606686ee JB |
667 | rcu_string_free(device->name); |
668 | rcu_assign_pointer(device->name, name); | |
cd02dca5 CM |
669 | if (device->missing) { |
670 | fs_devices->missing_devices--; | |
671 | device->missing = 0; | |
672 | } | |
8a4b83cc CM |
673 | } |
674 | ||
77bdae4d AJ |
675 | /* |
676 | * Unmount does not free the btrfs_device struct but would zero | |
677 | * generation along with most of the other members. So just update | |
678 | * it back. We need it to pick the disk with largest generation | |
679 | * (as above). | |
680 | */ | |
681 | if (!fs_devices->opened) | |
682 | device->generation = found_transid; | |
683 | ||
4fde46f0 AJ |
684 | /* |
685 | * if there is new btrfs on an already registered device, | |
686 | * then remove the stale device entry. | |
687 | */ | |
02feae3c AJ |
688 | if (ret > 0) |
689 | btrfs_free_stale_device(device); | |
4fde46f0 | 690 | |
8a4b83cc | 691 | *fs_devices_ret = fs_devices; |
60999ca4 DS |
692 | |
693 | return ret; | |
8a4b83cc CM |
694 | } |
695 | ||
e4404d6e YZ |
696 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
697 | { | |
698 | struct btrfs_fs_devices *fs_devices; | |
699 | struct btrfs_device *device; | |
700 | struct btrfs_device *orig_dev; | |
701 | ||
2208a378 ID |
702 | fs_devices = alloc_fs_devices(orig->fsid); |
703 | if (IS_ERR(fs_devices)) | |
704 | return fs_devices; | |
e4404d6e | 705 | |
adbbb863 | 706 | mutex_lock(&orig->device_list_mutex); |
02db0844 | 707 | fs_devices->total_devices = orig->total_devices; |
e4404d6e | 708 | |
46224705 | 709 | /* We have held the volume lock, it is safe to get the devices. */ |
e4404d6e | 710 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
606686ee JB |
711 | struct rcu_string *name; |
712 | ||
12bd2fc0 ID |
713 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
714 | orig_dev->uuid); | |
715 | if (IS_ERR(device)) | |
e4404d6e YZ |
716 | goto error; |
717 | ||
606686ee JB |
718 | /* |
719 | * This is ok to do without rcu read locked because we hold the | |
720 | * uuid mutex so nothing we touch in here is going to disappear. | |
721 | */ | |
e755f780 | 722 | if (orig_dev->name) { |
78f2c9e6 DS |
723 | name = rcu_string_strdup(orig_dev->name->str, |
724 | GFP_KERNEL); | |
e755f780 | 725 | if (!name) { |
3065ae5b | 726 | bio_put(device->flush_bio); |
e755f780 AJ |
727 | kfree(device); |
728 | goto error; | |
729 | } | |
730 | rcu_assign_pointer(device->name, name); | |
fd2696f3 | 731 | } |
e4404d6e | 732 | |
e4404d6e YZ |
733 | list_add(&device->dev_list, &fs_devices->devices); |
734 | device->fs_devices = fs_devices; | |
735 | fs_devices->num_devices++; | |
736 | } | |
adbbb863 | 737 | mutex_unlock(&orig->device_list_mutex); |
e4404d6e YZ |
738 | return fs_devices; |
739 | error: | |
adbbb863 | 740 | mutex_unlock(&orig->device_list_mutex); |
e4404d6e YZ |
741 | free_fs_devices(fs_devices); |
742 | return ERR_PTR(-ENOMEM); | |
743 | } | |
744 | ||
9eaed21e | 745 | void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step) |
dfe25020 | 746 | { |
c6e30871 | 747 | struct btrfs_device *device, *next; |
443f24fe | 748 | struct btrfs_device *latest_dev = NULL; |
a6b0d5c8 | 749 | |
dfe25020 CM |
750 | mutex_lock(&uuid_mutex); |
751 | again: | |
46224705 | 752 | /* This is the initialized path, it is safe to release the devices. */ |
c6e30871 | 753 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
a6b0d5c8 | 754 | if (device->in_fs_metadata) { |
63a212ab | 755 | if (!device->is_tgtdev_for_dev_replace && |
443f24fe MX |
756 | (!latest_dev || |
757 | device->generation > latest_dev->generation)) { | |
758 | latest_dev = device; | |
a6b0d5c8 | 759 | } |
2b82032c | 760 | continue; |
a6b0d5c8 | 761 | } |
2b82032c | 762 | |
8dabb742 SB |
763 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
764 | /* | |
765 | * In the first step, keep the device which has | |
766 | * the correct fsid and the devid that is used | |
767 | * for the dev_replace procedure. | |
768 | * In the second step, the dev_replace state is | |
769 | * read from the device tree and it is known | |
770 | * whether the procedure is really active or | |
771 | * not, which means whether this device is | |
772 | * used or whether it should be removed. | |
773 | */ | |
774 | if (step == 0 || device->is_tgtdev_for_dev_replace) { | |
775 | continue; | |
776 | } | |
777 | } | |
2b82032c | 778 | if (device->bdev) { |
d4d77629 | 779 | blkdev_put(device->bdev, device->mode); |
2b82032c YZ |
780 | device->bdev = NULL; |
781 | fs_devices->open_devices--; | |
782 | } | |
783 | if (device->writeable) { | |
784 | list_del_init(&device->dev_alloc_list); | |
785 | device->writeable = 0; | |
8dabb742 SB |
786 | if (!device->is_tgtdev_for_dev_replace) |
787 | fs_devices->rw_devices--; | |
2b82032c | 788 | } |
e4404d6e YZ |
789 | list_del_init(&device->dev_list); |
790 | fs_devices->num_devices--; | |
606686ee | 791 | rcu_string_free(device->name); |
3065ae5b | 792 | bio_put(device->flush_bio); |
e4404d6e | 793 | kfree(device); |
dfe25020 | 794 | } |
2b82032c YZ |
795 | |
796 | if (fs_devices->seed) { | |
797 | fs_devices = fs_devices->seed; | |
2b82032c YZ |
798 | goto again; |
799 | } | |
800 | ||
443f24fe | 801 | fs_devices->latest_bdev = latest_dev->bdev; |
a6b0d5c8 | 802 | |
dfe25020 | 803 | mutex_unlock(&uuid_mutex); |
dfe25020 | 804 | } |
a0af469b | 805 | |
1f78160c XG |
806 | static void __free_device(struct work_struct *work) |
807 | { | |
808 | struct btrfs_device *device; | |
809 | ||
810 | device = container_of(work, struct btrfs_device, rcu_work); | |
606686ee | 811 | rcu_string_free(device->name); |
e0ae9994 | 812 | bio_put(device->flush_bio); |
1f78160c XG |
813 | kfree(device); |
814 | } | |
815 | ||
816 | static void free_device(struct rcu_head *head) | |
817 | { | |
818 | struct btrfs_device *device; | |
819 | ||
820 | device = container_of(head, struct btrfs_device, rcu); | |
821 | ||
822 | INIT_WORK(&device->rcu_work, __free_device); | |
823 | schedule_work(&device->rcu_work); | |
824 | } | |
825 | ||
14238819 AJ |
826 | static void btrfs_close_bdev(struct btrfs_device *device) |
827 | { | |
828 | if (device->bdev && device->writeable) { | |
829 | sync_blockdev(device->bdev); | |
830 | invalidate_bdev(device->bdev); | |
831 | } | |
832 | ||
833 | if (device->bdev) | |
834 | blkdev_put(device->bdev, device->mode); | |
835 | } | |
836 | ||
0ccd0528 | 837 | static void btrfs_prepare_close_one_device(struct btrfs_device *device) |
f448341a AJ |
838 | { |
839 | struct btrfs_fs_devices *fs_devices = device->fs_devices; | |
840 | struct btrfs_device *new_device; | |
841 | struct rcu_string *name; | |
842 | ||
843 | if (device->bdev) | |
844 | fs_devices->open_devices--; | |
845 | ||
846 | if (device->writeable && | |
847 | device->devid != BTRFS_DEV_REPLACE_DEVID) { | |
848 | list_del_init(&device->dev_alloc_list); | |
849 | fs_devices->rw_devices--; | |
850 | } | |
851 | ||
852 | if (device->missing) | |
853 | fs_devices->missing_devices--; | |
854 | ||
855 | new_device = btrfs_alloc_device(NULL, &device->devid, | |
856 | device->uuid); | |
857 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ | |
858 | ||
859 | /* Safe because we are under uuid_mutex */ | |
860 | if (device->name) { | |
861 | name = rcu_string_strdup(device->name->str, GFP_NOFS); | |
862 | BUG_ON(!name); /* -ENOMEM */ | |
863 | rcu_assign_pointer(new_device->name, name); | |
864 | } | |
865 | ||
866 | list_replace_rcu(&device->dev_list, &new_device->dev_list); | |
867 | new_device->fs_devices = device->fs_devices; | |
f448341a AJ |
868 | } |
869 | ||
2b82032c | 870 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
8a4b83cc | 871 | { |
2037a093 | 872 | struct btrfs_device *device, *tmp; |
0ccd0528 AJ |
873 | struct list_head pending_put; |
874 | ||
875 | INIT_LIST_HEAD(&pending_put); | |
e4404d6e | 876 | |
2b82032c YZ |
877 | if (--fs_devices->opened > 0) |
878 | return 0; | |
8a4b83cc | 879 | |
c9513edb | 880 | mutex_lock(&fs_devices->device_list_mutex); |
2037a093 | 881 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
0ccd0528 AJ |
882 | btrfs_prepare_close_one_device(device); |
883 | list_add(&device->dev_list, &pending_put); | |
8a4b83cc | 884 | } |
c9513edb XG |
885 | mutex_unlock(&fs_devices->device_list_mutex); |
886 | ||
0ccd0528 AJ |
887 | /* |
888 | * btrfs_show_devname() is using the device_list_mutex, | |
889 | * sometimes call to blkdev_put() leads vfs calling | |
890 | * into this func. So do put outside of device_list_mutex, | |
891 | * as of now. | |
892 | */ | |
893 | while (!list_empty(&pending_put)) { | |
894 | device = list_first_entry(&pending_put, | |
895 | struct btrfs_device, dev_list); | |
896 | list_del(&device->dev_list); | |
897 | btrfs_close_bdev(device); | |
898 | call_rcu(&device->rcu, free_device); | |
899 | } | |
900 | ||
e4404d6e YZ |
901 | WARN_ON(fs_devices->open_devices); |
902 | WARN_ON(fs_devices->rw_devices); | |
2b82032c YZ |
903 | fs_devices->opened = 0; |
904 | fs_devices->seeding = 0; | |
2b82032c | 905 | |
8a4b83cc CM |
906 | return 0; |
907 | } | |
908 | ||
2b82032c YZ |
909 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
910 | { | |
e4404d6e | 911 | struct btrfs_fs_devices *seed_devices = NULL; |
2b82032c YZ |
912 | int ret; |
913 | ||
914 | mutex_lock(&uuid_mutex); | |
915 | ret = __btrfs_close_devices(fs_devices); | |
e4404d6e YZ |
916 | if (!fs_devices->opened) { |
917 | seed_devices = fs_devices->seed; | |
918 | fs_devices->seed = NULL; | |
919 | } | |
2b82032c | 920 | mutex_unlock(&uuid_mutex); |
e4404d6e YZ |
921 | |
922 | while (seed_devices) { | |
923 | fs_devices = seed_devices; | |
924 | seed_devices = fs_devices->seed; | |
925 | __btrfs_close_devices(fs_devices); | |
926 | free_fs_devices(fs_devices); | |
927 | } | |
bc178622 ES |
928 | /* |
929 | * Wait for rcu kworkers under __btrfs_close_devices | |
930 | * to finish all blkdev_puts so device is really | |
931 | * free when umount is done. | |
932 | */ | |
933 | rcu_barrier(); | |
2b82032c YZ |
934 | return ret; |
935 | } | |
936 | ||
e4404d6e YZ |
937 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
938 | fmode_t flags, void *holder) | |
8a4b83cc | 939 | { |
d5e2003c | 940 | struct request_queue *q; |
8a4b83cc CM |
941 | struct block_device *bdev; |
942 | struct list_head *head = &fs_devices->devices; | |
8a4b83cc | 943 | struct btrfs_device *device; |
443f24fe | 944 | struct btrfs_device *latest_dev = NULL; |
a0af469b CM |
945 | struct buffer_head *bh; |
946 | struct btrfs_super_block *disk_super; | |
a0af469b | 947 | u64 devid; |
2b82032c | 948 | int seeding = 1; |
a0af469b | 949 | int ret = 0; |
8a4b83cc | 950 | |
d4d77629 TH |
951 | flags |= FMODE_EXCL; |
952 | ||
c6e30871 | 953 | list_for_each_entry(device, head, dev_list) { |
c1c4d91c CM |
954 | if (device->bdev) |
955 | continue; | |
dfe25020 CM |
956 | if (!device->name) |
957 | continue; | |
958 | ||
f63e0cca ES |
959 | /* Just open everything we can; ignore failures here */ |
960 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, | |
961 | &bdev, &bh)) | |
beaf8ab3 | 962 | continue; |
a0af469b CM |
963 | |
964 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
a343832f | 965 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
a0af469b CM |
966 | if (devid != device->devid) |
967 | goto error_brelse; | |
968 | ||
2b82032c YZ |
969 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
970 | BTRFS_UUID_SIZE)) | |
971 | goto error_brelse; | |
972 | ||
973 | device->generation = btrfs_super_generation(disk_super); | |
443f24fe MX |
974 | if (!latest_dev || |
975 | device->generation > latest_dev->generation) | |
976 | latest_dev = device; | |
a0af469b | 977 | |
2b82032c YZ |
978 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
979 | device->writeable = 0; | |
980 | } else { | |
981 | device->writeable = !bdev_read_only(bdev); | |
982 | seeding = 0; | |
983 | } | |
984 | ||
d5e2003c | 985 | q = bdev_get_queue(bdev); |
90180da4 | 986 | if (blk_queue_discard(q)) |
d5e2003c | 987 | device->can_discard = 1; |
e884f4f0 AJ |
988 | if (!blk_queue_nonrot(q)) |
989 | fs_devices->rotating = 1; | |
d5e2003c | 990 | |
8a4b83cc | 991 | device->bdev = bdev; |
dfe25020 | 992 | device->in_fs_metadata = 0; |
15916de8 CM |
993 | device->mode = flags; |
994 | ||
a0af469b | 995 | fs_devices->open_devices++; |
55e50e45 ID |
996 | if (device->writeable && |
997 | device->devid != BTRFS_DEV_REPLACE_DEVID) { | |
2b82032c YZ |
998 | fs_devices->rw_devices++; |
999 | list_add(&device->dev_alloc_list, | |
1000 | &fs_devices->alloc_list); | |
1001 | } | |
4f6c9328 | 1002 | brelse(bh); |
a0af469b | 1003 | continue; |
a061fc8d | 1004 | |
a0af469b CM |
1005 | error_brelse: |
1006 | brelse(bh); | |
d4d77629 | 1007 | blkdev_put(bdev, flags); |
a0af469b | 1008 | continue; |
8a4b83cc | 1009 | } |
a0af469b | 1010 | if (fs_devices->open_devices == 0) { |
20bcd649 | 1011 | ret = -EINVAL; |
a0af469b CM |
1012 | goto out; |
1013 | } | |
2b82032c YZ |
1014 | fs_devices->seeding = seeding; |
1015 | fs_devices->opened = 1; | |
443f24fe | 1016 | fs_devices->latest_bdev = latest_dev->bdev; |
2b82032c | 1017 | fs_devices->total_rw_bytes = 0; |
a0af469b | 1018 | out: |
2b82032c YZ |
1019 | return ret; |
1020 | } | |
1021 | ||
1022 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, | |
97288f2c | 1023 | fmode_t flags, void *holder) |
2b82032c YZ |
1024 | { |
1025 | int ret; | |
1026 | ||
590d1e29 DS |
1027 | lockdep_assert_held(&uuid_mutex); |
1028 | ||
2b82032c | 1029 | if (fs_devices->opened) { |
e4404d6e YZ |
1030 | fs_devices->opened++; |
1031 | ret = 0; | |
2b82032c | 1032 | } else { |
15916de8 | 1033 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
2b82032c | 1034 | } |
8a4b83cc CM |
1035 | return ret; |
1036 | } | |
1037 | ||
c9162bdf | 1038 | static void btrfs_release_disk_super(struct page *page) |
6cf86a00 AJ |
1039 | { |
1040 | kunmap(page); | |
1041 | put_page(page); | |
1042 | } | |
1043 | ||
c9162bdf OS |
1044 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
1045 | struct page **page, | |
1046 | struct btrfs_super_block **disk_super) | |
6cf86a00 AJ |
1047 | { |
1048 | void *p; | |
1049 | pgoff_t index; | |
1050 | ||
1051 | /* make sure our super fits in the device */ | |
1052 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) | |
1053 | return 1; | |
1054 | ||
1055 | /* make sure our super fits in the page */ | |
1056 | if (sizeof(**disk_super) > PAGE_SIZE) | |
1057 | return 1; | |
1058 | ||
1059 | /* make sure our super doesn't straddle pages on disk */ | |
1060 | index = bytenr >> PAGE_SHIFT; | |
1061 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) | |
1062 | return 1; | |
1063 | ||
1064 | /* pull in the page with our super */ | |
1065 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, | |
1066 | index, GFP_KERNEL); | |
1067 | ||
1068 | if (IS_ERR_OR_NULL(*page)) | |
1069 | return 1; | |
1070 | ||
1071 | p = kmap(*page); | |
1072 | ||
1073 | /* align our pointer to the offset of the super block */ | |
1074 | *disk_super = p + (bytenr & ~PAGE_MASK); | |
1075 | ||
1076 | if (btrfs_super_bytenr(*disk_super) != bytenr || | |
1077 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { | |
1078 | btrfs_release_disk_super(*page); | |
1079 | return 1; | |
1080 | } | |
1081 | ||
1082 | if ((*disk_super)->label[0] && | |
1083 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) | |
1084 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; | |
1085 | ||
1086 | return 0; | |
1087 | } | |
1088 | ||
6f60cbd3 DS |
1089 | /* |
1090 | * Look for a btrfs signature on a device. This may be called out of the mount path | |
1091 | * and we are not allowed to call set_blocksize during the scan. The superblock | |
1092 | * is read via pagecache | |
1093 | */ | |
97288f2c | 1094 | int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, |
8a4b83cc CM |
1095 | struct btrfs_fs_devices **fs_devices_ret) |
1096 | { | |
1097 | struct btrfs_super_block *disk_super; | |
1098 | struct block_device *bdev; | |
6f60cbd3 | 1099 | struct page *page; |
6f60cbd3 | 1100 | int ret = -EINVAL; |
8a4b83cc | 1101 | u64 devid; |
f2984462 | 1102 | u64 transid; |
02db0844 | 1103 | u64 total_devices; |
6f60cbd3 | 1104 | u64 bytenr; |
8a4b83cc | 1105 | |
6f60cbd3 DS |
1106 | /* |
1107 | * we would like to check all the supers, but that would make | |
1108 | * a btrfs mount succeed after a mkfs from a different FS. | |
1109 | * So, we need to add a special mount option to scan for | |
1110 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead | |
1111 | */ | |
1112 | bytenr = btrfs_sb_offset(0); | |
d4d77629 | 1113 | flags |= FMODE_EXCL; |
10f6327b | 1114 | mutex_lock(&uuid_mutex); |
6f60cbd3 DS |
1115 | |
1116 | bdev = blkdev_get_by_path(path, flags, holder); | |
6f60cbd3 DS |
1117 | if (IS_ERR(bdev)) { |
1118 | ret = PTR_ERR(bdev); | |
beaf8ab3 | 1119 | goto error; |
6f60cbd3 DS |
1120 | } |
1121 | ||
6cf86a00 | 1122 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) |
6f60cbd3 DS |
1123 | goto error_bdev_put; |
1124 | ||
a343832f | 1125 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
f2984462 | 1126 | transid = btrfs_super_generation(disk_super); |
02db0844 | 1127 | total_devices = btrfs_super_num_devices(disk_super); |
6f60cbd3 | 1128 | |
8a4b83cc | 1129 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
60999ca4 DS |
1130 | if (ret > 0) { |
1131 | if (disk_super->label[0]) { | |
62e85577 | 1132 | pr_info("BTRFS: device label %s ", disk_super->label); |
60999ca4 | 1133 | } else { |
62e85577 | 1134 | pr_info("BTRFS: device fsid %pU ", disk_super->fsid); |
60999ca4 DS |
1135 | } |
1136 | ||
62e85577 | 1137 | pr_cont("devid %llu transid %llu %s\n", devid, transid, path); |
60999ca4 DS |
1138 | ret = 0; |
1139 | } | |
02db0844 JB |
1140 | if (!ret && fs_devices_ret) |
1141 | (*fs_devices_ret)->total_devices = total_devices; | |
6f60cbd3 | 1142 | |
6cf86a00 | 1143 | btrfs_release_disk_super(page); |
6f60cbd3 DS |
1144 | |
1145 | error_bdev_put: | |
d4d77629 | 1146 | blkdev_put(bdev, flags); |
8a4b83cc | 1147 | error: |
beaf8ab3 | 1148 | mutex_unlock(&uuid_mutex); |
8a4b83cc CM |
1149 | return ret; |
1150 | } | |
0b86a832 | 1151 | |
6d07bcec MX |
1152 | /* helper to account the used device space in the range */ |
1153 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, | |
1154 | u64 end, u64 *length) | |
1155 | { | |
1156 | struct btrfs_key key; | |
fb456252 | 1157 | struct btrfs_root *root = device->fs_info->dev_root; |
6d07bcec MX |
1158 | struct btrfs_dev_extent *dev_extent; |
1159 | struct btrfs_path *path; | |
1160 | u64 extent_end; | |
1161 | int ret; | |
1162 | int slot; | |
1163 | struct extent_buffer *l; | |
1164 | ||
1165 | *length = 0; | |
1166 | ||
63a212ab | 1167 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
6d07bcec MX |
1168 | return 0; |
1169 | ||
1170 | path = btrfs_alloc_path(); | |
1171 | if (!path) | |
1172 | return -ENOMEM; | |
e4058b54 | 1173 | path->reada = READA_FORWARD; |
6d07bcec MX |
1174 | |
1175 | key.objectid = device->devid; | |
1176 | key.offset = start; | |
1177 | key.type = BTRFS_DEV_EXTENT_KEY; | |
1178 | ||
1179 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1180 | if (ret < 0) | |
1181 | goto out; | |
1182 | if (ret > 0) { | |
1183 | ret = btrfs_previous_item(root, path, key.objectid, key.type); | |
1184 | if (ret < 0) | |
1185 | goto out; | |
1186 | } | |
1187 | ||
1188 | while (1) { | |
1189 | l = path->nodes[0]; | |
1190 | slot = path->slots[0]; | |
1191 | if (slot >= btrfs_header_nritems(l)) { | |
1192 | ret = btrfs_next_leaf(root, path); | |
1193 | if (ret == 0) | |
1194 | continue; | |
1195 | if (ret < 0) | |
1196 | goto out; | |
1197 | ||
1198 | break; | |
1199 | } | |
1200 | btrfs_item_key_to_cpu(l, &key, slot); | |
1201 | ||
1202 | if (key.objectid < device->devid) | |
1203 | goto next; | |
1204 | ||
1205 | if (key.objectid > device->devid) | |
1206 | break; | |
1207 | ||
962a298f | 1208 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
6d07bcec MX |
1209 | goto next; |
1210 | ||
1211 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
1212 | extent_end = key.offset + btrfs_dev_extent_length(l, | |
1213 | dev_extent); | |
1214 | if (key.offset <= start && extent_end > end) { | |
1215 | *length = end - start + 1; | |
1216 | break; | |
1217 | } else if (key.offset <= start && extent_end > start) | |
1218 | *length += extent_end - start; | |
1219 | else if (key.offset > start && extent_end <= end) | |
1220 | *length += extent_end - key.offset; | |
1221 | else if (key.offset > start && key.offset <= end) { | |
1222 | *length += end - key.offset + 1; | |
1223 | break; | |
1224 | } else if (key.offset > end) | |
1225 | break; | |
1226 | ||
1227 | next: | |
1228 | path->slots[0]++; | |
1229 | } | |
1230 | ret = 0; | |
1231 | out: | |
1232 | btrfs_free_path(path); | |
1233 | return ret; | |
1234 | } | |
1235 | ||
499f377f | 1236 | static int contains_pending_extent(struct btrfs_transaction *transaction, |
6df9a95e JB |
1237 | struct btrfs_device *device, |
1238 | u64 *start, u64 len) | |
1239 | { | |
fb456252 | 1240 | struct btrfs_fs_info *fs_info = device->fs_info; |
6df9a95e | 1241 | struct extent_map *em; |
499f377f | 1242 | struct list_head *search_list = &fs_info->pinned_chunks; |
6df9a95e | 1243 | int ret = 0; |
1b984508 | 1244 | u64 physical_start = *start; |
6df9a95e | 1245 | |
499f377f JM |
1246 | if (transaction) |
1247 | search_list = &transaction->pending_chunks; | |
04216820 FM |
1248 | again: |
1249 | list_for_each_entry(em, search_list, list) { | |
6df9a95e JB |
1250 | struct map_lookup *map; |
1251 | int i; | |
1252 | ||
95617d69 | 1253 | map = em->map_lookup; |
6df9a95e | 1254 | for (i = 0; i < map->num_stripes; i++) { |
c152b63e FM |
1255 | u64 end; |
1256 | ||
6df9a95e JB |
1257 | if (map->stripes[i].dev != device) |
1258 | continue; | |
1b984508 | 1259 | if (map->stripes[i].physical >= physical_start + len || |
6df9a95e | 1260 | map->stripes[i].physical + em->orig_block_len <= |
1b984508 | 1261 | physical_start) |
6df9a95e | 1262 | continue; |
c152b63e FM |
1263 | /* |
1264 | * Make sure that while processing the pinned list we do | |
1265 | * not override our *start with a lower value, because | |
1266 | * we can have pinned chunks that fall within this | |
1267 | * device hole and that have lower physical addresses | |
1268 | * than the pending chunks we processed before. If we | |
1269 | * do not take this special care we can end up getting | |
1270 | * 2 pending chunks that start at the same physical | |
1271 | * device offsets because the end offset of a pinned | |
1272 | * chunk can be equal to the start offset of some | |
1273 | * pending chunk. | |
1274 | */ | |
1275 | end = map->stripes[i].physical + em->orig_block_len; | |
1276 | if (end > *start) { | |
1277 | *start = end; | |
1278 | ret = 1; | |
1279 | } | |
6df9a95e JB |
1280 | } |
1281 | } | |
499f377f JM |
1282 | if (search_list != &fs_info->pinned_chunks) { |
1283 | search_list = &fs_info->pinned_chunks; | |
04216820 FM |
1284 | goto again; |
1285 | } | |
6df9a95e JB |
1286 | |
1287 | return ret; | |
1288 | } | |
1289 | ||
1290 | ||
0b86a832 | 1291 | /* |
499f377f JM |
1292 | * find_free_dev_extent_start - find free space in the specified device |
1293 | * @device: the device which we search the free space in | |
1294 | * @num_bytes: the size of the free space that we need | |
1295 | * @search_start: the position from which to begin the search | |
1296 | * @start: store the start of the free space. | |
1297 | * @len: the size of the free space. that we find, or the size | |
1298 | * of the max free space if we don't find suitable free space | |
7bfc837d | 1299 | * |
0b86a832 CM |
1300 | * this uses a pretty simple search, the expectation is that it is |
1301 | * called very infrequently and that a given device has a small number | |
1302 | * of extents | |
7bfc837d MX |
1303 | * |
1304 | * @start is used to store the start of the free space if we find. But if we | |
1305 | * don't find suitable free space, it will be used to store the start position | |
1306 | * of the max free space. | |
1307 | * | |
1308 | * @len is used to store the size of the free space that we find. | |
1309 | * But if we don't find suitable free space, it is used to store the size of | |
1310 | * the max free space. | |
0b86a832 | 1311 | */ |
499f377f JM |
1312 | int find_free_dev_extent_start(struct btrfs_transaction *transaction, |
1313 | struct btrfs_device *device, u64 num_bytes, | |
1314 | u64 search_start, u64 *start, u64 *len) | |
0b86a832 | 1315 | { |
0b246afa JM |
1316 | struct btrfs_fs_info *fs_info = device->fs_info; |
1317 | struct btrfs_root *root = fs_info->dev_root; | |
0b86a832 | 1318 | struct btrfs_key key; |
7bfc837d | 1319 | struct btrfs_dev_extent *dev_extent; |
2b82032c | 1320 | struct btrfs_path *path; |
7bfc837d MX |
1321 | u64 hole_size; |
1322 | u64 max_hole_start; | |
1323 | u64 max_hole_size; | |
1324 | u64 extent_end; | |
0b86a832 CM |
1325 | u64 search_end = device->total_bytes; |
1326 | int ret; | |
7bfc837d | 1327 | int slot; |
0b86a832 | 1328 | struct extent_buffer *l; |
8cdc7c5b FM |
1329 | |
1330 | /* | |
1331 | * We don't want to overwrite the superblock on the drive nor any area | |
1332 | * used by the boot loader (grub for example), so we make sure to start | |
1333 | * at an offset of at least 1MB. | |
1334 | */ | |
0d0c71b3 | 1335 | search_start = max_t(u64, search_start, SZ_1M); |
0b86a832 | 1336 | |
6df9a95e JB |
1337 | path = btrfs_alloc_path(); |
1338 | if (!path) | |
1339 | return -ENOMEM; | |
f2ab7618 | 1340 | |
7bfc837d MX |
1341 | max_hole_start = search_start; |
1342 | max_hole_size = 0; | |
1343 | ||
f2ab7618 | 1344 | again: |
63a212ab | 1345 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
7bfc837d | 1346 | ret = -ENOSPC; |
6df9a95e | 1347 | goto out; |
7bfc837d MX |
1348 | } |
1349 | ||
e4058b54 | 1350 | path->reada = READA_FORWARD; |
6df9a95e JB |
1351 | path->search_commit_root = 1; |
1352 | path->skip_locking = 1; | |
7bfc837d | 1353 | |
0b86a832 CM |
1354 | key.objectid = device->devid; |
1355 | key.offset = search_start; | |
1356 | key.type = BTRFS_DEV_EXTENT_KEY; | |
7bfc837d | 1357 | |
125ccb0a | 1358 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
0b86a832 | 1359 | if (ret < 0) |
7bfc837d | 1360 | goto out; |
1fcbac58 YZ |
1361 | if (ret > 0) { |
1362 | ret = btrfs_previous_item(root, path, key.objectid, key.type); | |
1363 | if (ret < 0) | |
7bfc837d | 1364 | goto out; |
1fcbac58 | 1365 | } |
7bfc837d | 1366 | |
0b86a832 CM |
1367 | while (1) { |
1368 | l = path->nodes[0]; | |
1369 | slot = path->slots[0]; | |
1370 | if (slot >= btrfs_header_nritems(l)) { | |
1371 | ret = btrfs_next_leaf(root, path); | |
1372 | if (ret == 0) | |
1373 | continue; | |
1374 | if (ret < 0) | |
7bfc837d MX |
1375 | goto out; |
1376 | ||
1377 | break; | |
0b86a832 CM |
1378 | } |
1379 | btrfs_item_key_to_cpu(l, &key, slot); | |
1380 | ||
1381 | if (key.objectid < device->devid) | |
1382 | goto next; | |
1383 | ||
1384 | if (key.objectid > device->devid) | |
7bfc837d | 1385 | break; |
0b86a832 | 1386 | |
962a298f | 1387 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
7bfc837d | 1388 | goto next; |
9779b72f | 1389 | |
7bfc837d MX |
1390 | if (key.offset > search_start) { |
1391 | hole_size = key.offset - search_start; | |
9779b72f | 1392 | |
6df9a95e JB |
1393 | /* |
1394 | * Have to check before we set max_hole_start, otherwise | |
1395 | * we could end up sending back this offset anyway. | |
1396 | */ | |
499f377f | 1397 | if (contains_pending_extent(transaction, device, |
6df9a95e | 1398 | &search_start, |
1b984508 FL |
1399 | hole_size)) { |
1400 | if (key.offset >= search_start) { | |
1401 | hole_size = key.offset - search_start; | |
1402 | } else { | |
1403 | WARN_ON_ONCE(1); | |
1404 | hole_size = 0; | |
1405 | } | |
1406 | } | |
6df9a95e | 1407 | |
7bfc837d MX |
1408 | if (hole_size > max_hole_size) { |
1409 | max_hole_start = search_start; | |
1410 | max_hole_size = hole_size; | |
1411 | } | |
9779b72f | 1412 | |
7bfc837d MX |
1413 | /* |
1414 | * If this free space is greater than which we need, | |
1415 | * it must be the max free space that we have found | |
1416 | * until now, so max_hole_start must point to the start | |
1417 | * of this free space and the length of this free space | |
1418 | * is stored in max_hole_size. Thus, we return | |
1419 | * max_hole_start and max_hole_size and go back to the | |
1420 | * caller. | |
1421 | */ | |
1422 | if (hole_size >= num_bytes) { | |
1423 | ret = 0; | |
1424 | goto out; | |
0b86a832 CM |
1425 | } |
1426 | } | |
0b86a832 | 1427 | |
0b86a832 | 1428 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
7bfc837d MX |
1429 | extent_end = key.offset + btrfs_dev_extent_length(l, |
1430 | dev_extent); | |
1431 | if (extent_end > search_start) | |
1432 | search_start = extent_end; | |
0b86a832 CM |
1433 | next: |
1434 | path->slots[0]++; | |
1435 | cond_resched(); | |
1436 | } | |
0b86a832 | 1437 | |
38c01b96 | 1438 | /* |
1439 | * At this point, search_start should be the end of | |
1440 | * allocated dev extents, and when shrinking the device, | |
1441 | * search_end may be smaller than search_start. | |
1442 | */ | |
f2ab7618 | 1443 | if (search_end > search_start) { |
38c01b96 | 1444 | hole_size = search_end - search_start; |
1445 | ||
499f377f | 1446 | if (contains_pending_extent(transaction, device, &search_start, |
f2ab7618 ZL |
1447 | hole_size)) { |
1448 | btrfs_release_path(path); | |
1449 | goto again; | |
1450 | } | |
0b86a832 | 1451 | |
f2ab7618 ZL |
1452 | if (hole_size > max_hole_size) { |
1453 | max_hole_start = search_start; | |
1454 | max_hole_size = hole_size; | |
1455 | } | |
6df9a95e JB |
1456 | } |
1457 | ||
7bfc837d | 1458 | /* See above. */ |
f2ab7618 | 1459 | if (max_hole_size < num_bytes) |
7bfc837d MX |
1460 | ret = -ENOSPC; |
1461 | else | |
1462 | ret = 0; | |
1463 | ||
1464 | out: | |
2b82032c | 1465 | btrfs_free_path(path); |
7bfc837d | 1466 | *start = max_hole_start; |
b2117a39 | 1467 | if (len) |
7bfc837d | 1468 | *len = max_hole_size; |
0b86a832 CM |
1469 | return ret; |
1470 | } | |
1471 | ||
499f377f JM |
1472 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
1473 | struct btrfs_device *device, u64 num_bytes, | |
1474 | u64 *start, u64 *len) | |
1475 | { | |
499f377f | 1476 | /* FIXME use last free of some kind */ |
499f377f | 1477 | return find_free_dev_extent_start(trans->transaction, device, |
8cdc7c5b | 1478 | num_bytes, 0, start, len); |
499f377f JM |
1479 | } |
1480 | ||
b2950863 | 1481 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
8f18cf13 | 1482 | struct btrfs_device *device, |
2196d6e8 | 1483 | u64 start, u64 *dev_extent_len) |
8f18cf13 | 1484 | { |
0b246afa JM |
1485 | struct btrfs_fs_info *fs_info = device->fs_info; |
1486 | struct btrfs_root *root = fs_info->dev_root; | |
8f18cf13 CM |
1487 | int ret; |
1488 | struct btrfs_path *path; | |
8f18cf13 | 1489 | struct btrfs_key key; |
a061fc8d CM |
1490 | struct btrfs_key found_key; |
1491 | struct extent_buffer *leaf = NULL; | |
1492 | struct btrfs_dev_extent *extent = NULL; | |
8f18cf13 CM |
1493 | |
1494 | path = btrfs_alloc_path(); | |
1495 | if (!path) | |
1496 | return -ENOMEM; | |
1497 | ||
1498 | key.objectid = device->devid; | |
1499 | key.offset = start; | |
1500 | key.type = BTRFS_DEV_EXTENT_KEY; | |
924cd8fb | 1501 | again: |
8f18cf13 | 1502 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
a061fc8d CM |
1503 | if (ret > 0) { |
1504 | ret = btrfs_previous_item(root, path, key.objectid, | |
1505 | BTRFS_DEV_EXTENT_KEY); | |
b0b802d7 TI |
1506 | if (ret) |
1507 | goto out; | |
a061fc8d CM |
1508 | leaf = path->nodes[0]; |
1509 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1510 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1511 | struct btrfs_dev_extent); | |
1512 | BUG_ON(found_key.offset > start || found_key.offset + | |
1513 | btrfs_dev_extent_length(leaf, extent) < start); | |
924cd8fb MX |
1514 | key = found_key; |
1515 | btrfs_release_path(path); | |
1516 | goto again; | |
a061fc8d CM |
1517 | } else if (ret == 0) { |
1518 | leaf = path->nodes[0]; | |
1519 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1520 | struct btrfs_dev_extent); | |
79787eaa | 1521 | } else { |
0b246afa | 1522 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
79787eaa | 1523 | goto out; |
a061fc8d | 1524 | } |
8f18cf13 | 1525 | |
2196d6e8 MX |
1526 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
1527 | ||
8f18cf13 | 1528 | ret = btrfs_del_item(trans, root, path); |
79787eaa | 1529 | if (ret) { |
0b246afa JM |
1530 | btrfs_handle_fs_error(fs_info, ret, |
1531 | "Failed to remove dev extent item"); | |
13212b54 | 1532 | } else { |
3204d33c | 1533 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
79787eaa | 1534 | } |
b0b802d7 | 1535 | out: |
8f18cf13 CM |
1536 | btrfs_free_path(path); |
1537 | return ret; | |
1538 | } | |
1539 | ||
48a3b636 ES |
1540 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
1541 | struct btrfs_device *device, | |
48a3b636 | 1542 | u64 chunk_offset, u64 start, u64 num_bytes) |
0b86a832 CM |
1543 | { |
1544 | int ret; | |
1545 | struct btrfs_path *path; | |
0b246afa JM |
1546 | struct btrfs_fs_info *fs_info = device->fs_info; |
1547 | struct btrfs_root *root = fs_info->dev_root; | |
0b86a832 CM |
1548 | struct btrfs_dev_extent *extent; |
1549 | struct extent_buffer *leaf; | |
1550 | struct btrfs_key key; | |
1551 | ||
dfe25020 | 1552 | WARN_ON(!device->in_fs_metadata); |
63a212ab | 1553 | WARN_ON(device->is_tgtdev_for_dev_replace); |
0b86a832 CM |
1554 | path = btrfs_alloc_path(); |
1555 | if (!path) | |
1556 | return -ENOMEM; | |
1557 | ||
0b86a832 | 1558 | key.objectid = device->devid; |
2b82032c | 1559 | key.offset = start; |
0b86a832 CM |
1560 | key.type = BTRFS_DEV_EXTENT_KEY; |
1561 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
1562 | sizeof(*extent)); | |
2cdcecbc MF |
1563 | if (ret) |
1564 | goto out; | |
0b86a832 CM |
1565 | |
1566 | leaf = path->nodes[0]; | |
1567 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1568 | struct btrfs_dev_extent); | |
b5d9071c NB |
1569 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
1570 | BTRFS_CHUNK_TREE_OBJECTID); | |
0ca00afb NB |
1571 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
1572 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); | |
e17cade2 CM |
1573 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
1574 | ||
0b86a832 CM |
1575 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
1576 | btrfs_mark_buffer_dirty(leaf); | |
2cdcecbc | 1577 | out: |
0b86a832 CM |
1578 | btrfs_free_path(path); |
1579 | return ret; | |
1580 | } | |
1581 | ||
6df9a95e | 1582 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
0b86a832 | 1583 | { |
6df9a95e JB |
1584 | struct extent_map_tree *em_tree; |
1585 | struct extent_map *em; | |
1586 | struct rb_node *n; | |
1587 | u64 ret = 0; | |
0b86a832 | 1588 | |
6df9a95e JB |
1589 | em_tree = &fs_info->mapping_tree.map_tree; |
1590 | read_lock(&em_tree->lock); | |
1591 | n = rb_last(&em_tree->map); | |
1592 | if (n) { | |
1593 | em = rb_entry(n, struct extent_map, rb_node); | |
1594 | ret = em->start + em->len; | |
0b86a832 | 1595 | } |
6df9a95e JB |
1596 | read_unlock(&em_tree->lock); |
1597 | ||
0b86a832 CM |
1598 | return ret; |
1599 | } | |
1600 | ||
53f10659 ID |
1601 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
1602 | u64 *devid_ret) | |
0b86a832 CM |
1603 | { |
1604 | int ret; | |
1605 | struct btrfs_key key; | |
1606 | struct btrfs_key found_key; | |
2b82032c YZ |
1607 | struct btrfs_path *path; |
1608 | ||
2b82032c YZ |
1609 | path = btrfs_alloc_path(); |
1610 | if (!path) | |
1611 | return -ENOMEM; | |
0b86a832 CM |
1612 | |
1613 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1614 | key.type = BTRFS_DEV_ITEM_KEY; | |
1615 | key.offset = (u64)-1; | |
1616 | ||
53f10659 | 1617 | ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0); |
0b86a832 CM |
1618 | if (ret < 0) |
1619 | goto error; | |
1620 | ||
79787eaa | 1621 | BUG_ON(ret == 0); /* Corruption */ |
0b86a832 | 1622 | |
53f10659 ID |
1623 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
1624 | BTRFS_DEV_ITEMS_OBJECTID, | |
0b86a832 CM |
1625 | BTRFS_DEV_ITEM_KEY); |
1626 | if (ret) { | |
53f10659 | 1627 | *devid_ret = 1; |
0b86a832 CM |
1628 | } else { |
1629 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1630 | path->slots[0]); | |
53f10659 | 1631 | *devid_ret = found_key.offset + 1; |
0b86a832 CM |
1632 | } |
1633 | ret = 0; | |
1634 | error: | |
2b82032c | 1635 | btrfs_free_path(path); |
0b86a832 CM |
1636 | return ret; |
1637 | } | |
1638 | ||
1639 | /* | |
1640 | * the device information is stored in the chunk root | |
1641 | * the btrfs_device struct should be fully filled in | |
1642 | */ | |
48a3b636 | 1643 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
5b4aacef | 1644 | struct btrfs_fs_info *fs_info, |
48a3b636 | 1645 | struct btrfs_device *device) |
0b86a832 | 1646 | { |
5b4aacef | 1647 | struct btrfs_root *root = fs_info->chunk_root; |
0b86a832 CM |
1648 | int ret; |
1649 | struct btrfs_path *path; | |
1650 | struct btrfs_dev_item *dev_item; | |
1651 | struct extent_buffer *leaf; | |
1652 | struct btrfs_key key; | |
1653 | unsigned long ptr; | |
0b86a832 | 1654 | |
0b86a832 CM |
1655 | path = btrfs_alloc_path(); |
1656 | if (!path) | |
1657 | return -ENOMEM; | |
1658 | ||
0b86a832 CM |
1659 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1660 | key.type = BTRFS_DEV_ITEM_KEY; | |
2b82032c | 1661 | key.offset = device->devid; |
0b86a832 CM |
1662 | |
1663 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
0d81ba5d | 1664 | sizeof(*dev_item)); |
0b86a832 CM |
1665 | if (ret) |
1666 | goto out; | |
1667 | ||
1668 | leaf = path->nodes[0]; | |
1669 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
1670 | ||
1671 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
2b82032c | 1672 | btrfs_set_device_generation(leaf, dev_item, 0); |
0b86a832 CM |
1673 | btrfs_set_device_type(leaf, dev_item, device->type); |
1674 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
1675 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
1676 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
7cc8e58d MX |
1677 | btrfs_set_device_total_bytes(leaf, dev_item, |
1678 | btrfs_device_get_disk_total_bytes(device)); | |
1679 | btrfs_set_device_bytes_used(leaf, dev_item, | |
1680 | btrfs_device_get_bytes_used(device)); | |
e17cade2 CM |
1681 | btrfs_set_device_group(leaf, dev_item, 0); |
1682 | btrfs_set_device_seek_speed(leaf, dev_item, 0); | |
1683 | btrfs_set_device_bandwidth(leaf, dev_item, 0); | |
c3027eb5 | 1684 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
0b86a832 | 1685 | |
410ba3a2 | 1686 | ptr = btrfs_device_uuid(dev_item); |
e17cade2 | 1687 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
1473b24e | 1688 | ptr = btrfs_device_fsid(dev_item); |
44880fdc | 1689 | write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE); |
0b86a832 | 1690 | btrfs_mark_buffer_dirty(leaf); |
0b86a832 | 1691 | |
2b82032c | 1692 | ret = 0; |
0b86a832 CM |
1693 | out: |
1694 | btrfs_free_path(path); | |
1695 | return ret; | |
1696 | } | |
8f18cf13 | 1697 | |
5a1972bd QW |
1698 | /* |
1699 | * Function to update ctime/mtime for a given device path. | |
1700 | * Mainly used for ctime/mtime based probe like libblkid. | |
1701 | */ | |
da353f6b | 1702 | static void update_dev_time(const char *path_name) |
5a1972bd QW |
1703 | { |
1704 | struct file *filp; | |
1705 | ||
1706 | filp = filp_open(path_name, O_RDWR, 0); | |
98af592f | 1707 | if (IS_ERR(filp)) |
5a1972bd QW |
1708 | return; |
1709 | file_update_time(filp); | |
1710 | filp_close(filp, NULL); | |
5a1972bd QW |
1711 | } |
1712 | ||
5b4aacef | 1713 | static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info, |
a061fc8d CM |
1714 | struct btrfs_device *device) |
1715 | { | |
5b4aacef | 1716 | struct btrfs_root *root = fs_info->chunk_root; |
a061fc8d CM |
1717 | int ret; |
1718 | struct btrfs_path *path; | |
a061fc8d | 1719 | struct btrfs_key key; |
a061fc8d CM |
1720 | struct btrfs_trans_handle *trans; |
1721 | ||
a061fc8d CM |
1722 | path = btrfs_alloc_path(); |
1723 | if (!path) | |
1724 | return -ENOMEM; | |
1725 | ||
a22285a6 | 1726 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
1727 | if (IS_ERR(trans)) { |
1728 | btrfs_free_path(path); | |
1729 | return PTR_ERR(trans); | |
1730 | } | |
a061fc8d CM |
1731 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1732 | key.type = BTRFS_DEV_ITEM_KEY; | |
1733 | key.offset = device->devid; | |
1734 | ||
1735 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
5e9f2ad5 NB |
1736 | if (ret) { |
1737 | if (ret > 0) | |
1738 | ret = -ENOENT; | |
1739 | btrfs_abort_transaction(trans, ret); | |
1740 | btrfs_end_transaction(trans); | |
a061fc8d CM |
1741 | goto out; |
1742 | } | |
1743 | ||
1744 | ret = btrfs_del_item(trans, root, path); | |
5e9f2ad5 NB |
1745 | if (ret) { |
1746 | btrfs_abort_transaction(trans, ret); | |
1747 | btrfs_end_transaction(trans); | |
1748 | } | |
1749 | ||
a061fc8d CM |
1750 | out: |
1751 | btrfs_free_path(path); | |
5e9f2ad5 NB |
1752 | if (!ret) |
1753 | ret = btrfs_commit_transaction(trans); | |
a061fc8d CM |
1754 | return ret; |
1755 | } | |
1756 | ||
3cc31a0d DS |
1757 | /* |
1758 | * Verify that @num_devices satisfies the RAID profile constraints in the whole | |
1759 | * filesystem. It's up to the caller to adjust that number regarding eg. device | |
1760 | * replace. | |
1761 | */ | |
1762 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, | |
1763 | u64 num_devices) | |
a061fc8d | 1764 | { |
a061fc8d | 1765 | u64 all_avail; |
de98ced9 | 1766 | unsigned seq; |
418775a2 | 1767 | int i; |
a061fc8d | 1768 | |
de98ced9 | 1769 | do { |
bd45ffbc | 1770 | seq = read_seqbegin(&fs_info->profiles_lock); |
de98ced9 | 1771 | |
bd45ffbc AJ |
1772 | all_avail = fs_info->avail_data_alloc_bits | |
1773 | fs_info->avail_system_alloc_bits | | |
1774 | fs_info->avail_metadata_alloc_bits; | |
1775 | } while (read_seqretry(&fs_info->profiles_lock, seq)); | |
a061fc8d | 1776 | |
418775a2 DS |
1777 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
1778 | if (!(all_avail & btrfs_raid_group[i])) | |
1779 | continue; | |
a061fc8d | 1780 | |
418775a2 DS |
1781 | if (num_devices < btrfs_raid_array[i].devs_min) { |
1782 | int ret = btrfs_raid_mindev_error[i]; | |
bd45ffbc | 1783 | |
418775a2 DS |
1784 | if (ret) |
1785 | return ret; | |
1786 | } | |
53b381b3 DW |
1787 | } |
1788 | ||
bd45ffbc | 1789 | return 0; |
f1fa7f26 AJ |
1790 | } |
1791 | ||
c9162bdf OS |
1792 | static struct btrfs_device * btrfs_find_next_active_device( |
1793 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) | |
a061fc8d | 1794 | { |
2b82032c | 1795 | struct btrfs_device *next_device; |
88acff64 AJ |
1796 | |
1797 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { | |
1798 | if (next_device != device && | |
1799 | !next_device->missing && next_device->bdev) | |
1800 | return next_device; | |
1801 | } | |
1802 | ||
1803 | return NULL; | |
1804 | } | |
1805 | ||
1806 | /* | |
1807 | * Helper function to check if the given device is part of s_bdev / latest_bdev | |
1808 | * and replace it with the provided or the next active device, in the context | |
1809 | * where this function called, there should be always be another device (or | |
1810 | * this_dev) which is active. | |
1811 | */ | |
1812 | void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info, | |
1813 | struct btrfs_device *device, struct btrfs_device *this_dev) | |
1814 | { | |
1815 | struct btrfs_device *next_device; | |
1816 | ||
1817 | if (this_dev) | |
1818 | next_device = this_dev; | |
1819 | else | |
1820 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, | |
1821 | device); | |
1822 | ASSERT(next_device); | |
1823 | ||
1824 | if (fs_info->sb->s_bdev && | |
1825 | (fs_info->sb->s_bdev == device->bdev)) | |
1826 | fs_info->sb->s_bdev = next_device->bdev; | |
1827 | ||
1828 | if (fs_info->fs_devices->latest_bdev == device->bdev) | |
1829 | fs_info->fs_devices->latest_bdev = next_device->bdev; | |
1830 | } | |
1831 | ||
da353f6b DS |
1832 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
1833 | u64 devid) | |
f1fa7f26 AJ |
1834 | { |
1835 | struct btrfs_device *device; | |
1f78160c | 1836 | struct btrfs_fs_devices *cur_devices; |
2b82032c | 1837 | u64 num_devices; |
a061fc8d CM |
1838 | int ret = 0; |
1839 | ||
a061fc8d CM |
1840 | mutex_lock(&uuid_mutex); |
1841 | ||
0b246afa JM |
1842 | num_devices = fs_info->fs_devices->num_devices; |
1843 | btrfs_dev_replace_lock(&fs_info->dev_replace, 0); | |
1844 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { | |
8dabb742 SB |
1845 | WARN_ON(num_devices < 1); |
1846 | num_devices--; | |
1847 | } | |
0b246afa | 1848 | btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); |
8dabb742 | 1849 | |
0b246afa | 1850 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
f1fa7f26 | 1851 | if (ret) |
a061fc8d | 1852 | goto out; |
a061fc8d | 1853 | |
2ff7e61e JM |
1854 | ret = btrfs_find_device_by_devspec(fs_info, devid, device_path, |
1855 | &device); | |
24fc572f | 1856 | if (ret) |
53b381b3 | 1857 | goto out; |
dfe25020 | 1858 | |
63a212ab | 1859 | if (device->is_tgtdev_for_dev_replace) { |
183860f6 | 1860 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
24fc572f | 1861 | goto out; |
63a212ab SB |
1862 | } |
1863 | ||
0b246afa | 1864 | if (device->writeable && fs_info->fs_devices->rw_devices == 1) { |
183860f6 | 1865 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
24fc572f | 1866 | goto out; |
2b82032c YZ |
1867 | } |
1868 | ||
1869 | if (device->writeable) { | |
34441361 | 1870 | mutex_lock(&fs_info->chunk_mutex); |
2b82032c | 1871 | list_del_init(&device->dev_alloc_list); |
c3929c36 | 1872 | device->fs_devices->rw_devices--; |
34441361 | 1873 | mutex_unlock(&fs_info->chunk_mutex); |
dfe25020 | 1874 | } |
a061fc8d | 1875 | |
d7901554 | 1876 | mutex_unlock(&uuid_mutex); |
a061fc8d | 1877 | ret = btrfs_shrink_device(device, 0); |
d7901554 | 1878 | mutex_lock(&uuid_mutex); |
a061fc8d | 1879 | if (ret) |
9b3517e9 | 1880 | goto error_undo; |
a061fc8d | 1881 | |
63a212ab SB |
1882 | /* |
1883 | * TODO: the superblock still includes this device in its num_devices | |
1884 | * counter although write_all_supers() is not locked out. This | |
1885 | * could give a filesystem state which requires a degraded mount. | |
1886 | */ | |
0b246afa | 1887 | ret = btrfs_rm_dev_item(fs_info, device); |
a061fc8d | 1888 | if (ret) |
9b3517e9 | 1889 | goto error_undo; |
a061fc8d | 1890 | |
2b82032c | 1891 | device->in_fs_metadata = 0; |
0b246afa | 1892 | btrfs_scrub_cancel_dev(fs_info, device); |
e5e9a520 CM |
1893 | |
1894 | /* | |
1895 | * the device list mutex makes sure that we don't change | |
1896 | * the device list while someone else is writing out all | |
d7306801 FDBM |
1897 | * the device supers. Whoever is writing all supers, should |
1898 | * lock the device list mutex before getting the number of | |
1899 | * devices in the super block (super_copy). Conversely, | |
1900 | * whoever updates the number of devices in the super block | |
1901 | * (super_copy) should hold the device list mutex. | |
e5e9a520 | 1902 | */ |
1f78160c XG |
1903 | |
1904 | cur_devices = device->fs_devices; | |
0b246afa | 1905 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
1f78160c | 1906 | list_del_rcu(&device->dev_list); |
e5e9a520 | 1907 | |
e4404d6e | 1908 | device->fs_devices->num_devices--; |
02db0844 | 1909 | device->fs_devices->total_devices--; |
2b82032c | 1910 | |
cd02dca5 | 1911 | if (device->missing) |
3a7d55c8 | 1912 | device->fs_devices->missing_devices--; |
cd02dca5 | 1913 | |
0b246afa | 1914 | btrfs_assign_next_active_device(fs_info, device, NULL); |
2b82032c | 1915 | |
0bfaa9c5 | 1916 | if (device->bdev) { |
e4404d6e | 1917 | device->fs_devices->open_devices--; |
0bfaa9c5 | 1918 | /* remove sysfs entry */ |
0b246afa | 1919 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, device); |
0bfaa9c5 | 1920 | } |
99994cde | 1921 | |
0b246afa JM |
1922 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
1923 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); | |
1924 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | |
2b82032c | 1925 | |
cea67ab9 JM |
1926 | /* |
1927 | * at this point, the device is zero sized and detached from | |
1928 | * the devices list. All that's left is to zero out the old | |
1929 | * supers and free the device. | |
1930 | */ | |
1931 | if (device->writeable) | |
1932 | btrfs_scratch_superblocks(device->bdev, device->name->str); | |
1933 | ||
1934 | btrfs_close_bdev(device); | |
1935 | call_rcu(&device->rcu, free_device); | |
1936 | ||
1f78160c | 1937 | if (cur_devices->open_devices == 0) { |
e4404d6e | 1938 | struct btrfs_fs_devices *fs_devices; |
0b246afa | 1939 | fs_devices = fs_info->fs_devices; |
e4404d6e | 1940 | while (fs_devices) { |
8321cf25 RS |
1941 | if (fs_devices->seed == cur_devices) { |
1942 | fs_devices->seed = cur_devices->seed; | |
e4404d6e | 1943 | break; |
8321cf25 | 1944 | } |
e4404d6e | 1945 | fs_devices = fs_devices->seed; |
2b82032c | 1946 | } |
1f78160c | 1947 | cur_devices->seed = NULL; |
1f78160c | 1948 | __btrfs_close_devices(cur_devices); |
1f78160c | 1949 | free_fs_devices(cur_devices); |
2b82032c YZ |
1950 | } |
1951 | ||
a061fc8d CM |
1952 | out: |
1953 | mutex_unlock(&uuid_mutex); | |
a061fc8d | 1954 | return ret; |
24fc572f | 1955 | |
9b3517e9 ID |
1956 | error_undo: |
1957 | if (device->writeable) { | |
34441361 | 1958 | mutex_lock(&fs_info->chunk_mutex); |
9b3517e9 | 1959 | list_add(&device->dev_alloc_list, |
0b246afa | 1960 | &fs_info->fs_devices->alloc_list); |
c3929c36 | 1961 | device->fs_devices->rw_devices++; |
34441361 | 1962 | mutex_unlock(&fs_info->chunk_mutex); |
9b3517e9 | 1963 | } |
24fc572f | 1964 | goto out; |
a061fc8d CM |
1965 | } |
1966 | ||
084b6e7c QW |
1967 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
1968 | struct btrfs_device *srcdev) | |
e93c89c1 | 1969 | { |
d51908ce AJ |
1970 | struct btrfs_fs_devices *fs_devices; |
1971 | ||
e93c89c1 | 1972 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
1357272f | 1973 | |
25e8e911 AJ |
1974 | /* |
1975 | * in case of fs with no seed, srcdev->fs_devices will point | |
1976 | * to fs_devices of fs_info. However when the dev being replaced is | |
1977 | * a seed dev it will point to the seed's local fs_devices. In short | |
1978 | * srcdev will have its correct fs_devices in both the cases. | |
1979 | */ | |
1980 | fs_devices = srcdev->fs_devices; | |
d51908ce | 1981 | |
e93c89c1 | 1982 | list_del_rcu(&srcdev->dev_list); |
619c47f3 | 1983 | list_del(&srcdev->dev_alloc_list); |
d51908ce | 1984 | fs_devices->num_devices--; |
82372bc8 | 1985 | if (srcdev->missing) |
d51908ce | 1986 | fs_devices->missing_devices--; |
e93c89c1 | 1987 | |
48b3b9d4 | 1988 | if (srcdev->writeable) |
82372bc8 | 1989 | fs_devices->rw_devices--; |
1357272f | 1990 | |
82372bc8 | 1991 | if (srcdev->bdev) |
d51908ce | 1992 | fs_devices->open_devices--; |
084b6e7c QW |
1993 | } |
1994 | ||
1995 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, | |
1996 | struct btrfs_device *srcdev) | |
1997 | { | |
1998 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; | |
e93c89c1 | 1999 | |
48b3b9d4 AJ |
2000 | if (srcdev->writeable) { |
2001 | /* zero out the old super if it is writable */ | |
2002 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); | |
2003 | } | |
14238819 AJ |
2004 | |
2005 | btrfs_close_bdev(srcdev); | |
e93c89c1 | 2006 | call_rcu(&srcdev->rcu, free_device); |
94d5f0c2 | 2007 | |
94d5f0c2 AJ |
2008 | /* if this is no devs we rather delete the fs_devices */ |
2009 | if (!fs_devices->num_devices) { | |
2010 | struct btrfs_fs_devices *tmp_fs_devices; | |
2011 | ||
6dd38f81 AJ |
2012 | /* |
2013 | * On a mounted FS, num_devices can't be zero unless it's a | |
2014 | * seed. In case of a seed device being replaced, the replace | |
2015 | * target added to the sprout FS, so there will be no more | |
2016 | * device left under the seed FS. | |
2017 | */ | |
2018 | ASSERT(fs_devices->seeding); | |
2019 | ||
94d5f0c2 AJ |
2020 | tmp_fs_devices = fs_info->fs_devices; |
2021 | while (tmp_fs_devices) { | |
2022 | if (tmp_fs_devices->seed == fs_devices) { | |
2023 | tmp_fs_devices->seed = fs_devices->seed; | |
2024 | break; | |
2025 | } | |
2026 | tmp_fs_devices = tmp_fs_devices->seed; | |
2027 | } | |
2028 | fs_devices->seed = NULL; | |
8bef8401 AJ |
2029 | __btrfs_close_devices(fs_devices); |
2030 | free_fs_devices(fs_devices); | |
94d5f0c2 | 2031 | } |
e93c89c1 SB |
2032 | } |
2033 | ||
2034 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, | |
2035 | struct btrfs_device *tgtdev) | |
2036 | { | |
67a2c45e | 2037 | mutex_lock(&uuid_mutex); |
e93c89c1 SB |
2038 | WARN_ON(!tgtdev); |
2039 | mutex_lock(&fs_info->fs_devices->device_list_mutex); | |
d2ff1b20 | 2040 | |
32576040 | 2041 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev); |
d2ff1b20 | 2042 | |
779bf3fe | 2043 | if (tgtdev->bdev) |
e93c89c1 | 2044 | fs_info->fs_devices->open_devices--; |
779bf3fe | 2045 | |
e93c89c1 | 2046 | fs_info->fs_devices->num_devices--; |
e93c89c1 | 2047 | |
88acff64 | 2048 | btrfs_assign_next_active_device(fs_info, tgtdev, NULL); |
e93c89c1 | 2049 | |
e93c89c1 | 2050 | list_del_rcu(&tgtdev->dev_list); |
e93c89c1 SB |
2051 | |
2052 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); | |
67a2c45e | 2053 | mutex_unlock(&uuid_mutex); |
779bf3fe AJ |
2054 | |
2055 | /* | |
2056 | * The update_dev_time() with in btrfs_scratch_superblocks() | |
2057 | * may lead to a call to btrfs_show_devname() which will try | |
2058 | * to hold device_list_mutex. And here this device | |
2059 | * is already out of device list, so we don't have to hold | |
2060 | * the device_list_mutex lock. | |
2061 | */ | |
2062 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); | |
14238819 AJ |
2063 | |
2064 | btrfs_close_bdev(tgtdev); | |
779bf3fe | 2065 | call_rcu(&tgtdev->rcu, free_device); |
e93c89c1 SB |
2066 | } |
2067 | ||
2ff7e61e | 2068 | static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info, |
da353f6b | 2069 | const char *device_path, |
48a3b636 | 2070 | struct btrfs_device **device) |
7ba15b7d SB |
2071 | { |
2072 | int ret = 0; | |
2073 | struct btrfs_super_block *disk_super; | |
2074 | u64 devid; | |
2075 | u8 *dev_uuid; | |
2076 | struct block_device *bdev; | |
2077 | struct buffer_head *bh; | |
2078 | ||
2079 | *device = NULL; | |
2080 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, | |
0b246afa | 2081 | fs_info->bdev_holder, 0, &bdev, &bh); |
7ba15b7d SB |
2082 | if (ret) |
2083 | return ret; | |
2084 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
2085 | devid = btrfs_stack_device_id(&disk_super->dev_item); | |
2086 | dev_uuid = disk_super->dev_item.uuid; | |
2f05c56f | 2087 | *device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
34857e15 | 2088 | disk_super->fsid, true); |
7ba15b7d SB |
2089 | brelse(bh); |
2090 | if (!*device) | |
2091 | ret = -ENOENT; | |
2092 | blkdev_put(bdev, FMODE_READ); | |
2093 | return ret; | |
2094 | } | |
2095 | ||
2ff7e61e | 2096 | int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info, |
da353f6b | 2097 | const char *device_path, |
7ba15b7d SB |
2098 | struct btrfs_device **device) |
2099 | { | |
2100 | *device = NULL; | |
2101 | if (strcmp(device_path, "missing") == 0) { | |
2102 | struct list_head *devices; | |
2103 | struct btrfs_device *tmp; | |
2104 | ||
0b246afa | 2105 | devices = &fs_info->fs_devices->devices; |
7ba15b7d SB |
2106 | /* |
2107 | * It is safe to read the devices since the volume_mutex | |
2108 | * is held by the caller. | |
2109 | */ | |
2110 | list_for_each_entry(tmp, devices, dev_list) { | |
2111 | if (tmp->in_fs_metadata && !tmp->bdev) { | |
2112 | *device = tmp; | |
2113 | break; | |
2114 | } | |
2115 | } | |
2116 | ||
d74a6259 AJ |
2117 | if (!*device) |
2118 | return BTRFS_ERROR_DEV_MISSING_NOT_FOUND; | |
7ba15b7d SB |
2119 | |
2120 | return 0; | |
2121 | } else { | |
2ff7e61e | 2122 | return btrfs_find_device_by_path(fs_info, device_path, device); |
7ba15b7d SB |
2123 | } |
2124 | } | |
2125 | ||
5c5c0df0 DS |
2126 | /* |
2127 | * Lookup a device given by device id, or the path if the id is 0. | |
2128 | */ | |
2ff7e61e | 2129 | int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid, |
da353f6b DS |
2130 | const char *devpath, |
2131 | struct btrfs_device **device) | |
24e0474b AJ |
2132 | { |
2133 | int ret; | |
2134 | ||
5c5c0df0 | 2135 | if (devid) { |
24e0474b | 2136 | ret = 0; |
2f05c56f | 2137 | *device = btrfs_find_device(fs_info->fs_devices, devid, NULL, |
34857e15 | 2138 | NULL, true); |
24e0474b AJ |
2139 | if (!*device) |
2140 | ret = -ENOENT; | |
2141 | } else { | |
5c5c0df0 | 2142 | if (!devpath || !devpath[0]) |
b3d1b153 AJ |
2143 | return -EINVAL; |
2144 | ||
2ff7e61e | 2145 | ret = btrfs_find_device_missing_or_by_path(fs_info, devpath, |
24e0474b AJ |
2146 | device); |
2147 | } | |
2148 | return ret; | |
2149 | } | |
2150 | ||
2b82032c YZ |
2151 | /* |
2152 | * does all the dirty work required for changing file system's UUID. | |
2153 | */ | |
2ff7e61e | 2154 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
2b82032c | 2155 | { |
0b246afa | 2156 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
2b82032c | 2157 | struct btrfs_fs_devices *old_devices; |
e4404d6e | 2158 | struct btrfs_fs_devices *seed_devices; |
0b246afa | 2159 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
2b82032c YZ |
2160 | struct btrfs_device *device; |
2161 | u64 super_flags; | |
2162 | ||
2163 | BUG_ON(!mutex_is_locked(&uuid_mutex)); | |
e4404d6e | 2164 | if (!fs_devices->seeding) |
2b82032c YZ |
2165 | return -EINVAL; |
2166 | ||
2dfeca9b | 2167 | seed_devices = alloc_fs_devices(NULL); |
2208a378 ID |
2168 | if (IS_ERR(seed_devices)) |
2169 | return PTR_ERR(seed_devices); | |
2b82032c | 2170 | |
e4404d6e YZ |
2171 | old_devices = clone_fs_devices(fs_devices); |
2172 | if (IS_ERR(old_devices)) { | |
2173 | kfree(seed_devices); | |
2174 | return PTR_ERR(old_devices); | |
2b82032c | 2175 | } |
e4404d6e | 2176 | |
2b82032c YZ |
2177 | list_add(&old_devices->list, &fs_uuids); |
2178 | ||
e4404d6e YZ |
2179 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
2180 | seed_devices->opened = 1; | |
2181 | INIT_LIST_HEAD(&seed_devices->devices); | |
2182 | INIT_LIST_HEAD(&seed_devices->alloc_list); | |
e5e9a520 | 2183 | mutex_init(&seed_devices->device_list_mutex); |
c9513edb | 2184 | |
0b246afa | 2185 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
1f78160c XG |
2186 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
2187 | synchronize_rcu); | |
2196d6e8 MX |
2188 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
2189 | device->fs_devices = seed_devices; | |
c9513edb | 2190 | |
34441361 | 2191 | mutex_lock(&fs_info->chunk_mutex); |
e4404d6e | 2192 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
34441361 | 2193 | mutex_unlock(&fs_info->chunk_mutex); |
e4404d6e | 2194 | |
2b82032c YZ |
2195 | fs_devices->seeding = 0; |
2196 | fs_devices->num_devices = 0; | |
2197 | fs_devices->open_devices = 0; | |
69611ac8 | 2198 | fs_devices->missing_devices = 0; |
69611ac8 | 2199 | fs_devices->rotating = 0; |
e4404d6e | 2200 | fs_devices->seed = seed_devices; |
2b82032c YZ |
2201 | |
2202 | generate_random_uuid(fs_devices->fsid); | |
0b246afa | 2203 | memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
2b82032c | 2204 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
0b246afa | 2205 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
f7171750 | 2206 | |
2b82032c YZ |
2207 | super_flags = btrfs_super_flags(disk_super) & |
2208 | ~BTRFS_SUPER_FLAG_SEEDING; | |
2209 | btrfs_set_super_flags(disk_super, super_flags); | |
2210 | ||
2211 | return 0; | |
2212 | } | |
2213 | ||
2214 | /* | |
01327610 | 2215 | * Store the expected generation for seed devices in device items. |
2b82032c YZ |
2216 | */ |
2217 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, | |
5b4aacef | 2218 | struct btrfs_fs_info *fs_info) |
2b82032c | 2219 | { |
5b4aacef | 2220 | struct btrfs_root *root = fs_info->chunk_root; |
2b82032c YZ |
2221 | struct btrfs_path *path; |
2222 | struct extent_buffer *leaf; | |
2223 | struct btrfs_dev_item *dev_item; | |
2224 | struct btrfs_device *device; | |
2225 | struct btrfs_key key; | |
44880fdc | 2226 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
2b82032c YZ |
2227 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
2228 | u64 devid; | |
2229 | int ret; | |
2230 | ||
2231 | path = btrfs_alloc_path(); | |
2232 | if (!path) | |
2233 | return -ENOMEM; | |
2234 | ||
2b82032c YZ |
2235 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
2236 | key.offset = 0; | |
2237 | key.type = BTRFS_DEV_ITEM_KEY; | |
2238 | ||
2239 | while (1) { | |
2240 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
2241 | if (ret < 0) | |
2242 | goto error; | |
2243 | ||
2244 | leaf = path->nodes[0]; | |
2245 | next_slot: | |
2246 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
2247 | ret = btrfs_next_leaf(root, path); | |
2248 | if (ret > 0) | |
2249 | break; | |
2250 | if (ret < 0) | |
2251 | goto error; | |
2252 | leaf = path->nodes[0]; | |
2253 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 2254 | btrfs_release_path(path); |
2b82032c YZ |
2255 | continue; |
2256 | } | |
2257 | ||
2258 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
2259 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || | |
2260 | key.type != BTRFS_DEV_ITEM_KEY) | |
2261 | break; | |
2262 | ||
2263 | dev_item = btrfs_item_ptr(leaf, path->slots[0], | |
2264 | struct btrfs_dev_item); | |
2265 | devid = btrfs_device_id(leaf, dev_item); | |
410ba3a2 | 2266 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
2b82032c | 2267 | BTRFS_UUID_SIZE); |
1473b24e | 2268 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
44880fdc | 2269 | BTRFS_FSID_SIZE); |
2f05c56f | 2270 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
34857e15 | 2271 | fs_uuid, true); |
79787eaa | 2272 | BUG_ON(!device); /* Logic error */ |
2b82032c YZ |
2273 | |
2274 | if (device->fs_devices->seeding) { | |
2275 | btrfs_set_device_generation(leaf, dev_item, | |
2276 | device->generation); | |
2277 | btrfs_mark_buffer_dirty(leaf); | |
2278 | } | |
2279 | ||
2280 | path->slots[0]++; | |
2281 | goto next_slot; | |
2282 | } | |
2283 | ret = 0; | |
2284 | error: | |
2285 | btrfs_free_path(path); | |
2286 | return ret; | |
2287 | } | |
2288 | ||
da353f6b | 2289 | int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path) |
788f20eb | 2290 | { |
5112febb | 2291 | struct btrfs_root *root = fs_info->dev_root; |
d5e2003c | 2292 | struct request_queue *q; |
788f20eb CM |
2293 | struct btrfs_trans_handle *trans; |
2294 | struct btrfs_device *device; | |
2295 | struct block_device *bdev; | |
788f20eb | 2296 | struct list_head *devices; |
0b246afa | 2297 | struct super_block *sb = fs_info->sb; |
606686ee | 2298 | struct rcu_string *name; |
3c1dbdf5 | 2299 | u64 tmp; |
2b82032c | 2300 | int seeding_dev = 0; |
788f20eb | 2301 | int ret = 0; |
7132a262 | 2302 | bool unlocked = false; |
788f20eb | 2303 | |
bc98a42c | 2304 | if (sb_rdonly(sb) && !fs_info->fs_devices->seeding) |
f8c5d0b4 | 2305 | return -EROFS; |
788f20eb | 2306 | |
a5d16333 | 2307 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
0b246afa | 2308 | fs_info->bdev_holder); |
7f59203a JB |
2309 | if (IS_ERR(bdev)) |
2310 | return PTR_ERR(bdev); | |
a2135011 | 2311 | |
0b246afa | 2312 | if (fs_info->fs_devices->seeding) { |
2b82032c YZ |
2313 | seeding_dev = 1; |
2314 | down_write(&sb->s_umount); | |
2315 | mutex_lock(&uuid_mutex); | |
2316 | } | |
2317 | ||
8c8bee1d | 2318 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
a2135011 | 2319 | |
0b246afa | 2320 | devices = &fs_info->fs_devices->devices; |
d25628bd | 2321 | |
0b246afa | 2322 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
c6e30871 | 2323 | list_for_each_entry(device, devices, dev_list) { |
788f20eb CM |
2324 | if (device->bdev == bdev) { |
2325 | ret = -EEXIST; | |
d25628bd | 2326 | mutex_unlock( |
0b246afa | 2327 | &fs_info->fs_devices->device_list_mutex); |
2b82032c | 2328 | goto error; |
788f20eb CM |
2329 | } |
2330 | } | |
0b246afa | 2331 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
788f20eb | 2332 | |
0b246afa | 2333 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
12bd2fc0 | 2334 | if (IS_ERR(device)) { |
788f20eb | 2335 | /* we can safely leave the fs_devices entry around */ |
12bd2fc0 | 2336 | ret = PTR_ERR(device); |
2b82032c | 2337 | goto error; |
788f20eb CM |
2338 | } |
2339 | ||
78f2c9e6 | 2340 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
606686ee | 2341 | if (!name) { |
3065ae5b | 2342 | bio_put(device->flush_bio); |
788f20eb | 2343 | kfree(device); |
2b82032c YZ |
2344 | ret = -ENOMEM; |
2345 | goto error; | |
788f20eb | 2346 | } |
606686ee | 2347 | rcu_assign_pointer(device->name, name); |
2b82032c | 2348 | |
a22285a6 | 2349 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 | 2350 | if (IS_ERR(trans)) { |
606686ee | 2351 | rcu_string_free(device->name); |
3065ae5b | 2352 | bio_put(device->flush_bio); |
98d5dc13 TI |
2353 | kfree(device); |
2354 | ret = PTR_ERR(trans); | |
2355 | goto error; | |
2356 | } | |
2357 | ||
d5e2003c JB |
2358 | q = bdev_get_queue(bdev); |
2359 | if (blk_queue_discard(q)) | |
2360 | device->can_discard = 1; | |
2b82032c | 2361 | device->writeable = 1; |
2b82032c | 2362 | device->generation = trans->transid; |
0b246afa JM |
2363 | device->io_width = fs_info->sectorsize; |
2364 | device->io_align = fs_info->sectorsize; | |
2365 | device->sector_size = fs_info->sectorsize; | |
7dfb8be1 NB |
2366 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
2367 | fs_info->sectorsize); | |
2cc3c559 | 2368 | device->disk_total_bytes = device->total_bytes; |
935e5cc9 | 2369 | device->commit_total_bytes = device->total_bytes; |
fb456252 | 2370 | device->fs_info = fs_info; |
788f20eb | 2371 | device->bdev = bdev; |
dfe25020 | 2372 | device->in_fs_metadata = 1; |
63a212ab | 2373 | device->is_tgtdev_for_dev_replace = 0; |
fb01aa85 | 2374 | device->mode = FMODE_EXCL; |
27087f37 | 2375 | device->dev_stats_valid = 1; |
9f6d2510 | 2376 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
788f20eb | 2377 | |
2b82032c | 2378 | if (seeding_dev) { |
1751e8a6 | 2379 | sb->s_flags &= ~SB_RDONLY; |
2ff7e61e | 2380 | ret = btrfs_prepare_sprout(fs_info); |
d31c32f6 AJ |
2381 | if (ret) { |
2382 | btrfs_abort_transaction(trans, ret); | |
2383 | goto error_trans; | |
2384 | } | |
2b82032c | 2385 | } |
788f20eb | 2386 | |
0b246afa | 2387 | device->fs_devices = fs_info->fs_devices; |
e5e9a520 | 2388 | |
0b246afa | 2389 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
34441361 | 2390 | mutex_lock(&fs_info->chunk_mutex); |
0b246afa | 2391 | list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices); |
2b82032c | 2392 | list_add(&device->dev_alloc_list, |
0b246afa JM |
2393 | &fs_info->fs_devices->alloc_list); |
2394 | fs_info->fs_devices->num_devices++; | |
2395 | fs_info->fs_devices->open_devices++; | |
2396 | fs_info->fs_devices->rw_devices++; | |
2397 | fs_info->fs_devices->total_devices++; | |
2398 | fs_info->fs_devices->total_rw_bytes += device->total_bytes; | |
325cd4ba | 2399 | |
a5ed45f8 | 2400 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
2bf64758 | 2401 | |
e884f4f0 | 2402 | if (!blk_queue_nonrot(q)) |
0b246afa | 2403 | fs_info->fs_devices->rotating = 1; |
c289811c | 2404 | |
0b246afa JM |
2405 | tmp = btrfs_super_total_bytes(fs_info->super_copy); |
2406 | btrfs_set_super_total_bytes(fs_info->super_copy, | |
7dfb8be1 | 2407 | round_down(tmp + device->total_bytes, fs_info->sectorsize)); |
788f20eb | 2408 | |
0b246afa JM |
2409 | tmp = btrfs_super_num_devices(fs_info->super_copy); |
2410 | btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1); | |
0d39376a AJ |
2411 | |
2412 | /* add sysfs device entry */ | |
0b246afa | 2413 | btrfs_sysfs_add_device_link(fs_info->fs_devices, device); |
0d39376a | 2414 | |
2196d6e8 MX |
2415 | /* |
2416 | * we've got more storage, clear any full flags on the space | |
2417 | * infos | |
2418 | */ | |
0b246afa | 2419 | btrfs_clear_space_info_full(fs_info); |
2196d6e8 | 2420 | |
34441361 | 2421 | mutex_unlock(&fs_info->chunk_mutex); |
0b246afa | 2422 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
788f20eb | 2423 | |
2b82032c | 2424 | if (seeding_dev) { |
34441361 | 2425 | mutex_lock(&fs_info->chunk_mutex); |
e4a4dce7 | 2426 | ret = init_first_rw_device(trans, fs_info); |
34441361 | 2427 | mutex_unlock(&fs_info->chunk_mutex); |
005d6427 | 2428 | if (ret) { |
66642832 | 2429 | btrfs_abort_transaction(trans, ret); |
d31c32f6 | 2430 | goto error_sysfs; |
005d6427 | 2431 | } |
2196d6e8 MX |
2432 | } |
2433 | ||
0b246afa | 2434 | ret = btrfs_add_device(trans, fs_info, device); |
2196d6e8 | 2435 | if (ret) { |
66642832 | 2436 | btrfs_abort_transaction(trans, ret); |
d31c32f6 | 2437 | goto error_sysfs; |
2196d6e8 MX |
2438 | } |
2439 | ||
2440 | if (seeding_dev) { | |
2441 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; | |
2442 | ||
0b246afa | 2443 | ret = btrfs_finish_sprout(trans, fs_info); |
005d6427 | 2444 | if (ret) { |
66642832 | 2445 | btrfs_abort_transaction(trans, ret); |
d31c32f6 | 2446 | goto error_sysfs; |
005d6427 | 2447 | } |
b2373f25 AJ |
2448 | |
2449 | /* Sprouting would change fsid of the mounted root, | |
2450 | * so rename the fsid on the sysfs | |
2451 | */ | |
2452 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", | |
0b246afa JM |
2453 | fs_info->fsid); |
2454 | if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf)) | |
2455 | btrfs_warn(fs_info, | |
2456 | "sysfs: failed to create fsid for sprout"); | |
2b82032c YZ |
2457 | } |
2458 | ||
3a45bb20 | 2459 | ret = btrfs_commit_transaction(trans); |
a2135011 | 2460 | |
2b82032c YZ |
2461 | if (seeding_dev) { |
2462 | mutex_unlock(&uuid_mutex); | |
2463 | up_write(&sb->s_umount); | |
7132a262 | 2464 | unlocked = true; |
788f20eb | 2465 | |
79787eaa JM |
2466 | if (ret) /* transaction commit */ |
2467 | return ret; | |
2468 | ||
2ff7e61e | 2469 | ret = btrfs_relocate_sys_chunks(fs_info); |
79787eaa | 2470 | if (ret < 0) |
0b246afa | 2471 | btrfs_handle_fs_error(fs_info, ret, |
5d163e0e | 2472 | "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command."); |
671415b7 MX |
2473 | trans = btrfs_attach_transaction(root); |
2474 | if (IS_ERR(trans)) { | |
2475 | if (PTR_ERR(trans) == -ENOENT) | |
2476 | return 0; | |
7132a262 AJ |
2477 | ret = PTR_ERR(trans); |
2478 | trans = NULL; | |
2479 | goto error_sysfs; | |
671415b7 | 2480 | } |
3a45bb20 | 2481 | ret = btrfs_commit_transaction(trans); |
2b82032c | 2482 | } |
c9e9f97b | 2483 | |
5a1972bd QW |
2484 | /* Update ctime/mtime for libblkid */ |
2485 | update_dev_time(device_path); | |
2b82032c | 2486 | return ret; |
79787eaa | 2487 | |
d31c32f6 AJ |
2488 | error_sysfs: |
2489 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, device); | |
79787eaa | 2490 | error_trans: |
0af2c4bf | 2491 | if (seeding_dev) |
1751e8a6 | 2492 | sb->s_flags |= SB_RDONLY; |
7132a262 AJ |
2493 | if (trans) |
2494 | btrfs_end_transaction(trans); | |
606686ee | 2495 | rcu_string_free(device->name); |
3065ae5b | 2496 | bio_put(device->flush_bio); |
79787eaa | 2497 | kfree(device); |
2b82032c | 2498 | error: |
e525fd89 | 2499 | blkdev_put(bdev, FMODE_EXCL); |
7132a262 | 2500 | if (seeding_dev && !unlocked) { |
2b82032c YZ |
2501 | mutex_unlock(&uuid_mutex); |
2502 | up_write(&sb->s_umount); | |
2503 | } | |
c9e9f97b | 2504 | return ret; |
788f20eb CM |
2505 | } |
2506 | ||
2ff7e61e | 2507 | int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
da353f6b | 2508 | const char *device_path, |
1c43366d | 2509 | struct btrfs_device *srcdev, |
e93c89c1 SB |
2510 | struct btrfs_device **device_out) |
2511 | { | |
2512 | struct request_queue *q; | |
2513 | struct btrfs_device *device; | |
2514 | struct block_device *bdev; | |
e93c89c1 SB |
2515 | struct list_head *devices; |
2516 | struct rcu_string *name; | |
12bd2fc0 | 2517 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
e93c89c1 SB |
2518 | int ret = 0; |
2519 | ||
2520 | *device_out = NULL; | |
1c43366d MX |
2521 | if (fs_info->fs_devices->seeding) { |
2522 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); | |
e93c89c1 | 2523 | return -EINVAL; |
1c43366d | 2524 | } |
e93c89c1 SB |
2525 | |
2526 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, | |
2527 | fs_info->bdev_holder); | |
1c43366d MX |
2528 | if (IS_ERR(bdev)) { |
2529 | btrfs_err(fs_info, "target device %s is invalid!", device_path); | |
e93c89c1 | 2530 | return PTR_ERR(bdev); |
1c43366d | 2531 | } |
e93c89c1 SB |
2532 | |
2533 | filemap_write_and_wait(bdev->bd_inode->i_mapping); | |
2534 | ||
2535 | devices = &fs_info->fs_devices->devices; | |
2536 | list_for_each_entry(device, devices, dev_list) { | |
2537 | if (device->bdev == bdev) { | |
5d163e0e JM |
2538 | btrfs_err(fs_info, |
2539 | "target device is in the filesystem!"); | |
e93c89c1 SB |
2540 | ret = -EEXIST; |
2541 | goto error; | |
2542 | } | |
2543 | } | |
2544 | ||
1c43366d | 2545 | |
7cc8e58d MX |
2546 | if (i_size_read(bdev->bd_inode) < |
2547 | btrfs_device_get_total_bytes(srcdev)) { | |
5d163e0e JM |
2548 | btrfs_err(fs_info, |
2549 | "target device is smaller than source device!"); | |
1c43366d MX |
2550 | ret = -EINVAL; |
2551 | goto error; | |
2552 | } | |
2553 | ||
2554 | ||
12bd2fc0 ID |
2555 | device = btrfs_alloc_device(NULL, &devid, NULL); |
2556 | if (IS_ERR(device)) { | |
2557 | ret = PTR_ERR(device); | |
e93c89c1 SB |
2558 | goto error; |
2559 | } | |
2560 | ||
6165572c | 2561 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
e93c89c1 | 2562 | if (!name) { |
3065ae5b | 2563 | bio_put(device->flush_bio); |
e93c89c1 SB |
2564 | kfree(device); |
2565 | ret = -ENOMEM; | |
2566 | goto error; | |
2567 | } | |
2568 | rcu_assign_pointer(device->name, name); | |
2569 | ||
2570 | q = bdev_get_queue(bdev); | |
2571 | if (blk_queue_discard(q)) | |
2572 | device->can_discard = 1; | |
0b246afa | 2573 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
e93c89c1 | 2574 | device->writeable = 1; |
e93c89c1 | 2575 | device->generation = 0; |
0b246afa JM |
2576 | device->io_width = fs_info->sectorsize; |
2577 | device->io_align = fs_info->sectorsize; | |
2578 | device->sector_size = fs_info->sectorsize; | |
7cc8e58d MX |
2579 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
2580 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); | |
2581 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); | |
935e5cc9 MX |
2582 | ASSERT(list_empty(&srcdev->resized_list)); |
2583 | device->commit_total_bytes = srcdev->commit_total_bytes; | |
ce7213c7 | 2584 | device->commit_bytes_used = device->bytes_used; |
fb456252 | 2585 | device->fs_info = fs_info; |
e93c89c1 SB |
2586 | device->bdev = bdev; |
2587 | device->in_fs_metadata = 1; | |
2588 | device->is_tgtdev_for_dev_replace = 1; | |
2589 | device->mode = FMODE_EXCL; | |
27087f37 | 2590 | device->dev_stats_valid = 1; |
9f6d2510 | 2591 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
e93c89c1 SB |
2592 | device->fs_devices = fs_info->fs_devices; |
2593 | list_add(&device->dev_list, &fs_info->fs_devices->devices); | |
2594 | fs_info->fs_devices->num_devices++; | |
2595 | fs_info->fs_devices->open_devices++; | |
0b246afa | 2596 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
e93c89c1 SB |
2597 | |
2598 | *device_out = device; | |
2599 | return ret; | |
2600 | ||
2601 | error: | |
2602 | blkdev_put(bdev, FMODE_EXCL); | |
2603 | return ret; | |
2604 | } | |
2605 | ||
2606 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, | |
2607 | struct btrfs_device *tgtdev) | |
2608 | { | |
da17066c JM |
2609 | u32 sectorsize = fs_info->sectorsize; |
2610 | ||
e93c89c1 | 2611 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
da17066c JM |
2612 | tgtdev->io_width = sectorsize; |
2613 | tgtdev->io_align = sectorsize; | |
2614 | tgtdev->sector_size = sectorsize; | |
fb456252 | 2615 | tgtdev->fs_info = fs_info; |
e93c89c1 SB |
2616 | tgtdev->in_fs_metadata = 1; |
2617 | } | |
2618 | ||
d397712b CM |
2619 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
2620 | struct btrfs_device *device) | |
0b86a832 CM |
2621 | { |
2622 | int ret; | |
2623 | struct btrfs_path *path; | |
0b246afa | 2624 | struct btrfs_root *root = device->fs_info->chunk_root; |
0b86a832 CM |
2625 | struct btrfs_dev_item *dev_item; |
2626 | struct extent_buffer *leaf; | |
2627 | struct btrfs_key key; | |
2628 | ||
0b86a832 CM |
2629 | path = btrfs_alloc_path(); |
2630 | if (!path) | |
2631 | return -ENOMEM; | |
2632 | ||
2633 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
2634 | key.type = BTRFS_DEV_ITEM_KEY; | |
2635 | key.offset = device->devid; | |
2636 | ||
2637 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
2638 | if (ret < 0) | |
2639 | goto out; | |
2640 | ||
2641 | if (ret > 0) { | |
2642 | ret = -ENOENT; | |
2643 | goto out; | |
2644 | } | |
2645 | ||
2646 | leaf = path->nodes[0]; | |
2647 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
2648 | ||
2649 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
2650 | btrfs_set_device_type(leaf, dev_item, device->type); | |
2651 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
2652 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
2653 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
7cc8e58d MX |
2654 | btrfs_set_device_total_bytes(leaf, dev_item, |
2655 | btrfs_device_get_disk_total_bytes(device)); | |
2656 | btrfs_set_device_bytes_used(leaf, dev_item, | |
2657 | btrfs_device_get_bytes_used(device)); | |
0b86a832 CM |
2658 | btrfs_mark_buffer_dirty(leaf); |
2659 | ||
2660 | out: | |
2661 | btrfs_free_path(path); | |
2662 | return ret; | |
2663 | } | |
2664 | ||
2196d6e8 | 2665 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
8f18cf13 CM |
2666 | struct btrfs_device *device, u64 new_size) |
2667 | { | |
0b246afa JM |
2668 | struct btrfs_fs_info *fs_info = device->fs_info; |
2669 | struct btrfs_super_block *super_copy = fs_info->super_copy; | |
935e5cc9 | 2670 | struct btrfs_fs_devices *fs_devices; |
2196d6e8 MX |
2671 | u64 old_total; |
2672 | u64 diff; | |
8f18cf13 | 2673 | |
2b82032c YZ |
2674 | if (!device->writeable) |
2675 | return -EACCES; | |
2196d6e8 | 2676 | |
7dfb8be1 NB |
2677 | new_size = round_down(new_size, fs_info->sectorsize); |
2678 | ||
34441361 | 2679 | mutex_lock(&fs_info->chunk_mutex); |
2196d6e8 | 2680 | old_total = btrfs_super_total_bytes(super_copy); |
0e4324a4 | 2681 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
2196d6e8 | 2682 | |
63a212ab | 2683 | if (new_size <= device->total_bytes || |
2196d6e8 | 2684 | device->is_tgtdev_for_dev_replace) { |
34441361 | 2685 | mutex_unlock(&fs_info->chunk_mutex); |
2b82032c | 2686 | return -EINVAL; |
2196d6e8 | 2687 | } |
2b82032c | 2688 | |
0b246afa | 2689 | fs_devices = fs_info->fs_devices; |
2b82032c | 2690 | |
7dfb8be1 NB |
2691 | btrfs_set_super_total_bytes(super_copy, |
2692 | round_down(old_total + diff, fs_info->sectorsize)); | |
2b82032c YZ |
2693 | device->fs_devices->total_rw_bytes += diff; |
2694 | ||
7cc8e58d MX |
2695 | btrfs_device_set_total_bytes(device, new_size); |
2696 | btrfs_device_set_disk_total_bytes(device, new_size); | |
fb456252 | 2697 | btrfs_clear_space_info_full(device->fs_info); |
935e5cc9 MX |
2698 | if (list_empty(&device->resized_list)) |
2699 | list_add_tail(&device->resized_list, | |
2700 | &fs_devices->resized_devices); | |
34441361 | 2701 | mutex_unlock(&fs_info->chunk_mutex); |
4184ea7f | 2702 | |
8f18cf13 CM |
2703 | return btrfs_update_device(trans, device); |
2704 | } | |
2705 | ||
2706 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, | |
408fbf19 | 2707 | struct btrfs_fs_info *fs_info, u64 chunk_offset) |
8f18cf13 | 2708 | { |
5b4aacef | 2709 | struct btrfs_root *root = fs_info->chunk_root; |
8f18cf13 CM |
2710 | int ret; |
2711 | struct btrfs_path *path; | |
2712 | struct btrfs_key key; | |
2713 | ||
8f18cf13 CM |
2714 | path = btrfs_alloc_path(); |
2715 | if (!path) | |
2716 | return -ENOMEM; | |
2717 | ||
408fbf19 | 2718 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
8f18cf13 CM |
2719 | key.offset = chunk_offset; |
2720 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2721 | ||
2722 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
79787eaa JM |
2723 | if (ret < 0) |
2724 | goto out; | |
2725 | else if (ret > 0) { /* Logic error or corruption */ | |
0b246afa JM |
2726 | btrfs_handle_fs_error(fs_info, -ENOENT, |
2727 | "Failed lookup while freeing chunk."); | |
79787eaa JM |
2728 | ret = -ENOENT; |
2729 | goto out; | |
2730 | } | |
8f18cf13 CM |
2731 | |
2732 | ret = btrfs_del_item(trans, root, path); | |
79787eaa | 2733 | if (ret < 0) |
0b246afa JM |
2734 | btrfs_handle_fs_error(fs_info, ret, |
2735 | "Failed to delete chunk item."); | |
79787eaa | 2736 | out: |
8f18cf13 | 2737 | btrfs_free_path(path); |
65a246c5 | 2738 | return ret; |
8f18cf13 CM |
2739 | } |
2740 | ||
408fbf19 | 2741 | static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
8f18cf13 | 2742 | { |
0b246afa | 2743 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
8f18cf13 CM |
2744 | struct btrfs_disk_key *disk_key; |
2745 | struct btrfs_chunk *chunk; | |
2746 | u8 *ptr; | |
2747 | int ret = 0; | |
2748 | u32 num_stripes; | |
2749 | u32 array_size; | |
2750 | u32 len = 0; | |
2751 | u32 cur; | |
2752 | struct btrfs_key key; | |
2753 | ||
34441361 | 2754 | mutex_lock(&fs_info->chunk_mutex); |
8f18cf13 CM |
2755 | array_size = btrfs_super_sys_array_size(super_copy); |
2756 | ||
2757 | ptr = super_copy->sys_chunk_array; | |
2758 | cur = 0; | |
2759 | ||
2760 | while (cur < array_size) { | |
2761 | disk_key = (struct btrfs_disk_key *)ptr; | |
2762 | btrfs_disk_key_to_cpu(&key, disk_key); | |
2763 | ||
2764 | len = sizeof(*disk_key); | |
2765 | ||
2766 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { | |
2767 | chunk = (struct btrfs_chunk *)(ptr + len); | |
2768 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); | |
2769 | len += btrfs_chunk_item_size(num_stripes); | |
2770 | } else { | |
2771 | ret = -EIO; | |
2772 | break; | |
2773 | } | |
408fbf19 | 2774 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
8f18cf13 CM |
2775 | key.offset == chunk_offset) { |
2776 | memmove(ptr, ptr + len, array_size - (cur + len)); | |
2777 | array_size -= len; | |
2778 | btrfs_set_super_sys_array_size(super_copy, array_size); | |
2779 | } else { | |
2780 | ptr += len; | |
2781 | cur += len; | |
2782 | } | |
2783 | } | |
34441361 | 2784 | mutex_unlock(&fs_info->chunk_mutex); |
8f18cf13 CM |
2785 | return ret; |
2786 | } | |
2787 | ||
592d92ee LB |
2788 | static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info, |
2789 | u64 logical, u64 length) | |
2790 | { | |
2791 | struct extent_map_tree *em_tree; | |
2792 | struct extent_map *em; | |
2793 | ||
2794 | em_tree = &fs_info->mapping_tree.map_tree; | |
2795 | read_lock(&em_tree->lock); | |
2796 | em = lookup_extent_mapping(em_tree, logical, length); | |
2797 | read_unlock(&em_tree->lock); | |
2798 | ||
2799 | if (!em) { | |
2800 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", | |
2801 | logical, length); | |
2802 | return ERR_PTR(-EINVAL); | |
2803 | } | |
2804 | ||
2805 | if (em->start > logical || em->start + em->len < logical) { | |
2806 | btrfs_crit(fs_info, | |
2807 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", | |
2808 | logical, length, em->start, em->start + em->len); | |
2809 | free_extent_map(em); | |
2810 | return ERR_PTR(-EINVAL); | |
2811 | } | |
2812 | ||
2813 | /* callers are responsible for dropping em's ref. */ | |
2814 | return em; | |
2815 | } | |
2816 | ||
47ab2a6c | 2817 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
5b4aacef | 2818 | struct btrfs_fs_info *fs_info, u64 chunk_offset) |
8f18cf13 | 2819 | { |
8f18cf13 CM |
2820 | struct extent_map *em; |
2821 | struct map_lookup *map; | |
2196d6e8 | 2822 | u64 dev_extent_len = 0; |
47ab2a6c | 2823 | int i, ret = 0; |
0b246afa | 2824 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
8f18cf13 | 2825 | |
592d92ee LB |
2826 | em = get_chunk_map(fs_info, chunk_offset, 1); |
2827 | if (IS_ERR(em)) { | |
47ab2a6c JB |
2828 | /* |
2829 | * This is a logic error, but we don't want to just rely on the | |
bb7ab3b9 | 2830 | * user having built with ASSERT enabled, so if ASSERT doesn't |
47ab2a6c JB |
2831 | * do anything we still error out. |
2832 | */ | |
2833 | ASSERT(0); | |
592d92ee | 2834 | return PTR_ERR(em); |
47ab2a6c | 2835 | } |
95617d69 | 2836 | map = em->map_lookup; |
34441361 | 2837 | mutex_lock(&fs_info->chunk_mutex); |
2ff7e61e | 2838 | check_system_chunk(trans, fs_info, map->type); |
34441361 | 2839 | mutex_unlock(&fs_info->chunk_mutex); |
8f18cf13 | 2840 | |
57ba4cb8 FM |
2841 | /* |
2842 | * Take the device list mutex to prevent races with the final phase of | |
2843 | * a device replace operation that replaces the device object associated | |
2844 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). | |
2845 | */ | |
2846 | mutex_lock(&fs_devices->device_list_mutex); | |
8f18cf13 | 2847 | for (i = 0; i < map->num_stripes; i++) { |
47ab2a6c | 2848 | struct btrfs_device *device = map->stripes[i].dev; |
2196d6e8 MX |
2849 | ret = btrfs_free_dev_extent(trans, device, |
2850 | map->stripes[i].physical, | |
2851 | &dev_extent_len); | |
47ab2a6c | 2852 | if (ret) { |
57ba4cb8 | 2853 | mutex_unlock(&fs_devices->device_list_mutex); |
66642832 | 2854 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2855 | goto out; |
2856 | } | |
a061fc8d | 2857 | |
2196d6e8 | 2858 | if (device->bytes_used > 0) { |
34441361 | 2859 | mutex_lock(&fs_info->chunk_mutex); |
2196d6e8 MX |
2860 | btrfs_device_set_bytes_used(device, |
2861 | device->bytes_used - dev_extent_len); | |
a5ed45f8 | 2862 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
0b246afa | 2863 | btrfs_clear_space_info_full(fs_info); |
34441361 | 2864 | mutex_unlock(&fs_info->chunk_mutex); |
2196d6e8 | 2865 | } |
a061fc8d | 2866 | |
dfe25020 CM |
2867 | if (map->stripes[i].dev) { |
2868 | ret = btrfs_update_device(trans, map->stripes[i].dev); | |
47ab2a6c | 2869 | if (ret) { |
57ba4cb8 | 2870 | mutex_unlock(&fs_devices->device_list_mutex); |
66642832 | 2871 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2872 | goto out; |
2873 | } | |
dfe25020 | 2874 | } |
8f18cf13 | 2875 | } |
57ba4cb8 FM |
2876 | mutex_unlock(&fs_devices->device_list_mutex); |
2877 | ||
408fbf19 | 2878 | ret = btrfs_free_chunk(trans, fs_info, chunk_offset); |
47ab2a6c | 2879 | if (ret) { |
66642832 | 2880 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2881 | goto out; |
2882 | } | |
8f18cf13 | 2883 | |
6bccf3ab | 2884 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
1abe9b8a | 2885 | |
8f18cf13 | 2886 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
408fbf19 | 2887 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
47ab2a6c | 2888 | if (ret) { |
66642832 | 2889 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2890 | goto out; |
2891 | } | |
8f18cf13 CM |
2892 | } |
2893 | ||
6bccf3ab | 2894 | ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em); |
47ab2a6c | 2895 | if (ret) { |
66642832 | 2896 | btrfs_abort_transaction(trans, ret); |
47ab2a6c JB |
2897 | goto out; |
2898 | } | |
2b82032c | 2899 | |
47ab2a6c | 2900 | out: |
2b82032c YZ |
2901 | /* once for us */ |
2902 | free_extent_map(em); | |
47ab2a6c JB |
2903 | return ret; |
2904 | } | |
2b82032c | 2905 | |
5b4aacef | 2906 | static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
47ab2a6c | 2907 | { |
5b4aacef | 2908 | struct btrfs_root *root = fs_info->chunk_root; |
19c4d2f9 | 2909 | struct btrfs_trans_handle *trans; |
47ab2a6c | 2910 | int ret; |
2b82032c | 2911 | |
67c5e7d4 FM |
2912 | /* |
2913 | * Prevent races with automatic removal of unused block groups. | |
2914 | * After we relocate and before we remove the chunk with offset | |
2915 | * chunk_offset, automatic removal of the block group can kick in, | |
2916 | * resulting in a failure when calling btrfs_remove_chunk() below. | |
2917 | * | |
2918 | * Make sure to acquire this mutex before doing a tree search (dev | |
2919 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might | |
2920 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after | |
2921 | * we release the path used to search the chunk/dev tree and before | |
2922 | * the current task acquires this mutex and calls us. | |
2923 | */ | |
0b246afa | 2924 | ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex)); |
67c5e7d4 | 2925 | |
0b246afa | 2926 | ret = btrfs_can_relocate(fs_info, chunk_offset); |
47ab2a6c JB |
2927 | if (ret) |
2928 | return -ENOSPC; | |
2929 | ||
2930 | /* step one, relocate all the extents inside this chunk */ | |
2ff7e61e | 2931 | btrfs_scrub_pause(fs_info); |
0b246afa | 2932 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
2ff7e61e | 2933 | btrfs_scrub_continue(fs_info); |
47ab2a6c JB |
2934 | if (ret) |
2935 | return ret; | |
2936 | ||
19c4d2f9 CM |
2937 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
2938 | chunk_offset); | |
2939 | if (IS_ERR(trans)) { | |
2940 | ret = PTR_ERR(trans); | |
2941 | btrfs_handle_fs_error(root->fs_info, ret, NULL); | |
2942 | return ret; | |
2943 | } | |
2944 | ||
47ab2a6c | 2945 | /* |
19c4d2f9 CM |
2946 | * step two, delete the device extents and the |
2947 | * chunk tree entries | |
47ab2a6c | 2948 | */ |
5b4aacef | 2949 | ret = btrfs_remove_chunk(trans, fs_info, chunk_offset); |
3a45bb20 | 2950 | btrfs_end_transaction(trans); |
19c4d2f9 | 2951 | return ret; |
2b82032c YZ |
2952 | } |
2953 | ||
2ff7e61e | 2954 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
2b82032c | 2955 | { |
0b246afa | 2956 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
2b82032c YZ |
2957 | struct btrfs_path *path; |
2958 | struct extent_buffer *leaf; | |
2959 | struct btrfs_chunk *chunk; | |
2960 | struct btrfs_key key; | |
2961 | struct btrfs_key found_key; | |
2b82032c | 2962 | u64 chunk_type; |
ba1bf481 JB |
2963 | bool retried = false; |
2964 | int failed = 0; | |
2b82032c YZ |
2965 | int ret; |
2966 | ||
2967 | path = btrfs_alloc_path(); | |
2968 | if (!path) | |
2969 | return -ENOMEM; | |
2970 | ||
ba1bf481 | 2971 | again: |
2b82032c YZ |
2972 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
2973 | key.offset = (u64)-1; | |
2974 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2975 | ||
2976 | while (1) { | |
0b246afa | 2977 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
2b82032c | 2978 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
67c5e7d4 | 2979 | if (ret < 0) { |
0b246afa | 2980 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
2b82032c | 2981 | goto error; |
67c5e7d4 | 2982 | } |
79787eaa | 2983 | BUG_ON(ret == 0); /* Corruption */ |
2b82032c YZ |
2984 | |
2985 | ret = btrfs_previous_item(chunk_root, path, key.objectid, | |
2986 | key.type); | |
67c5e7d4 | 2987 | if (ret) |
0b246afa | 2988 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
2b82032c YZ |
2989 | if (ret < 0) |
2990 | goto error; | |
2991 | if (ret > 0) | |
2992 | break; | |
1a40e23b | 2993 | |
2b82032c YZ |
2994 | leaf = path->nodes[0]; |
2995 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1a40e23b | 2996 | |
2b82032c YZ |
2997 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
2998 | struct btrfs_chunk); | |
2999 | chunk_type = btrfs_chunk_type(leaf, chunk); | |
b3b4aa74 | 3000 | btrfs_release_path(path); |
8f18cf13 | 3001 | |
2b82032c | 3002 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
0b246afa | 3003 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
ba1bf481 JB |
3004 | if (ret == -ENOSPC) |
3005 | failed++; | |
14586651 HS |
3006 | else |
3007 | BUG_ON(ret); | |
2b82032c | 3008 | } |
0b246afa | 3009 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
8f18cf13 | 3010 | |
2b82032c YZ |
3011 | if (found_key.offset == 0) |
3012 | break; | |
3013 | key.offset = found_key.offset - 1; | |
3014 | } | |
3015 | ret = 0; | |
ba1bf481 JB |
3016 | if (failed && !retried) { |
3017 | failed = 0; | |
3018 | retried = true; | |
3019 | goto again; | |
fae7f21c | 3020 | } else if (WARN_ON(failed && retried)) { |
ba1bf481 JB |
3021 | ret = -ENOSPC; |
3022 | } | |
2b82032c YZ |
3023 | error: |
3024 | btrfs_free_path(path); | |
3025 | return ret; | |
8f18cf13 CM |
3026 | } |
3027 | ||
6bccf3ab | 3028 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
0940ebf6 ID |
3029 | struct btrfs_balance_control *bctl) |
3030 | { | |
6bccf3ab | 3031 | struct btrfs_root *root = fs_info->tree_root; |
0940ebf6 ID |
3032 | struct btrfs_trans_handle *trans; |
3033 | struct btrfs_balance_item *item; | |
3034 | struct btrfs_disk_balance_args disk_bargs; | |
3035 | struct btrfs_path *path; | |
3036 | struct extent_buffer *leaf; | |
3037 | struct btrfs_key key; | |
3038 | int ret, err; | |
3039 | ||
3040 | path = btrfs_alloc_path(); | |
3041 | if (!path) | |
3042 | return -ENOMEM; | |
3043 | ||
3044 | trans = btrfs_start_transaction(root, 0); | |
3045 | if (IS_ERR(trans)) { | |
3046 | btrfs_free_path(path); | |
3047 | return PTR_ERR(trans); | |
3048 | } | |
3049 | ||
3050 | key.objectid = BTRFS_BALANCE_OBJECTID; | |
c479cb4f | 3051 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
0940ebf6 ID |
3052 | key.offset = 0; |
3053 | ||
3054 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
3055 | sizeof(*item)); | |
3056 | if (ret) | |
3057 | goto out; | |
3058 | ||
3059 | leaf = path->nodes[0]; | |
3060 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); | |
3061 | ||
b159fa28 | 3062 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
0940ebf6 ID |
3063 | |
3064 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); | |
3065 | btrfs_set_balance_data(leaf, item, &disk_bargs); | |
3066 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); | |
3067 | btrfs_set_balance_meta(leaf, item, &disk_bargs); | |
3068 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); | |
3069 | btrfs_set_balance_sys(leaf, item, &disk_bargs); | |
3070 | ||
3071 | btrfs_set_balance_flags(leaf, item, bctl->flags); | |
3072 | ||
3073 | btrfs_mark_buffer_dirty(leaf); | |
3074 | out: | |
3075 | btrfs_free_path(path); | |
3a45bb20 | 3076 | err = btrfs_commit_transaction(trans); |
0940ebf6 ID |
3077 | if (err && !ret) |
3078 | ret = err; | |
3079 | return ret; | |
3080 | } | |
3081 | ||
6bccf3ab | 3082 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
0940ebf6 | 3083 | { |
6bccf3ab | 3084 | struct btrfs_root *root = fs_info->tree_root; |
0940ebf6 ID |
3085 | struct btrfs_trans_handle *trans; |
3086 | struct btrfs_path *path; | |
3087 | struct btrfs_key key; | |
3088 | int ret, err; | |
3089 | ||
3090 | path = btrfs_alloc_path(); | |
3091 | if (!path) | |
3092 | return -ENOMEM; | |
3093 | ||
3094 | trans = btrfs_start_transaction(root, 0); | |
3095 | if (IS_ERR(trans)) { | |
3096 | btrfs_free_path(path); | |
3097 | return PTR_ERR(trans); | |
3098 | } | |
3099 | ||
3100 | key.objectid = BTRFS_BALANCE_OBJECTID; | |
c479cb4f | 3101 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
0940ebf6 ID |
3102 | key.offset = 0; |
3103 | ||
3104 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
3105 | if (ret < 0) | |
3106 | goto out; | |
3107 | if (ret > 0) { | |
3108 | ret = -ENOENT; | |
3109 | goto out; | |
3110 | } | |
3111 | ||
3112 | ret = btrfs_del_item(trans, root, path); | |
3113 | out: | |
3114 | btrfs_free_path(path); | |
3a45bb20 | 3115 | err = btrfs_commit_transaction(trans); |
0940ebf6 ID |
3116 | if (err && !ret) |
3117 | ret = err; | |
3118 | return ret; | |
3119 | } | |
3120 | ||
59641015 ID |
3121 | /* |
3122 | * This is a heuristic used to reduce the number of chunks balanced on | |
3123 | * resume after balance was interrupted. | |
3124 | */ | |
3125 | static void update_balance_args(struct btrfs_balance_control *bctl) | |
3126 | { | |
3127 | /* | |
3128 | * Turn on soft mode for chunk types that were being converted. | |
3129 | */ | |
3130 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) | |
3131 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; | |
3132 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) | |
3133 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; | |
3134 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) | |
3135 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; | |
3136 | ||
3137 | /* | |
3138 | * Turn on usage filter if is not already used. The idea is | |
3139 | * that chunks that we have already balanced should be | |
3140 | * reasonably full. Don't do it for chunks that are being | |
3141 | * converted - that will keep us from relocating unconverted | |
3142 | * (albeit full) chunks. | |
3143 | */ | |
3144 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && | |
bc309467 | 3145 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
59641015 ID |
3146 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
3147 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; | |
3148 | bctl->data.usage = 90; | |
3149 | } | |
3150 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && | |
bc309467 | 3151 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
59641015 ID |
3152 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
3153 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; | |
3154 | bctl->sys.usage = 90; | |
3155 | } | |
3156 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && | |
bc309467 | 3157 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
59641015 ID |
3158 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
3159 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; | |
3160 | bctl->meta.usage = 90; | |
3161 | } | |
3162 | } | |
3163 | ||
c9e9f97b ID |
3164 | /* |
3165 | * Should be called with both balance and volume mutexes held to | |
3166 | * serialize other volume operations (add_dev/rm_dev/resize) with | |
3167 | * restriper. Same goes for unset_balance_control. | |
3168 | */ | |
3169 | static void set_balance_control(struct btrfs_balance_control *bctl) | |
3170 | { | |
3171 | struct btrfs_fs_info *fs_info = bctl->fs_info; | |
3172 | ||
3173 | BUG_ON(fs_info->balance_ctl); | |
3174 | ||
3175 | spin_lock(&fs_info->balance_lock); | |
3176 | fs_info->balance_ctl = bctl; | |
3177 | spin_unlock(&fs_info->balance_lock); | |
3178 | } | |
3179 | ||
3180 | static void unset_balance_control(struct btrfs_fs_info *fs_info) | |
3181 | { | |
3182 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
3183 | ||
3184 | BUG_ON(!fs_info->balance_ctl); | |
3185 | ||
3186 | spin_lock(&fs_info->balance_lock); | |
3187 | fs_info->balance_ctl = NULL; | |
3188 | spin_unlock(&fs_info->balance_lock); | |
3189 | ||
3190 | kfree(bctl); | |
3191 | } | |
3192 | ||
ed25e9b2 ID |
3193 | /* |
3194 | * Balance filters. Return 1 if chunk should be filtered out | |
3195 | * (should not be balanced). | |
3196 | */ | |
899c81ea | 3197 | static int chunk_profiles_filter(u64 chunk_type, |
ed25e9b2 ID |
3198 | struct btrfs_balance_args *bargs) |
3199 | { | |
899c81ea ID |
3200 | chunk_type = chunk_to_extended(chunk_type) & |
3201 | BTRFS_EXTENDED_PROFILE_MASK; | |
ed25e9b2 | 3202 | |
899c81ea | 3203 | if (bargs->profiles & chunk_type) |
ed25e9b2 ID |
3204 | return 0; |
3205 | ||
3206 | return 1; | |
3207 | } | |
3208 | ||
dba72cb3 | 3209 | static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
5ce5b3c0 | 3210 | struct btrfs_balance_args *bargs) |
bc309467 DS |
3211 | { |
3212 | struct btrfs_block_group_cache *cache; | |
3213 | u64 chunk_used; | |
3214 | u64 user_thresh_min; | |
3215 | u64 user_thresh_max; | |
3216 | int ret = 1; | |
3217 | ||
3218 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
3219 | chunk_used = btrfs_block_group_used(&cache->item); | |
3220 | ||
3221 | if (bargs->usage_min == 0) | |
3222 | user_thresh_min = 0; | |
3223 | else | |
3224 | user_thresh_min = div_factor_fine(cache->key.offset, | |
3225 | bargs->usage_min); | |
3226 | ||
3227 | if (bargs->usage_max == 0) | |
3228 | user_thresh_max = 1; | |
3229 | else if (bargs->usage_max > 100) | |
3230 | user_thresh_max = cache->key.offset; | |
3231 | else | |
3232 | user_thresh_max = div_factor_fine(cache->key.offset, | |
3233 | bargs->usage_max); | |
3234 | ||
3235 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) | |
3236 | ret = 0; | |
3237 | ||
3238 | btrfs_put_block_group(cache); | |
3239 | return ret; | |
3240 | } | |
3241 | ||
dba72cb3 | 3242 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
bc309467 | 3243 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
5ce5b3c0 ID |
3244 | { |
3245 | struct btrfs_block_group_cache *cache; | |
3246 | u64 chunk_used, user_thresh; | |
3247 | int ret = 1; | |
3248 | ||
3249 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); | |
3250 | chunk_used = btrfs_block_group_used(&cache->item); | |
3251 | ||
bc309467 | 3252 | if (bargs->usage_min == 0) |
3e39cea6 | 3253 | user_thresh = 1; |
a105bb88 ID |
3254 | else if (bargs->usage > 100) |
3255 | user_thresh = cache->key.offset; | |
3256 | else | |
3257 | user_thresh = div_factor_fine(cache->key.offset, | |
3258 | bargs->usage); | |
3259 | ||
5ce5b3c0 ID |
3260 | if (chunk_used < user_thresh) |
3261 | ret = 0; | |
3262 | ||
3263 | btrfs_put_block_group(cache); | |
3264 | return ret; | |
3265 | } | |
3266 | ||
409d404b ID |
3267 | static int chunk_devid_filter(struct extent_buffer *leaf, |
3268 | struct btrfs_chunk *chunk, | |
3269 | struct btrfs_balance_args *bargs) | |
3270 | { | |
3271 | struct btrfs_stripe *stripe; | |
3272 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
3273 | int i; | |
3274 | ||
3275 | for (i = 0; i < num_stripes; i++) { | |
3276 | stripe = btrfs_stripe_nr(chunk, i); | |
3277 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) | |
3278 | return 0; | |
3279 | } | |
3280 | ||
3281 | return 1; | |
3282 | } | |
3283 | ||
94e60d5a ID |
3284 | /* [pstart, pend) */ |
3285 | static int chunk_drange_filter(struct extent_buffer *leaf, | |
3286 | struct btrfs_chunk *chunk, | |
94e60d5a ID |
3287 | struct btrfs_balance_args *bargs) |
3288 | { | |
3289 | struct btrfs_stripe *stripe; | |
3290 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
3291 | u64 stripe_offset; | |
3292 | u64 stripe_length; | |
3293 | int factor; | |
3294 | int i; | |
3295 | ||
3296 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) | |
3297 | return 0; | |
3298 | ||
3299 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | | |
53b381b3 DW |
3300 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
3301 | factor = num_stripes / 2; | |
3302 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { | |
3303 | factor = num_stripes - 1; | |
3304 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { | |
3305 | factor = num_stripes - 2; | |
3306 | } else { | |
3307 | factor = num_stripes; | |
3308 | } | |
94e60d5a ID |
3309 | |
3310 | for (i = 0; i < num_stripes; i++) { | |
3311 | stripe = btrfs_stripe_nr(chunk, i); | |
3312 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) | |
3313 | continue; | |
3314 | ||
3315 | stripe_offset = btrfs_stripe_offset(leaf, stripe); | |
3316 | stripe_length = btrfs_chunk_length(leaf, chunk); | |
b8b93add | 3317 | stripe_length = div_u64(stripe_length, factor); |
94e60d5a ID |
3318 | |
3319 | if (stripe_offset < bargs->pend && | |
3320 | stripe_offset + stripe_length > bargs->pstart) | |
3321 | return 0; | |
3322 | } | |
3323 | ||
3324 | return 1; | |
3325 | } | |
3326 | ||
ea67176a ID |
3327 | /* [vstart, vend) */ |
3328 | static int chunk_vrange_filter(struct extent_buffer *leaf, | |
3329 | struct btrfs_chunk *chunk, | |
3330 | u64 chunk_offset, | |
3331 | struct btrfs_balance_args *bargs) | |
3332 | { | |
3333 | if (chunk_offset < bargs->vend && | |
3334 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) | |
3335 | /* at least part of the chunk is inside this vrange */ | |
3336 | return 0; | |
3337 | ||
3338 | return 1; | |
3339 | } | |
3340 | ||
dee32d0a GAP |
3341 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
3342 | struct btrfs_chunk *chunk, | |
3343 | struct btrfs_balance_args *bargs) | |
3344 | { | |
3345 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
3346 | ||
3347 | if (bargs->stripes_min <= num_stripes | |
3348 | && num_stripes <= bargs->stripes_max) | |
3349 | return 0; | |
3350 | ||
3351 | return 1; | |
3352 | } | |
3353 | ||
899c81ea | 3354 | static int chunk_soft_convert_filter(u64 chunk_type, |
cfa4c961 ID |
3355 | struct btrfs_balance_args *bargs) |
3356 | { | |
3357 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) | |
3358 | return 0; | |
3359 | ||
899c81ea ID |
3360 | chunk_type = chunk_to_extended(chunk_type) & |
3361 | BTRFS_EXTENDED_PROFILE_MASK; | |
cfa4c961 | 3362 | |
899c81ea | 3363 | if (bargs->target == chunk_type) |
cfa4c961 ID |
3364 | return 1; |
3365 | ||
3366 | return 0; | |
3367 | } | |
3368 | ||
2ff7e61e | 3369 | static int should_balance_chunk(struct btrfs_fs_info *fs_info, |
f43ffb60 ID |
3370 | struct extent_buffer *leaf, |
3371 | struct btrfs_chunk *chunk, u64 chunk_offset) | |
3372 | { | |
0b246afa | 3373 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
f43ffb60 ID |
3374 | struct btrfs_balance_args *bargs = NULL; |
3375 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); | |
3376 | ||
3377 | /* type filter */ | |
3378 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & | |
3379 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { | |
3380 | return 0; | |
3381 | } | |
3382 | ||
3383 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) | |
3384 | bargs = &bctl->data; | |
3385 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) | |
3386 | bargs = &bctl->sys; | |
3387 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) | |
3388 | bargs = &bctl->meta; | |
3389 | ||
ed25e9b2 ID |
3390 | /* profiles filter */ |
3391 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && | |
3392 | chunk_profiles_filter(chunk_type, bargs)) { | |
3393 | return 0; | |
5ce5b3c0 ID |
3394 | } |
3395 | ||
3396 | /* usage filter */ | |
3397 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && | |
0b246afa | 3398 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
5ce5b3c0 | 3399 | return 0; |
bc309467 | 3400 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
0b246afa | 3401 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
bc309467 | 3402 | return 0; |
409d404b ID |
3403 | } |
3404 | ||
3405 | /* devid filter */ | |
3406 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && | |
3407 | chunk_devid_filter(leaf, chunk, bargs)) { | |
3408 | return 0; | |
94e60d5a ID |
3409 | } |
3410 | ||
3411 | /* drange filter, makes sense only with devid filter */ | |
3412 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && | |
e4ff5fb5 | 3413 | chunk_drange_filter(leaf, chunk, bargs)) { |
94e60d5a | 3414 | return 0; |
ea67176a ID |
3415 | } |
3416 | ||
3417 | /* vrange filter */ | |
3418 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && | |
3419 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { | |
3420 | return 0; | |
ed25e9b2 ID |
3421 | } |
3422 | ||
dee32d0a GAP |
3423 | /* stripes filter */ |
3424 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && | |
3425 | chunk_stripes_range_filter(leaf, chunk, bargs)) { | |
3426 | return 0; | |
3427 | } | |
3428 | ||
cfa4c961 ID |
3429 | /* soft profile changing mode */ |
3430 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && | |
3431 | chunk_soft_convert_filter(chunk_type, bargs)) { | |
3432 | return 0; | |
3433 | } | |
3434 | ||
7d824b6f DS |
3435 | /* |
3436 | * limited by count, must be the last filter | |
3437 | */ | |
3438 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { | |
3439 | if (bargs->limit == 0) | |
3440 | return 0; | |
3441 | else | |
3442 | bargs->limit--; | |
12907fc7 DS |
3443 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
3444 | /* | |
3445 | * Same logic as the 'limit' filter; the minimum cannot be | |
01327610 | 3446 | * determined here because we do not have the global information |
12907fc7 DS |
3447 | * about the count of all chunks that satisfy the filters. |
3448 | */ | |
3449 | if (bargs->limit_max == 0) | |
3450 | return 0; | |
3451 | else | |
3452 | bargs->limit_max--; | |
7d824b6f DS |
3453 | } |
3454 | ||
f43ffb60 ID |
3455 | return 1; |
3456 | } | |
3457 | ||
c9e9f97b | 3458 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
ec44a35c | 3459 | { |
19a39dce | 3460 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
c9e9f97b ID |
3461 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
3462 | struct btrfs_root *dev_root = fs_info->dev_root; | |
3463 | struct list_head *devices; | |
ec44a35c CM |
3464 | struct btrfs_device *device; |
3465 | u64 old_size; | |
3466 | u64 size_to_free; | |
12907fc7 | 3467 | u64 chunk_type; |
f43ffb60 | 3468 | struct btrfs_chunk *chunk; |
5a488b9d | 3469 | struct btrfs_path *path = NULL; |
ec44a35c | 3470 | struct btrfs_key key; |
ec44a35c | 3471 | struct btrfs_key found_key; |
c9e9f97b | 3472 | struct btrfs_trans_handle *trans; |
f43ffb60 ID |
3473 | struct extent_buffer *leaf; |
3474 | int slot; | |
c9e9f97b ID |
3475 | int ret; |
3476 | int enospc_errors = 0; | |
19a39dce | 3477 | bool counting = true; |
12907fc7 | 3478 | /* The single value limit and min/max limits use the same bytes in the */ |
7d824b6f DS |
3479 | u64 limit_data = bctl->data.limit; |
3480 | u64 limit_meta = bctl->meta.limit; | |
3481 | u64 limit_sys = bctl->sys.limit; | |
12907fc7 DS |
3482 | u32 count_data = 0; |
3483 | u32 count_meta = 0; | |
3484 | u32 count_sys = 0; | |
2c9fe835 | 3485 | int chunk_reserved = 0; |
cf25ce51 | 3486 | u64 bytes_used = 0; |
ec44a35c | 3487 | |
ec44a35c | 3488 | /* step one make some room on all the devices */ |
c9e9f97b | 3489 | devices = &fs_info->fs_devices->devices; |
c6e30871 | 3490 | list_for_each_entry(device, devices, dev_list) { |
7cc8e58d | 3491 | old_size = btrfs_device_get_total_bytes(device); |
ec44a35c | 3492 | size_to_free = div_factor(old_size, 1); |
ee22184b | 3493 | size_to_free = min_t(u64, size_to_free, SZ_1M); |
2b82032c | 3494 | if (!device->writeable || |
7cc8e58d MX |
3495 | btrfs_device_get_total_bytes(device) - |
3496 | btrfs_device_get_bytes_used(device) > size_to_free || | |
63a212ab | 3497 | device->is_tgtdev_for_dev_replace) |
ec44a35c CM |
3498 | continue; |
3499 | ||
3500 | ret = btrfs_shrink_device(device, old_size - size_to_free); | |
ba1bf481 JB |
3501 | if (ret == -ENOSPC) |
3502 | break; | |
5a488b9d LB |
3503 | if (ret) { |
3504 | /* btrfs_shrink_device never returns ret > 0 */ | |
3505 | WARN_ON(ret > 0); | |
3506 | goto error; | |
3507 | } | |
ec44a35c | 3508 | |
a22285a6 | 3509 | trans = btrfs_start_transaction(dev_root, 0); |
5a488b9d LB |
3510 | if (IS_ERR(trans)) { |
3511 | ret = PTR_ERR(trans); | |
3512 | btrfs_info_in_rcu(fs_info, | |
3513 | "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu", | |
3514 | rcu_str_deref(device->name), ret, | |
3515 | old_size, old_size - size_to_free); | |
3516 | goto error; | |
3517 | } | |
ec44a35c CM |
3518 | |
3519 | ret = btrfs_grow_device(trans, device, old_size); | |
5a488b9d | 3520 | if (ret) { |
3a45bb20 | 3521 | btrfs_end_transaction(trans); |
5a488b9d LB |
3522 | /* btrfs_grow_device never returns ret > 0 */ |
3523 | WARN_ON(ret > 0); | |
3524 | btrfs_info_in_rcu(fs_info, | |
3525 | "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu", | |
3526 | rcu_str_deref(device->name), ret, | |
3527 | old_size, old_size - size_to_free); | |
3528 | goto error; | |
3529 | } | |
ec44a35c | 3530 | |
3a45bb20 | 3531 | btrfs_end_transaction(trans); |
ec44a35c CM |
3532 | } |
3533 | ||
3534 | /* step two, relocate all the chunks */ | |
3535 | path = btrfs_alloc_path(); | |
17e9f796 MF |
3536 | if (!path) { |
3537 | ret = -ENOMEM; | |
3538 | goto error; | |
3539 | } | |
19a39dce ID |
3540 | |
3541 | /* zero out stat counters */ | |
3542 | spin_lock(&fs_info->balance_lock); | |
3543 | memset(&bctl->stat, 0, sizeof(bctl->stat)); | |
3544 | spin_unlock(&fs_info->balance_lock); | |
3545 | again: | |
7d824b6f | 3546 | if (!counting) { |
12907fc7 DS |
3547 | /* |
3548 | * The single value limit and min/max limits use the same bytes | |
3549 | * in the | |
3550 | */ | |
7d824b6f DS |
3551 | bctl->data.limit = limit_data; |
3552 | bctl->meta.limit = limit_meta; | |
3553 | bctl->sys.limit = limit_sys; | |
3554 | } | |
ec44a35c CM |
3555 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
3556 | key.offset = (u64)-1; | |
3557 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
3558 | ||
d397712b | 3559 | while (1) { |
19a39dce | 3560 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
a7e99c69 | 3561 | atomic_read(&fs_info->balance_cancel_req)) { |
837d5b6e ID |
3562 | ret = -ECANCELED; |
3563 | goto error; | |
3564 | } | |
3565 | ||
67c5e7d4 | 3566 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
ec44a35c | 3567 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
67c5e7d4 FM |
3568 | if (ret < 0) { |
3569 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
ec44a35c | 3570 | goto error; |
67c5e7d4 | 3571 | } |
ec44a35c CM |
3572 | |
3573 | /* | |
3574 | * this shouldn't happen, it means the last relocate | |
3575 | * failed | |
3576 | */ | |
3577 | if (ret == 0) | |
c9e9f97b | 3578 | BUG(); /* FIXME break ? */ |
ec44a35c CM |
3579 | |
3580 | ret = btrfs_previous_item(chunk_root, path, 0, | |
3581 | BTRFS_CHUNK_ITEM_KEY); | |
c9e9f97b | 3582 | if (ret) { |
67c5e7d4 | 3583 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
c9e9f97b | 3584 | ret = 0; |
ec44a35c | 3585 | break; |
c9e9f97b | 3586 | } |
7d9eb12c | 3587 | |
f43ffb60 ID |
3588 | leaf = path->nodes[0]; |
3589 | slot = path->slots[0]; | |
3590 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
7d9eb12c | 3591 | |
67c5e7d4 FM |
3592 | if (found_key.objectid != key.objectid) { |
3593 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
ec44a35c | 3594 | break; |
67c5e7d4 | 3595 | } |
7d9eb12c | 3596 | |
f43ffb60 | 3597 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
12907fc7 | 3598 | chunk_type = btrfs_chunk_type(leaf, chunk); |
f43ffb60 | 3599 | |
19a39dce ID |
3600 | if (!counting) { |
3601 | spin_lock(&fs_info->balance_lock); | |
3602 | bctl->stat.considered++; | |
3603 | spin_unlock(&fs_info->balance_lock); | |
3604 | } | |
3605 | ||
2ff7e61e | 3606 | ret = should_balance_chunk(fs_info, leaf, chunk, |
f43ffb60 | 3607 | found_key.offset); |
2c9fe835 | 3608 | |
b3b4aa74 | 3609 | btrfs_release_path(path); |
67c5e7d4 FM |
3610 | if (!ret) { |
3611 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
f43ffb60 | 3612 | goto loop; |
67c5e7d4 | 3613 | } |
f43ffb60 | 3614 | |
19a39dce | 3615 | if (counting) { |
67c5e7d4 | 3616 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
19a39dce ID |
3617 | spin_lock(&fs_info->balance_lock); |
3618 | bctl->stat.expected++; | |
3619 | spin_unlock(&fs_info->balance_lock); | |
12907fc7 DS |
3620 | |
3621 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) | |
3622 | count_data++; | |
3623 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) | |
3624 | count_sys++; | |
3625 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) | |
3626 | count_meta++; | |
3627 | ||
3628 | goto loop; | |
3629 | } | |
3630 | ||
3631 | /* | |
3632 | * Apply limit_min filter, no need to check if the LIMITS | |
3633 | * filter is used, limit_min is 0 by default | |
3634 | */ | |
3635 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && | |
3636 | count_data < bctl->data.limit_min) | |
3637 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && | |
3638 | count_meta < bctl->meta.limit_min) | |
3639 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && | |
3640 | count_sys < bctl->sys.limit_min)) { | |
3641 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
19a39dce ID |
3642 | goto loop; |
3643 | } | |
3644 | ||
cf25ce51 LB |
3645 | ASSERT(fs_info->data_sinfo); |
3646 | spin_lock(&fs_info->data_sinfo->lock); | |
3647 | bytes_used = fs_info->data_sinfo->bytes_used; | |
3648 | spin_unlock(&fs_info->data_sinfo->lock); | |
3649 | ||
3650 | if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) && | |
3651 | !chunk_reserved && !bytes_used) { | |
2c9fe835 ZL |
3652 | trans = btrfs_start_transaction(chunk_root, 0); |
3653 | if (IS_ERR(trans)) { | |
3654 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
3655 | ret = PTR_ERR(trans); | |
3656 | goto error; | |
3657 | } | |
3658 | ||
2ff7e61e | 3659 | ret = btrfs_force_chunk_alloc(trans, fs_info, |
2c9fe835 | 3660 | BTRFS_BLOCK_GROUP_DATA); |
3a45bb20 | 3661 | btrfs_end_transaction(trans); |
2c9fe835 ZL |
3662 | if (ret < 0) { |
3663 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
3664 | goto error; | |
3665 | } | |
2c9fe835 ZL |
3666 | chunk_reserved = 1; |
3667 | } | |
3668 | ||
5b4aacef | 3669 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
67c5e7d4 | 3670 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
508794eb JB |
3671 | if (ret && ret != -ENOSPC) |
3672 | goto error; | |
19a39dce | 3673 | if (ret == -ENOSPC) { |
c9e9f97b | 3674 | enospc_errors++; |
19a39dce ID |
3675 | } else { |
3676 | spin_lock(&fs_info->balance_lock); | |
3677 | bctl->stat.completed++; | |
3678 | spin_unlock(&fs_info->balance_lock); | |
3679 | } | |
f43ffb60 | 3680 | loop: |
795a3321 ID |
3681 | if (found_key.offset == 0) |
3682 | break; | |
ba1bf481 | 3683 | key.offset = found_key.offset - 1; |
ec44a35c | 3684 | } |
c9e9f97b | 3685 | |
19a39dce ID |
3686 | if (counting) { |
3687 | btrfs_release_path(path); | |
3688 | counting = false; | |
3689 | goto again; | |
3690 | } | |
ec44a35c CM |
3691 | error: |
3692 | btrfs_free_path(path); | |
c9e9f97b | 3693 | if (enospc_errors) { |
efe120a0 | 3694 | btrfs_info(fs_info, "%d enospc errors during balance", |
5d163e0e | 3695 | enospc_errors); |
c9e9f97b ID |
3696 | if (!ret) |
3697 | ret = -ENOSPC; | |
3698 | } | |
3699 | ||
ec44a35c CM |
3700 | return ret; |
3701 | } | |
3702 | ||
0c460c0d ID |
3703 | /** |
3704 | * alloc_profile_is_valid - see if a given profile is valid and reduced | |
3705 | * @flags: profile to validate | |
3706 | * @extended: if true @flags is treated as an extended profile | |
3707 | */ | |
3708 | static int alloc_profile_is_valid(u64 flags, int extended) | |
3709 | { | |
3710 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : | |
3711 | BTRFS_BLOCK_GROUP_PROFILE_MASK); | |
3712 | ||
3713 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; | |
3714 | ||
3715 | /* 1) check that all other bits are zeroed */ | |
3716 | if (flags & ~mask) | |
3717 | return 0; | |
3718 | ||
3719 | /* 2) see if profile is reduced */ | |
3720 | if (flags == 0) | |
3721 | return !extended; /* "0" is valid for usual profiles */ | |
3722 | ||
3723 | /* true if exactly one bit set */ | |
3724 | return (flags & (flags - 1)) == 0; | |
3725 | } | |
3726 | ||
837d5b6e ID |
3727 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
3728 | { | |
a7e99c69 ID |
3729 | /* cancel requested || normal exit path */ |
3730 | return atomic_read(&fs_info->balance_cancel_req) || | |
3731 | (atomic_read(&fs_info->balance_pause_req) == 0 && | |
3732 | atomic_read(&fs_info->balance_cancel_req) == 0); | |
837d5b6e ID |
3733 | } |
3734 | ||
c9e9f97b ID |
3735 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
3736 | { | |
0940ebf6 ID |
3737 | int ret; |
3738 | ||
c9e9f97b | 3739 | unset_balance_control(fs_info); |
6bccf3ab | 3740 | ret = del_balance_item(fs_info); |
0f788c58 | 3741 | if (ret) |
34d97007 | 3742 | btrfs_handle_fs_error(fs_info, ret, NULL); |
ed0fb78f | 3743 | |
171938e5 | 3744 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
c9e9f97b ID |
3745 | } |
3746 | ||
bdcd3c97 AM |
3747 | /* Non-zero return value signifies invalidity */ |
3748 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, | |
3749 | u64 allowed) | |
3750 | { | |
3751 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && | |
3752 | (!alloc_profile_is_valid(bctl_arg->target, 1) || | |
3753 | (bctl_arg->target & ~allowed))); | |
3754 | } | |
3755 | ||
c9e9f97b ID |
3756 | /* |
3757 | * Should be called with both balance and volume mutexes held | |
3758 | */ | |
3759 | int btrfs_balance(struct btrfs_balance_control *bctl, | |
3760 | struct btrfs_ioctl_balance_args *bargs) | |
3761 | { | |
3762 | struct btrfs_fs_info *fs_info = bctl->fs_info; | |
14506127 | 3763 | u64 meta_target, data_target; |
f43ffb60 | 3764 | u64 allowed; |
e4837f8f | 3765 | int mixed = 0; |
c9e9f97b | 3766 | int ret; |
8dabb742 | 3767 | u64 num_devices; |
de98ced9 | 3768 | unsigned seq; |
f23a8dcc | 3769 | bool reducing_integrity; |
c9e9f97b | 3770 | |
837d5b6e | 3771 | if (btrfs_fs_closing(fs_info) || |
a7e99c69 ID |
3772 | atomic_read(&fs_info->balance_pause_req) || |
3773 | atomic_read(&fs_info->balance_cancel_req)) { | |
c9e9f97b ID |
3774 | ret = -EINVAL; |
3775 | goto out; | |
3776 | } | |
3777 | ||
e4837f8f ID |
3778 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
3779 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) | |
3780 | mixed = 1; | |
3781 | ||
f43ffb60 ID |
3782 | /* |
3783 | * In case of mixed groups both data and meta should be picked, | |
3784 | * and identical options should be given for both of them. | |
3785 | */ | |
e4837f8f ID |
3786 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
3787 | if (mixed && (bctl->flags & allowed)) { | |
f43ffb60 ID |
3788 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
3789 | !(bctl->flags & BTRFS_BALANCE_METADATA) || | |
3790 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { | |
5d163e0e JM |
3791 | btrfs_err(fs_info, |
3792 | "with mixed groups data and metadata balance options must be the same"); | |
f43ffb60 ID |
3793 | ret = -EINVAL; |
3794 | goto out; | |
3795 | } | |
3796 | } | |
3797 | ||
8dabb742 | 3798 | num_devices = fs_info->fs_devices->num_devices; |
73beece9 | 3799 | btrfs_dev_replace_lock(&fs_info->dev_replace, 0); |
8dabb742 SB |
3800 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
3801 | BUG_ON(num_devices < 1); | |
3802 | num_devices--; | |
3803 | } | |
73beece9 | 3804 | btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); |
88be159c AH |
3805 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP; |
3806 | if (num_devices > 1) | |
e4d8ec0f | 3807 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
8250dabe AP |
3808 | if (num_devices > 2) |
3809 | allowed |= BTRFS_BLOCK_GROUP_RAID5; | |
3810 | if (num_devices > 3) | |
3811 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | | |
3812 | BTRFS_BLOCK_GROUP_RAID6); | |
bdcd3c97 | 3813 | if (validate_convert_profile(&bctl->data, allowed)) { |
5d163e0e JM |
3814 | btrfs_err(fs_info, |
3815 | "unable to start balance with target data profile %llu", | |
3816 | bctl->data.target); | |
e4d8ec0f ID |
3817 | ret = -EINVAL; |
3818 | goto out; | |
3819 | } | |
bdcd3c97 | 3820 | if (validate_convert_profile(&bctl->meta, allowed)) { |
efe120a0 | 3821 | btrfs_err(fs_info, |
5d163e0e JM |
3822 | "unable to start balance with target metadata profile %llu", |
3823 | bctl->meta.target); | |
e4d8ec0f ID |
3824 | ret = -EINVAL; |
3825 | goto out; | |
3826 | } | |
bdcd3c97 | 3827 | if (validate_convert_profile(&bctl->sys, allowed)) { |
efe120a0 | 3828 | btrfs_err(fs_info, |
5d163e0e JM |
3829 | "unable to start balance with target system profile %llu", |
3830 | bctl->sys.target); | |
e4d8ec0f ID |
3831 | ret = -EINVAL; |
3832 | goto out; | |
3833 | } | |
3834 | ||
e4d8ec0f ID |
3835 | /* allow to reduce meta or sys integrity only if force set */ |
3836 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | | |
53b381b3 DW |
3837 | BTRFS_BLOCK_GROUP_RAID10 | |
3838 | BTRFS_BLOCK_GROUP_RAID5 | | |
3839 | BTRFS_BLOCK_GROUP_RAID6; | |
de98ced9 MX |
3840 | do { |
3841 | seq = read_seqbegin(&fs_info->profiles_lock); | |
3842 | ||
3843 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && | |
3844 | (fs_info->avail_system_alloc_bits & allowed) && | |
3845 | !(bctl->sys.target & allowed)) || | |
3846 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && | |
3847 | (fs_info->avail_metadata_alloc_bits & allowed) && | |
f23a8dcc FM |
3848 | !(bctl->meta.target & allowed))) |
3849 | reducing_integrity = true; | |
3850 | else | |
3851 | reducing_integrity = false; | |
3852 | ||
3853 | /* if we're not converting, the target field is uninitialized */ | |
3854 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? | |
3855 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; | |
3856 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? | |
3857 | bctl->data.target : fs_info->avail_data_alloc_bits; | |
de98ced9 | 3858 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
e4d8ec0f | 3859 | |
f23a8dcc FM |
3860 | if (reducing_integrity) { |
3861 | if (bctl->flags & BTRFS_BALANCE_FORCE) { | |
3862 | btrfs_info(fs_info, | |
3863 | "balance: force reducing metadata integrity"); | |
3864 | } else { | |
3865 | btrfs_err(fs_info, | |
3866 | "balance: reduces metadata integrity, use --force if you want this"); | |
3867 | ret = -EINVAL; | |
3868 | goto out; | |
3869 | } | |
3870 | } | |
3871 | ||
14506127 AB |
3872 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
3873 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { | |
ee592d07 | 3874 | btrfs_warn(fs_info, |
5d163e0e | 3875 | "metadata profile 0x%llx has lower redundancy than data profile 0x%llx", |
14506127 | 3876 | meta_target, data_target); |
ee592d07 ST |
3877 | } |
3878 | ||
6bccf3ab | 3879 | ret = insert_balance_item(fs_info, bctl); |
59641015 | 3880 | if (ret && ret != -EEXIST) |
0940ebf6 ID |
3881 | goto out; |
3882 | ||
59641015 ID |
3883 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
3884 | BUG_ON(ret == -EEXIST); | |
3885 | set_balance_control(bctl); | |
3886 | } else { | |
3887 | BUG_ON(ret != -EEXIST); | |
3888 | spin_lock(&fs_info->balance_lock); | |
3889 | update_balance_args(bctl); | |
3890 | spin_unlock(&fs_info->balance_lock); | |
3891 | } | |
c9e9f97b | 3892 | |
837d5b6e | 3893 | atomic_inc(&fs_info->balance_running); |
c9e9f97b ID |
3894 | mutex_unlock(&fs_info->balance_mutex); |
3895 | ||
3896 | ret = __btrfs_balance(fs_info); | |
3897 | ||
3898 | mutex_lock(&fs_info->balance_mutex); | |
837d5b6e | 3899 | atomic_dec(&fs_info->balance_running); |
c9e9f97b ID |
3900 | |
3901 | if (bargs) { | |
3902 | memset(bargs, 0, sizeof(*bargs)); | |
19a39dce | 3903 | update_ioctl_balance_args(fs_info, 0, bargs); |
c9e9f97b ID |
3904 | } |
3905 | ||
3a01aa7a ID |
3906 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
3907 | balance_need_close(fs_info)) { | |
3908 | __cancel_balance(fs_info); | |
3909 | } | |
3910 | ||
837d5b6e | 3911 | wake_up(&fs_info->balance_wait_q); |
c9e9f97b ID |
3912 | |
3913 | return ret; | |
3914 | out: | |
59641015 ID |
3915 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
3916 | __cancel_balance(fs_info); | |
ed0fb78f | 3917 | else { |
59641015 | 3918 | kfree(bctl); |
171938e5 | 3919 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
ed0fb78f | 3920 | } |
59641015 ID |
3921 | return ret; |
3922 | } | |
3923 | ||
3924 | static int balance_kthread(void *data) | |
3925 | { | |
2b6ba629 | 3926 | struct btrfs_fs_info *fs_info = data; |
9555c6c1 | 3927 | int ret = 0; |
59641015 ID |
3928 | |
3929 | mutex_lock(&fs_info->volume_mutex); | |
3930 | mutex_lock(&fs_info->balance_mutex); | |
3931 | ||
2b6ba629 | 3932 | if (fs_info->balance_ctl) { |
efe120a0 | 3933 | btrfs_info(fs_info, "continuing balance"); |
2b6ba629 | 3934 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
9555c6c1 | 3935 | } |
59641015 ID |
3936 | |
3937 | mutex_unlock(&fs_info->balance_mutex); | |
3938 | mutex_unlock(&fs_info->volume_mutex); | |
2b6ba629 | 3939 | |
59641015 ID |
3940 | return ret; |
3941 | } | |
3942 | ||
2b6ba629 ID |
3943 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
3944 | { | |
3945 | struct task_struct *tsk; | |
3946 | ||
3947 | spin_lock(&fs_info->balance_lock); | |
3948 | if (!fs_info->balance_ctl) { | |
3949 | spin_unlock(&fs_info->balance_lock); | |
3950 | return 0; | |
3951 | } | |
3952 | spin_unlock(&fs_info->balance_lock); | |
3953 | ||
3cdde224 | 3954 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
efe120a0 | 3955 | btrfs_info(fs_info, "force skipping balance"); |
2b6ba629 ID |
3956 | return 0; |
3957 | } | |
3958 | ||
1556b8d3 AJ |
3959 | /* |
3960 | * A ro->rw remount sequence should continue with the paused balance | |
3961 | * regardless of who pauses it, system or the user as of now, so set | |
3962 | * the resume flag. | |
3963 | */ | |
3964 | spin_lock(&fs_info->balance_lock); | |
3965 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; | |
3966 | spin_unlock(&fs_info->balance_lock); | |
3967 | ||
2b6ba629 | 3968 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
cd633972 | 3969 | return PTR_ERR_OR_ZERO(tsk); |
2b6ba629 ID |
3970 | } |
3971 | ||
68310a5e | 3972 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
59641015 | 3973 | { |
59641015 ID |
3974 | struct btrfs_balance_control *bctl; |
3975 | struct btrfs_balance_item *item; | |
3976 | struct btrfs_disk_balance_args disk_bargs; | |
3977 | struct btrfs_path *path; | |
3978 | struct extent_buffer *leaf; | |
3979 | struct btrfs_key key; | |
3980 | int ret; | |
3981 | ||
3982 | path = btrfs_alloc_path(); | |
3983 | if (!path) | |
3984 | return -ENOMEM; | |
3985 | ||
59641015 | 3986 | key.objectid = BTRFS_BALANCE_OBJECTID; |
c479cb4f | 3987 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
59641015 ID |
3988 | key.offset = 0; |
3989 | ||
68310a5e | 3990 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
59641015 | 3991 | if (ret < 0) |
68310a5e | 3992 | goto out; |
59641015 ID |
3993 | if (ret > 0) { /* ret = -ENOENT; */ |
3994 | ret = 0; | |
68310a5e ID |
3995 | goto out; |
3996 | } | |
3997 | ||
3998 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); | |
3999 | if (!bctl) { | |
4000 | ret = -ENOMEM; | |
4001 | goto out; | |
59641015 ID |
4002 | } |
4003 | ||
4004 | leaf = path->nodes[0]; | |
4005 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); | |
4006 | ||
68310a5e ID |
4007 | bctl->fs_info = fs_info; |
4008 | bctl->flags = btrfs_balance_flags(leaf, item); | |
4009 | bctl->flags |= BTRFS_BALANCE_RESUME; | |
59641015 ID |
4010 | |
4011 | btrfs_balance_data(leaf, item, &disk_bargs); | |
4012 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); | |
4013 | btrfs_balance_meta(leaf, item, &disk_bargs); | |
4014 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); | |
4015 | btrfs_balance_sys(leaf, item, &disk_bargs); | |
4016 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); | |
4017 | ||
171938e5 | 4018 | WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); |
ed0fb78f | 4019 | |
68310a5e ID |
4020 | mutex_lock(&fs_info->volume_mutex); |
4021 | mutex_lock(&fs_info->balance_mutex); | |
59641015 | 4022 | |
68310a5e ID |
4023 | set_balance_control(bctl); |
4024 | ||
4025 | mutex_unlock(&fs_info->balance_mutex); | |
4026 | mutex_unlock(&fs_info->volume_mutex); | |
59641015 ID |
4027 | out: |
4028 | btrfs_free_path(path); | |
ec44a35c CM |
4029 | return ret; |
4030 | } | |
4031 | ||
837d5b6e ID |
4032 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
4033 | { | |
4034 | int ret = 0; | |
4035 | ||
4036 | mutex_lock(&fs_info->balance_mutex); | |
4037 | if (!fs_info->balance_ctl) { | |
4038 | mutex_unlock(&fs_info->balance_mutex); | |
4039 | return -ENOTCONN; | |
4040 | } | |
4041 | ||
4042 | if (atomic_read(&fs_info->balance_running)) { | |
4043 | atomic_inc(&fs_info->balance_pause_req); | |
4044 | mutex_unlock(&fs_info->balance_mutex); | |
4045 | ||
4046 | wait_event(fs_info->balance_wait_q, | |
4047 | atomic_read(&fs_info->balance_running) == 0); | |
4048 | ||
4049 | mutex_lock(&fs_info->balance_mutex); | |
4050 | /* we are good with balance_ctl ripped off from under us */ | |
4051 | BUG_ON(atomic_read(&fs_info->balance_running)); | |
4052 | atomic_dec(&fs_info->balance_pause_req); | |
4053 | } else { | |
4054 | ret = -ENOTCONN; | |
4055 | } | |
4056 | ||
4057 | mutex_unlock(&fs_info->balance_mutex); | |
4058 | return ret; | |
4059 | } | |
4060 | ||
a7e99c69 ID |
4061 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
4062 | { | |
bc98a42c | 4063 | if (sb_rdonly(fs_info->sb)) |
e649e587 ID |
4064 | return -EROFS; |
4065 | ||
a7e99c69 ID |
4066 | mutex_lock(&fs_info->balance_mutex); |
4067 | if (!fs_info->balance_ctl) { | |
4068 | mutex_unlock(&fs_info->balance_mutex); | |
4069 | return -ENOTCONN; | |
4070 | } | |
4071 | ||
4072 | atomic_inc(&fs_info->balance_cancel_req); | |
4073 | /* | |
4074 | * if we are running just wait and return, balance item is | |
4075 | * deleted in btrfs_balance in this case | |
4076 | */ | |
4077 | if (atomic_read(&fs_info->balance_running)) { | |
4078 | mutex_unlock(&fs_info->balance_mutex); | |
4079 | wait_event(fs_info->balance_wait_q, | |
4080 | atomic_read(&fs_info->balance_running) == 0); | |
4081 | mutex_lock(&fs_info->balance_mutex); | |
4082 | } else { | |
4083 | /* __cancel_balance needs volume_mutex */ | |
4084 | mutex_unlock(&fs_info->balance_mutex); | |
4085 | mutex_lock(&fs_info->volume_mutex); | |
4086 | mutex_lock(&fs_info->balance_mutex); | |
4087 | ||
4088 | if (fs_info->balance_ctl) | |
4089 | __cancel_balance(fs_info); | |
4090 | ||
4091 | mutex_unlock(&fs_info->volume_mutex); | |
4092 | } | |
4093 | ||
4094 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); | |
4095 | atomic_dec(&fs_info->balance_cancel_req); | |
4096 | mutex_unlock(&fs_info->balance_mutex); | |
4097 | return 0; | |
4098 | } | |
4099 | ||
803b2f54 SB |
4100 | static int btrfs_uuid_scan_kthread(void *data) |
4101 | { | |
4102 | struct btrfs_fs_info *fs_info = data; | |
4103 | struct btrfs_root *root = fs_info->tree_root; | |
4104 | struct btrfs_key key; | |
803b2f54 SB |
4105 | struct btrfs_path *path = NULL; |
4106 | int ret = 0; | |
4107 | struct extent_buffer *eb; | |
4108 | int slot; | |
4109 | struct btrfs_root_item root_item; | |
4110 | u32 item_size; | |
f45388f3 | 4111 | struct btrfs_trans_handle *trans = NULL; |
803b2f54 SB |
4112 | |
4113 | path = btrfs_alloc_path(); | |
4114 | if (!path) { | |
4115 | ret = -ENOMEM; | |
4116 | goto out; | |
4117 | } | |
4118 | ||
4119 | key.objectid = 0; | |
4120 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4121 | key.offset = 0; | |
4122 | ||
803b2f54 | 4123 | while (1) { |
6174d3cb | 4124 | ret = btrfs_search_forward(root, &key, path, 0); |
803b2f54 SB |
4125 | if (ret) { |
4126 | if (ret > 0) | |
4127 | ret = 0; | |
4128 | break; | |
4129 | } | |
4130 | ||
4131 | if (key.type != BTRFS_ROOT_ITEM_KEY || | |
4132 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && | |
4133 | key.objectid != BTRFS_FS_TREE_OBJECTID) || | |
4134 | key.objectid > BTRFS_LAST_FREE_OBJECTID) | |
4135 | goto skip; | |
4136 | ||
4137 | eb = path->nodes[0]; | |
4138 | slot = path->slots[0]; | |
4139 | item_size = btrfs_item_size_nr(eb, slot); | |
4140 | if (item_size < sizeof(root_item)) | |
4141 | goto skip; | |
4142 | ||
803b2f54 SB |
4143 | read_extent_buffer(eb, &root_item, |
4144 | btrfs_item_ptr_offset(eb, slot), | |
4145 | (int)sizeof(root_item)); | |
4146 | if (btrfs_root_refs(&root_item) == 0) | |
4147 | goto skip; | |
f45388f3 FDBM |
4148 | |
4149 | if (!btrfs_is_empty_uuid(root_item.uuid) || | |
4150 | !btrfs_is_empty_uuid(root_item.received_uuid)) { | |
4151 | if (trans) | |
4152 | goto update_tree; | |
4153 | ||
4154 | btrfs_release_path(path); | |
803b2f54 SB |
4155 | /* |
4156 | * 1 - subvol uuid item | |
4157 | * 1 - received_subvol uuid item | |
4158 | */ | |
4159 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); | |
4160 | if (IS_ERR(trans)) { | |
4161 | ret = PTR_ERR(trans); | |
4162 | break; | |
4163 | } | |
f45388f3 FDBM |
4164 | continue; |
4165 | } else { | |
4166 | goto skip; | |
4167 | } | |
4168 | update_tree: | |
4169 | if (!btrfs_is_empty_uuid(root_item.uuid)) { | |
6bccf3ab | 4170 | ret = btrfs_uuid_tree_add(trans, fs_info, |
803b2f54 SB |
4171 | root_item.uuid, |
4172 | BTRFS_UUID_KEY_SUBVOL, | |
4173 | key.objectid); | |
4174 | if (ret < 0) { | |
efe120a0 | 4175 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
803b2f54 | 4176 | ret); |
803b2f54 SB |
4177 | break; |
4178 | } | |
4179 | } | |
4180 | ||
4181 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { | |
6bccf3ab | 4182 | ret = btrfs_uuid_tree_add(trans, fs_info, |
803b2f54 SB |
4183 | root_item.received_uuid, |
4184 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, | |
4185 | key.objectid); | |
4186 | if (ret < 0) { | |
efe120a0 | 4187 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
803b2f54 | 4188 | ret); |
803b2f54 SB |
4189 | break; |
4190 | } | |
4191 | } | |
4192 | ||
f45388f3 | 4193 | skip: |
803b2f54 | 4194 | if (trans) { |
3a45bb20 | 4195 | ret = btrfs_end_transaction(trans); |
f45388f3 | 4196 | trans = NULL; |
803b2f54 SB |
4197 | if (ret) |
4198 | break; | |
4199 | } | |
4200 | ||
803b2f54 SB |
4201 | btrfs_release_path(path); |
4202 | if (key.offset < (u64)-1) { | |
4203 | key.offset++; | |
4204 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { | |
4205 | key.offset = 0; | |
4206 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4207 | } else if (key.objectid < (u64)-1) { | |
4208 | key.offset = 0; | |
4209 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4210 | key.objectid++; | |
4211 | } else { | |
4212 | break; | |
4213 | } | |
4214 | cond_resched(); | |
4215 | } | |
4216 | ||
4217 | out: | |
4218 | btrfs_free_path(path); | |
f45388f3 | 4219 | if (trans && !IS_ERR(trans)) |
3a45bb20 | 4220 | btrfs_end_transaction(trans); |
803b2f54 | 4221 | if (ret) |
efe120a0 | 4222 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
70f80175 | 4223 | else |
afcdd129 | 4224 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
803b2f54 SB |
4225 | up(&fs_info->uuid_tree_rescan_sem); |
4226 | return 0; | |
4227 | } | |
4228 | ||
70f80175 SB |
4229 | /* |
4230 | * Callback for btrfs_uuid_tree_iterate(). | |
4231 | * returns: | |
4232 | * 0 check succeeded, the entry is not outdated. | |
bb7ab3b9 | 4233 | * < 0 if an error occurred. |
70f80175 SB |
4234 | * > 0 if the check failed, which means the caller shall remove the entry. |
4235 | */ | |
4236 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, | |
4237 | u8 *uuid, u8 type, u64 subid) | |
4238 | { | |
4239 | struct btrfs_key key; | |
4240 | int ret = 0; | |
4241 | struct btrfs_root *subvol_root; | |
4242 | ||
4243 | if (type != BTRFS_UUID_KEY_SUBVOL && | |
4244 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) | |
4245 | goto out; | |
4246 | ||
4247 | key.objectid = subid; | |
4248 | key.type = BTRFS_ROOT_ITEM_KEY; | |
4249 | key.offset = (u64)-1; | |
4250 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); | |
4251 | if (IS_ERR(subvol_root)) { | |
4252 | ret = PTR_ERR(subvol_root); | |
4253 | if (ret == -ENOENT) | |
4254 | ret = 1; | |
4255 | goto out; | |
4256 | } | |
4257 | ||
4258 | switch (type) { | |
4259 | case BTRFS_UUID_KEY_SUBVOL: | |
4260 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) | |
4261 | ret = 1; | |
4262 | break; | |
4263 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: | |
4264 | if (memcmp(uuid, subvol_root->root_item.received_uuid, | |
4265 | BTRFS_UUID_SIZE)) | |
4266 | ret = 1; | |
4267 | break; | |
4268 | } | |
4269 | ||
4270 | out: | |
4271 | return ret; | |
4272 | } | |
4273 | ||
4274 | static int btrfs_uuid_rescan_kthread(void *data) | |
4275 | { | |
4276 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; | |
4277 | int ret; | |
4278 | ||
4279 | /* | |
4280 | * 1st step is to iterate through the existing UUID tree and | |
4281 | * to delete all entries that contain outdated data. | |
4282 | * 2nd step is to add all missing entries to the UUID tree. | |
4283 | */ | |
4284 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); | |
4285 | if (ret < 0) { | |
efe120a0 | 4286 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
70f80175 SB |
4287 | up(&fs_info->uuid_tree_rescan_sem); |
4288 | return ret; | |
4289 | } | |
4290 | return btrfs_uuid_scan_kthread(data); | |
4291 | } | |
4292 | ||
f7a81ea4 SB |
4293 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
4294 | { | |
4295 | struct btrfs_trans_handle *trans; | |
4296 | struct btrfs_root *tree_root = fs_info->tree_root; | |
4297 | struct btrfs_root *uuid_root; | |
803b2f54 SB |
4298 | struct task_struct *task; |
4299 | int ret; | |
f7a81ea4 SB |
4300 | |
4301 | /* | |
4302 | * 1 - root node | |
4303 | * 1 - root item | |
4304 | */ | |
4305 | trans = btrfs_start_transaction(tree_root, 2); | |
4306 | if (IS_ERR(trans)) | |
4307 | return PTR_ERR(trans); | |
4308 | ||
4309 | uuid_root = btrfs_create_tree(trans, fs_info, | |
4310 | BTRFS_UUID_TREE_OBJECTID); | |
4311 | if (IS_ERR(uuid_root)) { | |
6d13f549 | 4312 | ret = PTR_ERR(uuid_root); |
66642832 | 4313 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4314 | btrfs_end_transaction(trans); |
6d13f549 | 4315 | return ret; |
f7a81ea4 SB |
4316 | } |
4317 | ||
4318 | fs_info->uuid_root = uuid_root; | |
4319 | ||
3a45bb20 | 4320 | ret = btrfs_commit_transaction(trans); |
803b2f54 SB |
4321 | if (ret) |
4322 | return ret; | |
4323 | ||
4324 | down(&fs_info->uuid_tree_rescan_sem); | |
4325 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); | |
4326 | if (IS_ERR(task)) { | |
70f80175 | 4327 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
efe120a0 | 4328 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
803b2f54 SB |
4329 | up(&fs_info->uuid_tree_rescan_sem); |
4330 | return PTR_ERR(task); | |
4331 | } | |
4332 | ||
4333 | return 0; | |
f7a81ea4 | 4334 | } |
803b2f54 | 4335 | |
70f80175 SB |
4336 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
4337 | { | |
4338 | struct task_struct *task; | |
4339 | ||
4340 | down(&fs_info->uuid_tree_rescan_sem); | |
4341 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); | |
4342 | if (IS_ERR(task)) { | |
4343 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ | |
efe120a0 | 4344 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
70f80175 SB |
4345 | up(&fs_info->uuid_tree_rescan_sem); |
4346 | return PTR_ERR(task); | |
4347 | } | |
4348 | ||
4349 | return 0; | |
4350 | } | |
4351 | ||
8f18cf13 CM |
4352 | /* |
4353 | * shrinking a device means finding all of the device extents past | |
4354 | * the new size, and then following the back refs to the chunks. | |
4355 | * The chunk relocation code actually frees the device extent | |
4356 | */ | |
4357 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) | |
4358 | { | |
0b246afa JM |
4359 | struct btrfs_fs_info *fs_info = device->fs_info; |
4360 | struct btrfs_root *root = fs_info->dev_root; | |
8f18cf13 | 4361 | struct btrfs_trans_handle *trans; |
8f18cf13 CM |
4362 | struct btrfs_dev_extent *dev_extent = NULL; |
4363 | struct btrfs_path *path; | |
4364 | u64 length; | |
8f18cf13 CM |
4365 | u64 chunk_offset; |
4366 | int ret; | |
4367 | int slot; | |
ba1bf481 JB |
4368 | int failed = 0; |
4369 | bool retried = false; | |
53e489bc | 4370 | bool checked_pending_chunks = false; |
8f18cf13 CM |
4371 | struct extent_buffer *l; |
4372 | struct btrfs_key key; | |
0b246afa | 4373 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
8f18cf13 | 4374 | u64 old_total = btrfs_super_total_bytes(super_copy); |
7cc8e58d | 4375 | u64 old_size = btrfs_device_get_total_bytes(device); |
7dfb8be1 NB |
4376 | u64 diff; |
4377 | ||
4378 | new_size = round_down(new_size, fs_info->sectorsize); | |
0e4324a4 | 4379 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
8f18cf13 | 4380 | |
63a212ab SB |
4381 | if (device->is_tgtdev_for_dev_replace) |
4382 | return -EINVAL; | |
4383 | ||
8f18cf13 CM |
4384 | path = btrfs_alloc_path(); |
4385 | if (!path) | |
4386 | return -ENOMEM; | |
4387 | ||
e4058b54 | 4388 | path->reada = READA_FORWARD; |
8f18cf13 | 4389 | |
34441361 | 4390 | mutex_lock(&fs_info->chunk_mutex); |
7d9eb12c | 4391 | |
7cc8e58d | 4392 | btrfs_device_set_total_bytes(device, new_size); |
2bf64758 | 4393 | if (device->writeable) { |
2b82032c | 4394 | device->fs_devices->total_rw_bytes -= diff; |
a5ed45f8 | 4395 | atomic64_sub(diff, &fs_info->free_chunk_space); |
2bf64758 | 4396 | } |
34441361 | 4397 | mutex_unlock(&fs_info->chunk_mutex); |
8f18cf13 | 4398 | |
ba1bf481 | 4399 | again: |
8f18cf13 CM |
4400 | key.objectid = device->devid; |
4401 | key.offset = (u64)-1; | |
4402 | key.type = BTRFS_DEV_EXTENT_KEY; | |
4403 | ||
213e64da | 4404 | do { |
0b246afa | 4405 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
8f18cf13 | 4406 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
67c5e7d4 | 4407 | if (ret < 0) { |
0b246afa | 4408 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
8f18cf13 | 4409 | goto done; |
67c5e7d4 | 4410 | } |
8f18cf13 CM |
4411 | |
4412 | ret = btrfs_previous_item(root, path, 0, key.type); | |
67c5e7d4 | 4413 | if (ret) |
0b246afa | 4414 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
8f18cf13 CM |
4415 | if (ret < 0) |
4416 | goto done; | |
4417 | if (ret) { | |
4418 | ret = 0; | |
b3b4aa74 | 4419 | btrfs_release_path(path); |
bf1fb512 | 4420 | break; |
8f18cf13 CM |
4421 | } |
4422 | ||
4423 | l = path->nodes[0]; | |
4424 | slot = path->slots[0]; | |
4425 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
4426 | ||
ba1bf481 | 4427 | if (key.objectid != device->devid) { |
0b246afa | 4428 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
b3b4aa74 | 4429 | btrfs_release_path(path); |
bf1fb512 | 4430 | break; |
ba1bf481 | 4431 | } |
8f18cf13 CM |
4432 | |
4433 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
4434 | length = btrfs_dev_extent_length(l, dev_extent); | |
4435 | ||
ba1bf481 | 4436 | if (key.offset + length <= new_size) { |
0b246afa | 4437 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
b3b4aa74 | 4438 | btrfs_release_path(path); |
d6397bae | 4439 | break; |
ba1bf481 | 4440 | } |
8f18cf13 | 4441 | |
8f18cf13 | 4442 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
b3b4aa74 | 4443 | btrfs_release_path(path); |
8f18cf13 | 4444 | |
0b246afa JM |
4445 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
4446 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
ba1bf481 | 4447 | if (ret && ret != -ENOSPC) |
8f18cf13 | 4448 | goto done; |
ba1bf481 JB |
4449 | if (ret == -ENOSPC) |
4450 | failed++; | |
213e64da | 4451 | } while (key.offset-- > 0); |
ba1bf481 JB |
4452 | |
4453 | if (failed && !retried) { | |
4454 | failed = 0; | |
4455 | retried = true; | |
4456 | goto again; | |
4457 | } else if (failed && retried) { | |
4458 | ret = -ENOSPC; | |
ba1bf481 | 4459 | goto done; |
8f18cf13 CM |
4460 | } |
4461 | ||
d6397bae | 4462 | /* Shrinking succeeded, else we would be at "done". */ |
a22285a6 | 4463 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
4464 | if (IS_ERR(trans)) { |
4465 | ret = PTR_ERR(trans); | |
4466 | goto done; | |
4467 | } | |
4468 | ||
34441361 | 4469 | mutex_lock(&fs_info->chunk_mutex); |
53e489bc FM |
4470 | |
4471 | /* | |
4472 | * We checked in the above loop all device extents that were already in | |
4473 | * the device tree. However before we have updated the device's | |
4474 | * total_bytes to the new size, we might have had chunk allocations that | |
4475 | * have not complete yet (new block groups attached to transaction | |
4476 | * handles), and therefore their device extents were not yet in the | |
4477 | * device tree and we missed them in the loop above. So if we have any | |
4478 | * pending chunk using a device extent that overlaps the device range | |
4479 | * that we can not use anymore, commit the current transaction and | |
4480 | * repeat the search on the device tree - this way we guarantee we will | |
4481 | * not have chunks using device extents that end beyond 'new_size'. | |
4482 | */ | |
4483 | if (!checked_pending_chunks) { | |
4484 | u64 start = new_size; | |
4485 | u64 len = old_size - new_size; | |
4486 | ||
499f377f JM |
4487 | if (contains_pending_extent(trans->transaction, device, |
4488 | &start, len)) { | |
34441361 | 4489 | mutex_unlock(&fs_info->chunk_mutex); |
53e489bc FM |
4490 | checked_pending_chunks = true; |
4491 | failed = 0; | |
4492 | retried = false; | |
3a45bb20 | 4493 | ret = btrfs_commit_transaction(trans); |
53e489bc FM |
4494 | if (ret) |
4495 | goto done; | |
4496 | goto again; | |
4497 | } | |
4498 | } | |
4499 | ||
7cc8e58d | 4500 | btrfs_device_set_disk_total_bytes(device, new_size); |
935e5cc9 MX |
4501 | if (list_empty(&device->resized_list)) |
4502 | list_add_tail(&device->resized_list, | |
0b246afa | 4503 | &fs_info->fs_devices->resized_devices); |
d6397bae | 4504 | |
d6397bae | 4505 | WARN_ON(diff > old_total); |
7dfb8be1 NB |
4506 | btrfs_set_super_total_bytes(super_copy, |
4507 | round_down(old_total - diff, fs_info->sectorsize)); | |
34441361 | 4508 | mutex_unlock(&fs_info->chunk_mutex); |
2196d6e8 MX |
4509 | |
4510 | /* Now btrfs_update_device() will change the on-disk size. */ | |
4511 | ret = btrfs_update_device(trans, device); | |
ce46c90b AJ |
4512 | if (ret < 0) { |
4513 | btrfs_abort_transaction(trans, ret); | |
4514 | btrfs_end_transaction(trans); | |
4515 | } else { | |
4516 | ret = btrfs_commit_transaction(trans); | |
4517 | } | |
8f18cf13 CM |
4518 | done: |
4519 | btrfs_free_path(path); | |
53e489bc | 4520 | if (ret) { |
34441361 | 4521 | mutex_lock(&fs_info->chunk_mutex); |
53e489bc FM |
4522 | btrfs_device_set_total_bytes(device, old_size); |
4523 | if (device->writeable) | |
4524 | device->fs_devices->total_rw_bytes += diff; | |
a5ed45f8 | 4525 | atomic64_add(diff, &fs_info->free_chunk_space); |
34441361 | 4526 | mutex_unlock(&fs_info->chunk_mutex); |
53e489bc | 4527 | } |
8f18cf13 CM |
4528 | return ret; |
4529 | } | |
4530 | ||
2ff7e61e | 4531 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
0b86a832 CM |
4532 | struct btrfs_key *key, |
4533 | struct btrfs_chunk *chunk, int item_size) | |
4534 | { | |
0b246afa | 4535 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
0b86a832 CM |
4536 | struct btrfs_disk_key disk_key; |
4537 | u32 array_size; | |
4538 | u8 *ptr; | |
4539 | ||
34441361 | 4540 | mutex_lock(&fs_info->chunk_mutex); |
0b86a832 | 4541 | array_size = btrfs_super_sys_array_size(super_copy); |
5f43f86e | 4542 | if (array_size + item_size + sizeof(disk_key) |
fe48a5c0 | 4543 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
34441361 | 4544 | mutex_unlock(&fs_info->chunk_mutex); |
0b86a832 | 4545 | return -EFBIG; |
fe48a5c0 | 4546 | } |
0b86a832 CM |
4547 | |
4548 | ptr = super_copy->sys_chunk_array + array_size; | |
4549 | btrfs_cpu_key_to_disk(&disk_key, key); | |
4550 | memcpy(ptr, &disk_key, sizeof(disk_key)); | |
4551 | ptr += sizeof(disk_key); | |
4552 | memcpy(ptr, chunk, item_size); | |
4553 | item_size += sizeof(disk_key); | |
4554 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); | |
34441361 | 4555 | mutex_unlock(&fs_info->chunk_mutex); |
fe48a5c0 | 4556 | |
0b86a832 CM |
4557 | return 0; |
4558 | } | |
4559 | ||
73c5de00 AJ |
4560 | /* |
4561 | * sort the devices in descending order by max_avail, total_avail | |
4562 | */ | |
4563 | static int btrfs_cmp_device_info(const void *a, const void *b) | |
9b3f68b9 | 4564 | { |
73c5de00 AJ |
4565 | const struct btrfs_device_info *di_a = a; |
4566 | const struct btrfs_device_info *di_b = b; | |
9b3f68b9 | 4567 | |
73c5de00 | 4568 | if (di_a->max_avail > di_b->max_avail) |
b2117a39 | 4569 | return -1; |
73c5de00 | 4570 | if (di_a->max_avail < di_b->max_avail) |
b2117a39 | 4571 | return 1; |
73c5de00 AJ |
4572 | if (di_a->total_avail > di_b->total_avail) |
4573 | return -1; | |
4574 | if (di_a->total_avail < di_b->total_avail) | |
4575 | return 1; | |
4576 | return 0; | |
b2117a39 | 4577 | } |
0b86a832 | 4578 | |
53b381b3 DW |
4579 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
4580 | { | |
ffe2d203 | 4581 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
53b381b3 DW |
4582 | return; |
4583 | ||
ceda0864 | 4584 | btrfs_set_fs_incompat(info, RAID56); |
53b381b3 DW |
4585 | } |
4586 | ||
da17066c | 4587 | #define BTRFS_MAX_DEVS(r) ((BTRFS_MAX_ITEM_SIZE(r->fs_info) \ |
23f8f9b7 GH |
4588 | - sizeof(struct btrfs_chunk)) \ |
4589 | / sizeof(struct btrfs_stripe) + 1) | |
4590 | ||
4591 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ | |
4592 | - 2 * sizeof(struct btrfs_disk_key) \ | |
4593 | - 2 * sizeof(struct btrfs_chunk)) \ | |
4594 | / sizeof(struct btrfs_stripe) + 1) | |
4595 | ||
73c5de00 | 4596 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
72b468c8 | 4597 | u64 start, u64 type) |
b2117a39 | 4598 | { |
2ff7e61e | 4599 | struct btrfs_fs_info *info = trans->fs_info; |
73c5de00 | 4600 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
ebcc9301 | 4601 | struct btrfs_device *device; |
73c5de00 AJ |
4602 | struct map_lookup *map = NULL; |
4603 | struct extent_map_tree *em_tree; | |
4604 | struct extent_map *em; | |
4605 | struct btrfs_device_info *devices_info = NULL; | |
4606 | u64 total_avail; | |
4607 | int num_stripes; /* total number of stripes to allocate */ | |
53b381b3 DW |
4608 | int data_stripes; /* number of stripes that count for |
4609 | block group size */ | |
73c5de00 AJ |
4610 | int sub_stripes; /* sub_stripes info for map */ |
4611 | int dev_stripes; /* stripes per dev */ | |
4612 | int devs_max; /* max devs to use */ | |
4613 | int devs_min; /* min devs needed */ | |
4614 | int devs_increment; /* ndevs has to be a multiple of this */ | |
4615 | int ncopies; /* how many copies to data has */ | |
4616 | int ret; | |
4617 | u64 max_stripe_size; | |
4618 | u64 max_chunk_size; | |
4619 | u64 stripe_size; | |
4620 | u64 num_bytes; | |
4621 | int ndevs; | |
4622 | int i; | |
4623 | int j; | |
31e50229 | 4624 | int index; |
593060d7 | 4625 | |
0c460c0d | 4626 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
9b3f68b9 | 4627 | |
73c5de00 AJ |
4628 | if (list_empty(&fs_devices->alloc_list)) |
4629 | return -ENOSPC; | |
b2117a39 | 4630 | |
31e50229 | 4631 | index = __get_raid_index(type); |
73c5de00 | 4632 | |
31e50229 LB |
4633 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
4634 | dev_stripes = btrfs_raid_array[index].dev_stripes; | |
4635 | devs_max = btrfs_raid_array[index].devs_max; | |
4636 | devs_min = btrfs_raid_array[index].devs_min; | |
4637 | devs_increment = btrfs_raid_array[index].devs_increment; | |
4638 | ncopies = btrfs_raid_array[index].ncopies; | |
b2117a39 | 4639 | |
9b3f68b9 | 4640 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
ee22184b | 4641 | max_stripe_size = SZ_1G; |
404f17e9 | 4642 | max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
23f8f9b7 GH |
4643 | if (!devs_max) |
4644 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); | |
9b3f68b9 | 4645 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
1100373f | 4646 | /* for larger filesystems, use larger metadata chunks */ |
ee22184b BL |
4647 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
4648 | max_stripe_size = SZ_1G; | |
1100373f | 4649 | else |
ee22184b | 4650 | max_stripe_size = SZ_256M; |
73c5de00 | 4651 | max_chunk_size = max_stripe_size; |
23f8f9b7 GH |
4652 | if (!devs_max) |
4653 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); | |
a40a90a0 | 4654 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
ee22184b | 4655 | max_stripe_size = SZ_32M; |
73c5de00 | 4656 | max_chunk_size = 2 * max_stripe_size; |
23f8f9b7 GH |
4657 | if (!devs_max) |
4658 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; | |
73c5de00 | 4659 | } else { |
351fd353 | 4660 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
73c5de00 AJ |
4661 | type); |
4662 | BUG_ON(1); | |
9b3f68b9 CM |
4663 | } |
4664 | ||
2b82032c YZ |
4665 | /* we don't want a chunk larger than 10% of writeable space */ |
4666 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), | |
4667 | max_chunk_size); | |
9b3f68b9 | 4668 | |
31e818fe | 4669 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
73c5de00 AJ |
4670 | GFP_NOFS); |
4671 | if (!devices_info) | |
4672 | return -ENOMEM; | |
0cad8a11 | 4673 | |
9f680ce0 | 4674 | /* |
73c5de00 AJ |
4675 | * in the first pass through the devices list, we gather information |
4676 | * about the available holes on each device. | |
9f680ce0 | 4677 | */ |
73c5de00 | 4678 | ndevs = 0; |
ebcc9301 | 4679 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
73c5de00 AJ |
4680 | u64 max_avail; |
4681 | u64 dev_offset; | |
b2117a39 | 4682 | |
73c5de00 | 4683 | if (!device->writeable) { |
31b1a2bd | 4684 | WARN(1, KERN_ERR |
efe120a0 | 4685 | "BTRFS: read-only device in alloc_list\n"); |
73c5de00 AJ |
4686 | continue; |
4687 | } | |
b2117a39 | 4688 | |
63a212ab SB |
4689 | if (!device->in_fs_metadata || |
4690 | device->is_tgtdev_for_dev_replace) | |
73c5de00 | 4691 | continue; |
b2117a39 | 4692 | |
73c5de00 AJ |
4693 | if (device->total_bytes > device->bytes_used) |
4694 | total_avail = device->total_bytes - device->bytes_used; | |
4695 | else | |
4696 | total_avail = 0; | |
38c01b96 | 4697 | |
4698 | /* If there is no space on this device, skip it. */ | |
4699 | if (total_avail == 0) | |
4700 | continue; | |
b2117a39 | 4701 | |
6df9a95e | 4702 | ret = find_free_dev_extent(trans, device, |
73c5de00 AJ |
4703 | max_stripe_size * dev_stripes, |
4704 | &dev_offset, &max_avail); | |
4705 | if (ret && ret != -ENOSPC) | |
4706 | goto error; | |
b2117a39 | 4707 | |
73c5de00 AJ |
4708 | if (ret == 0) |
4709 | max_avail = max_stripe_size * dev_stripes; | |
b2117a39 | 4710 | |
73c5de00 AJ |
4711 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
4712 | continue; | |
b2117a39 | 4713 | |
063d006f ES |
4714 | if (ndevs == fs_devices->rw_devices) { |
4715 | WARN(1, "%s: found more than %llu devices\n", | |
4716 | __func__, fs_devices->rw_devices); | |
4717 | break; | |
4718 | } | |
73c5de00 AJ |
4719 | devices_info[ndevs].dev_offset = dev_offset; |
4720 | devices_info[ndevs].max_avail = max_avail; | |
4721 | devices_info[ndevs].total_avail = total_avail; | |
4722 | devices_info[ndevs].dev = device; | |
4723 | ++ndevs; | |
4724 | } | |
b2117a39 | 4725 | |
73c5de00 AJ |
4726 | /* |
4727 | * now sort the devices by hole size / available space | |
4728 | */ | |
4729 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), | |
4730 | btrfs_cmp_device_info, NULL); | |
b2117a39 | 4731 | |
73c5de00 | 4732 | /* round down to number of usable stripes */ |
e5600fd6 | 4733 | ndevs = round_down(ndevs, devs_increment); |
b2117a39 | 4734 | |
73c5de00 AJ |
4735 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
4736 | ret = -ENOSPC; | |
4737 | goto error; | |
b2117a39 | 4738 | } |
9f680ce0 | 4739 | |
f148ef4d NB |
4740 | ndevs = min(ndevs, devs_max); |
4741 | ||
73c5de00 | 4742 | /* |
272b3be7 HK |
4743 | * The primary goal is to maximize the number of stripes, so use as |
4744 | * many devices as possible, even if the stripes are not maximum sized. | |
4745 | * | |
4746 | * The DUP profile stores more than one stripe per device, the | |
4747 | * max_avail is the total size so we have to adjust. | |
73c5de00 | 4748 | */ |
272b3be7 | 4749 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
73c5de00 | 4750 | num_stripes = ndevs * dev_stripes; |
b2117a39 | 4751 | |
53b381b3 DW |
4752 | /* |
4753 | * this will have to be fixed for RAID1 and RAID10 over | |
4754 | * more drives | |
4755 | */ | |
4756 | data_stripes = num_stripes / ncopies; | |
4757 | ||
500ceed8 | 4758 | if (type & BTRFS_BLOCK_GROUP_RAID5) |
53b381b3 | 4759 | data_stripes = num_stripes - 1; |
500ceed8 NB |
4760 | |
4761 | if (type & BTRFS_BLOCK_GROUP_RAID6) | |
53b381b3 | 4762 | data_stripes = num_stripes - 2; |
86db2578 CM |
4763 | |
4764 | /* | |
4765 | * Use the number of data stripes to figure out how big this chunk | |
4766 | * is really going to be in terms of logical address space, | |
4767 | * and compare that answer with the max chunk size | |
4768 | */ | |
4769 | if (stripe_size * data_stripes > max_chunk_size) { | |
4770 | u64 mask = (1ULL << 24) - 1; | |
b8b93add DS |
4771 | |
4772 | stripe_size = div_u64(max_chunk_size, data_stripes); | |
86db2578 CM |
4773 | |
4774 | /* bump the answer up to a 16MB boundary */ | |
4775 | stripe_size = (stripe_size + mask) & ~mask; | |
4776 | ||
4777 | /* but don't go higher than the limits we found | |
4778 | * while searching for free extents | |
4779 | */ | |
4780 | if (stripe_size > devices_info[ndevs-1].max_avail) | |
4781 | stripe_size = devices_info[ndevs-1].max_avail; | |
4782 | } | |
4783 | ||
37db63a4 | 4784 | /* align to BTRFS_STRIPE_LEN */ |
500ceed8 | 4785 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
b2117a39 MX |
4786 | |
4787 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); | |
4788 | if (!map) { | |
4789 | ret = -ENOMEM; | |
4790 | goto error; | |
4791 | } | |
4792 | map->num_stripes = num_stripes; | |
9b3f68b9 | 4793 | |
73c5de00 AJ |
4794 | for (i = 0; i < ndevs; ++i) { |
4795 | for (j = 0; j < dev_stripes; ++j) { | |
4796 | int s = i * dev_stripes + j; | |
4797 | map->stripes[s].dev = devices_info[i].dev; | |
4798 | map->stripes[s].physical = devices_info[i].dev_offset + | |
4799 | j * stripe_size; | |
6324fbf3 | 4800 | } |
6324fbf3 | 4801 | } |
500ceed8 NB |
4802 | map->stripe_len = BTRFS_STRIPE_LEN; |
4803 | map->io_align = BTRFS_STRIPE_LEN; | |
4804 | map->io_width = BTRFS_STRIPE_LEN; | |
2b82032c | 4805 | map->type = type; |
2b82032c | 4806 | map->sub_stripes = sub_stripes; |
0b86a832 | 4807 | |
53b381b3 | 4808 | num_bytes = stripe_size * data_stripes; |
0b86a832 | 4809 | |
6bccf3ab | 4810 | trace_btrfs_chunk_alloc(info, map, start, num_bytes); |
1abe9b8a | 4811 | |
172ddd60 | 4812 | em = alloc_extent_map(); |
2b82032c | 4813 | if (!em) { |
298a8f9c | 4814 | kfree(map); |
b2117a39 MX |
4815 | ret = -ENOMEM; |
4816 | goto error; | |
593060d7 | 4817 | } |
298a8f9c | 4818 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
95617d69 | 4819 | em->map_lookup = map; |
2b82032c | 4820 | em->start = start; |
73c5de00 | 4821 | em->len = num_bytes; |
2b82032c YZ |
4822 | em->block_start = 0; |
4823 | em->block_len = em->len; | |
6df9a95e | 4824 | em->orig_block_len = stripe_size; |
593060d7 | 4825 | |
0b246afa | 4826 | em_tree = &info->mapping_tree.map_tree; |
890871be | 4827 | write_lock(&em_tree->lock); |
09a2a8f9 | 4828 | ret = add_extent_mapping(em_tree, em, 0); |
0f5d42b2 | 4829 | if (ret) { |
1efb72a3 | 4830 | write_unlock(&em_tree->lock); |
0f5d42b2 | 4831 | free_extent_map(em); |
1dd4602f | 4832 | goto error; |
0f5d42b2 | 4833 | } |
0b86a832 | 4834 | |
1efb72a3 NB |
4835 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
4836 | refcount_inc(&em->refs); | |
4837 | write_unlock(&em_tree->lock); | |
4838 | ||
0174484d | 4839 | ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes); |
6df9a95e JB |
4840 | if (ret) |
4841 | goto error_del_extent; | |
2b82032c | 4842 | |
7cc8e58d MX |
4843 | for (i = 0; i < map->num_stripes; i++) { |
4844 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; | |
4845 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); | |
8217e0f8 | 4846 | map->stripes[i].dev->has_pending_chunks = true; |
7cc8e58d | 4847 | } |
43530c46 | 4848 | |
a5ed45f8 | 4849 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
1c116187 | 4850 | |
0f5d42b2 | 4851 | free_extent_map(em); |
0b246afa | 4852 | check_raid56_incompat_flag(info, type); |
53b381b3 | 4853 | |
b2117a39 | 4854 | kfree(devices_info); |
2b82032c | 4855 | return 0; |
b2117a39 | 4856 | |
6df9a95e | 4857 | error_del_extent: |
0f5d42b2 JB |
4858 | write_lock(&em_tree->lock); |
4859 | remove_extent_mapping(em_tree, em); | |
4860 | write_unlock(&em_tree->lock); | |
4861 | ||
4862 | /* One for our allocation */ | |
4863 | free_extent_map(em); | |
4864 | /* One for the tree reference */ | |
4865 | free_extent_map(em); | |
495e64f4 FM |
4866 | /* One for the pending_chunks list reference */ |
4867 | free_extent_map(em); | |
b2117a39 | 4868 | error: |
b2117a39 MX |
4869 | kfree(devices_info); |
4870 | return ret; | |
2b82032c YZ |
4871 | } |
4872 | ||
6df9a95e | 4873 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
6bccf3ab | 4874 | struct btrfs_fs_info *fs_info, |
6df9a95e | 4875 | u64 chunk_offset, u64 chunk_size) |
2b82032c | 4876 | { |
6bccf3ab JM |
4877 | struct btrfs_root *extent_root = fs_info->extent_root; |
4878 | struct btrfs_root *chunk_root = fs_info->chunk_root; | |
2b82032c | 4879 | struct btrfs_key key; |
2b82032c YZ |
4880 | struct btrfs_device *device; |
4881 | struct btrfs_chunk *chunk; | |
4882 | struct btrfs_stripe *stripe; | |
6df9a95e JB |
4883 | struct extent_map *em; |
4884 | struct map_lookup *map; | |
4885 | size_t item_size; | |
4886 | u64 dev_offset; | |
4887 | u64 stripe_size; | |
4888 | int i = 0; | |
140e639f | 4889 | int ret = 0; |
2b82032c | 4890 | |
592d92ee LB |
4891 | em = get_chunk_map(fs_info, chunk_offset, chunk_size); |
4892 | if (IS_ERR(em)) | |
4893 | return PTR_ERR(em); | |
6df9a95e | 4894 | |
95617d69 | 4895 | map = em->map_lookup; |
6df9a95e JB |
4896 | item_size = btrfs_chunk_item_size(map->num_stripes); |
4897 | stripe_size = em->orig_block_len; | |
4898 | ||
2b82032c | 4899 | chunk = kzalloc(item_size, GFP_NOFS); |
6df9a95e JB |
4900 | if (!chunk) { |
4901 | ret = -ENOMEM; | |
4902 | goto out; | |
4903 | } | |
4904 | ||
50460e37 FM |
4905 | /* |
4906 | * Take the device list mutex to prevent races with the final phase of | |
4907 | * a device replace operation that replaces the device object associated | |
4908 | * with the map's stripes, because the device object's id can change | |
4909 | * at any time during that final phase of the device replace operation | |
4910 | * (dev-replace.c:btrfs_dev_replace_finishing()). | |
4911 | */ | |
0b246afa | 4912 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
6df9a95e JB |
4913 | for (i = 0; i < map->num_stripes; i++) { |
4914 | device = map->stripes[i].dev; | |
4915 | dev_offset = map->stripes[i].physical; | |
2b82032c | 4916 | |
0b86a832 | 4917 | ret = btrfs_update_device(trans, device); |
3acd3953 | 4918 | if (ret) |
50460e37 | 4919 | break; |
b5d9071c NB |
4920 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
4921 | dev_offset, stripe_size); | |
6df9a95e | 4922 | if (ret) |
50460e37 FM |
4923 | break; |
4924 | } | |
4925 | if (ret) { | |
0b246afa | 4926 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
50460e37 | 4927 | goto out; |
2b82032c YZ |
4928 | } |
4929 | ||
2b82032c | 4930 | stripe = &chunk->stripe; |
6df9a95e JB |
4931 | for (i = 0; i < map->num_stripes; i++) { |
4932 | device = map->stripes[i].dev; | |
4933 | dev_offset = map->stripes[i].physical; | |
0b86a832 | 4934 | |
e17cade2 CM |
4935 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
4936 | btrfs_set_stack_stripe_offset(stripe, dev_offset); | |
4937 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); | |
2b82032c | 4938 | stripe++; |
0b86a832 | 4939 | } |
0b246afa | 4940 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
0b86a832 | 4941 | |
2b82032c | 4942 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
0b86a832 | 4943 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
2b82032c YZ |
4944 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
4945 | btrfs_set_stack_chunk_type(chunk, map->type); | |
4946 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); | |
4947 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); | |
4948 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); | |
0b246afa | 4949 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
2b82032c | 4950 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
0b86a832 | 4951 | |
2b82032c YZ |
4952 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
4953 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
4954 | key.offset = chunk_offset; | |
0b86a832 | 4955 | |
2b82032c | 4956 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
4ed1d16e MF |
4957 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
4958 | /* | |
4959 | * TODO: Cleanup of inserted chunk root in case of | |
4960 | * failure. | |
4961 | */ | |
2ff7e61e | 4962 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
8f18cf13 | 4963 | } |
1abe9b8a | 4964 | |
6df9a95e | 4965 | out: |
0b86a832 | 4966 | kfree(chunk); |
6df9a95e | 4967 | free_extent_map(em); |
4ed1d16e | 4968 | return ret; |
2b82032c | 4969 | } |
0b86a832 | 4970 | |
2b82032c YZ |
4971 | /* |
4972 | * Chunk allocation falls into two parts. The first part does works | |
4973 | * that make the new allocated chunk useable, but not do any operation | |
4974 | * that modifies the chunk tree. The second part does the works that | |
4975 | * require modifying the chunk tree. This division is important for the | |
4976 | * bootstrap process of adding storage to a seed btrfs. | |
4977 | */ | |
4978 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, | |
2ff7e61e | 4979 | struct btrfs_fs_info *fs_info, u64 type) |
2b82032c YZ |
4980 | { |
4981 | u64 chunk_offset; | |
2b82032c | 4982 | |
0b246afa JM |
4983 | ASSERT(mutex_is_locked(&fs_info->chunk_mutex)); |
4984 | chunk_offset = find_next_chunk(fs_info); | |
72b468c8 | 4985 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
2b82032c YZ |
4986 | } |
4987 | ||
d397712b | 4988 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
e4a4dce7 | 4989 | struct btrfs_fs_info *fs_info) |
2b82032c YZ |
4990 | { |
4991 | u64 chunk_offset; | |
4992 | u64 sys_chunk_offset; | |
2b82032c | 4993 | u64 alloc_profile; |
2b82032c YZ |
4994 | int ret; |
4995 | ||
6df9a95e | 4996 | chunk_offset = find_next_chunk(fs_info); |
1b86826d | 4997 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
72b468c8 | 4998 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
79787eaa JM |
4999 | if (ret) |
5000 | return ret; | |
2b82032c | 5001 | |
0b246afa | 5002 | sys_chunk_offset = find_next_chunk(fs_info); |
1b86826d | 5003 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
72b468c8 | 5004 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
79787eaa | 5005 | return ret; |
2b82032c YZ |
5006 | } |
5007 | ||
d20983b4 MX |
5008 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
5009 | { | |
5010 | int max_errors; | |
5011 | ||
5012 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | | |
5013 | BTRFS_BLOCK_GROUP_RAID10 | | |
79b33f0c | 5014 | BTRFS_BLOCK_GROUP_RAID5)) { |
d20983b4 MX |
5015 | max_errors = 1; |
5016 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { | |
5017 | max_errors = 2; | |
5018 | } else { | |
5019 | max_errors = 0; | |
005d6427 | 5020 | } |
2b82032c | 5021 | |
d20983b4 | 5022 | return max_errors; |
2b82032c YZ |
5023 | } |
5024 | ||
2ff7e61e | 5025 | int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset) |
2b82032c YZ |
5026 | { |
5027 | struct extent_map *em; | |
5028 | struct map_lookup *map; | |
2b82032c | 5029 | int readonly = 0; |
d20983b4 | 5030 | int miss_ndevs = 0; |
2b82032c YZ |
5031 | int i; |
5032 | ||
592d92ee LB |
5033 | em = get_chunk_map(fs_info, chunk_offset, 1); |
5034 | if (IS_ERR(em)) | |
2b82032c YZ |
5035 | return 1; |
5036 | ||
95617d69 | 5037 | map = em->map_lookup; |
2b82032c | 5038 | for (i = 0; i < map->num_stripes; i++) { |
d20983b4 MX |
5039 | if (map->stripes[i].dev->missing) { |
5040 | miss_ndevs++; | |
5041 | continue; | |
5042 | } | |
5043 | ||
2b82032c YZ |
5044 | if (!map->stripes[i].dev->writeable) { |
5045 | readonly = 1; | |
d20983b4 | 5046 | goto end; |
2b82032c YZ |
5047 | } |
5048 | } | |
d20983b4 MX |
5049 | |
5050 | /* | |
5051 | * If the number of missing devices is larger than max errors, | |
5052 | * we can not write the data into that chunk successfully, so | |
5053 | * set it readonly. | |
5054 | */ | |
5055 | if (miss_ndevs > btrfs_chunk_max_errors(map)) | |
5056 | readonly = 1; | |
5057 | end: | |
0b86a832 | 5058 | free_extent_map(em); |
2b82032c | 5059 | return readonly; |
0b86a832 CM |
5060 | } |
5061 | ||
5062 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) | |
5063 | { | |
a8067e02 | 5064 | extent_map_tree_init(&tree->map_tree); |
0b86a832 CM |
5065 | } |
5066 | ||
5067 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) | |
5068 | { | |
5069 | struct extent_map *em; | |
5070 | ||
d397712b | 5071 | while (1) { |
890871be | 5072 | write_lock(&tree->map_tree.lock); |
0b86a832 CM |
5073 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
5074 | if (em) | |
5075 | remove_extent_mapping(&tree->map_tree, em); | |
890871be | 5076 | write_unlock(&tree->map_tree.lock); |
0b86a832 CM |
5077 | if (!em) |
5078 | break; | |
0b86a832 CM |
5079 | /* once for us */ |
5080 | free_extent_map(em); | |
5081 | /* once for the tree */ | |
5082 | free_extent_map(em); | |
5083 | } | |
5084 | } | |
5085 | ||
5d964051 | 5086 | int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
f188591e CM |
5087 | { |
5088 | struct extent_map *em; | |
5089 | struct map_lookup *map; | |
f188591e CM |
5090 | int ret; |
5091 | ||
592d92ee LB |
5092 | em = get_chunk_map(fs_info, logical, len); |
5093 | if (IS_ERR(em)) | |
5094 | /* | |
5095 | * We could return errors for these cases, but that could get | |
5096 | * ugly and we'd probably do the same thing which is just not do | |
5097 | * anything else and exit, so return 1 so the callers don't try | |
5098 | * to use other copies. | |
5099 | */ | |
fb7669b5 | 5100 | return 1; |
fb7669b5 | 5101 | |
95617d69 | 5102 | map = em->map_lookup; |
f188591e CM |
5103 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
5104 | ret = map->num_stripes; | |
321aecc6 CM |
5105 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
5106 | ret = map->sub_stripes; | |
53b381b3 DW |
5107 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
5108 | ret = 2; | |
5109 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) | |
cc41f4fe LB |
5110 | /* |
5111 | * There could be two corrupted data stripes, we need | |
5112 | * to loop retry in order to rebuild the correct data. | |
5113 | * | |
5114 | * Fail a stripe at a time on every retry except the | |
5115 | * stripe under reconstruction. | |
5116 | */ | |
5117 | ret = map->num_stripes; | |
f188591e CM |
5118 | else |
5119 | ret = 1; | |
5120 | free_extent_map(em); | |
ad6d620e | 5121 | |
73beece9 | 5122 | btrfs_dev_replace_lock(&fs_info->dev_replace, 0); |
6fad823f LB |
5123 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
5124 | fs_info->dev_replace.tgtdev) | |
ad6d620e | 5125 | ret++; |
73beece9 | 5126 | btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); |
ad6d620e | 5127 | |
f188591e CM |
5128 | return ret; |
5129 | } | |
5130 | ||
2ff7e61e | 5131 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
53b381b3 DW |
5132 | u64 logical) |
5133 | { | |
5134 | struct extent_map *em; | |
5135 | struct map_lookup *map; | |
0b246afa | 5136 | unsigned long len = fs_info->sectorsize; |
53b381b3 | 5137 | |
592d92ee | 5138 | em = get_chunk_map(fs_info, logical, len); |
53b381b3 | 5139 | |
69f03f13 NB |
5140 | if (!WARN_ON(IS_ERR(em))) { |
5141 | map = em->map_lookup; | |
5142 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) | |
5143 | len = map->stripe_len * nr_data_stripes(map); | |
5144 | free_extent_map(em); | |
5145 | } | |
53b381b3 DW |
5146 | return len; |
5147 | } | |
5148 | ||
e4ff5fb5 | 5149 | int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len) |
53b381b3 DW |
5150 | { |
5151 | struct extent_map *em; | |
5152 | struct map_lookup *map; | |
53b381b3 DW |
5153 | int ret = 0; |
5154 | ||
592d92ee | 5155 | em = get_chunk_map(fs_info, logical, len); |
53b381b3 | 5156 | |
69f03f13 NB |
5157 | if(!WARN_ON(IS_ERR(em))) { |
5158 | map = em->map_lookup; | |
5159 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) | |
5160 | ret = 1; | |
5161 | free_extent_map(em); | |
5162 | } | |
53b381b3 DW |
5163 | return ret; |
5164 | } | |
5165 | ||
30d9861f SB |
5166 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
5167 | struct map_lookup *map, int first, int num, | |
5168 | int optimal, int dev_replace_is_ongoing) | |
dfe25020 CM |
5169 | { |
5170 | int i; | |
30d9861f SB |
5171 | int tolerance; |
5172 | struct btrfs_device *srcdev; | |
5173 | ||
5174 | if (dev_replace_is_ongoing && | |
5175 | fs_info->dev_replace.cont_reading_from_srcdev_mode == | |
5176 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) | |
5177 | srcdev = fs_info->dev_replace.srcdev; | |
5178 | else | |
5179 | srcdev = NULL; | |
5180 | ||
5181 | /* | |
5182 | * try to avoid the drive that is the source drive for a | |
5183 | * dev-replace procedure, only choose it if no other non-missing | |
5184 | * mirror is available | |
5185 | */ | |
5186 | for (tolerance = 0; tolerance < 2; tolerance++) { | |
5187 | if (map->stripes[optimal].dev->bdev && | |
5188 | (tolerance || map->stripes[optimal].dev != srcdev)) | |
5189 | return optimal; | |
5190 | for (i = first; i < first + num; i++) { | |
5191 | if (map->stripes[i].dev->bdev && | |
5192 | (tolerance || map->stripes[i].dev != srcdev)) | |
5193 | return i; | |
5194 | } | |
dfe25020 | 5195 | } |
30d9861f | 5196 | |
dfe25020 CM |
5197 | /* we couldn't find one that doesn't fail. Just return something |
5198 | * and the io error handling code will clean up eventually | |
5199 | */ | |
5200 | return optimal; | |
5201 | } | |
5202 | ||
53b381b3 DW |
5203 | static inline int parity_smaller(u64 a, u64 b) |
5204 | { | |
5205 | return a > b; | |
5206 | } | |
5207 | ||
5208 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ | |
8e5cfb55 | 5209 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
53b381b3 DW |
5210 | { |
5211 | struct btrfs_bio_stripe s; | |
5212 | int i; | |
5213 | u64 l; | |
5214 | int again = 1; | |
5215 | ||
5216 | while (again) { | |
5217 | again = 0; | |
cc7539ed | 5218 | for (i = 0; i < num_stripes - 1; i++) { |
8e5cfb55 ZL |
5219 | if (parity_smaller(bbio->raid_map[i], |
5220 | bbio->raid_map[i+1])) { | |
53b381b3 | 5221 | s = bbio->stripes[i]; |
8e5cfb55 | 5222 | l = bbio->raid_map[i]; |
53b381b3 | 5223 | bbio->stripes[i] = bbio->stripes[i+1]; |
8e5cfb55 | 5224 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
53b381b3 | 5225 | bbio->stripes[i+1] = s; |
8e5cfb55 | 5226 | bbio->raid_map[i+1] = l; |
2c8cdd6e | 5227 | |
53b381b3 DW |
5228 | again = 1; |
5229 | } | |
5230 | } | |
5231 | } | |
5232 | } | |
5233 | ||
6e9606d2 ZL |
5234 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
5235 | { | |
5236 | struct btrfs_bio *bbio = kzalloc( | |
e57cf21e | 5237 | /* the size of the btrfs_bio */ |
6e9606d2 | 5238 | sizeof(struct btrfs_bio) + |
e57cf21e | 5239 | /* plus the variable array for the stripes */ |
6e9606d2 | 5240 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
e57cf21e | 5241 | /* plus the variable array for the tgt dev */ |
6e9606d2 | 5242 | sizeof(int) * (real_stripes) + |
e57cf21e CM |
5243 | /* |
5244 | * plus the raid_map, which includes both the tgt dev | |
5245 | * and the stripes | |
5246 | */ | |
5247 | sizeof(u64) * (total_stripes), | |
277fb5fc | 5248 | GFP_NOFS|__GFP_NOFAIL); |
6e9606d2 ZL |
5249 | |
5250 | atomic_set(&bbio->error, 0); | |
140475ae | 5251 | refcount_set(&bbio->refs, 1); |
6e9606d2 ZL |
5252 | |
5253 | return bbio; | |
5254 | } | |
5255 | ||
5256 | void btrfs_get_bbio(struct btrfs_bio *bbio) | |
5257 | { | |
140475ae ER |
5258 | WARN_ON(!refcount_read(&bbio->refs)); |
5259 | refcount_inc(&bbio->refs); | |
6e9606d2 ZL |
5260 | } |
5261 | ||
5262 | void btrfs_put_bbio(struct btrfs_bio *bbio) | |
5263 | { | |
5264 | if (!bbio) | |
5265 | return; | |
140475ae | 5266 | if (refcount_dec_and_test(&bbio->refs)) |
6e9606d2 ZL |
5267 | kfree(bbio); |
5268 | } | |
5269 | ||
0b3d4cd3 LB |
5270 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
5271 | /* | |
5272 | * Please note that, discard won't be sent to target device of device | |
5273 | * replace. | |
5274 | */ | |
5275 | static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, | |
5276 | u64 logical, u64 length, | |
5277 | struct btrfs_bio **bbio_ret) | |
5278 | { | |
5279 | struct extent_map *em; | |
5280 | struct map_lookup *map; | |
5281 | struct btrfs_bio *bbio; | |
5282 | u64 offset; | |
5283 | u64 stripe_nr; | |
5284 | u64 stripe_nr_end; | |
5285 | u64 stripe_end_offset; | |
5286 | u64 stripe_cnt; | |
5287 | u64 stripe_len; | |
5288 | u64 stripe_offset; | |
5289 | u64 num_stripes; | |
5290 | u32 stripe_index; | |
5291 | u32 factor = 0; | |
5292 | u32 sub_stripes = 0; | |
5293 | u64 stripes_per_dev = 0; | |
5294 | u32 remaining_stripes = 0; | |
5295 | u32 last_stripe = 0; | |
5296 | int ret = 0; | |
5297 | int i; | |
5298 | ||
5299 | /* discard always return a bbio */ | |
5300 | ASSERT(bbio_ret); | |
5301 | ||
5302 | em = get_chunk_map(fs_info, logical, length); | |
5303 | if (IS_ERR(em)) | |
5304 | return PTR_ERR(em); | |
5305 | ||
5306 | map = em->map_lookup; | |
5307 | /* we don't discard raid56 yet */ | |
5308 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { | |
5309 | ret = -EOPNOTSUPP; | |
5310 | goto out; | |
5311 | } | |
5312 | ||
5313 | offset = logical - em->start; | |
5314 | length = min_t(u64, em->len - offset, length); | |
5315 | ||
5316 | stripe_len = map->stripe_len; | |
5317 | /* | |
5318 | * stripe_nr counts the total number of stripes we have to stride | |
5319 | * to get to this block | |
5320 | */ | |
5321 | stripe_nr = div64_u64(offset, stripe_len); | |
5322 | ||
5323 | /* stripe_offset is the offset of this block in its stripe */ | |
5324 | stripe_offset = offset - stripe_nr * stripe_len; | |
5325 | ||
5326 | stripe_nr_end = round_up(offset + length, map->stripe_len); | |
42c61ab6 | 5327 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
0b3d4cd3 LB |
5328 | stripe_cnt = stripe_nr_end - stripe_nr; |
5329 | stripe_end_offset = stripe_nr_end * map->stripe_len - | |
5330 | (offset + length); | |
5331 | /* | |
5332 | * after this, stripe_nr is the number of stripes on this | |
5333 | * device we have to walk to find the data, and stripe_index is | |
5334 | * the number of our device in the stripe array | |
5335 | */ | |
5336 | num_stripes = 1; | |
5337 | stripe_index = 0; | |
5338 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
5339 | BTRFS_BLOCK_GROUP_RAID10)) { | |
5340 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) | |
5341 | sub_stripes = 1; | |
5342 | else | |
5343 | sub_stripes = map->sub_stripes; | |
5344 | ||
5345 | factor = map->num_stripes / sub_stripes; | |
5346 | num_stripes = min_t(u64, map->num_stripes, | |
5347 | sub_stripes * stripe_cnt); | |
5348 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); | |
5349 | stripe_index *= sub_stripes; | |
5350 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, | |
5351 | &remaining_stripes); | |
5352 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); | |
5353 | last_stripe *= sub_stripes; | |
5354 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | | |
5355 | BTRFS_BLOCK_GROUP_DUP)) { | |
5356 | num_stripes = map->num_stripes; | |
5357 | } else { | |
5358 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, | |
5359 | &stripe_index); | |
5360 | } | |
5361 | ||
5362 | bbio = alloc_btrfs_bio(num_stripes, 0); | |
5363 | if (!bbio) { | |
5364 | ret = -ENOMEM; | |
5365 | goto out; | |
5366 | } | |
5367 | ||
5368 | for (i = 0; i < num_stripes; i++) { | |
5369 | bbio->stripes[i].physical = | |
5370 | map->stripes[stripe_index].physical + | |
5371 | stripe_offset + stripe_nr * map->stripe_len; | |
5372 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; | |
5373 | ||
5374 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | | |
5375 | BTRFS_BLOCK_GROUP_RAID10)) { | |
5376 | bbio->stripes[i].length = stripes_per_dev * | |
5377 | map->stripe_len; | |
5378 | ||
5379 | if (i / sub_stripes < remaining_stripes) | |
5380 | bbio->stripes[i].length += | |
5381 | map->stripe_len; | |
5382 | ||
5383 | /* | |
5384 | * Special for the first stripe and | |
5385 | * the last stripe: | |
5386 | * | |
5387 | * |-------|...|-------| | |
5388 | * |----------| | |
5389 | * off end_off | |
5390 | */ | |
5391 | if (i < sub_stripes) | |
5392 | bbio->stripes[i].length -= | |
5393 | stripe_offset; | |
5394 | ||
5395 | if (stripe_index >= last_stripe && | |
5396 | stripe_index <= (last_stripe + | |
5397 | sub_stripes - 1)) | |
5398 | bbio->stripes[i].length -= | |
5399 | stripe_end_offset; | |
5400 | ||
5401 | if (i == sub_stripes - 1) | |
5402 | stripe_offset = 0; | |
5403 | } else { | |
5404 | bbio->stripes[i].length = length; | |
5405 | } | |
5406 | ||
5407 | stripe_index++; | |
5408 | if (stripe_index == map->num_stripes) { | |
5409 | stripe_index = 0; | |
5410 | stripe_nr++; | |
5411 | } | |
5412 | } | |
5413 | ||
5414 | *bbio_ret = bbio; | |
5415 | bbio->map_type = map->type; | |
5416 | bbio->num_stripes = num_stripes; | |
5417 | out: | |
5418 | free_extent_map(em); | |
5419 | return ret; | |
5420 | } | |
5421 | ||
5ab56090 LB |
5422 | /* |
5423 | * In dev-replace case, for repair case (that's the only case where the mirror | |
5424 | * is selected explicitly when calling btrfs_map_block), blocks left of the | |
5425 | * left cursor can also be read from the target drive. | |
5426 | * | |
5427 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the | |
5428 | * array of stripes. | |
5429 | * For READ, it also needs to be supported using the same mirror number. | |
5430 | * | |
5431 | * If the requested block is not left of the left cursor, EIO is returned. This | |
5432 | * can happen because btrfs_num_copies() returns one more in the dev-replace | |
5433 | * case. | |
5434 | */ | |
5435 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, | |
5436 | u64 logical, u64 length, | |
5437 | u64 srcdev_devid, int *mirror_num, | |
5438 | u64 *physical) | |
5439 | { | |
5440 | struct btrfs_bio *bbio = NULL; | |
5441 | int num_stripes; | |
5442 | int index_srcdev = 0; | |
5443 | int found = 0; | |
5444 | u64 physical_of_found = 0; | |
5445 | int i; | |
5446 | int ret = 0; | |
5447 | ||
5448 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, | |
5449 | logical, &length, &bbio, 0, 0); | |
5450 | if (ret) { | |
5451 | ASSERT(bbio == NULL); | |
5452 | return ret; | |
5453 | } | |
5454 | ||
5455 | num_stripes = bbio->num_stripes; | |
5456 | if (*mirror_num > num_stripes) { | |
5457 | /* | |
5458 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, | |
5459 | * that means that the requested area is not left of the left | |
5460 | * cursor | |
5461 | */ | |
5462 | btrfs_put_bbio(bbio); | |
5463 | return -EIO; | |
5464 | } | |
5465 | ||
5466 | /* | |
5467 | * process the rest of the function using the mirror_num of the source | |
5468 | * drive. Therefore look it up first. At the end, patch the device | |
5469 | * pointer to the one of the target drive. | |
5470 | */ | |
5471 | for (i = 0; i < num_stripes; i++) { | |
5472 | if (bbio->stripes[i].dev->devid != srcdev_devid) | |
5473 | continue; | |
5474 | ||
5475 | /* | |
5476 | * In case of DUP, in order to keep it simple, only add the | |
5477 | * mirror with the lowest physical address | |
5478 | */ | |
5479 | if (found && | |
5480 | physical_of_found <= bbio->stripes[i].physical) | |
5481 | continue; | |
5482 | ||
5483 | index_srcdev = i; | |
5484 | found = 1; | |
5485 | physical_of_found = bbio->stripes[i].physical; | |
5486 | } | |
5487 | ||
5488 | btrfs_put_bbio(bbio); | |
5489 | ||
5490 | ASSERT(found); | |
5491 | if (!found) | |
5492 | return -EIO; | |
5493 | ||
5494 | *mirror_num = index_srcdev + 1; | |
5495 | *physical = physical_of_found; | |
5496 | return ret; | |
5497 | } | |
5498 | ||
73c0f228 LB |
5499 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
5500 | struct btrfs_bio **bbio_ret, | |
5501 | struct btrfs_dev_replace *dev_replace, | |
5502 | int *num_stripes_ret, int *max_errors_ret) | |
5503 | { | |
5504 | struct btrfs_bio *bbio = *bbio_ret; | |
5505 | u64 srcdev_devid = dev_replace->srcdev->devid; | |
5506 | int tgtdev_indexes = 0; | |
5507 | int num_stripes = *num_stripes_ret; | |
5508 | int max_errors = *max_errors_ret; | |
5509 | int i; | |
5510 | ||
5511 | if (op == BTRFS_MAP_WRITE) { | |
5512 | int index_where_to_add; | |
5513 | ||
5514 | /* | |
5515 | * duplicate the write operations while the dev replace | |
5516 | * procedure is running. Since the copying of the old disk to | |
5517 | * the new disk takes place at run time while the filesystem is | |
5518 | * mounted writable, the regular write operations to the old | |
5519 | * disk have to be duplicated to go to the new disk as well. | |
5520 | * | |
5521 | * Note that device->missing is handled by the caller, and that | |
5522 | * the write to the old disk is already set up in the stripes | |
5523 | * array. | |
5524 | */ | |
5525 | index_where_to_add = num_stripes; | |
5526 | for (i = 0; i < num_stripes; i++) { | |
5527 | if (bbio->stripes[i].dev->devid == srcdev_devid) { | |
5528 | /* write to new disk, too */ | |
5529 | struct btrfs_bio_stripe *new = | |
5530 | bbio->stripes + index_where_to_add; | |
5531 | struct btrfs_bio_stripe *old = | |
5532 | bbio->stripes + i; | |
5533 | ||
5534 | new->physical = old->physical; | |
5535 | new->length = old->length; | |
5536 | new->dev = dev_replace->tgtdev; | |
5537 | bbio->tgtdev_map[i] = index_where_to_add; | |
5538 | index_where_to_add++; | |
5539 | max_errors++; | |
5540 | tgtdev_indexes++; | |
5541 | } | |
5542 | } | |
5543 | num_stripes = index_where_to_add; | |
5544 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { | |
5545 | int index_srcdev = 0; | |
5546 | int found = 0; | |
5547 | u64 physical_of_found = 0; | |
5548 | ||
5549 | /* | |
5550 | * During the dev-replace procedure, the target drive can also | |
5551 | * be used to read data in case it is needed to repair a corrupt | |
5552 | * block elsewhere. This is possible if the requested area is | |
5553 | * left of the left cursor. In this area, the target drive is a | |
5554 | * full copy of the source drive. | |
5555 | */ | |
5556 | for (i = 0; i < num_stripes; i++) { | |
5557 | if (bbio->stripes[i].dev->devid == srcdev_devid) { | |
5558 | /* | |
5559 | * In case of DUP, in order to keep it simple, | |
5560 | * only add the mirror with the lowest physical | |
5561 | * address | |
5562 | */ | |
5563 | if (found && | |
5564 | physical_of_found <= | |
5565 | bbio->stripes[i].physical) | |
5566 | continue; | |
5567 | index_srcdev = i; | |
5568 | found = 1; | |
5569 | physical_of_found = bbio->stripes[i].physical; | |
5570 | } | |
5571 | } | |
5572 | if (found) { | |
5573 | struct btrfs_bio_stripe *tgtdev_stripe = | |
5574 | bbio->stripes + num_stripes; | |
5575 | ||
5576 | tgtdev_stripe->physical = physical_of_found; | |
5577 | tgtdev_stripe->length = | |
5578 | bbio->stripes[index_srcdev].length; | |
5579 | tgtdev_stripe->dev = dev_replace->tgtdev; | |
5580 | bbio->tgtdev_map[index_srcdev] = num_stripes; | |
5581 | ||
5582 | tgtdev_indexes++; | |
5583 | num_stripes++; | |
5584 | } | |
5585 | } | |
5586 | ||
5587 | *num_stripes_ret = num_stripes; | |
5588 | *max_errors_ret = max_errors; | |
5589 | bbio->num_tgtdevs = tgtdev_indexes; | |
5590 | *bbio_ret = bbio; | |
5591 | } | |
5592 | ||
2b19a1fe LB |
5593 | static bool need_full_stripe(enum btrfs_map_op op) |
5594 | { | |
5595 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); | |
5596 | } | |
5597 | ||
cf8cddd3 CH |
5598 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
5599 | enum btrfs_map_op op, | |
f2d8d74d | 5600 | u64 logical, u64 *length, |
a1d3c478 | 5601 | struct btrfs_bio **bbio_ret, |
8e5cfb55 | 5602 | int mirror_num, int need_raid_map) |
0b86a832 CM |
5603 | { |
5604 | struct extent_map *em; | |
5605 | struct map_lookup *map; | |
0b86a832 | 5606 | u64 offset; |
593060d7 CM |
5607 | u64 stripe_offset; |
5608 | u64 stripe_nr; | |
53b381b3 | 5609 | u64 stripe_len; |
9d644a62 | 5610 | u32 stripe_index; |
cea9e445 | 5611 | int i; |
de11cc12 | 5612 | int ret = 0; |
f2d8d74d | 5613 | int num_stripes; |
a236aed1 | 5614 | int max_errors = 0; |
2c8cdd6e | 5615 | int tgtdev_indexes = 0; |
a1d3c478 | 5616 | struct btrfs_bio *bbio = NULL; |
472262f3 SB |
5617 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
5618 | int dev_replace_is_ongoing = 0; | |
5619 | int num_alloc_stripes; | |
ad6d620e SB |
5620 | int patch_the_first_stripe_for_dev_replace = 0; |
5621 | u64 physical_to_patch_in_first_stripe = 0; | |
53b381b3 | 5622 | u64 raid56_full_stripe_start = (u64)-1; |
0b86a832 | 5623 | |
0b3d4cd3 LB |
5624 | if (op == BTRFS_MAP_DISCARD) |
5625 | return __btrfs_map_block_for_discard(fs_info, logical, | |
5626 | *length, bbio_ret); | |
5627 | ||
592d92ee LB |
5628 | em = get_chunk_map(fs_info, logical, *length); |
5629 | if (IS_ERR(em)) | |
5630 | return PTR_ERR(em); | |
0b86a832 | 5631 | |
95617d69 | 5632 | map = em->map_lookup; |
0b86a832 | 5633 | offset = logical - em->start; |
593060d7 | 5634 | |
53b381b3 | 5635 | stripe_len = map->stripe_len; |
593060d7 CM |
5636 | stripe_nr = offset; |
5637 | /* | |
5638 | * stripe_nr counts the total number of stripes we have to stride | |
5639 | * to get to this block | |
5640 | */ | |
47c5713f | 5641 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
593060d7 | 5642 | |
53b381b3 | 5643 | stripe_offset = stripe_nr * stripe_len; |
e042d1ec | 5644 | if (offset < stripe_offset) { |
5d163e0e JM |
5645 | btrfs_crit(fs_info, |
5646 | "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu", | |
e042d1ec JB |
5647 | stripe_offset, offset, em->start, logical, |
5648 | stripe_len); | |
5649 | free_extent_map(em); | |
5650 | return -EINVAL; | |
5651 | } | |
593060d7 CM |
5652 | |
5653 | /* stripe_offset is the offset of this block in its stripe*/ | |
5654 | stripe_offset = offset - stripe_offset; | |
5655 | ||
53b381b3 | 5656 | /* if we're here for raid56, we need to know the stripe aligned start */ |
ffe2d203 | 5657 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
53b381b3 DW |
5658 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
5659 | raid56_full_stripe_start = offset; | |
5660 | ||
5661 | /* allow a write of a full stripe, but make sure we don't | |
5662 | * allow straddling of stripes | |
5663 | */ | |
47c5713f DS |
5664 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
5665 | full_stripe_len); | |
53b381b3 DW |
5666 | raid56_full_stripe_start *= full_stripe_len; |
5667 | } | |
5668 | ||
0b3d4cd3 | 5669 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
53b381b3 DW |
5670 | u64 max_len; |
5671 | /* For writes to RAID[56], allow a full stripeset across all disks. | |
5672 | For other RAID types and for RAID[56] reads, just allow a single | |
5673 | stripe (on a single disk). */ | |
ffe2d203 | 5674 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
cf8cddd3 | 5675 | (op == BTRFS_MAP_WRITE)) { |
53b381b3 DW |
5676 | max_len = stripe_len * nr_data_stripes(map) - |
5677 | (offset - raid56_full_stripe_start); | |
5678 | } else { | |
5679 | /* we limit the length of each bio to what fits in a stripe */ | |
5680 | max_len = stripe_len - stripe_offset; | |
5681 | } | |
5682 | *length = min_t(u64, em->len - offset, max_len); | |
cea9e445 CM |
5683 | } else { |
5684 | *length = em->len - offset; | |
5685 | } | |
f2d8d74d | 5686 | |
53b381b3 DW |
5687 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
5688 | it cares about is the length */ | |
a1d3c478 | 5689 | if (!bbio_ret) |
cea9e445 CM |
5690 | goto out; |
5691 | ||
73beece9 | 5692 | btrfs_dev_replace_lock(dev_replace, 0); |
472262f3 SB |
5693 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
5694 | if (!dev_replace_is_ongoing) | |
73beece9 LB |
5695 | btrfs_dev_replace_unlock(dev_replace, 0); |
5696 | else | |
5697 | btrfs_dev_replace_set_lock_blocking(dev_replace); | |
472262f3 | 5698 | |
ad6d620e | 5699 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
2b19a1fe | 5700 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
5ab56090 LB |
5701 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
5702 | dev_replace->srcdev->devid, | |
5703 | &mirror_num, | |
5704 | &physical_to_patch_in_first_stripe); | |
5705 | if (ret) | |
ad6d620e | 5706 | goto out; |
5ab56090 LB |
5707 | else |
5708 | patch_the_first_stripe_for_dev_replace = 1; | |
ad6d620e SB |
5709 | } else if (mirror_num > map->num_stripes) { |
5710 | mirror_num = 0; | |
5711 | } | |
5712 | ||
f2d8d74d | 5713 | num_stripes = 1; |
cea9e445 | 5714 | stripe_index = 0; |
fce3bb9a | 5715 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
47c5713f DS |
5716 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
5717 | &stripe_index); | |
de483734 | 5718 | if (!need_full_stripe(op)) |
28e1cc7d | 5719 | mirror_num = 1; |
fce3bb9a | 5720 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
de483734 | 5721 | if (need_full_stripe(op)) |
f2d8d74d | 5722 | num_stripes = map->num_stripes; |
2fff734f | 5723 | else if (mirror_num) |
f188591e | 5724 | stripe_index = mirror_num - 1; |
dfe25020 | 5725 | else { |
30d9861f | 5726 | stripe_index = find_live_mirror(fs_info, map, 0, |
dfe25020 | 5727 | map->num_stripes, |
30d9861f SB |
5728 | current->pid % map->num_stripes, |
5729 | dev_replace_is_ongoing); | |
a1d3c478 | 5730 | mirror_num = stripe_index + 1; |
dfe25020 | 5731 | } |
2fff734f | 5732 | |
611f0e00 | 5733 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
de483734 | 5734 | if (need_full_stripe(op)) { |
f2d8d74d | 5735 | num_stripes = map->num_stripes; |
a1d3c478 | 5736 | } else if (mirror_num) { |
f188591e | 5737 | stripe_index = mirror_num - 1; |
a1d3c478 JS |
5738 | } else { |
5739 | mirror_num = 1; | |
5740 | } | |
2fff734f | 5741 | |
321aecc6 | 5742 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
9d644a62 | 5743 | u32 factor = map->num_stripes / map->sub_stripes; |
321aecc6 | 5744 | |
47c5713f | 5745 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
321aecc6 CM |
5746 | stripe_index *= map->sub_stripes; |
5747 | ||
de483734 | 5748 | if (need_full_stripe(op)) |
f2d8d74d | 5749 | num_stripes = map->sub_stripes; |
321aecc6 CM |
5750 | else if (mirror_num) |
5751 | stripe_index += mirror_num - 1; | |
dfe25020 | 5752 | else { |
3e74317a | 5753 | int old_stripe_index = stripe_index; |
30d9861f SB |
5754 | stripe_index = find_live_mirror(fs_info, map, |
5755 | stripe_index, | |
dfe25020 | 5756 | map->sub_stripes, stripe_index + |
30d9861f SB |
5757 | current->pid % map->sub_stripes, |
5758 | dev_replace_is_ongoing); | |
3e74317a | 5759 | mirror_num = stripe_index - old_stripe_index + 1; |
dfe25020 | 5760 | } |
53b381b3 | 5761 | |
ffe2d203 | 5762 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
de483734 | 5763 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
53b381b3 | 5764 | /* push stripe_nr back to the start of the full stripe */ |
42c61ab6 | 5765 | stripe_nr = div64_u64(raid56_full_stripe_start, |
b8b93add | 5766 | stripe_len * nr_data_stripes(map)); |
53b381b3 DW |
5767 | |
5768 | /* RAID[56] write or recovery. Return all stripes */ | |
5769 | num_stripes = map->num_stripes; | |
5770 | max_errors = nr_parity_stripes(map); | |
5771 | ||
53b381b3 DW |
5772 | *length = map->stripe_len; |
5773 | stripe_index = 0; | |
5774 | stripe_offset = 0; | |
5775 | } else { | |
5776 | /* | |
5777 | * Mirror #0 or #1 means the original data block. | |
5778 | * Mirror #2 is RAID5 parity block. | |
5779 | * Mirror #3 is RAID6 Q block. | |
5780 | */ | |
47c5713f DS |
5781 | stripe_nr = div_u64_rem(stripe_nr, |
5782 | nr_data_stripes(map), &stripe_index); | |
53b381b3 DW |
5783 | if (mirror_num > 1) |
5784 | stripe_index = nr_data_stripes(map) + | |
5785 | mirror_num - 2; | |
5786 | ||
5787 | /* We distribute the parity blocks across stripes */ | |
47c5713f DS |
5788 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
5789 | &stripe_index); | |
de483734 | 5790 | if (!need_full_stripe(op) && mirror_num <= 1) |
28e1cc7d | 5791 | mirror_num = 1; |
53b381b3 | 5792 | } |
8790d502 CM |
5793 | } else { |
5794 | /* | |
47c5713f DS |
5795 | * after this, stripe_nr is the number of stripes on this |
5796 | * device we have to walk to find the data, and stripe_index is | |
5797 | * the number of our device in the stripe array | |
8790d502 | 5798 | */ |
47c5713f DS |
5799 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
5800 | &stripe_index); | |
a1d3c478 | 5801 | mirror_num = stripe_index + 1; |
8790d502 | 5802 | } |
e042d1ec | 5803 | if (stripe_index >= map->num_stripes) { |
5d163e0e JM |
5804 | btrfs_crit(fs_info, |
5805 | "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u", | |
e042d1ec JB |
5806 | stripe_index, map->num_stripes); |
5807 | ret = -EINVAL; | |
5808 | goto out; | |
5809 | } | |
cea9e445 | 5810 | |
472262f3 | 5811 | num_alloc_stripes = num_stripes; |
6fad823f | 5812 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
0b3d4cd3 | 5813 | if (op == BTRFS_MAP_WRITE) |
ad6d620e | 5814 | num_alloc_stripes <<= 1; |
cf8cddd3 | 5815 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
ad6d620e | 5816 | num_alloc_stripes++; |
2c8cdd6e | 5817 | tgtdev_indexes = num_stripes; |
ad6d620e | 5818 | } |
2c8cdd6e | 5819 | |
6e9606d2 | 5820 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
de11cc12 LZ |
5821 | if (!bbio) { |
5822 | ret = -ENOMEM; | |
5823 | goto out; | |
5824 | } | |
6fad823f | 5825 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
2c8cdd6e | 5826 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
de11cc12 | 5827 | |
8e5cfb55 | 5828 | /* build raid_map */ |
2b19a1fe LB |
5829 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
5830 | (need_full_stripe(op) || mirror_num > 1)) { | |
8e5cfb55 | 5831 | u64 tmp; |
9d644a62 | 5832 | unsigned rot; |
8e5cfb55 ZL |
5833 | |
5834 | bbio->raid_map = (u64 *)((void *)bbio->stripes + | |
5835 | sizeof(struct btrfs_bio_stripe) * | |
5836 | num_alloc_stripes + | |
5837 | sizeof(int) * tgtdev_indexes); | |
5838 | ||
5839 | /* Work out the disk rotation on this stripe-set */ | |
47c5713f | 5840 | div_u64_rem(stripe_nr, num_stripes, &rot); |
8e5cfb55 ZL |
5841 | |
5842 | /* Fill in the logical address of each stripe */ | |
5843 | tmp = stripe_nr * nr_data_stripes(map); | |
5844 | for (i = 0; i < nr_data_stripes(map); i++) | |
5845 | bbio->raid_map[(i+rot) % num_stripes] = | |
5846 | em->start + (tmp + i) * map->stripe_len; | |
5847 | ||
5848 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; | |
5849 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) | |
5850 | bbio->raid_map[(i+rot+1) % num_stripes] = | |
5851 | RAID6_Q_STRIPE; | |
5852 | } | |
5853 | ||
b89203f7 | 5854 | |
0b3d4cd3 LB |
5855 | for (i = 0; i < num_stripes; i++) { |
5856 | bbio->stripes[i].physical = | |
5857 | map->stripes[stripe_index].physical + | |
5858 | stripe_offset + | |
5859 | stripe_nr * map->stripe_len; | |
5860 | bbio->stripes[i].dev = | |
5861 | map->stripes[stripe_index].dev; | |
5862 | stripe_index++; | |
593060d7 | 5863 | } |
de11cc12 | 5864 | |
2b19a1fe | 5865 | if (need_full_stripe(op)) |
d20983b4 | 5866 | max_errors = btrfs_chunk_max_errors(map); |
de11cc12 | 5867 | |
8e5cfb55 ZL |
5868 | if (bbio->raid_map) |
5869 | sort_parity_stripes(bbio, num_stripes); | |
cc7539ed | 5870 | |
73c0f228 | 5871 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
2b19a1fe | 5872 | need_full_stripe(op)) { |
73c0f228 LB |
5873 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
5874 | &max_errors); | |
472262f3 SB |
5875 | } |
5876 | ||
de11cc12 | 5877 | *bbio_ret = bbio; |
10f11900 | 5878 | bbio->map_type = map->type; |
de11cc12 LZ |
5879 | bbio->num_stripes = num_stripes; |
5880 | bbio->max_errors = max_errors; | |
5881 | bbio->mirror_num = mirror_num; | |
ad6d620e SB |
5882 | |
5883 | /* | |
5884 | * this is the case that REQ_READ && dev_replace_is_ongoing && | |
5885 | * mirror_num == num_stripes + 1 && dev_replace target drive is | |
5886 | * available as a mirror | |
5887 | */ | |
5888 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { | |
5889 | WARN_ON(num_stripes > 1); | |
5890 | bbio->stripes[0].dev = dev_replace->tgtdev; | |
5891 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; | |
5892 | bbio->mirror_num = map->num_stripes + 1; | |
5893 | } | |
cea9e445 | 5894 | out: |
73beece9 LB |
5895 | if (dev_replace_is_ongoing) { |
5896 | btrfs_dev_replace_clear_lock_blocking(dev_replace); | |
5897 | btrfs_dev_replace_unlock(dev_replace, 0); | |
5898 | } | |
0b86a832 | 5899 | free_extent_map(em); |
de11cc12 | 5900 | return ret; |
0b86a832 CM |
5901 | } |
5902 | ||
cf8cddd3 | 5903 | int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, |
f2d8d74d | 5904 | u64 logical, u64 *length, |
a1d3c478 | 5905 | struct btrfs_bio **bbio_ret, int mirror_num) |
f2d8d74d | 5906 | { |
b3d3fa51 | 5907 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
8e5cfb55 | 5908 | mirror_num, 0); |
f2d8d74d CM |
5909 | } |
5910 | ||
af8e2d1d | 5911 | /* For Scrub/replace */ |
cf8cddd3 | 5912 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, |
af8e2d1d | 5913 | u64 logical, u64 *length, |
825ad4c9 | 5914 | struct btrfs_bio **bbio_ret) |
af8e2d1d | 5915 | { |
825ad4c9 | 5916 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1); |
af8e2d1d MX |
5917 | } |
5918 | ||
ab8d0fc4 | 5919 | int btrfs_rmap_block(struct btrfs_fs_info *fs_info, |
a512bbf8 YZ |
5920 | u64 chunk_start, u64 physical, u64 devid, |
5921 | u64 **logical, int *naddrs, int *stripe_len) | |
5922 | { | |
a512bbf8 YZ |
5923 | struct extent_map *em; |
5924 | struct map_lookup *map; | |
5925 | u64 *buf; | |
5926 | u64 bytenr; | |
5927 | u64 length; | |
5928 | u64 stripe_nr; | |
53b381b3 | 5929 | u64 rmap_len; |
a512bbf8 YZ |
5930 | int i, j, nr = 0; |
5931 | ||
592d92ee LB |
5932 | em = get_chunk_map(fs_info, chunk_start, 1); |
5933 | if (IS_ERR(em)) | |
835d974f | 5934 | return -EIO; |
835d974f | 5935 | |
95617d69 | 5936 | map = em->map_lookup; |
a512bbf8 | 5937 | length = em->len; |
53b381b3 DW |
5938 | rmap_len = map->stripe_len; |
5939 | ||
a512bbf8 | 5940 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
b8b93add | 5941 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
a512bbf8 | 5942 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
b8b93add | 5943 | length = div_u64(length, map->num_stripes); |
ffe2d203 | 5944 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
b8b93add | 5945 | length = div_u64(length, nr_data_stripes(map)); |
53b381b3 DW |
5946 | rmap_len = map->stripe_len * nr_data_stripes(map); |
5947 | } | |
a512bbf8 | 5948 | |
31e818fe | 5949 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
79787eaa | 5950 | BUG_ON(!buf); /* -ENOMEM */ |
a512bbf8 YZ |
5951 | |
5952 | for (i = 0; i < map->num_stripes; i++) { | |
5953 | if (devid && map->stripes[i].dev->devid != devid) | |
5954 | continue; | |
5955 | if (map->stripes[i].physical > physical || | |
5956 | map->stripes[i].physical + length <= physical) | |
5957 | continue; | |
5958 | ||
5959 | stripe_nr = physical - map->stripes[i].physical; | |
42c61ab6 | 5960 | stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
a512bbf8 YZ |
5961 | |
5962 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { | |
5963 | stripe_nr = stripe_nr * map->num_stripes + i; | |
b8b93add | 5964 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
a512bbf8 YZ |
5965 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
5966 | stripe_nr = stripe_nr * map->num_stripes + i; | |
53b381b3 DW |
5967 | } /* else if RAID[56], multiply by nr_data_stripes(). |
5968 | * Alternatively, just use rmap_len below instead of | |
5969 | * map->stripe_len */ | |
5970 | ||
5971 | bytenr = chunk_start + stripe_nr * rmap_len; | |
934d375b | 5972 | WARN_ON(nr >= map->num_stripes); |
a512bbf8 YZ |
5973 | for (j = 0; j < nr; j++) { |
5974 | if (buf[j] == bytenr) | |
5975 | break; | |
5976 | } | |
934d375b CM |
5977 | if (j == nr) { |
5978 | WARN_ON(nr >= map->num_stripes); | |
a512bbf8 | 5979 | buf[nr++] = bytenr; |
934d375b | 5980 | } |
a512bbf8 YZ |
5981 | } |
5982 | ||
a512bbf8 YZ |
5983 | *logical = buf; |
5984 | *naddrs = nr; | |
53b381b3 | 5985 | *stripe_len = rmap_len; |
a512bbf8 YZ |
5986 | |
5987 | free_extent_map(em); | |
5988 | return 0; | |
f2d8d74d CM |
5989 | } |
5990 | ||
4246a0b6 | 5991 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio) |
8408c716 | 5992 | { |
326e1dbb MS |
5993 | bio->bi_private = bbio->private; |
5994 | bio->bi_end_io = bbio->end_io; | |
4246a0b6 | 5995 | bio_endio(bio); |
326e1dbb | 5996 | |
6e9606d2 | 5997 | btrfs_put_bbio(bbio); |
8408c716 MX |
5998 | } |
5999 | ||
4246a0b6 | 6000 | static void btrfs_end_bio(struct bio *bio) |
8790d502 | 6001 | { |
9be3395b | 6002 | struct btrfs_bio *bbio = bio->bi_private; |
7d2b4daa | 6003 | int is_orig_bio = 0; |
8790d502 | 6004 | |
4e4cbee9 | 6005 | if (bio->bi_status) { |
a1d3c478 | 6006 | atomic_inc(&bbio->error); |
4e4cbee9 CH |
6007 | if (bio->bi_status == BLK_STS_IOERR || |
6008 | bio->bi_status == BLK_STS_TARGET) { | |
442a4f63 | 6009 | unsigned int stripe_index = |
9be3395b | 6010 | btrfs_io_bio(bio)->stripe_index; |
65f53338 | 6011 | struct btrfs_device *dev; |
442a4f63 SB |
6012 | |
6013 | BUG_ON(stripe_index >= bbio->num_stripes); | |
6014 | dev = bbio->stripes[stripe_index].dev; | |
597a60fa | 6015 | if (dev->bdev) { |
37226b21 | 6016 | if (bio_op(bio) == REQ_OP_WRITE) |
597a60fa SB |
6017 | btrfs_dev_stat_inc(dev, |
6018 | BTRFS_DEV_STAT_WRITE_ERRS); | |
6019 | else | |
6020 | btrfs_dev_stat_inc(dev, | |
6021 | BTRFS_DEV_STAT_READ_ERRS); | |
70fd7614 | 6022 | if (bio->bi_opf & REQ_PREFLUSH) |
597a60fa SB |
6023 | btrfs_dev_stat_inc(dev, |
6024 | BTRFS_DEV_STAT_FLUSH_ERRS); | |
6025 | btrfs_dev_stat_print_on_error(dev); | |
6026 | } | |
442a4f63 SB |
6027 | } |
6028 | } | |
8790d502 | 6029 | |
a1d3c478 | 6030 | if (bio == bbio->orig_bio) |
7d2b4daa CM |
6031 | is_orig_bio = 1; |
6032 | ||
c404e0dc MX |
6033 | btrfs_bio_counter_dec(bbio->fs_info); |
6034 | ||
a1d3c478 | 6035 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
7d2b4daa CM |
6036 | if (!is_orig_bio) { |
6037 | bio_put(bio); | |
a1d3c478 | 6038 | bio = bbio->orig_bio; |
7d2b4daa | 6039 | } |
c7b22bb1 | 6040 | |
9be3395b | 6041 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
a236aed1 | 6042 | /* only send an error to the higher layers if it is |
53b381b3 | 6043 | * beyond the tolerance of the btrfs bio |
a236aed1 | 6044 | */ |
a1d3c478 | 6045 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
4e4cbee9 | 6046 | bio->bi_status = BLK_STS_IOERR; |
5dbc8fca | 6047 | } else { |
1259ab75 CM |
6048 | /* |
6049 | * this bio is actually up to date, we didn't | |
6050 | * go over the max number of errors | |
6051 | */ | |
2dbe0c77 | 6052 | bio->bi_status = BLK_STS_OK; |
1259ab75 | 6053 | } |
c55f1396 | 6054 | |
4246a0b6 | 6055 | btrfs_end_bbio(bbio, bio); |
7d2b4daa | 6056 | } else if (!is_orig_bio) { |
8790d502 CM |
6057 | bio_put(bio); |
6058 | } | |
8790d502 CM |
6059 | } |
6060 | ||
8b712842 CM |
6061 | /* |
6062 | * see run_scheduled_bios for a description of why bios are collected for | |
6063 | * async submit. | |
6064 | * | |
6065 | * This will add one bio to the pending list for a device and make sure | |
6066 | * the work struct is scheduled. | |
6067 | */ | |
2ff7e61e | 6068 | static noinline void btrfs_schedule_bio(struct btrfs_device *device, |
4e49ea4a | 6069 | struct bio *bio) |
8b712842 | 6070 | { |
0b246afa | 6071 | struct btrfs_fs_info *fs_info = device->fs_info; |
8b712842 | 6072 | int should_queue = 1; |
ffbd517d | 6073 | struct btrfs_pending_bios *pending_bios; |
8b712842 | 6074 | |
53b381b3 | 6075 | if (device->missing || !device->bdev) { |
4246a0b6 | 6076 | bio_io_error(bio); |
53b381b3 DW |
6077 | return; |
6078 | } | |
6079 | ||
8b712842 | 6080 | /* don't bother with additional async steps for reads, right now */ |
37226b21 | 6081 | if (bio_op(bio) == REQ_OP_READ) { |
492bb6de | 6082 | bio_get(bio); |
4e49ea4a | 6083 | btrfsic_submit_bio(bio); |
492bb6de | 6084 | bio_put(bio); |
143bede5 | 6085 | return; |
8b712842 CM |
6086 | } |
6087 | ||
492bb6de | 6088 | WARN_ON(bio->bi_next); |
8b712842 | 6089 | bio->bi_next = NULL; |
8b712842 CM |
6090 | |
6091 | spin_lock(&device->io_lock); | |
67f055c7 | 6092 | if (op_is_sync(bio->bi_opf)) |
ffbd517d CM |
6093 | pending_bios = &device->pending_sync_bios; |
6094 | else | |
6095 | pending_bios = &device->pending_bios; | |
8b712842 | 6096 | |
ffbd517d CM |
6097 | if (pending_bios->tail) |
6098 | pending_bios->tail->bi_next = bio; | |
8b712842 | 6099 | |
ffbd517d CM |
6100 | pending_bios->tail = bio; |
6101 | if (!pending_bios->head) | |
6102 | pending_bios->head = bio; | |
8b712842 CM |
6103 | if (device->running_pending) |
6104 | should_queue = 0; | |
6105 | ||
6106 | spin_unlock(&device->io_lock); | |
6107 | ||
6108 | if (should_queue) | |
0b246afa | 6109 | btrfs_queue_work(fs_info->submit_workers, &device->work); |
8b712842 CM |
6110 | } |
6111 | ||
2ff7e61e JM |
6112 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
6113 | u64 physical, int dev_nr, int async) | |
de1ee92a JB |
6114 | { |
6115 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; | |
2ff7e61e | 6116 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
de1ee92a JB |
6117 | |
6118 | bio->bi_private = bbio; | |
9be3395b | 6119 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
de1ee92a | 6120 | bio->bi_end_io = btrfs_end_bio; |
4f024f37 | 6121 | bio->bi_iter.bi_sector = physical >> 9; |
de1ee92a JB |
6122 | #ifdef DEBUG |
6123 | { | |
6124 | struct rcu_string *name; | |
6125 | ||
6126 | rcu_read_lock(); | |
6127 | name = rcu_dereference(dev->name); | |
ab8d0fc4 JM |
6128 | btrfs_debug(fs_info, |
6129 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", | |
6130 | bio_op(bio), bio->bi_opf, | |
6131 | (u64)bio->bi_iter.bi_sector, | |
6132 | (u_long)dev->bdev->bd_dev, name->str, dev->devid, | |
6133 | bio->bi_iter.bi_size); | |
de1ee92a JB |
6134 | rcu_read_unlock(); |
6135 | } | |
6136 | #endif | |
74d46992 | 6137 | bio_set_dev(bio, dev->bdev); |
c404e0dc | 6138 | |
2ff7e61e | 6139 | btrfs_bio_counter_inc_noblocked(fs_info); |
c404e0dc | 6140 | |
de1ee92a | 6141 | if (async) |
2ff7e61e | 6142 | btrfs_schedule_bio(dev, bio); |
de1ee92a | 6143 | else |
4e49ea4a | 6144 | btrfsic_submit_bio(bio); |
de1ee92a JB |
6145 | } |
6146 | ||
de1ee92a JB |
6147 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
6148 | { | |
6149 | atomic_inc(&bbio->error); | |
6150 | if (atomic_dec_and_test(&bbio->stripes_pending)) { | |
01327610 | 6151 | /* Should be the original bio. */ |
8408c716 MX |
6152 | WARN_ON(bio != bbio->orig_bio); |
6153 | ||
9be3395b | 6154 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
4f024f37 | 6155 | bio->bi_iter.bi_sector = logical >> 9; |
102ed2c5 AJ |
6156 | if (atomic_read(&bbio->error) > bbio->max_errors) |
6157 | bio->bi_status = BLK_STS_IOERR; | |
6158 | else | |
6159 | bio->bi_status = BLK_STS_OK; | |
4246a0b6 | 6160 | btrfs_end_bbio(bbio, bio); |
de1ee92a JB |
6161 | } |
6162 | } | |
6163 | ||
58efbc9f OS |
6164 | blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, |
6165 | int mirror_num, int async_submit) | |
0b86a832 | 6166 | { |
0b86a832 | 6167 | struct btrfs_device *dev; |
8790d502 | 6168 | struct bio *first_bio = bio; |
4f024f37 | 6169 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
0b86a832 CM |
6170 | u64 length = 0; |
6171 | u64 map_length; | |
0b86a832 | 6172 | int ret; |
08da757d ZL |
6173 | int dev_nr; |
6174 | int total_devs; | |
a1d3c478 | 6175 | struct btrfs_bio *bbio = NULL; |
0b86a832 | 6176 | |
4f024f37 | 6177 | length = bio->bi_iter.bi_size; |
0b86a832 | 6178 | map_length = length; |
cea9e445 | 6179 | |
0b246afa | 6180 | btrfs_bio_counter_inc_blocked(fs_info); |
bd7d63c2 | 6181 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
37226b21 | 6182 | &map_length, &bbio, mirror_num, 1); |
c404e0dc | 6183 | if (ret) { |
0b246afa | 6184 | btrfs_bio_counter_dec(fs_info); |
58efbc9f | 6185 | return errno_to_blk_status(ret); |
c404e0dc | 6186 | } |
cea9e445 | 6187 | |
a1d3c478 | 6188 | total_devs = bbio->num_stripes; |
53b381b3 DW |
6189 | bbio->orig_bio = first_bio; |
6190 | bbio->private = first_bio->bi_private; | |
6191 | bbio->end_io = first_bio->bi_end_io; | |
0b246afa | 6192 | bbio->fs_info = fs_info; |
53b381b3 DW |
6193 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
6194 | ||
ad1ba2a0 | 6195 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
37226b21 | 6196 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
53b381b3 DW |
6197 | /* In this case, map_length has been set to the length of |
6198 | a single stripe; not the whole write */ | |
37226b21 | 6199 | if (bio_op(bio) == REQ_OP_WRITE) { |
2ff7e61e JM |
6200 | ret = raid56_parity_write(fs_info, bio, bbio, |
6201 | map_length); | |
53b381b3 | 6202 | } else { |
2ff7e61e JM |
6203 | ret = raid56_parity_recover(fs_info, bio, bbio, |
6204 | map_length, mirror_num, 1); | |
53b381b3 | 6205 | } |
4245215d | 6206 | |
0b246afa | 6207 | btrfs_bio_counter_dec(fs_info); |
58efbc9f | 6208 | return errno_to_blk_status(ret); |
53b381b3 DW |
6209 | } |
6210 | ||
cea9e445 | 6211 | if (map_length < length) { |
0b246afa | 6212 | btrfs_crit(fs_info, |
5d163e0e JM |
6213 | "mapping failed logical %llu bio len %llu len %llu", |
6214 | logical, length, map_length); | |
cea9e445 CM |
6215 | BUG(); |
6216 | } | |
a1d3c478 | 6217 | |
08da757d | 6218 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
de1ee92a | 6219 | dev = bbio->stripes[dev_nr].dev; |
37226b21 | 6220 | if (!dev || !dev->bdev || |
a967efb3 | 6221 | (bio_op(first_bio) == REQ_OP_WRITE && !dev->writeable)) { |
de1ee92a | 6222 | bbio_error(bbio, first_bio, logical); |
de1ee92a JB |
6223 | continue; |
6224 | } | |
6225 | ||
3aa8e074 | 6226 | if (dev_nr < total_devs - 1) |
8b6c1d56 | 6227 | bio = btrfs_bio_clone(first_bio); |
3aa8e074 | 6228 | else |
a1d3c478 | 6229 | bio = first_bio; |
de1ee92a | 6230 | |
2ff7e61e JM |
6231 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
6232 | dev_nr, async_submit); | |
8790d502 | 6233 | } |
0b246afa | 6234 | btrfs_bio_counter_dec(fs_info); |
58efbc9f | 6235 | return BLK_STS_OK; |
0b86a832 CM |
6236 | } |
6237 | ||
34857e15 AJ |
6238 | /* |
6239 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or | |
6240 | * return NULL. | |
6241 | * | |
6242 | * If devid and uuid are both specified, the match must be exact, otherwise | |
6243 | * only devid is used. | |
6244 | * | |
6245 | * If @seed is true, traverse through the seed devices. | |
6246 | */ | |
2f05c56f | 6247 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices, |
34857e15 AJ |
6248 | u64 devid, u8 *uuid, u8 *fsid, |
6249 | bool seed) | |
0b86a832 | 6250 | { |
2b82032c | 6251 | struct btrfs_device *device; |
2b82032c | 6252 | |
2f05c56f | 6253 | while (fs_devices) { |
2b82032c | 6254 | if (!fsid || |
34857e15 AJ |
6255 | !memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) { |
6256 | list_for_each_entry(device, &fs_devices->devices, | |
6257 | dev_list) { | |
6258 | if (device->devid == devid && | |
6259 | (!uuid || memcmp(device->uuid, uuid, | |
6260 | BTRFS_UUID_SIZE) == 0)) | |
6261 | return device; | |
6262 | } | |
2b82032c | 6263 | } |
34857e15 AJ |
6264 | if (seed) |
6265 | fs_devices = fs_devices->seed; | |
6266 | else | |
6267 | return NULL; | |
2b82032c YZ |
6268 | } |
6269 | return NULL; | |
0b86a832 CM |
6270 | } |
6271 | ||
2ff7e61e | 6272 | static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices, |
dfe25020 CM |
6273 | u64 devid, u8 *dev_uuid) |
6274 | { | |
6275 | struct btrfs_device *device; | |
dfe25020 | 6276 | |
12bd2fc0 ID |
6277 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
6278 | if (IS_ERR(device)) | |
adfb69af | 6279 | return device; |
12bd2fc0 ID |
6280 | |
6281 | list_add(&device->dev_list, &fs_devices->devices); | |
e4404d6e | 6282 | device->fs_devices = fs_devices; |
dfe25020 | 6283 | fs_devices->num_devices++; |
12bd2fc0 ID |
6284 | |
6285 | device->missing = 1; | |
cd02dca5 | 6286 | fs_devices->missing_devices++; |
12bd2fc0 | 6287 | |
dfe25020 CM |
6288 | return device; |
6289 | } | |
6290 | ||
12bd2fc0 ID |
6291 | /** |
6292 | * btrfs_alloc_device - allocate struct btrfs_device | |
6293 | * @fs_info: used only for generating a new devid, can be NULL if | |
6294 | * devid is provided (i.e. @devid != NULL). | |
6295 | * @devid: a pointer to devid for this device. If NULL a new devid | |
6296 | * is generated. | |
6297 | * @uuid: a pointer to UUID for this device. If NULL a new UUID | |
6298 | * is generated. | |
6299 | * | |
6300 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() | |
6301 | * on error. Returned struct is not linked onto any lists and can be | |
6302 | * destroyed with kfree() right away. | |
6303 | */ | |
6304 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, | |
6305 | const u64 *devid, | |
6306 | const u8 *uuid) | |
6307 | { | |
6308 | struct btrfs_device *dev; | |
6309 | u64 tmp; | |
6310 | ||
fae7f21c | 6311 | if (WARN_ON(!devid && !fs_info)) |
12bd2fc0 | 6312 | return ERR_PTR(-EINVAL); |
12bd2fc0 ID |
6313 | |
6314 | dev = __alloc_device(); | |
6315 | if (IS_ERR(dev)) | |
6316 | return dev; | |
6317 | ||
6318 | if (devid) | |
6319 | tmp = *devid; | |
6320 | else { | |
6321 | int ret; | |
6322 | ||
6323 | ret = find_next_devid(fs_info, &tmp); | |
6324 | if (ret) { | |
3065ae5b | 6325 | bio_put(dev->flush_bio); |
12bd2fc0 ID |
6326 | kfree(dev); |
6327 | return ERR_PTR(ret); | |
6328 | } | |
6329 | } | |
6330 | dev->devid = tmp; | |
6331 | ||
6332 | if (uuid) | |
6333 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); | |
6334 | else | |
6335 | generate_random_uuid(dev->uuid); | |
6336 | ||
9e0af237 LB |
6337 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
6338 | pending_bios_fn, NULL, NULL); | |
12bd2fc0 ID |
6339 | |
6340 | return dev; | |
6341 | } | |
6342 | ||
e06cd3dd | 6343 | /* Return -EIO if any error, otherwise return 0. */ |
2ff7e61e | 6344 | static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, |
e06cd3dd LB |
6345 | struct extent_buffer *leaf, |
6346 | struct btrfs_chunk *chunk, u64 logical) | |
0b86a832 | 6347 | { |
0b86a832 | 6348 | u64 length; |
f04b772b | 6349 | u64 stripe_len; |
e06cd3dd LB |
6350 | u16 num_stripes; |
6351 | u16 sub_stripes; | |
6352 | u64 type; | |
75d0186f GJ |
6353 | u64 features; |
6354 | bool mixed = false; | |
0b86a832 | 6355 | |
e17cade2 | 6356 | length = btrfs_chunk_length(leaf, chunk); |
f04b772b QW |
6357 | stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
6358 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); | |
e06cd3dd LB |
6359 | sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
6360 | type = btrfs_chunk_type(leaf, chunk); | |
6361 | ||
f04b772b | 6362 | if (!num_stripes) { |
0b246afa | 6363 | btrfs_err(fs_info, "invalid chunk num_stripes: %u", |
f04b772b QW |
6364 | num_stripes); |
6365 | return -EIO; | |
6366 | } | |
0b246afa JM |
6367 | if (!IS_ALIGNED(logical, fs_info->sectorsize)) { |
6368 | btrfs_err(fs_info, "invalid chunk logical %llu", logical); | |
f04b772b QW |
6369 | return -EIO; |
6370 | } | |
0b246afa JM |
6371 | if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) { |
6372 | btrfs_err(fs_info, "invalid chunk sectorsize %u", | |
e06cd3dd LB |
6373 | btrfs_chunk_sector_size(leaf, chunk)); |
6374 | return -EIO; | |
6375 | } | |
0b246afa JM |
6376 | if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) { |
6377 | btrfs_err(fs_info, "invalid chunk length %llu", length); | |
f04b772b QW |
6378 | return -EIO; |
6379 | } | |
3d8da678 | 6380 | if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) { |
0b246afa | 6381 | btrfs_err(fs_info, "invalid chunk stripe length: %llu", |
f04b772b QW |
6382 | stripe_len); |
6383 | return -EIO; | |
6384 | } | |
6385 | if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) & | |
e06cd3dd | 6386 | type) { |
0b246afa | 6387 | btrfs_err(fs_info, "unrecognized chunk type: %llu", |
f04b772b QW |
6388 | ~(BTRFS_BLOCK_GROUP_TYPE_MASK | |
6389 | BTRFS_BLOCK_GROUP_PROFILE_MASK) & | |
6390 | btrfs_chunk_type(leaf, chunk)); | |
6391 | return -EIO; | |
6392 | } | |
75d0186f GJ |
6393 | |
6394 | if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) { | |
6395 | btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type); | |
6396 | return -EIO; | |
6397 | } | |
6398 | ||
6399 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) && | |
6400 | (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) { | |
6401 | btrfs_err(fs_info, | |
6402 | "system chunk with data or metadata type: 0x%llx", type); | |
6403 | return -EIO; | |
6404 | } | |
6405 | ||
6406 | features = btrfs_super_incompat_flags(fs_info->super_copy); | |
6407 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) | |
6408 | mixed = true; | |
6409 | ||
6410 | if (!mixed) { | |
6411 | if ((type & BTRFS_BLOCK_GROUP_METADATA) && | |
6412 | (type & BTRFS_BLOCK_GROUP_DATA)) { | |
6413 | btrfs_err(fs_info, | |
6414 | "mixed chunk type in non-mixed mode: 0x%llx", type); | |
6415 | return -EIO; | |
6416 | } | |
6417 | } | |
6418 | ||
e06cd3dd | 6419 | if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) || |
72460cff | 6420 | (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) || |
e06cd3dd LB |
6421 | (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || |
6422 | (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || | |
72460cff | 6423 | (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || |
e06cd3dd LB |
6424 | ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && |
6425 | num_stripes != 1)) { | |
0b246afa | 6426 | btrfs_err(fs_info, |
e06cd3dd LB |
6427 | "invalid num_stripes:sub_stripes %u:%u for profile %llu", |
6428 | num_stripes, sub_stripes, | |
6429 | type & BTRFS_BLOCK_GROUP_PROFILE_MASK); | |
6430 | return -EIO; | |
6431 | } | |
6432 | ||
6433 | return 0; | |
6434 | } | |
6435 | ||
5a2b8e60 | 6436 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
2b902dfc | 6437 | u64 devid, u8 *uuid, bool error) |
5a2b8e60 | 6438 | { |
2b902dfc AJ |
6439 | if (error) |
6440 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", | |
6441 | devid, uuid); | |
6442 | else | |
6443 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", | |
6444 | devid, uuid); | |
5a2b8e60 AJ |
6445 | } |
6446 | ||
2ff7e61e | 6447 | static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key, |
e06cd3dd LB |
6448 | struct extent_buffer *leaf, |
6449 | struct btrfs_chunk *chunk) | |
6450 | { | |
0b246afa | 6451 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
e06cd3dd LB |
6452 | struct map_lookup *map; |
6453 | struct extent_map *em; | |
6454 | u64 logical; | |
6455 | u64 length; | |
e06cd3dd LB |
6456 | u64 devid; |
6457 | u8 uuid[BTRFS_UUID_SIZE]; | |
6458 | int num_stripes; | |
6459 | int ret; | |
6460 | int i; | |
6461 | ||
6462 | logical = key->offset; | |
6463 | length = btrfs_chunk_length(leaf, chunk); | |
e06cd3dd LB |
6464 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
6465 | ||
2ff7e61e | 6466 | ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical); |
e06cd3dd LB |
6467 | if (ret) |
6468 | return ret; | |
a061fc8d | 6469 | |
890871be | 6470 | read_lock(&map_tree->map_tree.lock); |
0b86a832 | 6471 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
890871be | 6472 | read_unlock(&map_tree->map_tree.lock); |
0b86a832 CM |
6473 | |
6474 | /* already mapped? */ | |
6475 | if (em && em->start <= logical && em->start + em->len > logical) { | |
6476 | free_extent_map(em); | |
0b86a832 CM |
6477 | return 0; |
6478 | } else if (em) { | |
6479 | free_extent_map(em); | |
6480 | } | |
0b86a832 | 6481 | |
172ddd60 | 6482 | em = alloc_extent_map(); |
0b86a832 CM |
6483 | if (!em) |
6484 | return -ENOMEM; | |
593060d7 | 6485 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
0b86a832 CM |
6486 | if (!map) { |
6487 | free_extent_map(em); | |
6488 | return -ENOMEM; | |
6489 | } | |
6490 | ||
298a8f9c | 6491 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
95617d69 | 6492 | em->map_lookup = map; |
0b86a832 CM |
6493 | em->start = logical; |
6494 | em->len = length; | |
70c8a91c | 6495 | em->orig_start = 0; |
0b86a832 | 6496 | em->block_start = 0; |
c8b97818 | 6497 | em->block_len = em->len; |
0b86a832 | 6498 | |
593060d7 CM |
6499 | map->num_stripes = num_stripes; |
6500 | map->io_width = btrfs_chunk_io_width(leaf, chunk); | |
6501 | map->io_align = btrfs_chunk_io_align(leaf, chunk); | |
593060d7 CM |
6502 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
6503 | map->type = btrfs_chunk_type(leaf, chunk); | |
321aecc6 | 6504 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
593060d7 CM |
6505 | for (i = 0; i < num_stripes; i++) { |
6506 | map->stripes[i].physical = | |
6507 | btrfs_stripe_offset_nr(leaf, chunk, i); | |
6508 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); | |
a443755f CM |
6509 | read_extent_buffer(leaf, uuid, (unsigned long) |
6510 | btrfs_stripe_dev_uuid_nr(chunk, i), | |
6511 | BTRFS_UUID_SIZE); | |
2f05c56f | 6512 | map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, |
34857e15 | 6513 | devid, uuid, NULL, true); |
3cdde224 | 6514 | if (!map->stripes[i].dev && |
0b246afa | 6515 | !btrfs_test_opt(fs_info, DEGRADED)) { |
593060d7 | 6516 | free_extent_map(em); |
2b902dfc | 6517 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
45dbdbc9 | 6518 | return -ENOENT; |
593060d7 | 6519 | } |
dfe25020 CM |
6520 | if (!map->stripes[i].dev) { |
6521 | map->stripes[i].dev = | |
2ff7e61e JM |
6522 | add_missing_dev(fs_info->fs_devices, devid, |
6523 | uuid); | |
adfb69af | 6524 | if (IS_ERR(map->stripes[i].dev)) { |
dfe25020 | 6525 | free_extent_map(em); |
adfb69af AJ |
6526 | btrfs_err(fs_info, |
6527 | "failed to init missing dev %llu: %ld", | |
6528 | devid, PTR_ERR(map->stripes[i].dev)); | |
6529 | return PTR_ERR(map->stripes[i].dev); | |
dfe25020 | 6530 | } |
2b902dfc | 6531 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
dfe25020 CM |
6532 | } |
6533 | map->stripes[i].dev->in_fs_metadata = 1; | |
0b86a832 CM |
6534 | } |
6535 | ||
890871be | 6536 | write_lock(&map_tree->map_tree.lock); |
09a2a8f9 | 6537 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
890871be | 6538 | write_unlock(&map_tree->map_tree.lock); |
0ede0a53 QW |
6539 | if (ret < 0) { |
6540 | btrfs_err(fs_info, | |
6541 | "failed to add chunk map, start=%llu len=%llu: %d", | |
6542 | em->start, em->len, ret); | |
6543 | } | |
0b86a832 CM |
6544 | free_extent_map(em); |
6545 | ||
0ede0a53 | 6546 | return ret; |
0b86a832 CM |
6547 | } |
6548 | ||
143bede5 | 6549 | static void fill_device_from_item(struct extent_buffer *leaf, |
0b86a832 CM |
6550 | struct btrfs_dev_item *dev_item, |
6551 | struct btrfs_device *device) | |
6552 | { | |
6553 | unsigned long ptr; | |
0b86a832 CM |
6554 | |
6555 | device->devid = btrfs_device_id(leaf, dev_item); | |
d6397bae CB |
6556 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
6557 | device->total_bytes = device->disk_total_bytes; | |
935e5cc9 | 6558 | device->commit_total_bytes = device->disk_total_bytes; |
0b86a832 | 6559 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
ce7213c7 | 6560 | device->commit_bytes_used = device->bytes_used; |
0b86a832 CM |
6561 | device->type = btrfs_device_type(leaf, dev_item); |
6562 | device->io_align = btrfs_device_io_align(leaf, dev_item); | |
6563 | device->io_width = btrfs_device_io_width(leaf, dev_item); | |
6564 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); | |
8dabb742 | 6565 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
63a212ab | 6566 | device->is_tgtdev_for_dev_replace = 0; |
0b86a832 | 6567 | |
410ba3a2 | 6568 | ptr = btrfs_device_uuid(dev_item); |
e17cade2 | 6569 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
0b86a832 CM |
6570 | } |
6571 | ||
2ff7e61e | 6572 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info, |
5f375835 | 6573 | u8 *fsid) |
2b82032c YZ |
6574 | { |
6575 | struct btrfs_fs_devices *fs_devices; | |
6576 | int ret; | |
6577 | ||
b367e47f | 6578 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
2dfeca9b | 6579 | ASSERT(fsid); |
2b82032c | 6580 | |
0b246afa | 6581 | fs_devices = fs_info->fs_devices->seed; |
2b82032c | 6582 | while (fs_devices) { |
44880fdc | 6583 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
5f375835 MX |
6584 | return fs_devices; |
6585 | ||
2b82032c YZ |
6586 | fs_devices = fs_devices->seed; |
6587 | } | |
6588 | ||
6589 | fs_devices = find_fsid(fsid); | |
6590 | if (!fs_devices) { | |
0b246afa | 6591 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
5f375835 MX |
6592 | return ERR_PTR(-ENOENT); |
6593 | ||
6594 | fs_devices = alloc_fs_devices(fsid); | |
6595 | if (IS_ERR(fs_devices)) | |
6596 | return fs_devices; | |
6597 | ||
6598 | fs_devices->seeding = 1; | |
6599 | fs_devices->opened = 1; | |
6600 | return fs_devices; | |
2b82032c | 6601 | } |
e4404d6e YZ |
6602 | |
6603 | fs_devices = clone_fs_devices(fs_devices); | |
5f375835 MX |
6604 | if (IS_ERR(fs_devices)) |
6605 | return fs_devices; | |
2b82032c | 6606 | |
97288f2c | 6607 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
0b246afa | 6608 | fs_info->bdev_holder); |
48d28232 JL |
6609 | if (ret) { |
6610 | free_fs_devices(fs_devices); | |
5f375835 | 6611 | fs_devices = ERR_PTR(ret); |
2b82032c | 6612 | goto out; |
48d28232 | 6613 | } |
2b82032c YZ |
6614 | |
6615 | if (!fs_devices->seeding) { | |
6616 | __btrfs_close_devices(fs_devices); | |
e4404d6e | 6617 | free_fs_devices(fs_devices); |
5f375835 | 6618 | fs_devices = ERR_PTR(-EINVAL); |
2b82032c YZ |
6619 | goto out; |
6620 | } | |
6621 | ||
0b246afa JM |
6622 | fs_devices->seed = fs_info->fs_devices->seed; |
6623 | fs_info->fs_devices->seed = fs_devices; | |
2b82032c | 6624 | out: |
5f375835 | 6625 | return fs_devices; |
2b82032c YZ |
6626 | } |
6627 | ||
2ff7e61e | 6628 | static int read_one_dev(struct btrfs_fs_info *fs_info, |
0b86a832 CM |
6629 | struct extent_buffer *leaf, |
6630 | struct btrfs_dev_item *dev_item) | |
6631 | { | |
0b246afa | 6632 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
0b86a832 CM |
6633 | struct btrfs_device *device; |
6634 | u64 devid; | |
6635 | int ret; | |
44880fdc | 6636 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
a443755f CM |
6637 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
6638 | ||
0b86a832 | 6639 | devid = btrfs_device_id(leaf, dev_item); |
410ba3a2 | 6640 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
a443755f | 6641 | BTRFS_UUID_SIZE); |
1473b24e | 6642 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
44880fdc | 6643 | BTRFS_FSID_SIZE); |
2b82032c | 6644 | |
44880fdc | 6645 | if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) { |
2ff7e61e | 6646 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
5f375835 MX |
6647 | if (IS_ERR(fs_devices)) |
6648 | return PTR_ERR(fs_devices); | |
2b82032c YZ |
6649 | } |
6650 | ||
2f05c56f | 6651 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
34857e15 | 6652 | fs_uuid, true); |
5f375835 | 6653 | if (!device) { |
c5502451 | 6654 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
2b902dfc AJ |
6655 | btrfs_report_missing_device(fs_info, devid, |
6656 | dev_uuid, true); | |
45dbdbc9 | 6657 | return -ENOENT; |
c5502451 | 6658 | } |
2b82032c | 6659 | |
2ff7e61e | 6660 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
adfb69af AJ |
6661 | if (IS_ERR(device)) { |
6662 | btrfs_err(fs_info, | |
6663 | "failed to add missing dev %llu: %ld", | |
6664 | devid, PTR_ERR(device)); | |
6665 | return PTR_ERR(device); | |
6666 | } | |
2b902dfc | 6667 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
5f375835 | 6668 | } else { |
c5502451 | 6669 | if (!device->bdev) { |
2b902dfc AJ |
6670 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
6671 | btrfs_report_missing_device(fs_info, | |
6672 | devid, dev_uuid, true); | |
45dbdbc9 | 6673 | return -ENOENT; |
2b902dfc AJ |
6674 | } |
6675 | btrfs_report_missing_device(fs_info, devid, | |
6676 | dev_uuid, false); | |
c5502451 | 6677 | } |
5f375835 MX |
6678 | |
6679 | if(!device->bdev && !device->missing) { | |
cd02dca5 CM |
6680 | /* |
6681 | * this happens when a device that was properly setup | |
6682 | * in the device info lists suddenly goes bad. | |
6683 | * device->bdev is NULL, and so we have to set | |
6684 | * device->missing to one here | |
6685 | */ | |
5f375835 | 6686 | device->fs_devices->missing_devices++; |
cd02dca5 | 6687 | device->missing = 1; |
2b82032c | 6688 | } |
5f375835 MX |
6689 | |
6690 | /* Move the device to its own fs_devices */ | |
6691 | if (device->fs_devices != fs_devices) { | |
6692 | ASSERT(device->missing); | |
6693 | ||
6694 | list_move(&device->dev_list, &fs_devices->devices); | |
6695 | device->fs_devices->num_devices--; | |
6696 | fs_devices->num_devices++; | |
6697 | ||
6698 | device->fs_devices->missing_devices--; | |
6699 | fs_devices->missing_devices++; | |
6700 | ||
6701 | device->fs_devices = fs_devices; | |
6702 | } | |
2b82032c YZ |
6703 | } |
6704 | ||
0b246afa | 6705 | if (device->fs_devices != fs_info->fs_devices) { |
2b82032c YZ |
6706 | BUG_ON(device->writeable); |
6707 | if (device->generation != | |
6708 | btrfs_device_generation(leaf, dev_item)) | |
6709 | return -EINVAL; | |
6324fbf3 | 6710 | } |
0b86a832 CM |
6711 | |
6712 | fill_device_from_item(leaf, dev_item, device); | |
dfe25020 | 6713 | device->in_fs_metadata = 1; |
63a212ab | 6714 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
2b82032c | 6715 | device->fs_devices->total_rw_bytes += device->total_bytes; |
a5ed45f8 NB |
6716 | atomic64_add(device->total_bytes - device->bytes_used, |
6717 | &fs_info->free_chunk_space); | |
2bf64758 | 6718 | } |
0b86a832 | 6719 | ret = 0; |
0b86a832 CM |
6720 | return ret; |
6721 | } | |
6722 | ||
6bccf3ab | 6723 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
0b86a832 | 6724 | { |
6bccf3ab | 6725 | struct btrfs_root *root = fs_info->tree_root; |
ab8d0fc4 | 6726 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
a061fc8d | 6727 | struct extent_buffer *sb; |
0b86a832 | 6728 | struct btrfs_disk_key *disk_key; |
0b86a832 | 6729 | struct btrfs_chunk *chunk; |
1ffb22cf DS |
6730 | u8 *array_ptr; |
6731 | unsigned long sb_array_offset; | |
84eed90f | 6732 | int ret = 0; |
0b86a832 CM |
6733 | u32 num_stripes; |
6734 | u32 array_size; | |
6735 | u32 len = 0; | |
1ffb22cf | 6736 | u32 cur_offset; |
e06cd3dd | 6737 | u64 type; |
84eed90f | 6738 | struct btrfs_key key; |
0b86a832 | 6739 | |
0b246afa | 6740 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
a83fffb7 DS |
6741 | /* |
6742 | * This will create extent buffer of nodesize, superblock size is | |
6743 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will | |
6744 | * overallocate but we can keep it as-is, only the first page is used. | |
6745 | */ | |
2ff7e61e | 6746 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
c871b0f2 LB |
6747 | if (IS_ERR(sb)) |
6748 | return PTR_ERR(sb); | |
4db8c528 | 6749 | set_extent_buffer_uptodate(sb); |
85d4e461 | 6750 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
8a334426 | 6751 | /* |
01327610 | 6752 | * The sb extent buffer is artificial and just used to read the system array. |
4db8c528 | 6753 | * set_extent_buffer_uptodate() call does not properly mark all it's |
8a334426 DS |
6754 | * pages up-to-date when the page is larger: extent does not cover the |
6755 | * whole page and consequently check_page_uptodate does not find all | |
6756 | * the page's extents up-to-date (the hole beyond sb), | |
6757 | * write_extent_buffer then triggers a WARN_ON. | |
6758 | * | |
6759 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, | |
6760 | * but sb spans only this function. Add an explicit SetPageUptodate call | |
6761 | * to silence the warning eg. on PowerPC 64. | |
6762 | */ | |
09cbfeaf | 6763 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
727011e0 | 6764 | SetPageUptodate(sb->pages[0]); |
4008c04a | 6765 | |
a061fc8d | 6766 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
0b86a832 CM |
6767 | array_size = btrfs_super_sys_array_size(super_copy); |
6768 | ||
1ffb22cf DS |
6769 | array_ptr = super_copy->sys_chunk_array; |
6770 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); | |
6771 | cur_offset = 0; | |
0b86a832 | 6772 | |
1ffb22cf DS |
6773 | while (cur_offset < array_size) { |
6774 | disk_key = (struct btrfs_disk_key *)array_ptr; | |
e3540eab DS |
6775 | len = sizeof(*disk_key); |
6776 | if (cur_offset + len > array_size) | |
6777 | goto out_short_read; | |
6778 | ||
0b86a832 CM |
6779 | btrfs_disk_key_to_cpu(&key, disk_key); |
6780 | ||
1ffb22cf DS |
6781 | array_ptr += len; |
6782 | sb_array_offset += len; | |
6783 | cur_offset += len; | |
0b86a832 | 6784 | |
0d81ba5d | 6785 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
1ffb22cf | 6786 | chunk = (struct btrfs_chunk *)sb_array_offset; |
e3540eab DS |
6787 | /* |
6788 | * At least one btrfs_chunk with one stripe must be | |
6789 | * present, exact stripe count check comes afterwards | |
6790 | */ | |
6791 | len = btrfs_chunk_item_size(1); | |
6792 | if (cur_offset + len > array_size) | |
6793 | goto out_short_read; | |
6794 | ||
6795 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); | |
f5cdedd7 | 6796 | if (!num_stripes) { |
ab8d0fc4 JM |
6797 | btrfs_err(fs_info, |
6798 | "invalid number of stripes %u in sys_array at offset %u", | |
f5cdedd7 DS |
6799 | num_stripes, cur_offset); |
6800 | ret = -EIO; | |
6801 | break; | |
6802 | } | |
6803 | ||
e06cd3dd LB |
6804 | type = btrfs_chunk_type(sb, chunk); |
6805 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { | |
ab8d0fc4 | 6806 | btrfs_err(fs_info, |
e06cd3dd LB |
6807 | "invalid chunk type %llu in sys_array at offset %u", |
6808 | type, cur_offset); | |
6809 | ret = -EIO; | |
6810 | break; | |
6811 | } | |
6812 | ||
e3540eab DS |
6813 | len = btrfs_chunk_item_size(num_stripes); |
6814 | if (cur_offset + len > array_size) | |
6815 | goto out_short_read; | |
6816 | ||
2ff7e61e | 6817 | ret = read_one_chunk(fs_info, &key, sb, chunk); |
84eed90f CM |
6818 | if (ret) |
6819 | break; | |
0b86a832 | 6820 | } else { |
ab8d0fc4 JM |
6821 | btrfs_err(fs_info, |
6822 | "unexpected item type %u in sys_array at offset %u", | |
6823 | (u32)key.type, cur_offset); | |
84eed90f CM |
6824 | ret = -EIO; |
6825 | break; | |
0b86a832 | 6826 | } |
1ffb22cf DS |
6827 | array_ptr += len; |
6828 | sb_array_offset += len; | |
6829 | cur_offset += len; | |
0b86a832 | 6830 | } |
d865177a | 6831 | clear_extent_buffer_uptodate(sb); |
1c8b5b6e | 6832 | free_extent_buffer_stale(sb); |
84eed90f | 6833 | return ret; |
e3540eab DS |
6834 | |
6835 | out_short_read: | |
ab8d0fc4 | 6836 | btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u", |
e3540eab | 6837 | len, cur_offset); |
d865177a | 6838 | clear_extent_buffer_uptodate(sb); |
1c8b5b6e | 6839 | free_extent_buffer_stale(sb); |
e3540eab | 6840 | return -EIO; |
0b86a832 CM |
6841 | } |
6842 | ||
21634a19 QW |
6843 | /* |
6844 | * Check if all chunks in the fs are OK for read-write degraded mount | |
6845 | * | |
6846 | * Return true if all chunks meet the minimal RW mount requirements. | |
6847 | * Return false if any chunk doesn't meet the minimal RW mount requirements. | |
6848 | */ | |
6849 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info) | |
6850 | { | |
6851 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; | |
6852 | struct extent_map *em; | |
6853 | u64 next_start = 0; | |
6854 | bool ret = true; | |
6855 | ||
6856 | read_lock(&map_tree->map_tree.lock); | |
6857 | em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1); | |
6858 | read_unlock(&map_tree->map_tree.lock); | |
6859 | /* No chunk at all? Return false anyway */ | |
6860 | if (!em) { | |
6861 | ret = false; | |
6862 | goto out; | |
6863 | } | |
6864 | while (em) { | |
6865 | struct map_lookup *map; | |
6866 | int missing = 0; | |
6867 | int max_tolerated; | |
6868 | int i; | |
6869 | ||
6870 | map = em->map_lookup; | |
6871 | max_tolerated = | |
6872 | btrfs_get_num_tolerated_disk_barrier_failures( | |
6873 | map->type); | |
6874 | for (i = 0; i < map->num_stripes; i++) { | |
6875 | struct btrfs_device *dev = map->stripes[i].dev; | |
6876 | ||
6877 | if (!dev || !dev->bdev || dev->missing || | |
6878 | dev->last_flush_error) | |
6879 | missing++; | |
6880 | } | |
6881 | if (missing > max_tolerated) { | |
6882 | btrfs_warn(fs_info, | |
6883 | "chunk %llu missing %d devices, max tolerance is %d for writeable mount", | |
6884 | em->start, missing, max_tolerated); | |
6885 | free_extent_map(em); | |
6886 | ret = false; | |
6887 | goto out; | |
6888 | } | |
6889 | next_start = extent_map_end(em); | |
6890 | free_extent_map(em); | |
6891 | ||
6892 | read_lock(&map_tree->map_tree.lock); | |
6893 | em = lookup_extent_mapping(&map_tree->map_tree, next_start, | |
6894 | (u64)(-1) - next_start); | |
6895 | read_unlock(&map_tree->map_tree.lock); | |
6896 | } | |
6897 | out: | |
6898 | return ret; | |
6899 | } | |
6900 | ||
5b4aacef | 6901 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
0b86a832 | 6902 | { |
5b4aacef | 6903 | struct btrfs_root *root = fs_info->chunk_root; |
0b86a832 CM |
6904 | struct btrfs_path *path; |
6905 | struct extent_buffer *leaf; | |
6906 | struct btrfs_key key; | |
6907 | struct btrfs_key found_key; | |
6908 | int ret; | |
6909 | int slot; | |
99e3ecfc | 6910 | u64 total_dev = 0; |
0b86a832 | 6911 | |
0b86a832 CM |
6912 | path = btrfs_alloc_path(); |
6913 | if (!path) | |
6914 | return -ENOMEM; | |
6915 | ||
b367e47f | 6916 | mutex_lock(&uuid_mutex); |
34441361 | 6917 | mutex_lock(&fs_info->chunk_mutex); |
b367e47f | 6918 | |
395927a9 FDBM |
6919 | /* |
6920 | * Read all device items, and then all the chunk items. All | |
6921 | * device items are found before any chunk item (their object id | |
6922 | * is smaller than the lowest possible object id for a chunk | |
6923 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). | |
0b86a832 CM |
6924 | */ |
6925 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
6926 | key.offset = 0; | |
6927 | key.type = 0; | |
0b86a832 | 6928 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
ab59381e ZL |
6929 | if (ret < 0) |
6930 | goto error; | |
d397712b | 6931 | while (1) { |
0b86a832 CM |
6932 | leaf = path->nodes[0]; |
6933 | slot = path->slots[0]; | |
6934 | if (slot >= btrfs_header_nritems(leaf)) { | |
6935 | ret = btrfs_next_leaf(root, path); | |
6936 | if (ret == 0) | |
6937 | continue; | |
6938 | if (ret < 0) | |
6939 | goto error; | |
6940 | break; | |
6941 | } | |
6942 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
395927a9 FDBM |
6943 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
6944 | struct btrfs_dev_item *dev_item; | |
6945 | dev_item = btrfs_item_ptr(leaf, slot, | |
0b86a832 | 6946 | struct btrfs_dev_item); |
2ff7e61e | 6947 | ret = read_one_dev(fs_info, leaf, dev_item); |
395927a9 FDBM |
6948 | if (ret) |
6949 | goto error; | |
99e3ecfc | 6950 | total_dev++; |
0b86a832 CM |
6951 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
6952 | struct btrfs_chunk *chunk; | |
6953 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); | |
2ff7e61e | 6954 | ret = read_one_chunk(fs_info, &found_key, leaf, chunk); |
2b82032c YZ |
6955 | if (ret) |
6956 | goto error; | |
0b86a832 CM |
6957 | } |
6958 | path->slots[0]++; | |
6959 | } | |
99e3ecfc LB |
6960 | |
6961 | /* | |
6962 | * After loading chunk tree, we've got all device information, | |
6963 | * do another round of validation checks. | |
6964 | */ | |
0b246afa JM |
6965 | if (total_dev != fs_info->fs_devices->total_devices) { |
6966 | btrfs_err(fs_info, | |
99e3ecfc | 6967 | "super_num_devices %llu mismatch with num_devices %llu found here", |
0b246afa | 6968 | btrfs_super_num_devices(fs_info->super_copy), |
99e3ecfc LB |
6969 | total_dev); |
6970 | ret = -EINVAL; | |
6971 | goto error; | |
6972 | } | |
0b246afa JM |
6973 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
6974 | fs_info->fs_devices->total_rw_bytes) { | |
6975 | btrfs_err(fs_info, | |
99e3ecfc | 6976 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
0b246afa JM |
6977 | btrfs_super_total_bytes(fs_info->super_copy), |
6978 | fs_info->fs_devices->total_rw_bytes); | |
99e3ecfc LB |
6979 | ret = -EINVAL; |
6980 | goto error; | |
6981 | } | |
0b86a832 CM |
6982 | ret = 0; |
6983 | error: | |
34441361 | 6984 | mutex_unlock(&fs_info->chunk_mutex); |
b367e47f LZ |
6985 | mutex_unlock(&uuid_mutex); |
6986 | ||
2b82032c | 6987 | btrfs_free_path(path); |
0b86a832 CM |
6988 | return ret; |
6989 | } | |
442a4f63 | 6990 | |
cb517eab MX |
6991 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
6992 | { | |
6993 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
6994 | struct btrfs_device *device; | |
6995 | ||
29cc83f6 LB |
6996 | while (fs_devices) { |
6997 | mutex_lock(&fs_devices->device_list_mutex); | |
6998 | list_for_each_entry(device, &fs_devices->devices, dev_list) | |
fb456252 | 6999 | device->fs_info = fs_info; |
29cc83f6 LB |
7000 | mutex_unlock(&fs_devices->device_list_mutex); |
7001 | ||
7002 | fs_devices = fs_devices->seed; | |
7003 | } | |
cb517eab MX |
7004 | } |
7005 | ||
733f4fbb SB |
7006 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
7007 | { | |
7008 | int i; | |
7009 | ||
7010 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7011 | btrfs_dev_stat_reset(dev, i); | |
7012 | } | |
7013 | ||
7014 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) | |
7015 | { | |
7016 | struct btrfs_key key; | |
7017 | struct btrfs_key found_key; | |
7018 | struct btrfs_root *dev_root = fs_info->dev_root; | |
7019 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
7020 | struct extent_buffer *eb; | |
7021 | int slot; | |
7022 | int ret = 0; | |
7023 | struct btrfs_device *device; | |
7024 | struct btrfs_path *path = NULL; | |
7025 | int i; | |
7026 | ||
7027 | path = btrfs_alloc_path(); | |
7028 | if (!path) { | |
7029 | ret = -ENOMEM; | |
7030 | goto out; | |
7031 | } | |
7032 | ||
7033 | mutex_lock(&fs_devices->device_list_mutex); | |
7034 | list_for_each_entry(device, &fs_devices->devices, dev_list) { | |
7035 | int item_size; | |
7036 | struct btrfs_dev_stats_item *ptr; | |
7037 | ||
242e2956 DS |
7038 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
7039 | key.type = BTRFS_PERSISTENT_ITEM_KEY; | |
733f4fbb SB |
7040 | key.offset = device->devid; |
7041 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); | |
7042 | if (ret) { | |
733f4fbb SB |
7043 | __btrfs_reset_dev_stats(device); |
7044 | device->dev_stats_valid = 1; | |
7045 | btrfs_release_path(path); | |
7046 | continue; | |
7047 | } | |
7048 | slot = path->slots[0]; | |
7049 | eb = path->nodes[0]; | |
7050 | btrfs_item_key_to_cpu(eb, &found_key, slot); | |
7051 | item_size = btrfs_item_size_nr(eb, slot); | |
7052 | ||
7053 | ptr = btrfs_item_ptr(eb, slot, | |
7054 | struct btrfs_dev_stats_item); | |
7055 | ||
7056 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { | |
7057 | if (item_size >= (1 + i) * sizeof(__le64)) | |
7058 | btrfs_dev_stat_set(device, i, | |
7059 | btrfs_dev_stats_value(eb, ptr, i)); | |
7060 | else | |
7061 | btrfs_dev_stat_reset(device, i); | |
7062 | } | |
7063 | ||
7064 | device->dev_stats_valid = 1; | |
7065 | btrfs_dev_stat_print_on_load(device); | |
7066 | btrfs_release_path(path); | |
7067 | } | |
7068 | mutex_unlock(&fs_devices->device_list_mutex); | |
7069 | ||
7070 | out: | |
7071 | btrfs_free_path(path); | |
7072 | return ret < 0 ? ret : 0; | |
7073 | } | |
7074 | ||
7075 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, | |
6bccf3ab | 7076 | struct btrfs_fs_info *fs_info, |
733f4fbb SB |
7077 | struct btrfs_device *device) |
7078 | { | |
6bccf3ab | 7079 | struct btrfs_root *dev_root = fs_info->dev_root; |
733f4fbb SB |
7080 | struct btrfs_path *path; |
7081 | struct btrfs_key key; | |
7082 | struct extent_buffer *eb; | |
7083 | struct btrfs_dev_stats_item *ptr; | |
7084 | int ret; | |
7085 | int i; | |
7086 | ||
242e2956 DS |
7087 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
7088 | key.type = BTRFS_PERSISTENT_ITEM_KEY; | |
733f4fbb SB |
7089 | key.offset = device->devid; |
7090 | ||
7091 | path = btrfs_alloc_path(); | |
fa252992 DS |
7092 | if (!path) |
7093 | return -ENOMEM; | |
733f4fbb SB |
7094 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
7095 | if (ret < 0) { | |
0b246afa | 7096 | btrfs_warn_in_rcu(fs_info, |
ecaeb14b | 7097 | "error %d while searching for dev_stats item for device %s", |
606686ee | 7098 | ret, rcu_str_deref(device->name)); |
733f4fbb SB |
7099 | goto out; |
7100 | } | |
7101 | ||
7102 | if (ret == 0 && | |
7103 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { | |
7104 | /* need to delete old one and insert a new one */ | |
7105 | ret = btrfs_del_item(trans, dev_root, path); | |
7106 | if (ret != 0) { | |
0b246afa | 7107 | btrfs_warn_in_rcu(fs_info, |
ecaeb14b | 7108 | "delete too small dev_stats item for device %s failed %d", |
606686ee | 7109 | rcu_str_deref(device->name), ret); |
733f4fbb SB |
7110 | goto out; |
7111 | } | |
7112 | ret = 1; | |
7113 | } | |
7114 | ||
7115 | if (ret == 1) { | |
7116 | /* need to insert a new item */ | |
7117 | btrfs_release_path(path); | |
7118 | ret = btrfs_insert_empty_item(trans, dev_root, path, | |
7119 | &key, sizeof(*ptr)); | |
7120 | if (ret < 0) { | |
0b246afa | 7121 | btrfs_warn_in_rcu(fs_info, |
ecaeb14b DS |
7122 | "insert dev_stats item for device %s failed %d", |
7123 | rcu_str_deref(device->name), ret); | |
733f4fbb SB |
7124 | goto out; |
7125 | } | |
7126 | } | |
7127 | ||
7128 | eb = path->nodes[0]; | |
7129 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); | |
7130 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7131 | btrfs_set_dev_stats_value(eb, ptr, i, | |
7132 | btrfs_dev_stat_read(device, i)); | |
7133 | btrfs_mark_buffer_dirty(eb); | |
7134 | ||
7135 | out: | |
7136 | btrfs_free_path(path); | |
7137 | return ret; | |
7138 | } | |
7139 | ||
7140 | /* | |
7141 | * called from commit_transaction. Writes all changed device stats to disk. | |
7142 | */ | |
7143 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, | |
7144 | struct btrfs_fs_info *fs_info) | |
7145 | { | |
733f4fbb SB |
7146 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
7147 | struct btrfs_device *device; | |
addc3fa7 | 7148 | int stats_cnt; |
733f4fbb SB |
7149 | int ret = 0; |
7150 | ||
7151 | mutex_lock(&fs_devices->device_list_mutex); | |
7152 | list_for_each_entry(device, &fs_devices->devices, dev_list) { | |
9ee59c3a NB |
7153 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
7154 | if (!device->dev_stats_valid || stats_cnt == 0) | |
733f4fbb SB |
7155 | continue; |
7156 | ||
9ee59c3a NB |
7157 | |
7158 | /* | |
7159 | * There is a LOAD-LOAD control dependency between the value of | |
7160 | * dev_stats_ccnt and updating the on-disk values which requires | |
7161 | * reading the in-memory counters. Such control dependencies | |
7162 | * require explicit read memory barriers. | |
7163 | * | |
7164 | * This memory barriers pairs with smp_mb__before_atomic in | |
7165 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full | |
7166 | * barrier implied by atomic_xchg in | |
7167 | * btrfs_dev_stats_read_and_reset | |
7168 | */ | |
7169 | smp_rmb(); | |
7170 | ||
6bccf3ab | 7171 | ret = update_dev_stat_item(trans, fs_info, device); |
733f4fbb | 7172 | if (!ret) |
addc3fa7 | 7173 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
733f4fbb SB |
7174 | } |
7175 | mutex_unlock(&fs_devices->device_list_mutex); | |
7176 | ||
7177 | return ret; | |
7178 | } | |
7179 | ||
442a4f63 SB |
7180 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
7181 | { | |
7182 | btrfs_dev_stat_inc(dev, index); | |
7183 | btrfs_dev_stat_print_on_error(dev); | |
7184 | } | |
7185 | ||
48a3b636 | 7186 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
442a4f63 | 7187 | { |
733f4fbb SB |
7188 | if (!dev->dev_stats_valid) |
7189 | return; | |
fb456252 | 7190 | btrfs_err_rl_in_rcu(dev->fs_info, |
b14af3b4 | 7191 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u", |
606686ee | 7192 | rcu_str_deref(dev->name), |
442a4f63 SB |
7193 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
7194 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), | |
7195 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), | |
efe120a0 FH |
7196 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
7197 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); | |
442a4f63 | 7198 | } |
c11d2c23 | 7199 | |
733f4fbb SB |
7200 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
7201 | { | |
a98cdb85 SB |
7202 | int i; |
7203 | ||
7204 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7205 | if (btrfs_dev_stat_read(dev, i) != 0) | |
7206 | break; | |
7207 | if (i == BTRFS_DEV_STAT_VALUES_MAX) | |
7208 | return; /* all values == 0, suppress message */ | |
7209 | ||
fb456252 | 7210 | btrfs_info_in_rcu(dev->fs_info, |
ecaeb14b | 7211 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u", |
606686ee | 7212 | rcu_str_deref(dev->name), |
733f4fbb SB |
7213 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
7214 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), | |
7215 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), | |
7216 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), | |
7217 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); | |
7218 | } | |
7219 | ||
2ff7e61e | 7220 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
b27f7c0c | 7221 | struct btrfs_ioctl_get_dev_stats *stats) |
c11d2c23 SB |
7222 | { |
7223 | struct btrfs_device *dev; | |
0b246afa | 7224 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
c11d2c23 SB |
7225 | int i; |
7226 | ||
7227 | mutex_lock(&fs_devices->device_list_mutex); | |
34857e15 AJ |
7228 | dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL, |
7229 | true); | |
c11d2c23 SB |
7230 | mutex_unlock(&fs_devices->device_list_mutex); |
7231 | ||
7232 | if (!dev) { | |
0b246afa | 7233 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
c11d2c23 | 7234 | return -ENODEV; |
733f4fbb | 7235 | } else if (!dev->dev_stats_valid) { |
0b246afa | 7236 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
733f4fbb | 7237 | return -ENODEV; |
b27f7c0c | 7238 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
c11d2c23 SB |
7239 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
7240 | if (stats->nr_items > i) | |
7241 | stats->values[i] = | |
7242 | btrfs_dev_stat_read_and_reset(dev, i); | |
7243 | else | |
7244 | btrfs_dev_stat_reset(dev, i); | |
7245 | } | |
7246 | } else { | |
7247 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) | |
7248 | if (stats->nr_items > i) | |
7249 | stats->values[i] = btrfs_dev_stat_read(dev, i); | |
7250 | } | |
7251 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) | |
7252 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; | |
7253 | return 0; | |
7254 | } | |
a8a6dab7 | 7255 | |
da353f6b | 7256 | void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path) |
a8a6dab7 SB |
7257 | { |
7258 | struct buffer_head *bh; | |
7259 | struct btrfs_super_block *disk_super; | |
12b1c263 | 7260 | int copy_num; |
a8a6dab7 | 7261 | |
12b1c263 AJ |
7262 | if (!bdev) |
7263 | return; | |
a8a6dab7 | 7264 | |
12b1c263 AJ |
7265 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
7266 | copy_num++) { | |
a8a6dab7 | 7267 | |
12b1c263 AJ |
7268 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
7269 | continue; | |
7270 | ||
7271 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
7272 | ||
7273 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); | |
7274 | set_buffer_dirty(bh); | |
7275 | sync_dirty_buffer(bh); | |
7276 | brelse(bh); | |
7277 | } | |
7278 | ||
7279 | /* Notify udev that device has changed */ | |
7280 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); | |
7281 | ||
7282 | /* Update ctime/mtime for device path for libblkid */ | |
7283 | update_dev_time(device_path); | |
a8a6dab7 | 7284 | } |
935e5cc9 MX |
7285 | |
7286 | /* | |
7287 | * Update the size of all devices, which is used for writing out the | |
7288 | * super blocks. | |
7289 | */ | |
7290 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) | |
7291 | { | |
7292 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
7293 | struct btrfs_device *curr, *next; | |
7294 | ||
7295 | if (list_empty(&fs_devices->resized_devices)) | |
7296 | return; | |
7297 | ||
7298 | mutex_lock(&fs_devices->device_list_mutex); | |
34441361 | 7299 | mutex_lock(&fs_info->chunk_mutex); |
935e5cc9 MX |
7300 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
7301 | resized_list) { | |
7302 | list_del_init(&curr->resized_list); | |
7303 | curr->commit_total_bytes = curr->disk_total_bytes; | |
7304 | } | |
34441361 | 7305 | mutex_unlock(&fs_info->chunk_mutex); |
935e5cc9 MX |
7306 | mutex_unlock(&fs_devices->device_list_mutex); |
7307 | } | |
ce7213c7 MX |
7308 | |
7309 | /* Must be invoked during the transaction commit */ | |
2ff7e61e | 7310 | void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info, |
ce7213c7 MX |
7311 | struct btrfs_transaction *transaction) |
7312 | { | |
7313 | struct extent_map *em; | |
7314 | struct map_lookup *map; | |
7315 | struct btrfs_device *dev; | |
7316 | int i; | |
7317 | ||
7318 | if (list_empty(&transaction->pending_chunks)) | |
7319 | return; | |
7320 | ||
7321 | /* In order to kick the device replace finish process */ | |
34441361 | 7322 | mutex_lock(&fs_info->chunk_mutex); |
ce7213c7 | 7323 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
95617d69 | 7324 | map = em->map_lookup; |
ce7213c7 MX |
7325 | |
7326 | for (i = 0; i < map->num_stripes; i++) { | |
7327 | dev = map->stripes[i].dev; | |
7328 | dev->commit_bytes_used = dev->bytes_used; | |
8217e0f8 | 7329 | dev->has_pending_chunks = false; |
ce7213c7 MX |
7330 | } |
7331 | } | |
34441361 | 7332 | mutex_unlock(&fs_info->chunk_mutex); |
ce7213c7 | 7333 | } |
5a13f430 AJ |
7334 | |
7335 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) | |
7336 | { | |
7337 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
7338 | while (fs_devices) { | |
7339 | fs_devices->fs_info = fs_info; | |
7340 | fs_devices = fs_devices->seed; | |
7341 | } | |
7342 | } | |
7343 | ||
7344 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) | |
7345 | { | |
7346 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; | |
7347 | while (fs_devices) { | |
7348 | fs_devices->fs_info = NULL; | |
7349 | fs_devices = fs_devices->seed; | |
7350 | } | |
7351 | } |