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 <linux/raid/pq.h>
29 #include <asm/div64.h>
32 #include "extent_map.h"
34 #include "transaction.h"
35 #include "print-tree.h"
38 #include "async-thread.h"
39 #include "check-integrity.h"
40 #include "rcu-string.h"
42 #include "dev-replace.h"
44 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
45 struct btrfs_root
*root
,
46 struct btrfs_device
*device
);
47 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
48 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
49 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
51 static DEFINE_MUTEX(uuid_mutex
);
52 static LIST_HEAD(fs_uuids
);
54 static void lock_chunks(struct btrfs_root
*root
)
56 mutex_lock(&root
->fs_info
->chunk_mutex
);
59 static void unlock_chunks(struct btrfs_root
*root
)
61 mutex_unlock(&root
->fs_info
->chunk_mutex
);
64 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
66 struct btrfs_device
*device
;
67 WARN_ON(fs_devices
->opened
);
68 while (!list_empty(&fs_devices
->devices
)) {
69 device
= list_entry(fs_devices
->devices
.next
,
70 struct btrfs_device
, dev_list
);
71 list_del(&device
->dev_list
);
72 rcu_string_free(device
->name
);
78 static void btrfs_kobject_uevent(struct block_device
*bdev
,
79 enum kobject_action action
)
83 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
85 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
87 kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
88 &disk_to_dev(bdev
->bd_disk
)->kobj
);
91 void btrfs_cleanup_fs_uuids(void)
93 struct btrfs_fs_devices
*fs_devices
;
95 while (!list_empty(&fs_uuids
)) {
96 fs_devices
= list_entry(fs_uuids
.next
,
97 struct btrfs_fs_devices
, list
);
98 list_del(&fs_devices
->list
);
99 free_fs_devices(fs_devices
);
103 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
106 struct btrfs_device
*dev
;
108 list_for_each_entry(dev
, head
, dev_list
) {
109 if (dev
->devid
== devid
&&
110 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
117 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
119 struct btrfs_fs_devices
*fs_devices
;
121 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
122 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
129 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
130 int flush
, struct block_device
**bdev
,
131 struct buffer_head
**bh
)
135 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
138 ret
= PTR_ERR(*bdev
);
139 printk(KERN_INFO
"btrfs: open %s failed\n", device_path
);
144 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
145 ret
= set_blocksize(*bdev
, 4096);
147 blkdev_put(*bdev
, flags
);
150 invalidate_bdev(*bdev
);
151 *bh
= btrfs_read_dev_super(*bdev
);
154 blkdev_put(*bdev
, flags
);
166 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
167 struct bio
*head
, struct bio
*tail
)
170 struct bio
*old_head
;
172 old_head
= pending_bios
->head
;
173 pending_bios
->head
= head
;
174 if (pending_bios
->tail
)
175 tail
->bi_next
= old_head
;
177 pending_bios
->tail
= tail
;
181 * we try to collect pending bios for a device so we don't get a large
182 * number of procs sending bios down to the same device. This greatly
183 * improves the schedulers ability to collect and merge the bios.
185 * But, it also turns into a long list of bios to process and that is sure
186 * to eventually make the worker thread block. The solution here is to
187 * make some progress and then put this work struct back at the end of
188 * the list if the block device is congested. This way, multiple devices
189 * can make progress from a single worker thread.
191 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
194 struct backing_dev_info
*bdi
;
195 struct btrfs_fs_info
*fs_info
;
196 struct btrfs_pending_bios
*pending_bios
;
200 unsigned long num_run
;
201 unsigned long batch_run
= 0;
203 unsigned long last_waited
= 0;
205 int sync_pending
= 0;
206 struct blk_plug plug
;
209 * this function runs all the bios we've collected for
210 * a particular device. We don't want to wander off to
211 * another device without first sending all of these down.
212 * So, setup a plug here and finish it off before we return
214 blk_start_plug(&plug
);
216 bdi
= blk_get_backing_dev_info(device
->bdev
);
217 fs_info
= device
->dev_root
->fs_info
;
218 limit
= btrfs_async_submit_limit(fs_info
);
219 limit
= limit
* 2 / 3;
222 spin_lock(&device
->io_lock
);
227 /* take all the bios off the list at once and process them
228 * later on (without the lock held). But, remember the
229 * tail and other pointers so the bios can be properly reinserted
230 * into the list if we hit congestion
232 if (!force_reg
&& device
->pending_sync_bios
.head
) {
233 pending_bios
= &device
->pending_sync_bios
;
236 pending_bios
= &device
->pending_bios
;
240 pending
= pending_bios
->head
;
241 tail
= pending_bios
->tail
;
242 WARN_ON(pending
&& !tail
);
245 * if pending was null this time around, no bios need processing
246 * at all and we can stop. Otherwise it'll loop back up again
247 * and do an additional check so no bios are missed.
249 * device->running_pending is used to synchronize with the
252 if (device
->pending_sync_bios
.head
== NULL
&&
253 device
->pending_bios
.head
== NULL
) {
255 device
->running_pending
= 0;
258 device
->running_pending
= 1;
261 pending_bios
->head
= NULL
;
262 pending_bios
->tail
= NULL
;
264 spin_unlock(&device
->io_lock
);
269 /* we want to work on both lists, but do more bios on the
270 * sync list than the regular list
273 pending_bios
!= &device
->pending_sync_bios
&&
274 device
->pending_sync_bios
.head
) ||
275 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
276 device
->pending_bios
.head
)) {
277 spin_lock(&device
->io_lock
);
278 requeue_list(pending_bios
, pending
, tail
);
283 pending
= pending
->bi_next
;
286 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
287 waitqueue_active(&fs_info
->async_submit_wait
))
288 wake_up(&fs_info
->async_submit_wait
);
290 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
293 * if we're doing the sync list, record that our
294 * plug has some sync requests on it
296 * If we're doing the regular list and there are
297 * sync requests sitting around, unplug before
300 if (pending_bios
== &device
->pending_sync_bios
) {
302 } else if (sync_pending
) {
303 blk_finish_plug(&plug
);
304 blk_start_plug(&plug
);
308 btrfsic_submit_bio(cur
->bi_rw
, cur
);
315 * we made progress, there is more work to do and the bdi
316 * is now congested. Back off and let other work structs
319 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
320 fs_info
->fs_devices
->open_devices
> 1) {
321 struct io_context
*ioc
;
323 ioc
= current
->io_context
;
326 * the main goal here is that we don't want to
327 * block if we're going to be able to submit
328 * more requests without blocking.
330 * This code does two great things, it pokes into
331 * the elevator code from a filesystem _and_
332 * it makes assumptions about how batching works.
334 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
335 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
337 ioc
->last_waited
== last_waited
)) {
339 * we want to go through our batch of
340 * requests and stop. So, we copy out
341 * the ioc->last_waited time and test
342 * against it before looping
344 last_waited
= ioc
->last_waited
;
349 spin_lock(&device
->io_lock
);
350 requeue_list(pending_bios
, pending
, tail
);
351 device
->running_pending
= 1;
353 spin_unlock(&device
->io_lock
);
354 btrfs_requeue_work(&device
->work
);
357 /* unplug every 64 requests just for good measure */
358 if (batch_run
% 64 == 0) {
359 blk_finish_plug(&plug
);
360 blk_start_plug(&plug
);
369 spin_lock(&device
->io_lock
);
370 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
372 spin_unlock(&device
->io_lock
);
375 blk_finish_plug(&plug
);
378 static void pending_bios_fn(struct btrfs_work
*work
)
380 struct btrfs_device
*device
;
382 device
= container_of(work
, struct btrfs_device
, work
);
383 run_scheduled_bios(device
);
386 static noinline
int device_list_add(const char *path
,
387 struct btrfs_super_block
*disk_super
,
388 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
390 struct btrfs_device
*device
;
391 struct btrfs_fs_devices
*fs_devices
;
392 struct rcu_string
*name
;
393 u64 found_transid
= btrfs_super_generation(disk_super
);
395 fs_devices
= find_fsid(disk_super
->fsid
);
397 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
400 INIT_LIST_HEAD(&fs_devices
->devices
);
401 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
402 list_add(&fs_devices
->list
, &fs_uuids
);
403 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
404 fs_devices
->latest_devid
= devid
;
405 fs_devices
->latest_trans
= found_transid
;
406 mutex_init(&fs_devices
->device_list_mutex
);
409 device
= __find_device(&fs_devices
->devices
, devid
,
410 disk_super
->dev_item
.uuid
);
413 if (fs_devices
->opened
)
416 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
418 /* we can safely leave the fs_devices entry around */
421 device
->devid
= devid
;
422 device
->dev_stats_valid
= 0;
423 device
->work
.func
= pending_bios_fn
;
424 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
426 spin_lock_init(&device
->io_lock
);
428 name
= rcu_string_strdup(path
, GFP_NOFS
);
433 rcu_assign_pointer(device
->name
, name
);
434 INIT_LIST_HEAD(&device
->dev_alloc_list
);
436 /* init readahead state */
437 spin_lock_init(&device
->reada_lock
);
438 device
->reada_curr_zone
= NULL
;
439 atomic_set(&device
->reada_in_flight
, 0);
440 device
->reada_next
= 0;
441 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
442 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
444 mutex_lock(&fs_devices
->device_list_mutex
);
445 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
446 mutex_unlock(&fs_devices
->device_list_mutex
);
448 device
->fs_devices
= fs_devices
;
449 fs_devices
->num_devices
++;
450 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
451 name
= rcu_string_strdup(path
, GFP_NOFS
);
454 rcu_string_free(device
->name
);
455 rcu_assign_pointer(device
->name
, name
);
456 if (device
->missing
) {
457 fs_devices
->missing_devices
--;
462 if (found_transid
> fs_devices
->latest_trans
) {
463 fs_devices
->latest_devid
= devid
;
464 fs_devices
->latest_trans
= found_transid
;
466 *fs_devices_ret
= fs_devices
;
470 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
472 struct btrfs_fs_devices
*fs_devices
;
473 struct btrfs_device
*device
;
474 struct btrfs_device
*orig_dev
;
476 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
478 return ERR_PTR(-ENOMEM
);
480 INIT_LIST_HEAD(&fs_devices
->devices
);
481 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
482 INIT_LIST_HEAD(&fs_devices
->list
);
483 mutex_init(&fs_devices
->device_list_mutex
);
484 fs_devices
->latest_devid
= orig
->latest_devid
;
485 fs_devices
->latest_trans
= orig
->latest_trans
;
486 fs_devices
->total_devices
= orig
->total_devices
;
487 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
489 /* We have held the volume lock, it is safe to get the devices. */
490 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
491 struct rcu_string
*name
;
493 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
498 * This is ok to do without rcu read locked because we hold the
499 * uuid mutex so nothing we touch in here is going to disappear.
501 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
506 rcu_assign_pointer(device
->name
, name
);
508 device
->devid
= orig_dev
->devid
;
509 device
->work
.func
= pending_bios_fn
;
510 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
511 spin_lock_init(&device
->io_lock
);
512 INIT_LIST_HEAD(&device
->dev_list
);
513 INIT_LIST_HEAD(&device
->dev_alloc_list
);
515 list_add(&device
->dev_list
, &fs_devices
->devices
);
516 device
->fs_devices
= fs_devices
;
517 fs_devices
->num_devices
++;
521 free_fs_devices(fs_devices
);
522 return ERR_PTR(-ENOMEM
);
525 void btrfs_close_extra_devices(struct btrfs_fs_info
*fs_info
,
526 struct btrfs_fs_devices
*fs_devices
, int step
)
528 struct btrfs_device
*device
, *next
;
530 struct block_device
*latest_bdev
= NULL
;
531 u64 latest_devid
= 0;
532 u64 latest_transid
= 0;
534 mutex_lock(&uuid_mutex
);
536 /* This is the initialized path, it is safe to release the devices. */
537 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
538 if (device
->in_fs_metadata
) {
539 if (!device
->is_tgtdev_for_dev_replace
&&
541 device
->generation
> latest_transid
)) {
542 latest_devid
= device
->devid
;
543 latest_transid
= device
->generation
;
544 latest_bdev
= device
->bdev
;
549 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
551 * In the first step, keep the device which has
552 * the correct fsid and the devid that is used
553 * for the dev_replace procedure.
554 * In the second step, the dev_replace state is
555 * read from the device tree and it is known
556 * whether the procedure is really active or
557 * not, which means whether this device is
558 * used or whether it should be removed.
560 if (step
== 0 || device
->is_tgtdev_for_dev_replace
) {
565 blkdev_put(device
->bdev
, device
->mode
);
567 fs_devices
->open_devices
--;
569 if (device
->writeable
) {
570 list_del_init(&device
->dev_alloc_list
);
571 device
->writeable
= 0;
572 if (!device
->is_tgtdev_for_dev_replace
)
573 fs_devices
->rw_devices
--;
575 list_del_init(&device
->dev_list
);
576 fs_devices
->num_devices
--;
577 rcu_string_free(device
->name
);
581 if (fs_devices
->seed
) {
582 fs_devices
= fs_devices
->seed
;
586 fs_devices
->latest_bdev
= latest_bdev
;
587 fs_devices
->latest_devid
= latest_devid
;
588 fs_devices
->latest_trans
= latest_transid
;
590 mutex_unlock(&uuid_mutex
);
593 static void __free_device(struct work_struct
*work
)
595 struct btrfs_device
*device
;
597 device
= container_of(work
, struct btrfs_device
, rcu_work
);
600 blkdev_put(device
->bdev
, device
->mode
);
602 rcu_string_free(device
->name
);
606 static void free_device(struct rcu_head
*head
)
608 struct btrfs_device
*device
;
610 device
= container_of(head
, struct btrfs_device
, rcu
);
612 INIT_WORK(&device
->rcu_work
, __free_device
);
613 schedule_work(&device
->rcu_work
);
616 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
618 struct btrfs_device
*device
;
620 if (--fs_devices
->opened
> 0)
623 mutex_lock(&fs_devices
->device_list_mutex
);
624 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
625 struct btrfs_device
*new_device
;
626 struct rcu_string
*name
;
629 fs_devices
->open_devices
--;
631 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
632 list_del_init(&device
->dev_alloc_list
);
633 fs_devices
->rw_devices
--;
636 if (device
->can_discard
)
637 fs_devices
->num_can_discard
--;
639 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
640 BUG_ON(!new_device
); /* -ENOMEM */
641 memcpy(new_device
, device
, sizeof(*new_device
));
643 /* Safe because we are under uuid_mutex */
645 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
646 BUG_ON(device
->name
&& !name
); /* -ENOMEM */
647 rcu_assign_pointer(new_device
->name
, name
);
649 new_device
->bdev
= NULL
;
650 new_device
->writeable
= 0;
651 new_device
->in_fs_metadata
= 0;
652 new_device
->can_discard
= 0;
653 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
655 call_rcu(&device
->rcu
, free_device
);
657 mutex_unlock(&fs_devices
->device_list_mutex
);
659 WARN_ON(fs_devices
->open_devices
);
660 WARN_ON(fs_devices
->rw_devices
);
661 fs_devices
->opened
= 0;
662 fs_devices
->seeding
= 0;
667 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
669 struct btrfs_fs_devices
*seed_devices
= NULL
;
672 mutex_lock(&uuid_mutex
);
673 ret
= __btrfs_close_devices(fs_devices
);
674 if (!fs_devices
->opened
) {
675 seed_devices
= fs_devices
->seed
;
676 fs_devices
->seed
= NULL
;
678 mutex_unlock(&uuid_mutex
);
680 while (seed_devices
) {
681 fs_devices
= seed_devices
;
682 seed_devices
= fs_devices
->seed
;
683 __btrfs_close_devices(fs_devices
);
684 free_fs_devices(fs_devices
);
689 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
690 fmode_t flags
, void *holder
)
692 struct request_queue
*q
;
693 struct block_device
*bdev
;
694 struct list_head
*head
= &fs_devices
->devices
;
695 struct btrfs_device
*device
;
696 struct block_device
*latest_bdev
= NULL
;
697 struct buffer_head
*bh
;
698 struct btrfs_super_block
*disk_super
;
699 u64 latest_devid
= 0;
700 u64 latest_transid
= 0;
707 list_for_each_entry(device
, head
, dev_list
) {
713 ret
= btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
718 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
719 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
720 if (devid
!= device
->devid
)
723 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
727 device
->generation
= btrfs_super_generation(disk_super
);
728 if (!latest_transid
|| device
->generation
> latest_transid
) {
729 latest_devid
= devid
;
730 latest_transid
= device
->generation
;
734 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
735 device
->writeable
= 0;
737 device
->writeable
= !bdev_read_only(bdev
);
741 q
= bdev_get_queue(bdev
);
742 if (blk_queue_discard(q
)) {
743 device
->can_discard
= 1;
744 fs_devices
->num_can_discard
++;
748 device
->in_fs_metadata
= 0;
749 device
->mode
= flags
;
751 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
752 fs_devices
->rotating
= 1;
754 fs_devices
->open_devices
++;
755 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
756 fs_devices
->rw_devices
++;
757 list_add(&device
->dev_alloc_list
,
758 &fs_devices
->alloc_list
);
765 blkdev_put(bdev
, flags
);
768 if (fs_devices
->open_devices
== 0) {
772 fs_devices
->seeding
= seeding
;
773 fs_devices
->opened
= 1;
774 fs_devices
->latest_bdev
= latest_bdev
;
775 fs_devices
->latest_devid
= latest_devid
;
776 fs_devices
->latest_trans
= latest_transid
;
777 fs_devices
->total_rw_bytes
= 0;
782 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
783 fmode_t flags
, void *holder
)
787 mutex_lock(&uuid_mutex
);
788 if (fs_devices
->opened
) {
789 fs_devices
->opened
++;
792 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
794 mutex_unlock(&uuid_mutex
);
798 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
799 struct btrfs_fs_devices
**fs_devices_ret
)
801 struct btrfs_super_block
*disk_super
;
802 struct block_device
*bdev
;
803 struct buffer_head
*bh
;
810 mutex_lock(&uuid_mutex
);
811 ret
= btrfs_get_bdev_and_sb(path
, flags
, holder
, 0, &bdev
, &bh
);
814 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
815 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
816 transid
= btrfs_super_generation(disk_super
);
817 total_devices
= btrfs_super_num_devices(disk_super
);
818 if (disk_super
->label
[0]) {
819 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
820 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
821 printk(KERN_INFO
"device label %s ", disk_super
->label
);
823 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
825 printk(KERN_CONT
"devid %llu transid %llu %s\n",
826 (unsigned long long)devid
, (unsigned long long)transid
, path
);
827 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
828 if (!ret
&& fs_devices_ret
)
829 (*fs_devices_ret
)->total_devices
= total_devices
;
831 blkdev_put(bdev
, flags
);
833 mutex_unlock(&uuid_mutex
);
837 /* helper to account the used device space in the range */
838 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
839 u64 end
, u64
*length
)
841 struct btrfs_key key
;
842 struct btrfs_root
*root
= device
->dev_root
;
843 struct btrfs_dev_extent
*dev_extent
;
844 struct btrfs_path
*path
;
848 struct extent_buffer
*l
;
852 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
855 path
= btrfs_alloc_path();
860 key
.objectid
= device
->devid
;
862 key
.type
= BTRFS_DEV_EXTENT_KEY
;
864 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
868 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
875 slot
= path
->slots
[0];
876 if (slot
>= btrfs_header_nritems(l
)) {
877 ret
= btrfs_next_leaf(root
, path
);
885 btrfs_item_key_to_cpu(l
, &key
, slot
);
887 if (key
.objectid
< device
->devid
)
890 if (key
.objectid
> device
->devid
)
893 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
896 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
897 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
899 if (key
.offset
<= start
&& extent_end
> end
) {
900 *length
= end
- start
+ 1;
902 } else if (key
.offset
<= start
&& extent_end
> start
)
903 *length
+= extent_end
- start
;
904 else if (key
.offset
> start
&& extent_end
<= end
)
905 *length
+= extent_end
- key
.offset
;
906 else if (key
.offset
> start
&& key
.offset
<= end
) {
907 *length
+= end
- key
.offset
+ 1;
909 } else if (key
.offset
> end
)
917 btrfs_free_path(path
);
922 * find_free_dev_extent - find free space in the specified device
923 * @device: the device which we search the free space in
924 * @num_bytes: the size of the free space that we need
925 * @start: store the start of the free space.
926 * @len: the size of the free space. that we find, or the size of the max
927 * free space if we don't find suitable free space
929 * this uses a pretty simple search, the expectation is that it is
930 * called very infrequently and that a given device has a small number
933 * @start is used to store the start of the free space if we find. But if we
934 * don't find suitable free space, it will be used to store the start position
935 * of the max free space.
937 * @len is used to store the size of the free space that we find.
938 * But if we don't find suitable free space, it is used to store the size of
939 * the max free space.
941 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
942 u64
*start
, u64
*len
)
944 struct btrfs_key key
;
945 struct btrfs_root
*root
= device
->dev_root
;
946 struct btrfs_dev_extent
*dev_extent
;
947 struct btrfs_path
*path
;
953 u64 search_end
= device
->total_bytes
;
956 struct extent_buffer
*l
;
958 /* FIXME use last free of some kind */
960 /* we don't want to overwrite the superblock on the drive,
961 * so we make sure to start at an offset of at least 1MB
963 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
965 max_hole_start
= search_start
;
969 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
974 path
= btrfs_alloc_path();
981 key
.objectid
= device
->devid
;
982 key
.offset
= search_start
;
983 key
.type
= BTRFS_DEV_EXTENT_KEY
;
985 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
989 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
996 slot
= path
->slots
[0];
997 if (slot
>= btrfs_header_nritems(l
)) {
998 ret
= btrfs_next_leaf(root
, path
);
1006 btrfs_item_key_to_cpu(l
, &key
, slot
);
1008 if (key
.objectid
< device
->devid
)
1011 if (key
.objectid
> device
->devid
)
1014 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
1017 if (key
.offset
> search_start
) {
1018 hole_size
= key
.offset
- search_start
;
1020 if (hole_size
> max_hole_size
) {
1021 max_hole_start
= search_start
;
1022 max_hole_size
= hole_size
;
1026 * If this free space is greater than which we need,
1027 * it must be the max free space that we have found
1028 * until now, so max_hole_start must point to the start
1029 * of this free space and the length of this free space
1030 * is stored in max_hole_size. Thus, we return
1031 * max_hole_start and max_hole_size and go back to the
1034 if (hole_size
>= num_bytes
) {
1040 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1041 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1043 if (extent_end
> search_start
)
1044 search_start
= extent_end
;
1051 * At this point, search_start should be the end of
1052 * allocated dev extents, and when shrinking the device,
1053 * search_end may be smaller than search_start.
1055 if (search_end
> search_start
)
1056 hole_size
= search_end
- search_start
;
1058 if (hole_size
> max_hole_size
) {
1059 max_hole_start
= search_start
;
1060 max_hole_size
= hole_size
;
1064 if (hole_size
< num_bytes
)
1070 btrfs_free_path(path
);
1072 *start
= max_hole_start
;
1074 *len
= max_hole_size
;
1078 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1079 struct btrfs_device
*device
,
1083 struct btrfs_path
*path
;
1084 struct btrfs_root
*root
= device
->dev_root
;
1085 struct btrfs_key key
;
1086 struct btrfs_key found_key
;
1087 struct extent_buffer
*leaf
= NULL
;
1088 struct btrfs_dev_extent
*extent
= NULL
;
1090 path
= btrfs_alloc_path();
1094 key
.objectid
= device
->devid
;
1096 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1098 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1100 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1101 BTRFS_DEV_EXTENT_KEY
);
1104 leaf
= path
->nodes
[0];
1105 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1106 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1107 struct btrfs_dev_extent
);
1108 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1109 btrfs_dev_extent_length(leaf
, extent
) < start
);
1111 btrfs_release_path(path
);
1113 } else if (ret
== 0) {
1114 leaf
= path
->nodes
[0];
1115 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1116 struct btrfs_dev_extent
);
1118 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1122 if (device
->bytes_used
> 0) {
1123 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1124 device
->bytes_used
-= len
;
1125 spin_lock(&root
->fs_info
->free_chunk_lock
);
1126 root
->fs_info
->free_chunk_space
+= len
;
1127 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1129 ret
= btrfs_del_item(trans
, root
, path
);
1131 btrfs_error(root
->fs_info
, ret
,
1132 "Failed to remove dev extent item");
1135 btrfs_free_path(path
);
1139 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1140 struct btrfs_device
*device
,
1141 u64 chunk_tree
, u64 chunk_objectid
,
1142 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1145 struct btrfs_path
*path
;
1146 struct btrfs_root
*root
= device
->dev_root
;
1147 struct btrfs_dev_extent
*extent
;
1148 struct extent_buffer
*leaf
;
1149 struct btrfs_key key
;
1151 WARN_ON(!device
->in_fs_metadata
);
1152 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1153 path
= btrfs_alloc_path();
1157 key
.objectid
= device
->devid
;
1159 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1160 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1165 leaf
= path
->nodes
[0];
1166 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1167 struct btrfs_dev_extent
);
1168 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1169 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1170 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1172 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1173 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1176 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1177 btrfs_mark_buffer_dirty(leaf
);
1179 btrfs_free_path(path
);
1183 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1184 u64 objectid
, u64
*offset
)
1186 struct btrfs_path
*path
;
1188 struct btrfs_key key
;
1189 struct btrfs_chunk
*chunk
;
1190 struct btrfs_key found_key
;
1192 path
= btrfs_alloc_path();
1196 key
.objectid
= objectid
;
1197 key
.offset
= (u64
)-1;
1198 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1200 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1204 BUG_ON(ret
== 0); /* Corruption */
1206 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1210 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1212 if (found_key
.objectid
!= objectid
)
1215 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1216 struct btrfs_chunk
);
1217 *offset
= found_key
.offset
+
1218 btrfs_chunk_length(path
->nodes
[0], chunk
);
1223 btrfs_free_path(path
);
1227 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1230 struct btrfs_key key
;
1231 struct btrfs_key found_key
;
1232 struct btrfs_path
*path
;
1234 root
= root
->fs_info
->chunk_root
;
1236 path
= btrfs_alloc_path();
1240 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1241 key
.type
= BTRFS_DEV_ITEM_KEY
;
1242 key
.offset
= (u64
)-1;
1244 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1248 BUG_ON(ret
== 0); /* Corruption */
1250 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1251 BTRFS_DEV_ITEM_KEY
);
1255 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1257 *objectid
= found_key
.offset
+ 1;
1261 btrfs_free_path(path
);
1266 * the device information is stored in the chunk root
1267 * the btrfs_device struct should be fully filled in
1269 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1270 struct btrfs_root
*root
,
1271 struct btrfs_device
*device
)
1274 struct btrfs_path
*path
;
1275 struct btrfs_dev_item
*dev_item
;
1276 struct extent_buffer
*leaf
;
1277 struct btrfs_key key
;
1280 root
= root
->fs_info
->chunk_root
;
1282 path
= btrfs_alloc_path();
1286 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1287 key
.type
= BTRFS_DEV_ITEM_KEY
;
1288 key
.offset
= device
->devid
;
1290 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1295 leaf
= path
->nodes
[0];
1296 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1298 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1299 btrfs_set_device_generation(leaf
, dev_item
, 0);
1300 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1301 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1302 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1303 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1304 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1305 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1306 btrfs_set_device_group(leaf
, dev_item
, 0);
1307 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1308 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1309 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1311 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1312 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1313 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1314 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1315 btrfs_mark_buffer_dirty(leaf
);
1319 btrfs_free_path(path
);
1323 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1324 struct btrfs_device
*device
)
1327 struct btrfs_path
*path
;
1328 struct btrfs_key key
;
1329 struct btrfs_trans_handle
*trans
;
1331 root
= root
->fs_info
->chunk_root
;
1333 path
= btrfs_alloc_path();
1337 trans
= btrfs_start_transaction(root
, 0);
1338 if (IS_ERR(trans
)) {
1339 btrfs_free_path(path
);
1340 return PTR_ERR(trans
);
1342 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1343 key
.type
= BTRFS_DEV_ITEM_KEY
;
1344 key
.offset
= device
->devid
;
1347 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1356 ret
= btrfs_del_item(trans
, root
, path
);
1360 btrfs_free_path(path
);
1361 unlock_chunks(root
);
1362 btrfs_commit_transaction(trans
, root
);
1366 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1368 struct btrfs_device
*device
;
1369 struct btrfs_device
*next_device
;
1370 struct block_device
*bdev
;
1371 struct buffer_head
*bh
= NULL
;
1372 struct btrfs_super_block
*disk_super
;
1373 struct btrfs_fs_devices
*cur_devices
;
1379 bool clear_super
= false;
1381 mutex_lock(&uuid_mutex
);
1383 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1384 root
->fs_info
->avail_system_alloc_bits
|
1385 root
->fs_info
->avail_metadata_alloc_bits
;
1387 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1388 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1389 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1390 WARN_ON(num_devices
< 1);
1393 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1395 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1396 printk(KERN_ERR
"btrfs: unable to go below four devices "
1402 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1403 printk(KERN_ERR
"btrfs: unable to go below two "
1404 "devices on raid1\n");
1409 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID5
) &&
1410 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1411 printk(KERN_ERR
"btrfs: unable to go below two "
1412 "devices on raid5\n");
1416 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID6
) &&
1417 root
->fs_info
->fs_devices
->rw_devices
<= 3) {
1418 printk(KERN_ERR
"btrfs: unable to go below three "
1419 "devices on raid6\n");
1424 if (strcmp(device_path
, "missing") == 0) {
1425 struct list_head
*devices
;
1426 struct btrfs_device
*tmp
;
1429 devices
= &root
->fs_info
->fs_devices
->devices
;
1431 * It is safe to read the devices since the volume_mutex
1434 list_for_each_entry(tmp
, devices
, dev_list
) {
1435 if (tmp
->in_fs_metadata
&&
1436 !tmp
->is_tgtdev_for_dev_replace
&&
1446 printk(KERN_ERR
"btrfs: no missing devices found to "
1451 ret
= btrfs_get_bdev_and_sb(device_path
,
1452 FMODE_WRITE
| FMODE_EXCL
,
1453 root
->fs_info
->bdev_holder
, 0,
1457 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1458 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1459 dev_uuid
= disk_super
->dev_item
.uuid
;
1460 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1468 if (device
->is_tgtdev_for_dev_replace
) {
1469 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1474 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1475 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1481 if (device
->writeable
) {
1483 list_del_init(&device
->dev_alloc_list
);
1484 unlock_chunks(root
);
1485 root
->fs_info
->fs_devices
->rw_devices
--;
1489 ret
= btrfs_shrink_device(device
, 0);
1494 * TODO: the superblock still includes this device in its num_devices
1495 * counter although write_all_supers() is not locked out. This
1496 * could give a filesystem state which requires a degraded mount.
1498 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1502 spin_lock(&root
->fs_info
->free_chunk_lock
);
1503 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1505 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1507 device
->in_fs_metadata
= 0;
1508 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1511 * the device list mutex makes sure that we don't change
1512 * the device list while someone else is writing out all
1513 * the device supers.
1516 cur_devices
= device
->fs_devices
;
1517 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1518 list_del_rcu(&device
->dev_list
);
1520 device
->fs_devices
->num_devices
--;
1521 device
->fs_devices
->total_devices
--;
1523 if (device
->missing
)
1524 root
->fs_info
->fs_devices
->missing_devices
--;
1526 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1527 struct btrfs_device
, dev_list
);
1528 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1529 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1530 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1531 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1534 device
->fs_devices
->open_devices
--;
1536 call_rcu(&device
->rcu
, free_device
);
1537 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1539 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1540 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1542 if (cur_devices
->open_devices
== 0) {
1543 struct btrfs_fs_devices
*fs_devices
;
1544 fs_devices
= root
->fs_info
->fs_devices
;
1545 while (fs_devices
) {
1546 if (fs_devices
->seed
== cur_devices
)
1548 fs_devices
= fs_devices
->seed
;
1550 fs_devices
->seed
= cur_devices
->seed
;
1551 cur_devices
->seed
= NULL
;
1553 __btrfs_close_devices(cur_devices
);
1554 unlock_chunks(root
);
1555 free_fs_devices(cur_devices
);
1558 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1559 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1562 * at this point, the device is zero sized. We want to
1563 * remove it from the devices list and zero out the old super
1565 if (clear_super
&& disk_super
) {
1566 /* make sure this device isn't detected as part of
1569 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1570 set_buffer_dirty(bh
);
1571 sync_dirty_buffer(bh
);
1576 /* Notify udev that device has changed */
1578 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
1583 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1585 mutex_unlock(&uuid_mutex
);
1588 if (device
->writeable
) {
1590 list_add(&device
->dev_alloc_list
,
1591 &root
->fs_info
->fs_devices
->alloc_list
);
1592 unlock_chunks(root
);
1593 root
->fs_info
->fs_devices
->rw_devices
++;
1598 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info
*fs_info
,
1599 struct btrfs_device
*srcdev
)
1601 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1602 list_del_rcu(&srcdev
->dev_list
);
1603 list_del_rcu(&srcdev
->dev_alloc_list
);
1604 fs_info
->fs_devices
->num_devices
--;
1605 if (srcdev
->missing
) {
1606 fs_info
->fs_devices
->missing_devices
--;
1607 fs_info
->fs_devices
->rw_devices
++;
1609 if (srcdev
->can_discard
)
1610 fs_info
->fs_devices
->num_can_discard
--;
1612 fs_info
->fs_devices
->open_devices
--;
1614 call_rcu(&srcdev
->rcu
, free_device
);
1617 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1618 struct btrfs_device
*tgtdev
)
1620 struct btrfs_device
*next_device
;
1623 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1625 btrfs_scratch_superblock(tgtdev
);
1626 fs_info
->fs_devices
->open_devices
--;
1628 fs_info
->fs_devices
->num_devices
--;
1629 if (tgtdev
->can_discard
)
1630 fs_info
->fs_devices
->num_can_discard
++;
1632 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1633 struct btrfs_device
, dev_list
);
1634 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1635 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1636 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1637 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1638 list_del_rcu(&tgtdev
->dev_list
);
1640 call_rcu(&tgtdev
->rcu
, free_device
);
1642 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1645 int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1646 struct btrfs_device
**device
)
1649 struct btrfs_super_block
*disk_super
;
1652 struct block_device
*bdev
;
1653 struct buffer_head
*bh
;
1656 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1657 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1660 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1661 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1662 dev_uuid
= disk_super
->dev_item
.uuid
;
1663 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1668 blkdev_put(bdev
, FMODE_READ
);
1672 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1674 struct btrfs_device
**device
)
1677 if (strcmp(device_path
, "missing") == 0) {
1678 struct list_head
*devices
;
1679 struct btrfs_device
*tmp
;
1681 devices
= &root
->fs_info
->fs_devices
->devices
;
1683 * It is safe to read the devices since the volume_mutex
1684 * is held by the caller.
1686 list_for_each_entry(tmp
, devices
, dev_list
) {
1687 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1694 pr_err("btrfs: no missing device found\n");
1700 return btrfs_find_device_by_path(root
, device_path
, device
);
1705 * does all the dirty work required for changing file system's UUID.
1707 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1709 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1710 struct btrfs_fs_devices
*old_devices
;
1711 struct btrfs_fs_devices
*seed_devices
;
1712 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1713 struct btrfs_device
*device
;
1716 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1717 if (!fs_devices
->seeding
)
1720 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1724 old_devices
= clone_fs_devices(fs_devices
);
1725 if (IS_ERR(old_devices
)) {
1726 kfree(seed_devices
);
1727 return PTR_ERR(old_devices
);
1730 list_add(&old_devices
->list
, &fs_uuids
);
1732 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1733 seed_devices
->opened
= 1;
1734 INIT_LIST_HEAD(&seed_devices
->devices
);
1735 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1736 mutex_init(&seed_devices
->device_list_mutex
);
1738 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1739 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1741 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1743 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1744 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1745 device
->fs_devices
= seed_devices
;
1748 fs_devices
->seeding
= 0;
1749 fs_devices
->num_devices
= 0;
1750 fs_devices
->open_devices
= 0;
1751 fs_devices
->total_devices
= 0;
1752 fs_devices
->seed
= seed_devices
;
1754 generate_random_uuid(fs_devices
->fsid
);
1755 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1756 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1757 super_flags
= btrfs_super_flags(disk_super
) &
1758 ~BTRFS_SUPER_FLAG_SEEDING
;
1759 btrfs_set_super_flags(disk_super
, super_flags
);
1765 * strore the expected generation for seed devices in device items.
1767 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1768 struct btrfs_root
*root
)
1770 struct btrfs_path
*path
;
1771 struct extent_buffer
*leaf
;
1772 struct btrfs_dev_item
*dev_item
;
1773 struct btrfs_device
*device
;
1774 struct btrfs_key key
;
1775 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1776 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1780 path
= btrfs_alloc_path();
1784 root
= root
->fs_info
->chunk_root
;
1785 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1787 key
.type
= BTRFS_DEV_ITEM_KEY
;
1790 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1794 leaf
= path
->nodes
[0];
1796 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1797 ret
= btrfs_next_leaf(root
, path
);
1802 leaf
= path
->nodes
[0];
1803 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1804 btrfs_release_path(path
);
1808 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1809 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1810 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1813 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1814 struct btrfs_dev_item
);
1815 devid
= btrfs_device_id(leaf
, dev_item
);
1816 read_extent_buffer(leaf
, dev_uuid
,
1817 (unsigned long)btrfs_device_uuid(dev_item
),
1819 read_extent_buffer(leaf
, fs_uuid
,
1820 (unsigned long)btrfs_device_fsid(dev_item
),
1822 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1824 BUG_ON(!device
); /* Logic error */
1826 if (device
->fs_devices
->seeding
) {
1827 btrfs_set_device_generation(leaf
, dev_item
,
1828 device
->generation
);
1829 btrfs_mark_buffer_dirty(leaf
);
1837 btrfs_free_path(path
);
1841 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1843 struct request_queue
*q
;
1844 struct btrfs_trans_handle
*trans
;
1845 struct btrfs_device
*device
;
1846 struct block_device
*bdev
;
1847 struct list_head
*devices
;
1848 struct super_block
*sb
= root
->fs_info
->sb
;
1849 struct rcu_string
*name
;
1851 int seeding_dev
= 0;
1854 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1857 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1858 root
->fs_info
->bdev_holder
);
1860 return PTR_ERR(bdev
);
1862 if (root
->fs_info
->fs_devices
->seeding
) {
1864 down_write(&sb
->s_umount
);
1865 mutex_lock(&uuid_mutex
);
1868 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1870 devices
= &root
->fs_info
->fs_devices
->devices
;
1872 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1873 list_for_each_entry(device
, devices
, dev_list
) {
1874 if (device
->bdev
== bdev
) {
1877 &root
->fs_info
->fs_devices
->device_list_mutex
);
1881 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1883 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1885 /* we can safely leave the fs_devices entry around */
1890 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
1896 rcu_assign_pointer(device
->name
, name
);
1898 ret
= find_next_devid(root
, &device
->devid
);
1900 rcu_string_free(device
->name
);
1905 trans
= btrfs_start_transaction(root
, 0);
1906 if (IS_ERR(trans
)) {
1907 rcu_string_free(device
->name
);
1909 ret
= PTR_ERR(trans
);
1915 q
= bdev_get_queue(bdev
);
1916 if (blk_queue_discard(q
))
1917 device
->can_discard
= 1;
1918 device
->writeable
= 1;
1919 device
->work
.func
= pending_bios_fn
;
1920 generate_random_uuid(device
->uuid
);
1921 spin_lock_init(&device
->io_lock
);
1922 device
->generation
= trans
->transid
;
1923 device
->io_width
= root
->sectorsize
;
1924 device
->io_align
= root
->sectorsize
;
1925 device
->sector_size
= root
->sectorsize
;
1926 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1927 device
->disk_total_bytes
= device
->total_bytes
;
1928 device
->dev_root
= root
->fs_info
->dev_root
;
1929 device
->bdev
= bdev
;
1930 device
->in_fs_metadata
= 1;
1931 device
->is_tgtdev_for_dev_replace
= 0;
1932 device
->mode
= FMODE_EXCL
;
1933 set_blocksize(device
->bdev
, 4096);
1936 sb
->s_flags
&= ~MS_RDONLY
;
1937 ret
= btrfs_prepare_sprout(root
);
1938 BUG_ON(ret
); /* -ENOMEM */
1941 device
->fs_devices
= root
->fs_info
->fs_devices
;
1943 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1944 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1945 list_add(&device
->dev_alloc_list
,
1946 &root
->fs_info
->fs_devices
->alloc_list
);
1947 root
->fs_info
->fs_devices
->num_devices
++;
1948 root
->fs_info
->fs_devices
->open_devices
++;
1949 root
->fs_info
->fs_devices
->rw_devices
++;
1950 root
->fs_info
->fs_devices
->total_devices
++;
1951 if (device
->can_discard
)
1952 root
->fs_info
->fs_devices
->num_can_discard
++;
1953 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1955 spin_lock(&root
->fs_info
->free_chunk_lock
);
1956 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1957 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1959 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1960 root
->fs_info
->fs_devices
->rotating
= 1;
1962 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1963 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1964 total_bytes
+ device
->total_bytes
);
1966 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1967 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1969 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1972 ret
= init_first_rw_device(trans
, root
, device
);
1974 btrfs_abort_transaction(trans
, root
, ret
);
1977 ret
= btrfs_finish_sprout(trans
, root
);
1979 btrfs_abort_transaction(trans
, root
, ret
);
1983 ret
= btrfs_add_device(trans
, root
, device
);
1985 btrfs_abort_transaction(trans
, root
, ret
);
1991 * we've got more storage, clear any full flags on the space
1994 btrfs_clear_space_info_full(root
->fs_info
);
1996 unlock_chunks(root
);
1997 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1998 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1999 ret
= btrfs_commit_transaction(trans
, root
);
2002 mutex_unlock(&uuid_mutex
);
2003 up_write(&sb
->s_umount
);
2005 if (ret
) /* transaction commit */
2008 ret
= btrfs_relocate_sys_chunks(root
);
2010 btrfs_error(root
->fs_info
, ret
,
2011 "Failed to relocate sys chunks after "
2012 "device initialization. This can be fixed "
2013 "using the \"btrfs balance\" command.");
2014 trans
= btrfs_attach_transaction(root
);
2015 if (IS_ERR(trans
)) {
2016 if (PTR_ERR(trans
) == -ENOENT
)
2018 return PTR_ERR(trans
);
2020 ret
= btrfs_commit_transaction(trans
, root
);
2026 unlock_chunks(root
);
2027 btrfs_end_transaction(trans
, root
);
2028 rcu_string_free(device
->name
);
2031 blkdev_put(bdev
, FMODE_EXCL
);
2033 mutex_unlock(&uuid_mutex
);
2034 up_write(&sb
->s_umount
);
2039 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2040 struct btrfs_device
**device_out
)
2042 struct request_queue
*q
;
2043 struct btrfs_device
*device
;
2044 struct block_device
*bdev
;
2045 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2046 struct list_head
*devices
;
2047 struct rcu_string
*name
;
2051 if (fs_info
->fs_devices
->seeding
)
2054 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2055 fs_info
->bdev_holder
);
2057 return PTR_ERR(bdev
);
2059 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2061 devices
= &fs_info
->fs_devices
->devices
;
2062 list_for_each_entry(device
, devices
, dev_list
) {
2063 if (device
->bdev
== bdev
) {
2069 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2075 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2081 rcu_assign_pointer(device
->name
, name
);
2083 q
= bdev_get_queue(bdev
);
2084 if (blk_queue_discard(q
))
2085 device
->can_discard
= 1;
2086 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2087 device
->writeable
= 1;
2088 device
->work
.func
= pending_bios_fn
;
2089 generate_random_uuid(device
->uuid
);
2090 device
->devid
= BTRFS_DEV_REPLACE_DEVID
;
2091 spin_lock_init(&device
->io_lock
);
2092 device
->generation
= 0;
2093 device
->io_width
= root
->sectorsize
;
2094 device
->io_align
= root
->sectorsize
;
2095 device
->sector_size
= root
->sectorsize
;
2096 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2097 device
->disk_total_bytes
= device
->total_bytes
;
2098 device
->dev_root
= fs_info
->dev_root
;
2099 device
->bdev
= bdev
;
2100 device
->in_fs_metadata
= 1;
2101 device
->is_tgtdev_for_dev_replace
= 1;
2102 device
->mode
= FMODE_EXCL
;
2103 set_blocksize(device
->bdev
, 4096);
2104 device
->fs_devices
= fs_info
->fs_devices
;
2105 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2106 fs_info
->fs_devices
->num_devices
++;
2107 fs_info
->fs_devices
->open_devices
++;
2108 if (device
->can_discard
)
2109 fs_info
->fs_devices
->num_can_discard
++;
2110 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2112 *device_out
= device
;
2116 blkdev_put(bdev
, FMODE_EXCL
);
2120 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2121 struct btrfs_device
*tgtdev
)
2123 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2124 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2125 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2126 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2127 tgtdev
->dev_root
= fs_info
->dev_root
;
2128 tgtdev
->in_fs_metadata
= 1;
2131 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2132 struct btrfs_device
*device
)
2135 struct btrfs_path
*path
;
2136 struct btrfs_root
*root
;
2137 struct btrfs_dev_item
*dev_item
;
2138 struct extent_buffer
*leaf
;
2139 struct btrfs_key key
;
2141 root
= device
->dev_root
->fs_info
->chunk_root
;
2143 path
= btrfs_alloc_path();
2147 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2148 key
.type
= BTRFS_DEV_ITEM_KEY
;
2149 key
.offset
= device
->devid
;
2151 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2160 leaf
= path
->nodes
[0];
2161 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2163 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2164 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2165 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2166 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2167 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2168 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
2169 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
2170 btrfs_mark_buffer_dirty(leaf
);
2173 btrfs_free_path(path
);
2177 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2178 struct btrfs_device
*device
, u64 new_size
)
2180 struct btrfs_super_block
*super_copy
=
2181 device
->dev_root
->fs_info
->super_copy
;
2182 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2183 u64 diff
= new_size
- device
->total_bytes
;
2185 if (!device
->writeable
)
2187 if (new_size
<= device
->total_bytes
||
2188 device
->is_tgtdev_for_dev_replace
)
2191 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2192 device
->fs_devices
->total_rw_bytes
+= diff
;
2194 device
->total_bytes
= new_size
;
2195 device
->disk_total_bytes
= new_size
;
2196 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2198 return btrfs_update_device(trans
, device
);
2201 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2202 struct btrfs_device
*device
, u64 new_size
)
2205 lock_chunks(device
->dev_root
);
2206 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2207 unlock_chunks(device
->dev_root
);
2211 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2212 struct btrfs_root
*root
,
2213 u64 chunk_tree
, u64 chunk_objectid
,
2217 struct btrfs_path
*path
;
2218 struct btrfs_key key
;
2220 root
= root
->fs_info
->chunk_root
;
2221 path
= btrfs_alloc_path();
2225 key
.objectid
= chunk_objectid
;
2226 key
.offset
= chunk_offset
;
2227 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2229 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2232 else if (ret
> 0) { /* Logic error or corruption */
2233 btrfs_error(root
->fs_info
, -ENOENT
,
2234 "Failed lookup while freeing chunk.");
2239 ret
= btrfs_del_item(trans
, root
, path
);
2241 btrfs_error(root
->fs_info
, ret
,
2242 "Failed to delete chunk item.");
2244 btrfs_free_path(path
);
2248 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2251 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2252 struct btrfs_disk_key
*disk_key
;
2253 struct btrfs_chunk
*chunk
;
2260 struct btrfs_key key
;
2262 array_size
= btrfs_super_sys_array_size(super_copy
);
2264 ptr
= super_copy
->sys_chunk_array
;
2267 while (cur
< array_size
) {
2268 disk_key
= (struct btrfs_disk_key
*)ptr
;
2269 btrfs_disk_key_to_cpu(&key
, disk_key
);
2271 len
= sizeof(*disk_key
);
2273 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2274 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2275 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2276 len
+= btrfs_chunk_item_size(num_stripes
);
2281 if (key
.objectid
== chunk_objectid
&&
2282 key
.offset
== chunk_offset
) {
2283 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2285 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2294 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2295 u64 chunk_tree
, u64 chunk_objectid
,
2298 struct extent_map_tree
*em_tree
;
2299 struct btrfs_root
*extent_root
;
2300 struct btrfs_trans_handle
*trans
;
2301 struct extent_map
*em
;
2302 struct map_lookup
*map
;
2306 root
= root
->fs_info
->chunk_root
;
2307 extent_root
= root
->fs_info
->extent_root
;
2308 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2310 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2314 /* step one, relocate all the extents inside this chunk */
2315 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2319 trans
= btrfs_start_transaction(root
, 0);
2320 BUG_ON(IS_ERR(trans
));
2325 * step two, delete the device extents and the
2326 * chunk tree entries
2328 read_lock(&em_tree
->lock
);
2329 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2330 read_unlock(&em_tree
->lock
);
2332 BUG_ON(!em
|| em
->start
> chunk_offset
||
2333 em
->start
+ em
->len
< chunk_offset
);
2334 map
= (struct map_lookup
*)em
->bdev
;
2336 for (i
= 0; i
< map
->num_stripes
; i
++) {
2337 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2338 map
->stripes
[i
].physical
);
2341 if (map
->stripes
[i
].dev
) {
2342 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2346 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2351 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2353 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2354 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2358 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2361 write_lock(&em_tree
->lock
);
2362 remove_extent_mapping(em_tree
, em
);
2363 write_unlock(&em_tree
->lock
);
2368 /* once for the tree */
2369 free_extent_map(em
);
2371 free_extent_map(em
);
2373 unlock_chunks(root
);
2374 btrfs_end_transaction(trans
, root
);
2378 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2380 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2381 struct btrfs_path
*path
;
2382 struct extent_buffer
*leaf
;
2383 struct btrfs_chunk
*chunk
;
2384 struct btrfs_key key
;
2385 struct btrfs_key found_key
;
2386 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2388 bool retried
= false;
2392 path
= btrfs_alloc_path();
2397 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2398 key
.offset
= (u64
)-1;
2399 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2402 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2405 BUG_ON(ret
== 0); /* Corruption */
2407 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2414 leaf
= path
->nodes
[0];
2415 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2417 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2418 struct btrfs_chunk
);
2419 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2420 btrfs_release_path(path
);
2422 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2423 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2432 if (found_key
.offset
== 0)
2434 key
.offset
= found_key
.offset
- 1;
2437 if (failed
&& !retried
) {
2441 } else if (failed
&& retried
) {
2446 btrfs_free_path(path
);
2450 static int insert_balance_item(struct btrfs_root
*root
,
2451 struct btrfs_balance_control
*bctl
)
2453 struct btrfs_trans_handle
*trans
;
2454 struct btrfs_balance_item
*item
;
2455 struct btrfs_disk_balance_args disk_bargs
;
2456 struct btrfs_path
*path
;
2457 struct extent_buffer
*leaf
;
2458 struct btrfs_key key
;
2461 path
= btrfs_alloc_path();
2465 trans
= btrfs_start_transaction(root
, 0);
2466 if (IS_ERR(trans
)) {
2467 btrfs_free_path(path
);
2468 return PTR_ERR(trans
);
2471 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2472 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2475 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2480 leaf
= path
->nodes
[0];
2481 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2483 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2485 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2486 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2487 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2488 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2489 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2490 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2492 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2494 btrfs_mark_buffer_dirty(leaf
);
2496 btrfs_free_path(path
);
2497 err
= btrfs_commit_transaction(trans
, root
);
2503 static int del_balance_item(struct btrfs_root
*root
)
2505 struct btrfs_trans_handle
*trans
;
2506 struct btrfs_path
*path
;
2507 struct btrfs_key key
;
2510 path
= btrfs_alloc_path();
2514 trans
= btrfs_start_transaction(root
, 0);
2515 if (IS_ERR(trans
)) {
2516 btrfs_free_path(path
);
2517 return PTR_ERR(trans
);
2520 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2521 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2524 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2532 ret
= btrfs_del_item(trans
, root
, path
);
2534 btrfs_free_path(path
);
2535 err
= btrfs_commit_transaction(trans
, root
);
2542 * This is a heuristic used to reduce the number of chunks balanced on
2543 * resume after balance was interrupted.
2545 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2548 * Turn on soft mode for chunk types that were being converted.
2550 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2551 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2552 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2553 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2554 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2555 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2558 * Turn on usage filter if is not already used. The idea is
2559 * that chunks that we have already balanced should be
2560 * reasonably full. Don't do it for chunks that are being
2561 * converted - that will keep us from relocating unconverted
2562 * (albeit full) chunks.
2564 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2565 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2566 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2567 bctl
->data
.usage
= 90;
2569 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2570 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2571 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2572 bctl
->sys
.usage
= 90;
2574 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2575 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2576 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2577 bctl
->meta
.usage
= 90;
2582 * Should be called with both balance and volume mutexes held to
2583 * serialize other volume operations (add_dev/rm_dev/resize) with
2584 * restriper. Same goes for unset_balance_control.
2586 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2588 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2590 BUG_ON(fs_info
->balance_ctl
);
2592 spin_lock(&fs_info
->balance_lock
);
2593 fs_info
->balance_ctl
= bctl
;
2594 spin_unlock(&fs_info
->balance_lock
);
2597 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2599 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2601 BUG_ON(!fs_info
->balance_ctl
);
2603 spin_lock(&fs_info
->balance_lock
);
2604 fs_info
->balance_ctl
= NULL
;
2605 spin_unlock(&fs_info
->balance_lock
);
2611 * Balance filters. Return 1 if chunk should be filtered out
2612 * (should not be balanced).
2614 static int chunk_profiles_filter(u64 chunk_type
,
2615 struct btrfs_balance_args
*bargs
)
2617 chunk_type
= chunk_to_extended(chunk_type
) &
2618 BTRFS_EXTENDED_PROFILE_MASK
;
2620 if (bargs
->profiles
& chunk_type
)
2626 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2627 struct btrfs_balance_args
*bargs
)
2629 struct btrfs_block_group_cache
*cache
;
2630 u64 chunk_used
, user_thresh
;
2633 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2634 chunk_used
= btrfs_block_group_used(&cache
->item
);
2636 if (bargs
->usage
== 0)
2638 else if (bargs
->usage
> 100)
2639 user_thresh
= cache
->key
.offset
;
2641 user_thresh
= div_factor_fine(cache
->key
.offset
,
2644 if (chunk_used
< user_thresh
)
2647 btrfs_put_block_group(cache
);
2651 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2652 struct btrfs_chunk
*chunk
,
2653 struct btrfs_balance_args
*bargs
)
2655 struct btrfs_stripe
*stripe
;
2656 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2659 for (i
= 0; i
< num_stripes
; i
++) {
2660 stripe
= btrfs_stripe_nr(chunk
, i
);
2661 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2668 /* [pstart, pend) */
2669 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2670 struct btrfs_chunk
*chunk
,
2672 struct btrfs_balance_args
*bargs
)
2674 struct btrfs_stripe
*stripe
;
2675 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2681 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2684 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2685 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)) {
2686 factor
= num_stripes
/ 2;
2687 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID5
) {
2688 factor
= num_stripes
- 1;
2689 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID6
) {
2690 factor
= num_stripes
- 2;
2692 factor
= num_stripes
;
2695 for (i
= 0; i
< num_stripes
; i
++) {
2696 stripe
= btrfs_stripe_nr(chunk
, i
);
2697 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2700 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2701 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2702 do_div(stripe_length
, factor
);
2704 if (stripe_offset
< bargs
->pend
&&
2705 stripe_offset
+ stripe_length
> bargs
->pstart
)
2712 /* [vstart, vend) */
2713 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2714 struct btrfs_chunk
*chunk
,
2716 struct btrfs_balance_args
*bargs
)
2718 if (chunk_offset
< bargs
->vend
&&
2719 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2720 /* at least part of the chunk is inside this vrange */
2726 static int chunk_soft_convert_filter(u64 chunk_type
,
2727 struct btrfs_balance_args
*bargs
)
2729 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2732 chunk_type
= chunk_to_extended(chunk_type
) &
2733 BTRFS_EXTENDED_PROFILE_MASK
;
2735 if (bargs
->target
== chunk_type
)
2741 static int should_balance_chunk(struct btrfs_root
*root
,
2742 struct extent_buffer
*leaf
,
2743 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2745 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2746 struct btrfs_balance_args
*bargs
= NULL
;
2747 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2750 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2751 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2755 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2756 bargs
= &bctl
->data
;
2757 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2759 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2760 bargs
= &bctl
->meta
;
2762 /* profiles filter */
2763 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2764 chunk_profiles_filter(chunk_type
, bargs
)) {
2769 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2770 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2775 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2776 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2780 /* drange filter, makes sense only with devid filter */
2781 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2782 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2787 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2788 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2792 /* soft profile changing mode */
2793 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2794 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2801 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2803 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2804 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2805 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2806 struct list_head
*devices
;
2807 struct btrfs_device
*device
;
2810 struct btrfs_chunk
*chunk
;
2811 struct btrfs_path
*path
;
2812 struct btrfs_key key
;
2813 struct btrfs_key found_key
;
2814 struct btrfs_trans_handle
*trans
;
2815 struct extent_buffer
*leaf
;
2818 int enospc_errors
= 0;
2819 bool counting
= true;
2821 /* step one make some room on all the devices */
2822 devices
= &fs_info
->fs_devices
->devices
;
2823 list_for_each_entry(device
, devices
, dev_list
) {
2824 old_size
= device
->total_bytes
;
2825 size_to_free
= div_factor(old_size
, 1);
2826 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2827 if (!device
->writeable
||
2828 device
->total_bytes
- device
->bytes_used
> size_to_free
||
2829 device
->is_tgtdev_for_dev_replace
)
2832 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2837 trans
= btrfs_start_transaction(dev_root
, 0);
2838 BUG_ON(IS_ERR(trans
));
2840 ret
= btrfs_grow_device(trans
, device
, old_size
);
2843 btrfs_end_transaction(trans
, dev_root
);
2846 /* step two, relocate all the chunks */
2847 path
= btrfs_alloc_path();
2853 /* zero out stat counters */
2854 spin_lock(&fs_info
->balance_lock
);
2855 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2856 spin_unlock(&fs_info
->balance_lock
);
2858 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2859 key
.offset
= (u64
)-1;
2860 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2863 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2864 atomic_read(&fs_info
->balance_cancel_req
)) {
2869 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2874 * this shouldn't happen, it means the last relocate
2878 BUG(); /* FIXME break ? */
2880 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2881 BTRFS_CHUNK_ITEM_KEY
);
2887 leaf
= path
->nodes
[0];
2888 slot
= path
->slots
[0];
2889 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2891 if (found_key
.objectid
!= key
.objectid
)
2894 /* chunk zero is special */
2895 if (found_key
.offset
== 0)
2898 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2901 spin_lock(&fs_info
->balance_lock
);
2902 bctl
->stat
.considered
++;
2903 spin_unlock(&fs_info
->balance_lock
);
2906 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2908 btrfs_release_path(path
);
2913 spin_lock(&fs_info
->balance_lock
);
2914 bctl
->stat
.expected
++;
2915 spin_unlock(&fs_info
->balance_lock
);
2919 ret
= btrfs_relocate_chunk(chunk_root
,
2920 chunk_root
->root_key
.objectid
,
2923 if (ret
&& ret
!= -ENOSPC
)
2925 if (ret
== -ENOSPC
) {
2928 spin_lock(&fs_info
->balance_lock
);
2929 bctl
->stat
.completed
++;
2930 spin_unlock(&fs_info
->balance_lock
);
2933 key
.offset
= found_key
.offset
- 1;
2937 btrfs_release_path(path
);
2942 btrfs_free_path(path
);
2943 if (enospc_errors
) {
2944 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2954 * alloc_profile_is_valid - see if a given profile is valid and reduced
2955 * @flags: profile to validate
2956 * @extended: if true @flags is treated as an extended profile
2958 static int alloc_profile_is_valid(u64 flags
, int extended
)
2960 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
2961 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
2963 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
2965 /* 1) check that all other bits are zeroed */
2969 /* 2) see if profile is reduced */
2971 return !extended
; /* "0" is valid for usual profiles */
2973 /* true if exactly one bit set */
2974 return (flags
& (flags
- 1)) == 0;
2977 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
2979 /* cancel requested || normal exit path */
2980 return atomic_read(&fs_info
->balance_cancel_req
) ||
2981 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
2982 atomic_read(&fs_info
->balance_cancel_req
) == 0);
2985 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
2989 unset_balance_control(fs_info
);
2990 ret
= del_balance_item(fs_info
->tree_root
);
2993 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
2996 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
2997 struct btrfs_ioctl_balance_args
*bargs
);
3000 * Should be called with both balance and volume mutexes held
3002 int btrfs_balance(struct btrfs_balance_control
*bctl
,
3003 struct btrfs_ioctl_balance_args
*bargs
)
3005 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
3012 if (btrfs_fs_closing(fs_info
) ||
3013 atomic_read(&fs_info
->balance_pause_req
) ||
3014 atomic_read(&fs_info
->balance_cancel_req
)) {
3019 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3020 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3024 * In case of mixed groups both data and meta should be picked,
3025 * and identical options should be given for both of them.
3027 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3028 if (mixed
&& (bctl
->flags
& allowed
)) {
3029 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3030 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3031 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3032 printk(KERN_ERR
"btrfs: with mixed groups data and "
3033 "metadata balance options must be the same\n");
3039 num_devices
= fs_info
->fs_devices
->num_devices
;
3040 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3041 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3042 BUG_ON(num_devices
< 1);
3045 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3046 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
3047 if (num_devices
== 1)
3048 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3049 else if (num_devices
< 4)
3050 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3052 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
3053 BTRFS_BLOCK_GROUP_RAID10
|
3054 BTRFS_BLOCK_GROUP_RAID5
|
3055 BTRFS_BLOCK_GROUP_RAID6
);
3057 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3058 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3059 (bctl
->data
.target
& ~allowed
))) {
3060 printk(KERN_ERR
"btrfs: unable to start balance with target "
3061 "data profile %llu\n",
3062 (unsigned long long)bctl
->data
.target
);
3066 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3067 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3068 (bctl
->meta
.target
& ~allowed
))) {
3069 printk(KERN_ERR
"btrfs: unable to start balance with target "
3070 "metadata profile %llu\n",
3071 (unsigned long long)bctl
->meta
.target
);
3075 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3076 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3077 (bctl
->sys
.target
& ~allowed
))) {
3078 printk(KERN_ERR
"btrfs: unable to start balance with target "
3079 "system profile %llu\n",
3080 (unsigned long long)bctl
->sys
.target
);
3085 /* allow dup'ed data chunks only in mixed mode */
3086 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3087 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3088 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
3093 /* allow to reduce meta or sys integrity only if force set */
3094 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3095 BTRFS_BLOCK_GROUP_RAID10
|
3096 BTRFS_BLOCK_GROUP_RAID5
|
3097 BTRFS_BLOCK_GROUP_RAID6
;
3099 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3100 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3101 !(bctl
->sys
.target
& allowed
)) ||
3102 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3103 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3104 !(bctl
->meta
.target
& allowed
))) {
3105 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3106 printk(KERN_INFO
"btrfs: force reducing metadata "
3109 printk(KERN_ERR
"btrfs: balance will reduce metadata "
3110 "integrity, use force if you want this\n");
3116 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3117 int num_tolerated_disk_barrier_failures
;
3118 u64 target
= bctl
->sys
.target
;
3120 num_tolerated_disk_barrier_failures
=
3121 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3122 if (num_tolerated_disk_barrier_failures
> 0 &&
3124 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3125 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3126 num_tolerated_disk_barrier_failures
= 0;
3127 else if (num_tolerated_disk_barrier_failures
> 1 &&
3129 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3130 num_tolerated_disk_barrier_failures
= 1;
3132 fs_info
->num_tolerated_disk_barrier_failures
=
3133 num_tolerated_disk_barrier_failures
;
3136 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3137 if (ret
&& ret
!= -EEXIST
)
3140 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3141 BUG_ON(ret
== -EEXIST
);
3142 set_balance_control(bctl
);
3144 BUG_ON(ret
!= -EEXIST
);
3145 spin_lock(&fs_info
->balance_lock
);
3146 update_balance_args(bctl
);
3147 spin_unlock(&fs_info
->balance_lock
);
3150 atomic_inc(&fs_info
->balance_running
);
3151 mutex_unlock(&fs_info
->balance_mutex
);
3153 ret
= __btrfs_balance(fs_info
);
3155 mutex_lock(&fs_info
->balance_mutex
);
3156 atomic_dec(&fs_info
->balance_running
);
3159 memset(bargs
, 0, sizeof(*bargs
));
3160 update_ioctl_balance_args(fs_info
, 0, bargs
);
3163 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3164 balance_need_close(fs_info
))
3167 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3168 fs_info
->num_tolerated_disk_barrier_failures
=
3169 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3173 __cancel_balance(fs_info
);
3175 wake_up(&fs_info
->balance_wait_q
);
3179 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3180 __cancel_balance(fs_info
);
3183 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3188 static int balance_kthread(void *data
)
3190 struct btrfs_fs_info
*fs_info
= data
;
3193 mutex_lock(&fs_info
->volume_mutex
);
3194 mutex_lock(&fs_info
->balance_mutex
);
3196 if (fs_info
->balance_ctl
) {
3197 printk(KERN_INFO
"btrfs: continuing balance\n");
3198 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3201 mutex_unlock(&fs_info
->balance_mutex
);
3202 mutex_unlock(&fs_info
->volume_mutex
);
3207 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3209 struct task_struct
*tsk
;
3211 spin_lock(&fs_info
->balance_lock
);
3212 if (!fs_info
->balance_ctl
) {
3213 spin_unlock(&fs_info
->balance_lock
);
3216 spin_unlock(&fs_info
->balance_lock
);
3218 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3219 printk(KERN_INFO
"btrfs: force skipping balance\n");
3223 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3225 return PTR_ERR(tsk
);
3230 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3232 struct btrfs_balance_control
*bctl
;
3233 struct btrfs_balance_item
*item
;
3234 struct btrfs_disk_balance_args disk_bargs
;
3235 struct btrfs_path
*path
;
3236 struct extent_buffer
*leaf
;
3237 struct btrfs_key key
;
3240 path
= btrfs_alloc_path();
3244 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3245 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3248 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3251 if (ret
> 0) { /* ret = -ENOENT; */
3256 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3262 leaf
= path
->nodes
[0];
3263 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3265 bctl
->fs_info
= fs_info
;
3266 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3267 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3269 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3270 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3271 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3272 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3273 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3274 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3276 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3278 mutex_lock(&fs_info
->volume_mutex
);
3279 mutex_lock(&fs_info
->balance_mutex
);
3281 set_balance_control(bctl
);
3283 mutex_unlock(&fs_info
->balance_mutex
);
3284 mutex_unlock(&fs_info
->volume_mutex
);
3286 btrfs_free_path(path
);
3290 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3294 mutex_lock(&fs_info
->balance_mutex
);
3295 if (!fs_info
->balance_ctl
) {
3296 mutex_unlock(&fs_info
->balance_mutex
);
3300 if (atomic_read(&fs_info
->balance_running
)) {
3301 atomic_inc(&fs_info
->balance_pause_req
);
3302 mutex_unlock(&fs_info
->balance_mutex
);
3304 wait_event(fs_info
->balance_wait_q
,
3305 atomic_read(&fs_info
->balance_running
) == 0);
3307 mutex_lock(&fs_info
->balance_mutex
);
3308 /* we are good with balance_ctl ripped off from under us */
3309 BUG_ON(atomic_read(&fs_info
->balance_running
));
3310 atomic_dec(&fs_info
->balance_pause_req
);
3315 mutex_unlock(&fs_info
->balance_mutex
);
3319 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3321 mutex_lock(&fs_info
->balance_mutex
);
3322 if (!fs_info
->balance_ctl
) {
3323 mutex_unlock(&fs_info
->balance_mutex
);
3327 atomic_inc(&fs_info
->balance_cancel_req
);
3329 * if we are running just wait and return, balance item is
3330 * deleted in btrfs_balance in this case
3332 if (atomic_read(&fs_info
->balance_running
)) {
3333 mutex_unlock(&fs_info
->balance_mutex
);
3334 wait_event(fs_info
->balance_wait_q
,
3335 atomic_read(&fs_info
->balance_running
) == 0);
3336 mutex_lock(&fs_info
->balance_mutex
);
3338 /* __cancel_balance needs volume_mutex */
3339 mutex_unlock(&fs_info
->balance_mutex
);
3340 mutex_lock(&fs_info
->volume_mutex
);
3341 mutex_lock(&fs_info
->balance_mutex
);
3343 if (fs_info
->balance_ctl
)
3344 __cancel_balance(fs_info
);
3346 mutex_unlock(&fs_info
->volume_mutex
);
3349 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3350 atomic_dec(&fs_info
->balance_cancel_req
);
3351 mutex_unlock(&fs_info
->balance_mutex
);
3356 * shrinking a device means finding all of the device extents past
3357 * the new size, and then following the back refs to the chunks.
3358 * The chunk relocation code actually frees the device extent
3360 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3362 struct btrfs_trans_handle
*trans
;
3363 struct btrfs_root
*root
= device
->dev_root
;
3364 struct btrfs_dev_extent
*dev_extent
= NULL
;
3365 struct btrfs_path
*path
;
3373 bool retried
= false;
3374 struct extent_buffer
*l
;
3375 struct btrfs_key key
;
3376 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3377 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3378 u64 old_size
= device
->total_bytes
;
3379 u64 diff
= device
->total_bytes
- new_size
;
3381 if (device
->is_tgtdev_for_dev_replace
)
3384 path
= btrfs_alloc_path();
3392 device
->total_bytes
= new_size
;
3393 if (device
->writeable
) {
3394 device
->fs_devices
->total_rw_bytes
-= diff
;
3395 spin_lock(&root
->fs_info
->free_chunk_lock
);
3396 root
->fs_info
->free_chunk_space
-= diff
;
3397 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3399 unlock_chunks(root
);
3402 key
.objectid
= device
->devid
;
3403 key
.offset
= (u64
)-1;
3404 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3407 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3411 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3416 btrfs_release_path(path
);
3421 slot
= path
->slots
[0];
3422 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3424 if (key
.objectid
!= device
->devid
) {
3425 btrfs_release_path(path
);
3429 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3430 length
= btrfs_dev_extent_length(l
, dev_extent
);
3432 if (key
.offset
+ length
<= new_size
) {
3433 btrfs_release_path(path
);
3437 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3438 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3439 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3440 btrfs_release_path(path
);
3442 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3444 if (ret
&& ret
!= -ENOSPC
)
3448 } while (key
.offset
-- > 0);
3450 if (failed
&& !retried
) {
3454 } else if (failed
&& retried
) {
3458 device
->total_bytes
= old_size
;
3459 if (device
->writeable
)
3460 device
->fs_devices
->total_rw_bytes
+= diff
;
3461 spin_lock(&root
->fs_info
->free_chunk_lock
);
3462 root
->fs_info
->free_chunk_space
+= diff
;
3463 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3464 unlock_chunks(root
);
3468 /* Shrinking succeeded, else we would be at "done". */
3469 trans
= btrfs_start_transaction(root
, 0);
3470 if (IS_ERR(trans
)) {
3471 ret
= PTR_ERR(trans
);
3477 device
->disk_total_bytes
= new_size
;
3478 /* Now btrfs_update_device() will change the on-disk size. */
3479 ret
= btrfs_update_device(trans
, device
);
3481 unlock_chunks(root
);
3482 btrfs_end_transaction(trans
, root
);
3485 WARN_ON(diff
> old_total
);
3486 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3487 unlock_chunks(root
);
3488 btrfs_end_transaction(trans
, root
);
3490 btrfs_free_path(path
);
3494 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3495 struct btrfs_key
*key
,
3496 struct btrfs_chunk
*chunk
, int item_size
)
3498 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3499 struct btrfs_disk_key disk_key
;
3503 array_size
= btrfs_super_sys_array_size(super_copy
);
3504 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3507 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3508 btrfs_cpu_key_to_disk(&disk_key
, key
);
3509 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3510 ptr
+= sizeof(disk_key
);
3511 memcpy(ptr
, chunk
, item_size
);
3512 item_size
+= sizeof(disk_key
);
3513 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3518 * sort the devices in descending order by max_avail, total_avail
3520 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3522 const struct btrfs_device_info
*di_a
= a
;
3523 const struct btrfs_device_info
*di_b
= b
;
3525 if (di_a
->max_avail
> di_b
->max_avail
)
3527 if (di_a
->max_avail
< di_b
->max_avail
)
3529 if (di_a
->total_avail
> di_b
->total_avail
)
3531 if (di_a
->total_avail
< di_b
->total_avail
)
3536 struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
3538 * sub_stripes info for map,
3539 * dev_stripes -- stripes per dev, 2 for DUP, 1 other wise
3540 * devs_max -- max devices per stripe, 0 for unlimited
3541 * devs_min -- min devices per stripe
3542 * devs_increment -- ndevs must be a multiple of this
3543 * ncopies -- how many copies of the data we have
3545 { 2, 1, 0, 4, 2, 2 /* raid10 */ },
3546 { 1, 1, 2, 2, 2, 2 /* raid1 */ },
3547 { 1, 2, 1, 1, 1, 2 /* dup */ },
3548 { 1, 1, 0, 2, 1, 1 /* raid0 */ },
3549 { 1, 1, 1, 1, 1, 1 /* single */ },
3550 { 1, 1, 0, 2, 1, 2 /* raid5 */ },
3551 { 1, 1, 0, 3, 1, 3 /* raid6 */ },
3554 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
3556 /* TODO allow them to set a preferred stripe size */
3560 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
3564 if (!(type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)))
3567 features
= btrfs_super_incompat_flags(info
->super_copy
);
3568 if (features
& BTRFS_FEATURE_INCOMPAT_RAID56
)
3571 features
|= BTRFS_FEATURE_INCOMPAT_RAID56
;
3572 btrfs_set_super_incompat_flags(info
->super_copy
, features
);
3573 printk(KERN_INFO
"btrfs: setting RAID5/6 feature flag\n");
3576 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3577 struct btrfs_root
*extent_root
,
3578 struct map_lookup
**map_ret
,
3579 u64
*num_bytes_out
, u64
*stripe_size_out
,
3580 u64 start
, u64 type
)
3582 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3583 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3584 struct list_head
*cur
;
3585 struct map_lookup
*map
= NULL
;
3586 struct extent_map_tree
*em_tree
;
3587 struct extent_map
*em
;
3588 struct btrfs_device_info
*devices_info
= NULL
;
3590 int num_stripes
; /* total number of stripes to allocate */
3591 int data_stripes
; /* number of stripes that count for
3593 int sub_stripes
; /* sub_stripes info for map */
3594 int dev_stripes
; /* stripes per dev */
3595 int devs_max
; /* max devs to use */
3596 int devs_min
; /* min devs needed */
3597 int devs_increment
; /* ndevs has to be a multiple of this */
3598 int ncopies
; /* how many copies to data has */
3600 u64 max_stripe_size
;
3604 u64 raid_stripe_len
= BTRFS_STRIPE_LEN
;
3610 BUG_ON(!alloc_profile_is_valid(type
, 0));
3612 if (list_empty(&fs_devices
->alloc_list
))
3615 index
= __get_raid_index(type
);
3617 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
3618 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
3619 devs_max
= btrfs_raid_array
[index
].devs_max
;
3620 devs_min
= btrfs_raid_array
[index
].devs_min
;
3621 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
3622 ncopies
= btrfs_raid_array
[index
].ncopies
;
3624 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3625 max_stripe_size
= 1024 * 1024 * 1024;
3626 max_chunk_size
= 10 * max_stripe_size
;
3627 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3628 /* for larger filesystems, use larger metadata chunks */
3629 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3630 max_stripe_size
= 1024 * 1024 * 1024;
3632 max_stripe_size
= 256 * 1024 * 1024;
3633 max_chunk_size
= max_stripe_size
;
3634 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3635 max_stripe_size
= 32 * 1024 * 1024;
3636 max_chunk_size
= 2 * max_stripe_size
;
3638 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3643 /* we don't want a chunk larger than 10% of writeable space */
3644 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3647 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3652 cur
= fs_devices
->alloc_list
.next
;
3655 * in the first pass through the devices list, we gather information
3656 * about the available holes on each device.
3659 while (cur
!= &fs_devices
->alloc_list
) {
3660 struct btrfs_device
*device
;
3664 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3668 if (!device
->writeable
) {
3670 "btrfs: read-only device in alloc_list\n");
3674 if (!device
->in_fs_metadata
||
3675 device
->is_tgtdev_for_dev_replace
)
3678 if (device
->total_bytes
> device
->bytes_used
)
3679 total_avail
= device
->total_bytes
- device
->bytes_used
;
3683 /* If there is no space on this device, skip it. */
3684 if (total_avail
== 0)
3687 ret
= find_free_dev_extent(device
,
3688 max_stripe_size
* dev_stripes
,
3689 &dev_offset
, &max_avail
);
3690 if (ret
&& ret
!= -ENOSPC
)
3694 max_avail
= max_stripe_size
* dev_stripes
;
3696 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3699 devices_info
[ndevs
].dev_offset
= dev_offset
;
3700 devices_info
[ndevs
].max_avail
= max_avail
;
3701 devices_info
[ndevs
].total_avail
= total_avail
;
3702 devices_info
[ndevs
].dev
= device
;
3704 WARN_ON(ndevs
> fs_devices
->rw_devices
);
3708 * now sort the devices by hole size / available space
3710 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3711 btrfs_cmp_device_info
, NULL
);
3713 /* round down to number of usable stripes */
3714 ndevs
-= ndevs
% devs_increment
;
3716 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3721 if (devs_max
&& ndevs
> devs_max
)
3724 * the primary goal is to maximize the number of stripes, so use as many
3725 * devices as possible, even if the stripes are not maximum sized.
3727 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3728 num_stripes
= ndevs
* dev_stripes
;
3731 * this will have to be fixed for RAID1 and RAID10 over
3734 data_stripes
= num_stripes
/ ncopies
;
3736 if (stripe_size
* ndevs
> max_chunk_size
* ncopies
) {
3737 stripe_size
= max_chunk_size
* ncopies
;
3738 do_div(stripe_size
, ndevs
);
3740 if (type
& BTRFS_BLOCK_GROUP_RAID5
) {
3741 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 1,
3742 btrfs_super_stripesize(info
->super_copy
));
3743 data_stripes
= num_stripes
- 1;
3745 if (type
& BTRFS_BLOCK_GROUP_RAID6
) {
3746 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 2,
3747 btrfs_super_stripesize(info
->super_copy
));
3748 data_stripes
= num_stripes
- 2;
3750 do_div(stripe_size
, dev_stripes
);
3752 /* align to BTRFS_STRIPE_LEN */
3753 do_div(stripe_size
, raid_stripe_len
);
3754 stripe_size
*= raid_stripe_len
;
3756 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3761 map
->num_stripes
= num_stripes
;
3763 for (i
= 0; i
< ndevs
; ++i
) {
3764 for (j
= 0; j
< dev_stripes
; ++j
) {
3765 int s
= i
* dev_stripes
+ j
;
3766 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3767 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3771 map
->sector_size
= extent_root
->sectorsize
;
3772 map
->stripe_len
= raid_stripe_len
;
3773 map
->io_align
= raid_stripe_len
;
3774 map
->io_width
= raid_stripe_len
;
3776 map
->sub_stripes
= sub_stripes
;
3779 num_bytes
= stripe_size
* data_stripes
;
3781 *stripe_size_out
= stripe_size
;
3782 *num_bytes_out
= num_bytes
;
3784 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3786 em
= alloc_extent_map();
3791 em
->bdev
= (struct block_device
*)map
;
3793 em
->len
= num_bytes
;
3794 em
->block_start
= 0;
3795 em
->block_len
= em
->len
;
3797 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3798 write_lock(&em_tree
->lock
);
3799 ret
= add_extent_mapping(em_tree
, em
);
3800 write_unlock(&em_tree
->lock
);
3801 free_extent_map(em
);
3805 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3806 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3811 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3812 struct btrfs_device
*device
;
3815 device
= map
->stripes
[i
].dev
;
3816 dev_offset
= map
->stripes
[i
].physical
;
3818 ret
= btrfs_alloc_dev_extent(trans
, device
,
3819 info
->chunk_root
->root_key
.objectid
,
3820 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3821 start
, dev_offset
, stripe_size
);
3823 btrfs_abort_transaction(trans
, extent_root
, ret
);
3828 check_raid56_incompat_flag(extent_root
->fs_info
, type
);
3830 kfree(devices_info
);
3835 kfree(devices_info
);
3839 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3840 struct btrfs_root
*extent_root
,
3841 struct map_lookup
*map
, u64 chunk_offset
,
3842 u64 chunk_size
, u64 stripe_size
)
3845 struct btrfs_key key
;
3846 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3847 struct btrfs_device
*device
;
3848 struct btrfs_chunk
*chunk
;
3849 struct btrfs_stripe
*stripe
;
3850 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3854 chunk
= kzalloc(item_size
, GFP_NOFS
);
3859 while (index
< map
->num_stripes
) {
3860 device
= map
->stripes
[index
].dev
;
3861 device
->bytes_used
+= stripe_size
;
3862 ret
= btrfs_update_device(trans
, device
);
3868 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3869 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3871 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3874 stripe
= &chunk
->stripe
;
3875 while (index
< map
->num_stripes
) {
3876 device
= map
->stripes
[index
].dev
;
3877 dev_offset
= map
->stripes
[index
].physical
;
3879 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3880 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3881 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3886 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3887 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3888 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3889 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3890 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3891 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3892 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3893 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3894 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3896 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3897 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3898 key
.offset
= chunk_offset
;
3900 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3902 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3904 * TODO: Cleanup of inserted chunk root in case of
3907 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
3917 * Chunk allocation falls into two parts. The first part does works
3918 * that make the new allocated chunk useable, but not do any operation
3919 * that modifies the chunk tree. The second part does the works that
3920 * require modifying the chunk tree. This division is important for the
3921 * bootstrap process of adding storage to a seed btrfs.
3923 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3924 struct btrfs_root
*extent_root
, u64 type
)
3929 struct map_lookup
*map
;
3930 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3933 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3938 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3939 &stripe_size
, chunk_offset
, type
);
3943 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3944 chunk_size
, stripe_size
);
3950 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3951 struct btrfs_root
*root
,
3952 struct btrfs_device
*device
)
3955 u64 sys_chunk_offset
;
3959 u64 sys_stripe_size
;
3961 struct map_lookup
*map
;
3962 struct map_lookup
*sys_map
;
3963 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3964 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3967 ret
= find_next_chunk(fs_info
->chunk_root
,
3968 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3972 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3973 fs_info
->avail_metadata_alloc_bits
;
3974 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3976 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3977 &stripe_size
, chunk_offset
, alloc_profile
);
3981 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3983 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3984 fs_info
->avail_system_alloc_bits
;
3985 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3987 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3988 &sys_chunk_size
, &sys_stripe_size
,
3989 sys_chunk_offset
, alloc_profile
);
3991 btrfs_abort_transaction(trans
, root
, ret
);
3995 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3997 btrfs_abort_transaction(trans
, root
, ret
);
4002 * Modifying chunk tree needs allocating new blocks from both
4003 * system block group and metadata block group. So we only can
4004 * do operations require modifying the chunk tree after both
4005 * block groups were created.
4007 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
4008 chunk_size
, stripe_size
);
4010 btrfs_abort_transaction(trans
, root
, ret
);
4014 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
4015 sys_chunk_offset
, sys_chunk_size
,
4018 btrfs_abort_transaction(trans
, root
, ret
);
4025 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
4027 struct extent_map
*em
;
4028 struct map_lookup
*map
;
4029 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4033 read_lock(&map_tree
->map_tree
.lock
);
4034 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
4035 read_unlock(&map_tree
->map_tree
.lock
);
4039 if (btrfs_test_opt(root
, DEGRADED
)) {
4040 free_extent_map(em
);
4044 map
= (struct map_lookup
*)em
->bdev
;
4045 for (i
= 0; i
< map
->num_stripes
; i
++) {
4046 if (!map
->stripes
[i
].dev
->writeable
) {
4051 free_extent_map(em
);
4055 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
4057 extent_map_tree_init(&tree
->map_tree
);
4060 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
4062 struct extent_map
*em
;
4065 write_lock(&tree
->map_tree
.lock
);
4066 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
4068 remove_extent_mapping(&tree
->map_tree
, em
);
4069 write_unlock(&tree
->map_tree
.lock
);
4074 free_extent_map(em
);
4075 /* once for the tree */
4076 free_extent_map(em
);
4080 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
4082 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4083 struct extent_map
*em
;
4084 struct map_lookup
*map
;
4085 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4088 read_lock(&em_tree
->lock
);
4089 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4090 read_unlock(&em_tree
->lock
);
4093 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4094 map
= (struct map_lookup
*)em
->bdev
;
4095 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4096 ret
= map
->num_stripes
;
4097 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4098 ret
= map
->sub_stripes
;
4099 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
4101 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
4105 free_extent_map(em
);
4107 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
4108 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))
4110 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
4115 unsigned long btrfs_full_stripe_len(struct btrfs_root
*root
,
4116 struct btrfs_mapping_tree
*map_tree
,
4119 struct extent_map
*em
;
4120 struct map_lookup
*map
;
4121 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4122 unsigned long len
= root
->sectorsize
;
4124 read_lock(&em_tree
->lock
);
4125 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4126 read_unlock(&em_tree
->lock
);
4129 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4130 map
= (struct map_lookup
*)em
->bdev
;
4131 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4132 BTRFS_BLOCK_GROUP_RAID6
)) {
4133 len
= map
->stripe_len
* nr_data_stripes(map
);
4135 free_extent_map(em
);
4139 int btrfs_is_parity_mirror(struct btrfs_mapping_tree
*map_tree
,
4140 u64 logical
, u64 len
, int mirror_num
)
4142 struct extent_map
*em
;
4143 struct map_lookup
*map
;
4144 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4147 read_lock(&em_tree
->lock
);
4148 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4149 read_unlock(&em_tree
->lock
);
4152 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4153 map
= (struct map_lookup
*)em
->bdev
;
4154 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4155 BTRFS_BLOCK_GROUP_RAID6
))
4157 free_extent_map(em
);
4161 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
4162 struct map_lookup
*map
, int first
, int num
,
4163 int optimal
, int dev_replace_is_ongoing
)
4167 struct btrfs_device
*srcdev
;
4169 if (dev_replace_is_ongoing
&&
4170 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
4171 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
4172 srcdev
= fs_info
->dev_replace
.srcdev
;
4177 * try to avoid the drive that is the source drive for a
4178 * dev-replace procedure, only choose it if no other non-missing
4179 * mirror is available
4181 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
4182 if (map
->stripes
[optimal
].dev
->bdev
&&
4183 (tolerance
|| map
->stripes
[optimal
].dev
!= srcdev
))
4185 for (i
= first
; i
< first
+ num
; i
++) {
4186 if (map
->stripes
[i
].dev
->bdev
&&
4187 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
4192 /* we couldn't find one that doesn't fail. Just return something
4193 * and the io error handling code will clean up eventually
4198 static inline int parity_smaller(u64 a
, u64 b
)
4203 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4204 static void sort_parity_stripes(struct btrfs_bio
*bbio
, u64
*raid_map
)
4206 struct btrfs_bio_stripe s
;
4213 for (i
= 0; i
< bbio
->num_stripes
- 1; i
++) {
4214 if (parity_smaller(raid_map
[i
], raid_map
[i
+1])) {
4215 s
= bbio
->stripes
[i
];
4217 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
4218 raid_map
[i
] = raid_map
[i
+1];
4219 bbio
->stripes
[i
+1] = s
;
4227 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4228 u64 logical
, u64
*length
,
4229 struct btrfs_bio
**bbio_ret
,
4230 int mirror_num
, u64
**raid_map_ret
)
4232 struct extent_map
*em
;
4233 struct map_lookup
*map
;
4234 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4235 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4238 u64 stripe_end_offset
;
4243 u64
*raid_map
= NULL
;
4249 struct btrfs_bio
*bbio
= NULL
;
4250 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
4251 int dev_replace_is_ongoing
= 0;
4252 int num_alloc_stripes
;
4253 int patch_the_first_stripe_for_dev_replace
= 0;
4254 u64 physical_to_patch_in_first_stripe
= 0;
4255 u64 raid56_full_stripe_start
= (u64
)-1;
4257 read_lock(&em_tree
->lock
);
4258 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
4259 read_unlock(&em_tree
->lock
);
4262 printk(KERN_CRIT
"btrfs: unable to find logical %llu len %llu\n",
4263 (unsigned long long)logical
,
4264 (unsigned long long)*length
);
4268 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4269 map
= (struct map_lookup
*)em
->bdev
;
4270 offset
= logical
- em
->start
;
4272 if (mirror_num
> map
->num_stripes
)
4275 stripe_len
= map
->stripe_len
;
4278 * stripe_nr counts the total number of stripes we have to stride
4279 * to get to this block
4281 do_div(stripe_nr
, stripe_len
);
4283 stripe_offset
= stripe_nr
* stripe_len
;
4284 BUG_ON(offset
< stripe_offset
);
4286 /* stripe_offset is the offset of this block in its stripe*/
4287 stripe_offset
= offset
- stripe_offset
;
4289 /* if we're here for raid56, we need to know the stripe aligned start */
4290 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4291 unsigned long full_stripe_len
= stripe_len
* nr_data_stripes(map
);
4292 raid56_full_stripe_start
= offset
;
4294 /* allow a write of a full stripe, but make sure we don't
4295 * allow straddling of stripes
4297 do_div(raid56_full_stripe_start
, full_stripe_len
);
4298 raid56_full_stripe_start
*= full_stripe_len
;
4301 if (rw
& REQ_DISCARD
) {
4302 /* we don't discard raid56 yet */
4304 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4308 *length
= min_t(u64
, em
->len
- offset
, *length
);
4309 } else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
4311 /* For writes to RAID[56], allow a full stripeset across all disks.
4312 For other RAID types and for RAID[56] reads, just allow a single
4313 stripe (on a single disk). */
4314 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
) &&
4316 max_len
= stripe_len
* nr_data_stripes(map
) -
4317 (offset
- raid56_full_stripe_start
);
4319 /* we limit the length of each bio to what fits in a stripe */
4320 max_len
= stripe_len
- stripe_offset
;
4322 *length
= min_t(u64
, em
->len
- offset
, max_len
);
4324 *length
= em
->len
- offset
;
4327 /* This is for when we're called from btrfs_merge_bio_hook() and all
4328 it cares about is the length */
4332 btrfs_dev_replace_lock(dev_replace
);
4333 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
4334 if (!dev_replace_is_ongoing
)
4335 btrfs_dev_replace_unlock(dev_replace
);
4337 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
4338 !(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) &&
4339 dev_replace
->tgtdev
!= NULL
) {
4341 * in dev-replace case, for repair case (that's the only
4342 * case where the mirror is selected explicitly when
4343 * calling btrfs_map_block), blocks left of the left cursor
4344 * can also be read from the target drive.
4345 * For REQ_GET_READ_MIRRORS, the target drive is added as
4346 * the last one to the array of stripes. For READ, it also
4347 * needs to be supported using the same mirror number.
4348 * If the requested block is not left of the left cursor,
4349 * EIO is returned. This can happen because btrfs_num_copies()
4350 * returns one more in the dev-replace case.
4352 u64 tmp_length
= *length
;
4353 struct btrfs_bio
*tmp_bbio
= NULL
;
4354 int tmp_num_stripes
;
4355 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4356 int index_srcdev
= 0;
4358 u64 physical_of_found
= 0;
4360 ret
= __btrfs_map_block(fs_info
, REQ_GET_READ_MIRRORS
,
4361 logical
, &tmp_length
, &tmp_bbio
, 0, NULL
);
4363 WARN_ON(tmp_bbio
!= NULL
);
4367 tmp_num_stripes
= tmp_bbio
->num_stripes
;
4368 if (mirror_num
> tmp_num_stripes
) {
4370 * REQ_GET_READ_MIRRORS does not contain this
4371 * mirror, that means that the requested area
4372 * is not left of the left cursor
4380 * process the rest of the function using the mirror_num
4381 * of the source drive. Therefore look it up first.
4382 * At the end, patch the device pointer to the one of the
4385 for (i
= 0; i
< tmp_num_stripes
; i
++) {
4386 if (tmp_bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4388 * In case of DUP, in order to keep it
4389 * simple, only add the mirror with the
4390 * lowest physical address
4393 physical_of_found
<=
4394 tmp_bbio
->stripes
[i
].physical
)
4399 tmp_bbio
->stripes
[i
].physical
;
4404 mirror_num
= index_srcdev
+ 1;
4405 patch_the_first_stripe_for_dev_replace
= 1;
4406 physical_to_patch_in_first_stripe
= physical_of_found
;
4415 } else if (mirror_num
> map
->num_stripes
) {
4421 stripe_nr_orig
= stripe_nr
;
4422 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
4423 (~(map
->stripe_len
- 1));
4424 do_div(stripe_nr_end
, map
->stripe_len
);
4425 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
4428 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4429 if (rw
& REQ_DISCARD
)
4430 num_stripes
= min_t(u64
, map
->num_stripes
,
4431 stripe_nr_end
- stripe_nr_orig
);
4432 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4433 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
4434 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
))
4435 num_stripes
= map
->num_stripes
;
4436 else if (mirror_num
)
4437 stripe_index
= mirror_num
- 1;
4439 stripe_index
= find_live_mirror(fs_info
, map
, 0,
4441 current
->pid
% map
->num_stripes
,
4442 dev_replace_is_ongoing
);
4443 mirror_num
= stripe_index
+ 1;
4446 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
4447 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) {
4448 num_stripes
= map
->num_stripes
;
4449 } else if (mirror_num
) {
4450 stripe_index
= mirror_num
- 1;
4455 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4456 int factor
= map
->num_stripes
/ map
->sub_stripes
;
4458 stripe_index
= do_div(stripe_nr
, factor
);
4459 stripe_index
*= map
->sub_stripes
;
4461 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
4462 num_stripes
= map
->sub_stripes
;
4463 else if (rw
& REQ_DISCARD
)
4464 num_stripes
= min_t(u64
, map
->sub_stripes
*
4465 (stripe_nr_end
- stripe_nr_orig
),
4467 else if (mirror_num
)
4468 stripe_index
+= mirror_num
- 1;
4470 int old_stripe_index
= stripe_index
;
4471 stripe_index
= find_live_mirror(fs_info
, map
,
4473 map
->sub_stripes
, stripe_index
+
4474 current
->pid
% map
->sub_stripes
,
4475 dev_replace_is_ongoing
);
4476 mirror_num
= stripe_index
- old_stripe_index
+ 1;
4479 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4480 BTRFS_BLOCK_GROUP_RAID6
)) {
4483 if (bbio_ret
&& ((rw
& REQ_WRITE
) || mirror_num
> 1)
4487 /* push stripe_nr back to the start of the full stripe */
4488 stripe_nr
= raid56_full_stripe_start
;
4489 do_div(stripe_nr
, stripe_len
);
4491 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
4493 /* RAID[56] write or recovery. Return all stripes */
4494 num_stripes
= map
->num_stripes
;
4495 max_errors
= nr_parity_stripes(map
);
4497 raid_map
= kmalloc(sizeof(u64
) * num_stripes
,
4504 /* Work out the disk rotation on this stripe-set */
4506 rot
= do_div(tmp
, num_stripes
);
4508 /* Fill in the logical address of each stripe */
4509 tmp
= stripe_nr
* nr_data_stripes(map
);
4510 for (i
= 0; i
< nr_data_stripes(map
); i
++)
4511 raid_map
[(i
+rot
) % num_stripes
] =
4512 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
4514 raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
4515 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
4516 raid_map
[(i
+rot
+1) % num_stripes
] =
4519 *length
= map
->stripe_len
;
4524 * Mirror #0 or #1 means the original data block.
4525 * Mirror #2 is RAID5 parity block.
4526 * Mirror #3 is RAID6 Q block.
4528 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
4530 stripe_index
= nr_data_stripes(map
) +
4533 /* We distribute the parity blocks across stripes */
4534 tmp
= stripe_nr
+ stripe_index
;
4535 stripe_index
= do_div(tmp
, map
->num_stripes
);
4539 * after this do_div call, stripe_nr is the number of stripes
4540 * on this device we have to walk to find the data, and
4541 * stripe_index is the number of our device in the stripe array
4543 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4544 mirror_num
= stripe_index
+ 1;
4546 BUG_ON(stripe_index
>= map
->num_stripes
);
4548 num_alloc_stripes
= num_stripes
;
4549 if (dev_replace_is_ongoing
) {
4550 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
4551 num_alloc_stripes
<<= 1;
4552 if (rw
& REQ_GET_READ_MIRRORS
)
4553 num_alloc_stripes
++;
4555 bbio
= kzalloc(btrfs_bio_size(num_alloc_stripes
), GFP_NOFS
);
4560 atomic_set(&bbio
->error
, 0);
4562 if (rw
& REQ_DISCARD
) {
4564 int sub_stripes
= 0;
4565 u64 stripes_per_dev
= 0;
4566 u32 remaining_stripes
= 0;
4567 u32 last_stripe
= 0;
4570 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
4571 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4574 sub_stripes
= map
->sub_stripes
;
4576 factor
= map
->num_stripes
/ sub_stripes
;
4577 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
4580 &remaining_stripes
);
4581 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
4582 last_stripe
*= sub_stripes
;
4585 for (i
= 0; i
< num_stripes
; i
++) {
4586 bbio
->stripes
[i
].physical
=
4587 map
->stripes
[stripe_index
].physical
+
4588 stripe_offset
+ stripe_nr
* map
->stripe_len
;
4589 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
4591 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
4592 BTRFS_BLOCK_GROUP_RAID10
)) {
4593 bbio
->stripes
[i
].length
= stripes_per_dev
*
4596 if (i
/ sub_stripes
< remaining_stripes
)
4597 bbio
->stripes
[i
].length
+=
4601 * Special for the first stripe and
4604 * |-------|...|-------|
4608 if (i
< sub_stripes
)
4609 bbio
->stripes
[i
].length
-=
4612 if (stripe_index
>= last_stripe
&&
4613 stripe_index
<= (last_stripe
+
4615 bbio
->stripes
[i
].length
-=
4618 if (i
== sub_stripes
- 1)
4621 bbio
->stripes
[i
].length
= *length
;
4624 if (stripe_index
== map
->num_stripes
) {
4625 /* This could only happen for RAID0/10 */
4631 for (i
= 0; i
< num_stripes
; i
++) {
4632 bbio
->stripes
[i
].physical
=
4633 map
->stripes
[stripe_index
].physical
+
4635 stripe_nr
* map
->stripe_len
;
4636 bbio
->stripes
[i
].dev
=
4637 map
->stripes
[stripe_index
].dev
;
4642 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
)) {
4643 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4644 BTRFS_BLOCK_GROUP_RAID10
|
4645 BTRFS_BLOCK_GROUP_RAID5
|
4646 BTRFS_BLOCK_GROUP_DUP
)) {
4648 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
) {
4653 if (dev_replace_is_ongoing
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
4654 dev_replace
->tgtdev
!= NULL
) {
4655 int index_where_to_add
;
4656 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4659 * duplicate the write operations while the dev replace
4660 * procedure is running. Since the copying of the old disk
4661 * to the new disk takes place at run time while the
4662 * filesystem is mounted writable, the regular write
4663 * operations to the old disk have to be duplicated to go
4664 * to the new disk as well.
4665 * Note that device->missing is handled by the caller, and
4666 * that the write to the old disk is already set up in the
4669 index_where_to_add
= num_stripes
;
4670 for (i
= 0; i
< num_stripes
; i
++) {
4671 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4672 /* write to new disk, too */
4673 struct btrfs_bio_stripe
*new =
4674 bbio
->stripes
+ index_where_to_add
;
4675 struct btrfs_bio_stripe
*old
=
4678 new->physical
= old
->physical
;
4679 new->length
= old
->length
;
4680 new->dev
= dev_replace
->tgtdev
;
4681 index_where_to_add
++;
4685 num_stripes
= index_where_to_add
;
4686 } else if (dev_replace_is_ongoing
&& (rw
& REQ_GET_READ_MIRRORS
) &&
4687 dev_replace
->tgtdev
!= NULL
) {
4688 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4689 int index_srcdev
= 0;
4691 u64 physical_of_found
= 0;
4694 * During the dev-replace procedure, the target drive can
4695 * also be used to read data in case it is needed to repair
4696 * a corrupt block elsewhere. This is possible if the
4697 * requested area is left of the left cursor. In this area,
4698 * the target drive is a full copy of the source drive.
4700 for (i
= 0; i
< num_stripes
; i
++) {
4701 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4703 * In case of DUP, in order to keep it
4704 * simple, only add the mirror with the
4705 * lowest physical address
4708 physical_of_found
<=
4709 bbio
->stripes
[i
].physical
)
4713 physical_of_found
= bbio
->stripes
[i
].physical
;
4717 u64 length
= map
->stripe_len
;
4719 if (physical_of_found
+ length
<=
4720 dev_replace
->cursor_left
) {
4721 struct btrfs_bio_stripe
*tgtdev_stripe
=
4722 bbio
->stripes
+ num_stripes
;
4724 tgtdev_stripe
->physical
= physical_of_found
;
4725 tgtdev_stripe
->length
=
4726 bbio
->stripes
[index_srcdev
].length
;
4727 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
4735 bbio
->num_stripes
= num_stripes
;
4736 bbio
->max_errors
= max_errors
;
4737 bbio
->mirror_num
= mirror_num
;
4740 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4741 * mirror_num == num_stripes + 1 && dev_replace target drive is
4742 * available as a mirror
4744 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
4745 WARN_ON(num_stripes
> 1);
4746 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
4747 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
4748 bbio
->mirror_num
= map
->num_stripes
+ 1;
4751 sort_parity_stripes(bbio
, raid_map
);
4752 *raid_map_ret
= raid_map
;
4755 if (dev_replace_is_ongoing
)
4756 btrfs_dev_replace_unlock(dev_replace
);
4757 free_extent_map(em
);
4761 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4762 u64 logical
, u64
*length
,
4763 struct btrfs_bio
**bbio_ret
, int mirror_num
)
4765 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
4769 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
4770 u64 chunk_start
, u64 physical
, u64 devid
,
4771 u64
**logical
, int *naddrs
, int *stripe_len
)
4773 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4774 struct extent_map
*em
;
4775 struct map_lookup
*map
;
4783 read_lock(&em_tree
->lock
);
4784 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
4785 read_unlock(&em_tree
->lock
);
4787 BUG_ON(!em
|| em
->start
!= chunk_start
);
4788 map
= (struct map_lookup
*)em
->bdev
;
4791 rmap_len
= map
->stripe_len
;
4793 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4794 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
4795 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4796 do_div(length
, map
->num_stripes
);
4797 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4798 BTRFS_BLOCK_GROUP_RAID6
)) {
4799 do_div(length
, nr_data_stripes(map
));
4800 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
4803 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
4804 BUG_ON(!buf
); /* -ENOMEM */
4806 for (i
= 0; i
< map
->num_stripes
; i
++) {
4807 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
4809 if (map
->stripes
[i
].physical
> physical
||
4810 map
->stripes
[i
].physical
+ length
<= physical
)
4813 stripe_nr
= physical
- map
->stripes
[i
].physical
;
4814 do_div(stripe_nr
, map
->stripe_len
);
4816 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4817 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4818 do_div(stripe_nr
, map
->sub_stripes
);
4819 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4820 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4821 } /* else if RAID[56], multiply by nr_data_stripes().
4822 * Alternatively, just use rmap_len below instead of
4823 * map->stripe_len */
4825 bytenr
= chunk_start
+ stripe_nr
* rmap_len
;
4826 WARN_ON(nr
>= map
->num_stripes
);
4827 for (j
= 0; j
< nr
; j
++) {
4828 if (buf
[j
] == bytenr
)
4832 WARN_ON(nr
>= map
->num_stripes
);
4839 *stripe_len
= rmap_len
;
4841 free_extent_map(em
);
4845 static void *merge_stripe_index_into_bio_private(void *bi_private
,
4846 unsigned int stripe_index
)
4849 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4851 * The alternative solution (instead of stealing bits from the
4852 * pointer) would be to allocate an intermediate structure
4853 * that contains the old private pointer plus the stripe_index.
4855 BUG_ON((((uintptr_t)bi_private
) & 3) != 0);
4856 BUG_ON(stripe_index
> 3);
4857 return (void *)(((uintptr_t)bi_private
) | stripe_index
);
4860 static struct btrfs_bio
*extract_bbio_from_bio_private(void *bi_private
)
4862 return (struct btrfs_bio
*)(((uintptr_t)bi_private
) & ~((uintptr_t)3));
4865 static unsigned int extract_stripe_index_from_bio_private(void *bi_private
)
4867 return (unsigned int)((uintptr_t)bi_private
) & 3;
4870 static void btrfs_end_bio(struct bio
*bio
, int err
)
4872 struct btrfs_bio
*bbio
= extract_bbio_from_bio_private(bio
->bi_private
);
4873 int is_orig_bio
= 0;
4876 atomic_inc(&bbio
->error
);
4877 if (err
== -EIO
|| err
== -EREMOTEIO
) {
4878 unsigned int stripe_index
=
4879 extract_stripe_index_from_bio_private(
4881 struct btrfs_device
*dev
;
4883 BUG_ON(stripe_index
>= bbio
->num_stripes
);
4884 dev
= bbio
->stripes
[stripe_index
].dev
;
4886 if (bio
->bi_rw
& WRITE
)
4887 btrfs_dev_stat_inc(dev
,
4888 BTRFS_DEV_STAT_WRITE_ERRS
);
4890 btrfs_dev_stat_inc(dev
,
4891 BTRFS_DEV_STAT_READ_ERRS
);
4892 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
4893 btrfs_dev_stat_inc(dev
,
4894 BTRFS_DEV_STAT_FLUSH_ERRS
);
4895 btrfs_dev_stat_print_on_error(dev
);
4900 if (bio
== bbio
->orig_bio
)
4903 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4906 bio
= bbio
->orig_bio
;
4908 bio
->bi_private
= bbio
->private;
4909 bio
->bi_end_io
= bbio
->end_io
;
4910 bio
->bi_bdev
= (struct block_device
*)
4911 (unsigned long)bbio
->mirror_num
;
4912 /* only send an error to the higher layers if it is
4913 * beyond the tolerance of the btrfs bio
4915 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
4919 * this bio is actually up to date, we didn't
4920 * go over the max number of errors
4922 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
4927 bio_endio(bio
, err
);
4928 } else if (!is_orig_bio
) {
4933 struct async_sched
{
4936 struct btrfs_fs_info
*info
;
4937 struct btrfs_work work
;
4941 * see run_scheduled_bios for a description of why bios are collected for
4944 * This will add one bio to the pending list for a device and make sure
4945 * the work struct is scheduled.
4947 noinline
void btrfs_schedule_bio(struct btrfs_root
*root
,
4948 struct btrfs_device
*device
,
4949 int rw
, struct bio
*bio
)
4951 int should_queue
= 1;
4952 struct btrfs_pending_bios
*pending_bios
;
4954 if (device
->missing
|| !device
->bdev
) {
4955 bio_endio(bio
, -EIO
);
4959 /* don't bother with additional async steps for reads, right now */
4960 if (!(rw
& REQ_WRITE
)) {
4962 btrfsic_submit_bio(rw
, bio
);
4968 * nr_async_bios allows us to reliably return congestion to the
4969 * higher layers. Otherwise, the async bio makes it appear we have
4970 * made progress against dirty pages when we've really just put it
4971 * on a queue for later
4973 atomic_inc(&root
->fs_info
->nr_async_bios
);
4974 WARN_ON(bio
->bi_next
);
4975 bio
->bi_next
= NULL
;
4978 spin_lock(&device
->io_lock
);
4979 if (bio
->bi_rw
& REQ_SYNC
)
4980 pending_bios
= &device
->pending_sync_bios
;
4982 pending_bios
= &device
->pending_bios
;
4984 if (pending_bios
->tail
)
4985 pending_bios
->tail
->bi_next
= bio
;
4987 pending_bios
->tail
= bio
;
4988 if (!pending_bios
->head
)
4989 pending_bios
->head
= bio
;
4990 if (device
->running_pending
)
4993 spin_unlock(&device
->io_lock
);
4996 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
5000 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
5003 struct bio_vec
*prev
;
5004 struct request_queue
*q
= bdev_get_queue(bdev
);
5005 unsigned short max_sectors
= queue_max_sectors(q
);
5006 struct bvec_merge_data bvm
= {
5008 .bi_sector
= sector
,
5009 .bi_rw
= bio
->bi_rw
,
5012 if (bio
->bi_vcnt
== 0) {
5017 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
5018 if ((bio
->bi_size
>> 9) > max_sectors
)
5021 if (!q
->merge_bvec_fn
)
5024 bvm
.bi_size
= bio
->bi_size
- prev
->bv_len
;
5025 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
5030 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5031 struct bio
*bio
, u64 physical
, int dev_nr
,
5034 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
5036 bio
->bi_private
= bbio
;
5037 bio
->bi_private
= merge_stripe_index_into_bio_private(
5038 bio
->bi_private
, (unsigned int)dev_nr
);
5039 bio
->bi_end_io
= btrfs_end_bio
;
5040 bio
->bi_sector
= physical
>> 9;
5043 struct rcu_string
*name
;
5046 name
= rcu_dereference(dev
->name
);
5047 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5048 "(%s id %llu), size=%u\n", rw
,
5049 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
5050 name
->str
, dev
->devid
, bio
->bi_size
);
5054 bio
->bi_bdev
= dev
->bdev
;
5056 btrfs_schedule_bio(root
, dev
, rw
, bio
);
5058 btrfsic_submit_bio(rw
, bio
);
5061 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5062 struct bio
*first_bio
, struct btrfs_device
*dev
,
5063 int dev_nr
, int rw
, int async
)
5065 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
5067 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
5068 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
5071 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
5075 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
5076 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5077 bvec
->bv_offset
) < bvec
->bv_len
) {
5078 u64 len
= bio
->bi_size
;
5080 atomic_inc(&bbio
->stripes_pending
);
5081 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
5089 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
5093 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
5095 atomic_inc(&bbio
->error
);
5096 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5097 bio
->bi_private
= bbio
->private;
5098 bio
->bi_end_io
= bbio
->end_io
;
5099 bio
->bi_bdev
= (struct block_device
*)
5100 (unsigned long)bbio
->mirror_num
;
5101 bio
->bi_sector
= logical
>> 9;
5103 bio_endio(bio
, -EIO
);
5107 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
5108 int mirror_num
, int async_submit
)
5110 struct btrfs_device
*dev
;
5111 struct bio
*first_bio
= bio
;
5112 u64 logical
= (u64
)bio
->bi_sector
<< 9;
5115 u64
*raid_map
= NULL
;
5119 struct btrfs_bio
*bbio
= NULL
;
5121 length
= bio
->bi_size
;
5122 map_length
= length
;
5124 ret
= __btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
5125 mirror_num
, &raid_map
);
5126 if (ret
) /* -ENOMEM */
5129 total_devs
= bbio
->num_stripes
;
5130 bbio
->orig_bio
= first_bio
;
5131 bbio
->private = first_bio
->bi_private
;
5132 bbio
->end_io
= first_bio
->bi_end_io
;
5133 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
5136 /* In this case, map_length has been set to the length of
5137 a single stripe; not the whole write */
5139 return raid56_parity_write(root
, bio
, bbio
,
5140 raid_map
, map_length
);
5142 return raid56_parity_recover(root
, bio
, bbio
,
5143 raid_map
, map_length
,
5148 if (map_length
< length
) {
5149 printk(KERN_CRIT
"btrfs: mapping failed logical %llu bio len %llu "
5150 "len %llu\n", (unsigned long long)logical
,
5151 (unsigned long long)length
,
5152 (unsigned long long)map_length
);
5156 while (dev_nr
< total_devs
) {
5157 dev
= bbio
->stripes
[dev_nr
].dev
;
5158 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
5159 bbio_error(bbio
, first_bio
, logical
);
5165 * Check and see if we're ok with this bio based on it's size
5166 * and offset with the given device.
5168 if (!bio_size_ok(dev
->bdev
, first_bio
,
5169 bbio
->stripes
[dev_nr
].physical
>> 9)) {
5170 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
5171 dev_nr
, rw
, async_submit
);
5177 if (dev_nr
< total_devs
- 1) {
5178 bio
= bio_clone(first_bio
, GFP_NOFS
);
5179 BUG_ON(!bio
); /* -ENOMEM */
5184 submit_stripe_bio(root
, bbio
, bio
,
5185 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
5192 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
5195 struct btrfs_device
*device
;
5196 struct btrfs_fs_devices
*cur_devices
;
5198 cur_devices
= fs_info
->fs_devices
;
5199 while (cur_devices
) {
5201 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5202 device
= __find_device(&cur_devices
->devices
,
5207 cur_devices
= cur_devices
->seed
;
5212 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
5213 u64 devid
, u8
*dev_uuid
)
5215 struct btrfs_device
*device
;
5216 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5218 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
5221 list_add(&device
->dev_list
,
5222 &fs_devices
->devices
);
5223 device
->dev_root
= root
->fs_info
->dev_root
;
5224 device
->devid
= devid
;
5225 device
->work
.func
= pending_bios_fn
;
5226 device
->fs_devices
= fs_devices
;
5227 device
->missing
= 1;
5228 fs_devices
->num_devices
++;
5229 fs_devices
->missing_devices
++;
5230 spin_lock_init(&device
->io_lock
);
5231 INIT_LIST_HEAD(&device
->dev_alloc_list
);
5232 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
5236 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
5237 struct extent_buffer
*leaf
,
5238 struct btrfs_chunk
*chunk
)
5240 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5241 struct map_lookup
*map
;
5242 struct extent_map
*em
;
5246 u8 uuid
[BTRFS_UUID_SIZE
];
5251 logical
= key
->offset
;
5252 length
= btrfs_chunk_length(leaf
, chunk
);
5254 read_lock(&map_tree
->map_tree
.lock
);
5255 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
5256 read_unlock(&map_tree
->map_tree
.lock
);
5258 /* already mapped? */
5259 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
5260 free_extent_map(em
);
5263 free_extent_map(em
);
5266 em
= alloc_extent_map();
5269 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
5270 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
5272 free_extent_map(em
);
5276 em
->bdev
= (struct block_device
*)map
;
5277 em
->start
= logical
;
5280 em
->block_start
= 0;
5281 em
->block_len
= em
->len
;
5283 map
->num_stripes
= num_stripes
;
5284 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
5285 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
5286 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
5287 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
5288 map
->type
= btrfs_chunk_type(leaf
, chunk
);
5289 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
5290 for (i
= 0; i
< num_stripes
; i
++) {
5291 map
->stripes
[i
].physical
=
5292 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
5293 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
5294 read_extent_buffer(leaf
, uuid
, (unsigned long)
5295 btrfs_stripe_dev_uuid_nr(chunk
, i
),
5297 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
5299 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
5301 free_extent_map(em
);
5304 if (!map
->stripes
[i
].dev
) {
5305 map
->stripes
[i
].dev
=
5306 add_missing_dev(root
, devid
, uuid
);
5307 if (!map
->stripes
[i
].dev
) {
5309 free_extent_map(em
);
5313 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
5316 write_lock(&map_tree
->map_tree
.lock
);
5317 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
5318 write_unlock(&map_tree
->map_tree
.lock
);
5319 BUG_ON(ret
); /* Tree corruption */
5320 free_extent_map(em
);
5325 static void fill_device_from_item(struct extent_buffer
*leaf
,
5326 struct btrfs_dev_item
*dev_item
,
5327 struct btrfs_device
*device
)
5331 device
->devid
= btrfs_device_id(leaf
, dev_item
);
5332 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
5333 device
->total_bytes
= device
->disk_total_bytes
;
5334 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
5335 device
->type
= btrfs_device_type(leaf
, dev_item
);
5336 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
5337 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
5338 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
5339 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
5340 device
->is_tgtdev_for_dev_replace
= 0;
5342 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
5343 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
5346 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
5348 struct btrfs_fs_devices
*fs_devices
;
5351 BUG_ON(!mutex_is_locked(&uuid_mutex
));
5353 fs_devices
= root
->fs_info
->fs_devices
->seed
;
5354 while (fs_devices
) {
5355 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5359 fs_devices
= fs_devices
->seed
;
5362 fs_devices
= find_fsid(fsid
);
5368 fs_devices
= clone_fs_devices(fs_devices
);
5369 if (IS_ERR(fs_devices
)) {
5370 ret
= PTR_ERR(fs_devices
);
5374 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
5375 root
->fs_info
->bdev_holder
);
5377 free_fs_devices(fs_devices
);
5381 if (!fs_devices
->seeding
) {
5382 __btrfs_close_devices(fs_devices
);
5383 free_fs_devices(fs_devices
);
5388 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
5389 root
->fs_info
->fs_devices
->seed
= fs_devices
;
5394 static int read_one_dev(struct btrfs_root
*root
,
5395 struct extent_buffer
*leaf
,
5396 struct btrfs_dev_item
*dev_item
)
5398 struct btrfs_device
*device
;
5401 u8 fs_uuid
[BTRFS_UUID_SIZE
];
5402 u8 dev_uuid
[BTRFS_UUID_SIZE
];
5404 devid
= btrfs_device_id(leaf
, dev_item
);
5405 read_extent_buffer(leaf
, dev_uuid
,
5406 (unsigned long)btrfs_device_uuid(dev_item
),
5408 read_extent_buffer(leaf
, fs_uuid
,
5409 (unsigned long)btrfs_device_fsid(dev_item
),
5412 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
5413 ret
= open_seed_devices(root
, fs_uuid
);
5414 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
5418 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
5419 if (!device
|| !device
->bdev
) {
5420 if (!btrfs_test_opt(root
, DEGRADED
))
5424 printk(KERN_WARNING
"warning devid %llu missing\n",
5425 (unsigned long long)devid
);
5426 device
= add_missing_dev(root
, devid
, dev_uuid
);
5429 } else if (!device
->missing
) {
5431 * this happens when a device that was properly setup
5432 * in the device info lists suddenly goes bad.
5433 * device->bdev is NULL, and so we have to set
5434 * device->missing to one here
5436 root
->fs_info
->fs_devices
->missing_devices
++;
5437 device
->missing
= 1;
5441 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
5442 BUG_ON(device
->writeable
);
5443 if (device
->generation
!=
5444 btrfs_device_generation(leaf
, dev_item
))
5448 fill_device_from_item(leaf
, dev_item
, device
);
5449 device
->dev_root
= root
->fs_info
->dev_root
;
5450 device
->in_fs_metadata
= 1;
5451 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
5452 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
5453 spin_lock(&root
->fs_info
->free_chunk_lock
);
5454 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
5456 spin_unlock(&root
->fs_info
->free_chunk_lock
);
5462 int btrfs_read_sys_array(struct btrfs_root
*root
)
5464 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
5465 struct extent_buffer
*sb
;
5466 struct btrfs_disk_key
*disk_key
;
5467 struct btrfs_chunk
*chunk
;
5469 unsigned long sb_ptr
;
5475 struct btrfs_key key
;
5477 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
5478 BTRFS_SUPER_INFO_SIZE
);
5481 btrfs_set_buffer_uptodate(sb
);
5482 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
5484 * The sb extent buffer is artifical and just used to read the system array.
5485 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5486 * pages up-to-date when the page is larger: extent does not cover the
5487 * whole page and consequently check_page_uptodate does not find all
5488 * the page's extents up-to-date (the hole beyond sb),
5489 * write_extent_buffer then triggers a WARN_ON.
5491 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5492 * but sb spans only this function. Add an explicit SetPageUptodate call
5493 * to silence the warning eg. on PowerPC 64.
5495 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
5496 SetPageUptodate(sb
->pages
[0]);
5498 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
5499 array_size
= btrfs_super_sys_array_size(super_copy
);
5501 ptr
= super_copy
->sys_chunk_array
;
5502 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
5505 while (cur
< array_size
) {
5506 disk_key
= (struct btrfs_disk_key
*)ptr
;
5507 btrfs_disk_key_to_cpu(&key
, disk_key
);
5509 len
= sizeof(*disk_key
); ptr
+= len
;
5513 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
5514 chunk
= (struct btrfs_chunk
*)sb_ptr
;
5515 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
5518 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
5519 len
= btrfs_chunk_item_size(num_stripes
);
5528 free_extent_buffer(sb
);
5532 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
5534 struct btrfs_path
*path
;
5535 struct extent_buffer
*leaf
;
5536 struct btrfs_key key
;
5537 struct btrfs_key found_key
;
5541 root
= root
->fs_info
->chunk_root
;
5543 path
= btrfs_alloc_path();
5547 mutex_lock(&uuid_mutex
);
5550 /* first we search for all of the device items, and then we
5551 * read in all of the chunk items. This way we can create chunk
5552 * mappings that reference all of the devices that are afound
5554 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
5558 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5562 leaf
= path
->nodes
[0];
5563 slot
= path
->slots
[0];
5564 if (slot
>= btrfs_header_nritems(leaf
)) {
5565 ret
= btrfs_next_leaf(root
, path
);
5572 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5573 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5574 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
5576 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
5577 struct btrfs_dev_item
*dev_item
;
5578 dev_item
= btrfs_item_ptr(leaf
, slot
,
5579 struct btrfs_dev_item
);
5580 ret
= read_one_dev(root
, leaf
, dev_item
);
5584 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
5585 struct btrfs_chunk
*chunk
;
5586 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
5587 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
5593 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5595 btrfs_release_path(path
);
5600 unlock_chunks(root
);
5601 mutex_unlock(&uuid_mutex
);
5603 btrfs_free_path(path
);
5607 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
5611 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5612 btrfs_dev_stat_reset(dev
, i
);
5615 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
5617 struct btrfs_key key
;
5618 struct btrfs_key found_key
;
5619 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5620 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5621 struct extent_buffer
*eb
;
5624 struct btrfs_device
*device
;
5625 struct btrfs_path
*path
= NULL
;
5628 path
= btrfs_alloc_path();
5634 mutex_lock(&fs_devices
->device_list_mutex
);
5635 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5637 struct btrfs_dev_stats_item
*ptr
;
5640 key
.type
= BTRFS_DEV_STATS_KEY
;
5641 key
.offset
= device
->devid
;
5642 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
5644 __btrfs_reset_dev_stats(device
);
5645 device
->dev_stats_valid
= 1;
5646 btrfs_release_path(path
);
5649 slot
= path
->slots
[0];
5650 eb
= path
->nodes
[0];
5651 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5652 item_size
= btrfs_item_size_nr(eb
, slot
);
5654 ptr
= btrfs_item_ptr(eb
, slot
,
5655 struct btrfs_dev_stats_item
);
5657 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5658 if (item_size
>= (1 + i
) * sizeof(__le64
))
5659 btrfs_dev_stat_set(device
, i
,
5660 btrfs_dev_stats_value(eb
, ptr
, i
));
5662 btrfs_dev_stat_reset(device
, i
);
5665 device
->dev_stats_valid
= 1;
5666 btrfs_dev_stat_print_on_load(device
);
5667 btrfs_release_path(path
);
5669 mutex_unlock(&fs_devices
->device_list_mutex
);
5672 btrfs_free_path(path
);
5673 return ret
< 0 ? ret
: 0;
5676 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
5677 struct btrfs_root
*dev_root
,
5678 struct btrfs_device
*device
)
5680 struct btrfs_path
*path
;
5681 struct btrfs_key key
;
5682 struct extent_buffer
*eb
;
5683 struct btrfs_dev_stats_item
*ptr
;
5688 key
.type
= BTRFS_DEV_STATS_KEY
;
5689 key
.offset
= device
->devid
;
5691 path
= btrfs_alloc_path();
5693 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
5695 printk_in_rcu(KERN_WARNING
"btrfs: error %d while searching for dev_stats item for device %s!\n",
5696 ret
, rcu_str_deref(device
->name
));
5701 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
5702 /* need to delete old one and insert a new one */
5703 ret
= btrfs_del_item(trans
, dev_root
, path
);
5705 printk_in_rcu(KERN_WARNING
"btrfs: delete too small dev_stats item for device %s failed %d!\n",
5706 rcu_str_deref(device
->name
), ret
);
5713 /* need to insert a new item */
5714 btrfs_release_path(path
);
5715 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
5716 &key
, sizeof(*ptr
));
5718 printk_in_rcu(KERN_WARNING
"btrfs: insert dev_stats item for device %s failed %d!\n",
5719 rcu_str_deref(device
->name
), ret
);
5724 eb
= path
->nodes
[0];
5725 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
5726 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5727 btrfs_set_dev_stats_value(eb
, ptr
, i
,
5728 btrfs_dev_stat_read(device
, i
));
5729 btrfs_mark_buffer_dirty(eb
);
5732 btrfs_free_path(path
);
5737 * called from commit_transaction. Writes all changed device stats to disk.
5739 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
5740 struct btrfs_fs_info
*fs_info
)
5742 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5743 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5744 struct btrfs_device
*device
;
5747 mutex_lock(&fs_devices
->device_list_mutex
);
5748 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5749 if (!device
->dev_stats_valid
|| !device
->dev_stats_dirty
)
5752 ret
= update_dev_stat_item(trans
, dev_root
, device
);
5754 device
->dev_stats_dirty
= 0;
5756 mutex_unlock(&fs_devices
->device_list_mutex
);
5761 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
5763 btrfs_dev_stat_inc(dev
, index
);
5764 btrfs_dev_stat_print_on_error(dev
);
5767 void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
5769 if (!dev
->dev_stats_valid
)
5771 printk_ratelimited_in_rcu(KERN_ERR
5772 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5773 rcu_str_deref(dev
->name
),
5774 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5775 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5776 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5777 btrfs_dev_stat_read(dev
,
5778 BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5779 btrfs_dev_stat_read(dev
,
5780 BTRFS_DEV_STAT_GENERATION_ERRS
));
5783 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
5787 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5788 if (btrfs_dev_stat_read(dev
, i
) != 0)
5790 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
5791 return; /* all values == 0, suppress message */
5793 printk_in_rcu(KERN_INFO
"btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5794 rcu_str_deref(dev
->name
),
5795 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5796 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5797 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5798 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5799 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
5802 int btrfs_get_dev_stats(struct btrfs_root
*root
,
5803 struct btrfs_ioctl_get_dev_stats
*stats
)
5805 struct btrfs_device
*dev
;
5806 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5809 mutex_lock(&fs_devices
->device_list_mutex
);
5810 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
5811 mutex_unlock(&fs_devices
->device_list_mutex
);
5815 "btrfs: get dev_stats failed, device not found\n");
5817 } else if (!dev
->dev_stats_valid
) {
5819 "btrfs: get dev_stats failed, not yet valid\n");
5821 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
5822 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5823 if (stats
->nr_items
> i
)
5825 btrfs_dev_stat_read_and_reset(dev
, i
);
5827 btrfs_dev_stat_reset(dev
, i
);
5830 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5831 if (stats
->nr_items
> i
)
5832 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
5834 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
5835 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
5839 int btrfs_scratch_superblock(struct btrfs_device
*device
)
5841 struct buffer_head
*bh
;
5842 struct btrfs_super_block
*disk_super
;
5844 bh
= btrfs_read_dev_super(device
->bdev
);
5847 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
5849 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
5850 set_buffer_dirty(bh
);
5851 sync_dirty_buffer(bh
);