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