2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <linux/ratelimit.h>
27 #include <linux/kthread.h>
30 #include "extent_map.h"
32 #include "transaction.h"
33 #include "print-tree.h"
35 #include "async-thread.h"
36 #include "check-integrity.h"
37 #include "rcu-string.h"
39 #include "dev-replace.h"
41 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
42 struct btrfs_root
*root
,
43 struct btrfs_device
*device
);
44 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
45 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
46 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
48 static DEFINE_MUTEX(uuid_mutex
);
49 static LIST_HEAD(fs_uuids
);
51 static void lock_chunks(struct btrfs_root
*root
)
53 mutex_lock(&root
->fs_info
->chunk_mutex
);
56 static void unlock_chunks(struct btrfs_root
*root
)
58 mutex_unlock(&root
->fs_info
->chunk_mutex
);
61 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
63 struct btrfs_device
*device
;
64 WARN_ON(fs_devices
->opened
);
65 while (!list_empty(&fs_devices
->devices
)) {
66 device
= list_entry(fs_devices
->devices
.next
,
67 struct btrfs_device
, dev_list
);
68 list_del(&device
->dev_list
);
69 rcu_string_free(device
->name
);
75 static void btrfs_kobject_uevent(struct block_device
*bdev
,
76 enum kobject_action action
)
80 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
82 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
84 kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
85 &disk_to_dev(bdev
->bd_disk
)->kobj
);
88 void btrfs_cleanup_fs_uuids(void)
90 struct btrfs_fs_devices
*fs_devices
;
92 while (!list_empty(&fs_uuids
)) {
93 fs_devices
= list_entry(fs_uuids
.next
,
94 struct btrfs_fs_devices
, list
);
95 list_del(&fs_devices
->list
);
96 free_fs_devices(fs_devices
);
100 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
103 struct btrfs_device
*dev
;
105 list_for_each_entry(dev
, head
, dev_list
) {
106 if (dev
->devid
== devid
&&
107 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
114 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
116 struct btrfs_fs_devices
*fs_devices
;
118 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
119 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
126 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
127 int flush
, struct block_device
**bdev
,
128 struct buffer_head
**bh
)
132 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
135 ret
= PTR_ERR(*bdev
);
136 printk(KERN_INFO
"btrfs: open %s failed\n", device_path
);
141 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
142 ret
= set_blocksize(*bdev
, 4096);
144 blkdev_put(*bdev
, flags
);
147 invalidate_bdev(*bdev
);
148 *bh
= btrfs_read_dev_super(*bdev
);
151 blkdev_put(*bdev
, flags
);
163 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
164 struct bio
*head
, struct bio
*tail
)
167 struct bio
*old_head
;
169 old_head
= pending_bios
->head
;
170 pending_bios
->head
= head
;
171 if (pending_bios
->tail
)
172 tail
->bi_next
= old_head
;
174 pending_bios
->tail
= tail
;
178 * we try to collect pending bios for a device so we don't get a large
179 * number of procs sending bios down to the same device. This greatly
180 * improves the schedulers ability to collect and merge the bios.
182 * But, it also turns into a long list of bios to process and that is sure
183 * to eventually make the worker thread block. The solution here is to
184 * make some progress and then put this work struct back at the end of
185 * the list if the block device is congested. This way, multiple devices
186 * can make progress from a single worker thread.
188 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
191 struct backing_dev_info
*bdi
;
192 struct btrfs_fs_info
*fs_info
;
193 struct btrfs_pending_bios
*pending_bios
;
197 unsigned long num_run
;
198 unsigned long batch_run
= 0;
200 unsigned long last_waited
= 0;
202 int sync_pending
= 0;
203 struct blk_plug plug
;
206 * this function runs all the bios we've collected for
207 * a particular device. We don't want to wander off to
208 * another device without first sending all of these down.
209 * So, setup a plug here and finish it off before we return
211 blk_start_plug(&plug
);
213 bdi
= blk_get_backing_dev_info(device
->bdev
);
214 fs_info
= device
->dev_root
->fs_info
;
215 limit
= btrfs_async_submit_limit(fs_info
);
216 limit
= limit
* 2 / 3;
219 spin_lock(&device
->io_lock
);
224 /* take all the bios off the list at once and process them
225 * later on (without the lock held). But, remember the
226 * tail and other pointers so the bios can be properly reinserted
227 * into the list if we hit congestion
229 if (!force_reg
&& device
->pending_sync_bios
.head
) {
230 pending_bios
= &device
->pending_sync_bios
;
233 pending_bios
= &device
->pending_bios
;
237 pending
= pending_bios
->head
;
238 tail
= pending_bios
->tail
;
239 WARN_ON(pending
&& !tail
);
242 * if pending was null this time around, no bios need processing
243 * at all and we can stop. Otherwise it'll loop back up again
244 * and do an additional check so no bios are missed.
246 * device->running_pending is used to synchronize with the
249 if (device
->pending_sync_bios
.head
== NULL
&&
250 device
->pending_bios
.head
== NULL
) {
252 device
->running_pending
= 0;
255 device
->running_pending
= 1;
258 pending_bios
->head
= NULL
;
259 pending_bios
->tail
= NULL
;
261 spin_unlock(&device
->io_lock
);
266 /* we want to work on both lists, but do more bios on the
267 * sync list than the regular list
270 pending_bios
!= &device
->pending_sync_bios
&&
271 device
->pending_sync_bios
.head
) ||
272 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
273 device
->pending_bios
.head
)) {
274 spin_lock(&device
->io_lock
);
275 requeue_list(pending_bios
, pending
, tail
);
280 pending
= pending
->bi_next
;
283 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
284 waitqueue_active(&fs_info
->async_submit_wait
))
285 wake_up(&fs_info
->async_submit_wait
);
287 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
290 * if we're doing the sync list, record that our
291 * plug has some sync requests on it
293 * If we're doing the regular list and there are
294 * sync requests sitting around, unplug before
297 if (pending_bios
== &device
->pending_sync_bios
) {
299 } else if (sync_pending
) {
300 blk_finish_plug(&plug
);
301 blk_start_plug(&plug
);
305 btrfsic_submit_bio(cur
->bi_rw
, cur
);
312 * we made progress, there is more work to do and the bdi
313 * is now congested. Back off and let other work structs
316 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
317 fs_info
->fs_devices
->open_devices
> 1) {
318 struct io_context
*ioc
;
320 ioc
= current
->io_context
;
323 * the main goal here is that we don't want to
324 * block if we're going to be able to submit
325 * more requests without blocking.
327 * This code does two great things, it pokes into
328 * the elevator code from a filesystem _and_
329 * it makes assumptions about how batching works.
331 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
332 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
334 ioc
->last_waited
== last_waited
)) {
336 * we want to go through our batch of
337 * requests and stop. So, we copy out
338 * the ioc->last_waited time and test
339 * against it before looping
341 last_waited
= ioc
->last_waited
;
346 spin_lock(&device
->io_lock
);
347 requeue_list(pending_bios
, pending
, tail
);
348 device
->running_pending
= 1;
350 spin_unlock(&device
->io_lock
);
351 btrfs_requeue_work(&device
->work
);
354 /* unplug every 64 requests just for good measure */
355 if (batch_run
% 64 == 0) {
356 blk_finish_plug(&plug
);
357 blk_start_plug(&plug
);
366 spin_lock(&device
->io_lock
);
367 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
369 spin_unlock(&device
->io_lock
);
372 blk_finish_plug(&plug
);
375 static void pending_bios_fn(struct btrfs_work
*work
)
377 struct btrfs_device
*device
;
379 device
= container_of(work
, struct btrfs_device
, work
);
380 run_scheduled_bios(device
);
383 static noinline
int device_list_add(const char *path
,
384 struct btrfs_super_block
*disk_super
,
385 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
387 struct btrfs_device
*device
;
388 struct btrfs_fs_devices
*fs_devices
;
389 struct rcu_string
*name
;
390 u64 found_transid
= btrfs_super_generation(disk_super
);
392 fs_devices
= find_fsid(disk_super
->fsid
);
394 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
397 INIT_LIST_HEAD(&fs_devices
->devices
);
398 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
399 list_add(&fs_devices
->list
, &fs_uuids
);
400 memcpy(fs_devices
->fsid
, disk_super
->fsid
, BTRFS_FSID_SIZE
);
401 fs_devices
->latest_devid
= devid
;
402 fs_devices
->latest_trans
= found_transid
;
403 mutex_init(&fs_devices
->device_list_mutex
);
406 device
= __find_device(&fs_devices
->devices
, devid
,
407 disk_super
->dev_item
.uuid
);
410 if (fs_devices
->opened
)
413 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
415 /* we can safely leave the fs_devices entry around */
418 device
->devid
= devid
;
419 device
->dev_stats_valid
= 0;
420 device
->work
.func
= pending_bios_fn
;
421 memcpy(device
->uuid
, disk_super
->dev_item
.uuid
,
423 spin_lock_init(&device
->io_lock
);
425 name
= rcu_string_strdup(path
, GFP_NOFS
);
430 rcu_assign_pointer(device
->name
, name
);
431 INIT_LIST_HEAD(&device
->dev_alloc_list
);
433 /* init readahead state */
434 spin_lock_init(&device
->reada_lock
);
435 device
->reada_curr_zone
= NULL
;
436 atomic_set(&device
->reada_in_flight
, 0);
437 device
->reada_next
= 0;
438 INIT_RADIX_TREE(&device
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
439 INIT_RADIX_TREE(&device
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
441 mutex_lock(&fs_devices
->device_list_mutex
);
442 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
443 mutex_unlock(&fs_devices
->device_list_mutex
);
445 device
->fs_devices
= fs_devices
;
446 fs_devices
->num_devices
++;
447 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
448 name
= rcu_string_strdup(path
, GFP_NOFS
);
451 rcu_string_free(device
->name
);
452 rcu_assign_pointer(device
->name
, name
);
453 if (device
->missing
) {
454 fs_devices
->missing_devices
--;
459 if (found_transid
> fs_devices
->latest_trans
) {
460 fs_devices
->latest_devid
= devid
;
461 fs_devices
->latest_trans
= found_transid
;
463 *fs_devices_ret
= fs_devices
;
467 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
469 struct btrfs_fs_devices
*fs_devices
;
470 struct btrfs_device
*device
;
471 struct btrfs_device
*orig_dev
;
473 fs_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
475 return ERR_PTR(-ENOMEM
);
477 INIT_LIST_HEAD(&fs_devices
->devices
);
478 INIT_LIST_HEAD(&fs_devices
->alloc_list
);
479 INIT_LIST_HEAD(&fs_devices
->list
);
480 mutex_init(&fs_devices
->device_list_mutex
);
481 fs_devices
->latest_devid
= orig
->latest_devid
;
482 fs_devices
->latest_trans
= orig
->latest_trans
;
483 fs_devices
->total_devices
= orig
->total_devices
;
484 memcpy(fs_devices
->fsid
, orig
->fsid
, sizeof(fs_devices
->fsid
));
486 /* We have held the volume lock, it is safe to get the devices. */
487 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
488 struct rcu_string
*name
;
490 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
495 * This is ok to do without rcu read locked because we hold the
496 * uuid mutex so nothing we touch in here is going to disappear.
498 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
503 rcu_assign_pointer(device
->name
, name
);
505 device
->devid
= orig_dev
->devid
;
506 device
->work
.func
= pending_bios_fn
;
507 memcpy(device
->uuid
, orig_dev
->uuid
, sizeof(device
->uuid
));
508 spin_lock_init(&device
->io_lock
);
509 INIT_LIST_HEAD(&device
->dev_list
);
510 INIT_LIST_HEAD(&device
->dev_alloc_list
);
512 list_add(&device
->dev_list
, &fs_devices
->devices
);
513 device
->fs_devices
= fs_devices
;
514 fs_devices
->num_devices
++;
518 free_fs_devices(fs_devices
);
519 return ERR_PTR(-ENOMEM
);
522 void btrfs_close_extra_devices(struct btrfs_fs_info
*fs_info
,
523 struct btrfs_fs_devices
*fs_devices
, int step
)
525 struct btrfs_device
*device
, *next
;
527 struct block_device
*latest_bdev
= NULL
;
528 u64 latest_devid
= 0;
529 u64 latest_transid
= 0;
531 mutex_lock(&uuid_mutex
);
533 /* This is the initialized path, it is safe to release the devices. */
534 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
535 if (device
->in_fs_metadata
) {
536 if (!device
->is_tgtdev_for_dev_replace
&&
538 device
->generation
> latest_transid
)) {
539 latest_devid
= device
->devid
;
540 latest_transid
= device
->generation
;
541 latest_bdev
= device
->bdev
;
546 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
548 * In the first step, keep the device which has
549 * the correct fsid and the devid that is used
550 * for the dev_replace procedure.
551 * In the second step, the dev_replace state is
552 * read from the device tree and it is known
553 * whether the procedure is really active or
554 * not, which means whether this device is
555 * used or whether it should be removed.
557 if (step
== 0 || device
->is_tgtdev_for_dev_replace
) {
562 blkdev_put(device
->bdev
, device
->mode
);
564 fs_devices
->open_devices
--;
566 if (device
->writeable
) {
567 list_del_init(&device
->dev_alloc_list
);
568 device
->writeable
= 0;
569 if (!device
->is_tgtdev_for_dev_replace
)
570 fs_devices
->rw_devices
--;
572 list_del_init(&device
->dev_list
);
573 fs_devices
->num_devices
--;
574 rcu_string_free(device
->name
);
578 if (fs_devices
->seed
) {
579 fs_devices
= fs_devices
->seed
;
583 fs_devices
->latest_bdev
= latest_bdev
;
584 fs_devices
->latest_devid
= latest_devid
;
585 fs_devices
->latest_trans
= latest_transid
;
587 mutex_unlock(&uuid_mutex
);
590 static void __free_device(struct work_struct
*work
)
592 struct btrfs_device
*device
;
594 device
= container_of(work
, struct btrfs_device
, rcu_work
);
597 blkdev_put(device
->bdev
, device
->mode
);
599 rcu_string_free(device
->name
);
603 static void free_device(struct rcu_head
*head
)
605 struct btrfs_device
*device
;
607 device
= container_of(head
, struct btrfs_device
, rcu
);
609 INIT_WORK(&device
->rcu_work
, __free_device
);
610 schedule_work(&device
->rcu_work
);
613 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
615 struct btrfs_device
*device
;
617 if (--fs_devices
->opened
> 0)
620 mutex_lock(&fs_devices
->device_list_mutex
);
621 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
622 struct btrfs_device
*new_device
;
623 struct rcu_string
*name
;
626 fs_devices
->open_devices
--;
628 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
629 list_del_init(&device
->dev_alloc_list
);
630 fs_devices
->rw_devices
--;
633 if (device
->can_discard
)
634 fs_devices
->num_can_discard
--;
636 new_device
= kmalloc(sizeof(*new_device
), GFP_NOFS
);
637 BUG_ON(!new_device
); /* -ENOMEM */
638 memcpy(new_device
, device
, sizeof(*new_device
));
640 /* Safe because we are under uuid_mutex */
642 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
643 BUG_ON(device
->name
&& !name
); /* -ENOMEM */
644 rcu_assign_pointer(new_device
->name
, name
);
646 new_device
->bdev
= NULL
;
647 new_device
->writeable
= 0;
648 new_device
->in_fs_metadata
= 0;
649 new_device
->can_discard
= 0;
650 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
652 call_rcu(&device
->rcu
, free_device
);
654 mutex_unlock(&fs_devices
->device_list_mutex
);
656 WARN_ON(fs_devices
->open_devices
);
657 WARN_ON(fs_devices
->rw_devices
);
658 fs_devices
->opened
= 0;
659 fs_devices
->seeding
= 0;
664 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
666 struct btrfs_fs_devices
*seed_devices
= NULL
;
669 mutex_lock(&uuid_mutex
);
670 ret
= __btrfs_close_devices(fs_devices
);
671 if (!fs_devices
->opened
) {
672 seed_devices
= fs_devices
->seed
;
673 fs_devices
->seed
= NULL
;
675 mutex_unlock(&uuid_mutex
);
677 while (seed_devices
) {
678 fs_devices
= seed_devices
;
679 seed_devices
= fs_devices
->seed
;
680 __btrfs_close_devices(fs_devices
);
681 free_fs_devices(fs_devices
);
686 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
687 fmode_t flags
, void *holder
)
689 struct request_queue
*q
;
690 struct block_device
*bdev
;
691 struct list_head
*head
= &fs_devices
->devices
;
692 struct btrfs_device
*device
;
693 struct block_device
*latest_bdev
= NULL
;
694 struct buffer_head
*bh
;
695 struct btrfs_super_block
*disk_super
;
696 u64 latest_devid
= 0;
697 u64 latest_transid
= 0;
704 list_for_each_entry(device
, head
, dev_list
) {
710 ret
= btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
715 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
716 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
717 if (devid
!= device
->devid
)
720 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
724 device
->generation
= btrfs_super_generation(disk_super
);
725 if (!latest_transid
|| device
->generation
> latest_transid
) {
726 latest_devid
= devid
;
727 latest_transid
= device
->generation
;
731 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
732 device
->writeable
= 0;
734 device
->writeable
= !bdev_read_only(bdev
);
738 q
= bdev_get_queue(bdev
);
739 if (blk_queue_discard(q
)) {
740 device
->can_discard
= 1;
741 fs_devices
->num_can_discard
++;
745 device
->in_fs_metadata
= 0;
746 device
->mode
= flags
;
748 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
749 fs_devices
->rotating
= 1;
751 fs_devices
->open_devices
++;
752 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
753 fs_devices
->rw_devices
++;
754 list_add(&device
->dev_alloc_list
,
755 &fs_devices
->alloc_list
);
762 blkdev_put(bdev
, flags
);
765 if (fs_devices
->open_devices
== 0) {
769 fs_devices
->seeding
= seeding
;
770 fs_devices
->opened
= 1;
771 fs_devices
->latest_bdev
= latest_bdev
;
772 fs_devices
->latest_devid
= latest_devid
;
773 fs_devices
->latest_trans
= latest_transid
;
774 fs_devices
->total_rw_bytes
= 0;
779 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
780 fmode_t flags
, void *holder
)
784 mutex_lock(&uuid_mutex
);
785 if (fs_devices
->opened
) {
786 fs_devices
->opened
++;
789 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
791 mutex_unlock(&uuid_mutex
);
795 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
796 struct btrfs_fs_devices
**fs_devices_ret
)
798 struct btrfs_super_block
*disk_super
;
799 struct block_device
*bdev
;
800 struct buffer_head
*bh
;
807 mutex_lock(&uuid_mutex
);
808 ret
= btrfs_get_bdev_and_sb(path
, flags
, holder
, 0, &bdev
, &bh
);
811 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
812 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
813 transid
= btrfs_super_generation(disk_super
);
814 total_devices
= btrfs_super_num_devices(disk_super
);
815 if (disk_super
->label
[0]) {
816 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
817 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
818 printk(KERN_INFO
"device label %s ", disk_super
->label
);
820 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
822 printk(KERN_CONT
"devid %llu transid %llu %s\n",
823 (unsigned long long)devid
, (unsigned long long)transid
, path
);
824 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
825 if (!ret
&& fs_devices_ret
)
826 (*fs_devices_ret
)->total_devices
= total_devices
;
828 blkdev_put(bdev
, flags
);
830 mutex_unlock(&uuid_mutex
);
834 /* helper to account the used device space in the range */
835 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
836 u64 end
, u64
*length
)
838 struct btrfs_key key
;
839 struct btrfs_root
*root
= device
->dev_root
;
840 struct btrfs_dev_extent
*dev_extent
;
841 struct btrfs_path
*path
;
845 struct extent_buffer
*l
;
849 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
852 path
= btrfs_alloc_path();
857 key
.objectid
= device
->devid
;
859 key
.type
= BTRFS_DEV_EXTENT_KEY
;
861 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
865 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
872 slot
= path
->slots
[0];
873 if (slot
>= btrfs_header_nritems(l
)) {
874 ret
= btrfs_next_leaf(root
, path
);
882 btrfs_item_key_to_cpu(l
, &key
, slot
);
884 if (key
.objectid
< device
->devid
)
887 if (key
.objectid
> device
->devid
)
890 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
893 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
894 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
896 if (key
.offset
<= start
&& extent_end
> end
) {
897 *length
= end
- start
+ 1;
899 } else if (key
.offset
<= start
&& extent_end
> start
)
900 *length
+= extent_end
- start
;
901 else if (key
.offset
> start
&& extent_end
<= end
)
902 *length
+= extent_end
- key
.offset
;
903 else if (key
.offset
> start
&& key
.offset
<= end
) {
904 *length
+= end
- key
.offset
+ 1;
906 } else if (key
.offset
> end
)
914 btrfs_free_path(path
);
919 * find_free_dev_extent - find free space in the specified device
920 * @device: the device which we search the free space in
921 * @num_bytes: the size of the free space that we need
922 * @start: store the start of the free space.
923 * @len: the size of the free space. that we find, or the size of the max
924 * free space if we don't find suitable free space
926 * this uses a pretty simple search, the expectation is that it is
927 * called very infrequently and that a given device has a small number
930 * @start is used to store the start of the free space if we find. But if we
931 * don't find suitable free space, it will be used to store the start position
932 * of the max free space.
934 * @len is used to store the size of the free space that we find.
935 * But if we don't find suitable free space, it is used to store the size of
936 * the max free space.
938 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
939 u64
*start
, u64
*len
)
941 struct btrfs_key key
;
942 struct btrfs_root
*root
= device
->dev_root
;
943 struct btrfs_dev_extent
*dev_extent
;
944 struct btrfs_path
*path
;
950 u64 search_end
= device
->total_bytes
;
953 struct extent_buffer
*l
;
955 /* FIXME use last free of some kind */
957 /* we don't want to overwrite the superblock on the drive,
958 * so we make sure to start at an offset of at least 1MB
960 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
962 max_hole_start
= search_start
;
966 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
971 path
= btrfs_alloc_path();
978 key
.objectid
= device
->devid
;
979 key
.offset
= search_start
;
980 key
.type
= BTRFS_DEV_EXTENT_KEY
;
982 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
986 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
993 slot
= path
->slots
[0];
994 if (slot
>= btrfs_header_nritems(l
)) {
995 ret
= btrfs_next_leaf(root
, path
);
1003 btrfs_item_key_to_cpu(l
, &key
, slot
);
1005 if (key
.objectid
< device
->devid
)
1008 if (key
.objectid
> device
->devid
)
1011 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
1014 if (key
.offset
> search_start
) {
1015 hole_size
= key
.offset
- search_start
;
1017 if (hole_size
> max_hole_size
) {
1018 max_hole_start
= search_start
;
1019 max_hole_size
= hole_size
;
1023 * If this free space is greater than which we need,
1024 * it must be the max free space that we have found
1025 * until now, so max_hole_start must point to the start
1026 * of this free space and the length of this free space
1027 * is stored in max_hole_size. Thus, we return
1028 * max_hole_start and max_hole_size and go back to the
1031 if (hole_size
>= num_bytes
) {
1037 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1038 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1040 if (extent_end
> search_start
)
1041 search_start
= extent_end
;
1048 * At this point, search_start should be the end of
1049 * allocated dev extents, and when shrinking the device,
1050 * search_end may be smaller than search_start.
1052 if (search_end
> search_start
)
1053 hole_size
= search_end
- search_start
;
1055 if (hole_size
> max_hole_size
) {
1056 max_hole_start
= search_start
;
1057 max_hole_size
= hole_size
;
1061 if (hole_size
< num_bytes
)
1067 btrfs_free_path(path
);
1069 *start
= max_hole_start
;
1071 *len
= max_hole_size
;
1075 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1076 struct btrfs_device
*device
,
1080 struct btrfs_path
*path
;
1081 struct btrfs_root
*root
= device
->dev_root
;
1082 struct btrfs_key key
;
1083 struct btrfs_key found_key
;
1084 struct extent_buffer
*leaf
= NULL
;
1085 struct btrfs_dev_extent
*extent
= NULL
;
1087 path
= btrfs_alloc_path();
1091 key
.objectid
= device
->devid
;
1093 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1095 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1097 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1098 BTRFS_DEV_EXTENT_KEY
);
1101 leaf
= path
->nodes
[0];
1102 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1103 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1104 struct btrfs_dev_extent
);
1105 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1106 btrfs_dev_extent_length(leaf
, extent
) < start
);
1108 btrfs_release_path(path
);
1110 } else if (ret
== 0) {
1111 leaf
= path
->nodes
[0];
1112 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1113 struct btrfs_dev_extent
);
1115 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1119 if (device
->bytes_used
> 0) {
1120 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1121 device
->bytes_used
-= len
;
1122 spin_lock(&root
->fs_info
->free_chunk_lock
);
1123 root
->fs_info
->free_chunk_space
+= len
;
1124 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1126 ret
= btrfs_del_item(trans
, root
, path
);
1128 btrfs_error(root
->fs_info
, ret
,
1129 "Failed to remove dev extent item");
1132 btrfs_free_path(path
);
1136 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1137 struct btrfs_device
*device
,
1138 u64 chunk_tree
, u64 chunk_objectid
,
1139 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1142 struct btrfs_path
*path
;
1143 struct btrfs_root
*root
= device
->dev_root
;
1144 struct btrfs_dev_extent
*extent
;
1145 struct extent_buffer
*leaf
;
1146 struct btrfs_key key
;
1148 WARN_ON(!device
->in_fs_metadata
);
1149 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1150 path
= btrfs_alloc_path();
1154 key
.objectid
= device
->devid
;
1156 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1157 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1162 leaf
= path
->nodes
[0];
1163 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1164 struct btrfs_dev_extent
);
1165 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1166 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1167 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1169 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1170 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1173 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1174 btrfs_mark_buffer_dirty(leaf
);
1176 btrfs_free_path(path
);
1180 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1181 u64 objectid
, u64
*offset
)
1183 struct btrfs_path
*path
;
1185 struct btrfs_key key
;
1186 struct btrfs_chunk
*chunk
;
1187 struct btrfs_key found_key
;
1189 path
= btrfs_alloc_path();
1193 key
.objectid
= objectid
;
1194 key
.offset
= (u64
)-1;
1195 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1197 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1201 BUG_ON(ret
== 0); /* Corruption */
1203 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1207 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1209 if (found_key
.objectid
!= objectid
)
1212 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1213 struct btrfs_chunk
);
1214 *offset
= found_key
.offset
+
1215 btrfs_chunk_length(path
->nodes
[0], chunk
);
1220 btrfs_free_path(path
);
1224 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1227 struct btrfs_key key
;
1228 struct btrfs_key found_key
;
1229 struct btrfs_path
*path
;
1231 root
= root
->fs_info
->chunk_root
;
1233 path
= btrfs_alloc_path();
1237 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1238 key
.type
= BTRFS_DEV_ITEM_KEY
;
1239 key
.offset
= (u64
)-1;
1241 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1245 BUG_ON(ret
== 0); /* Corruption */
1247 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1248 BTRFS_DEV_ITEM_KEY
);
1252 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1254 *objectid
= found_key
.offset
+ 1;
1258 btrfs_free_path(path
);
1263 * the device information is stored in the chunk root
1264 * the btrfs_device struct should be fully filled in
1266 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1267 struct btrfs_root
*root
,
1268 struct btrfs_device
*device
)
1271 struct btrfs_path
*path
;
1272 struct btrfs_dev_item
*dev_item
;
1273 struct extent_buffer
*leaf
;
1274 struct btrfs_key key
;
1277 root
= root
->fs_info
->chunk_root
;
1279 path
= btrfs_alloc_path();
1283 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1284 key
.type
= BTRFS_DEV_ITEM_KEY
;
1285 key
.offset
= device
->devid
;
1287 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1292 leaf
= path
->nodes
[0];
1293 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1295 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1296 btrfs_set_device_generation(leaf
, dev_item
, 0);
1297 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1298 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1299 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1300 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1301 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1302 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1303 btrfs_set_device_group(leaf
, dev_item
, 0);
1304 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1305 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1306 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1308 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1309 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1310 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1311 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1312 btrfs_mark_buffer_dirty(leaf
);
1316 btrfs_free_path(path
);
1320 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1321 struct btrfs_device
*device
)
1324 struct btrfs_path
*path
;
1325 struct btrfs_key key
;
1326 struct btrfs_trans_handle
*trans
;
1328 root
= root
->fs_info
->chunk_root
;
1330 path
= btrfs_alloc_path();
1334 trans
= btrfs_start_transaction(root
, 0);
1335 if (IS_ERR(trans
)) {
1336 btrfs_free_path(path
);
1337 return PTR_ERR(trans
);
1339 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1340 key
.type
= BTRFS_DEV_ITEM_KEY
;
1341 key
.offset
= device
->devid
;
1344 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1353 ret
= btrfs_del_item(trans
, root
, path
);
1357 btrfs_free_path(path
);
1358 unlock_chunks(root
);
1359 btrfs_commit_transaction(trans
, root
);
1363 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1365 struct btrfs_device
*device
;
1366 struct btrfs_device
*next_device
;
1367 struct block_device
*bdev
;
1368 struct buffer_head
*bh
= NULL
;
1369 struct btrfs_super_block
*disk_super
;
1370 struct btrfs_fs_devices
*cur_devices
;
1376 bool clear_super
= false;
1378 mutex_lock(&uuid_mutex
);
1380 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1381 root
->fs_info
->avail_system_alloc_bits
|
1382 root
->fs_info
->avail_metadata_alloc_bits
;
1384 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1385 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1386 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1387 WARN_ON(num_devices
< 1);
1390 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1392 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1393 printk(KERN_ERR
"btrfs: unable to go below four devices "
1399 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1400 printk(KERN_ERR
"btrfs: unable to go below two "
1401 "devices on raid1\n");
1406 if (strcmp(device_path
, "missing") == 0) {
1407 struct list_head
*devices
;
1408 struct btrfs_device
*tmp
;
1411 devices
= &root
->fs_info
->fs_devices
->devices
;
1413 * It is safe to read the devices since the volume_mutex
1416 list_for_each_entry(tmp
, devices
, dev_list
) {
1417 if (tmp
->in_fs_metadata
&&
1418 !tmp
->is_tgtdev_for_dev_replace
&&
1428 printk(KERN_ERR
"btrfs: no missing devices found to "
1433 ret
= btrfs_get_bdev_and_sb(device_path
,
1434 FMODE_READ
| FMODE_EXCL
,
1435 root
->fs_info
->bdev_holder
, 0,
1439 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1440 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1441 dev_uuid
= disk_super
->dev_item
.uuid
;
1442 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1450 if (device
->is_tgtdev_for_dev_replace
) {
1451 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1456 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1457 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1463 if (device
->writeable
) {
1465 list_del_init(&device
->dev_alloc_list
);
1466 unlock_chunks(root
);
1467 root
->fs_info
->fs_devices
->rw_devices
--;
1471 ret
= btrfs_shrink_device(device
, 0);
1476 * TODO: the superblock still includes this device in its num_devices
1477 * counter although write_all_supers() is not locked out. This
1478 * could give a filesystem state which requires a degraded mount.
1480 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1484 spin_lock(&root
->fs_info
->free_chunk_lock
);
1485 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1487 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1489 device
->in_fs_metadata
= 0;
1490 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1493 * the device list mutex makes sure that we don't change
1494 * the device list while someone else is writing out all
1495 * the device supers.
1498 cur_devices
= device
->fs_devices
;
1499 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1500 list_del_rcu(&device
->dev_list
);
1502 device
->fs_devices
->num_devices
--;
1503 device
->fs_devices
->total_devices
--;
1505 if (device
->missing
)
1506 root
->fs_info
->fs_devices
->missing_devices
--;
1508 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1509 struct btrfs_device
, dev_list
);
1510 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1511 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1512 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1513 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1516 device
->fs_devices
->open_devices
--;
1518 call_rcu(&device
->rcu
, free_device
);
1519 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1521 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1522 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1524 if (cur_devices
->open_devices
== 0) {
1525 struct btrfs_fs_devices
*fs_devices
;
1526 fs_devices
= root
->fs_info
->fs_devices
;
1527 while (fs_devices
) {
1528 if (fs_devices
->seed
== cur_devices
)
1530 fs_devices
= fs_devices
->seed
;
1532 fs_devices
->seed
= cur_devices
->seed
;
1533 cur_devices
->seed
= NULL
;
1535 __btrfs_close_devices(cur_devices
);
1536 unlock_chunks(root
);
1537 free_fs_devices(cur_devices
);
1540 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1541 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1544 * at this point, the device is zero sized. We want to
1545 * remove it from the devices list and zero out the old super
1547 if (clear_super
&& disk_super
) {
1548 /* make sure this device isn't detected as part of
1551 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1552 set_buffer_dirty(bh
);
1553 sync_dirty_buffer(bh
);
1558 /* Notify udev that device has changed */
1560 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
1565 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1567 mutex_unlock(&uuid_mutex
);
1570 if (device
->writeable
) {
1572 list_add(&device
->dev_alloc_list
,
1573 &root
->fs_info
->fs_devices
->alloc_list
);
1574 unlock_chunks(root
);
1575 root
->fs_info
->fs_devices
->rw_devices
++;
1580 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info
*fs_info
,
1581 struct btrfs_device
*srcdev
)
1583 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1584 list_del_rcu(&srcdev
->dev_list
);
1585 list_del_rcu(&srcdev
->dev_alloc_list
);
1586 fs_info
->fs_devices
->num_devices
--;
1587 if (srcdev
->missing
) {
1588 fs_info
->fs_devices
->missing_devices
--;
1589 fs_info
->fs_devices
->rw_devices
++;
1591 if (srcdev
->can_discard
)
1592 fs_info
->fs_devices
->num_can_discard
--;
1594 fs_info
->fs_devices
->open_devices
--;
1596 call_rcu(&srcdev
->rcu
, free_device
);
1599 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1600 struct btrfs_device
*tgtdev
)
1602 struct btrfs_device
*next_device
;
1605 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1607 btrfs_scratch_superblock(tgtdev
);
1608 fs_info
->fs_devices
->open_devices
--;
1610 fs_info
->fs_devices
->num_devices
--;
1611 if (tgtdev
->can_discard
)
1612 fs_info
->fs_devices
->num_can_discard
++;
1614 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1615 struct btrfs_device
, dev_list
);
1616 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1617 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1618 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1619 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1620 list_del_rcu(&tgtdev
->dev_list
);
1622 call_rcu(&tgtdev
->rcu
, free_device
);
1624 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1627 int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1628 struct btrfs_device
**device
)
1631 struct btrfs_super_block
*disk_super
;
1634 struct block_device
*bdev
;
1635 struct buffer_head
*bh
;
1638 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1639 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1642 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1643 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1644 dev_uuid
= disk_super
->dev_item
.uuid
;
1645 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1650 blkdev_put(bdev
, FMODE_READ
);
1654 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1656 struct btrfs_device
**device
)
1659 if (strcmp(device_path
, "missing") == 0) {
1660 struct list_head
*devices
;
1661 struct btrfs_device
*tmp
;
1663 devices
= &root
->fs_info
->fs_devices
->devices
;
1665 * It is safe to read the devices since the volume_mutex
1666 * is held by the caller.
1668 list_for_each_entry(tmp
, devices
, dev_list
) {
1669 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1676 pr_err("btrfs: no missing device found\n");
1682 return btrfs_find_device_by_path(root
, device_path
, device
);
1687 * does all the dirty work required for changing file system's UUID.
1689 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1691 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1692 struct btrfs_fs_devices
*old_devices
;
1693 struct btrfs_fs_devices
*seed_devices
;
1694 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1695 struct btrfs_device
*device
;
1698 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1699 if (!fs_devices
->seeding
)
1702 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1706 old_devices
= clone_fs_devices(fs_devices
);
1707 if (IS_ERR(old_devices
)) {
1708 kfree(seed_devices
);
1709 return PTR_ERR(old_devices
);
1712 list_add(&old_devices
->list
, &fs_uuids
);
1714 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1715 seed_devices
->opened
= 1;
1716 INIT_LIST_HEAD(&seed_devices
->devices
);
1717 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1718 mutex_init(&seed_devices
->device_list_mutex
);
1720 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1721 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1723 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1725 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1726 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1727 device
->fs_devices
= seed_devices
;
1730 fs_devices
->seeding
= 0;
1731 fs_devices
->num_devices
= 0;
1732 fs_devices
->open_devices
= 0;
1733 fs_devices
->total_devices
= 0;
1734 fs_devices
->seed
= seed_devices
;
1736 generate_random_uuid(fs_devices
->fsid
);
1737 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1738 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1739 super_flags
= btrfs_super_flags(disk_super
) &
1740 ~BTRFS_SUPER_FLAG_SEEDING
;
1741 btrfs_set_super_flags(disk_super
, super_flags
);
1747 * strore the expected generation for seed devices in device items.
1749 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1750 struct btrfs_root
*root
)
1752 struct btrfs_path
*path
;
1753 struct extent_buffer
*leaf
;
1754 struct btrfs_dev_item
*dev_item
;
1755 struct btrfs_device
*device
;
1756 struct btrfs_key key
;
1757 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1758 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1762 path
= btrfs_alloc_path();
1766 root
= root
->fs_info
->chunk_root
;
1767 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1769 key
.type
= BTRFS_DEV_ITEM_KEY
;
1772 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1776 leaf
= path
->nodes
[0];
1778 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1779 ret
= btrfs_next_leaf(root
, path
);
1784 leaf
= path
->nodes
[0];
1785 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1786 btrfs_release_path(path
);
1790 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1791 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1792 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1795 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1796 struct btrfs_dev_item
);
1797 devid
= btrfs_device_id(leaf
, dev_item
);
1798 read_extent_buffer(leaf
, dev_uuid
,
1799 (unsigned long)btrfs_device_uuid(dev_item
),
1801 read_extent_buffer(leaf
, fs_uuid
,
1802 (unsigned long)btrfs_device_fsid(dev_item
),
1804 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1806 BUG_ON(!device
); /* Logic error */
1808 if (device
->fs_devices
->seeding
) {
1809 btrfs_set_device_generation(leaf
, dev_item
,
1810 device
->generation
);
1811 btrfs_mark_buffer_dirty(leaf
);
1819 btrfs_free_path(path
);
1823 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1825 struct request_queue
*q
;
1826 struct btrfs_trans_handle
*trans
;
1827 struct btrfs_device
*device
;
1828 struct block_device
*bdev
;
1829 struct list_head
*devices
;
1830 struct super_block
*sb
= root
->fs_info
->sb
;
1831 struct rcu_string
*name
;
1833 int seeding_dev
= 0;
1836 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1839 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1840 root
->fs_info
->bdev_holder
);
1842 return PTR_ERR(bdev
);
1844 if (root
->fs_info
->fs_devices
->seeding
) {
1846 down_write(&sb
->s_umount
);
1847 mutex_lock(&uuid_mutex
);
1850 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1852 devices
= &root
->fs_info
->fs_devices
->devices
;
1854 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1855 list_for_each_entry(device
, devices
, dev_list
) {
1856 if (device
->bdev
== bdev
) {
1859 &root
->fs_info
->fs_devices
->device_list_mutex
);
1863 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1865 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1867 /* we can safely leave the fs_devices entry around */
1872 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
1878 rcu_assign_pointer(device
->name
, name
);
1880 ret
= find_next_devid(root
, &device
->devid
);
1882 rcu_string_free(device
->name
);
1887 trans
= btrfs_start_transaction(root
, 0);
1888 if (IS_ERR(trans
)) {
1889 rcu_string_free(device
->name
);
1891 ret
= PTR_ERR(trans
);
1897 q
= bdev_get_queue(bdev
);
1898 if (blk_queue_discard(q
))
1899 device
->can_discard
= 1;
1900 device
->writeable
= 1;
1901 device
->work
.func
= pending_bios_fn
;
1902 generate_random_uuid(device
->uuid
);
1903 spin_lock_init(&device
->io_lock
);
1904 device
->generation
= trans
->transid
;
1905 device
->io_width
= root
->sectorsize
;
1906 device
->io_align
= root
->sectorsize
;
1907 device
->sector_size
= root
->sectorsize
;
1908 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1909 device
->disk_total_bytes
= device
->total_bytes
;
1910 device
->dev_root
= root
->fs_info
->dev_root
;
1911 device
->bdev
= bdev
;
1912 device
->in_fs_metadata
= 1;
1913 device
->is_tgtdev_for_dev_replace
= 0;
1914 device
->mode
= FMODE_EXCL
;
1915 set_blocksize(device
->bdev
, 4096);
1918 sb
->s_flags
&= ~MS_RDONLY
;
1919 ret
= btrfs_prepare_sprout(root
);
1920 BUG_ON(ret
); /* -ENOMEM */
1923 device
->fs_devices
= root
->fs_info
->fs_devices
;
1925 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1926 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1927 list_add(&device
->dev_alloc_list
,
1928 &root
->fs_info
->fs_devices
->alloc_list
);
1929 root
->fs_info
->fs_devices
->num_devices
++;
1930 root
->fs_info
->fs_devices
->open_devices
++;
1931 root
->fs_info
->fs_devices
->rw_devices
++;
1932 root
->fs_info
->fs_devices
->total_devices
++;
1933 if (device
->can_discard
)
1934 root
->fs_info
->fs_devices
->num_can_discard
++;
1935 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1937 spin_lock(&root
->fs_info
->free_chunk_lock
);
1938 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1939 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1941 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
1942 root
->fs_info
->fs_devices
->rotating
= 1;
1944 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
1945 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
1946 total_bytes
+ device
->total_bytes
);
1948 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
1949 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
1951 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1954 ret
= init_first_rw_device(trans
, root
, device
);
1956 btrfs_abort_transaction(trans
, root
, ret
);
1959 ret
= btrfs_finish_sprout(trans
, root
);
1961 btrfs_abort_transaction(trans
, root
, ret
);
1965 ret
= btrfs_add_device(trans
, root
, device
);
1967 btrfs_abort_transaction(trans
, root
, ret
);
1973 * we've got more storage, clear any full flags on the space
1976 btrfs_clear_space_info_full(root
->fs_info
);
1978 unlock_chunks(root
);
1979 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1980 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1981 ret
= btrfs_commit_transaction(trans
, root
);
1984 mutex_unlock(&uuid_mutex
);
1985 up_write(&sb
->s_umount
);
1987 if (ret
) /* transaction commit */
1990 ret
= btrfs_relocate_sys_chunks(root
);
1992 btrfs_error(root
->fs_info
, ret
,
1993 "Failed to relocate sys chunks after "
1994 "device initialization. This can be fixed "
1995 "using the \"btrfs balance\" command.");
1996 trans
= btrfs_attach_transaction(root
);
1997 if (IS_ERR(trans
)) {
1998 if (PTR_ERR(trans
) == -ENOENT
)
2000 return PTR_ERR(trans
);
2002 ret
= btrfs_commit_transaction(trans
, root
);
2008 unlock_chunks(root
);
2009 btrfs_end_transaction(trans
, root
);
2010 rcu_string_free(device
->name
);
2013 blkdev_put(bdev
, FMODE_EXCL
);
2015 mutex_unlock(&uuid_mutex
);
2016 up_write(&sb
->s_umount
);
2021 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2022 struct btrfs_device
**device_out
)
2024 struct request_queue
*q
;
2025 struct btrfs_device
*device
;
2026 struct block_device
*bdev
;
2027 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2028 struct list_head
*devices
;
2029 struct rcu_string
*name
;
2033 if (fs_info
->fs_devices
->seeding
)
2036 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2037 fs_info
->bdev_holder
);
2039 return PTR_ERR(bdev
);
2041 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2043 devices
= &fs_info
->fs_devices
->devices
;
2044 list_for_each_entry(device
, devices
, dev_list
) {
2045 if (device
->bdev
== bdev
) {
2051 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2057 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2063 rcu_assign_pointer(device
->name
, name
);
2065 q
= bdev_get_queue(bdev
);
2066 if (blk_queue_discard(q
))
2067 device
->can_discard
= 1;
2068 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2069 device
->writeable
= 1;
2070 device
->work
.func
= pending_bios_fn
;
2071 generate_random_uuid(device
->uuid
);
2072 device
->devid
= BTRFS_DEV_REPLACE_DEVID
;
2073 spin_lock_init(&device
->io_lock
);
2074 device
->generation
= 0;
2075 device
->io_width
= root
->sectorsize
;
2076 device
->io_align
= root
->sectorsize
;
2077 device
->sector_size
= root
->sectorsize
;
2078 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2079 device
->disk_total_bytes
= device
->total_bytes
;
2080 device
->dev_root
= fs_info
->dev_root
;
2081 device
->bdev
= bdev
;
2082 device
->in_fs_metadata
= 1;
2083 device
->is_tgtdev_for_dev_replace
= 1;
2084 device
->mode
= FMODE_EXCL
;
2085 set_blocksize(device
->bdev
, 4096);
2086 device
->fs_devices
= fs_info
->fs_devices
;
2087 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2088 fs_info
->fs_devices
->num_devices
++;
2089 fs_info
->fs_devices
->open_devices
++;
2090 if (device
->can_discard
)
2091 fs_info
->fs_devices
->num_can_discard
++;
2092 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2094 *device_out
= device
;
2098 blkdev_put(bdev
, FMODE_EXCL
);
2102 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2103 struct btrfs_device
*tgtdev
)
2105 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2106 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2107 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2108 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2109 tgtdev
->dev_root
= fs_info
->dev_root
;
2110 tgtdev
->in_fs_metadata
= 1;
2113 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2114 struct btrfs_device
*device
)
2117 struct btrfs_path
*path
;
2118 struct btrfs_root
*root
;
2119 struct btrfs_dev_item
*dev_item
;
2120 struct extent_buffer
*leaf
;
2121 struct btrfs_key key
;
2123 root
= device
->dev_root
->fs_info
->chunk_root
;
2125 path
= btrfs_alloc_path();
2129 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2130 key
.type
= BTRFS_DEV_ITEM_KEY
;
2131 key
.offset
= device
->devid
;
2133 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2142 leaf
= path
->nodes
[0];
2143 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2145 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2146 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2147 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2148 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2149 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2150 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
2151 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
2152 btrfs_mark_buffer_dirty(leaf
);
2155 btrfs_free_path(path
);
2159 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2160 struct btrfs_device
*device
, u64 new_size
)
2162 struct btrfs_super_block
*super_copy
=
2163 device
->dev_root
->fs_info
->super_copy
;
2164 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2165 u64 diff
= new_size
- device
->total_bytes
;
2167 if (!device
->writeable
)
2169 if (new_size
<= device
->total_bytes
||
2170 device
->is_tgtdev_for_dev_replace
)
2173 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2174 device
->fs_devices
->total_rw_bytes
+= diff
;
2176 device
->total_bytes
= new_size
;
2177 device
->disk_total_bytes
= new_size
;
2178 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2180 return btrfs_update_device(trans
, device
);
2183 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2184 struct btrfs_device
*device
, u64 new_size
)
2187 lock_chunks(device
->dev_root
);
2188 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2189 unlock_chunks(device
->dev_root
);
2193 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2194 struct btrfs_root
*root
,
2195 u64 chunk_tree
, u64 chunk_objectid
,
2199 struct btrfs_path
*path
;
2200 struct btrfs_key key
;
2202 root
= root
->fs_info
->chunk_root
;
2203 path
= btrfs_alloc_path();
2207 key
.objectid
= chunk_objectid
;
2208 key
.offset
= chunk_offset
;
2209 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2211 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2214 else if (ret
> 0) { /* Logic error or corruption */
2215 btrfs_error(root
->fs_info
, -ENOENT
,
2216 "Failed lookup while freeing chunk.");
2221 ret
= btrfs_del_item(trans
, root
, path
);
2223 btrfs_error(root
->fs_info
, ret
,
2224 "Failed to delete chunk item.");
2226 btrfs_free_path(path
);
2230 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2233 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2234 struct btrfs_disk_key
*disk_key
;
2235 struct btrfs_chunk
*chunk
;
2242 struct btrfs_key key
;
2244 array_size
= btrfs_super_sys_array_size(super_copy
);
2246 ptr
= super_copy
->sys_chunk_array
;
2249 while (cur
< array_size
) {
2250 disk_key
= (struct btrfs_disk_key
*)ptr
;
2251 btrfs_disk_key_to_cpu(&key
, disk_key
);
2253 len
= sizeof(*disk_key
);
2255 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2256 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2257 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2258 len
+= btrfs_chunk_item_size(num_stripes
);
2263 if (key
.objectid
== chunk_objectid
&&
2264 key
.offset
== chunk_offset
) {
2265 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2267 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2276 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2277 u64 chunk_tree
, u64 chunk_objectid
,
2280 struct extent_map_tree
*em_tree
;
2281 struct btrfs_root
*extent_root
;
2282 struct btrfs_trans_handle
*trans
;
2283 struct extent_map
*em
;
2284 struct map_lookup
*map
;
2288 root
= root
->fs_info
->chunk_root
;
2289 extent_root
= root
->fs_info
->extent_root
;
2290 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2292 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2296 /* step one, relocate all the extents inside this chunk */
2297 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2301 trans
= btrfs_start_transaction(root
, 0);
2302 BUG_ON(IS_ERR(trans
));
2307 * step two, delete the device extents and the
2308 * chunk tree entries
2310 read_lock(&em_tree
->lock
);
2311 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2312 read_unlock(&em_tree
->lock
);
2314 BUG_ON(!em
|| em
->start
> chunk_offset
||
2315 em
->start
+ em
->len
< chunk_offset
);
2316 map
= (struct map_lookup
*)em
->bdev
;
2318 for (i
= 0; i
< map
->num_stripes
; i
++) {
2319 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2320 map
->stripes
[i
].physical
);
2323 if (map
->stripes
[i
].dev
) {
2324 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2328 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2333 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2335 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2336 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2340 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2343 write_lock(&em_tree
->lock
);
2344 remove_extent_mapping(em_tree
, em
);
2345 write_unlock(&em_tree
->lock
);
2350 /* once for the tree */
2351 free_extent_map(em
);
2353 free_extent_map(em
);
2355 unlock_chunks(root
);
2356 btrfs_end_transaction(trans
, root
);
2360 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2362 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2363 struct btrfs_path
*path
;
2364 struct extent_buffer
*leaf
;
2365 struct btrfs_chunk
*chunk
;
2366 struct btrfs_key key
;
2367 struct btrfs_key found_key
;
2368 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2370 bool retried
= false;
2374 path
= btrfs_alloc_path();
2379 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2380 key
.offset
= (u64
)-1;
2381 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2384 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2387 BUG_ON(ret
== 0); /* Corruption */
2389 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2396 leaf
= path
->nodes
[0];
2397 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2399 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2400 struct btrfs_chunk
);
2401 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2402 btrfs_release_path(path
);
2404 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2405 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2414 if (found_key
.offset
== 0)
2416 key
.offset
= found_key
.offset
- 1;
2419 if (failed
&& !retried
) {
2423 } else if (failed
&& retried
) {
2428 btrfs_free_path(path
);
2432 static int insert_balance_item(struct btrfs_root
*root
,
2433 struct btrfs_balance_control
*bctl
)
2435 struct btrfs_trans_handle
*trans
;
2436 struct btrfs_balance_item
*item
;
2437 struct btrfs_disk_balance_args disk_bargs
;
2438 struct btrfs_path
*path
;
2439 struct extent_buffer
*leaf
;
2440 struct btrfs_key key
;
2443 path
= btrfs_alloc_path();
2447 trans
= btrfs_start_transaction(root
, 0);
2448 if (IS_ERR(trans
)) {
2449 btrfs_free_path(path
);
2450 return PTR_ERR(trans
);
2453 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2454 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2457 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2462 leaf
= path
->nodes
[0];
2463 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2465 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2467 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2468 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2469 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2470 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2471 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2472 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2474 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2476 btrfs_mark_buffer_dirty(leaf
);
2478 btrfs_free_path(path
);
2479 err
= btrfs_commit_transaction(trans
, root
);
2485 static int del_balance_item(struct btrfs_root
*root
)
2487 struct btrfs_trans_handle
*trans
;
2488 struct btrfs_path
*path
;
2489 struct btrfs_key key
;
2492 path
= btrfs_alloc_path();
2496 trans
= btrfs_start_transaction(root
, 0);
2497 if (IS_ERR(trans
)) {
2498 btrfs_free_path(path
);
2499 return PTR_ERR(trans
);
2502 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2503 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2506 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2514 ret
= btrfs_del_item(trans
, root
, path
);
2516 btrfs_free_path(path
);
2517 err
= btrfs_commit_transaction(trans
, root
);
2524 * This is a heuristic used to reduce the number of chunks balanced on
2525 * resume after balance was interrupted.
2527 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2530 * Turn on soft mode for chunk types that were being converted.
2532 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2533 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2534 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2535 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2536 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2537 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2540 * Turn on usage filter if is not already used. The idea is
2541 * that chunks that we have already balanced should be
2542 * reasonably full. Don't do it for chunks that are being
2543 * converted - that will keep us from relocating unconverted
2544 * (albeit full) chunks.
2546 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2547 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2548 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2549 bctl
->data
.usage
= 90;
2551 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2552 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2553 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2554 bctl
->sys
.usage
= 90;
2556 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2557 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2558 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2559 bctl
->meta
.usage
= 90;
2564 * Should be called with both balance and volume mutexes held to
2565 * serialize other volume operations (add_dev/rm_dev/resize) with
2566 * restriper. Same goes for unset_balance_control.
2568 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2570 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2572 BUG_ON(fs_info
->balance_ctl
);
2574 spin_lock(&fs_info
->balance_lock
);
2575 fs_info
->balance_ctl
= bctl
;
2576 spin_unlock(&fs_info
->balance_lock
);
2579 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2581 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2583 BUG_ON(!fs_info
->balance_ctl
);
2585 spin_lock(&fs_info
->balance_lock
);
2586 fs_info
->balance_ctl
= NULL
;
2587 spin_unlock(&fs_info
->balance_lock
);
2593 * Balance filters. Return 1 if chunk should be filtered out
2594 * (should not be balanced).
2596 static int chunk_profiles_filter(u64 chunk_type
,
2597 struct btrfs_balance_args
*bargs
)
2599 chunk_type
= chunk_to_extended(chunk_type
) &
2600 BTRFS_EXTENDED_PROFILE_MASK
;
2602 if (bargs
->profiles
& chunk_type
)
2608 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2609 struct btrfs_balance_args
*bargs
)
2611 struct btrfs_block_group_cache
*cache
;
2612 u64 chunk_used
, user_thresh
;
2615 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2616 chunk_used
= btrfs_block_group_used(&cache
->item
);
2618 user_thresh
= div_factor_fine(cache
->key
.offset
, bargs
->usage
);
2619 if (chunk_used
< user_thresh
)
2622 btrfs_put_block_group(cache
);
2626 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2627 struct btrfs_chunk
*chunk
,
2628 struct btrfs_balance_args
*bargs
)
2630 struct btrfs_stripe
*stripe
;
2631 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2634 for (i
= 0; i
< num_stripes
; i
++) {
2635 stripe
= btrfs_stripe_nr(chunk
, i
);
2636 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2643 /* [pstart, pend) */
2644 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2645 struct btrfs_chunk
*chunk
,
2647 struct btrfs_balance_args
*bargs
)
2649 struct btrfs_stripe
*stripe
;
2650 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2656 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2659 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2660 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
))
2664 factor
= num_stripes
/ factor
;
2666 for (i
= 0; i
< num_stripes
; i
++) {
2667 stripe
= btrfs_stripe_nr(chunk
, i
);
2668 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2671 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2672 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2673 do_div(stripe_length
, factor
);
2675 if (stripe_offset
< bargs
->pend
&&
2676 stripe_offset
+ stripe_length
> bargs
->pstart
)
2683 /* [vstart, vend) */
2684 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2685 struct btrfs_chunk
*chunk
,
2687 struct btrfs_balance_args
*bargs
)
2689 if (chunk_offset
< bargs
->vend
&&
2690 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2691 /* at least part of the chunk is inside this vrange */
2697 static int chunk_soft_convert_filter(u64 chunk_type
,
2698 struct btrfs_balance_args
*bargs
)
2700 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2703 chunk_type
= chunk_to_extended(chunk_type
) &
2704 BTRFS_EXTENDED_PROFILE_MASK
;
2706 if (bargs
->target
== chunk_type
)
2712 static int should_balance_chunk(struct btrfs_root
*root
,
2713 struct extent_buffer
*leaf
,
2714 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2716 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2717 struct btrfs_balance_args
*bargs
= NULL
;
2718 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2721 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2722 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2726 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2727 bargs
= &bctl
->data
;
2728 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2730 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2731 bargs
= &bctl
->meta
;
2733 /* profiles filter */
2734 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2735 chunk_profiles_filter(chunk_type
, bargs
)) {
2740 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2741 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2746 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2747 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2751 /* drange filter, makes sense only with devid filter */
2752 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2753 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2758 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2759 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2763 /* soft profile changing mode */
2764 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2765 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2772 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2774 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2775 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2776 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2777 struct list_head
*devices
;
2778 struct btrfs_device
*device
;
2781 struct btrfs_chunk
*chunk
;
2782 struct btrfs_path
*path
;
2783 struct btrfs_key key
;
2784 struct btrfs_key found_key
;
2785 struct btrfs_trans_handle
*trans
;
2786 struct extent_buffer
*leaf
;
2789 int enospc_errors
= 0;
2790 bool counting
= true;
2792 /* step one make some room on all the devices */
2793 devices
= &fs_info
->fs_devices
->devices
;
2794 list_for_each_entry(device
, devices
, dev_list
) {
2795 old_size
= device
->total_bytes
;
2796 size_to_free
= div_factor(old_size
, 1);
2797 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2798 if (!device
->writeable
||
2799 device
->total_bytes
- device
->bytes_used
> size_to_free
||
2800 device
->is_tgtdev_for_dev_replace
)
2803 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2808 trans
= btrfs_start_transaction(dev_root
, 0);
2809 BUG_ON(IS_ERR(trans
));
2811 ret
= btrfs_grow_device(trans
, device
, old_size
);
2814 btrfs_end_transaction(trans
, dev_root
);
2817 /* step two, relocate all the chunks */
2818 path
= btrfs_alloc_path();
2824 /* zero out stat counters */
2825 spin_lock(&fs_info
->balance_lock
);
2826 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2827 spin_unlock(&fs_info
->balance_lock
);
2829 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2830 key
.offset
= (u64
)-1;
2831 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2834 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2835 atomic_read(&fs_info
->balance_cancel_req
)) {
2840 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2845 * this shouldn't happen, it means the last relocate
2849 BUG(); /* FIXME break ? */
2851 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2852 BTRFS_CHUNK_ITEM_KEY
);
2858 leaf
= path
->nodes
[0];
2859 slot
= path
->slots
[0];
2860 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2862 if (found_key
.objectid
!= key
.objectid
)
2865 /* chunk zero is special */
2866 if (found_key
.offset
== 0)
2869 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2872 spin_lock(&fs_info
->balance_lock
);
2873 bctl
->stat
.considered
++;
2874 spin_unlock(&fs_info
->balance_lock
);
2877 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2879 btrfs_release_path(path
);
2884 spin_lock(&fs_info
->balance_lock
);
2885 bctl
->stat
.expected
++;
2886 spin_unlock(&fs_info
->balance_lock
);
2890 ret
= btrfs_relocate_chunk(chunk_root
,
2891 chunk_root
->root_key
.objectid
,
2894 if (ret
&& ret
!= -ENOSPC
)
2896 if (ret
== -ENOSPC
) {
2899 spin_lock(&fs_info
->balance_lock
);
2900 bctl
->stat
.completed
++;
2901 spin_unlock(&fs_info
->balance_lock
);
2904 key
.offset
= found_key
.offset
- 1;
2908 btrfs_release_path(path
);
2913 btrfs_free_path(path
);
2914 if (enospc_errors
) {
2915 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2925 * alloc_profile_is_valid - see if a given profile is valid and reduced
2926 * @flags: profile to validate
2927 * @extended: if true @flags is treated as an extended profile
2929 static int alloc_profile_is_valid(u64 flags
, int extended
)
2931 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
2932 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
2934 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
2936 /* 1) check that all other bits are zeroed */
2940 /* 2) see if profile is reduced */
2942 return !extended
; /* "0" is valid for usual profiles */
2944 /* true if exactly one bit set */
2945 return (flags
& (flags
- 1)) == 0;
2948 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
2950 /* cancel requested || normal exit path */
2951 return atomic_read(&fs_info
->balance_cancel_req
) ||
2952 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
2953 atomic_read(&fs_info
->balance_cancel_req
) == 0);
2956 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
2960 unset_balance_control(fs_info
);
2961 ret
= del_balance_item(fs_info
->tree_root
);
2965 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
2966 struct btrfs_ioctl_balance_args
*bargs
);
2969 * Should be called with both balance and volume mutexes held
2971 int btrfs_balance(struct btrfs_balance_control
*bctl
,
2972 struct btrfs_ioctl_balance_args
*bargs
)
2974 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2980 if (btrfs_fs_closing(fs_info
) ||
2981 atomic_read(&fs_info
->balance_pause_req
) ||
2982 atomic_read(&fs_info
->balance_cancel_req
)) {
2987 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
2988 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
2992 * In case of mixed groups both data and meta should be picked,
2993 * and identical options should be given for both of them.
2995 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
2996 if (mixed
&& (bctl
->flags
& allowed
)) {
2997 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
2998 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
2999 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3000 printk(KERN_ERR
"btrfs: with mixed groups data and "
3001 "metadata balance options must be the same\n");
3007 num_devices
= fs_info
->fs_devices
->num_devices
;
3008 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3009 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3010 BUG_ON(num_devices
< 1);
3013 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3014 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
3015 if (num_devices
== 1)
3016 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3017 else if (num_devices
< 4)
3018 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3020 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
3021 BTRFS_BLOCK_GROUP_RAID10
);
3023 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3024 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3025 (bctl
->data
.target
& ~allowed
))) {
3026 printk(KERN_ERR
"btrfs: unable to start balance with target "
3027 "data profile %llu\n",
3028 (unsigned long long)bctl
->data
.target
);
3032 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3033 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3034 (bctl
->meta
.target
& ~allowed
))) {
3035 printk(KERN_ERR
"btrfs: unable to start balance with target "
3036 "metadata profile %llu\n",
3037 (unsigned long long)bctl
->meta
.target
);
3041 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3042 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3043 (bctl
->sys
.target
& ~allowed
))) {
3044 printk(KERN_ERR
"btrfs: unable to start balance with target "
3045 "system profile %llu\n",
3046 (unsigned long long)bctl
->sys
.target
);
3051 /* allow dup'ed data chunks only in mixed mode */
3052 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3053 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3054 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
3059 /* allow to reduce meta or sys integrity only if force set */
3060 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3061 BTRFS_BLOCK_GROUP_RAID10
;
3062 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3063 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3064 !(bctl
->sys
.target
& allowed
)) ||
3065 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3066 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3067 !(bctl
->meta
.target
& allowed
))) {
3068 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3069 printk(KERN_INFO
"btrfs: force reducing metadata "
3072 printk(KERN_ERR
"btrfs: balance will reduce metadata "
3073 "integrity, use force if you want this\n");
3079 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3080 int num_tolerated_disk_barrier_failures
;
3081 u64 target
= bctl
->sys
.target
;
3083 num_tolerated_disk_barrier_failures
=
3084 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3085 if (num_tolerated_disk_barrier_failures
> 0 &&
3087 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3088 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3089 num_tolerated_disk_barrier_failures
= 0;
3090 else if (num_tolerated_disk_barrier_failures
> 1 &&
3092 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3093 num_tolerated_disk_barrier_failures
= 1;
3095 fs_info
->num_tolerated_disk_barrier_failures
=
3096 num_tolerated_disk_barrier_failures
;
3099 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3100 if (ret
&& ret
!= -EEXIST
)
3103 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3104 BUG_ON(ret
== -EEXIST
);
3105 set_balance_control(bctl
);
3107 BUG_ON(ret
!= -EEXIST
);
3108 spin_lock(&fs_info
->balance_lock
);
3109 update_balance_args(bctl
);
3110 spin_unlock(&fs_info
->balance_lock
);
3113 atomic_inc(&fs_info
->balance_running
);
3114 mutex_unlock(&fs_info
->balance_mutex
);
3116 ret
= __btrfs_balance(fs_info
);
3118 mutex_lock(&fs_info
->balance_mutex
);
3119 atomic_dec(&fs_info
->balance_running
);
3122 memset(bargs
, 0, sizeof(*bargs
));
3123 update_ioctl_balance_args(fs_info
, 0, bargs
);
3126 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3127 balance_need_close(fs_info
)) {
3128 __cancel_balance(fs_info
);
3131 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3132 fs_info
->num_tolerated_disk_barrier_failures
=
3133 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3136 wake_up(&fs_info
->balance_wait_q
);
3140 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3141 __cancel_balance(fs_info
);
3147 static int balance_kthread(void *data
)
3149 struct btrfs_fs_info
*fs_info
= data
;
3152 mutex_lock(&fs_info
->volume_mutex
);
3153 mutex_lock(&fs_info
->balance_mutex
);
3155 if (fs_info
->balance_ctl
) {
3156 printk(KERN_INFO
"btrfs: continuing balance\n");
3157 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3160 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3161 mutex_unlock(&fs_info
->balance_mutex
);
3162 mutex_unlock(&fs_info
->volume_mutex
);
3167 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3169 struct task_struct
*tsk
;
3171 spin_lock(&fs_info
->balance_lock
);
3172 if (!fs_info
->balance_ctl
) {
3173 spin_unlock(&fs_info
->balance_lock
);
3176 spin_unlock(&fs_info
->balance_lock
);
3178 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3179 printk(KERN_INFO
"btrfs: force skipping balance\n");
3183 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3184 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3186 return PTR_ERR(tsk
);
3191 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3193 struct btrfs_balance_control
*bctl
;
3194 struct btrfs_balance_item
*item
;
3195 struct btrfs_disk_balance_args disk_bargs
;
3196 struct btrfs_path
*path
;
3197 struct extent_buffer
*leaf
;
3198 struct btrfs_key key
;
3201 path
= btrfs_alloc_path();
3205 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3206 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3209 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3212 if (ret
> 0) { /* ret = -ENOENT; */
3217 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3223 leaf
= path
->nodes
[0];
3224 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3226 bctl
->fs_info
= fs_info
;
3227 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3228 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3230 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3231 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3232 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3233 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3234 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3235 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3237 mutex_lock(&fs_info
->volume_mutex
);
3238 mutex_lock(&fs_info
->balance_mutex
);
3240 set_balance_control(bctl
);
3242 mutex_unlock(&fs_info
->balance_mutex
);
3243 mutex_unlock(&fs_info
->volume_mutex
);
3245 btrfs_free_path(path
);
3249 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3253 mutex_lock(&fs_info
->balance_mutex
);
3254 if (!fs_info
->balance_ctl
) {
3255 mutex_unlock(&fs_info
->balance_mutex
);
3259 if (atomic_read(&fs_info
->balance_running
)) {
3260 atomic_inc(&fs_info
->balance_pause_req
);
3261 mutex_unlock(&fs_info
->balance_mutex
);
3263 wait_event(fs_info
->balance_wait_q
,
3264 atomic_read(&fs_info
->balance_running
) == 0);
3266 mutex_lock(&fs_info
->balance_mutex
);
3267 /* we are good with balance_ctl ripped off from under us */
3268 BUG_ON(atomic_read(&fs_info
->balance_running
));
3269 atomic_dec(&fs_info
->balance_pause_req
);
3274 mutex_unlock(&fs_info
->balance_mutex
);
3278 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3280 mutex_lock(&fs_info
->balance_mutex
);
3281 if (!fs_info
->balance_ctl
) {
3282 mutex_unlock(&fs_info
->balance_mutex
);
3286 atomic_inc(&fs_info
->balance_cancel_req
);
3288 * if we are running just wait and return, balance item is
3289 * deleted in btrfs_balance in this case
3291 if (atomic_read(&fs_info
->balance_running
)) {
3292 mutex_unlock(&fs_info
->balance_mutex
);
3293 wait_event(fs_info
->balance_wait_q
,
3294 atomic_read(&fs_info
->balance_running
) == 0);
3295 mutex_lock(&fs_info
->balance_mutex
);
3297 /* __cancel_balance needs volume_mutex */
3298 mutex_unlock(&fs_info
->balance_mutex
);
3299 mutex_lock(&fs_info
->volume_mutex
);
3300 mutex_lock(&fs_info
->balance_mutex
);
3302 if (fs_info
->balance_ctl
)
3303 __cancel_balance(fs_info
);
3305 mutex_unlock(&fs_info
->volume_mutex
);
3308 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3309 atomic_dec(&fs_info
->balance_cancel_req
);
3310 mutex_unlock(&fs_info
->balance_mutex
);
3315 * shrinking a device means finding all of the device extents past
3316 * the new size, and then following the back refs to the chunks.
3317 * The chunk relocation code actually frees the device extent
3319 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3321 struct btrfs_trans_handle
*trans
;
3322 struct btrfs_root
*root
= device
->dev_root
;
3323 struct btrfs_dev_extent
*dev_extent
= NULL
;
3324 struct btrfs_path
*path
;
3332 bool retried
= false;
3333 struct extent_buffer
*l
;
3334 struct btrfs_key key
;
3335 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3336 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3337 u64 old_size
= device
->total_bytes
;
3338 u64 diff
= device
->total_bytes
- new_size
;
3340 if (device
->is_tgtdev_for_dev_replace
)
3343 path
= btrfs_alloc_path();
3351 device
->total_bytes
= new_size
;
3352 if (device
->writeable
) {
3353 device
->fs_devices
->total_rw_bytes
-= diff
;
3354 spin_lock(&root
->fs_info
->free_chunk_lock
);
3355 root
->fs_info
->free_chunk_space
-= diff
;
3356 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3358 unlock_chunks(root
);
3361 key
.objectid
= device
->devid
;
3362 key
.offset
= (u64
)-1;
3363 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3366 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3370 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3375 btrfs_release_path(path
);
3380 slot
= path
->slots
[0];
3381 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3383 if (key
.objectid
!= device
->devid
) {
3384 btrfs_release_path(path
);
3388 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3389 length
= btrfs_dev_extent_length(l
, dev_extent
);
3391 if (key
.offset
+ length
<= new_size
) {
3392 btrfs_release_path(path
);
3396 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3397 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3398 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3399 btrfs_release_path(path
);
3401 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3403 if (ret
&& ret
!= -ENOSPC
)
3407 } while (key
.offset
-- > 0);
3409 if (failed
&& !retried
) {
3413 } else if (failed
&& retried
) {
3417 device
->total_bytes
= old_size
;
3418 if (device
->writeable
)
3419 device
->fs_devices
->total_rw_bytes
+= diff
;
3420 spin_lock(&root
->fs_info
->free_chunk_lock
);
3421 root
->fs_info
->free_chunk_space
+= diff
;
3422 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3423 unlock_chunks(root
);
3427 /* Shrinking succeeded, else we would be at "done". */
3428 trans
= btrfs_start_transaction(root
, 0);
3429 if (IS_ERR(trans
)) {
3430 ret
= PTR_ERR(trans
);
3436 device
->disk_total_bytes
= new_size
;
3437 /* Now btrfs_update_device() will change the on-disk size. */
3438 ret
= btrfs_update_device(trans
, device
);
3440 unlock_chunks(root
);
3441 btrfs_end_transaction(trans
, root
);
3444 WARN_ON(diff
> old_total
);
3445 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3446 unlock_chunks(root
);
3447 btrfs_end_transaction(trans
, root
);
3449 btrfs_free_path(path
);
3453 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3454 struct btrfs_key
*key
,
3455 struct btrfs_chunk
*chunk
, int item_size
)
3457 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3458 struct btrfs_disk_key disk_key
;
3462 array_size
= btrfs_super_sys_array_size(super_copy
);
3463 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3466 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3467 btrfs_cpu_key_to_disk(&disk_key
, key
);
3468 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3469 ptr
+= sizeof(disk_key
);
3470 memcpy(ptr
, chunk
, item_size
);
3471 item_size
+= sizeof(disk_key
);
3472 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3477 * sort the devices in descending order by max_avail, total_avail
3479 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3481 const struct btrfs_device_info
*di_a
= a
;
3482 const struct btrfs_device_info
*di_b
= b
;
3484 if (di_a
->max_avail
> di_b
->max_avail
)
3486 if (di_a
->max_avail
< di_b
->max_avail
)
3488 if (di_a
->total_avail
> di_b
->total_avail
)
3490 if (di_a
->total_avail
< di_b
->total_avail
)
3495 struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
3496 { 2, 1, 0, 4, 2, 2 /* raid10 */ },
3497 { 1, 1, 2, 2, 2, 2 /* raid1 */ },
3498 { 1, 2, 1, 1, 1, 2 /* dup */ },
3499 { 1, 1, 0, 2, 1, 1 /* raid0 */ },
3500 { 1, 1, 0, 1, 1, 1 /* single */ },
3503 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3504 struct btrfs_root
*extent_root
,
3505 struct map_lookup
**map_ret
,
3506 u64
*num_bytes_out
, u64
*stripe_size_out
,
3507 u64 start
, u64 type
)
3509 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3510 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3511 struct list_head
*cur
;
3512 struct map_lookup
*map
= NULL
;
3513 struct extent_map_tree
*em_tree
;
3514 struct extent_map
*em
;
3515 struct btrfs_device_info
*devices_info
= NULL
;
3517 int num_stripes
; /* total number of stripes to allocate */
3518 int sub_stripes
; /* sub_stripes info for map */
3519 int dev_stripes
; /* stripes per dev */
3520 int devs_max
; /* max devs to use */
3521 int devs_min
; /* min devs needed */
3522 int devs_increment
; /* ndevs has to be a multiple of this */
3523 int ncopies
; /* how many copies to data has */
3525 u64 max_stripe_size
;
3534 BUG_ON(!alloc_profile_is_valid(type
, 0));
3536 if (list_empty(&fs_devices
->alloc_list
))
3539 index
= __get_raid_index(type
);
3541 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
3542 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
3543 devs_max
= btrfs_raid_array
[index
].devs_max
;
3544 devs_min
= btrfs_raid_array
[index
].devs_min
;
3545 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
3546 ncopies
= btrfs_raid_array
[index
].ncopies
;
3548 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3549 max_stripe_size
= 1024 * 1024 * 1024;
3550 max_chunk_size
= 10 * max_stripe_size
;
3551 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3552 /* for larger filesystems, use larger metadata chunks */
3553 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3554 max_stripe_size
= 1024 * 1024 * 1024;
3556 max_stripe_size
= 256 * 1024 * 1024;
3557 max_chunk_size
= max_stripe_size
;
3558 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3559 max_stripe_size
= 32 * 1024 * 1024;
3560 max_chunk_size
= 2 * max_stripe_size
;
3562 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3567 /* we don't want a chunk larger than 10% of writeable space */
3568 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3571 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3576 cur
= fs_devices
->alloc_list
.next
;
3579 * in the first pass through the devices list, we gather information
3580 * about the available holes on each device.
3583 while (cur
!= &fs_devices
->alloc_list
) {
3584 struct btrfs_device
*device
;
3588 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3592 if (!device
->writeable
) {
3594 "btrfs: read-only device in alloc_list\n");
3598 if (!device
->in_fs_metadata
||
3599 device
->is_tgtdev_for_dev_replace
)
3602 if (device
->total_bytes
> device
->bytes_used
)
3603 total_avail
= device
->total_bytes
- device
->bytes_used
;
3607 /* If there is no space on this device, skip it. */
3608 if (total_avail
== 0)
3611 ret
= find_free_dev_extent(device
,
3612 max_stripe_size
* dev_stripes
,
3613 &dev_offset
, &max_avail
);
3614 if (ret
&& ret
!= -ENOSPC
)
3618 max_avail
= max_stripe_size
* dev_stripes
;
3620 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3623 devices_info
[ndevs
].dev_offset
= dev_offset
;
3624 devices_info
[ndevs
].max_avail
= max_avail
;
3625 devices_info
[ndevs
].total_avail
= total_avail
;
3626 devices_info
[ndevs
].dev
= device
;
3628 WARN_ON(ndevs
> fs_devices
->rw_devices
);
3632 * now sort the devices by hole size / available space
3634 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3635 btrfs_cmp_device_info
, NULL
);
3637 /* round down to number of usable stripes */
3638 ndevs
-= ndevs
% devs_increment
;
3640 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3645 if (devs_max
&& ndevs
> devs_max
)
3648 * the primary goal is to maximize the number of stripes, so use as many
3649 * devices as possible, even if the stripes are not maximum sized.
3651 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3652 num_stripes
= ndevs
* dev_stripes
;
3654 if (stripe_size
* ndevs
> max_chunk_size
* ncopies
) {
3655 stripe_size
= max_chunk_size
* ncopies
;
3656 do_div(stripe_size
, ndevs
);
3659 do_div(stripe_size
, dev_stripes
);
3661 /* align to BTRFS_STRIPE_LEN */
3662 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
3663 stripe_size
*= BTRFS_STRIPE_LEN
;
3665 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3670 map
->num_stripes
= num_stripes
;
3672 for (i
= 0; i
< ndevs
; ++i
) {
3673 for (j
= 0; j
< dev_stripes
; ++j
) {
3674 int s
= i
* dev_stripes
+ j
;
3675 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3676 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3680 map
->sector_size
= extent_root
->sectorsize
;
3681 map
->stripe_len
= BTRFS_STRIPE_LEN
;
3682 map
->io_align
= BTRFS_STRIPE_LEN
;
3683 map
->io_width
= BTRFS_STRIPE_LEN
;
3685 map
->sub_stripes
= sub_stripes
;
3688 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
3690 *stripe_size_out
= stripe_size
;
3691 *num_bytes_out
= num_bytes
;
3693 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3695 em
= alloc_extent_map();
3700 em
->bdev
= (struct block_device
*)map
;
3702 em
->len
= num_bytes
;
3703 em
->block_start
= 0;
3704 em
->block_len
= em
->len
;
3706 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3707 write_lock(&em_tree
->lock
);
3708 ret
= add_extent_mapping(em_tree
, em
);
3709 write_unlock(&em_tree
->lock
);
3710 free_extent_map(em
);
3714 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3715 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3720 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3721 struct btrfs_device
*device
;
3724 device
= map
->stripes
[i
].dev
;
3725 dev_offset
= map
->stripes
[i
].physical
;
3727 ret
= btrfs_alloc_dev_extent(trans
, device
,
3728 info
->chunk_root
->root_key
.objectid
,
3729 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3730 start
, dev_offset
, stripe_size
);
3732 btrfs_abort_transaction(trans
, extent_root
, ret
);
3737 kfree(devices_info
);
3742 kfree(devices_info
);
3746 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3747 struct btrfs_root
*extent_root
,
3748 struct map_lookup
*map
, u64 chunk_offset
,
3749 u64 chunk_size
, u64 stripe_size
)
3752 struct btrfs_key key
;
3753 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3754 struct btrfs_device
*device
;
3755 struct btrfs_chunk
*chunk
;
3756 struct btrfs_stripe
*stripe
;
3757 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3761 chunk
= kzalloc(item_size
, GFP_NOFS
);
3766 while (index
< map
->num_stripes
) {
3767 device
= map
->stripes
[index
].dev
;
3768 device
->bytes_used
+= stripe_size
;
3769 ret
= btrfs_update_device(trans
, device
);
3775 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3776 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3778 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3781 stripe
= &chunk
->stripe
;
3782 while (index
< map
->num_stripes
) {
3783 device
= map
->stripes
[index
].dev
;
3784 dev_offset
= map
->stripes
[index
].physical
;
3786 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3787 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3788 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3793 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3794 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3795 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3796 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3797 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3798 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3799 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3800 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3801 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3803 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3804 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3805 key
.offset
= chunk_offset
;
3807 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3809 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3811 * TODO: Cleanup of inserted chunk root in case of
3814 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
3824 * Chunk allocation falls into two parts. The first part does works
3825 * that make the new allocated chunk useable, but not do any operation
3826 * that modifies the chunk tree. The second part does the works that
3827 * require modifying the chunk tree. This division is important for the
3828 * bootstrap process of adding storage to a seed btrfs.
3830 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3831 struct btrfs_root
*extent_root
, u64 type
)
3836 struct map_lookup
*map
;
3837 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3840 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3845 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3846 &stripe_size
, chunk_offset
, type
);
3850 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3851 chunk_size
, stripe_size
);
3857 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3858 struct btrfs_root
*root
,
3859 struct btrfs_device
*device
)
3862 u64 sys_chunk_offset
;
3866 u64 sys_stripe_size
;
3868 struct map_lookup
*map
;
3869 struct map_lookup
*sys_map
;
3870 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3871 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3874 ret
= find_next_chunk(fs_info
->chunk_root
,
3875 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3879 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3880 fs_info
->avail_metadata_alloc_bits
;
3881 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3883 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3884 &stripe_size
, chunk_offset
, alloc_profile
);
3888 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3890 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3891 fs_info
->avail_system_alloc_bits
;
3892 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3894 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3895 &sys_chunk_size
, &sys_stripe_size
,
3896 sys_chunk_offset
, alloc_profile
);
3898 btrfs_abort_transaction(trans
, root
, ret
);
3902 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3904 btrfs_abort_transaction(trans
, root
, ret
);
3909 * Modifying chunk tree needs allocating new blocks from both
3910 * system block group and metadata block group. So we only can
3911 * do operations require modifying the chunk tree after both
3912 * block groups were created.
3914 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3915 chunk_size
, stripe_size
);
3917 btrfs_abort_transaction(trans
, root
, ret
);
3921 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
3922 sys_chunk_offset
, sys_chunk_size
,
3925 btrfs_abort_transaction(trans
, root
, ret
);
3932 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
3934 struct extent_map
*em
;
3935 struct map_lookup
*map
;
3936 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
3940 read_lock(&map_tree
->map_tree
.lock
);
3941 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
3942 read_unlock(&map_tree
->map_tree
.lock
);
3946 if (btrfs_test_opt(root
, DEGRADED
)) {
3947 free_extent_map(em
);
3951 map
= (struct map_lookup
*)em
->bdev
;
3952 for (i
= 0; i
< map
->num_stripes
; i
++) {
3953 if (!map
->stripes
[i
].dev
->writeable
) {
3958 free_extent_map(em
);
3962 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
3964 extent_map_tree_init(&tree
->map_tree
);
3967 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
3969 struct extent_map
*em
;
3972 write_lock(&tree
->map_tree
.lock
);
3973 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
3975 remove_extent_mapping(&tree
->map_tree
, em
);
3976 write_unlock(&tree
->map_tree
.lock
);
3981 free_extent_map(em
);
3982 /* once for the tree */
3983 free_extent_map(em
);
3987 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
3989 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
3990 struct extent_map
*em
;
3991 struct map_lookup
*map
;
3992 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
3995 read_lock(&em_tree
->lock
);
3996 em
= lookup_extent_mapping(em_tree
, logical
, len
);
3997 read_unlock(&em_tree
->lock
);
4000 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4001 map
= (struct map_lookup
*)em
->bdev
;
4002 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4003 ret
= map
->num_stripes
;
4004 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4005 ret
= map
->sub_stripes
;
4008 free_extent_map(em
);
4010 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
4011 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))
4013 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
4018 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
4019 struct map_lookup
*map
, int first
, int num
,
4020 int optimal
, int dev_replace_is_ongoing
)
4024 struct btrfs_device
*srcdev
;
4026 if (dev_replace_is_ongoing
&&
4027 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
4028 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
4029 srcdev
= fs_info
->dev_replace
.srcdev
;
4034 * try to avoid the drive that is the source drive for a
4035 * dev-replace procedure, only choose it if no other non-missing
4036 * mirror is available
4038 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
4039 if (map
->stripes
[optimal
].dev
->bdev
&&
4040 (tolerance
|| map
->stripes
[optimal
].dev
!= srcdev
))
4042 for (i
= first
; i
< first
+ num
; i
++) {
4043 if (map
->stripes
[i
].dev
->bdev
&&
4044 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
4049 /* we couldn't find one that doesn't fail. Just return something
4050 * and the io error handling code will clean up eventually
4055 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4056 u64 logical
, u64
*length
,
4057 struct btrfs_bio
**bbio_ret
,
4060 struct extent_map
*em
;
4061 struct map_lookup
*map
;
4062 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4063 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4066 u64 stripe_end_offset
;
4075 struct btrfs_bio
*bbio
= NULL
;
4076 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
4077 int dev_replace_is_ongoing
= 0;
4078 int num_alloc_stripes
;
4079 int patch_the_first_stripe_for_dev_replace
= 0;
4080 u64 physical_to_patch_in_first_stripe
= 0;
4082 read_lock(&em_tree
->lock
);
4083 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
4084 read_unlock(&em_tree
->lock
);
4087 printk(KERN_CRIT
"btrfs: unable to find logical %llu len %llu\n",
4088 (unsigned long long)logical
,
4089 (unsigned long long)*length
);
4093 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4094 map
= (struct map_lookup
*)em
->bdev
;
4095 offset
= logical
- em
->start
;
4099 * stripe_nr counts the total number of stripes we have to stride
4100 * to get to this block
4102 do_div(stripe_nr
, map
->stripe_len
);
4104 stripe_offset
= stripe_nr
* map
->stripe_len
;
4105 BUG_ON(offset
< stripe_offset
);
4107 /* stripe_offset is the offset of this block in its stripe*/
4108 stripe_offset
= offset
- stripe_offset
;
4110 if (rw
& REQ_DISCARD
)
4111 *length
= min_t(u64
, em
->len
- offset
, *length
);
4112 else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
4113 /* we limit the length of each bio to what fits in a stripe */
4114 *length
= min_t(u64
, em
->len
- offset
,
4115 map
->stripe_len
- stripe_offset
);
4117 *length
= em
->len
- offset
;
4123 btrfs_dev_replace_lock(dev_replace
);
4124 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
4125 if (!dev_replace_is_ongoing
)
4126 btrfs_dev_replace_unlock(dev_replace
);
4128 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
4129 !(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) &&
4130 dev_replace
->tgtdev
!= NULL
) {
4132 * in dev-replace case, for repair case (that's the only
4133 * case where the mirror is selected explicitly when
4134 * calling btrfs_map_block), blocks left of the left cursor
4135 * can also be read from the target drive.
4136 * For REQ_GET_READ_MIRRORS, the target drive is added as
4137 * the last one to the array of stripes. For READ, it also
4138 * needs to be supported using the same mirror number.
4139 * If the requested block is not left of the left cursor,
4140 * EIO is returned. This can happen because btrfs_num_copies()
4141 * returns one more in the dev-replace case.
4143 u64 tmp_length
= *length
;
4144 struct btrfs_bio
*tmp_bbio
= NULL
;
4145 int tmp_num_stripes
;
4146 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4147 int index_srcdev
= 0;
4149 u64 physical_of_found
= 0;
4151 ret
= __btrfs_map_block(fs_info
, REQ_GET_READ_MIRRORS
,
4152 logical
, &tmp_length
, &tmp_bbio
, 0);
4154 WARN_ON(tmp_bbio
!= NULL
);
4158 tmp_num_stripes
= tmp_bbio
->num_stripes
;
4159 if (mirror_num
> tmp_num_stripes
) {
4161 * REQ_GET_READ_MIRRORS does not contain this
4162 * mirror, that means that the requested area
4163 * is not left of the left cursor
4171 * process the rest of the function using the mirror_num
4172 * of the source drive. Therefore look it up first.
4173 * At the end, patch the device pointer to the one of the
4176 for (i
= 0; i
< tmp_num_stripes
; i
++) {
4177 if (tmp_bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4179 * In case of DUP, in order to keep it
4180 * simple, only add the mirror with the
4181 * lowest physical address
4184 physical_of_found
<=
4185 tmp_bbio
->stripes
[i
].physical
)
4190 tmp_bbio
->stripes
[i
].physical
;
4195 mirror_num
= index_srcdev
+ 1;
4196 patch_the_first_stripe_for_dev_replace
= 1;
4197 physical_to_patch_in_first_stripe
= physical_of_found
;
4206 } else if (mirror_num
> map
->num_stripes
) {
4212 stripe_nr_orig
= stripe_nr
;
4213 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
4214 (~(map
->stripe_len
- 1));
4215 do_div(stripe_nr_end
, map
->stripe_len
);
4216 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
4218 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4219 if (rw
& REQ_DISCARD
)
4220 num_stripes
= min_t(u64
, map
->num_stripes
,
4221 stripe_nr_end
- stripe_nr_orig
);
4222 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4223 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
4224 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
))
4225 num_stripes
= map
->num_stripes
;
4226 else if (mirror_num
)
4227 stripe_index
= mirror_num
- 1;
4229 stripe_index
= find_live_mirror(fs_info
, map
, 0,
4231 current
->pid
% map
->num_stripes
,
4232 dev_replace_is_ongoing
);
4233 mirror_num
= stripe_index
+ 1;
4236 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
4237 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) {
4238 num_stripes
= map
->num_stripes
;
4239 } else if (mirror_num
) {
4240 stripe_index
= mirror_num
- 1;
4245 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4246 int factor
= map
->num_stripes
/ map
->sub_stripes
;
4248 stripe_index
= do_div(stripe_nr
, factor
);
4249 stripe_index
*= map
->sub_stripes
;
4251 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
4252 num_stripes
= map
->sub_stripes
;
4253 else if (rw
& REQ_DISCARD
)
4254 num_stripes
= min_t(u64
, map
->sub_stripes
*
4255 (stripe_nr_end
- stripe_nr_orig
),
4257 else if (mirror_num
)
4258 stripe_index
+= mirror_num
- 1;
4260 int old_stripe_index
= stripe_index
;
4261 stripe_index
= find_live_mirror(fs_info
, map
,
4263 map
->sub_stripes
, stripe_index
+
4264 current
->pid
% map
->sub_stripes
,
4265 dev_replace_is_ongoing
);
4266 mirror_num
= stripe_index
- old_stripe_index
+ 1;
4270 * after this do_div call, stripe_nr is the number of stripes
4271 * on this device we have to walk to find the data, and
4272 * stripe_index is the number of our device in the stripe array
4274 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4275 mirror_num
= stripe_index
+ 1;
4277 BUG_ON(stripe_index
>= map
->num_stripes
);
4279 num_alloc_stripes
= num_stripes
;
4280 if (dev_replace_is_ongoing
) {
4281 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
4282 num_alloc_stripes
<<= 1;
4283 if (rw
& REQ_GET_READ_MIRRORS
)
4284 num_alloc_stripes
++;
4286 bbio
= kzalloc(btrfs_bio_size(num_alloc_stripes
), GFP_NOFS
);
4291 atomic_set(&bbio
->error
, 0);
4293 if (rw
& REQ_DISCARD
) {
4295 int sub_stripes
= 0;
4296 u64 stripes_per_dev
= 0;
4297 u32 remaining_stripes
= 0;
4298 u32 last_stripe
= 0;
4301 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
4302 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4305 sub_stripes
= map
->sub_stripes
;
4307 factor
= map
->num_stripes
/ sub_stripes
;
4308 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
4311 &remaining_stripes
);
4312 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
4313 last_stripe
*= sub_stripes
;
4316 for (i
= 0; i
< num_stripes
; i
++) {
4317 bbio
->stripes
[i
].physical
=
4318 map
->stripes
[stripe_index
].physical
+
4319 stripe_offset
+ stripe_nr
* map
->stripe_len
;
4320 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
4322 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
4323 BTRFS_BLOCK_GROUP_RAID10
)) {
4324 bbio
->stripes
[i
].length
= stripes_per_dev
*
4327 if (i
/ sub_stripes
< remaining_stripes
)
4328 bbio
->stripes
[i
].length
+=
4332 * Special for the first stripe and
4335 * |-------|...|-------|
4339 if (i
< sub_stripes
)
4340 bbio
->stripes
[i
].length
-=
4343 if (stripe_index
>= last_stripe
&&
4344 stripe_index
<= (last_stripe
+
4346 bbio
->stripes
[i
].length
-=
4349 if (i
== sub_stripes
- 1)
4352 bbio
->stripes
[i
].length
= *length
;
4355 if (stripe_index
== map
->num_stripes
) {
4356 /* This could only happen for RAID0/10 */
4362 for (i
= 0; i
< num_stripes
; i
++) {
4363 bbio
->stripes
[i
].physical
=
4364 map
->stripes
[stripe_index
].physical
+
4366 stripe_nr
* map
->stripe_len
;
4367 bbio
->stripes
[i
].dev
=
4368 map
->stripes
[stripe_index
].dev
;
4373 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
)) {
4374 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4375 BTRFS_BLOCK_GROUP_RAID10
|
4376 BTRFS_BLOCK_GROUP_DUP
)) {
4381 if (dev_replace_is_ongoing
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
4382 dev_replace
->tgtdev
!= NULL
) {
4383 int index_where_to_add
;
4384 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4387 * duplicate the write operations while the dev replace
4388 * procedure is running. Since the copying of the old disk
4389 * to the new disk takes place at run time while the
4390 * filesystem is mounted writable, the regular write
4391 * operations to the old disk have to be duplicated to go
4392 * to the new disk as well.
4393 * Note that device->missing is handled by the caller, and
4394 * that the write to the old disk is already set up in the
4397 index_where_to_add
= num_stripes
;
4398 for (i
= 0; i
< num_stripes
; i
++) {
4399 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4400 /* write to new disk, too */
4401 struct btrfs_bio_stripe
*new =
4402 bbio
->stripes
+ index_where_to_add
;
4403 struct btrfs_bio_stripe
*old
=
4406 new->physical
= old
->physical
;
4407 new->length
= old
->length
;
4408 new->dev
= dev_replace
->tgtdev
;
4409 index_where_to_add
++;
4413 num_stripes
= index_where_to_add
;
4414 } else if (dev_replace_is_ongoing
&& (rw
& REQ_GET_READ_MIRRORS
) &&
4415 dev_replace
->tgtdev
!= NULL
) {
4416 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4417 int index_srcdev
= 0;
4419 u64 physical_of_found
= 0;
4422 * During the dev-replace procedure, the target drive can
4423 * also be used to read data in case it is needed to repair
4424 * a corrupt block elsewhere. This is possible if the
4425 * requested area is left of the left cursor. In this area,
4426 * the target drive is a full copy of the source drive.
4428 for (i
= 0; i
< num_stripes
; i
++) {
4429 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4431 * In case of DUP, in order to keep it
4432 * simple, only add the mirror with the
4433 * lowest physical address
4436 physical_of_found
<=
4437 bbio
->stripes
[i
].physical
)
4441 physical_of_found
= bbio
->stripes
[i
].physical
;
4445 u64 length
= map
->stripe_len
;
4447 if (physical_of_found
+ length
<=
4448 dev_replace
->cursor_left
) {
4449 struct btrfs_bio_stripe
*tgtdev_stripe
=
4450 bbio
->stripes
+ num_stripes
;
4452 tgtdev_stripe
->physical
= physical_of_found
;
4453 tgtdev_stripe
->length
=
4454 bbio
->stripes
[index_srcdev
].length
;
4455 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
4463 bbio
->num_stripes
= num_stripes
;
4464 bbio
->max_errors
= max_errors
;
4465 bbio
->mirror_num
= mirror_num
;
4468 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4469 * mirror_num == num_stripes + 1 && dev_replace target drive is
4470 * available as a mirror
4472 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
4473 WARN_ON(num_stripes
> 1);
4474 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
4475 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
4476 bbio
->mirror_num
= map
->num_stripes
+ 1;
4479 if (dev_replace_is_ongoing
)
4480 btrfs_dev_replace_unlock(dev_replace
);
4481 free_extent_map(em
);
4485 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4486 u64 logical
, u64
*length
,
4487 struct btrfs_bio
**bbio_ret
, int mirror_num
)
4489 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
4493 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
4494 u64 chunk_start
, u64 physical
, u64 devid
,
4495 u64
**logical
, int *naddrs
, int *stripe_len
)
4497 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4498 struct extent_map
*em
;
4499 struct map_lookup
*map
;
4506 read_lock(&em_tree
->lock
);
4507 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
4508 read_unlock(&em_tree
->lock
);
4510 BUG_ON(!em
|| em
->start
!= chunk_start
);
4511 map
= (struct map_lookup
*)em
->bdev
;
4514 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4515 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
4516 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4517 do_div(length
, map
->num_stripes
);
4519 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
4520 BUG_ON(!buf
); /* -ENOMEM */
4522 for (i
= 0; i
< map
->num_stripes
; i
++) {
4523 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
4525 if (map
->stripes
[i
].physical
> physical
||
4526 map
->stripes
[i
].physical
+ length
<= physical
)
4529 stripe_nr
= physical
- map
->stripes
[i
].physical
;
4530 do_div(stripe_nr
, map
->stripe_len
);
4532 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4533 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4534 do_div(stripe_nr
, map
->sub_stripes
);
4535 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4536 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4538 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
4539 WARN_ON(nr
>= map
->num_stripes
);
4540 for (j
= 0; j
< nr
; j
++) {
4541 if (buf
[j
] == bytenr
)
4545 WARN_ON(nr
>= map
->num_stripes
);
4552 *stripe_len
= map
->stripe_len
;
4554 free_extent_map(em
);
4558 static void *merge_stripe_index_into_bio_private(void *bi_private
,
4559 unsigned int stripe_index
)
4562 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4564 * The alternative solution (instead of stealing bits from the
4565 * pointer) would be to allocate an intermediate structure
4566 * that contains the old private pointer plus the stripe_index.
4568 BUG_ON((((uintptr_t)bi_private
) & 3) != 0);
4569 BUG_ON(stripe_index
> 3);
4570 return (void *)(((uintptr_t)bi_private
) | stripe_index
);
4573 static struct btrfs_bio
*extract_bbio_from_bio_private(void *bi_private
)
4575 return (struct btrfs_bio
*)(((uintptr_t)bi_private
) & ~((uintptr_t)3));
4578 static unsigned int extract_stripe_index_from_bio_private(void *bi_private
)
4580 return (unsigned int)((uintptr_t)bi_private
) & 3;
4583 static void btrfs_end_bio(struct bio
*bio
, int err
)
4585 struct btrfs_bio
*bbio
= extract_bbio_from_bio_private(bio
->bi_private
);
4586 int is_orig_bio
= 0;
4589 atomic_inc(&bbio
->error
);
4590 if (err
== -EIO
|| err
== -EREMOTEIO
) {
4591 unsigned int stripe_index
=
4592 extract_stripe_index_from_bio_private(
4594 struct btrfs_device
*dev
;
4596 BUG_ON(stripe_index
>= bbio
->num_stripes
);
4597 dev
= bbio
->stripes
[stripe_index
].dev
;
4599 if (bio
->bi_rw
& WRITE
)
4600 btrfs_dev_stat_inc(dev
,
4601 BTRFS_DEV_STAT_WRITE_ERRS
);
4603 btrfs_dev_stat_inc(dev
,
4604 BTRFS_DEV_STAT_READ_ERRS
);
4605 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
4606 btrfs_dev_stat_inc(dev
,
4607 BTRFS_DEV_STAT_FLUSH_ERRS
);
4608 btrfs_dev_stat_print_on_error(dev
);
4613 if (bio
== bbio
->orig_bio
)
4616 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4619 bio
= bbio
->orig_bio
;
4621 bio
->bi_private
= bbio
->private;
4622 bio
->bi_end_io
= bbio
->end_io
;
4623 bio
->bi_bdev
= (struct block_device
*)
4624 (unsigned long)bbio
->mirror_num
;
4625 /* only send an error to the higher layers if it is
4626 * beyond the tolerance of the multi-bio
4628 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
4632 * this bio is actually up to date, we didn't
4633 * go over the max number of errors
4635 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
4640 bio_endio(bio
, err
);
4641 } else if (!is_orig_bio
) {
4646 struct async_sched
{
4649 struct btrfs_fs_info
*info
;
4650 struct btrfs_work work
;
4654 * see run_scheduled_bios for a description of why bios are collected for
4657 * This will add one bio to the pending list for a device and make sure
4658 * the work struct is scheduled.
4660 static noinline
void schedule_bio(struct btrfs_root
*root
,
4661 struct btrfs_device
*device
,
4662 int rw
, struct bio
*bio
)
4664 int should_queue
= 1;
4665 struct btrfs_pending_bios
*pending_bios
;
4667 /* don't bother with additional async steps for reads, right now */
4668 if (!(rw
& REQ_WRITE
)) {
4670 btrfsic_submit_bio(rw
, bio
);
4676 * nr_async_bios allows us to reliably return congestion to the
4677 * higher layers. Otherwise, the async bio makes it appear we have
4678 * made progress against dirty pages when we've really just put it
4679 * on a queue for later
4681 atomic_inc(&root
->fs_info
->nr_async_bios
);
4682 WARN_ON(bio
->bi_next
);
4683 bio
->bi_next
= NULL
;
4686 spin_lock(&device
->io_lock
);
4687 if (bio
->bi_rw
& REQ_SYNC
)
4688 pending_bios
= &device
->pending_sync_bios
;
4690 pending_bios
= &device
->pending_bios
;
4692 if (pending_bios
->tail
)
4693 pending_bios
->tail
->bi_next
= bio
;
4695 pending_bios
->tail
= bio
;
4696 if (!pending_bios
->head
)
4697 pending_bios
->head
= bio
;
4698 if (device
->running_pending
)
4701 spin_unlock(&device
->io_lock
);
4704 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
4708 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
4711 struct bio_vec
*prev
;
4712 struct request_queue
*q
= bdev_get_queue(bdev
);
4713 unsigned short max_sectors
= queue_max_sectors(q
);
4714 struct bvec_merge_data bvm
= {
4716 .bi_sector
= sector
,
4717 .bi_rw
= bio
->bi_rw
,
4720 if (bio
->bi_vcnt
== 0) {
4725 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
4726 if ((bio
->bi_size
>> 9) > max_sectors
)
4729 if (!q
->merge_bvec_fn
)
4732 bvm
.bi_size
= bio
->bi_size
- prev
->bv_len
;
4733 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
4738 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
4739 struct bio
*bio
, u64 physical
, int dev_nr
,
4742 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
4744 bio
->bi_private
= bbio
;
4745 bio
->bi_private
= merge_stripe_index_into_bio_private(
4746 bio
->bi_private
, (unsigned int)dev_nr
);
4747 bio
->bi_end_io
= btrfs_end_bio
;
4748 bio
->bi_sector
= physical
>> 9;
4751 struct rcu_string
*name
;
4754 name
= rcu_dereference(dev
->name
);
4755 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
4756 "(%s id %llu), size=%u\n", rw
,
4757 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
4758 name
->str
, dev
->devid
, bio
->bi_size
);
4762 bio
->bi_bdev
= dev
->bdev
;
4764 schedule_bio(root
, dev
, rw
, bio
);
4766 btrfsic_submit_bio(rw
, bio
);
4769 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
4770 struct bio
*first_bio
, struct btrfs_device
*dev
,
4771 int dev_nr
, int rw
, int async
)
4773 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
4775 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
4776 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
4779 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
4783 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
4784 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
4785 bvec
->bv_offset
) < bvec
->bv_len
) {
4786 u64 len
= bio
->bi_size
;
4788 atomic_inc(&bbio
->stripes_pending
);
4789 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
4797 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
4801 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
4803 atomic_inc(&bbio
->error
);
4804 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4805 bio
->bi_private
= bbio
->private;
4806 bio
->bi_end_io
= bbio
->end_io
;
4807 bio
->bi_bdev
= (struct block_device
*)
4808 (unsigned long)bbio
->mirror_num
;
4809 bio
->bi_sector
= logical
>> 9;
4811 bio_endio(bio
, -EIO
);
4815 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
4816 int mirror_num
, int async_submit
)
4818 struct btrfs_device
*dev
;
4819 struct bio
*first_bio
= bio
;
4820 u64 logical
= (u64
)bio
->bi_sector
<< 9;
4826 struct btrfs_bio
*bbio
= NULL
;
4828 length
= bio
->bi_size
;
4829 map_length
= length
;
4831 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
4836 total_devs
= bbio
->num_stripes
;
4837 if (map_length
< length
) {
4838 printk(KERN_CRIT
"btrfs: mapping failed logical %llu bio len %llu "
4839 "len %llu\n", (unsigned long long)logical
,
4840 (unsigned long long)length
,
4841 (unsigned long long)map_length
);
4845 bbio
->orig_bio
= first_bio
;
4846 bbio
->private = first_bio
->bi_private
;
4847 bbio
->end_io
= first_bio
->bi_end_io
;
4848 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
4850 while (dev_nr
< total_devs
) {
4851 dev
= bbio
->stripes
[dev_nr
].dev
;
4852 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
4853 bbio_error(bbio
, first_bio
, logical
);
4859 * Check and see if we're ok with this bio based on it's size
4860 * and offset with the given device.
4862 if (!bio_size_ok(dev
->bdev
, first_bio
,
4863 bbio
->stripes
[dev_nr
].physical
>> 9)) {
4864 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
4865 dev_nr
, rw
, async_submit
);
4871 if (dev_nr
< total_devs
- 1) {
4872 bio
= bio_clone(first_bio
, GFP_NOFS
);
4873 BUG_ON(!bio
); /* -ENOMEM */
4878 submit_stripe_bio(root
, bbio
, bio
,
4879 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
4886 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
4889 struct btrfs_device
*device
;
4890 struct btrfs_fs_devices
*cur_devices
;
4892 cur_devices
= fs_info
->fs_devices
;
4893 while (cur_devices
) {
4895 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4896 device
= __find_device(&cur_devices
->devices
,
4901 cur_devices
= cur_devices
->seed
;
4906 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
4907 u64 devid
, u8
*dev_uuid
)
4909 struct btrfs_device
*device
;
4910 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4912 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
4915 list_add(&device
->dev_list
,
4916 &fs_devices
->devices
);
4917 device
->dev_root
= root
->fs_info
->dev_root
;
4918 device
->devid
= devid
;
4919 device
->work
.func
= pending_bios_fn
;
4920 device
->fs_devices
= fs_devices
;
4921 device
->missing
= 1;
4922 fs_devices
->num_devices
++;
4923 fs_devices
->missing_devices
++;
4924 spin_lock_init(&device
->io_lock
);
4925 INIT_LIST_HEAD(&device
->dev_alloc_list
);
4926 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
4930 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
4931 struct extent_buffer
*leaf
,
4932 struct btrfs_chunk
*chunk
)
4934 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4935 struct map_lookup
*map
;
4936 struct extent_map
*em
;
4940 u8 uuid
[BTRFS_UUID_SIZE
];
4945 logical
= key
->offset
;
4946 length
= btrfs_chunk_length(leaf
, chunk
);
4948 read_lock(&map_tree
->map_tree
.lock
);
4949 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
4950 read_unlock(&map_tree
->map_tree
.lock
);
4952 /* already mapped? */
4953 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
4954 free_extent_map(em
);
4957 free_extent_map(em
);
4960 em
= alloc_extent_map();
4963 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
4964 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4966 free_extent_map(em
);
4970 em
->bdev
= (struct block_device
*)map
;
4971 em
->start
= logical
;
4974 em
->block_start
= 0;
4975 em
->block_len
= em
->len
;
4977 map
->num_stripes
= num_stripes
;
4978 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
4979 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
4980 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
4981 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
4982 map
->type
= btrfs_chunk_type(leaf
, chunk
);
4983 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
4984 for (i
= 0; i
< num_stripes
; i
++) {
4985 map
->stripes
[i
].physical
=
4986 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
4987 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
4988 read_extent_buffer(leaf
, uuid
, (unsigned long)
4989 btrfs_stripe_dev_uuid_nr(chunk
, i
),
4991 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
4993 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
4995 free_extent_map(em
);
4998 if (!map
->stripes
[i
].dev
) {
4999 map
->stripes
[i
].dev
=
5000 add_missing_dev(root
, devid
, uuid
);
5001 if (!map
->stripes
[i
].dev
) {
5003 free_extent_map(em
);
5007 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
5010 write_lock(&map_tree
->map_tree
.lock
);
5011 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
5012 write_unlock(&map_tree
->map_tree
.lock
);
5013 BUG_ON(ret
); /* Tree corruption */
5014 free_extent_map(em
);
5019 static void fill_device_from_item(struct extent_buffer
*leaf
,
5020 struct btrfs_dev_item
*dev_item
,
5021 struct btrfs_device
*device
)
5025 device
->devid
= btrfs_device_id(leaf
, dev_item
);
5026 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
5027 device
->total_bytes
= device
->disk_total_bytes
;
5028 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
5029 device
->type
= btrfs_device_type(leaf
, dev_item
);
5030 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
5031 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
5032 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
5033 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
5034 device
->is_tgtdev_for_dev_replace
= 0;
5036 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
5037 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
5040 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
5042 struct btrfs_fs_devices
*fs_devices
;
5045 BUG_ON(!mutex_is_locked(&uuid_mutex
));
5047 fs_devices
= root
->fs_info
->fs_devices
->seed
;
5048 while (fs_devices
) {
5049 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5053 fs_devices
= fs_devices
->seed
;
5056 fs_devices
= find_fsid(fsid
);
5062 fs_devices
= clone_fs_devices(fs_devices
);
5063 if (IS_ERR(fs_devices
)) {
5064 ret
= PTR_ERR(fs_devices
);
5068 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
5069 root
->fs_info
->bdev_holder
);
5071 free_fs_devices(fs_devices
);
5075 if (!fs_devices
->seeding
) {
5076 __btrfs_close_devices(fs_devices
);
5077 free_fs_devices(fs_devices
);
5082 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
5083 root
->fs_info
->fs_devices
->seed
= fs_devices
;
5088 static int read_one_dev(struct btrfs_root
*root
,
5089 struct extent_buffer
*leaf
,
5090 struct btrfs_dev_item
*dev_item
)
5092 struct btrfs_device
*device
;
5095 u8 fs_uuid
[BTRFS_UUID_SIZE
];
5096 u8 dev_uuid
[BTRFS_UUID_SIZE
];
5098 devid
= btrfs_device_id(leaf
, dev_item
);
5099 read_extent_buffer(leaf
, dev_uuid
,
5100 (unsigned long)btrfs_device_uuid(dev_item
),
5102 read_extent_buffer(leaf
, fs_uuid
,
5103 (unsigned long)btrfs_device_fsid(dev_item
),
5106 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
5107 ret
= open_seed_devices(root
, fs_uuid
);
5108 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
5112 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
5113 if (!device
|| !device
->bdev
) {
5114 if (!btrfs_test_opt(root
, DEGRADED
))
5118 printk(KERN_WARNING
"warning devid %llu missing\n",
5119 (unsigned long long)devid
);
5120 device
= add_missing_dev(root
, devid
, dev_uuid
);
5123 } else if (!device
->missing
) {
5125 * this happens when a device that was properly setup
5126 * in the device info lists suddenly goes bad.
5127 * device->bdev is NULL, and so we have to set
5128 * device->missing to one here
5130 root
->fs_info
->fs_devices
->missing_devices
++;
5131 device
->missing
= 1;
5135 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
5136 BUG_ON(device
->writeable
);
5137 if (device
->generation
!=
5138 btrfs_device_generation(leaf
, dev_item
))
5142 fill_device_from_item(leaf
, dev_item
, device
);
5143 device
->dev_root
= root
->fs_info
->dev_root
;
5144 device
->in_fs_metadata
= 1;
5145 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
5146 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
5147 spin_lock(&root
->fs_info
->free_chunk_lock
);
5148 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
5150 spin_unlock(&root
->fs_info
->free_chunk_lock
);
5156 int btrfs_read_sys_array(struct btrfs_root
*root
)
5158 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
5159 struct extent_buffer
*sb
;
5160 struct btrfs_disk_key
*disk_key
;
5161 struct btrfs_chunk
*chunk
;
5163 unsigned long sb_ptr
;
5169 struct btrfs_key key
;
5171 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
5172 BTRFS_SUPER_INFO_SIZE
);
5175 btrfs_set_buffer_uptodate(sb
);
5176 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
5178 * The sb extent buffer is artifical and just used to read the system array.
5179 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5180 * pages up-to-date when the page is larger: extent does not cover the
5181 * whole page and consequently check_page_uptodate does not find all
5182 * the page's extents up-to-date (the hole beyond sb),
5183 * write_extent_buffer then triggers a WARN_ON.
5185 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5186 * but sb spans only this function. Add an explicit SetPageUptodate call
5187 * to silence the warning eg. on PowerPC 64.
5189 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
5190 SetPageUptodate(sb
->pages
[0]);
5192 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
5193 array_size
= btrfs_super_sys_array_size(super_copy
);
5195 ptr
= super_copy
->sys_chunk_array
;
5196 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
5199 while (cur
< array_size
) {
5200 disk_key
= (struct btrfs_disk_key
*)ptr
;
5201 btrfs_disk_key_to_cpu(&key
, disk_key
);
5203 len
= sizeof(*disk_key
); ptr
+= len
;
5207 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
5208 chunk
= (struct btrfs_chunk
*)sb_ptr
;
5209 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
5212 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
5213 len
= btrfs_chunk_item_size(num_stripes
);
5222 free_extent_buffer(sb
);
5226 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
5228 struct btrfs_path
*path
;
5229 struct extent_buffer
*leaf
;
5230 struct btrfs_key key
;
5231 struct btrfs_key found_key
;
5235 root
= root
->fs_info
->chunk_root
;
5237 path
= btrfs_alloc_path();
5241 mutex_lock(&uuid_mutex
);
5244 /* first we search for all of the device items, and then we
5245 * read in all of the chunk items. This way we can create chunk
5246 * mappings that reference all of the devices that are afound
5248 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
5252 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5256 leaf
= path
->nodes
[0];
5257 slot
= path
->slots
[0];
5258 if (slot
>= btrfs_header_nritems(leaf
)) {
5259 ret
= btrfs_next_leaf(root
, path
);
5266 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5267 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5268 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
5270 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
5271 struct btrfs_dev_item
*dev_item
;
5272 dev_item
= btrfs_item_ptr(leaf
, slot
,
5273 struct btrfs_dev_item
);
5274 ret
= read_one_dev(root
, leaf
, dev_item
);
5278 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
5279 struct btrfs_chunk
*chunk
;
5280 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
5281 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
5287 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5289 btrfs_release_path(path
);
5294 unlock_chunks(root
);
5295 mutex_unlock(&uuid_mutex
);
5297 btrfs_free_path(path
);
5301 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
5305 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5306 btrfs_dev_stat_reset(dev
, i
);
5309 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
5311 struct btrfs_key key
;
5312 struct btrfs_key found_key
;
5313 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5314 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5315 struct extent_buffer
*eb
;
5318 struct btrfs_device
*device
;
5319 struct btrfs_path
*path
= NULL
;
5322 path
= btrfs_alloc_path();
5328 mutex_lock(&fs_devices
->device_list_mutex
);
5329 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5331 struct btrfs_dev_stats_item
*ptr
;
5334 key
.type
= BTRFS_DEV_STATS_KEY
;
5335 key
.offset
= device
->devid
;
5336 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
5338 __btrfs_reset_dev_stats(device
);
5339 device
->dev_stats_valid
= 1;
5340 btrfs_release_path(path
);
5343 slot
= path
->slots
[0];
5344 eb
= path
->nodes
[0];
5345 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5346 item_size
= btrfs_item_size_nr(eb
, slot
);
5348 ptr
= btrfs_item_ptr(eb
, slot
,
5349 struct btrfs_dev_stats_item
);
5351 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5352 if (item_size
>= (1 + i
) * sizeof(__le64
))
5353 btrfs_dev_stat_set(device
, i
,
5354 btrfs_dev_stats_value(eb
, ptr
, i
));
5356 btrfs_dev_stat_reset(device
, i
);
5359 device
->dev_stats_valid
= 1;
5360 btrfs_dev_stat_print_on_load(device
);
5361 btrfs_release_path(path
);
5363 mutex_unlock(&fs_devices
->device_list_mutex
);
5366 btrfs_free_path(path
);
5367 return ret
< 0 ? ret
: 0;
5370 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
5371 struct btrfs_root
*dev_root
,
5372 struct btrfs_device
*device
)
5374 struct btrfs_path
*path
;
5375 struct btrfs_key key
;
5376 struct extent_buffer
*eb
;
5377 struct btrfs_dev_stats_item
*ptr
;
5382 key
.type
= BTRFS_DEV_STATS_KEY
;
5383 key
.offset
= device
->devid
;
5385 path
= btrfs_alloc_path();
5387 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
5389 printk_in_rcu(KERN_WARNING
"btrfs: error %d while searching for dev_stats item for device %s!\n",
5390 ret
, rcu_str_deref(device
->name
));
5395 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
5396 /* need to delete old one and insert a new one */
5397 ret
= btrfs_del_item(trans
, dev_root
, path
);
5399 printk_in_rcu(KERN_WARNING
"btrfs: delete too small dev_stats item for device %s failed %d!\n",
5400 rcu_str_deref(device
->name
), ret
);
5407 /* need to insert a new item */
5408 btrfs_release_path(path
);
5409 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
5410 &key
, sizeof(*ptr
));
5412 printk_in_rcu(KERN_WARNING
"btrfs: insert dev_stats item for device %s failed %d!\n",
5413 rcu_str_deref(device
->name
), ret
);
5418 eb
= path
->nodes
[0];
5419 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
5420 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5421 btrfs_set_dev_stats_value(eb
, ptr
, i
,
5422 btrfs_dev_stat_read(device
, i
));
5423 btrfs_mark_buffer_dirty(eb
);
5426 btrfs_free_path(path
);
5431 * called from commit_transaction. Writes all changed device stats to disk.
5433 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
5434 struct btrfs_fs_info
*fs_info
)
5436 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5437 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5438 struct btrfs_device
*device
;
5441 mutex_lock(&fs_devices
->device_list_mutex
);
5442 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5443 if (!device
->dev_stats_valid
|| !device
->dev_stats_dirty
)
5446 ret
= update_dev_stat_item(trans
, dev_root
, device
);
5448 device
->dev_stats_dirty
= 0;
5450 mutex_unlock(&fs_devices
->device_list_mutex
);
5455 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
5457 btrfs_dev_stat_inc(dev
, index
);
5458 btrfs_dev_stat_print_on_error(dev
);
5461 void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
5463 if (!dev
->dev_stats_valid
)
5465 printk_ratelimited_in_rcu(KERN_ERR
5466 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5467 rcu_str_deref(dev
->name
),
5468 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5469 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5470 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5471 btrfs_dev_stat_read(dev
,
5472 BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5473 btrfs_dev_stat_read(dev
,
5474 BTRFS_DEV_STAT_GENERATION_ERRS
));
5477 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
5481 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5482 if (btrfs_dev_stat_read(dev
, i
) != 0)
5484 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
5485 return; /* all values == 0, suppress message */
5487 printk_in_rcu(KERN_INFO
"btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5488 rcu_str_deref(dev
->name
),
5489 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5490 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5491 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5492 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5493 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
5496 int btrfs_get_dev_stats(struct btrfs_root
*root
,
5497 struct btrfs_ioctl_get_dev_stats
*stats
)
5499 struct btrfs_device
*dev
;
5500 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5503 mutex_lock(&fs_devices
->device_list_mutex
);
5504 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
5505 mutex_unlock(&fs_devices
->device_list_mutex
);
5509 "btrfs: get dev_stats failed, device not found\n");
5511 } else if (!dev
->dev_stats_valid
) {
5513 "btrfs: get dev_stats failed, not yet valid\n");
5515 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
5516 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5517 if (stats
->nr_items
> i
)
5519 btrfs_dev_stat_read_and_reset(dev
, i
);
5521 btrfs_dev_stat_reset(dev
, i
);
5524 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5525 if (stats
->nr_items
> i
)
5526 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
5528 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
5529 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
5533 int btrfs_scratch_superblock(struct btrfs_device
*device
)
5535 struct buffer_head
*bh
;
5536 struct btrfs_super_block
*disk_super
;
5538 bh
= btrfs_read_dev_super(device
->bdev
);
5541 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
5543 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
5544 set_buffer_dirty(bh
);
5545 sync_dirty_buffer(bh
);