2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <linux/ratelimit.h>
27 #include <linux/kthread.h>
28 #include <linux/raid/pq.h>
29 #include <linux/semaphore.h>
30 #include <asm/div64.h>
32 #include "extent_map.h"
34 #include "transaction.h"
35 #include "print-tree.h"
38 #include "async-thread.h"
39 #include "check-integrity.h"
40 #include "rcu-string.h"
42 #include "dev-replace.h"
45 static int init_first_rw_device(struct btrfs_trans_handle
*trans
,
46 struct btrfs_root
*root
,
47 struct btrfs_device
*device
);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
);
49 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
);
50 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
);
51 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
53 static DEFINE_MUTEX(uuid_mutex
);
54 static LIST_HEAD(fs_uuids
);
56 static void lock_chunks(struct btrfs_root
*root
)
58 mutex_lock(&root
->fs_info
->chunk_mutex
);
61 static void unlock_chunks(struct btrfs_root
*root
)
63 mutex_unlock(&root
->fs_info
->chunk_mutex
);
66 static struct btrfs_fs_devices
*__alloc_fs_devices(void)
68 struct btrfs_fs_devices
*fs_devs
;
70 fs_devs
= kzalloc(sizeof(*fs_devs
), GFP_NOFS
);
72 return ERR_PTR(-ENOMEM
);
74 mutex_init(&fs_devs
->device_list_mutex
);
76 INIT_LIST_HEAD(&fs_devs
->devices
);
77 INIT_LIST_HEAD(&fs_devs
->alloc_list
);
78 INIT_LIST_HEAD(&fs_devs
->list
);
84 * alloc_fs_devices - allocate struct btrfs_fs_devices
85 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
88 * Return: a pointer to a new &struct btrfs_fs_devices on success;
89 * ERR_PTR() on error. Returned struct is not linked onto any lists and
90 * can be destroyed with kfree() right away.
92 static struct btrfs_fs_devices
*alloc_fs_devices(const u8
*fsid
)
94 struct btrfs_fs_devices
*fs_devs
;
96 fs_devs
= __alloc_fs_devices();
101 memcpy(fs_devs
->fsid
, fsid
, BTRFS_FSID_SIZE
);
103 generate_random_uuid(fs_devs
->fsid
);
108 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
110 struct btrfs_device
*device
;
111 WARN_ON(fs_devices
->opened
);
112 while (!list_empty(&fs_devices
->devices
)) {
113 device
= list_entry(fs_devices
->devices
.next
,
114 struct btrfs_device
, dev_list
);
115 list_del(&device
->dev_list
);
116 rcu_string_free(device
->name
);
122 static void btrfs_kobject_uevent(struct block_device
*bdev
,
123 enum kobject_action action
)
127 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
129 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
131 kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
132 &disk_to_dev(bdev
->bd_disk
)->kobj
);
135 void btrfs_cleanup_fs_uuids(void)
137 struct btrfs_fs_devices
*fs_devices
;
139 while (!list_empty(&fs_uuids
)) {
140 fs_devices
= list_entry(fs_uuids
.next
,
141 struct btrfs_fs_devices
, list
);
142 list_del(&fs_devices
->list
);
143 free_fs_devices(fs_devices
);
147 static struct btrfs_device
*__alloc_device(void)
149 struct btrfs_device
*dev
;
151 dev
= kzalloc(sizeof(*dev
), GFP_NOFS
);
153 return ERR_PTR(-ENOMEM
);
155 INIT_LIST_HEAD(&dev
->dev_list
);
156 INIT_LIST_HEAD(&dev
->dev_alloc_list
);
158 spin_lock_init(&dev
->io_lock
);
160 spin_lock_init(&dev
->reada_lock
);
161 atomic_set(&dev
->reada_in_flight
, 0);
162 INIT_RADIX_TREE(&dev
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
163 INIT_RADIX_TREE(&dev
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
168 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
171 struct btrfs_device
*dev
;
173 list_for_each_entry(dev
, head
, dev_list
) {
174 if (dev
->devid
== devid
&&
175 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
182 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
184 struct btrfs_fs_devices
*fs_devices
;
186 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
187 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
194 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
195 int flush
, struct block_device
**bdev
,
196 struct buffer_head
**bh
)
200 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
203 ret
= PTR_ERR(*bdev
);
204 printk(KERN_INFO
"BTRFS: open %s failed\n", device_path
);
209 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
210 ret
= set_blocksize(*bdev
, 4096);
212 blkdev_put(*bdev
, flags
);
215 invalidate_bdev(*bdev
);
216 *bh
= btrfs_read_dev_super(*bdev
);
219 blkdev_put(*bdev
, flags
);
231 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
232 struct bio
*head
, struct bio
*tail
)
235 struct bio
*old_head
;
237 old_head
= pending_bios
->head
;
238 pending_bios
->head
= head
;
239 if (pending_bios
->tail
)
240 tail
->bi_next
= old_head
;
242 pending_bios
->tail
= tail
;
246 * we try to collect pending bios for a device so we don't get a large
247 * number of procs sending bios down to the same device. This greatly
248 * improves the schedulers ability to collect and merge the bios.
250 * But, it also turns into a long list of bios to process and that is sure
251 * to eventually make the worker thread block. The solution here is to
252 * make some progress and then put this work struct back at the end of
253 * the list if the block device is congested. This way, multiple devices
254 * can make progress from a single worker thread.
256 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
259 struct backing_dev_info
*bdi
;
260 struct btrfs_fs_info
*fs_info
;
261 struct btrfs_pending_bios
*pending_bios
;
265 unsigned long num_run
;
266 unsigned long batch_run
= 0;
268 unsigned long last_waited
= 0;
270 int sync_pending
= 0;
271 struct blk_plug plug
;
274 * this function runs all the bios we've collected for
275 * a particular device. We don't want to wander off to
276 * another device without first sending all of these down.
277 * So, setup a plug here and finish it off before we return
279 blk_start_plug(&plug
);
281 bdi
= blk_get_backing_dev_info(device
->bdev
);
282 fs_info
= device
->dev_root
->fs_info
;
283 limit
= btrfs_async_submit_limit(fs_info
);
284 limit
= limit
* 2 / 3;
287 spin_lock(&device
->io_lock
);
292 /* take all the bios off the list at once and process them
293 * later on (without the lock held). But, remember the
294 * tail and other pointers so the bios can be properly reinserted
295 * into the list if we hit congestion
297 if (!force_reg
&& device
->pending_sync_bios
.head
) {
298 pending_bios
= &device
->pending_sync_bios
;
301 pending_bios
= &device
->pending_bios
;
305 pending
= pending_bios
->head
;
306 tail
= pending_bios
->tail
;
307 WARN_ON(pending
&& !tail
);
310 * if pending was null this time around, no bios need processing
311 * at all and we can stop. Otherwise it'll loop back up again
312 * and do an additional check so no bios are missed.
314 * device->running_pending is used to synchronize with the
317 if (device
->pending_sync_bios
.head
== NULL
&&
318 device
->pending_bios
.head
== NULL
) {
320 device
->running_pending
= 0;
323 device
->running_pending
= 1;
326 pending_bios
->head
= NULL
;
327 pending_bios
->tail
= NULL
;
329 spin_unlock(&device
->io_lock
);
334 /* we want to work on both lists, but do more bios on the
335 * sync list than the regular list
338 pending_bios
!= &device
->pending_sync_bios
&&
339 device
->pending_sync_bios
.head
) ||
340 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
341 device
->pending_bios
.head
)) {
342 spin_lock(&device
->io_lock
);
343 requeue_list(pending_bios
, pending
, tail
);
348 pending
= pending
->bi_next
;
351 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
352 waitqueue_active(&fs_info
->async_submit_wait
))
353 wake_up(&fs_info
->async_submit_wait
);
355 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
358 * if we're doing the sync list, record that our
359 * plug has some sync requests on it
361 * If we're doing the regular list and there are
362 * sync requests sitting around, unplug before
365 if (pending_bios
== &device
->pending_sync_bios
) {
367 } else if (sync_pending
) {
368 blk_finish_plug(&plug
);
369 blk_start_plug(&plug
);
373 btrfsic_submit_bio(cur
->bi_rw
, cur
);
380 * we made progress, there is more work to do and the bdi
381 * is now congested. Back off and let other work structs
384 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
385 fs_info
->fs_devices
->open_devices
> 1) {
386 struct io_context
*ioc
;
388 ioc
= current
->io_context
;
391 * the main goal here is that we don't want to
392 * block if we're going to be able to submit
393 * more requests without blocking.
395 * This code does two great things, it pokes into
396 * the elevator code from a filesystem _and_
397 * it makes assumptions about how batching works.
399 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
400 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
402 ioc
->last_waited
== last_waited
)) {
404 * we want to go through our batch of
405 * requests and stop. So, we copy out
406 * the ioc->last_waited time and test
407 * against it before looping
409 last_waited
= ioc
->last_waited
;
414 spin_lock(&device
->io_lock
);
415 requeue_list(pending_bios
, pending
, tail
);
416 device
->running_pending
= 1;
418 spin_unlock(&device
->io_lock
);
419 btrfs_queue_work(fs_info
->submit_workers
,
423 /* unplug every 64 requests just for good measure */
424 if (batch_run
% 64 == 0) {
425 blk_finish_plug(&plug
);
426 blk_start_plug(&plug
);
435 spin_lock(&device
->io_lock
);
436 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
438 spin_unlock(&device
->io_lock
);
441 blk_finish_plug(&plug
);
444 static void pending_bios_fn(struct btrfs_work
*work
)
446 struct btrfs_device
*device
;
448 device
= container_of(work
, struct btrfs_device
, work
);
449 run_scheduled_bios(device
);
453 * Add new device to list of registered devices
456 * 1 - first time device is seen
457 * 0 - device already known
460 static noinline
int device_list_add(const char *path
,
461 struct btrfs_super_block
*disk_super
,
462 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
464 struct btrfs_device
*device
;
465 struct btrfs_fs_devices
*fs_devices
;
466 struct rcu_string
*name
;
468 u64 found_transid
= btrfs_super_generation(disk_super
);
470 fs_devices
= find_fsid(disk_super
->fsid
);
472 fs_devices
= alloc_fs_devices(disk_super
->fsid
);
473 if (IS_ERR(fs_devices
))
474 return PTR_ERR(fs_devices
);
476 list_add(&fs_devices
->list
, &fs_uuids
);
477 fs_devices
->latest_devid
= devid
;
478 fs_devices
->latest_trans
= found_transid
;
482 device
= __find_device(&fs_devices
->devices
, devid
,
483 disk_super
->dev_item
.uuid
);
486 if (fs_devices
->opened
)
489 device
= btrfs_alloc_device(NULL
, &devid
,
490 disk_super
->dev_item
.uuid
);
491 if (IS_ERR(device
)) {
492 /* we can safely leave the fs_devices entry around */
493 return PTR_ERR(device
);
496 name
= rcu_string_strdup(path
, GFP_NOFS
);
501 rcu_assign_pointer(device
->name
, name
);
503 mutex_lock(&fs_devices
->device_list_mutex
);
504 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
505 fs_devices
->num_devices
++;
506 mutex_unlock(&fs_devices
->device_list_mutex
);
509 device
->fs_devices
= fs_devices
;
510 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
512 * When FS is already mounted.
513 * 1. If you are here and if the device->name is NULL that
514 * means this device was missing at time of FS mount.
515 * 2. If you are here and if the device->name is different
516 * from 'path' that means either
517 * a. The same device disappeared and reappeared with
519 * b. The missing-disk-which-was-replaced, has
522 * We must allow 1 and 2a above. But 2b would be a spurious
525 * Further in case of 1 and 2a above, the disk at 'path'
526 * would have missed some transaction when it was away and
527 * in case of 2a the stale bdev has to be updated as well.
528 * 2b must not be allowed at all time.
532 * As of now don't allow update to btrfs_fs_device through
533 * the btrfs dev scan cli, after FS has been mounted.
535 if (fs_devices
->opened
)
538 name
= rcu_string_strdup(path
, GFP_NOFS
);
541 rcu_string_free(device
->name
);
542 rcu_assign_pointer(device
->name
, name
);
543 if (device
->missing
) {
544 fs_devices
->missing_devices
--;
549 if (found_transid
> fs_devices
->latest_trans
) {
550 fs_devices
->latest_devid
= devid
;
551 fs_devices
->latest_trans
= found_transid
;
553 *fs_devices_ret
= fs_devices
;
558 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
560 struct btrfs_fs_devices
*fs_devices
;
561 struct btrfs_device
*device
;
562 struct btrfs_device
*orig_dev
;
564 fs_devices
= alloc_fs_devices(orig
->fsid
);
565 if (IS_ERR(fs_devices
))
568 fs_devices
->latest_devid
= orig
->latest_devid
;
569 fs_devices
->latest_trans
= orig
->latest_trans
;
570 fs_devices
->total_devices
= orig
->total_devices
;
572 /* We have held the volume lock, it is safe to get the devices. */
573 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
574 struct rcu_string
*name
;
576 device
= btrfs_alloc_device(NULL
, &orig_dev
->devid
,
582 * This is ok to do without rcu read locked because we hold the
583 * uuid mutex so nothing we touch in here is going to disappear.
585 if (orig_dev
->name
) {
586 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
591 rcu_assign_pointer(device
->name
, name
);
594 list_add(&device
->dev_list
, &fs_devices
->devices
);
595 device
->fs_devices
= fs_devices
;
596 fs_devices
->num_devices
++;
600 free_fs_devices(fs_devices
);
601 return ERR_PTR(-ENOMEM
);
604 void btrfs_close_extra_devices(struct btrfs_fs_info
*fs_info
,
605 struct btrfs_fs_devices
*fs_devices
, int step
)
607 struct btrfs_device
*device
, *next
;
609 struct block_device
*latest_bdev
= NULL
;
610 u64 latest_devid
= 0;
611 u64 latest_transid
= 0;
613 mutex_lock(&uuid_mutex
);
615 /* This is the initialized path, it is safe to release the devices. */
616 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
617 if (device
->in_fs_metadata
) {
618 if (!device
->is_tgtdev_for_dev_replace
&&
620 device
->generation
> latest_transid
)) {
621 latest_devid
= device
->devid
;
622 latest_transid
= device
->generation
;
623 latest_bdev
= device
->bdev
;
628 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
630 * In the first step, keep the device which has
631 * the correct fsid and the devid that is used
632 * for the dev_replace procedure.
633 * In the second step, the dev_replace state is
634 * read from the device tree and it is known
635 * whether the procedure is really active or
636 * not, which means whether this device is
637 * used or whether it should be removed.
639 if (step
== 0 || device
->is_tgtdev_for_dev_replace
) {
644 blkdev_put(device
->bdev
, device
->mode
);
646 fs_devices
->open_devices
--;
648 if (device
->writeable
) {
649 list_del_init(&device
->dev_alloc_list
);
650 device
->writeable
= 0;
651 if (!device
->is_tgtdev_for_dev_replace
)
652 fs_devices
->rw_devices
--;
654 list_del_init(&device
->dev_list
);
655 fs_devices
->num_devices
--;
656 rcu_string_free(device
->name
);
660 if (fs_devices
->seed
) {
661 fs_devices
= fs_devices
->seed
;
665 fs_devices
->latest_bdev
= latest_bdev
;
666 fs_devices
->latest_devid
= latest_devid
;
667 fs_devices
->latest_trans
= latest_transid
;
669 mutex_unlock(&uuid_mutex
);
672 static void __free_device(struct work_struct
*work
)
674 struct btrfs_device
*device
;
676 device
= container_of(work
, struct btrfs_device
, rcu_work
);
679 blkdev_put(device
->bdev
, device
->mode
);
681 rcu_string_free(device
->name
);
685 static void free_device(struct rcu_head
*head
)
687 struct btrfs_device
*device
;
689 device
= container_of(head
, struct btrfs_device
, rcu
);
691 INIT_WORK(&device
->rcu_work
, __free_device
);
692 schedule_work(&device
->rcu_work
);
695 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
697 struct btrfs_device
*device
;
699 if (--fs_devices
->opened
> 0)
702 mutex_lock(&fs_devices
->device_list_mutex
);
703 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
704 struct btrfs_device
*new_device
;
705 struct rcu_string
*name
;
708 fs_devices
->open_devices
--;
710 if (device
->writeable
&&
711 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
712 list_del_init(&device
->dev_alloc_list
);
713 fs_devices
->rw_devices
--;
716 if (device
->can_discard
)
717 fs_devices
->num_can_discard
--;
719 fs_devices
->missing_devices
--;
721 new_device
= btrfs_alloc_device(NULL
, &device
->devid
,
723 BUG_ON(IS_ERR(new_device
)); /* -ENOMEM */
725 /* Safe because we are under uuid_mutex */
727 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
728 BUG_ON(!name
); /* -ENOMEM */
729 rcu_assign_pointer(new_device
->name
, name
);
732 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
733 new_device
->fs_devices
= device
->fs_devices
;
735 call_rcu(&device
->rcu
, free_device
);
737 mutex_unlock(&fs_devices
->device_list_mutex
);
739 WARN_ON(fs_devices
->open_devices
);
740 WARN_ON(fs_devices
->rw_devices
);
741 fs_devices
->opened
= 0;
742 fs_devices
->seeding
= 0;
747 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
749 struct btrfs_fs_devices
*seed_devices
= NULL
;
752 mutex_lock(&uuid_mutex
);
753 ret
= __btrfs_close_devices(fs_devices
);
754 if (!fs_devices
->opened
) {
755 seed_devices
= fs_devices
->seed
;
756 fs_devices
->seed
= NULL
;
758 mutex_unlock(&uuid_mutex
);
760 while (seed_devices
) {
761 fs_devices
= seed_devices
;
762 seed_devices
= fs_devices
->seed
;
763 __btrfs_close_devices(fs_devices
);
764 free_fs_devices(fs_devices
);
767 * Wait for rcu kworkers under __btrfs_close_devices
768 * to finish all blkdev_puts so device is really
769 * free when umount is done.
775 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
776 fmode_t flags
, void *holder
)
778 struct request_queue
*q
;
779 struct block_device
*bdev
;
780 struct list_head
*head
= &fs_devices
->devices
;
781 struct btrfs_device
*device
;
782 struct block_device
*latest_bdev
= NULL
;
783 struct buffer_head
*bh
;
784 struct btrfs_super_block
*disk_super
;
785 u64 latest_devid
= 0;
786 u64 latest_transid
= 0;
793 list_for_each_entry(device
, head
, dev_list
) {
799 /* Just open everything we can; ignore failures here */
800 if (btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
804 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
805 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
806 if (devid
!= device
->devid
)
809 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
813 device
->generation
= btrfs_super_generation(disk_super
);
814 if (!latest_transid
|| device
->generation
> latest_transid
) {
815 latest_devid
= devid
;
816 latest_transid
= device
->generation
;
820 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
821 device
->writeable
= 0;
823 device
->writeable
= !bdev_read_only(bdev
);
827 q
= bdev_get_queue(bdev
);
828 if (blk_queue_discard(q
)) {
829 device
->can_discard
= 1;
830 fs_devices
->num_can_discard
++;
834 device
->in_fs_metadata
= 0;
835 device
->mode
= flags
;
837 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
838 fs_devices
->rotating
= 1;
840 fs_devices
->open_devices
++;
841 if (device
->writeable
&&
842 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
843 fs_devices
->rw_devices
++;
844 list_add(&device
->dev_alloc_list
,
845 &fs_devices
->alloc_list
);
852 blkdev_put(bdev
, flags
);
855 if (fs_devices
->open_devices
== 0) {
859 fs_devices
->seeding
= seeding
;
860 fs_devices
->opened
= 1;
861 fs_devices
->latest_bdev
= latest_bdev
;
862 fs_devices
->latest_devid
= latest_devid
;
863 fs_devices
->latest_trans
= latest_transid
;
864 fs_devices
->total_rw_bytes
= 0;
869 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
870 fmode_t flags
, void *holder
)
874 mutex_lock(&uuid_mutex
);
875 if (fs_devices
->opened
) {
876 fs_devices
->opened
++;
879 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
881 mutex_unlock(&uuid_mutex
);
886 * Look for a btrfs signature on a device. This may be called out of the mount path
887 * and we are not allowed to call set_blocksize during the scan. The superblock
888 * is read via pagecache
890 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
891 struct btrfs_fs_devices
**fs_devices_ret
)
893 struct btrfs_super_block
*disk_super
;
894 struct block_device
*bdev
;
905 * we would like to check all the supers, but that would make
906 * a btrfs mount succeed after a mkfs from a different FS.
907 * So, we need to add a special mount option to scan for
908 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
910 bytenr
= btrfs_sb_offset(0);
912 mutex_lock(&uuid_mutex
);
914 bdev
= blkdev_get_by_path(path
, flags
, holder
);
921 /* make sure our super fits in the device */
922 if (bytenr
+ PAGE_CACHE_SIZE
>= i_size_read(bdev
->bd_inode
))
925 /* make sure our super fits in the page */
926 if (sizeof(*disk_super
) > PAGE_CACHE_SIZE
)
929 /* make sure our super doesn't straddle pages on disk */
930 index
= bytenr
>> PAGE_CACHE_SHIFT
;
931 if ((bytenr
+ sizeof(*disk_super
) - 1) >> PAGE_CACHE_SHIFT
!= index
)
934 /* pull in the page with our super */
935 page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
938 if (IS_ERR_OR_NULL(page
))
943 /* align our pointer to the offset of the super block */
944 disk_super
= p
+ (bytenr
& ~PAGE_CACHE_MASK
);
946 if (btrfs_super_bytenr(disk_super
) != bytenr
||
947 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
)
950 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
951 transid
= btrfs_super_generation(disk_super
);
952 total_devices
= btrfs_super_num_devices(disk_super
);
954 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
956 if (disk_super
->label
[0]) {
957 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
958 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
959 printk(KERN_INFO
"BTRFS: device label %s ", disk_super
->label
);
961 printk(KERN_INFO
"BTRFS: device fsid %pU ", disk_super
->fsid
);
964 printk(KERN_CONT
"devid %llu transid %llu %s\n", devid
, transid
, path
);
967 if (!ret
&& fs_devices_ret
)
968 (*fs_devices_ret
)->total_devices
= total_devices
;
972 page_cache_release(page
);
975 blkdev_put(bdev
, flags
);
977 mutex_unlock(&uuid_mutex
);
981 /* helper to account the used device space in the range */
982 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
983 u64 end
, u64
*length
)
985 struct btrfs_key key
;
986 struct btrfs_root
*root
= device
->dev_root
;
987 struct btrfs_dev_extent
*dev_extent
;
988 struct btrfs_path
*path
;
992 struct extent_buffer
*l
;
996 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
999 path
= btrfs_alloc_path();
1004 key
.objectid
= device
->devid
;
1006 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1008 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1012 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1019 slot
= path
->slots
[0];
1020 if (slot
>= btrfs_header_nritems(l
)) {
1021 ret
= btrfs_next_leaf(root
, path
);
1029 btrfs_item_key_to_cpu(l
, &key
, slot
);
1031 if (key
.objectid
< device
->devid
)
1034 if (key
.objectid
> device
->devid
)
1037 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
1040 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1041 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1043 if (key
.offset
<= start
&& extent_end
> end
) {
1044 *length
= end
- start
+ 1;
1046 } else if (key
.offset
<= start
&& extent_end
> start
)
1047 *length
+= extent_end
- start
;
1048 else if (key
.offset
> start
&& extent_end
<= end
)
1049 *length
+= extent_end
- key
.offset
;
1050 else if (key
.offset
> start
&& key
.offset
<= end
) {
1051 *length
+= end
- key
.offset
+ 1;
1053 } else if (key
.offset
> end
)
1061 btrfs_free_path(path
);
1065 static int contains_pending_extent(struct btrfs_trans_handle
*trans
,
1066 struct btrfs_device
*device
,
1067 u64
*start
, u64 len
)
1069 struct extent_map
*em
;
1072 list_for_each_entry(em
, &trans
->transaction
->pending_chunks
, list
) {
1073 struct map_lookup
*map
;
1076 map
= (struct map_lookup
*)em
->bdev
;
1077 for (i
= 0; i
< map
->num_stripes
; i
++) {
1078 if (map
->stripes
[i
].dev
!= device
)
1080 if (map
->stripes
[i
].physical
>= *start
+ len
||
1081 map
->stripes
[i
].physical
+ em
->orig_block_len
<=
1084 *start
= map
->stripes
[i
].physical
+
1095 * find_free_dev_extent - find free space in the specified device
1096 * @device: the device which we search the free space in
1097 * @num_bytes: the size of the free space that we need
1098 * @start: store the start of the free space.
1099 * @len: the size of the free space. that we find, or the size of the max
1100 * free space if we don't find suitable free space
1102 * this uses a pretty simple search, the expectation is that it is
1103 * called very infrequently and that a given device has a small number
1106 * @start is used to store the start of the free space if we find. But if we
1107 * don't find suitable free space, it will be used to store the start position
1108 * of the max free space.
1110 * @len is used to store the size of the free space that we find.
1111 * But if we don't find suitable free space, it is used to store the size of
1112 * the max free space.
1114 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
1115 struct btrfs_device
*device
, u64 num_bytes
,
1116 u64
*start
, u64
*len
)
1118 struct btrfs_key key
;
1119 struct btrfs_root
*root
= device
->dev_root
;
1120 struct btrfs_dev_extent
*dev_extent
;
1121 struct btrfs_path
*path
;
1127 u64 search_end
= device
->total_bytes
;
1130 struct extent_buffer
*l
;
1132 /* FIXME use last free of some kind */
1134 /* we don't want to overwrite the superblock on the drive,
1135 * so we make sure to start at an offset of at least 1MB
1137 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
1139 path
= btrfs_alloc_path();
1143 max_hole_start
= search_start
;
1147 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
1153 path
->search_commit_root
= 1;
1154 path
->skip_locking
= 1;
1156 key
.objectid
= device
->devid
;
1157 key
.offset
= search_start
;
1158 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1160 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1164 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1171 slot
= path
->slots
[0];
1172 if (slot
>= btrfs_header_nritems(l
)) {
1173 ret
= btrfs_next_leaf(root
, path
);
1181 btrfs_item_key_to_cpu(l
, &key
, slot
);
1183 if (key
.objectid
< device
->devid
)
1186 if (key
.objectid
> device
->devid
)
1189 if (btrfs_key_type(&key
) != BTRFS_DEV_EXTENT_KEY
)
1192 if (key
.offset
> search_start
) {
1193 hole_size
= key
.offset
- search_start
;
1196 * Have to check before we set max_hole_start, otherwise
1197 * we could end up sending back this offset anyway.
1199 if (contains_pending_extent(trans
, device
,
1204 if (hole_size
> max_hole_size
) {
1205 max_hole_start
= search_start
;
1206 max_hole_size
= hole_size
;
1210 * If this free space is greater than which we need,
1211 * it must be the max free space that we have found
1212 * until now, so max_hole_start must point to the start
1213 * of this free space and the length of this free space
1214 * is stored in max_hole_size. Thus, we return
1215 * max_hole_start and max_hole_size and go back to the
1218 if (hole_size
>= num_bytes
) {
1224 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1225 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1227 if (extent_end
> search_start
)
1228 search_start
= extent_end
;
1235 * At this point, search_start should be the end of
1236 * allocated dev extents, and when shrinking the device,
1237 * search_end may be smaller than search_start.
1239 if (search_end
> search_start
)
1240 hole_size
= search_end
- search_start
;
1242 if (hole_size
> max_hole_size
) {
1243 max_hole_start
= search_start
;
1244 max_hole_size
= hole_size
;
1247 if (contains_pending_extent(trans
, device
, &search_start
, hole_size
)) {
1248 btrfs_release_path(path
);
1253 if (hole_size
< num_bytes
)
1259 btrfs_free_path(path
);
1260 *start
= max_hole_start
;
1262 *len
= max_hole_size
;
1266 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1267 struct btrfs_device
*device
,
1271 struct btrfs_path
*path
;
1272 struct btrfs_root
*root
= device
->dev_root
;
1273 struct btrfs_key key
;
1274 struct btrfs_key found_key
;
1275 struct extent_buffer
*leaf
= NULL
;
1276 struct btrfs_dev_extent
*extent
= NULL
;
1278 path
= btrfs_alloc_path();
1282 key
.objectid
= device
->devid
;
1284 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1286 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1288 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1289 BTRFS_DEV_EXTENT_KEY
);
1292 leaf
= path
->nodes
[0];
1293 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1294 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1295 struct btrfs_dev_extent
);
1296 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1297 btrfs_dev_extent_length(leaf
, extent
) < start
);
1299 btrfs_release_path(path
);
1301 } else if (ret
== 0) {
1302 leaf
= path
->nodes
[0];
1303 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1304 struct btrfs_dev_extent
);
1306 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1310 if (device
->bytes_used
> 0) {
1311 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1312 device
->bytes_used
-= len
;
1313 spin_lock(&root
->fs_info
->free_chunk_lock
);
1314 root
->fs_info
->free_chunk_space
+= len
;
1315 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1317 ret
= btrfs_del_item(trans
, root
, path
);
1319 btrfs_error(root
->fs_info
, ret
,
1320 "Failed to remove dev extent item");
1323 btrfs_free_path(path
);
1327 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1328 struct btrfs_device
*device
,
1329 u64 chunk_tree
, u64 chunk_objectid
,
1330 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1333 struct btrfs_path
*path
;
1334 struct btrfs_root
*root
= device
->dev_root
;
1335 struct btrfs_dev_extent
*extent
;
1336 struct extent_buffer
*leaf
;
1337 struct btrfs_key key
;
1339 WARN_ON(!device
->in_fs_metadata
);
1340 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1341 path
= btrfs_alloc_path();
1345 key
.objectid
= device
->devid
;
1347 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1348 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1353 leaf
= path
->nodes
[0];
1354 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1355 struct btrfs_dev_extent
);
1356 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1357 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1358 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1360 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1361 btrfs_dev_extent_chunk_tree_uuid(extent
), BTRFS_UUID_SIZE
);
1363 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1364 btrfs_mark_buffer_dirty(leaf
);
1366 btrfs_free_path(path
);
1370 static u64
find_next_chunk(struct btrfs_fs_info
*fs_info
)
1372 struct extent_map_tree
*em_tree
;
1373 struct extent_map
*em
;
1377 em_tree
= &fs_info
->mapping_tree
.map_tree
;
1378 read_lock(&em_tree
->lock
);
1379 n
= rb_last(&em_tree
->map
);
1381 em
= rb_entry(n
, struct extent_map
, rb_node
);
1382 ret
= em
->start
+ em
->len
;
1384 read_unlock(&em_tree
->lock
);
1389 static noinline
int find_next_devid(struct btrfs_fs_info
*fs_info
,
1393 struct btrfs_key key
;
1394 struct btrfs_key found_key
;
1395 struct btrfs_path
*path
;
1397 path
= btrfs_alloc_path();
1401 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1402 key
.type
= BTRFS_DEV_ITEM_KEY
;
1403 key
.offset
= (u64
)-1;
1405 ret
= btrfs_search_slot(NULL
, fs_info
->chunk_root
, &key
, path
, 0, 0);
1409 BUG_ON(ret
== 0); /* Corruption */
1411 ret
= btrfs_previous_item(fs_info
->chunk_root
, path
,
1412 BTRFS_DEV_ITEMS_OBJECTID
,
1413 BTRFS_DEV_ITEM_KEY
);
1417 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1419 *devid_ret
= found_key
.offset
+ 1;
1423 btrfs_free_path(path
);
1428 * the device information is stored in the chunk root
1429 * the btrfs_device struct should be fully filled in
1431 static int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1432 struct btrfs_root
*root
,
1433 struct btrfs_device
*device
)
1436 struct btrfs_path
*path
;
1437 struct btrfs_dev_item
*dev_item
;
1438 struct extent_buffer
*leaf
;
1439 struct btrfs_key key
;
1442 root
= root
->fs_info
->chunk_root
;
1444 path
= btrfs_alloc_path();
1448 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1449 key
.type
= BTRFS_DEV_ITEM_KEY
;
1450 key
.offset
= device
->devid
;
1452 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1457 leaf
= path
->nodes
[0];
1458 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1460 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1461 btrfs_set_device_generation(leaf
, dev_item
, 0);
1462 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1463 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1464 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1465 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1466 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->total_bytes
);
1467 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1468 btrfs_set_device_group(leaf
, dev_item
, 0);
1469 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1470 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1471 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1473 ptr
= btrfs_device_uuid(dev_item
);
1474 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1475 ptr
= btrfs_device_fsid(dev_item
);
1476 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1477 btrfs_mark_buffer_dirty(leaf
);
1481 btrfs_free_path(path
);
1486 * Function to update ctime/mtime for a given device path.
1487 * Mainly used for ctime/mtime based probe like libblkid.
1489 static void update_dev_time(char *path_name
)
1493 filp
= filp_open(path_name
, O_RDWR
, 0);
1496 file_update_time(filp
);
1497 filp_close(filp
, NULL
);
1501 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1502 struct btrfs_device
*device
)
1505 struct btrfs_path
*path
;
1506 struct btrfs_key key
;
1507 struct btrfs_trans_handle
*trans
;
1509 root
= root
->fs_info
->chunk_root
;
1511 path
= btrfs_alloc_path();
1515 trans
= btrfs_start_transaction(root
, 0);
1516 if (IS_ERR(trans
)) {
1517 btrfs_free_path(path
);
1518 return PTR_ERR(trans
);
1520 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1521 key
.type
= BTRFS_DEV_ITEM_KEY
;
1522 key
.offset
= device
->devid
;
1525 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1534 ret
= btrfs_del_item(trans
, root
, path
);
1538 btrfs_free_path(path
);
1539 unlock_chunks(root
);
1540 btrfs_commit_transaction(trans
, root
);
1544 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1546 struct btrfs_device
*device
;
1547 struct btrfs_device
*next_device
;
1548 struct block_device
*bdev
;
1549 struct buffer_head
*bh
= NULL
;
1550 struct btrfs_super_block
*disk_super
;
1551 struct btrfs_fs_devices
*cur_devices
;
1558 bool clear_super
= false;
1560 mutex_lock(&uuid_mutex
);
1563 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
1565 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1566 root
->fs_info
->avail_system_alloc_bits
|
1567 root
->fs_info
->avail_metadata_alloc_bits
;
1568 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
1570 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1571 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1572 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1573 WARN_ON(num_devices
< 1);
1576 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1578 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1579 ret
= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET
;
1583 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1584 ret
= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET
;
1588 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID5
) &&
1589 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1590 ret
= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET
;
1593 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID6
) &&
1594 root
->fs_info
->fs_devices
->rw_devices
<= 3) {
1595 ret
= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET
;
1599 if (strcmp(device_path
, "missing") == 0) {
1600 struct list_head
*devices
;
1601 struct btrfs_device
*tmp
;
1604 devices
= &root
->fs_info
->fs_devices
->devices
;
1606 * It is safe to read the devices since the volume_mutex
1609 list_for_each_entry(tmp
, devices
, dev_list
) {
1610 if (tmp
->in_fs_metadata
&&
1611 !tmp
->is_tgtdev_for_dev_replace
&&
1621 ret
= BTRFS_ERROR_DEV_MISSING_NOT_FOUND
;
1625 ret
= btrfs_get_bdev_and_sb(device_path
,
1626 FMODE_WRITE
| FMODE_EXCL
,
1627 root
->fs_info
->bdev_holder
, 0,
1631 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1632 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1633 dev_uuid
= disk_super
->dev_item
.uuid
;
1634 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1642 if (device
->is_tgtdev_for_dev_replace
) {
1643 ret
= BTRFS_ERROR_DEV_TGT_REPLACE
;
1647 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1648 ret
= BTRFS_ERROR_DEV_ONLY_WRITABLE
;
1652 if (device
->writeable
) {
1654 list_del_init(&device
->dev_alloc_list
);
1655 unlock_chunks(root
);
1656 root
->fs_info
->fs_devices
->rw_devices
--;
1660 mutex_unlock(&uuid_mutex
);
1661 ret
= btrfs_shrink_device(device
, 0);
1662 mutex_lock(&uuid_mutex
);
1667 * TODO: the superblock still includes this device in its num_devices
1668 * counter although write_all_supers() is not locked out. This
1669 * could give a filesystem state which requires a degraded mount.
1671 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1675 spin_lock(&root
->fs_info
->free_chunk_lock
);
1676 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1678 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1680 device
->in_fs_metadata
= 0;
1681 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1684 * the device list mutex makes sure that we don't change
1685 * the device list while someone else is writing out all
1686 * the device supers. Whoever is writing all supers, should
1687 * lock the device list mutex before getting the number of
1688 * devices in the super block (super_copy). Conversely,
1689 * whoever updates the number of devices in the super block
1690 * (super_copy) should hold the device list mutex.
1693 cur_devices
= device
->fs_devices
;
1694 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1695 list_del_rcu(&device
->dev_list
);
1697 device
->fs_devices
->num_devices
--;
1698 device
->fs_devices
->total_devices
--;
1700 if (device
->missing
)
1701 root
->fs_info
->fs_devices
->missing_devices
--;
1703 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1704 struct btrfs_device
, dev_list
);
1705 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1706 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1707 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1708 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1711 device
->fs_devices
->open_devices
--;
1712 /* remove sysfs entry */
1713 btrfs_kobj_rm_device(root
->fs_info
, device
);
1716 call_rcu(&device
->rcu
, free_device
);
1718 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1719 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1720 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1722 if (cur_devices
->open_devices
== 0) {
1723 struct btrfs_fs_devices
*fs_devices
;
1724 fs_devices
= root
->fs_info
->fs_devices
;
1725 while (fs_devices
) {
1726 if (fs_devices
->seed
== cur_devices
) {
1727 fs_devices
->seed
= cur_devices
->seed
;
1730 fs_devices
= fs_devices
->seed
;
1732 cur_devices
->seed
= NULL
;
1734 __btrfs_close_devices(cur_devices
);
1735 unlock_chunks(root
);
1736 free_fs_devices(cur_devices
);
1739 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1740 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1743 * at this point, the device is zero sized. We want to
1744 * remove it from the devices list and zero out the old super
1746 if (clear_super
&& disk_super
) {
1750 /* make sure this device isn't detected as part of
1753 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1754 set_buffer_dirty(bh
);
1755 sync_dirty_buffer(bh
);
1757 /* clear the mirror copies of super block on the disk
1758 * being removed, 0th copy is been taken care above and
1759 * the below would take of the rest
1761 for (i
= 1; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1762 bytenr
= btrfs_sb_offset(i
);
1763 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>=
1764 i_size_read(bdev
->bd_inode
))
1768 bh
= __bread(bdev
, bytenr
/ 4096,
1769 BTRFS_SUPER_INFO_SIZE
);
1773 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1775 if (btrfs_super_bytenr(disk_super
) != bytenr
||
1776 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
) {
1779 memset(&disk_super
->magic
, 0,
1780 sizeof(disk_super
->magic
));
1781 set_buffer_dirty(bh
);
1782 sync_dirty_buffer(bh
);
1789 /* Notify udev that device has changed */
1790 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
1792 /* Update ctime/mtime for device path for libblkid */
1793 update_dev_time(device_path
);
1799 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1801 mutex_unlock(&uuid_mutex
);
1804 if (device
->writeable
) {
1806 list_add(&device
->dev_alloc_list
,
1807 &root
->fs_info
->fs_devices
->alloc_list
);
1808 unlock_chunks(root
);
1809 root
->fs_info
->fs_devices
->rw_devices
++;
1814 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info
*fs_info
,
1815 struct btrfs_device
*srcdev
)
1817 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1819 list_del_rcu(&srcdev
->dev_list
);
1820 list_del_rcu(&srcdev
->dev_alloc_list
);
1821 fs_info
->fs_devices
->num_devices
--;
1822 if (srcdev
->missing
) {
1823 fs_info
->fs_devices
->missing_devices
--;
1824 fs_info
->fs_devices
->rw_devices
++;
1826 if (srcdev
->can_discard
)
1827 fs_info
->fs_devices
->num_can_discard
--;
1829 fs_info
->fs_devices
->open_devices
--;
1831 /* zero out the old super */
1832 btrfs_scratch_superblock(srcdev
);
1835 call_rcu(&srcdev
->rcu
, free_device
);
1838 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1839 struct btrfs_device
*tgtdev
)
1841 struct btrfs_device
*next_device
;
1844 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1846 btrfs_scratch_superblock(tgtdev
);
1847 fs_info
->fs_devices
->open_devices
--;
1849 fs_info
->fs_devices
->num_devices
--;
1850 if (tgtdev
->can_discard
)
1851 fs_info
->fs_devices
->num_can_discard
++;
1853 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1854 struct btrfs_device
, dev_list
);
1855 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1856 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1857 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1858 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1859 list_del_rcu(&tgtdev
->dev_list
);
1861 call_rcu(&tgtdev
->rcu
, free_device
);
1863 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1866 static int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1867 struct btrfs_device
**device
)
1870 struct btrfs_super_block
*disk_super
;
1873 struct block_device
*bdev
;
1874 struct buffer_head
*bh
;
1877 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1878 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1881 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1882 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1883 dev_uuid
= disk_super
->dev_item
.uuid
;
1884 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1889 blkdev_put(bdev
, FMODE_READ
);
1893 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1895 struct btrfs_device
**device
)
1898 if (strcmp(device_path
, "missing") == 0) {
1899 struct list_head
*devices
;
1900 struct btrfs_device
*tmp
;
1902 devices
= &root
->fs_info
->fs_devices
->devices
;
1904 * It is safe to read the devices since the volume_mutex
1905 * is held by the caller.
1907 list_for_each_entry(tmp
, devices
, dev_list
) {
1908 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1915 btrfs_err(root
->fs_info
, "no missing device found");
1921 return btrfs_find_device_by_path(root
, device_path
, device
);
1926 * does all the dirty work required for changing file system's UUID.
1928 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1930 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1931 struct btrfs_fs_devices
*old_devices
;
1932 struct btrfs_fs_devices
*seed_devices
;
1933 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1934 struct btrfs_device
*device
;
1937 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1938 if (!fs_devices
->seeding
)
1941 seed_devices
= __alloc_fs_devices();
1942 if (IS_ERR(seed_devices
))
1943 return PTR_ERR(seed_devices
);
1945 old_devices
= clone_fs_devices(fs_devices
);
1946 if (IS_ERR(old_devices
)) {
1947 kfree(seed_devices
);
1948 return PTR_ERR(old_devices
);
1951 list_add(&old_devices
->list
, &fs_uuids
);
1953 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1954 seed_devices
->opened
= 1;
1955 INIT_LIST_HEAD(&seed_devices
->devices
);
1956 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1957 mutex_init(&seed_devices
->device_list_mutex
);
1959 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1960 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1963 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1964 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1965 device
->fs_devices
= seed_devices
;
1968 fs_devices
->seeding
= 0;
1969 fs_devices
->num_devices
= 0;
1970 fs_devices
->open_devices
= 0;
1971 fs_devices
->seed
= seed_devices
;
1973 generate_random_uuid(fs_devices
->fsid
);
1974 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1975 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1976 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1978 super_flags
= btrfs_super_flags(disk_super
) &
1979 ~BTRFS_SUPER_FLAG_SEEDING
;
1980 btrfs_set_super_flags(disk_super
, super_flags
);
1986 * strore the expected generation for seed devices in device items.
1988 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
1989 struct btrfs_root
*root
)
1991 struct btrfs_path
*path
;
1992 struct extent_buffer
*leaf
;
1993 struct btrfs_dev_item
*dev_item
;
1994 struct btrfs_device
*device
;
1995 struct btrfs_key key
;
1996 u8 fs_uuid
[BTRFS_UUID_SIZE
];
1997 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2001 path
= btrfs_alloc_path();
2005 root
= root
->fs_info
->chunk_root
;
2006 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2008 key
.type
= BTRFS_DEV_ITEM_KEY
;
2011 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2015 leaf
= path
->nodes
[0];
2017 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
2018 ret
= btrfs_next_leaf(root
, path
);
2023 leaf
= path
->nodes
[0];
2024 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2025 btrfs_release_path(path
);
2029 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2030 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
2031 key
.type
!= BTRFS_DEV_ITEM_KEY
)
2034 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2035 struct btrfs_dev_item
);
2036 devid
= btrfs_device_id(leaf
, dev_item
);
2037 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
2039 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
2041 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
2043 BUG_ON(!device
); /* Logic error */
2045 if (device
->fs_devices
->seeding
) {
2046 btrfs_set_device_generation(leaf
, dev_item
,
2047 device
->generation
);
2048 btrfs_mark_buffer_dirty(leaf
);
2056 btrfs_free_path(path
);
2060 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
2062 struct request_queue
*q
;
2063 struct btrfs_trans_handle
*trans
;
2064 struct btrfs_device
*device
;
2065 struct block_device
*bdev
;
2066 struct list_head
*devices
;
2067 struct super_block
*sb
= root
->fs_info
->sb
;
2068 struct rcu_string
*name
;
2070 int seeding_dev
= 0;
2073 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
2076 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2077 root
->fs_info
->bdev_holder
);
2079 return PTR_ERR(bdev
);
2081 if (root
->fs_info
->fs_devices
->seeding
) {
2083 down_write(&sb
->s_umount
);
2084 mutex_lock(&uuid_mutex
);
2087 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2089 devices
= &root
->fs_info
->fs_devices
->devices
;
2091 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2092 list_for_each_entry(device
, devices
, dev_list
) {
2093 if (device
->bdev
== bdev
) {
2096 &root
->fs_info
->fs_devices
->device_list_mutex
);
2100 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2102 device
= btrfs_alloc_device(root
->fs_info
, NULL
, NULL
);
2103 if (IS_ERR(device
)) {
2104 /* we can safely leave the fs_devices entry around */
2105 ret
= PTR_ERR(device
);
2109 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2115 rcu_assign_pointer(device
->name
, name
);
2117 trans
= btrfs_start_transaction(root
, 0);
2118 if (IS_ERR(trans
)) {
2119 rcu_string_free(device
->name
);
2121 ret
= PTR_ERR(trans
);
2127 q
= bdev_get_queue(bdev
);
2128 if (blk_queue_discard(q
))
2129 device
->can_discard
= 1;
2130 device
->writeable
= 1;
2131 device
->generation
= trans
->transid
;
2132 device
->io_width
= root
->sectorsize
;
2133 device
->io_align
= root
->sectorsize
;
2134 device
->sector_size
= root
->sectorsize
;
2135 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2136 device
->disk_total_bytes
= device
->total_bytes
;
2137 device
->dev_root
= root
->fs_info
->dev_root
;
2138 device
->bdev
= bdev
;
2139 device
->in_fs_metadata
= 1;
2140 device
->is_tgtdev_for_dev_replace
= 0;
2141 device
->mode
= FMODE_EXCL
;
2142 device
->dev_stats_valid
= 1;
2143 set_blocksize(device
->bdev
, 4096);
2146 sb
->s_flags
&= ~MS_RDONLY
;
2147 ret
= btrfs_prepare_sprout(root
);
2148 BUG_ON(ret
); /* -ENOMEM */
2151 device
->fs_devices
= root
->fs_info
->fs_devices
;
2153 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2154 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
2155 list_add(&device
->dev_alloc_list
,
2156 &root
->fs_info
->fs_devices
->alloc_list
);
2157 root
->fs_info
->fs_devices
->num_devices
++;
2158 root
->fs_info
->fs_devices
->open_devices
++;
2159 root
->fs_info
->fs_devices
->rw_devices
++;
2160 root
->fs_info
->fs_devices
->total_devices
++;
2161 if (device
->can_discard
)
2162 root
->fs_info
->fs_devices
->num_can_discard
++;
2163 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2165 spin_lock(&root
->fs_info
->free_chunk_lock
);
2166 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
2167 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2169 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
2170 root
->fs_info
->fs_devices
->rotating
= 1;
2172 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
2173 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
2174 total_bytes
+ device
->total_bytes
);
2176 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
2177 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
2180 /* add sysfs device entry */
2181 btrfs_kobj_add_device(root
->fs_info
, device
);
2183 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2186 char fsid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
2187 ret
= init_first_rw_device(trans
, root
, device
);
2189 btrfs_abort_transaction(trans
, root
, ret
);
2192 ret
= btrfs_finish_sprout(trans
, root
);
2194 btrfs_abort_transaction(trans
, root
, ret
);
2198 /* Sprouting would change fsid of the mounted root,
2199 * so rename the fsid on the sysfs
2201 snprintf(fsid_buf
, BTRFS_UUID_UNPARSED_SIZE
, "%pU",
2202 root
->fs_info
->fsid
);
2203 if (kobject_rename(&root
->fs_info
->super_kobj
, fsid_buf
))
2206 ret
= btrfs_add_device(trans
, root
, device
);
2208 btrfs_abort_transaction(trans
, root
, ret
);
2214 * we've got more storage, clear any full flags on the space
2217 btrfs_clear_space_info_full(root
->fs_info
);
2219 unlock_chunks(root
);
2220 root
->fs_info
->num_tolerated_disk_barrier_failures
=
2221 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
2222 ret
= btrfs_commit_transaction(trans
, root
);
2225 mutex_unlock(&uuid_mutex
);
2226 up_write(&sb
->s_umount
);
2228 if (ret
) /* transaction commit */
2231 ret
= btrfs_relocate_sys_chunks(root
);
2233 btrfs_error(root
->fs_info
, ret
,
2234 "Failed to relocate sys chunks after "
2235 "device initialization. This can be fixed "
2236 "using the \"btrfs balance\" command.");
2237 trans
= btrfs_attach_transaction(root
);
2238 if (IS_ERR(trans
)) {
2239 if (PTR_ERR(trans
) == -ENOENT
)
2241 return PTR_ERR(trans
);
2243 ret
= btrfs_commit_transaction(trans
, root
);
2246 /* Update ctime/mtime for libblkid */
2247 update_dev_time(device_path
);
2251 unlock_chunks(root
);
2252 btrfs_end_transaction(trans
, root
);
2253 rcu_string_free(device
->name
);
2254 btrfs_kobj_rm_device(root
->fs_info
, device
);
2257 blkdev_put(bdev
, FMODE_EXCL
);
2259 mutex_unlock(&uuid_mutex
);
2260 up_write(&sb
->s_umount
);
2265 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2266 struct btrfs_device
**device_out
)
2268 struct request_queue
*q
;
2269 struct btrfs_device
*device
;
2270 struct block_device
*bdev
;
2271 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2272 struct list_head
*devices
;
2273 struct rcu_string
*name
;
2274 u64 devid
= BTRFS_DEV_REPLACE_DEVID
;
2278 if (fs_info
->fs_devices
->seeding
)
2281 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2282 fs_info
->bdev_holder
);
2284 return PTR_ERR(bdev
);
2286 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2288 devices
= &fs_info
->fs_devices
->devices
;
2289 list_for_each_entry(device
, devices
, dev_list
) {
2290 if (device
->bdev
== bdev
) {
2296 device
= btrfs_alloc_device(NULL
, &devid
, NULL
);
2297 if (IS_ERR(device
)) {
2298 ret
= PTR_ERR(device
);
2302 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2308 rcu_assign_pointer(device
->name
, name
);
2310 q
= bdev_get_queue(bdev
);
2311 if (blk_queue_discard(q
))
2312 device
->can_discard
= 1;
2313 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2314 device
->writeable
= 1;
2315 device
->generation
= 0;
2316 device
->io_width
= root
->sectorsize
;
2317 device
->io_align
= root
->sectorsize
;
2318 device
->sector_size
= root
->sectorsize
;
2319 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2320 device
->disk_total_bytes
= device
->total_bytes
;
2321 device
->dev_root
= fs_info
->dev_root
;
2322 device
->bdev
= bdev
;
2323 device
->in_fs_metadata
= 1;
2324 device
->is_tgtdev_for_dev_replace
= 1;
2325 device
->mode
= FMODE_EXCL
;
2326 device
->dev_stats_valid
= 1;
2327 set_blocksize(device
->bdev
, 4096);
2328 device
->fs_devices
= fs_info
->fs_devices
;
2329 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2330 fs_info
->fs_devices
->num_devices
++;
2331 fs_info
->fs_devices
->open_devices
++;
2332 if (device
->can_discard
)
2333 fs_info
->fs_devices
->num_can_discard
++;
2334 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2336 *device_out
= device
;
2340 blkdev_put(bdev
, FMODE_EXCL
);
2344 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2345 struct btrfs_device
*tgtdev
)
2347 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2348 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2349 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2350 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2351 tgtdev
->dev_root
= fs_info
->dev_root
;
2352 tgtdev
->in_fs_metadata
= 1;
2355 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2356 struct btrfs_device
*device
)
2359 struct btrfs_path
*path
;
2360 struct btrfs_root
*root
;
2361 struct btrfs_dev_item
*dev_item
;
2362 struct extent_buffer
*leaf
;
2363 struct btrfs_key key
;
2365 root
= device
->dev_root
->fs_info
->chunk_root
;
2367 path
= btrfs_alloc_path();
2371 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2372 key
.type
= BTRFS_DEV_ITEM_KEY
;
2373 key
.offset
= device
->devid
;
2375 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2384 leaf
= path
->nodes
[0];
2385 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2387 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2388 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2389 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2390 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2391 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2392 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
2393 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
2394 btrfs_mark_buffer_dirty(leaf
);
2397 btrfs_free_path(path
);
2401 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2402 struct btrfs_device
*device
, u64 new_size
)
2404 struct btrfs_super_block
*super_copy
=
2405 device
->dev_root
->fs_info
->super_copy
;
2406 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2407 u64 diff
= new_size
- device
->total_bytes
;
2409 if (!device
->writeable
)
2411 if (new_size
<= device
->total_bytes
||
2412 device
->is_tgtdev_for_dev_replace
)
2415 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2416 device
->fs_devices
->total_rw_bytes
+= diff
;
2418 device
->total_bytes
= new_size
;
2419 device
->disk_total_bytes
= new_size
;
2420 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2422 return btrfs_update_device(trans
, device
);
2425 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2426 struct btrfs_device
*device
, u64 new_size
)
2429 lock_chunks(device
->dev_root
);
2430 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2431 unlock_chunks(device
->dev_root
);
2435 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2436 struct btrfs_root
*root
,
2437 u64 chunk_tree
, u64 chunk_objectid
,
2441 struct btrfs_path
*path
;
2442 struct btrfs_key key
;
2444 root
= root
->fs_info
->chunk_root
;
2445 path
= btrfs_alloc_path();
2449 key
.objectid
= chunk_objectid
;
2450 key
.offset
= chunk_offset
;
2451 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2453 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2456 else if (ret
> 0) { /* Logic error or corruption */
2457 btrfs_error(root
->fs_info
, -ENOENT
,
2458 "Failed lookup while freeing chunk.");
2463 ret
= btrfs_del_item(trans
, root
, path
);
2465 btrfs_error(root
->fs_info
, ret
,
2466 "Failed to delete chunk item.");
2468 btrfs_free_path(path
);
2472 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2475 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2476 struct btrfs_disk_key
*disk_key
;
2477 struct btrfs_chunk
*chunk
;
2484 struct btrfs_key key
;
2486 array_size
= btrfs_super_sys_array_size(super_copy
);
2488 ptr
= super_copy
->sys_chunk_array
;
2491 while (cur
< array_size
) {
2492 disk_key
= (struct btrfs_disk_key
*)ptr
;
2493 btrfs_disk_key_to_cpu(&key
, disk_key
);
2495 len
= sizeof(*disk_key
);
2497 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2498 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2499 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2500 len
+= btrfs_chunk_item_size(num_stripes
);
2505 if (key
.objectid
== chunk_objectid
&&
2506 key
.offset
== chunk_offset
) {
2507 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2509 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2518 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2519 u64 chunk_tree
, u64 chunk_objectid
,
2522 struct extent_map_tree
*em_tree
;
2523 struct btrfs_root
*extent_root
;
2524 struct btrfs_trans_handle
*trans
;
2525 struct extent_map
*em
;
2526 struct map_lookup
*map
;
2530 root
= root
->fs_info
->chunk_root
;
2531 extent_root
= root
->fs_info
->extent_root
;
2532 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2534 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2538 /* step one, relocate all the extents inside this chunk */
2539 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2543 trans
= btrfs_start_transaction(root
, 0);
2544 if (IS_ERR(trans
)) {
2545 ret
= PTR_ERR(trans
);
2546 btrfs_std_error(root
->fs_info
, ret
);
2553 * step two, delete the device extents and the
2554 * chunk tree entries
2556 read_lock(&em_tree
->lock
);
2557 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2558 read_unlock(&em_tree
->lock
);
2560 BUG_ON(!em
|| em
->start
> chunk_offset
||
2561 em
->start
+ em
->len
< chunk_offset
);
2562 map
= (struct map_lookup
*)em
->bdev
;
2564 for (i
= 0; i
< map
->num_stripes
; i
++) {
2565 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2566 map
->stripes
[i
].physical
);
2569 if (map
->stripes
[i
].dev
) {
2570 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2574 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2579 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2581 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2582 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2586 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2589 write_lock(&em_tree
->lock
);
2590 remove_extent_mapping(em_tree
, em
);
2591 write_unlock(&em_tree
->lock
);
2593 /* once for the tree */
2594 free_extent_map(em
);
2596 free_extent_map(em
);
2598 unlock_chunks(root
);
2599 btrfs_end_transaction(trans
, root
);
2603 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2605 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2606 struct btrfs_path
*path
;
2607 struct extent_buffer
*leaf
;
2608 struct btrfs_chunk
*chunk
;
2609 struct btrfs_key key
;
2610 struct btrfs_key found_key
;
2611 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2613 bool retried
= false;
2617 path
= btrfs_alloc_path();
2622 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2623 key
.offset
= (u64
)-1;
2624 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2627 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2630 BUG_ON(ret
== 0); /* Corruption */
2632 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2639 leaf
= path
->nodes
[0];
2640 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2642 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2643 struct btrfs_chunk
);
2644 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2645 btrfs_release_path(path
);
2647 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2648 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2657 if (found_key
.offset
== 0)
2659 key
.offset
= found_key
.offset
- 1;
2662 if (failed
&& !retried
) {
2666 } else if (WARN_ON(failed
&& retried
)) {
2670 btrfs_free_path(path
);
2674 static int insert_balance_item(struct btrfs_root
*root
,
2675 struct btrfs_balance_control
*bctl
)
2677 struct btrfs_trans_handle
*trans
;
2678 struct btrfs_balance_item
*item
;
2679 struct btrfs_disk_balance_args disk_bargs
;
2680 struct btrfs_path
*path
;
2681 struct extent_buffer
*leaf
;
2682 struct btrfs_key key
;
2685 path
= btrfs_alloc_path();
2689 trans
= btrfs_start_transaction(root
, 0);
2690 if (IS_ERR(trans
)) {
2691 btrfs_free_path(path
);
2692 return PTR_ERR(trans
);
2695 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2696 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2699 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2704 leaf
= path
->nodes
[0];
2705 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2707 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2709 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2710 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2711 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2712 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2713 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2714 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2716 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2718 btrfs_mark_buffer_dirty(leaf
);
2720 btrfs_free_path(path
);
2721 err
= btrfs_commit_transaction(trans
, root
);
2727 static int del_balance_item(struct btrfs_root
*root
)
2729 struct btrfs_trans_handle
*trans
;
2730 struct btrfs_path
*path
;
2731 struct btrfs_key key
;
2734 path
= btrfs_alloc_path();
2738 trans
= btrfs_start_transaction(root
, 0);
2739 if (IS_ERR(trans
)) {
2740 btrfs_free_path(path
);
2741 return PTR_ERR(trans
);
2744 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2745 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2748 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2756 ret
= btrfs_del_item(trans
, root
, path
);
2758 btrfs_free_path(path
);
2759 err
= btrfs_commit_transaction(trans
, root
);
2766 * This is a heuristic used to reduce the number of chunks balanced on
2767 * resume after balance was interrupted.
2769 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2772 * Turn on soft mode for chunk types that were being converted.
2774 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2775 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2776 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2777 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2778 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2779 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2782 * Turn on usage filter if is not already used. The idea is
2783 * that chunks that we have already balanced should be
2784 * reasonably full. Don't do it for chunks that are being
2785 * converted - that will keep us from relocating unconverted
2786 * (albeit full) chunks.
2788 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2789 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2790 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2791 bctl
->data
.usage
= 90;
2793 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2794 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2795 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2796 bctl
->sys
.usage
= 90;
2798 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2799 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2800 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2801 bctl
->meta
.usage
= 90;
2806 * Should be called with both balance and volume mutexes held to
2807 * serialize other volume operations (add_dev/rm_dev/resize) with
2808 * restriper. Same goes for unset_balance_control.
2810 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2812 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2814 BUG_ON(fs_info
->balance_ctl
);
2816 spin_lock(&fs_info
->balance_lock
);
2817 fs_info
->balance_ctl
= bctl
;
2818 spin_unlock(&fs_info
->balance_lock
);
2821 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2823 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2825 BUG_ON(!fs_info
->balance_ctl
);
2827 spin_lock(&fs_info
->balance_lock
);
2828 fs_info
->balance_ctl
= NULL
;
2829 spin_unlock(&fs_info
->balance_lock
);
2835 * Balance filters. Return 1 if chunk should be filtered out
2836 * (should not be balanced).
2838 static int chunk_profiles_filter(u64 chunk_type
,
2839 struct btrfs_balance_args
*bargs
)
2841 chunk_type
= chunk_to_extended(chunk_type
) &
2842 BTRFS_EXTENDED_PROFILE_MASK
;
2844 if (bargs
->profiles
& chunk_type
)
2850 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2851 struct btrfs_balance_args
*bargs
)
2853 struct btrfs_block_group_cache
*cache
;
2854 u64 chunk_used
, user_thresh
;
2857 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2858 chunk_used
= btrfs_block_group_used(&cache
->item
);
2860 if (bargs
->usage
== 0)
2862 else if (bargs
->usage
> 100)
2863 user_thresh
= cache
->key
.offset
;
2865 user_thresh
= div_factor_fine(cache
->key
.offset
,
2868 if (chunk_used
< user_thresh
)
2871 btrfs_put_block_group(cache
);
2875 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2876 struct btrfs_chunk
*chunk
,
2877 struct btrfs_balance_args
*bargs
)
2879 struct btrfs_stripe
*stripe
;
2880 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2883 for (i
= 0; i
< num_stripes
; i
++) {
2884 stripe
= btrfs_stripe_nr(chunk
, i
);
2885 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2892 /* [pstart, pend) */
2893 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2894 struct btrfs_chunk
*chunk
,
2896 struct btrfs_balance_args
*bargs
)
2898 struct btrfs_stripe
*stripe
;
2899 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2905 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2908 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2909 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)) {
2910 factor
= num_stripes
/ 2;
2911 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID5
) {
2912 factor
= num_stripes
- 1;
2913 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID6
) {
2914 factor
= num_stripes
- 2;
2916 factor
= num_stripes
;
2919 for (i
= 0; i
< num_stripes
; i
++) {
2920 stripe
= btrfs_stripe_nr(chunk
, i
);
2921 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2924 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2925 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2926 do_div(stripe_length
, factor
);
2928 if (stripe_offset
< bargs
->pend
&&
2929 stripe_offset
+ stripe_length
> bargs
->pstart
)
2936 /* [vstart, vend) */
2937 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2938 struct btrfs_chunk
*chunk
,
2940 struct btrfs_balance_args
*bargs
)
2942 if (chunk_offset
< bargs
->vend
&&
2943 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2944 /* at least part of the chunk is inside this vrange */
2950 static int chunk_soft_convert_filter(u64 chunk_type
,
2951 struct btrfs_balance_args
*bargs
)
2953 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2956 chunk_type
= chunk_to_extended(chunk_type
) &
2957 BTRFS_EXTENDED_PROFILE_MASK
;
2959 if (bargs
->target
== chunk_type
)
2965 static int should_balance_chunk(struct btrfs_root
*root
,
2966 struct extent_buffer
*leaf
,
2967 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2969 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2970 struct btrfs_balance_args
*bargs
= NULL
;
2971 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2974 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2975 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2979 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2980 bargs
= &bctl
->data
;
2981 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
2983 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
2984 bargs
= &bctl
->meta
;
2986 /* profiles filter */
2987 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
2988 chunk_profiles_filter(chunk_type
, bargs
)) {
2993 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2994 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
2999 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
3000 chunk_devid_filter(leaf
, chunk
, bargs
)) {
3004 /* drange filter, makes sense only with devid filter */
3005 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
3006 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3011 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
3012 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3016 /* soft profile changing mode */
3017 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
3018 chunk_soft_convert_filter(chunk_type
, bargs
)) {
3023 * limited by count, must be the last filter
3025 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT
)) {
3026 if (bargs
->limit
== 0)
3035 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
3037 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3038 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3039 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
3040 struct list_head
*devices
;
3041 struct btrfs_device
*device
;
3044 struct btrfs_chunk
*chunk
;
3045 struct btrfs_path
*path
;
3046 struct btrfs_key key
;
3047 struct btrfs_key found_key
;
3048 struct btrfs_trans_handle
*trans
;
3049 struct extent_buffer
*leaf
;
3052 int enospc_errors
= 0;
3053 bool counting
= true;
3054 u64 limit_data
= bctl
->data
.limit
;
3055 u64 limit_meta
= bctl
->meta
.limit
;
3056 u64 limit_sys
= bctl
->sys
.limit
;
3058 /* step one make some room on all the devices */
3059 devices
= &fs_info
->fs_devices
->devices
;
3060 list_for_each_entry(device
, devices
, dev_list
) {
3061 old_size
= device
->total_bytes
;
3062 size_to_free
= div_factor(old_size
, 1);
3063 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
3064 if (!device
->writeable
||
3065 device
->total_bytes
- device
->bytes_used
> size_to_free
||
3066 device
->is_tgtdev_for_dev_replace
)
3069 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
3074 trans
= btrfs_start_transaction(dev_root
, 0);
3075 BUG_ON(IS_ERR(trans
));
3077 ret
= btrfs_grow_device(trans
, device
, old_size
);
3080 btrfs_end_transaction(trans
, dev_root
);
3083 /* step two, relocate all the chunks */
3084 path
= btrfs_alloc_path();
3090 /* zero out stat counters */
3091 spin_lock(&fs_info
->balance_lock
);
3092 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
3093 spin_unlock(&fs_info
->balance_lock
);
3096 bctl
->data
.limit
= limit_data
;
3097 bctl
->meta
.limit
= limit_meta
;
3098 bctl
->sys
.limit
= limit_sys
;
3100 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3101 key
.offset
= (u64
)-1;
3102 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3105 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
3106 atomic_read(&fs_info
->balance_cancel_req
)) {
3111 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3116 * this shouldn't happen, it means the last relocate
3120 BUG(); /* FIXME break ? */
3122 ret
= btrfs_previous_item(chunk_root
, path
, 0,
3123 BTRFS_CHUNK_ITEM_KEY
);
3129 leaf
= path
->nodes
[0];
3130 slot
= path
->slots
[0];
3131 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3133 if (found_key
.objectid
!= key
.objectid
)
3136 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3139 spin_lock(&fs_info
->balance_lock
);
3140 bctl
->stat
.considered
++;
3141 spin_unlock(&fs_info
->balance_lock
);
3144 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
3146 btrfs_release_path(path
);
3151 spin_lock(&fs_info
->balance_lock
);
3152 bctl
->stat
.expected
++;
3153 spin_unlock(&fs_info
->balance_lock
);
3157 ret
= btrfs_relocate_chunk(chunk_root
,
3158 chunk_root
->root_key
.objectid
,
3161 if (ret
&& ret
!= -ENOSPC
)
3163 if (ret
== -ENOSPC
) {
3166 spin_lock(&fs_info
->balance_lock
);
3167 bctl
->stat
.completed
++;
3168 spin_unlock(&fs_info
->balance_lock
);
3171 if (found_key
.offset
== 0)
3173 key
.offset
= found_key
.offset
- 1;
3177 btrfs_release_path(path
);
3182 btrfs_free_path(path
);
3183 if (enospc_errors
) {
3184 btrfs_info(fs_info
, "%d enospc errors during balance",
3194 * alloc_profile_is_valid - see if a given profile is valid and reduced
3195 * @flags: profile to validate
3196 * @extended: if true @flags is treated as an extended profile
3198 static int alloc_profile_is_valid(u64 flags
, int extended
)
3200 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3201 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3203 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3205 /* 1) check that all other bits are zeroed */
3209 /* 2) see if profile is reduced */
3211 return !extended
; /* "0" is valid for usual profiles */
3213 /* true if exactly one bit set */
3214 return (flags
& (flags
- 1)) == 0;
3217 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3219 /* cancel requested || normal exit path */
3220 return atomic_read(&fs_info
->balance_cancel_req
) ||
3221 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3222 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3225 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
3229 unset_balance_control(fs_info
);
3230 ret
= del_balance_item(fs_info
->tree_root
);
3232 btrfs_std_error(fs_info
, ret
);
3234 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3238 * Should be called with both balance and volume mutexes held
3240 int btrfs_balance(struct btrfs_balance_control
*bctl
,
3241 struct btrfs_ioctl_balance_args
*bargs
)
3243 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
3250 if (btrfs_fs_closing(fs_info
) ||
3251 atomic_read(&fs_info
->balance_pause_req
) ||
3252 atomic_read(&fs_info
->balance_cancel_req
)) {
3257 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3258 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3262 * In case of mixed groups both data and meta should be picked,
3263 * and identical options should be given for both of them.
3265 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3266 if (mixed
&& (bctl
->flags
& allowed
)) {
3267 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3268 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3269 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3270 btrfs_err(fs_info
, "with mixed groups data and "
3271 "metadata balance options must be the same");
3277 num_devices
= fs_info
->fs_devices
->num_devices
;
3278 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3279 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3280 BUG_ON(num_devices
< 1);
3283 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3284 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
3285 if (num_devices
== 1)
3286 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3287 else if (num_devices
> 1)
3288 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3289 if (num_devices
> 2)
3290 allowed
|= BTRFS_BLOCK_GROUP_RAID5
;
3291 if (num_devices
> 3)
3292 allowed
|= (BTRFS_BLOCK_GROUP_RAID10
|
3293 BTRFS_BLOCK_GROUP_RAID6
);
3294 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3295 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3296 (bctl
->data
.target
& ~allowed
))) {
3297 btrfs_err(fs_info
, "unable to start balance with target "
3298 "data profile %llu",
3303 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3304 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3305 (bctl
->meta
.target
& ~allowed
))) {
3307 "unable to start balance with target metadata profile %llu",
3312 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3313 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3314 (bctl
->sys
.target
& ~allowed
))) {
3316 "unable to start balance with target system profile %llu",
3322 /* allow dup'ed data chunks only in mixed mode */
3323 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3324 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3325 btrfs_err(fs_info
, "dup for data is not allowed");
3330 /* allow to reduce meta or sys integrity only if force set */
3331 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3332 BTRFS_BLOCK_GROUP_RAID10
|
3333 BTRFS_BLOCK_GROUP_RAID5
|
3334 BTRFS_BLOCK_GROUP_RAID6
;
3336 seq
= read_seqbegin(&fs_info
->profiles_lock
);
3338 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3339 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3340 !(bctl
->sys
.target
& allowed
)) ||
3341 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3342 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3343 !(bctl
->meta
.target
& allowed
))) {
3344 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3345 btrfs_info(fs_info
, "force reducing metadata integrity");
3347 btrfs_err(fs_info
, "balance will reduce metadata "
3348 "integrity, use force if you want this");
3353 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
3355 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3356 int num_tolerated_disk_barrier_failures
;
3357 u64 target
= bctl
->sys
.target
;
3359 num_tolerated_disk_barrier_failures
=
3360 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3361 if (num_tolerated_disk_barrier_failures
> 0 &&
3363 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3364 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3365 num_tolerated_disk_barrier_failures
= 0;
3366 else if (num_tolerated_disk_barrier_failures
> 1 &&
3368 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3369 num_tolerated_disk_barrier_failures
= 1;
3371 fs_info
->num_tolerated_disk_barrier_failures
=
3372 num_tolerated_disk_barrier_failures
;
3375 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3376 if (ret
&& ret
!= -EEXIST
)
3379 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3380 BUG_ON(ret
== -EEXIST
);
3381 set_balance_control(bctl
);
3383 BUG_ON(ret
!= -EEXIST
);
3384 spin_lock(&fs_info
->balance_lock
);
3385 update_balance_args(bctl
);
3386 spin_unlock(&fs_info
->balance_lock
);
3389 atomic_inc(&fs_info
->balance_running
);
3390 mutex_unlock(&fs_info
->balance_mutex
);
3392 ret
= __btrfs_balance(fs_info
);
3394 mutex_lock(&fs_info
->balance_mutex
);
3395 atomic_dec(&fs_info
->balance_running
);
3397 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3398 fs_info
->num_tolerated_disk_barrier_failures
=
3399 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3403 memset(bargs
, 0, sizeof(*bargs
));
3404 update_ioctl_balance_args(fs_info
, 0, bargs
);
3407 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3408 balance_need_close(fs_info
)) {
3409 __cancel_balance(fs_info
);
3412 wake_up(&fs_info
->balance_wait_q
);
3416 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3417 __cancel_balance(fs_info
);
3420 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3425 static int balance_kthread(void *data
)
3427 struct btrfs_fs_info
*fs_info
= data
;
3430 mutex_lock(&fs_info
->volume_mutex
);
3431 mutex_lock(&fs_info
->balance_mutex
);
3433 if (fs_info
->balance_ctl
) {
3434 btrfs_info(fs_info
, "continuing balance");
3435 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3438 mutex_unlock(&fs_info
->balance_mutex
);
3439 mutex_unlock(&fs_info
->volume_mutex
);
3444 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3446 struct task_struct
*tsk
;
3448 spin_lock(&fs_info
->balance_lock
);
3449 if (!fs_info
->balance_ctl
) {
3450 spin_unlock(&fs_info
->balance_lock
);
3453 spin_unlock(&fs_info
->balance_lock
);
3455 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3456 btrfs_info(fs_info
, "force skipping balance");
3460 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3461 return PTR_ERR_OR_ZERO(tsk
);
3464 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3466 struct btrfs_balance_control
*bctl
;
3467 struct btrfs_balance_item
*item
;
3468 struct btrfs_disk_balance_args disk_bargs
;
3469 struct btrfs_path
*path
;
3470 struct extent_buffer
*leaf
;
3471 struct btrfs_key key
;
3474 path
= btrfs_alloc_path();
3478 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3479 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3482 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3485 if (ret
> 0) { /* ret = -ENOENT; */
3490 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3496 leaf
= path
->nodes
[0];
3497 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3499 bctl
->fs_info
= fs_info
;
3500 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3501 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3503 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3504 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3505 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3506 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3507 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3508 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3510 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3512 mutex_lock(&fs_info
->volume_mutex
);
3513 mutex_lock(&fs_info
->balance_mutex
);
3515 set_balance_control(bctl
);
3517 mutex_unlock(&fs_info
->balance_mutex
);
3518 mutex_unlock(&fs_info
->volume_mutex
);
3520 btrfs_free_path(path
);
3524 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3528 mutex_lock(&fs_info
->balance_mutex
);
3529 if (!fs_info
->balance_ctl
) {
3530 mutex_unlock(&fs_info
->balance_mutex
);
3534 if (atomic_read(&fs_info
->balance_running
)) {
3535 atomic_inc(&fs_info
->balance_pause_req
);
3536 mutex_unlock(&fs_info
->balance_mutex
);
3538 wait_event(fs_info
->balance_wait_q
,
3539 atomic_read(&fs_info
->balance_running
) == 0);
3541 mutex_lock(&fs_info
->balance_mutex
);
3542 /* we are good with balance_ctl ripped off from under us */
3543 BUG_ON(atomic_read(&fs_info
->balance_running
));
3544 atomic_dec(&fs_info
->balance_pause_req
);
3549 mutex_unlock(&fs_info
->balance_mutex
);
3553 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3555 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
3558 mutex_lock(&fs_info
->balance_mutex
);
3559 if (!fs_info
->balance_ctl
) {
3560 mutex_unlock(&fs_info
->balance_mutex
);
3564 atomic_inc(&fs_info
->balance_cancel_req
);
3566 * if we are running just wait and return, balance item is
3567 * deleted in btrfs_balance in this case
3569 if (atomic_read(&fs_info
->balance_running
)) {
3570 mutex_unlock(&fs_info
->balance_mutex
);
3571 wait_event(fs_info
->balance_wait_q
,
3572 atomic_read(&fs_info
->balance_running
) == 0);
3573 mutex_lock(&fs_info
->balance_mutex
);
3575 /* __cancel_balance needs volume_mutex */
3576 mutex_unlock(&fs_info
->balance_mutex
);
3577 mutex_lock(&fs_info
->volume_mutex
);
3578 mutex_lock(&fs_info
->balance_mutex
);
3580 if (fs_info
->balance_ctl
)
3581 __cancel_balance(fs_info
);
3583 mutex_unlock(&fs_info
->volume_mutex
);
3586 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3587 atomic_dec(&fs_info
->balance_cancel_req
);
3588 mutex_unlock(&fs_info
->balance_mutex
);
3592 static int btrfs_uuid_scan_kthread(void *data
)
3594 struct btrfs_fs_info
*fs_info
= data
;
3595 struct btrfs_root
*root
= fs_info
->tree_root
;
3596 struct btrfs_key key
;
3597 struct btrfs_key max_key
;
3598 struct btrfs_path
*path
= NULL
;
3600 struct extent_buffer
*eb
;
3602 struct btrfs_root_item root_item
;
3604 struct btrfs_trans_handle
*trans
= NULL
;
3606 path
= btrfs_alloc_path();
3613 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3616 max_key
.objectid
= (u64
)-1;
3617 max_key
.type
= BTRFS_ROOT_ITEM_KEY
;
3618 max_key
.offset
= (u64
)-1;
3620 path
->keep_locks
= 1;
3623 ret
= btrfs_search_forward(root
, &key
, path
, 0);
3630 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
||
3631 (key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
3632 key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) ||
3633 key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
3636 eb
= path
->nodes
[0];
3637 slot
= path
->slots
[0];
3638 item_size
= btrfs_item_size_nr(eb
, slot
);
3639 if (item_size
< sizeof(root_item
))
3642 read_extent_buffer(eb
, &root_item
,
3643 btrfs_item_ptr_offset(eb
, slot
),
3644 (int)sizeof(root_item
));
3645 if (btrfs_root_refs(&root_item
) == 0)
3648 if (!btrfs_is_empty_uuid(root_item
.uuid
) ||
3649 !btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3653 btrfs_release_path(path
);
3655 * 1 - subvol uuid item
3656 * 1 - received_subvol uuid item
3658 trans
= btrfs_start_transaction(fs_info
->uuid_root
, 2);
3659 if (IS_ERR(trans
)) {
3660 ret
= PTR_ERR(trans
);
3668 if (!btrfs_is_empty_uuid(root_item
.uuid
)) {
3669 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3671 BTRFS_UUID_KEY_SUBVOL
,
3674 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3680 if (!btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3681 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3682 root_item
.received_uuid
,
3683 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
3686 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3694 ret
= btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3700 btrfs_release_path(path
);
3701 if (key
.offset
< (u64
)-1) {
3703 } else if (key
.type
< BTRFS_ROOT_ITEM_KEY
) {
3705 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3706 } else if (key
.objectid
< (u64
)-1) {
3708 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3717 btrfs_free_path(path
);
3718 if (trans
&& !IS_ERR(trans
))
3719 btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3721 btrfs_warn(fs_info
, "btrfs_uuid_scan_kthread failed %d", ret
);
3723 fs_info
->update_uuid_tree_gen
= 1;
3724 up(&fs_info
->uuid_tree_rescan_sem
);
3729 * Callback for btrfs_uuid_tree_iterate().
3731 * 0 check succeeded, the entry is not outdated.
3732 * < 0 if an error occured.
3733 * > 0 if the check failed, which means the caller shall remove the entry.
3735 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info
*fs_info
,
3736 u8
*uuid
, u8 type
, u64 subid
)
3738 struct btrfs_key key
;
3740 struct btrfs_root
*subvol_root
;
3742 if (type
!= BTRFS_UUID_KEY_SUBVOL
&&
3743 type
!= BTRFS_UUID_KEY_RECEIVED_SUBVOL
)
3746 key
.objectid
= subid
;
3747 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3748 key
.offset
= (u64
)-1;
3749 subvol_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
3750 if (IS_ERR(subvol_root
)) {
3751 ret
= PTR_ERR(subvol_root
);
3758 case BTRFS_UUID_KEY_SUBVOL
:
3759 if (memcmp(uuid
, subvol_root
->root_item
.uuid
, BTRFS_UUID_SIZE
))
3762 case BTRFS_UUID_KEY_RECEIVED_SUBVOL
:
3763 if (memcmp(uuid
, subvol_root
->root_item
.received_uuid
,
3773 static int btrfs_uuid_rescan_kthread(void *data
)
3775 struct btrfs_fs_info
*fs_info
= (struct btrfs_fs_info
*)data
;
3779 * 1st step is to iterate through the existing UUID tree and
3780 * to delete all entries that contain outdated data.
3781 * 2nd step is to add all missing entries to the UUID tree.
3783 ret
= btrfs_uuid_tree_iterate(fs_info
, btrfs_check_uuid_tree_entry
);
3785 btrfs_warn(fs_info
, "iterating uuid_tree failed %d", ret
);
3786 up(&fs_info
->uuid_tree_rescan_sem
);
3789 return btrfs_uuid_scan_kthread(data
);
3792 int btrfs_create_uuid_tree(struct btrfs_fs_info
*fs_info
)
3794 struct btrfs_trans_handle
*trans
;
3795 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
3796 struct btrfs_root
*uuid_root
;
3797 struct task_struct
*task
;
3804 trans
= btrfs_start_transaction(tree_root
, 2);
3806 return PTR_ERR(trans
);
3808 uuid_root
= btrfs_create_tree(trans
, fs_info
,
3809 BTRFS_UUID_TREE_OBJECTID
);
3810 if (IS_ERR(uuid_root
)) {
3811 btrfs_abort_transaction(trans
, tree_root
,
3812 PTR_ERR(uuid_root
));
3813 return PTR_ERR(uuid_root
);
3816 fs_info
->uuid_root
= uuid_root
;
3818 ret
= btrfs_commit_transaction(trans
, tree_root
);
3822 down(&fs_info
->uuid_tree_rescan_sem
);
3823 task
= kthread_run(btrfs_uuid_scan_kthread
, fs_info
, "btrfs-uuid");
3825 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3826 btrfs_warn(fs_info
, "failed to start uuid_scan task");
3827 up(&fs_info
->uuid_tree_rescan_sem
);
3828 return PTR_ERR(task
);
3834 int btrfs_check_uuid_tree(struct btrfs_fs_info
*fs_info
)
3836 struct task_struct
*task
;
3838 down(&fs_info
->uuid_tree_rescan_sem
);
3839 task
= kthread_run(btrfs_uuid_rescan_kthread
, fs_info
, "btrfs-uuid");
3841 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3842 btrfs_warn(fs_info
, "failed to start uuid_rescan task");
3843 up(&fs_info
->uuid_tree_rescan_sem
);
3844 return PTR_ERR(task
);
3851 * shrinking a device means finding all of the device extents past
3852 * the new size, and then following the back refs to the chunks.
3853 * The chunk relocation code actually frees the device extent
3855 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3857 struct btrfs_trans_handle
*trans
;
3858 struct btrfs_root
*root
= device
->dev_root
;
3859 struct btrfs_dev_extent
*dev_extent
= NULL
;
3860 struct btrfs_path
*path
;
3868 bool retried
= false;
3869 struct extent_buffer
*l
;
3870 struct btrfs_key key
;
3871 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3872 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3873 u64 old_size
= device
->total_bytes
;
3874 u64 diff
= device
->total_bytes
- new_size
;
3876 if (device
->is_tgtdev_for_dev_replace
)
3879 path
= btrfs_alloc_path();
3887 device
->total_bytes
= new_size
;
3888 if (device
->writeable
) {
3889 device
->fs_devices
->total_rw_bytes
-= diff
;
3890 spin_lock(&root
->fs_info
->free_chunk_lock
);
3891 root
->fs_info
->free_chunk_space
-= diff
;
3892 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3894 unlock_chunks(root
);
3897 key
.objectid
= device
->devid
;
3898 key
.offset
= (u64
)-1;
3899 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3902 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3906 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3911 btrfs_release_path(path
);
3916 slot
= path
->slots
[0];
3917 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3919 if (key
.objectid
!= device
->devid
) {
3920 btrfs_release_path(path
);
3924 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3925 length
= btrfs_dev_extent_length(l
, dev_extent
);
3927 if (key
.offset
+ length
<= new_size
) {
3928 btrfs_release_path(path
);
3932 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3933 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3934 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3935 btrfs_release_path(path
);
3937 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3939 if (ret
&& ret
!= -ENOSPC
)
3943 } while (key
.offset
-- > 0);
3945 if (failed
&& !retried
) {
3949 } else if (failed
&& retried
) {
3953 device
->total_bytes
= old_size
;
3954 if (device
->writeable
)
3955 device
->fs_devices
->total_rw_bytes
+= diff
;
3956 spin_lock(&root
->fs_info
->free_chunk_lock
);
3957 root
->fs_info
->free_chunk_space
+= diff
;
3958 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3959 unlock_chunks(root
);
3963 /* Shrinking succeeded, else we would be at "done". */
3964 trans
= btrfs_start_transaction(root
, 0);
3965 if (IS_ERR(trans
)) {
3966 ret
= PTR_ERR(trans
);
3972 device
->disk_total_bytes
= new_size
;
3973 /* Now btrfs_update_device() will change the on-disk size. */
3974 ret
= btrfs_update_device(trans
, device
);
3976 unlock_chunks(root
);
3977 btrfs_end_transaction(trans
, root
);
3980 WARN_ON(diff
> old_total
);
3981 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3982 unlock_chunks(root
);
3983 btrfs_end_transaction(trans
, root
);
3985 btrfs_free_path(path
);
3989 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
3990 struct btrfs_key
*key
,
3991 struct btrfs_chunk
*chunk
, int item_size
)
3993 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3994 struct btrfs_disk_key disk_key
;
3998 array_size
= btrfs_super_sys_array_size(super_copy
);
3999 if (array_size
+ item_size
+ sizeof(disk_key
)
4000 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
4003 ptr
= super_copy
->sys_chunk_array
+ array_size
;
4004 btrfs_cpu_key_to_disk(&disk_key
, key
);
4005 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
4006 ptr
+= sizeof(disk_key
);
4007 memcpy(ptr
, chunk
, item_size
);
4008 item_size
+= sizeof(disk_key
);
4009 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
4014 * sort the devices in descending order by max_avail, total_avail
4016 static int btrfs_cmp_device_info(const void *a
, const void *b
)
4018 const struct btrfs_device_info
*di_a
= a
;
4019 const struct btrfs_device_info
*di_b
= b
;
4021 if (di_a
->max_avail
> di_b
->max_avail
)
4023 if (di_a
->max_avail
< di_b
->max_avail
)
4025 if (di_a
->total_avail
> di_b
->total_avail
)
4027 if (di_a
->total_avail
< di_b
->total_avail
)
4032 static struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
4033 [BTRFS_RAID_RAID10
] = {
4036 .devs_max
= 0, /* 0 == as many as possible */
4038 .devs_increment
= 2,
4041 [BTRFS_RAID_RAID1
] = {
4046 .devs_increment
= 2,
4049 [BTRFS_RAID_DUP
] = {
4054 .devs_increment
= 1,
4057 [BTRFS_RAID_RAID0
] = {
4062 .devs_increment
= 1,
4065 [BTRFS_RAID_SINGLE
] = {
4070 .devs_increment
= 1,
4073 [BTRFS_RAID_RAID5
] = {
4078 .devs_increment
= 1,
4081 [BTRFS_RAID_RAID6
] = {
4086 .devs_increment
= 1,
4091 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
4093 /* TODO allow them to set a preferred stripe size */
4097 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4099 if (!(type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)))
4102 btrfs_set_fs_incompat(info
, RAID56
);
4105 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4106 - sizeof(struct btrfs_item) \
4107 - sizeof(struct btrfs_chunk)) \
4108 / sizeof(struct btrfs_stripe) + 1)
4110 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4111 - 2 * sizeof(struct btrfs_disk_key) \
4112 - 2 * sizeof(struct btrfs_chunk)) \
4113 / sizeof(struct btrfs_stripe) + 1)
4115 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4116 struct btrfs_root
*extent_root
, u64 start
,
4119 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
4120 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
4121 struct list_head
*cur
;
4122 struct map_lookup
*map
= NULL
;
4123 struct extent_map_tree
*em_tree
;
4124 struct extent_map
*em
;
4125 struct btrfs_device_info
*devices_info
= NULL
;
4127 int num_stripes
; /* total number of stripes to allocate */
4128 int data_stripes
; /* number of stripes that count for
4130 int sub_stripes
; /* sub_stripes info for map */
4131 int dev_stripes
; /* stripes per dev */
4132 int devs_max
; /* max devs to use */
4133 int devs_min
; /* min devs needed */
4134 int devs_increment
; /* ndevs has to be a multiple of this */
4135 int ncopies
; /* how many copies to data has */
4137 u64 max_stripe_size
;
4141 u64 raid_stripe_len
= BTRFS_STRIPE_LEN
;
4147 BUG_ON(!alloc_profile_is_valid(type
, 0));
4149 if (list_empty(&fs_devices
->alloc_list
))
4152 index
= __get_raid_index(type
);
4154 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
4155 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
4156 devs_max
= btrfs_raid_array
[index
].devs_max
;
4157 devs_min
= btrfs_raid_array
[index
].devs_min
;
4158 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
4159 ncopies
= btrfs_raid_array
[index
].ncopies
;
4161 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
4162 max_stripe_size
= 1024 * 1024 * 1024;
4163 max_chunk_size
= 10 * max_stripe_size
;
4165 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4166 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
4167 /* for larger filesystems, use larger metadata chunks */
4168 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
4169 max_stripe_size
= 1024 * 1024 * 1024;
4171 max_stripe_size
= 256 * 1024 * 1024;
4172 max_chunk_size
= max_stripe_size
;
4174 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4175 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4176 max_stripe_size
= 32 * 1024 * 1024;
4177 max_chunk_size
= 2 * max_stripe_size
;
4179 devs_max
= BTRFS_MAX_DEVS_SYS_CHUNK
;
4181 btrfs_err(info
, "invalid chunk type 0x%llx requested",
4186 /* we don't want a chunk larger than 10% of writeable space */
4187 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
4190 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
4195 cur
= fs_devices
->alloc_list
.next
;
4198 * in the first pass through the devices list, we gather information
4199 * about the available holes on each device.
4202 while (cur
!= &fs_devices
->alloc_list
) {
4203 struct btrfs_device
*device
;
4207 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
4211 if (!device
->writeable
) {
4213 "BTRFS: read-only device in alloc_list\n");
4217 if (!device
->in_fs_metadata
||
4218 device
->is_tgtdev_for_dev_replace
)
4221 if (device
->total_bytes
> device
->bytes_used
)
4222 total_avail
= device
->total_bytes
- device
->bytes_used
;
4226 /* If there is no space on this device, skip it. */
4227 if (total_avail
== 0)
4230 ret
= find_free_dev_extent(trans
, device
,
4231 max_stripe_size
* dev_stripes
,
4232 &dev_offset
, &max_avail
);
4233 if (ret
&& ret
!= -ENOSPC
)
4237 max_avail
= max_stripe_size
* dev_stripes
;
4239 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
4242 if (ndevs
== fs_devices
->rw_devices
) {
4243 WARN(1, "%s: found more than %llu devices\n",
4244 __func__
, fs_devices
->rw_devices
);
4247 devices_info
[ndevs
].dev_offset
= dev_offset
;
4248 devices_info
[ndevs
].max_avail
= max_avail
;
4249 devices_info
[ndevs
].total_avail
= total_avail
;
4250 devices_info
[ndevs
].dev
= device
;
4255 * now sort the devices by hole size / available space
4257 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
4258 btrfs_cmp_device_info
, NULL
);
4260 /* round down to number of usable stripes */
4261 ndevs
-= ndevs
% devs_increment
;
4263 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
4268 if (devs_max
&& ndevs
> devs_max
)
4271 * the primary goal is to maximize the number of stripes, so use as many
4272 * devices as possible, even if the stripes are not maximum sized.
4274 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4275 num_stripes
= ndevs
* dev_stripes
;
4278 * this will have to be fixed for RAID1 and RAID10 over
4281 data_stripes
= num_stripes
/ ncopies
;
4283 if (type
& BTRFS_BLOCK_GROUP_RAID5
) {
4284 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 1,
4285 btrfs_super_stripesize(info
->super_copy
));
4286 data_stripes
= num_stripes
- 1;
4288 if (type
& BTRFS_BLOCK_GROUP_RAID6
) {
4289 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 2,
4290 btrfs_super_stripesize(info
->super_copy
));
4291 data_stripes
= num_stripes
- 2;
4295 * Use the number of data stripes to figure out how big this chunk
4296 * is really going to be in terms of logical address space,
4297 * and compare that answer with the max chunk size
4299 if (stripe_size
* data_stripes
> max_chunk_size
) {
4300 u64 mask
= (1ULL << 24) - 1;
4301 stripe_size
= max_chunk_size
;
4302 do_div(stripe_size
, data_stripes
);
4304 /* bump the answer up to a 16MB boundary */
4305 stripe_size
= (stripe_size
+ mask
) & ~mask
;
4307 /* but don't go higher than the limits we found
4308 * while searching for free extents
4310 if (stripe_size
> devices_info
[ndevs
-1].max_avail
)
4311 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4314 do_div(stripe_size
, dev_stripes
);
4316 /* align to BTRFS_STRIPE_LEN */
4317 do_div(stripe_size
, raid_stripe_len
);
4318 stripe_size
*= raid_stripe_len
;
4320 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4325 map
->num_stripes
= num_stripes
;
4327 for (i
= 0; i
< ndevs
; ++i
) {
4328 for (j
= 0; j
< dev_stripes
; ++j
) {
4329 int s
= i
* dev_stripes
+ j
;
4330 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
4331 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
4335 map
->sector_size
= extent_root
->sectorsize
;
4336 map
->stripe_len
= raid_stripe_len
;
4337 map
->io_align
= raid_stripe_len
;
4338 map
->io_width
= raid_stripe_len
;
4340 map
->sub_stripes
= sub_stripes
;
4342 num_bytes
= stripe_size
* data_stripes
;
4344 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
4346 em
= alloc_extent_map();
4352 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
4353 em
->bdev
= (struct block_device
*)map
;
4355 em
->len
= num_bytes
;
4356 em
->block_start
= 0;
4357 em
->block_len
= em
->len
;
4358 em
->orig_block_len
= stripe_size
;
4360 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4361 write_lock(&em_tree
->lock
);
4362 ret
= add_extent_mapping(em_tree
, em
, 0);
4364 list_add_tail(&em
->list
, &trans
->transaction
->pending_chunks
);
4365 atomic_inc(&em
->refs
);
4367 write_unlock(&em_tree
->lock
);
4369 free_extent_map(em
);
4373 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
4374 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4377 goto error_del_extent
;
4379 free_extent_map(em
);
4380 check_raid56_incompat_flag(extent_root
->fs_info
, type
);
4382 kfree(devices_info
);
4386 write_lock(&em_tree
->lock
);
4387 remove_extent_mapping(em_tree
, em
);
4388 write_unlock(&em_tree
->lock
);
4390 /* One for our allocation */
4391 free_extent_map(em
);
4392 /* One for the tree reference */
4393 free_extent_map(em
);
4395 kfree(devices_info
);
4399 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
4400 struct btrfs_root
*extent_root
,
4401 u64 chunk_offset
, u64 chunk_size
)
4403 struct btrfs_key key
;
4404 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
4405 struct btrfs_device
*device
;
4406 struct btrfs_chunk
*chunk
;
4407 struct btrfs_stripe
*stripe
;
4408 struct extent_map_tree
*em_tree
;
4409 struct extent_map
*em
;
4410 struct map_lookup
*map
;
4417 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4418 read_lock(&em_tree
->lock
);
4419 em
= lookup_extent_mapping(em_tree
, chunk_offset
, chunk_size
);
4420 read_unlock(&em_tree
->lock
);
4423 btrfs_crit(extent_root
->fs_info
, "unable to find logical "
4424 "%Lu len %Lu", chunk_offset
, chunk_size
);
4428 if (em
->start
!= chunk_offset
|| em
->len
!= chunk_size
) {
4429 btrfs_crit(extent_root
->fs_info
, "found a bad mapping, wanted"
4430 " %Lu-%Lu, found %Lu-%Lu", chunk_offset
,
4431 chunk_size
, em
->start
, em
->len
);
4432 free_extent_map(em
);
4436 map
= (struct map_lookup
*)em
->bdev
;
4437 item_size
= btrfs_chunk_item_size(map
->num_stripes
);
4438 stripe_size
= em
->orig_block_len
;
4440 chunk
= kzalloc(item_size
, GFP_NOFS
);
4446 for (i
= 0; i
< map
->num_stripes
; i
++) {
4447 device
= map
->stripes
[i
].dev
;
4448 dev_offset
= map
->stripes
[i
].physical
;
4450 device
->bytes_used
+= stripe_size
;
4451 ret
= btrfs_update_device(trans
, device
);
4454 ret
= btrfs_alloc_dev_extent(trans
, device
,
4455 chunk_root
->root_key
.objectid
,
4456 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4457 chunk_offset
, dev_offset
,
4463 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
4464 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
4466 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
4468 stripe
= &chunk
->stripe
;
4469 for (i
= 0; i
< map
->num_stripes
; i
++) {
4470 device
= map
->stripes
[i
].dev
;
4471 dev_offset
= map
->stripes
[i
].physical
;
4473 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
4474 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
4475 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
4479 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
4480 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
4481 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
4482 btrfs_set_stack_chunk_type(chunk
, map
->type
);
4483 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
4484 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
4485 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
4486 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
4487 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
4489 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
4490 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
4491 key
.offset
= chunk_offset
;
4493 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
4494 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4496 * TODO: Cleanup of inserted chunk root in case of
4499 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
4505 free_extent_map(em
);
4510 * Chunk allocation falls into two parts. The first part does works
4511 * that make the new allocated chunk useable, but not do any operation
4512 * that modifies the chunk tree. The second part does the works that
4513 * require modifying the chunk tree. This division is important for the
4514 * bootstrap process of adding storage to a seed btrfs.
4516 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4517 struct btrfs_root
*extent_root
, u64 type
)
4521 chunk_offset
= find_next_chunk(extent_root
->fs_info
);
4522 return __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
, type
);
4525 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
4526 struct btrfs_root
*root
,
4527 struct btrfs_device
*device
)
4530 u64 sys_chunk_offset
;
4532 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4533 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4536 chunk_offset
= find_next_chunk(fs_info
);
4537 alloc_profile
= btrfs_get_alloc_profile(extent_root
, 0);
4538 ret
= __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
,
4543 sys_chunk_offset
= find_next_chunk(root
->fs_info
);
4544 alloc_profile
= btrfs_get_alloc_profile(fs_info
->chunk_root
, 0);
4545 ret
= __btrfs_alloc_chunk(trans
, extent_root
, sys_chunk_offset
,
4548 btrfs_abort_transaction(trans
, root
, ret
);
4552 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
4554 btrfs_abort_transaction(trans
, root
, ret
);
4559 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
4561 struct extent_map
*em
;
4562 struct map_lookup
*map
;
4563 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4567 read_lock(&map_tree
->map_tree
.lock
);
4568 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
4569 read_unlock(&map_tree
->map_tree
.lock
);
4573 if (btrfs_test_opt(root
, DEGRADED
)) {
4574 free_extent_map(em
);
4578 map
= (struct map_lookup
*)em
->bdev
;
4579 for (i
= 0; i
< map
->num_stripes
; i
++) {
4580 if (!map
->stripes
[i
].dev
->writeable
) {
4585 free_extent_map(em
);
4589 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
4591 extent_map_tree_init(&tree
->map_tree
);
4594 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
4596 struct extent_map
*em
;
4599 write_lock(&tree
->map_tree
.lock
);
4600 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
4602 remove_extent_mapping(&tree
->map_tree
, em
);
4603 write_unlock(&tree
->map_tree
.lock
);
4607 free_extent_map(em
);
4608 /* once for the tree */
4609 free_extent_map(em
);
4613 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
4615 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4616 struct extent_map
*em
;
4617 struct map_lookup
*map
;
4618 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4621 read_lock(&em_tree
->lock
);
4622 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4623 read_unlock(&em_tree
->lock
);
4626 * We could return errors for these cases, but that could get ugly and
4627 * we'd probably do the same thing which is just not do anything else
4628 * and exit, so return 1 so the callers don't try to use other copies.
4631 btrfs_crit(fs_info
, "No mapping for %Lu-%Lu", logical
,
4636 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4637 btrfs_crit(fs_info
, "Invalid mapping for %Lu-%Lu, got "
4638 "%Lu-%Lu", logical
, logical
+len
, em
->start
,
4639 em
->start
+ em
->len
);
4640 free_extent_map(em
);
4644 map
= (struct map_lookup
*)em
->bdev
;
4645 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4646 ret
= map
->num_stripes
;
4647 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4648 ret
= map
->sub_stripes
;
4649 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
4651 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
4655 free_extent_map(em
);
4657 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
4658 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))
4660 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
4665 unsigned long btrfs_full_stripe_len(struct btrfs_root
*root
,
4666 struct btrfs_mapping_tree
*map_tree
,
4669 struct extent_map
*em
;
4670 struct map_lookup
*map
;
4671 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4672 unsigned long len
= root
->sectorsize
;
4674 read_lock(&em_tree
->lock
);
4675 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4676 read_unlock(&em_tree
->lock
);
4679 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4680 map
= (struct map_lookup
*)em
->bdev
;
4681 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4682 BTRFS_BLOCK_GROUP_RAID6
)) {
4683 len
= map
->stripe_len
* nr_data_stripes(map
);
4685 free_extent_map(em
);
4689 int btrfs_is_parity_mirror(struct btrfs_mapping_tree
*map_tree
,
4690 u64 logical
, u64 len
, int mirror_num
)
4692 struct extent_map
*em
;
4693 struct map_lookup
*map
;
4694 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4697 read_lock(&em_tree
->lock
);
4698 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4699 read_unlock(&em_tree
->lock
);
4702 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4703 map
= (struct map_lookup
*)em
->bdev
;
4704 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4705 BTRFS_BLOCK_GROUP_RAID6
))
4707 free_extent_map(em
);
4711 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
4712 struct map_lookup
*map
, int first
, int num
,
4713 int optimal
, int dev_replace_is_ongoing
)
4717 struct btrfs_device
*srcdev
;
4719 if (dev_replace_is_ongoing
&&
4720 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
4721 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
4722 srcdev
= fs_info
->dev_replace
.srcdev
;
4727 * try to avoid the drive that is the source drive for a
4728 * dev-replace procedure, only choose it if no other non-missing
4729 * mirror is available
4731 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
4732 if (map
->stripes
[optimal
].dev
->bdev
&&
4733 (tolerance
|| map
->stripes
[optimal
].dev
!= srcdev
))
4735 for (i
= first
; i
< first
+ num
; i
++) {
4736 if (map
->stripes
[i
].dev
->bdev
&&
4737 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
4742 /* we couldn't find one that doesn't fail. Just return something
4743 * and the io error handling code will clean up eventually
4748 static inline int parity_smaller(u64 a
, u64 b
)
4753 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4754 static void sort_parity_stripes(struct btrfs_bio
*bbio
, u64
*raid_map
)
4756 struct btrfs_bio_stripe s
;
4763 for (i
= 0; i
< bbio
->num_stripes
- 1; i
++) {
4764 if (parity_smaller(raid_map
[i
], raid_map
[i
+1])) {
4765 s
= bbio
->stripes
[i
];
4767 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
4768 raid_map
[i
] = raid_map
[i
+1];
4769 bbio
->stripes
[i
+1] = s
;
4777 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4778 u64 logical
, u64
*length
,
4779 struct btrfs_bio
**bbio_ret
,
4780 int mirror_num
, u64
**raid_map_ret
)
4782 struct extent_map
*em
;
4783 struct map_lookup
*map
;
4784 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4785 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4788 u64 stripe_end_offset
;
4793 u64
*raid_map
= NULL
;
4799 struct btrfs_bio
*bbio
= NULL
;
4800 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
4801 int dev_replace_is_ongoing
= 0;
4802 int num_alloc_stripes
;
4803 int patch_the_first_stripe_for_dev_replace
= 0;
4804 u64 physical_to_patch_in_first_stripe
= 0;
4805 u64 raid56_full_stripe_start
= (u64
)-1;
4807 read_lock(&em_tree
->lock
);
4808 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
4809 read_unlock(&em_tree
->lock
);
4812 btrfs_crit(fs_info
, "unable to find logical %llu len %llu",
4817 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4818 btrfs_crit(fs_info
, "found a bad mapping, wanted %Lu, "
4819 "found %Lu-%Lu", logical
, em
->start
,
4820 em
->start
+ em
->len
);
4821 free_extent_map(em
);
4825 map
= (struct map_lookup
*)em
->bdev
;
4826 offset
= logical
- em
->start
;
4828 stripe_len
= map
->stripe_len
;
4831 * stripe_nr counts the total number of stripes we have to stride
4832 * to get to this block
4834 do_div(stripe_nr
, stripe_len
);
4836 stripe_offset
= stripe_nr
* stripe_len
;
4837 BUG_ON(offset
< stripe_offset
);
4839 /* stripe_offset is the offset of this block in its stripe*/
4840 stripe_offset
= offset
- stripe_offset
;
4842 /* if we're here for raid56, we need to know the stripe aligned start */
4843 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4844 unsigned long full_stripe_len
= stripe_len
* nr_data_stripes(map
);
4845 raid56_full_stripe_start
= offset
;
4847 /* allow a write of a full stripe, but make sure we don't
4848 * allow straddling of stripes
4850 do_div(raid56_full_stripe_start
, full_stripe_len
);
4851 raid56_full_stripe_start
*= full_stripe_len
;
4854 if (rw
& REQ_DISCARD
) {
4855 /* we don't discard raid56 yet */
4857 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4861 *length
= min_t(u64
, em
->len
- offset
, *length
);
4862 } else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
4864 /* For writes to RAID[56], allow a full stripeset across all disks.
4865 For other RAID types and for RAID[56] reads, just allow a single
4866 stripe (on a single disk). */
4867 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
) &&
4869 max_len
= stripe_len
* nr_data_stripes(map
) -
4870 (offset
- raid56_full_stripe_start
);
4872 /* we limit the length of each bio to what fits in a stripe */
4873 max_len
= stripe_len
- stripe_offset
;
4875 *length
= min_t(u64
, em
->len
- offset
, max_len
);
4877 *length
= em
->len
- offset
;
4880 /* This is for when we're called from btrfs_merge_bio_hook() and all
4881 it cares about is the length */
4885 btrfs_dev_replace_lock(dev_replace
);
4886 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
4887 if (!dev_replace_is_ongoing
)
4888 btrfs_dev_replace_unlock(dev_replace
);
4890 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
4891 !(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) &&
4892 dev_replace
->tgtdev
!= NULL
) {
4894 * in dev-replace case, for repair case (that's the only
4895 * case where the mirror is selected explicitly when
4896 * calling btrfs_map_block), blocks left of the left cursor
4897 * can also be read from the target drive.
4898 * For REQ_GET_READ_MIRRORS, the target drive is added as
4899 * the last one to the array of stripes. For READ, it also
4900 * needs to be supported using the same mirror number.
4901 * If the requested block is not left of the left cursor,
4902 * EIO is returned. This can happen because btrfs_num_copies()
4903 * returns one more in the dev-replace case.
4905 u64 tmp_length
= *length
;
4906 struct btrfs_bio
*tmp_bbio
= NULL
;
4907 int tmp_num_stripes
;
4908 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4909 int index_srcdev
= 0;
4911 u64 physical_of_found
= 0;
4913 ret
= __btrfs_map_block(fs_info
, REQ_GET_READ_MIRRORS
,
4914 logical
, &tmp_length
, &tmp_bbio
, 0, NULL
);
4916 WARN_ON(tmp_bbio
!= NULL
);
4920 tmp_num_stripes
= tmp_bbio
->num_stripes
;
4921 if (mirror_num
> tmp_num_stripes
) {
4923 * REQ_GET_READ_MIRRORS does not contain this
4924 * mirror, that means that the requested area
4925 * is not left of the left cursor
4933 * process the rest of the function using the mirror_num
4934 * of the source drive. Therefore look it up first.
4935 * At the end, patch the device pointer to the one of the
4938 for (i
= 0; i
< tmp_num_stripes
; i
++) {
4939 if (tmp_bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4941 * In case of DUP, in order to keep it
4942 * simple, only add the mirror with the
4943 * lowest physical address
4946 physical_of_found
<=
4947 tmp_bbio
->stripes
[i
].physical
)
4952 tmp_bbio
->stripes
[i
].physical
;
4957 mirror_num
= index_srcdev
+ 1;
4958 patch_the_first_stripe_for_dev_replace
= 1;
4959 physical_to_patch_in_first_stripe
= physical_of_found
;
4968 } else if (mirror_num
> map
->num_stripes
) {
4974 stripe_nr_orig
= stripe_nr
;
4975 stripe_nr_end
= ALIGN(offset
+ *length
, map
->stripe_len
);
4976 do_div(stripe_nr_end
, map
->stripe_len
);
4977 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
4980 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
4981 if (rw
& REQ_DISCARD
)
4982 num_stripes
= min_t(u64
, map
->num_stripes
,
4983 stripe_nr_end
- stripe_nr_orig
);
4984 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
4985 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
4986 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
))
4987 num_stripes
= map
->num_stripes
;
4988 else if (mirror_num
)
4989 stripe_index
= mirror_num
- 1;
4991 stripe_index
= find_live_mirror(fs_info
, map
, 0,
4993 current
->pid
% map
->num_stripes
,
4994 dev_replace_is_ongoing
);
4995 mirror_num
= stripe_index
+ 1;
4998 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
4999 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) {
5000 num_stripes
= map
->num_stripes
;
5001 } else if (mirror_num
) {
5002 stripe_index
= mirror_num
- 1;
5007 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5008 int factor
= map
->num_stripes
/ map
->sub_stripes
;
5010 stripe_index
= do_div(stripe_nr
, factor
);
5011 stripe_index
*= map
->sub_stripes
;
5013 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
5014 num_stripes
= map
->sub_stripes
;
5015 else if (rw
& REQ_DISCARD
)
5016 num_stripes
= min_t(u64
, map
->sub_stripes
*
5017 (stripe_nr_end
- stripe_nr_orig
),
5019 else if (mirror_num
)
5020 stripe_index
+= mirror_num
- 1;
5022 int old_stripe_index
= stripe_index
;
5023 stripe_index
= find_live_mirror(fs_info
, map
,
5025 map
->sub_stripes
, stripe_index
+
5026 current
->pid
% map
->sub_stripes
,
5027 dev_replace_is_ongoing
);
5028 mirror_num
= stripe_index
- old_stripe_index
+ 1;
5031 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
5032 BTRFS_BLOCK_GROUP_RAID6
)) {
5035 if (bbio_ret
&& ((rw
& REQ_WRITE
) || mirror_num
> 1)
5039 /* push stripe_nr back to the start of the full stripe */
5040 stripe_nr
= raid56_full_stripe_start
;
5041 do_div(stripe_nr
, stripe_len
);
5043 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
5045 /* RAID[56] write or recovery. Return all stripes */
5046 num_stripes
= map
->num_stripes
;
5047 max_errors
= nr_parity_stripes(map
);
5049 raid_map
= kmalloc_array(num_stripes
, sizeof(u64
),
5056 /* Work out the disk rotation on this stripe-set */
5058 rot
= do_div(tmp
, num_stripes
);
5060 /* Fill in the logical address of each stripe */
5061 tmp
= stripe_nr
* nr_data_stripes(map
);
5062 for (i
= 0; i
< nr_data_stripes(map
); i
++)
5063 raid_map
[(i
+rot
) % num_stripes
] =
5064 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
5066 raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
5067 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5068 raid_map
[(i
+rot
+1) % num_stripes
] =
5071 *length
= map
->stripe_len
;
5076 * Mirror #0 or #1 means the original data block.
5077 * Mirror #2 is RAID5 parity block.
5078 * Mirror #3 is RAID6 Q block.
5080 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
5082 stripe_index
= nr_data_stripes(map
) +
5085 /* We distribute the parity blocks across stripes */
5086 tmp
= stripe_nr
+ stripe_index
;
5087 stripe_index
= do_div(tmp
, map
->num_stripes
);
5091 * after this do_div call, stripe_nr is the number of stripes
5092 * on this device we have to walk to find the data, and
5093 * stripe_index is the number of our device in the stripe array
5095 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
5096 mirror_num
= stripe_index
+ 1;
5098 BUG_ON(stripe_index
>= map
->num_stripes
);
5100 num_alloc_stripes
= num_stripes
;
5101 if (dev_replace_is_ongoing
) {
5102 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
5103 num_alloc_stripes
<<= 1;
5104 if (rw
& REQ_GET_READ_MIRRORS
)
5105 num_alloc_stripes
++;
5107 bbio
= kzalloc(btrfs_bio_size(num_alloc_stripes
), GFP_NOFS
);
5113 atomic_set(&bbio
->error
, 0);
5115 if (rw
& REQ_DISCARD
) {
5117 int sub_stripes
= 0;
5118 u64 stripes_per_dev
= 0;
5119 u32 remaining_stripes
= 0;
5120 u32 last_stripe
= 0;
5123 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
5124 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5127 sub_stripes
= map
->sub_stripes
;
5129 factor
= map
->num_stripes
/ sub_stripes
;
5130 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
5133 &remaining_stripes
);
5134 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
5135 last_stripe
*= sub_stripes
;
5138 for (i
= 0; i
< num_stripes
; i
++) {
5139 bbio
->stripes
[i
].physical
=
5140 map
->stripes
[stripe_index
].physical
+
5141 stripe_offset
+ stripe_nr
* map
->stripe_len
;
5142 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
5144 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5145 BTRFS_BLOCK_GROUP_RAID10
)) {
5146 bbio
->stripes
[i
].length
= stripes_per_dev
*
5149 if (i
/ sub_stripes
< remaining_stripes
)
5150 bbio
->stripes
[i
].length
+=
5154 * Special for the first stripe and
5157 * |-------|...|-------|
5161 if (i
< sub_stripes
)
5162 bbio
->stripes
[i
].length
-=
5165 if (stripe_index
>= last_stripe
&&
5166 stripe_index
<= (last_stripe
+
5168 bbio
->stripes
[i
].length
-=
5171 if (i
== sub_stripes
- 1)
5174 bbio
->stripes
[i
].length
= *length
;
5177 if (stripe_index
== map
->num_stripes
) {
5178 /* This could only happen for RAID0/10 */
5184 for (i
= 0; i
< num_stripes
; i
++) {
5185 bbio
->stripes
[i
].physical
=
5186 map
->stripes
[stripe_index
].physical
+
5188 stripe_nr
* map
->stripe_len
;
5189 bbio
->stripes
[i
].dev
=
5190 map
->stripes
[stripe_index
].dev
;
5195 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
)) {
5196 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
5197 BTRFS_BLOCK_GROUP_RAID10
|
5198 BTRFS_BLOCK_GROUP_RAID5
|
5199 BTRFS_BLOCK_GROUP_DUP
)) {
5201 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
) {
5206 if (dev_replace_is_ongoing
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
5207 dev_replace
->tgtdev
!= NULL
) {
5208 int index_where_to_add
;
5209 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5212 * duplicate the write operations while the dev replace
5213 * procedure is running. Since the copying of the old disk
5214 * to the new disk takes place at run time while the
5215 * filesystem is mounted writable, the regular write
5216 * operations to the old disk have to be duplicated to go
5217 * to the new disk as well.
5218 * Note that device->missing is handled by the caller, and
5219 * that the write to the old disk is already set up in the
5222 index_where_to_add
= num_stripes
;
5223 for (i
= 0; i
< num_stripes
; i
++) {
5224 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5225 /* write to new disk, too */
5226 struct btrfs_bio_stripe
*new =
5227 bbio
->stripes
+ index_where_to_add
;
5228 struct btrfs_bio_stripe
*old
=
5231 new->physical
= old
->physical
;
5232 new->length
= old
->length
;
5233 new->dev
= dev_replace
->tgtdev
;
5234 index_where_to_add
++;
5238 num_stripes
= index_where_to_add
;
5239 } else if (dev_replace_is_ongoing
&& (rw
& REQ_GET_READ_MIRRORS
) &&
5240 dev_replace
->tgtdev
!= NULL
) {
5241 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5242 int index_srcdev
= 0;
5244 u64 physical_of_found
= 0;
5247 * During the dev-replace procedure, the target drive can
5248 * also be used to read data in case it is needed to repair
5249 * a corrupt block elsewhere. This is possible if the
5250 * requested area is left of the left cursor. In this area,
5251 * the target drive is a full copy of the source drive.
5253 for (i
= 0; i
< num_stripes
; i
++) {
5254 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5256 * In case of DUP, in order to keep it
5257 * simple, only add the mirror with the
5258 * lowest physical address
5261 physical_of_found
<=
5262 bbio
->stripes
[i
].physical
)
5266 physical_of_found
= bbio
->stripes
[i
].physical
;
5270 u64 length
= map
->stripe_len
;
5272 if (physical_of_found
+ length
<=
5273 dev_replace
->cursor_left
) {
5274 struct btrfs_bio_stripe
*tgtdev_stripe
=
5275 bbio
->stripes
+ num_stripes
;
5277 tgtdev_stripe
->physical
= physical_of_found
;
5278 tgtdev_stripe
->length
=
5279 bbio
->stripes
[index_srcdev
].length
;
5280 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
5288 bbio
->num_stripes
= num_stripes
;
5289 bbio
->max_errors
= max_errors
;
5290 bbio
->mirror_num
= mirror_num
;
5293 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5294 * mirror_num == num_stripes + 1 && dev_replace target drive is
5295 * available as a mirror
5297 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
5298 WARN_ON(num_stripes
> 1);
5299 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
5300 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
5301 bbio
->mirror_num
= map
->num_stripes
+ 1;
5304 sort_parity_stripes(bbio
, raid_map
);
5305 *raid_map_ret
= raid_map
;
5308 if (dev_replace_is_ongoing
)
5309 btrfs_dev_replace_unlock(dev_replace
);
5310 free_extent_map(em
);
5314 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
5315 u64 logical
, u64
*length
,
5316 struct btrfs_bio
**bbio_ret
, int mirror_num
)
5318 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
5322 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
5323 u64 chunk_start
, u64 physical
, u64 devid
,
5324 u64
**logical
, int *naddrs
, int *stripe_len
)
5326 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
5327 struct extent_map
*em
;
5328 struct map_lookup
*map
;
5336 read_lock(&em_tree
->lock
);
5337 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
5338 read_unlock(&em_tree
->lock
);
5341 printk(KERN_ERR
"BTRFS: couldn't find em for chunk %Lu\n",
5346 if (em
->start
!= chunk_start
) {
5347 printk(KERN_ERR
"BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
5348 em
->start
, chunk_start
);
5349 free_extent_map(em
);
5352 map
= (struct map_lookup
*)em
->bdev
;
5355 rmap_len
= map
->stripe_len
;
5357 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5358 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
5359 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5360 do_div(length
, map
->num_stripes
);
5361 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
5362 BTRFS_BLOCK_GROUP_RAID6
)) {
5363 do_div(length
, nr_data_stripes(map
));
5364 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
5367 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
5368 BUG_ON(!buf
); /* -ENOMEM */
5370 for (i
= 0; i
< map
->num_stripes
; i
++) {
5371 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
5373 if (map
->stripes
[i
].physical
> physical
||
5374 map
->stripes
[i
].physical
+ length
<= physical
)
5377 stripe_nr
= physical
- map
->stripes
[i
].physical
;
5378 do_div(stripe_nr
, map
->stripe_len
);
5380 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5381 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5382 do_div(stripe_nr
, map
->sub_stripes
);
5383 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5384 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5385 } /* else if RAID[56], multiply by nr_data_stripes().
5386 * Alternatively, just use rmap_len below instead of
5387 * map->stripe_len */
5389 bytenr
= chunk_start
+ stripe_nr
* rmap_len
;
5390 WARN_ON(nr
>= map
->num_stripes
);
5391 for (j
= 0; j
< nr
; j
++) {
5392 if (buf
[j
] == bytenr
)
5396 WARN_ON(nr
>= map
->num_stripes
);
5403 *stripe_len
= rmap_len
;
5405 free_extent_map(em
);
5409 static inline void btrfs_end_bbio(struct btrfs_bio
*bbio
, struct bio
*bio
, int err
)
5411 if (likely(bbio
->flags
& BTRFS_BIO_ORIG_BIO_SUBMITTED
))
5412 bio_endio_nodec(bio
, err
);
5414 bio_endio(bio
, err
);
5418 static void btrfs_end_bio(struct bio
*bio
, int err
)
5420 struct btrfs_bio
*bbio
= bio
->bi_private
;
5421 struct btrfs_device
*dev
= bbio
->stripes
[0].dev
;
5422 int is_orig_bio
= 0;
5425 atomic_inc(&bbio
->error
);
5426 if (err
== -EIO
|| err
== -EREMOTEIO
) {
5427 unsigned int stripe_index
=
5428 btrfs_io_bio(bio
)->stripe_index
;
5430 BUG_ON(stripe_index
>= bbio
->num_stripes
);
5431 dev
= bbio
->stripes
[stripe_index
].dev
;
5433 if (bio
->bi_rw
& WRITE
)
5434 btrfs_dev_stat_inc(dev
,
5435 BTRFS_DEV_STAT_WRITE_ERRS
);
5437 btrfs_dev_stat_inc(dev
,
5438 BTRFS_DEV_STAT_READ_ERRS
);
5439 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
5440 btrfs_dev_stat_inc(dev
,
5441 BTRFS_DEV_STAT_FLUSH_ERRS
);
5442 btrfs_dev_stat_print_on_error(dev
);
5447 if (bio
== bbio
->orig_bio
)
5450 btrfs_bio_counter_dec(bbio
->fs_info
);
5452 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5455 bio
= bbio
->orig_bio
;
5458 bio
->bi_private
= bbio
->private;
5459 bio
->bi_end_io
= bbio
->end_io
;
5460 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5461 /* only send an error to the higher layers if it is
5462 * beyond the tolerance of the btrfs bio
5464 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
5468 * this bio is actually up to date, we didn't
5469 * go over the max number of errors
5471 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5475 btrfs_end_bbio(bbio
, bio
, err
);
5476 } else if (!is_orig_bio
) {
5482 * see run_scheduled_bios for a description of why bios are collected for
5485 * This will add one bio to the pending list for a device and make sure
5486 * the work struct is scheduled.
5488 static noinline
void btrfs_schedule_bio(struct btrfs_root
*root
,
5489 struct btrfs_device
*device
,
5490 int rw
, struct bio
*bio
)
5492 int should_queue
= 1;
5493 struct btrfs_pending_bios
*pending_bios
;
5495 if (device
->missing
|| !device
->bdev
) {
5496 bio_endio(bio
, -EIO
);
5500 /* don't bother with additional async steps for reads, right now */
5501 if (!(rw
& REQ_WRITE
)) {
5503 btrfsic_submit_bio(rw
, bio
);
5509 * nr_async_bios allows us to reliably return congestion to the
5510 * higher layers. Otherwise, the async bio makes it appear we have
5511 * made progress against dirty pages when we've really just put it
5512 * on a queue for later
5514 atomic_inc(&root
->fs_info
->nr_async_bios
);
5515 WARN_ON(bio
->bi_next
);
5516 bio
->bi_next
= NULL
;
5519 spin_lock(&device
->io_lock
);
5520 if (bio
->bi_rw
& REQ_SYNC
)
5521 pending_bios
= &device
->pending_sync_bios
;
5523 pending_bios
= &device
->pending_bios
;
5525 if (pending_bios
->tail
)
5526 pending_bios
->tail
->bi_next
= bio
;
5528 pending_bios
->tail
= bio
;
5529 if (!pending_bios
->head
)
5530 pending_bios
->head
= bio
;
5531 if (device
->running_pending
)
5534 spin_unlock(&device
->io_lock
);
5537 btrfs_queue_work(root
->fs_info
->submit_workers
,
5541 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
5544 struct bio_vec
*prev
;
5545 struct request_queue
*q
= bdev_get_queue(bdev
);
5546 unsigned int max_sectors
= queue_max_sectors(q
);
5547 struct bvec_merge_data bvm
= {
5549 .bi_sector
= sector
,
5550 .bi_rw
= bio
->bi_rw
,
5553 if (WARN_ON(bio
->bi_vcnt
== 0))
5556 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
5557 if (bio_sectors(bio
) > max_sectors
)
5560 if (!q
->merge_bvec_fn
)
5563 bvm
.bi_size
= bio
->bi_iter
.bi_size
- prev
->bv_len
;
5564 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
5569 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5570 struct bio
*bio
, u64 physical
, int dev_nr
,
5573 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
5575 bio
->bi_private
= bbio
;
5576 btrfs_io_bio(bio
)->stripe_index
= dev_nr
;
5577 bio
->bi_end_io
= btrfs_end_bio
;
5578 bio
->bi_iter
.bi_sector
= physical
>> 9;
5581 struct rcu_string
*name
;
5584 name
= rcu_dereference(dev
->name
);
5585 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5586 "(%s id %llu), size=%u\n", rw
,
5587 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
5588 name
->str
, dev
->devid
, bio
->bi_size
);
5592 bio
->bi_bdev
= dev
->bdev
;
5594 btrfs_bio_counter_inc_noblocked(root
->fs_info
);
5597 btrfs_schedule_bio(root
, dev
, rw
, bio
);
5599 btrfsic_submit_bio(rw
, bio
);
5602 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5603 struct bio
*first_bio
, struct btrfs_device
*dev
,
5604 int dev_nr
, int rw
, int async
)
5606 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
5608 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
5609 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
5612 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
5616 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
5617 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5618 bvec
->bv_offset
) < bvec
->bv_len
) {
5619 u64 len
= bio
->bi_iter
.bi_size
;
5621 atomic_inc(&bbio
->stripes_pending
);
5622 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
5630 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
5634 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
5636 atomic_inc(&bbio
->error
);
5637 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5638 /* Shoud be the original bio. */
5639 WARN_ON(bio
!= bbio
->orig_bio
);
5641 bio
->bi_private
= bbio
->private;
5642 bio
->bi_end_io
= bbio
->end_io
;
5643 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5644 bio
->bi_iter
.bi_sector
= logical
>> 9;
5646 btrfs_end_bbio(bbio
, bio
, -EIO
);
5650 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
5651 int mirror_num
, int async_submit
)
5653 struct btrfs_device
*dev
;
5654 struct bio
*first_bio
= bio
;
5655 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
5658 u64
*raid_map
= NULL
;
5662 struct btrfs_bio
*bbio
= NULL
;
5664 length
= bio
->bi_iter
.bi_size
;
5665 map_length
= length
;
5667 btrfs_bio_counter_inc_blocked(root
->fs_info
);
5668 ret
= __btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
5669 mirror_num
, &raid_map
);
5671 btrfs_bio_counter_dec(root
->fs_info
);
5675 total_devs
= bbio
->num_stripes
;
5676 bbio
->orig_bio
= first_bio
;
5677 bbio
->private = first_bio
->bi_private
;
5678 bbio
->end_io
= first_bio
->bi_end_io
;
5679 bbio
->fs_info
= root
->fs_info
;
5680 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
5683 /* In this case, map_length has been set to the length of
5684 a single stripe; not the whole write */
5686 ret
= raid56_parity_write(root
, bio
, bbio
,
5687 raid_map
, map_length
);
5689 ret
= raid56_parity_recover(root
, bio
, bbio
,
5690 raid_map
, map_length
,
5694 * FIXME, replace dosen't support raid56 yet, please fix
5697 btrfs_bio_counter_dec(root
->fs_info
);
5701 if (map_length
< length
) {
5702 btrfs_crit(root
->fs_info
, "mapping failed logical %llu bio len %llu len %llu",
5703 logical
, length
, map_length
);
5707 while (dev_nr
< total_devs
) {
5708 dev
= bbio
->stripes
[dev_nr
].dev
;
5709 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
5710 bbio_error(bbio
, first_bio
, logical
);
5716 * Check and see if we're ok with this bio based on it's size
5717 * and offset with the given device.
5719 if (!bio_size_ok(dev
->bdev
, first_bio
,
5720 bbio
->stripes
[dev_nr
].physical
>> 9)) {
5721 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
5722 dev_nr
, rw
, async_submit
);
5728 if (dev_nr
< total_devs
- 1) {
5729 bio
= btrfs_bio_clone(first_bio
, GFP_NOFS
);
5730 BUG_ON(!bio
); /* -ENOMEM */
5733 bbio
->flags
|= BTRFS_BIO_ORIG_BIO_SUBMITTED
;
5736 submit_stripe_bio(root
, bbio
, bio
,
5737 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
5741 btrfs_bio_counter_dec(root
->fs_info
);
5745 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
5748 struct btrfs_device
*device
;
5749 struct btrfs_fs_devices
*cur_devices
;
5751 cur_devices
= fs_info
->fs_devices
;
5752 while (cur_devices
) {
5754 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5755 device
= __find_device(&cur_devices
->devices
,
5760 cur_devices
= cur_devices
->seed
;
5765 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
5766 u64 devid
, u8
*dev_uuid
)
5768 struct btrfs_device
*device
;
5769 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5771 device
= btrfs_alloc_device(NULL
, &devid
, dev_uuid
);
5775 list_add(&device
->dev_list
, &fs_devices
->devices
);
5776 device
->fs_devices
= fs_devices
;
5777 fs_devices
->num_devices
++;
5779 device
->missing
= 1;
5780 fs_devices
->missing_devices
++;
5786 * btrfs_alloc_device - allocate struct btrfs_device
5787 * @fs_info: used only for generating a new devid, can be NULL if
5788 * devid is provided (i.e. @devid != NULL).
5789 * @devid: a pointer to devid for this device. If NULL a new devid
5791 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5794 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5795 * on error. Returned struct is not linked onto any lists and can be
5796 * destroyed with kfree() right away.
5798 struct btrfs_device
*btrfs_alloc_device(struct btrfs_fs_info
*fs_info
,
5802 struct btrfs_device
*dev
;
5805 if (WARN_ON(!devid
&& !fs_info
))
5806 return ERR_PTR(-EINVAL
);
5808 dev
= __alloc_device();
5817 ret
= find_next_devid(fs_info
, &tmp
);
5820 return ERR_PTR(ret
);
5826 memcpy(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
);
5828 generate_random_uuid(dev
->uuid
);
5830 btrfs_init_work(&dev
->work
, pending_bios_fn
, NULL
, NULL
);
5835 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
5836 struct extent_buffer
*leaf
,
5837 struct btrfs_chunk
*chunk
)
5839 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5840 struct map_lookup
*map
;
5841 struct extent_map
*em
;
5845 u8 uuid
[BTRFS_UUID_SIZE
];
5850 logical
= key
->offset
;
5851 length
= btrfs_chunk_length(leaf
, chunk
);
5853 read_lock(&map_tree
->map_tree
.lock
);
5854 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
5855 read_unlock(&map_tree
->map_tree
.lock
);
5857 /* already mapped? */
5858 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
5859 free_extent_map(em
);
5862 free_extent_map(em
);
5865 em
= alloc_extent_map();
5868 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
5869 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
5871 free_extent_map(em
);
5875 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
5876 em
->bdev
= (struct block_device
*)map
;
5877 em
->start
= logical
;
5880 em
->block_start
= 0;
5881 em
->block_len
= em
->len
;
5883 map
->num_stripes
= num_stripes
;
5884 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
5885 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
5886 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
5887 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
5888 map
->type
= btrfs_chunk_type(leaf
, chunk
);
5889 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
5890 for (i
= 0; i
< num_stripes
; i
++) {
5891 map
->stripes
[i
].physical
=
5892 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
5893 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
5894 read_extent_buffer(leaf
, uuid
, (unsigned long)
5895 btrfs_stripe_dev_uuid_nr(chunk
, i
),
5897 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
5899 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
5900 free_extent_map(em
);
5903 if (!map
->stripes
[i
].dev
) {
5904 map
->stripes
[i
].dev
=
5905 add_missing_dev(root
, devid
, uuid
);
5906 if (!map
->stripes
[i
].dev
) {
5907 free_extent_map(em
);
5911 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
5914 write_lock(&map_tree
->map_tree
.lock
);
5915 ret
= add_extent_mapping(&map_tree
->map_tree
, em
, 0);
5916 write_unlock(&map_tree
->map_tree
.lock
);
5917 BUG_ON(ret
); /* Tree corruption */
5918 free_extent_map(em
);
5923 static void fill_device_from_item(struct extent_buffer
*leaf
,
5924 struct btrfs_dev_item
*dev_item
,
5925 struct btrfs_device
*device
)
5929 device
->devid
= btrfs_device_id(leaf
, dev_item
);
5930 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
5931 device
->total_bytes
= device
->disk_total_bytes
;
5932 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
5933 device
->type
= btrfs_device_type(leaf
, dev_item
);
5934 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
5935 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
5936 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
5937 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
5938 device
->is_tgtdev_for_dev_replace
= 0;
5940 ptr
= btrfs_device_uuid(dev_item
);
5941 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
5944 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
5946 struct btrfs_fs_devices
*fs_devices
;
5949 BUG_ON(!mutex_is_locked(&uuid_mutex
));
5951 fs_devices
= root
->fs_info
->fs_devices
->seed
;
5952 while (fs_devices
) {
5953 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5957 fs_devices
= fs_devices
->seed
;
5960 fs_devices
= find_fsid(fsid
);
5966 fs_devices
= clone_fs_devices(fs_devices
);
5967 if (IS_ERR(fs_devices
)) {
5968 ret
= PTR_ERR(fs_devices
);
5972 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
5973 root
->fs_info
->bdev_holder
);
5975 free_fs_devices(fs_devices
);
5979 if (!fs_devices
->seeding
) {
5980 __btrfs_close_devices(fs_devices
);
5981 free_fs_devices(fs_devices
);
5986 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
5987 root
->fs_info
->fs_devices
->seed
= fs_devices
;
5992 static int read_one_dev(struct btrfs_root
*root
,
5993 struct extent_buffer
*leaf
,
5994 struct btrfs_dev_item
*dev_item
)
5996 struct btrfs_device
*device
;
5999 u8 fs_uuid
[BTRFS_UUID_SIZE
];
6000 u8 dev_uuid
[BTRFS_UUID_SIZE
];
6002 devid
= btrfs_device_id(leaf
, dev_item
);
6003 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
6005 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
6008 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
6009 ret
= open_seed_devices(root
, fs_uuid
);
6010 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
6014 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
6015 if (!device
|| !device
->bdev
) {
6016 if (!btrfs_test_opt(root
, DEGRADED
))
6020 btrfs_warn(root
->fs_info
, "devid %llu missing", devid
);
6021 device
= add_missing_dev(root
, devid
, dev_uuid
);
6024 } else if (!device
->missing
) {
6026 * this happens when a device that was properly setup
6027 * in the device info lists suddenly goes bad.
6028 * device->bdev is NULL, and so we have to set
6029 * device->missing to one here
6031 root
->fs_info
->fs_devices
->missing_devices
++;
6032 device
->missing
= 1;
6036 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
6037 BUG_ON(device
->writeable
);
6038 if (device
->generation
!=
6039 btrfs_device_generation(leaf
, dev_item
))
6043 fill_device_from_item(leaf
, dev_item
, device
);
6044 device
->in_fs_metadata
= 1;
6045 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
6046 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
6047 spin_lock(&root
->fs_info
->free_chunk_lock
);
6048 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
6050 spin_unlock(&root
->fs_info
->free_chunk_lock
);
6056 int btrfs_read_sys_array(struct btrfs_root
*root
)
6058 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
6059 struct extent_buffer
*sb
;
6060 struct btrfs_disk_key
*disk_key
;
6061 struct btrfs_chunk
*chunk
;
6063 unsigned long sb_ptr
;
6069 struct btrfs_key key
;
6071 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
6072 BTRFS_SUPER_INFO_SIZE
);
6075 btrfs_set_buffer_uptodate(sb
);
6076 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
6078 * The sb extent buffer is artifical and just used to read the system array.
6079 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6080 * pages up-to-date when the page is larger: extent does not cover the
6081 * whole page and consequently check_page_uptodate does not find all
6082 * the page's extents up-to-date (the hole beyond sb),
6083 * write_extent_buffer then triggers a WARN_ON.
6085 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6086 * but sb spans only this function. Add an explicit SetPageUptodate call
6087 * to silence the warning eg. on PowerPC 64.
6089 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
6090 SetPageUptodate(sb
->pages
[0]);
6092 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
6093 array_size
= btrfs_super_sys_array_size(super_copy
);
6095 ptr
= super_copy
->sys_chunk_array
;
6096 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
6099 while (cur
< array_size
) {
6100 disk_key
= (struct btrfs_disk_key
*)ptr
;
6101 btrfs_disk_key_to_cpu(&key
, disk_key
);
6103 len
= sizeof(*disk_key
); ptr
+= len
;
6107 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6108 chunk
= (struct btrfs_chunk
*)sb_ptr
;
6109 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
6112 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
6113 len
= btrfs_chunk_item_size(num_stripes
);
6122 free_extent_buffer(sb
);
6126 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
6128 struct btrfs_path
*path
;
6129 struct extent_buffer
*leaf
;
6130 struct btrfs_key key
;
6131 struct btrfs_key found_key
;
6135 root
= root
->fs_info
->chunk_root
;
6137 path
= btrfs_alloc_path();
6141 mutex_lock(&uuid_mutex
);
6145 * Read all device items, and then all the chunk items. All
6146 * device items are found before any chunk item (their object id
6147 * is smaller than the lowest possible object id for a chunk
6148 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6150 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
6153 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6157 leaf
= path
->nodes
[0];
6158 slot
= path
->slots
[0];
6159 if (slot
>= btrfs_header_nritems(leaf
)) {
6160 ret
= btrfs_next_leaf(root
, path
);
6167 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6168 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
6169 struct btrfs_dev_item
*dev_item
;
6170 dev_item
= btrfs_item_ptr(leaf
, slot
,
6171 struct btrfs_dev_item
);
6172 ret
= read_one_dev(root
, leaf
, dev_item
);
6175 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6176 struct btrfs_chunk
*chunk
;
6177 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
6178 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
6186 unlock_chunks(root
);
6187 mutex_unlock(&uuid_mutex
);
6189 btrfs_free_path(path
);
6193 void btrfs_init_devices_late(struct btrfs_fs_info
*fs_info
)
6195 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6196 struct btrfs_device
*device
;
6198 while (fs_devices
) {
6199 mutex_lock(&fs_devices
->device_list_mutex
);
6200 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
6201 device
->dev_root
= fs_info
->dev_root
;
6202 mutex_unlock(&fs_devices
->device_list_mutex
);
6204 fs_devices
= fs_devices
->seed
;
6208 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
6212 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6213 btrfs_dev_stat_reset(dev
, i
);
6216 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
6218 struct btrfs_key key
;
6219 struct btrfs_key found_key
;
6220 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6221 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6222 struct extent_buffer
*eb
;
6225 struct btrfs_device
*device
;
6226 struct btrfs_path
*path
= NULL
;
6229 path
= btrfs_alloc_path();
6235 mutex_lock(&fs_devices
->device_list_mutex
);
6236 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6238 struct btrfs_dev_stats_item
*ptr
;
6241 key
.type
= BTRFS_DEV_STATS_KEY
;
6242 key
.offset
= device
->devid
;
6243 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
6245 __btrfs_reset_dev_stats(device
);
6246 device
->dev_stats_valid
= 1;
6247 btrfs_release_path(path
);
6250 slot
= path
->slots
[0];
6251 eb
= path
->nodes
[0];
6252 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
6253 item_size
= btrfs_item_size_nr(eb
, slot
);
6255 ptr
= btrfs_item_ptr(eb
, slot
,
6256 struct btrfs_dev_stats_item
);
6258 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6259 if (item_size
>= (1 + i
) * sizeof(__le64
))
6260 btrfs_dev_stat_set(device
, i
,
6261 btrfs_dev_stats_value(eb
, ptr
, i
));
6263 btrfs_dev_stat_reset(device
, i
);
6266 device
->dev_stats_valid
= 1;
6267 btrfs_dev_stat_print_on_load(device
);
6268 btrfs_release_path(path
);
6270 mutex_unlock(&fs_devices
->device_list_mutex
);
6273 btrfs_free_path(path
);
6274 return ret
< 0 ? ret
: 0;
6277 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
6278 struct btrfs_root
*dev_root
,
6279 struct btrfs_device
*device
)
6281 struct btrfs_path
*path
;
6282 struct btrfs_key key
;
6283 struct extent_buffer
*eb
;
6284 struct btrfs_dev_stats_item
*ptr
;
6289 key
.type
= BTRFS_DEV_STATS_KEY
;
6290 key
.offset
= device
->devid
;
6292 path
= btrfs_alloc_path();
6294 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
6296 printk_in_rcu(KERN_WARNING
"BTRFS: "
6297 "error %d while searching for dev_stats item for device %s!\n",
6298 ret
, rcu_str_deref(device
->name
));
6303 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
6304 /* need to delete old one and insert a new one */
6305 ret
= btrfs_del_item(trans
, dev_root
, path
);
6307 printk_in_rcu(KERN_WARNING
"BTRFS: "
6308 "delete too small dev_stats item for device %s failed %d!\n",
6309 rcu_str_deref(device
->name
), ret
);
6316 /* need to insert a new item */
6317 btrfs_release_path(path
);
6318 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
6319 &key
, sizeof(*ptr
));
6321 printk_in_rcu(KERN_WARNING
"BTRFS: "
6322 "insert dev_stats item for device %s failed %d!\n",
6323 rcu_str_deref(device
->name
), ret
);
6328 eb
= path
->nodes
[0];
6329 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
6330 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6331 btrfs_set_dev_stats_value(eb
, ptr
, i
,
6332 btrfs_dev_stat_read(device
, i
));
6333 btrfs_mark_buffer_dirty(eb
);
6336 btrfs_free_path(path
);
6341 * called from commit_transaction. Writes all changed device stats to disk.
6343 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
6344 struct btrfs_fs_info
*fs_info
)
6346 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6347 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6348 struct btrfs_device
*device
;
6351 mutex_lock(&fs_devices
->device_list_mutex
);
6352 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6353 if (!device
->dev_stats_valid
|| !device
->dev_stats_dirty
)
6356 ret
= update_dev_stat_item(trans
, dev_root
, device
);
6358 device
->dev_stats_dirty
= 0;
6360 mutex_unlock(&fs_devices
->device_list_mutex
);
6365 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
6367 btrfs_dev_stat_inc(dev
, index
);
6368 btrfs_dev_stat_print_on_error(dev
);
6371 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
6373 if (!dev
->dev_stats_valid
)
6375 printk_ratelimited_in_rcu(KERN_ERR
"BTRFS: "
6376 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6377 rcu_str_deref(dev
->name
),
6378 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6379 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6380 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6381 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6382 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6385 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
6389 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6390 if (btrfs_dev_stat_read(dev
, i
) != 0)
6392 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
6393 return; /* all values == 0, suppress message */
6395 printk_in_rcu(KERN_INFO
"BTRFS: "
6396 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6397 rcu_str_deref(dev
->name
),
6398 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6399 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6400 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6401 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6402 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6405 int btrfs_get_dev_stats(struct btrfs_root
*root
,
6406 struct btrfs_ioctl_get_dev_stats
*stats
)
6408 struct btrfs_device
*dev
;
6409 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
6412 mutex_lock(&fs_devices
->device_list_mutex
);
6413 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
6414 mutex_unlock(&fs_devices
->device_list_mutex
);
6417 btrfs_warn(root
->fs_info
, "get dev_stats failed, device not found");
6419 } else if (!dev
->dev_stats_valid
) {
6420 btrfs_warn(root
->fs_info
, "get dev_stats failed, not yet valid");
6422 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
6423 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6424 if (stats
->nr_items
> i
)
6426 btrfs_dev_stat_read_and_reset(dev
, i
);
6428 btrfs_dev_stat_reset(dev
, i
);
6431 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6432 if (stats
->nr_items
> i
)
6433 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
6435 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
6436 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
6440 int btrfs_scratch_superblock(struct btrfs_device
*device
)
6442 struct buffer_head
*bh
;
6443 struct btrfs_super_block
*disk_super
;
6445 bh
= btrfs_read_dev_super(device
->bdev
);
6448 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
6450 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
6451 set_buffer_dirty(bh
);
6452 sync_dirty_buffer(bh
);