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