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/ratelimit.h>
27 #include <linux/kthread.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"
37 #include "rcu-string.h"
40 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
41 struct btrfs_root
*root
,
42 struct btrfs_device
*device
);
43 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
44 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
45 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
47 static DEFINE_MUTEX(uuid_mutex
);
48 static LIST_HEAD(fs_uuids
);
50 static void lock_chunks(struct btrfs_root
*root
)
52 mutex_lock(&root
->fs_info
->chunk_mutex
);
55 static void unlock_chunks(struct btrfs_root
*root
)
57 mutex_unlock(&root
->fs_info
->chunk_mutex
);
60 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
62 struct btrfs_device
*device
;
63 WARN_ON(fs_devices
->opened
);
64 while (!list_empty(&fs_devices
->devices
)) {
65 device
= list_entry(fs_devices
->devices
.next
,
66 struct btrfs_device
, dev_list
);
67 list_del(&device
->dev_list
);
68 rcu_string_free(device
->name
);
74 void btrfs_cleanup_fs_uuids(void)
76 struct btrfs_fs_devices
*fs_devices
;
78 while (!list_empty(&fs_uuids
)) {
79 fs_devices
= list_entry(fs_uuids
.next
,
80 struct btrfs_fs_devices
, list
);
81 list_del(&fs_devices
->list
);
82 free_fs_devices(fs_devices
);
86 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
89 struct btrfs_device
*dev
;
91 list_for_each_entry(dev
, head
, dev_list
) {
92 if (dev
->devid
== devid
&&
93 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
100 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
102 struct btrfs_fs_devices
*fs_devices
;
104 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
105 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
112 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
113 int flush
, struct block_device
**bdev
,
114 struct buffer_head
**bh
)
118 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
121 ret
= PTR_ERR(*bdev
);
122 printk(KERN_INFO
"btrfs: open %s failed\n", device_path
);
127 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
128 ret
= set_blocksize(*bdev
, 4096);
130 blkdev_put(*bdev
, flags
);
133 invalidate_bdev(*bdev
);
134 *bh
= btrfs_read_dev_super(*bdev
);
137 blkdev_put(*bdev
, flags
);
149 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
150 struct bio
*head
, struct bio
*tail
)
153 struct bio
*old_head
;
155 old_head
= pending_bios
->head
;
156 pending_bios
->head
= head
;
157 if (pending_bios
->tail
)
158 tail
->bi_next
= old_head
;
160 pending_bios
->tail
= tail
;
164 * we try to collect pending bios for a device so we don't get a large
165 * number of procs sending bios down to the same device. This greatly
166 * improves the schedulers ability to collect and merge the bios.
168 * But, it also turns into a long list of bios to process and that is sure
169 * to eventually make the worker thread block. The solution here is to
170 * make some progress and then put this work struct back at the end of
171 * the list if the block device is congested. This way, multiple devices
172 * can make progress from a single worker thread.
174 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
177 struct backing_dev_info
*bdi
;
178 struct btrfs_fs_info
*fs_info
;
179 struct btrfs_pending_bios
*pending_bios
;
183 unsigned long num_run
;
184 unsigned long batch_run
= 0;
186 unsigned long last_waited
= 0;
188 int sync_pending
= 0;
189 struct blk_plug plug
;
192 * this function runs all the bios we've collected for
193 * a particular device. We don't want to wander off to
194 * another device without first sending all of these down.
195 * So, setup a plug here and finish it off before we return
197 blk_start_plug(&plug
);
199 bdi
= blk_get_backing_dev_info(device
->bdev
);
200 fs_info
= device
->dev_root
->fs_info
;
201 limit
= btrfs_async_submit_limit(fs_info
);
202 limit
= limit
* 2 / 3;
205 spin_lock(&device
->io_lock
);
210 /* take all the bios off the list at once and process them
211 * later on (without the lock held). But, remember the
212 * tail and other pointers so the bios can be properly reinserted
213 * into the list if we hit congestion
215 if (!force_reg
&& device
->pending_sync_bios
.head
) {
216 pending_bios
= &device
->pending_sync_bios
;
219 pending_bios
= &device
->pending_bios
;
223 pending
= pending_bios
->head
;
224 tail
= pending_bios
->tail
;
225 WARN_ON(pending
&& !tail
);
228 * if pending was null this time around, no bios need processing
229 * at all and we can stop. Otherwise it'll loop back up again
230 * and do an additional check so no bios are missed.
232 * device->running_pending is used to synchronize with the
235 if (device
->pending_sync_bios
.head
== NULL
&&
236 device
->pending_bios
.head
== NULL
) {
238 device
->running_pending
= 0;
241 device
->running_pending
= 1;
244 pending_bios
->head
= NULL
;
245 pending_bios
->tail
= NULL
;
247 spin_unlock(&device
->io_lock
);
252 /* we want to work on both lists, but do more bios on the
253 * sync list than the regular list
256 pending_bios
!= &device
->pending_sync_bios
&&
257 device
->pending_sync_bios
.head
) ||
258 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
259 device
->pending_bios
.head
)) {
260 spin_lock(&device
->io_lock
);
261 requeue_list(pending_bios
, pending
, tail
);
266 pending
= pending
->bi_next
;
269 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
270 waitqueue_active(&fs_info
->async_submit_wait
))
271 wake_up(&fs_info
->async_submit_wait
);
273 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
276 * if we're doing the sync list, record that our
277 * plug has some sync requests on it
279 * If we're doing the regular list and there are
280 * sync requests sitting around, unplug before
283 if (pending_bios
== &device
->pending_sync_bios
) {
285 } else if (sync_pending
) {
286 blk_finish_plug(&plug
);
287 blk_start_plug(&plug
);
291 btrfsic_submit_bio(cur
->bi_rw
, cur
);
298 * we made progress, there is more work to do and the bdi
299 * is now congested. Back off and let other work structs
302 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
303 fs_info
->fs_devices
->open_devices
> 1) {
304 struct io_context
*ioc
;
306 ioc
= current
->io_context
;
309 * the main goal here is that we don't want to
310 * block if we're going to be able to submit
311 * more requests without blocking.
313 * This code does two great things, it pokes into
314 * the elevator code from a filesystem _and_
315 * it makes assumptions about how batching works.
317 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
318 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
320 ioc
->last_waited
== last_waited
)) {
322 * we want to go through our batch of
323 * requests and stop. So, we copy out
324 * the ioc->last_waited time and test
325 * against it before looping
327 last_waited
= ioc
->last_waited
;
332 spin_lock(&device
->io_lock
);
333 requeue_list(pending_bios
, pending
, tail
);
334 device
->running_pending
= 1;
336 spin_unlock(&device
->io_lock
);
337 btrfs_requeue_work(&device
->work
);
340 /* unplug every 64 requests just for good measure */
341 if (batch_run
% 64 == 0) {
342 blk_finish_plug(&plug
);
343 blk_start_plug(&plug
);
352 spin_lock(&device
->io_lock
);
353 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
355 spin_unlock(&device
->io_lock
);
358 blk_finish_plug(&plug
);
361 static void pending_bios_fn(struct btrfs_work
*work
)
363 struct btrfs_device
*device
;
365 device
= container_of(work
, struct btrfs_device
, work
);
366 run_scheduled_bios(device
);
369 static noinline
int device_list_add(const char *path
,
370 struct btrfs_super_block
*disk_super
,
371 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
373 struct btrfs_device
*device
;
374 struct btrfs_fs_devices
*fs_devices
;
375 struct rcu_string
*name
;
376 u64 found_transid
= btrfs_super_generation(disk_super
);
378 fs_devices
= find_fsid(disk_super
->fsid
);
380 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
383 INIT_LIST_HEAD(&fs_devices
->devices
);
384 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
385 list_add(&fs_devices
->list
, &fs_uuids
);
386 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
387 fs_devices
->latest_devid
= devid
;
388 fs_devices
->latest_trans
= found_transid
;
389 mutex_init(&fs_devices
->device_list_mutex
);
392 device
= __find_device(&fs_devices
->devices
, devid
,
393 disk_super
->dev_item
.uuid
);
396 if (fs_devices
->opened
)
399 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
401 /* we can safely leave the fs_devices entry around */
404 device
->devid
= devid
;
405 device
->dev_stats_valid
= 0;
406 device
->work
.func
= pending_bios_fn
;
407 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
409 spin_lock_init(&device
->io_lock
);
411 name
= rcu_string_strdup(path
, GFP_NOFS
);
416 rcu_assign_pointer(device
->name
, name
);
417 INIT_LIST_HEAD(&device
->dev_alloc_list
);
419 /* init readahead state */
420 spin_lock_init(&device
->reada_lock
);
421 device
->reada_curr_zone
= NULL
;
422 atomic_set(&device
->reada_in_flight
, 0);
423 device
->reada_next
= 0;
424 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
425 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
427 mutex_lock(&fs_devices
->device_list_mutex
);
428 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
429 mutex_unlock(&fs_devices
->device_list_mutex
);
431 device
->fs_devices
= fs_devices
;
432 fs_devices
->num_devices
++;
433 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
434 name
= rcu_string_strdup(path
, GFP_NOFS
);
437 rcu_string_free(device
->name
);
438 rcu_assign_pointer(device
->name
, name
);
439 if (device
->missing
) {
440 fs_devices
->missing_devices
--;
445 if (found_transid
> fs_devices
->latest_trans
) {
446 fs_devices
->latest_devid
= devid
;
447 fs_devices
->latest_trans
= found_transid
;
449 *fs_devices_ret
= fs_devices
;
453 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
455 struct btrfs_fs_devices
*fs_devices
;
456 struct btrfs_device
*device
;
457 struct btrfs_device
*orig_dev
;
459 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
461 return ERR_PTR(-ENOMEM
);
463 INIT_LIST_HEAD(&fs_devices
->devices
);
464 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
465 INIT_LIST_HEAD(&fs_devices
->list
);
466 mutex_init(&fs_devices
->device_list_mutex
);
467 fs_devices
->latest_devid
= orig
->latest_devid
;
468 fs_devices
->latest_trans
= orig
->latest_trans
;
469 fs_devices
->total_devices
= orig
->total_devices
;
470 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
472 /* We have held the volume lock, it is safe to get the devices. */
473 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
474 struct rcu_string
*name
;
476 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
481 * This is ok to do without rcu read locked because we hold the
482 * uuid mutex so nothing we touch in here is going to disappear.
484 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
489 rcu_assign_pointer(device
->name
, name
);
491 device
->devid
= orig_dev
->devid
;
492 device
->work
.func
= pending_bios_fn
;
493 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
494 spin_lock_init(&device
->io_lock
);
495 INIT_LIST_HEAD(&device
->dev_list
);
496 INIT_LIST_HEAD(&device
->dev_alloc_list
);
498 list_add(&device
->dev_list
, &fs_devices
->devices
);
499 device
->fs_devices
= fs_devices
;
500 fs_devices
->num_devices
++;
504 free_fs_devices(fs_devices
);
505 return ERR_PTR(-ENOMEM
);
508 void btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
510 struct btrfs_device
*device
, *next
;
512 struct block_device
*latest_bdev
= NULL
;
513 u64 latest_devid
= 0;
514 u64 latest_transid
= 0;
516 mutex_lock(&uuid_mutex
);
518 /* This is the initialized path, it is safe to release the devices. */
519 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
520 if (device
->in_fs_metadata
) {
521 if (!device
->is_tgtdev_for_dev_replace
&&
523 device
->generation
> latest_transid
)) {
524 latest_devid
= device
->devid
;
525 latest_transid
= device
->generation
;
526 latest_bdev
= device
->bdev
;
532 blkdev_put(device
->bdev
, device
->mode
);
534 fs_devices
->open_devices
--;
536 if (device
->writeable
) {
537 list_del_init(&device
->dev_alloc_list
);
538 device
->writeable
= 0;
539 fs_devices
->rw_devices
--;
541 list_del_init(&device
->dev_list
);
542 fs_devices
->num_devices
--;
543 rcu_string_free(device
->name
);
547 if (fs_devices
->seed
) {
548 fs_devices
= fs_devices
->seed
;
552 fs_devices
->latest_bdev
= latest_bdev
;
553 fs_devices
->latest_devid
= latest_devid
;
554 fs_devices
->latest_trans
= latest_transid
;
556 mutex_unlock(&uuid_mutex
);
559 static void __free_device(struct work_struct
*work
)
561 struct btrfs_device
*device
;
563 device
= container_of(work
, struct btrfs_device
, rcu_work
);
566 blkdev_put(device
->bdev
, device
->mode
);
568 rcu_string_free(device
->name
);
572 static void free_device(struct rcu_head
*head
)
574 struct btrfs_device
*device
;
576 device
= container_of(head
, struct btrfs_device
, rcu
);
578 INIT_WORK(&device
->rcu_work
, __free_device
);
579 schedule_work(&device
->rcu_work
);
582 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
584 struct btrfs_device
*device
;
586 if (--fs_devices
->opened
> 0)
589 mutex_lock(&fs_devices
->device_list_mutex
);
590 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
591 struct btrfs_device
*new_device
;
592 struct rcu_string
*name
;
595 fs_devices
->open_devices
--;
597 if (device
->writeable
) {
598 list_del_init(&device
->dev_alloc_list
);
599 fs_devices
->rw_devices
--;
602 if (device
->can_discard
)
603 fs_devices
->num_can_discard
--;
605 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
606 BUG_ON(!new_device
); /* -ENOMEM */
607 memcpy(new_device
, device
, sizeof(*new_device
));
609 /* Safe because we are under uuid_mutex */
611 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
612 BUG_ON(device
->name
&& !name
); /* -ENOMEM */
613 rcu_assign_pointer(new_device
->name
, name
);
615 new_device
->bdev
= NULL
;
616 new_device
->writeable
= 0;
617 new_device
->in_fs_metadata
= 0;
618 new_device
->can_discard
= 0;
619 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
621 call_rcu(&device
->rcu
, free_device
);
623 mutex_unlock(&fs_devices
->device_list_mutex
);
625 WARN_ON(fs_devices
->open_devices
);
626 WARN_ON(fs_devices
->rw_devices
);
627 fs_devices
->opened
= 0;
628 fs_devices
->seeding
= 0;
633 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
635 struct btrfs_fs_devices
*seed_devices
= NULL
;
638 mutex_lock(&uuid_mutex
);
639 ret
= __btrfs_close_devices(fs_devices
);
640 if (!fs_devices
->opened
) {
641 seed_devices
= fs_devices
->seed
;
642 fs_devices
->seed
= NULL
;
644 mutex_unlock(&uuid_mutex
);
646 while (seed_devices
) {
647 fs_devices
= seed_devices
;
648 seed_devices
= fs_devices
->seed
;
649 __btrfs_close_devices(fs_devices
);
650 free_fs_devices(fs_devices
);
655 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
656 fmode_t flags
, void *holder
)
658 struct request_queue
*q
;
659 struct block_device
*bdev
;
660 struct list_head
*head
= &fs_devices
->devices
;
661 struct btrfs_device
*device
;
662 struct block_device
*latest_bdev
= NULL
;
663 struct buffer_head
*bh
;
664 struct btrfs_super_block
*disk_super
;
665 u64 latest_devid
= 0;
666 u64 latest_transid
= 0;
673 list_for_each_entry(device
, head
, dev_list
) {
679 ret
= btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
684 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
685 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
686 if (devid
!= device
->devid
)
689 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
693 device
->generation
= btrfs_super_generation(disk_super
);
694 if (!latest_transid
|| device
->generation
> latest_transid
) {
695 latest_devid
= devid
;
696 latest_transid
= device
->generation
;
700 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
701 device
->writeable
= 0;
703 device
->writeable
= !bdev_read_only(bdev
);
707 q
= bdev_get_queue(bdev
);
708 if (blk_queue_discard(q
)) {
709 device
->can_discard
= 1;
710 fs_devices
->num_can_discard
++;
714 device
->in_fs_metadata
= 0;
715 device
->mode
= flags
;
717 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
718 fs_devices
->rotating
= 1;
720 fs_devices
->open_devices
++;
721 if (device
->writeable
) {
722 fs_devices
->rw_devices
++;
723 list_add(&device
->dev_alloc_list
,
724 &fs_devices
->alloc_list
);
731 blkdev_put(bdev
, flags
);
734 if (fs_devices
->open_devices
== 0) {
738 fs_devices
->seeding
= seeding
;
739 fs_devices
->opened
= 1;
740 fs_devices
->latest_bdev
= latest_bdev
;
741 fs_devices
->latest_devid
= latest_devid
;
742 fs_devices
->latest_trans
= latest_transid
;
743 fs_devices
->total_rw_bytes
= 0;
748 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
749 fmode_t flags
, void *holder
)
753 mutex_lock(&uuid_mutex
);
754 if (fs_devices
->opened
) {
755 fs_devices
->opened
++;
758 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
760 mutex_unlock(&uuid_mutex
);
764 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
765 struct btrfs_fs_devices
**fs_devices_ret
)
767 struct btrfs_super_block
*disk_super
;
768 struct block_device
*bdev
;
769 struct buffer_head
*bh
;
776 mutex_lock(&uuid_mutex
);
777 ret
= btrfs_get_bdev_and_sb(path
, flags
, holder
, 0, &bdev
, &bh
);
780 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
781 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
782 transid
= btrfs_super_generation(disk_super
);
783 total_devices
= btrfs_super_num_devices(disk_super
);
784 if (disk_super
->label
[0]) {
785 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
786 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
787 printk(KERN_INFO
"device label %s ", disk_super
->label
);
789 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
791 printk(KERN_CONT
"devid %llu transid %llu %s\n",
792 (unsigned long long)devid
, (unsigned long long)transid
, path
);
793 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
794 if (!ret
&& fs_devices_ret
)
795 (*fs_devices_ret
)->total_devices
= total_devices
;
797 blkdev_put(bdev
, flags
);
799 mutex_unlock(&uuid_mutex
);
803 /* helper to account the used device space in the range */
804 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
805 u64 end
, u64
*length
)
807 struct btrfs_key key
;
808 struct btrfs_root
*root
= device
->dev_root
;
809 struct btrfs_dev_extent
*dev_extent
;
810 struct btrfs_path
*path
;
814 struct extent_buffer
*l
;
818 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
821 path
= btrfs_alloc_path();
826 key
.objectid
= device
->devid
;
828 key
.type
= BTRFS_DEV_EXTENT_KEY
;
830 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
834 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
841 slot
= path
->slots
[0];
842 if (slot
>= btrfs_header_nritems(l
)) {
843 ret
= btrfs_next_leaf(root
, path
);
851 btrfs_item_key_to_cpu(l
, &key
, slot
);
853 if (key
.objectid
< device
->devid
)
856 if (key
.objectid
> device
->devid
)
859 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
862 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
863 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
865 if (key
.offset
<= start
&& extent_end
> end
) {
866 *length
= end
- start
+ 1;
868 } else if (key
.offset
<= start
&& extent_end
> start
)
869 *length
+= extent_end
- start
;
870 else if (key
.offset
> start
&& extent_end
<= end
)
871 *length
+= extent_end
- key
.offset
;
872 else if (key
.offset
> start
&& key
.offset
<= end
) {
873 *length
+= end
- key
.offset
+ 1;
875 } else if (key
.offset
> end
)
883 btrfs_free_path(path
);
888 * find_free_dev_extent - find free space in the specified device
889 * @device: the device which we search the free space in
890 * @num_bytes: the size of the free space that we need
891 * @start: store the start of the free space.
892 * @len: the size of the free space. that we find, or the size of the max
893 * free space if we don't find suitable free space
895 * this uses a pretty simple search, the expectation is that it is
896 * called very infrequently and that a given device has a small number
899 * @start is used to store the start of the free space if we find. But if we
900 * don't find suitable free space, it will be used to store the start position
901 * of the max free space.
903 * @len is used to store the size of the free space that we find.
904 * But if we don't find suitable free space, it is used to store the size of
905 * the max free space.
907 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
908 u64
*start
, u64
*len
)
910 struct btrfs_key key
;
911 struct btrfs_root
*root
= device
->dev_root
;
912 struct btrfs_dev_extent
*dev_extent
;
913 struct btrfs_path
*path
;
919 u64 search_end
= device
->total_bytes
;
922 struct extent_buffer
*l
;
924 /* FIXME use last free of some kind */
926 /* we don't want to overwrite the superblock on the drive,
927 * so we make sure to start at an offset of at least 1MB
929 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
931 max_hole_start
= search_start
;
935 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
940 path
= btrfs_alloc_path();
947 key
.objectid
= device
->devid
;
948 key
.offset
= search_start
;
949 key
.type
= BTRFS_DEV_EXTENT_KEY
;
951 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
955 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
962 slot
= path
->slots
[0];
963 if (slot
>= btrfs_header_nritems(l
)) {
964 ret
= btrfs_next_leaf(root
, path
);
972 btrfs_item_key_to_cpu(l
, &key
, slot
);
974 if (key
.objectid
< device
->devid
)
977 if (key
.objectid
> device
->devid
)
980 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
983 if (key
.offset
> search_start
) {
984 hole_size
= key
.offset
- search_start
;
986 if (hole_size
> max_hole_size
) {
987 max_hole_start
= search_start
;
988 max_hole_size
= hole_size
;
992 * If this free space is greater than which we need,
993 * it must be the max free space that we have found
994 * until now, so max_hole_start must point to the start
995 * of this free space and the length of this free space
996 * is stored in max_hole_size. Thus, we return
997 * max_hole_start and max_hole_size and go back to the
1000 if (hole_size
>= num_bytes
) {
1006 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1007 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1009 if (extent_end
> search_start
)
1010 search_start
= extent_end
;
1017 * At this point, search_start should be the end of
1018 * allocated dev extents, and when shrinking the device,
1019 * search_end may be smaller than search_start.
1021 if (search_end
> search_start
)
1022 hole_size
= search_end
- search_start
;
1024 if (hole_size
> max_hole_size
) {
1025 max_hole_start
= search_start
;
1026 max_hole_size
= hole_size
;
1030 if (hole_size
< num_bytes
)
1036 btrfs_free_path(path
);
1038 *start
= max_hole_start
;
1040 *len
= max_hole_size
;
1044 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1045 struct btrfs_device
*device
,
1049 struct btrfs_path
*path
;
1050 struct btrfs_root
*root
= device
->dev_root
;
1051 struct btrfs_key key
;
1052 struct btrfs_key found_key
;
1053 struct extent_buffer
*leaf
= NULL
;
1054 struct btrfs_dev_extent
*extent
= NULL
;
1056 path
= btrfs_alloc_path();
1060 key
.objectid
= device
->devid
;
1062 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1064 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1066 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1067 BTRFS_DEV_EXTENT_KEY
);
1070 leaf
= path
->nodes
[0];
1071 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1072 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1073 struct btrfs_dev_extent
);
1074 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1075 btrfs_dev_extent_length(leaf
, extent
) < start
);
1077 btrfs_release_path(path
);
1079 } else if (ret
== 0) {
1080 leaf
= path
->nodes
[0];
1081 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1082 struct btrfs_dev_extent
);
1084 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1088 if (device
->bytes_used
> 0) {
1089 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1090 device
->bytes_used
-= len
;
1091 spin_lock(&root
->fs_info
->free_chunk_lock
);
1092 root
->fs_info
->free_chunk_space
+= len
;
1093 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1095 ret
= btrfs_del_item(trans
, root
, path
);
1097 btrfs_error(root
->fs_info
, ret
,
1098 "Failed to remove dev extent item");
1101 btrfs_free_path(path
);
1105 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1106 struct btrfs_device
*device
,
1107 u64 chunk_tree
, u64 chunk_objectid
,
1108 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1111 struct btrfs_path
*path
;
1112 struct btrfs_root
*root
= device
->dev_root
;
1113 struct btrfs_dev_extent
*extent
;
1114 struct extent_buffer
*leaf
;
1115 struct btrfs_key key
;
1117 WARN_ON(!device
->in_fs_metadata
);
1118 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1119 path
= btrfs_alloc_path();
1123 key
.objectid
= device
->devid
;
1125 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1126 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1131 leaf
= path
->nodes
[0];
1132 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1133 struct btrfs_dev_extent
);
1134 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1135 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1136 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1138 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1139 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1142 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1143 btrfs_mark_buffer_dirty(leaf
);
1145 btrfs_free_path(path
);
1149 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1150 u64 objectid
, u64
*offset
)
1152 struct btrfs_path
*path
;
1154 struct btrfs_key key
;
1155 struct btrfs_chunk
*chunk
;
1156 struct btrfs_key found_key
;
1158 path
= btrfs_alloc_path();
1162 key
.objectid
= objectid
;
1163 key
.offset
= (u64
)-1;
1164 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1166 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1170 BUG_ON(ret
== 0); /* Corruption */
1172 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1176 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1178 if (found_key
.objectid
!= objectid
)
1181 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1182 struct btrfs_chunk
);
1183 *offset
= found_key
.offset
+
1184 btrfs_chunk_length(path
->nodes
[0], chunk
);
1189 btrfs_free_path(path
);
1193 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1196 struct btrfs_key key
;
1197 struct btrfs_key found_key
;
1198 struct btrfs_path
*path
;
1200 root
= root
->fs_info
->chunk_root
;
1202 path
= btrfs_alloc_path();
1206 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1207 key
.type
= BTRFS_DEV_ITEM_KEY
;
1208 key
.offset
= (u64
)-1;
1210 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1214 BUG_ON(ret
== 0); /* Corruption */
1216 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1217 BTRFS_DEV_ITEM_KEY
);
1221 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1223 *objectid
= found_key
.offset
+ 1;
1227 btrfs_free_path(path
);
1232 * the device information is stored in the chunk root
1233 * the btrfs_device struct should be fully filled in
1235 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1236 struct btrfs_root
*root
,
1237 struct btrfs_device
*device
)
1240 struct btrfs_path
*path
;
1241 struct btrfs_dev_item
*dev_item
;
1242 struct extent_buffer
*leaf
;
1243 struct btrfs_key key
;
1246 root
= root
->fs_info
->chunk_root
;
1248 path
= btrfs_alloc_path();
1252 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1253 key
.type
= BTRFS_DEV_ITEM_KEY
;
1254 key
.offset
= device
->devid
;
1256 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1261 leaf
= path
->nodes
[0];
1262 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1264 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1265 btrfs_set_device_generation(leaf
, dev_item
, 0);
1266 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1267 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1268 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1269 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1270 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1271 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1272 btrfs_set_device_group(leaf
, dev_item
, 0);
1273 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1274 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1275 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1277 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1278 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1279 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1280 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1281 btrfs_mark_buffer_dirty(leaf
);
1285 btrfs_free_path(path
);
1289 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1290 struct btrfs_device
*device
)
1293 struct btrfs_path
*path
;
1294 struct btrfs_key key
;
1295 struct btrfs_trans_handle
*trans
;
1297 root
= root
->fs_info
->chunk_root
;
1299 path
= btrfs_alloc_path();
1303 trans
= btrfs_start_transaction(root
, 0);
1304 if (IS_ERR(trans
)) {
1305 btrfs_free_path(path
);
1306 return PTR_ERR(trans
);
1308 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1309 key
.type
= BTRFS_DEV_ITEM_KEY
;
1310 key
.offset
= device
->devid
;
1313 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1322 ret
= btrfs_del_item(trans
, root
, path
);
1326 btrfs_free_path(path
);
1327 unlock_chunks(root
);
1328 btrfs_commit_transaction(trans
, root
);
1332 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1334 struct btrfs_device
*device
;
1335 struct btrfs_device
*next_device
;
1336 struct block_device
*bdev
;
1337 struct buffer_head
*bh
= NULL
;
1338 struct btrfs_super_block
*disk_super
;
1339 struct btrfs_fs_devices
*cur_devices
;
1345 bool clear_super
= false;
1347 mutex_lock(&uuid_mutex
);
1349 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1350 root
->fs_info
->avail_system_alloc_bits
|
1351 root
->fs_info
->avail_metadata_alloc_bits
;
1353 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1354 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1355 printk(KERN_ERR
"btrfs: unable to go below four devices "
1361 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1362 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1363 printk(KERN_ERR
"btrfs: unable to go below two "
1364 "devices on raid1\n");
1369 if (strcmp(device_path
, "missing") == 0) {
1370 struct list_head
*devices
;
1371 struct btrfs_device
*tmp
;
1374 devices
= &root
->fs_info
->fs_devices
->devices
;
1376 * It is safe to read the devices since the volume_mutex
1379 list_for_each_entry(tmp
, devices
, dev_list
) {
1380 if (tmp
->in_fs_metadata
&&
1381 !tmp
->is_tgtdev_for_dev_replace
&&
1391 printk(KERN_ERR
"btrfs: no missing devices found to "
1396 ret
= btrfs_get_bdev_and_sb(device_path
,
1397 FMODE_READ
| FMODE_EXCL
,
1398 root
->fs_info
->bdev_holder
, 0,
1402 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1403 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1404 dev_uuid
= disk_super
->dev_item
.uuid
;
1405 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1413 if (device
->is_tgtdev_for_dev_replace
) {
1414 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1419 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1420 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1426 if (device
->writeable
) {
1428 list_del_init(&device
->dev_alloc_list
);
1429 unlock_chunks(root
);
1430 root
->fs_info
->fs_devices
->rw_devices
--;
1434 ret
= btrfs_shrink_device(device
, 0);
1439 * TODO: the superblock still includes this device in its num_devices
1440 * counter although write_all_supers() is not locked out. This
1441 * could give a filesystem state which requires a degraded mount.
1443 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1447 spin_lock(&root
->fs_info
->free_chunk_lock
);
1448 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1450 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1452 device
->in_fs_metadata
= 0;
1453 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1456 * the device list mutex makes sure that we don't change
1457 * the device list while someone else is writing out all
1458 * the device supers.
1461 cur_devices
= device
->fs_devices
;
1462 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1463 list_del_rcu(&device
->dev_list
);
1465 device
->fs_devices
->num_devices
--;
1466 device
->fs_devices
->total_devices
--;
1468 if (device
->missing
)
1469 root
->fs_info
->fs_devices
->missing_devices
--;
1471 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1472 struct btrfs_device
, dev_list
);
1473 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1474 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1475 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1476 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1479 device
->fs_devices
->open_devices
--;
1481 call_rcu(&device
->rcu
, free_device
);
1482 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1484 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1485 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1487 if (cur_devices
->open_devices
== 0) {
1488 struct btrfs_fs_devices
*fs_devices
;
1489 fs_devices
= root
->fs_info
->fs_devices
;
1490 while (fs_devices
) {
1491 if (fs_devices
->seed
== cur_devices
)
1493 fs_devices
= fs_devices
->seed
;
1495 fs_devices
->seed
= cur_devices
->seed
;
1496 cur_devices
->seed
= NULL
;
1498 __btrfs_close_devices(cur_devices
);
1499 unlock_chunks(root
);
1500 free_fs_devices(cur_devices
);
1503 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1504 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1507 * at this point, the device is zero sized. We want to
1508 * remove it from the devices list and zero out the old super
1510 if (clear_super
&& disk_super
) {
1511 /* make sure this device isn't detected as part of
1514 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1515 set_buffer_dirty(bh
);
1516 sync_dirty_buffer(bh
);
1525 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1527 mutex_unlock(&uuid_mutex
);
1530 if (device
->writeable
) {
1532 list_add(&device
->dev_alloc_list
,
1533 &root
->fs_info
->fs_devices
->alloc_list
);
1534 unlock_chunks(root
);
1535 root
->fs_info
->fs_devices
->rw_devices
++;
1540 int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1541 struct btrfs_device
**device
)
1544 struct btrfs_super_block
*disk_super
;
1547 struct block_device
*bdev
;
1548 struct buffer_head
*bh
;
1551 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1552 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1555 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1556 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1557 dev_uuid
= disk_super
->dev_item
.uuid
;
1558 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1563 blkdev_put(bdev
, FMODE_READ
);
1567 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1569 struct btrfs_device
**device
)
1572 if (strcmp(device_path
, "missing") == 0) {
1573 struct list_head
*devices
;
1574 struct btrfs_device
*tmp
;
1576 devices
= &root
->fs_info
->fs_devices
->devices
;
1578 * It is safe to read the devices since the volume_mutex
1579 * is held by the caller.
1581 list_for_each_entry(tmp
, devices
, dev_list
) {
1582 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1589 pr_err("btrfs: no missing device found\n");
1595 return btrfs_find_device_by_path(root
, device_path
, device
);
1600 * does all the dirty work required for changing file system's UUID.
1602 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1604 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1605 struct btrfs_fs_devices
*old_devices
;
1606 struct btrfs_fs_devices
*seed_devices
;
1607 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1608 struct btrfs_device
*device
;
1611 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1612 if (!fs_devices
->seeding
)
1615 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1619 old_devices
= clone_fs_devices(fs_devices
);
1620 if (IS_ERR(old_devices
)) {
1621 kfree(seed_devices
);
1622 return PTR_ERR(old_devices
);
1625 list_add(&old_devices
->list
, &fs_uuids
);
1627 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1628 seed_devices
->opened
= 1;
1629 INIT_LIST_HEAD(&seed_devices
->devices
);
1630 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1631 mutex_init(&seed_devices
->device_list_mutex
);
1633 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1634 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1636 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1638 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1639 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1640 device
->fs_devices
= seed_devices
;
1643 fs_devices
->seeding
= 0;
1644 fs_devices
->num_devices
= 0;
1645 fs_devices
->open_devices
= 0;
1646 fs_devices
->total_devices
= 0;
1647 fs_devices
->seed
= seed_devices
;
1649 generate_random_uuid(fs_devices
->fsid
);
1650 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1651 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1652 super_flags
= btrfs_super_flags(disk_super
) &
1653 ~BTRFS_SUPER_FLAG_SEEDING
;
1654 btrfs_set_super_flags(disk_super
, super_flags
);
1660 * strore the expected generation for seed devices in device items.
1662 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1663 struct btrfs_root
*root
)
1665 struct btrfs_path
*path
;
1666 struct extent_buffer
*leaf
;
1667 struct btrfs_dev_item
*dev_item
;
1668 struct btrfs_device
*device
;
1669 struct btrfs_key key
;
1670 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1671 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1675 path
= btrfs_alloc_path();
1679 root
= root
->fs_info
->chunk_root
;
1680 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1682 key
.type
= BTRFS_DEV_ITEM_KEY
;
1685 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1689 leaf
= path
->nodes
[0];
1691 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1692 ret
= btrfs_next_leaf(root
, path
);
1697 leaf
= path
->nodes
[0];
1698 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1699 btrfs_release_path(path
);
1703 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1704 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1705 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1708 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1709 struct btrfs_dev_item
);
1710 devid
= btrfs_device_id(leaf
, dev_item
);
1711 read_extent_buffer(leaf
, dev_uuid
,
1712 (unsigned long)btrfs_device_uuid(dev_item
),
1714 read_extent_buffer(leaf
, fs_uuid
,
1715 (unsigned long)btrfs_device_fsid(dev_item
),
1717 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1719 BUG_ON(!device
); /* Logic error */
1721 if (device
->fs_devices
->seeding
) {
1722 btrfs_set_device_generation(leaf
, dev_item
,
1723 device
->generation
);
1724 btrfs_mark_buffer_dirty(leaf
);
1732 btrfs_free_path(path
);
1736 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1738 struct request_queue
*q
;
1739 struct btrfs_trans_handle
*trans
;
1740 struct btrfs_device
*device
;
1741 struct block_device
*bdev
;
1742 struct list_head
*devices
;
1743 struct super_block
*sb
= root
->fs_info
->sb
;
1744 struct rcu_string
*name
;
1746 int seeding_dev
= 0;
1749 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1752 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1753 root
->fs_info
->bdev_holder
);
1755 return PTR_ERR(bdev
);
1757 if (root
->fs_info
->fs_devices
->seeding
) {
1759 down_write(&sb
->s_umount
);
1760 mutex_lock(&uuid_mutex
);
1763 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1765 devices
= &root
->fs_info
->fs_devices
->devices
;
1767 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1768 list_for_each_entry(device
, devices
, dev_list
) {
1769 if (device
->bdev
== bdev
) {
1772 &root
->fs_info
->fs_devices
->device_list_mutex
);
1776 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1778 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1780 /* we can safely leave the fs_devices entry around */
1785 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
1791 rcu_assign_pointer(device
->name
, name
);
1793 ret
= find_next_devid(root
, &device
->devid
);
1795 rcu_string_free(device
->name
);
1800 trans
= btrfs_start_transaction(root
, 0);
1801 if (IS_ERR(trans
)) {
1802 rcu_string_free(device
->name
);
1804 ret
= PTR_ERR(trans
);
1810 q
= bdev_get_queue(bdev
);
1811 if (blk_queue_discard(q
))
1812 device
->can_discard
= 1;
1813 device
->writeable
= 1;
1814 device
->work
.func
= pending_bios_fn
;
1815 generate_random_uuid(device
->uuid
);
1816 spin_lock_init(&device
->io_lock
);
1817 device
->generation
= trans
->transid
;
1818 device
->io_width
= root
->sectorsize
;
1819 device
->io_align
= root
->sectorsize
;
1820 device
->sector_size
= root
->sectorsize
;
1821 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1822 device
->disk_total_bytes
= device
->total_bytes
;
1823 device
->dev_root
= root
->fs_info
->dev_root
;
1824 device
->bdev
= bdev
;
1825 device
->in_fs_metadata
= 1;
1826 device
->is_tgtdev_for_dev_replace
= 0;
1827 device
->mode
= FMODE_EXCL
;
1828 set_blocksize(device
->bdev
, 4096);
1831 sb
->s_flags
&= ~MS_RDONLY
;
1832 ret
= btrfs_prepare_sprout(root
);
1833 BUG_ON(ret
); /* -ENOMEM */
1836 device
->fs_devices
= root
->fs_info
->fs_devices
;
1838 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1839 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1840 list_add(&device
->dev_alloc_list
,
1841 &root
->fs_info
->fs_devices
->alloc_list
);
1842 root
->fs_info
->fs_devices
->num_devices
++;
1843 root
->fs_info
->fs_devices
->open_devices
++;
1844 root
->fs_info
->fs_devices
->rw_devices
++;
1845 root
->fs_info
->fs_devices
->total_devices
++;
1846 if (device
->can_discard
)
1847 root
->fs_info
->fs_devices
->num_can_discard
++;
1848 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1850 spin_lock(&root
->fs_info
->free_chunk_lock
);
1851 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1852 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1854 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1855 root
->fs_info
->fs_devices
->rotating
= 1;
1857 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1858 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1859 total_bytes
+ device
->total_bytes
);
1861 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1862 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1864 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1867 ret
= init_first_rw_device(trans
, root
, device
);
1869 btrfs_abort_transaction(trans
, root
, ret
);
1872 ret
= btrfs_finish_sprout(trans
, root
);
1874 btrfs_abort_transaction(trans
, root
, ret
);
1878 ret
= btrfs_add_device(trans
, root
, device
);
1880 btrfs_abort_transaction(trans
, root
, ret
);
1886 * we've got more storage, clear any full flags on the space
1889 btrfs_clear_space_info_full(root
->fs_info
);
1891 unlock_chunks(root
);
1892 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1893 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1894 ret
= btrfs_commit_transaction(trans
, root
);
1897 mutex_unlock(&uuid_mutex
);
1898 up_write(&sb
->s_umount
);
1900 if (ret
) /* transaction commit */
1903 ret
= btrfs_relocate_sys_chunks(root
);
1905 btrfs_error(root
->fs_info
, ret
,
1906 "Failed to relocate sys chunks after "
1907 "device initialization. This can be fixed "
1908 "using the \"btrfs balance\" command.");
1909 trans
= btrfs_attach_transaction(root
);
1910 if (IS_ERR(trans
)) {
1911 if (PTR_ERR(trans
) == -ENOENT
)
1913 return PTR_ERR(trans
);
1915 ret
= btrfs_commit_transaction(trans
, root
);
1921 unlock_chunks(root
);
1922 btrfs_end_transaction(trans
, root
);
1923 rcu_string_free(device
->name
);
1926 blkdev_put(bdev
, FMODE_EXCL
);
1928 mutex_unlock(&uuid_mutex
);
1929 up_write(&sb
->s_umount
);
1934 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1935 struct btrfs_device
*device
)
1938 struct btrfs_path
*path
;
1939 struct btrfs_root
*root
;
1940 struct btrfs_dev_item
*dev_item
;
1941 struct extent_buffer
*leaf
;
1942 struct btrfs_key key
;
1944 root
= device
->dev_root
->fs_info
->chunk_root
;
1946 path
= btrfs_alloc_path();
1950 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1951 key
.type
= BTRFS_DEV_ITEM_KEY
;
1952 key
.offset
= device
->devid
;
1954 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1963 leaf
= path
->nodes
[0];
1964 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1966 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1967 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1968 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1969 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1970 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1971 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1972 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1973 btrfs_mark_buffer_dirty(leaf
);
1976 btrfs_free_path(path
);
1980 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1981 struct btrfs_device
*device
, u64 new_size
)
1983 struct btrfs_super_block
*super_copy
=
1984 device
->dev_root
->fs_info
->super_copy
;
1985 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1986 u64 diff
= new_size
- device
->total_bytes
;
1988 if (!device
->writeable
)
1990 if (new_size
<= device
->total_bytes
||
1991 device
->is_tgtdev_for_dev_replace
)
1994 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1995 device
->fs_devices
->total_rw_bytes
+= diff
;
1997 device
->total_bytes
= new_size
;
1998 device
->disk_total_bytes
= new_size
;
1999 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2001 return btrfs_update_device(trans
, device
);
2004 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2005 struct btrfs_device
*device
, u64 new_size
)
2008 lock_chunks(device
->dev_root
);
2009 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2010 unlock_chunks(device
->dev_root
);
2014 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2015 struct btrfs_root
*root
,
2016 u64 chunk_tree
, u64 chunk_objectid
,
2020 struct btrfs_path
*path
;
2021 struct btrfs_key key
;
2023 root
= root
->fs_info
->chunk_root
;
2024 path
= btrfs_alloc_path();
2028 key
.objectid
= chunk_objectid
;
2029 key
.offset
= chunk_offset
;
2030 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2032 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2035 else if (ret
> 0) { /* Logic error or corruption */
2036 btrfs_error(root
->fs_info
, -ENOENT
,
2037 "Failed lookup while freeing chunk.");
2042 ret
= btrfs_del_item(trans
, root
, path
);
2044 btrfs_error(root
->fs_info
, ret
,
2045 "Failed to delete chunk item.");
2047 btrfs_free_path(path
);
2051 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2054 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2055 struct btrfs_disk_key
*disk_key
;
2056 struct btrfs_chunk
*chunk
;
2063 struct btrfs_key key
;
2065 array_size
= btrfs_super_sys_array_size(super_copy
);
2067 ptr
= super_copy
->sys_chunk_array
;
2070 while (cur
< array_size
) {
2071 disk_key
= (struct btrfs_disk_key
*)ptr
;
2072 btrfs_disk_key_to_cpu(&key
, disk_key
);
2074 len
= sizeof(*disk_key
);
2076 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2077 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2078 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2079 len
+= btrfs_chunk_item_size(num_stripes
);
2084 if (key
.objectid
== chunk_objectid
&&
2085 key
.offset
== chunk_offset
) {
2086 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2088 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2097 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2098 u64 chunk_tree
, u64 chunk_objectid
,
2101 struct extent_map_tree
*em_tree
;
2102 struct btrfs_root
*extent_root
;
2103 struct btrfs_trans_handle
*trans
;
2104 struct extent_map
*em
;
2105 struct map_lookup
*map
;
2109 root
= root
->fs_info
->chunk_root
;
2110 extent_root
= root
->fs_info
->extent_root
;
2111 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2113 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2117 /* step one, relocate all the extents inside this chunk */
2118 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2122 trans
= btrfs_start_transaction(root
, 0);
2123 BUG_ON(IS_ERR(trans
));
2128 * step two, delete the device extents and the
2129 * chunk tree entries
2131 read_lock(&em_tree
->lock
);
2132 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2133 read_unlock(&em_tree
->lock
);
2135 BUG_ON(!em
|| em
->start
> chunk_offset
||
2136 em
->start
+ em
->len
< chunk_offset
);
2137 map
= (struct map_lookup
*)em
->bdev
;
2139 for (i
= 0; i
< map
->num_stripes
; i
++) {
2140 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2141 map
->stripes
[i
].physical
);
2144 if (map
->stripes
[i
].dev
) {
2145 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2149 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2154 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2156 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2157 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2161 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2164 write_lock(&em_tree
->lock
);
2165 remove_extent_mapping(em_tree
, em
);
2166 write_unlock(&em_tree
->lock
);
2171 /* once for the tree */
2172 free_extent_map(em
);
2174 free_extent_map(em
);
2176 unlock_chunks(root
);
2177 btrfs_end_transaction(trans
, root
);
2181 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2183 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2184 struct btrfs_path
*path
;
2185 struct extent_buffer
*leaf
;
2186 struct btrfs_chunk
*chunk
;
2187 struct btrfs_key key
;
2188 struct btrfs_key found_key
;
2189 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2191 bool retried
= false;
2195 path
= btrfs_alloc_path();
2200 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2201 key
.offset
= (u64
)-1;
2202 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2205 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2208 BUG_ON(ret
== 0); /* Corruption */
2210 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2217 leaf
= path
->nodes
[0];
2218 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2220 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2221 struct btrfs_chunk
);
2222 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2223 btrfs_release_path(path
);
2225 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2226 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2235 if (found_key
.offset
== 0)
2237 key
.offset
= found_key
.offset
- 1;
2240 if (failed
&& !retried
) {
2244 } else if (failed
&& retried
) {
2249 btrfs_free_path(path
);
2253 static int insert_balance_item(struct btrfs_root
*root
,
2254 struct btrfs_balance_control
*bctl
)
2256 struct btrfs_trans_handle
*trans
;
2257 struct btrfs_balance_item
*item
;
2258 struct btrfs_disk_balance_args disk_bargs
;
2259 struct btrfs_path
*path
;
2260 struct extent_buffer
*leaf
;
2261 struct btrfs_key key
;
2264 path
= btrfs_alloc_path();
2268 trans
= btrfs_start_transaction(root
, 0);
2269 if (IS_ERR(trans
)) {
2270 btrfs_free_path(path
);
2271 return PTR_ERR(trans
);
2274 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2275 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2278 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2283 leaf
= path
->nodes
[0];
2284 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2286 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2288 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2289 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2290 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2291 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2292 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2293 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2295 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2297 btrfs_mark_buffer_dirty(leaf
);
2299 btrfs_free_path(path
);
2300 err
= btrfs_commit_transaction(trans
, root
);
2306 static int del_balance_item(struct btrfs_root
*root
)
2308 struct btrfs_trans_handle
*trans
;
2309 struct btrfs_path
*path
;
2310 struct btrfs_key key
;
2313 path
= btrfs_alloc_path();
2317 trans
= btrfs_start_transaction(root
, 0);
2318 if (IS_ERR(trans
)) {
2319 btrfs_free_path(path
);
2320 return PTR_ERR(trans
);
2323 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2324 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2327 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2335 ret
= btrfs_del_item(trans
, root
, path
);
2337 btrfs_free_path(path
);
2338 err
= btrfs_commit_transaction(trans
, root
);
2345 * This is a heuristic used to reduce the number of chunks balanced on
2346 * resume after balance was interrupted.
2348 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2351 * Turn on soft mode for chunk types that were being converted.
2353 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2354 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2355 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2356 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2357 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2358 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2361 * Turn on usage filter if is not already used. The idea is
2362 * that chunks that we have already balanced should be
2363 * reasonably full. Don't do it for chunks that are being
2364 * converted - that will keep us from relocating unconverted
2365 * (albeit full) chunks.
2367 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2368 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2369 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2370 bctl
->data
.usage
= 90;
2372 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2373 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2374 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2375 bctl
->sys
.usage
= 90;
2377 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2378 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2379 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2380 bctl
->meta
.usage
= 90;
2385 * Should be called with both balance and volume mutexes held to
2386 * serialize other volume operations (add_dev/rm_dev/resize) with
2387 * restriper. Same goes for unset_balance_control.
2389 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2391 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2393 BUG_ON(fs_info
->balance_ctl
);
2395 spin_lock(&fs_info
->balance_lock
);
2396 fs_info
->balance_ctl
= bctl
;
2397 spin_unlock(&fs_info
->balance_lock
);
2400 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2402 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2404 BUG_ON(!fs_info
->balance_ctl
);
2406 spin_lock(&fs_info
->balance_lock
);
2407 fs_info
->balance_ctl
= NULL
;
2408 spin_unlock(&fs_info
->balance_lock
);
2414 * Balance filters. Return 1 if chunk should be filtered out
2415 * (should not be balanced).
2417 static int chunk_profiles_filter(u64 chunk_type
,
2418 struct btrfs_balance_args
*bargs
)
2420 chunk_type
= chunk_to_extended(chunk_type
) &
2421 BTRFS_EXTENDED_PROFILE_MASK
;
2423 if (bargs
->profiles
& chunk_type
)
2429 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2430 struct btrfs_balance_args
*bargs
)
2432 struct btrfs_block_group_cache
*cache
;
2433 u64 chunk_used
, user_thresh
;
2436 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2437 chunk_used
= btrfs_block_group_used(&cache
->item
);
2439 user_thresh
= div_factor_fine(cache
->key
.offset
, bargs
->usage
);
2440 if (chunk_used
< user_thresh
)
2443 btrfs_put_block_group(cache
);
2447 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2448 struct btrfs_chunk
*chunk
,
2449 struct btrfs_balance_args
*bargs
)
2451 struct btrfs_stripe
*stripe
;
2452 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2455 for (i
= 0; i
< num_stripes
; i
++) {
2456 stripe
= btrfs_stripe_nr(chunk
, i
);
2457 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2464 /* [pstart, pend) */
2465 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2466 struct btrfs_chunk
*chunk
,
2468 struct btrfs_balance_args
*bargs
)
2470 struct btrfs_stripe
*stripe
;
2471 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2477 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2480 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2481 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
))
2485 factor
= num_stripes
/ factor
;
2487 for (i
= 0; i
< num_stripes
; i
++) {
2488 stripe
= btrfs_stripe_nr(chunk
, i
);
2489 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2492 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2493 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2494 do_div(stripe_length
, factor
);
2496 if (stripe_offset
< bargs
->pend
&&
2497 stripe_offset
+ stripe_length
> bargs
->pstart
)
2504 /* [vstart, vend) */
2505 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2506 struct btrfs_chunk
*chunk
,
2508 struct btrfs_balance_args
*bargs
)
2510 if (chunk_offset
< bargs
->vend
&&
2511 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2512 /* at least part of the chunk is inside this vrange */
2518 static int chunk_soft_convert_filter(u64 chunk_type
,
2519 struct btrfs_balance_args
*bargs
)
2521 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2524 chunk_type
= chunk_to_extended(chunk_type
) &
2525 BTRFS_EXTENDED_PROFILE_MASK
;
2527 if (bargs
->target
== chunk_type
)
2533 static int should_balance_chunk(struct btrfs_root
*root
,
2534 struct extent_buffer
*leaf
,
2535 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2537 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2538 struct btrfs_balance_args
*bargs
= NULL
;
2539 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2542 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2543 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2547 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2548 bargs
= &bctl
->data
;
2549 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2551 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2552 bargs
= &bctl
->meta
;
2554 /* profiles filter */
2555 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2556 chunk_profiles_filter(chunk_type
, bargs
)) {
2561 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2562 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2567 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2568 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2572 /* drange filter, makes sense only with devid filter */
2573 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2574 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2579 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2580 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2584 /* soft profile changing mode */
2585 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2586 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2593 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2595 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2596 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2597 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2598 struct list_head
*devices
;
2599 struct btrfs_device
*device
;
2602 struct btrfs_chunk
*chunk
;
2603 struct btrfs_path
*path
;
2604 struct btrfs_key key
;
2605 struct btrfs_key found_key
;
2606 struct btrfs_trans_handle
*trans
;
2607 struct extent_buffer
*leaf
;
2610 int enospc_errors
= 0;
2611 bool counting
= true;
2613 /* step one make some room on all the devices */
2614 devices
= &fs_info
->fs_devices
->devices
;
2615 list_for_each_entry(device
, devices
, dev_list
) {
2616 old_size
= device
->total_bytes
;
2617 size_to_free
= div_factor(old_size
, 1);
2618 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2619 if (!device
->writeable
||
2620 device
->total_bytes
- device
->bytes_used
> size_to_free
||
2621 device
->is_tgtdev_for_dev_replace
)
2624 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2629 trans
= btrfs_start_transaction(dev_root
, 0);
2630 BUG_ON(IS_ERR(trans
));
2632 ret
= btrfs_grow_device(trans
, device
, old_size
);
2635 btrfs_end_transaction(trans
, dev_root
);
2638 /* step two, relocate all the chunks */
2639 path
= btrfs_alloc_path();
2645 /* zero out stat counters */
2646 spin_lock(&fs_info
->balance_lock
);
2647 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2648 spin_unlock(&fs_info
->balance_lock
);
2650 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2651 key
.offset
= (u64
)-1;
2652 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2655 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2656 atomic_read(&fs_info
->balance_cancel_req
)) {
2661 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2666 * this shouldn't happen, it means the last relocate
2670 BUG(); /* FIXME break ? */
2672 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2673 BTRFS_CHUNK_ITEM_KEY
);
2679 leaf
= path
->nodes
[0];
2680 slot
= path
->slots
[0];
2681 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2683 if (found_key
.objectid
!= key
.objectid
)
2686 /* chunk zero is special */
2687 if (found_key
.offset
== 0)
2690 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2693 spin_lock(&fs_info
->balance_lock
);
2694 bctl
->stat
.considered
++;
2695 spin_unlock(&fs_info
->balance_lock
);
2698 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2700 btrfs_release_path(path
);
2705 spin_lock(&fs_info
->balance_lock
);
2706 bctl
->stat
.expected
++;
2707 spin_unlock(&fs_info
->balance_lock
);
2711 ret
= btrfs_relocate_chunk(chunk_root
,
2712 chunk_root
->root_key
.objectid
,
2715 if (ret
&& ret
!= -ENOSPC
)
2717 if (ret
== -ENOSPC
) {
2720 spin_lock(&fs_info
->balance_lock
);
2721 bctl
->stat
.completed
++;
2722 spin_unlock(&fs_info
->balance_lock
);
2725 key
.offset
= found_key
.offset
- 1;
2729 btrfs_release_path(path
);
2734 btrfs_free_path(path
);
2735 if (enospc_errors
) {
2736 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2746 * alloc_profile_is_valid - see if a given profile is valid and reduced
2747 * @flags: profile to validate
2748 * @extended: if true @flags is treated as an extended profile
2750 static int alloc_profile_is_valid(u64 flags
, int extended
)
2752 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
2753 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
2755 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
2757 /* 1) check that all other bits are zeroed */
2761 /* 2) see if profile is reduced */
2763 return !extended
; /* "0" is valid for usual profiles */
2765 /* true if exactly one bit set */
2766 return (flags
& (flags
- 1)) == 0;
2769 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
2771 /* cancel requested || normal exit path */
2772 return atomic_read(&fs_info
->balance_cancel_req
) ||
2773 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
2774 atomic_read(&fs_info
->balance_cancel_req
) == 0);
2777 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
2781 unset_balance_control(fs_info
);
2782 ret
= del_balance_item(fs_info
->tree_root
);
2786 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
2787 struct btrfs_ioctl_balance_args
*bargs
);
2790 * Should be called with both balance and volume mutexes held
2792 int btrfs_balance(struct btrfs_balance_control
*bctl
,
2793 struct btrfs_ioctl_balance_args
*bargs
)
2795 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2800 if (btrfs_fs_closing(fs_info
) ||
2801 atomic_read(&fs_info
->balance_pause_req
) ||
2802 atomic_read(&fs_info
->balance_cancel_req
)) {
2807 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
2808 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
2812 * In case of mixed groups both data and meta should be picked,
2813 * and identical options should be given for both of them.
2815 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
2816 if (mixed
&& (bctl
->flags
& allowed
)) {
2817 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
2818 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
2819 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
2820 printk(KERN_ERR
"btrfs: with mixed groups data and "
2821 "metadata balance options must be the same\n");
2827 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2828 if (fs_info
->fs_devices
->num_devices
== 1)
2829 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
2830 else if (fs_info
->fs_devices
->num_devices
< 4)
2831 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
2833 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2834 BTRFS_BLOCK_GROUP_RAID10
);
2836 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2837 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
2838 (bctl
->data
.target
& ~allowed
))) {
2839 printk(KERN_ERR
"btrfs: unable to start balance with target "
2840 "data profile %llu\n",
2841 (unsigned long long)bctl
->data
.target
);
2845 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2846 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
2847 (bctl
->meta
.target
& ~allowed
))) {
2848 printk(KERN_ERR
"btrfs: unable to start balance with target "
2849 "metadata profile %llu\n",
2850 (unsigned long long)bctl
->meta
.target
);
2854 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2855 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
2856 (bctl
->sys
.target
& ~allowed
))) {
2857 printk(KERN_ERR
"btrfs: unable to start balance with target "
2858 "system profile %llu\n",
2859 (unsigned long long)bctl
->sys
.target
);
2864 /* allow dup'ed data chunks only in mixed mode */
2865 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2866 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
2867 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
2872 /* allow to reduce meta or sys integrity only if force set */
2873 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2874 BTRFS_BLOCK_GROUP_RAID10
;
2875 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2876 (fs_info
->avail_system_alloc_bits
& allowed
) &&
2877 !(bctl
->sys
.target
& allowed
)) ||
2878 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2879 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
2880 !(bctl
->meta
.target
& allowed
))) {
2881 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
2882 printk(KERN_INFO
"btrfs: force reducing metadata "
2885 printk(KERN_ERR
"btrfs: balance will reduce metadata "
2886 "integrity, use force if you want this\n");
2892 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
2893 int num_tolerated_disk_barrier_failures
;
2894 u64 target
= bctl
->sys
.target
;
2896 num_tolerated_disk_barrier_failures
=
2897 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
2898 if (num_tolerated_disk_barrier_failures
> 0 &&
2900 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
2901 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
2902 num_tolerated_disk_barrier_failures
= 0;
2903 else if (num_tolerated_disk_barrier_failures
> 1 &&
2905 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
2906 num_tolerated_disk_barrier_failures
= 1;
2908 fs_info
->num_tolerated_disk_barrier_failures
=
2909 num_tolerated_disk_barrier_failures
;
2912 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
2913 if (ret
&& ret
!= -EEXIST
)
2916 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
2917 BUG_ON(ret
== -EEXIST
);
2918 set_balance_control(bctl
);
2920 BUG_ON(ret
!= -EEXIST
);
2921 spin_lock(&fs_info
->balance_lock
);
2922 update_balance_args(bctl
);
2923 spin_unlock(&fs_info
->balance_lock
);
2926 atomic_inc(&fs_info
->balance_running
);
2927 mutex_unlock(&fs_info
->balance_mutex
);
2929 ret
= __btrfs_balance(fs_info
);
2931 mutex_lock(&fs_info
->balance_mutex
);
2932 atomic_dec(&fs_info
->balance_running
);
2935 memset(bargs
, 0, sizeof(*bargs
));
2936 update_ioctl_balance_args(fs_info
, 0, bargs
);
2939 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
2940 balance_need_close(fs_info
)) {
2941 __cancel_balance(fs_info
);
2944 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
2945 fs_info
->num_tolerated_disk_barrier_failures
=
2946 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
2949 wake_up(&fs_info
->balance_wait_q
);
2953 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
2954 __cancel_balance(fs_info
);
2960 static int balance_kthread(void *data
)
2962 struct btrfs_fs_info
*fs_info
= data
;
2965 mutex_lock(&fs_info
->volume_mutex
);
2966 mutex_lock(&fs_info
->balance_mutex
);
2968 if (fs_info
->balance_ctl
) {
2969 printk(KERN_INFO
"btrfs: continuing balance\n");
2970 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
2973 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
2974 mutex_unlock(&fs_info
->balance_mutex
);
2975 mutex_unlock(&fs_info
->volume_mutex
);
2980 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
2982 struct task_struct
*tsk
;
2984 spin_lock(&fs_info
->balance_lock
);
2985 if (!fs_info
->balance_ctl
) {
2986 spin_unlock(&fs_info
->balance_lock
);
2989 spin_unlock(&fs_info
->balance_lock
);
2991 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
2992 printk(KERN_INFO
"btrfs: force skipping balance\n");
2996 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
2997 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
2999 return PTR_ERR(tsk
);
3004 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3006 struct btrfs_balance_control
*bctl
;
3007 struct btrfs_balance_item
*item
;
3008 struct btrfs_disk_balance_args disk_bargs
;
3009 struct btrfs_path
*path
;
3010 struct extent_buffer
*leaf
;
3011 struct btrfs_key key
;
3014 path
= btrfs_alloc_path();
3018 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3019 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3022 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3025 if (ret
> 0) { /* ret = -ENOENT; */
3030 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3036 leaf
= path
->nodes
[0];
3037 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3039 bctl
->fs_info
= fs_info
;
3040 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3041 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3043 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3044 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3045 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3046 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3047 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3048 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3050 mutex_lock(&fs_info
->volume_mutex
);
3051 mutex_lock(&fs_info
->balance_mutex
);
3053 set_balance_control(bctl
);
3055 mutex_unlock(&fs_info
->balance_mutex
);
3056 mutex_unlock(&fs_info
->volume_mutex
);
3058 btrfs_free_path(path
);
3062 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3066 mutex_lock(&fs_info
->balance_mutex
);
3067 if (!fs_info
->balance_ctl
) {
3068 mutex_unlock(&fs_info
->balance_mutex
);
3072 if (atomic_read(&fs_info
->balance_running
)) {
3073 atomic_inc(&fs_info
->balance_pause_req
);
3074 mutex_unlock(&fs_info
->balance_mutex
);
3076 wait_event(fs_info
->balance_wait_q
,
3077 atomic_read(&fs_info
->balance_running
) == 0);
3079 mutex_lock(&fs_info
->balance_mutex
);
3080 /* we are good with balance_ctl ripped off from under us */
3081 BUG_ON(atomic_read(&fs_info
->balance_running
));
3082 atomic_dec(&fs_info
->balance_pause_req
);
3087 mutex_unlock(&fs_info
->balance_mutex
);
3091 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3093 mutex_lock(&fs_info
->balance_mutex
);
3094 if (!fs_info
->balance_ctl
) {
3095 mutex_unlock(&fs_info
->balance_mutex
);
3099 atomic_inc(&fs_info
->balance_cancel_req
);
3101 * if we are running just wait and return, balance item is
3102 * deleted in btrfs_balance in this case
3104 if (atomic_read(&fs_info
->balance_running
)) {
3105 mutex_unlock(&fs_info
->balance_mutex
);
3106 wait_event(fs_info
->balance_wait_q
,
3107 atomic_read(&fs_info
->balance_running
) == 0);
3108 mutex_lock(&fs_info
->balance_mutex
);
3110 /* __cancel_balance needs volume_mutex */
3111 mutex_unlock(&fs_info
->balance_mutex
);
3112 mutex_lock(&fs_info
->volume_mutex
);
3113 mutex_lock(&fs_info
->balance_mutex
);
3115 if (fs_info
->balance_ctl
)
3116 __cancel_balance(fs_info
);
3118 mutex_unlock(&fs_info
->volume_mutex
);
3121 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3122 atomic_dec(&fs_info
->balance_cancel_req
);
3123 mutex_unlock(&fs_info
->balance_mutex
);
3128 * shrinking a device means finding all of the device extents past
3129 * the new size, and then following the back refs to the chunks.
3130 * The chunk relocation code actually frees the device extent
3132 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3134 struct btrfs_trans_handle
*trans
;
3135 struct btrfs_root
*root
= device
->dev_root
;
3136 struct btrfs_dev_extent
*dev_extent
= NULL
;
3137 struct btrfs_path
*path
;
3145 bool retried
= false;
3146 struct extent_buffer
*l
;
3147 struct btrfs_key key
;
3148 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3149 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3150 u64 old_size
= device
->total_bytes
;
3151 u64 diff
= device
->total_bytes
- new_size
;
3153 if (device
->is_tgtdev_for_dev_replace
)
3156 path
= btrfs_alloc_path();
3164 device
->total_bytes
= new_size
;
3165 if (device
->writeable
) {
3166 device
->fs_devices
->total_rw_bytes
-= diff
;
3167 spin_lock(&root
->fs_info
->free_chunk_lock
);
3168 root
->fs_info
->free_chunk_space
-= diff
;
3169 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3171 unlock_chunks(root
);
3174 key
.objectid
= device
->devid
;
3175 key
.offset
= (u64
)-1;
3176 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3179 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3183 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3188 btrfs_release_path(path
);
3193 slot
= path
->slots
[0];
3194 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3196 if (key
.objectid
!= device
->devid
) {
3197 btrfs_release_path(path
);
3201 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3202 length
= btrfs_dev_extent_length(l
, dev_extent
);
3204 if (key
.offset
+ length
<= new_size
) {
3205 btrfs_release_path(path
);
3209 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3210 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3211 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3212 btrfs_release_path(path
);
3214 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3216 if (ret
&& ret
!= -ENOSPC
)
3220 } while (key
.offset
-- > 0);
3222 if (failed
&& !retried
) {
3226 } else if (failed
&& retried
) {
3230 device
->total_bytes
= old_size
;
3231 if (device
->writeable
)
3232 device
->fs_devices
->total_rw_bytes
+= diff
;
3233 spin_lock(&root
->fs_info
->free_chunk_lock
);
3234 root
->fs_info
->free_chunk_space
+= diff
;
3235 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3236 unlock_chunks(root
);
3240 /* Shrinking succeeded, else we would be at "done". */
3241 trans
= btrfs_start_transaction(root
, 0);
3242 if (IS_ERR(trans
)) {
3243 ret
= PTR_ERR(trans
);
3249 device
->disk_total_bytes
= new_size
;
3250 /* Now btrfs_update_device() will change the on-disk size. */
3251 ret
= btrfs_update_device(trans
, device
);
3253 unlock_chunks(root
);
3254 btrfs_end_transaction(trans
, root
);
3257 WARN_ON(diff
> old_total
);
3258 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3259 unlock_chunks(root
);
3260 btrfs_end_transaction(trans
, root
);
3262 btrfs_free_path(path
);
3266 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3267 struct btrfs_key
*key
,
3268 struct btrfs_chunk
*chunk
, int item_size
)
3270 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3271 struct btrfs_disk_key disk_key
;
3275 array_size
= btrfs_super_sys_array_size(super_copy
);
3276 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3279 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3280 btrfs_cpu_key_to_disk(&disk_key
, key
);
3281 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3282 ptr
+= sizeof(disk_key
);
3283 memcpy(ptr
, chunk
, item_size
);
3284 item_size
+= sizeof(disk_key
);
3285 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3290 * sort the devices in descending order by max_avail, total_avail
3292 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3294 const struct btrfs_device_info
*di_a
= a
;
3295 const struct btrfs_device_info
*di_b
= b
;
3297 if (di_a
->max_avail
> di_b
->max_avail
)
3299 if (di_a
->max_avail
< di_b
->max_avail
)
3301 if (di_a
->total_avail
> di_b
->total_avail
)
3303 if (di_a
->total_avail
< di_b
->total_avail
)
3308 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3309 struct btrfs_root
*extent_root
,
3310 struct map_lookup
**map_ret
,
3311 u64
*num_bytes_out
, u64
*stripe_size_out
,
3312 u64 start
, u64 type
)
3314 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3315 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3316 struct list_head
*cur
;
3317 struct map_lookup
*map
= NULL
;
3318 struct extent_map_tree
*em_tree
;
3319 struct extent_map
*em
;
3320 struct btrfs_device_info
*devices_info
= NULL
;
3322 int num_stripes
; /* total number of stripes to allocate */
3323 int sub_stripes
; /* sub_stripes info for map */
3324 int dev_stripes
; /* stripes per dev */
3325 int devs_max
; /* max devs to use */
3326 int devs_min
; /* min devs needed */
3327 int devs_increment
; /* ndevs has to be a multiple of this */
3328 int ncopies
; /* how many copies to data has */
3330 u64 max_stripe_size
;
3338 BUG_ON(!alloc_profile_is_valid(type
, 0));
3340 if (list_empty(&fs_devices
->alloc_list
))
3347 devs_max
= 0; /* 0 == as many as possible */
3351 * define the properties of each RAID type.
3352 * FIXME: move this to a global table and use it in all RAID
3355 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
3359 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
3361 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
3366 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
3375 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3376 max_stripe_size
= 1024 * 1024 * 1024;
3377 max_chunk_size
= 10 * max_stripe_size
;
3378 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3379 /* for larger filesystems, use larger metadata chunks */
3380 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3381 max_stripe_size
= 1024 * 1024 * 1024;
3383 max_stripe_size
= 256 * 1024 * 1024;
3384 max_chunk_size
= max_stripe_size
;
3385 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3386 max_stripe_size
= 32 * 1024 * 1024;
3387 max_chunk_size
= 2 * max_stripe_size
;
3389 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3394 /* we don't want a chunk larger than 10% of writeable space */
3395 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3398 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3403 cur
= fs_devices
->alloc_list
.next
;
3406 * in the first pass through the devices list, we gather information
3407 * about the available holes on each device.
3410 while (cur
!= &fs_devices
->alloc_list
) {
3411 struct btrfs_device
*device
;
3415 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3419 if (!device
->writeable
) {
3421 "btrfs: read-only device in alloc_list\n");
3425 if (!device
->in_fs_metadata
||
3426 device
->is_tgtdev_for_dev_replace
)
3429 if (device
->total_bytes
> device
->bytes_used
)
3430 total_avail
= device
->total_bytes
- device
->bytes_used
;
3434 /* If there is no space on this device, skip it. */
3435 if (total_avail
== 0)
3438 ret
= find_free_dev_extent(device
,
3439 max_stripe_size
* dev_stripes
,
3440 &dev_offset
, &max_avail
);
3441 if (ret
&& ret
!= -ENOSPC
)
3445 max_avail
= max_stripe_size
* dev_stripes
;
3447 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3450 devices_info
[ndevs
].dev_offset
= dev_offset
;
3451 devices_info
[ndevs
].max_avail
= max_avail
;
3452 devices_info
[ndevs
].total_avail
= total_avail
;
3453 devices_info
[ndevs
].dev
= device
;
3458 * now sort the devices by hole size / available space
3460 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3461 btrfs_cmp_device_info
, NULL
);
3463 /* round down to number of usable stripes */
3464 ndevs
-= ndevs
% devs_increment
;
3466 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3471 if (devs_max
&& ndevs
> devs_max
)
3474 * the primary goal is to maximize the number of stripes, so use as many
3475 * devices as possible, even if the stripes are not maximum sized.
3477 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3478 num_stripes
= ndevs
* dev_stripes
;
3480 if (stripe_size
* ndevs
> max_chunk_size
* ncopies
) {
3481 stripe_size
= max_chunk_size
* ncopies
;
3482 do_div(stripe_size
, ndevs
);
3485 do_div(stripe_size
, dev_stripes
);
3487 /* align to BTRFS_STRIPE_LEN */
3488 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
3489 stripe_size
*= BTRFS_STRIPE_LEN
;
3491 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3496 map
->num_stripes
= num_stripes
;
3498 for (i
= 0; i
< ndevs
; ++i
) {
3499 for (j
= 0; j
< dev_stripes
; ++j
) {
3500 int s
= i
* dev_stripes
+ j
;
3501 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3502 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3506 map
->sector_size
= extent_root
->sectorsize
;
3507 map
->stripe_len
= BTRFS_STRIPE_LEN
;
3508 map
->io_align
= BTRFS_STRIPE_LEN
;
3509 map
->io_width
= BTRFS_STRIPE_LEN
;
3511 map
->sub_stripes
= sub_stripes
;
3514 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
3516 *stripe_size_out
= stripe_size
;
3517 *num_bytes_out
= num_bytes
;
3519 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3521 em
= alloc_extent_map();
3526 em
->bdev
= (struct block_device
*)map
;
3528 em
->len
= num_bytes
;
3529 em
->block_start
= 0;
3530 em
->block_len
= em
->len
;
3532 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3533 write_lock(&em_tree
->lock
);
3534 ret
= add_extent_mapping(em_tree
, em
);
3535 write_unlock(&em_tree
->lock
);
3536 free_extent_map(em
);
3540 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3541 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3546 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3547 struct btrfs_device
*device
;
3550 device
= map
->stripes
[i
].dev
;
3551 dev_offset
= map
->stripes
[i
].physical
;
3553 ret
= btrfs_alloc_dev_extent(trans
, device
,
3554 info
->chunk_root
->root_key
.objectid
,
3555 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3556 start
, dev_offset
, stripe_size
);
3558 btrfs_abort_transaction(trans
, extent_root
, ret
);
3563 kfree(devices_info
);
3568 kfree(devices_info
);
3572 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3573 struct btrfs_root
*extent_root
,
3574 struct map_lookup
*map
, u64 chunk_offset
,
3575 u64 chunk_size
, u64 stripe_size
)
3578 struct btrfs_key key
;
3579 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3580 struct btrfs_device
*device
;
3581 struct btrfs_chunk
*chunk
;
3582 struct btrfs_stripe
*stripe
;
3583 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3587 chunk
= kzalloc(item_size
, GFP_NOFS
);
3592 while (index
< map
->num_stripes
) {
3593 device
= map
->stripes
[index
].dev
;
3594 device
->bytes_used
+= stripe_size
;
3595 ret
= btrfs_update_device(trans
, device
);
3601 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3602 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3604 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3607 stripe
= &chunk
->stripe
;
3608 while (index
< map
->num_stripes
) {
3609 device
= map
->stripes
[index
].dev
;
3610 dev_offset
= map
->stripes
[index
].physical
;
3612 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3613 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3614 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3619 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3620 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3621 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3622 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3623 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3624 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3625 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3626 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3627 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3629 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3630 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3631 key
.offset
= chunk_offset
;
3633 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3635 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3637 * TODO: Cleanup of inserted chunk root in case of
3640 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
3650 * Chunk allocation falls into two parts. The first part does works
3651 * that make the new allocated chunk useable, but not do any operation
3652 * that modifies the chunk tree. The second part does the works that
3653 * require modifying the chunk tree. This division is important for the
3654 * bootstrap process of adding storage to a seed btrfs.
3656 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3657 struct btrfs_root
*extent_root
, u64 type
)
3662 struct map_lookup
*map
;
3663 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3666 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3671 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3672 &stripe_size
, chunk_offset
, type
);
3676 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3677 chunk_size
, stripe_size
);
3683 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3684 struct btrfs_root
*root
,
3685 struct btrfs_device
*device
)
3688 u64 sys_chunk_offset
;
3692 u64 sys_stripe_size
;
3694 struct map_lookup
*map
;
3695 struct map_lookup
*sys_map
;
3696 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3697 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3700 ret
= find_next_chunk(fs_info
->chunk_root
,
3701 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3705 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3706 fs_info
->avail_metadata_alloc_bits
;
3707 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3709 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3710 &stripe_size
, chunk_offset
, alloc_profile
);
3714 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3716 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3717 fs_info
->avail_system_alloc_bits
;
3718 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3720 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3721 &sys_chunk_size
, &sys_stripe_size
,
3722 sys_chunk_offset
, alloc_profile
);
3724 btrfs_abort_transaction(trans
, root
, ret
);
3728 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3730 btrfs_abort_transaction(trans
, root
, ret
);
3735 * Modifying chunk tree needs allocating new blocks from both
3736 * system block group and metadata block group. So we only can
3737 * do operations require modifying the chunk tree after both
3738 * block groups were created.
3740 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3741 chunk_size
, stripe_size
);
3743 btrfs_abort_transaction(trans
, root
, ret
);
3747 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
3748 sys_chunk_offset
, sys_chunk_size
,
3751 btrfs_abort_transaction(trans
, root
, ret
);
3758 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
3760 struct extent_map
*em
;
3761 struct map_lookup
*map
;
3762 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3766 read_lock(&map_tree
->map_tree
.lock
);
3767 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3768 read_unlock(&map_tree
->map_tree
.lock
);
3772 if (btrfs_test_opt(root
, DEGRADED
)) {
3773 free_extent_map(em
);
3777 map
= (struct map_lookup
*)em
->bdev
;
3778 for (i
= 0; i
< map
->num_stripes
; i
++) {
3779 if (!map
->stripes
[i
].dev
->writeable
) {
3784 free_extent_map(em
);
3788 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
3790 extent_map_tree_init(&tree
->map_tree
);
3793 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
3795 struct extent_map
*em
;
3798 write_lock(&tree
->map_tree
.lock
);
3799 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
3801 remove_extent_mapping(&tree
->map_tree
, em
);
3802 write_unlock(&tree
->map_tree
.lock
);
3807 free_extent_map(em
);
3808 /* once for the tree */
3809 free_extent_map(em
);
3813 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
3815 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
3816 struct extent_map
*em
;
3817 struct map_lookup
*map
;
3818 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3821 read_lock(&em_tree
->lock
);
3822 em
= lookup_extent_mapping(em_tree
, logical
, len
);
3823 read_unlock(&em_tree
->lock
);
3826 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3827 map
= (struct map_lookup
*)em
->bdev
;
3828 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
3829 ret
= map
->num_stripes
;
3830 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3831 ret
= map
->sub_stripes
;
3834 free_extent_map(em
);
3838 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
3842 if (map
->stripes
[optimal
].dev
->bdev
)
3844 for (i
= first
; i
< first
+ num
; i
++) {
3845 if (map
->stripes
[i
].dev
->bdev
)
3848 /* we couldn't find one that doesn't fail. Just return something
3849 * and the io error handling code will clean up eventually
3854 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
3855 u64 logical
, u64
*length
,
3856 struct btrfs_bio
**bbio_ret
,
3859 struct extent_map
*em
;
3860 struct map_lookup
*map
;
3861 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
3862 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3865 u64 stripe_end_offset
;
3874 struct btrfs_bio
*bbio
= NULL
;
3876 read_lock(&em_tree
->lock
);
3877 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
3878 read_unlock(&em_tree
->lock
);
3881 printk(KERN_CRIT
"btrfs: unable to find logical %llu len %llu\n",
3882 (unsigned long long)logical
,
3883 (unsigned long long)*length
);
3887 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3888 map
= (struct map_lookup
*)em
->bdev
;
3889 offset
= logical
- em
->start
;
3891 if (mirror_num
> map
->num_stripes
)
3896 * stripe_nr counts the total number of stripes we have to stride
3897 * to get to this block
3899 do_div(stripe_nr
, map
->stripe_len
);
3901 stripe_offset
= stripe_nr
* map
->stripe_len
;
3902 BUG_ON(offset
< stripe_offset
);
3904 /* stripe_offset is the offset of this block in its stripe*/
3905 stripe_offset
= offset
- stripe_offset
;
3907 if (rw
& REQ_DISCARD
)
3908 *length
= min_t(u64
, em
->len
- offset
, *length
);
3909 else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
3910 /* we limit the length of each bio to what fits in a stripe */
3911 *length
= min_t(u64
, em
->len
- offset
,
3912 map
->stripe_len
- stripe_offset
);
3914 *length
= em
->len
- offset
;
3922 stripe_nr_orig
= stripe_nr
;
3923 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
3924 (~(map
->stripe_len
- 1));
3925 do_div(stripe_nr_end
, map
->stripe_len
);
3926 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
3928 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3929 if (rw
& REQ_DISCARD
)
3930 num_stripes
= min_t(u64
, map
->num_stripes
,
3931 stripe_nr_end
- stripe_nr_orig
);
3932 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3933 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3934 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3935 num_stripes
= map
->num_stripes
;
3936 else if (mirror_num
)
3937 stripe_index
= mirror_num
- 1;
3939 stripe_index
= find_live_mirror(map
, 0,
3941 current
->pid
% map
->num_stripes
);
3942 mirror_num
= stripe_index
+ 1;
3945 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3946 if (rw
& (REQ_WRITE
| REQ_DISCARD
)) {
3947 num_stripes
= map
->num_stripes
;
3948 } else if (mirror_num
) {
3949 stripe_index
= mirror_num
- 1;
3954 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3955 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3957 stripe_index
= do_div(stripe_nr
, factor
);
3958 stripe_index
*= map
->sub_stripes
;
3961 num_stripes
= map
->sub_stripes
;
3962 else if (rw
& REQ_DISCARD
)
3963 num_stripes
= min_t(u64
, map
->sub_stripes
*
3964 (stripe_nr_end
- stripe_nr_orig
),
3966 else if (mirror_num
)
3967 stripe_index
+= mirror_num
- 1;
3969 int old_stripe_index
= stripe_index
;
3970 stripe_index
= find_live_mirror(map
, stripe_index
,
3971 map
->sub_stripes
, stripe_index
+
3972 current
->pid
% map
->sub_stripes
);
3973 mirror_num
= stripe_index
- old_stripe_index
+ 1;
3977 * after this do_div call, stripe_nr is the number of stripes
3978 * on this device we have to walk to find the data, and
3979 * stripe_index is the number of our device in the stripe array
3981 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3982 mirror_num
= stripe_index
+ 1;
3984 BUG_ON(stripe_index
>= map
->num_stripes
);
3986 bbio
= kzalloc(btrfs_bio_size(num_stripes
), GFP_NOFS
);
3991 atomic_set(&bbio
->error
, 0);
3993 if (rw
& REQ_DISCARD
) {
3995 int sub_stripes
= 0;
3996 u64 stripes_per_dev
= 0;
3997 u32 remaining_stripes
= 0;
3998 u32 last_stripe
= 0;
4001 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
4002 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4005 sub_stripes
= map
->sub_stripes
;
4007 factor
= map
->num_stripes
/ sub_stripes
;
4008 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
4011 &remaining_stripes
);
4012 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
4013 last_stripe
*= sub_stripes
;
4016 for (i
= 0; i
< num_stripes
; i
++) {
4017 bbio
->stripes
[i
].physical
=
4018 map
->stripes
[stripe_index
].physical
+
4019 stripe_offset
+ stripe_nr
* map
->stripe_len
;
4020 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
4022 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
4023 BTRFS_BLOCK_GROUP_RAID10
)) {
4024 bbio
->stripes
[i
].length
= stripes_per_dev
*
4027 if (i
/ sub_stripes
< remaining_stripes
)
4028 bbio
->stripes
[i
].length
+=
4032 * Special for the first stripe and
4035 * |-------|...|-------|
4039 if (i
< sub_stripes
)
4040 bbio
->stripes
[i
].length
-=
4043 if (stripe_index
>= last_stripe
&&
4044 stripe_index
<= (last_stripe
+
4046 bbio
->stripes
[i
].length
-=
4049 if (i
== sub_stripes
- 1)
4052 bbio
->stripes
[i
].length
= *length
;
4055 if (stripe_index
== map
->num_stripes
) {
4056 /* This could only happen for RAID0/10 */
4062 for (i
= 0; i
< num_stripes
; i
++) {
4063 bbio
->stripes
[i
].physical
=
4064 map
->stripes
[stripe_index
].physical
+
4066 stripe_nr
* map
->stripe_len
;
4067 bbio
->stripes
[i
].dev
=
4068 map
->stripes
[stripe_index
].dev
;
4073 if (rw
& REQ_WRITE
) {
4074 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4075 BTRFS_BLOCK_GROUP_RAID10
|
4076 BTRFS_BLOCK_GROUP_DUP
)) {
4082 bbio
->num_stripes
= num_stripes
;
4083 bbio
->max_errors
= max_errors
;
4084 bbio
->mirror_num
= mirror_num
;
4086 free_extent_map(em
);
4090 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4091 u64 logical
, u64
*length
,
4092 struct btrfs_bio
**bbio_ret
, int mirror_num
)
4094 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
4098 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
4099 u64 chunk_start
, u64 physical
, u64 devid
,
4100 u64
**logical
, int *naddrs
, int *stripe_len
)
4102 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4103 struct extent_map
*em
;
4104 struct map_lookup
*map
;
4111 read_lock(&em_tree
->lock
);
4112 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
4113 read_unlock(&em_tree
->lock
);
4115 BUG_ON(!em
|| em
->start
!= chunk_start
);
4116 map
= (struct map_lookup
*)em
->bdev
;
4119 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4120 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
4121 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4122 do_div(length
, map
->num_stripes
);
4124 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
4125 BUG_ON(!buf
); /* -ENOMEM */
4127 for (i
= 0; i
< map
->num_stripes
; i
++) {
4128 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
4130 if (map
->stripes
[i
].physical
> physical
||
4131 map
->stripes
[i
].physical
+ length
<= physical
)
4134 stripe_nr
= physical
- map
->stripes
[i
].physical
;
4135 do_div(stripe_nr
, map
->stripe_len
);
4137 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4138 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4139 do_div(stripe_nr
, map
->sub_stripes
);
4140 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4141 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4143 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
4144 WARN_ON(nr
>= map
->num_stripes
);
4145 for (j
= 0; j
< nr
; j
++) {
4146 if (buf
[j
] == bytenr
)
4150 WARN_ON(nr
>= map
->num_stripes
);
4157 *stripe_len
= map
->stripe_len
;
4159 free_extent_map(em
);
4163 static void *merge_stripe_index_into_bio_private(void *bi_private
,
4164 unsigned int stripe_index
)
4167 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4169 * The alternative solution (instead of stealing bits from the
4170 * pointer) would be to allocate an intermediate structure
4171 * that contains the old private pointer plus the stripe_index.
4173 BUG_ON((((uintptr_t)bi_private
) & 3) != 0);
4174 BUG_ON(stripe_index
> 3);
4175 return (void *)(((uintptr_t)bi_private
) | stripe_index
);
4178 static struct btrfs_bio
*extract_bbio_from_bio_private(void *bi_private
)
4180 return (struct btrfs_bio
*)(((uintptr_t)bi_private
) & ~((uintptr_t)3));
4183 static unsigned int extract_stripe_index_from_bio_private(void *bi_private
)
4185 return (unsigned int)((uintptr_t)bi_private
) & 3;
4188 static void btrfs_end_bio(struct bio
*bio
, int err
)
4190 struct btrfs_bio
*bbio
= extract_bbio_from_bio_private(bio
->bi_private
);
4191 int is_orig_bio
= 0;
4194 atomic_inc(&bbio
->error
);
4195 if (err
== -EIO
|| err
== -EREMOTEIO
) {
4196 unsigned int stripe_index
=
4197 extract_stripe_index_from_bio_private(
4199 struct btrfs_device
*dev
;
4201 BUG_ON(stripe_index
>= bbio
->num_stripes
);
4202 dev
= bbio
->stripes
[stripe_index
].dev
;
4204 if (bio
->bi_rw
& WRITE
)
4205 btrfs_dev_stat_inc(dev
,
4206 BTRFS_DEV_STAT_WRITE_ERRS
);
4208 btrfs_dev_stat_inc(dev
,
4209 BTRFS_DEV_STAT_READ_ERRS
);
4210 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
4211 btrfs_dev_stat_inc(dev
,
4212 BTRFS_DEV_STAT_FLUSH_ERRS
);
4213 btrfs_dev_stat_print_on_error(dev
);
4218 if (bio
== bbio
->orig_bio
)
4221 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4224 bio
= bbio
->orig_bio
;
4226 bio
->bi_private
= bbio
->private;
4227 bio
->bi_end_io
= bbio
->end_io
;
4228 bio
->bi_bdev
= (struct block_device
*)
4229 (unsigned long)bbio
->mirror_num
;
4230 /* only send an error to the higher layers if it is
4231 * beyond the tolerance of the multi-bio
4233 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
4237 * this bio is actually up to date, we didn't
4238 * go over the max number of errors
4240 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
4245 bio_endio(bio
, err
);
4246 } else if (!is_orig_bio
) {
4251 struct async_sched
{
4254 struct btrfs_fs_info
*info
;
4255 struct btrfs_work work
;
4259 * see run_scheduled_bios for a description of why bios are collected for
4262 * This will add one bio to the pending list for a device and make sure
4263 * the work struct is scheduled.
4265 static noinline
void schedule_bio(struct btrfs_root
*root
,
4266 struct btrfs_device
*device
,
4267 int rw
, struct bio
*bio
)
4269 int should_queue
= 1;
4270 struct btrfs_pending_bios
*pending_bios
;
4272 /* don't bother with additional async steps for reads, right now */
4273 if (!(rw
& REQ_WRITE
)) {
4275 btrfsic_submit_bio(rw
, bio
);
4281 * nr_async_bios allows us to reliably return congestion to the
4282 * higher layers. Otherwise, the async bio makes it appear we have
4283 * made progress against dirty pages when we've really just put it
4284 * on a queue for later
4286 atomic_inc(&root
->fs_info
->nr_async_bios
);
4287 WARN_ON(bio
->bi_next
);
4288 bio
->bi_next
= NULL
;
4291 spin_lock(&device
->io_lock
);
4292 if (bio
->bi_rw
& REQ_SYNC
)
4293 pending_bios
= &device
->pending_sync_bios
;
4295 pending_bios
= &device
->pending_bios
;
4297 if (pending_bios
->tail
)
4298 pending_bios
->tail
->bi_next
= bio
;
4300 pending_bios
->tail
= bio
;
4301 if (!pending_bios
->head
)
4302 pending_bios
->head
= bio
;
4303 if (device
->running_pending
)
4306 spin_unlock(&device
->io_lock
);
4309 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
4313 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
4316 struct bio_vec
*prev
;
4317 struct request_queue
*q
= bdev_get_queue(bdev
);
4318 unsigned short max_sectors
= queue_max_sectors(q
);
4319 struct bvec_merge_data bvm
= {
4321 .bi_sector
= sector
,
4322 .bi_rw
= bio
->bi_rw
,
4325 if (bio
->bi_vcnt
== 0) {
4330 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
4331 if ((bio
->bi_size
>> 9) > max_sectors
)
4334 if (!q
->merge_bvec_fn
)
4337 bvm
.bi_size
= bio
->bi_size
- prev
->bv_len
;
4338 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
4343 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
4344 struct bio
*bio
, u64 physical
, int dev_nr
,
4347 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
4349 bio
->bi_private
= bbio
;
4350 bio
->bi_private
= merge_stripe_index_into_bio_private(
4351 bio
->bi_private
, (unsigned int)dev_nr
);
4352 bio
->bi_end_io
= btrfs_end_bio
;
4353 bio
->bi_sector
= physical
>> 9;
4356 struct rcu_string
*name
;
4359 name
= rcu_dereference(dev
->name
);
4360 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
4361 "(%s id %llu), size=%u\n", rw
,
4362 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
4363 name
->str
, dev
->devid
, bio
->bi_size
);
4367 bio
->bi_bdev
= dev
->bdev
;
4369 schedule_bio(root
, dev
, rw
, bio
);
4371 btrfsic_submit_bio(rw
, bio
);
4374 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
4375 struct bio
*first_bio
, struct btrfs_device
*dev
,
4376 int dev_nr
, int rw
, int async
)
4378 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
4380 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
4381 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
4384 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
4388 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
4389 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
4390 bvec
->bv_offset
) < bvec
->bv_len
) {
4391 u64 len
= bio
->bi_size
;
4393 atomic_inc(&bbio
->stripes_pending
);
4394 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
4402 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
4406 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
4408 atomic_inc(&bbio
->error
);
4409 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4410 bio
->bi_private
= bbio
->private;
4411 bio
->bi_end_io
= bbio
->end_io
;
4412 bio
->bi_bdev
= (struct block_device
*)
4413 (unsigned long)bbio
->mirror_num
;
4414 bio
->bi_sector
= logical
>> 9;
4416 bio_endio(bio
, -EIO
);
4420 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
4421 int mirror_num
, int async_submit
)
4423 struct btrfs_device
*dev
;
4424 struct bio
*first_bio
= bio
;
4425 u64 logical
= (u64
)bio
->bi_sector
<< 9;
4431 struct btrfs_bio
*bbio
= NULL
;
4433 length
= bio
->bi_size
;
4434 map_length
= length
;
4436 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
4441 total_devs
= bbio
->num_stripes
;
4442 if (map_length
< length
) {
4443 printk(KERN_CRIT
"btrfs: mapping failed logical %llu bio len %llu "
4444 "len %llu\n", (unsigned long long)logical
,
4445 (unsigned long long)length
,
4446 (unsigned long long)map_length
);
4450 bbio
->orig_bio
= first_bio
;
4451 bbio
->private = first_bio
->bi_private
;
4452 bbio
->end_io
= first_bio
->bi_end_io
;
4453 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
4455 while (dev_nr
< total_devs
) {
4456 dev
= bbio
->stripes
[dev_nr
].dev
;
4457 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
4458 bbio_error(bbio
, first_bio
, logical
);
4464 * Check and see if we're ok with this bio based on it's size
4465 * and offset with the given device.
4467 if (!bio_size_ok(dev
->bdev
, first_bio
,
4468 bbio
->stripes
[dev_nr
].physical
>> 9)) {
4469 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
4470 dev_nr
, rw
, async_submit
);
4476 if (dev_nr
< total_devs
- 1) {
4477 bio
= bio_clone(first_bio
, GFP_NOFS
);
4478 BUG_ON(!bio
); /* -ENOMEM */
4483 submit_stripe_bio(root
, bbio
, bio
,
4484 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
4491 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
4494 struct btrfs_device
*device
;
4495 struct btrfs_fs_devices
*cur_devices
;
4497 cur_devices
= fs_info
->fs_devices
;
4498 while (cur_devices
) {
4500 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4501 device
= __find_device(&cur_devices
->devices
,
4506 cur_devices
= cur_devices
->seed
;
4511 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
4512 u64 devid
, u8
*dev_uuid
)
4514 struct btrfs_device
*device
;
4515 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4517 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
4520 list_add(&device
->dev_list
,
4521 &fs_devices
->devices
);
4522 device
->dev_root
= root
->fs_info
->dev_root
;
4523 device
->devid
= devid
;
4524 device
->work
.func
= pending_bios_fn
;
4525 device
->fs_devices
= fs_devices
;
4526 device
->missing
= 1;
4527 fs_devices
->num_devices
++;
4528 fs_devices
->missing_devices
++;
4529 spin_lock_init(&device
->io_lock
);
4530 INIT_LIST_HEAD(&device
->dev_alloc_list
);
4531 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
4535 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
4536 struct extent_buffer
*leaf
,
4537 struct btrfs_chunk
*chunk
)
4539 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4540 struct map_lookup
*map
;
4541 struct extent_map
*em
;
4545 u8 uuid
[BTRFS_UUID_SIZE
];
4550 logical
= key
->offset
;
4551 length
= btrfs_chunk_length(leaf
, chunk
);
4553 read_lock(&map_tree
->map_tree
.lock
);
4554 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
4555 read_unlock(&map_tree
->map_tree
.lock
);
4557 /* already mapped? */
4558 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
4559 free_extent_map(em
);
4562 free_extent_map(em
);
4565 em
= alloc_extent_map();
4568 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
4569 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4571 free_extent_map(em
);
4575 em
->bdev
= (struct block_device
*)map
;
4576 em
->start
= logical
;
4578 em
->block_start
= 0;
4579 em
->block_len
= em
->len
;
4581 map
->num_stripes
= num_stripes
;
4582 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
4583 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
4584 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
4585 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
4586 map
->type
= btrfs_chunk_type(leaf
, chunk
);
4587 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
4588 for (i
= 0; i
< num_stripes
; i
++) {
4589 map
->stripes
[i
].physical
=
4590 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
4591 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
4592 read_extent_buffer(leaf
, uuid
, (unsigned long)
4593 btrfs_stripe_dev_uuid_nr(chunk
, i
),
4595 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
4597 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
4599 free_extent_map(em
);
4602 if (!map
->stripes
[i
].dev
) {
4603 map
->stripes
[i
].dev
=
4604 add_missing_dev(root
, devid
, uuid
);
4605 if (!map
->stripes
[i
].dev
) {
4607 free_extent_map(em
);
4611 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
4614 write_lock(&map_tree
->map_tree
.lock
);
4615 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
4616 write_unlock(&map_tree
->map_tree
.lock
);
4617 BUG_ON(ret
); /* Tree corruption */
4618 free_extent_map(em
);
4623 static void fill_device_from_item(struct extent_buffer
*leaf
,
4624 struct btrfs_dev_item
*dev_item
,
4625 struct btrfs_device
*device
)
4629 device
->devid
= btrfs_device_id(leaf
, dev_item
);
4630 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
4631 device
->total_bytes
= device
->disk_total_bytes
;
4632 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
4633 device
->type
= btrfs_device_type(leaf
, dev_item
);
4634 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
4635 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
4636 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
4637 device
->is_tgtdev_for_dev_replace
= 0;
4639 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
4640 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
4643 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
4645 struct btrfs_fs_devices
*fs_devices
;
4648 BUG_ON(!mutex_is_locked(&uuid_mutex
));
4650 fs_devices
= root
->fs_info
->fs_devices
->seed
;
4651 while (fs_devices
) {
4652 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4656 fs_devices
= fs_devices
->seed
;
4659 fs_devices
= find_fsid(fsid
);
4665 fs_devices
= clone_fs_devices(fs_devices
);
4666 if (IS_ERR(fs_devices
)) {
4667 ret
= PTR_ERR(fs_devices
);
4671 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
4672 root
->fs_info
->bdev_holder
);
4674 free_fs_devices(fs_devices
);
4678 if (!fs_devices
->seeding
) {
4679 __btrfs_close_devices(fs_devices
);
4680 free_fs_devices(fs_devices
);
4685 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
4686 root
->fs_info
->fs_devices
->seed
= fs_devices
;
4691 static int read_one_dev(struct btrfs_root
*root
,
4692 struct extent_buffer
*leaf
,
4693 struct btrfs_dev_item
*dev_item
)
4695 struct btrfs_device
*device
;
4698 u8 fs_uuid
[BTRFS_UUID_SIZE
];
4699 u8 dev_uuid
[BTRFS_UUID_SIZE
];
4701 devid
= btrfs_device_id(leaf
, dev_item
);
4702 read_extent_buffer(leaf
, dev_uuid
,
4703 (unsigned long)btrfs_device_uuid(dev_item
),
4705 read_extent_buffer(leaf
, fs_uuid
,
4706 (unsigned long)btrfs_device_fsid(dev_item
),
4709 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
4710 ret
= open_seed_devices(root
, fs_uuid
);
4711 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
4715 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
4716 if (!device
|| !device
->bdev
) {
4717 if (!btrfs_test_opt(root
, DEGRADED
))
4721 printk(KERN_WARNING
"warning devid %llu missing\n",
4722 (unsigned long long)devid
);
4723 device
= add_missing_dev(root
, devid
, dev_uuid
);
4726 } else if (!device
->missing
) {
4728 * this happens when a device that was properly setup
4729 * in the device info lists suddenly goes bad.
4730 * device->bdev is NULL, and so we have to set
4731 * device->missing to one here
4733 root
->fs_info
->fs_devices
->missing_devices
++;
4734 device
->missing
= 1;
4738 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
4739 BUG_ON(device
->writeable
);
4740 if (device
->generation
!=
4741 btrfs_device_generation(leaf
, dev_item
))
4745 fill_device_from_item(leaf
, dev_item
, device
);
4746 device
->dev_root
= root
->fs_info
->dev_root
;
4747 device
->in_fs_metadata
= 1;
4748 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
4749 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
4750 spin_lock(&root
->fs_info
->free_chunk_lock
);
4751 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
4753 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4759 int btrfs_read_sys_array(struct btrfs_root
*root
)
4761 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4762 struct extent_buffer
*sb
;
4763 struct btrfs_disk_key
*disk_key
;
4764 struct btrfs_chunk
*chunk
;
4766 unsigned long sb_ptr
;
4772 struct btrfs_key key
;
4774 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
4775 BTRFS_SUPER_INFO_SIZE
);
4778 btrfs_set_buffer_uptodate(sb
);
4779 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
4781 * The sb extent buffer is artifical and just used to read the system array.
4782 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4783 * pages up-to-date when the page is larger: extent does not cover the
4784 * whole page and consequently check_page_uptodate does not find all
4785 * the page's extents up-to-date (the hole beyond sb),
4786 * write_extent_buffer then triggers a WARN_ON.
4788 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4789 * but sb spans only this function. Add an explicit SetPageUptodate call
4790 * to silence the warning eg. on PowerPC 64.
4792 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
4793 SetPageUptodate(sb
->pages
[0]);
4795 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
4796 array_size
= btrfs_super_sys_array_size(super_copy
);
4798 ptr
= super_copy
->sys_chunk_array
;
4799 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
4802 while (cur
< array_size
) {
4803 disk_key
= (struct btrfs_disk_key
*)ptr
;
4804 btrfs_disk_key_to_cpu(&key
, disk_key
);
4806 len
= sizeof(*disk_key
); ptr
+= len
;
4810 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4811 chunk
= (struct btrfs_chunk
*)sb_ptr
;
4812 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
4815 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
4816 len
= btrfs_chunk_item_size(num_stripes
);
4825 free_extent_buffer(sb
);
4829 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
4831 struct btrfs_path
*path
;
4832 struct extent_buffer
*leaf
;
4833 struct btrfs_key key
;
4834 struct btrfs_key found_key
;
4838 root
= root
->fs_info
->chunk_root
;
4840 path
= btrfs_alloc_path();
4844 mutex_lock(&uuid_mutex
);
4847 /* first we search for all of the device items, and then we
4848 * read in all of the chunk items. This way we can create chunk
4849 * mappings that reference all of the devices that are afound
4851 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
4855 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4859 leaf
= path
->nodes
[0];
4860 slot
= path
->slots
[0];
4861 if (slot
>= btrfs_header_nritems(leaf
)) {
4862 ret
= btrfs_next_leaf(root
, path
);
4869 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4870 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4871 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
4873 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
4874 struct btrfs_dev_item
*dev_item
;
4875 dev_item
= btrfs_item_ptr(leaf
, slot
,
4876 struct btrfs_dev_item
);
4877 ret
= read_one_dev(root
, leaf
, dev_item
);
4881 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4882 struct btrfs_chunk
*chunk
;
4883 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
4884 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
4890 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4892 btrfs_release_path(path
);
4897 unlock_chunks(root
);
4898 mutex_unlock(&uuid_mutex
);
4900 btrfs_free_path(path
);
4904 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
4908 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
4909 btrfs_dev_stat_reset(dev
, i
);
4912 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
4914 struct btrfs_key key
;
4915 struct btrfs_key found_key
;
4916 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
4917 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
4918 struct extent_buffer
*eb
;
4921 struct btrfs_device
*device
;
4922 struct btrfs_path
*path
= NULL
;
4925 path
= btrfs_alloc_path();
4931 mutex_lock(&fs_devices
->device_list_mutex
);
4932 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
4934 struct btrfs_dev_stats_item
*ptr
;
4937 key
.type
= BTRFS_DEV_STATS_KEY
;
4938 key
.offset
= device
->devid
;
4939 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
4941 __btrfs_reset_dev_stats(device
);
4942 device
->dev_stats_valid
= 1;
4943 btrfs_release_path(path
);
4946 slot
= path
->slots
[0];
4947 eb
= path
->nodes
[0];
4948 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4949 item_size
= btrfs_item_size_nr(eb
, slot
);
4951 ptr
= btrfs_item_ptr(eb
, slot
,
4952 struct btrfs_dev_stats_item
);
4954 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
4955 if (item_size
>= (1 + i
) * sizeof(__le64
))
4956 btrfs_dev_stat_set(device
, i
,
4957 btrfs_dev_stats_value(eb
, ptr
, i
));
4959 btrfs_dev_stat_reset(device
, i
);
4962 device
->dev_stats_valid
= 1;
4963 btrfs_dev_stat_print_on_load(device
);
4964 btrfs_release_path(path
);
4966 mutex_unlock(&fs_devices
->device_list_mutex
);
4969 btrfs_free_path(path
);
4970 return ret
< 0 ? ret
: 0;
4973 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
4974 struct btrfs_root
*dev_root
,
4975 struct btrfs_device
*device
)
4977 struct btrfs_path
*path
;
4978 struct btrfs_key key
;
4979 struct extent_buffer
*eb
;
4980 struct btrfs_dev_stats_item
*ptr
;
4985 key
.type
= BTRFS_DEV_STATS_KEY
;
4986 key
.offset
= device
->devid
;
4988 path
= btrfs_alloc_path();
4990 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
4992 printk_in_rcu(KERN_WARNING
"btrfs: error %d while searching for dev_stats item for device %s!\n",
4993 ret
, rcu_str_deref(device
->name
));
4998 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
4999 /* need to delete old one and insert a new one */
5000 ret
= btrfs_del_item(trans
, dev_root
, path
);
5002 printk_in_rcu(KERN_WARNING
"btrfs: delete too small dev_stats item for device %s failed %d!\n",
5003 rcu_str_deref(device
->name
), ret
);
5010 /* need to insert a new item */
5011 btrfs_release_path(path
);
5012 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
5013 &key
, sizeof(*ptr
));
5015 printk_in_rcu(KERN_WARNING
"btrfs: insert dev_stats item for device %s failed %d!\n",
5016 rcu_str_deref(device
->name
), ret
);
5021 eb
= path
->nodes
[0];
5022 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
5023 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5024 btrfs_set_dev_stats_value(eb
, ptr
, i
,
5025 btrfs_dev_stat_read(device
, i
));
5026 btrfs_mark_buffer_dirty(eb
);
5029 btrfs_free_path(path
);
5034 * called from commit_transaction. Writes all changed device stats to disk.
5036 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
5037 struct btrfs_fs_info
*fs_info
)
5039 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5040 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5041 struct btrfs_device
*device
;
5044 mutex_lock(&fs_devices
->device_list_mutex
);
5045 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5046 if (!device
->dev_stats_valid
|| !device
->dev_stats_dirty
)
5049 ret
= update_dev_stat_item(trans
, dev_root
, device
);
5051 device
->dev_stats_dirty
= 0;
5053 mutex_unlock(&fs_devices
->device_list_mutex
);
5058 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
5060 btrfs_dev_stat_inc(dev
, index
);
5061 btrfs_dev_stat_print_on_error(dev
);
5064 void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
5066 if (!dev
->dev_stats_valid
)
5068 printk_ratelimited_in_rcu(KERN_ERR
5069 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5070 rcu_str_deref(dev
->name
),
5071 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5072 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5073 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5074 btrfs_dev_stat_read(dev
,
5075 BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5076 btrfs_dev_stat_read(dev
,
5077 BTRFS_DEV_STAT_GENERATION_ERRS
));
5080 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
5084 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5085 if (btrfs_dev_stat_read(dev
, i
) != 0)
5087 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
5088 return; /* all values == 0, suppress message */
5090 printk_in_rcu(KERN_INFO
"btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5091 rcu_str_deref(dev
->name
),
5092 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5093 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5094 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5095 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5096 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
5099 int btrfs_get_dev_stats(struct btrfs_root
*root
,
5100 struct btrfs_ioctl_get_dev_stats
*stats
)
5102 struct btrfs_device
*dev
;
5103 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5106 mutex_lock(&fs_devices
->device_list_mutex
);
5107 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
5108 mutex_unlock(&fs_devices
->device_list_mutex
);
5112 "btrfs: get dev_stats failed, device not found\n");
5114 } else if (!dev
->dev_stats_valid
) {
5116 "btrfs: get dev_stats failed, not yet valid\n");
5118 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
5119 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5120 if (stats
->nr_items
> i
)
5122 btrfs_dev_stat_read_and_reset(dev
, i
);
5124 btrfs_dev_stat_reset(dev
, i
);
5127 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5128 if (stats
->nr_items
> i
)
5129 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
5131 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
5132 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
5136 int btrfs_scratch_superblock(struct btrfs_device
*device
)
5138 struct buffer_head
*bh
;
5139 struct btrfs_super_block
*disk_super
;
5141 bh
= btrfs_read_dev_super(device
->bdev
);
5144 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
5146 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
5147 set_buffer_dirty(bh
);
5148 sync_dirty_buffer(bh
);