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
);
796 * Look for a btrfs signature on a device. This may be called out of the mount path
797 * and we are not allowed to call set_blocksize during the scan. The superblock
798 * is read via pagecache
800 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
801 struct btrfs_fs_devices
**fs_devices_ret
)
803 struct btrfs_super_block
*disk_super
;
804 struct block_device
*bdev
;
815 * we would like to check all the supers, but that would make
816 * a btrfs mount succeed after a mkfs from a different FS.
817 * So, we need to add a special mount option to scan for
818 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
820 bytenr
= btrfs_sb_offset(0);
822 mutex_lock(&uuid_mutex
);
824 bdev
= blkdev_get_by_path(path
, flags
, holder
);
828 printk(KERN_INFO
"btrfs: open %s failed\n", path
);
832 /* make sure our super fits in the device */
833 if (bytenr
+ PAGE_CACHE_SIZE
>= i_size_read(bdev
->bd_inode
))
836 /* make sure our super fits in the page */
837 if (sizeof(*disk_super
) > PAGE_CACHE_SIZE
)
840 /* make sure our super doesn't straddle pages on disk */
841 index
= bytenr
>> PAGE_CACHE_SHIFT
;
842 if ((bytenr
+ sizeof(*disk_super
) - 1) >> PAGE_CACHE_SHIFT
!= index
)
845 /* pull in the page with our super */
846 page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
849 if (IS_ERR_OR_NULL(page
))
854 /* align our pointer to the offset of the super block */
855 disk_super
= p
+ (bytenr
& ~PAGE_CACHE_MASK
);
857 if (btrfs_super_bytenr(disk_super
) != bytenr
||
858 strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
859 sizeof(disk_super
->magic
)))
862 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
863 transid
= btrfs_super_generation(disk_super
);
864 total_devices
= btrfs_super_num_devices(disk_super
);
866 if (disk_super
->label
[0]) {
867 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
868 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
869 printk(KERN_INFO
"device label %s ", disk_super
->label
);
871 printk(KERN_INFO
"device fsid %pU ", disk_super
->fsid
);
874 printk(KERN_CONT
"devid %llu transid %llu %s\n",
875 (unsigned long long)devid
, (unsigned long long)transid
, path
);
877 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
878 if (!ret
&& fs_devices_ret
)
879 (*fs_devices_ret
)->total_devices
= total_devices
;
883 page_cache_release(page
);
886 blkdev_put(bdev
, flags
);
888 mutex_unlock(&uuid_mutex
);
892 /* helper to account the used device space in the range */
893 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
894 u64 end
, u64
*length
)
896 struct btrfs_key key
;
897 struct btrfs_root
*root
= device
->dev_root
;
898 struct btrfs_dev_extent
*dev_extent
;
899 struct btrfs_path
*path
;
903 struct extent_buffer
*l
;
907 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
910 path
= btrfs_alloc_path();
915 key
.objectid
= device
->devid
;
917 key
.type
= BTRFS_DEV_EXTENT_KEY
;
919 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
923 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
930 slot
= path
->slots
[0];
931 if (slot
>= btrfs_header_nritems(l
)) {
932 ret
= btrfs_next_leaf(root
, path
);
940 btrfs_item_key_to_cpu(l
, &key
, slot
);
942 if (key
.objectid
< device
->devid
)
945 if (key
.objectid
> device
->devid
)
948 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
951 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
952 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
954 if (key
.offset
<= start
&& extent_end
> end
) {
955 *length
= end
- start
+ 1;
957 } else if (key
.offset
<= start
&& extent_end
> start
)
958 *length
+= extent_end
- start
;
959 else if (key
.offset
> start
&& extent_end
<= end
)
960 *length
+= extent_end
- key
.offset
;
961 else if (key
.offset
> start
&& key
.offset
<= end
) {
962 *length
+= end
- key
.offset
+ 1;
964 } else if (key
.offset
> end
)
972 btrfs_free_path(path
);
977 * find_free_dev_extent - find free space in the specified device
978 * @device: the device which we search the free space in
979 * @num_bytes: the size of the free space that we need
980 * @start: store the start of the free space.
981 * @len: the size of the free space. that we find, or the size of the max
982 * free space if we don't find suitable free space
984 * this uses a pretty simple search, the expectation is that it is
985 * called very infrequently and that a given device has a small number
988 * @start is used to store the start of the free space if we find. But if we
989 * don't find suitable free space, it will be used to store the start position
990 * of the max free space.
992 * @len is used to store the size of the free space that we find.
993 * But if we don't find suitable free space, it is used to store the size of
994 * the max free space.
996 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
997 u64
*start
, u64
*len
)
999 struct btrfs_key key
;
1000 struct btrfs_root
*root
= device
->dev_root
;
1001 struct btrfs_dev_extent
*dev_extent
;
1002 struct btrfs_path
*path
;
1008 u64 search_end
= device
->total_bytes
;
1011 struct extent_buffer
*l
;
1013 /* FIXME use last free of some kind */
1015 /* we don't want to overwrite the superblock on the drive,
1016 * so we make sure to start at an offset of at least 1MB
1018 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
1020 max_hole_start
= search_start
;
1024 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
1029 path
= btrfs_alloc_path();
1036 key
.objectid
= device
->devid
;
1037 key
.offset
= search_start
;
1038 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1040 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1044 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1051 slot
= path
->slots
[0];
1052 if (slot
>= btrfs_header_nritems(l
)) {
1053 ret
= btrfs_next_leaf(root
, path
);
1061 btrfs_item_key_to_cpu(l
, &key
, slot
);
1063 if (key
.objectid
< device
->devid
)
1066 if (key
.objectid
> device
->devid
)
1069 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
1072 if (key
.offset
> search_start
) {
1073 hole_size
= key
.offset
- search_start
;
1075 if (hole_size
> max_hole_size
) {
1076 max_hole_start
= search_start
;
1077 max_hole_size
= hole_size
;
1081 * If this free space is greater than which we need,
1082 * it must be the max free space that we have found
1083 * until now, so max_hole_start must point to the start
1084 * of this free space and the length of this free space
1085 * is stored in max_hole_size. Thus, we return
1086 * max_hole_start and max_hole_size and go back to the
1089 if (hole_size
>= num_bytes
) {
1095 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1096 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1098 if (extent_end
> search_start
)
1099 search_start
= extent_end
;
1106 * At this point, search_start should be the end of
1107 * allocated dev extents, and when shrinking the device,
1108 * search_end may be smaller than search_start.
1110 if (search_end
> search_start
)
1111 hole_size
= search_end
- search_start
;
1113 if (hole_size
> max_hole_size
) {
1114 max_hole_start
= search_start
;
1115 max_hole_size
= hole_size
;
1119 if (hole_size
< num_bytes
)
1125 btrfs_free_path(path
);
1127 *start
= max_hole_start
;
1129 *len
= max_hole_size
;
1133 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1134 struct btrfs_device
*device
,
1138 struct btrfs_path
*path
;
1139 struct btrfs_root
*root
= device
->dev_root
;
1140 struct btrfs_key key
;
1141 struct btrfs_key found_key
;
1142 struct extent_buffer
*leaf
= NULL
;
1143 struct btrfs_dev_extent
*extent
= NULL
;
1145 path
= btrfs_alloc_path();
1149 key
.objectid
= device
->devid
;
1151 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1153 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1155 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1156 BTRFS_DEV_EXTENT_KEY
);
1159 leaf
= path
->nodes
[0];
1160 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1161 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1162 struct btrfs_dev_extent
);
1163 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1164 btrfs_dev_extent_length(leaf
, extent
) < start
);
1166 btrfs_release_path(path
);
1168 } else if (ret
== 0) {
1169 leaf
= path
->nodes
[0];
1170 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1171 struct btrfs_dev_extent
);
1173 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1177 if (device
->bytes_used
> 0) {
1178 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1179 device
->bytes_used
-= len
;
1180 spin_lock(&root
->fs_info
->free_chunk_lock
);
1181 root
->fs_info
->free_chunk_space
+= len
;
1182 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1184 ret
= btrfs_del_item(trans
, root
, path
);
1186 btrfs_error(root
->fs_info
, ret
,
1187 "Failed to remove dev extent item");
1190 btrfs_free_path(path
);
1194 int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1195 struct btrfs_device
*device
,
1196 u64 chunk_tree
, u64 chunk_objectid
,
1197 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1200 struct btrfs_path
*path
;
1201 struct btrfs_root
*root
= device
->dev_root
;
1202 struct btrfs_dev_extent
*extent
;
1203 struct extent_buffer
*leaf
;
1204 struct btrfs_key key
;
1206 WARN_ON(!device
->in_fs_metadata
);
1207 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1208 path
= btrfs_alloc_path();
1212 key
.objectid
= device
->devid
;
1214 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1215 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1220 leaf
= path
->nodes
[0];
1221 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1222 struct btrfs_dev_extent
);
1223 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1224 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1225 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1227 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1228 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent
),
1231 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1232 btrfs_mark_buffer_dirty(leaf
);
1234 btrfs_free_path(path
);
1238 static noinline
int find_next_chunk(struct btrfs_root
*root
,
1239 u64 objectid
, u64
*offset
)
1241 struct btrfs_path
*path
;
1243 struct btrfs_key key
;
1244 struct btrfs_chunk
*chunk
;
1245 struct btrfs_key found_key
;
1247 path
= btrfs_alloc_path();
1251 key
.objectid
= objectid
;
1252 key
.offset
= (u64
)-1;
1253 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
1255 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1259 BUG_ON(ret
== 0); /* Corruption */
1261 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_CHUNK_ITEM_KEY
);
1265 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1267 if (found_key
.objectid
!= objectid
)
1270 chunk
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1271 struct btrfs_chunk
);
1272 *offset
= found_key
.offset
+
1273 btrfs_chunk_length(path
->nodes
[0], chunk
);
1278 btrfs_free_path(path
);
1282 static noinline
int find_next_devid(struct btrfs_root
*root
, u64
*objectid
)
1285 struct btrfs_key key
;
1286 struct btrfs_key found_key
;
1287 struct btrfs_path
*path
;
1289 root
= root
->fs_info
->chunk_root
;
1291 path
= btrfs_alloc_path();
1295 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1296 key
.type
= BTRFS_DEV_ITEM_KEY
;
1297 key
.offset
= (u64
)-1;
1299 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1303 BUG_ON(ret
== 0); /* Corruption */
1305 ret
= btrfs_previous_item(root
, path
, BTRFS_DEV_ITEMS_OBJECTID
,
1306 BTRFS_DEV_ITEM_KEY
);
1310 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1312 *objectid
= found_key
.offset
+ 1;
1316 btrfs_free_path(path
);
1321 * the device information is stored in the chunk root
1322 * the btrfs_device struct should be fully filled in
1324 int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1325 struct btrfs_root
*root
,
1326 struct btrfs_device
*device
)
1329 struct btrfs_path
*path
;
1330 struct btrfs_dev_item
*dev_item
;
1331 struct extent_buffer
*leaf
;
1332 struct btrfs_key key
;
1335 root
= root
->fs_info
->chunk_root
;
1337 path
= btrfs_alloc_path();
1341 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1342 key
.type
= BTRFS_DEV_ITEM_KEY
;
1343 key
.offset
= device
->devid
;
1345 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1350 leaf
= path
->nodes
[0];
1351 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1353 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1354 btrfs_set_device_generation(leaf
, dev_item
, 0);
1355 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1356 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1357 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1358 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1359 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1360 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1361 btrfs_set_device_group(leaf
, dev_item
, 0);
1362 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1363 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1364 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1366 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
1367 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1368 ptr
= (unsigned long)btrfs_device_fsid(dev_item
);
1369 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1370 btrfs_mark_buffer_dirty(leaf
);
1374 btrfs_free_path(path
);
1378 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1379 struct btrfs_device
*device
)
1382 struct btrfs_path
*path
;
1383 struct btrfs_key key
;
1384 struct btrfs_trans_handle
*trans
;
1386 root
= root
->fs_info
->chunk_root
;
1388 path
= btrfs_alloc_path();
1392 trans
= btrfs_start_transaction(root
, 0);
1393 if (IS_ERR(trans
)) {
1394 btrfs_free_path(path
);
1395 return PTR_ERR(trans
);
1397 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1398 key
.type
= BTRFS_DEV_ITEM_KEY
;
1399 key
.offset
= device
->devid
;
1402 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1411 ret
= btrfs_del_item(trans
, root
, path
);
1415 btrfs_free_path(path
);
1416 unlock_chunks(root
);
1417 btrfs_commit_transaction(trans
, root
);
1421 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1423 struct btrfs_device
*device
;
1424 struct btrfs_device
*next_device
;
1425 struct block_device
*bdev
;
1426 struct buffer_head
*bh
= NULL
;
1427 struct btrfs_super_block
*disk_super
;
1428 struct btrfs_fs_devices
*cur_devices
;
1434 bool clear_super
= false;
1436 mutex_lock(&uuid_mutex
);
1438 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1439 root
->fs_info
->avail_system_alloc_bits
|
1440 root
->fs_info
->avail_metadata_alloc_bits
;
1442 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1443 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1444 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1445 WARN_ON(num_devices
< 1);
1448 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1450 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1451 printk(KERN_ERR
"btrfs: unable to go below four devices "
1457 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1458 printk(KERN_ERR
"btrfs: unable to go below two "
1459 "devices on raid1\n");
1464 if (strcmp(device_path
, "missing") == 0) {
1465 struct list_head
*devices
;
1466 struct btrfs_device
*tmp
;
1469 devices
= &root
->fs_info
->fs_devices
->devices
;
1471 * It is safe to read the devices since the volume_mutex
1474 list_for_each_entry(tmp
, devices
, dev_list
) {
1475 if (tmp
->in_fs_metadata
&&
1476 !tmp
->is_tgtdev_for_dev_replace
&&
1486 printk(KERN_ERR
"btrfs: no missing devices found to "
1491 ret
= btrfs_get_bdev_and_sb(device_path
,
1492 FMODE_WRITE
| FMODE_EXCL
,
1493 root
->fs_info
->bdev_holder
, 0,
1497 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1498 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1499 dev_uuid
= disk_super
->dev_item
.uuid
;
1500 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1508 if (device
->is_tgtdev_for_dev_replace
) {
1509 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1514 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1515 printk(KERN_ERR
"btrfs: unable to remove the only writeable "
1521 if (device
->writeable
) {
1523 list_del_init(&device
->dev_alloc_list
);
1524 unlock_chunks(root
);
1525 root
->fs_info
->fs_devices
->rw_devices
--;
1529 ret
= btrfs_shrink_device(device
, 0);
1534 * TODO: the superblock still includes this device in its num_devices
1535 * counter although write_all_supers() is not locked out. This
1536 * could give a filesystem state which requires a degraded mount.
1538 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1542 spin_lock(&root
->fs_info
->free_chunk_lock
);
1543 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1545 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1547 device
->in_fs_metadata
= 0;
1548 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1551 * the device list mutex makes sure that we don't change
1552 * the device list while someone else is writing out all
1553 * the device supers.
1556 cur_devices
= device
->fs_devices
;
1557 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1558 list_del_rcu(&device
->dev_list
);
1560 device
->fs_devices
->num_devices
--;
1561 device
->fs_devices
->total_devices
--;
1563 if (device
->missing
)
1564 root
->fs_info
->fs_devices
->missing_devices
--;
1566 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1567 struct btrfs_device
, dev_list
);
1568 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1569 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1570 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1571 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1574 device
->fs_devices
->open_devices
--;
1576 call_rcu(&device
->rcu
, free_device
);
1577 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1579 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1580 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1582 if (cur_devices
->open_devices
== 0) {
1583 struct btrfs_fs_devices
*fs_devices
;
1584 fs_devices
= root
->fs_info
->fs_devices
;
1585 while (fs_devices
) {
1586 if (fs_devices
->seed
== cur_devices
)
1588 fs_devices
= fs_devices
->seed
;
1590 fs_devices
->seed
= cur_devices
->seed
;
1591 cur_devices
->seed
= NULL
;
1593 __btrfs_close_devices(cur_devices
);
1594 unlock_chunks(root
);
1595 free_fs_devices(cur_devices
);
1598 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1599 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1602 * at this point, the device is zero sized. We want to
1603 * remove it from the devices list and zero out the old super
1605 if (clear_super
&& disk_super
) {
1606 /* make sure this device isn't detected as part of
1609 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1610 set_buffer_dirty(bh
);
1611 sync_dirty_buffer(bh
);
1616 /* Notify udev that device has changed */
1618 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
1623 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1625 mutex_unlock(&uuid_mutex
);
1628 if (device
->writeable
) {
1630 list_add(&device
->dev_alloc_list
,
1631 &root
->fs_info
->fs_devices
->alloc_list
);
1632 unlock_chunks(root
);
1633 root
->fs_info
->fs_devices
->rw_devices
++;
1638 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info
*fs_info
,
1639 struct btrfs_device
*srcdev
)
1641 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1642 list_del_rcu(&srcdev
->dev_list
);
1643 list_del_rcu(&srcdev
->dev_alloc_list
);
1644 fs_info
->fs_devices
->num_devices
--;
1645 if (srcdev
->missing
) {
1646 fs_info
->fs_devices
->missing_devices
--;
1647 fs_info
->fs_devices
->rw_devices
++;
1649 if (srcdev
->can_discard
)
1650 fs_info
->fs_devices
->num_can_discard
--;
1652 fs_info
->fs_devices
->open_devices
--;
1654 call_rcu(&srcdev
->rcu
, free_device
);
1657 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1658 struct btrfs_device
*tgtdev
)
1660 struct btrfs_device
*next_device
;
1663 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1665 btrfs_scratch_superblock(tgtdev
);
1666 fs_info
->fs_devices
->open_devices
--;
1668 fs_info
->fs_devices
->num_devices
--;
1669 if (tgtdev
->can_discard
)
1670 fs_info
->fs_devices
->num_can_discard
++;
1672 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1673 struct btrfs_device
, dev_list
);
1674 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1675 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1676 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1677 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1678 list_del_rcu(&tgtdev
->dev_list
);
1680 call_rcu(&tgtdev
->rcu
, free_device
);
1682 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1685 int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1686 struct btrfs_device
**device
)
1689 struct btrfs_super_block
*disk_super
;
1692 struct block_device
*bdev
;
1693 struct buffer_head
*bh
;
1696 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1697 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1700 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1701 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1702 dev_uuid
= disk_super
->dev_item
.uuid
;
1703 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1708 blkdev_put(bdev
, FMODE_READ
);
1712 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1714 struct btrfs_device
**device
)
1717 if (strcmp(device_path
, "missing") == 0) {
1718 struct list_head
*devices
;
1719 struct btrfs_device
*tmp
;
1721 devices
= &root
->fs_info
->fs_devices
->devices
;
1723 * It is safe to read the devices since the volume_mutex
1724 * is held by the caller.
1726 list_for_each_entry(tmp
, devices
, dev_list
) {
1727 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1734 pr_err("btrfs: no missing device found\n");
1740 return btrfs_find_device_by_path(root
, device_path
, device
);
1745 * does all the dirty work required for changing file system's UUID.
1747 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1749 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1750 struct btrfs_fs_devices
*old_devices
;
1751 struct btrfs_fs_devices
*seed_devices
;
1752 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1753 struct btrfs_device
*device
;
1756 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1757 if (!fs_devices
->seeding
)
1760 seed_devices
= kzalloc(sizeof(*fs_devices
), GFP_NOFS
);
1764 old_devices
= clone_fs_devices(fs_devices
);
1765 if (IS_ERR(old_devices
)) {
1766 kfree(seed_devices
);
1767 return PTR_ERR(old_devices
);
1770 list_add(&old_devices
->list
, &fs_uuids
);
1772 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1773 seed_devices
->opened
= 1;
1774 INIT_LIST_HEAD(&seed_devices
->devices
);
1775 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1776 mutex_init(&seed_devices
->device_list_mutex
);
1778 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1779 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1781 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1783 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1784 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1785 device
->fs_devices
= seed_devices
;
1788 fs_devices
->seeding
= 0;
1789 fs_devices
->num_devices
= 0;
1790 fs_devices
->open_devices
= 0;
1791 fs_devices
->total_devices
= 0;
1792 fs_devices
->seed
= seed_devices
;
1794 generate_random_uuid(fs_devices
->fsid
);
1795 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1796 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1797 super_flags
= btrfs_super_flags(disk_super
) &
1798 ~BTRFS_SUPER_FLAG_SEEDING
;
1799 btrfs_set_super_flags(disk_super
, super_flags
);
1805 * strore the expected generation for seed devices in device items.
1807 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1808 struct btrfs_root
*root
)
1810 struct btrfs_path
*path
;
1811 struct extent_buffer
*leaf
;
1812 struct btrfs_dev_item
*dev_item
;
1813 struct btrfs_device
*device
;
1814 struct btrfs_key key
;
1815 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1816 u8 dev_uuid
[BTRFS_UUID_SIZE
];
1820 path
= btrfs_alloc_path();
1824 root
= root
->fs_info
->chunk_root
;
1825 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1827 key
.type
= BTRFS_DEV_ITEM_KEY
;
1830 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1834 leaf
= path
->nodes
[0];
1836 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1837 ret
= btrfs_next_leaf(root
, path
);
1842 leaf
= path
->nodes
[0];
1843 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1844 btrfs_release_path(path
);
1848 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
1849 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
1850 key
.type
!= BTRFS_DEV_ITEM_KEY
)
1853 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1854 struct btrfs_dev_item
);
1855 devid
= btrfs_device_id(leaf
, dev_item
);
1856 read_extent_buffer(leaf
, dev_uuid
,
1857 (unsigned long)btrfs_device_uuid(dev_item
),
1859 read_extent_buffer(leaf
, fs_uuid
,
1860 (unsigned long)btrfs_device_fsid(dev_item
),
1862 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1864 BUG_ON(!device
); /* Logic error */
1866 if (device
->fs_devices
->seeding
) {
1867 btrfs_set_device_generation(leaf
, dev_item
,
1868 device
->generation
);
1869 btrfs_mark_buffer_dirty(leaf
);
1877 btrfs_free_path(path
);
1881 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
1883 struct request_queue
*q
;
1884 struct btrfs_trans_handle
*trans
;
1885 struct btrfs_device
*device
;
1886 struct block_device
*bdev
;
1887 struct list_head
*devices
;
1888 struct super_block
*sb
= root
->fs_info
->sb
;
1889 struct rcu_string
*name
;
1891 int seeding_dev
= 0;
1894 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
1897 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
1898 root
->fs_info
->bdev_holder
);
1900 return PTR_ERR(bdev
);
1902 if (root
->fs_info
->fs_devices
->seeding
) {
1904 down_write(&sb
->s_umount
);
1905 mutex_lock(&uuid_mutex
);
1908 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
1910 devices
= &root
->fs_info
->fs_devices
->devices
;
1912 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1913 list_for_each_entry(device
, devices
, dev_list
) {
1914 if (device
->bdev
== bdev
) {
1917 &root
->fs_info
->fs_devices
->device_list_mutex
);
1921 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1923 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
1925 /* we can safely leave the fs_devices entry around */
1930 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
1936 rcu_assign_pointer(device
->name
, name
);
1938 ret
= find_next_devid(root
, &device
->devid
);
1940 rcu_string_free(device
->name
);
1945 trans
= btrfs_start_transaction(root
, 0);
1946 if (IS_ERR(trans
)) {
1947 rcu_string_free(device
->name
);
1949 ret
= PTR_ERR(trans
);
1955 q
= bdev_get_queue(bdev
);
1956 if (blk_queue_discard(q
))
1957 device
->can_discard
= 1;
1958 device
->writeable
= 1;
1959 device
->work
.func
= pending_bios_fn
;
1960 generate_random_uuid(device
->uuid
);
1961 spin_lock_init(&device
->io_lock
);
1962 device
->generation
= trans
->transid
;
1963 device
->io_width
= root
->sectorsize
;
1964 device
->io_align
= root
->sectorsize
;
1965 device
->sector_size
= root
->sectorsize
;
1966 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
1967 device
->disk_total_bytes
= device
->total_bytes
;
1968 device
->dev_root
= root
->fs_info
->dev_root
;
1969 device
->bdev
= bdev
;
1970 device
->in_fs_metadata
= 1;
1971 device
->is_tgtdev_for_dev_replace
= 0;
1972 device
->mode
= FMODE_EXCL
;
1973 set_blocksize(device
->bdev
, 4096);
1976 sb
->s_flags
&= ~MS_RDONLY
;
1977 ret
= btrfs_prepare_sprout(root
);
1978 BUG_ON(ret
); /* -ENOMEM */
1981 device
->fs_devices
= root
->fs_info
->fs_devices
;
1983 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1984 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
1985 list_add(&device
->dev_alloc_list
,
1986 &root
->fs_info
->fs_devices
->alloc_list
);
1987 root
->fs_info
->fs_devices
->num_devices
++;
1988 root
->fs_info
->fs_devices
->open_devices
++;
1989 root
->fs_info
->fs_devices
->rw_devices
++;
1990 root
->fs_info
->fs_devices
->total_devices
++;
1991 if (device
->can_discard
)
1992 root
->fs_info
->fs_devices
->num_can_discard
++;
1993 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
1995 spin_lock(&root
->fs_info
->free_chunk_lock
);
1996 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
1997 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1999 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
2000 root
->fs_info
->fs_devices
->rotating
= 1;
2002 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
2003 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
2004 total_bytes
+ device
->total_bytes
);
2006 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
2007 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
2009 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2012 ret
= init_first_rw_device(trans
, root
, device
);
2014 btrfs_abort_transaction(trans
, root
, ret
);
2017 ret
= btrfs_finish_sprout(trans
, root
);
2019 btrfs_abort_transaction(trans
, root
, ret
);
2023 ret
= btrfs_add_device(trans
, root
, device
);
2025 btrfs_abort_transaction(trans
, root
, ret
);
2031 * we've got more storage, clear any full flags on the space
2034 btrfs_clear_space_info_full(root
->fs_info
);
2036 unlock_chunks(root
);
2037 root
->fs_info
->num_tolerated_disk_barrier_failures
=
2038 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
2039 ret
= btrfs_commit_transaction(trans
, root
);
2042 mutex_unlock(&uuid_mutex
);
2043 up_write(&sb
->s_umount
);
2045 if (ret
) /* transaction commit */
2048 ret
= btrfs_relocate_sys_chunks(root
);
2050 btrfs_error(root
->fs_info
, ret
,
2051 "Failed to relocate sys chunks after "
2052 "device initialization. This can be fixed "
2053 "using the \"btrfs balance\" command.");
2054 trans
= btrfs_attach_transaction(root
);
2055 if (IS_ERR(trans
)) {
2056 if (PTR_ERR(trans
) == -ENOENT
)
2058 return PTR_ERR(trans
);
2060 ret
= btrfs_commit_transaction(trans
, root
);
2066 unlock_chunks(root
);
2067 btrfs_end_transaction(trans
, root
);
2068 rcu_string_free(device
->name
);
2071 blkdev_put(bdev
, FMODE_EXCL
);
2073 mutex_unlock(&uuid_mutex
);
2074 up_write(&sb
->s_umount
);
2079 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2080 struct btrfs_device
**device_out
)
2082 struct request_queue
*q
;
2083 struct btrfs_device
*device
;
2084 struct block_device
*bdev
;
2085 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2086 struct list_head
*devices
;
2087 struct rcu_string
*name
;
2091 if (fs_info
->fs_devices
->seeding
)
2094 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2095 fs_info
->bdev_holder
);
2097 return PTR_ERR(bdev
);
2099 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2101 devices
= &fs_info
->fs_devices
->devices
;
2102 list_for_each_entry(device
, devices
, dev_list
) {
2103 if (device
->bdev
== bdev
) {
2109 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
2115 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2121 rcu_assign_pointer(device
->name
, name
);
2123 q
= bdev_get_queue(bdev
);
2124 if (blk_queue_discard(q
))
2125 device
->can_discard
= 1;
2126 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2127 device
->writeable
= 1;
2128 device
->work
.func
= pending_bios_fn
;
2129 generate_random_uuid(device
->uuid
);
2130 device
->devid
= BTRFS_DEV_REPLACE_DEVID
;
2131 spin_lock_init(&device
->io_lock
);
2132 device
->generation
= 0;
2133 device
->io_width
= root
->sectorsize
;
2134 device
->io_align
= root
->sectorsize
;
2135 device
->sector_size
= root
->sectorsize
;
2136 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2137 device
->disk_total_bytes
= device
->total_bytes
;
2138 device
->dev_root
= fs_info
->dev_root
;
2139 device
->bdev
= bdev
;
2140 device
->in_fs_metadata
= 1;
2141 device
->is_tgtdev_for_dev_replace
= 1;
2142 device
->mode
= FMODE_EXCL
;
2143 set_blocksize(device
->bdev
, 4096);
2144 device
->fs_devices
= fs_info
->fs_devices
;
2145 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2146 fs_info
->fs_devices
->num_devices
++;
2147 fs_info
->fs_devices
->open_devices
++;
2148 if (device
->can_discard
)
2149 fs_info
->fs_devices
->num_can_discard
++;
2150 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2152 *device_out
= device
;
2156 blkdev_put(bdev
, FMODE_EXCL
);
2160 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2161 struct btrfs_device
*tgtdev
)
2163 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2164 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2165 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2166 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2167 tgtdev
->dev_root
= fs_info
->dev_root
;
2168 tgtdev
->in_fs_metadata
= 1;
2171 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2172 struct btrfs_device
*device
)
2175 struct btrfs_path
*path
;
2176 struct btrfs_root
*root
;
2177 struct btrfs_dev_item
*dev_item
;
2178 struct extent_buffer
*leaf
;
2179 struct btrfs_key key
;
2181 root
= device
->dev_root
->fs_info
->chunk_root
;
2183 path
= btrfs_alloc_path();
2187 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2188 key
.type
= BTRFS_DEV_ITEM_KEY
;
2189 key
.offset
= device
->devid
;
2191 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2200 leaf
= path
->nodes
[0];
2201 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2203 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2204 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2205 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2206 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2207 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2208 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
2209 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
2210 btrfs_mark_buffer_dirty(leaf
);
2213 btrfs_free_path(path
);
2217 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2218 struct btrfs_device
*device
, u64 new_size
)
2220 struct btrfs_super_block
*super_copy
=
2221 device
->dev_root
->fs_info
->super_copy
;
2222 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2223 u64 diff
= new_size
- device
->total_bytes
;
2225 if (!device
->writeable
)
2227 if (new_size
<= device
->total_bytes
||
2228 device
->is_tgtdev_for_dev_replace
)
2231 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2232 device
->fs_devices
->total_rw_bytes
+= diff
;
2234 device
->total_bytes
= new_size
;
2235 device
->disk_total_bytes
= new_size
;
2236 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2238 return btrfs_update_device(trans
, device
);
2241 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2242 struct btrfs_device
*device
, u64 new_size
)
2245 lock_chunks(device
->dev_root
);
2246 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2247 unlock_chunks(device
->dev_root
);
2251 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2252 struct btrfs_root
*root
,
2253 u64 chunk_tree
, u64 chunk_objectid
,
2257 struct btrfs_path
*path
;
2258 struct btrfs_key key
;
2260 root
= root
->fs_info
->chunk_root
;
2261 path
= btrfs_alloc_path();
2265 key
.objectid
= chunk_objectid
;
2266 key
.offset
= chunk_offset
;
2267 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2269 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2272 else if (ret
> 0) { /* Logic error or corruption */
2273 btrfs_error(root
->fs_info
, -ENOENT
,
2274 "Failed lookup while freeing chunk.");
2279 ret
= btrfs_del_item(trans
, root
, path
);
2281 btrfs_error(root
->fs_info
, ret
,
2282 "Failed to delete chunk item.");
2284 btrfs_free_path(path
);
2288 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2291 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2292 struct btrfs_disk_key
*disk_key
;
2293 struct btrfs_chunk
*chunk
;
2300 struct btrfs_key key
;
2302 array_size
= btrfs_super_sys_array_size(super_copy
);
2304 ptr
= super_copy
->sys_chunk_array
;
2307 while (cur
< array_size
) {
2308 disk_key
= (struct btrfs_disk_key
*)ptr
;
2309 btrfs_disk_key_to_cpu(&key
, disk_key
);
2311 len
= sizeof(*disk_key
);
2313 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2314 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2315 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2316 len
+= btrfs_chunk_item_size(num_stripes
);
2321 if (key
.objectid
== chunk_objectid
&&
2322 key
.offset
== chunk_offset
) {
2323 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2325 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2334 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2335 u64 chunk_tree
, u64 chunk_objectid
,
2338 struct extent_map_tree
*em_tree
;
2339 struct btrfs_root
*extent_root
;
2340 struct btrfs_trans_handle
*trans
;
2341 struct extent_map
*em
;
2342 struct map_lookup
*map
;
2346 root
= root
->fs_info
->chunk_root
;
2347 extent_root
= root
->fs_info
->extent_root
;
2348 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2350 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2354 /* step one, relocate all the extents inside this chunk */
2355 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2359 trans
= btrfs_start_transaction(root
, 0);
2360 BUG_ON(IS_ERR(trans
));
2365 * step two, delete the device extents and the
2366 * chunk tree entries
2368 read_lock(&em_tree
->lock
);
2369 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2370 read_unlock(&em_tree
->lock
);
2372 BUG_ON(!em
|| em
->start
> chunk_offset
||
2373 em
->start
+ em
->len
< chunk_offset
);
2374 map
= (struct map_lookup
*)em
->bdev
;
2376 for (i
= 0; i
< map
->num_stripes
; i
++) {
2377 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2378 map
->stripes
[i
].physical
);
2381 if (map
->stripes
[i
].dev
) {
2382 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2386 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2391 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2393 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2394 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2398 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2401 write_lock(&em_tree
->lock
);
2402 remove_extent_mapping(em_tree
, em
);
2403 write_unlock(&em_tree
->lock
);
2408 /* once for the tree */
2409 free_extent_map(em
);
2411 free_extent_map(em
);
2413 unlock_chunks(root
);
2414 btrfs_end_transaction(trans
, root
);
2418 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2420 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2421 struct btrfs_path
*path
;
2422 struct extent_buffer
*leaf
;
2423 struct btrfs_chunk
*chunk
;
2424 struct btrfs_key key
;
2425 struct btrfs_key found_key
;
2426 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2428 bool retried
= false;
2432 path
= btrfs_alloc_path();
2437 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2438 key
.offset
= (u64
)-1;
2439 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2442 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2445 BUG_ON(ret
== 0); /* Corruption */
2447 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2454 leaf
= path
->nodes
[0];
2455 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2457 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2458 struct btrfs_chunk
);
2459 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2460 btrfs_release_path(path
);
2462 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2463 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2472 if (found_key
.offset
== 0)
2474 key
.offset
= found_key
.offset
- 1;
2477 if (failed
&& !retried
) {
2481 } else if (failed
&& retried
) {
2486 btrfs_free_path(path
);
2490 static int insert_balance_item(struct btrfs_root
*root
,
2491 struct btrfs_balance_control
*bctl
)
2493 struct btrfs_trans_handle
*trans
;
2494 struct btrfs_balance_item
*item
;
2495 struct btrfs_disk_balance_args disk_bargs
;
2496 struct btrfs_path
*path
;
2497 struct extent_buffer
*leaf
;
2498 struct btrfs_key key
;
2501 path
= btrfs_alloc_path();
2505 trans
= btrfs_start_transaction(root
, 0);
2506 if (IS_ERR(trans
)) {
2507 btrfs_free_path(path
);
2508 return PTR_ERR(trans
);
2511 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2512 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2515 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2520 leaf
= path
->nodes
[0];
2521 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2523 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2525 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2526 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2527 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2528 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2529 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2530 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2532 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2534 btrfs_mark_buffer_dirty(leaf
);
2536 btrfs_free_path(path
);
2537 err
= btrfs_commit_transaction(trans
, root
);
2543 static int del_balance_item(struct btrfs_root
*root
)
2545 struct btrfs_trans_handle
*trans
;
2546 struct btrfs_path
*path
;
2547 struct btrfs_key key
;
2550 path
= btrfs_alloc_path();
2554 trans
= btrfs_start_transaction(root
, 0);
2555 if (IS_ERR(trans
)) {
2556 btrfs_free_path(path
);
2557 return PTR_ERR(trans
);
2560 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2561 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2564 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2572 ret
= btrfs_del_item(trans
, root
, path
);
2574 btrfs_free_path(path
);
2575 err
= btrfs_commit_transaction(trans
, root
);
2582 * This is a heuristic used to reduce the number of chunks balanced on
2583 * resume after balance was interrupted.
2585 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2588 * Turn on soft mode for chunk types that were being converted.
2590 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2591 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2592 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2593 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2594 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2595 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2598 * Turn on usage filter if is not already used. The idea is
2599 * that chunks that we have already balanced should be
2600 * reasonably full. Don't do it for chunks that are being
2601 * converted - that will keep us from relocating unconverted
2602 * (albeit full) chunks.
2604 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2605 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2606 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2607 bctl
->data
.usage
= 90;
2609 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2610 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2611 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2612 bctl
->sys
.usage
= 90;
2614 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2615 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2616 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2617 bctl
->meta
.usage
= 90;
2622 * Should be called with both balance and volume mutexes held to
2623 * serialize other volume operations (add_dev/rm_dev/resize) with
2624 * restriper. Same goes for unset_balance_control.
2626 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2628 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2630 BUG_ON(fs_info
->balance_ctl
);
2632 spin_lock(&fs_info
->balance_lock
);
2633 fs_info
->balance_ctl
= bctl
;
2634 spin_unlock(&fs_info
->balance_lock
);
2637 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2639 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2641 BUG_ON(!fs_info
->balance_ctl
);
2643 spin_lock(&fs_info
->balance_lock
);
2644 fs_info
->balance_ctl
= NULL
;
2645 spin_unlock(&fs_info
->balance_lock
);
2651 * Balance filters. Return 1 if chunk should be filtered out
2652 * (should not be balanced).
2654 static int chunk_profiles_filter(u64 chunk_type
,
2655 struct btrfs_balance_args
*bargs
)
2657 chunk_type
= chunk_to_extended(chunk_type
) &
2658 BTRFS_EXTENDED_PROFILE_MASK
;
2660 if (bargs
->profiles
& chunk_type
)
2666 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2667 struct btrfs_balance_args
*bargs
)
2669 struct btrfs_block_group_cache
*cache
;
2670 u64 chunk_used
, user_thresh
;
2673 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2674 chunk_used
= btrfs_block_group_used(&cache
->item
);
2676 if (bargs
->usage
== 0)
2678 else if (bargs
->usage
> 100)
2679 user_thresh
= cache
->key
.offset
;
2681 user_thresh
= div_factor_fine(cache
->key
.offset
,
2684 if (chunk_used
< user_thresh
)
2687 btrfs_put_block_group(cache
);
2691 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2692 struct btrfs_chunk
*chunk
,
2693 struct btrfs_balance_args
*bargs
)
2695 struct btrfs_stripe
*stripe
;
2696 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2699 for (i
= 0; i
< num_stripes
; i
++) {
2700 stripe
= btrfs_stripe_nr(chunk
, i
);
2701 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2708 /* [pstart, pend) */
2709 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2710 struct btrfs_chunk
*chunk
,
2712 struct btrfs_balance_args
*bargs
)
2714 struct btrfs_stripe
*stripe
;
2715 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2721 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2724 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2725 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
))
2729 factor
= num_stripes
/ factor
;
2731 for (i
= 0; i
< num_stripes
; i
++) {
2732 stripe
= btrfs_stripe_nr(chunk
, i
);
2733 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2736 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2737 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2738 do_div(stripe_length
, factor
);
2740 if (stripe_offset
< bargs
->pend
&&
2741 stripe_offset
+ stripe_length
> bargs
->pstart
)
2748 /* [vstart, vend) */
2749 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2750 struct btrfs_chunk
*chunk
,
2752 struct btrfs_balance_args
*bargs
)
2754 if (chunk_offset
< bargs
->vend
&&
2755 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2756 /* at least part of the chunk is inside this vrange */
2762 static int chunk_soft_convert_filter(u64 chunk_type
,
2763 struct btrfs_balance_args
*bargs
)
2765 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2768 chunk_type
= chunk_to_extended(chunk_type
) &
2769 BTRFS_EXTENDED_PROFILE_MASK
;
2771 if (bargs
->target
== chunk_type
)
2777 static int should_balance_chunk(struct btrfs_root
*root
,
2778 struct extent_buffer
*leaf
,
2779 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2781 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2782 struct btrfs_balance_args
*bargs
= NULL
;
2783 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2786 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2787 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2791 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2792 bargs
= &bctl
->data
;
2793 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2795 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2796 bargs
= &bctl
->meta
;
2798 /* profiles filter */
2799 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2800 chunk_profiles_filter(chunk_type
, bargs
)) {
2805 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2806 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2811 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
2812 chunk_devid_filter(leaf
, chunk
, bargs
)) {
2816 /* drange filter, makes sense only with devid filter */
2817 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
2818 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2823 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
2824 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
2828 /* soft profile changing mode */
2829 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
2830 chunk_soft_convert_filter(chunk_type
, bargs
)) {
2837 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
2839 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2840 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
2841 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
2842 struct list_head
*devices
;
2843 struct btrfs_device
*device
;
2846 struct btrfs_chunk
*chunk
;
2847 struct btrfs_path
*path
;
2848 struct btrfs_key key
;
2849 struct btrfs_key found_key
;
2850 struct btrfs_trans_handle
*trans
;
2851 struct extent_buffer
*leaf
;
2854 int enospc_errors
= 0;
2855 bool counting
= true;
2857 /* step one make some room on all the devices */
2858 devices
= &fs_info
->fs_devices
->devices
;
2859 list_for_each_entry(device
, devices
, dev_list
) {
2860 old_size
= device
->total_bytes
;
2861 size_to_free
= div_factor(old_size
, 1);
2862 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
2863 if (!device
->writeable
||
2864 device
->total_bytes
- device
->bytes_used
> size_to_free
||
2865 device
->is_tgtdev_for_dev_replace
)
2868 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
2873 trans
= btrfs_start_transaction(dev_root
, 0);
2874 BUG_ON(IS_ERR(trans
));
2876 ret
= btrfs_grow_device(trans
, device
, old_size
);
2879 btrfs_end_transaction(trans
, dev_root
);
2882 /* step two, relocate all the chunks */
2883 path
= btrfs_alloc_path();
2889 /* zero out stat counters */
2890 spin_lock(&fs_info
->balance_lock
);
2891 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
2892 spin_unlock(&fs_info
->balance_lock
);
2894 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2895 key
.offset
= (u64
)-1;
2896 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2899 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
2900 atomic_read(&fs_info
->balance_cancel_req
)) {
2905 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2910 * this shouldn't happen, it means the last relocate
2914 BUG(); /* FIXME break ? */
2916 ret
= btrfs_previous_item(chunk_root
, path
, 0,
2917 BTRFS_CHUNK_ITEM_KEY
);
2923 leaf
= path
->nodes
[0];
2924 slot
= path
->slots
[0];
2925 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2927 if (found_key
.objectid
!= key
.objectid
)
2930 /* chunk zero is special */
2931 if (found_key
.offset
== 0)
2934 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
2937 spin_lock(&fs_info
->balance_lock
);
2938 bctl
->stat
.considered
++;
2939 spin_unlock(&fs_info
->balance_lock
);
2942 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
2944 btrfs_release_path(path
);
2949 spin_lock(&fs_info
->balance_lock
);
2950 bctl
->stat
.expected
++;
2951 spin_unlock(&fs_info
->balance_lock
);
2955 ret
= btrfs_relocate_chunk(chunk_root
,
2956 chunk_root
->root_key
.objectid
,
2959 if (ret
&& ret
!= -ENOSPC
)
2961 if (ret
== -ENOSPC
) {
2964 spin_lock(&fs_info
->balance_lock
);
2965 bctl
->stat
.completed
++;
2966 spin_unlock(&fs_info
->balance_lock
);
2969 key
.offset
= found_key
.offset
- 1;
2973 btrfs_release_path(path
);
2978 btrfs_free_path(path
);
2979 if (enospc_errors
) {
2980 printk(KERN_INFO
"btrfs: %d enospc errors during balance\n",
2990 * alloc_profile_is_valid - see if a given profile is valid and reduced
2991 * @flags: profile to validate
2992 * @extended: if true @flags is treated as an extended profile
2994 static int alloc_profile_is_valid(u64 flags
, int extended
)
2996 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
2997 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
2999 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3001 /* 1) check that all other bits are zeroed */
3005 /* 2) see if profile is reduced */
3007 return !extended
; /* "0" is valid for usual profiles */
3009 /* true if exactly one bit set */
3010 return (flags
& (flags
- 1)) == 0;
3013 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3015 /* cancel requested || normal exit path */
3016 return atomic_read(&fs_info
->balance_cancel_req
) ||
3017 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3018 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3021 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
3025 unset_balance_control(fs_info
);
3026 ret
= del_balance_item(fs_info
->tree_root
);
3029 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3032 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
3033 struct btrfs_ioctl_balance_args
*bargs
);
3036 * Should be called with both balance and volume mutexes held
3038 int btrfs_balance(struct btrfs_balance_control
*bctl
,
3039 struct btrfs_ioctl_balance_args
*bargs
)
3041 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
3047 if (btrfs_fs_closing(fs_info
) ||
3048 atomic_read(&fs_info
->balance_pause_req
) ||
3049 atomic_read(&fs_info
->balance_cancel_req
)) {
3054 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3055 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3059 * In case of mixed groups both data and meta should be picked,
3060 * and identical options should be given for both of them.
3062 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3063 if (mixed
&& (bctl
->flags
& allowed
)) {
3064 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3065 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3066 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3067 printk(KERN_ERR
"btrfs: with mixed groups data and "
3068 "metadata balance options must be the same\n");
3074 num_devices
= fs_info
->fs_devices
->num_devices
;
3075 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3076 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3077 BUG_ON(num_devices
< 1);
3080 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3081 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
3082 if (num_devices
== 1)
3083 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3084 else if (num_devices
< 4)
3085 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3087 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
3088 BTRFS_BLOCK_GROUP_RAID10
);
3090 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3091 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3092 (bctl
->data
.target
& ~allowed
))) {
3093 printk(KERN_ERR
"btrfs: unable to start balance with target "
3094 "data profile %llu\n",
3095 (unsigned long long)bctl
->data
.target
);
3099 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3100 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3101 (bctl
->meta
.target
& ~allowed
))) {
3102 printk(KERN_ERR
"btrfs: unable to start balance with target "
3103 "metadata profile %llu\n",
3104 (unsigned long long)bctl
->meta
.target
);
3108 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3109 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3110 (bctl
->sys
.target
& ~allowed
))) {
3111 printk(KERN_ERR
"btrfs: unable to start balance with target "
3112 "system profile %llu\n",
3113 (unsigned long long)bctl
->sys
.target
);
3118 /* allow dup'ed data chunks only in mixed mode */
3119 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3120 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3121 printk(KERN_ERR
"btrfs: dup for data is not allowed\n");
3126 /* allow to reduce meta or sys integrity only if force set */
3127 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3128 BTRFS_BLOCK_GROUP_RAID10
;
3129 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3130 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3131 !(bctl
->sys
.target
& allowed
)) ||
3132 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3133 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3134 !(bctl
->meta
.target
& allowed
))) {
3135 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3136 printk(KERN_INFO
"btrfs: force reducing metadata "
3139 printk(KERN_ERR
"btrfs: balance will reduce metadata "
3140 "integrity, use force if you want this\n");
3146 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3147 int num_tolerated_disk_barrier_failures
;
3148 u64 target
= bctl
->sys
.target
;
3150 num_tolerated_disk_barrier_failures
=
3151 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3152 if (num_tolerated_disk_barrier_failures
> 0 &&
3154 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3155 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3156 num_tolerated_disk_barrier_failures
= 0;
3157 else if (num_tolerated_disk_barrier_failures
> 1 &&
3159 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3160 num_tolerated_disk_barrier_failures
= 1;
3162 fs_info
->num_tolerated_disk_barrier_failures
=
3163 num_tolerated_disk_barrier_failures
;
3166 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3167 if (ret
&& ret
!= -EEXIST
)
3170 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3171 BUG_ON(ret
== -EEXIST
);
3172 set_balance_control(bctl
);
3174 BUG_ON(ret
!= -EEXIST
);
3175 spin_lock(&fs_info
->balance_lock
);
3176 update_balance_args(bctl
);
3177 spin_unlock(&fs_info
->balance_lock
);
3180 atomic_inc(&fs_info
->balance_running
);
3181 mutex_unlock(&fs_info
->balance_mutex
);
3183 ret
= __btrfs_balance(fs_info
);
3185 mutex_lock(&fs_info
->balance_mutex
);
3186 atomic_dec(&fs_info
->balance_running
);
3189 memset(bargs
, 0, sizeof(*bargs
));
3190 update_ioctl_balance_args(fs_info
, 0, bargs
);
3193 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3194 balance_need_close(fs_info
)) {
3195 __cancel_balance(fs_info
);
3198 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3199 fs_info
->num_tolerated_disk_barrier_failures
=
3200 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3203 wake_up(&fs_info
->balance_wait_q
);
3207 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3208 __cancel_balance(fs_info
);
3211 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3216 static int balance_kthread(void *data
)
3218 struct btrfs_fs_info
*fs_info
= data
;
3221 mutex_lock(&fs_info
->volume_mutex
);
3222 mutex_lock(&fs_info
->balance_mutex
);
3224 if (fs_info
->balance_ctl
) {
3225 printk(KERN_INFO
"btrfs: continuing balance\n");
3226 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3229 mutex_unlock(&fs_info
->balance_mutex
);
3230 mutex_unlock(&fs_info
->volume_mutex
);
3235 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3237 struct task_struct
*tsk
;
3239 spin_lock(&fs_info
->balance_lock
);
3240 if (!fs_info
->balance_ctl
) {
3241 spin_unlock(&fs_info
->balance_lock
);
3244 spin_unlock(&fs_info
->balance_lock
);
3246 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3247 printk(KERN_INFO
"btrfs: force skipping balance\n");
3251 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3253 return PTR_ERR(tsk
);
3258 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3260 struct btrfs_balance_control
*bctl
;
3261 struct btrfs_balance_item
*item
;
3262 struct btrfs_disk_balance_args disk_bargs
;
3263 struct btrfs_path
*path
;
3264 struct extent_buffer
*leaf
;
3265 struct btrfs_key key
;
3268 path
= btrfs_alloc_path();
3272 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3273 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3276 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3279 if (ret
> 0) { /* ret = -ENOENT; */
3284 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3290 leaf
= path
->nodes
[0];
3291 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3293 bctl
->fs_info
= fs_info
;
3294 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3295 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3297 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3298 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3299 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3300 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3301 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3302 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3304 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3306 mutex_lock(&fs_info
->volume_mutex
);
3307 mutex_lock(&fs_info
->balance_mutex
);
3309 set_balance_control(bctl
);
3311 mutex_unlock(&fs_info
->balance_mutex
);
3312 mutex_unlock(&fs_info
->volume_mutex
);
3314 btrfs_free_path(path
);
3318 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3322 mutex_lock(&fs_info
->balance_mutex
);
3323 if (!fs_info
->balance_ctl
) {
3324 mutex_unlock(&fs_info
->balance_mutex
);
3328 if (atomic_read(&fs_info
->balance_running
)) {
3329 atomic_inc(&fs_info
->balance_pause_req
);
3330 mutex_unlock(&fs_info
->balance_mutex
);
3332 wait_event(fs_info
->balance_wait_q
,
3333 atomic_read(&fs_info
->balance_running
) == 0);
3335 mutex_lock(&fs_info
->balance_mutex
);
3336 /* we are good with balance_ctl ripped off from under us */
3337 BUG_ON(atomic_read(&fs_info
->balance_running
));
3338 atomic_dec(&fs_info
->balance_pause_req
);
3343 mutex_unlock(&fs_info
->balance_mutex
);
3347 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3349 mutex_lock(&fs_info
->balance_mutex
);
3350 if (!fs_info
->balance_ctl
) {
3351 mutex_unlock(&fs_info
->balance_mutex
);
3355 atomic_inc(&fs_info
->balance_cancel_req
);
3357 * if we are running just wait and return, balance item is
3358 * deleted in btrfs_balance in this case
3360 if (atomic_read(&fs_info
->balance_running
)) {
3361 mutex_unlock(&fs_info
->balance_mutex
);
3362 wait_event(fs_info
->balance_wait_q
,
3363 atomic_read(&fs_info
->balance_running
) == 0);
3364 mutex_lock(&fs_info
->balance_mutex
);
3366 /* __cancel_balance needs volume_mutex */
3367 mutex_unlock(&fs_info
->balance_mutex
);
3368 mutex_lock(&fs_info
->volume_mutex
);
3369 mutex_lock(&fs_info
->balance_mutex
);
3371 if (fs_info
->balance_ctl
)
3372 __cancel_balance(fs_info
);
3374 mutex_unlock(&fs_info
->volume_mutex
);
3377 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3378 atomic_dec(&fs_info
->balance_cancel_req
);
3379 mutex_unlock(&fs_info
->balance_mutex
);
3384 * shrinking a device means finding all of the device extents past
3385 * the new size, and then following the back refs to the chunks.
3386 * The chunk relocation code actually frees the device extent
3388 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3390 struct btrfs_trans_handle
*trans
;
3391 struct btrfs_root
*root
= device
->dev_root
;
3392 struct btrfs_dev_extent
*dev_extent
= NULL
;
3393 struct btrfs_path
*path
;
3401 bool retried
= false;
3402 struct extent_buffer
*l
;
3403 struct btrfs_key key
;
3404 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3405 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3406 u64 old_size
= device
->total_bytes
;
3407 u64 diff
= device
->total_bytes
- new_size
;
3409 if (device
->is_tgtdev_for_dev_replace
)
3412 path
= btrfs_alloc_path();
3420 device
->total_bytes
= new_size
;
3421 if (device
->writeable
) {
3422 device
->fs_devices
->total_rw_bytes
-= diff
;
3423 spin_lock(&root
->fs_info
->free_chunk_lock
);
3424 root
->fs_info
->free_chunk_space
-= diff
;
3425 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3427 unlock_chunks(root
);
3430 key
.objectid
= device
->devid
;
3431 key
.offset
= (u64
)-1;
3432 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3435 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3439 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3444 btrfs_release_path(path
);
3449 slot
= path
->slots
[0];
3450 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3452 if (key
.objectid
!= device
->devid
) {
3453 btrfs_release_path(path
);
3457 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3458 length
= btrfs_dev_extent_length(l
, dev_extent
);
3460 if (key
.offset
+ length
<= new_size
) {
3461 btrfs_release_path(path
);
3465 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3466 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3467 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3468 btrfs_release_path(path
);
3470 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3472 if (ret
&& ret
!= -ENOSPC
)
3476 } while (key
.offset
-- > 0);
3478 if (failed
&& !retried
) {
3482 } else if (failed
&& retried
) {
3486 device
->total_bytes
= old_size
;
3487 if (device
->writeable
)
3488 device
->fs_devices
->total_rw_bytes
+= diff
;
3489 spin_lock(&root
->fs_info
->free_chunk_lock
);
3490 root
->fs_info
->free_chunk_space
+= diff
;
3491 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3492 unlock_chunks(root
);
3496 /* Shrinking succeeded, else we would be at "done". */
3497 trans
= btrfs_start_transaction(root
, 0);
3498 if (IS_ERR(trans
)) {
3499 ret
= PTR_ERR(trans
);
3505 device
->disk_total_bytes
= new_size
;
3506 /* Now btrfs_update_device() will change the on-disk size. */
3507 ret
= btrfs_update_device(trans
, device
);
3509 unlock_chunks(root
);
3510 btrfs_end_transaction(trans
, root
);
3513 WARN_ON(diff
> old_total
);
3514 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3515 unlock_chunks(root
);
3516 btrfs_end_transaction(trans
, root
);
3518 btrfs_free_path(path
);
3522 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3523 struct btrfs_key
*key
,
3524 struct btrfs_chunk
*chunk
, int item_size
)
3526 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3527 struct btrfs_disk_key disk_key
;
3531 array_size
= btrfs_super_sys_array_size(super_copy
);
3532 if (array_size
+ item_size
> BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
3535 ptr
= super_copy
->sys_chunk_array
+ array_size
;
3536 btrfs_cpu_key_to_disk(&disk_key
, key
);
3537 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
3538 ptr
+= sizeof(disk_key
);
3539 memcpy(ptr
, chunk
, item_size
);
3540 item_size
+= sizeof(disk_key
);
3541 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
3546 * sort the devices in descending order by max_avail, total_avail
3548 static int btrfs_cmp_device_info(const void *a
, const void *b
)
3550 const struct btrfs_device_info
*di_a
= a
;
3551 const struct btrfs_device_info
*di_b
= b
;
3553 if (di_a
->max_avail
> di_b
->max_avail
)
3555 if (di_a
->max_avail
< di_b
->max_avail
)
3557 if (di_a
->total_avail
> di_b
->total_avail
)
3559 if (di_a
->total_avail
< di_b
->total_avail
)
3564 struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
3565 { 2, 1, 0, 4, 2, 2 /* raid10 */ },
3566 { 1, 1, 2, 2, 2, 2 /* raid1 */ },
3567 { 1, 2, 1, 1, 1, 2 /* dup */ },
3568 { 1, 1, 0, 2, 1, 1 /* raid0 */ },
3569 { 1, 1, 1, 1, 1, 1 /* single */ },
3572 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3573 struct btrfs_root
*extent_root
,
3574 struct map_lookup
**map_ret
,
3575 u64
*num_bytes_out
, u64
*stripe_size_out
,
3576 u64 start
, u64 type
)
3578 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
3579 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
3580 struct list_head
*cur
;
3581 struct map_lookup
*map
= NULL
;
3582 struct extent_map_tree
*em_tree
;
3583 struct extent_map
*em
;
3584 struct btrfs_device_info
*devices_info
= NULL
;
3586 int num_stripes
; /* total number of stripes to allocate */
3587 int sub_stripes
; /* sub_stripes info for map */
3588 int dev_stripes
; /* stripes per dev */
3589 int devs_max
; /* max devs to use */
3590 int devs_min
; /* min devs needed */
3591 int devs_increment
; /* ndevs has to be a multiple of this */
3592 int ncopies
; /* how many copies to data has */
3594 u64 max_stripe_size
;
3603 BUG_ON(!alloc_profile_is_valid(type
, 0));
3605 if (list_empty(&fs_devices
->alloc_list
))
3608 index
= __get_raid_index(type
);
3610 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
3611 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
3612 devs_max
= btrfs_raid_array
[index
].devs_max
;
3613 devs_min
= btrfs_raid_array
[index
].devs_min
;
3614 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
3615 ncopies
= btrfs_raid_array
[index
].ncopies
;
3617 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
3618 max_stripe_size
= 1024 * 1024 * 1024;
3619 max_chunk_size
= 10 * max_stripe_size
;
3620 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
3621 /* for larger filesystems, use larger metadata chunks */
3622 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
3623 max_stripe_size
= 1024 * 1024 * 1024;
3625 max_stripe_size
= 256 * 1024 * 1024;
3626 max_chunk_size
= max_stripe_size
;
3627 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3628 max_stripe_size
= 32 * 1024 * 1024;
3629 max_chunk_size
= 2 * max_stripe_size
;
3631 printk(KERN_ERR
"btrfs: invalid chunk type 0x%llx requested\n",
3636 /* we don't want a chunk larger than 10% of writeable space */
3637 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
3640 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
3645 cur
= fs_devices
->alloc_list
.next
;
3648 * in the first pass through the devices list, we gather information
3649 * about the available holes on each device.
3652 while (cur
!= &fs_devices
->alloc_list
) {
3653 struct btrfs_device
*device
;
3657 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
3661 if (!device
->writeable
) {
3663 "btrfs: read-only device in alloc_list\n");
3667 if (!device
->in_fs_metadata
||
3668 device
->is_tgtdev_for_dev_replace
)
3671 if (device
->total_bytes
> device
->bytes_used
)
3672 total_avail
= device
->total_bytes
- device
->bytes_used
;
3676 /* If there is no space on this device, skip it. */
3677 if (total_avail
== 0)
3680 ret
= find_free_dev_extent(device
,
3681 max_stripe_size
* dev_stripes
,
3682 &dev_offset
, &max_avail
);
3683 if (ret
&& ret
!= -ENOSPC
)
3687 max_avail
= max_stripe_size
* dev_stripes
;
3689 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
3692 devices_info
[ndevs
].dev_offset
= dev_offset
;
3693 devices_info
[ndevs
].max_avail
= max_avail
;
3694 devices_info
[ndevs
].total_avail
= total_avail
;
3695 devices_info
[ndevs
].dev
= device
;
3697 WARN_ON(ndevs
> fs_devices
->rw_devices
);
3701 * now sort the devices by hole size / available space
3703 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
3704 btrfs_cmp_device_info
, NULL
);
3706 /* round down to number of usable stripes */
3707 ndevs
-= ndevs
% devs_increment
;
3709 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
3714 if (devs_max
&& ndevs
> devs_max
)
3717 * the primary goal is to maximize the number of stripes, so use as many
3718 * devices as possible, even if the stripes are not maximum sized.
3720 stripe_size
= devices_info
[ndevs
-1].max_avail
;
3721 num_stripes
= ndevs
* dev_stripes
;
3723 if (stripe_size
* ndevs
> max_chunk_size
* ncopies
) {
3724 stripe_size
= max_chunk_size
* ncopies
;
3725 do_div(stripe_size
, ndevs
);
3728 do_div(stripe_size
, dev_stripes
);
3730 /* align to BTRFS_STRIPE_LEN */
3731 do_div(stripe_size
, BTRFS_STRIPE_LEN
);
3732 stripe_size
*= BTRFS_STRIPE_LEN
;
3734 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
3739 map
->num_stripes
= num_stripes
;
3741 for (i
= 0; i
< ndevs
; ++i
) {
3742 for (j
= 0; j
< dev_stripes
; ++j
) {
3743 int s
= i
* dev_stripes
+ j
;
3744 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
3745 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
3749 map
->sector_size
= extent_root
->sectorsize
;
3750 map
->stripe_len
= BTRFS_STRIPE_LEN
;
3751 map
->io_align
= BTRFS_STRIPE_LEN
;
3752 map
->io_width
= BTRFS_STRIPE_LEN
;
3754 map
->sub_stripes
= sub_stripes
;
3757 num_bytes
= stripe_size
* (num_stripes
/ ncopies
);
3759 *stripe_size_out
= stripe_size
;
3760 *num_bytes_out
= num_bytes
;
3762 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
3764 em
= alloc_extent_map();
3769 em
->bdev
= (struct block_device
*)map
;
3771 em
->len
= num_bytes
;
3772 em
->block_start
= 0;
3773 em
->block_len
= em
->len
;
3775 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
3776 write_lock(&em_tree
->lock
);
3777 ret
= add_extent_mapping(em_tree
, em
);
3778 write_unlock(&em_tree
->lock
);
3779 free_extent_map(em
);
3783 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
3784 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3789 for (i
= 0; i
< map
->num_stripes
; ++i
) {
3790 struct btrfs_device
*device
;
3793 device
= map
->stripes
[i
].dev
;
3794 dev_offset
= map
->stripes
[i
].physical
;
3796 ret
= btrfs_alloc_dev_extent(trans
, device
,
3797 info
->chunk_root
->root_key
.objectid
,
3798 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3799 start
, dev_offset
, stripe_size
);
3801 btrfs_abort_transaction(trans
, extent_root
, ret
);
3806 kfree(devices_info
);
3811 kfree(devices_info
);
3815 static int __finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
3816 struct btrfs_root
*extent_root
,
3817 struct map_lookup
*map
, u64 chunk_offset
,
3818 u64 chunk_size
, u64 stripe_size
)
3821 struct btrfs_key key
;
3822 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3823 struct btrfs_device
*device
;
3824 struct btrfs_chunk
*chunk
;
3825 struct btrfs_stripe
*stripe
;
3826 size_t item_size
= btrfs_chunk_item_size(map
->num_stripes
);
3830 chunk
= kzalloc(item_size
, GFP_NOFS
);
3835 while (index
< map
->num_stripes
) {
3836 device
= map
->stripes
[index
].dev
;
3837 device
->bytes_used
+= stripe_size
;
3838 ret
= btrfs_update_device(trans
, device
);
3844 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
3845 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
3847 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
3850 stripe
= &chunk
->stripe
;
3851 while (index
< map
->num_stripes
) {
3852 device
= map
->stripes
[index
].dev
;
3853 dev_offset
= map
->stripes
[index
].physical
;
3855 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
3856 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
3857 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
3862 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
3863 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
3864 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
3865 btrfs_set_stack_chunk_type(chunk
, map
->type
);
3866 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
3867 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
3868 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
3869 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
3870 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
3872 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3873 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3874 key
.offset
= chunk_offset
;
3876 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
3878 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3880 * TODO: Cleanup of inserted chunk root in case of
3883 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
3893 * Chunk allocation falls into two parts. The first part does works
3894 * that make the new allocated chunk useable, but not do any operation
3895 * that modifies the chunk tree. The second part does the works that
3896 * require modifying the chunk tree. This division is important for the
3897 * bootstrap process of adding storage to a seed btrfs.
3899 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
3900 struct btrfs_root
*extent_root
, u64 type
)
3905 struct map_lookup
*map
;
3906 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
3909 ret
= find_next_chunk(chunk_root
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
3914 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3915 &stripe_size
, chunk_offset
, type
);
3919 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3920 chunk_size
, stripe_size
);
3926 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
3927 struct btrfs_root
*root
,
3928 struct btrfs_device
*device
)
3931 u64 sys_chunk_offset
;
3935 u64 sys_stripe_size
;
3937 struct map_lookup
*map
;
3938 struct map_lookup
*sys_map
;
3939 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3940 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
3943 ret
= find_next_chunk(fs_info
->chunk_root
,
3944 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, &chunk_offset
);
3948 alloc_profile
= BTRFS_BLOCK_GROUP_METADATA
|
3949 fs_info
->avail_metadata_alloc_bits
;
3950 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3952 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &map
, &chunk_size
,
3953 &stripe_size
, chunk_offset
, alloc_profile
);
3957 sys_chunk_offset
= chunk_offset
+ chunk_size
;
3959 alloc_profile
= BTRFS_BLOCK_GROUP_SYSTEM
|
3960 fs_info
->avail_system_alloc_bits
;
3961 alloc_profile
= btrfs_reduce_alloc_profile(root
, alloc_profile
);
3963 ret
= __btrfs_alloc_chunk(trans
, extent_root
, &sys_map
,
3964 &sys_chunk_size
, &sys_stripe_size
,
3965 sys_chunk_offset
, alloc_profile
);
3967 btrfs_abort_transaction(trans
, root
, ret
);
3971 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
3973 btrfs_abort_transaction(trans
, root
, ret
);
3978 * Modifying chunk tree needs allocating new blocks from both
3979 * system block group and metadata block group. So we only can
3980 * do operations require modifying the chunk tree after both
3981 * block groups were created.
3983 ret
= __finish_chunk_alloc(trans
, extent_root
, map
, chunk_offset
,
3984 chunk_size
, stripe_size
);
3986 btrfs_abort_transaction(trans
, root
, ret
);
3990 ret
= __finish_chunk_alloc(trans
, extent_root
, sys_map
,
3991 sys_chunk_offset
, sys_chunk_size
,
3994 btrfs_abort_transaction(trans
, root
, ret
);
4001 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
4003 struct extent_map
*em
;
4004 struct map_lookup
*map
;
4005 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4009 read_lock(&map_tree
->map_tree
.lock
);
4010 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
4011 read_unlock(&map_tree
->map_tree
.lock
);
4015 if (btrfs_test_opt(root
, DEGRADED
)) {
4016 free_extent_map(em
);
4020 map
= (struct map_lookup
*)em
->bdev
;
4021 for (i
= 0; i
< map
->num_stripes
; i
++) {
4022 if (!map
->stripes
[i
].dev
->writeable
) {
4027 free_extent_map(em
);
4031 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
4033 extent_map_tree_init(&tree
->map_tree
);
4036 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
4038 struct extent_map
*em
;
4041 write_lock(&tree
->map_tree
.lock
);
4042 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
4044 remove_extent_mapping(&tree
->map_tree
, em
);
4045 write_unlock(&tree
->map_tree
.lock
);
4050 free_extent_map(em
);
4051 /* once for the tree */
4052 free_extent_map(em
);
4056 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
4058 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4059 struct extent_map
*em
;
4060 struct map_lookup
*map
;
4061 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4064 read_lock(&em_tree
->lock
);
4065 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4066 read_unlock(&em_tree
->lock
);
4069 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4070 map
= (struct map_lookup
*)em
->bdev
;
4071 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4072 ret
= map
->num_stripes
;
4073 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4074 ret
= map
->sub_stripes
;
4077 free_extent_map(em
);
4079 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
4080 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))
4082 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
4087 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
4088 struct map_lookup
*map
, int first
, int num
,
4089 int optimal
, int dev_replace_is_ongoing
)
4093 struct btrfs_device
*srcdev
;
4095 if (dev_replace_is_ongoing
&&
4096 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
4097 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
4098 srcdev
= fs_info
->dev_replace
.srcdev
;
4103 * try to avoid the drive that is the source drive for a
4104 * dev-replace procedure, only choose it if no other non-missing
4105 * mirror is available
4107 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
4108 if (map
->stripes
[optimal
].dev
->bdev
&&
4109 (tolerance
|| map
->stripes
[optimal
].dev
!= srcdev
))
4111 for (i
= first
; i
< first
+ num
; i
++) {
4112 if (map
->stripes
[i
].dev
->bdev
&&
4113 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
4118 /* we couldn't find one that doesn't fail. Just return something
4119 * and the io error handling code will clean up eventually
4124 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4125 u64 logical
, u64
*length
,
4126 struct btrfs_bio
**bbio_ret
,
4129 struct extent_map
*em
;
4130 struct map_lookup
*map
;
4131 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4132 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4135 u64 stripe_end_offset
;
4144 struct btrfs_bio
*bbio
= NULL
;
4145 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
4146 int dev_replace_is_ongoing
= 0;
4147 int num_alloc_stripes
;
4148 int patch_the_first_stripe_for_dev_replace
= 0;
4149 u64 physical_to_patch_in_first_stripe
= 0;
4151 read_lock(&em_tree
->lock
);
4152 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
4153 read_unlock(&em_tree
->lock
);
4156 printk(KERN_CRIT
"btrfs: unable to find logical %llu len %llu\n",
4157 (unsigned long long)logical
,
4158 (unsigned long long)*length
);
4162 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4163 map
= (struct map_lookup
*)em
->bdev
;
4164 offset
= logical
- em
->start
;
4168 * stripe_nr counts the total number of stripes we have to stride
4169 * to get to this block
4171 do_div(stripe_nr
, map
->stripe_len
);
4173 stripe_offset
= stripe_nr
* map
->stripe_len
;
4174 BUG_ON(offset
< stripe_offset
);
4176 /* stripe_offset is the offset of this block in its stripe*/
4177 stripe_offset
= offset
- stripe_offset
;
4179 if (rw
& REQ_DISCARD
)
4180 *length
= min_t(u64
, em
->len
- offset
, *length
);
4181 else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
4182 /* we limit the length of each bio to what fits in a stripe */
4183 *length
= min_t(u64
, em
->len
- offset
,
4184 map
->stripe_len
- stripe_offset
);
4186 *length
= em
->len
- offset
;
4192 btrfs_dev_replace_lock(dev_replace
);
4193 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
4194 if (!dev_replace_is_ongoing
)
4195 btrfs_dev_replace_unlock(dev_replace
);
4197 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
4198 !(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) &&
4199 dev_replace
->tgtdev
!= NULL
) {
4201 * in dev-replace case, for repair case (that's the only
4202 * case where the mirror is selected explicitly when
4203 * calling btrfs_map_block), blocks left of the left cursor
4204 * can also be read from the target drive.
4205 * For REQ_GET_READ_MIRRORS, the target drive is added as
4206 * the last one to the array of stripes. For READ, it also
4207 * needs to be supported using the same mirror number.
4208 * If the requested block is not left of the left cursor,
4209 * EIO is returned. This can happen because btrfs_num_copies()
4210 * returns one more in the dev-replace case.
4212 u64 tmp_length
= *length
;
4213 struct btrfs_bio
*tmp_bbio
= NULL
;
4214 int tmp_num_stripes
;
4215 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4216 int index_srcdev
= 0;
4218 u64 physical_of_found
= 0;
4220 ret
= __btrfs_map_block(fs_info
, REQ_GET_READ_MIRRORS
,
4221 logical
, &tmp_length
, &tmp_bbio
, 0);
4223 WARN_ON(tmp_bbio
!= NULL
);
4227 tmp_num_stripes
= tmp_bbio
->num_stripes
;
4228 if (mirror_num
> tmp_num_stripes
) {
4230 * REQ_GET_READ_MIRRORS does not contain this
4231 * mirror, that means that the requested area
4232 * is not left of the left cursor
4240 * process the rest of the function using the mirror_num
4241 * of the source drive. Therefore look it up first.
4242 * At the end, patch the device pointer to the one of the
4245 for (i
= 0; i
< tmp_num_stripes
; i
++) {
4246 if (tmp_bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4248 * In case of DUP, in order to keep it
4249 * simple, only add the mirror with the
4250 * lowest physical address
4253 physical_of_found
<=
4254 tmp_bbio
->stripes
[i
].physical
)
4259 tmp_bbio
->stripes
[i
].physical
;
4264 mirror_num
= index_srcdev
+ 1;
4265 patch_the_first_stripe_for_dev_replace
= 1;
4266 physical_to_patch_in_first_stripe
= physical_of_found
;
4275 } else if (mirror_num
> map
->num_stripes
) {
4281 stripe_nr_orig
= stripe_nr
;
4282 stripe_nr_end
= (offset
+ *length
+ map
->stripe_len
- 1) &
4283 (~(map
->stripe_len
- 1));
4284 do_div(stripe_nr_end
, map
->stripe_len
);
4285 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
4287 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4288 if (rw
& REQ_DISCARD
)
4289 num_stripes
= min_t(u64
, map
->num_stripes
,
4290 stripe_nr_end
- stripe_nr_orig
);
4291 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4292 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
4293 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
))
4294 num_stripes
= map
->num_stripes
;
4295 else if (mirror_num
)
4296 stripe_index
= mirror_num
- 1;
4298 stripe_index
= find_live_mirror(fs_info
, map
, 0,
4300 current
->pid
% map
->num_stripes
,
4301 dev_replace_is_ongoing
);
4302 mirror_num
= stripe_index
+ 1;
4305 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
4306 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) {
4307 num_stripes
= map
->num_stripes
;
4308 } else if (mirror_num
) {
4309 stripe_index
= mirror_num
- 1;
4314 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4315 int factor
= map
->num_stripes
/ map
->sub_stripes
;
4317 stripe_index
= do_div(stripe_nr
, factor
);
4318 stripe_index
*= map
->sub_stripes
;
4320 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
4321 num_stripes
= map
->sub_stripes
;
4322 else if (rw
& REQ_DISCARD
)
4323 num_stripes
= min_t(u64
, map
->sub_stripes
*
4324 (stripe_nr_end
- stripe_nr_orig
),
4326 else if (mirror_num
)
4327 stripe_index
+= mirror_num
- 1;
4329 int old_stripe_index
= stripe_index
;
4330 stripe_index
= find_live_mirror(fs_info
, map
,
4332 map
->sub_stripes
, stripe_index
+
4333 current
->pid
% map
->sub_stripes
,
4334 dev_replace_is_ongoing
);
4335 mirror_num
= stripe_index
- old_stripe_index
+ 1;
4339 * after this do_div call, stripe_nr is the number of stripes
4340 * on this device we have to walk to find the data, and
4341 * stripe_index is the number of our device in the stripe array
4343 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4344 mirror_num
= stripe_index
+ 1;
4346 BUG_ON(stripe_index
>= map
->num_stripes
);
4348 num_alloc_stripes
= num_stripes
;
4349 if (dev_replace_is_ongoing
) {
4350 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
4351 num_alloc_stripes
<<= 1;
4352 if (rw
& REQ_GET_READ_MIRRORS
)
4353 num_alloc_stripes
++;
4355 bbio
= kzalloc(btrfs_bio_size(num_alloc_stripes
), GFP_NOFS
);
4360 atomic_set(&bbio
->error
, 0);
4362 if (rw
& REQ_DISCARD
) {
4364 int sub_stripes
= 0;
4365 u64 stripes_per_dev
= 0;
4366 u32 remaining_stripes
= 0;
4367 u32 last_stripe
= 0;
4370 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
4371 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4374 sub_stripes
= map
->sub_stripes
;
4376 factor
= map
->num_stripes
/ sub_stripes
;
4377 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
4380 &remaining_stripes
);
4381 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
4382 last_stripe
*= sub_stripes
;
4385 for (i
= 0; i
< num_stripes
; i
++) {
4386 bbio
->stripes
[i
].physical
=
4387 map
->stripes
[stripe_index
].physical
+
4388 stripe_offset
+ stripe_nr
* map
->stripe_len
;
4389 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
4391 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
4392 BTRFS_BLOCK_GROUP_RAID10
)) {
4393 bbio
->stripes
[i
].length
= stripes_per_dev
*
4396 if (i
/ sub_stripes
< remaining_stripes
)
4397 bbio
->stripes
[i
].length
+=
4401 * Special for the first stripe and
4404 * |-------|...|-------|
4408 if (i
< sub_stripes
)
4409 bbio
->stripes
[i
].length
-=
4412 if (stripe_index
>= last_stripe
&&
4413 stripe_index
<= (last_stripe
+
4415 bbio
->stripes
[i
].length
-=
4418 if (i
== sub_stripes
- 1)
4421 bbio
->stripes
[i
].length
= *length
;
4424 if (stripe_index
== map
->num_stripes
) {
4425 /* This could only happen for RAID0/10 */
4431 for (i
= 0; i
< num_stripes
; i
++) {
4432 bbio
->stripes
[i
].physical
=
4433 map
->stripes
[stripe_index
].physical
+
4435 stripe_nr
* map
->stripe_len
;
4436 bbio
->stripes
[i
].dev
=
4437 map
->stripes
[stripe_index
].dev
;
4442 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
)) {
4443 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4444 BTRFS_BLOCK_GROUP_RAID10
|
4445 BTRFS_BLOCK_GROUP_DUP
)) {
4450 if (dev_replace_is_ongoing
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
4451 dev_replace
->tgtdev
!= NULL
) {
4452 int index_where_to_add
;
4453 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4456 * duplicate the write operations while the dev replace
4457 * procedure is running. Since the copying of the old disk
4458 * to the new disk takes place at run time while the
4459 * filesystem is mounted writable, the regular write
4460 * operations to the old disk have to be duplicated to go
4461 * to the new disk as well.
4462 * Note that device->missing is handled by the caller, and
4463 * that the write to the old disk is already set up in the
4466 index_where_to_add
= num_stripes
;
4467 for (i
= 0; i
< num_stripes
; i
++) {
4468 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4469 /* write to new disk, too */
4470 struct btrfs_bio_stripe
*new =
4471 bbio
->stripes
+ index_where_to_add
;
4472 struct btrfs_bio_stripe
*old
=
4475 new->physical
= old
->physical
;
4476 new->length
= old
->length
;
4477 new->dev
= dev_replace
->tgtdev
;
4478 index_where_to_add
++;
4482 num_stripes
= index_where_to_add
;
4483 } else if (dev_replace_is_ongoing
&& (rw
& REQ_GET_READ_MIRRORS
) &&
4484 dev_replace
->tgtdev
!= NULL
) {
4485 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4486 int index_srcdev
= 0;
4488 u64 physical_of_found
= 0;
4491 * During the dev-replace procedure, the target drive can
4492 * also be used to read data in case it is needed to repair
4493 * a corrupt block elsewhere. This is possible if the
4494 * requested area is left of the left cursor. In this area,
4495 * the target drive is a full copy of the source drive.
4497 for (i
= 0; i
< num_stripes
; i
++) {
4498 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4500 * In case of DUP, in order to keep it
4501 * simple, only add the mirror with the
4502 * lowest physical address
4505 physical_of_found
<=
4506 bbio
->stripes
[i
].physical
)
4510 physical_of_found
= bbio
->stripes
[i
].physical
;
4514 u64 length
= map
->stripe_len
;
4516 if (physical_of_found
+ length
<=
4517 dev_replace
->cursor_left
) {
4518 struct btrfs_bio_stripe
*tgtdev_stripe
=
4519 bbio
->stripes
+ num_stripes
;
4521 tgtdev_stripe
->physical
= physical_of_found
;
4522 tgtdev_stripe
->length
=
4523 bbio
->stripes
[index_srcdev
].length
;
4524 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
4532 bbio
->num_stripes
= num_stripes
;
4533 bbio
->max_errors
= max_errors
;
4534 bbio
->mirror_num
= mirror_num
;
4537 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4538 * mirror_num == num_stripes + 1 && dev_replace target drive is
4539 * available as a mirror
4541 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
4542 WARN_ON(num_stripes
> 1);
4543 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
4544 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
4545 bbio
->mirror_num
= map
->num_stripes
+ 1;
4548 if (dev_replace_is_ongoing
)
4549 btrfs_dev_replace_unlock(dev_replace
);
4550 free_extent_map(em
);
4554 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4555 u64 logical
, u64
*length
,
4556 struct btrfs_bio
**bbio_ret
, int mirror_num
)
4558 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
4562 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
4563 u64 chunk_start
, u64 physical
, u64 devid
,
4564 u64
**logical
, int *naddrs
, int *stripe_len
)
4566 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4567 struct extent_map
*em
;
4568 struct map_lookup
*map
;
4575 read_lock(&em_tree
->lock
);
4576 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
4577 read_unlock(&em_tree
->lock
);
4579 BUG_ON(!em
|| em
->start
!= chunk_start
);
4580 map
= (struct map_lookup
*)em
->bdev
;
4583 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4584 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
4585 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
4586 do_div(length
, map
->num_stripes
);
4588 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
4589 BUG_ON(!buf
); /* -ENOMEM */
4591 for (i
= 0; i
< map
->num_stripes
; i
++) {
4592 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
4594 if (map
->stripes
[i
].physical
> physical
||
4595 map
->stripes
[i
].physical
+ length
<= physical
)
4598 stripe_nr
= physical
- map
->stripes
[i
].physical
;
4599 do_div(stripe_nr
, map
->stripe_len
);
4601 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
4602 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4603 do_div(stripe_nr
, map
->sub_stripes
);
4604 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4605 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
4607 bytenr
= chunk_start
+ stripe_nr
* map
->stripe_len
;
4608 WARN_ON(nr
>= map
->num_stripes
);
4609 for (j
= 0; j
< nr
; j
++) {
4610 if (buf
[j
] == bytenr
)
4614 WARN_ON(nr
>= map
->num_stripes
);
4621 *stripe_len
= map
->stripe_len
;
4623 free_extent_map(em
);
4627 static void *merge_stripe_index_into_bio_private(void *bi_private
,
4628 unsigned int stripe_index
)
4631 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4633 * The alternative solution (instead of stealing bits from the
4634 * pointer) would be to allocate an intermediate structure
4635 * that contains the old private pointer plus the stripe_index.
4637 BUG_ON((((uintptr_t)bi_private
) & 3) != 0);
4638 BUG_ON(stripe_index
> 3);
4639 return (void *)(((uintptr_t)bi_private
) | stripe_index
);
4642 static struct btrfs_bio
*extract_bbio_from_bio_private(void *bi_private
)
4644 return (struct btrfs_bio
*)(((uintptr_t)bi_private
) & ~((uintptr_t)3));
4647 static unsigned int extract_stripe_index_from_bio_private(void *bi_private
)
4649 return (unsigned int)((uintptr_t)bi_private
) & 3;
4652 static void btrfs_end_bio(struct bio
*bio
, int err
)
4654 struct btrfs_bio
*bbio
= extract_bbio_from_bio_private(bio
->bi_private
);
4655 int is_orig_bio
= 0;
4658 atomic_inc(&bbio
->error
);
4659 if (err
== -EIO
|| err
== -EREMOTEIO
) {
4660 unsigned int stripe_index
=
4661 extract_stripe_index_from_bio_private(
4663 struct btrfs_device
*dev
;
4665 BUG_ON(stripe_index
>= bbio
->num_stripes
);
4666 dev
= bbio
->stripes
[stripe_index
].dev
;
4668 if (bio
->bi_rw
& WRITE
)
4669 btrfs_dev_stat_inc(dev
,
4670 BTRFS_DEV_STAT_WRITE_ERRS
);
4672 btrfs_dev_stat_inc(dev
,
4673 BTRFS_DEV_STAT_READ_ERRS
);
4674 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
4675 btrfs_dev_stat_inc(dev
,
4676 BTRFS_DEV_STAT_FLUSH_ERRS
);
4677 btrfs_dev_stat_print_on_error(dev
);
4682 if (bio
== bbio
->orig_bio
)
4685 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4688 bio
= bbio
->orig_bio
;
4690 bio
->bi_private
= bbio
->private;
4691 bio
->bi_end_io
= bbio
->end_io
;
4692 bio
->bi_bdev
= (struct block_device
*)
4693 (unsigned long)bbio
->mirror_num
;
4694 /* only send an error to the higher layers if it is
4695 * beyond the tolerance of the multi-bio
4697 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
4701 * this bio is actually up to date, we didn't
4702 * go over the max number of errors
4704 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
4709 bio_endio(bio
, err
);
4710 } else if (!is_orig_bio
) {
4715 struct async_sched
{
4718 struct btrfs_fs_info
*info
;
4719 struct btrfs_work work
;
4723 * see run_scheduled_bios for a description of why bios are collected for
4726 * This will add one bio to the pending list for a device and make sure
4727 * the work struct is scheduled.
4729 static noinline
void schedule_bio(struct btrfs_root
*root
,
4730 struct btrfs_device
*device
,
4731 int rw
, struct bio
*bio
)
4733 int should_queue
= 1;
4734 struct btrfs_pending_bios
*pending_bios
;
4736 /* don't bother with additional async steps for reads, right now */
4737 if (!(rw
& REQ_WRITE
)) {
4739 btrfsic_submit_bio(rw
, bio
);
4745 * nr_async_bios allows us to reliably return congestion to the
4746 * higher layers. Otherwise, the async bio makes it appear we have
4747 * made progress against dirty pages when we've really just put it
4748 * on a queue for later
4750 atomic_inc(&root
->fs_info
->nr_async_bios
);
4751 WARN_ON(bio
->bi_next
);
4752 bio
->bi_next
= NULL
;
4755 spin_lock(&device
->io_lock
);
4756 if (bio
->bi_rw
& REQ_SYNC
)
4757 pending_bios
= &device
->pending_sync_bios
;
4759 pending_bios
= &device
->pending_bios
;
4761 if (pending_bios
->tail
)
4762 pending_bios
->tail
->bi_next
= bio
;
4764 pending_bios
->tail
= bio
;
4765 if (!pending_bios
->head
)
4766 pending_bios
->head
= bio
;
4767 if (device
->running_pending
)
4770 spin_unlock(&device
->io_lock
);
4773 btrfs_queue_worker(&root
->fs_info
->submit_workers
,
4777 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
4780 struct bio_vec
*prev
;
4781 struct request_queue
*q
= bdev_get_queue(bdev
);
4782 unsigned short max_sectors
= queue_max_sectors(q
);
4783 struct bvec_merge_data bvm
= {
4785 .bi_sector
= sector
,
4786 .bi_rw
= bio
->bi_rw
,
4789 if (bio
->bi_vcnt
== 0) {
4794 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
4795 if ((bio
->bi_size
>> 9) > max_sectors
)
4798 if (!q
->merge_bvec_fn
)
4801 bvm
.bi_size
= bio
->bi_size
- prev
->bv_len
;
4802 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
4807 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
4808 struct bio
*bio
, u64 physical
, int dev_nr
,
4811 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
4813 bio
->bi_private
= bbio
;
4814 bio
->bi_private
= merge_stripe_index_into_bio_private(
4815 bio
->bi_private
, (unsigned int)dev_nr
);
4816 bio
->bi_end_io
= btrfs_end_bio
;
4817 bio
->bi_sector
= physical
>> 9;
4820 struct rcu_string
*name
;
4823 name
= rcu_dereference(dev
->name
);
4824 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
4825 "(%s id %llu), size=%u\n", rw
,
4826 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
4827 name
->str
, dev
->devid
, bio
->bi_size
);
4831 bio
->bi_bdev
= dev
->bdev
;
4833 schedule_bio(root
, dev
, rw
, bio
);
4835 btrfsic_submit_bio(rw
, bio
);
4838 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
4839 struct bio
*first_bio
, struct btrfs_device
*dev
,
4840 int dev_nr
, int rw
, int async
)
4842 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
4844 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
4845 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
4848 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
4852 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
4853 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
4854 bvec
->bv_offset
) < bvec
->bv_len
) {
4855 u64 len
= bio
->bi_size
;
4857 atomic_inc(&bbio
->stripes_pending
);
4858 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
4866 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
4870 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
4872 atomic_inc(&bbio
->error
);
4873 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
4874 bio
->bi_private
= bbio
->private;
4875 bio
->bi_end_io
= bbio
->end_io
;
4876 bio
->bi_bdev
= (struct block_device
*)
4877 (unsigned long)bbio
->mirror_num
;
4878 bio
->bi_sector
= logical
>> 9;
4880 bio_endio(bio
, -EIO
);
4884 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
4885 int mirror_num
, int async_submit
)
4887 struct btrfs_device
*dev
;
4888 struct bio
*first_bio
= bio
;
4889 u64 logical
= (u64
)bio
->bi_sector
<< 9;
4895 struct btrfs_bio
*bbio
= NULL
;
4897 length
= bio
->bi_size
;
4898 map_length
= length
;
4900 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
4905 total_devs
= bbio
->num_stripes
;
4906 if (map_length
< length
) {
4907 printk(KERN_CRIT
"btrfs: mapping failed logical %llu bio len %llu "
4908 "len %llu\n", (unsigned long long)logical
,
4909 (unsigned long long)length
,
4910 (unsigned long long)map_length
);
4914 bbio
->orig_bio
= first_bio
;
4915 bbio
->private = first_bio
->bi_private
;
4916 bbio
->end_io
= first_bio
->bi_end_io
;
4917 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
4919 while (dev_nr
< total_devs
) {
4920 dev
= bbio
->stripes
[dev_nr
].dev
;
4921 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
4922 bbio_error(bbio
, first_bio
, logical
);
4928 * Check and see if we're ok with this bio based on it's size
4929 * and offset with the given device.
4931 if (!bio_size_ok(dev
->bdev
, first_bio
,
4932 bbio
->stripes
[dev_nr
].physical
>> 9)) {
4933 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
4934 dev_nr
, rw
, async_submit
);
4940 if (dev_nr
< total_devs
- 1) {
4941 bio
= bio_clone(first_bio
, GFP_NOFS
);
4942 BUG_ON(!bio
); /* -ENOMEM */
4947 submit_stripe_bio(root
, bbio
, bio
,
4948 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
4955 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
4958 struct btrfs_device
*device
;
4959 struct btrfs_fs_devices
*cur_devices
;
4961 cur_devices
= fs_info
->fs_devices
;
4962 while (cur_devices
) {
4964 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
4965 device
= __find_device(&cur_devices
->devices
,
4970 cur_devices
= cur_devices
->seed
;
4975 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
4976 u64 devid
, u8
*dev_uuid
)
4978 struct btrfs_device
*device
;
4979 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
4981 device
= kzalloc(sizeof(*device
), GFP_NOFS
);
4984 list_add(&device
->dev_list
,
4985 &fs_devices
->devices
);
4986 device
->dev_root
= root
->fs_info
->dev_root
;
4987 device
->devid
= devid
;
4988 device
->work
.func
= pending_bios_fn
;
4989 device
->fs_devices
= fs_devices
;
4990 device
->missing
= 1;
4991 fs_devices
->num_devices
++;
4992 fs_devices
->missing_devices
++;
4993 spin_lock_init(&device
->io_lock
);
4994 INIT_LIST_HEAD(&device
->dev_alloc_list
);
4995 memcpy(device
->uuid
, dev_uuid
, BTRFS_UUID_SIZE
);
4999 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
5000 struct extent_buffer
*leaf
,
5001 struct btrfs_chunk
*chunk
)
5003 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5004 struct map_lookup
*map
;
5005 struct extent_map
*em
;
5009 u8 uuid
[BTRFS_UUID_SIZE
];
5014 logical
= key
->offset
;
5015 length
= btrfs_chunk_length(leaf
, chunk
);
5017 read_lock(&map_tree
->map_tree
.lock
);
5018 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
5019 read_unlock(&map_tree
->map_tree
.lock
);
5021 /* already mapped? */
5022 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
5023 free_extent_map(em
);
5026 free_extent_map(em
);
5029 em
= alloc_extent_map();
5032 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
5033 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
5035 free_extent_map(em
);
5039 em
->bdev
= (struct block_device
*)map
;
5040 em
->start
= logical
;
5043 em
->block_start
= 0;
5044 em
->block_len
= em
->len
;
5046 map
->num_stripes
= num_stripes
;
5047 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
5048 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
5049 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
5050 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
5051 map
->type
= btrfs_chunk_type(leaf
, chunk
);
5052 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
5053 for (i
= 0; i
< num_stripes
; i
++) {
5054 map
->stripes
[i
].physical
=
5055 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
5056 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
5057 read_extent_buffer(leaf
, uuid
, (unsigned long)
5058 btrfs_stripe_dev_uuid_nr(chunk
, i
),
5060 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
5062 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
5064 free_extent_map(em
);
5067 if (!map
->stripes
[i
].dev
) {
5068 map
->stripes
[i
].dev
=
5069 add_missing_dev(root
, devid
, uuid
);
5070 if (!map
->stripes
[i
].dev
) {
5072 free_extent_map(em
);
5076 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
5079 write_lock(&map_tree
->map_tree
.lock
);
5080 ret
= add_extent_mapping(&map_tree
->map_tree
, em
);
5081 write_unlock(&map_tree
->map_tree
.lock
);
5082 BUG_ON(ret
); /* Tree corruption */
5083 free_extent_map(em
);
5088 static void fill_device_from_item(struct extent_buffer
*leaf
,
5089 struct btrfs_dev_item
*dev_item
,
5090 struct btrfs_device
*device
)
5094 device
->devid
= btrfs_device_id(leaf
, dev_item
);
5095 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
5096 device
->total_bytes
= device
->disk_total_bytes
;
5097 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
5098 device
->type
= btrfs_device_type(leaf
, dev_item
);
5099 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
5100 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
5101 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
5102 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
5103 device
->is_tgtdev_for_dev_replace
= 0;
5105 ptr
= (unsigned long)btrfs_device_uuid(dev_item
);
5106 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
5109 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
5111 struct btrfs_fs_devices
*fs_devices
;
5114 BUG_ON(!mutex_is_locked(&uuid_mutex
));
5116 fs_devices
= root
->fs_info
->fs_devices
->seed
;
5117 while (fs_devices
) {
5118 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5122 fs_devices
= fs_devices
->seed
;
5125 fs_devices
= find_fsid(fsid
);
5131 fs_devices
= clone_fs_devices(fs_devices
);
5132 if (IS_ERR(fs_devices
)) {
5133 ret
= PTR_ERR(fs_devices
);
5137 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
5138 root
->fs_info
->bdev_holder
);
5140 free_fs_devices(fs_devices
);
5144 if (!fs_devices
->seeding
) {
5145 __btrfs_close_devices(fs_devices
);
5146 free_fs_devices(fs_devices
);
5151 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
5152 root
->fs_info
->fs_devices
->seed
= fs_devices
;
5157 static int read_one_dev(struct btrfs_root
*root
,
5158 struct extent_buffer
*leaf
,
5159 struct btrfs_dev_item
*dev_item
)
5161 struct btrfs_device
*device
;
5164 u8 fs_uuid
[BTRFS_UUID_SIZE
];
5165 u8 dev_uuid
[BTRFS_UUID_SIZE
];
5167 devid
= btrfs_device_id(leaf
, dev_item
);
5168 read_extent_buffer(leaf
, dev_uuid
,
5169 (unsigned long)btrfs_device_uuid(dev_item
),
5171 read_extent_buffer(leaf
, fs_uuid
,
5172 (unsigned long)btrfs_device_fsid(dev_item
),
5175 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
5176 ret
= open_seed_devices(root
, fs_uuid
);
5177 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
5181 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
5182 if (!device
|| !device
->bdev
) {
5183 if (!btrfs_test_opt(root
, DEGRADED
))
5187 printk(KERN_WARNING
"warning devid %llu missing\n",
5188 (unsigned long long)devid
);
5189 device
= add_missing_dev(root
, devid
, dev_uuid
);
5192 } else if (!device
->missing
) {
5194 * this happens when a device that was properly setup
5195 * in the device info lists suddenly goes bad.
5196 * device->bdev is NULL, and so we have to set
5197 * device->missing to one here
5199 root
->fs_info
->fs_devices
->missing_devices
++;
5200 device
->missing
= 1;
5204 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
5205 BUG_ON(device
->writeable
);
5206 if (device
->generation
!=
5207 btrfs_device_generation(leaf
, dev_item
))
5211 fill_device_from_item(leaf
, dev_item
, device
);
5212 device
->dev_root
= root
->fs_info
->dev_root
;
5213 device
->in_fs_metadata
= 1;
5214 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
5215 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
5216 spin_lock(&root
->fs_info
->free_chunk_lock
);
5217 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
5219 spin_unlock(&root
->fs_info
->free_chunk_lock
);
5225 int btrfs_read_sys_array(struct btrfs_root
*root
)
5227 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
5228 struct extent_buffer
*sb
;
5229 struct btrfs_disk_key
*disk_key
;
5230 struct btrfs_chunk
*chunk
;
5232 unsigned long sb_ptr
;
5238 struct btrfs_key key
;
5240 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
5241 BTRFS_SUPER_INFO_SIZE
);
5244 btrfs_set_buffer_uptodate(sb
);
5245 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
5247 * The sb extent buffer is artifical and just used to read the system array.
5248 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5249 * pages up-to-date when the page is larger: extent does not cover the
5250 * whole page and consequently check_page_uptodate does not find all
5251 * the page's extents up-to-date (the hole beyond sb),
5252 * write_extent_buffer then triggers a WARN_ON.
5254 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5255 * but sb spans only this function. Add an explicit SetPageUptodate call
5256 * to silence the warning eg. on PowerPC 64.
5258 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
5259 SetPageUptodate(sb
->pages
[0]);
5261 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
5262 array_size
= btrfs_super_sys_array_size(super_copy
);
5264 ptr
= super_copy
->sys_chunk_array
;
5265 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
5268 while (cur
< array_size
) {
5269 disk_key
= (struct btrfs_disk_key
*)ptr
;
5270 btrfs_disk_key_to_cpu(&key
, disk_key
);
5272 len
= sizeof(*disk_key
); ptr
+= len
;
5276 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
5277 chunk
= (struct btrfs_chunk
*)sb_ptr
;
5278 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
5281 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
5282 len
= btrfs_chunk_item_size(num_stripes
);
5291 free_extent_buffer(sb
);
5295 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
5297 struct btrfs_path
*path
;
5298 struct extent_buffer
*leaf
;
5299 struct btrfs_key key
;
5300 struct btrfs_key found_key
;
5304 root
= root
->fs_info
->chunk_root
;
5306 path
= btrfs_alloc_path();
5310 mutex_lock(&uuid_mutex
);
5313 /* first we search for all of the device items, and then we
5314 * read in all of the chunk items. This way we can create chunk
5315 * mappings that reference all of the devices that are afound
5317 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
5321 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5325 leaf
= path
->nodes
[0];
5326 slot
= path
->slots
[0];
5327 if (slot
>= btrfs_header_nritems(leaf
)) {
5328 ret
= btrfs_next_leaf(root
, path
);
5335 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5336 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5337 if (found_key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
)
5339 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
5340 struct btrfs_dev_item
*dev_item
;
5341 dev_item
= btrfs_item_ptr(leaf
, slot
,
5342 struct btrfs_dev_item
);
5343 ret
= read_one_dev(root
, leaf
, dev_item
);
5347 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
5348 struct btrfs_chunk
*chunk
;
5349 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
5350 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
5356 if (key
.objectid
== BTRFS_DEV_ITEMS_OBJECTID
) {
5358 btrfs_release_path(path
);
5363 unlock_chunks(root
);
5364 mutex_unlock(&uuid_mutex
);
5366 btrfs_free_path(path
);
5370 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
5374 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5375 btrfs_dev_stat_reset(dev
, i
);
5378 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
5380 struct btrfs_key key
;
5381 struct btrfs_key found_key
;
5382 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5383 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5384 struct extent_buffer
*eb
;
5387 struct btrfs_device
*device
;
5388 struct btrfs_path
*path
= NULL
;
5391 path
= btrfs_alloc_path();
5397 mutex_lock(&fs_devices
->device_list_mutex
);
5398 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5400 struct btrfs_dev_stats_item
*ptr
;
5403 key
.type
= BTRFS_DEV_STATS_KEY
;
5404 key
.offset
= device
->devid
;
5405 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
5407 __btrfs_reset_dev_stats(device
);
5408 device
->dev_stats_valid
= 1;
5409 btrfs_release_path(path
);
5412 slot
= path
->slots
[0];
5413 eb
= path
->nodes
[0];
5414 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5415 item_size
= btrfs_item_size_nr(eb
, slot
);
5417 ptr
= btrfs_item_ptr(eb
, slot
,
5418 struct btrfs_dev_stats_item
);
5420 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5421 if (item_size
>= (1 + i
) * sizeof(__le64
))
5422 btrfs_dev_stat_set(device
, i
,
5423 btrfs_dev_stats_value(eb
, ptr
, i
));
5425 btrfs_dev_stat_reset(device
, i
);
5428 device
->dev_stats_valid
= 1;
5429 btrfs_dev_stat_print_on_load(device
);
5430 btrfs_release_path(path
);
5432 mutex_unlock(&fs_devices
->device_list_mutex
);
5435 btrfs_free_path(path
);
5436 return ret
< 0 ? ret
: 0;
5439 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
5440 struct btrfs_root
*dev_root
,
5441 struct btrfs_device
*device
)
5443 struct btrfs_path
*path
;
5444 struct btrfs_key key
;
5445 struct extent_buffer
*eb
;
5446 struct btrfs_dev_stats_item
*ptr
;
5451 key
.type
= BTRFS_DEV_STATS_KEY
;
5452 key
.offset
= device
->devid
;
5454 path
= btrfs_alloc_path();
5456 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
5458 printk_in_rcu(KERN_WARNING
"btrfs: error %d while searching for dev_stats item for device %s!\n",
5459 ret
, rcu_str_deref(device
->name
));
5464 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
5465 /* need to delete old one and insert a new one */
5466 ret
= btrfs_del_item(trans
, dev_root
, path
);
5468 printk_in_rcu(KERN_WARNING
"btrfs: delete too small dev_stats item for device %s failed %d!\n",
5469 rcu_str_deref(device
->name
), ret
);
5476 /* need to insert a new item */
5477 btrfs_release_path(path
);
5478 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
5479 &key
, sizeof(*ptr
));
5481 printk_in_rcu(KERN_WARNING
"btrfs: insert dev_stats item for device %s failed %d!\n",
5482 rcu_str_deref(device
->name
), ret
);
5487 eb
= path
->nodes
[0];
5488 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
5489 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5490 btrfs_set_dev_stats_value(eb
, ptr
, i
,
5491 btrfs_dev_stat_read(device
, i
));
5492 btrfs_mark_buffer_dirty(eb
);
5495 btrfs_free_path(path
);
5500 * called from commit_transaction. Writes all changed device stats to disk.
5502 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
5503 struct btrfs_fs_info
*fs_info
)
5505 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
5506 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
5507 struct btrfs_device
*device
;
5510 mutex_lock(&fs_devices
->device_list_mutex
);
5511 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
5512 if (!device
->dev_stats_valid
|| !device
->dev_stats_dirty
)
5515 ret
= update_dev_stat_item(trans
, dev_root
, device
);
5517 device
->dev_stats_dirty
= 0;
5519 mutex_unlock(&fs_devices
->device_list_mutex
);
5524 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
5526 btrfs_dev_stat_inc(dev
, index
);
5527 btrfs_dev_stat_print_on_error(dev
);
5530 void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
5532 if (!dev
->dev_stats_valid
)
5534 printk_ratelimited_in_rcu(KERN_ERR
5535 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5536 rcu_str_deref(dev
->name
),
5537 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5538 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5539 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5540 btrfs_dev_stat_read(dev
,
5541 BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5542 btrfs_dev_stat_read(dev
,
5543 BTRFS_DEV_STAT_GENERATION_ERRS
));
5546 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
5550 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5551 if (btrfs_dev_stat_read(dev
, i
) != 0)
5553 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
5554 return; /* all values == 0, suppress message */
5556 printk_in_rcu(KERN_INFO
"btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5557 rcu_str_deref(dev
->name
),
5558 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
5559 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
5560 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
5561 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
5562 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
5565 int btrfs_get_dev_stats(struct btrfs_root
*root
,
5566 struct btrfs_ioctl_get_dev_stats
*stats
)
5568 struct btrfs_device
*dev
;
5569 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5572 mutex_lock(&fs_devices
->device_list_mutex
);
5573 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
5574 mutex_unlock(&fs_devices
->device_list_mutex
);
5578 "btrfs: get dev_stats failed, device not found\n");
5580 } else if (!dev
->dev_stats_valid
) {
5582 "btrfs: get dev_stats failed, not yet valid\n");
5584 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
5585 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
5586 if (stats
->nr_items
> i
)
5588 btrfs_dev_stat_read_and_reset(dev
, i
);
5590 btrfs_dev_stat_reset(dev
, i
);
5593 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
5594 if (stats
->nr_items
> i
)
5595 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
5597 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
5598 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
5602 int btrfs_scratch_superblock(struct btrfs_device
*device
)
5604 struct buffer_head
*bh
;
5605 struct btrfs_super_block
*disk_super
;
5607 bh
= btrfs_read_dev_super(device
->bdev
);
5610 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
5612 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
5613 set_buffer_dirty(bh
);
5614 sync_dirty_buffer(bh
);