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