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>
28 #include <asm/div64.h>
31 #include "extent_map.h"
33 #include "transaction.h"
34 #include "print-tree.h"
36 #include "async-thread.h"
37 #include "check-integrity.h"
38 #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)
111 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
112 struct bio
*head
, struct bio
*tail
)
115 struct bio
*old_head
;
117 old_head
= pending_bios
->head
;
118 pending_bios
->head
= head
;
119 if (pending_bios
->tail
)
120 tail
->bi_next
= old_head
;
122 pending_bios
->tail
= tail
;
126 * we try to collect pending bios for a device so we don't get a large
127 * number of procs sending bios down to the same device. This greatly
128 * improves the schedulers ability to collect and merge the bios.
130 * But, it also turns into a long list of bios to process and that is sure
131 * to eventually make the worker thread block. The solution here is to
132 * make some progress and then put this work struct back at the end of
133 * the list if the block device is congested. This way, multiple devices
134 * can make progress from a single worker thread.
136 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
139 struct backing_dev_info
*bdi
;
140 struct btrfs_fs_info
*fs_info
;
141 struct btrfs_pending_bios
*pending_bios
;
145 unsigned long num_run
;
146 unsigned long batch_run
= 0;
148 unsigned long last_waited
= 0;
150 int sync_pending
= 0;
151 struct blk_plug plug
;
154 * this function runs all the bios we've collected for
155 * a particular device. We don't want to wander off to
156 * another device without first sending all of these down.
157 * So, setup a plug here and finish it off before we return
159 blk_start_plug(&plug
);
161 bdi
= blk_get_backing_dev_info(device
->bdev
);
162 fs_info
= device
->dev_root
->fs_info
;
163 limit
= btrfs_async_submit_limit(fs_info
);
164 limit
= limit
* 2 / 3;
167 spin_lock(&device
->io_lock
);
172 /* take all the bios off the list at once and process them
173 * later on (without the lock held). But, remember the
174 * tail and other pointers so the bios can be properly reinserted
175 * into the list if we hit congestion
177 if (!force_reg
&& device
->pending_sync_bios
.head
) {
178 pending_bios
= &device
->pending_sync_bios
;
181 pending_bios
= &device
->pending_bios
;
185 pending
= pending_bios
->head
;
186 tail
= pending_bios
->tail
;
187 WARN_ON(pending
&& !tail
);
190 * if pending was null this time around, no bios need processing
191 * at all and we can stop. Otherwise it'll loop back up again
192 * and do an additional check so no bios are missed.
194 * device->running_pending is used to synchronize with the
197 if (device
->pending_sync_bios
.head
== NULL
&&
198 device
->pending_bios
.head
== NULL
) {
200 device
->running_pending
= 0;
203 device
->running_pending
= 1;
206 pending_bios
->head
= NULL
;
207 pending_bios
->tail
= NULL
;
209 spin_unlock(&device
->io_lock
);
214 /* we want to work on both lists, but do more bios on the
215 * sync list than the regular list
218 pending_bios
!= &device
->pending_sync_bios
&&
219 device
->pending_sync_bios
.head
) ||
220 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
221 device
->pending_bios
.head
)) {
222 spin_lock(&device
->io_lock
);
223 requeue_list(pending_bios
, pending
, tail
);
228 pending
= pending
->bi_next
;
231 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
232 waitqueue_active(&fs_info
->async_submit_wait
))
233 wake_up(&fs_info
->async_submit_wait
);
235 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
238 * if we're doing the sync list, record that our
239 * plug has some sync requests on it
241 * If we're doing the regular list and there are
242 * sync requests sitting around, unplug before
245 if (pending_bios
== &device
->pending_sync_bios
) {
247 } else if (sync_pending
) {
248 blk_finish_plug(&plug
);
249 blk_start_plug(&plug
);
253 btrfsic_submit_bio(cur
->bi_rw
, cur
);
260 * we made progress, there is more work to do and the bdi
261 * is now congested. Back off and let other work structs
264 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
265 fs_info
->fs_devices
->open_devices
> 1) {
266 struct io_context
*ioc
;
268 ioc
= current
->io_context
;
271 * the main goal here is that we don't want to
272 * block if we're going to be able to submit
273 * more requests without blocking.
275 * This code does two great things, it pokes into
276 * the elevator code from a filesystem _and_
277 * it makes assumptions about how batching works.
279 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
280 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
282 ioc
->last_waited
== last_waited
)) {
284 * we want to go through our batch of
285 * requests and stop. So, we copy out
286 * the ioc->last_waited time and test
287 * against it before looping
289 last_waited
= ioc
->last_waited
;
294 spin_lock(&device
->io_lock
);
295 requeue_list(pending_bios
, pending
, tail
);
296 device
->running_pending
= 1;
298 spin_unlock(&device
->io_lock
);
299 btrfs_requeue_work(&device
->work
);
302 /* unplug every 64 requests just for good measure */
303 if (batch_run
% 64 == 0) {
304 blk_finish_plug(&plug
);
305 blk_start_plug(&plug
);
314 spin_lock(&device
->io_lock
);
315 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
317 spin_unlock(&device
->io_lock
);
320 blk_finish_plug(&plug
);
323 static void pending_bios_fn(struct btrfs_work
*work
)
325 struct btrfs_device
*device
;
327 device
= container_of(work
, struct btrfs_device
, work
);
328 run_scheduled_bios(device
);
331 static noinline
int device_list_add(const char *path
,
332 struct btrfs_super_block
*disk_super
,
333 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
335 struct btrfs_device
*device
;
336 struct btrfs_fs_devices
*fs_devices
;
337 struct rcu_string
*name
;
338 u64 found_transid
= btrfs_super_generation(disk_super
);
340 fs_devices
= find_fsid(disk_super
->fsid
);
342 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
345 INIT_LIST_HEAD(&fs_devices
->devices
);
346 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
347 list_add(&fs_devices
->list
, &fs_uuids
);
348 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
349 fs_devices
->latest_devid
= devid
;
350 fs_devices
->latest_trans
= found_transid
;
351 mutex_init(&fs_devices
->device_list_mutex
);
354 device
= __find_device(&fs_devices
->devices
, devid
,
355 disk_super
->dev_item
.uuid
);
358 if (fs_devices
->opened
)
361 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
363 /* we can safely leave the fs_devices entry around */
366 device
->devid
= devid
;
367 device
->dev_stats_valid
= 0;
368 device
->work
.func
= pending_bios_fn
;
369 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
371 spin_lock_init(&device
->io_lock
);
373 name
= rcu_string_strdup(path
, GFP_NOFS
);
378 rcu_assign_pointer(device
->name
, name
);
379 INIT_LIST_HEAD(&device
->dev_alloc_list
);
381 /* init readahead state */
382 spin_lock_init(&device
->reada_lock
);
383 device
->reada_curr_zone
= NULL
;
384 atomic_set(&device
->reada_in_flight
, 0);
385 device
->reada_next
= 0;
386 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
387 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
389 mutex_lock(&fs_devices
->device_list_mutex
);
390 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
391 mutex_unlock(&fs_devices
->device_list_mutex
);
393 device
->fs_devices
= fs_devices
;
394 fs_devices
->num_devices
++;
395 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
396 name
= rcu_string_strdup(path
, GFP_NOFS
);
399 rcu_string_free(device
->name
);
400 rcu_assign_pointer(device
->name
, name
);
401 if (device
->missing
) {
402 fs_devices
->missing_devices
--;
407 if (found_transid
> fs_devices
->latest_trans
) {
408 fs_devices
->latest_devid
= devid
;
409 fs_devices
->latest_trans
= found_transid
;
411 *fs_devices_ret
= fs_devices
;
415 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
417 struct btrfs_fs_devices
*fs_devices
;
418 struct btrfs_device
*device
;
419 struct btrfs_device
*orig_dev
;
421 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
423 return ERR_PTR(-ENOMEM
);
425 INIT_LIST_HEAD(&fs_devices
->devices
);
426 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
427 INIT_LIST_HEAD(&fs_devices
->list
);
428 mutex_init(&fs_devices
->device_list_mutex
);
429 fs_devices
->latest_devid
= orig
->latest_devid
;
430 fs_devices
->latest_trans
= orig
->latest_trans
;
431 fs_devices
->total_devices
= orig
->total_devices
;
432 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
434 /* We have held the volume lock, it is safe to get the devices. */
435 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
436 struct rcu_string
*name
;
438 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
443 * This is ok to do without rcu read locked because we hold the
444 * uuid mutex so nothing we touch in here is going to disappear.
446 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
451 rcu_assign_pointer(device
->name
, name
);
453 device
->devid
= orig_dev
->devid
;
454 device
->work
.func
= pending_bios_fn
;
455 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
456 spin_lock_init(&device
->io_lock
);
457 INIT_LIST_HEAD(&device
->dev_list
);
458 INIT_LIST_HEAD(&device
->dev_alloc_list
);
460 list_add(&device
->dev_list
, &fs_devices
->devices
);
461 device
->fs_devices
= fs_devices
;
462 fs_devices
->num_devices
++;
466 free_fs_devices(fs_devices
);
467 return ERR_PTR(-ENOMEM
);
470 void btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
)
472 struct btrfs_device
*device
, *next
;
474 struct block_device
*latest_bdev
= NULL
;
475 u64 latest_devid
= 0;
476 u64 latest_transid
= 0;
478 mutex_lock(&uuid_mutex
);
480 /* This is the initialized path, it is safe to release the devices. */
481 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
482 if (device
->in_fs_metadata
) {
483 if (!latest_transid
||
484 device
->generation
> latest_transid
) {
485 latest_devid
= device
->devid
;
486 latest_transid
= device
->generation
;
487 latest_bdev
= device
->bdev
;
493 blkdev_put(device
->bdev
, device
->mode
);
495 fs_devices
->open_devices
--;
497 if (device
->writeable
) {
498 list_del_init(&device
->dev_alloc_list
);
499 device
->writeable
= 0;
500 fs_devices
->rw_devices
--;
502 list_del_init(&device
->dev_list
);
503 fs_devices
->num_devices
--;
504 rcu_string_free(device
->name
);
508 if (fs_devices
->seed
) {
509 fs_devices
= fs_devices
->seed
;
513 fs_devices
->latest_bdev
= latest_bdev
;
514 fs_devices
->latest_devid
= latest_devid
;
515 fs_devices
->latest_trans
= latest_transid
;
517 mutex_unlock(&uuid_mutex
);
520 static void __free_device(struct work_struct
*work
)
522 struct btrfs_device
*device
;
524 device
= container_of(work
, struct btrfs_device
, rcu_work
);
527 blkdev_put(device
->bdev
, device
->mode
);
529 rcu_string_free(device
->name
);
533 static void free_device(struct rcu_head
*head
)
535 struct btrfs_device
*device
;
537 device
= container_of(head
, struct btrfs_device
, rcu
);
539 INIT_WORK(&device
->rcu_work
, __free_device
);
540 schedule_work(&device
->rcu_work
);
543 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
545 struct btrfs_device
*device
;
547 if (--fs_devices
->opened
> 0)
550 mutex_lock(&fs_devices
->device_list_mutex
);
551 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
552 struct btrfs_device
*new_device
;
553 struct rcu_string
*name
;
556 fs_devices
->open_devices
--;
558 if (device
->writeable
) {
559 list_del_init(&device
->dev_alloc_list
);
560 fs_devices
->rw_devices
--;
563 if (device
->can_discard
)
564 fs_devices
->num_can_discard
--;
566 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
567 BUG_ON(!new_device
); /* -ENOMEM */
568 memcpy(new_device
, device
, sizeof(*new_device
));
570 /* Safe because we are under uuid_mutex */
572 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
573 BUG_ON(device
->name
&& !name
); /* -ENOMEM */
574 rcu_assign_pointer(new_device
->name
, name
);
576 new_device
->bdev
= NULL
;
577 new_device
->writeable
= 0;
578 new_device
->in_fs_metadata
= 0;
579 new_device
->can_discard
= 0;
580 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
582 call_rcu(&device
->rcu
, free_device
);
584 mutex_unlock(&fs_devices
->device_list_mutex
);
586 WARN_ON(fs_devices
->open_devices
);
587 WARN_ON(fs_devices
->rw_devices
);
588 fs_devices
->opened
= 0;
589 fs_devices
->seeding
= 0;
594 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
596 struct btrfs_fs_devices
*seed_devices
= NULL
;
599 mutex_lock(&uuid_mutex
);
600 ret
= __btrfs_close_devices(fs_devices
);
601 if (!fs_devices
->opened
) {
602 seed_devices
= fs_devices
->seed
;
603 fs_devices
->seed
= NULL
;
605 mutex_unlock(&uuid_mutex
);
607 while (seed_devices
) {
608 fs_devices
= seed_devices
;
609 seed_devices
= fs_devices
->seed
;
610 __btrfs_close_devices(fs_devices
);
611 free_fs_devices(fs_devices
);
616 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
617 fmode_t flags
, void *holder
)
619 struct request_queue
*q
;
620 struct block_device
*bdev
;
621 struct list_head
*head
= &fs_devices
->devices
;
622 struct btrfs_device
*device
;
623 struct block_device
*latest_bdev
= NULL
;
624 struct buffer_head
*bh
;
625 struct btrfs_super_block
*disk_super
;
626 u64 latest_devid
= 0;
627 u64 latest_transid
= 0;
634 list_for_each_entry(device
, head
, dev_list
) {
640 bdev
= blkdev_get_by_path(device
->name
->str
, flags
, holder
);
642 printk(KERN_INFO
"btrfs: open %s failed\n", device
->name
->str
);
645 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
646 invalidate_bdev(bdev
);
647 set_blocksize(bdev
, 4096);
649 bh
= btrfs_read_dev_super(bdev
);
653 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
654 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
655 if (devid
!= device
->devid
)
658 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
662 device
->generation
= btrfs_super_generation(disk_super
);
663 if (!latest_transid
|| device
->generation
> latest_transid
) {
664 latest_devid
= devid
;
665 latest_transid
= device
->generation
;
669 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
670 device
->writeable
= 0;
672 device
->writeable
= !bdev_read_only(bdev
);
676 q
= bdev_get_queue(bdev
);
677 if (blk_queue_discard(q
)) {
678 device
->can_discard
= 1;
679 fs_devices
->num_can_discard
++;
683 device
->in_fs_metadata
= 0;
684 device
->mode
= flags
;
686 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
687 fs_devices
->rotating
= 1;
689 fs_devices
->open_devices
++;
690 if (device
->writeable
) {
691 fs_devices
->rw_devices
++;
692 list_add(&device
->dev_alloc_list
,
693 &fs_devices
->alloc_list
);
701 blkdev_put(bdev
, flags
);
705 if (fs_devices
->open_devices
== 0) {
709 fs_devices
->seeding
= seeding
;
710 fs_devices
->opened
= 1;
711 fs_devices
->latest_bdev
= latest_bdev
;
712 fs_devices
->latest_devid
= latest_devid
;
713 fs_devices
->latest_trans
= latest_transid
;
714 fs_devices
->total_rw_bytes
= 0;
719 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
720 fmode_t flags
, void *holder
)
724 mutex_lock(&uuid_mutex
);
725 if (fs_devices
->opened
) {
726 fs_devices
->opened
++;
729 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
731 mutex_unlock(&uuid_mutex
);
735 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
736 struct btrfs_fs_devices
**fs_devices_ret
)
738 struct btrfs_super_block
*disk_super
;
739 struct block_device
*bdev
;
740 struct buffer_head
*bh
;
747 bdev
= blkdev_get_by_path(path
, flags
, holder
);
754 mutex_lock(&uuid_mutex
);
755 ret
= set_blocksize(bdev
, 4096);
758 bh
= btrfs_read_dev_super(bdev
);
763 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
764 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
765 transid
= btrfs_super_generation(disk_super
);
766 total_devices
= btrfs_super_num_devices(disk_super
);
767 if (disk_super
->label
[0])
768 printk(KERN_INFO
"device label %s ", disk_super
->label
);
770 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
771 printk(KERN_CONT
"devid %llu transid %llu %s\n",
772 (unsigned long long)devid
, (unsigned long long)transid
, path
);
773 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
774 if (!ret
&& fs_devices_ret
)
775 (*fs_devices_ret
)->total_devices
= total_devices
;
778 mutex_unlock(&uuid_mutex
);
779 blkdev_put(bdev
, flags
);
784 /* helper to account the used device space in the range */
785 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
786 u64 end
, u64
*length
)
788 struct btrfs_key key
;
789 struct btrfs_root
*root
= device
->dev_root
;
790 struct btrfs_dev_extent
*dev_extent
;
791 struct btrfs_path
*path
;
795 struct extent_buffer
*l
;
799 if (start
>= device
->total_bytes
)
802 path
= btrfs_alloc_path();
807 key
.objectid
= device
->devid
;
809 key
.type
= BTRFS_DEV_EXTENT_KEY
;
811 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
815 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
822 slot
= path
->slots
[0];
823 if (slot
>= btrfs_header_nritems(l
)) {
824 ret
= btrfs_next_leaf(root
, path
);
832 btrfs_item_key_to_cpu(l
, &key
, slot
);
834 if (key
.objectid
< device
->devid
)
837 if (key
.objectid
> device
->devid
)
840 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
843 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
844 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
846 if (key
.offset
<= start
&& extent_end
> end
) {
847 *length
= end
- start
+ 1;
849 } else if (key
.offset
<= start
&& extent_end
> start
)
850 *length
+= extent_end
- start
;
851 else if (key
.offset
> start
&& extent_end
<= end
)
852 *length
+= extent_end
- key
.offset
;
853 else if (key
.offset
> start
&& key
.offset
<= end
) {
854 *length
+= end
- key
.offset
+ 1;
856 } else if (key
.offset
> end
)
864 btrfs_free_path(path
);
869 * find_free_dev_extent - find free space in the specified device
870 * @device: the device which we search the free space in
871 * @num_bytes: the size of the free space that we need
872 * @start: store the start of the free space.
873 * @len: the size of the free space. that we find, or the size of the max
874 * free space if we don't find suitable free space
876 * this uses a pretty simple search, the expectation is that it is
877 * called very infrequently and that a given device has a small number
880 * @start is used to store the start of the free space if we find. But if we
881 * don't find suitable free space, it will be used to store the start position
882 * of the max free space.
884 * @len is used to store the size of the free space that we find.
885 * But if we don't find suitable free space, it is used to store the size of
886 * the max free space.
888 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
889 u64
*start
, u64
*len
)
891 struct btrfs_key key
;
892 struct btrfs_root
*root
= device
->dev_root
;
893 struct btrfs_dev_extent
*dev_extent
;
894 struct btrfs_path
*path
;
900 u64 search_end
= device
->total_bytes
;
903 struct extent_buffer
*l
;
905 /* FIXME use last free of some kind */
907 /* we don't want to overwrite the superblock on the drive,
908 * so we make sure to start at an offset of at least 1MB
910 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
912 max_hole_start
= search_start
;
916 if (search_start
>= search_end
) {
921 path
= btrfs_alloc_path();
928 key
.objectid
= device
->devid
;
929 key
.offset
= search_start
;
930 key
.type
= BTRFS_DEV_EXTENT_KEY
;
932 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
936 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
943 slot
= path
->slots
[0];
944 if (slot
>= btrfs_header_nritems(l
)) {
945 ret
= btrfs_next_leaf(root
, path
);
953 btrfs_item_key_to_cpu(l
, &key
, slot
);
955 if (key
.objectid
< device
->devid
)
958 if (key
.objectid
> device
->devid
)
961 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
964 if (key
.offset
> search_start
) {
965 hole_size
= key
.offset
- search_start
;
967 if (hole_size
> max_hole_size
) {
968 max_hole_start
= search_start
;
969 max_hole_size
= hole_size
;
973 * If this free space is greater than which we need,
974 * it must be the max free space that we have found
975 * until now, so max_hole_start must point to the start
976 * of this free space and the length of this free space
977 * is stored in max_hole_size. Thus, we return
978 * max_hole_start and max_hole_size and go back to the
981 if (hole_size
>= num_bytes
) {
987 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
988 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
990 if (extent_end
> search_start
)
991 search_start
= extent_end
;
998 * At this point, search_start should be the end of
999 * allocated dev extents, and when shrinking the device,
1000 * search_end may be smaller than search_start.
1002 if (search_end
> search_start
)
1003 hole_size
= search_end
- search_start
;
1005 if (hole_size
> max_hole_size
) {
1006 max_hole_start
= search_start
;
1007 max_hole_size
= hole_size
;
1011 if (hole_size
< num_bytes
)
1017 btrfs_free_path(path
);
1019 *start
= max_hole_start
;
1021 *len
= max_hole_size
;
1025 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1026 struct btrfs_device
*device
,
1030 struct btrfs_path
*path
;
1031 struct btrfs_root
*root
= device
->dev_root
;
1032 struct btrfs_key key
;
1033 struct btrfs_key found_key
;
1034 struct extent_buffer
*leaf
= NULL
;
1035 struct btrfs_dev_extent
*extent
= NULL
;
1037 path
= btrfs_alloc_path();
1041 key
.objectid
= device
->devid
;
1043 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1045 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1047 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1048 BTRFS_DEV_EXTENT_KEY
);
1051 leaf
= path
->nodes
[0];
1052 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1053 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1054 struct btrfs_dev_extent
);
1055 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1056 btrfs_dev_extent_length(leaf
, extent
) < start
);
1058 btrfs_release_path(path
);
1060 } else if (ret
== 0) {
1061 leaf
= path
->nodes
[0];
1062 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1063 struct btrfs_dev_extent
);
1065 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1069 if (device
->bytes_used
> 0) {
1070 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1071 device
->bytes_used
-= len
;
1072 spin_lock(&root
->fs_info
->free_chunk_lock
);
1073 root
->fs_info
->free_chunk_space
+= len
;
1074 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1076 ret
= btrfs_del_item(trans
, root
, path
);
1078 btrfs_error(root
->fs_info
, ret
,
1079 "Failed to remove dev extent item");
1082 btrfs_free_path(path
);
1086 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1087 struct btrfs_device
*device
,
1088 u64 chunk_tree
, u64 chunk_objectid
,
1089 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1092 struct btrfs_path
*path
;
1093 struct btrfs_root
*root
= device
->dev_root
;
1094 struct btrfs_dev_extent
*extent
;
1095 struct extent_buffer
*leaf
;
1096 struct btrfs_key key
;
1098 WARN_ON(!device
->in_fs_metadata
);
1099 path
= btrfs_alloc_path();
1103 key
.objectid
= device
->devid
;
1105 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1106 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1111 leaf
= path
->nodes
[0];
1112 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1113 struct btrfs_dev_extent
);
1114 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1115 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1116 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1118 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1119 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1122 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1123 btrfs_mark_buffer_dirty(leaf
);
1125 btrfs_free_path(path
);
1129 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1130 u64 objectid
, u64
*offset
)
1132 struct btrfs_path
*path
;
1134 struct btrfs_key key
;
1135 struct btrfs_chunk
*chunk
;
1136 struct btrfs_key found_key
;
1138 path
= btrfs_alloc_path();
1142 key
.objectid
= objectid
;
1143 key
.offset
= (u64
)-1;
1144 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1146 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1150 BUG_ON(ret
== 0); /* Corruption */
1152 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1156 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1158 if (found_key
.objectid
!= objectid
)
1161 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1162 struct btrfs_chunk
);
1163 *offset
= found_key
.offset
+
1164 btrfs_chunk_length(path
->nodes
[0], chunk
);
1169 btrfs_free_path(path
);
1173 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1176 struct btrfs_key key
;
1177 struct btrfs_key found_key
;
1178 struct btrfs_path
*path
;
1180 root
= root
->fs_info
->chunk_root
;
1182 path
= btrfs_alloc_path();
1186 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1187 key
.type
= BTRFS_DEV_ITEM_KEY
;
1188 key
.offset
= (u64
)-1;
1190 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1194 BUG_ON(ret
== 0); /* Corruption */
1196 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1197 BTRFS_DEV_ITEM_KEY
);
1201 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1203 *objectid
= found_key
.offset
+ 1;
1207 btrfs_free_path(path
);
1212 * the device information is stored in the chunk root
1213 * the btrfs_device struct should be fully filled in
1215 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1216 struct btrfs_root
*root
,
1217 struct btrfs_device
*device
)
1220 struct btrfs_path
*path
;
1221 struct btrfs_dev_item
*dev_item
;
1222 struct extent_buffer
*leaf
;
1223 struct btrfs_key key
;
1226 root
= root
->fs_info
->chunk_root
;
1228 path
= btrfs_alloc_path();
1232 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1233 key
.type
= BTRFS_DEV_ITEM_KEY
;
1234 key
.offset
= device
->devid
;
1236 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1241 leaf
= path
->nodes
[0];
1242 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1244 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1245 btrfs_set_device_generation(leaf
, dev_item
, 0);
1246 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1247 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1248 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1249 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1250 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1251 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1252 btrfs_set_device_group(leaf
, dev_item
, 0);
1253 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1254 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1255 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1257 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1258 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1259 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1260 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1261 btrfs_mark_buffer_dirty(leaf
);
1265 btrfs_free_path(path
);
1269 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1270 struct btrfs_device
*device
)
1273 struct btrfs_path
*path
;
1274 struct btrfs_key key
;
1275 struct btrfs_trans_handle
*trans
;
1277 root
= root
->fs_info
->chunk_root
;
1279 path
= btrfs_alloc_path();
1283 trans
= btrfs_start_transaction(root
, 0);
1284 if (IS_ERR(trans
)) {
1285 btrfs_free_path(path
);
1286 return PTR_ERR(trans
);
1288 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1289 key
.type
= BTRFS_DEV_ITEM_KEY
;
1290 key
.offset
= device
->devid
;
1293 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1302 ret
= btrfs_del_item(trans
, root
, path
);
1306 btrfs_free_path(path
);
1307 unlock_chunks(root
);
1308 btrfs_commit_transaction(trans
, root
);
1312 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1314 struct btrfs_device
*device
;
1315 struct btrfs_device
*next_device
;
1316 struct block_device
*bdev
;
1317 struct buffer_head
*bh
= NULL
;
1318 struct btrfs_super_block
*disk_super
;
1319 struct btrfs_fs_devices
*cur_devices
;
1325 bool clear_super
= false;
1327 mutex_lock(&uuid_mutex
);
1329 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1330 root
->fs_info
->avail_system_alloc_bits
|
1331 root
->fs_info
->avail_metadata_alloc_bits
;
1333 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) &&
1334 root
->fs_info
->fs_devices
->num_devices
<= 4) {
1335 printk(KERN_ERR
"btrfs: unable to go below four devices "
1341 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) &&
1342 root
->fs_info
->fs_devices
->num_devices
<= 2) {
1343 printk(KERN_ERR
"btrfs: unable to go below two "
1344 "devices on raid1\n");
1349 if (strcmp(device_path
, "missing") == 0) {
1350 struct list_head
*devices
;
1351 struct btrfs_device
*tmp
;
1354 devices
= &root
->fs_info
->fs_devices
->devices
;
1356 * It is safe to read the devices since the volume_mutex
1359 list_for_each_entry(tmp
, devices
, dev_list
) {
1360 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1369 printk(KERN_ERR
"btrfs: no missing devices found to "
1374 bdev
= blkdev_get_by_path(device_path
, FMODE_READ
| FMODE_EXCL
,
1375 root
->fs_info
->bdev_holder
);
1377 ret
= PTR_ERR(bdev
);
1381 set_blocksize(bdev
, 4096);
1382 invalidate_bdev(bdev
);
1383 bh
= btrfs_read_dev_super(bdev
);
1388 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1389 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1390 dev_uuid
= disk_super
->dev_item
.uuid
;
1391 device
= btrfs_find_device(root
, devid
, dev_uuid
,
1399 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1400 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1406 if (device
->writeable
) {
1408 list_del_init(&device
->dev_alloc_list
);
1409 unlock_chunks(root
);
1410 root
->fs_info
->fs_devices
->rw_devices
--;
1414 ret
= btrfs_shrink_device(device
, 0);
1418 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1422 spin_lock(&root
->fs_info
->free_chunk_lock
);
1423 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1425 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1427 device
->in_fs_metadata
= 0;
1428 btrfs_scrub_cancel_dev(root
, device
);
1431 * the device list mutex makes sure that we don't change
1432 * the device list while someone else is writing out all
1433 * the device supers.
1436 cur_devices
= device
->fs_devices
;
1437 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1438 list_del_rcu(&device
->dev_list
);
1440 device
->fs_devices
->num_devices
--;
1441 device
->fs_devices
->total_devices
--;
1443 if (device
->missing
)
1444 root
->fs_info
->fs_devices
->missing_devices
--;
1446 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1447 struct btrfs_device
, dev_list
);
1448 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1449 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1450 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1451 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1454 device
->fs_devices
->open_devices
--;
1456 call_rcu(&device
->rcu
, free_device
);
1457 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1459 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1460 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1462 if (cur_devices
->open_devices
== 0) {
1463 struct btrfs_fs_devices
*fs_devices
;
1464 fs_devices
= root
->fs_info
->fs_devices
;
1465 while (fs_devices
) {
1466 if (fs_devices
->seed
== cur_devices
)
1468 fs_devices
= fs_devices
->seed
;
1470 fs_devices
->seed
= cur_devices
->seed
;
1471 cur_devices
->seed
= NULL
;
1473 __btrfs_close_devices(cur_devices
);
1474 unlock_chunks(root
);
1475 free_fs_devices(cur_devices
);
1478 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1479 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1482 * at this point, the device is zero sized. We want to
1483 * remove it from the devices list and zero out the old super
1486 /* make sure this device isn't detected as part of
1489 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1490 set_buffer_dirty(bh
);
1491 sync_dirty_buffer(bh
);
1500 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1502 mutex_unlock(&uuid_mutex
);
1505 if (device
->writeable
) {
1507 list_add(&device
->dev_alloc_list
,
1508 &root
->fs_info
->fs_devices
->alloc_list
);
1509 unlock_chunks(root
);
1510 root
->fs_info
->fs_devices
->rw_devices
++;
1516 * does all the dirty work required for changing file system's UUID.
1518 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1520 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1521 struct btrfs_fs_devices
*old_devices
;
1522 struct btrfs_fs_devices
*seed_devices
;
1523 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1524 struct btrfs_device
*device
;
1527 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1528 if (!fs_devices
->seeding
)
1531 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1535 old_devices
= clone_fs_devices(fs_devices
);
1536 if (IS_ERR(old_devices
)) {
1537 kfree(seed_devices
);
1538 return PTR_ERR(old_devices
);
1541 list_add(&old_devices
->list
, &fs_uuids
);
1543 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1544 seed_devices
->opened
= 1;
1545 INIT_LIST_HEAD(&seed_devices
->devices
);
1546 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1547 mutex_init(&seed_devices
->device_list_mutex
);
1549 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1550 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1552 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1554 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1555 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1556 device
->fs_devices
= seed_devices
;
1559 fs_devices
->seeding
= 0;
1560 fs_devices
->num_devices
= 0;
1561 fs_devices
->open_devices
= 0;
1562 fs_devices
->total_devices
= 0;
1563 fs_devices
->seed
= seed_devices
;
1565 generate_random_uuid(fs_devices
->fsid
);
1566 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1567 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1568 super_flags
= btrfs_super_flags(disk_super
) &
1569 ~BTRFS_SUPER_FLAG_SEEDING
;
1570 btrfs_set_super_flags(disk_super
, super_flags
);
1576 * strore the expected generation for seed devices in device items.
1578 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1579 struct btrfs_root
*root
)
1581 struct btrfs_path
*path
;
1582 struct extent_buffer
*leaf
;
1583 struct btrfs_dev_item
*dev_item
;
1584 struct btrfs_device
*device
;
1585 struct btrfs_key key
;
1586 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1587 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1591 path
= btrfs_alloc_path();
1595 root
= root
->fs_info
->chunk_root
;
1596 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1598 key
.type
= BTRFS_DEV_ITEM_KEY
;
1601 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1605 leaf
= path
->nodes
[0];
1607 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1608 ret
= btrfs_next_leaf(root
, path
);
1613 leaf
= path
->nodes
[0];
1614 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1615 btrfs_release_path(path
);
1619 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1620 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1621 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1624 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1625 struct btrfs_dev_item
);
1626 devid
= btrfs_device_id(leaf
, dev_item
);
1627 read_extent_buffer(leaf
, dev_uuid
,
1628 (unsigned long)btrfs_device_uuid(dev_item
),
1630 read_extent_buffer(leaf
, fs_uuid
,
1631 (unsigned long)btrfs_device_fsid(dev_item
),
1633 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
1634 BUG_ON(!device
); /* Logic error */
1636 if (device
->fs_devices
->seeding
) {
1637 btrfs_set_device_generation(leaf
, dev_item
,
1638 device
->generation
);
1639 btrfs_mark_buffer_dirty(leaf
);
1647 btrfs_free_path(path
);
1651 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1653 struct request_queue
*q
;
1654 struct btrfs_trans_handle
*trans
;
1655 struct btrfs_device
*device
;
1656 struct block_device
*bdev
;
1657 struct list_head
*devices
;
1658 struct super_block
*sb
= root
->fs_info
->sb
;
1659 struct rcu_string
*name
;
1661 int seeding_dev
= 0;
1664 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1667 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1668 root
->fs_info
->bdev_holder
);
1670 return PTR_ERR(bdev
);
1672 if (root
->fs_info
->fs_devices
->seeding
) {
1674 down_write(&sb
->s_umount
);
1675 mutex_lock(&uuid_mutex
);
1678 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1680 devices
= &root
->fs_info
->fs_devices
->devices
;
1682 * we have the volume lock, so we don't need the extra
1683 * device list mutex while reading the list here.
1685 list_for_each_entry(device
, devices
, dev_list
) {
1686 if (device
->bdev
== bdev
) {
1692 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1694 /* we can safely leave the fs_devices entry around */
1699 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
1705 rcu_assign_pointer(device
->name
, name
);
1707 ret
= find_next_devid(root
, &device
->devid
);
1709 rcu_string_free(device
->name
);
1714 trans
= btrfs_start_transaction(root
, 0);
1715 if (IS_ERR(trans
)) {
1716 rcu_string_free(device
->name
);
1718 ret
= PTR_ERR(trans
);
1724 q
= bdev_get_queue(bdev
);
1725 if (blk_queue_discard(q
))
1726 device
->can_discard
= 1;
1727 device
->writeable
= 1;
1728 device
->work
.func
= pending_bios_fn
;
1729 generate_random_uuid(device
->uuid
);
1730 spin_lock_init(&device
->io_lock
);
1731 device
->generation
= trans
->transid
;
1732 device
->io_width
= root
->sectorsize
;
1733 device
->io_align
= root
->sectorsize
;
1734 device
->sector_size
= root
->sectorsize
;
1735 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1736 device
->disk_total_bytes
= device
->total_bytes
;
1737 device
->dev_root
= root
->fs_info
->dev_root
;
1738 device
->bdev
= bdev
;
1739 device
->in_fs_metadata
= 1;
1740 device
->mode
= FMODE_EXCL
;
1741 set_blocksize(device
->bdev
, 4096);
1744 sb
->s_flags
&= ~MS_RDONLY
;
1745 ret
= btrfs_prepare_sprout(root
);
1746 BUG_ON(ret
); /* -ENOMEM */
1749 device
->fs_devices
= root
->fs_info
->fs_devices
;
1751 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1752 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1753 list_add(&device
->dev_alloc_list
,
1754 &root
->fs_info
->fs_devices
->alloc_list
);
1755 root
->fs_info
->fs_devices
->num_devices
++;
1756 root
->fs_info
->fs_devices
->open_devices
++;
1757 root
->fs_info
->fs_devices
->rw_devices
++;
1758 root
->fs_info
->fs_devices
->total_devices
++;
1759 if (device
->can_discard
)
1760 root
->fs_info
->fs_devices
->num_can_discard
++;
1761 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1763 spin_lock(&root
->fs_info
->free_chunk_lock
);
1764 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1765 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1767 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1768 root
->fs_info
->fs_devices
->rotating
= 1;
1770 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1771 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1772 total_bytes
+ device
->total_bytes
);
1774 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1775 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1777 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1780 ret
= init_first_rw_device(trans
, root
, device
);
1782 btrfs_abort_transaction(trans
, root
, ret
);
1785 ret
= btrfs_finish_sprout(trans
, root
);
1787 btrfs_abort_transaction(trans
, root
, ret
);
1791 ret
= btrfs_add_device(trans
, root
, device
);
1793 btrfs_abort_transaction(trans
, root
, ret
);
1799 * we've got more storage, clear any full flags on the space
1802 btrfs_clear_space_info_full(root
->fs_info
);
1804 unlock_chunks(root
);
1805 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1806 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1807 ret
= btrfs_commit_transaction(trans
, root
);
1810 mutex_unlock(&uuid_mutex
);
1811 up_write(&sb
->s_umount
);
1813 if (ret
) /* transaction commit */
1816 ret
= btrfs_relocate_sys_chunks(root
);
1818 btrfs_error(root
->fs_info
, ret
,
1819 "Failed to relocate sys chunks after "
1820 "device initialization. This can be fixed "
1821 "using the \"btrfs balance\" command.");
1827 unlock_chunks(root
);
1828 btrfs_end_transaction(trans
, root
);
1829 rcu_string_free(device
->name
);
1832 blkdev_put(bdev
, FMODE_EXCL
);
1834 mutex_unlock(&uuid_mutex
);
1835 up_write(&sb
->s_umount
);
1840 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
1841 struct btrfs_device
*device
)
1844 struct btrfs_path
*path
;
1845 struct btrfs_root
*root
;
1846 struct btrfs_dev_item
*dev_item
;
1847 struct extent_buffer
*leaf
;
1848 struct btrfs_key key
;
1850 root
= device
->dev_root
->fs_info
->chunk_root
;
1852 path
= btrfs_alloc_path();
1856 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1857 key
.type
= BTRFS_DEV_ITEM_KEY
;
1858 key
.offset
= device
->devid
;
1860 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1869 leaf
= path
->nodes
[0];
1870 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1872 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1873 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1874 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1875 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1876 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1877 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1878 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1879 btrfs_mark_buffer_dirty(leaf
);
1882 btrfs_free_path(path
);
1886 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1887 struct btrfs_device
*device
, u64 new_size
)
1889 struct btrfs_super_block
*super_copy
=
1890 device
->dev_root
->fs_info
->super_copy
;
1891 u64 old_total
= btrfs_super_total_bytes(super_copy
);
1892 u64 diff
= new_size
- device
->total_bytes
;
1894 if (!device
->writeable
)
1896 if (new_size
<= device
->total_bytes
)
1899 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
1900 device
->fs_devices
->total_rw_bytes
+= diff
;
1902 device
->total_bytes
= new_size
;
1903 device
->disk_total_bytes
= new_size
;
1904 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
1906 return btrfs_update_device(trans
, device
);
1909 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
1910 struct btrfs_device
*device
, u64 new_size
)
1913 lock_chunks(device
->dev_root
);
1914 ret
= __btrfs_grow_device(trans
, device
, new_size
);
1915 unlock_chunks(device
->dev_root
);
1919 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
1920 struct btrfs_root
*root
,
1921 u64 chunk_tree
, u64 chunk_objectid
,
1925 struct btrfs_path
*path
;
1926 struct btrfs_key key
;
1928 root
= root
->fs_info
->chunk_root
;
1929 path
= btrfs_alloc_path();
1933 key
.objectid
= chunk_objectid
;
1934 key
.offset
= chunk_offset
;
1935 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1937 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1940 else if (ret
> 0) { /* Logic error or corruption */
1941 btrfs_error(root
->fs_info
, -ENOENT
,
1942 "Failed lookup while freeing chunk.");
1947 ret
= btrfs_del_item(trans
, root
, path
);
1949 btrfs_error(root
->fs_info
, ret
,
1950 "Failed to delete chunk item.");
1952 btrfs_free_path(path
);
1956 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
1959 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
1960 struct btrfs_disk_key
*disk_key
;
1961 struct btrfs_chunk
*chunk
;
1968 struct btrfs_key key
;
1970 array_size
= btrfs_super_sys_array_size(super_copy
);
1972 ptr
= super_copy
->sys_chunk_array
;
1975 while (cur
< array_size
) {
1976 disk_key
= (struct btrfs_disk_key
*)ptr
;
1977 btrfs_disk_key_to_cpu(&key
, disk_key
);
1979 len
= sizeof(*disk_key
);
1981 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
1982 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
1983 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
1984 len
+= btrfs_chunk_item_size(num_stripes
);
1989 if (key
.objectid
== chunk_objectid
&&
1990 key
.offset
== chunk_offset
) {
1991 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
1993 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2002 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2003 u64 chunk_tree
, u64 chunk_objectid
,
2006 struct extent_map_tree
*em_tree
;
2007 struct btrfs_root
*extent_root
;
2008 struct btrfs_trans_handle
*trans
;
2009 struct extent_map
*em
;
2010 struct map_lookup
*map
;
2014 root
= root
->fs_info
->chunk_root
;
2015 extent_root
= root
->fs_info
->extent_root
;
2016 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2018 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2022 /* step one, relocate all the extents inside this chunk */
2023 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2027 trans
= btrfs_start_transaction(root
, 0);
2028 BUG_ON(IS_ERR(trans
));
2033 * step two, delete the device extents and the
2034 * chunk tree entries
2036 read_lock(&em_tree
->lock
);
2037 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2038 read_unlock(&em_tree
->lock
);
2040 BUG_ON(!em
|| em
->start
> chunk_offset
||
2041 em
->start
+ em
->len
< chunk_offset
);
2042 map
= (struct map_lookup
*)em
->bdev
;
2044 for (i
= 0; i
< map
->num_stripes
; i
++) {
2045 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2046 map
->stripes
[i
].physical
);
2049 if (map
->stripes
[i
].dev
) {
2050 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2054 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2059 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2061 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2062 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2066 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2069 write_lock(&em_tree
->lock
);
2070 remove_extent_mapping(em_tree
, em
);
2071 write_unlock(&em_tree
->lock
);
2076 /* once for the tree */
2077 free_extent_map(em
);
2079 free_extent_map(em
);
2081 unlock_chunks(root
);
2082 btrfs_end_transaction(trans
, root
);
2086 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2088 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2089 struct btrfs_path
*path
;
2090 struct extent_buffer
*leaf
;
2091 struct btrfs_chunk
*chunk
;
2092 struct btrfs_key key
;
2093 struct btrfs_key found_key
;
2094 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2096 bool retried
= false;
2100 path
= btrfs_alloc_path();
2105 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2106 key
.offset
= (u64
)-1;
2107 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2110 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2113 BUG_ON(ret
== 0); /* Corruption */
2115 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2122 leaf
= path
->nodes
[0];
2123 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2125 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2126 struct btrfs_chunk
);
2127 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2128 btrfs_release_path(path
);
2130 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2131 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2140 if (found_key
.offset
== 0)
2142 key
.offset
= found_key
.offset
- 1;
2145 if (failed
&& !retried
) {
2149 } else if (failed
&& retried
) {
2154 btrfs_free_path(path
);
2158 static int insert_balance_item(struct btrfs_root
*root
,
2159 struct btrfs_balance_control
*bctl
)
2161 struct btrfs_trans_handle
*trans
;
2162 struct btrfs_balance_item
*item
;
2163 struct btrfs_disk_balance_args disk_bargs
;
2164 struct btrfs_path
*path
;
2165 struct extent_buffer
*leaf
;
2166 struct btrfs_key key
;
2169 path
= btrfs_alloc_path();
2173 trans
= btrfs_start_transaction(root
, 0);
2174 if (IS_ERR(trans
)) {
2175 btrfs_free_path(path
);
2176 return PTR_ERR(trans
);
2179 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2180 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2183 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2188 leaf
= path
->nodes
[0];
2189 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2191 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2193 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2194 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2195 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2196 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2197 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2198 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2200 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2202 btrfs_mark_buffer_dirty(leaf
);
2204 btrfs_free_path(path
);
2205 err
= btrfs_commit_transaction(trans
, root
);
2211 static int del_balance_item(struct btrfs_root
*root
)
2213 struct btrfs_trans_handle
*trans
;
2214 struct btrfs_path
*path
;
2215 struct btrfs_key key
;
2218 path
= btrfs_alloc_path();
2222 trans
= btrfs_start_transaction(root
, 0);
2223 if (IS_ERR(trans
)) {
2224 btrfs_free_path(path
);
2225 return PTR_ERR(trans
);
2228 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2229 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2232 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2240 ret
= btrfs_del_item(trans
, root
, path
);
2242 btrfs_free_path(path
);
2243 err
= btrfs_commit_transaction(trans
, root
);
2250 * This is a heuristic used to reduce the number of chunks balanced on
2251 * resume after balance was interrupted.
2253 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2256 * Turn on soft mode for chunk types that were being converted.
2258 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2259 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2260 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2261 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2262 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2263 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2266 * Turn on usage filter if is not already used. The idea is
2267 * that chunks that we have already balanced should be
2268 * reasonably full. Don't do it for chunks that are being
2269 * converted - that will keep us from relocating unconverted
2270 * (albeit full) chunks.
2272 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2273 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2274 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2275 bctl
->data
.usage
= 90;
2277 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2278 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2279 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2280 bctl
->sys
.usage
= 90;
2282 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2283 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2284 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2285 bctl
->meta
.usage
= 90;
2290 * Should be called with both balance and volume mutexes held to
2291 * serialize other volume operations (add_dev/rm_dev/resize) with
2292 * restriper. Same goes for unset_balance_control.
2294 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2296 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2298 BUG_ON(fs_info
->balance_ctl
);
2300 spin_lock(&fs_info
->balance_lock
);
2301 fs_info
->balance_ctl
= bctl
;
2302 spin_unlock(&fs_info
->balance_lock
);
2305 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2307 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2309 BUG_ON(!fs_info
->balance_ctl
);
2311 spin_lock(&fs_info
->balance_lock
);
2312 fs_info
->balance_ctl
= NULL
;
2313 spin_unlock(&fs_info
->balance_lock
);
2319 * Balance filters. Return 1 if chunk should be filtered out
2320 * (should not be balanced).
2322 static int chunk_profiles_filter(u64 chunk_type
,
2323 struct btrfs_balance_args
*bargs
)
2325 chunk_type
= chunk_to_extended(chunk_type
) &
2326 BTRFS_EXTENDED_PROFILE_MASK
;
2328 if (bargs
->profiles
& chunk_type
)
2334 static u64
div_factor_fine(u64 num
, int factor
)
2346 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2347 struct btrfs_balance_args
*bargs
)
2349 struct btrfs_block_group_cache
*cache
;
2350 u64 chunk_used
, user_thresh
;
2353 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2354 chunk_used
= btrfs_block_group_used(&cache
->item
);
2356 user_thresh
= div_factor_fine(cache
->key
.offset
, bargs
->usage
);
2357 if (chunk_used
< user_thresh
)
2360 btrfs_put_block_group(cache
);
2364 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2365 struct btrfs_chunk
*chunk
,
2366 struct btrfs_balance_args
*bargs
)
2368 struct btrfs_stripe
*stripe
;
2369 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2372 for (i
= 0; i
< num_stripes
; i
++) {
2373 stripe
= btrfs_stripe_nr(chunk
, i
);
2374 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2381 /* [pstart, pend) */
2382 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2383 struct btrfs_chunk
*chunk
,
2385 struct btrfs_balance_args
*bargs
)
2387 struct btrfs_stripe
*stripe
;
2388 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2394 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2397 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2398 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
))
2402 factor
= num_stripes
/ factor
;
2404 for (i
= 0; i
< num_stripes
; i
++) {
2405 stripe
= btrfs_stripe_nr(chunk
, i
);
2406 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2409 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2410 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2411 do_div(stripe_length
, factor
);
2413 if (stripe_offset
< bargs
->pend
&&
2414 stripe_offset
+ stripe_length
> bargs
->pstart
)
2421 /* [vstart, vend) */
2422 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2423 struct btrfs_chunk
*chunk
,
2425 struct btrfs_balance_args
*bargs
)
2427 if (chunk_offset
< bargs
->vend
&&
2428 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2429 /* at least part of the chunk is inside this vrange */
2435 static int chunk_soft_convert_filter(u64 chunk_type
,
2436 struct btrfs_balance_args
*bargs
)
2438 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2441 chunk_type
= chunk_to_extended(chunk_type
) &
2442 BTRFS_EXTENDED_PROFILE_MASK
;
2444 if (bargs
->target
== chunk_type
)
2450 static int should_balance_chunk(struct btrfs_root
*root
,
2451 struct extent_buffer
*leaf
,
2452 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2454 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2455 struct btrfs_balance_args
*bargs
= NULL
;
2456 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2459 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2460 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2464 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2465 bargs
= &bctl
->data
;
2466 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2468 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2469 bargs
= &bctl
->meta
;
2471 /* profiles filter */
2472 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2473 chunk_profiles_filter(chunk_type
, bargs
)) {
2478 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2479 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2484 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2485 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2489 /* drange filter, makes sense only with devid filter */
2490 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2491 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2496 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2497 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2501 /* soft profile changing mode */
2502 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2503 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2510 static u64
div_factor(u64 num
, int factor
)
2519 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2521 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2522 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2523 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2524 struct list_head
*devices
;
2525 struct btrfs_device
*device
;
2528 struct btrfs_chunk
*chunk
;
2529 struct btrfs_path
*path
;
2530 struct btrfs_key key
;
2531 struct btrfs_key found_key
;
2532 struct btrfs_trans_handle
*trans
;
2533 struct extent_buffer
*leaf
;
2536 int enospc_errors
= 0;
2537 bool counting
= true;
2539 /* step one make some room on all the devices */
2540 devices
= &fs_info
->fs_devices
->devices
;
2541 list_for_each_entry(device
, devices
, dev_list
) {
2542 old_size
= device
->total_bytes
;
2543 size_to_free
= div_factor(old_size
, 1);
2544 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2545 if (!device
->writeable
||
2546 device
->total_bytes
- device
->bytes_used
> size_to_free
)
2549 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2554 trans
= btrfs_start_transaction(dev_root
, 0);
2555 BUG_ON(IS_ERR(trans
));
2557 ret
= btrfs_grow_device(trans
, device
, old_size
);
2560 btrfs_end_transaction(trans
, dev_root
);
2563 /* step two, relocate all the chunks */
2564 path
= btrfs_alloc_path();
2570 /* zero out stat counters */
2571 spin_lock(&fs_info
->balance_lock
);
2572 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2573 spin_unlock(&fs_info
->balance_lock
);
2575 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2576 key
.offset
= (u64
)-1;
2577 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2580 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2581 atomic_read(&fs_info
->balance_cancel_req
)) {
2586 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2591 * this shouldn't happen, it means the last relocate
2595 BUG(); /* FIXME break ? */
2597 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2598 BTRFS_CHUNK_ITEM_KEY
);
2604 leaf
= path
->nodes
[0];
2605 slot
= path
->slots
[0];
2606 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2608 if (found_key
.objectid
!= key
.objectid
)
2611 /* chunk zero is special */
2612 if (found_key
.offset
== 0)
2615 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2618 spin_lock(&fs_info
->balance_lock
);
2619 bctl
->stat
.considered
++;
2620 spin_unlock(&fs_info
->balance_lock
);
2623 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2625 btrfs_release_path(path
);
2630 spin_lock(&fs_info
->balance_lock
);
2631 bctl
->stat
.expected
++;
2632 spin_unlock(&fs_info
->balance_lock
);
2636 ret
= btrfs_relocate_chunk(chunk_root
,
2637 chunk_root
->root_key
.objectid
,
2640 if (ret
&& ret
!= -ENOSPC
)
2642 if (ret
== -ENOSPC
) {
2645 spin_lock(&fs_info
->balance_lock
);
2646 bctl
->stat
.completed
++;
2647 spin_unlock(&fs_info
->balance_lock
);
2650 key
.offset
= found_key
.offset
- 1;
2654 btrfs_release_path(path
);
2659 btrfs_free_path(path
);
2660 if (enospc_errors
) {
2661 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2671 * alloc_profile_is_valid - see if a given profile is valid and reduced
2672 * @flags: profile to validate
2673 * @extended: if true @flags is treated as an extended profile
2675 static int alloc_profile_is_valid(u64 flags
, int extended
)
2677 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
2678 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
2680 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
2682 /* 1) check that all other bits are zeroed */
2686 /* 2) see if profile is reduced */
2688 return !extended
; /* "0" is valid for usual profiles */
2690 /* true if exactly one bit set */
2691 return (flags
& (flags
- 1)) == 0;
2694 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
2696 /* cancel requested || normal exit path */
2697 return atomic_read(&fs_info
->balance_cancel_req
) ||
2698 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
2699 atomic_read(&fs_info
->balance_cancel_req
) == 0);
2702 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
2706 unset_balance_control(fs_info
);
2707 ret
= del_balance_item(fs_info
->tree_root
);
2711 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
2712 struct btrfs_ioctl_balance_args
*bargs
);
2715 * Should be called with both balance and volume mutexes held
2717 int btrfs_balance(struct btrfs_balance_control
*bctl
,
2718 struct btrfs_ioctl_balance_args
*bargs
)
2720 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2725 if (btrfs_fs_closing(fs_info
) ||
2726 atomic_read(&fs_info
->balance_pause_req
) ||
2727 atomic_read(&fs_info
->balance_cancel_req
)) {
2732 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
2733 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
2737 * In case of mixed groups both data and meta should be picked,
2738 * and identical options should be given for both of them.
2740 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
2741 if (mixed
&& (bctl
->flags
& allowed
)) {
2742 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
2743 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
2744 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
2745 printk(KERN_ERR
"btrfs: with mixed groups data and "
2746 "metadata balance options must be the same\n");
2752 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
2753 if (fs_info
->fs_devices
->num_devices
== 1)
2754 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
2755 else if (fs_info
->fs_devices
->num_devices
< 4)
2756 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
2758 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
2759 BTRFS_BLOCK_GROUP_RAID10
);
2761 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2762 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
2763 (bctl
->data
.target
& ~allowed
))) {
2764 printk(KERN_ERR
"btrfs: unable to start balance with target "
2765 "data profile %llu\n",
2766 (unsigned long long)bctl
->data
.target
);
2770 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2771 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
2772 (bctl
->meta
.target
& ~allowed
))) {
2773 printk(KERN_ERR
"btrfs: unable to start balance with target "
2774 "metadata profile %llu\n",
2775 (unsigned long long)bctl
->meta
.target
);
2779 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2780 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
2781 (bctl
->sys
.target
& ~allowed
))) {
2782 printk(KERN_ERR
"btrfs: unable to start balance with target "
2783 "system profile %llu\n",
2784 (unsigned long long)bctl
->sys
.target
);
2789 /* allow dup'ed data chunks only in mixed mode */
2790 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2791 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
2792 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
2797 /* allow to reduce meta or sys integrity only if force set */
2798 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
2799 BTRFS_BLOCK_GROUP_RAID10
;
2800 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2801 (fs_info
->avail_system_alloc_bits
& allowed
) &&
2802 !(bctl
->sys
.target
& allowed
)) ||
2803 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
2804 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
2805 !(bctl
->meta
.target
& allowed
))) {
2806 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
2807 printk(KERN_INFO
"btrfs: force reducing metadata "
2810 printk(KERN_ERR
"btrfs: balance will reduce metadata "
2811 "integrity, use force if you want this\n");
2817 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
2818 int num_tolerated_disk_barrier_failures
;
2819 u64 target
= bctl
->sys
.target
;
2821 num_tolerated_disk_barrier_failures
=
2822 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
2823 if (num_tolerated_disk_barrier_failures
> 0 &&
2825 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
2826 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
2827 num_tolerated_disk_barrier_failures
= 0;
2828 else if (num_tolerated_disk_barrier_failures
> 1 &&
2830 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
2831 num_tolerated_disk_barrier_failures
= 1;
2833 fs_info
->num_tolerated_disk_barrier_failures
=
2834 num_tolerated_disk_barrier_failures
;
2837 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
2838 if (ret
&& ret
!= -EEXIST
)
2841 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
2842 BUG_ON(ret
== -EEXIST
);
2843 set_balance_control(bctl
);
2845 BUG_ON(ret
!= -EEXIST
);
2846 spin_lock(&fs_info
->balance_lock
);
2847 update_balance_args(bctl
);
2848 spin_unlock(&fs_info
->balance_lock
);
2851 atomic_inc(&fs_info
->balance_running
);
2852 mutex_unlock(&fs_info
->balance_mutex
);
2854 ret
= __btrfs_balance(fs_info
);
2856 mutex_lock(&fs_info
->balance_mutex
);
2857 atomic_dec(&fs_info
->balance_running
);
2860 memset(bargs
, 0, sizeof(*bargs
));
2861 update_ioctl_balance_args(fs_info
, 0, bargs
);
2864 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
2865 balance_need_close(fs_info
)) {
2866 __cancel_balance(fs_info
);
2869 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
2870 fs_info
->num_tolerated_disk_barrier_failures
=
2871 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
2874 wake_up(&fs_info
->balance_wait_q
);
2878 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
2879 __cancel_balance(fs_info
);
2885 static int balance_kthread(void *data
)
2887 struct btrfs_fs_info
*fs_info
= data
;
2890 mutex_lock(&fs_info
->volume_mutex
);
2891 mutex_lock(&fs_info
->balance_mutex
);
2893 if (fs_info
->balance_ctl
) {
2894 printk(KERN_INFO
"btrfs: continuing balance\n");
2895 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
2898 mutex_unlock(&fs_info
->balance_mutex
);
2899 mutex_unlock(&fs_info
->volume_mutex
);
2904 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
2906 struct task_struct
*tsk
;
2908 spin_lock(&fs_info
->balance_lock
);
2909 if (!fs_info
->balance_ctl
) {
2910 spin_unlock(&fs_info
->balance_lock
);
2913 spin_unlock(&fs_info
->balance_lock
);
2915 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
2916 printk(KERN_INFO
"btrfs: force skipping balance\n");
2920 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
2922 return PTR_ERR(tsk
);
2927 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
2929 struct btrfs_balance_control
*bctl
;
2930 struct btrfs_balance_item
*item
;
2931 struct btrfs_disk_balance_args disk_bargs
;
2932 struct btrfs_path
*path
;
2933 struct extent_buffer
*leaf
;
2934 struct btrfs_key key
;
2937 path
= btrfs_alloc_path();
2941 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2942 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2945 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
2948 if (ret
> 0) { /* ret = -ENOENT; */
2953 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
2959 leaf
= path
->nodes
[0];
2960 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2962 bctl
->fs_info
= fs_info
;
2963 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
2964 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
2966 btrfs_balance_data(leaf
, item
, &disk_bargs
);
2967 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
2968 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
2969 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
2970 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
2971 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
2973 mutex_lock(&fs_info
->volume_mutex
);
2974 mutex_lock(&fs_info
->balance_mutex
);
2976 set_balance_control(bctl
);
2978 mutex_unlock(&fs_info
->balance_mutex
);
2979 mutex_unlock(&fs_info
->volume_mutex
);
2981 btrfs_free_path(path
);
2985 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
2989 mutex_lock(&fs_info
->balance_mutex
);
2990 if (!fs_info
->balance_ctl
) {
2991 mutex_unlock(&fs_info
->balance_mutex
);
2995 if (atomic_read(&fs_info
->balance_running
)) {
2996 atomic_inc(&fs_info
->balance_pause_req
);
2997 mutex_unlock(&fs_info
->balance_mutex
);
2999 wait_event(fs_info
->balance_wait_q
,
3000 atomic_read(&fs_info
->balance_running
) == 0);
3002 mutex_lock(&fs_info
->balance_mutex
);
3003 /* we are good with balance_ctl ripped off from under us */
3004 BUG_ON(atomic_read(&fs_info
->balance_running
));
3005 atomic_dec(&fs_info
->balance_pause_req
);
3010 mutex_unlock(&fs_info
->balance_mutex
);
3014 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3016 mutex_lock(&fs_info
->balance_mutex
);
3017 if (!fs_info
->balance_ctl
) {
3018 mutex_unlock(&fs_info
->balance_mutex
);
3022 atomic_inc(&fs_info
->balance_cancel_req
);
3024 * if we are running just wait and return, balance item is
3025 * deleted in btrfs_balance in this case
3027 if (atomic_read(&fs_info
->balance_running
)) {
3028 mutex_unlock(&fs_info
->balance_mutex
);
3029 wait_event(fs_info
->balance_wait_q
,
3030 atomic_read(&fs_info
->balance_running
) == 0);
3031 mutex_lock(&fs_info
->balance_mutex
);
3033 /* __cancel_balance needs volume_mutex */
3034 mutex_unlock(&fs_info
->balance_mutex
);
3035 mutex_lock(&fs_info
->volume_mutex
);
3036 mutex_lock(&fs_info
->balance_mutex
);
3038 if (fs_info
->balance_ctl
)
3039 __cancel_balance(fs_info
);
3041 mutex_unlock(&fs_info
->volume_mutex
);
3044 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3045 atomic_dec(&fs_info
->balance_cancel_req
);
3046 mutex_unlock(&fs_info
->balance_mutex
);
3051 * shrinking a device means finding all of the device extents past
3052 * the new size, and then following the back refs to the chunks.
3053 * The chunk relocation code actually frees the device extent
3055 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3057 struct btrfs_trans_handle
*trans
;
3058 struct btrfs_root
*root
= device
->dev_root
;
3059 struct btrfs_dev_extent
*dev_extent
= NULL
;
3060 struct btrfs_path
*path
;
3068 bool retried
= false;
3069 struct extent_buffer
*l
;
3070 struct btrfs_key key
;
3071 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3072 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3073 u64 old_size
= device
->total_bytes
;
3074 u64 diff
= device
->total_bytes
- new_size
;
3076 if (new_size
>= device
->total_bytes
)
3079 path
= btrfs_alloc_path();
3087 device
->total_bytes
= new_size
;
3088 if (device
->writeable
) {
3089 device
->fs_devices
->total_rw_bytes
-= diff
;
3090 spin_lock(&root
->fs_info
->free_chunk_lock
);
3091 root
->fs_info
->free_chunk_space
-= diff
;
3092 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3094 unlock_chunks(root
);
3097 key
.objectid
= device
->devid
;
3098 key
.offset
= (u64
)-1;
3099 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3102 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3106 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3111 btrfs_release_path(path
);
3116 slot
= path
->slots
[0];
3117 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3119 if (key
.objectid
!= device
->devid
) {
3120 btrfs_release_path(path
);
3124 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3125 length
= btrfs_dev_extent_length(l
, dev_extent
);
3127 if (key
.offset
+ length
<= new_size
) {
3128 btrfs_release_path(path
);
3132 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3133 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3134 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3135 btrfs_release_path(path
);
3137 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3139 if (ret
&& ret
!= -ENOSPC
)
3143 } while (key
.offset
-- > 0);
3145 if (failed
&& !retried
) {
3149 } else if (failed
&& retried
) {
3153 device
->total_bytes
= old_size
;
3154 if (device
->writeable
)
3155 device
->fs_devices
->total_rw_bytes
+= diff
;
3156 spin_lock(&root
->fs_info
->free_chunk_lock
);
3157 root
->fs_info
->free_chunk_space
+= diff
;
3158 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3159 unlock_chunks(root
);
3163 /* Shrinking succeeded, else we would be at "done". */
3164 trans
= btrfs_start_transaction(root
, 0);
3165 if (IS_ERR(trans
)) {
3166 ret
= PTR_ERR(trans
);
3172 device
->disk_total_bytes
= new_size
;
3173 /* Now btrfs_update_device() will change the on-disk size. */
3174 ret
= btrfs_update_device(trans
, device
);
3176 unlock_chunks(root
);
3177 btrfs_end_transaction(trans
, root
);
3180 WARN_ON(diff
> old_total
);
3181 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3182 unlock_chunks(root
);
3183 btrfs_end_transaction(trans
, root
);
3185 btrfs_free_path(path
);
3189 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3190 struct btrfs_key
*key
,
3191 struct btrfs_chunk
*chunk
, int item_size
)
3193 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3194 struct btrfs_disk_key disk_key
;
3198 array_size
= btrfs_super_sys_array_size(super_copy
);
3199 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3202 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3203 btrfs_cpu_key_to_disk(&disk_key
, key
);
3204 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3205 ptr
+= sizeof(disk_key
);
3206 memcpy(ptr
, chunk
, item_size
);
3207 item_size
+= sizeof(disk_key
);
3208 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3213 * sort the devices in descending order by max_avail, total_avail
3215 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3217 const struct btrfs_device_info
*di_a
= a
;
3218 const struct btrfs_device_info
*di_b
= b
;
3220 if (di_a
->max_avail
> di_b
->max_avail
)
3222 if (di_a
->max_avail
< di_b
->max_avail
)
3224 if (di_a
->total_avail
> di_b
->total_avail
)
3226 if (di_a
->total_avail
< di_b
->total_avail
)
3231 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3232 struct btrfs_root
*extent_root
,
3233 struct map_lookup
**map_ret
,
3234 u64
*num_bytes_out
, u64
*stripe_size_out
,
3235 u64 start
, u64 type
)
3237 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3238 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3239 struct list_head
*cur
;
3240 struct map_lookup
*map
= NULL
;
3241 struct extent_map_tree
*em_tree
;
3242 struct extent_map
*em
;
3243 struct btrfs_device_info
*devices_info
= NULL
;
3245 int num_stripes
; /* total number of stripes to allocate */
3246 int sub_stripes
; /* sub_stripes info for map */
3247 int dev_stripes
; /* stripes per dev */
3248 int devs_max
; /* max devs to use */
3249 int devs_min
; /* min devs needed */
3250 int devs_increment
; /* ndevs has to be a multiple of this */
3251 int ncopies
; /* how many copies to data has */
3253 u64 max_stripe_size
;
3261 BUG_ON(!alloc_profile_is_valid(type
, 0));
3263 if (list_empty(&fs_devices
->alloc_list
))
3270 devs_max
= 0; /* 0 == as many as possible */
3274 * define the properties of each RAID type.
3275 * FIXME: move this to a global table and use it in all RAID
3278 if (type
& (BTRFS_BLOCK_GROUP_DUP
)) {
3282 } else if (type
& (BTRFS_BLOCK_GROUP_RAID0
)) {
3284 } else if (type
& (BTRFS_BLOCK_GROUP_RAID1
)) {
3289 } else if (type
& (BTRFS_BLOCK_GROUP_RAID10
)) {
3298 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3299 max_stripe_size
= 1024 * 1024 * 1024;
3300 max_chunk_size
= 10 * max_stripe_size
;
3301 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3302 /* for larger filesystems, use larger metadata chunks */
3303 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3304 max_stripe_size
= 1024 * 1024 * 1024;
3306 max_stripe_size
= 256 * 1024 * 1024;
3307 max_chunk_size
= max_stripe_size
;
3308 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3309 max_stripe_size
= 32 * 1024 * 1024;
3310 max_chunk_size
= 2 * max_stripe_size
;
3312 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3317 /* we don't want a chunk larger than 10% of writeable space */
3318 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3321 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3326 cur
= fs_devices
->alloc_list
.next
;
3329 * in the first pass through the devices list, we gather information
3330 * about the available holes on each device.
3333 while (cur
!= &fs_devices
->alloc_list
) {
3334 struct btrfs_device
*device
;
3338 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3342 if (!device
->writeable
) {
3344 "btrfs: read-only device in alloc_list\n");
3349 if (!device
->in_fs_metadata
)
3352 if (device
->total_bytes
> device
->bytes_used
)
3353 total_avail
= device
->total_bytes
- device
->bytes_used
;
3357 /* If there is no space on this device, skip it. */
3358 if (total_avail
== 0)
3361 ret
= find_free_dev_extent(device
,
3362 max_stripe_size
* dev_stripes
,
3363 &dev_offset
, &max_avail
);
3364 if (ret
&& ret
!= -ENOSPC
)
3368 max_avail
= max_stripe_size
* dev_stripes
;
3370 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3373 devices_info
[ndevs
].dev_offset
= dev_offset
;
3374 devices_info
[ndevs
].max_avail
= max_avail
;
3375 devices_info
[ndevs
].total_avail
= total_avail
;
3376 devices_info
[ndevs
].dev
= device
;
3381 * now sort the devices by hole size / available space
3383 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3384 btrfs_cmp_device_info
, NULL
);
3386 /* round down to number of usable stripes */
3387 ndevs
-= ndevs
% devs_increment
;
3389 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3394 if (devs_max
&& ndevs
> devs_max
)
3397 * the primary goal is to maximize the number of stripes, so use as many
3398 * devices as possible, even if the stripes are not maximum sized.
3400 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3401 num_stripes
= ndevs
* dev_stripes
;
3403 if (stripe_size
* ndevs
> max_chunk_size
* ncopies
) {
3404 stripe_size
= max_chunk_size
* ncopies
;
3405 do_div(stripe_size
, ndevs
);
3408 do_div(stripe_size
, dev_stripes
);
3410 /* align to BTRFS_STRIPE_LEN */
3411 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
3412 stripe_size
*= BTRFS_STRIPE_LEN
;
3414 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3419 map
->num_stripes
= num_stripes
;
3421 for (i
= 0; i
< ndevs
; ++i
) {
3422 for (j
= 0; j
< dev_stripes
; ++j
) {
3423 int s
= i
* dev_stripes
+ j
;
3424 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3425 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3429 map
->sector_size
= extent_root
->sectorsize
;
3430 map
->stripe_len
= BTRFS_STRIPE_LEN
;
3431 map
->io_align
= BTRFS_STRIPE_LEN
;
3432 map
->io_width
= BTRFS_STRIPE_LEN
;
3434 map
->sub_stripes
= sub_stripes
;
3437 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
3439 *stripe_size_out
= stripe_size
;
3440 *num_bytes_out
= num_bytes
;
3442 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3444 em
= alloc_extent_map();
3449 em
->bdev
= (struct block_device
*)map
;
3451 em
->len
= num_bytes
;
3452 em
->block_start
= 0;
3453 em
->block_len
= em
->len
;
3455 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3456 write_lock(&em_tree
->lock
);
3457 ret
= add_extent_mapping(em_tree
, em
);
3458 write_unlock(&em_tree
->lock
);
3459 free_extent_map(em
);
3463 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3464 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3469 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3470 struct btrfs_device
*device
;
3473 device
= map
->stripes
[i
].dev
;
3474 dev_offset
= map
->stripes
[i
].physical
;
3476 ret
= btrfs_alloc_dev_extent(trans
, device
,
3477 info
->chunk_root
->root_key
.objectid
,
3478 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3479 start
, dev_offset
, stripe_size
);
3481 btrfs_abort_transaction(trans
, extent_root
, ret
);
3486 kfree(devices_info
);
3491 kfree(devices_info
);
3495 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3496 struct btrfs_root
*extent_root
,
3497 struct map_lookup
*map
, u64 chunk_offset
,
3498 u64 chunk_size
, u64 stripe_size
)
3501 struct btrfs_key key
;
3502 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3503 struct btrfs_device
*device
;
3504 struct btrfs_chunk
*chunk
;
3505 struct btrfs_stripe
*stripe
;
3506 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3510 chunk
= kzalloc(item_size
, GFP_NOFS
);
3515 while (index
< map
->num_stripes
) {
3516 device
= map
->stripes
[index
].dev
;
3517 device
->bytes_used
+= stripe_size
;
3518 ret
= btrfs_update_device(trans
, device
);
3524 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3525 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3527 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3530 stripe
= &chunk
->stripe
;
3531 while (index
< map
->num_stripes
) {
3532 device
= map
->stripes
[index
].dev
;
3533 dev_offset
= map
->stripes
[index
].physical
;
3535 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3536 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3537 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3542 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3543 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3544 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3545 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3546 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3547 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3548 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3549 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3550 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3552 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3553 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3554 key
.offset
= chunk_offset
;
3556 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3558 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3560 * TODO: Cleanup of inserted chunk root in case of
3563 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
3573 * Chunk allocation falls into two parts. The first part does works
3574 * that make the new allocated chunk useable, but not do any operation
3575 * that modifies the chunk tree. The second part does the works that
3576 * require modifying the chunk tree. This division is important for the
3577 * bootstrap process of adding storage to a seed btrfs.
3579 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3580 struct btrfs_root
*extent_root
, u64 type
)
3585 struct map_lookup
*map
;
3586 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3589 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3594 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3595 &stripe_size
, chunk_offset
, type
);
3599 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3600 chunk_size
, stripe_size
);
3606 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3607 struct btrfs_root
*root
,
3608 struct btrfs_device
*device
)
3611 u64 sys_chunk_offset
;
3615 u64 sys_stripe_size
;
3617 struct map_lookup
*map
;
3618 struct map_lookup
*sys_map
;
3619 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3620 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3623 ret
= find_next_chunk(fs_info
->chunk_root
,
3624 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3628 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3629 fs_info
->avail_metadata_alloc_bits
;
3630 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3632 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3633 &stripe_size
, chunk_offset
, alloc_profile
);
3637 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3639 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3640 fs_info
->avail_system_alloc_bits
;
3641 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3643 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3644 &sys_chunk_size
, &sys_stripe_size
,
3645 sys_chunk_offset
, alloc_profile
);
3647 btrfs_abort_transaction(trans
, root
, ret
);
3651 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3653 btrfs_abort_transaction(trans
, root
, ret
);
3658 * Modifying chunk tree needs allocating new blocks from both
3659 * system block group and metadata block group. So we only can
3660 * do operations require modifying the chunk tree after both
3661 * block groups were created.
3663 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3664 chunk_size
, stripe_size
);
3666 btrfs_abort_transaction(trans
, root
, ret
);
3670 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
3671 sys_chunk_offset
, sys_chunk_size
,
3674 btrfs_abort_transaction(trans
, root
, ret
);
3681 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
3683 struct extent_map
*em
;
3684 struct map_lookup
*map
;
3685 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3689 read_lock(&map_tree
->map_tree
.lock
);
3690 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3691 read_unlock(&map_tree
->map_tree
.lock
);
3695 if (btrfs_test_opt(root
, DEGRADED
)) {
3696 free_extent_map(em
);
3700 map
= (struct map_lookup
*)em
->bdev
;
3701 for (i
= 0; i
< map
->num_stripes
; i
++) {
3702 if (!map
->stripes
[i
].dev
->writeable
) {
3707 free_extent_map(em
);
3711 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
3713 extent_map_tree_init(&tree
->map_tree
);
3716 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
3718 struct extent_map
*em
;
3721 write_lock(&tree
->map_tree
.lock
);
3722 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
3724 remove_extent_mapping(&tree
->map_tree
, em
);
3725 write_unlock(&tree
->map_tree
.lock
);
3730 free_extent_map(em
);
3731 /* once for the tree */
3732 free_extent_map(em
);
3736 int btrfs_num_copies(struct btrfs_mapping_tree
*map_tree
, u64 logical
, u64 len
)
3738 struct extent_map
*em
;
3739 struct map_lookup
*map
;
3740 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3743 read_lock(&em_tree
->lock
);
3744 em
= lookup_extent_mapping(em_tree
, logical
, len
);
3745 read_unlock(&em_tree
->lock
);
3748 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3749 map
= (struct map_lookup
*)em
->bdev
;
3750 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
3751 ret
= map
->num_stripes
;
3752 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
3753 ret
= map
->sub_stripes
;
3756 free_extent_map(em
);
3760 static int find_live_mirror(struct map_lookup
*map
, int first
, int num
,
3764 if (map
->stripes
[optimal
].dev
->bdev
)
3766 for (i
= first
; i
< first
+ num
; i
++) {
3767 if (map
->stripes
[i
].dev
->bdev
)
3770 /* we couldn't find one that doesn't fail. Just return something
3771 * and the io error handling code will clean up eventually
3776 static int __btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
3777 u64 logical
, u64
*length
,
3778 struct btrfs_bio
**bbio_ret
,
3781 struct extent_map
*em
;
3782 struct map_lookup
*map
;
3783 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3786 u64 stripe_end_offset
;
3795 struct btrfs_bio
*bbio
= NULL
;
3797 read_lock(&em_tree
->lock
);
3798 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
3799 read_unlock(&em_tree
->lock
);
3802 printk(KERN_CRIT
"btrfs: unable to find logical %llu len %llu\n",
3803 (unsigned long long)logical
,
3804 (unsigned long long)*length
);
3808 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
3809 map
= (struct map_lookup
*)em
->bdev
;
3810 offset
= logical
- em
->start
;
3812 if (mirror_num
> map
->num_stripes
)
3817 * stripe_nr counts the total number of stripes we have to stride
3818 * to get to this block
3820 do_div(stripe_nr
, map
->stripe_len
);
3822 stripe_offset
= stripe_nr
* map
->stripe_len
;
3823 BUG_ON(offset
< stripe_offset
);
3825 /* stripe_offset is the offset of this block in its stripe*/
3826 stripe_offset
= offset
- stripe_offset
;
3828 if (rw
& REQ_DISCARD
)
3829 *length
= min_t(u64
, em
->len
- offset
, *length
);
3830 else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
3831 /* we limit the length of each bio to what fits in a stripe */
3832 *length
= min_t(u64
, em
->len
- offset
,
3833 map
->stripe_len
- stripe_offset
);
3835 *length
= em
->len
- offset
;
3843 stripe_nr_orig
= stripe_nr
;
3844 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
3845 (~(map
->stripe_len
- 1));
3846 do_div(stripe_nr_end
, map
->stripe_len
);
3847 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
3849 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
3850 if (rw
& REQ_DISCARD
)
3851 num_stripes
= min_t(u64
, map
->num_stripes
,
3852 stripe_nr_end
- stripe_nr_orig
);
3853 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3854 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
3855 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
3856 num_stripes
= map
->num_stripes
;
3857 else if (mirror_num
)
3858 stripe_index
= mirror_num
- 1;
3860 stripe_index
= find_live_mirror(map
, 0,
3862 current
->pid
% map
->num_stripes
);
3863 mirror_num
= stripe_index
+ 1;
3866 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
3867 if (rw
& (REQ_WRITE
| REQ_DISCARD
)) {
3868 num_stripes
= map
->num_stripes
;
3869 } else if (mirror_num
) {
3870 stripe_index
= mirror_num
- 1;
3875 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
3876 int factor
= map
->num_stripes
/ map
->sub_stripes
;
3878 stripe_index
= do_div(stripe_nr
, factor
);
3879 stripe_index
*= map
->sub_stripes
;
3882 num_stripes
= map
->sub_stripes
;
3883 else if (rw
& REQ_DISCARD
)
3884 num_stripes
= min_t(u64
, map
->sub_stripes
*
3885 (stripe_nr_end
- stripe_nr_orig
),
3887 else if (mirror_num
)
3888 stripe_index
+= mirror_num
- 1;
3890 int old_stripe_index
= stripe_index
;
3891 stripe_index
= find_live_mirror(map
, stripe_index
,
3892 map
->sub_stripes
, stripe_index
+
3893 current
->pid
% map
->sub_stripes
);
3894 mirror_num
= stripe_index
- old_stripe_index
+ 1;
3898 * after this do_div call, stripe_nr is the number of stripes
3899 * on this device we have to walk to find the data, and
3900 * stripe_index is the number of our device in the stripe array
3902 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
3903 mirror_num
= stripe_index
+ 1;
3905 BUG_ON(stripe_index
>= map
->num_stripes
);
3907 bbio
= kzalloc(btrfs_bio_size(num_stripes
), GFP_NOFS
);
3912 atomic_set(&bbio
->error
, 0);
3914 if (rw
& REQ_DISCARD
) {
3916 int sub_stripes
= 0;
3917 u64 stripes_per_dev
= 0;
3918 u32 remaining_stripes
= 0;
3919 u32 last_stripe
= 0;
3922 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
3923 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
3926 sub_stripes
= map
->sub_stripes
;
3928 factor
= map
->num_stripes
/ sub_stripes
;
3929 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
3932 &remaining_stripes
);
3933 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
3934 last_stripe
*= sub_stripes
;
3937 for (i
= 0; i
< num_stripes
; i
++) {
3938 bbio
->stripes
[i
].physical
=
3939 map
->stripes
[stripe_index
].physical
+
3940 stripe_offset
+ stripe_nr
* map
->stripe_len
;
3941 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
3943 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
3944 BTRFS_BLOCK_GROUP_RAID10
)) {
3945 bbio
->stripes
[i
].length
= stripes_per_dev
*
3948 if (i
/ sub_stripes
< remaining_stripes
)
3949 bbio
->stripes
[i
].length
+=
3953 * Special for the first stripe and
3956 * |-------|...|-------|
3960 if (i
< sub_stripes
)
3961 bbio
->stripes
[i
].length
-=
3964 if (stripe_index
>= last_stripe
&&
3965 stripe_index
<= (last_stripe
+
3967 bbio
->stripes
[i
].length
-=
3970 if (i
== sub_stripes
- 1)
3973 bbio
->stripes
[i
].length
= *length
;
3976 if (stripe_index
== map
->num_stripes
) {
3977 /* This could only happen for RAID0/10 */
3983 for (i
= 0; i
< num_stripes
; i
++) {
3984 bbio
->stripes
[i
].physical
=
3985 map
->stripes
[stripe_index
].physical
+
3987 stripe_nr
* map
->stripe_len
;
3988 bbio
->stripes
[i
].dev
=
3989 map
->stripes
[stripe_index
].dev
;
3994 if (rw
& REQ_WRITE
) {
3995 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
3996 BTRFS_BLOCK_GROUP_RAID10
|
3997 BTRFS_BLOCK_GROUP_DUP
)) {
4003 bbio
->num_stripes
= num_stripes
;
4004 bbio
->max_errors
= max_errors
;
4005 bbio
->mirror_num
= mirror_num
;
4007 free_extent_map(em
);
4011 int btrfs_map_block(struct btrfs_mapping_tree
*map_tree
, int rw
,
4012 u64 logical
, u64
*length
,
4013 struct btrfs_bio
**bbio_ret
, int mirror_num
)
4015 return __btrfs_map_block(map_tree
, rw
, logical
, length
, bbio_ret
,
4019 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
4020 u64 chunk_start
, u64 physical
, u64 devid
,
4021 u64
**logical
, int *naddrs
, int *stripe_len
)
4023 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4024 struct extent_map
*em
;
4025 struct map_lookup
*map
;
4032 read_lock(&em_tree
->lock
);
4033 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
4034 read_unlock(&em_tree
->lock
);
4036 BUG_ON(!em
|| em
->start
!= chunk_start
);
4037 map
= (struct map_lookup
*)em
->bdev
;
4040 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4041 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
4042 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4043 do_div(length
, map
->num_stripes
);
4045 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
4046 BUG_ON(!buf
); /* -ENOMEM */
4048 for (i
= 0; i
< map
->num_stripes
; i
++) {
4049 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
4051 if (map
->stripes
[i
].physical
> physical
||
4052 map
->stripes
[i
].physical
+ length
<= physical
)
4055 stripe_nr
= physical
- map
->stripes
[i
].physical
;
4056 do_div(stripe_nr
, map
->stripe_len
);
4058 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4059 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4060 do_div(stripe_nr
, map
->sub_stripes
);
4061 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4062 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4064 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
4065 WARN_ON(nr
>= map
->num_stripes
);
4066 for (j
= 0; j
< nr
; j
++) {
4067 if (buf
[j
] == bytenr
)
4071 WARN_ON(nr
>= map
->num_stripes
);
4078 *stripe_len
= map
->stripe_len
;
4080 free_extent_map(em
);
4084 static void *merge_stripe_index_into_bio_private(void *bi_private
,
4085 unsigned int stripe_index
)
4088 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4090 * The alternative solution (instead of stealing bits from the
4091 * pointer) would be to allocate an intermediate structure
4092 * that contains the old private pointer plus the stripe_index.
4094 BUG_ON((((uintptr_t)bi_private
) & 3) != 0);
4095 BUG_ON(stripe_index
> 3);
4096 return (void *)(((uintptr_t)bi_private
) | stripe_index
);
4099 static struct btrfs_bio
*extract_bbio_from_bio_private(void *bi_private
)
4101 return (struct btrfs_bio
*)(((uintptr_t)bi_private
) & ~((uintptr_t)3));
4104 static unsigned int extract_stripe_index_from_bio_private(void *bi_private
)
4106 return (unsigned int)((uintptr_t)bi_private
) & 3;
4109 static void btrfs_end_bio(struct bio
*bio
, int err
)
4111 struct btrfs_bio
*bbio
= extract_bbio_from_bio_private(bio
->bi_private
);
4112 int is_orig_bio
= 0;
4115 atomic_inc(&bbio
->error
);
4116 if (err
== -EIO
|| err
== -EREMOTEIO
) {
4117 unsigned int stripe_index
=
4118 extract_stripe_index_from_bio_private(
4120 struct btrfs_device
*dev
;
4122 BUG_ON(stripe_index
>= bbio
->num_stripes
);
4123 dev
= bbio
->stripes
[stripe_index
].dev
;
4125 if (bio
->bi_rw
& WRITE
)
4126 btrfs_dev_stat_inc(dev
,
4127 BTRFS_DEV_STAT_WRITE_ERRS
);
4129 btrfs_dev_stat_inc(dev
,
4130 BTRFS_DEV_STAT_READ_ERRS
);
4131 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
4132 btrfs_dev_stat_inc(dev
,
4133 BTRFS_DEV_STAT_FLUSH_ERRS
);
4134 btrfs_dev_stat_print_on_error(dev
);
4139 if (bio
== bbio
->orig_bio
)
4142 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4145 bio
= bbio
->orig_bio
;
4147 bio
->bi_private
= bbio
->private;
4148 bio
->bi_end_io
= bbio
->end_io
;
4149 bio
->bi_bdev
= (struct block_device
*)
4150 (unsigned long)bbio
->mirror_num
;
4151 /* only send an error to the higher layers if it is
4152 * beyond the tolerance of the multi-bio
4154 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
4158 * this bio is actually up to date, we didn't
4159 * go over the max number of errors
4161 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
4166 bio_endio(bio
, err
);
4167 } else if (!is_orig_bio
) {
4172 struct async_sched
{
4175 struct btrfs_fs_info
*info
;
4176 struct btrfs_work work
;
4180 * see run_scheduled_bios for a description of why bios are collected for
4183 * This will add one bio to the pending list for a device and make sure
4184 * the work struct is scheduled.
4186 static noinline
void schedule_bio(struct btrfs_root
*root
,
4187 struct btrfs_device
*device
,
4188 int rw
, struct bio
*bio
)
4190 int should_queue
= 1;
4191 struct btrfs_pending_bios
*pending_bios
;
4193 /* don't bother with additional async steps for reads, right now */
4194 if (!(rw
& REQ_WRITE
)) {
4196 btrfsic_submit_bio(rw
, bio
);
4202 * nr_async_bios allows us to reliably return congestion to the
4203 * higher layers. Otherwise, the async bio makes it appear we have
4204 * made progress against dirty pages when we've really just put it
4205 * on a queue for later
4207 atomic_inc(&root
->fs_info
->nr_async_bios
);
4208 WARN_ON(bio
->bi_next
);
4209 bio
->bi_next
= NULL
;
4212 spin_lock(&device
->io_lock
);
4213 if (bio
->bi_rw
& REQ_SYNC
)
4214 pending_bios
= &device
->pending_sync_bios
;
4216 pending_bios
= &device
->pending_bios
;
4218 if (pending_bios
->tail
)
4219 pending_bios
->tail
->bi_next
= bio
;
4221 pending_bios
->tail
= bio
;
4222 if (!pending_bios
->head
)
4223 pending_bios
->head
= bio
;
4224 if (device
->running_pending
)
4227 spin_unlock(&device
->io_lock
);
4230 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
4234 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
4235 int mirror_num
, int async_submit
)
4237 struct btrfs_mapping_tree
*map_tree
;
4238 struct btrfs_device
*dev
;
4239 struct bio
*first_bio
= bio
;
4240 u64 logical
= (u64
)bio
->bi_sector
<< 9;
4246 struct btrfs_bio
*bbio
= NULL
;
4248 length
= bio
->bi_size
;
4249 map_tree
= &root
->fs_info
->mapping_tree
;
4250 map_length
= length
;
4252 ret
= btrfs_map_block(map_tree
, rw
, logical
, &map_length
, &bbio
,
4254 if (ret
) /* -ENOMEM */
4257 total_devs
= bbio
->num_stripes
;
4258 if (map_length
< length
) {
4259 printk(KERN_CRIT
"btrfs: mapping failed logical %llu bio len %llu "
4260 "len %llu\n", (unsigned long long)logical
,
4261 (unsigned long long)length
,
4262 (unsigned long long)map_length
);
4266 bbio
->orig_bio
= first_bio
;
4267 bbio
->private = first_bio
->bi_private
;
4268 bbio
->end_io
= first_bio
->bi_end_io
;
4269 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
4271 while (dev_nr
< total_devs
) {
4272 if (dev_nr
< total_devs
- 1) {
4273 bio
= bio_clone(first_bio
, GFP_NOFS
);
4274 BUG_ON(!bio
); /* -ENOMEM */
4278 bio
->bi_private
= bbio
;
4279 bio
->bi_private
= merge_stripe_index_into_bio_private(
4280 bio
->bi_private
, (unsigned int)dev_nr
);
4281 bio
->bi_end_io
= btrfs_end_bio
;
4282 bio
->bi_sector
= bbio
->stripes
[dev_nr
].physical
>> 9;
4283 dev
= bbio
->stripes
[dev_nr
].dev
;
4284 if (dev
&& dev
->bdev
&& (rw
!= WRITE
|| dev
->writeable
)) {
4286 struct rcu_string
*name
;
4289 name
= rcu_dereference(dev
->name
);
4290 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4291 "(%s id %llu), size=%u\n", rw
,
4292 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
4293 name
->str
, dev
->devid
, bio
->bi_size
);
4296 bio
->bi_bdev
= dev
->bdev
;
4298 schedule_bio(root
, dev
, rw
, bio
);
4300 btrfsic_submit_bio(rw
, bio
);
4302 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4303 bio
->bi_sector
= logical
>> 9;
4304 bio_endio(bio
, -EIO
);
4311 struct btrfs_device
*btrfs_find_device(struct btrfs_root
*root
, u64 devid
,
4314 struct btrfs_device
*device
;
4315 struct btrfs_fs_devices
*cur_devices
;
4317 cur_devices
= root
->fs_info
->fs_devices
;
4318 while (cur_devices
) {
4320 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4321 device
= __find_device(&cur_devices
->devices
,
4326 cur_devices
= cur_devices
->seed
;
4331 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
4332 u64 devid
, u8
*dev_uuid
)
4334 struct btrfs_device
*device
;
4335 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4337 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
4340 list_add(&device
->dev_list
,
4341 &fs_devices
->devices
);
4342 device
->dev_root
= root
->fs_info
->dev_root
;
4343 device
->devid
= devid
;
4344 device
->work
.func
= pending_bios_fn
;
4345 device
->fs_devices
= fs_devices
;
4346 device
->missing
= 1;
4347 fs_devices
->num_devices
++;
4348 fs_devices
->missing_devices
++;
4349 spin_lock_init(&device
->io_lock
);
4350 INIT_LIST_HEAD(&device
->dev_alloc_list
);
4351 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
4355 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
4356 struct extent_buffer
*leaf
,
4357 struct btrfs_chunk
*chunk
)
4359 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4360 struct map_lookup
*map
;
4361 struct extent_map
*em
;
4365 u8 uuid
[BTRFS_UUID_SIZE
];
4370 logical
= key
->offset
;
4371 length
= btrfs_chunk_length(leaf
, chunk
);
4373 read_lock(&map_tree
->map_tree
.lock
);
4374 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
4375 read_unlock(&map_tree
->map_tree
.lock
);
4377 /* already mapped? */
4378 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
4379 free_extent_map(em
);
4382 free_extent_map(em
);
4385 em
= alloc_extent_map();
4388 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
4389 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4391 free_extent_map(em
);
4395 em
->bdev
= (struct block_device
*)map
;
4396 em
->start
= logical
;
4398 em
->block_start
= 0;
4399 em
->block_len
= em
->len
;
4401 map
->num_stripes
= num_stripes
;
4402 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
4403 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
4404 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
4405 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
4406 map
->type
= btrfs_chunk_type(leaf
, chunk
);
4407 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
4408 for (i
= 0; i
< num_stripes
; i
++) {
4409 map
->stripes
[i
].physical
=
4410 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
4411 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
4412 read_extent_buffer(leaf
, uuid
, (unsigned long)
4413 btrfs_stripe_dev_uuid_nr(chunk
, i
),
4415 map
->stripes
[i
].dev
= btrfs_find_device(root
, devid
, uuid
,
4417 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
4419 free_extent_map(em
);
4422 if (!map
->stripes
[i
].dev
) {
4423 map
->stripes
[i
].dev
=
4424 add_missing_dev(root
, devid
, uuid
);
4425 if (!map
->stripes
[i
].dev
) {
4427 free_extent_map(em
);
4431 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
4434 write_lock(&map_tree
->map_tree
.lock
);
4435 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
4436 write_unlock(&map_tree
->map_tree
.lock
);
4437 BUG_ON(ret
); /* Tree corruption */
4438 free_extent_map(em
);
4443 static void fill_device_from_item(struct extent_buffer
*leaf
,
4444 struct btrfs_dev_item
*dev_item
,
4445 struct btrfs_device
*device
)
4449 device
->devid
= btrfs_device_id(leaf
, dev_item
);
4450 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
4451 device
->total_bytes
= device
->disk_total_bytes
;
4452 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
4453 device
->type
= btrfs_device_type(leaf
, dev_item
);
4454 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
4455 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
4456 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
4458 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
4459 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
4462 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
4464 struct btrfs_fs_devices
*fs_devices
;
4467 BUG_ON(!mutex_is_locked(&uuid_mutex
));
4469 fs_devices
= root
->fs_info
->fs_devices
->seed
;
4470 while (fs_devices
) {
4471 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4475 fs_devices
= fs_devices
->seed
;
4478 fs_devices
= find_fsid(fsid
);
4484 fs_devices
= clone_fs_devices(fs_devices
);
4485 if (IS_ERR(fs_devices
)) {
4486 ret
= PTR_ERR(fs_devices
);
4490 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
4491 root
->fs_info
->bdev_holder
);
4493 free_fs_devices(fs_devices
);
4497 if (!fs_devices
->seeding
) {
4498 __btrfs_close_devices(fs_devices
);
4499 free_fs_devices(fs_devices
);
4504 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
4505 root
->fs_info
->fs_devices
->seed
= fs_devices
;
4510 static int read_one_dev(struct btrfs_root
*root
,
4511 struct extent_buffer
*leaf
,
4512 struct btrfs_dev_item
*dev_item
)
4514 struct btrfs_device
*device
;
4517 u8 fs_uuid
[BTRFS_UUID_SIZE
];
4518 u8 dev_uuid
[BTRFS_UUID_SIZE
];
4520 devid
= btrfs_device_id(leaf
, dev_item
);
4521 read_extent_buffer(leaf
, dev_uuid
,
4522 (unsigned long)btrfs_device_uuid(dev_item
),
4524 read_extent_buffer(leaf
, fs_uuid
,
4525 (unsigned long)btrfs_device_fsid(dev_item
),
4528 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
4529 ret
= open_seed_devices(root
, fs_uuid
);
4530 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
4534 device
= btrfs_find_device(root
, devid
, dev_uuid
, fs_uuid
);
4535 if (!device
|| !device
->bdev
) {
4536 if (!btrfs_test_opt(root
, DEGRADED
))
4540 printk(KERN_WARNING
"warning devid %llu missing\n",
4541 (unsigned long long)devid
);
4542 device
= add_missing_dev(root
, devid
, dev_uuid
);
4545 } else if (!device
->missing
) {
4547 * this happens when a device that was properly setup
4548 * in the device info lists suddenly goes bad.
4549 * device->bdev is NULL, and so we have to set
4550 * device->missing to one here
4552 root
->fs_info
->fs_devices
->missing_devices
++;
4553 device
->missing
= 1;
4557 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
4558 BUG_ON(device
->writeable
);
4559 if (device
->generation
!=
4560 btrfs_device_generation(leaf
, dev_item
))
4564 fill_device_from_item(leaf
, dev_item
, device
);
4565 device
->dev_root
= root
->fs_info
->dev_root
;
4566 device
->in_fs_metadata
= 1;
4567 if (device
->writeable
) {
4568 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
4569 spin_lock(&root
->fs_info
->free_chunk_lock
);
4570 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
4572 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4578 int btrfs_read_sys_array(struct btrfs_root
*root
)
4580 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4581 struct extent_buffer
*sb
;
4582 struct btrfs_disk_key
*disk_key
;
4583 struct btrfs_chunk
*chunk
;
4585 unsigned long sb_ptr
;
4591 struct btrfs_key key
;
4593 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
4594 BTRFS_SUPER_INFO_SIZE
);
4597 btrfs_set_buffer_uptodate(sb
);
4598 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
4600 * The sb extent buffer is artifical and just used to read the system array.
4601 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4602 * pages up-to-date when the page is larger: extent does not cover the
4603 * whole page and consequently check_page_uptodate does not find all
4604 * the page's extents up-to-date (the hole beyond sb),
4605 * write_extent_buffer then triggers a WARN_ON.
4607 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4608 * but sb spans only this function. Add an explicit SetPageUptodate call
4609 * to silence the warning eg. on PowerPC 64.
4611 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
4612 SetPageUptodate(sb
->pages
[0]);
4614 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
4615 array_size
= btrfs_super_sys_array_size(super_copy
);
4617 ptr
= super_copy
->sys_chunk_array
;
4618 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
4621 while (cur
< array_size
) {
4622 disk_key
= (struct btrfs_disk_key
*)ptr
;
4623 btrfs_disk_key_to_cpu(&key
, disk_key
);
4625 len
= sizeof(*disk_key
); ptr
+= len
;
4629 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4630 chunk
= (struct btrfs_chunk
*)sb_ptr
;
4631 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
4634 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
4635 len
= btrfs_chunk_item_size(num_stripes
);
4644 free_extent_buffer(sb
);
4648 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
4650 struct btrfs_path
*path
;
4651 struct extent_buffer
*leaf
;
4652 struct btrfs_key key
;
4653 struct btrfs_key found_key
;
4657 root
= root
->fs_info
->chunk_root
;
4659 path
= btrfs_alloc_path();
4663 mutex_lock(&uuid_mutex
);
4666 /* first we search for all of the device items, and then we
4667 * read in all of the chunk items. This way we can create chunk
4668 * mappings that reference all of the devices that are afound
4670 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
4674 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4678 leaf
= path
->nodes
[0];
4679 slot
= path
->slots
[0];
4680 if (slot
>= btrfs_header_nritems(leaf
)) {
4681 ret
= btrfs_next_leaf(root
, path
);
4688 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
4689 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4690 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
4692 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
4693 struct btrfs_dev_item
*dev_item
;
4694 dev_item
= btrfs_item_ptr(leaf
, slot
,
4695 struct btrfs_dev_item
);
4696 ret
= read_one_dev(root
, leaf
, dev_item
);
4700 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
4701 struct btrfs_chunk
*chunk
;
4702 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
4703 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
4709 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
4711 btrfs_release_path(path
);
4716 unlock_chunks(root
);
4717 mutex_unlock(&uuid_mutex
);
4719 btrfs_free_path(path
);
4723 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
4727 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
4728 btrfs_dev_stat_reset(dev
, i
);
4731 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
4733 struct btrfs_key key
;
4734 struct btrfs_key found_key
;
4735 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
4736 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
4737 struct extent_buffer
*eb
;
4740 struct btrfs_device
*device
;
4741 struct btrfs_path
*path
= NULL
;
4744 path
= btrfs_alloc_path();
4750 mutex_lock(&fs_devices
->device_list_mutex
);
4751 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
4753 struct btrfs_dev_stats_item
*ptr
;
4756 key
.type
= BTRFS_DEV_STATS_KEY
;
4757 key
.offset
= device
->devid
;
4758 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
4760 __btrfs_reset_dev_stats(device
);
4761 device
->dev_stats_valid
= 1;
4762 btrfs_release_path(path
);
4765 slot
= path
->slots
[0];
4766 eb
= path
->nodes
[0];
4767 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4768 item_size
= btrfs_item_size_nr(eb
, slot
);
4770 ptr
= btrfs_item_ptr(eb
, slot
,
4771 struct btrfs_dev_stats_item
);
4773 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
4774 if (item_size
>= (1 + i
) * sizeof(__le64
))
4775 btrfs_dev_stat_set(device
, i
,
4776 btrfs_dev_stats_value(eb
, ptr
, i
));
4778 btrfs_dev_stat_reset(device
, i
);
4781 device
->dev_stats_valid
= 1;
4782 btrfs_dev_stat_print_on_load(device
);
4783 btrfs_release_path(path
);
4785 mutex_unlock(&fs_devices
->device_list_mutex
);
4788 btrfs_free_path(path
);
4789 return ret
< 0 ? ret
: 0;
4792 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
4793 struct btrfs_root
*dev_root
,
4794 struct btrfs_device
*device
)
4796 struct btrfs_path
*path
;
4797 struct btrfs_key key
;
4798 struct extent_buffer
*eb
;
4799 struct btrfs_dev_stats_item
*ptr
;
4804 key
.type
= BTRFS_DEV_STATS_KEY
;
4805 key
.offset
= device
->devid
;
4807 path
= btrfs_alloc_path();
4809 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
4811 printk_in_rcu(KERN_WARNING
"btrfs: error %d while searching for dev_stats item for device %s!\n",
4812 ret
, rcu_str_deref(device
->name
));
4817 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
4818 /* need to delete old one and insert a new one */
4819 ret
= btrfs_del_item(trans
, dev_root
, path
);
4821 printk_in_rcu(KERN_WARNING
"btrfs: delete too small dev_stats item for device %s failed %d!\n",
4822 rcu_str_deref(device
->name
), ret
);
4829 /* need to insert a new item */
4830 btrfs_release_path(path
);
4831 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
4832 &key
, sizeof(*ptr
));
4834 printk_in_rcu(KERN_WARNING
"btrfs: insert dev_stats item for device %s failed %d!\n",
4835 rcu_str_deref(device
->name
), ret
);
4840 eb
= path
->nodes
[0];
4841 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
4842 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
4843 btrfs_set_dev_stats_value(eb
, ptr
, i
,
4844 btrfs_dev_stat_read(device
, i
));
4845 btrfs_mark_buffer_dirty(eb
);
4848 btrfs_free_path(path
);
4853 * called from commit_transaction. Writes all changed device stats to disk.
4855 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
4856 struct btrfs_fs_info
*fs_info
)
4858 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
4859 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
4860 struct btrfs_device
*device
;
4863 mutex_lock(&fs_devices
->device_list_mutex
);
4864 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
4865 if (!device
->dev_stats_valid
|| !device
->dev_stats_dirty
)
4868 ret
= update_dev_stat_item(trans
, dev_root
, device
);
4870 device
->dev_stats_dirty
= 0;
4872 mutex_unlock(&fs_devices
->device_list_mutex
);
4877 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
4879 btrfs_dev_stat_inc(dev
, index
);
4880 btrfs_dev_stat_print_on_error(dev
);
4883 void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
4885 if (!dev
->dev_stats_valid
)
4887 printk_ratelimited_in_rcu(KERN_ERR
4888 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4889 rcu_str_deref(dev
->name
),
4890 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
4891 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
4892 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
4893 btrfs_dev_stat_read(dev
,
4894 BTRFS_DEV_STAT_CORRUPTION_ERRS
),
4895 btrfs_dev_stat_read(dev
,
4896 BTRFS_DEV_STAT_GENERATION_ERRS
));
4899 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
4903 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
4904 if (btrfs_dev_stat_read(dev
, i
) != 0)
4906 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
4907 return; /* all values == 0, suppress message */
4909 printk_in_rcu(KERN_INFO
"btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4910 rcu_str_deref(dev
->name
),
4911 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
4912 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
4913 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
4914 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
4915 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
4918 int btrfs_get_dev_stats(struct btrfs_root
*root
,
4919 struct btrfs_ioctl_get_dev_stats
*stats
)
4921 struct btrfs_device
*dev
;
4922 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4925 mutex_lock(&fs_devices
->device_list_mutex
);
4926 dev
= btrfs_find_device(root
, stats
->devid
, NULL
, NULL
);
4927 mutex_unlock(&fs_devices
->device_list_mutex
);
4931 "btrfs: get dev_stats failed, device not found\n");
4933 } else if (!dev
->dev_stats_valid
) {
4935 "btrfs: get dev_stats failed, not yet valid\n");
4937 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
4938 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
4939 if (stats
->nr_items
> i
)
4941 btrfs_dev_stat_read_and_reset(dev
, i
);
4943 btrfs_dev_stat_reset(dev
, i
);
4946 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
4947 if (stats
->nr_items
> i
)
4948 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
4950 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
4951 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;