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