]>
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> | |
8a4b83cc | 20 | #include <linux/buffer_head.h> |
f2d8d74d | 21 | #include <linux/blkdev.h> |
788f20eb | 22 | #include <linux/random.h> |
b765ead5 | 23 | #include <linux/iocontext.h> |
593060d7 | 24 | #include <asm/div64.h> |
4b4e25f2 | 25 | #include "compat.h" |
0b86a832 CM |
26 | #include "ctree.h" |
27 | #include "extent_map.h" | |
28 | #include "disk-io.h" | |
29 | #include "transaction.h" | |
30 | #include "print-tree.h" | |
31 | #include "volumes.h" | |
8b712842 | 32 | #include "async-thread.h" |
0b86a832 | 33 | |
593060d7 CM |
34 | struct map_lookup { |
35 | u64 type; | |
36 | int io_align; | |
37 | int io_width; | |
38 | int stripe_len; | |
39 | int sector_size; | |
40 | int num_stripes; | |
321aecc6 | 41 | int sub_stripes; |
cea9e445 | 42 | struct btrfs_bio_stripe stripes[]; |
593060d7 CM |
43 | }; |
44 | ||
2b82032c YZ |
45 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
46 | struct btrfs_root *root, | |
47 | struct btrfs_device *device); | |
48 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); | |
49 | ||
593060d7 | 50 | #define map_lookup_size(n) (sizeof(struct map_lookup) + \ |
cea9e445 | 51 | (sizeof(struct btrfs_bio_stripe) * (n))) |
593060d7 | 52 | |
8a4b83cc CM |
53 | static DEFINE_MUTEX(uuid_mutex); |
54 | static LIST_HEAD(fs_uuids); | |
55 | ||
a061fc8d CM |
56 | void btrfs_lock_volumes(void) |
57 | { | |
58 | mutex_lock(&uuid_mutex); | |
59 | } | |
60 | ||
61 | void btrfs_unlock_volumes(void) | |
62 | { | |
63 | mutex_unlock(&uuid_mutex); | |
64 | } | |
65 | ||
7d9eb12c CM |
66 | static void lock_chunks(struct btrfs_root *root) |
67 | { | |
7d9eb12c CM |
68 | mutex_lock(&root->fs_info->chunk_mutex); |
69 | } | |
70 | ||
71 | static void unlock_chunks(struct btrfs_root *root) | |
72 | { | |
7d9eb12c CM |
73 | mutex_unlock(&root->fs_info->chunk_mutex); |
74 | } | |
75 | ||
e4404d6e YZ |
76 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
77 | { | |
78 | struct btrfs_device *device; | |
79 | WARN_ON(fs_devices->opened); | |
80 | while (!list_empty(&fs_devices->devices)) { | |
81 | device = list_entry(fs_devices->devices.next, | |
82 | struct btrfs_device, dev_list); | |
83 | list_del(&device->dev_list); | |
84 | kfree(device->name); | |
85 | kfree(device); | |
86 | } | |
87 | kfree(fs_devices); | |
88 | } | |
89 | ||
8a4b83cc CM |
90 | int btrfs_cleanup_fs_uuids(void) |
91 | { | |
92 | struct btrfs_fs_devices *fs_devices; | |
8a4b83cc | 93 | |
2b82032c YZ |
94 | while (!list_empty(&fs_uuids)) { |
95 | fs_devices = list_entry(fs_uuids.next, | |
96 | struct btrfs_fs_devices, list); | |
97 | list_del(&fs_devices->list); | |
e4404d6e | 98 | free_fs_devices(fs_devices); |
8a4b83cc CM |
99 | } |
100 | return 0; | |
101 | } | |
102 | ||
a1b32a59 CM |
103 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
104 | u64 devid, u8 *uuid) | |
8a4b83cc CM |
105 | { |
106 | struct btrfs_device *dev; | |
8a4b83cc | 107 | |
c6e30871 | 108 | list_for_each_entry(dev, head, dev_list) { |
a443755f | 109 | if (dev->devid == devid && |
8f18cf13 | 110 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
8a4b83cc | 111 | return dev; |
a443755f | 112 | } |
8a4b83cc CM |
113 | } |
114 | return NULL; | |
115 | } | |
116 | ||
a1b32a59 | 117 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
8a4b83cc | 118 | { |
8a4b83cc CM |
119 | struct btrfs_fs_devices *fs_devices; |
120 | ||
c6e30871 | 121 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
8a4b83cc CM |
122 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
123 | return fs_devices; | |
124 | } | |
125 | return NULL; | |
126 | } | |
127 | ||
ffbd517d CM |
128 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
129 | struct bio *head, struct bio *tail) | |
130 | { | |
131 | ||
132 | struct bio *old_head; | |
133 | ||
134 | old_head = pending_bios->head; | |
135 | pending_bios->head = head; | |
136 | if (pending_bios->tail) | |
137 | tail->bi_next = old_head; | |
138 | else | |
139 | pending_bios->tail = tail; | |
140 | } | |
141 | ||
8b712842 CM |
142 | /* |
143 | * we try to collect pending bios for a device so we don't get a large | |
144 | * number of procs sending bios down to the same device. This greatly | |
145 | * improves the schedulers ability to collect and merge the bios. | |
146 | * | |
147 | * But, it also turns into a long list of bios to process and that is sure | |
148 | * to eventually make the worker thread block. The solution here is to | |
149 | * make some progress and then put this work struct back at the end of | |
150 | * the list if the block device is congested. This way, multiple devices | |
151 | * can make progress from a single worker thread. | |
152 | */ | |
d397712b | 153 | static noinline int run_scheduled_bios(struct btrfs_device *device) |
8b712842 CM |
154 | { |
155 | struct bio *pending; | |
156 | struct backing_dev_info *bdi; | |
b64a2851 | 157 | struct btrfs_fs_info *fs_info; |
ffbd517d | 158 | struct btrfs_pending_bios *pending_bios; |
8b712842 CM |
159 | struct bio *tail; |
160 | struct bio *cur; | |
161 | int again = 0; | |
ffbd517d CM |
162 | unsigned long num_run; |
163 | unsigned long num_sync_run; | |
d644d8a1 | 164 | unsigned long batch_run = 0; |
b64a2851 | 165 | unsigned long limit; |
b765ead5 | 166 | unsigned long last_waited = 0; |
d84275c9 | 167 | int force_reg = 0; |
8b712842 | 168 | |
bedf762b | 169 | bdi = blk_get_backing_dev_info(device->bdev); |
b64a2851 CM |
170 | fs_info = device->dev_root->fs_info; |
171 | limit = btrfs_async_submit_limit(fs_info); | |
172 | limit = limit * 2 / 3; | |
173 | ||
ffbd517d CM |
174 | /* we want to make sure that every time we switch from the sync |
175 | * list to the normal list, we unplug | |
176 | */ | |
177 | num_sync_run = 0; | |
178 | ||
8b712842 CM |
179 | loop: |
180 | spin_lock(&device->io_lock); | |
181 | ||
a6837051 | 182 | loop_lock: |
d84275c9 | 183 | num_run = 0; |
ffbd517d | 184 | |
8b712842 CM |
185 | /* take all the bios off the list at once and process them |
186 | * later on (without the lock held). But, remember the | |
187 | * tail and other pointers so the bios can be properly reinserted | |
188 | * into the list if we hit congestion | |
189 | */ | |
d84275c9 | 190 | if (!force_reg && device->pending_sync_bios.head) { |
ffbd517d | 191 | pending_bios = &device->pending_sync_bios; |
d84275c9 CM |
192 | force_reg = 1; |
193 | } else { | |
ffbd517d | 194 | pending_bios = &device->pending_bios; |
d84275c9 CM |
195 | force_reg = 0; |
196 | } | |
ffbd517d CM |
197 | |
198 | pending = pending_bios->head; | |
199 | tail = pending_bios->tail; | |
8b712842 | 200 | WARN_ON(pending && !tail); |
8b712842 CM |
201 | |
202 | /* | |
203 | * if pending was null this time around, no bios need processing | |
204 | * at all and we can stop. Otherwise it'll loop back up again | |
205 | * and do an additional check so no bios are missed. | |
206 | * | |
207 | * device->running_pending is used to synchronize with the | |
208 | * schedule_bio code. | |
209 | */ | |
ffbd517d CM |
210 | if (device->pending_sync_bios.head == NULL && |
211 | device->pending_bios.head == NULL) { | |
8b712842 CM |
212 | again = 0; |
213 | device->running_pending = 0; | |
ffbd517d CM |
214 | } else { |
215 | again = 1; | |
216 | device->running_pending = 1; | |
8b712842 | 217 | } |
ffbd517d CM |
218 | |
219 | pending_bios->head = NULL; | |
220 | pending_bios->tail = NULL; | |
221 | ||
8b712842 CM |
222 | spin_unlock(&device->io_lock); |
223 | ||
ffbd517d CM |
224 | /* |
225 | * if we're doing the regular priority list, make sure we unplug | |
226 | * for any high prio bios we've sent down | |
227 | */ | |
228 | if (pending_bios == &device->pending_bios && num_sync_run > 0) { | |
229 | num_sync_run = 0; | |
230 | blk_run_backing_dev(bdi, NULL); | |
231 | } | |
232 | ||
d397712b | 233 | while (pending) { |
ffbd517d CM |
234 | |
235 | rmb(); | |
d84275c9 CM |
236 | /* we want to work on both lists, but do more bios on the |
237 | * sync list than the regular list | |
238 | */ | |
239 | if ((num_run > 32 && | |
240 | pending_bios != &device->pending_sync_bios && | |
241 | device->pending_sync_bios.head) || | |
242 | (num_run > 64 && pending_bios == &device->pending_sync_bios && | |
243 | device->pending_bios.head)) { | |
ffbd517d CM |
244 | spin_lock(&device->io_lock); |
245 | requeue_list(pending_bios, pending, tail); | |
246 | goto loop_lock; | |
247 | } | |
248 | ||
8b712842 CM |
249 | cur = pending; |
250 | pending = pending->bi_next; | |
251 | cur->bi_next = NULL; | |
b64a2851 CM |
252 | atomic_dec(&fs_info->nr_async_bios); |
253 | ||
254 | if (atomic_read(&fs_info->nr_async_bios) < limit && | |
255 | waitqueue_active(&fs_info->async_submit_wait)) | |
256 | wake_up(&fs_info->async_submit_wait); | |
492bb6de CM |
257 | |
258 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); | |
8b712842 CM |
259 | submit_bio(cur->bi_rw, cur); |
260 | num_run++; | |
d644d8a1 CM |
261 | batch_run++; |
262 | ||
ffbd517d CM |
263 | if (bio_sync(cur)) |
264 | num_sync_run++; | |
265 | ||
266 | if (need_resched()) { | |
267 | if (num_sync_run) { | |
268 | blk_run_backing_dev(bdi, NULL); | |
269 | num_sync_run = 0; | |
270 | } | |
271 | cond_resched(); | |
272 | } | |
8b712842 CM |
273 | |
274 | /* | |
275 | * we made progress, there is more work to do and the bdi | |
276 | * is now congested. Back off and let other work structs | |
277 | * run instead | |
278 | */ | |
d644d8a1 | 279 | if (pending && bdi_write_congested(bdi) && batch_run > 32 && |
5f2cc086 | 280 | fs_info->fs_devices->open_devices > 1) { |
b765ead5 | 281 | struct io_context *ioc; |
8b712842 | 282 | |
b765ead5 CM |
283 | ioc = current->io_context; |
284 | ||
285 | /* | |
286 | * the main goal here is that we don't want to | |
287 | * block if we're going to be able to submit | |
288 | * more requests without blocking. | |
289 | * | |
290 | * This code does two great things, it pokes into | |
291 | * the elevator code from a filesystem _and_ | |
292 | * it makes assumptions about how batching works. | |
293 | */ | |
294 | if (ioc && ioc->nr_batch_requests > 0 && | |
295 | time_before(jiffies, ioc->last_waited + HZ/50UL) && | |
296 | (last_waited == 0 || | |
297 | ioc->last_waited == last_waited)) { | |
298 | /* | |
299 | * we want to go through our batch of | |
300 | * requests and stop. So, we copy out | |
301 | * the ioc->last_waited time and test | |
302 | * against it before looping | |
303 | */ | |
304 | last_waited = ioc->last_waited; | |
ffbd517d CM |
305 | if (need_resched()) { |
306 | if (num_sync_run) { | |
307 | blk_run_backing_dev(bdi, NULL); | |
308 | num_sync_run = 0; | |
309 | } | |
310 | cond_resched(); | |
311 | } | |
b765ead5 CM |
312 | continue; |
313 | } | |
8b712842 | 314 | spin_lock(&device->io_lock); |
ffbd517d | 315 | requeue_list(pending_bios, pending, tail); |
a6837051 | 316 | device->running_pending = 1; |
8b712842 CM |
317 | |
318 | spin_unlock(&device->io_lock); | |
319 | btrfs_requeue_work(&device->work); | |
320 | goto done; | |
321 | } | |
322 | } | |
ffbd517d CM |
323 | |
324 | if (num_sync_run) { | |
325 | num_sync_run = 0; | |
326 | blk_run_backing_dev(bdi, NULL); | |
327 | } | |
328 | ||
329 | cond_resched(); | |
8b712842 CM |
330 | if (again) |
331 | goto loop; | |
a6837051 CM |
332 | |
333 | spin_lock(&device->io_lock); | |
ffbd517d | 334 | if (device->pending_bios.head || device->pending_sync_bios.head) |
a6837051 CM |
335 | goto loop_lock; |
336 | spin_unlock(&device->io_lock); | |
bedf762b CM |
337 | |
338 | /* | |
339 | * IO has already been through a long path to get here. Checksumming, | |
340 | * async helper threads, perhaps compression. We've done a pretty | |
341 | * good job of collecting a batch of IO and should just unplug | |
342 | * the device right away. | |
343 | * | |
344 | * This will help anyone who is waiting on the IO, they might have | |
345 | * already unplugged, but managed to do so before the bio they | |
346 | * cared about found its way down here. | |
347 | */ | |
348 | blk_run_backing_dev(bdi, NULL); | |
8b712842 CM |
349 | done: |
350 | return 0; | |
351 | } | |
352 | ||
b2950863 | 353 | static void pending_bios_fn(struct btrfs_work *work) |
8b712842 CM |
354 | { |
355 | struct btrfs_device *device; | |
356 | ||
357 | device = container_of(work, struct btrfs_device, work); | |
358 | run_scheduled_bios(device); | |
359 | } | |
360 | ||
a1b32a59 | 361 | static noinline int device_list_add(const char *path, |
8a4b83cc CM |
362 | struct btrfs_super_block *disk_super, |
363 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) | |
364 | { | |
365 | struct btrfs_device *device; | |
366 | struct btrfs_fs_devices *fs_devices; | |
367 | u64 found_transid = btrfs_super_generation(disk_super); | |
368 | ||
369 | fs_devices = find_fsid(disk_super->fsid); | |
370 | if (!fs_devices) { | |
515dc322 | 371 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
8a4b83cc CM |
372 | if (!fs_devices) |
373 | return -ENOMEM; | |
374 | INIT_LIST_HEAD(&fs_devices->devices); | |
b3075717 | 375 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
8a4b83cc CM |
376 | list_add(&fs_devices->list, &fs_uuids); |
377 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); | |
378 | fs_devices->latest_devid = devid; | |
379 | fs_devices->latest_trans = found_transid; | |
8a4b83cc CM |
380 | device = NULL; |
381 | } else { | |
a443755f CM |
382 | device = __find_device(&fs_devices->devices, devid, |
383 | disk_super->dev_item.uuid); | |
8a4b83cc CM |
384 | } |
385 | if (!device) { | |
2b82032c YZ |
386 | if (fs_devices->opened) |
387 | return -EBUSY; | |
388 | ||
8a4b83cc CM |
389 | device = kzalloc(sizeof(*device), GFP_NOFS); |
390 | if (!device) { | |
391 | /* we can safely leave the fs_devices entry around */ | |
392 | return -ENOMEM; | |
393 | } | |
394 | device->devid = devid; | |
8b712842 | 395 | device->work.func = pending_bios_fn; |
a443755f CM |
396 | memcpy(device->uuid, disk_super->dev_item.uuid, |
397 | BTRFS_UUID_SIZE); | |
f2984462 | 398 | device->barriers = 1; |
b248a415 | 399 | spin_lock_init(&device->io_lock); |
8a4b83cc CM |
400 | device->name = kstrdup(path, GFP_NOFS); |
401 | if (!device->name) { | |
402 | kfree(device); | |
403 | return -ENOMEM; | |
404 | } | |
2b82032c | 405 | INIT_LIST_HEAD(&device->dev_alloc_list); |
8a4b83cc | 406 | list_add(&device->dev_list, &fs_devices->devices); |
2b82032c | 407 | device->fs_devices = fs_devices; |
8a4b83cc CM |
408 | fs_devices->num_devices++; |
409 | } | |
410 | ||
411 | if (found_transid > fs_devices->latest_trans) { | |
412 | fs_devices->latest_devid = devid; | |
413 | fs_devices->latest_trans = found_transid; | |
414 | } | |
8a4b83cc CM |
415 | *fs_devices_ret = fs_devices; |
416 | return 0; | |
417 | } | |
418 | ||
e4404d6e YZ |
419 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
420 | { | |
421 | struct btrfs_fs_devices *fs_devices; | |
422 | struct btrfs_device *device; | |
423 | struct btrfs_device *orig_dev; | |
424 | ||
425 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); | |
426 | if (!fs_devices) | |
427 | return ERR_PTR(-ENOMEM); | |
428 | ||
429 | INIT_LIST_HEAD(&fs_devices->devices); | |
430 | INIT_LIST_HEAD(&fs_devices->alloc_list); | |
431 | INIT_LIST_HEAD(&fs_devices->list); | |
432 | fs_devices->latest_devid = orig->latest_devid; | |
433 | fs_devices->latest_trans = orig->latest_trans; | |
434 | memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid)); | |
435 | ||
436 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { | |
437 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
438 | if (!device) | |
439 | goto error; | |
440 | ||
441 | device->name = kstrdup(orig_dev->name, GFP_NOFS); | |
442 | if (!device->name) | |
443 | goto error; | |
444 | ||
445 | device->devid = orig_dev->devid; | |
446 | device->work.func = pending_bios_fn; | |
447 | memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid)); | |
448 | device->barriers = 1; | |
449 | spin_lock_init(&device->io_lock); | |
450 | INIT_LIST_HEAD(&device->dev_list); | |
451 | INIT_LIST_HEAD(&device->dev_alloc_list); | |
452 | ||
453 | list_add(&device->dev_list, &fs_devices->devices); | |
454 | device->fs_devices = fs_devices; | |
455 | fs_devices->num_devices++; | |
456 | } | |
457 | return fs_devices; | |
458 | error: | |
459 | free_fs_devices(fs_devices); | |
460 | return ERR_PTR(-ENOMEM); | |
461 | } | |
462 | ||
dfe25020 CM |
463 | int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices) |
464 | { | |
c6e30871 | 465 | struct btrfs_device *device, *next; |
dfe25020 CM |
466 | |
467 | mutex_lock(&uuid_mutex); | |
468 | again: | |
c6e30871 | 469 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
2b82032c YZ |
470 | if (device->in_fs_metadata) |
471 | continue; | |
472 | ||
473 | if (device->bdev) { | |
15916de8 | 474 | close_bdev_exclusive(device->bdev, device->mode); |
2b82032c YZ |
475 | device->bdev = NULL; |
476 | fs_devices->open_devices--; | |
477 | } | |
478 | if (device->writeable) { | |
479 | list_del_init(&device->dev_alloc_list); | |
480 | device->writeable = 0; | |
481 | fs_devices->rw_devices--; | |
482 | } | |
e4404d6e YZ |
483 | list_del_init(&device->dev_list); |
484 | fs_devices->num_devices--; | |
485 | kfree(device->name); | |
486 | kfree(device); | |
dfe25020 | 487 | } |
2b82032c YZ |
488 | |
489 | if (fs_devices->seed) { | |
490 | fs_devices = fs_devices->seed; | |
2b82032c YZ |
491 | goto again; |
492 | } | |
493 | ||
dfe25020 CM |
494 | mutex_unlock(&uuid_mutex); |
495 | return 0; | |
496 | } | |
a0af469b | 497 | |
2b82032c | 498 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
8a4b83cc | 499 | { |
8a4b83cc | 500 | struct btrfs_device *device; |
e4404d6e | 501 | |
2b82032c YZ |
502 | if (--fs_devices->opened > 0) |
503 | return 0; | |
8a4b83cc | 504 | |
c6e30871 | 505 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
8a4b83cc | 506 | if (device->bdev) { |
15916de8 | 507 | close_bdev_exclusive(device->bdev, device->mode); |
a0af469b | 508 | fs_devices->open_devices--; |
8a4b83cc | 509 | } |
2b82032c YZ |
510 | if (device->writeable) { |
511 | list_del_init(&device->dev_alloc_list); | |
512 | fs_devices->rw_devices--; | |
513 | } | |
514 | ||
8a4b83cc | 515 | device->bdev = NULL; |
2b82032c | 516 | device->writeable = 0; |
dfe25020 | 517 | device->in_fs_metadata = 0; |
8a4b83cc | 518 | } |
e4404d6e YZ |
519 | WARN_ON(fs_devices->open_devices); |
520 | WARN_ON(fs_devices->rw_devices); | |
2b82032c YZ |
521 | fs_devices->opened = 0; |
522 | fs_devices->seeding = 0; | |
2b82032c | 523 | |
8a4b83cc CM |
524 | return 0; |
525 | } | |
526 | ||
2b82032c YZ |
527 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
528 | { | |
e4404d6e | 529 | struct btrfs_fs_devices *seed_devices = NULL; |
2b82032c YZ |
530 | int ret; |
531 | ||
532 | mutex_lock(&uuid_mutex); | |
533 | ret = __btrfs_close_devices(fs_devices); | |
e4404d6e YZ |
534 | if (!fs_devices->opened) { |
535 | seed_devices = fs_devices->seed; | |
536 | fs_devices->seed = NULL; | |
537 | } | |
2b82032c | 538 | mutex_unlock(&uuid_mutex); |
e4404d6e YZ |
539 | |
540 | while (seed_devices) { | |
541 | fs_devices = seed_devices; | |
542 | seed_devices = fs_devices->seed; | |
543 | __btrfs_close_devices(fs_devices); | |
544 | free_fs_devices(fs_devices); | |
545 | } | |
2b82032c YZ |
546 | return ret; |
547 | } | |
548 | ||
e4404d6e YZ |
549 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
550 | fmode_t flags, void *holder) | |
8a4b83cc CM |
551 | { |
552 | struct block_device *bdev; | |
553 | struct list_head *head = &fs_devices->devices; | |
8a4b83cc | 554 | struct btrfs_device *device; |
a0af469b CM |
555 | struct block_device *latest_bdev = NULL; |
556 | struct buffer_head *bh; | |
557 | struct btrfs_super_block *disk_super; | |
558 | u64 latest_devid = 0; | |
559 | u64 latest_transid = 0; | |
a0af469b | 560 | u64 devid; |
2b82032c | 561 | int seeding = 1; |
a0af469b | 562 | int ret = 0; |
8a4b83cc | 563 | |
c6e30871 | 564 | list_for_each_entry(device, head, dev_list) { |
c1c4d91c CM |
565 | if (device->bdev) |
566 | continue; | |
dfe25020 CM |
567 | if (!device->name) |
568 | continue; | |
569 | ||
15916de8 | 570 | bdev = open_bdev_exclusive(device->name, flags, holder); |
8a4b83cc | 571 | if (IS_ERR(bdev)) { |
d397712b | 572 | printk(KERN_INFO "open %s failed\n", device->name); |
a0af469b | 573 | goto error; |
8a4b83cc | 574 | } |
a061fc8d | 575 | set_blocksize(bdev, 4096); |
a0af469b | 576 | |
a512bbf8 | 577 | bh = btrfs_read_dev_super(bdev); |
a0af469b CM |
578 | if (!bh) |
579 | goto error_close; | |
580 | ||
581 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
a0af469b CM |
582 | devid = le64_to_cpu(disk_super->dev_item.devid); |
583 | if (devid != device->devid) | |
584 | goto error_brelse; | |
585 | ||
2b82032c YZ |
586 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
587 | BTRFS_UUID_SIZE)) | |
588 | goto error_brelse; | |
589 | ||
590 | device->generation = btrfs_super_generation(disk_super); | |
591 | if (!latest_transid || device->generation > latest_transid) { | |
a0af469b | 592 | latest_devid = devid; |
2b82032c | 593 | latest_transid = device->generation; |
a0af469b CM |
594 | latest_bdev = bdev; |
595 | } | |
596 | ||
2b82032c YZ |
597 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
598 | device->writeable = 0; | |
599 | } else { | |
600 | device->writeable = !bdev_read_only(bdev); | |
601 | seeding = 0; | |
602 | } | |
603 | ||
8a4b83cc | 604 | device->bdev = bdev; |
dfe25020 | 605 | device->in_fs_metadata = 0; |
15916de8 CM |
606 | device->mode = flags; |
607 | ||
c289811c CM |
608 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
609 | fs_devices->rotating = 1; | |
610 | ||
a0af469b | 611 | fs_devices->open_devices++; |
2b82032c YZ |
612 | if (device->writeable) { |
613 | fs_devices->rw_devices++; | |
614 | list_add(&device->dev_alloc_list, | |
615 | &fs_devices->alloc_list); | |
616 | } | |
a0af469b | 617 | continue; |
a061fc8d | 618 | |
a0af469b CM |
619 | error_brelse: |
620 | brelse(bh); | |
621 | error_close: | |
97288f2c | 622 | close_bdev_exclusive(bdev, FMODE_READ); |
a0af469b CM |
623 | error: |
624 | continue; | |
8a4b83cc | 625 | } |
a0af469b CM |
626 | if (fs_devices->open_devices == 0) { |
627 | ret = -EIO; | |
628 | goto out; | |
629 | } | |
2b82032c YZ |
630 | fs_devices->seeding = seeding; |
631 | fs_devices->opened = 1; | |
a0af469b CM |
632 | fs_devices->latest_bdev = latest_bdev; |
633 | fs_devices->latest_devid = latest_devid; | |
634 | fs_devices->latest_trans = latest_transid; | |
2b82032c | 635 | fs_devices->total_rw_bytes = 0; |
a0af469b | 636 | out: |
2b82032c YZ |
637 | return ret; |
638 | } | |
639 | ||
640 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, | |
97288f2c | 641 | fmode_t flags, void *holder) |
2b82032c YZ |
642 | { |
643 | int ret; | |
644 | ||
645 | mutex_lock(&uuid_mutex); | |
646 | if (fs_devices->opened) { | |
e4404d6e YZ |
647 | fs_devices->opened++; |
648 | ret = 0; | |
2b82032c | 649 | } else { |
15916de8 | 650 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
2b82032c | 651 | } |
8a4b83cc | 652 | mutex_unlock(&uuid_mutex); |
8a4b83cc CM |
653 | return ret; |
654 | } | |
655 | ||
97288f2c | 656 | int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, |
8a4b83cc CM |
657 | struct btrfs_fs_devices **fs_devices_ret) |
658 | { | |
659 | struct btrfs_super_block *disk_super; | |
660 | struct block_device *bdev; | |
661 | struct buffer_head *bh; | |
662 | int ret; | |
663 | u64 devid; | |
f2984462 | 664 | u64 transid; |
8a4b83cc CM |
665 | |
666 | mutex_lock(&uuid_mutex); | |
667 | ||
15916de8 | 668 | bdev = open_bdev_exclusive(path, flags, holder); |
8a4b83cc CM |
669 | |
670 | if (IS_ERR(bdev)) { | |
8a4b83cc CM |
671 | ret = PTR_ERR(bdev); |
672 | goto error; | |
673 | } | |
674 | ||
675 | ret = set_blocksize(bdev, 4096); | |
676 | if (ret) | |
677 | goto error_close; | |
a512bbf8 | 678 | bh = btrfs_read_dev_super(bdev); |
8a4b83cc CM |
679 | if (!bh) { |
680 | ret = -EIO; | |
681 | goto error_close; | |
682 | } | |
683 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
8a4b83cc | 684 | devid = le64_to_cpu(disk_super->dev_item.devid); |
f2984462 | 685 | transid = btrfs_super_generation(disk_super); |
7ae9c09d | 686 | if (disk_super->label[0]) |
d397712b | 687 | printk(KERN_INFO "device label %s ", disk_super->label); |
7ae9c09d CM |
688 | else { |
689 | /* FIXME, make a readl uuid parser */ | |
d397712b | 690 | printk(KERN_INFO "device fsid %llx-%llx ", |
7ae9c09d CM |
691 | *(unsigned long long *)disk_super->fsid, |
692 | *(unsigned long long *)(disk_super->fsid + 8)); | |
693 | } | |
119e10cf | 694 | printk(KERN_CONT "devid %llu transid %llu %s\n", |
d397712b | 695 | (unsigned long long)devid, (unsigned long long)transid, path); |
8a4b83cc CM |
696 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
697 | ||
8a4b83cc CM |
698 | brelse(bh); |
699 | error_close: | |
15916de8 | 700 | close_bdev_exclusive(bdev, flags); |
8a4b83cc CM |
701 | error: |
702 | mutex_unlock(&uuid_mutex); | |
703 | return ret; | |
704 | } | |
0b86a832 CM |
705 | |
706 | /* | |
707 | * this uses a pretty simple search, the expectation is that it is | |
708 | * called very infrequently and that a given device has a small number | |
709 | * of extents | |
710 | */ | |
a1b32a59 CM |
711 | static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans, |
712 | struct btrfs_device *device, | |
a1b32a59 | 713 | u64 num_bytes, u64 *start) |
0b86a832 CM |
714 | { |
715 | struct btrfs_key key; | |
716 | struct btrfs_root *root = device->dev_root; | |
717 | struct btrfs_dev_extent *dev_extent = NULL; | |
2b82032c | 718 | struct btrfs_path *path; |
0b86a832 CM |
719 | u64 hole_size = 0; |
720 | u64 last_byte = 0; | |
721 | u64 search_start = 0; | |
722 | u64 search_end = device->total_bytes; | |
723 | int ret; | |
724 | int slot = 0; | |
725 | int start_found; | |
726 | struct extent_buffer *l; | |
727 | ||
2b82032c YZ |
728 | path = btrfs_alloc_path(); |
729 | if (!path) | |
730 | return -ENOMEM; | |
0b86a832 | 731 | path->reada = 2; |
2b82032c | 732 | start_found = 0; |
0b86a832 CM |
733 | |
734 | /* FIXME use last free of some kind */ | |
735 | ||
8a4b83cc CM |
736 | /* we don't want to overwrite the superblock on the drive, |
737 | * so we make sure to start at an offset of at least 1MB | |
738 | */ | |
739 | search_start = max((u64)1024 * 1024, search_start); | |
8f18cf13 CM |
740 | |
741 | if (root->fs_info->alloc_start + num_bytes <= device->total_bytes) | |
742 | search_start = max(root->fs_info->alloc_start, search_start); | |
743 | ||
0b86a832 CM |
744 | key.objectid = device->devid; |
745 | key.offset = search_start; | |
746 | key.type = BTRFS_DEV_EXTENT_KEY; | |
747 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); | |
748 | if (ret < 0) | |
749 | goto error; | |
750 | ret = btrfs_previous_item(root, path, 0, key.type); | |
751 | if (ret < 0) | |
752 | goto error; | |
753 | l = path->nodes[0]; | |
754 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
755 | while (1) { | |
756 | l = path->nodes[0]; | |
757 | slot = path->slots[0]; | |
758 | if (slot >= btrfs_header_nritems(l)) { | |
759 | ret = btrfs_next_leaf(root, path); | |
760 | if (ret == 0) | |
761 | continue; | |
762 | if (ret < 0) | |
763 | goto error; | |
764 | no_more_items: | |
765 | if (!start_found) { | |
766 | if (search_start >= search_end) { | |
767 | ret = -ENOSPC; | |
768 | goto error; | |
769 | } | |
770 | *start = search_start; | |
771 | start_found = 1; | |
772 | goto check_pending; | |
773 | } | |
774 | *start = last_byte > search_start ? | |
775 | last_byte : search_start; | |
776 | if (search_end <= *start) { | |
777 | ret = -ENOSPC; | |
778 | goto error; | |
779 | } | |
780 | goto check_pending; | |
781 | } | |
782 | btrfs_item_key_to_cpu(l, &key, slot); | |
783 | ||
784 | if (key.objectid < device->devid) | |
785 | goto next; | |
786 | ||
787 | if (key.objectid > device->devid) | |
788 | goto no_more_items; | |
789 | ||
790 | if (key.offset >= search_start && key.offset > last_byte && | |
791 | start_found) { | |
792 | if (last_byte < search_start) | |
793 | last_byte = search_start; | |
794 | hole_size = key.offset - last_byte; | |
795 | if (key.offset > last_byte && | |
796 | hole_size >= num_bytes) { | |
797 | *start = last_byte; | |
798 | goto check_pending; | |
799 | } | |
800 | } | |
d397712b | 801 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
0b86a832 | 802 | goto next; |
0b86a832 CM |
803 | |
804 | start_found = 1; | |
805 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
806 | last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent); | |
807 | next: | |
808 | path->slots[0]++; | |
809 | cond_resched(); | |
810 | } | |
811 | check_pending: | |
812 | /* we have to make sure we didn't find an extent that has already | |
813 | * been allocated by the map tree or the original allocation | |
814 | */ | |
0b86a832 CM |
815 | BUG_ON(*start < search_start); |
816 | ||
6324fbf3 | 817 | if (*start + num_bytes > search_end) { |
0b86a832 CM |
818 | ret = -ENOSPC; |
819 | goto error; | |
820 | } | |
821 | /* check for pending inserts here */ | |
2b82032c | 822 | ret = 0; |
0b86a832 CM |
823 | |
824 | error: | |
2b82032c | 825 | btrfs_free_path(path); |
0b86a832 CM |
826 | return ret; |
827 | } | |
828 | ||
b2950863 | 829 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
8f18cf13 CM |
830 | struct btrfs_device *device, |
831 | u64 start) | |
832 | { | |
833 | int ret; | |
834 | struct btrfs_path *path; | |
835 | struct btrfs_root *root = device->dev_root; | |
836 | struct btrfs_key key; | |
a061fc8d CM |
837 | struct btrfs_key found_key; |
838 | struct extent_buffer *leaf = NULL; | |
839 | struct btrfs_dev_extent *extent = NULL; | |
8f18cf13 CM |
840 | |
841 | path = btrfs_alloc_path(); | |
842 | if (!path) | |
843 | return -ENOMEM; | |
844 | ||
845 | key.objectid = device->devid; | |
846 | key.offset = start; | |
847 | key.type = BTRFS_DEV_EXTENT_KEY; | |
848 | ||
849 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
a061fc8d CM |
850 | if (ret > 0) { |
851 | ret = btrfs_previous_item(root, path, key.objectid, | |
852 | BTRFS_DEV_EXTENT_KEY); | |
853 | BUG_ON(ret); | |
854 | leaf = path->nodes[0]; | |
855 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
856 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
857 | struct btrfs_dev_extent); | |
858 | BUG_ON(found_key.offset > start || found_key.offset + | |
859 | btrfs_dev_extent_length(leaf, extent) < start); | |
860 | ret = 0; | |
861 | } else if (ret == 0) { | |
862 | leaf = path->nodes[0]; | |
863 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
864 | struct btrfs_dev_extent); | |
865 | } | |
8f18cf13 CM |
866 | BUG_ON(ret); |
867 | ||
dfe25020 CM |
868 | if (device->bytes_used > 0) |
869 | device->bytes_used -= btrfs_dev_extent_length(leaf, extent); | |
8f18cf13 CM |
870 | ret = btrfs_del_item(trans, root, path); |
871 | BUG_ON(ret); | |
872 | ||
873 | btrfs_free_path(path); | |
874 | return ret; | |
875 | } | |
876 | ||
2b82032c | 877 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
0b86a832 | 878 | struct btrfs_device *device, |
e17cade2 | 879 | u64 chunk_tree, u64 chunk_objectid, |
2b82032c | 880 | u64 chunk_offset, u64 start, u64 num_bytes) |
0b86a832 CM |
881 | { |
882 | int ret; | |
883 | struct btrfs_path *path; | |
884 | struct btrfs_root *root = device->dev_root; | |
885 | struct btrfs_dev_extent *extent; | |
886 | struct extent_buffer *leaf; | |
887 | struct btrfs_key key; | |
888 | ||
dfe25020 | 889 | WARN_ON(!device->in_fs_metadata); |
0b86a832 CM |
890 | path = btrfs_alloc_path(); |
891 | if (!path) | |
892 | return -ENOMEM; | |
893 | ||
0b86a832 | 894 | key.objectid = device->devid; |
2b82032c | 895 | key.offset = start; |
0b86a832 CM |
896 | key.type = BTRFS_DEV_EXTENT_KEY; |
897 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
898 | sizeof(*extent)); | |
899 | BUG_ON(ret); | |
900 | ||
901 | leaf = path->nodes[0]; | |
902 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
903 | struct btrfs_dev_extent); | |
e17cade2 CM |
904 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
905 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); | |
906 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); | |
907 | ||
908 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, | |
909 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), | |
910 | BTRFS_UUID_SIZE); | |
911 | ||
0b86a832 CM |
912 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
913 | btrfs_mark_buffer_dirty(leaf); | |
0b86a832 CM |
914 | btrfs_free_path(path); |
915 | return ret; | |
916 | } | |
917 | ||
a1b32a59 CM |
918 | static noinline int find_next_chunk(struct btrfs_root *root, |
919 | u64 objectid, u64 *offset) | |
0b86a832 CM |
920 | { |
921 | struct btrfs_path *path; | |
922 | int ret; | |
923 | struct btrfs_key key; | |
e17cade2 | 924 | struct btrfs_chunk *chunk; |
0b86a832 CM |
925 | struct btrfs_key found_key; |
926 | ||
927 | path = btrfs_alloc_path(); | |
928 | BUG_ON(!path); | |
929 | ||
e17cade2 | 930 | key.objectid = objectid; |
0b86a832 CM |
931 | key.offset = (u64)-1; |
932 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
933 | ||
934 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
935 | if (ret < 0) | |
936 | goto error; | |
937 | ||
938 | BUG_ON(ret == 0); | |
939 | ||
940 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); | |
941 | if (ret) { | |
e17cade2 | 942 | *offset = 0; |
0b86a832 CM |
943 | } else { |
944 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
945 | path->slots[0]); | |
e17cade2 CM |
946 | if (found_key.objectid != objectid) |
947 | *offset = 0; | |
948 | else { | |
949 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
950 | struct btrfs_chunk); | |
951 | *offset = found_key.offset + | |
952 | btrfs_chunk_length(path->nodes[0], chunk); | |
953 | } | |
0b86a832 CM |
954 | } |
955 | ret = 0; | |
956 | error: | |
957 | btrfs_free_path(path); | |
958 | return ret; | |
959 | } | |
960 | ||
2b82032c | 961 | static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid) |
0b86a832 CM |
962 | { |
963 | int ret; | |
964 | struct btrfs_key key; | |
965 | struct btrfs_key found_key; | |
2b82032c YZ |
966 | struct btrfs_path *path; |
967 | ||
968 | root = root->fs_info->chunk_root; | |
969 | ||
970 | path = btrfs_alloc_path(); | |
971 | if (!path) | |
972 | return -ENOMEM; | |
0b86a832 CM |
973 | |
974 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
975 | key.type = BTRFS_DEV_ITEM_KEY; | |
976 | key.offset = (u64)-1; | |
977 | ||
978 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
979 | if (ret < 0) | |
980 | goto error; | |
981 | ||
982 | BUG_ON(ret == 0); | |
983 | ||
984 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, | |
985 | BTRFS_DEV_ITEM_KEY); | |
986 | if (ret) { | |
987 | *objectid = 1; | |
988 | } else { | |
989 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
990 | path->slots[0]); | |
991 | *objectid = found_key.offset + 1; | |
992 | } | |
993 | ret = 0; | |
994 | error: | |
2b82032c | 995 | btrfs_free_path(path); |
0b86a832 CM |
996 | return ret; |
997 | } | |
998 | ||
999 | /* | |
1000 | * the device information is stored in the chunk root | |
1001 | * the btrfs_device struct should be fully filled in | |
1002 | */ | |
1003 | int btrfs_add_device(struct btrfs_trans_handle *trans, | |
1004 | struct btrfs_root *root, | |
1005 | struct btrfs_device *device) | |
1006 | { | |
1007 | int ret; | |
1008 | struct btrfs_path *path; | |
1009 | struct btrfs_dev_item *dev_item; | |
1010 | struct extent_buffer *leaf; | |
1011 | struct btrfs_key key; | |
1012 | unsigned long ptr; | |
0b86a832 CM |
1013 | |
1014 | root = root->fs_info->chunk_root; | |
1015 | ||
1016 | path = btrfs_alloc_path(); | |
1017 | if (!path) | |
1018 | return -ENOMEM; | |
1019 | ||
0b86a832 CM |
1020 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1021 | key.type = BTRFS_DEV_ITEM_KEY; | |
2b82032c | 1022 | key.offset = device->devid; |
0b86a832 CM |
1023 | |
1024 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
0d81ba5d | 1025 | sizeof(*dev_item)); |
0b86a832 CM |
1026 | if (ret) |
1027 | goto out; | |
1028 | ||
1029 | leaf = path->nodes[0]; | |
1030 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
1031 | ||
1032 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
2b82032c | 1033 | btrfs_set_device_generation(leaf, dev_item, 0); |
0b86a832 CM |
1034 | btrfs_set_device_type(leaf, dev_item, device->type); |
1035 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
1036 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
1037 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
0b86a832 CM |
1038 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
1039 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); | |
e17cade2 CM |
1040 | btrfs_set_device_group(leaf, dev_item, 0); |
1041 | btrfs_set_device_seek_speed(leaf, dev_item, 0); | |
1042 | btrfs_set_device_bandwidth(leaf, dev_item, 0); | |
c3027eb5 | 1043 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
0b86a832 | 1044 | |
0b86a832 | 1045 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
e17cade2 | 1046 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
2b82032c YZ |
1047 | ptr = (unsigned long)btrfs_device_fsid(dev_item); |
1048 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); | |
0b86a832 | 1049 | btrfs_mark_buffer_dirty(leaf); |
0b86a832 | 1050 | |
2b82032c | 1051 | ret = 0; |
0b86a832 CM |
1052 | out: |
1053 | btrfs_free_path(path); | |
1054 | return ret; | |
1055 | } | |
8f18cf13 | 1056 | |
a061fc8d CM |
1057 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
1058 | struct btrfs_device *device) | |
1059 | { | |
1060 | int ret; | |
1061 | struct btrfs_path *path; | |
a061fc8d | 1062 | struct btrfs_key key; |
a061fc8d CM |
1063 | struct btrfs_trans_handle *trans; |
1064 | ||
1065 | root = root->fs_info->chunk_root; | |
1066 | ||
1067 | path = btrfs_alloc_path(); | |
1068 | if (!path) | |
1069 | return -ENOMEM; | |
1070 | ||
1071 | trans = btrfs_start_transaction(root, 1); | |
1072 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1073 | key.type = BTRFS_DEV_ITEM_KEY; | |
1074 | key.offset = device->devid; | |
7d9eb12c | 1075 | lock_chunks(root); |
a061fc8d CM |
1076 | |
1077 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1078 | if (ret < 0) | |
1079 | goto out; | |
1080 | ||
1081 | if (ret > 0) { | |
1082 | ret = -ENOENT; | |
1083 | goto out; | |
1084 | } | |
1085 | ||
1086 | ret = btrfs_del_item(trans, root, path); | |
1087 | if (ret) | |
1088 | goto out; | |
a061fc8d CM |
1089 | out: |
1090 | btrfs_free_path(path); | |
7d9eb12c | 1091 | unlock_chunks(root); |
a061fc8d CM |
1092 | btrfs_commit_transaction(trans, root); |
1093 | return ret; | |
1094 | } | |
1095 | ||
1096 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) | |
1097 | { | |
1098 | struct btrfs_device *device; | |
2b82032c | 1099 | struct btrfs_device *next_device; |
a061fc8d | 1100 | struct block_device *bdev; |
dfe25020 | 1101 | struct buffer_head *bh = NULL; |
a061fc8d CM |
1102 | struct btrfs_super_block *disk_super; |
1103 | u64 all_avail; | |
1104 | u64 devid; | |
2b82032c YZ |
1105 | u64 num_devices; |
1106 | u8 *dev_uuid; | |
a061fc8d CM |
1107 | int ret = 0; |
1108 | ||
a061fc8d | 1109 | mutex_lock(&uuid_mutex); |
7d9eb12c | 1110 | mutex_lock(&root->fs_info->volume_mutex); |
a061fc8d CM |
1111 | |
1112 | all_avail = root->fs_info->avail_data_alloc_bits | | |
1113 | root->fs_info->avail_system_alloc_bits | | |
1114 | root->fs_info->avail_metadata_alloc_bits; | |
1115 | ||
1116 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && | |
2b82032c | 1117 | root->fs_info->fs_devices->rw_devices <= 4) { |
d397712b CM |
1118 | printk(KERN_ERR "btrfs: unable to go below four devices " |
1119 | "on raid10\n"); | |
a061fc8d CM |
1120 | ret = -EINVAL; |
1121 | goto out; | |
1122 | } | |
1123 | ||
1124 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && | |
2b82032c | 1125 | root->fs_info->fs_devices->rw_devices <= 2) { |
d397712b CM |
1126 | printk(KERN_ERR "btrfs: unable to go below two " |
1127 | "devices on raid1\n"); | |
a061fc8d CM |
1128 | ret = -EINVAL; |
1129 | goto out; | |
1130 | } | |
1131 | ||
dfe25020 | 1132 | if (strcmp(device_path, "missing") == 0) { |
dfe25020 CM |
1133 | struct list_head *devices; |
1134 | struct btrfs_device *tmp; | |
a061fc8d | 1135 | |
dfe25020 CM |
1136 | device = NULL; |
1137 | devices = &root->fs_info->fs_devices->devices; | |
c6e30871 | 1138 | list_for_each_entry(tmp, devices, dev_list) { |
dfe25020 CM |
1139 | if (tmp->in_fs_metadata && !tmp->bdev) { |
1140 | device = tmp; | |
1141 | break; | |
1142 | } | |
1143 | } | |
1144 | bdev = NULL; | |
1145 | bh = NULL; | |
1146 | disk_super = NULL; | |
1147 | if (!device) { | |
d397712b CM |
1148 | printk(KERN_ERR "btrfs: no missing devices found to " |
1149 | "remove\n"); | |
dfe25020 CM |
1150 | goto out; |
1151 | } | |
dfe25020 | 1152 | } else { |
97288f2c | 1153 | bdev = open_bdev_exclusive(device_path, FMODE_READ, |
dfe25020 CM |
1154 | root->fs_info->bdev_holder); |
1155 | if (IS_ERR(bdev)) { | |
1156 | ret = PTR_ERR(bdev); | |
1157 | goto out; | |
1158 | } | |
a061fc8d | 1159 | |
2b82032c | 1160 | set_blocksize(bdev, 4096); |
a512bbf8 | 1161 | bh = btrfs_read_dev_super(bdev); |
dfe25020 CM |
1162 | if (!bh) { |
1163 | ret = -EIO; | |
1164 | goto error_close; | |
1165 | } | |
1166 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
dfe25020 | 1167 | devid = le64_to_cpu(disk_super->dev_item.devid); |
2b82032c YZ |
1168 | dev_uuid = disk_super->dev_item.uuid; |
1169 | device = btrfs_find_device(root, devid, dev_uuid, | |
1170 | disk_super->fsid); | |
dfe25020 CM |
1171 | if (!device) { |
1172 | ret = -ENOENT; | |
1173 | goto error_brelse; | |
1174 | } | |
2b82032c | 1175 | } |
dfe25020 | 1176 | |
2b82032c | 1177 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
d397712b CM |
1178 | printk(KERN_ERR "btrfs: unable to remove the only writeable " |
1179 | "device\n"); | |
2b82032c YZ |
1180 | ret = -EINVAL; |
1181 | goto error_brelse; | |
1182 | } | |
1183 | ||
1184 | if (device->writeable) { | |
1185 | list_del_init(&device->dev_alloc_list); | |
1186 | root->fs_info->fs_devices->rw_devices--; | |
dfe25020 | 1187 | } |
a061fc8d CM |
1188 | |
1189 | ret = btrfs_shrink_device(device, 0); | |
1190 | if (ret) | |
1191 | goto error_brelse; | |
1192 | ||
a061fc8d CM |
1193 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
1194 | if (ret) | |
1195 | goto error_brelse; | |
1196 | ||
2b82032c | 1197 | device->in_fs_metadata = 0; |
e4404d6e YZ |
1198 | list_del_init(&device->dev_list); |
1199 | device->fs_devices->num_devices--; | |
2b82032c YZ |
1200 | |
1201 | next_device = list_entry(root->fs_info->fs_devices->devices.next, | |
1202 | struct btrfs_device, dev_list); | |
1203 | if (device->bdev == root->fs_info->sb->s_bdev) | |
1204 | root->fs_info->sb->s_bdev = next_device->bdev; | |
1205 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) | |
1206 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; | |
1207 | ||
e4404d6e YZ |
1208 | if (device->bdev) { |
1209 | close_bdev_exclusive(device->bdev, device->mode); | |
1210 | device->bdev = NULL; | |
1211 | device->fs_devices->open_devices--; | |
1212 | } | |
1213 | ||
2b82032c YZ |
1214 | num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1; |
1215 | btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices); | |
1216 | ||
e4404d6e YZ |
1217 | if (device->fs_devices->open_devices == 0) { |
1218 | struct btrfs_fs_devices *fs_devices; | |
1219 | fs_devices = root->fs_info->fs_devices; | |
1220 | while (fs_devices) { | |
1221 | if (fs_devices->seed == device->fs_devices) | |
1222 | break; | |
1223 | fs_devices = fs_devices->seed; | |
2b82032c | 1224 | } |
e4404d6e YZ |
1225 | fs_devices->seed = device->fs_devices->seed; |
1226 | device->fs_devices->seed = NULL; | |
1227 | __btrfs_close_devices(device->fs_devices); | |
1228 | free_fs_devices(device->fs_devices); | |
2b82032c YZ |
1229 | } |
1230 | ||
1231 | /* | |
1232 | * at this point, the device is zero sized. We want to | |
1233 | * remove it from the devices list and zero out the old super | |
1234 | */ | |
1235 | if (device->writeable) { | |
dfe25020 CM |
1236 | /* make sure this device isn't detected as part of |
1237 | * the FS anymore | |
1238 | */ | |
1239 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); | |
1240 | set_buffer_dirty(bh); | |
1241 | sync_dirty_buffer(bh); | |
dfe25020 | 1242 | } |
a061fc8d CM |
1243 | |
1244 | kfree(device->name); | |
1245 | kfree(device); | |
1246 | ret = 0; | |
a061fc8d CM |
1247 | |
1248 | error_brelse: | |
1249 | brelse(bh); | |
1250 | error_close: | |
dfe25020 | 1251 | if (bdev) |
97288f2c | 1252 | close_bdev_exclusive(bdev, FMODE_READ); |
a061fc8d | 1253 | out: |
7d9eb12c | 1254 | mutex_unlock(&root->fs_info->volume_mutex); |
a061fc8d | 1255 | mutex_unlock(&uuid_mutex); |
a061fc8d CM |
1256 | return ret; |
1257 | } | |
1258 | ||
2b82032c YZ |
1259 | /* |
1260 | * does all the dirty work required for changing file system's UUID. | |
1261 | */ | |
1262 | static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans, | |
1263 | struct btrfs_root *root) | |
1264 | { | |
1265 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
1266 | struct btrfs_fs_devices *old_devices; | |
e4404d6e | 1267 | struct btrfs_fs_devices *seed_devices; |
2b82032c YZ |
1268 | struct btrfs_super_block *disk_super = &root->fs_info->super_copy; |
1269 | struct btrfs_device *device; | |
1270 | u64 super_flags; | |
1271 | ||
1272 | BUG_ON(!mutex_is_locked(&uuid_mutex)); | |
e4404d6e | 1273 | if (!fs_devices->seeding) |
2b82032c YZ |
1274 | return -EINVAL; |
1275 | ||
e4404d6e YZ |
1276 | seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
1277 | if (!seed_devices) | |
2b82032c YZ |
1278 | return -ENOMEM; |
1279 | ||
e4404d6e YZ |
1280 | old_devices = clone_fs_devices(fs_devices); |
1281 | if (IS_ERR(old_devices)) { | |
1282 | kfree(seed_devices); | |
1283 | return PTR_ERR(old_devices); | |
2b82032c | 1284 | } |
e4404d6e | 1285 | |
2b82032c YZ |
1286 | list_add(&old_devices->list, &fs_uuids); |
1287 | ||
e4404d6e YZ |
1288 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
1289 | seed_devices->opened = 1; | |
1290 | INIT_LIST_HEAD(&seed_devices->devices); | |
1291 | INIT_LIST_HEAD(&seed_devices->alloc_list); | |
1292 | list_splice_init(&fs_devices->devices, &seed_devices->devices); | |
1293 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); | |
1294 | list_for_each_entry(device, &seed_devices->devices, dev_list) { | |
1295 | device->fs_devices = seed_devices; | |
1296 | } | |
1297 | ||
2b82032c YZ |
1298 | fs_devices->seeding = 0; |
1299 | fs_devices->num_devices = 0; | |
1300 | fs_devices->open_devices = 0; | |
e4404d6e | 1301 | fs_devices->seed = seed_devices; |
2b82032c YZ |
1302 | |
1303 | generate_random_uuid(fs_devices->fsid); | |
1304 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); | |
1305 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); | |
1306 | super_flags = btrfs_super_flags(disk_super) & | |
1307 | ~BTRFS_SUPER_FLAG_SEEDING; | |
1308 | btrfs_set_super_flags(disk_super, super_flags); | |
1309 | ||
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | /* | |
1314 | * strore the expected generation for seed devices in device items. | |
1315 | */ | |
1316 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, | |
1317 | struct btrfs_root *root) | |
1318 | { | |
1319 | struct btrfs_path *path; | |
1320 | struct extent_buffer *leaf; | |
1321 | struct btrfs_dev_item *dev_item; | |
1322 | struct btrfs_device *device; | |
1323 | struct btrfs_key key; | |
1324 | u8 fs_uuid[BTRFS_UUID_SIZE]; | |
1325 | u8 dev_uuid[BTRFS_UUID_SIZE]; | |
1326 | u64 devid; | |
1327 | int ret; | |
1328 | ||
1329 | path = btrfs_alloc_path(); | |
1330 | if (!path) | |
1331 | return -ENOMEM; | |
1332 | ||
1333 | root = root->fs_info->chunk_root; | |
1334 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1335 | key.offset = 0; | |
1336 | key.type = BTRFS_DEV_ITEM_KEY; | |
1337 | ||
1338 | while (1) { | |
1339 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
1340 | if (ret < 0) | |
1341 | goto error; | |
1342 | ||
1343 | leaf = path->nodes[0]; | |
1344 | next_slot: | |
1345 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1346 | ret = btrfs_next_leaf(root, path); | |
1347 | if (ret > 0) | |
1348 | break; | |
1349 | if (ret < 0) | |
1350 | goto error; | |
1351 | leaf = path->nodes[0]; | |
1352 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1353 | btrfs_release_path(root, path); | |
1354 | continue; | |
1355 | } | |
1356 | ||
1357 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1358 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || | |
1359 | key.type != BTRFS_DEV_ITEM_KEY) | |
1360 | break; | |
1361 | ||
1362 | dev_item = btrfs_item_ptr(leaf, path->slots[0], | |
1363 | struct btrfs_dev_item); | |
1364 | devid = btrfs_device_id(leaf, dev_item); | |
1365 | read_extent_buffer(leaf, dev_uuid, | |
1366 | (unsigned long)btrfs_device_uuid(dev_item), | |
1367 | BTRFS_UUID_SIZE); | |
1368 | read_extent_buffer(leaf, fs_uuid, | |
1369 | (unsigned long)btrfs_device_fsid(dev_item), | |
1370 | BTRFS_UUID_SIZE); | |
1371 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); | |
1372 | BUG_ON(!device); | |
1373 | ||
1374 | if (device->fs_devices->seeding) { | |
1375 | btrfs_set_device_generation(leaf, dev_item, | |
1376 | device->generation); | |
1377 | btrfs_mark_buffer_dirty(leaf); | |
1378 | } | |
1379 | ||
1380 | path->slots[0]++; | |
1381 | goto next_slot; | |
1382 | } | |
1383 | ret = 0; | |
1384 | error: | |
1385 | btrfs_free_path(path); | |
1386 | return ret; | |
1387 | } | |
1388 | ||
788f20eb CM |
1389 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
1390 | { | |
1391 | struct btrfs_trans_handle *trans; | |
1392 | struct btrfs_device *device; | |
1393 | struct block_device *bdev; | |
788f20eb | 1394 | struct list_head *devices; |
2b82032c | 1395 | struct super_block *sb = root->fs_info->sb; |
788f20eb | 1396 | u64 total_bytes; |
2b82032c | 1397 | int seeding_dev = 0; |
788f20eb CM |
1398 | int ret = 0; |
1399 | ||
2b82032c YZ |
1400 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
1401 | return -EINVAL; | |
788f20eb | 1402 | |
15916de8 | 1403 | bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder); |
d397712b | 1404 | if (!bdev) |
788f20eb | 1405 | return -EIO; |
a2135011 | 1406 | |
2b82032c YZ |
1407 | if (root->fs_info->fs_devices->seeding) { |
1408 | seeding_dev = 1; | |
1409 | down_write(&sb->s_umount); | |
1410 | mutex_lock(&uuid_mutex); | |
1411 | } | |
1412 | ||
8c8bee1d | 1413 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
7d9eb12c | 1414 | mutex_lock(&root->fs_info->volume_mutex); |
a2135011 | 1415 | |
788f20eb | 1416 | devices = &root->fs_info->fs_devices->devices; |
c6e30871 | 1417 | list_for_each_entry(device, devices, dev_list) { |
788f20eb CM |
1418 | if (device->bdev == bdev) { |
1419 | ret = -EEXIST; | |
2b82032c | 1420 | goto error; |
788f20eb CM |
1421 | } |
1422 | } | |
1423 | ||
1424 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
1425 | if (!device) { | |
1426 | /* we can safely leave the fs_devices entry around */ | |
1427 | ret = -ENOMEM; | |
2b82032c | 1428 | goto error; |
788f20eb CM |
1429 | } |
1430 | ||
788f20eb CM |
1431 | device->name = kstrdup(device_path, GFP_NOFS); |
1432 | if (!device->name) { | |
1433 | kfree(device); | |
2b82032c YZ |
1434 | ret = -ENOMEM; |
1435 | goto error; | |
788f20eb | 1436 | } |
2b82032c YZ |
1437 | |
1438 | ret = find_next_devid(root, &device->devid); | |
1439 | if (ret) { | |
1440 | kfree(device); | |
1441 | goto error; | |
1442 | } | |
1443 | ||
1444 | trans = btrfs_start_transaction(root, 1); | |
1445 | lock_chunks(root); | |
1446 | ||
1447 | device->barriers = 1; | |
1448 | device->writeable = 1; | |
1449 | device->work.func = pending_bios_fn; | |
1450 | generate_random_uuid(device->uuid); | |
1451 | spin_lock_init(&device->io_lock); | |
1452 | device->generation = trans->transid; | |
788f20eb CM |
1453 | device->io_width = root->sectorsize; |
1454 | device->io_align = root->sectorsize; | |
1455 | device->sector_size = root->sectorsize; | |
1456 | device->total_bytes = i_size_read(bdev->bd_inode); | |
2cc3c559 | 1457 | device->disk_total_bytes = device->total_bytes; |
788f20eb CM |
1458 | device->dev_root = root->fs_info->dev_root; |
1459 | device->bdev = bdev; | |
dfe25020 | 1460 | device->in_fs_metadata = 1; |
15916de8 | 1461 | device->mode = 0; |
2b82032c | 1462 | set_blocksize(device->bdev, 4096); |
788f20eb | 1463 | |
2b82032c YZ |
1464 | if (seeding_dev) { |
1465 | sb->s_flags &= ~MS_RDONLY; | |
1466 | ret = btrfs_prepare_sprout(trans, root); | |
1467 | BUG_ON(ret); | |
1468 | } | |
788f20eb | 1469 | |
2b82032c YZ |
1470 | device->fs_devices = root->fs_info->fs_devices; |
1471 | list_add(&device->dev_list, &root->fs_info->fs_devices->devices); | |
1472 | list_add(&device->dev_alloc_list, | |
1473 | &root->fs_info->fs_devices->alloc_list); | |
1474 | root->fs_info->fs_devices->num_devices++; | |
1475 | root->fs_info->fs_devices->open_devices++; | |
1476 | root->fs_info->fs_devices->rw_devices++; | |
1477 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; | |
325cd4ba | 1478 | |
c289811c CM |
1479 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
1480 | root->fs_info->fs_devices->rotating = 1; | |
1481 | ||
788f20eb CM |
1482 | total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
1483 | btrfs_set_super_total_bytes(&root->fs_info->super_copy, | |
1484 | total_bytes + device->total_bytes); | |
1485 | ||
1486 | total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy); | |
1487 | btrfs_set_super_num_devices(&root->fs_info->super_copy, | |
1488 | total_bytes + 1); | |
1489 | ||
2b82032c YZ |
1490 | if (seeding_dev) { |
1491 | ret = init_first_rw_device(trans, root, device); | |
1492 | BUG_ON(ret); | |
1493 | ret = btrfs_finish_sprout(trans, root); | |
1494 | BUG_ON(ret); | |
1495 | } else { | |
1496 | ret = btrfs_add_device(trans, root, device); | |
1497 | } | |
1498 | ||
913d952e CM |
1499 | /* |
1500 | * we've got more storage, clear any full flags on the space | |
1501 | * infos | |
1502 | */ | |
1503 | btrfs_clear_space_info_full(root->fs_info); | |
1504 | ||
7d9eb12c | 1505 | unlock_chunks(root); |
2b82032c | 1506 | btrfs_commit_transaction(trans, root); |
a2135011 | 1507 | |
2b82032c YZ |
1508 | if (seeding_dev) { |
1509 | mutex_unlock(&uuid_mutex); | |
1510 | up_write(&sb->s_umount); | |
788f20eb | 1511 | |
2b82032c YZ |
1512 | ret = btrfs_relocate_sys_chunks(root); |
1513 | BUG_ON(ret); | |
1514 | } | |
1515 | out: | |
1516 | mutex_unlock(&root->fs_info->volume_mutex); | |
1517 | return ret; | |
1518 | error: | |
15916de8 | 1519 | close_bdev_exclusive(bdev, 0); |
2b82032c YZ |
1520 | if (seeding_dev) { |
1521 | mutex_unlock(&uuid_mutex); | |
1522 | up_write(&sb->s_umount); | |
1523 | } | |
788f20eb CM |
1524 | goto out; |
1525 | } | |
1526 | ||
d397712b CM |
1527 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
1528 | struct btrfs_device *device) | |
0b86a832 CM |
1529 | { |
1530 | int ret; | |
1531 | struct btrfs_path *path; | |
1532 | struct btrfs_root *root; | |
1533 | struct btrfs_dev_item *dev_item; | |
1534 | struct extent_buffer *leaf; | |
1535 | struct btrfs_key key; | |
1536 | ||
1537 | root = device->dev_root->fs_info->chunk_root; | |
1538 | ||
1539 | path = btrfs_alloc_path(); | |
1540 | if (!path) | |
1541 | return -ENOMEM; | |
1542 | ||
1543 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1544 | key.type = BTRFS_DEV_ITEM_KEY; | |
1545 | key.offset = device->devid; | |
1546 | ||
1547 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
1548 | if (ret < 0) | |
1549 | goto out; | |
1550 | ||
1551 | if (ret > 0) { | |
1552 | ret = -ENOENT; | |
1553 | goto out; | |
1554 | } | |
1555 | ||
1556 | leaf = path->nodes[0]; | |
1557 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
1558 | ||
1559 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
1560 | btrfs_set_device_type(leaf, dev_item, device->type); | |
1561 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
1562 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
1563 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
d6397bae | 1564 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
0b86a832 CM |
1565 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
1566 | btrfs_mark_buffer_dirty(leaf); | |
1567 | ||
1568 | out: | |
1569 | btrfs_free_path(path); | |
1570 | return ret; | |
1571 | } | |
1572 | ||
7d9eb12c | 1573 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
8f18cf13 CM |
1574 | struct btrfs_device *device, u64 new_size) |
1575 | { | |
1576 | struct btrfs_super_block *super_copy = | |
1577 | &device->dev_root->fs_info->super_copy; | |
1578 | u64 old_total = btrfs_super_total_bytes(super_copy); | |
1579 | u64 diff = new_size - device->total_bytes; | |
1580 | ||
2b82032c YZ |
1581 | if (!device->writeable) |
1582 | return -EACCES; | |
1583 | if (new_size <= device->total_bytes) | |
1584 | return -EINVAL; | |
1585 | ||
8f18cf13 | 1586 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
2b82032c YZ |
1587 | device->fs_devices->total_rw_bytes += diff; |
1588 | ||
1589 | device->total_bytes = new_size; | |
4184ea7f CM |
1590 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
1591 | ||
8f18cf13 CM |
1592 | return btrfs_update_device(trans, device); |
1593 | } | |
1594 | ||
7d9eb12c CM |
1595 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
1596 | struct btrfs_device *device, u64 new_size) | |
1597 | { | |
1598 | int ret; | |
1599 | lock_chunks(device->dev_root); | |
1600 | ret = __btrfs_grow_device(trans, device, new_size); | |
1601 | unlock_chunks(device->dev_root); | |
1602 | return ret; | |
1603 | } | |
1604 | ||
8f18cf13 CM |
1605 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
1606 | struct btrfs_root *root, | |
1607 | u64 chunk_tree, u64 chunk_objectid, | |
1608 | u64 chunk_offset) | |
1609 | { | |
1610 | int ret; | |
1611 | struct btrfs_path *path; | |
1612 | struct btrfs_key key; | |
1613 | ||
1614 | root = root->fs_info->chunk_root; | |
1615 | path = btrfs_alloc_path(); | |
1616 | if (!path) | |
1617 | return -ENOMEM; | |
1618 | ||
1619 | key.objectid = chunk_objectid; | |
1620 | key.offset = chunk_offset; | |
1621 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
1622 | ||
1623 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1624 | BUG_ON(ret); | |
1625 | ||
1626 | ret = btrfs_del_item(trans, root, path); | |
1627 | BUG_ON(ret); | |
1628 | ||
1629 | btrfs_free_path(path); | |
1630 | return 0; | |
1631 | } | |
1632 | ||
b2950863 | 1633 | static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 |
8f18cf13 CM |
1634 | chunk_offset) |
1635 | { | |
1636 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
1637 | struct btrfs_disk_key *disk_key; | |
1638 | struct btrfs_chunk *chunk; | |
1639 | u8 *ptr; | |
1640 | int ret = 0; | |
1641 | u32 num_stripes; | |
1642 | u32 array_size; | |
1643 | u32 len = 0; | |
1644 | u32 cur; | |
1645 | struct btrfs_key key; | |
1646 | ||
1647 | array_size = btrfs_super_sys_array_size(super_copy); | |
1648 | ||
1649 | ptr = super_copy->sys_chunk_array; | |
1650 | cur = 0; | |
1651 | ||
1652 | while (cur < array_size) { | |
1653 | disk_key = (struct btrfs_disk_key *)ptr; | |
1654 | btrfs_disk_key_to_cpu(&key, disk_key); | |
1655 | ||
1656 | len = sizeof(*disk_key); | |
1657 | ||
1658 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { | |
1659 | chunk = (struct btrfs_chunk *)(ptr + len); | |
1660 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); | |
1661 | len += btrfs_chunk_item_size(num_stripes); | |
1662 | } else { | |
1663 | ret = -EIO; | |
1664 | break; | |
1665 | } | |
1666 | if (key.objectid == chunk_objectid && | |
1667 | key.offset == chunk_offset) { | |
1668 | memmove(ptr, ptr + len, array_size - (cur + len)); | |
1669 | array_size -= len; | |
1670 | btrfs_set_super_sys_array_size(super_copy, array_size); | |
1671 | } else { | |
1672 | ptr += len; | |
1673 | cur += len; | |
1674 | } | |
1675 | } | |
1676 | return ret; | |
1677 | } | |
1678 | ||
b2950863 | 1679 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
8f18cf13 CM |
1680 | u64 chunk_tree, u64 chunk_objectid, |
1681 | u64 chunk_offset) | |
1682 | { | |
1683 | struct extent_map_tree *em_tree; | |
1684 | struct btrfs_root *extent_root; | |
1685 | struct btrfs_trans_handle *trans; | |
1686 | struct extent_map *em; | |
1687 | struct map_lookup *map; | |
1688 | int ret; | |
1689 | int i; | |
1690 | ||
1691 | root = root->fs_info->chunk_root; | |
1692 | extent_root = root->fs_info->extent_root; | |
1693 | em_tree = &root->fs_info->mapping_tree.map_tree; | |
1694 | ||
1695 | /* step one, relocate all the extents inside this chunk */ | |
1a40e23b | 1696 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
8f18cf13 CM |
1697 | BUG_ON(ret); |
1698 | ||
1699 | trans = btrfs_start_transaction(root, 1); | |
1700 | BUG_ON(!trans); | |
1701 | ||
7d9eb12c CM |
1702 | lock_chunks(root); |
1703 | ||
8f18cf13 CM |
1704 | /* |
1705 | * step two, delete the device extents and the | |
1706 | * chunk tree entries | |
1707 | */ | |
1708 | spin_lock(&em_tree->lock); | |
1709 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); | |
1710 | spin_unlock(&em_tree->lock); | |
1711 | ||
a061fc8d CM |
1712 | BUG_ON(em->start > chunk_offset || |
1713 | em->start + em->len < chunk_offset); | |
8f18cf13 CM |
1714 | map = (struct map_lookup *)em->bdev; |
1715 | ||
1716 | for (i = 0; i < map->num_stripes; i++) { | |
1717 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, | |
1718 | map->stripes[i].physical); | |
1719 | BUG_ON(ret); | |
a061fc8d | 1720 | |
dfe25020 CM |
1721 | if (map->stripes[i].dev) { |
1722 | ret = btrfs_update_device(trans, map->stripes[i].dev); | |
1723 | BUG_ON(ret); | |
1724 | } | |
8f18cf13 CM |
1725 | } |
1726 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, | |
1727 | chunk_offset); | |
1728 | ||
1729 | BUG_ON(ret); | |
1730 | ||
1731 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { | |
1732 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); | |
1733 | BUG_ON(ret); | |
8f18cf13 CM |
1734 | } |
1735 | ||
2b82032c YZ |
1736 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
1737 | BUG_ON(ret); | |
1738 | ||
1739 | spin_lock(&em_tree->lock); | |
1740 | remove_extent_mapping(em_tree, em); | |
1741 | spin_unlock(&em_tree->lock); | |
1742 | ||
1743 | kfree(map); | |
1744 | em->bdev = NULL; | |
1745 | ||
1746 | /* once for the tree */ | |
1747 | free_extent_map(em); | |
1748 | /* once for us */ | |
1749 | free_extent_map(em); | |
1750 | ||
1751 | unlock_chunks(root); | |
1752 | btrfs_end_transaction(trans, root); | |
1753 | return 0; | |
1754 | } | |
1755 | ||
1756 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) | |
1757 | { | |
1758 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; | |
1759 | struct btrfs_path *path; | |
1760 | struct extent_buffer *leaf; | |
1761 | struct btrfs_chunk *chunk; | |
1762 | struct btrfs_key key; | |
1763 | struct btrfs_key found_key; | |
1764 | u64 chunk_tree = chunk_root->root_key.objectid; | |
1765 | u64 chunk_type; | |
1766 | int ret; | |
1767 | ||
1768 | path = btrfs_alloc_path(); | |
1769 | if (!path) | |
1770 | return -ENOMEM; | |
1771 | ||
1772 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; | |
1773 | key.offset = (u64)-1; | |
1774 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
1775 | ||
1776 | while (1) { | |
1777 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); | |
1778 | if (ret < 0) | |
1779 | goto error; | |
1780 | BUG_ON(ret == 0); | |
1781 | ||
1782 | ret = btrfs_previous_item(chunk_root, path, key.objectid, | |
1783 | key.type); | |
1784 | if (ret < 0) | |
1785 | goto error; | |
1786 | if (ret > 0) | |
1787 | break; | |
1a40e23b | 1788 | |
2b82032c YZ |
1789 | leaf = path->nodes[0]; |
1790 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1a40e23b | 1791 | |
2b82032c YZ |
1792 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
1793 | struct btrfs_chunk); | |
1794 | chunk_type = btrfs_chunk_type(leaf, chunk); | |
1795 | btrfs_release_path(chunk_root, path); | |
8f18cf13 | 1796 | |
2b82032c YZ |
1797 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
1798 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, | |
1799 | found_key.objectid, | |
1800 | found_key.offset); | |
1801 | BUG_ON(ret); | |
1802 | } | |
8f18cf13 | 1803 | |
2b82032c YZ |
1804 | if (found_key.offset == 0) |
1805 | break; | |
1806 | key.offset = found_key.offset - 1; | |
1807 | } | |
1808 | ret = 0; | |
1809 | error: | |
1810 | btrfs_free_path(path); | |
1811 | return ret; | |
8f18cf13 CM |
1812 | } |
1813 | ||
ec44a35c CM |
1814 | static u64 div_factor(u64 num, int factor) |
1815 | { | |
1816 | if (factor == 10) | |
1817 | return num; | |
1818 | num *= factor; | |
1819 | do_div(num, 10); | |
1820 | return num; | |
1821 | } | |
1822 | ||
ec44a35c CM |
1823 | int btrfs_balance(struct btrfs_root *dev_root) |
1824 | { | |
1825 | int ret; | |
ec44a35c CM |
1826 | struct list_head *devices = &dev_root->fs_info->fs_devices->devices; |
1827 | struct btrfs_device *device; | |
1828 | u64 old_size; | |
1829 | u64 size_to_free; | |
1830 | struct btrfs_path *path; | |
1831 | struct btrfs_key key; | |
1832 | struct btrfs_chunk *chunk; | |
1833 | struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root; | |
1834 | struct btrfs_trans_handle *trans; | |
1835 | struct btrfs_key found_key; | |
1836 | ||
2b82032c YZ |
1837 | if (dev_root->fs_info->sb->s_flags & MS_RDONLY) |
1838 | return -EROFS; | |
ec44a35c | 1839 | |
7d9eb12c | 1840 | mutex_lock(&dev_root->fs_info->volume_mutex); |
ec44a35c CM |
1841 | dev_root = dev_root->fs_info->dev_root; |
1842 | ||
ec44a35c | 1843 | /* step one make some room on all the devices */ |
c6e30871 | 1844 | list_for_each_entry(device, devices, dev_list) { |
ec44a35c CM |
1845 | old_size = device->total_bytes; |
1846 | size_to_free = div_factor(old_size, 1); | |
1847 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); | |
2b82032c YZ |
1848 | if (!device->writeable || |
1849 | device->total_bytes - device->bytes_used > size_to_free) | |
ec44a35c CM |
1850 | continue; |
1851 | ||
1852 | ret = btrfs_shrink_device(device, old_size - size_to_free); | |
1853 | BUG_ON(ret); | |
1854 | ||
1855 | trans = btrfs_start_transaction(dev_root, 1); | |
1856 | BUG_ON(!trans); | |
1857 | ||
1858 | ret = btrfs_grow_device(trans, device, old_size); | |
1859 | BUG_ON(ret); | |
1860 | ||
1861 | btrfs_end_transaction(trans, dev_root); | |
1862 | } | |
1863 | ||
1864 | /* step two, relocate all the chunks */ | |
1865 | path = btrfs_alloc_path(); | |
1866 | BUG_ON(!path); | |
1867 | ||
1868 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; | |
1869 | key.offset = (u64)-1; | |
1870 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
1871 | ||
d397712b | 1872 | while (1) { |
ec44a35c CM |
1873 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
1874 | if (ret < 0) | |
1875 | goto error; | |
1876 | ||
1877 | /* | |
1878 | * this shouldn't happen, it means the last relocate | |
1879 | * failed | |
1880 | */ | |
1881 | if (ret == 0) | |
1882 | break; | |
1883 | ||
1884 | ret = btrfs_previous_item(chunk_root, path, 0, | |
1885 | BTRFS_CHUNK_ITEM_KEY); | |
7d9eb12c | 1886 | if (ret) |
ec44a35c | 1887 | break; |
7d9eb12c | 1888 | |
ec44a35c CM |
1889 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
1890 | path->slots[0]); | |
1891 | if (found_key.objectid != key.objectid) | |
1892 | break; | |
7d9eb12c | 1893 | |
ec44a35c CM |
1894 | chunk = btrfs_item_ptr(path->nodes[0], |
1895 | path->slots[0], | |
1896 | struct btrfs_chunk); | |
1897 | key.offset = found_key.offset; | |
1898 | /* chunk zero is special */ | |
1899 | if (key.offset == 0) | |
1900 | break; | |
1901 | ||
7d9eb12c | 1902 | btrfs_release_path(chunk_root, path); |
ec44a35c CM |
1903 | ret = btrfs_relocate_chunk(chunk_root, |
1904 | chunk_root->root_key.objectid, | |
1905 | found_key.objectid, | |
1906 | found_key.offset); | |
1907 | BUG_ON(ret); | |
ec44a35c CM |
1908 | } |
1909 | ret = 0; | |
1910 | error: | |
1911 | btrfs_free_path(path); | |
7d9eb12c | 1912 | mutex_unlock(&dev_root->fs_info->volume_mutex); |
ec44a35c CM |
1913 | return ret; |
1914 | } | |
1915 | ||
8f18cf13 CM |
1916 | /* |
1917 | * shrinking a device means finding all of the device extents past | |
1918 | * the new size, and then following the back refs to the chunks. | |
1919 | * The chunk relocation code actually frees the device extent | |
1920 | */ | |
1921 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) | |
1922 | { | |
1923 | struct btrfs_trans_handle *trans; | |
1924 | struct btrfs_root *root = device->dev_root; | |
1925 | struct btrfs_dev_extent *dev_extent = NULL; | |
1926 | struct btrfs_path *path; | |
1927 | u64 length; | |
1928 | u64 chunk_tree; | |
1929 | u64 chunk_objectid; | |
1930 | u64 chunk_offset; | |
1931 | int ret; | |
1932 | int slot; | |
1933 | struct extent_buffer *l; | |
1934 | struct btrfs_key key; | |
1935 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
1936 | u64 old_total = btrfs_super_total_bytes(super_copy); | |
1937 | u64 diff = device->total_bytes - new_size; | |
1938 | ||
2b82032c YZ |
1939 | if (new_size >= device->total_bytes) |
1940 | return -EINVAL; | |
8f18cf13 CM |
1941 | |
1942 | path = btrfs_alloc_path(); | |
1943 | if (!path) | |
1944 | return -ENOMEM; | |
1945 | ||
1946 | trans = btrfs_start_transaction(root, 1); | |
1947 | if (!trans) { | |
1948 | ret = -ENOMEM; | |
1949 | goto done; | |
1950 | } | |
1951 | ||
1952 | path->reada = 2; | |
1953 | ||
7d9eb12c CM |
1954 | lock_chunks(root); |
1955 | ||
8f18cf13 | 1956 | device->total_bytes = new_size; |
2b82032c YZ |
1957 | if (device->writeable) |
1958 | device->fs_devices->total_rw_bytes -= diff; | |
7d9eb12c | 1959 | unlock_chunks(root); |
8f18cf13 CM |
1960 | btrfs_end_transaction(trans, root); |
1961 | ||
1962 | key.objectid = device->devid; | |
1963 | key.offset = (u64)-1; | |
1964 | key.type = BTRFS_DEV_EXTENT_KEY; | |
1965 | ||
1966 | while (1) { | |
1967 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1968 | if (ret < 0) | |
1969 | goto done; | |
1970 | ||
1971 | ret = btrfs_previous_item(root, path, 0, key.type); | |
1972 | if (ret < 0) | |
1973 | goto done; | |
1974 | if (ret) { | |
1975 | ret = 0; | |
1976 | goto done; | |
1977 | } | |
1978 | ||
1979 | l = path->nodes[0]; | |
1980 | slot = path->slots[0]; | |
1981 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
1982 | ||
1983 | if (key.objectid != device->devid) | |
1984 | goto done; | |
1985 | ||
1986 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
1987 | length = btrfs_dev_extent_length(l, dev_extent); | |
1988 | ||
1989 | if (key.offset + length <= new_size) | |
d6397bae | 1990 | break; |
8f18cf13 CM |
1991 | |
1992 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); | |
1993 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); | |
1994 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); | |
1995 | btrfs_release_path(root, path); | |
1996 | ||
1997 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, | |
1998 | chunk_offset); | |
1999 | if (ret) | |
2000 | goto done; | |
2001 | } | |
2002 | ||
d6397bae CB |
2003 | /* Shrinking succeeded, else we would be at "done". */ |
2004 | trans = btrfs_start_transaction(root, 1); | |
2005 | if (!trans) { | |
2006 | ret = -ENOMEM; | |
2007 | goto done; | |
2008 | } | |
2009 | lock_chunks(root); | |
2010 | ||
2011 | device->disk_total_bytes = new_size; | |
2012 | /* Now btrfs_update_device() will change the on-disk size. */ | |
2013 | ret = btrfs_update_device(trans, device); | |
2014 | if (ret) { | |
2015 | unlock_chunks(root); | |
2016 | btrfs_end_transaction(trans, root); | |
2017 | goto done; | |
2018 | } | |
2019 | WARN_ON(diff > old_total); | |
2020 | btrfs_set_super_total_bytes(super_copy, old_total - diff); | |
2021 | unlock_chunks(root); | |
2022 | btrfs_end_transaction(trans, root); | |
8f18cf13 CM |
2023 | done: |
2024 | btrfs_free_path(path); | |
2025 | return ret; | |
2026 | } | |
2027 | ||
b2950863 | 2028 | static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans, |
0b86a832 CM |
2029 | struct btrfs_root *root, |
2030 | struct btrfs_key *key, | |
2031 | struct btrfs_chunk *chunk, int item_size) | |
2032 | { | |
2033 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
2034 | struct btrfs_disk_key disk_key; | |
2035 | u32 array_size; | |
2036 | u8 *ptr; | |
2037 | ||
2038 | array_size = btrfs_super_sys_array_size(super_copy); | |
2039 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) | |
2040 | return -EFBIG; | |
2041 | ||
2042 | ptr = super_copy->sys_chunk_array + array_size; | |
2043 | btrfs_cpu_key_to_disk(&disk_key, key); | |
2044 | memcpy(ptr, &disk_key, sizeof(disk_key)); | |
2045 | ptr += sizeof(disk_key); | |
2046 | memcpy(ptr, chunk, item_size); | |
2047 | item_size += sizeof(disk_key); | |
2048 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); | |
2049 | return 0; | |
2050 | } | |
2051 | ||
d397712b | 2052 | static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size, |
a1b32a59 | 2053 | int num_stripes, int sub_stripes) |
9b3f68b9 CM |
2054 | { |
2055 | if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP)) | |
2056 | return calc_size; | |
2057 | else if (type & BTRFS_BLOCK_GROUP_RAID10) | |
2058 | return calc_size * (num_stripes / sub_stripes); | |
2059 | else | |
2060 | return calc_size * num_stripes; | |
2061 | } | |
2062 | ||
2b82032c YZ |
2063 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
2064 | struct btrfs_root *extent_root, | |
2065 | struct map_lookup **map_ret, | |
2066 | u64 *num_bytes, u64 *stripe_size, | |
2067 | u64 start, u64 type) | |
0b86a832 | 2068 | { |
593060d7 | 2069 | struct btrfs_fs_info *info = extent_root->fs_info; |
0b86a832 | 2070 | struct btrfs_device *device = NULL; |
2b82032c | 2071 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
6324fbf3 | 2072 | struct list_head *cur; |
2b82032c | 2073 | struct map_lookup *map = NULL; |
0b86a832 | 2074 | struct extent_map_tree *em_tree; |
0b86a832 | 2075 | struct extent_map *em; |
2b82032c | 2076 | struct list_head private_devs; |
a40a90a0 | 2077 | int min_stripe_size = 1 * 1024 * 1024; |
0b86a832 | 2078 | u64 calc_size = 1024 * 1024 * 1024; |
9b3f68b9 CM |
2079 | u64 max_chunk_size = calc_size; |
2080 | u64 min_free; | |
6324fbf3 CM |
2081 | u64 avail; |
2082 | u64 max_avail = 0; | |
2b82032c | 2083 | u64 dev_offset; |
6324fbf3 | 2084 | int num_stripes = 1; |
a40a90a0 | 2085 | int min_stripes = 1; |
321aecc6 | 2086 | int sub_stripes = 0; |
6324fbf3 | 2087 | int looped = 0; |
0b86a832 | 2088 | int ret; |
6324fbf3 | 2089 | int index; |
593060d7 | 2090 | int stripe_len = 64 * 1024; |
0b86a832 | 2091 | |
ec44a35c CM |
2092 | if ((type & BTRFS_BLOCK_GROUP_RAID1) && |
2093 | (type & BTRFS_BLOCK_GROUP_DUP)) { | |
2094 | WARN_ON(1); | |
2095 | type &= ~BTRFS_BLOCK_GROUP_DUP; | |
2096 | } | |
2b82032c | 2097 | if (list_empty(&fs_devices->alloc_list)) |
6324fbf3 | 2098 | return -ENOSPC; |
593060d7 | 2099 | |
a40a90a0 | 2100 | if (type & (BTRFS_BLOCK_GROUP_RAID0)) { |
2b82032c | 2101 | num_stripes = fs_devices->rw_devices; |
a40a90a0 CM |
2102 | min_stripes = 2; |
2103 | } | |
2104 | if (type & (BTRFS_BLOCK_GROUP_DUP)) { | |
611f0e00 | 2105 | num_stripes = 2; |
a40a90a0 CM |
2106 | min_stripes = 2; |
2107 | } | |
8790d502 | 2108 | if (type & (BTRFS_BLOCK_GROUP_RAID1)) { |
2b82032c | 2109 | num_stripes = min_t(u64, 2, fs_devices->rw_devices); |
9b3f68b9 CM |
2110 | if (num_stripes < 2) |
2111 | return -ENOSPC; | |
a40a90a0 | 2112 | min_stripes = 2; |
8790d502 | 2113 | } |
321aecc6 | 2114 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
2b82032c | 2115 | num_stripes = fs_devices->rw_devices; |
321aecc6 CM |
2116 | if (num_stripes < 4) |
2117 | return -ENOSPC; | |
2118 | num_stripes &= ~(u32)1; | |
2119 | sub_stripes = 2; | |
a40a90a0 | 2120 | min_stripes = 4; |
321aecc6 | 2121 | } |
9b3f68b9 CM |
2122 | |
2123 | if (type & BTRFS_BLOCK_GROUP_DATA) { | |
2124 | max_chunk_size = 10 * calc_size; | |
a40a90a0 | 2125 | min_stripe_size = 64 * 1024 * 1024; |
9b3f68b9 CM |
2126 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
2127 | max_chunk_size = 4 * calc_size; | |
a40a90a0 CM |
2128 | min_stripe_size = 32 * 1024 * 1024; |
2129 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { | |
2130 | calc_size = 8 * 1024 * 1024; | |
2131 | max_chunk_size = calc_size * 2; | |
2132 | min_stripe_size = 1 * 1024 * 1024; | |
9b3f68b9 CM |
2133 | } |
2134 | ||
2b82032c YZ |
2135 | /* we don't want a chunk larger than 10% of writeable space */ |
2136 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), | |
2137 | max_chunk_size); | |
9b3f68b9 | 2138 | |
a40a90a0 | 2139 | again: |
2b82032c YZ |
2140 | if (!map || map->num_stripes != num_stripes) { |
2141 | kfree(map); | |
2142 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); | |
2143 | if (!map) | |
2144 | return -ENOMEM; | |
2145 | map->num_stripes = num_stripes; | |
2146 | } | |
2147 | ||
9b3f68b9 CM |
2148 | if (calc_size * num_stripes > max_chunk_size) { |
2149 | calc_size = max_chunk_size; | |
2150 | do_div(calc_size, num_stripes); | |
2151 | do_div(calc_size, stripe_len); | |
2152 | calc_size *= stripe_len; | |
2153 | } | |
2154 | /* we don't want tiny stripes */ | |
a40a90a0 | 2155 | calc_size = max_t(u64, min_stripe_size, calc_size); |
9b3f68b9 | 2156 | |
9b3f68b9 CM |
2157 | do_div(calc_size, stripe_len); |
2158 | calc_size *= stripe_len; | |
2159 | ||
2b82032c | 2160 | cur = fs_devices->alloc_list.next; |
6324fbf3 | 2161 | index = 0; |
611f0e00 CM |
2162 | |
2163 | if (type & BTRFS_BLOCK_GROUP_DUP) | |
2164 | min_free = calc_size * 2; | |
9b3f68b9 CM |
2165 | else |
2166 | min_free = calc_size; | |
611f0e00 | 2167 | |
0f9dd46c JB |
2168 | /* |
2169 | * we add 1MB because we never use the first 1MB of the device, unless | |
2170 | * we've looped, then we are likely allocating the maximum amount of | |
2171 | * space left already | |
2172 | */ | |
2173 | if (!looped) | |
2174 | min_free += 1024 * 1024; | |
ad5bd91e | 2175 | |
2b82032c | 2176 | INIT_LIST_HEAD(&private_devs); |
d397712b | 2177 | while (index < num_stripes) { |
b3075717 | 2178 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
2b82032c | 2179 | BUG_ON(!device->writeable); |
dfe25020 CM |
2180 | if (device->total_bytes > device->bytes_used) |
2181 | avail = device->total_bytes - device->bytes_used; | |
2182 | else | |
2183 | avail = 0; | |
6324fbf3 | 2184 | cur = cur->next; |
8f18cf13 | 2185 | |
dfe25020 | 2186 | if (device->in_fs_metadata && avail >= min_free) { |
2b82032c YZ |
2187 | ret = find_free_dev_extent(trans, device, |
2188 | min_free, &dev_offset); | |
8f18cf13 CM |
2189 | if (ret == 0) { |
2190 | list_move_tail(&device->dev_alloc_list, | |
2191 | &private_devs); | |
2b82032c YZ |
2192 | map->stripes[index].dev = device; |
2193 | map->stripes[index].physical = dev_offset; | |
611f0e00 | 2194 | index++; |
2b82032c YZ |
2195 | if (type & BTRFS_BLOCK_GROUP_DUP) { |
2196 | map->stripes[index].dev = device; | |
2197 | map->stripes[index].physical = | |
2198 | dev_offset + calc_size; | |
8f18cf13 | 2199 | index++; |
2b82032c | 2200 | } |
8f18cf13 | 2201 | } |
dfe25020 | 2202 | } else if (device->in_fs_metadata && avail > max_avail) |
a40a90a0 | 2203 | max_avail = avail; |
2b82032c | 2204 | if (cur == &fs_devices->alloc_list) |
6324fbf3 CM |
2205 | break; |
2206 | } | |
2b82032c | 2207 | list_splice(&private_devs, &fs_devices->alloc_list); |
6324fbf3 | 2208 | if (index < num_stripes) { |
a40a90a0 CM |
2209 | if (index >= min_stripes) { |
2210 | num_stripes = index; | |
2211 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { | |
2212 | num_stripes /= sub_stripes; | |
2213 | num_stripes *= sub_stripes; | |
2214 | } | |
2215 | looped = 1; | |
2216 | goto again; | |
2217 | } | |
6324fbf3 CM |
2218 | if (!looped && max_avail > 0) { |
2219 | looped = 1; | |
2220 | calc_size = max_avail; | |
2221 | goto again; | |
2222 | } | |
2b82032c | 2223 | kfree(map); |
6324fbf3 CM |
2224 | return -ENOSPC; |
2225 | } | |
2b82032c YZ |
2226 | map->sector_size = extent_root->sectorsize; |
2227 | map->stripe_len = stripe_len; | |
2228 | map->io_align = stripe_len; | |
2229 | map->io_width = stripe_len; | |
2230 | map->type = type; | |
2231 | map->num_stripes = num_stripes; | |
2232 | map->sub_stripes = sub_stripes; | |
0b86a832 | 2233 | |
2b82032c YZ |
2234 | *map_ret = map; |
2235 | *stripe_size = calc_size; | |
2236 | *num_bytes = chunk_bytes_by_type(type, calc_size, | |
2237 | num_stripes, sub_stripes); | |
0b86a832 | 2238 | |
2b82032c YZ |
2239 | em = alloc_extent_map(GFP_NOFS); |
2240 | if (!em) { | |
2241 | kfree(map); | |
593060d7 CM |
2242 | return -ENOMEM; |
2243 | } | |
2b82032c YZ |
2244 | em->bdev = (struct block_device *)map; |
2245 | em->start = start; | |
2246 | em->len = *num_bytes; | |
2247 | em->block_start = 0; | |
2248 | em->block_len = em->len; | |
593060d7 | 2249 | |
2b82032c YZ |
2250 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
2251 | spin_lock(&em_tree->lock); | |
2252 | ret = add_extent_mapping(em_tree, em); | |
2253 | spin_unlock(&em_tree->lock); | |
2254 | BUG_ON(ret); | |
2255 | free_extent_map(em); | |
0b86a832 | 2256 | |
2b82032c YZ |
2257 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
2258 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
2259 | start, *num_bytes); | |
2260 | BUG_ON(ret); | |
611f0e00 | 2261 | |
2b82032c YZ |
2262 | index = 0; |
2263 | while (index < map->num_stripes) { | |
2264 | device = map->stripes[index].dev; | |
2265 | dev_offset = map->stripes[index].physical; | |
0b86a832 CM |
2266 | |
2267 | ret = btrfs_alloc_dev_extent(trans, device, | |
2b82032c YZ |
2268 | info->chunk_root->root_key.objectid, |
2269 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
2270 | start, dev_offset, calc_size); | |
0b86a832 | 2271 | BUG_ON(ret); |
2b82032c YZ |
2272 | index++; |
2273 | } | |
2274 | ||
2275 | return 0; | |
2276 | } | |
2277 | ||
2278 | static int __finish_chunk_alloc(struct btrfs_trans_handle *trans, | |
2279 | struct btrfs_root *extent_root, | |
2280 | struct map_lookup *map, u64 chunk_offset, | |
2281 | u64 chunk_size, u64 stripe_size) | |
2282 | { | |
2283 | u64 dev_offset; | |
2284 | struct btrfs_key key; | |
2285 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; | |
2286 | struct btrfs_device *device; | |
2287 | struct btrfs_chunk *chunk; | |
2288 | struct btrfs_stripe *stripe; | |
2289 | size_t item_size = btrfs_chunk_item_size(map->num_stripes); | |
2290 | int index = 0; | |
2291 | int ret; | |
2292 | ||
2293 | chunk = kzalloc(item_size, GFP_NOFS); | |
2294 | if (!chunk) | |
2295 | return -ENOMEM; | |
2296 | ||
2297 | index = 0; | |
2298 | while (index < map->num_stripes) { | |
2299 | device = map->stripes[index].dev; | |
2300 | device->bytes_used += stripe_size; | |
0b86a832 CM |
2301 | ret = btrfs_update_device(trans, device); |
2302 | BUG_ON(ret); | |
2b82032c YZ |
2303 | index++; |
2304 | } | |
2305 | ||
2306 | index = 0; | |
2307 | stripe = &chunk->stripe; | |
2308 | while (index < map->num_stripes) { | |
2309 | device = map->stripes[index].dev; | |
2310 | dev_offset = map->stripes[index].physical; | |
0b86a832 | 2311 | |
e17cade2 CM |
2312 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
2313 | btrfs_set_stack_stripe_offset(stripe, dev_offset); | |
2314 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); | |
2b82032c | 2315 | stripe++; |
0b86a832 CM |
2316 | index++; |
2317 | } | |
2318 | ||
2b82032c | 2319 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
0b86a832 | 2320 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
2b82032c YZ |
2321 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
2322 | btrfs_set_stack_chunk_type(chunk, map->type); | |
2323 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); | |
2324 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); | |
2325 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); | |
0b86a832 | 2326 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
2b82032c | 2327 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
0b86a832 | 2328 | |
2b82032c YZ |
2329 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
2330 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2331 | key.offset = chunk_offset; | |
0b86a832 | 2332 | |
2b82032c YZ |
2333 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
2334 | BUG_ON(ret); | |
0b86a832 | 2335 | |
2b82032c YZ |
2336 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
2337 | ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk, | |
2338 | item_size); | |
8f18cf13 CM |
2339 | BUG_ON(ret); |
2340 | } | |
0b86a832 | 2341 | kfree(chunk); |
2b82032c YZ |
2342 | return 0; |
2343 | } | |
0b86a832 | 2344 | |
2b82032c YZ |
2345 | /* |
2346 | * Chunk allocation falls into two parts. The first part does works | |
2347 | * that make the new allocated chunk useable, but not do any operation | |
2348 | * that modifies the chunk tree. The second part does the works that | |
2349 | * require modifying the chunk tree. This division is important for the | |
2350 | * bootstrap process of adding storage to a seed btrfs. | |
2351 | */ | |
2352 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, | |
2353 | struct btrfs_root *extent_root, u64 type) | |
2354 | { | |
2355 | u64 chunk_offset; | |
2356 | u64 chunk_size; | |
2357 | u64 stripe_size; | |
2358 | struct map_lookup *map; | |
2359 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; | |
2360 | int ret; | |
2361 | ||
2362 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
2363 | &chunk_offset); | |
2364 | if (ret) | |
2365 | return ret; | |
2366 | ||
2367 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, | |
2368 | &stripe_size, chunk_offset, type); | |
2369 | if (ret) | |
2370 | return ret; | |
2371 | ||
2372 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, | |
2373 | chunk_size, stripe_size); | |
2374 | BUG_ON(ret); | |
2375 | return 0; | |
2376 | } | |
2377 | ||
d397712b | 2378 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
2b82032c YZ |
2379 | struct btrfs_root *root, |
2380 | struct btrfs_device *device) | |
2381 | { | |
2382 | u64 chunk_offset; | |
2383 | u64 sys_chunk_offset; | |
2384 | u64 chunk_size; | |
2385 | u64 sys_chunk_size; | |
2386 | u64 stripe_size; | |
2387 | u64 sys_stripe_size; | |
2388 | u64 alloc_profile; | |
2389 | struct map_lookup *map; | |
2390 | struct map_lookup *sys_map; | |
2391 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2392 | struct btrfs_root *extent_root = fs_info->extent_root; | |
2393 | int ret; | |
2394 | ||
2395 | ret = find_next_chunk(fs_info->chunk_root, | |
2396 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset); | |
2397 | BUG_ON(ret); | |
2398 | ||
2399 | alloc_profile = BTRFS_BLOCK_GROUP_METADATA | | |
2400 | (fs_info->metadata_alloc_profile & | |
2401 | fs_info->avail_metadata_alloc_bits); | |
2402 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); | |
2403 | ||
2404 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, | |
2405 | &stripe_size, chunk_offset, alloc_profile); | |
2406 | BUG_ON(ret); | |
2407 | ||
2408 | sys_chunk_offset = chunk_offset + chunk_size; | |
2409 | ||
2410 | alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM | | |
2411 | (fs_info->system_alloc_profile & | |
2412 | fs_info->avail_system_alloc_bits); | |
2413 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); | |
2414 | ||
2415 | ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map, | |
2416 | &sys_chunk_size, &sys_stripe_size, | |
2417 | sys_chunk_offset, alloc_profile); | |
2418 | BUG_ON(ret); | |
2419 | ||
2420 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); | |
2421 | BUG_ON(ret); | |
2422 | ||
2423 | /* | |
2424 | * Modifying chunk tree needs allocating new blocks from both | |
2425 | * system block group and metadata block group. So we only can | |
2426 | * do operations require modifying the chunk tree after both | |
2427 | * block groups were created. | |
2428 | */ | |
2429 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, | |
2430 | chunk_size, stripe_size); | |
2431 | BUG_ON(ret); | |
2432 | ||
2433 | ret = __finish_chunk_alloc(trans, extent_root, sys_map, | |
2434 | sys_chunk_offset, sys_chunk_size, | |
2435 | sys_stripe_size); | |
b248a415 | 2436 | BUG_ON(ret); |
2b82032c YZ |
2437 | return 0; |
2438 | } | |
2439 | ||
2440 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) | |
2441 | { | |
2442 | struct extent_map *em; | |
2443 | struct map_lookup *map; | |
2444 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; | |
2445 | int readonly = 0; | |
2446 | int i; | |
2447 | ||
2448 | spin_lock(&map_tree->map_tree.lock); | |
2449 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); | |
2450 | spin_unlock(&map_tree->map_tree.lock); | |
2451 | if (!em) | |
2452 | return 1; | |
2453 | ||
2454 | map = (struct map_lookup *)em->bdev; | |
2455 | for (i = 0; i < map->num_stripes; i++) { | |
2456 | if (!map->stripes[i].dev->writeable) { | |
2457 | readonly = 1; | |
2458 | break; | |
2459 | } | |
2460 | } | |
0b86a832 | 2461 | free_extent_map(em); |
2b82032c | 2462 | return readonly; |
0b86a832 CM |
2463 | } |
2464 | ||
2465 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) | |
2466 | { | |
2467 | extent_map_tree_init(&tree->map_tree, GFP_NOFS); | |
2468 | } | |
2469 | ||
2470 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) | |
2471 | { | |
2472 | struct extent_map *em; | |
2473 | ||
d397712b | 2474 | while (1) { |
0b86a832 CM |
2475 | spin_lock(&tree->map_tree.lock); |
2476 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); | |
2477 | if (em) | |
2478 | remove_extent_mapping(&tree->map_tree, em); | |
2479 | spin_unlock(&tree->map_tree.lock); | |
2480 | if (!em) | |
2481 | break; | |
2482 | kfree(em->bdev); | |
2483 | /* once for us */ | |
2484 | free_extent_map(em); | |
2485 | /* once for the tree */ | |
2486 | free_extent_map(em); | |
2487 | } | |
2488 | } | |
2489 | ||
f188591e CM |
2490 | int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len) |
2491 | { | |
2492 | struct extent_map *em; | |
2493 | struct map_lookup *map; | |
2494 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
2495 | int ret; | |
2496 | ||
2497 | spin_lock(&em_tree->lock); | |
2498 | em = lookup_extent_mapping(em_tree, logical, len); | |
b248a415 | 2499 | spin_unlock(&em_tree->lock); |
f188591e CM |
2500 | BUG_ON(!em); |
2501 | ||
2502 | BUG_ON(em->start > logical || em->start + em->len < logical); | |
2503 | map = (struct map_lookup *)em->bdev; | |
2504 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) | |
2505 | ret = map->num_stripes; | |
321aecc6 CM |
2506 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
2507 | ret = map->sub_stripes; | |
f188591e CM |
2508 | else |
2509 | ret = 1; | |
2510 | free_extent_map(em); | |
f188591e CM |
2511 | return ret; |
2512 | } | |
2513 | ||
dfe25020 CM |
2514 | static int find_live_mirror(struct map_lookup *map, int first, int num, |
2515 | int optimal) | |
2516 | { | |
2517 | int i; | |
2518 | if (map->stripes[optimal].dev->bdev) | |
2519 | return optimal; | |
2520 | for (i = first; i < first + num; i++) { | |
2521 | if (map->stripes[i].dev->bdev) | |
2522 | return i; | |
2523 | } | |
2524 | /* we couldn't find one that doesn't fail. Just return something | |
2525 | * and the io error handling code will clean up eventually | |
2526 | */ | |
2527 | return optimal; | |
2528 | } | |
2529 | ||
f2d8d74d CM |
2530 | static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
2531 | u64 logical, u64 *length, | |
2532 | struct btrfs_multi_bio **multi_ret, | |
2533 | int mirror_num, struct page *unplug_page) | |
0b86a832 CM |
2534 | { |
2535 | struct extent_map *em; | |
2536 | struct map_lookup *map; | |
2537 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
2538 | u64 offset; | |
593060d7 CM |
2539 | u64 stripe_offset; |
2540 | u64 stripe_nr; | |
cea9e445 | 2541 | int stripes_allocated = 8; |
321aecc6 | 2542 | int stripes_required = 1; |
593060d7 | 2543 | int stripe_index; |
cea9e445 | 2544 | int i; |
f2d8d74d | 2545 | int num_stripes; |
a236aed1 | 2546 | int max_errors = 0; |
cea9e445 | 2547 | struct btrfs_multi_bio *multi = NULL; |
0b86a832 | 2548 | |
d397712b | 2549 | if (multi_ret && !(rw & (1 << BIO_RW))) |
cea9e445 | 2550 | stripes_allocated = 1; |
cea9e445 CM |
2551 | again: |
2552 | if (multi_ret) { | |
2553 | multi = kzalloc(btrfs_multi_bio_size(stripes_allocated), | |
2554 | GFP_NOFS); | |
2555 | if (!multi) | |
2556 | return -ENOMEM; | |
a236aed1 CM |
2557 | |
2558 | atomic_set(&multi->error, 0); | |
cea9e445 | 2559 | } |
0b86a832 CM |
2560 | |
2561 | spin_lock(&em_tree->lock); | |
2562 | em = lookup_extent_mapping(em_tree, logical, *length); | |
b248a415 | 2563 | spin_unlock(&em_tree->lock); |
f2d8d74d CM |
2564 | |
2565 | if (!em && unplug_page) | |
2566 | return 0; | |
2567 | ||
3b951516 | 2568 | if (!em) { |
d397712b CM |
2569 | printk(KERN_CRIT "unable to find logical %llu len %llu\n", |
2570 | (unsigned long long)logical, | |
2571 | (unsigned long long)*length); | |
f2d8d74d | 2572 | BUG(); |
3b951516 | 2573 | } |
0b86a832 CM |
2574 | |
2575 | BUG_ON(em->start > logical || em->start + em->len < logical); | |
2576 | map = (struct map_lookup *)em->bdev; | |
2577 | offset = logical - em->start; | |
593060d7 | 2578 | |
f188591e CM |
2579 | if (mirror_num > map->num_stripes) |
2580 | mirror_num = 0; | |
2581 | ||
cea9e445 | 2582 | /* if our multi bio struct is too small, back off and try again */ |
321aecc6 CM |
2583 | if (rw & (1 << BIO_RW)) { |
2584 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | | |
2585 | BTRFS_BLOCK_GROUP_DUP)) { | |
2586 | stripes_required = map->num_stripes; | |
a236aed1 | 2587 | max_errors = 1; |
321aecc6 CM |
2588 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
2589 | stripes_required = map->sub_stripes; | |
a236aed1 | 2590 | max_errors = 1; |
321aecc6 CM |
2591 | } |
2592 | } | |
ffbd517d | 2593 | if (multi_ret && (rw & (1 << BIO_RW)) && |
321aecc6 | 2594 | stripes_allocated < stripes_required) { |
cea9e445 | 2595 | stripes_allocated = map->num_stripes; |
cea9e445 CM |
2596 | free_extent_map(em); |
2597 | kfree(multi); | |
2598 | goto again; | |
2599 | } | |
593060d7 CM |
2600 | stripe_nr = offset; |
2601 | /* | |
2602 | * stripe_nr counts the total number of stripes we have to stride | |
2603 | * to get to this block | |
2604 | */ | |
2605 | do_div(stripe_nr, map->stripe_len); | |
2606 | ||
2607 | stripe_offset = stripe_nr * map->stripe_len; | |
2608 | BUG_ON(offset < stripe_offset); | |
2609 | ||
2610 | /* stripe_offset is the offset of this block in its stripe*/ | |
2611 | stripe_offset = offset - stripe_offset; | |
2612 | ||
cea9e445 | 2613 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | |
321aecc6 | 2614 | BTRFS_BLOCK_GROUP_RAID10 | |
cea9e445 CM |
2615 | BTRFS_BLOCK_GROUP_DUP)) { |
2616 | /* we limit the length of each bio to what fits in a stripe */ | |
2617 | *length = min_t(u64, em->len - offset, | |
2618 | map->stripe_len - stripe_offset); | |
2619 | } else { | |
2620 | *length = em->len - offset; | |
2621 | } | |
f2d8d74d CM |
2622 | |
2623 | if (!multi_ret && !unplug_page) | |
cea9e445 CM |
2624 | goto out; |
2625 | ||
f2d8d74d | 2626 | num_stripes = 1; |
cea9e445 | 2627 | stripe_index = 0; |
8790d502 | 2628 | if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
f2d8d74d CM |
2629 | if (unplug_page || (rw & (1 << BIO_RW))) |
2630 | num_stripes = map->num_stripes; | |
2fff734f | 2631 | else if (mirror_num) |
f188591e | 2632 | stripe_index = mirror_num - 1; |
dfe25020 CM |
2633 | else { |
2634 | stripe_index = find_live_mirror(map, 0, | |
2635 | map->num_stripes, | |
2636 | current->pid % map->num_stripes); | |
2637 | } | |
2fff734f | 2638 | |
611f0e00 | 2639 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
cea9e445 | 2640 | if (rw & (1 << BIO_RW)) |
f2d8d74d | 2641 | num_stripes = map->num_stripes; |
f188591e CM |
2642 | else if (mirror_num) |
2643 | stripe_index = mirror_num - 1; | |
2fff734f | 2644 | |
321aecc6 CM |
2645 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
2646 | int factor = map->num_stripes / map->sub_stripes; | |
321aecc6 CM |
2647 | |
2648 | stripe_index = do_div(stripe_nr, factor); | |
2649 | stripe_index *= map->sub_stripes; | |
2650 | ||
f2d8d74d CM |
2651 | if (unplug_page || (rw & (1 << BIO_RW))) |
2652 | num_stripes = map->sub_stripes; | |
321aecc6 CM |
2653 | else if (mirror_num) |
2654 | stripe_index += mirror_num - 1; | |
dfe25020 CM |
2655 | else { |
2656 | stripe_index = find_live_mirror(map, stripe_index, | |
2657 | map->sub_stripes, stripe_index + | |
2658 | current->pid % map->sub_stripes); | |
2659 | } | |
8790d502 CM |
2660 | } else { |
2661 | /* | |
2662 | * after this do_div call, stripe_nr is the number of stripes | |
2663 | * on this device we have to walk to find the data, and | |
2664 | * stripe_index is the number of our device in the stripe array | |
2665 | */ | |
2666 | stripe_index = do_div(stripe_nr, map->num_stripes); | |
2667 | } | |
593060d7 | 2668 | BUG_ON(stripe_index >= map->num_stripes); |
cea9e445 | 2669 | |
f2d8d74d CM |
2670 | for (i = 0; i < num_stripes; i++) { |
2671 | if (unplug_page) { | |
2672 | struct btrfs_device *device; | |
2673 | struct backing_dev_info *bdi; | |
2674 | ||
2675 | device = map->stripes[stripe_index].dev; | |
dfe25020 CM |
2676 | if (device->bdev) { |
2677 | bdi = blk_get_backing_dev_info(device->bdev); | |
d397712b | 2678 | if (bdi->unplug_io_fn) |
dfe25020 | 2679 | bdi->unplug_io_fn(bdi, unplug_page); |
f2d8d74d CM |
2680 | } |
2681 | } else { | |
2682 | multi->stripes[i].physical = | |
2683 | map->stripes[stripe_index].physical + | |
2684 | stripe_offset + stripe_nr * map->stripe_len; | |
2685 | multi->stripes[i].dev = map->stripes[stripe_index].dev; | |
2686 | } | |
cea9e445 | 2687 | stripe_index++; |
593060d7 | 2688 | } |
f2d8d74d CM |
2689 | if (multi_ret) { |
2690 | *multi_ret = multi; | |
2691 | multi->num_stripes = num_stripes; | |
a236aed1 | 2692 | multi->max_errors = max_errors; |
f2d8d74d | 2693 | } |
cea9e445 | 2694 | out: |
0b86a832 | 2695 | free_extent_map(em); |
0b86a832 CM |
2696 | return 0; |
2697 | } | |
2698 | ||
f2d8d74d CM |
2699 | int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
2700 | u64 logical, u64 *length, | |
2701 | struct btrfs_multi_bio **multi_ret, int mirror_num) | |
2702 | { | |
2703 | return __btrfs_map_block(map_tree, rw, logical, length, multi_ret, | |
2704 | mirror_num, NULL); | |
2705 | } | |
2706 | ||
a512bbf8 YZ |
2707 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
2708 | u64 chunk_start, u64 physical, u64 devid, | |
2709 | u64 **logical, int *naddrs, int *stripe_len) | |
2710 | { | |
2711 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
2712 | struct extent_map *em; | |
2713 | struct map_lookup *map; | |
2714 | u64 *buf; | |
2715 | u64 bytenr; | |
2716 | u64 length; | |
2717 | u64 stripe_nr; | |
2718 | int i, j, nr = 0; | |
2719 | ||
2720 | spin_lock(&em_tree->lock); | |
2721 | em = lookup_extent_mapping(em_tree, chunk_start, 1); | |
2722 | spin_unlock(&em_tree->lock); | |
2723 | ||
2724 | BUG_ON(!em || em->start != chunk_start); | |
2725 | map = (struct map_lookup *)em->bdev; | |
2726 | ||
2727 | length = em->len; | |
2728 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) | |
2729 | do_div(length, map->num_stripes / map->sub_stripes); | |
2730 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) | |
2731 | do_div(length, map->num_stripes); | |
2732 | ||
2733 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); | |
2734 | BUG_ON(!buf); | |
2735 | ||
2736 | for (i = 0; i < map->num_stripes; i++) { | |
2737 | if (devid && map->stripes[i].dev->devid != devid) | |
2738 | continue; | |
2739 | if (map->stripes[i].physical > physical || | |
2740 | map->stripes[i].physical + length <= physical) | |
2741 | continue; | |
2742 | ||
2743 | stripe_nr = physical - map->stripes[i].physical; | |
2744 | do_div(stripe_nr, map->stripe_len); | |
2745 | ||
2746 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { | |
2747 | stripe_nr = stripe_nr * map->num_stripes + i; | |
2748 | do_div(stripe_nr, map->sub_stripes); | |
2749 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { | |
2750 | stripe_nr = stripe_nr * map->num_stripes + i; | |
2751 | } | |
2752 | bytenr = chunk_start + stripe_nr * map->stripe_len; | |
934d375b | 2753 | WARN_ON(nr >= map->num_stripes); |
a512bbf8 YZ |
2754 | for (j = 0; j < nr; j++) { |
2755 | if (buf[j] == bytenr) | |
2756 | break; | |
2757 | } | |
934d375b CM |
2758 | if (j == nr) { |
2759 | WARN_ON(nr >= map->num_stripes); | |
a512bbf8 | 2760 | buf[nr++] = bytenr; |
934d375b | 2761 | } |
a512bbf8 YZ |
2762 | } |
2763 | ||
2764 | for (i = 0; i > nr; i++) { | |
2765 | struct btrfs_multi_bio *multi; | |
2766 | struct btrfs_bio_stripe *stripe; | |
2767 | int ret; | |
2768 | ||
2769 | length = 1; | |
2770 | ret = btrfs_map_block(map_tree, WRITE, buf[i], | |
2771 | &length, &multi, 0); | |
2772 | BUG_ON(ret); | |
2773 | ||
2774 | stripe = multi->stripes; | |
2775 | for (j = 0; j < multi->num_stripes; j++) { | |
2776 | if (stripe->physical >= physical && | |
2777 | physical < stripe->physical + length) | |
2778 | break; | |
2779 | } | |
2780 | BUG_ON(j >= multi->num_stripes); | |
2781 | kfree(multi); | |
2782 | } | |
2783 | ||
2784 | *logical = buf; | |
2785 | *naddrs = nr; | |
2786 | *stripe_len = map->stripe_len; | |
2787 | ||
2788 | free_extent_map(em); | |
2789 | return 0; | |
2790 | } | |
2791 | ||
f2d8d74d CM |
2792 | int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree, |
2793 | u64 logical, struct page *page) | |
2794 | { | |
2795 | u64 length = PAGE_CACHE_SIZE; | |
2796 | return __btrfs_map_block(map_tree, READ, logical, &length, | |
2797 | NULL, 0, page); | |
2798 | } | |
2799 | ||
8790d502 | 2800 | static void end_bio_multi_stripe(struct bio *bio, int err) |
8790d502 | 2801 | { |
cea9e445 | 2802 | struct btrfs_multi_bio *multi = bio->bi_private; |
7d2b4daa | 2803 | int is_orig_bio = 0; |
8790d502 | 2804 | |
8790d502 | 2805 | if (err) |
a236aed1 | 2806 | atomic_inc(&multi->error); |
8790d502 | 2807 | |
7d2b4daa CM |
2808 | if (bio == multi->orig_bio) |
2809 | is_orig_bio = 1; | |
2810 | ||
cea9e445 | 2811 | if (atomic_dec_and_test(&multi->stripes_pending)) { |
7d2b4daa CM |
2812 | if (!is_orig_bio) { |
2813 | bio_put(bio); | |
2814 | bio = multi->orig_bio; | |
2815 | } | |
8790d502 CM |
2816 | bio->bi_private = multi->private; |
2817 | bio->bi_end_io = multi->end_io; | |
a236aed1 CM |
2818 | /* only send an error to the higher layers if it is |
2819 | * beyond the tolerance of the multi-bio | |
2820 | */ | |
1259ab75 | 2821 | if (atomic_read(&multi->error) > multi->max_errors) { |
a236aed1 | 2822 | err = -EIO; |
1259ab75 CM |
2823 | } else if (err) { |
2824 | /* | |
2825 | * this bio is actually up to date, we didn't | |
2826 | * go over the max number of errors | |
2827 | */ | |
2828 | set_bit(BIO_UPTODATE, &bio->bi_flags); | |
a236aed1 | 2829 | err = 0; |
1259ab75 | 2830 | } |
8790d502 CM |
2831 | kfree(multi); |
2832 | ||
2833 | bio_endio(bio, err); | |
7d2b4daa | 2834 | } else if (!is_orig_bio) { |
8790d502 CM |
2835 | bio_put(bio); |
2836 | } | |
8790d502 CM |
2837 | } |
2838 | ||
8b712842 CM |
2839 | struct async_sched { |
2840 | struct bio *bio; | |
2841 | int rw; | |
2842 | struct btrfs_fs_info *info; | |
2843 | struct btrfs_work work; | |
2844 | }; | |
2845 | ||
2846 | /* | |
2847 | * see run_scheduled_bios for a description of why bios are collected for | |
2848 | * async submit. | |
2849 | * | |
2850 | * This will add one bio to the pending list for a device and make sure | |
2851 | * the work struct is scheduled. | |
2852 | */ | |
d397712b | 2853 | static noinline int schedule_bio(struct btrfs_root *root, |
a1b32a59 CM |
2854 | struct btrfs_device *device, |
2855 | int rw, struct bio *bio) | |
8b712842 CM |
2856 | { |
2857 | int should_queue = 1; | |
ffbd517d | 2858 | struct btrfs_pending_bios *pending_bios; |
8b712842 CM |
2859 | |
2860 | /* don't bother with additional async steps for reads, right now */ | |
2861 | if (!(rw & (1 << BIO_RW))) { | |
492bb6de | 2862 | bio_get(bio); |
8b712842 | 2863 | submit_bio(rw, bio); |
492bb6de | 2864 | bio_put(bio); |
8b712842 CM |
2865 | return 0; |
2866 | } | |
2867 | ||
2868 | /* | |
0986fe9e | 2869 | * nr_async_bios allows us to reliably return congestion to the |
8b712842 CM |
2870 | * higher layers. Otherwise, the async bio makes it appear we have |
2871 | * made progress against dirty pages when we've really just put it | |
2872 | * on a queue for later | |
2873 | */ | |
0986fe9e | 2874 | atomic_inc(&root->fs_info->nr_async_bios); |
492bb6de | 2875 | WARN_ON(bio->bi_next); |
8b712842 CM |
2876 | bio->bi_next = NULL; |
2877 | bio->bi_rw |= rw; | |
2878 | ||
2879 | spin_lock(&device->io_lock); | |
ffbd517d CM |
2880 | if (bio_sync(bio)) |
2881 | pending_bios = &device->pending_sync_bios; | |
2882 | else | |
2883 | pending_bios = &device->pending_bios; | |
8b712842 | 2884 | |
ffbd517d CM |
2885 | if (pending_bios->tail) |
2886 | pending_bios->tail->bi_next = bio; | |
8b712842 | 2887 | |
ffbd517d CM |
2888 | pending_bios->tail = bio; |
2889 | if (!pending_bios->head) | |
2890 | pending_bios->head = bio; | |
8b712842 CM |
2891 | if (device->running_pending) |
2892 | should_queue = 0; | |
2893 | ||
2894 | spin_unlock(&device->io_lock); | |
2895 | ||
2896 | if (should_queue) | |
1cc127b5 CM |
2897 | btrfs_queue_worker(&root->fs_info->submit_workers, |
2898 | &device->work); | |
8b712842 CM |
2899 | return 0; |
2900 | } | |
2901 | ||
f188591e | 2902 | int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio, |
8b712842 | 2903 | int mirror_num, int async_submit) |
0b86a832 CM |
2904 | { |
2905 | struct btrfs_mapping_tree *map_tree; | |
2906 | struct btrfs_device *dev; | |
8790d502 | 2907 | struct bio *first_bio = bio; |
a62b9401 | 2908 | u64 logical = (u64)bio->bi_sector << 9; |
0b86a832 CM |
2909 | u64 length = 0; |
2910 | u64 map_length; | |
cea9e445 | 2911 | struct btrfs_multi_bio *multi = NULL; |
0b86a832 | 2912 | int ret; |
8790d502 CM |
2913 | int dev_nr = 0; |
2914 | int total_devs = 1; | |
0b86a832 | 2915 | |
f2d8d74d | 2916 | length = bio->bi_size; |
0b86a832 CM |
2917 | map_tree = &root->fs_info->mapping_tree; |
2918 | map_length = length; | |
cea9e445 | 2919 | |
f188591e CM |
2920 | ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi, |
2921 | mirror_num); | |
cea9e445 CM |
2922 | BUG_ON(ret); |
2923 | ||
2924 | total_devs = multi->num_stripes; | |
2925 | if (map_length < length) { | |
d397712b CM |
2926 | printk(KERN_CRIT "mapping failed logical %llu bio len %llu " |
2927 | "len %llu\n", (unsigned long long)logical, | |
2928 | (unsigned long long)length, | |
2929 | (unsigned long long)map_length); | |
cea9e445 CM |
2930 | BUG(); |
2931 | } | |
2932 | multi->end_io = first_bio->bi_end_io; | |
2933 | multi->private = first_bio->bi_private; | |
7d2b4daa | 2934 | multi->orig_bio = first_bio; |
cea9e445 CM |
2935 | atomic_set(&multi->stripes_pending, multi->num_stripes); |
2936 | ||
d397712b | 2937 | while (dev_nr < total_devs) { |
8790d502 | 2938 | if (total_devs > 1) { |
8790d502 CM |
2939 | if (dev_nr < total_devs - 1) { |
2940 | bio = bio_clone(first_bio, GFP_NOFS); | |
2941 | BUG_ON(!bio); | |
2942 | } else { | |
2943 | bio = first_bio; | |
2944 | } | |
2945 | bio->bi_private = multi; | |
2946 | bio->bi_end_io = end_bio_multi_stripe; | |
2947 | } | |
cea9e445 CM |
2948 | bio->bi_sector = multi->stripes[dev_nr].physical >> 9; |
2949 | dev = multi->stripes[dev_nr].dev; | |
2b82032c | 2950 | BUG_ON(rw == WRITE && !dev->writeable); |
dfe25020 CM |
2951 | if (dev && dev->bdev) { |
2952 | bio->bi_bdev = dev->bdev; | |
8b712842 CM |
2953 | if (async_submit) |
2954 | schedule_bio(root, dev, rw, bio); | |
2955 | else | |
2956 | submit_bio(rw, bio); | |
dfe25020 CM |
2957 | } else { |
2958 | bio->bi_bdev = root->fs_info->fs_devices->latest_bdev; | |
2959 | bio->bi_sector = logical >> 9; | |
dfe25020 | 2960 | bio_endio(bio, -EIO); |
dfe25020 | 2961 | } |
8790d502 CM |
2962 | dev_nr++; |
2963 | } | |
cea9e445 CM |
2964 | if (total_devs == 1) |
2965 | kfree(multi); | |
0b86a832 CM |
2966 | return 0; |
2967 | } | |
2968 | ||
a443755f | 2969 | struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid, |
2b82032c | 2970 | u8 *uuid, u8 *fsid) |
0b86a832 | 2971 | { |
2b82032c YZ |
2972 | struct btrfs_device *device; |
2973 | struct btrfs_fs_devices *cur_devices; | |
2974 | ||
2975 | cur_devices = root->fs_info->fs_devices; | |
2976 | while (cur_devices) { | |
2977 | if (!fsid || | |
2978 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { | |
2979 | device = __find_device(&cur_devices->devices, | |
2980 | devid, uuid); | |
2981 | if (device) | |
2982 | return device; | |
2983 | } | |
2984 | cur_devices = cur_devices->seed; | |
2985 | } | |
2986 | return NULL; | |
0b86a832 CM |
2987 | } |
2988 | ||
dfe25020 CM |
2989 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
2990 | u64 devid, u8 *dev_uuid) | |
2991 | { | |
2992 | struct btrfs_device *device; | |
2993 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
2994 | ||
2995 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
7cbd8a83 | 2996 | if (!device) |
2997 | return NULL; | |
dfe25020 CM |
2998 | list_add(&device->dev_list, |
2999 | &fs_devices->devices); | |
dfe25020 CM |
3000 | device->barriers = 1; |
3001 | device->dev_root = root->fs_info->dev_root; | |
3002 | device->devid = devid; | |
8b712842 | 3003 | device->work.func = pending_bios_fn; |
e4404d6e | 3004 | device->fs_devices = fs_devices; |
dfe25020 CM |
3005 | fs_devices->num_devices++; |
3006 | spin_lock_init(&device->io_lock); | |
d20f7043 | 3007 | INIT_LIST_HEAD(&device->dev_alloc_list); |
dfe25020 CM |
3008 | memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE); |
3009 | return device; | |
3010 | } | |
3011 | ||
0b86a832 CM |
3012 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
3013 | struct extent_buffer *leaf, | |
3014 | struct btrfs_chunk *chunk) | |
3015 | { | |
3016 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; | |
3017 | struct map_lookup *map; | |
3018 | struct extent_map *em; | |
3019 | u64 logical; | |
3020 | u64 length; | |
3021 | u64 devid; | |
a443755f | 3022 | u8 uuid[BTRFS_UUID_SIZE]; |
593060d7 | 3023 | int num_stripes; |
0b86a832 | 3024 | int ret; |
593060d7 | 3025 | int i; |
0b86a832 | 3026 | |
e17cade2 CM |
3027 | logical = key->offset; |
3028 | length = btrfs_chunk_length(leaf, chunk); | |
a061fc8d | 3029 | |
0b86a832 CM |
3030 | spin_lock(&map_tree->map_tree.lock); |
3031 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); | |
b248a415 | 3032 | spin_unlock(&map_tree->map_tree.lock); |
0b86a832 CM |
3033 | |
3034 | /* already mapped? */ | |
3035 | if (em && em->start <= logical && em->start + em->len > logical) { | |
3036 | free_extent_map(em); | |
0b86a832 CM |
3037 | return 0; |
3038 | } else if (em) { | |
3039 | free_extent_map(em); | |
3040 | } | |
0b86a832 | 3041 | |
0b86a832 CM |
3042 | em = alloc_extent_map(GFP_NOFS); |
3043 | if (!em) | |
3044 | return -ENOMEM; | |
593060d7 CM |
3045 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
3046 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); | |
0b86a832 CM |
3047 | if (!map) { |
3048 | free_extent_map(em); | |
3049 | return -ENOMEM; | |
3050 | } | |
3051 | ||
3052 | em->bdev = (struct block_device *)map; | |
3053 | em->start = logical; | |
3054 | em->len = length; | |
3055 | em->block_start = 0; | |
c8b97818 | 3056 | em->block_len = em->len; |
0b86a832 | 3057 | |
593060d7 CM |
3058 | map->num_stripes = num_stripes; |
3059 | map->io_width = btrfs_chunk_io_width(leaf, chunk); | |
3060 | map->io_align = btrfs_chunk_io_align(leaf, chunk); | |
3061 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); | |
3062 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); | |
3063 | map->type = btrfs_chunk_type(leaf, chunk); | |
321aecc6 | 3064 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
593060d7 CM |
3065 | for (i = 0; i < num_stripes; i++) { |
3066 | map->stripes[i].physical = | |
3067 | btrfs_stripe_offset_nr(leaf, chunk, i); | |
3068 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); | |
a443755f CM |
3069 | read_extent_buffer(leaf, uuid, (unsigned long) |
3070 | btrfs_stripe_dev_uuid_nr(chunk, i), | |
3071 | BTRFS_UUID_SIZE); | |
2b82032c YZ |
3072 | map->stripes[i].dev = btrfs_find_device(root, devid, uuid, |
3073 | NULL); | |
dfe25020 | 3074 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
593060d7 CM |
3075 | kfree(map); |
3076 | free_extent_map(em); | |
3077 | return -EIO; | |
3078 | } | |
dfe25020 CM |
3079 | if (!map->stripes[i].dev) { |
3080 | map->stripes[i].dev = | |
3081 | add_missing_dev(root, devid, uuid); | |
3082 | if (!map->stripes[i].dev) { | |
3083 | kfree(map); | |
3084 | free_extent_map(em); | |
3085 | return -EIO; | |
3086 | } | |
3087 | } | |
3088 | map->stripes[i].dev->in_fs_metadata = 1; | |
0b86a832 CM |
3089 | } |
3090 | ||
3091 | spin_lock(&map_tree->map_tree.lock); | |
3092 | ret = add_extent_mapping(&map_tree->map_tree, em); | |
0b86a832 | 3093 | spin_unlock(&map_tree->map_tree.lock); |
b248a415 | 3094 | BUG_ON(ret); |
0b86a832 CM |
3095 | free_extent_map(em); |
3096 | ||
3097 | return 0; | |
3098 | } | |
3099 | ||
3100 | static int fill_device_from_item(struct extent_buffer *leaf, | |
3101 | struct btrfs_dev_item *dev_item, | |
3102 | struct btrfs_device *device) | |
3103 | { | |
3104 | unsigned long ptr; | |
0b86a832 CM |
3105 | |
3106 | device->devid = btrfs_device_id(leaf, dev_item); | |
d6397bae CB |
3107 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
3108 | device->total_bytes = device->disk_total_bytes; | |
0b86a832 CM |
3109 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
3110 | device->type = btrfs_device_type(leaf, dev_item); | |
3111 | device->io_align = btrfs_device_io_align(leaf, dev_item); | |
3112 | device->io_width = btrfs_device_io_width(leaf, dev_item); | |
3113 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); | |
0b86a832 CM |
3114 | |
3115 | ptr = (unsigned long)btrfs_device_uuid(dev_item); | |
e17cade2 | 3116 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
0b86a832 | 3117 | |
0b86a832 CM |
3118 | return 0; |
3119 | } | |
3120 | ||
2b82032c YZ |
3121 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
3122 | { | |
3123 | struct btrfs_fs_devices *fs_devices; | |
3124 | int ret; | |
3125 | ||
3126 | mutex_lock(&uuid_mutex); | |
3127 | ||
3128 | fs_devices = root->fs_info->fs_devices->seed; | |
3129 | while (fs_devices) { | |
3130 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { | |
3131 | ret = 0; | |
3132 | goto out; | |
3133 | } | |
3134 | fs_devices = fs_devices->seed; | |
3135 | } | |
3136 | ||
3137 | fs_devices = find_fsid(fsid); | |
3138 | if (!fs_devices) { | |
3139 | ret = -ENOENT; | |
3140 | goto out; | |
3141 | } | |
e4404d6e YZ |
3142 | |
3143 | fs_devices = clone_fs_devices(fs_devices); | |
3144 | if (IS_ERR(fs_devices)) { | |
3145 | ret = PTR_ERR(fs_devices); | |
2b82032c YZ |
3146 | goto out; |
3147 | } | |
3148 | ||
97288f2c | 3149 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
15916de8 | 3150 | root->fs_info->bdev_holder); |
2b82032c YZ |
3151 | if (ret) |
3152 | goto out; | |
3153 | ||
3154 | if (!fs_devices->seeding) { | |
3155 | __btrfs_close_devices(fs_devices); | |
e4404d6e | 3156 | free_fs_devices(fs_devices); |
2b82032c YZ |
3157 | ret = -EINVAL; |
3158 | goto out; | |
3159 | } | |
3160 | ||
3161 | fs_devices->seed = root->fs_info->fs_devices->seed; | |
3162 | root->fs_info->fs_devices->seed = fs_devices; | |
2b82032c YZ |
3163 | out: |
3164 | mutex_unlock(&uuid_mutex); | |
3165 | return ret; | |
3166 | } | |
3167 | ||
0d81ba5d | 3168 | static int read_one_dev(struct btrfs_root *root, |
0b86a832 CM |
3169 | struct extent_buffer *leaf, |
3170 | struct btrfs_dev_item *dev_item) | |
3171 | { | |
3172 | struct btrfs_device *device; | |
3173 | u64 devid; | |
3174 | int ret; | |
2b82032c | 3175 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
a443755f CM |
3176 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
3177 | ||
0b86a832 | 3178 | devid = btrfs_device_id(leaf, dev_item); |
a443755f CM |
3179 | read_extent_buffer(leaf, dev_uuid, |
3180 | (unsigned long)btrfs_device_uuid(dev_item), | |
3181 | BTRFS_UUID_SIZE); | |
2b82032c YZ |
3182 | read_extent_buffer(leaf, fs_uuid, |
3183 | (unsigned long)btrfs_device_fsid(dev_item), | |
3184 | BTRFS_UUID_SIZE); | |
3185 | ||
3186 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { | |
3187 | ret = open_seed_devices(root, fs_uuid); | |
e4404d6e | 3188 | if (ret && !btrfs_test_opt(root, DEGRADED)) |
2b82032c | 3189 | return ret; |
2b82032c YZ |
3190 | } |
3191 | ||
3192 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); | |
3193 | if (!device || !device->bdev) { | |
e4404d6e | 3194 | if (!btrfs_test_opt(root, DEGRADED)) |
2b82032c YZ |
3195 | return -EIO; |
3196 | ||
3197 | if (!device) { | |
d397712b CM |
3198 | printk(KERN_WARNING "warning devid %llu missing\n", |
3199 | (unsigned long long)devid); | |
2b82032c YZ |
3200 | device = add_missing_dev(root, devid, dev_uuid); |
3201 | if (!device) | |
3202 | return -ENOMEM; | |
3203 | } | |
3204 | } | |
3205 | ||
3206 | if (device->fs_devices != root->fs_info->fs_devices) { | |
3207 | BUG_ON(device->writeable); | |
3208 | if (device->generation != | |
3209 | btrfs_device_generation(leaf, dev_item)) | |
3210 | return -EINVAL; | |
6324fbf3 | 3211 | } |
0b86a832 CM |
3212 | |
3213 | fill_device_from_item(leaf, dev_item, device); | |
3214 | device->dev_root = root->fs_info->dev_root; | |
dfe25020 | 3215 | device->in_fs_metadata = 1; |
2b82032c YZ |
3216 | if (device->writeable) |
3217 | device->fs_devices->total_rw_bytes += device->total_bytes; | |
0b86a832 | 3218 | ret = 0; |
0b86a832 CM |
3219 | return ret; |
3220 | } | |
3221 | ||
0d81ba5d CM |
3222 | int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf) |
3223 | { | |
3224 | struct btrfs_dev_item *dev_item; | |
3225 | ||
3226 | dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block, | |
3227 | dev_item); | |
3228 | return read_one_dev(root, buf, dev_item); | |
3229 | } | |
3230 | ||
e4404d6e | 3231 | int btrfs_read_sys_array(struct btrfs_root *root) |
0b86a832 CM |
3232 | { |
3233 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
a061fc8d | 3234 | struct extent_buffer *sb; |
0b86a832 | 3235 | struct btrfs_disk_key *disk_key; |
0b86a832 | 3236 | struct btrfs_chunk *chunk; |
84eed90f CM |
3237 | u8 *ptr; |
3238 | unsigned long sb_ptr; | |
3239 | int ret = 0; | |
0b86a832 CM |
3240 | u32 num_stripes; |
3241 | u32 array_size; | |
3242 | u32 len = 0; | |
0b86a832 | 3243 | u32 cur; |
84eed90f | 3244 | struct btrfs_key key; |
0b86a832 | 3245 | |
e4404d6e | 3246 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
a061fc8d CM |
3247 | BTRFS_SUPER_INFO_SIZE); |
3248 | if (!sb) | |
3249 | return -ENOMEM; | |
3250 | btrfs_set_buffer_uptodate(sb); | |
4008c04a CM |
3251 | btrfs_set_buffer_lockdep_class(sb, 0); |
3252 | ||
a061fc8d | 3253 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
0b86a832 CM |
3254 | array_size = btrfs_super_sys_array_size(super_copy); |
3255 | ||
0b86a832 CM |
3256 | ptr = super_copy->sys_chunk_array; |
3257 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); | |
3258 | cur = 0; | |
3259 | ||
3260 | while (cur < array_size) { | |
3261 | disk_key = (struct btrfs_disk_key *)ptr; | |
3262 | btrfs_disk_key_to_cpu(&key, disk_key); | |
3263 | ||
a061fc8d | 3264 | len = sizeof(*disk_key); ptr += len; |
0b86a832 CM |
3265 | sb_ptr += len; |
3266 | cur += len; | |
3267 | ||
0d81ba5d | 3268 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
0b86a832 | 3269 | chunk = (struct btrfs_chunk *)sb_ptr; |
0d81ba5d | 3270 | ret = read_one_chunk(root, &key, sb, chunk); |
84eed90f CM |
3271 | if (ret) |
3272 | break; | |
0b86a832 CM |
3273 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
3274 | len = btrfs_chunk_item_size(num_stripes); | |
3275 | } else { | |
84eed90f CM |
3276 | ret = -EIO; |
3277 | break; | |
0b86a832 CM |
3278 | } |
3279 | ptr += len; | |
3280 | sb_ptr += len; | |
3281 | cur += len; | |
3282 | } | |
a061fc8d | 3283 | free_extent_buffer(sb); |
84eed90f | 3284 | return ret; |
0b86a832 CM |
3285 | } |
3286 | ||
3287 | int btrfs_read_chunk_tree(struct btrfs_root *root) | |
3288 | { | |
3289 | struct btrfs_path *path; | |
3290 | struct extent_buffer *leaf; | |
3291 | struct btrfs_key key; | |
3292 | struct btrfs_key found_key; | |
3293 | int ret; | |
3294 | int slot; | |
3295 | ||
3296 | root = root->fs_info->chunk_root; | |
3297 | ||
3298 | path = btrfs_alloc_path(); | |
3299 | if (!path) | |
3300 | return -ENOMEM; | |
3301 | ||
3302 | /* first we search for all of the device items, and then we | |
3303 | * read in all of the chunk items. This way we can create chunk | |
3304 | * mappings that reference all of the devices that are afound | |
3305 | */ | |
3306 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
3307 | key.offset = 0; | |
3308 | key.type = 0; | |
3309 | again: | |
3310 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
d397712b | 3311 | while (1) { |
0b86a832 CM |
3312 | leaf = path->nodes[0]; |
3313 | slot = path->slots[0]; | |
3314 | if (slot >= btrfs_header_nritems(leaf)) { | |
3315 | ret = btrfs_next_leaf(root, path); | |
3316 | if (ret == 0) | |
3317 | continue; | |
3318 | if (ret < 0) | |
3319 | goto error; | |
3320 | break; | |
3321 | } | |
3322 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3323 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | |
3324 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) | |
3325 | break; | |
3326 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { | |
3327 | struct btrfs_dev_item *dev_item; | |
3328 | dev_item = btrfs_item_ptr(leaf, slot, | |
3329 | struct btrfs_dev_item); | |
0d81ba5d | 3330 | ret = read_one_dev(root, leaf, dev_item); |
2b82032c YZ |
3331 | if (ret) |
3332 | goto error; | |
0b86a832 CM |
3333 | } |
3334 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { | |
3335 | struct btrfs_chunk *chunk; | |
3336 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); | |
3337 | ret = read_one_chunk(root, &found_key, leaf, chunk); | |
2b82032c YZ |
3338 | if (ret) |
3339 | goto error; | |
0b86a832 CM |
3340 | } |
3341 | path->slots[0]++; | |
3342 | } | |
3343 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | |
3344 | key.objectid = 0; | |
3345 | btrfs_release_path(root, path); | |
3346 | goto again; | |
3347 | } | |
0b86a832 CM |
3348 | ret = 0; |
3349 | error: | |
2b82032c | 3350 | btrfs_free_path(path); |
0b86a832 CM |
3351 | return ret; |
3352 | } |