2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
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 <linux/kthread.h>
27 #include <asm/div64.h>
30 #include "extent_map.h"
32 #include "transaction.h"
33 #include "print-tree.h"
35 #include "async-thread.h"
36 #include "check-integrity.h"
38 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
39 struct btrfs_root
*root
,
40 struct btrfs_device
*device
);
41 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
43 static DEFINE_MUTEX(uuid_mutex
);
44 static LIST_HEAD(fs_uuids
);
46 static void lock_chunks(struct btrfs_root
*root
)
48 mutex_lock(&root
->fs_info
->chunk_mutex
);
51 static void unlock_chunks(struct btrfs_root
*root
)
53 mutex_unlock(&root
->fs_info
->chunk_mutex
);
56 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
58 struct btrfs_device
*device
;
59 WARN_ON(fs_devices
->opened
);
60 while (!list_empty(&fs_devices
->devices
)) {
61 device
= list_entry(fs_devices
->devices
.next
,
62 struct btrfs_device
, dev_list
);
63 list_del(&device
->dev_list
);
70 void btrfs_cleanup_fs_uuids(void)
72 struct btrfs_fs_devices
*fs_devices
;
74 while (!list_empty(&fs_uuids
)) {
75 fs_devices
= list_entry(fs_uuids
.next
,
76 struct btrfs_fs_devices
, list
);
77 list_del(&fs_devices
->list
);
78 free_fs_devices(fs_devices
);
82 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
85 struct btrfs_device
*dev
;
87 list_for_each_entry(dev
, head
, dev_list
) {
88 if (dev
->devid
== devid
&&
89 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
96 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
98 struct btrfs_fs_devices
*fs_devices
;
100 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
101 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
107 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
108 struct bio
*head
, struct bio
*tail
)
111 struct bio
*old_head
;
113 old_head
= pending_bios
->head
;
114 pending_bios
->head
= head
;
115 if (pending_bios
->tail
)
116 tail
->bi_next
= old_head
;
118 pending_bios
->tail
= tail
;
122 * we try to collect pending bios for a device so we don't get a large
123 * number of procs sending bios down to the same device. This greatly
124 * improves the schedulers ability to collect and merge the bios.
126 * But, it also turns into a long list of bios to process and that is sure
127 * to eventually make the worker thread block. The solution here is to
128 * make some progress and then put this work struct back at the end of
129 * the list if the block device is congested. This way, multiple devices
130 * can make progress from a single worker thread.
132 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
135 struct backing_dev_info
*bdi
;
136 struct btrfs_fs_info
*fs_info
;
137 struct btrfs_pending_bios
*pending_bios
;
141 unsigned long num_run
;
142 unsigned long batch_run
= 0;
144 unsigned long last_waited
= 0;
146 int sync_pending
= 0;
147 struct blk_plug plug
;
150 * this function runs all the bios we've collected for
151 * a particular device. We don't want to wander off to
152 * another device without first sending all of these down.
153 * So, setup a plug here and finish it off before we return
155 blk_start_plug(&plug
);
157 bdi
= blk_get_backing_dev_info(device
->bdev
);
158 fs_info
= device
->dev_root
->fs_info
;
159 limit
= btrfs_async_submit_limit(fs_info
);
160 limit
= limit
* 2 / 3;
163 spin_lock(&device
->io_lock
);
168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
173 if (!force_reg
&& device
->pending_sync_bios
.head
) {
174 pending_bios
= &device
->pending_sync_bios
;
177 pending_bios
= &device
->pending_bios
;
181 pending
= pending_bios
->head
;
182 tail
= pending_bios
->tail
;
183 WARN_ON(pending
&& !tail
);
186 * if pending was null this time around, no bios need processing
187 * at all and we can stop. Otherwise it'll loop back up again
188 * and do an additional check so no bios are missed.
190 * device->running_pending is used to synchronize with the
193 if (device
->pending_sync_bios
.head
== NULL
&&
194 device
->pending_bios
.head
== NULL
) {
196 device
->running_pending
= 0;
199 device
->running_pending
= 1;
202 pending_bios
->head
= NULL
;
203 pending_bios
->tail
= NULL
;
205 spin_unlock(&device
->io_lock
);
210 /* we want to work on both lists, but do more bios on the
211 * sync list than the regular list
214 pending_bios
!= &device
->pending_sync_bios
&&
215 device
->pending_sync_bios
.head
) ||
216 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
217 device
->pending_bios
.head
)) {
218 spin_lock(&device
->io_lock
);
219 requeue_list(pending_bios
, pending
, tail
);
224 pending
= pending
->bi_next
;
226 atomic_dec(&fs_info
->nr_async_bios
);
228 if (atomic_read(&fs_info
->nr_async_bios
) < limit
&&
229 waitqueue_active(&fs_info
->async_submit_wait
))
230 wake_up(&fs_info
->async_submit_wait
);
232 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
235 * if we're doing the sync list, record that our
236 * plug has some sync requests on it
238 * If we're doing the regular list and there are
239 * sync requests sitting around, unplug before
242 if (pending_bios
== &device
->pending_sync_bios
) {
244 } else if (sync_pending
) {
245 blk_finish_plug(&plug
);
246 blk_start_plug(&plug
);
250 btrfsic_submit_bio(cur
->bi_rw
, cur
);
257 * we made progress, there is more work to do and the bdi
258 * is now congested. Back off and let other work structs
261 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
262 fs_info
->fs_devices
->open_devices
> 1) {
263 struct io_context
*ioc
;
265 ioc
= current
->io_context
;
268 * the main goal here is that we don't want to
269 * block if we're going to be able to submit
270 * more requests without blocking.
272 * This code does two great things, it pokes into
273 * the elevator code from a filesystem _and_
274 * it makes assumptions about how batching works.
276 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
277 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
279 ioc
->last_waited
== last_waited
)) {
281 * we want to go through our batch of
282 * requests and stop. So, we copy out
283 * the ioc->last_waited time and test
284 * against it before looping
286 last_waited
= ioc
->last_waited
;
291 spin_lock(&device
->io_lock
);
292 requeue_list(pending_bios
, pending
, tail
);
293 device
->running_pending
= 1;
295 spin_unlock(&device
->io_lock
);
296 btrfs_requeue_work(&device
->work
);
299 /* unplug every 64 requests just for good measure */
300 if (batch_run
% 64 == 0) {
301 blk_finish_plug(&plug
);
302 blk_start_plug(&plug
);
311 spin_lock(&device
->io_lock
);
312 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
314 spin_unlock(&device
->io_lock
);
317 blk_finish_plug(&plug
);
320 static void pending_bios_fn(struct btrfs_work
*work
)
322 struct btrfs_device
*device
;
324 device
= container_of(work
, struct btrfs_device
, work
);
325 run_scheduled_bios(device
);
328 static noinline
int device_list_add(const char *path
,
329 struct btrfs_super_block
*disk_super
,
330 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
332 struct btrfs_device
*device
;
333 struct btrfs_fs_devices
*fs_devices
;
334 u64 found_transid
= btrfs_super_generation(disk_super
);
337 fs_devices
= find_fsid(disk_super
->fsid
);
339 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
342 INIT_LIST_HEAD(&fs_devices
->devices
);
343 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
344 list_add(&fs_devices
->list
, &fs_uuids
);
345 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
346 fs_devices
->latest_devid
= devid
;
347 fs_devices
->latest_trans
= found_transid
;
348 mutex_init(&fs_devices
->device_list_mutex
);
351 device
= __find_device(&fs_devices
->devices
, devid
,
352 disk_super
->dev_item
.uuid
);
355 if (fs_devices
->opened
)
358 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
360 /* we can safely leave the fs_devices entry around */
363 device
->devid
= devid
;
364 device
->work
.func
= pending_bios_fn
;
365 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
367 spin_lock_init(&device
->io_lock
);
368 device
->name
= kstrdup(path
, GFP_NOFS
);
373 INIT_LIST_HEAD(&device
->dev_alloc_list
);
375 /* init readahead state */
376 spin_lock_init(&device
->reada_lock
);
377 device
->reada_curr_zone
= NULL
;
378 atomic_set(&device
->reada_in_flight
, 0);
379 device
->reada_next
= 0;
380 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
381 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
383 mutex_lock(&fs_devices
->device_list_mutex
);
384 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
385 mutex_unlock(&fs_devices
->device_list_mutex
);
387 device
->fs_devices
= fs_devices
;
388 fs_devices
->num_devices
++;
389 } else if (!device
->name
|| strcmp(device
->name
, path
)) {
390 name
= kstrdup(path
, GFP_NOFS
);
395 if (device
->missing
) {
396 fs_devices
->missing_devices
--;
401 if (found_transid
> fs_devices
->latest_trans
) {
402 fs_devices
->latest_devid
= devid
;
403 fs_devices
->latest_trans
= found_transid
;
405 *fs_devices_ret
= fs_devices
;
409 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
411 struct btrfs_fs_devices
*fs_devices
;
412 struct btrfs_device
*device
;
413 struct btrfs_device
*orig_dev
;
415 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
417 return ERR_PTR(-ENOMEM
);
419 INIT_LIST_HEAD(&fs_devices
->devices
);
420 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
421 INIT_LIST_HEAD(&fs_devices
->list
);
422 mutex_init(&fs_devices
->device_list_mutex
);
423 fs_devices
->latest_devid
= orig
->latest_devid
;
424 fs_devices
->latest_trans
= orig
->latest_trans
;
425 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
427 /* We have held the volume lock, it is safe to get the devices. */
428 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
429 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
433 device
->name
= kstrdup(orig_dev
->name
, GFP_NOFS
);
439 device
->devid
= orig_dev
->devid
;
440 device
->work
.func
= pending_bios_fn
;
441 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
442 spin_lock_init(&device
->io_lock
);
443 INIT_LIST_HEAD(&device
->dev_list
);
444 INIT_LIST_HEAD(&device
->dev_alloc_list
);
446 list_add(&device
->dev_list
, &fs_devices
->devices
);
447 device
->fs_devices
= fs_devices
;
448 fs_devices
->num_devices
++;
452 free_fs_devices(fs_devices
);
453 return ERR_PTR(-ENOMEM
);
456 void btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
458 struct btrfs_device
*device
, *next
;
460 struct block_device
*latest_bdev
= NULL
;
461 u64 latest_devid
= 0;
462 u64 latest_transid
= 0;
464 mutex_lock(&uuid_mutex
);
466 /* This is the initialized path, it is safe to release the devices. */
467 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
468 if (device
->in_fs_metadata
) {
469 if (!latest_transid
||
470 device
->generation
> latest_transid
) {
471 latest_devid
= device
->devid
;
472 latest_transid
= device
->generation
;
473 latest_bdev
= device
->bdev
;
479 blkdev_put(device
->bdev
, device
->mode
);
481 fs_devices
->open_devices
--;
483 if (device
->writeable
) {
484 list_del_init(&device
->dev_alloc_list
);
485 device
->writeable
= 0;
486 fs_devices
->rw_devices
--;
488 list_del_init(&device
->dev_list
);
489 fs_devices
->num_devices
--;
494 if (fs_devices
->seed
) {
495 fs_devices
= fs_devices
->seed
;
499 fs_devices
->latest_bdev
= latest_bdev
;
500 fs_devices
->latest_devid
= latest_devid
;
501 fs_devices
->latest_trans
= latest_transid
;
503 mutex_unlock(&uuid_mutex
);
506 static void __free_device(struct work_struct
*work
)
508 struct btrfs_device
*device
;
510 device
= container_of(work
, struct btrfs_device
, rcu_work
);
513 blkdev_put(device
->bdev
, device
->mode
);
519 static void free_device(struct rcu_head
*head
)
521 struct btrfs_device
*device
;
523 device
= container_of(head
, struct btrfs_device
, rcu
);
525 INIT_WORK(&device
->rcu_work
, __free_device
);
526 schedule_work(&device
->rcu_work
);
529 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
531 struct btrfs_device
*device
;
533 if (--fs_devices
->opened
> 0)
536 mutex_lock(&fs_devices
->device_list_mutex
);
537 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
538 struct btrfs_device
*new_device
;
541 fs_devices
->open_devices
--;
543 if (device
->writeable
) {
544 list_del_init(&device
->dev_alloc_list
);
545 fs_devices
->rw_devices
--;
548 if (device
->can_discard
)
549 fs_devices
->num_can_discard
--;
551 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
552 BUG_ON(!new_device
); /* -ENOMEM */
553 memcpy(new_device
, device
, sizeof(*new_device
));
554 new_device
->name
= kstrdup(device
->name
, GFP_NOFS
);
555 BUG_ON(device
->name
&& !new_device
->name
); /* -ENOMEM */
556 new_device
->bdev
= NULL
;
557 new_device
->writeable
= 0;
558 new_device
->in_fs_metadata
= 0;
559 new_device
->can_discard
= 0;
560 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
562 call_rcu(&device
->rcu
, free_device
);
564 mutex_unlock(&fs_devices
->device_list_mutex
);
566 WARN_ON(fs_devices
->open_devices
);
567 WARN_ON(fs_devices
->rw_devices
);
568 fs_devices
->opened
= 0;
569 fs_devices
->seeding
= 0;
574 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
576 struct btrfs_fs_devices
*seed_devices
= NULL
;
579 mutex_lock(&uuid_mutex
);
580 ret
= __btrfs_close_devices(fs_devices
);
581 if (!fs_devices
->opened
) {
582 seed_devices
= fs_devices
->seed
;
583 fs_devices
->seed
= NULL
;
585 mutex_unlock(&uuid_mutex
);
587 while (seed_devices
) {
588 fs_devices
= seed_devices
;
589 seed_devices
= fs_devices
->seed
;
590 __btrfs_close_devices(fs_devices
);
591 free_fs_devices(fs_devices
);
596 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
597 fmode_t flags
, void *holder
)
599 struct request_queue
*q
;
600 struct block_device
*bdev
;
601 struct list_head
*head
= &fs_devices
->devices
;
602 struct btrfs_device
*device
;
603 struct block_device
*latest_bdev
= NULL
;
604 struct buffer_head
*bh
;
605 struct btrfs_super_block
*disk_super
;
606 u64 latest_devid
= 0;
607 u64 latest_transid
= 0;
614 list_for_each_entry(device
, head
, dev_list
) {
620 bdev
= blkdev_get_by_path(device
->name
, flags
, holder
);
622 printk(KERN_INFO
"open %s failed\n", device
->name
);
625 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
626 invalidate_bdev(bdev
);
627 set_blocksize(bdev
, 4096);
629 bh
= btrfs_read_dev_super(bdev
);
633 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
634 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
635 if (devid
!= device
->devid
)
638 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
642 device
->generation
= btrfs_super_generation(disk_super
);
643 if (!latest_transid
|| device
->generation
> latest_transid
) {
644 latest_devid
= devid
;
645 latest_transid
= device
->generation
;
649 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
650 device
->writeable
= 0;
652 device
->writeable
= !bdev_read_only(bdev
);
656 q
= bdev_get_queue(bdev
);
657 if (blk_queue_discard(q
)) {
658 device
->can_discard
= 1;
659 fs_devices
->num_can_discard
++;
663 device
->in_fs_metadata
= 0;
664 device
->mode
= flags
;
666 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
667 fs_devices
->rotating
= 1;
669 fs_devices
->open_devices
++;
670 if (device
->writeable
) {
671 fs_devices
->rw_devices
++;
672 list_add(&device
->dev_alloc_list
,
673 &fs_devices
->alloc_list
);
681 blkdev_put(bdev
, flags
);
685 if (fs_devices
->open_devices
== 0) {
689 fs_devices
->seeding
= seeding
;
690 fs_devices
->opened
= 1;
691 fs_devices
->latest_bdev
= latest_bdev
;
692 fs_devices
->latest_devid
= latest_devid
;
693 fs_devices
->latest_trans
= latest_transid
;
694 fs_devices
->total_rw_bytes
= 0;
699 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
700 fmode_t flags
, void *holder
)
704 mutex_lock(&uuid_mutex
);
705 if (fs_devices
->opened
) {
706 fs_devices
->opened
++;
709 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
711 mutex_unlock(&uuid_mutex
);
715 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
716 struct btrfs_fs_devices
**fs_devices_ret
)
718 struct btrfs_super_block
*disk_super
;
719 struct block_device
*bdev
;
720 struct buffer_head
*bh
;
726 bdev
= blkdev_get_by_path(path
, flags
, holder
);
733 mutex_lock(&uuid_mutex
);
734 ret
= set_blocksize(bdev
, 4096);
737 bh
= btrfs_read_dev_super(bdev
);
742 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
743 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
744 transid
= btrfs_super_generation(disk_super
);
745 if (disk_super
->label
[0])
746 printk(KERN_INFO
"device label %s ", disk_super
->label
);
748 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
749 printk(KERN_CONT
"devid %llu transid %llu %s\n",
750 (unsigned long long)devid
, (unsigned long long)transid
, path
);
751 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
755 mutex_unlock(&uuid_mutex
);
756 blkdev_put(bdev
, flags
);
761 /* helper to account the used device space in the range */
762 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
763 u64 end
, u64
*length
)
765 struct btrfs_key key
;
766 struct btrfs_root
*root
= device
->dev_root
;
767 struct btrfs_dev_extent
*dev_extent
;
768 struct btrfs_path
*path
;
772 struct extent_buffer
*l
;
776 if (start
>= device
->total_bytes
)
779 path
= btrfs_alloc_path();
784 key
.objectid
= device
->devid
;
786 key
.type
= BTRFS_DEV_EXTENT_KEY
;
788 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
792 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
799 slot
= path
->slots
[0];
800 if (slot
>= btrfs_header_nritems(l
)) {
801 ret
= btrfs_next_leaf(root
, path
);
809 btrfs_item_key_to_cpu(l
, &key
, slot
);
811 if (key
.objectid
< device
->devid
)
814 if (key
.objectid
> device
->devid
)
817 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
820 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
821 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
823 if (key
.offset
<= start
&& extent_end
> end
) {
824 *length
= end
- start
+ 1;
826 } else if (key
.offset
<= start
&& extent_end
> start
)
827 *length
+= extent_end
- start
;
828 else if (key
.offset
> start
&& extent_end
<= end
)
829 *length
+= extent_end
- key
.offset
;
830 else if (key
.offset
> start
&& key
.offset
<= end
) {
831 *length
+= end
- key
.offset
+ 1;
833 } else if (key
.offset
> end
)
841 btrfs_free_path(path
);
846 * find_free_dev_extent - find free space in the specified device
847 * @device: the device which we search the free space in
848 * @num_bytes: the size of the free space that we need
849 * @start: store the start of the free space.
850 * @len: the size of the free space. that we find, or the size of the max
851 * free space if we don't find suitable free space
853 * this uses a pretty simple search, the expectation is that it is
854 * called very infrequently and that a given device has a small number
857 * @start is used to store the start of the free space if we find. But if we
858 * don't find suitable free space, it will be used to store the start position
859 * of the max free space.
861 * @len is used to store the size of the free space that we find.
862 * But if we don't find suitable free space, it is used to store the size of
863 * the max free space.
865 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
866 u64
*start
, u64
*len
)
868 struct btrfs_key key
;
869 struct btrfs_root
*root
= device
->dev_root
;
870 struct btrfs_dev_extent
*dev_extent
;
871 struct btrfs_path
*path
;
877 u64 search_end
= device
->total_bytes
;
880 struct extent_buffer
*l
;
882 /* FIXME use last free of some kind */
884 /* we don't want to overwrite the superblock on the drive,
885 * so we make sure to start at an offset of at least 1MB
887 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
889 max_hole_start
= search_start
;
893 if (search_start
>= search_end
) {
898 path
= btrfs_alloc_path();
905 key
.objectid
= device
->devid
;
906 key
.offset
= search_start
;
907 key
.type
= BTRFS_DEV_EXTENT_KEY
;
909 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
913 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
920 slot
= path
->slots
[0];
921 if (slot
>= btrfs_header_nritems(l
)) {
922 ret
= btrfs_next_leaf(root
, path
);
930 btrfs_item_key_to_cpu(l
, &key
, slot
);
932 if (key
.objectid
< device
->devid
)
935 if (key
.objectid
> device
->devid
)
938 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
941 if (key
.offset
> search_start
) {
942 hole_size
= key
.offset
- search_start
;
944 if (hole_size
> max_hole_size
) {
945 max_hole_start
= search_start
;
946 max_hole_size
= hole_size
;
950 * If this free space is greater than which we need,
951 * it must be the max free space that we have found
952 * until now, so max_hole_start must point to the start
953 * of this free space and the length of this free space
954 * is stored in max_hole_size. Thus, we return
955 * max_hole_start and max_hole_size and go back to the
958 if (hole_size
>= num_bytes
) {
964 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
965 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
967 if (extent_end
> search_start
)
968 search_start
= extent_end
;
975 * At this point, search_start should be the end of
976 * allocated dev extents, and when shrinking the device,
977 * search_end may be smaller than search_start.
979 if (search_end
> search_start
)
980 hole_size
= search_end
- search_start
;
982 if (hole_size
> max_hole_size
) {
983 max_hole_start
= search_start
;
984 max_hole_size
= hole_size
;
988 if (hole_size
< num_bytes
)
994 btrfs_free_path(path
);
996 *start
= max_hole_start
;
998 *len
= max_hole_size
;
1002 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1003 struct btrfs_device
*device
,
1007 struct btrfs_path
*path
;
1008 struct btrfs_root
*root
= device
->dev_root
;
1009 struct btrfs_key key
;
1010 struct btrfs_key found_key
;
1011 struct extent_buffer
*leaf
= NULL
;
1012 struct btrfs_dev_extent
*extent
= NULL
;
1014 path
= btrfs_alloc_path();
1018 key
.objectid
= device
->devid
;
1020 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1022 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1024 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1025 BTRFS_DEV_EXTENT_KEY
);
1028 leaf
= path
->nodes
[0];
1029 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1030 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1031 struct btrfs_dev_extent
);
1032 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1033 btrfs_dev_extent_length(leaf
, extent
) < start
);
1035 btrfs_release_path(path
);
1037 } else if (ret
== 0) {
1038 leaf
= path
->nodes
[0];
1039 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1040 struct btrfs_dev_extent
);
1042 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1046 if (device
->bytes_used
> 0) {
1047 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1048 device
->bytes_used
-= len
;
1049 spin_lock(&root
->fs_info
->free_chunk_lock
);
1050 root
->fs_info
->free_chunk_space
+= len
;
1051 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1053 ret
= btrfs_del_item(trans
, root
, path
);
1055 btrfs_error(root
->fs_info
, ret
,
1056 "Failed to remove dev extent item");
1059 btrfs_free_path(path
);
1063 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1064 struct btrfs_device
*device
,
1065 u64 chunk_tree
, u64 chunk_objectid
,
1066 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1069 struct btrfs_path
*path
;
1070 struct btrfs_root
*root
= device
->dev_root
;
1071 struct btrfs_dev_extent
*extent
;
1072 struct extent_buffer
*leaf
;
1073 struct btrfs_key key
;
1075 WARN_ON(!device
->in_fs_metadata
);
1076 path
= btrfs_alloc_path();
1080 key
.objectid
= device
->devid
;
1082 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1083 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1088 leaf
= path
->nodes
[0];
1089 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1090 struct btrfs_dev_extent
);
1091 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1092 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1093 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1095 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1096 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1099 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1100 btrfs_mark_buffer_dirty(leaf
);
1102 btrfs_free_path(path
);
1106 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1107 u64 objectid
, u64
*offset
)
1109 struct btrfs_path
*path
;
1111 struct btrfs_key key
;
1112 struct btrfs_chunk
*chunk
;
1113 struct btrfs_key found_key
;
1115 path
= btrfs_alloc_path();
1119 key
.objectid
= objectid
;
1120 key
.offset
= (u64
)-1;
1121 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1123 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1127 BUG_ON(ret
== 0); /* Corruption */
1129 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1133 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1135 if (found_key
.objectid
!= objectid
)
1138 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1139 struct btrfs_chunk
);
1140 *offset
= found_key
.offset
+
1141 btrfs_chunk_length(path
->nodes
[0], chunk
);
1146 btrfs_free_path(path
);
1150 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1153 struct btrfs_key key
;
1154 struct btrfs_key found_key
;
1155 struct btrfs_path
*path
;
1157 root
= root
->fs_info
->chunk_root
;
1159 path
= btrfs_alloc_path();
1163 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1164 key
.type
= BTRFS_DEV_ITEM_KEY
;
1165 key
.offset
= (u64
)-1;
1167 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1171 BUG_ON(ret
== 0); /* Corruption */
1173 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1174 BTRFS_DEV_ITEM_KEY
);
1178 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1180 *objectid
= found_key
.offset
+ 1;
1184 btrfs_free_path(path
);
1189 * the device information is stored in the chunk root
1190 * the btrfs_device struct should be fully filled in
1192 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1193 struct btrfs_root
*root
,
1194 struct btrfs_device
*device
)
1197 struct btrfs_path
*path
;
1198 struct btrfs_dev_item
*dev_item
;
1199 struct extent_buffer
*leaf
;
1200 struct btrfs_key key
;
1203 root
= root
->fs_info
->chunk_root
;
1205 path
= btrfs_alloc_path();
1209 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1210 key
.type
= BTRFS_DEV_ITEM_KEY
;
1211 key
.offset
= device
->devid
;
1213 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1218 leaf
= path
->nodes
[0];
1219 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1221 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1222 btrfs_set_device_generation(leaf
, dev_item
, 0);
1223 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1224 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1225 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1226 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1227 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1228 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1229 btrfs_set_device_group(leaf
, dev_item
, 0);
1230 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1231 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1232 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1234 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1235 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1236 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1237 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1238 btrfs_mark_buffer_dirty(leaf
);
1242 btrfs_free_path(path
);
1246 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1247 struct btrfs_device
*device
)
1250 struct btrfs_path
*path
;
1251 struct btrfs_key key
;
1252 struct btrfs_trans_handle
*trans
;
1254 root
= root
->fs_info
->chunk_root
;
1256 path
= btrfs_alloc_path();
1260 trans
= btrfs_start_transaction(root
, 0);
1261 if (IS_ERR(trans
)) {
1262 btrfs_free_path(path
);
1263 return PTR_ERR(trans
);
1265 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1266 key
.type
= BTRFS_DEV_ITEM_KEY
;
1267 key
.offset
= device
->devid
;
1270 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1279 ret
= btrfs_del_item(trans
, root
, path
);
1283 btrfs_free_path(path
);
1284 unlock_chunks(root
);
1285 btrfs_commit_transaction(trans
, root
);
1289 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1291 struct btrfs_device
*device
;
1292 struct btrfs_device
*next_device
;
1293 struct block_device
*bdev
;
1294 struct buffer_head
*bh
= NULL
;
1295 struct btrfs_super_block
*disk_super
;
1296 struct btrfs_fs_devices
*cur_devices
;
1302 bool clear_super
= false;
1304 mutex_lock(&uuid_mutex
);
1306 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1307 root
->fs_info
->avail_system_alloc_bits
|
1308 root
->fs_info
->avail_metadata_alloc_bits
;
1310 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1311 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1312 printk(KERN_ERR
"btrfs: unable to go below four devices "
1318 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1319 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1320 printk(KERN_ERR
"btrfs: unable to go below two "
1321 "devices on raid1\n");
1326 if (strcmp(device_path
, "missing") == 0) {
1327 struct list_head
*devices
;
1328 struct btrfs_device
*tmp
;
1331 devices
= &root
->fs_info
->fs_devices
->devices
;
1333 * It is safe to read the devices since the volume_mutex
1336 list_for_each_entry(tmp
, devices
, dev_list
) {
1337 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1346 printk(KERN_ERR
"btrfs: no missing devices found to "
1351 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1352 root
->fs_info
->bdev_holder
);
1354 ret
= PTR_ERR(bdev
);
1358 set_blocksize(bdev
, 4096);
1359 invalidate_bdev(bdev
);
1360 bh
= btrfs_read_dev_super(bdev
);
1365 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1366 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1367 dev_uuid
= disk_super
->dev_item
.uuid
;
1368 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1376 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1377 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1383 if (device
->writeable
) {
1385 list_del_init(&device
->dev_alloc_list
);
1386 unlock_chunks(root
);
1387 root
->fs_info
->fs_devices
->rw_devices
--;
1391 ret
= btrfs_shrink_device(device
, 0);
1395 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1399 spin_lock(&root
->fs_info
->free_chunk_lock
);
1400 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1402 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1404 device
->in_fs_metadata
= 0;
1405 btrfs_scrub_cancel_dev(root
, device
);
1408 * the device list mutex makes sure that we don't change
1409 * the device list while someone else is writing out all
1410 * the device supers.
1413 cur_devices
= device
->fs_devices
;
1414 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1415 list_del_rcu(&device
->dev_list
);
1417 device
->fs_devices
->num_devices
--;
1419 if (device
->missing
)
1420 root
->fs_info
->fs_devices
->missing_devices
--;
1422 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1423 struct btrfs_device
, dev_list
);
1424 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1425 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1426 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1427 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1430 device
->fs_devices
->open_devices
--;
1432 call_rcu(&device
->rcu
, free_device
);
1433 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1435 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1436 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1438 if (cur_devices
->open_devices
== 0) {
1439 struct btrfs_fs_devices
*fs_devices
;
1440 fs_devices
= root
->fs_info
->fs_devices
;
1441 while (fs_devices
) {
1442 if (fs_devices
->seed
== cur_devices
)
1444 fs_devices
= fs_devices
->seed
;
1446 fs_devices
->seed
= cur_devices
->seed
;
1447 cur_devices
->seed
= NULL
;
1449 __btrfs_close_devices(cur_devices
);
1450 unlock_chunks(root
);
1451 free_fs_devices(cur_devices
);
1455 * at this point, the device is zero sized. We want to
1456 * remove it from the devices list and zero out the old super
1459 /* make sure this device isn't detected as part of
1462 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1463 set_buffer_dirty(bh
);
1464 sync_dirty_buffer(bh
);
1473 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1475 mutex_unlock(&uuid_mutex
);
1478 if (device
->writeable
) {
1480 list_add(&device
->dev_alloc_list
,
1481 &root
->fs_info
->fs_devices
->alloc_list
);
1482 unlock_chunks(root
);
1483 root
->fs_info
->fs_devices
->rw_devices
++;
1489 * does all the dirty work required for changing file system's UUID.
1491 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1493 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1494 struct btrfs_fs_devices
*old_devices
;
1495 struct btrfs_fs_devices
*seed_devices
;
1496 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1497 struct btrfs_device
*device
;
1500 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1501 if (!fs_devices
->seeding
)
1504 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1508 old_devices
= clone_fs_devices(fs_devices
);
1509 if (IS_ERR(old_devices
)) {
1510 kfree(seed_devices
);
1511 return PTR_ERR(old_devices
);
1514 list_add(&old_devices
->list
, &fs_uuids
);
1516 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1517 seed_devices
->opened
= 1;
1518 INIT_LIST_HEAD(&seed_devices
->devices
);
1519 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1520 mutex_init(&seed_devices
->device_list_mutex
);
1522 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1523 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1525 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1527 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1528 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1529 device
->fs_devices
= seed_devices
;
1532 fs_devices
->seeding
= 0;
1533 fs_devices
->num_devices
= 0;
1534 fs_devices
->open_devices
= 0;
1535 fs_devices
->seed
= seed_devices
;
1537 generate_random_uuid(fs_devices
->fsid
);
1538 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1539 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1540 super_flags
= btrfs_super_flags(disk_super
) &
1541 ~BTRFS_SUPER_FLAG_SEEDING
;
1542 btrfs_set_super_flags(disk_super
, super_flags
);
1548 * strore the expected generation for seed devices in device items.
1550 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1551 struct btrfs_root
*root
)
1553 struct btrfs_path
*path
;
1554 struct extent_buffer
*leaf
;
1555 struct btrfs_dev_item
*dev_item
;
1556 struct btrfs_device
*device
;
1557 struct btrfs_key key
;
1558 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1559 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1563 path
= btrfs_alloc_path();
1567 root
= root
->fs_info
->chunk_root
;
1568 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1570 key
.type
= BTRFS_DEV_ITEM_KEY
;
1573 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1577 leaf
= path
->nodes
[0];
1579 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1580 ret
= btrfs_next_leaf(root
, path
);
1585 leaf
= path
->nodes
[0];
1586 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1587 btrfs_release_path(path
);
1591 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1592 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1593 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1596 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1597 struct btrfs_dev_item
);
1598 devid
= btrfs_device_id(leaf
, dev_item
);
1599 read_extent_buffer(leaf
, dev_uuid
,
1600 (unsigned long)btrfs_device_uuid(dev_item
),
1602 read_extent_buffer(leaf
, fs_uuid
,
1603 (unsigned long)btrfs_device_fsid(dev_item
),
1605 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1606 BUG_ON(!device
); /* Logic error */
1608 if (device
->fs_devices
->seeding
) {
1609 btrfs_set_device_generation(leaf
, dev_item
,
1610 device
->generation
);
1611 btrfs_mark_buffer_dirty(leaf
);
1619 btrfs_free_path(path
);
1623 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1625 struct request_queue
*q
;
1626 struct btrfs_trans_handle
*trans
;
1627 struct btrfs_device
*device
;
1628 struct block_device
*bdev
;
1629 struct list_head
*devices
;
1630 struct super_block
*sb
= root
->fs_info
->sb
;
1632 int seeding_dev
= 0;
1635 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1638 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1639 root
->fs_info
->bdev_holder
);
1641 return PTR_ERR(bdev
);
1643 if (root
->fs_info
->fs_devices
->seeding
) {
1645 down_write(&sb
->s_umount
);
1646 mutex_lock(&uuid_mutex
);
1649 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1651 devices
= &root
->fs_info
->fs_devices
->devices
;
1653 * we have the volume lock, so we don't need the extra
1654 * device list mutex while reading the list here.
1656 list_for_each_entry(device
, devices
, dev_list
) {
1657 if (device
->bdev
== bdev
) {
1663 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1665 /* we can safely leave the fs_devices entry around */
1670 device
->name
= kstrdup(device_path
, GFP_NOFS
);
1671 if (!device
->name
) {
1677 ret
= find_next_devid(root
, &device
->devid
);
1679 kfree(device
->name
);
1684 trans
= btrfs_start_transaction(root
, 0);
1685 if (IS_ERR(trans
)) {
1686 kfree(device
->name
);
1688 ret
= PTR_ERR(trans
);
1694 q
= bdev_get_queue(bdev
);
1695 if (blk_queue_discard(q
))
1696 device
->can_discard
= 1;
1697 device
->writeable
= 1;
1698 device
->work
.func
= pending_bios_fn
;
1699 generate_random_uuid(device
->uuid
);
1700 spin_lock_init(&device
->io_lock
);
1701 device
->generation
= trans
->transid
;
1702 device
->io_width
= root
->sectorsize
;
1703 device
->io_align
= root
->sectorsize
;
1704 device
->sector_size
= root
->sectorsize
;
1705 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1706 device
->disk_total_bytes
= device
->total_bytes
;
1707 device
->dev_root
= root
->fs_info
->dev_root
;
1708 device
->bdev
= bdev
;
1709 device
->in_fs_metadata
= 1;
1710 device
->mode
= FMODE_EXCL
;
1711 set_blocksize(device
->bdev
, 4096);
1714 sb
->s_flags
&= ~MS_RDONLY
;
1715 ret
= btrfs_prepare_sprout(root
);
1716 BUG_ON(ret
); /* -ENOMEM */
1719 device
->fs_devices
= root
->fs_info
->fs_devices
;
1722 * we don't want write_supers to jump in here with our device
1725 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1726 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1727 list_add(&device
->dev_alloc_list
,
1728 &root
->fs_info
->fs_devices
->alloc_list
);
1729 root
->fs_info
->fs_devices
->num_devices
++;
1730 root
->fs_info
->fs_devices
->open_devices
++;
1731 root
->fs_info
->fs_devices
->rw_devices
++;
1732 if (device
->can_discard
)
1733 root
->fs_info
->fs_devices
->num_can_discard
++;
1734 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1736 spin_lock(&root
->fs_info
->free_chunk_lock
);
1737 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1738 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1740 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1741 root
->fs_info
->fs_devices
->rotating
= 1;
1743 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1744 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1745 total_bytes
+ device
->total_bytes
);
1747 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1748 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1750 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1753 ret
= init_first_rw_device(trans
, root
, device
);
1756 ret
= btrfs_finish_sprout(trans
, root
);
1760 ret
= btrfs_add_device(trans
, root
, device
);
1766 * we've got more storage, clear any full flags on the space
1769 btrfs_clear_space_info_full(root
->fs_info
);
1771 unlock_chunks(root
);
1772 ret
= btrfs_commit_transaction(trans
, root
);
1775 mutex_unlock(&uuid_mutex
);
1776 up_write(&sb
->s_umount
);
1778 if (ret
) /* transaction commit */
1781 ret
= btrfs_relocate_sys_chunks(root
);
1783 btrfs_error(root
->fs_info
, ret
,
1784 "Failed to relocate sys chunks after "
1785 "device initialization. This can be fixed "
1786 "using the \"btrfs balance\" command.");
1792 unlock_chunks(root
);
1793 btrfs_abort_transaction(trans
, root
, ret
);
1794 btrfs_end_transaction(trans
, root
);
1795 kfree(device
->name
);
1798 blkdev_put(bdev
, FMODE_EXCL
);
1800 mutex_unlock(&uuid_mutex
);
1801 up_write(&sb
->s_umount
);
1806 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1807 struct btrfs_device
*device
)
1810 struct btrfs_path
*path
;
1811 struct btrfs_root
*root
;
1812 struct btrfs_dev_item
*dev_item
;
1813 struct extent_buffer
*leaf
;
1814 struct btrfs_key key
;
1816 root
= device
->dev_root
->fs_info
->chunk_root
;
1818 path
= btrfs_alloc_path();
1822 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1823 key
.type
= BTRFS_DEV_ITEM_KEY
;
1824 key
.offset
= device
->devid
;
1826 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1835 leaf
= path
->nodes
[0];
1836 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1838 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1839 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1840 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1841 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1842 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1843 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1844 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1845 btrfs_mark_buffer_dirty(leaf
);
1848 btrfs_free_path(path
);
1852 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1853 struct btrfs_device
*device
, u64 new_size
)
1855 struct btrfs_super_block
*super_copy
=
1856 device
->dev_root
->fs_info
->super_copy
;
1857 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1858 u64 diff
= new_size
- device
->total_bytes
;
1860 if (!device
->writeable
)
1862 if (new_size
<= device
->total_bytes
)
1865 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1866 device
->fs_devices
->total_rw_bytes
+= diff
;
1868 device
->total_bytes
= new_size
;
1869 device
->disk_total_bytes
= new_size
;
1870 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1872 return btrfs_update_device(trans
, device
);
1875 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1876 struct btrfs_device
*device
, u64 new_size
)
1879 lock_chunks(device
->dev_root
);
1880 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1881 unlock_chunks(device
->dev_root
);
1885 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1886 struct btrfs_root
*root
,
1887 u64 chunk_tree
, u64 chunk_objectid
,
1891 struct btrfs_path
*path
;
1892 struct btrfs_key key
;
1894 root
= root
->fs_info
->chunk_root
;
1895 path
= btrfs_alloc_path();
1899 key
.objectid
= chunk_objectid
;
1900 key
.offset
= chunk_offset
;
1901 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1903 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1906 else if (ret
> 0) { /* Logic error or corruption */
1907 btrfs_error(root
->fs_info
, -ENOENT
,
1908 "Failed lookup while freeing chunk.");
1913 ret
= btrfs_del_item(trans
, root
, path
);
1915 btrfs_error(root
->fs_info
, ret
,
1916 "Failed to delete chunk item.");
1918 btrfs_free_path(path
);
1922 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1925 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1926 struct btrfs_disk_key
*disk_key
;
1927 struct btrfs_chunk
*chunk
;
1934 struct btrfs_key key
;
1936 array_size
= btrfs_super_sys_array_size(super_copy
);
1938 ptr
= super_copy
->sys_chunk_array
;
1941 while (cur
< array_size
) {
1942 disk_key
= (struct btrfs_disk_key
*)ptr
;
1943 btrfs_disk_key_to_cpu(&key
, disk_key
);
1945 len
= sizeof(*disk_key
);
1947 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1948 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1949 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1950 len
+= btrfs_chunk_item_size(num_stripes
);
1955 if (key
.objectid
== chunk_objectid
&&
1956 key
.offset
== chunk_offset
) {
1957 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1959 btrfs_set_super_sys_array_size(super_copy
, array_size
);
1968 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
1969 u64 chunk_tree
, u64 chunk_objectid
,
1972 struct extent_map_tree
*em_tree
;
1973 struct btrfs_root
*extent_root
;
1974 struct btrfs_trans_handle
*trans
;
1975 struct extent_map
*em
;
1976 struct map_lookup
*map
;
1980 root
= root
->fs_info
->chunk_root
;
1981 extent_root
= root
->fs_info
->extent_root
;
1982 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
1984 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
1988 /* step one, relocate all the extents inside this chunk */
1989 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
1993 trans
= btrfs_start_transaction(root
, 0);
1994 BUG_ON(IS_ERR(trans
));
1999 * step two, delete the device extents and the
2000 * chunk tree entries
2002 read_lock(&em_tree
->lock
);
2003 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2004 read_unlock(&em_tree
->lock
);
2006 BUG_ON(!em
|| em
->start
> chunk_offset
||
2007 em
->start
+ em
->len
< chunk_offset
);
2008 map
= (struct map_lookup
*)em
->bdev
;
2010 for (i
= 0; i
< map
->num_stripes
; i
++) {
2011 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2012 map
->stripes
[i
].physical
);
2015 if (map
->stripes
[i
].dev
) {
2016 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2020 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2025 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2027 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2028 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2032 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2035 write_lock(&em_tree
->lock
);
2036 remove_extent_mapping(em_tree
, em
);
2037 write_unlock(&em_tree
->lock
);
2042 /* once for the tree */
2043 free_extent_map(em
);
2045 free_extent_map(em
);
2047 unlock_chunks(root
);
2048 btrfs_end_transaction(trans
, root
);
2052 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2054 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2055 struct btrfs_path
*path
;
2056 struct extent_buffer
*leaf
;
2057 struct btrfs_chunk
*chunk
;
2058 struct btrfs_key key
;
2059 struct btrfs_key found_key
;
2060 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2062 bool retried
= false;
2066 path
= btrfs_alloc_path();
2071 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2072 key
.offset
= (u64
)-1;
2073 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2076 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2079 BUG_ON(ret
== 0); /* Corruption */
2081 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2088 leaf
= path
->nodes
[0];
2089 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2091 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2092 struct btrfs_chunk
);
2093 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2094 btrfs_release_path(path
);
2096 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2097 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2106 if (found_key
.offset
== 0)
2108 key
.offset
= found_key
.offset
- 1;
2111 if (failed
&& !retried
) {
2115 } else if (failed
&& retried
) {
2120 btrfs_free_path(path
);
2124 static int insert_balance_item(struct btrfs_root
*root
,
2125 struct btrfs_balance_control
*bctl
)
2127 struct btrfs_trans_handle
*trans
;
2128 struct btrfs_balance_item
*item
;
2129 struct btrfs_disk_balance_args disk_bargs
;
2130 struct btrfs_path
*path
;
2131 struct extent_buffer
*leaf
;
2132 struct btrfs_key key
;
2135 path
= btrfs_alloc_path();
2139 trans
= btrfs_start_transaction(root
, 0);
2140 if (IS_ERR(trans
)) {
2141 btrfs_free_path(path
);
2142 return PTR_ERR(trans
);
2145 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2146 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2149 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2154 leaf
= path
->nodes
[0];
2155 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2157 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2159 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2160 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2161 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2162 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2163 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2164 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2166 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2168 btrfs_mark_buffer_dirty(leaf
);
2170 btrfs_free_path(path
);
2171 err
= btrfs_commit_transaction(trans
, root
);
2177 static int del_balance_item(struct btrfs_root
*root
)
2179 struct btrfs_trans_handle
*trans
;
2180 struct btrfs_path
*path
;
2181 struct btrfs_key key
;
2184 path
= btrfs_alloc_path();
2188 trans
= btrfs_start_transaction(root
, 0);
2189 if (IS_ERR(trans
)) {
2190 btrfs_free_path(path
);
2191 return PTR_ERR(trans
);
2194 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2195 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2198 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2206 ret
= btrfs_del_item(trans
, root
, path
);
2208 btrfs_free_path(path
);
2209 err
= btrfs_commit_transaction(trans
, root
);
2216 * This is a heuristic used to reduce the number of chunks balanced on
2217 * resume after balance was interrupted.
2219 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2222 * Turn on soft mode for chunk types that were being converted.
2224 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2225 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2226 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2227 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2228 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2229 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2232 * Turn on usage filter if is not already used. The idea is
2233 * that chunks that we have already balanced should be
2234 * reasonably full. Don't do it for chunks that are being
2235 * converted - that will keep us from relocating unconverted
2236 * (albeit full) chunks.
2238 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2239 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2240 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2241 bctl
->data
.usage
= 90;
2243 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2244 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2245 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2246 bctl
->sys
.usage
= 90;
2248 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2249 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2250 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2251 bctl
->meta
.usage
= 90;
2256 * Should be called with both balance and volume mutexes held to
2257 * serialize other volume operations (add_dev/rm_dev/resize) with
2258 * restriper. Same goes for unset_balance_control.
2260 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2262 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2264 BUG_ON(fs_info
->balance_ctl
);
2266 spin_lock(&fs_info
->balance_lock
);
2267 fs_info
->balance_ctl
= bctl
;
2268 spin_unlock(&fs_info
->balance_lock
);
2271 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2273 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2275 BUG_ON(!fs_info
->balance_ctl
);
2277 spin_lock(&fs_info
->balance_lock
);
2278 fs_info
->balance_ctl
= NULL
;
2279 spin_unlock(&fs_info
->balance_lock
);
2285 * Balance filters. Return 1 if chunk should be filtered out
2286 * (should not be balanced).
2288 static int chunk_profiles_filter(u64 chunk_type
,
2289 struct btrfs_balance_args
*bargs
)
2291 chunk_type
= chunk_to_extended(chunk_type
) &
2292 BTRFS_EXTENDED_PROFILE_MASK
;
2294 if (bargs
->profiles
& chunk_type
)
2300 static u64
div_factor_fine(u64 num
, int factor
)
2312 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2313 struct btrfs_balance_args
*bargs
)
2315 struct btrfs_block_group_cache
*cache
;
2316 u64 chunk_used
, user_thresh
;
2319 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2320 chunk_used
= btrfs_block_group_used(&cache
->item
);
2322 user_thresh
= div_factor_fine(cache
->key
.offset
, bargs
->usage
);
2323 if (chunk_used
< user_thresh
)
2326 btrfs_put_block_group(cache
);
2330 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2331 struct btrfs_chunk
*chunk
,
2332 struct btrfs_balance_args
*bargs
)
2334 struct btrfs_stripe
*stripe
;
2335 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2338 for (i
= 0; i
< num_stripes
; i
++) {
2339 stripe
= btrfs_stripe_nr(chunk
, i
);
2340 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2347 /* [pstart, pend) */
2348 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2349 struct btrfs_chunk
*chunk
,
2351 struct btrfs_balance_args
*bargs
)
2353 struct btrfs_stripe
*stripe
;
2354 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2360 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2363 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2364 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
))
2368 factor
= num_stripes
/ factor
;
2370 for (i
= 0; i
< num_stripes
; i
++) {
2371 stripe
= btrfs_stripe_nr(chunk
, i
);
2372 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2375 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2376 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2377 do_div(stripe_length
, factor
);
2379 if (stripe_offset
< bargs
->pend
&&
2380 stripe_offset
+ stripe_length
> bargs
->pstart
)
2387 /* [vstart, vend) */
2388 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2389 struct btrfs_chunk
*chunk
,
2391 struct btrfs_balance_args
*bargs
)
2393 if (chunk_offset
< bargs
->vend
&&
2394 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2395 /* at least part of the chunk is inside this vrange */
2401 static int chunk_soft_convert_filter(u64 chunk_type
,
2402 struct btrfs_balance_args
*bargs
)
2404 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2407 chunk_type
= chunk_to_extended(chunk_type
) &
2408 BTRFS_EXTENDED_PROFILE_MASK
;
2410 if (bargs
->target
== chunk_type
)
2416 static int should_balance_chunk(struct btrfs_root
*root
,
2417 struct extent_buffer
*leaf
,
2418 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2420 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2421 struct btrfs_balance_args
*bargs
= NULL
;
2422 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2425 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2426 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2430 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2431 bargs
= &bctl
->data
;
2432 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2434 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2435 bargs
= &bctl
->meta
;
2437 /* profiles filter */
2438 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2439 chunk_profiles_filter(chunk_type
, bargs
)) {
2444 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2445 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2450 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2451 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2455 /* drange filter, makes sense only with devid filter */
2456 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2457 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2462 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2463 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2467 /* soft profile changing mode */
2468 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2469 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2476 static u64
div_factor(u64 num
, int factor
)
2485 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2487 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2488 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2489 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2490 struct list_head
*devices
;
2491 struct btrfs_device
*device
;
2494 struct btrfs_chunk
*chunk
;
2495 struct btrfs_path
*path
;
2496 struct btrfs_key key
;
2497 struct btrfs_key found_key
;
2498 struct btrfs_trans_handle
*trans
;
2499 struct extent_buffer
*leaf
;
2502 int enospc_errors
= 0;
2503 bool counting
= true;
2505 /* step one make some room on all the devices */
2506 devices
= &fs_info
->fs_devices
->devices
;
2507 list_for_each_entry(device
, devices
, dev_list
) {
2508 old_size
= device
->total_bytes
;
2509 size_to_free
= div_factor(old_size
, 1);
2510 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2511 if (!device
->writeable
||
2512 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2515 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2520 trans
= btrfs_start_transaction(dev_root
, 0);
2521 BUG_ON(IS_ERR(trans
));
2523 ret
= btrfs_grow_device(trans
, device
, old_size
);
2526 btrfs_end_transaction(trans
, dev_root
);
2529 /* step two, relocate all the chunks */
2530 path
= btrfs_alloc_path();
2536 /* zero out stat counters */
2537 spin_lock(&fs_info
->balance_lock
);
2538 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2539 spin_unlock(&fs_info
->balance_lock
);
2541 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2542 key
.offset
= (u64
)-1;
2543 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2546 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2547 atomic_read(&fs_info
->balance_cancel_req
)) {
2552 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2557 * this shouldn't happen, it means the last relocate
2561 BUG(); /* FIXME break ? */
2563 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2564 BTRFS_CHUNK_ITEM_KEY
);
2570 leaf
= path
->nodes
[0];
2571 slot
= path
->slots
[0];
2572 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2574 if (found_key
.objectid
!= key
.objectid
)
2577 /* chunk zero is special */
2578 if (found_key
.offset
== 0)
2581 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2584 spin_lock(&fs_info
->balance_lock
);
2585 bctl
->stat
.considered
++;
2586 spin_unlock(&fs_info
->balance_lock
);
2589 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2591 btrfs_release_path(path
);
2596 spin_lock(&fs_info
->balance_lock
);
2597 bctl
->stat
.expected
++;
2598 spin_unlock(&fs_info
->balance_lock
);
2602 ret
= btrfs_relocate_chunk(chunk_root
,
2603 chunk_root
->root_key
.objectid
,
2606 if (ret
&& ret
!= -ENOSPC
)
2608 if (ret
== -ENOSPC
) {
2611 spin_lock(&fs_info
->balance_lock
);
2612 bctl
->stat
.completed
++;
2613 spin_unlock(&fs_info
->balance_lock
);
2616 key
.offset
= found_key
.offset
- 1;
2620 btrfs_release_path(path
);
2625 btrfs_free_path(path
);
2626 if (enospc_errors
) {
2627 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2637 * alloc_profile_is_valid - see if a given profile is valid and reduced
2638 * @flags: profile to validate
2639 * @extended: if true @flags is treated as an extended profile
2641 static int alloc_profile_is_valid(u64 flags
, int extended
)
2643 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
2644 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
2646 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
2648 /* 1) check that all other bits are zeroed */
2652 /* 2) see if profile is reduced */
2654 return !extended
; /* "0" is valid for usual profiles */
2656 /* true if exactly one bit set */
2657 return (flags
& (flags
- 1)) == 0;
2660 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
2662 /* cancel requested || normal exit path */
2663 return atomic_read(&fs_info
->balance_cancel_req
) ||
2664 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
2665 atomic_read(&fs_info
->balance_cancel_req
) == 0);
2668 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
2672 unset_balance_control(fs_info
);
2673 ret
= del_balance_item(fs_info
->tree_root
);
2677 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
2678 struct btrfs_ioctl_balance_args
*bargs
);
2681 * Should be called with both balance and volume mutexes held
2683 int btrfs_balance(struct btrfs_balance_control
*bctl
,
2684 struct btrfs_ioctl_balance_args
*bargs
)
2686 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2691 if (btrfs_fs_closing(fs_info
) ||
2692 atomic_read(&fs_info
->balance_pause_req
) ||
2693 atomic_read(&fs_info
->balance_cancel_req
)) {
2698 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
2699 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
2703 * In case of mixed groups both data and meta should be picked,
2704 * and identical options should be given for both of them.
2706 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
2707 if (mixed
&& (bctl
->flags
& allowed
)) {
2708 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
2709 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
2710 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
2711 printk(KERN_ERR
"btrfs: with mixed groups data and "
2712 "metadata balance options must be the same\n");
2718 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2719 if (fs_info
->fs_devices
->num_devices
== 1)
2720 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
2721 else if (fs_info
->fs_devices
->num_devices
< 4)
2722 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
2724 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2725 BTRFS_BLOCK_GROUP_RAID10
);
2727 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2728 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
2729 (bctl
->data
.target
& ~allowed
))) {
2730 printk(KERN_ERR
"btrfs: unable to start balance with target "
2731 "data profile %llu\n",
2732 (unsigned long long)bctl
->data
.target
);
2736 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2737 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
2738 (bctl
->meta
.target
& ~allowed
))) {
2739 printk(KERN_ERR
"btrfs: unable to start balance with target "
2740 "metadata profile %llu\n",
2741 (unsigned long long)bctl
->meta
.target
);
2745 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2746 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
2747 (bctl
->sys
.target
& ~allowed
))) {
2748 printk(KERN_ERR
"btrfs: unable to start balance with target "
2749 "system profile %llu\n",
2750 (unsigned long long)bctl
->sys
.target
);
2755 /* allow dup'ed data chunks only in mixed mode */
2756 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2757 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
2758 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
2763 /* allow to reduce meta or sys integrity only if force set */
2764 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2765 BTRFS_BLOCK_GROUP_RAID10
;
2766 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2767 (fs_info
->avail_system_alloc_bits
& allowed
) &&
2768 !(bctl
->sys
.target
& allowed
)) ||
2769 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2770 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
2771 !(bctl
->meta
.target
& allowed
))) {
2772 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
2773 printk(KERN_INFO
"btrfs: force reducing metadata "
2776 printk(KERN_ERR
"btrfs: balance will reduce metadata "
2777 "integrity, use force if you want this\n");
2783 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
2784 if (ret
&& ret
!= -EEXIST
)
2787 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
2788 BUG_ON(ret
== -EEXIST
);
2789 set_balance_control(bctl
);
2791 BUG_ON(ret
!= -EEXIST
);
2792 spin_lock(&fs_info
->balance_lock
);
2793 update_balance_args(bctl
);
2794 spin_unlock(&fs_info
->balance_lock
);
2797 atomic_inc(&fs_info
->balance_running
);
2798 mutex_unlock(&fs_info
->balance_mutex
);
2800 ret
= __btrfs_balance(fs_info
);
2802 mutex_lock(&fs_info
->balance_mutex
);
2803 atomic_dec(&fs_info
->balance_running
);
2806 memset(bargs
, 0, sizeof(*bargs
));
2807 update_ioctl_balance_args(fs_info
, 0, bargs
);
2810 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
2811 balance_need_close(fs_info
)) {
2812 __cancel_balance(fs_info
);
2815 wake_up(&fs_info
->balance_wait_q
);
2819 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
2820 __cancel_balance(fs_info
);
2826 static int balance_kthread(void *data
)
2828 struct btrfs_balance_control
*bctl
=
2829 (struct btrfs_balance_control
*)data
;
2830 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2833 mutex_lock(&fs_info
->volume_mutex
);
2834 mutex_lock(&fs_info
->balance_mutex
);
2836 set_balance_control(bctl
);
2838 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
2839 printk(KERN_INFO
"btrfs: force skipping balance\n");
2841 printk(KERN_INFO
"btrfs: continuing balance\n");
2842 ret
= btrfs_balance(bctl
, NULL
);
2845 mutex_unlock(&fs_info
->balance_mutex
);
2846 mutex_unlock(&fs_info
->volume_mutex
);
2850 int btrfs_recover_balance(struct btrfs_root
*tree_root
)
2852 struct task_struct
*tsk
;
2853 struct btrfs_balance_control
*bctl
;
2854 struct btrfs_balance_item
*item
;
2855 struct btrfs_disk_balance_args disk_bargs
;
2856 struct btrfs_path
*path
;
2857 struct extent_buffer
*leaf
;
2858 struct btrfs_key key
;
2861 path
= btrfs_alloc_path();
2865 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
2871 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2872 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2875 ret
= btrfs_search_slot(NULL
, tree_root
, &key
, path
, 0, 0);
2878 if (ret
> 0) { /* ret = -ENOENT; */
2883 leaf
= path
->nodes
[0];
2884 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2886 bctl
->fs_info
= tree_root
->fs_info
;
2887 bctl
->flags
= btrfs_balance_flags(leaf
, item
) | BTRFS_BALANCE_RESUME
;
2889 btrfs_balance_data(leaf
, item
, &disk_bargs
);
2890 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
2891 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
2892 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
2893 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
2894 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
2896 tsk
= kthread_run(balance_kthread
, bctl
, "btrfs-balance");
2905 btrfs_free_path(path
);
2909 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
2913 mutex_lock(&fs_info
->balance_mutex
);
2914 if (!fs_info
->balance_ctl
) {
2915 mutex_unlock(&fs_info
->balance_mutex
);
2919 if (atomic_read(&fs_info
->balance_running
)) {
2920 atomic_inc(&fs_info
->balance_pause_req
);
2921 mutex_unlock(&fs_info
->balance_mutex
);
2923 wait_event(fs_info
->balance_wait_q
,
2924 atomic_read(&fs_info
->balance_running
) == 0);
2926 mutex_lock(&fs_info
->balance_mutex
);
2927 /* we are good with balance_ctl ripped off from under us */
2928 BUG_ON(atomic_read(&fs_info
->balance_running
));
2929 atomic_dec(&fs_info
->balance_pause_req
);
2934 mutex_unlock(&fs_info
->balance_mutex
);
2938 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
2940 mutex_lock(&fs_info
->balance_mutex
);
2941 if (!fs_info
->balance_ctl
) {
2942 mutex_unlock(&fs_info
->balance_mutex
);
2946 atomic_inc(&fs_info
->balance_cancel_req
);
2948 * if we are running just wait and return, balance item is
2949 * deleted in btrfs_balance in this case
2951 if (atomic_read(&fs_info
->balance_running
)) {
2952 mutex_unlock(&fs_info
->balance_mutex
);
2953 wait_event(fs_info
->balance_wait_q
,
2954 atomic_read(&fs_info
->balance_running
) == 0);
2955 mutex_lock(&fs_info
->balance_mutex
);
2957 /* __cancel_balance needs volume_mutex */
2958 mutex_unlock(&fs_info
->balance_mutex
);
2959 mutex_lock(&fs_info
->volume_mutex
);
2960 mutex_lock(&fs_info
->balance_mutex
);
2962 if (fs_info
->balance_ctl
)
2963 __cancel_balance(fs_info
);
2965 mutex_unlock(&fs_info
->volume_mutex
);
2968 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
2969 atomic_dec(&fs_info
->balance_cancel_req
);
2970 mutex_unlock(&fs_info
->balance_mutex
);
2975 * shrinking a device means finding all of the device extents past
2976 * the new size, and then following the back refs to the chunks.
2977 * The chunk relocation code actually frees the device extent
2979 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
2981 struct btrfs_trans_handle
*trans
;
2982 struct btrfs_root
*root
= device
->dev_root
;
2983 struct btrfs_dev_extent
*dev_extent
= NULL
;
2984 struct btrfs_path
*path
;
2992 bool retried
= false;
2993 struct extent_buffer
*l
;
2994 struct btrfs_key key
;
2995 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2996 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2997 u64 old_size
= device
->total_bytes
;
2998 u64 diff
= device
->total_bytes
- new_size
;
3000 if (new_size
>= device
->total_bytes
)
3003 path
= btrfs_alloc_path();
3011 device
->total_bytes
= new_size
;
3012 if (device
->writeable
) {
3013 device
->fs_devices
->total_rw_bytes
-= diff
;
3014 spin_lock(&root
->fs_info
->free_chunk_lock
);
3015 root
->fs_info
->free_chunk_space
-= diff
;
3016 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3018 unlock_chunks(root
);
3021 key
.objectid
= device
->devid
;
3022 key
.offset
= (u64
)-1;
3023 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3026 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3030 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3035 btrfs_release_path(path
);
3040 slot
= path
->slots
[0];
3041 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3043 if (key
.objectid
!= device
->devid
) {
3044 btrfs_release_path(path
);
3048 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3049 length
= btrfs_dev_extent_length(l
, dev_extent
);
3051 if (key
.offset
+ length
<= new_size
) {
3052 btrfs_release_path(path
);
3056 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3057 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3058 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3059 btrfs_release_path(path
);
3061 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3063 if (ret
&& ret
!= -ENOSPC
)
3067 } while (key
.offset
-- > 0);
3069 if (failed
&& !retried
) {
3073 } else if (failed
&& retried
) {
3077 device
->total_bytes
= old_size
;
3078 if (device
->writeable
)
3079 device
->fs_devices
->total_rw_bytes
+= diff
;
3080 spin_lock(&root
->fs_info
->free_chunk_lock
);
3081 root
->fs_info
->free_chunk_space
+= diff
;
3082 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3083 unlock_chunks(root
);
3087 /* Shrinking succeeded, else we would be at "done". */
3088 trans
= btrfs_start_transaction(root
, 0);
3089 if (IS_ERR(trans
)) {
3090 ret
= PTR_ERR(trans
);
3096 device
->disk_total_bytes
= new_size
;
3097 /* Now btrfs_update_device() will change the on-disk size. */
3098 ret
= btrfs_update_device(trans
, device
);
3100 unlock_chunks(root
);
3101 btrfs_end_transaction(trans
, root
);
3104 WARN_ON(diff
> old_total
);
3105 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3106 unlock_chunks(root
);
3107 btrfs_end_transaction(trans
, root
);
3109 btrfs_free_path(path
);
3113 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3114 struct btrfs_key
*key
,
3115 struct btrfs_chunk
*chunk
, int item_size
)
3117 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3118 struct btrfs_disk_key disk_key
;
3122 array_size
= btrfs_super_sys_array_size(super_copy
);
3123 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3126 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3127 btrfs_cpu_key_to_disk(&disk_key
, key
);
3128 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3129 ptr
+= sizeof(disk_key
);
3130 memcpy(ptr
, chunk
, item_size
);
3131 item_size
+= sizeof(disk_key
);
3132 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3137 * sort the devices in descending order by max_avail, total_avail
3139 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3141 const struct btrfs_device_info
*di_a
= a
;
3142 const struct btrfs_device_info
*di_b
= b
;
3144 if (di_a
->max_avail
> di_b
->max_avail
)
3146 if (di_a
->max_avail
< di_b
->max_avail
)
3148 if (di_a
->total_avail
> di_b
->total_avail
)
3150 if (di_a
->total_avail
< di_b
->total_avail
)
3155 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3156 struct btrfs_root
*extent_root
,
3157 struct map_lookup
**map_ret
,
3158 u64
*num_bytes_out
, u64
*stripe_size_out
,
3159 u64 start
, u64 type
)
3161 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3162 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3163 struct list_head
*cur
;
3164 struct map_lookup
*map
= NULL
;
3165 struct extent_map_tree
*em_tree
;
3166 struct extent_map
*em
;
3167 struct btrfs_device_info
*devices_info
= NULL
;
3169 int num_stripes
; /* total number of stripes to allocate */
3170 int sub_stripes
; /* sub_stripes info for map */
3171 int dev_stripes
; /* stripes per dev */
3172 int devs_max
; /* max devs to use */
3173 int devs_min
; /* min devs needed */
3174 int devs_increment
; /* ndevs has to be a multiple of this */
3175 int ncopies
; /* how many copies to data has */
3177 u64 max_stripe_size
;
3185 BUG_ON(!alloc_profile_is_valid(type
, 0));
3187 if (list_empty(&fs_devices
->alloc_list
))
3194 devs_max
= 0; /* 0 == as many as possible */
3198 * define the properties of each RAID type.
3199 * FIXME: move this to a global table and use it in all RAID
3202 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
3206 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
3208 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
3213 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
3222 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3223 max_stripe_size
= 1024 * 1024 * 1024;
3224 max_chunk_size
= 10 * max_stripe_size
;
3225 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3226 /* for larger filesystems, use larger metadata chunks */
3227 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3228 max_stripe_size
= 1024 * 1024 * 1024;
3230 max_stripe_size
= 256 * 1024 * 1024;
3231 max_chunk_size
= max_stripe_size
;
3232 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3233 max_stripe_size
= 32 * 1024 * 1024;
3234 max_chunk_size
= 2 * max_stripe_size
;
3236 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3241 /* we don't want a chunk larger than 10% of writeable space */
3242 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3245 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3250 cur
= fs_devices
->alloc_list
.next
;
3253 * in the first pass through the devices list, we gather information
3254 * about the available holes on each device.
3257 while (cur
!= &fs_devices
->alloc_list
) {
3258 struct btrfs_device
*device
;
3262 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3266 if (!device
->writeable
) {
3268 "btrfs: read-only device in alloc_list\n");
3273 if (!device
->in_fs_metadata
)
3276 if (device
->total_bytes
> device
->bytes_used
)
3277 total_avail
= device
->total_bytes
- device
->bytes_used
;
3281 /* If there is no space on this device, skip it. */
3282 if (total_avail
== 0)
3285 ret
= find_free_dev_extent(device
,
3286 max_stripe_size
* dev_stripes
,
3287 &dev_offset
, &max_avail
);
3288 if (ret
&& ret
!= -ENOSPC
)
3292 max_avail
= max_stripe_size
* dev_stripes
;
3294 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3297 devices_info
[ndevs
].dev_offset
= dev_offset
;
3298 devices_info
[ndevs
].max_avail
= max_avail
;
3299 devices_info
[ndevs
].total_avail
= total_avail
;
3300 devices_info
[ndevs
].dev
= device
;
3305 * now sort the devices by hole size / available space
3307 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3308 btrfs_cmp_device_info
, NULL
);
3310 /* round down to number of usable stripes */
3311 ndevs
-= ndevs
% devs_increment
;
3313 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3318 if (devs_max
&& ndevs
> devs_max
)
3321 * the primary goal is to maximize the number of stripes, so use as many
3322 * devices as possible, even if the stripes are not maximum sized.
3324 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3325 num_stripes
= ndevs
* dev_stripes
;
3327 if (stripe_size
* ndevs
> max_chunk_size
* ncopies
) {
3328 stripe_size
= max_chunk_size
* ncopies
;
3329 do_div(stripe_size
, ndevs
);
3332 do_div(stripe_size
, dev_stripes
);
3334 /* align to BTRFS_STRIPE_LEN */
3335 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
3336 stripe_size
*= BTRFS_STRIPE_LEN
;
3338 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3343 map
->num_stripes
= num_stripes
;
3345 for (i
= 0; i
< ndevs
; ++i
) {
3346 for (j
= 0; j
< dev_stripes
; ++j
) {
3347 int s
= i
* dev_stripes
+ j
;
3348 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3349 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3353 map
->sector_size
= extent_root
->sectorsize
;
3354 map
->stripe_len
= BTRFS_STRIPE_LEN
;
3355 map
->io_align
= BTRFS_STRIPE_LEN
;
3356 map
->io_width
= BTRFS_STRIPE_LEN
;
3358 map
->sub_stripes
= sub_stripes
;
3361 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
3363 *stripe_size_out
= stripe_size
;
3364 *num_bytes_out
= num_bytes
;
3366 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3368 em
= alloc_extent_map();
3373 em
->bdev
= (struct block_device
*)map
;
3375 em
->len
= num_bytes
;
3376 em
->block_start
= 0;
3377 em
->block_len
= em
->len
;
3379 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3380 write_lock(&em_tree
->lock
);
3381 ret
= add_extent_mapping(em_tree
, em
);
3382 write_unlock(&em_tree
->lock
);
3383 free_extent_map(em
);
3387 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3388 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3393 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3394 struct btrfs_device
*device
;
3397 device
= map
->stripes
[i
].dev
;
3398 dev_offset
= map
->stripes
[i
].physical
;
3400 ret
= btrfs_alloc_dev_extent(trans
, device
,
3401 info
->chunk_root
->root_key
.objectid
,
3402 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3403 start
, dev_offset
, stripe_size
);
3405 btrfs_abort_transaction(trans
, extent_root
, ret
);
3410 kfree(devices_info
);
3415 kfree(devices_info
);
3419 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3420 struct btrfs_root
*extent_root
,
3421 struct map_lookup
*map
, u64 chunk_offset
,
3422 u64 chunk_size
, u64 stripe_size
)
3425 struct btrfs_key key
;
3426 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3427 struct btrfs_device
*device
;
3428 struct btrfs_chunk
*chunk
;
3429 struct btrfs_stripe
*stripe
;
3430 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3434 chunk
= kzalloc(item_size
, GFP_NOFS
);
3439 while (index
< map
->num_stripes
) {
3440 device
= map
->stripes
[index
].dev
;
3441 device
->bytes_used
+= stripe_size
;
3442 ret
= btrfs_update_device(trans
, device
);
3448 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3449 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3451 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3454 stripe
= &chunk
->stripe
;
3455 while (index
< map
->num_stripes
) {
3456 device
= map
->stripes
[index
].dev
;
3457 dev_offset
= map
->stripes
[index
].physical
;
3459 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3460 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3461 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3466 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3467 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3468 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3469 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3470 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3471 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3472 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3473 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3474 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3476 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3477 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3478 key
.offset
= chunk_offset
;
3480 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3482 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3484 * TODO: Cleanup of inserted chunk root in case of
3487 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
3497 * Chunk allocation falls into two parts. The first part does works
3498 * that make the new allocated chunk useable, but not do any operation
3499 * that modifies the chunk tree. The second part does the works that
3500 * require modifying the chunk tree. This division is important for the
3501 * bootstrap process of adding storage to a seed btrfs.
3503 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3504 struct btrfs_root
*extent_root
, u64 type
)
3509 struct map_lookup
*map
;
3510 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3513 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3518 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3519 &stripe_size
, chunk_offset
, type
);
3523 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3524 chunk_size
, stripe_size
);
3530 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3531 struct btrfs_root
*root
,
3532 struct btrfs_device
*device
)
3535 u64 sys_chunk_offset
;
3539 u64 sys_stripe_size
;
3541 struct map_lookup
*map
;
3542 struct map_lookup
*sys_map
;
3543 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3544 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3547 ret
= find_next_chunk(fs_info
->chunk_root
,
3548 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3552 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3553 fs_info
->avail_metadata_alloc_bits
;
3554 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3556 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3557 &stripe_size
, chunk_offset
, alloc_profile
);
3561 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3563 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3564 fs_info
->avail_system_alloc_bits
;
3565 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3567 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3568 &sys_chunk_size
, &sys_stripe_size
,
3569 sys_chunk_offset
, alloc_profile
);
3573 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3578 * Modifying chunk tree needs allocating new blocks from both
3579 * system block group and metadata block group. So we only can
3580 * do operations require modifying the chunk tree after both
3581 * block groups were created.
3583 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3584 chunk_size
, stripe_size
);
3588 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
3589 sys_chunk_offset
, sys_chunk_size
,
3597 btrfs_abort_transaction(trans
, root
, ret
);
3601 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
3603 struct extent_map
*em
;
3604 struct map_lookup
*map
;
3605 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3609 read_lock(&map_tree
->map_tree
.lock
);
3610 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3611 read_unlock(&map_tree
->map_tree
.lock
);
3615 if (btrfs_test_opt(root
, DEGRADED
)) {
3616 free_extent_map(em
);
3620 map
= (struct map_lookup
*)em
->bdev
;
3621 for (i
= 0; i
< map
->num_stripes
; i
++) {
3622 if (!map
->stripes
[i
].dev
->writeable
) {
3627 free_extent_map(em
);
3631 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
3633 extent_map_tree_init(&tree
->map_tree
);
3636 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
3638 struct extent_map
*em
;
3641 write_lock(&tree
->map_tree
.lock
);
3642 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
3644 remove_extent_mapping(&tree
->map_tree
, em
);
3645 write_unlock(&tree
->map_tree
.lock
);
3650 free_extent_map(em
);
3651 /* once for the tree */
3652 free_extent_map(em
);
3656 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
3658 struct extent_map
*em
;
3659 struct map_lookup
*map
;
3660 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3663 read_lock(&em_tree
->lock
);
3664 em
= lookup_extent_mapping(em_tree
, logical
, len
);
3665 read_unlock(&em_tree
->lock
);
3668 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3669 map
= (struct map_lookup
*)em
->bdev
;
3670 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
3671 ret
= map
->num_stripes
;
3672 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3673 ret
= map
->sub_stripes
;
3676 free_extent_map(em
);
3680 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
3684 if (map
->stripes
[optimal
].dev
->bdev
)
3686 for (i
= first
; i
< first
+ num
; i
++) {
3687 if (map
->stripes
[i
].dev
->bdev
)
3690 /* we couldn't find one that doesn't fail. Just return something
3691 * and the io error handling code will clean up eventually
3696 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3697 u64 logical
, u64
*length
,
3698 struct btrfs_bio
**bbio_ret
,
3701 struct extent_map
*em
;
3702 struct map_lookup
*map
;
3703 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3706 u64 stripe_end_offset
;
3715 struct btrfs_bio
*bbio
= NULL
;
3717 read_lock(&em_tree
->lock
);
3718 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
3719 read_unlock(&em_tree
->lock
);
3722 printk(KERN_CRIT
"unable to find logical %llu len %llu\n",
3723 (unsigned long long)logical
,
3724 (unsigned long long)*length
);
3728 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3729 map
= (struct map_lookup
*)em
->bdev
;
3730 offset
= logical
- em
->start
;
3732 if (mirror_num
> map
->num_stripes
)
3737 * stripe_nr counts the total number of stripes we have to stride
3738 * to get to this block
3740 do_div(stripe_nr
, map
->stripe_len
);
3742 stripe_offset
= stripe_nr
* map
->stripe_len
;
3743 BUG_ON(offset
< stripe_offset
);
3745 /* stripe_offset is the offset of this block in its stripe*/
3746 stripe_offset
= offset
- stripe_offset
;
3748 if (rw
& REQ_DISCARD
)
3749 *length
= min_t(u64
, em
->len
- offset
, *length
);
3750 else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
3751 /* we limit the length of each bio to what fits in a stripe */
3752 *length
= min_t(u64
, em
->len
- offset
,
3753 map
->stripe_len
- stripe_offset
);
3755 *length
= em
->len
- offset
;
3763 stripe_nr_orig
= stripe_nr
;
3764 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
3765 (~(map
->stripe_len
- 1));
3766 do_div(stripe_nr_end
, map
->stripe_len
);
3767 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
3769 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3770 if (rw
& REQ_DISCARD
)
3771 num_stripes
= min_t(u64
, map
->num_stripes
,
3772 stripe_nr_end
- stripe_nr_orig
);
3773 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3774 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3775 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3776 num_stripes
= map
->num_stripes
;
3777 else if (mirror_num
)
3778 stripe_index
= mirror_num
- 1;
3780 stripe_index
= find_live_mirror(map
, 0,
3782 current
->pid
% map
->num_stripes
);
3783 mirror_num
= stripe_index
+ 1;
3786 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3787 if (rw
& (REQ_WRITE
| REQ_DISCARD
)) {
3788 num_stripes
= map
->num_stripes
;
3789 } else if (mirror_num
) {
3790 stripe_index
= mirror_num
- 1;
3795 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3796 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3798 stripe_index
= do_div(stripe_nr
, factor
);
3799 stripe_index
*= map
->sub_stripes
;
3802 num_stripes
= map
->sub_stripes
;
3803 else if (rw
& REQ_DISCARD
)
3804 num_stripes
= min_t(u64
, map
->sub_stripes
*
3805 (stripe_nr_end
- stripe_nr_orig
),
3807 else if (mirror_num
)
3808 stripe_index
+= mirror_num
- 1;
3810 int old_stripe_index
= stripe_index
;
3811 stripe_index
= find_live_mirror(map
, stripe_index
,
3812 map
->sub_stripes
, stripe_index
+
3813 current
->pid
% map
->sub_stripes
);
3814 mirror_num
= stripe_index
- old_stripe_index
+ 1;
3818 * after this do_div call, stripe_nr is the number of stripes
3819 * on this device we have to walk to find the data, and
3820 * stripe_index is the number of our device in the stripe array
3822 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3823 mirror_num
= stripe_index
+ 1;
3825 BUG_ON(stripe_index
>= map
->num_stripes
);
3827 bbio
= kzalloc(btrfs_bio_size(num_stripes
), GFP_NOFS
);
3832 atomic_set(&bbio
->error
, 0);
3834 if (rw
& REQ_DISCARD
) {
3836 int sub_stripes
= 0;
3837 u64 stripes_per_dev
= 0;
3838 u32 remaining_stripes
= 0;
3839 u32 last_stripe
= 0;
3842 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
3843 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3846 sub_stripes
= map
->sub_stripes
;
3848 factor
= map
->num_stripes
/ sub_stripes
;
3849 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
3852 &remaining_stripes
);
3853 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
3854 last_stripe
*= sub_stripes
;
3857 for (i
= 0; i
< num_stripes
; i
++) {
3858 bbio
->stripes
[i
].physical
=
3859 map
->stripes
[stripe_index
].physical
+
3860 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3861 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3863 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
3864 BTRFS_BLOCK_GROUP_RAID10
)) {
3865 bbio
->stripes
[i
].length
= stripes_per_dev
*
3868 if (i
/ sub_stripes
< remaining_stripes
)
3869 bbio
->stripes
[i
].length
+=
3873 * Special for the first stripe and
3876 * |-------|...|-------|
3880 if (i
< sub_stripes
)
3881 bbio
->stripes
[i
].length
-=
3884 if (stripe_index
>= last_stripe
&&
3885 stripe_index
<= (last_stripe
+
3887 bbio
->stripes
[i
].length
-=
3890 if (i
== sub_stripes
- 1)
3893 bbio
->stripes
[i
].length
= *length
;
3896 if (stripe_index
== map
->num_stripes
) {
3897 /* This could only happen for RAID0/10 */
3903 for (i
= 0; i
< num_stripes
; i
++) {
3904 bbio
->stripes
[i
].physical
=
3905 map
->stripes
[stripe_index
].physical
+
3907 stripe_nr
* map
->stripe_len
;
3908 bbio
->stripes
[i
].dev
=
3909 map
->stripes
[stripe_index
].dev
;
3914 if (rw
& REQ_WRITE
) {
3915 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
3916 BTRFS_BLOCK_GROUP_RAID10
|
3917 BTRFS_BLOCK_GROUP_DUP
)) {
3923 bbio
->num_stripes
= num_stripes
;
3924 bbio
->max_errors
= max_errors
;
3925 bbio
->mirror_num
= mirror_num
;
3927 free_extent_map(em
);
3931 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3932 u64 logical
, u64
*length
,
3933 struct btrfs_bio
**bbio_ret
, int mirror_num
)
3935 return __btrfs_map_block(map_tree
, rw
, logical
, length
, bbio_ret
,
3939 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
3940 u64 chunk_start
, u64 physical
, u64 devid
,
3941 u64
**logical
, int *naddrs
, int *stripe_len
)
3943 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3944 struct extent_map
*em
;
3945 struct map_lookup
*map
;
3952 read_lock(&em_tree
->lock
);
3953 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
3954 read_unlock(&em_tree
->lock
);
3956 BUG_ON(!em
|| em
->start
!= chunk_start
);
3957 map
= (struct map_lookup
*)em
->bdev
;
3960 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3961 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
3962 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3963 do_div(length
, map
->num_stripes
);
3965 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
3966 BUG_ON(!buf
); /* -ENOMEM */
3968 for (i
= 0; i
< map
->num_stripes
; i
++) {
3969 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
3971 if (map
->stripes
[i
].physical
> physical
||
3972 map
->stripes
[i
].physical
+ length
<= physical
)
3975 stripe_nr
= physical
- map
->stripes
[i
].physical
;
3976 do_div(stripe_nr
, map
->stripe_len
);
3978 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3979 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3980 do_div(stripe_nr
, map
->sub_stripes
);
3981 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3982 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
3984 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
3985 WARN_ON(nr
>= map
->num_stripes
);
3986 for (j
= 0; j
< nr
; j
++) {
3987 if (buf
[j
] == bytenr
)
3991 WARN_ON(nr
>= map
->num_stripes
);
3998 *stripe_len
= map
->stripe_len
;
4000 free_extent_map(em
);
4004 static void btrfs_end_bio(struct bio
*bio
, int err
)
4006 struct btrfs_bio
*bbio
= bio
->bi_private
;
4007 int is_orig_bio
= 0;
4010 atomic_inc(&bbio
->error
);
4012 if (bio
== bbio
->orig_bio
)
4015 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4018 bio
= bbio
->orig_bio
;
4020 bio
->bi_private
= bbio
->private;
4021 bio
->bi_end_io
= bbio
->end_io
;
4022 bio
->bi_bdev
= (struct block_device
*)
4023 (unsigned long)bbio
->mirror_num
;
4024 /* only send an error to the higher layers if it is
4025 * beyond the tolerance of the multi-bio
4027 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
4031 * this bio is actually up to date, we didn't
4032 * go over the max number of errors
4034 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
4039 bio_endio(bio
, err
);
4040 } else if (!is_orig_bio
) {
4045 struct async_sched
{
4048 struct btrfs_fs_info
*info
;
4049 struct btrfs_work work
;
4053 * see run_scheduled_bios for a description of why bios are collected for
4056 * This will add one bio to the pending list for a device and make sure
4057 * the work struct is scheduled.
4059 static noinline
void schedule_bio(struct btrfs_root
*root
,
4060 struct btrfs_device
*device
,
4061 int rw
, struct bio
*bio
)
4063 int should_queue
= 1;
4064 struct btrfs_pending_bios
*pending_bios
;
4066 /* don't bother with additional async steps for reads, right now */
4067 if (!(rw
& REQ_WRITE
)) {
4069 btrfsic_submit_bio(rw
, bio
);
4075 * nr_async_bios allows us to reliably return congestion to the
4076 * higher layers. Otherwise, the async bio makes it appear we have
4077 * made progress against dirty pages when we've really just put it
4078 * on a queue for later
4080 atomic_inc(&root
->fs_info
->nr_async_bios
);
4081 WARN_ON(bio
->bi_next
);
4082 bio
->bi_next
= NULL
;
4085 spin_lock(&device
->io_lock
);
4086 if (bio
->bi_rw
& REQ_SYNC
)
4087 pending_bios
= &device
->pending_sync_bios
;
4089 pending_bios
= &device
->pending_bios
;
4091 if (pending_bios
->tail
)
4092 pending_bios
->tail
->bi_next
= bio
;
4094 pending_bios
->tail
= bio
;
4095 if (!pending_bios
->head
)
4096 pending_bios
->head
= bio
;
4097 if (device
->running_pending
)
4100 spin_unlock(&device
->io_lock
);
4103 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
4107 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
4108 int mirror_num
, int async_submit
)
4110 struct btrfs_mapping_tree
*map_tree
;
4111 struct btrfs_device
*dev
;
4112 struct bio
*first_bio
= bio
;
4113 u64 logical
= (u64
)bio
->bi_sector
<< 9;
4119 struct btrfs_bio
*bbio
= NULL
;
4121 length
= bio
->bi_size
;
4122 map_tree
= &root
->fs_info
->mapping_tree
;
4123 map_length
= length
;
4125 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &bbio
,
4127 if (ret
) /* -ENOMEM */
4130 total_devs
= bbio
->num_stripes
;
4131 if (map_length
< length
) {
4132 printk(KERN_CRIT
"mapping failed logical %llu bio len %llu "
4133 "len %llu\n", (unsigned long long)logical
,
4134 (unsigned long long)length
,
4135 (unsigned long long)map_length
);
4139 bbio
->orig_bio
= first_bio
;
4140 bbio
->private = first_bio
->bi_private
;
4141 bbio
->end_io
= first_bio
->bi_end_io
;
4142 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
4144 while (dev_nr
< total_devs
) {
4145 if (dev_nr
< total_devs
- 1) {
4146 bio
= bio_clone(first_bio
, GFP_NOFS
);
4147 BUG_ON(!bio
); /* -ENOMEM */
4151 bio
->bi_private
= bbio
;
4152 bio
->bi_end_io
= btrfs_end_bio
;
4153 bio
->bi_sector
= bbio
->stripes
[dev_nr
].physical
>> 9;
4154 dev
= bbio
->stripes
[dev_nr
].dev
;
4155 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
4156 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4157 "(%s id %llu), size=%u\n", rw
,
4158 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
4159 dev
->name
, dev
->devid
, bio
->bi_size
);
4160 bio
->bi_bdev
= dev
->bdev
;
4162 schedule_bio(root
, dev
, rw
, bio
);
4164 btrfsic_submit_bio(rw
, bio
);
4166 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4167 bio
->bi_sector
= logical
>> 9;
4168 bio_endio(bio
, -EIO
);
4175 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
4178 struct btrfs_device
*device
;
4179 struct btrfs_fs_devices
*cur_devices
;
4181 cur_devices
= root
->fs_info
->fs_devices
;
4182 while (cur_devices
) {
4184 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4185 device
= __find_device(&cur_devices
->devices
,
4190 cur_devices
= cur_devices
->seed
;
4195 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
4196 u64 devid
, u8
*dev_uuid
)
4198 struct btrfs_device
*device
;
4199 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4201 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
4204 list_add(&device
->dev_list
,
4205 &fs_devices
->devices
);
4206 device
->dev_root
= root
->fs_info
->dev_root
;
4207 device
->devid
= devid
;
4208 device
->work
.func
= pending_bios_fn
;
4209 device
->fs_devices
= fs_devices
;
4210 device
->missing
= 1;
4211 fs_devices
->num_devices
++;
4212 fs_devices
->missing_devices
++;
4213 spin_lock_init(&device
->io_lock
);
4214 INIT_LIST_HEAD(&device
->dev_alloc_list
);
4215 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
4219 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
4220 struct extent_buffer
*leaf
,
4221 struct btrfs_chunk
*chunk
)
4223 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4224 struct map_lookup
*map
;
4225 struct extent_map
*em
;
4229 u8 uuid
[BTRFS_UUID_SIZE
];
4234 logical
= key
->offset
;
4235 length
= btrfs_chunk_length(leaf
, chunk
);
4237 read_lock(&map_tree
->map_tree
.lock
);
4238 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
4239 read_unlock(&map_tree
->map_tree
.lock
);
4241 /* already mapped? */
4242 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
4243 free_extent_map(em
);
4246 free_extent_map(em
);
4249 em
= alloc_extent_map();
4252 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
4253 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4255 free_extent_map(em
);
4259 em
->bdev
= (struct block_device
*)map
;
4260 em
->start
= logical
;
4262 em
->block_start
= 0;
4263 em
->block_len
= em
->len
;
4265 map
->num_stripes
= num_stripes
;
4266 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
4267 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
4268 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
4269 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
4270 map
->type
= btrfs_chunk_type(leaf
, chunk
);
4271 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
4272 for (i
= 0; i
< num_stripes
; i
++) {
4273 map
->stripes
[i
].physical
=
4274 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
4275 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
4276 read_extent_buffer(leaf
, uuid
, (unsigned long)
4277 btrfs_stripe_dev_uuid_nr(chunk
, i
),
4279 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
4281 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
4283 free_extent_map(em
);
4286 if (!map
->stripes
[i
].dev
) {
4287 map
->stripes
[i
].dev
=
4288 add_missing_dev(root
, devid
, uuid
);
4289 if (!map
->stripes
[i
].dev
) {
4291 free_extent_map(em
);
4295 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
4298 write_lock(&map_tree
->map_tree
.lock
);
4299 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
4300 write_unlock(&map_tree
->map_tree
.lock
);
4301 BUG_ON(ret
); /* Tree corruption */
4302 free_extent_map(em
);
4307 static void fill_device_from_item(struct extent_buffer
*leaf
,
4308 struct btrfs_dev_item
*dev_item
,
4309 struct btrfs_device
*device
)
4313 device
->devid
= btrfs_device_id(leaf
, dev_item
);
4314 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
4315 device
->total_bytes
= device
->disk_total_bytes
;
4316 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
4317 device
->type
= btrfs_device_type(leaf
, dev_item
);
4318 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
4319 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
4320 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
4322 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
4323 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
4326 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
4328 struct btrfs_fs_devices
*fs_devices
;
4331 BUG_ON(!mutex_is_locked(&uuid_mutex
));
4333 fs_devices
= root
->fs_info
->fs_devices
->seed
;
4334 while (fs_devices
) {
4335 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4339 fs_devices
= fs_devices
->seed
;
4342 fs_devices
= find_fsid(fsid
);
4348 fs_devices
= clone_fs_devices(fs_devices
);
4349 if (IS_ERR(fs_devices
)) {
4350 ret
= PTR_ERR(fs_devices
);
4354 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
4355 root
->fs_info
->bdev_holder
);
4357 free_fs_devices(fs_devices
);
4361 if (!fs_devices
->seeding
) {
4362 __btrfs_close_devices(fs_devices
);
4363 free_fs_devices(fs_devices
);
4368 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
4369 root
->fs_info
->fs_devices
->seed
= fs_devices
;
4374 static int read_one_dev(struct btrfs_root
*root
,
4375 struct extent_buffer
*leaf
,
4376 struct btrfs_dev_item
*dev_item
)
4378 struct btrfs_device
*device
;
4381 u8 fs_uuid
[BTRFS_UUID_SIZE
];
4382 u8 dev_uuid
[BTRFS_UUID_SIZE
];
4384 devid
= btrfs_device_id(leaf
, dev_item
);
4385 read_extent_buffer(leaf
, dev_uuid
,
4386 (unsigned long)btrfs_device_uuid(dev_item
),
4388 read_extent_buffer(leaf
, fs_uuid
,
4389 (unsigned long)btrfs_device_fsid(dev_item
),
4392 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
4393 ret
= open_seed_devices(root
, fs_uuid
);
4394 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
4398 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
4399 if (!device
|| !device
->bdev
) {
4400 if (!btrfs_test_opt(root
, DEGRADED
))
4404 printk(KERN_WARNING
"warning devid %llu missing\n",
4405 (unsigned long long)devid
);
4406 device
= add_missing_dev(root
, devid
, dev_uuid
);
4409 } else if (!device
->missing
) {
4411 * this happens when a device that was properly setup
4412 * in the device info lists suddenly goes bad.
4413 * device->bdev is NULL, and so we have to set
4414 * device->missing to one here
4416 root
->fs_info
->fs_devices
->missing_devices
++;
4417 device
->missing
= 1;
4421 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
4422 BUG_ON(device
->writeable
);
4423 if (device
->generation
!=
4424 btrfs_device_generation(leaf
, dev_item
))
4428 fill_device_from_item(leaf
, dev_item
, device
);
4429 device
->dev_root
= root
->fs_info
->dev_root
;
4430 device
->in_fs_metadata
= 1;
4431 if (device
->writeable
) {
4432 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
4433 spin_lock(&root
->fs_info
->free_chunk_lock
);
4434 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
4436 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4442 int btrfs_read_sys_array(struct btrfs_root
*root
)
4444 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4445 struct extent_buffer
*sb
;
4446 struct btrfs_disk_key
*disk_key
;
4447 struct btrfs_chunk
*chunk
;
4449 unsigned long sb_ptr
;
4455 struct btrfs_key key
;
4457 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
4458 BTRFS_SUPER_INFO_SIZE
);
4461 btrfs_set_buffer_uptodate(sb
);
4462 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
4464 * The sb extent buffer is artifical and just used to read the system array.
4465 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4466 * pages up-to-date when the page is larger: extent does not cover the
4467 * whole page and consequently check_page_uptodate does not find all
4468 * the page's extents up-to-date (the hole beyond sb),
4469 * write_extent_buffer then triggers a WARN_ON.
4471 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4472 * but sb spans only this function. Add an explicit SetPageUptodate call
4473 * to silence the warning eg. on PowerPC 64.
4475 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
4476 SetPageUptodate(sb
->pages
[0]);
4478 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
4479 array_size
= btrfs_super_sys_array_size(super_copy
);
4481 ptr
= super_copy
->sys_chunk_array
;
4482 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
4485 while (cur
< array_size
) {
4486 disk_key
= (struct btrfs_disk_key
*)ptr
;
4487 btrfs_disk_key_to_cpu(&key
, disk_key
);
4489 len
= sizeof(*disk_key
); ptr
+= len
;
4493 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4494 chunk
= (struct btrfs_chunk
*)sb_ptr
;
4495 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
4498 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
4499 len
= btrfs_chunk_item_size(num_stripes
);
4508 free_extent_buffer(sb
);
4512 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
4514 struct btrfs_path
*path
;
4515 struct extent_buffer
*leaf
;
4516 struct btrfs_key key
;
4517 struct btrfs_key found_key
;
4521 root
= root
->fs_info
->chunk_root
;
4523 path
= btrfs_alloc_path();
4527 mutex_lock(&uuid_mutex
);
4530 /* first we search for all of the device items, and then we
4531 * read in all of the chunk items. This way we can create chunk
4532 * mappings that reference all of the devices that are afound
4534 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
4538 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4542 leaf
= path
->nodes
[0];
4543 slot
= path
->slots
[0];
4544 if (slot
>= btrfs_header_nritems(leaf
)) {
4545 ret
= btrfs_next_leaf(root
, path
);
4552 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4553 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4554 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
4556 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
4557 struct btrfs_dev_item
*dev_item
;
4558 dev_item
= btrfs_item_ptr(leaf
, slot
,
4559 struct btrfs_dev_item
);
4560 ret
= read_one_dev(root
, leaf
, dev_item
);
4564 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4565 struct btrfs_chunk
*chunk
;
4566 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
4567 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
4573 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4575 btrfs_release_path(path
);
4580 unlock_chunks(root
);
4581 mutex_unlock(&uuid_mutex
);
4583 btrfs_free_path(path
);