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