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