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 atomic_set(&dev
->dev_stats_ccnt
, 0);
163 INIT_RADIX_TREE(&dev
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
164 INIT_RADIX_TREE(&dev
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
169 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
172 struct btrfs_device
*dev
;
174 list_for_each_entry(dev
, head
, dev_list
) {
175 if (dev
->devid
== devid
&&
176 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
183 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
185 struct btrfs_fs_devices
*fs_devices
;
187 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
188 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
195 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
196 int flush
, struct block_device
**bdev
,
197 struct buffer_head
**bh
)
201 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
204 ret
= PTR_ERR(*bdev
);
205 printk(KERN_INFO
"BTRFS: open %s failed\n", device_path
);
210 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
211 ret
= set_blocksize(*bdev
, 4096);
213 blkdev_put(*bdev
, flags
);
216 invalidate_bdev(*bdev
);
217 *bh
= btrfs_read_dev_super(*bdev
);
220 blkdev_put(*bdev
, flags
);
232 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
233 struct bio
*head
, struct bio
*tail
)
236 struct bio
*old_head
;
238 old_head
= pending_bios
->head
;
239 pending_bios
->head
= head
;
240 if (pending_bios
->tail
)
241 tail
->bi_next
= old_head
;
243 pending_bios
->tail
= tail
;
247 * we try to collect pending bios for a device so we don't get a large
248 * number of procs sending bios down to the same device. This greatly
249 * improves the schedulers ability to collect and merge the bios.
251 * But, it also turns into a long list of bios to process and that is sure
252 * to eventually make the worker thread block. The solution here is to
253 * make some progress and then put this work struct back at the end of
254 * the list if the block device is congested. This way, multiple devices
255 * can make progress from a single worker thread.
257 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
260 struct backing_dev_info
*bdi
;
261 struct btrfs_fs_info
*fs_info
;
262 struct btrfs_pending_bios
*pending_bios
;
266 unsigned long num_run
;
267 unsigned long batch_run
= 0;
269 unsigned long last_waited
= 0;
271 int sync_pending
= 0;
272 struct blk_plug plug
;
275 * this function runs all the bios we've collected for
276 * a particular device. We don't want to wander off to
277 * another device without first sending all of these down.
278 * So, setup a plug here and finish it off before we return
280 blk_start_plug(&plug
);
282 bdi
= blk_get_backing_dev_info(device
->bdev
);
283 fs_info
= device
->dev_root
->fs_info
;
284 limit
= btrfs_async_submit_limit(fs_info
);
285 limit
= limit
* 2 / 3;
288 spin_lock(&device
->io_lock
);
293 /* take all the bios off the list at once and process them
294 * later on (without the lock held). But, remember the
295 * tail and other pointers so the bios can be properly reinserted
296 * into the list if we hit congestion
298 if (!force_reg
&& device
->pending_sync_bios
.head
) {
299 pending_bios
= &device
->pending_sync_bios
;
302 pending_bios
= &device
->pending_bios
;
306 pending
= pending_bios
->head
;
307 tail
= pending_bios
->tail
;
308 WARN_ON(pending
&& !tail
);
311 * if pending was null this time around, no bios need processing
312 * at all and we can stop. Otherwise it'll loop back up again
313 * and do an additional check so no bios are missed.
315 * device->running_pending is used to synchronize with the
318 if (device
->pending_sync_bios
.head
== NULL
&&
319 device
->pending_bios
.head
== NULL
) {
321 device
->running_pending
= 0;
324 device
->running_pending
= 1;
327 pending_bios
->head
= NULL
;
328 pending_bios
->tail
= NULL
;
330 spin_unlock(&device
->io_lock
);
335 /* we want to work on both lists, but do more bios on the
336 * sync list than the regular list
339 pending_bios
!= &device
->pending_sync_bios
&&
340 device
->pending_sync_bios
.head
) ||
341 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
342 device
->pending_bios
.head
)) {
343 spin_lock(&device
->io_lock
);
344 requeue_list(pending_bios
, pending
, tail
);
349 pending
= pending
->bi_next
;
352 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
353 waitqueue_active(&fs_info
->async_submit_wait
))
354 wake_up(&fs_info
->async_submit_wait
);
356 BUG_ON(atomic_read(&cur
->bi_cnt
) == 0);
359 * if we're doing the sync list, record that our
360 * plug has some sync requests on it
362 * If we're doing the regular list and there are
363 * sync requests sitting around, unplug before
366 if (pending_bios
== &device
->pending_sync_bios
) {
368 } else if (sync_pending
) {
369 blk_finish_plug(&plug
);
370 blk_start_plug(&plug
);
374 btrfsic_submit_bio(cur
->bi_rw
, cur
);
381 * we made progress, there is more work to do and the bdi
382 * is now congested. Back off and let other work structs
385 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
386 fs_info
->fs_devices
->open_devices
> 1) {
387 struct io_context
*ioc
;
389 ioc
= current
->io_context
;
392 * the main goal here is that we don't want to
393 * block if we're going to be able to submit
394 * more requests without blocking.
396 * This code does two great things, it pokes into
397 * the elevator code from a filesystem _and_
398 * it makes assumptions about how batching works.
400 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
401 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
403 ioc
->last_waited
== last_waited
)) {
405 * we want to go through our batch of
406 * requests and stop. So, we copy out
407 * the ioc->last_waited time and test
408 * against it before looping
410 last_waited
= ioc
->last_waited
;
415 spin_lock(&device
->io_lock
);
416 requeue_list(pending_bios
, pending
, tail
);
417 device
->running_pending
= 1;
419 spin_unlock(&device
->io_lock
);
420 btrfs_queue_work(fs_info
->submit_workers
,
424 /* unplug every 64 requests just for good measure */
425 if (batch_run
% 64 == 0) {
426 blk_finish_plug(&plug
);
427 blk_start_plug(&plug
);
436 spin_lock(&device
->io_lock
);
437 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
439 spin_unlock(&device
->io_lock
);
442 blk_finish_plug(&plug
);
445 static void pending_bios_fn(struct btrfs_work
*work
)
447 struct btrfs_device
*device
;
449 device
= container_of(work
, struct btrfs_device
, work
);
450 run_scheduled_bios(device
);
454 * Add new device to list of registered devices
457 * 1 - first time device is seen
458 * 0 - device already known
461 static noinline
int device_list_add(const char *path
,
462 struct btrfs_super_block
*disk_super
,
463 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
465 struct btrfs_device
*device
;
466 struct btrfs_fs_devices
*fs_devices
;
467 struct rcu_string
*name
;
469 u64 found_transid
= btrfs_super_generation(disk_super
);
471 fs_devices
= find_fsid(disk_super
->fsid
);
473 fs_devices
= alloc_fs_devices(disk_super
->fsid
);
474 if (IS_ERR(fs_devices
))
475 return PTR_ERR(fs_devices
);
477 list_add(&fs_devices
->list
, &fs_uuids
);
481 device
= __find_device(&fs_devices
->devices
, devid
,
482 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
) {
539 * That is if the FS is _not_ mounted and if you
540 * are here, that means there is more than one
541 * disk with same uuid and devid.We keep the one
542 * with larger generation number or the last-in if
543 * generation are equal.
545 if (found_transid
< device
->generation
)
549 name
= rcu_string_strdup(path
, GFP_NOFS
);
552 rcu_string_free(device
->name
);
553 rcu_assign_pointer(device
->name
, name
);
554 if (device
->missing
) {
555 fs_devices
->missing_devices
--;
561 * Unmount does not free the btrfs_device struct but would zero
562 * generation along with most of the other members. So just update
563 * it back. We need it to pick the disk with largest generation
566 if (!fs_devices
->opened
)
567 device
->generation
= found_transid
;
569 *fs_devices_ret
= fs_devices
;
574 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
576 struct btrfs_fs_devices
*fs_devices
;
577 struct btrfs_device
*device
;
578 struct btrfs_device
*orig_dev
;
580 fs_devices
= alloc_fs_devices(orig
->fsid
);
581 if (IS_ERR(fs_devices
))
584 fs_devices
->total_devices
= orig
->total_devices
;
586 /* We have held the volume lock, it is safe to get the devices. */
587 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
588 struct rcu_string
*name
;
590 device
= btrfs_alloc_device(NULL
, &orig_dev
->devid
,
596 * This is ok to do without rcu read locked because we hold the
597 * uuid mutex so nothing we touch in here is going to disappear.
599 if (orig_dev
->name
) {
600 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
605 rcu_assign_pointer(device
->name
, name
);
608 list_add(&device
->dev_list
, &fs_devices
->devices
);
609 device
->fs_devices
= fs_devices
;
610 fs_devices
->num_devices
++;
614 free_fs_devices(fs_devices
);
615 return ERR_PTR(-ENOMEM
);
618 void btrfs_close_extra_devices(struct btrfs_fs_info
*fs_info
,
619 struct btrfs_fs_devices
*fs_devices
, int step
)
621 struct btrfs_device
*device
, *next
;
622 struct btrfs_device
*latest_dev
= NULL
;
624 mutex_lock(&uuid_mutex
);
626 /* This is the initialized path, it is safe to release the devices. */
627 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
628 if (device
->in_fs_metadata
) {
629 if (!device
->is_tgtdev_for_dev_replace
&&
631 device
->generation
> latest_dev
->generation
)) {
637 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
639 * In the first step, keep the device which has
640 * the correct fsid and the devid that is used
641 * for the dev_replace procedure.
642 * In the second step, the dev_replace state is
643 * read from the device tree and it is known
644 * whether the procedure is really active or
645 * not, which means whether this device is
646 * used or whether it should be removed.
648 if (step
== 0 || device
->is_tgtdev_for_dev_replace
) {
653 blkdev_put(device
->bdev
, device
->mode
);
655 fs_devices
->open_devices
--;
657 if (device
->writeable
) {
658 list_del_init(&device
->dev_alloc_list
);
659 device
->writeable
= 0;
660 if (!device
->is_tgtdev_for_dev_replace
)
661 fs_devices
->rw_devices
--;
663 list_del_init(&device
->dev_list
);
664 fs_devices
->num_devices
--;
665 rcu_string_free(device
->name
);
669 if (fs_devices
->seed
) {
670 fs_devices
= fs_devices
->seed
;
674 fs_devices
->latest_bdev
= latest_dev
->bdev
;
676 mutex_unlock(&uuid_mutex
);
679 static void __free_device(struct work_struct
*work
)
681 struct btrfs_device
*device
;
683 device
= container_of(work
, struct btrfs_device
, rcu_work
);
686 blkdev_put(device
->bdev
, device
->mode
);
688 rcu_string_free(device
->name
);
692 static void free_device(struct rcu_head
*head
)
694 struct btrfs_device
*device
;
696 device
= container_of(head
, struct btrfs_device
, rcu
);
698 INIT_WORK(&device
->rcu_work
, __free_device
);
699 schedule_work(&device
->rcu_work
);
702 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
704 struct btrfs_device
*device
;
706 if (--fs_devices
->opened
> 0)
709 mutex_lock(&fs_devices
->device_list_mutex
);
710 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
711 struct btrfs_device
*new_device
;
712 struct rcu_string
*name
;
715 fs_devices
->open_devices
--;
717 if (device
->writeable
&&
718 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
719 list_del_init(&device
->dev_alloc_list
);
720 fs_devices
->rw_devices
--;
723 if (device
->can_discard
)
724 fs_devices
->num_can_discard
--;
726 fs_devices
->missing_devices
--;
728 new_device
= btrfs_alloc_device(NULL
, &device
->devid
,
730 BUG_ON(IS_ERR(new_device
)); /* -ENOMEM */
732 /* Safe because we are under uuid_mutex */
734 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
735 BUG_ON(!name
); /* -ENOMEM */
736 rcu_assign_pointer(new_device
->name
, name
);
739 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
740 new_device
->fs_devices
= device
->fs_devices
;
742 call_rcu(&device
->rcu
, free_device
);
744 mutex_unlock(&fs_devices
->device_list_mutex
);
746 WARN_ON(fs_devices
->open_devices
);
747 WARN_ON(fs_devices
->rw_devices
);
748 fs_devices
->opened
= 0;
749 fs_devices
->seeding
= 0;
754 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
756 struct btrfs_fs_devices
*seed_devices
= NULL
;
759 mutex_lock(&uuid_mutex
);
760 ret
= __btrfs_close_devices(fs_devices
);
761 if (!fs_devices
->opened
) {
762 seed_devices
= fs_devices
->seed
;
763 fs_devices
->seed
= NULL
;
765 mutex_unlock(&uuid_mutex
);
767 while (seed_devices
) {
768 fs_devices
= seed_devices
;
769 seed_devices
= fs_devices
->seed
;
770 __btrfs_close_devices(fs_devices
);
771 free_fs_devices(fs_devices
);
774 * Wait for rcu kworkers under __btrfs_close_devices
775 * to finish all blkdev_puts so device is really
776 * free when umount is done.
782 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
783 fmode_t flags
, void *holder
)
785 struct request_queue
*q
;
786 struct block_device
*bdev
;
787 struct list_head
*head
= &fs_devices
->devices
;
788 struct btrfs_device
*device
;
789 struct btrfs_device
*latest_dev
= NULL
;
790 struct buffer_head
*bh
;
791 struct btrfs_super_block
*disk_super
;
798 list_for_each_entry(device
, head
, dev_list
) {
804 /* Just open everything we can; ignore failures here */
805 if (btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
809 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
810 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
811 if (devid
!= device
->devid
)
814 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
818 device
->generation
= btrfs_super_generation(disk_super
);
820 device
->generation
> latest_dev
->generation
)
823 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
824 device
->writeable
= 0;
826 device
->writeable
= !bdev_read_only(bdev
);
830 q
= bdev_get_queue(bdev
);
831 if (blk_queue_discard(q
)) {
832 device
->can_discard
= 1;
833 fs_devices
->num_can_discard
++;
837 device
->in_fs_metadata
= 0;
838 device
->mode
= flags
;
840 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
841 fs_devices
->rotating
= 1;
843 fs_devices
->open_devices
++;
844 if (device
->writeable
&&
845 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
846 fs_devices
->rw_devices
++;
847 list_add(&device
->dev_alloc_list
,
848 &fs_devices
->alloc_list
);
855 blkdev_put(bdev
, flags
);
858 if (fs_devices
->open_devices
== 0) {
862 fs_devices
->seeding
= seeding
;
863 fs_devices
->opened
= 1;
864 fs_devices
->latest_bdev
= latest_dev
->bdev
;
865 fs_devices
->total_rw_bytes
= 0;
870 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
871 fmode_t flags
, void *holder
)
875 mutex_lock(&uuid_mutex
);
876 if (fs_devices
->opened
) {
877 fs_devices
->opened
++;
880 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
882 mutex_unlock(&uuid_mutex
);
887 * Look for a btrfs signature on a device. This may be called out of the mount path
888 * and we are not allowed to call set_blocksize during the scan. The superblock
889 * is read via pagecache
891 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
892 struct btrfs_fs_devices
**fs_devices_ret
)
894 struct btrfs_super_block
*disk_super
;
895 struct block_device
*bdev
;
906 * we would like to check all the supers, but that would make
907 * a btrfs mount succeed after a mkfs from a different FS.
908 * So, we need to add a special mount option to scan for
909 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
911 bytenr
= btrfs_sb_offset(0);
913 mutex_lock(&uuid_mutex
);
915 bdev
= blkdev_get_by_path(path
, flags
, holder
);
922 /* make sure our super fits in the device */
923 if (bytenr
+ PAGE_CACHE_SIZE
>= i_size_read(bdev
->bd_inode
))
926 /* make sure our super fits in the page */
927 if (sizeof(*disk_super
) > PAGE_CACHE_SIZE
)
930 /* make sure our super doesn't straddle pages on disk */
931 index
= bytenr
>> PAGE_CACHE_SHIFT
;
932 if ((bytenr
+ sizeof(*disk_super
) - 1) >> PAGE_CACHE_SHIFT
!= index
)
935 /* pull in the page with our super */
936 page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
939 if (IS_ERR_OR_NULL(page
))
944 /* align our pointer to the offset of the super block */
945 disk_super
= p
+ (bytenr
& ~PAGE_CACHE_MASK
);
947 if (btrfs_super_bytenr(disk_super
) != bytenr
||
948 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
)
951 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
952 transid
= btrfs_super_generation(disk_super
);
953 total_devices
= btrfs_super_num_devices(disk_super
);
955 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
957 if (disk_super
->label
[0]) {
958 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
959 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
960 printk(KERN_INFO
"BTRFS: device label %s ", disk_super
->label
);
962 printk(KERN_INFO
"BTRFS: device fsid %pU ", disk_super
->fsid
);
965 printk(KERN_CONT
"devid %llu transid %llu %s\n", devid
, transid
, path
);
968 if (!ret
&& fs_devices_ret
)
969 (*fs_devices_ret
)->total_devices
= total_devices
;
973 page_cache_release(page
);
976 blkdev_put(bdev
, flags
);
978 mutex_unlock(&uuid_mutex
);
982 /* helper to account the used device space in the range */
983 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
984 u64 end
, u64
*length
)
986 struct btrfs_key key
;
987 struct btrfs_root
*root
= device
->dev_root
;
988 struct btrfs_dev_extent
*dev_extent
;
989 struct btrfs_path
*path
;
993 struct extent_buffer
*l
;
997 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
1000 path
= btrfs_alloc_path();
1005 key
.objectid
= device
->devid
;
1007 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1009 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1013 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1020 slot
= path
->slots
[0];
1021 if (slot
>= btrfs_header_nritems(l
)) {
1022 ret
= btrfs_next_leaf(root
, path
);
1030 btrfs_item_key_to_cpu(l
, &key
, slot
);
1032 if (key
.objectid
< device
->devid
)
1035 if (key
.objectid
> device
->devid
)
1038 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1041 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1042 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1044 if (key
.offset
<= start
&& extent_end
> end
) {
1045 *length
= end
- start
+ 1;
1047 } else if (key
.offset
<= start
&& extent_end
> start
)
1048 *length
+= extent_end
- start
;
1049 else if (key
.offset
> start
&& extent_end
<= end
)
1050 *length
+= extent_end
- key
.offset
;
1051 else if (key
.offset
> start
&& key
.offset
<= end
) {
1052 *length
+= end
- key
.offset
+ 1;
1054 } else if (key
.offset
> end
)
1062 btrfs_free_path(path
);
1066 static int contains_pending_extent(struct btrfs_trans_handle
*trans
,
1067 struct btrfs_device
*device
,
1068 u64
*start
, u64 len
)
1070 struct extent_map
*em
;
1073 list_for_each_entry(em
, &trans
->transaction
->pending_chunks
, list
) {
1074 struct map_lookup
*map
;
1077 map
= (struct map_lookup
*)em
->bdev
;
1078 for (i
= 0; i
< map
->num_stripes
; i
++) {
1079 if (map
->stripes
[i
].dev
!= device
)
1081 if (map
->stripes
[i
].physical
>= *start
+ len
||
1082 map
->stripes
[i
].physical
+ em
->orig_block_len
<=
1085 *start
= map
->stripes
[i
].physical
+
1096 * find_free_dev_extent - find free space in the specified device
1097 * @device: the device which we search the free space in
1098 * @num_bytes: the size of the free space that we need
1099 * @start: store the start of the free space.
1100 * @len: the size of the free space. that we find, or the size of the max
1101 * free space if we don't find suitable free space
1103 * this uses a pretty simple search, the expectation is that it is
1104 * called very infrequently and that a given device has a small number
1107 * @start is used to store the start of the free space if we find. But if we
1108 * don't find suitable free space, it will be used to store the start position
1109 * of the max free space.
1111 * @len is used to store the size of the free space that we find.
1112 * But if we don't find suitable free space, it is used to store the size of
1113 * the max free space.
1115 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
1116 struct btrfs_device
*device
, u64 num_bytes
,
1117 u64
*start
, u64
*len
)
1119 struct btrfs_key key
;
1120 struct btrfs_root
*root
= device
->dev_root
;
1121 struct btrfs_dev_extent
*dev_extent
;
1122 struct btrfs_path
*path
;
1128 u64 search_end
= device
->total_bytes
;
1131 struct extent_buffer
*l
;
1133 /* FIXME use last free of some kind */
1135 /* we don't want to overwrite the superblock on the drive,
1136 * so we make sure to start at an offset of at least 1MB
1138 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
1140 path
= btrfs_alloc_path();
1144 max_hole_start
= search_start
;
1148 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
1154 path
->search_commit_root
= 1;
1155 path
->skip_locking
= 1;
1157 key
.objectid
= device
->devid
;
1158 key
.offset
= search_start
;
1159 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1161 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1165 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1172 slot
= path
->slots
[0];
1173 if (slot
>= btrfs_header_nritems(l
)) {
1174 ret
= btrfs_next_leaf(root
, path
);
1182 btrfs_item_key_to_cpu(l
, &key
, slot
);
1184 if (key
.objectid
< device
->devid
)
1187 if (key
.objectid
> device
->devid
)
1190 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1193 if (key
.offset
> search_start
) {
1194 hole_size
= key
.offset
- search_start
;
1197 * Have to check before we set max_hole_start, otherwise
1198 * we could end up sending back this offset anyway.
1200 if (contains_pending_extent(trans
, device
,
1205 if (hole_size
> max_hole_size
) {
1206 max_hole_start
= search_start
;
1207 max_hole_size
= hole_size
;
1211 * If this free space is greater than which we need,
1212 * it must be the max free space that we have found
1213 * until now, so max_hole_start must point to the start
1214 * of this free space and the length of this free space
1215 * is stored in max_hole_size. Thus, we return
1216 * max_hole_start and max_hole_size and go back to the
1219 if (hole_size
>= num_bytes
) {
1225 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1226 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1228 if (extent_end
> search_start
)
1229 search_start
= extent_end
;
1236 * At this point, search_start should be the end of
1237 * allocated dev extents, and when shrinking the device,
1238 * search_end may be smaller than search_start.
1240 if (search_end
> search_start
)
1241 hole_size
= search_end
- search_start
;
1243 if (hole_size
> max_hole_size
) {
1244 max_hole_start
= search_start
;
1245 max_hole_size
= hole_size
;
1248 if (contains_pending_extent(trans
, device
, &search_start
, hole_size
)) {
1249 btrfs_release_path(path
);
1254 if (hole_size
< num_bytes
)
1260 btrfs_free_path(path
);
1261 *start
= max_hole_start
;
1263 *len
= max_hole_size
;
1267 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1268 struct btrfs_device
*device
,
1272 struct btrfs_path
*path
;
1273 struct btrfs_root
*root
= device
->dev_root
;
1274 struct btrfs_key key
;
1275 struct btrfs_key found_key
;
1276 struct extent_buffer
*leaf
= NULL
;
1277 struct btrfs_dev_extent
*extent
= NULL
;
1279 path
= btrfs_alloc_path();
1283 key
.objectid
= device
->devid
;
1285 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1287 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1289 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1290 BTRFS_DEV_EXTENT_KEY
);
1293 leaf
= path
->nodes
[0];
1294 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1295 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1296 struct btrfs_dev_extent
);
1297 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1298 btrfs_dev_extent_length(leaf
, extent
) < start
);
1300 btrfs_release_path(path
);
1302 } else if (ret
== 0) {
1303 leaf
= path
->nodes
[0];
1304 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1305 struct btrfs_dev_extent
);
1307 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1311 if (device
->bytes_used
> 0) {
1312 u64 len
= btrfs_dev_extent_length(leaf
, extent
);
1313 device
->bytes_used
-= len
;
1314 spin_lock(&root
->fs_info
->free_chunk_lock
);
1315 root
->fs_info
->free_chunk_space
+= len
;
1316 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1318 ret
= btrfs_del_item(trans
, root
, path
);
1320 btrfs_error(root
->fs_info
, ret
,
1321 "Failed to remove dev extent item");
1324 btrfs_free_path(path
);
1328 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1329 struct btrfs_device
*device
,
1330 u64 chunk_tree
, u64 chunk_objectid
,
1331 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1334 struct btrfs_path
*path
;
1335 struct btrfs_root
*root
= device
->dev_root
;
1336 struct btrfs_dev_extent
*extent
;
1337 struct extent_buffer
*leaf
;
1338 struct btrfs_key key
;
1340 WARN_ON(!device
->in_fs_metadata
);
1341 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1342 path
= btrfs_alloc_path();
1346 key
.objectid
= device
->devid
;
1348 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1349 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1354 leaf
= path
->nodes
[0];
1355 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1356 struct btrfs_dev_extent
);
1357 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1358 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1359 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1361 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1362 btrfs_dev_extent_chunk_tree_uuid(extent
), BTRFS_UUID_SIZE
);
1364 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1365 btrfs_mark_buffer_dirty(leaf
);
1367 btrfs_free_path(path
);
1371 static u64
find_next_chunk(struct btrfs_fs_info
*fs_info
)
1373 struct extent_map_tree
*em_tree
;
1374 struct extent_map
*em
;
1378 em_tree
= &fs_info
->mapping_tree
.map_tree
;
1379 read_lock(&em_tree
->lock
);
1380 n
= rb_last(&em_tree
->map
);
1382 em
= rb_entry(n
, struct extent_map
, rb_node
);
1383 ret
= em
->start
+ em
->len
;
1385 read_unlock(&em_tree
->lock
);
1390 static noinline
int find_next_devid(struct btrfs_fs_info
*fs_info
,
1394 struct btrfs_key key
;
1395 struct btrfs_key found_key
;
1396 struct btrfs_path
*path
;
1398 path
= btrfs_alloc_path();
1402 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1403 key
.type
= BTRFS_DEV_ITEM_KEY
;
1404 key
.offset
= (u64
)-1;
1406 ret
= btrfs_search_slot(NULL
, fs_info
->chunk_root
, &key
, path
, 0, 0);
1410 BUG_ON(ret
== 0); /* Corruption */
1412 ret
= btrfs_previous_item(fs_info
->chunk_root
, path
,
1413 BTRFS_DEV_ITEMS_OBJECTID
,
1414 BTRFS_DEV_ITEM_KEY
);
1418 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1420 *devid_ret
= found_key
.offset
+ 1;
1424 btrfs_free_path(path
);
1429 * the device information is stored in the chunk root
1430 * the btrfs_device struct should be fully filled in
1432 static int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1433 struct btrfs_root
*root
,
1434 struct btrfs_device
*device
)
1437 struct btrfs_path
*path
;
1438 struct btrfs_dev_item
*dev_item
;
1439 struct extent_buffer
*leaf
;
1440 struct btrfs_key key
;
1443 root
= root
->fs_info
->chunk_root
;
1445 path
= btrfs_alloc_path();
1449 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1450 key
.type
= BTRFS_DEV_ITEM_KEY
;
1451 key
.offset
= device
->devid
;
1453 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1458 leaf
= path
->nodes
[0];
1459 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1461 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1462 btrfs_set_device_generation(leaf
, dev_item
, 0);
1463 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1464 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1465 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1466 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1467 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
1468 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
1469 btrfs_set_device_group(leaf
, dev_item
, 0);
1470 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1471 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1472 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1474 ptr
= btrfs_device_uuid(dev_item
);
1475 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1476 ptr
= btrfs_device_fsid(dev_item
);
1477 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1478 btrfs_mark_buffer_dirty(leaf
);
1482 btrfs_free_path(path
);
1487 * Function to update ctime/mtime for a given device path.
1488 * Mainly used for ctime/mtime based probe like libblkid.
1490 static void update_dev_time(char *path_name
)
1494 filp
= filp_open(path_name
, O_RDWR
, 0);
1497 file_update_time(filp
);
1498 filp_close(filp
, NULL
);
1502 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1503 struct btrfs_device
*device
)
1506 struct btrfs_path
*path
;
1507 struct btrfs_key key
;
1508 struct btrfs_trans_handle
*trans
;
1510 root
= root
->fs_info
->chunk_root
;
1512 path
= btrfs_alloc_path();
1516 trans
= btrfs_start_transaction(root
, 0);
1517 if (IS_ERR(trans
)) {
1518 btrfs_free_path(path
);
1519 return PTR_ERR(trans
);
1521 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1522 key
.type
= BTRFS_DEV_ITEM_KEY
;
1523 key
.offset
= device
->devid
;
1526 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1535 ret
= btrfs_del_item(trans
, root
, path
);
1539 btrfs_free_path(path
);
1540 unlock_chunks(root
);
1541 btrfs_commit_transaction(trans
, root
);
1545 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1547 struct btrfs_device
*device
;
1548 struct btrfs_device
*next_device
;
1549 struct block_device
*bdev
;
1550 struct buffer_head
*bh
= NULL
;
1551 struct btrfs_super_block
*disk_super
;
1552 struct btrfs_fs_devices
*cur_devices
;
1559 bool clear_super
= false;
1561 mutex_lock(&uuid_mutex
);
1564 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
1566 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1567 root
->fs_info
->avail_system_alloc_bits
|
1568 root
->fs_info
->avail_metadata_alloc_bits
;
1569 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
1571 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1572 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1573 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1574 WARN_ON(num_devices
< 1);
1577 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1579 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1580 ret
= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET
;
1584 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1585 ret
= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET
;
1589 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID5
) &&
1590 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1591 ret
= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET
;
1594 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID6
) &&
1595 root
->fs_info
->fs_devices
->rw_devices
<= 3) {
1596 ret
= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET
;
1600 if (strcmp(device_path
, "missing") == 0) {
1601 struct list_head
*devices
;
1602 struct btrfs_device
*tmp
;
1605 devices
= &root
->fs_info
->fs_devices
->devices
;
1607 * It is safe to read the devices since the volume_mutex
1610 list_for_each_entry(tmp
, devices
, dev_list
) {
1611 if (tmp
->in_fs_metadata
&&
1612 !tmp
->is_tgtdev_for_dev_replace
&&
1622 ret
= BTRFS_ERROR_DEV_MISSING_NOT_FOUND
;
1626 ret
= btrfs_get_bdev_and_sb(device_path
,
1627 FMODE_WRITE
| FMODE_EXCL
,
1628 root
->fs_info
->bdev_holder
, 0,
1632 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1633 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1634 dev_uuid
= disk_super
->dev_item
.uuid
;
1635 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1643 if (device
->is_tgtdev_for_dev_replace
) {
1644 ret
= BTRFS_ERROR_DEV_TGT_REPLACE
;
1648 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1649 ret
= BTRFS_ERROR_DEV_ONLY_WRITABLE
;
1653 if (device
->writeable
) {
1655 list_del_init(&device
->dev_alloc_list
);
1656 unlock_chunks(root
);
1657 root
->fs_info
->fs_devices
->rw_devices
--;
1661 mutex_unlock(&uuid_mutex
);
1662 ret
= btrfs_shrink_device(device
, 0);
1663 mutex_lock(&uuid_mutex
);
1668 * TODO: the superblock still includes this device in its num_devices
1669 * counter although write_all_supers() is not locked out. This
1670 * could give a filesystem state which requires a degraded mount.
1672 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1676 spin_lock(&root
->fs_info
->free_chunk_lock
);
1677 root
->fs_info
->free_chunk_space
= device
->total_bytes
-
1679 spin_unlock(&root
->fs_info
->free_chunk_lock
);
1681 device
->in_fs_metadata
= 0;
1682 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1685 * the device list mutex makes sure that we don't change
1686 * the device list while someone else is writing out all
1687 * the device supers. Whoever is writing all supers, should
1688 * lock the device list mutex before getting the number of
1689 * devices in the super block (super_copy). Conversely,
1690 * whoever updates the number of devices in the super block
1691 * (super_copy) should hold the device list mutex.
1694 cur_devices
= device
->fs_devices
;
1695 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1696 list_del_rcu(&device
->dev_list
);
1698 device
->fs_devices
->num_devices
--;
1699 device
->fs_devices
->total_devices
--;
1701 if (device
->missing
)
1702 device
->fs_devices
->missing_devices
--;
1704 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1705 struct btrfs_device
, dev_list
);
1706 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1707 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1708 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1709 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1712 device
->fs_devices
->open_devices
--;
1713 /* remove sysfs entry */
1714 btrfs_kobj_rm_device(root
->fs_info
, device
);
1717 call_rcu(&device
->rcu
, free_device
);
1719 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1720 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1721 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1723 if (cur_devices
->open_devices
== 0) {
1724 struct btrfs_fs_devices
*fs_devices
;
1725 fs_devices
= root
->fs_info
->fs_devices
;
1726 while (fs_devices
) {
1727 if (fs_devices
->seed
== cur_devices
) {
1728 fs_devices
->seed
= cur_devices
->seed
;
1731 fs_devices
= fs_devices
->seed
;
1733 cur_devices
->seed
= NULL
;
1735 __btrfs_close_devices(cur_devices
);
1736 unlock_chunks(root
);
1737 free_fs_devices(cur_devices
);
1740 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1741 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1744 * at this point, the device is zero sized. We want to
1745 * remove it from the devices list and zero out the old super
1747 if (clear_super
&& disk_super
) {
1751 /* make sure this device isn't detected as part of
1754 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1755 set_buffer_dirty(bh
);
1756 sync_dirty_buffer(bh
);
1758 /* clear the mirror copies of super block on the disk
1759 * being removed, 0th copy is been taken care above and
1760 * the below would take of the rest
1762 for (i
= 1; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1763 bytenr
= btrfs_sb_offset(i
);
1764 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>=
1765 i_size_read(bdev
->bd_inode
))
1769 bh
= __bread(bdev
, bytenr
/ 4096,
1770 BTRFS_SUPER_INFO_SIZE
);
1774 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1776 if (btrfs_super_bytenr(disk_super
) != bytenr
||
1777 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
) {
1780 memset(&disk_super
->magic
, 0,
1781 sizeof(disk_super
->magic
));
1782 set_buffer_dirty(bh
);
1783 sync_dirty_buffer(bh
);
1790 /* Notify udev that device has changed */
1791 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
1793 /* Update ctime/mtime for device path for libblkid */
1794 update_dev_time(device_path
);
1800 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1802 mutex_unlock(&uuid_mutex
);
1805 if (device
->writeable
) {
1807 list_add(&device
->dev_alloc_list
,
1808 &root
->fs_info
->fs_devices
->alloc_list
);
1809 unlock_chunks(root
);
1810 root
->fs_info
->fs_devices
->rw_devices
++;
1815 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info
*fs_info
,
1816 struct btrfs_device
*srcdev
)
1818 struct btrfs_fs_devices
*fs_devices
;
1820 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1823 * in case of fs with no seed, srcdev->fs_devices will point
1824 * to fs_devices of fs_info. However when the dev being replaced is
1825 * a seed dev it will point to the seed's local fs_devices. In short
1826 * srcdev will have its correct fs_devices in both the cases.
1828 fs_devices
= srcdev
->fs_devices
;
1830 list_del_rcu(&srcdev
->dev_list
);
1831 list_del_rcu(&srcdev
->dev_alloc_list
);
1832 fs_devices
->num_devices
--;
1833 if (srcdev
->missing
) {
1834 fs_devices
->missing_devices
--;
1835 fs_devices
->rw_devices
++;
1837 if (srcdev
->can_discard
)
1838 fs_devices
->num_can_discard
--;
1840 fs_devices
->open_devices
--;
1843 * zero out the old super if it is not writable
1844 * (e.g. seed device)
1846 if (srcdev
->writeable
)
1847 btrfs_scratch_superblock(srcdev
);
1850 call_rcu(&srcdev
->rcu
, free_device
);
1853 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1854 struct btrfs_device
*tgtdev
)
1856 struct btrfs_device
*next_device
;
1859 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1861 btrfs_scratch_superblock(tgtdev
);
1862 fs_info
->fs_devices
->open_devices
--;
1864 fs_info
->fs_devices
->num_devices
--;
1865 if (tgtdev
->can_discard
)
1866 fs_info
->fs_devices
->num_can_discard
++;
1868 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1869 struct btrfs_device
, dev_list
);
1870 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1871 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1872 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1873 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1874 list_del_rcu(&tgtdev
->dev_list
);
1876 call_rcu(&tgtdev
->rcu
, free_device
);
1878 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1881 static int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1882 struct btrfs_device
**device
)
1885 struct btrfs_super_block
*disk_super
;
1888 struct block_device
*bdev
;
1889 struct buffer_head
*bh
;
1892 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1893 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1896 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1897 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1898 dev_uuid
= disk_super
->dev_item
.uuid
;
1899 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1904 blkdev_put(bdev
, FMODE_READ
);
1908 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
1910 struct btrfs_device
**device
)
1913 if (strcmp(device_path
, "missing") == 0) {
1914 struct list_head
*devices
;
1915 struct btrfs_device
*tmp
;
1917 devices
= &root
->fs_info
->fs_devices
->devices
;
1919 * It is safe to read the devices since the volume_mutex
1920 * is held by the caller.
1922 list_for_each_entry(tmp
, devices
, dev_list
) {
1923 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
1930 btrfs_err(root
->fs_info
, "no missing device found");
1936 return btrfs_find_device_by_path(root
, device_path
, device
);
1941 * does all the dirty work required for changing file system's UUID.
1943 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
1945 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1946 struct btrfs_fs_devices
*old_devices
;
1947 struct btrfs_fs_devices
*seed_devices
;
1948 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
1949 struct btrfs_device
*device
;
1952 BUG_ON(!mutex_is_locked(&uuid_mutex
));
1953 if (!fs_devices
->seeding
)
1956 seed_devices
= __alloc_fs_devices();
1957 if (IS_ERR(seed_devices
))
1958 return PTR_ERR(seed_devices
);
1960 old_devices
= clone_fs_devices(fs_devices
);
1961 if (IS_ERR(old_devices
)) {
1962 kfree(seed_devices
);
1963 return PTR_ERR(old_devices
);
1966 list_add(&old_devices
->list
, &fs_uuids
);
1968 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
1969 seed_devices
->opened
= 1;
1970 INIT_LIST_HEAD(&seed_devices
->devices
);
1971 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
1972 mutex_init(&seed_devices
->device_list_mutex
);
1974 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1975 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
1978 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
1979 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
) {
1980 device
->fs_devices
= seed_devices
;
1983 fs_devices
->seeding
= 0;
1984 fs_devices
->num_devices
= 0;
1985 fs_devices
->open_devices
= 0;
1986 fs_devices
->missing_devices
= 0;
1987 fs_devices
->num_can_discard
= 0;
1988 fs_devices
->rotating
= 0;
1989 fs_devices
->seed
= seed_devices
;
1991 generate_random_uuid(fs_devices
->fsid
);
1992 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1993 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
1994 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1996 super_flags
= btrfs_super_flags(disk_super
) &
1997 ~BTRFS_SUPER_FLAG_SEEDING
;
1998 btrfs_set_super_flags(disk_super
, super_flags
);
2004 * strore the expected generation for seed devices in device items.
2006 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
2007 struct btrfs_root
*root
)
2009 struct btrfs_path
*path
;
2010 struct extent_buffer
*leaf
;
2011 struct btrfs_dev_item
*dev_item
;
2012 struct btrfs_device
*device
;
2013 struct btrfs_key key
;
2014 u8 fs_uuid
[BTRFS_UUID_SIZE
];
2015 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2019 path
= btrfs_alloc_path();
2023 root
= root
->fs_info
->chunk_root
;
2024 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2026 key
.type
= BTRFS_DEV_ITEM_KEY
;
2029 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2033 leaf
= path
->nodes
[0];
2035 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
2036 ret
= btrfs_next_leaf(root
, path
);
2041 leaf
= path
->nodes
[0];
2042 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2043 btrfs_release_path(path
);
2047 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2048 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
2049 key
.type
!= BTRFS_DEV_ITEM_KEY
)
2052 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2053 struct btrfs_dev_item
);
2054 devid
= btrfs_device_id(leaf
, dev_item
);
2055 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
2057 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
2059 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
2061 BUG_ON(!device
); /* Logic error */
2063 if (device
->fs_devices
->seeding
) {
2064 btrfs_set_device_generation(leaf
, dev_item
,
2065 device
->generation
);
2066 btrfs_mark_buffer_dirty(leaf
);
2074 btrfs_free_path(path
);
2078 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
2080 struct request_queue
*q
;
2081 struct btrfs_trans_handle
*trans
;
2082 struct btrfs_device
*device
;
2083 struct block_device
*bdev
;
2084 struct list_head
*devices
;
2085 struct super_block
*sb
= root
->fs_info
->sb
;
2086 struct rcu_string
*name
;
2088 int seeding_dev
= 0;
2091 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
2094 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2095 root
->fs_info
->bdev_holder
);
2097 return PTR_ERR(bdev
);
2099 if (root
->fs_info
->fs_devices
->seeding
) {
2101 down_write(&sb
->s_umount
);
2102 mutex_lock(&uuid_mutex
);
2105 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2107 devices
= &root
->fs_info
->fs_devices
->devices
;
2109 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2110 list_for_each_entry(device
, devices
, dev_list
) {
2111 if (device
->bdev
== bdev
) {
2114 &root
->fs_info
->fs_devices
->device_list_mutex
);
2118 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2120 device
= btrfs_alloc_device(root
->fs_info
, NULL
, NULL
);
2121 if (IS_ERR(device
)) {
2122 /* we can safely leave the fs_devices entry around */
2123 ret
= PTR_ERR(device
);
2127 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2133 rcu_assign_pointer(device
->name
, name
);
2135 trans
= btrfs_start_transaction(root
, 0);
2136 if (IS_ERR(trans
)) {
2137 rcu_string_free(device
->name
);
2139 ret
= PTR_ERR(trans
);
2145 q
= bdev_get_queue(bdev
);
2146 if (blk_queue_discard(q
))
2147 device
->can_discard
= 1;
2148 device
->writeable
= 1;
2149 device
->generation
= trans
->transid
;
2150 device
->io_width
= root
->sectorsize
;
2151 device
->io_align
= root
->sectorsize
;
2152 device
->sector_size
= root
->sectorsize
;
2153 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2154 device
->disk_total_bytes
= device
->total_bytes
;
2155 device
->dev_root
= root
->fs_info
->dev_root
;
2156 device
->bdev
= bdev
;
2157 device
->in_fs_metadata
= 1;
2158 device
->is_tgtdev_for_dev_replace
= 0;
2159 device
->mode
= FMODE_EXCL
;
2160 device
->dev_stats_valid
= 1;
2161 set_blocksize(device
->bdev
, 4096);
2164 sb
->s_flags
&= ~MS_RDONLY
;
2165 ret
= btrfs_prepare_sprout(root
);
2166 BUG_ON(ret
); /* -ENOMEM */
2169 device
->fs_devices
= root
->fs_info
->fs_devices
;
2171 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2172 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
2173 list_add(&device
->dev_alloc_list
,
2174 &root
->fs_info
->fs_devices
->alloc_list
);
2175 root
->fs_info
->fs_devices
->num_devices
++;
2176 root
->fs_info
->fs_devices
->open_devices
++;
2177 root
->fs_info
->fs_devices
->rw_devices
++;
2178 root
->fs_info
->fs_devices
->total_devices
++;
2179 if (device
->can_discard
)
2180 root
->fs_info
->fs_devices
->num_can_discard
++;
2181 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2183 spin_lock(&root
->fs_info
->free_chunk_lock
);
2184 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
2185 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2187 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
2188 root
->fs_info
->fs_devices
->rotating
= 1;
2190 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
2191 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
2192 total_bytes
+ device
->total_bytes
);
2194 total_bytes
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
2195 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
2198 /* add sysfs device entry */
2199 btrfs_kobj_add_device(root
->fs_info
, device
);
2201 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2204 char fsid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
2205 ret
= init_first_rw_device(trans
, root
, device
);
2207 btrfs_abort_transaction(trans
, root
, ret
);
2210 ret
= btrfs_finish_sprout(trans
, root
);
2212 btrfs_abort_transaction(trans
, root
, ret
);
2216 /* Sprouting would change fsid of the mounted root,
2217 * so rename the fsid on the sysfs
2219 snprintf(fsid_buf
, BTRFS_UUID_UNPARSED_SIZE
, "%pU",
2220 root
->fs_info
->fsid
);
2221 if (kobject_rename(&root
->fs_info
->super_kobj
, fsid_buf
))
2224 ret
= btrfs_add_device(trans
, root
, device
);
2226 btrfs_abort_transaction(trans
, root
, ret
);
2232 * we've got more storage, clear any full flags on the space
2235 btrfs_clear_space_info_full(root
->fs_info
);
2237 unlock_chunks(root
);
2238 root
->fs_info
->num_tolerated_disk_barrier_failures
=
2239 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
2240 ret
= btrfs_commit_transaction(trans
, root
);
2243 mutex_unlock(&uuid_mutex
);
2244 up_write(&sb
->s_umount
);
2246 if (ret
) /* transaction commit */
2249 ret
= btrfs_relocate_sys_chunks(root
);
2251 btrfs_error(root
->fs_info
, ret
,
2252 "Failed to relocate sys chunks after "
2253 "device initialization. This can be fixed "
2254 "using the \"btrfs balance\" command.");
2255 trans
= btrfs_attach_transaction(root
);
2256 if (IS_ERR(trans
)) {
2257 if (PTR_ERR(trans
) == -ENOENT
)
2259 return PTR_ERR(trans
);
2261 ret
= btrfs_commit_transaction(trans
, root
);
2264 /* Update ctime/mtime for libblkid */
2265 update_dev_time(device_path
);
2269 unlock_chunks(root
);
2270 btrfs_end_transaction(trans
, root
);
2271 rcu_string_free(device
->name
);
2272 btrfs_kobj_rm_device(root
->fs_info
, device
);
2275 blkdev_put(bdev
, FMODE_EXCL
);
2277 mutex_unlock(&uuid_mutex
);
2278 up_write(&sb
->s_umount
);
2283 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2284 struct btrfs_device
**device_out
)
2286 struct request_queue
*q
;
2287 struct btrfs_device
*device
;
2288 struct block_device
*bdev
;
2289 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2290 struct list_head
*devices
;
2291 struct rcu_string
*name
;
2292 u64 devid
= BTRFS_DEV_REPLACE_DEVID
;
2296 if (fs_info
->fs_devices
->seeding
)
2299 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2300 fs_info
->bdev_holder
);
2302 return PTR_ERR(bdev
);
2304 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2306 devices
= &fs_info
->fs_devices
->devices
;
2307 list_for_each_entry(device
, devices
, dev_list
) {
2308 if (device
->bdev
== bdev
) {
2314 device
= btrfs_alloc_device(NULL
, &devid
, NULL
);
2315 if (IS_ERR(device
)) {
2316 ret
= PTR_ERR(device
);
2320 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2326 rcu_assign_pointer(device
->name
, name
);
2328 q
= bdev_get_queue(bdev
);
2329 if (blk_queue_discard(q
))
2330 device
->can_discard
= 1;
2331 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2332 device
->writeable
= 1;
2333 device
->generation
= 0;
2334 device
->io_width
= root
->sectorsize
;
2335 device
->io_align
= root
->sectorsize
;
2336 device
->sector_size
= root
->sectorsize
;
2337 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2338 device
->disk_total_bytes
= device
->total_bytes
;
2339 device
->dev_root
= fs_info
->dev_root
;
2340 device
->bdev
= bdev
;
2341 device
->in_fs_metadata
= 1;
2342 device
->is_tgtdev_for_dev_replace
= 1;
2343 device
->mode
= FMODE_EXCL
;
2344 device
->dev_stats_valid
= 1;
2345 set_blocksize(device
->bdev
, 4096);
2346 device
->fs_devices
= fs_info
->fs_devices
;
2347 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2348 fs_info
->fs_devices
->num_devices
++;
2349 fs_info
->fs_devices
->open_devices
++;
2350 if (device
->can_discard
)
2351 fs_info
->fs_devices
->num_can_discard
++;
2352 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2354 *device_out
= device
;
2358 blkdev_put(bdev
, FMODE_EXCL
);
2362 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2363 struct btrfs_device
*tgtdev
)
2365 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2366 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2367 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2368 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2369 tgtdev
->dev_root
= fs_info
->dev_root
;
2370 tgtdev
->in_fs_metadata
= 1;
2373 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2374 struct btrfs_device
*device
)
2377 struct btrfs_path
*path
;
2378 struct btrfs_root
*root
;
2379 struct btrfs_dev_item
*dev_item
;
2380 struct extent_buffer
*leaf
;
2381 struct btrfs_key key
;
2383 root
= device
->dev_root
->fs_info
->chunk_root
;
2385 path
= btrfs_alloc_path();
2389 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2390 key
.type
= BTRFS_DEV_ITEM_KEY
;
2391 key
.offset
= device
->devid
;
2393 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2402 leaf
= path
->nodes
[0];
2403 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2405 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2406 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2407 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2408 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2409 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2410 btrfs_set_device_total_bytes(leaf
, dev_item
, device
->disk_total_bytes
);
2411 btrfs_set_device_bytes_used(leaf
, dev_item
, device
->bytes_used
);
2412 btrfs_mark_buffer_dirty(leaf
);
2415 btrfs_free_path(path
);
2419 static int __btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2420 struct btrfs_device
*device
, u64 new_size
)
2422 struct btrfs_super_block
*super_copy
=
2423 device
->dev_root
->fs_info
->super_copy
;
2424 u64 old_total
= btrfs_super_total_bytes(super_copy
);
2425 u64 diff
= new_size
- device
->total_bytes
;
2427 if (!device
->writeable
)
2429 if (new_size
<= device
->total_bytes
||
2430 device
->is_tgtdev_for_dev_replace
)
2433 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2434 device
->fs_devices
->total_rw_bytes
+= diff
;
2436 device
->total_bytes
= new_size
;
2437 device
->disk_total_bytes
= new_size
;
2438 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2440 return btrfs_update_device(trans
, device
);
2443 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2444 struct btrfs_device
*device
, u64 new_size
)
2447 lock_chunks(device
->dev_root
);
2448 ret
= __btrfs_grow_device(trans
, device
, new_size
);
2449 unlock_chunks(device
->dev_root
);
2453 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2454 struct btrfs_root
*root
,
2455 u64 chunk_tree
, u64 chunk_objectid
,
2459 struct btrfs_path
*path
;
2460 struct btrfs_key key
;
2462 root
= root
->fs_info
->chunk_root
;
2463 path
= btrfs_alloc_path();
2467 key
.objectid
= chunk_objectid
;
2468 key
.offset
= chunk_offset
;
2469 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2471 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2474 else if (ret
> 0) { /* Logic error or corruption */
2475 btrfs_error(root
->fs_info
, -ENOENT
,
2476 "Failed lookup while freeing chunk.");
2481 ret
= btrfs_del_item(trans
, root
, path
);
2483 btrfs_error(root
->fs_info
, ret
,
2484 "Failed to delete chunk item.");
2486 btrfs_free_path(path
);
2490 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2493 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2494 struct btrfs_disk_key
*disk_key
;
2495 struct btrfs_chunk
*chunk
;
2502 struct btrfs_key key
;
2504 array_size
= btrfs_super_sys_array_size(super_copy
);
2506 ptr
= super_copy
->sys_chunk_array
;
2509 while (cur
< array_size
) {
2510 disk_key
= (struct btrfs_disk_key
*)ptr
;
2511 btrfs_disk_key_to_cpu(&key
, disk_key
);
2513 len
= sizeof(*disk_key
);
2515 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2516 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2517 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2518 len
+= btrfs_chunk_item_size(num_stripes
);
2523 if (key
.objectid
== chunk_objectid
&&
2524 key
.offset
== chunk_offset
) {
2525 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2527 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2536 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2537 u64 chunk_tree
, u64 chunk_objectid
,
2540 struct extent_map_tree
*em_tree
;
2541 struct btrfs_root
*extent_root
;
2542 struct btrfs_trans_handle
*trans
;
2543 struct extent_map
*em
;
2544 struct map_lookup
*map
;
2548 root
= root
->fs_info
->chunk_root
;
2549 extent_root
= root
->fs_info
->extent_root
;
2550 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2552 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2556 /* step one, relocate all the extents inside this chunk */
2557 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2561 trans
= btrfs_start_transaction(root
, 0);
2562 if (IS_ERR(trans
)) {
2563 ret
= PTR_ERR(trans
);
2564 btrfs_std_error(root
->fs_info
, ret
);
2571 * step two, delete the device extents and the
2572 * chunk tree entries
2574 read_lock(&em_tree
->lock
);
2575 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2576 read_unlock(&em_tree
->lock
);
2578 BUG_ON(!em
|| em
->start
> chunk_offset
||
2579 em
->start
+ em
->len
< chunk_offset
);
2580 map
= (struct map_lookup
*)em
->bdev
;
2582 for (i
= 0; i
< map
->num_stripes
; i
++) {
2583 ret
= btrfs_free_dev_extent(trans
, map
->stripes
[i
].dev
,
2584 map
->stripes
[i
].physical
);
2587 if (map
->stripes
[i
].dev
) {
2588 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2592 ret
= btrfs_free_chunk(trans
, root
, chunk_tree
, chunk_objectid
,
2597 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2599 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2600 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2604 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
);
2607 write_lock(&em_tree
->lock
);
2608 remove_extent_mapping(em_tree
, em
);
2609 write_unlock(&em_tree
->lock
);
2611 /* once for the tree */
2612 free_extent_map(em
);
2614 free_extent_map(em
);
2616 unlock_chunks(root
);
2617 btrfs_end_transaction(trans
, root
);
2621 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2623 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2624 struct btrfs_path
*path
;
2625 struct extent_buffer
*leaf
;
2626 struct btrfs_chunk
*chunk
;
2627 struct btrfs_key key
;
2628 struct btrfs_key found_key
;
2629 u64 chunk_tree
= chunk_root
->root_key
.objectid
;
2631 bool retried
= false;
2635 path
= btrfs_alloc_path();
2640 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2641 key
.offset
= (u64
)-1;
2642 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2645 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2648 BUG_ON(ret
== 0); /* Corruption */
2650 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2657 leaf
= path
->nodes
[0];
2658 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2660 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2661 struct btrfs_chunk
);
2662 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2663 btrfs_release_path(path
);
2665 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2666 ret
= btrfs_relocate_chunk(chunk_root
, chunk_tree
,
2675 if (found_key
.offset
== 0)
2677 key
.offset
= found_key
.offset
- 1;
2680 if (failed
&& !retried
) {
2684 } else if (WARN_ON(failed
&& retried
)) {
2688 btrfs_free_path(path
);
2692 static int insert_balance_item(struct btrfs_root
*root
,
2693 struct btrfs_balance_control
*bctl
)
2695 struct btrfs_trans_handle
*trans
;
2696 struct btrfs_balance_item
*item
;
2697 struct btrfs_disk_balance_args disk_bargs
;
2698 struct btrfs_path
*path
;
2699 struct extent_buffer
*leaf
;
2700 struct btrfs_key key
;
2703 path
= btrfs_alloc_path();
2707 trans
= btrfs_start_transaction(root
, 0);
2708 if (IS_ERR(trans
)) {
2709 btrfs_free_path(path
);
2710 return PTR_ERR(trans
);
2713 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2714 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2717 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2722 leaf
= path
->nodes
[0];
2723 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2725 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2727 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2728 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2729 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2730 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2731 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2732 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2734 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2736 btrfs_mark_buffer_dirty(leaf
);
2738 btrfs_free_path(path
);
2739 err
= btrfs_commit_transaction(trans
, root
);
2745 static int del_balance_item(struct btrfs_root
*root
)
2747 struct btrfs_trans_handle
*trans
;
2748 struct btrfs_path
*path
;
2749 struct btrfs_key key
;
2752 path
= btrfs_alloc_path();
2756 trans
= btrfs_start_transaction(root
, 0);
2757 if (IS_ERR(trans
)) {
2758 btrfs_free_path(path
);
2759 return PTR_ERR(trans
);
2762 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2763 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2766 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2774 ret
= btrfs_del_item(trans
, root
, path
);
2776 btrfs_free_path(path
);
2777 err
= btrfs_commit_transaction(trans
, root
);
2784 * This is a heuristic used to reduce the number of chunks balanced on
2785 * resume after balance was interrupted.
2787 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2790 * Turn on soft mode for chunk types that were being converted.
2792 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2793 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2794 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2795 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2796 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2797 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2800 * Turn on usage filter if is not already used. The idea is
2801 * that chunks that we have already balanced should be
2802 * reasonably full. Don't do it for chunks that are being
2803 * converted - that will keep us from relocating unconverted
2804 * (albeit full) chunks.
2806 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2807 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2808 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2809 bctl
->data
.usage
= 90;
2811 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2812 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2813 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2814 bctl
->sys
.usage
= 90;
2816 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2817 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2818 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2819 bctl
->meta
.usage
= 90;
2824 * Should be called with both balance and volume mutexes held to
2825 * serialize other volume operations (add_dev/rm_dev/resize) with
2826 * restriper. Same goes for unset_balance_control.
2828 static void set_balance_control(struct btrfs_balance_control
*bctl
)
2830 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
2832 BUG_ON(fs_info
->balance_ctl
);
2834 spin_lock(&fs_info
->balance_lock
);
2835 fs_info
->balance_ctl
= bctl
;
2836 spin_unlock(&fs_info
->balance_lock
);
2839 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
2841 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
2843 BUG_ON(!fs_info
->balance_ctl
);
2845 spin_lock(&fs_info
->balance_lock
);
2846 fs_info
->balance_ctl
= NULL
;
2847 spin_unlock(&fs_info
->balance_lock
);
2853 * Balance filters. Return 1 if chunk should be filtered out
2854 * (should not be balanced).
2856 static int chunk_profiles_filter(u64 chunk_type
,
2857 struct btrfs_balance_args
*bargs
)
2859 chunk_type
= chunk_to_extended(chunk_type
) &
2860 BTRFS_EXTENDED_PROFILE_MASK
;
2862 if (bargs
->profiles
& chunk_type
)
2868 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
2869 struct btrfs_balance_args
*bargs
)
2871 struct btrfs_block_group_cache
*cache
;
2872 u64 chunk_used
, user_thresh
;
2875 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
2876 chunk_used
= btrfs_block_group_used(&cache
->item
);
2878 if (bargs
->usage
== 0)
2880 else if (bargs
->usage
> 100)
2881 user_thresh
= cache
->key
.offset
;
2883 user_thresh
= div_factor_fine(cache
->key
.offset
,
2886 if (chunk_used
< user_thresh
)
2889 btrfs_put_block_group(cache
);
2893 static int chunk_devid_filter(struct extent_buffer
*leaf
,
2894 struct btrfs_chunk
*chunk
,
2895 struct btrfs_balance_args
*bargs
)
2897 struct btrfs_stripe
*stripe
;
2898 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2901 for (i
= 0; i
< num_stripes
; i
++) {
2902 stripe
= btrfs_stripe_nr(chunk
, i
);
2903 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
2910 /* [pstart, pend) */
2911 static int chunk_drange_filter(struct extent_buffer
*leaf
,
2912 struct btrfs_chunk
*chunk
,
2914 struct btrfs_balance_args
*bargs
)
2916 struct btrfs_stripe
*stripe
;
2917 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
2923 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
2926 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
2927 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)) {
2928 factor
= num_stripes
/ 2;
2929 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID5
) {
2930 factor
= num_stripes
- 1;
2931 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID6
) {
2932 factor
= num_stripes
- 2;
2934 factor
= num_stripes
;
2937 for (i
= 0; i
< num_stripes
; i
++) {
2938 stripe
= btrfs_stripe_nr(chunk
, i
);
2939 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
2942 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
2943 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
2944 do_div(stripe_length
, factor
);
2946 if (stripe_offset
< bargs
->pend
&&
2947 stripe_offset
+ stripe_length
> bargs
->pstart
)
2954 /* [vstart, vend) */
2955 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
2956 struct btrfs_chunk
*chunk
,
2958 struct btrfs_balance_args
*bargs
)
2960 if (chunk_offset
< bargs
->vend
&&
2961 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
2962 /* at least part of the chunk is inside this vrange */
2968 static int chunk_soft_convert_filter(u64 chunk_type
,
2969 struct btrfs_balance_args
*bargs
)
2971 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
2974 chunk_type
= chunk_to_extended(chunk_type
) &
2975 BTRFS_EXTENDED_PROFILE_MASK
;
2977 if (bargs
->target
== chunk_type
)
2983 static int should_balance_chunk(struct btrfs_root
*root
,
2984 struct extent_buffer
*leaf
,
2985 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
2987 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
2988 struct btrfs_balance_args
*bargs
= NULL
;
2989 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2992 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
2993 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
2997 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
2998 bargs
= &bctl
->data
;
2999 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3001 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3002 bargs
= &bctl
->meta
;
3004 /* profiles filter */
3005 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
3006 chunk_profiles_filter(chunk_type
, bargs
)) {
3011 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3012 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
3017 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
3018 chunk_devid_filter(leaf
, chunk
, bargs
)) {
3022 /* drange filter, makes sense only with devid filter */
3023 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
3024 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3029 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
3030 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3034 /* soft profile changing mode */
3035 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
3036 chunk_soft_convert_filter(chunk_type
, bargs
)) {
3041 * limited by count, must be the last filter
3043 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT
)) {
3044 if (bargs
->limit
== 0)
3053 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
3055 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3056 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3057 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
3058 struct list_head
*devices
;
3059 struct btrfs_device
*device
;
3062 struct btrfs_chunk
*chunk
;
3063 struct btrfs_path
*path
;
3064 struct btrfs_key key
;
3065 struct btrfs_key found_key
;
3066 struct btrfs_trans_handle
*trans
;
3067 struct extent_buffer
*leaf
;
3070 int enospc_errors
= 0;
3071 bool counting
= true;
3072 u64 limit_data
= bctl
->data
.limit
;
3073 u64 limit_meta
= bctl
->meta
.limit
;
3074 u64 limit_sys
= bctl
->sys
.limit
;
3076 /* step one make some room on all the devices */
3077 devices
= &fs_info
->fs_devices
->devices
;
3078 list_for_each_entry(device
, devices
, dev_list
) {
3079 old_size
= device
->total_bytes
;
3080 size_to_free
= div_factor(old_size
, 1);
3081 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
3082 if (!device
->writeable
||
3083 device
->total_bytes
- device
->bytes_used
> size_to_free
||
3084 device
->is_tgtdev_for_dev_replace
)
3087 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
3092 trans
= btrfs_start_transaction(dev_root
, 0);
3093 BUG_ON(IS_ERR(trans
));
3095 ret
= btrfs_grow_device(trans
, device
, old_size
);
3098 btrfs_end_transaction(trans
, dev_root
);
3101 /* step two, relocate all the chunks */
3102 path
= btrfs_alloc_path();
3108 /* zero out stat counters */
3109 spin_lock(&fs_info
->balance_lock
);
3110 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
3111 spin_unlock(&fs_info
->balance_lock
);
3114 bctl
->data
.limit
= limit_data
;
3115 bctl
->meta
.limit
= limit_meta
;
3116 bctl
->sys
.limit
= limit_sys
;
3118 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3119 key
.offset
= (u64
)-1;
3120 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3123 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
3124 atomic_read(&fs_info
->balance_cancel_req
)) {
3129 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3134 * this shouldn't happen, it means the last relocate
3138 BUG(); /* FIXME break ? */
3140 ret
= btrfs_previous_item(chunk_root
, path
, 0,
3141 BTRFS_CHUNK_ITEM_KEY
);
3147 leaf
= path
->nodes
[0];
3148 slot
= path
->slots
[0];
3149 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3151 if (found_key
.objectid
!= key
.objectid
)
3154 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3157 spin_lock(&fs_info
->balance_lock
);
3158 bctl
->stat
.considered
++;
3159 spin_unlock(&fs_info
->balance_lock
);
3162 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
3164 btrfs_release_path(path
);
3169 spin_lock(&fs_info
->balance_lock
);
3170 bctl
->stat
.expected
++;
3171 spin_unlock(&fs_info
->balance_lock
);
3175 ret
= btrfs_relocate_chunk(chunk_root
,
3176 chunk_root
->root_key
.objectid
,
3179 if (ret
&& ret
!= -ENOSPC
)
3181 if (ret
== -ENOSPC
) {
3184 spin_lock(&fs_info
->balance_lock
);
3185 bctl
->stat
.completed
++;
3186 spin_unlock(&fs_info
->balance_lock
);
3189 if (found_key
.offset
== 0)
3191 key
.offset
= found_key
.offset
- 1;
3195 btrfs_release_path(path
);
3200 btrfs_free_path(path
);
3201 if (enospc_errors
) {
3202 btrfs_info(fs_info
, "%d enospc errors during balance",
3212 * alloc_profile_is_valid - see if a given profile is valid and reduced
3213 * @flags: profile to validate
3214 * @extended: if true @flags is treated as an extended profile
3216 static int alloc_profile_is_valid(u64 flags
, int extended
)
3218 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3219 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3221 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3223 /* 1) check that all other bits are zeroed */
3227 /* 2) see if profile is reduced */
3229 return !extended
; /* "0" is valid for usual profiles */
3231 /* true if exactly one bit set */
3232 return (flags
& (flags
- 1)) == 0;
3235 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3237 /* cancel requested || normal exit path */
3238 return atomic_read(&fs_info
->balance_cancel_req
) ||
3239 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3240 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3243 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
3247 unset_balance_control(fs_info
);
3248 ret
= del_balance_item(fs_info
->tree_root
);
3250 btrfs_std_error(fs_info
, ret
);
3252 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3256 * Should be called with both balance and volume mutexes held
3258 int btrfs_balance(struct btrfs_balance_control
*bctl
,
3259 struct btrfs_ioctl_balance_args
*bargs
)
3261 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
3268 if (btrfs_fs_closing(fs_info
) ||
3269 atomic_read(&fs_info
->balance_pause_req
) ||
3270 atomic_read(&fs_info
->balance_cancel_req
)) {
3275 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3276 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3280 * In case of mixed groups both data and meta should be picked,
3281 * and identical options should be given for both of them.
3283 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3284 if (mixed
&& (bctl
->flags
& allowed
)) {
3285 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3286 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3287 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3288 btrfs_err(fs_info
, "with mixed groups data and "
3289 "metadata balance options must be the same");
3295 num_devices
= fs_info
->fs_devices
->num_devices
;
3296 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3297 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3298 BUG_ON(num_devices
< 1);
3301 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3302 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
3303 if (num_devices
== 1)
3304 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3305 else if (num_devices
> 1)
3306 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3307 if (num_devices
> 2)
3308 allowed
|= BTRFS_BLOCK_GROUP_RAID5
;
3309 if (num_devices
> 3)
3310 allowed
|= (BTRFS_BLOCK_GROUP_RAID10
|
3311 BTRFS_BLOCK_GROUP_RAID6
);
3312 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3313 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3314 (bctl
->data
.target
& ~allowed
))) {
3315 btrfs_err(fs_info
, "unable to start balance with target "
3316 "data profile %llu",
3321 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3322 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3323 (bctl
->meta
.target
& ~allowed
))) {
3325 "unable to start balance with target metadata profile %llu",
3330 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3331 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3332 (bctl
->sys
.target
& ~allowed
))) {
3334 "unable to start balance with target system profile %llu",
3340 /* allow dup'ed data chunks only in mixed mode */
3341 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3342 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3343 btrfs_err(fs_info
, "dup for data is not allowed");
3348 /* allow to reduce meta or sys integrity only if force set */
3349 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3350 BTRFS_BLOCK_GROUP_RAID10
|
3351 BTRFS_BLOCK_GROUP_RAID5
|
3352 BTRFS_BLOCK_GROUP_RAID6
;
3354 seq
= read_seqbegin(&fs_info
->profiles_lock
);
3356 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3357 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3358 !(bctl
->sys
.target
& allowed
)) ||
3359 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3360 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3361 !(bctl
->meta
.target
& allowed
))) {
3362 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3363 btrfs_info(fs_info
, "force reducing metadata integrity");
3365 btrfs_err(fs_info
, "balance will reduce metadata "
3366 "integrity, use force if you want this");
3371 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
3373 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3374 int num_tolerated_disk_barrier_failures
;
3375 u64 target
= bctl
->sys
.target
;
3377 num_tolerated_disk_barrier_failures
=
3378 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3379 if (num_tolerated_disk_barrier_failures
> 0 &&
3381 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3382 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3383 num_tolerated_disk_barrier_failures
= 0;
3384 else if (num_tolerated_disk_barrier_failures
> 1 &&
3386 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3387 num_tolerated_disk_barrier_failures
= 1;
3389 fs_info
->num_tolerated_disk_barrier_failures
=
3390 num_tolerated_disk_barrier_failures
;
3393 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3394 if (ret
&& ret
!= -EEXIST
)
3397 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3398 BUG_ON(ret
== -EEXIST
);
3399 set_balance_control(bctl
);
3401 BUG_ON(ret
!= -EEXIST
);
3402 spin_lock(&fs_info
->balance_lock
);
3403 update_balance_args(bctl
);
3404 spin_unlock(&fs_info
->balance_lock
);
3407 atomic_inc(&fs_info
->balance_running
);
3408 mutex_unlock(&fs_info
->balance_mutex
);
3410 ret
= __btrfs_balance(fs_info
);
3412 mutex_lock(&fs_info
->balance_mutex
);
3413 atomic_dec(&fs_info
->balance_running
);
3415 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3416 fs_info
->num_tolerated_disk_barrier_failures
=
3417 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3421 memset(bargs
, 0, sizeof(*bargs
));
3422 update_ioctl_balance_args(fs_info
, 0, bargs
);
3425 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3426 balance_need_close(fs_info
)) {
3427 __cancel_balance(fs_info
);
3430 wake_up(&fs_info
->balance_wait_q
);
3434 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3435 __cancel_balance(fs_info
);
3438 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3443 static int balance_kthread(void *data
)
3445 struct btrfs_fs_info
*fs_info
= data
;
3448 mutex_lock(&fs_info
->volume_mutex
);
3449 mutex_lock(&fs_info
->balance_mutex
);
3451 if (fs_info
->balance_ctl
) {
3452 btrfs_info(fs_info
, "continuing balance");
3453 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3456 mutex_unlock(&fs_info
->balance_mutex
);
3457 mutex_unlock(&fs_info
->volume_mutex
);
3462 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3464 struct task_struct
*tsk
;
3466 spin_lock(&fs_info
->balance_lock
);
3467 if (!fs_info
->balance_ctl
) {
3468 spin_unlock(&fs_info
->balance_lock
);
3471 spin_unlock(&fs_info
->balance_lock
);
3473 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3474 btrfs_info(fs_info
, "force skipping balance");
3478 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3479 return PTR_ERR_OR_ZERO(tsk
);
3482 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3484 struct btrfs_balance_control
*bctl
;
3485 struct btrfs_balance_item
*item
;
3486 struct btrfs_disk_balance_args disk_bargs
;
3487 struct btrfs_path
*path
;
3488 struct extent_buffer
*leaf
;
3489 struct btrfs_key key
;
3492 path
= btrfs_alloc_path();
3496 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3497 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3500 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3503 if (ret
> 0) { /* ret = -ENOENT; */
3508 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3514 leaf
= path
->nodes
[0];
3515 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3517 bctl
->fs_info
= fs_info
;
3518 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3519 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3521 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3522 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3523 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3524 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3525 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3526 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3528 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3530 mutex_lock(&fs_info
->volume_mutex
);
3531 mutex_lock(&fs_info
->balance_mutex
);
3533 set_balance_control(bctl
);
3535 mutex_unlock(&fs_info
->balance_mutex
);
3536 mutex_unlock(&fs_info
->volume_mutex
);
3538 btrfs_free_path(path
);
3542 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3546 mutex_lock(&fs_info
->balance_mutex
);
3547 if (!fs_info
->balance_ctl
) {
3548 mutex_unlock(&fs_info
->balance_mutex
);
3552 if (atomic_read(&fs_info
->balance_running
)) {
3553 atomic_inc(&fs_info
->balance_pause_req
);
3554 mutex_unlock(&fs_info
->balance_mutex
);
3556 wait_event(fs_info
->balance_wait_q
,
3557 atomic_read(&fs_info
->balance_running
) == 0);
3559 mutex_lock(&fs_info
->balance_mutex
);
3560 /* we are good with balance_ctl ripped off from under us */
3561 BUG_ON(atomic_read(&fs_info
->balance_running
));
3562 atomic_dec(&fs_info
->balance_pause_req
);
3567 mutex_unlock(&fs_info
->balance_mutex
);
3571 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3573 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
3576 mutex_lock(&fs_info
->balance_mutex
);
3577 if (!fs_info
->balance_ctl
) {
3578 mutex_unlock(&fs_info
->balance_mutex
);
3582 atomic_inc(&fs_info
->balance_cancel_req
);
3584 * if we are running just wait and return, balance item is
3585 * deleted in btrfs_balance in this case
3587 if (atomic_read(&fs_info
->balance_running
)) {
3588 mutex_unlock(&fs_info
->balance_mutex
);
3589 wait_event(fs_info
->balance_wait_q
,
3590 atomic_read(&fs_info
->balance_running
) == 0);
3591 mutex_lock(&fs_info
->balance_mutex
);
3593 /* __cancel_balance needs volume_mutex */
3594 mutex_unlock(&fs_info
->balance_mutex
);
3595 mutex_lock(&fs_info
->volume_mutex
);
3596 mutex_lock(&fs_info
->balance_mutex
);
3598 if (fs_info
->balance_ctl
)
3599 __cancel_balance(fs_info
);
3601 mutex_unlock(&fs_info
->volume_mutex
);
3604 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3605 atomic_dec(&fs_info
->balance_cancel_req
);
3606 mutex_unlock(&fs_info
->balance_mutex
);
3610 static int btrfs_uuid_scan_kthread(void *data
)
3612 struct btrfs_fs_info
*fs_info
= data
;
3613 struct btrfs_root
*root
= fs_info
->tree_root
;
3614 struct btrfs_key key
;
3615 struct btrfs_key max_key
;
3616 struct btrfs_path
*path
= NULL
;
3618 struct extent_buffer
*eb
;
3620 struct btrfs_root_item root_item
;
3622 struct btrfs_trans_handle
*trans
= NULL
;
3624 path
= btrfs_alloc_path();
3631 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3634 max_key
.objectid
= (u64
)-1;
3635 max_key
.type
= BTRFS_ROOT_ITEM_KEY
;
3636 max_key
.offset
= (u64
)-1;
3639 ret
= btrfs_search_forward(root
, &key
, path
, 0);
3646 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
||
3647 (key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
3648 key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) ||
3649 key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
3652 eb
= path
->nodes
[0];
3653 slot
= path
->slots
[0];
3654 item_size
= btrfs_item_size_nr(eb
, slot
);
3655 if (item_size
< sizeof(root_item
))
3658 read_extent_buffer(eb
, &root_item
,
3659 btrfs_item_ptr_offset(eb
, slot
),
3660 (int)sizeof(root_item
));
3661 if (btrfs_root_refs(&root_item
) == 0)
3664 if (!btrfs_is_empty_uuid(root_item
.uuid
) ||
3665 !btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3669 btrfs_release_path(path
);
3671 * 1 - subvol uuid item
3672 * 1 - received_subvol uuid item
3674 trans
= btrfs_start_transaction(fs_info
->uuid_root
, 2);
3675 if (IS_ERR(trans
)) {
3676 ret
= PTR_ERR(trans
);
3684 if (!btrfs_is_empty_uuid(root_item
.uuid
)) {
3685 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3687 BTRFS_UUID_KEY_SUBVOL
,
3690 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3696 if (!btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3697 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3698 root_item
.received_uuid
,
3699 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
3702 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3710 ret
= btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3716 btrfs_release_path(path
);
3717 if (key
.offset
< (u64
)-1) {
3719 } else if (key
.type
< BTRFS_ROOT_ITEM_KEY
) {
3721 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3722 } else if (key
.objectid
< (u64
)-1) {
3724 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3733 btrfs_free_path(path
);
3734 if (trans
&& !IS_ERR(trans
))
3735 btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3737 btrfs_warn(fs_info
, "btrfs_uuid_scan_kthread failed %d", ret
);
3739 fs_info
->update_uuid_tree_gen
= 1;
3740 up(&fs_info
->uuid_tree_rescan_sem
);
3745 * Callback for btrfs_uuid_tree_iterate().
3747 * 0 check succeeded, the entry is not outdated.
3748 * < 0 if an error occured.
3749 * > 0 if the check failed, which means the caller shall remove the entry.
3751 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info
*fs_info
,
3752 u8
*uuid
, u8 type
, u64 subid
)
3754 struct btrfs_key key
;
3756 struct btrfs_root
*subvol_root
;
3758 if (type
!= BTRFS_UUID_KEY_SUBVOL
&&
3759 type
!= BTRFS_UUID_KEY_RECEIVED_SUBVOL
)
3762 key
.objectid
= subid
;
3763 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3764 key
.offset
= (u64
)-1;
3765 subvol_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
3766 if (IS_ERR(subvol_root
)) {
3767 ret
= PTR_ERR(subvol_root
);
3774 case BTRFS_UUID_KEY_SUBVOL
:
3775 if (memcmp(uuid
, subvol_root
->root_item
.uuid
, BTRFS_UUID_SIZE
))
3778 case BTRFS_UUID_KEY_RECEIVED_SUBVOL
:
3779 if (memcmp(uuid
, subvol_root
->root_item
.received_uuid
,
3789 static int btrfs_uuid_rescan_kthread(void *data
)
3791 struct btrfs_fs_info
*fs_info
= (struct btrfs_fs_info
*)data
;
3795 * 1st step is to iterate through the existing UUID tree and
3796 * to delete all entries that contain outdated data.
3797 * 2nd step is to add all missing entries to the UUID tree.
3799 ret
= btrfs_uuid_tree_iterate(fs_info
, btrfs_check_uuid_tree_entry
);
3801 btrfs_warn(fs_info
, "iterating uuid_tree failed %d", ret
);
3802 up(&fs_info
->uuid_tree_rescan_sem
);
3805 return btrfs_uuid_scan_kthread(data
);
3808 int btrfs_create_uuid_tree(struct btrfs_fs_info
*fs_info
)
3810 struct btrfs_trans_handle
*trans
;
3811 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
3812 struct btrfs_root
*uuid_root
;
3813 struct task_struct
*task
;
3820 trans
= btrfs_start_transaction(tree_root
, 2);
3822 return PTR_ERR(trans
);
3824 uuid_root
= btrfs_create_tree(trans
, fs_info
,
3825 BTRFS_UUID_TREE_OBJECTID
);
3826 if (IS_ERR(uuid_root
)) {
3827 btrfs_abort_transaction(trans
, tree_root
,
3828 PTR_ERR(uuid_root
));
3829 return PTR_ERR(uuid_root
);
3832 fs_info
->uuid_root
= uuid_root
;
3834 ret
= btrfs_commit_transaction(trans
, tree_root
);
3838 down(&fs_info
->uuid_tree_rescan_sem
);
3839 task
= kthread_run(btrfs_uuid_scan_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_scan task");
3843 up(&fs_info
->uuid_tree_rescan_sem
);
3844 return PTR_ERR(task
);
3850 int btrfs_check_uuid_tree(struct btrfs_fs_info
*fs_info
)
3852 struct task_struct
*task
;
3854 down(&fs_info
->uuid_tree_rescan_sem
);
3855 task
= kthread_run(btrfs_uuid_rescan_kthread
, fs_info
, "btrfs-uuid");
3857 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3858 btrfs_warn(fs_info
, "failed to start uuid_rescan task");
3859 up(&fs_info
->uuid_tree_rescan_sem
);
3860 return PTR_ERR(task
);
3867 * shrinking a device means finding all of the device extents past
3868 * the new size, and then following the back refs to the chunks.
3869 * The chunk relocation code actually frees the device extent
3871 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
3873 struct btrfs_trans_handle
*trans
;
3874 struct btrfs_root
*root
= device
->dev_root
;
3875 struct btrfs_dev_extent
*dev_extent
= NULL
;
3876 struct btrfs_path
*path
;
3884 bool retried
= false;
3885 struct extent_buffer
*l
;
3886 struct btrfs_key key
;
3887 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
3888 u64 old_total
= btrfs_super_total_bytes(super_copy
);
3889 u64 old_size
= device
->total_bytes
;
3890 u64 diff
= device
->total_bytes
- new_size
;
3892 if (device
->is_tgtdev_for_dev_replace
)
3895 path
= btrfs_alloc_path();
3903 device
->total_bytes
= new_size
;
3904 if (device
->writeable
) {
3905 device
->fs_devices
->total_rw_bytes
-= diff
;
3906 spin_lock(&root
->fs_info
->free_chunk_lock
);
3907 root
->fs_info
->free_chunk_space
-= diff
;
3908 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3910 unlock_chunks(root
);
3913 key
.objectid
= device
->devid
;
3914 key
.offset
= (u64
)-1;
3915 key
.type
= BTRFS_DEV_EXTENT_KEY
;
3918 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3922 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
3927 btrfs_release_path(path
);
3932 slot
= path
->slots
[0];
3933 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
3935 if (key
.objectid
!= device
->devid
) {
3936 btrfs_release_path(path
);
3940 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
3941 length
= btrfs_dev_extent_length(l
, dev_extent
);
3943 if (key
.offset
+ length
<= new_size
) {
3944 btrfs_release_path(path
);
3948 chunk_tree
= btrfs_dev_extent_chunk_tree(l
, dev_extent
);
3949 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
3950 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
3951 btrfs_release_path(path
);
3953 ret
= btrfs_relocate_chunk(root
, chunk_tree
, chunk_objectid
,
3955 if (ret
&& ret
!= -ENOSPC
)
3959 } while (key
.offset
-- > 0);
3961 if (failed
&& !retried
) {
3965 } else if (failed
&& retried
) {
3969 device
->total_bytes
= old_size
;
3970 if (device
->writeable
)
3971 device
->fs_devices
->total_rw_bytes
+= diff
;
3972 spin_lock(&root
->fs_info
->free_chunk_lock
);
3973 root
->fs_info
->free_chunk_space
+= diff
;
3974 spin_unlock(&root
->fs_info
->free_chunk_lock
);
3975 unlock_chunks(root
);
3979 /* Shrinking succeeded, else we would be at "done". */
3980 trans
= btrfs_start_transaction(root
, 0);
3981 if (IS_ERR(trans
)) {
3982 ret
= PTR_ERR(trans
);
3988 device
->disk_total_bytes
= new_size
;
3989 /* Now btrfs_update_device() will change the on-disk size. */
3990 ret
= btrfs_update_device(trans
, device
);
3992 unlock_chunks(root
);
3993 btrfs_end_transaction(trans
, root
);
3996 WARN_ON(diff
> old_total
);
3997 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
3998 unlock_chunks(root
);
3999 btrfs_end_transaction(trans
, root
);
4001 btrfs_free_path(path
);
4005 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
4006 struct btrfs_key
*key
,
4007 struct btrfs_chunk
*chunk
, int item_size
)
4009 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4010 struct btrfs_disk_key disk_key
;
4014 array_size
= btrfs_super_sys_array_size(super_copy
);
4015 if (array_size
+ item_size
+ sizeof(disk_key
)
4016 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
)
4019 ptr
= super_copy
->sys_chunk_array
+ array_size
;
4020 btrfs_cpu_key_to_disk(&disk_key
, key
);
4021 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
4022 ptr
+= sizeof(disk_key
);
4023 memcpy(ptr
, chunk
, item_size
);
4024 item_size
+= sizeof(disk_key
);
4025 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
4030 * sort the devices in descending order by max_avail, total_avail
4032 static int btrfs_cmp_device_info(const void *a
, const void *b
)
4034 const struct btrfs_device_info
*di_a
= a
;
4035 const struct btrfs_device_info
*di_b
= b
;
4037 if (di_a
->max_avail
> di_b
->max_avail
)
4039 if (di_a
->max_avail
< di_b
->max_avail
)
4041 if (di_a
->total_avail
> di_b
->total_avail
)
4043 if (di_a
->total_avail
< di_b
->total_avail
)
4048 static struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
4049 [BTRFS_RAID_RAID10
] = {
4052 .devs_max
= 0, /* 0 == as many as possible */
4054 .devs_increment
= 2,
4057 [BTRFS_RAID_RAID1
] = {
4062 .devs_increment
= 2,
4065 [BTRFS_RAID_DUP
] = {
4070 .devs_increment
= 1,
4073 [BTRFS_RAID_RAID0
] = {
4078 .devs_increment
= 1,
4081 [BTRFS_RAID_SINGLE
] = {
4086 .devs_increment
= 1,
4089 [BTRFS_RAID_RAID5
] = {
4094 .devs_increment
= 1,
4097 [BTRFS_RAID_RAID6
] = {
4102 .devs_increment
= 1,
4107 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
4109 /* TODO allow them to set a preferred stripe size */
4113 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4115 if (!(type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)))
4118 btrfs_set_fs_incompat(info
, RAID56
);
4121 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4122 - sizeof(struct btrfs_item) \
4123 - sizeof(struct btrfs_chunk)) \
4124 / sizeof(struct btrfs_stripe) + 1)
4126 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4127 - 2 * sizeof(struct btrfs_disk_key) \
4128 - 2 * sizeof(struct btrfs_chunk)) \
4129 / sizeof(struct btrfs_stripe) + 1)
4131 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4132 struct btrfs_root
*extent_root
, u64 start
,
4135 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
4136 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
4137 struct list_head
*cur
;
4138 struct map_lookup
*map
= NULL
;
4139 struct extent_map_tree
*em_tree
;
4140 struct extent_map
*em
;
4141 struct btrfs_device_info
*devices_info
= NULL
;
4143 int num_stripes
; /* total number of stripes to allocate */
4144 int data_stripes
; /* number of stripes that count for
4146 int sub_stripes
; /* sub_stripes info for map */
4147 int dev_stripes
; /* stripes per dev */
4148 int devs_max
; /* max devs to use */
4149 int devs_min
; /* min devs needed */
4150 int devs_increment
; /* ndevs has to be a multiple of this */
4151 int ncopies
; /* how many copies to data has */
4153 u64 max_stripe_size
;
4157 u64 raid_stripe_len
= BTRFS_STRIPE_LEN
;
4163 BUG_ON(!alloc_profile_is_valid(type
, 0));
4165 if (list_empty(&fs_devices
->alloc_list
))
4168 index
= __get_raid_index(type
);
4170 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
4171 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
4172 devs_max
= btrfs_raid_array
[index
].devs_max
;
4173 devs_min
= btrfs_raid_array
[index
].devs_min
;
4174 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
4175 ncopies
= btrfs_raid_array
[index
].ncopies
;
4177 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
4178 max_stripe_size
= 1024 * 1024 * 1024;
4179 max_chunk_size
= 10 * max_stripe_size
;
4181 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4182 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
4183 /* for larger filesystems, use larger metadata chunks */
4184 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
4185 max_stripe_size
= 1024 * 1024 * 1024;
4187 max_stripe_size
= 256 * 1024 * 1024;
4188 max_chunk_size
= max_stripe_size
;
4190 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4191 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4192 max_stripe_size
= 32 * 1024 * 1024;
4193 max_chunk_size
= 2 * max_stripe_size
;
4195 devs_max
= BTRFS_MAX_DEVS_SYS_CHUNK
;
4197 btrfs_err(info
, "invalid chunk type 0x%llx requested",
4202 /* we don't want a chunk larger than 10% of writeable space */
4203 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
4206 devices_info
= kzalloc(sizeof(*devices_info
) * fs_devices
->rw_devices
,
4211 cur
= fs_devices
->alloc_list
.next
;
4214 * in the first pass through the devices list, we gather information
4215 * about the available holes on each device.
4218 while (cur
!= &fs_devices
->alloc_list
) {
4219 struct btrfs_device
*device
;
4223 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
4227 if (!device
->writeable
) {
4229 "BTRFS: read-only device in alloc_list\n");
4233 if (!device
->in_fs_metadata
||
4234 device
->is_tgtdev_for_dev_replace
)
4237 if (device
->total_bytes
> device
->bytes_used
)
4238 total_avail
= device
->total_bytes
- device
->bytes_used
;
4242 /* If there is no space on this device, skip it. */
4243 if (total_avail
== 0)
4246 ret
= find_free_dev_extent(trans
, device
,
4247 max_stripe_size
* dev_stripes
,
4248 &dev_offset
, &max_avail
);
4249 if (ret
&& ret
!= -ENOSPC
)
4253 max_avail
= max_stripe_size
* dev_stripes
;
4255 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
4258 if (ndevs
== fs_devices
->rw_devices
) {
4259 WARN(1, "%s: found more than %llu devices\n",
4260 __func__
, fs_devices
->rw_devices
);
4263 devices_info
[ndevs
].dev_offset
= dev_offset
;
4264 devices_info
[ndevs
].max_avail
= max_avail
;
4265 devices_info
[ndevs
].total_avail
= total_avail
;
4266 devices_info
[ndevs
].dev
= device
;
4271 * now sort the devices by hole size / available space
4273 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
4274 btrfs_cmp_device_info
, NULL
);
4276 /* round down to number of usable stripes */
4277 ndevs
-= ndevs
% devs_increment
;
4279 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
4284 if (devs_max
&& ndevs
> devs_max
)
4287 * the primary goal is to maximize the number of stripes, so use as many
4288 * devices as possible, even if the stripes are not maximum sized.
4290 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4291 num_stripes
= ndevs
* dev_stripes
;
4294 * this will have to be fixed for RAID1 and RAID10 over
4297 data_stripes
= num_stripes
/ ncopies
;
4299 if (type
& BTRFS_BLOCK_GROUP_RAID5
) {
4300 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 1,
4301 btrfs_super_stripesize(info
->super_copy
));
4302 data_stripes
= num_stripes
- 1;
4304 if (type
& BTRFS_BLOCK_GROUP_RAID6
) {
4305 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 2,
4306 btrfs_super_stripesize(info
->super_copy
));
4307 data_stripes
= num_stripes
- 2;
4311 * Use the number of data stripes to figure out how big this chunk
4312 * is really going to be in terms of logical address space,
4313 * and compare that answer with the max chunk size
4315 if (stripe_size
* data_stripes
> max_chunk_size
) {
4316 u64 mask
= (1ULL << 24) - 1;
4317 stripe_size
= max_chunk_size
;
4318 do_div(stripe_size
, data_stripes
);
4320 /* bump the answer up to a 16MB boundary */
4321 stripe_size
= (stripe_size
+ mask
) & ~mask
;
4323 /* but don't go higher than the limits we found
4324 * while searching for free extents
4326 if (stripe_size
> devices_info
[ndevs
-1].max_avail
)
4327 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4330 do_div(stripe_size
, dev_stripes
);
4332 /* align to BTRFS_STRIPE_LEN */
4333 do_div(stripe_size
, raid_stripe_len
);
4334 stripe_size
*= raid_stripe_len
;
4336 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4341 map
->num_stripes
= num_stripes
;
4343 for (i
= 0; i
< ndevs
; ++i
) {
4344 for (j
= 0; j
< dev_stripes
; ++j
) {
4345 int s
= i
* dev_stripes
+ j
;
4346 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
4347 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
4351 map
->sector_size
= extent_root
->sectorsize
;
4352 map
->stripe_len
= raid_stripe_len
;
4353 map
->io_align
= raid_stripe_len
;
4354 map
->io_width
= raid_stripe_len
;
4356 map
->sub_stripes
= sub_stripes
;
4358 num_bytes
= stripe_size
* data_stripes
;
4360 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
4362 em
= alloc_extent_map();
4368 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
4369 em
->bdev
= (struct block_device
*)map
;
4371 em
->len
= num_bytes
;
4372 em
->block_start
= 0;
4373 em
->block_len
= em
->len
;
4374 em
->orig_block_len
= stripe_size
;
4376 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4377 write_lock(&em_tree
->lock
);
4378 ret
= add_extent_mapping(em_tree
, em
, 0);
4380 list_add_tail(&em
->list
, &trans
->transaction
->pending_chunks
);
4381 atomic_inc(&em
->refs
);
4383 write_unlock(&em_tree
->lock
);
4385 free_extent_map(em
);
4389 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
4390 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4393 goto error_del_extent
;
4395 free_extent_map(em
);
4396 check_raid56_incompat_flag(extent_root
->fs_info
, type
);
4398 kfree(devices_info
);
4402 write_lock(&em_tree
->lock
);
4403 remove_extent_mapping(em_tree
, em
);
4404 write_unlock(&em_tree
->lock
);
4406 /* One for our allocation */
4407 free_extent_map(em
);
4408 /* One for the tree reference */
4409 free_extent_map(em
);
4411 kfree(devices_info
);
4415 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
4416 struct btrfs_root
*extent_root
,
4417 u64 chunk_offset
, u64 chunk_size
)
4419 struct btrfs_key key
;
4420 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
4421 struct btrfs_device
*device
;
4422 struct btrfs_chunk
*chunk
;
4423 struct btrfs_stripe
*stripe
;
4424 struct extent_map_tree
*em_tree
;
4425 struct extent_map
*em
;
4426 struct map_lookup
*map
;
4433 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4434 read_lock(&em_tree
->lock
);
4435 em
= lookup_extent_mapping(em_tree
, chunk_offset
, chunk_size
);
4436 read_unlock(&em_tree
->lock
);
4439 btrfs_crit(extent_root
->fs_info
, "unable to find logical "
4440 "%Lu len %Lu", chunk_offset
, chunk_size
);
4444 if (em
->start
!= chunk_offset
|| em
->len
!= chunk_size
) {
4445 btrfs_crit(extent_root
->fs_info
, "found a bad mapping, wanted"
4446 " %Lu-%Lu, found %Lu-%Lu", chunk_offset
,
4447 chunk_size
, em
->start
, em
->len
);
4448 free_extent_map(em
);
4452 map
= (struct map_lookup
*)em
->bdev
;
4453 item_size
= btrfs_chunk_item_size(map
->num_stripes
);
4454 stripe_size
= em
->orig_block_len
;
4456 chunk
= kzalloc(item_size
, GFP_NOFS
);
4462 for (i
= 0; i
< map
->num_stripes
; i
++) {
4463 device
= map
->stripes
[i
].dev
;
4464 dev_offset
= map
->stripes
[i
].physical
;
4466 device
->bytes_used
+= stripe_size
;
4467 ret
= btrfs_update_device(trans
, device
);
4470 ret
= btrfs_alloc_dev_extent(trans
, device
,
4471 chunk_root
->root_key
.objectid
,
4472 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4473 chunk_offset
, dev_offset
,
4479 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
4480 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
4482 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
4484 stripe
= &chunk
->stripe
;
4485 for (i
= 0; i
< map
->num_stripes
; i
++) {
4486 device
= map
->stripes
[i
].dev
;
4487 dev_offset
= map
->stripes
[i
].physical
;
4489 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
4490 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
4491 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
4495 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
4496 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
4497 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
4498 btrfs_set_stack_chunk_type(chunk
, map
->type
);
4499 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
4500 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
4501 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
4502 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
4503 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
4505 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
4506 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
4507 key
.offset
= chunk_offset
;
4509 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
4510 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4512 * TODO: Cleanup of inserted chunk root in case of
4515 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
4521 free_extent_map(em
);
4526 * Chunk allocation falls into two parts. The first part does works
4527 * that make the new allocated chunk useable, but not do any operation
4528 * that modifies the chunk tree. The second part does the works that
4529 * require modifying the chunk tree. This division is important for the
4530 * bootstrap process of adding storage to a seed btrfs.
4532 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4533 struct btrfs_root
*extent_root
, u64 type
)
4537 chunk_offset
= find_next_chunk(extent_root
->fs_info
);
4538 return __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
, type
);
4541 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
4542 struct btrfs_root
*root
,
4543 struct btrfs_device
*device
)
4546 u64 sys_chunk_offset
;
4548 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4549 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4552 chunk_offset
= find_next_chunk(fs_info
);
4553 alloc_profile
= btrfs_get_alloc_profile(extent_root
, 0);
4554 ret
= __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
,
4559 sys_chunk_offset
= find_next_chunk(root
->fs_info
);
4560 alloc_profile
= btrfs_get_alloc_profile(fs_info
->chunk_root
, 0);
4561 ret
= __btrfs_alloc_chunk(trans
, extent_root
, sys_chunk_offset
,
4564 btrfs_abort_transaction(trans
, root
, ret
);
4568 ret
= btrfs_add_device(trans
, fs_info
->chunk_root
, device
);
4570 btrfs_abort_transaction(trans
, root
, ret
);
4575 static inline int btrfs_chunk_max_errors(struct map_lookup
*map
)
4579 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4580 BTRFS_BLOCK_GROUP_RAID10
|
4581 BTRFS_BLOCK_GROUP_RAID5
|
4582 BTRFS_BLOCK_GROUP_DUP
)) {
4584 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
) {
4593 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
4595 struct extent_map
*em
;
4596 struct map_lookup
*map
;
4597 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4602 read_lock(&map_tree
->map_tree
.lock
);
4603 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
4604 read_unlock(&map_tree
->map_tree
.lock
);
4608 map
= (struct map_lookup
*)em
->bdev
;
4609 for (i
= 0; i
< map
->num_stripes
; i
++) {
4610 if (map
->stripes
[i
].dev
->missing
) {
4615 if (!map
->stripes
[i
].dev
->writeable
) {
4622 * If the number of missing devices is larger than max errors,
4623 * we can not write the data into that chunk successfully, so
4626 if (miss_ndevs
> btrfs_chunk_max_errors(map
))
4629 free_extent_map(em
);
4633 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
4635 extent_map_tree_init(&tree
->map_tree
);
4638 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
4640 struct extent_map
*em
;
4643 write_lock(&tree
->map_tree
.lock
);
4644 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
4646 remove_extent_mapping(&tree
->map_tree
, em
);
4647 write_unlock(&tree
->map_tree
.lock
);
4651 free_extent_map(em
);
4652 /* once for the tree */
4653 free_extent_map(em
);
4657 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
4659 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4660 struct extent_map
*em
;
4661 struct map_lookup
*map
;
4662 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4665 read_lock(&em_tree
->lock
);
4666 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4667 read_unlock(&em_tree
->lock
);
4670 * We could return errors for these cases, but that could get ugly and
4671 * we'd probably do the same thing which is just not do anything else
4672 * and exit, so return 1 so the callers don't try to use other copies.
4675 btrfs_crit(fs_info
, "No mapping for %Lu-%Lu", logical
,
4680 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4681 btrfs_crit(fs_info
, "Invalid mapping for %Lu-%Lu, got "
4682 "%Lu-%Lu", logical
, logical
+len
, em
->start
,
4683 em
->start
+ em
->len
);
4684 free_extent_map(em
);
4688 map
= (struct map_lookup
*)em
->bdev
;
4689 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4690 ret
= map
->num_stripes
;
4691 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4692 ret
= map
->sub_stripes
;
4693 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
4695 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
4699 free_extent_map(em
);
4701 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
4702 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))
4704 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
4709 unsigned long btrfs_full_stripe_len(struct btrfs_root
*root
,
4710 struct btrfs_mapping_tree
*map_tree
,
4713 struct extent_map
*em
;
4714 struct map_lookup
*map
;
4715 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4716 unsigned long len
= root
->sectorsize
;
4718 read_lock(&em_tree
->lock
);
4719 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4720 read_unlock(&em_tree
->lock
);
4723 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4724 map
= (struct map_lookup
*)em
->bdev
;
4725 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4726 BTRFS_BLOCK_GROUP_RAID6
)) {
4727 len
= map
->stripe_len
* nr_data_stripes(map
);
4729 free_extent_map(em
);
4733 int btrfs_is_parity_mirror(struct btrfs_mapping_tree
*map_tree
,
4734 u64 logical
, u64 len
, int mirror_num
)
4736 struct extent_map
*em
;
4737 struct map_lookup
*map
;
4738 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4741 read_lock(&em_tree
->lock
);
4742 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4743 read_unlock(&em_tree
->lock
);
4746 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4747 map
= (struct map_lookup
*)em
->bdev
;
4748 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
4749 BTRFS_BLOCK_GROUP_RAID6
))
4751 free_extent_map(em
);
4755 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
4756 struct map_lookup
*map
, int first
, int num
,
4757 int optimal
, int dev_replace_is_ongoing
)
4761 struct btrfs_device
*srcdev
;
4763 if (dev_replace_is_ongoing
&&
4764 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
4765 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
4766 srcdev
= fs_info
->dev_replace
.srcdev
;
4771 * try to avoid the drive that is the source drive for a
4772 * dev-replace procedure, only choose it if no other non-missing
4773 * mirror is available
4775 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
4776 if (map
->stripes
[optimal
].dev
->bdev
&&
4777 (tolerance
|| map
->stripes
[optimal
].dev
!= srcdev
))
4779 for (i
= first
; i
< first
+ num
; i
++) {
4780 if (map
->stripes
[i
].dev
->bdev
&&
4781 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
4786 /* we couldn't find one that doesn't fail. Just return something
4787 * and the io error handling code will clean up eventually
4792 static inline int parity_smaller(u64 a
, u64 b
)
4797 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4798 static void sort_parity_stripes(struct btrfs_bio
*bbio
, u64
*raid_map
)
4800 struct btrfs_bio_stripe s
;
4807 for (i
= 0; i
< bbio
->num_stripes
- 1; i
++) {
4808 if (parity_smaller(raid_map
[i
], raid_map
[i
+1])) {
4809 s
= bbio
->stripes
[i
];
4811 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
4812 raid_map
[i
] = raid_map
[i
+1];
4813 bbio
->stripes
[i
+1] = s
;
4821 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
4822 u64 logical
, u64
*length
,
4823 struct btrfs_bio
**bbio_ret
,
4824 int mirror_num
, u64
**raid_map_ret
)
4826 struct extent_map
*em
;
4827 struct map_lookup
*map
;
4828 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4829 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4832 u64 stripe_end_offset
;
4837 u64
*raid_map
= NULL
;
4843 struct btrfs_bio
*bbio
= NULL
;
4844 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
4845 int dev_replace_is_ongoing
= 0;
4846 int num_alloc_stripes
;
4847 int patch_the_first_stripe_for_dev_replace
= 0;
4848 u64 physical_to_patch_in_first_stripe
= 0;
4849 u64 raid56_full_stripe_start
= (u64
)-1;
4851 read_lock(&em_tree
->lock
);
4852 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
4853 read_unlock(&em_tree
->lock
);
4856 btrfs_crit(fs_info
, "unable to find logical %llu len %llu",
4861 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4862 btrfs_crit(fs_info
, "found a bad mapping, wanted %Lu, "
4863 "found %Lu-%Lu", logical
, em
->start
,
4864 em
->start
+ em
->len
);
4865 free_extent_map(em
);
4869 map
= (struct map_lookup
*)em
->bdev
;
4870 offset
= logical
- em
->start
;
4872 stripe_len
= map
->stripe_len
;
4875 * stripe_nr counts the total number of stripes we have to stride
4876 * to get to this block
4878 do_div(stripe_nr
, stripe_len
);
4880 stripe_offset
= stripe_nr
* stripe_len
;
4881 BUG_ON(offset
< stripe_offset
);
4883 /* stripe_offset is the offset of this block in its stripe*/
4884 stripe_offset
= offset
- stripe_offset
;
4886 /* if we're here for raid56, we need to know the stripe aligned start */
4887 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4888 unsigned long full_stripe_len
= stripe_len
* nr_data_stripes(map
);
4889 raid56_full_stripe_start
= offset
;
4891 /* allow a write of a full stripe, but make sure we don't
4892 * allow straddling of stripes
4894 do_div(raid56_full_stripe_start
, full_stripe_len
);
4895 raid56_full_stripe_start
*= full_stripe_len
;
4898 if (rw
& REQ_DISCARD
) {
4899 /* we don't discard raid56 yet */
4901 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
4905 *length
= min_t(u64
, em
->len
- offset
, *length
);
4906 } else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
4908 /* For writes to RAID[56], allow a full stripeset across all disks.
4909 For other RAID types and for RAID[56] reads, just allow a single
4910 stripe (on a single disk). */
4911 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
) &&
4913 max_len
= stripe_len
* nr_data_stripes(map
) -
4914 (offset
- raid56_full_stripe_start
);
4916 /* we limit the length of each bio to what fits in a stripe */
4917 max_len
= stripe_len
- stripe_offset
;
4919 *length
= min_t(u64
, em
->len
- offset
, max_len
);
4921 *length
= em
->len
- offset
;
4924 /* This is for when we're called from btrfs_merge_bio_hook() and all
4925 it cares about is the length */
4929 btrfs_dev_replace_lock(dev_replace
);
4930 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
4931 if (!dev_replace_is_ongoing
)
4932 btrfs_dev_replace_unlock(dev_replace
);
4934 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
4935 !(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) &&
4936 dev_replace
->tgtdev
!= NULL
) {
4938 * in dev-replace case, for repair case (that's the only
4939 * case where the mirror is selected explicitly when
4940 * calling btrfs_map_block), blocks left of the left cursor
4941 * can also be read from the target drive.
4942 * For REQ_GET_READ_MIRRORS, the target drive is added as
4943 * the last one to the array of stripes. For READ, it also
4944 * needs to be supported using the same mirror number.
4945 * If the requested block is not left of the left cursor,
4946 * EIO is returned. This can happen because btrfs_num_copies()
4947 * returns one more in the dev-replace case.
4949 u64 tmp_length
= *length
;
4950 struct btrfs_bio
*tmp_bbio
= NULL
;
4951 int tmp_num_stripes
;
4952 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
4953 int index_srcdev
= 0;
4955 u64 physical_of_found
= 0;
4957 ret
= __btrfs_map_block(fs_info
, REQ_GET_READ_MIRRORS
,
4958 logical
, &tmp_length
, &tmp_bbio
, 0, NULL
);
4960 WARN_ON(tmp_bbio
!= NULL
);
4964 tmp_num_stripes
= tmp_bbio
->num_stripes
;
4965 if (mirror_num
> tmp_num_stripes
) {
4967 * REQ_GET_READ_MIRRORS does not contain this
4968 * mirror, that means that the requested area
4969 * is not left of the left cursor
4977 * process the rest of the function using the mirror_num
4978 * of the source drive. Therefore look it up first.
4979 * At the end, patch the device pointer to the one of the
4982 for (i
= 0; i
< tmp_num_stripes
; i
++) {
4983 if (tmp_bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
4985 * In case of DUP, in order to keep it
4986 * simple, only add the mirror with the
4987 * lowest physical address
4990 physical_of_found
<=
4991 tmp_bbio
->stripes
[i
].physical
)
4996 tmp_bbio
->stripes
[i
].physical
;
5001 mirror_num
= index_srcdev
+ 1;
5002 patch_the_first_stripe_for_dev_replace
= 1;
5003 physical_to_patch_in_first_stripe
= physical_of_found
;
5012 } else if (mirror_num
> map
->num_stripes
) {
5018 stripe_nr_orig
= stripe_nr
;
5019 stripe_nr_end
= ALIGN(offset
+ *length
, map
->stripe_len
);
5020 do_div(stripe_nr_end
, map
->stripe_len
);
5021 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
5024 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5025 if (rw
& REQ_DISCARD
)
5026 num_stripes
= min_t(u64
, map
->num_stripes
,
5027 stripe_nr_end
- stripe_nr_orig
);
5028 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
5029 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
5030 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
))
5031 num_stripes
= map
->num_stripes
;
5032 else if (mirror_num
)
5033 stripe_index
= mirror_num
- 1;
5035 stripe_index
= find_live_mirror(fs_info
, map
, 0,
5037 current
->pid
% map
->num_stripes
,
5038 dev_replace_is_ongoing
);
5039 mirror_num
= stripe_index
+ 1;
5042 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
5043 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) {
5044 num_stripes
= map
->num_stripes
;
5045 } else if (mirror_num
) {
5046 stripe_index
= mirror_num
- 1;
5051 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5052 int factor
= map
->num_stripes
/ map
->sub_stripes
;
5054 stripe_index
= do_div(stripe_nr
, factor
);
5055 stripe_index
*= map
->sub_stripes
;
5057 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
5058 num_stripes
= map
->sub_stripes
;
5059 else if (rw
& REQ_DISCARD
)
5060 num_stripes
= min_t(u64
, map
->sub_stripes
*
5061 (stripe_nr_end
- stripe_nr_orig
),
5063 else if (mirror_num
)
5064 stripe_index
+= mirror_num
- 1;
5066 int old_stripe_index
= stripe_index
;
5067 stripe_index
= find_live_mirror(fs_info
, map
,
5069 map
->sub_stripes
, stripe_index
+
5070 current
->pid
% map
->sub_stripes
,
5071 dev_replace_is_ongoing
);
5072 mirror_num
= stripe_index
- old_stripe_index
+ 1;
5075 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
5076 BTRFS_BLOCK_GROUP_RAID6
)) {
5079 if (bbio_ret
&& ((rw
& REQ_WRITE
) || mirror_num
> 1)
5083 /* push stripe_nr back to the start of the full stripe */
5084 stripe_nr
= raid56_full_stripe_start
;
5085 do_div(stripe_nr
, stripe_len
);
5087 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
5089 /* RAID[56] write or recovery. Return all stripes */
5090 num_stripes
= map
->num_stripes
;
5091 max_errors
= nr_parity_stripes(map
);
5093 raid_map
= kmalloc_array(num_stripes
, sizeof(u64
),
5100 /* Work out the disk rotation on this stripe-set */
5102 rot
= do_div(tmp
, num_stripes
);
5104 /* Fill in the logical address of each stripe */
5105 tmp
= stripe_nr
* nr_data_stripes(map
);
5106 for (i
= 0; i
< nr_data_stripes(map
); i
++)
5107 raid_map
[(i
+rot
) % num_stripes
] =
5108 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
5110 raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
5111 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5112 raid_map
[(i
+rot
+1) % num_stripes
] =
5115 *length
= map
->stripe_len
;
5120 * Mirror #0 or #1 means the original data block.
5121 * Mirror #2 is RAID5 parity block.
5122 * Mirror #3 is RAID6 Q block.
5124 stripe_index
= do_div(stripe_nr
, nr_data_stripes(map
));
5126 stripe_index
= nr_data_stripes(map
) +
5129 /* We distribute the parity blocks across stripes */
5130 tmp
= stripe_nr
+ stripe_index
;
5131 stripe_index
= do_div(tmp
, map
->num_stripes
);
5135 * after this do_div call, stripe_nr is the number of stripes
5136 * on this device we have to walk to find the data, and
5137 * stripe_index is the number of our device in the stripe array
5139 stripe_index
= do_div(stripe_nr
, map
->num_stripes
);
5140 mirror_num
= stripe_index
+ 1;
5142 BUG_ON(stripe_index
>= map
->num_stripes
);
5144 num_alloc_stripes
= num_stripes
;
5145 if (dev_replace_is_ongoing
) {
5146 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
5147 num_alloc_stripes
<<= 1;
5148 if (rw
& REQ_GET_READ_MIRRORS
)
5149 num_alloc_stripes
++;
5151 bbio
= kzalloc(btrfs_bio_size(num_alloc_stripes
), GFP_NOFS
);
5157 atomic_set(&bbio
->error
, 0);
5159 if (rw
& REQ_DISCARD
) {
5161 int sub_stripes
= 0;
5162 u64 stripes_per_dev
= 0;
5163 u32 remaining_stripes
= 0;
5164 u32 last_stripe
= 0;
5167 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
5168 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5171 sub_stripes
= map
->sub_stripes
;
5173 factor
= map
->num_stripes
/ sub_stripes
;
5174 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
5177 &remaining_stripes
);
5178 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
5179 last_stripe
*= sub_stripes
;
5182 for (i
= 0; i
< num_stripes
; i
++) {
5183 bbio
->stripes
[i
].physical
=
5184 map
->stripes
[stripe_index
].physical
+
5185 stripe_offset
+ stripe_nr
* map
->stripe_len
;
5186 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
5188 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5189 BTRFS_BLOCK_GROUP_RAID10
)) {
5190 bbio
->stripes
[i
].length
= stripes_per_dev
*
5193 if (i
/ sub_stripes
< remaining_stripes
)
5194 bbio
->stripes
[i
].length
+=
5198 * Special for the first stripe and
5201 * |-------|...|-------|
5205 if (i
< sub_stripes
)
5206 bbio
->stripes
[i
].length
-=
5209 if (stripe_index
>= last_stripe
&&
5210 stripe_index
<= (last_stripe
+
5212 bbio
->stripes
[i
].length
-=
5215 if (i
== sub_stripes
- 1)
5218 bbio
->stripes
[i
].length
= *length
;
5221 if (stripe_index
== map
->num_stripes
) {
5222 /* This could only happen for RAID0/10 */
5228 for (i
= 0; i
< num_stripes
; i
++) {
5229 bbio
->stripes
[i
].physical
=
5230 map
->stripes
[stripe_index
].physical
+
5232 stripe_nr
* map
->stripe_len
;
5233 bbio
->stripes
[i
].dev
=
5234 map
->stripes
[stripe_index
].dev
;
5239 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
5240 max_errors
= btrfs_chunk_max_errors(map
);
5242 if (dev_replace_is_ongoing
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
5243 dev_replace
->tgtdev
!= NULL
) {
5244 int index_where_to_add
;
5245 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5248 * duplicate the write operations while the dev replace
5249 * procedure is running. Since the copying of the old disk
5250 * to the new disk takes place at run time while the
5251 * filesystem is mounted writable, the regular write
5252 * operations to the old disk have to be duplicated to go
5253 * to the new disk as well.
5254 * Note that device->missing is handled by the caller, and
5255 * that the write to the old disk is already set up in the
5258 index_where_to_add
= num_stripes
;
5259 for (i
= 0; i
< num_stripes
; i
++) {
5260 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5261 /* write to new disk, too */
5262 struct btrfs_bio_stripe
*new =
5263 bbio
->stripes
+ index_where_to_add
;
5264 struct btrfs_bio_stripe
*old
=
5267 new->physical
= old
->physical
;
5268 new->length
= old
->length
;
5269 new->dev
= dev_replace
->tgtdev
;
5270 index_where_to_add
++;
5274 num_stripes
= index_where_to_add
;
5275 } else if (dev_replace_is_ongoing
&& (rw
& REQ_GET_READ_MIRRORS
) &&
5276 dev_replace
->tgtdev
!= NULL
) {
5277 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5278 int index_srcdev
= 0;
5280 u64 physical_of_found
= 0;
5283 * During the dev-replace procedure, the target drive can
5284 * also be used to read data in case it is needed to repair
5285 * a corrupt block elsewhere. This is possible if the
5286 * requested area is left of the left cursor. In this area,
5287 * the target drive is a full copy of the source drive.
5289 for (i
= 0; i
< num_stripes
; i
++) {
5290 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5292 * In case of DUP, in order to keep it
5293 * simple, only add the mirror with the
5294 * lowest physical address
5297 physical_of_found
<=
5298 bbio
->stripes
[i
].physical
)
5302 physical_of_found
= bbio
->stripes
[i
].physical
;
5306 u64 length
= map
->stripe_len
;
5308 if (physical_of_found
+ length
<=
5309 dev_replace
->cursor_left
) {
5310 struct btrfs_bio_stripe
*tgtdev_stripe
=
5311 bbio
->stripes
+ num_stripes
;
5313 tgtdev_stripe
->physical
= physical_of_found
;
5314 tgtdev_stripe
->length
=
5315 bbio
->stripes
[index_srcdev
].length
;
5316 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
5324 bbio
->num_stripes
= num_stripes
;
5325 bbio
->max_errors
= max_errors
;
5326 bbio
->mirror_num
= mirror_num
;
5329 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5330 * mirror_num == num_stripes + 1 && dev_replace target drive is
5331 * available as a mirror
5333 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
5334 WARN_ON(num_stripes
> 1);
5335 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
5336 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
5337 bbio
->mirror_num
= map
->num_stripes
+ 1;
5340 sort_parity_stripes(bbio
, raid_map
);
5341 *raid_map_ret
= raid_map
;
5344 if (dev_replace_is_ongoing
)
5345 btrfs_dev_replace_unlock(dev_replace
);
5346 free_extent_map(em
);
5350 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
5351 u64 logical
, u64
*length
,
5352 struct btrfs_bio
**bbio_ret
, int mirror_num
)
5354 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
5358 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
5359 u64 chunk_start
, u64 physical
, u64 devid
,
5360 u64
**logical
, int *naddrs
, int *stripe_len
)
5362 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
5363 struct extent_map
*em
;
5364 struct map_lookup
*map
;
5372 read_lock(&em_tree
->lock
);
5373 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
5374 read_unlock(&em_tree
->lock
);
5377 printk(KERN_ERR
"BTRFS: couldn't find em for chunk %Lu\n",
5382 if (em
->start
!= chunk_start
) {
5383 printk(KERN_ERR
"BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
5384 em
->start
, chunk_start
);
5385 free_extent_map(em
);
5388 map
= (struct map_lookup
*)em
->bdev
;
5391 rmap_len
= map
->stripe_len
;
5393 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5394 do_div(length
, map
->num_stripes
/ map
->sub_stripes
);
5395 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5396 do_div(length
, map
->num_stripes
);
5397 else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID5
|
5398 BTRFS_BLOCK_GROUP_RAID6
)) {
5399 do_div(length
, nr_data_stripes(map
));
5400 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
5403 buf
= kzalloc(sizeof(u64
) * map
->num_stripes
, GFP_NOFS
);
5404 BUG_ON(!buf
); /* -ENOMEM */
5406 for (i
= 0; i
< map
->num_stripes
; i
++) {
5407 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
5409 if (map
->stripes
[i
].physical
> physical
||
5410 map
->stripes
[i
].physical
+ length
<= physical
)
5413 stripe_nr
= physical
- map
->stripes
[i
].physical
;
5414 do_div(stripe_nr
, map
->stripe_len
);
5416 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5417 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5418 do_div(stripe_nr
, map
->sub_stripes
);
5419 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5420 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5421 } /* else if RAID[56], multiply by nr_data_stripes().
5422 * Alternatively, just use rmap_len below instead of
5423 * map->stripe_len */
5425 bytenr
= chunk_start
+ stripe_nr
* rmap_len
;
5426 WARN_ON(nr
>= map
->num_stripes
);
5427 for (j
= 0; j
< nr
; j
++) {
5428 if (buf
[j
] == bytenr
)
5432 WARN_ON(nr
>= map
->num_stripes
);
5439 *stripe_len
= rmap_len
;
5441 free_extent_map(em
);
5445 static inline void btrfs_end_bbio(struct btrfs_bio
*bbio
, struct bio
*bio
, int err
)
5447 if (likely(bbio
->flags
& BTRFS_BIO_ORIG_BIO_SUBMITTED
))
5448 bio_endio_nodec(bio
, err
);
5450 bio_endio(bio
, err
);
5454 static void btrfs_end_bio(struct bio
*bio
, int err
)
5456 struct btrfs_bio
*bbio
= bio
->bi_private
;
5457 struct btrfs_device
*dev
= bbio
->stripes
[0].dev
;
5458 int is_orig_bio
= 0;
5461 atomic_inc(&bbio
->error
);
5462 if (err
== -EIO
|| err
== -EREMOTEIO
) {
5463 unsigned int stripe_index
=
5464 btrfs_io_bio(bio
)->stripe_index
;
5466 BUG_ON(stripe_index
>= bbio
->num_stripes
);
5467 dev
= bbio
->stripes
[stripe_index
].dev
;
5469 if (bio
->bi_rw
& WRITE
)
5470 btrfs_dev_stat_inc(dev
,
5471 BTRFS_DEV_STAT_WRITE_ERRS
);
5473 btrfs_dev_stat_inc(dev
,
5474 BTRFS_DEV_STAT_READ_ERRS
);
5475 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
5476 btrfs_dev_stat_inc(dev
,
5477 BTRFS_DEV_STAT_FLUSH_ERRS
);
5478 btrfs_dev_stat_print_on_error(dev
);
5483 if (bio
== bbio
->orig_bio
)
5486 btrfs_bio_counter_dec(bbio
->fs_info
);
5488 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5491 bio
= bbio
->orig_bio
;
5494 bio
->bi_private
= bbio
->private;
5495 bio
->bi_end_io
= bbio
->end_io
;
5496 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5497 /* only send an error to the higher layers if it is
5498 * beyond the tolerance of the btrfs bio
5500 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
5504 * this bio is actually up to date, we didn't
5505 * go over the max number of errors
5507 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5511 btrfs_end_bbio(bbio
, bio
, err
);
5512 } else if (!is_orig_bio
) {
5518 * see run_scheduled_bios for a description of why bios are collected for
5521 * This will add one bio to the pending list for a device and make sure
5522 * the work struct is scheduled.
5524 static noinline
void btrfs_schedule_bio(struct btrfs_root
*root
,
5525 struct btrfs_device
*device
,
5526 int rw
, struct bio
*bio
)
5528 int should_queue
= 1;
5529 struct btrfs_pending_bios
*pending_bios
;
5531 if (device
->missing
|| !device
->bdev
) {
5532 bio_endio(bio
, -EIO
);
5536 /* don't bother with additional async steps for reads, right now */
5537 if (!(rw
& REQ_WRITE
)) {
5539 btrfsic_submit_bio(rw
, bio
);
5545 * nr_async_bios allows us to reliably return congestion to the
5546 * higher layers. Otherwise, the async bio makes it appear we have
5547 * made progress against dirty pages when we've really just put it
5548 * on a queue for later
5550 atomic_inc(&root
->fs_info
->nr_async_bios
);
5551 WARN_ON(bio
->bi_next
);
5552 bio
->bi_next
= NULL
;
5555 spin_lock(&device
->io_lock
);
5556 if (bio
->bi_rw
& REQ_SYNC
)
5557 pending_bios
= &device
->pending_sync_bios
;
5559 pending_bios
= &device
->pending_bios
;
5561 if (pending_bios
->tail
)
5562 pending_bios
->tail
->bi_next
= bio
;
5564 pending_bios
->tail
= bio
;
5565 if (!pending_bios
->head
)
5566 pending_bios
->head
= bio
;
5567 if (device
->running_pending
)
5570 spin_unlock(&device
->io_lock
);
5573 btrfs_queue_work(root
->fs_info
->submit_workers
,
5577 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
5580 struct bio_vec
*prev
;
5581 struct request_queue
*q
= bdev_get_queue(bdev
);
5582 unsigned int max_sectors
= queue_max_sectors(q
);
5583 struct bvec_merge_data bvm
= {
5585 .bi_sector
= sector
,
5586 .bi_rw
= bio
->bi_rw
,
5589 if (WARN_ON(bio
->bi_vcnt
== 0))
5592 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
5593 if (bio_sectors(bio
) > max_sectors
)
5596 if (!q
->merge_bvec_fn
)
5599 bvm
.bi_size
= bio
->bi_iter
.bi_size
- prev
->bv_len
;
5600 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
5605 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5606 struct bio
*bio
, u64 physical
, int dev_nr
,
5609 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
5611 bio
->bi_private
= bbio
;
5612 btrfs_io_bio(bio
)->stripe_index
= dev_nr
;
5613 bio
->bi_end_io
= btrfs_end_bio
;
5614 bio
->bi_iter
.bi_sector
= physical
>> 9;
5617 struct rcu_string
*name
;
5620 name
= rcu_dereference(dev
->name
);
5621 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5622 "(%s id %llu), size=%u\n", rw
,
5623 (u64
)bio
->bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
5624 name
->str
, dev
->devid
, bio
->bi_size
);
5628 bio
->bi_bdev
= dev
->bdev
;
5630 btrfs_bio_counter_inc_noblocked(root
->fs_info
);
5633 btrfs_schedule_bio(root
, dev
, rw
, bio
);
5635 btrfsic_submit_bio(rw
, bio
);
5638 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5639 struct bio
*first_bio
, struct btrfs_device
*dev
,
5640 int dev_nr
, int rw
, int async
)
5642 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
5644 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
5645 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
5648 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
5652 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
5653 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5654 bvec
->bv_offset
) < bvec
->bv_len
) {
5655 u64 len
= bio
->bi_iter
.bi_size
;
5657 atomic_inc(&bbio
->stripes_pending
);
5658 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
5666 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
5670 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
5672 atomic_inc(&bbio
->error
);
5673 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5674 /* Shoud be the original bio. */
5675 WARN_ON(bio
!= bbio
->orig_bio
);
5677 bio
->bi_private
= bbio
->private;
5678 bio
->bi_end_io
= bbio
->end_io
;
5679 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5680 bio
->bi_iter
.bi_sector
= logical
>> 9;
5682 btrfs_end_bbio(bbio
, bio
, -EIO
);
5686 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
5687 int mirror_num
, int async_submit
)
5689 struct btrfs_device
*dev
;
5690 struct bio
*first_bio
= bio
;
5691 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
5694 u64
*raid_map
= NULL
;
5698 struct btrfs_bio
*bbio
= NULL
;
5700 length
= bio
->bi_iter
.bi_size
;
5701 map_length
= length
;
5703 btrfs_bio_counter_inc_blocked(root
->fs_info
);
5704 ret
= __btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
5705 mirror_num
, &raid_map
);
5707 btrfs_bio_counter_dec(root
->fs_info
);
5711 total_devs
= bbio
->num_stripes
;
5712 bbio
->orig_bio
= first_bio
;
5713 bbio
->private = first_bio
->bi_private
;
5714 bbio
->end_io
= first_bio
->bi_end_io
;
5715 bbio
->fs_info
= root
->fs_info
;
5716 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
5719 /* In this case, map_length has been set to the length of
5720 a single stripe; not the whole write */
5722 ret
= raid56_parity_write(root
, bio
, bbio
,
5723 raid_map
, map_length
);
5725 ret
= raid56_parity_recover(root
, bio
, bbio
,
5726 raid_map
, map_length
,
5730 * FIXME, replace dosen't support raid56 yet, please fix
5733 btrfs_bio_counter_dec(root
->fs_info
);
5737 if (map_length
< length
) {
5738 btrfs_crit(root
->fs_info
, "mapping failed logical %llu bio len %llu len %llu",
5739 logical
, length
, map_length
);
5743 while (dev_nr
< total_devs
) {
5744 dev
= bbio
->stripes
[dev_nr
].dev
;
5745 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
5746 bbio_error(bbio
, first_bio
, logical
);
5752 * Check and see if we're ok with this bio based on it's size
5753 * and offset with the given device.
5755 if (!bio_size_ok(dev
->bdev
, first_bio
,
5756 bbio
->stripes
[dev_nr
].physical
>> 9)) {
5757 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
5758 dev_nr
, rw
, async_submit
);
5764 if (dev_nr
< total_devs
- 1) {
5765 bio
= btrfs_bio_clone(first_bio
, GFP_NOFS
);
5766 BUG_ON(!bio
); /* -ENOMEM */
5769 bbio
->flags
|= BTRFS_BIO_ORIG_BIO_SUBMITTED
;
5772 submit_stripe_bio(root
, bbio
, bio
,
5773 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
5777 btrfs_bio_counter_dec(root
->fs_info
);
5781 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
5784 struct btrfs_device
*device
;
5785 struct btrfs_fs_devices
*cur_devices
;
5787 cur_devices
= fs_info
->fs_devices
;
5788 while (cur_devices
) {
5790 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5791 device
= __find_device(&cur_devices
->devices
,
5796 cur_devices
= cur_devices
->seed
;
5801 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
5802 u64 devid
, u8
*dev_uuid
)
5804 struct btrfs_device
*device
;
5805 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
5807 device
= btrfs_alloc_device(NULL
, &devid
, dev_uuid
);
5811 list_add(&device
->dev_list
, &fs_devices
->devices
);
5812 device
->fs_devices
= fs_devices
;
5813 fs_devices
->num_devices
++;
5815 device
->missing
= 1;
5816 fs_devices
->missing_devices
++;
5822 * btrfs_alloc_device - allocate struct btrfs_device
5823 * @fs_info: used only for generating a new devid, can be NULL if
5824 * devid is provided (i.e. @devid != NULL).
5825 * @devid: a pointer to devid for this device. If NULL a new devid
5827 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5830 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5831 * on error. Returned struct is not linked onto any lists and can be
5832 * destroyed with kfree() right away.
5834 struct btrfs_device
*btrfs_alloc_device(struct btrfs_fs_info
*fs_info
,
5838 struct btrfs_device
*dev
;
5841 if (WARN_ON(!devid
&& !fs_info
))
5842 return ERR_PTR(-EINVAL
);
5844 dev
= __alloc_device();
5853 ret
= find_next_devid(fs_info
, &tmp
);
5856 return ERR_PTR(ret
);
5862 memcpy(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
);
5864 generate_random_uuid(dev
->uuid
);
5866 btrfs_init_work(&dev
->work
, btrfs_submit_helper
,
5867 pending_bios_fn
, NULL
, NULL
);
5872 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
5873 struct extent_buffer
*leaf
,
5874 struct btrfs_chunk
*chunk
)
5876 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
5877 struct map_lookup
*map
;
5878 struct extent_map
*em
;
5882 u8 uuid
[BTRFS_UUID_SIZE
];
5887 logical
= key
->offset
;
5888 length
= btrfs_chunk_length(leaf
, chunk
);
5890 read_lock(&map_tree
->map_tree
.lock
);
5891 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
5892 read_unlock(&map_tree
->map_tree
.lock
);
5894 /* already mapped? */
5895 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
5896 free_extent_map(em
);
5899 free_extent_map(em
);
5902 em
= alloc_extent_map();
5905 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
5906 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
5908 free_extent_map(em
);
5912 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
5913 em
->bdev
= (struct block_device
*)map
;
5914 em
->start
= logical
;
5917 em
->block_start
= 0;
5918 em
->block_len
= em
->len
;
5920 map
->num_stripes
= num_stripes
;
5921 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
5922 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
5923 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
5924 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
5925 map
->type
= btrfs_chunk_type(leaf
, chunk
);
5926 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
5927 for (i
= 0; i
< num_stripes
; i
++) {
5928 map
->stripes
[i
].physical
=
5929 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
5930 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
5931 read_extent_buffer(leaf
, uuid
, (unsigned long)
5932 btrfs_stripe_dev_uuid_nr(chunk
, i
),
5934 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
5936 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
5937 free_extent_map(em
);
5940 if (!map
->stripes
[i
].dev
) {
5941 map
->stripes
[i
].dev
=
5942 add_missing_dev(root
, devid
, uuid
);
5943 if (!map
->stripes
[i
].dev
) {
5944 free_extent_map(em
);
5948 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
5951 write_lock(&map_tree
->map_tree
.lock
);
5952 ret
= add_extent_mapping(&map_tree
->map_tree
, em
, 0);
5953 write_unlock(&map_tree
->map_tree
.lock
);
5954 BUG_ON(ret
); /* Tree corruption */
5955 free_extent_map(em
);
5960 static void fill_device_from_item(struct extent_buffer
*leaf
,
5961 struct btrfs_dev_item
*dev_item
,
5962 struct btrfs_device
*device
)
5966 device
->devid
= btrfs_device_id(leaf
, dev_item
);
5967 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
5968 device
->total_bytes
= device
->disk_total_bytes
;
5969 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
5970 device
->type
= btrfs_device_type(leaf
, dev_item
);
5971 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
5972 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
5973 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
5974 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
5975 device
->is_tgtdev_for_dev_replace
= 0;
5977 ptr
= btrfs_device_uuid(dev_item
);
5978 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
5981 static int open_seed_devices(struct btrfs_root
*root
, u8
*fsid
)
5983 struct btrfs_fs_devices
*fs_devices
;
5986 BUG_ON(!mutex_is_locked(&uuid_mutex
));
5988 fs_devices
= root
->fs_info
->fs_devices
->seed
;
5989 while (fs_devices
) {
5990 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
5994 fs_devices
= fs_devices
->seed
;
5997 fs_devices
= find_fsid(fsid
);
6003 fs_devices
= clone_fs_devices(fs_devices
);
6004 if (IS_ERR(fs_devices
)) {
6005 ret
= PTR_ERR(fs_devices
);
6009 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
6010 root
->fs_info
->bdev_holder
);
6012 free_fs_devices(fs_devices
);
6016 if (!fs_devices
->seeding
) {
6017 __btrfs_close_devices(fs_devices
);
6018 free_fs_devices(fs_devices
);
6023 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
6024 root
->fs_info
->fs_devices
->seed
= fs_devices
;
6029 static int read_one_dev(struct btrfs_root
*root
,
6030 struct extent_buffer
*leaf
,
6031 struct btrfs_dev_item
*dev_item
)
6033 struct btrfs_device
*device
;
6036 u8 fs_uuid
[BTRFS_UUID_SIZE
];
6037 u8 dev_uuid
[BTRFS_UUID_SIZE
];
6039 devid
= btrfs_device_id(leaf
, dev_item
);
6040 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
6042 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
6045 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
6046 ret
= open_seed_devices(root
, fs_uuid
);
6047 if (ret
&& !btrfs_test_opt(root
, DEGRADED
))
6051 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
6052 if (!device
|| !device
->bdev
) {
6053 if (!btrfs_test_opt(root
, DEGRADED
))
6057 btrfs_warn(root
->fs_info
, "devid %llu missing", devid
);
6058 device
= add_missing_dev(root
, devid
, dev_uuid
);
6061 } else if (!device
->missing
) {
6063 * this happens when a device that was properly setup
6064 * in the device info lists suddenly goes bad.
6065 * device->bdev is NULL, and so we have to set
6066 * device->missing to one here
6068 root
->fs_info
->fs_devices
->missing_devices
++;
6069 device
->missing
= 1;
6073 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
6074 BUG_ON(device
->writeable
);
6075 if (device
->generation
!=
6076 btrfs_device_generation(leaf
, dev_item
))
6080 fill_device_from_item(leaf
, dev_item
, device
);
6081 device
->in_fs_metadata
= 1;
6082 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
6083 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
6084 spin_lock(&root
->fs_info
->free_chunk_lock
);
6085 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
6087 spin_unlock(&root
->fs_info
->free_chunk_lock
);
6093 int btrfs_read_sys_array(struct btrfs_root
*root
)
6095 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
6096 struct extent_buffer
*sb
;
6097 struct btrfs_disk_key
*disk_key
;
6098 struct btrfs_chunk
*chunk
;
6100 unsigned long sb_ptr
;
6106 struct btrfs_key key
;
6108 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
,
6109 BTRFS_SUPER_INFO_SIZE
);
6112 btrfs_set_buffer_uptodate(sb
);
6113 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
6115 * The sb extent buffer is artifical and just used to read the system array.
6116 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6117 * pages up-to-date when the page is larger: extent does not cover the
6118 * whole page and consequently check_page_uptodate does not find all
6119 * the page's extents up-to-date (the hole beyond sb),
6120 * write_extent_buffer then triggers a WARN_ON.
6122 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6123 * but sb spans only this function. Add an explicit SetPageUptodate call
6124 * to silence the warning eg. on PowerPC 64.
6126 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
6127 SetPageUptodate(sb
->pages
[0]);
6129 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
6130 array_size
= btrfs_super_sys_array_size(super_copy
);
6132 ptr
= super_copy
->sys_chunk_array
;
6133 sb_ptr
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
6136 while (cur
< array_size
) {
6137 disk_key
= (struct btrfs_disk_key
*)ptr
;
6138 btrfs_disk_key_to_cpu(&key
, disk_key
);
6140 len
= sizeof(*disk_key
); ptr
+= len
;
6144 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6145 chunk
= (struct btrfs_chunk
*)sb_ptr
;
6146 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
6149 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
6150 len
= btrfs_chunk_item_size(num_stripes
);
6159 free_extent_buffer(sb
);
6163 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
6165 struct btrfs_path
*path
;
6166 struct extent_buffer
*leaf
;
6167 struct btrfs_key key
;
6168 struct btrfs_key found_key
;
6172 root
= root
->fs_info
->chunk_root
;
6174 path
= btrfs_alloc_path();
6178 mutex_lock(&uuid_mutex
);
6182 * Read all device items, and then all the chunk items. All
6183 * device items are found before any chunk item (their object id
6184 * is smaller than the lowest possible object id for a chunk
6185 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6187 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
6190 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6194 leaf
= path
->nodes
[0];
6195 slot
= path
->slots
[0];
6196 if (slot
>= btrfs_header_nritems(leaf
)) {
6197 ret
= btrfs_next_leaf(root
, path
);
6204 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6205 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
6206 struct btrfs_dev_item
*dev_item
;
6207 dev_item
= btrfs_item_ptr(leaf
, slot
,
6208 struct btrfs_dev_item
);
6209 ret
= read_one_dev(root
, leaf
, dev_item
);
6212 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6213 struct btrfs_chunk
*chunk
;
6214 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
6215 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
6223 unlock_chunks(root
);
6224 mutex_unlock(&uuid_mutex
);
6226 btrfs_free_path(path
);
6230 void btrfs_init_devices_late(struct btrfs_fs_info
*fs_info
)
6232 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6233 struct btrfs_device
*device
;
6235 while (fs_devices
) {
6236 mutex_lock(&fs_devices
->device_list_mutex
);
6237 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
6238 device
->dev_root
= fs_info
->dev_root
;
6239 mutex_unlock(&fs_devices
->device_list_mutex
);
6241 fs_devices
= fs_devices
->seed
;
6245 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
6249 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6250 btrfs_dev_stat_reset(dev
, i
);
6253 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
6255 struct btrfs_key key
;
6256 struct btrfs_key found_key
;
6257 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6258 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6259 struct extent_buffer
*eb
;
6262 struct btrfs_device
*device
;
6263 struct btrfs_path
*path
= NULL
;
6266 path
= btrfs_alloc_path();
6272 mutex_lock(&fs_devices
->device_list_mutex
);
6273 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6275 struct btrfs_dev_stats_item
*ptr
;
6278 key
.type
= BTRFS_DEV_STATS_KEY
;
6279 key
.offset
= device
->devid
;
6280 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
6282 __btrfs_reset_dev_stats(device
);
6283 device
->dev_stats_valid
= 1;
6284 btrfs_release_path(path
);
6287 slot
= path
->slots
[0];
6288 eb
= path
->nodes
[0];
6289 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
6290 item_size
= btrfs_item_size_nr(eb
, slot
);
6292 ptr
= btrfs_item_ptr(eb
, slot
,
6293 struct btrfs_dev_stats_item
);
6295 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6296 if (item_size
>= (1 + i
) * sizeof(__le64
))
6297 btrfs_dev_stat_set(device
, i
,
6298 btrfs_dev_stats_value(eb
, ptr
, i
));
6300 btrfs_dev_stat_reset(device
, i
);
6303 device
->dev_stats_valid
= 1;
6304 btrfs_dev_stat_print_on_load(device
);
6305 btrfs_release_path(path
);
6307 mutex_unlock(&fs_devices
->device_list_mutex
);
6310 btrfs_free_path(path
);
6311 return ret
< 0 ? ret
: 0;
6314 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
6315 struct btrfs_root
*dev_root
,
6316 struct btrfs_device
*device
)
6318 struct btrfs_path
*path
;
6319 struct btrfs_key key
;
6320 struct extent_buffer
*eb
;
6321 struct btrfs_dev_stats_item
*ptr
;
6326 key
.type
= BTRFS_DEV_STATS_KEY
;
6327 key
.offset
= device
->devid
;
6329 path
= btrfs_alloc_path();
6331 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
6333 printk_in_rcu(KERN_WARNING
"BTRFS: "
6334 "error %d while searching for dev_stats item for device %s!\n",
6335 ret
, rcu_str_deref(device
->name
));
6340 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
6341 /* need to delete old one and insert a new one */
6342 ret
= btrfs_del_item(trans
, dev_root
, path
);
6344 printk_in_rcu(KERN_WARNING
"BTRFS: "
6345 "delete too small dev_stats item for device %s failed %d!\n",
6346 rcu_str_deref(device
->name
), ret
);
6353 /* need to insert a new item */
6354 btrfs_release_path(path
);
6355 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
6356 &key
, sizeof(*ptr
));
6358 printk_in_rcu(KERN_WARNING
"BTRFS: "
6359 "insert dev_stats item for device %s failed %d!\n",
6360 rcu_str_deref(device
->name
), ret
);
6365 eb
= path
->nodes
[0];
6366 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
6367 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6368 btrfs_set_dev_stats_value(eb
, ptr
, i
,
6369 btrfs_dev_stat_read(device
, i
));
6370 btrfs_mark_buffer_dirty(eb
);
6373 btrfs_free_path(path
);
6378 * called from commit_transaction. Writes all changed device stats to disk.
6380 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
6381 struct btrfs_fs_info
*fs_info
)
6383 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6384 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6385 struct btrfs_device
*device
;
6389 mutex_lock(&fs_devices
->device_list_mutex
);
6390 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6391 if (!device
->dev_stats_valid
|| !btrfs_dev_stats_dirty(device
))
6394 stats_cnt
= atomic_read(&device
->dev_stats_ccnt
);
6395 ret
= update_dev_stat_item(trans
, dev_root
, device
);
6397 atomic_sub(stats_cnt
, &device
->dev_stats_ccnt
);
6399 mutex_unlock(&fs_devices
->device_list_mutex
);
6404 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
6406 btrfs_dev_stat_inc(dev
, index
);
6407 btrfs_dev_stat_print_on_error(dev
);
6410 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
6412 if (!dev
->dev_stats_valid
)
6414 printk_ratelimited_in_rcu(KERN_ERR
"BTRFS: "
6415 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6416 rcu_str_deref(dev
->name
),
6417 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6418 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6419 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6420 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6421 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6424 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
6428 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6429 if (btrfs_dev_stat_read(dev
, i
) != 0)
6431 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
6432 return; /* all values == 0, suppress message */
6434 printk_in_rcu(KERN_INFO
"BTRFS: "
6435 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6436 rcu_str_deref(dev
->name
),
6437 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6438 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6439 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6440 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6441 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6444 int btrfs_get_dev_stats(struct btrfs_root
*root
,
6445 struct btrfs_ioctl_get_dev_stats
*stats
)
6447 struct btrfs_device
*dev
;
6448 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
6451 mutex_lock(&fs_devices
->device_list_mutex
);
6452 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
6453 mutex_unlock(&fs_devices
->device_list_mutex
);
6456 btrfs_warn(root
->fs_info
, "get dev_stats failed, device not found");
6458 } else if (!dev
->dev_stats_valid
) {
6459 btrfs_warn(root
->fs_info
, "get dev_stats failed, not yet valid");
6461 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
6462 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6463 if (stats
->nr_items
> i
)
6465 btrfs_dev_stat_read_and_reset(dev
, i
);
6467 btrfs_dev_stat_reset(dev
, i
);
6470 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6471 if (stats
->nr_items
> i
)
6472 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
6474 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
6475 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
6479 int btrfs_scratch_superblock(struct btrfs_device
*device
)
6481 struct buffer_head
*bh
;
6482 struct btrfs_super_block
*disk_super
;
6484 bh
= btrfs_read_dev_super(device
->bdev
);
6487 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
6489 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
6490 set_buffer_dirty(bh
);
6491 sync_dirty_buffer(bh
);