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 DEFINE_MUTEX(uuid_mutex
);
54 static LIST_HEAD(fs_uuids
);
55 struct list_head
*btrfs_get_fs_uuids(void)
60 static struct btrfs_fs_devices
*__alloc_fs_devices(void)
62 struct btrfs_fs_devices
*fs_devs
;
64 fs_devs
= kzalloc(sizeof(*fs_devs
), GFP_NOFS
);
66 return ERR_PTR(-ENOMEM
);
68 mutex_init(&fs_devs
->device_list_mutex
);
70 INIT_LIST_HEAD(&fs_devs
->devices
);
71 INIT_LIST_HEAD(&fs_devs
->resized_devices
);
72 INIT_LIST_HEAD(&fs_devs
->alloc_list
);
73 INIT_LIST_HEAD(&fs_devs
->list
);
79 * alloc_fs_devices - allocate struct btrfs_fs_devices
80 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
83 * Return: a pointer to a new &struct btrfs_fs_devices on success;
84 * ERR_PTR() on error. Returned struct is not linked onto any lists and
85 * can be destroyed with kfree() right away.
87 static struct btrfs_fs_devices
*alloc_fs_devices(const u8
*fsid
)
89 struct btrfs_fs_devices
*fs_devs
;
91 fs_devs
= __alloc_fs_devices();
96 memcpy(fs_devs
->fsid
, fsid
, BTRFS_FSID_SIZE
);
98 generate_random_uuid(fs_devs
->fsid
);
103 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
105 struct btrfs_device
*device
;
106 WARN_ON(fs_devices
->opened
);
107 while (!list_empty(&fs_devices
->devices
)) {
108 device
= list_entry(fs_devices
->devices
.next
,
109 struct btrfs_device
, dev_list
);
110 list_del(&device
->dev_list
);
111 rcu_string_free(device
->name
);
117 static void btrfs_kobject_uevent(struct block_device
*bdev
,
118 enum kobject_action action
)
122 ret
= kobject_uevent(&disk_to_dev(bdev
->bd_disk
)->kobj
, action
);
124 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
126 kobject_name(&disk_to_dev(bdev
->bd_disk
)->kobj
),
127 &disk_to_dev(bdev
->bd_disk
)->kobj
);
130 void btrfs_cleanup_fs_uuids(void)
132 struct btrfs_fs_devices
*fs_devices
;
134 while (!list_empty(&fs_uuids
)) {
135 fs_devices
= list_entry(fs_uuids
.next
,
136 struct btrfs_fs_devices
, list
);
137 list_del(&fs_devices
->list
);
138 free_fs_devices(fs_devices
);
142 static struct btrfs_device
*__alloc_device(void)
144 struct btrfs_device
*dev
;
146 dev
= kzalloc(sizeof(*dev
), GFP_NOFS
);
148 return ERR_PTR(-ENOMEM
);
150 INIT_LIST_HEAD(&dev
->dev_list
);
151 INIT_LIST_HEAD(&dev
->dev_alloc_list
);
152 INIT_LIST_HEAD(&dev
->resized_list
);
154 spin_lock_init(&dev
->io_lock
);
156 spin_lock_init(&dev
->reada_lock
);
157 atomic_set(&dev
->reada_in_flight
, 0);
158 atomic_set(&dev
->dev_stats_ccnt
, 0);
159 INIT_RADIX_TREE(&dev
->reada_zones
, GFP_NOFS
& ~__GFP_WAIT
);
160 INIT_RADIX_TREE(&dev
->reada_extents
, GFP_NOFS
& ~__GFP_WAIT
);
165 static noinline
struct btrfs_device
*__find_device(struct list_head
*head
,
168 struct btrfs_device
*dev
;
170 list_for_each_entry(dev
, head
, dev_list
) {
171 if (dev
->devid
== devid
&&
172 (!uuid
|| !memcmp(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
))) {
179 static noinline
struct btrfs_fs_devices
*find_fsid(u8
*fsid
)
181 struct btrfs_fs_devices
*fs_devices
;
183 list_for_each_entry(fs_devices
, &fs_uuids
, list
) {
184 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
191 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
192 int flush
, struct block_device
**bdev
,
193 struct buffer_head
**bh
)
197 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
200 ret
= PTR_ERR(*bdev
);
201 printk(KERN_INFO
"BTRFS: open %s failed\n", device_path
);
206 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
207 ret
= set_blocksize(*bdev
, 4096);
209 blkdev_put(*bdev
, flags
);
212 invalidate_bdev(*bdev
);
213 *bh
= btrfs_read_dev_super(*bdev
);
216 blkdev_put(*bdev
, flags
);
228 static void requeue_list(struct btrfs_pending_bios
*pending_bios
,
229 struct bio
*head
, struct bio
*tail
)
232 struct bio
*old_head
;
234 old_head
= pending_bios
->head
;
235 pending_bios
->head
= head
;
236 if (pending_bios
->tail
)
237 tail
->bi_next
= old_head
;
239 pending_bios
->tail
= tail
;
243 * we try to collect pending bios for a device so we don't get a large
244 * number of procs sending bios down to the same device. This greatly
245 * improves the schedulers ability to collect and merge the bios.
247 * But, it also turns into a long list of bios to process and that is sure
248 * to eventually make the worker thread block. The solution here is to
249 * make some progress and then put this work struct back at the end of
250 * the list if the block device is congested. This way, multiple devices
251 * can make progress from a single worker thread.
253 static noinline
void run_scheduled_bios(struct btrfs_device
*device
)
256 struct backing_dev_info
*bdi
;
257 struct btrfs_fs_info
*fs_info
;
258 struct btrfs_pending_bios
*pending_bios
;
262 unsigned long num_run
;
263 unsigned long batch_run
= 0;
265 unsigned long last_waited
= 0;
267 int sync_pending
= 0;
268 struct blk_plug plug
;
271 * this function runs all the bios we've collected for
272 * a particular device. We don't want to wander off to
273 * another device without first sending all of these down.
274 * So, setup a plug here and finish it off before we return
276 blk_start_plug(&plug
);
278 bdi
= blk_get_backing_dev_info(device
->bdev
);
279 fs_info
= device
->dev_root
->fs_info
;
280 limit
= btrfs_async_submit_limit(fs_info
);
281 limit
= limit
* 2 / 3;
284 spin_lock(&device
->io_lock
);
289 /* take all the bios off the list at once and process them
290 * later on (without the lock held). But, remember the
291 * tail and other pointers so the bios can be properly reinserted
292 * into the list if we hit congestion
294 if (!force_reg
&& device
->pending_sync_bios
.head
) {
295 pending_bios
= &device
->pending_sync_bios
;
298 pending_bios
= &device
->pending_bios
;
302 pending
= pending_bios
->head
;
303 tail
= pending_bios
->tail
;
304 WARN_ON(pending
&& !tail
);
307 * if pending was null this time around, no bios need processing
308 * at all and we can stop. Otherwise it'll loop back up again
309 * and do an additional check so no bios are missed.
311 * device->running_pending is used to synchronize with the
314 if (device
->pending_sync_bios
.head
== NULL
&&
315 device
->pending_bios
.head
== NULL
) {
317 device
->running_pending
= 0;
320 device
->running_pending
= 1;
323 pending_bios
->head
= NULL
;
324 pending_bios
->tail
= NULL
;
326 spin_unlock(&device
->io_lock
);
331 /* we want to work on both lists, but do more bios on the
332 * sync list than the regular list
335 pending_bios
!= &device
->pending_sync_bios
&&
336 device
->pending_sync_bios
.head
) ||
337 (num_run
> 64 && pending_bios
== &device
->pending_sync_bios
&&
338 device
->pending_bios
.head
)) {
339 spin_lock(&device
->io_lock
);
340 requeue_list(pending_bios
, pending
, tail
);
345 pending
= pending
->bi_next
;
348 if (atomic_dec_return(&fs_info
->nr_async_bios
) < limit
&&
349 waitqueue_active(&fs_info
->async_submit_wait
))
350 wake_up(&fs_info
->async_submit_wait
);
352 BUG_ON(atomic_read(&cur
->__bi_cnt
) == 0);
355 * if we're doing the sync list, record that our
356 * plug has some sync requests on it
358 * If we're doing the regular list and there are
359 * sync requests sitting around, unplug before
362 if (pending_bios
== &device
->pending_sync_bios
) {
364 } else if (sync_pending
) {
365 blk_finish_plug(&plug
);
366 blk_start_plug(&plug
);
370 btrfsic_submit_bio(cur
->bi_rw
, cur
);
377 * we made progress, there is more work to do and the bdi
378 * is now congested. Back off and let other work structs
381 if (pending
&& bdi_write_congested(bdi
) && batch_run
> 8 &&
382 fs_info
->fs_devices
->open_devices
> 1) {
383 struct io_context
*ioc
;
385 ioc
= current
->io_context
;
388 * the main goal here is that we don't want to
389 * block if we're going to be able to submit
390 * more requests without blocking.
392 * This code does two great things, it pokes into
393 * the elevator code from a filesystem _and_
394 * it makes assumptions about how batching works.
396 if (ioc
&& ioc
->nr_batch_requests
> 0 &&
397 time_before(jiffies
, ioc
->last_waited
+ HZ
/50UL) &&
399 ioc
->last_waited
== last_waited
)) {
401 * we want to go through our batch of
402 * requests and stop. So, we copy out
403 * the ioc->last_waited time and test
404 * against it before looping
406 last_waited
= ioc
->last_waited
;
410 spin_lock(&device
->io_lock
);
411 requeue_list(pending_bios
, pending
, tail
);
412 device
->running_pending
= 1;
414 spin_unlock(&device
->io_lock
);
415 btrfs_queue_work(fs_info
->submit_workers
,
419 /* unplug every 64 requests just for good measure */
420 if (batch_run
% 64 == 0) {
421 blk_finish_plug(&plug
);
422 blk_start_plug(&plug
);
431 spin_lock(&device
->io_lock
);
432 if (device
->pending_bios
.head
|| device
->pending_sync_bios
.head
)
434 spin_unlock(&device
->io_lock
);
437 blk_finish_plug(&plug
);
440 static void pending_bios_fn(struct btrfs_work
*work
)
442 struct btrfs_device
*device
;
444 device
= container_of(work
, struct btrfs_device
, work
);
445 run_scheduled_bios(device
);
449 void btrfs_free_stale_device(struct btrfs_device
*cur_dev
)
451 struct btrfs_fs_devices
*fs_devs
;
452 struct btrfs_device
*dev
;
457 list_for_each_entry(fs_devs
, &fs_uuids
, list
) {
462 if (fs_devs
->seeding
)
465 list_for_each_entry(dev
, &fs_devs
->devices
, dev_list
) {
473 * Todo: This won't be enough. What if the same device
474 * comes back (with new uuid and) with its mapper path?
475 * But for now, this does help as mostly an admin will
476 * either use mapper or non mapper path throughout.
479 del
= strcmp(rcu_str_deref(dev
->name
),
480 rcu_str_deref(cur_dev
->name
));
487 /* delete the stale device */
488 if (fs_devs
->num_devices
== 1) {
489 btrfs_sysfs_remove_fsid(fs_devs
);
490 list_del(&fs_devs
->list
);
491 free_fs_devices(fs_devs
);
493 fs_devs
->num_devices
--;
494 list_del(&dev
->dev_list
);
495 rcu_string_free(dev
->name
);
504 * Add new device to list of registered devices
507 * 1 - first time device is seen
508 * 0 - device already known
511 static noinline
int device_list_add(const char *path
,
512 struct btrfs_super_block
*disk_super
,
513 u64 devid
, struct btrfs_fs_devices
**fs_devices_ret
)
515 struct btrfs_device
*device
;
516 struct btrfs_fs_devices
*fs_devices
;
517 struct rcu_string
*name
;
519 u64 found_transid
= btrfs_super_generation(disk_super
);
521 fs_devices
= find_fsid(disk_super
->fsid
);
523 fs_devices
= alloc_fs_devices(disk_super
->fsid
);
524 if (IS_ERR(fs_devices
))
525 return PTR_ERR(fs_devices
);
527 list_add(&fs_devices
->list
, &fs_uuids
);
531 device
= __find_device(&fs_devices
->devices
, devid
,
532 disk_super
->dev_item
.uuid
);
536 if (fs_devices
->opened
)
539 device
= btrfs_alloc_device(NULL
, &devid
,
540 disk_super
->dev_item
.uuid
);
541 if (IS_ERR(device
)) {
542 /* we can safely leave the fs_devices entry around */
543 return PTR_ERR(device
);
546 name
= rcu_string_strdup(path
, GFP_NOFS
);
551 rcu_assign_pointer(device
->name
, name
);
553 mutex_lock(&fs_devices
->device_list_mutex
);
554 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
555 fs_devices
->num_devices
++;
556 mutex_unlock(&fs_devices
->device_list_mutex
);
559 device
->fs_devices
= fs_devices
;
560 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
562 * When FS is already mounted.
563 * 1. If you are here and if the device->name is NULL that
564 * means this device was missing at time of FS mount.
565 * 2. If you are here and if the device->name is different
566 * from 'path' that means either
567 * a. The same device disappeared and reappeared with
569 * b. The missing-disk-which-was-replaced, has
572 * We must allow 1 and 2a above. But 2b would be a spurious
575 * Further in case of 1 and 2a above, the disk at 'path'
576 * would have missed some transaction when it was away and
577 * in case of 2a the stale bdev has to be updated as well.
578 * 2b must not be allowed at all time.
582 * For now, we do allow update to btrfs_fs_device through the
583 * btrfs dev scan cli after FS has been mounted. We're still
584 * tracking a problem where systems fail mount by subvolume id
585 * when we reject replacement on a mounted FS.
587 if (!fs_devices
->opened
&& found_transid
< device
->generation
) {
589 * That is if the FS is _not_ mounted and if you
590 * are here, that means there is more than one
591 * disk with same uuid and devid.We keep the one
592 * with larger generation number or the last-in if
593 * generation are equal.
598 name
= rcu_string_strdup(path
, GFP_NOFS
);
601 rcu_string_free(device
->name
);
602 rcu_assign_pointer(device
->name
, name
);
603 if (device
->missing
) {
604 fs_devices
->missing_devices
--;
610 * Unmount does not free the btrfs_device struct but would zero
611 * generation along with most of the other members. So just update
612 * it back. We need it to pick the disk with largest generation
615 if (!fs_devices
->opened
)
616 device
->generation
= found_transid
;
619 * if there is new btrfs on an already registered device,
620 * then remove the stale device entry.
622 btrfs_free_stale_device(device
);
624 *fs_devices_ret
= fs_devices
;
629 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
631 struct btrfs_fs_devices
*fs_devices
;
632 struct btrfs_device
*device
;
633 struct btrfs_device
*orig_dev
;
635 fs_devices
= alloc_fs_devices(orig
->fsid
);
636 if (IS_ERR(fs_devices
))
639 mutex_lock(&orig
->device_list_mutex
);
640 fs_devices
->total_devices
= orig
->total_devices
;
642 /* We have held the volume lock, it is safe to get the devices. */
643 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
644 struct rcu_string
*name
;
646 device
= btrfs_alloc_device(NULL
, &orig_dev
->devid
,
652 * This is ok to do without rcu read locked because we hold the
653 * uuid mutex so nothing we touch in here is going to disappear.
655 if (orig_dev
->name
) {
656 name
= rcu_string_strdup(orig_dev
->name
->str
, GFP_NOFS
);
661 rcu_assign_pointer(device
->name
, name
);
664 list_add(&device
->dev_list
, &fs_devices
->devices
);
665 device
->fs_devices
= fs_devices
;
666 fs_devices
->num_devices
++;
668 mutex_unlock(&orig
->device_list_mutex
);
671 mutex_unlock(&orig
->device_list_mutex
);
672 free_fs_devices(fs_devices
);
673 return ERR_PTR(-ENOMEM
);
676 void btrfs_close_extra_devices(struct btrfs_fs_devices
*fs_devices
, int step
)
678 struct btrfs_device
*device
, *next
;
679 struct btrfs_device
*latest_dev
= NULL
;
681 mutex_lock(&uuid_mutex
);
683 /* This is the initialized path, it is safe to release the devices. */
684 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
685 if (device
->in_fs_metadata
) {
686 if (!device
->is_tgtdev_for_dev_replace
&&
688 device
->generation
> latest_dev
->generation
)) {
694 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
696 * In the first step, keep the device which has
697 * the correct fsid and the devid that is used
698 * for the dev_replace procedure.
699 * In the second step, the dev_replace state is
700 * read from the device tree and it is known
701 * whether the procedure is really active or
702 * not, which means whether this device is
703 * used or whether it should be removed.
705 if (step
== 0 || device
->is_tgtdev_for_dev_replace
) {
710 blkdev_put(device
->bdev
, device
->mode
);
712 fs_devices
->open_devices
--;
714 if (device
->writeable
) {
715 list_del_init(&device
->dev_alloc_list
);
716 device
->writeable
= 0;
717 if (!device
->is_tgtdev_for_dev_replace
)
718 fs_devices
->rw_devices
--;
720 list_del_init(&device
->dev_list
);
721 fs_devices
->num_devices
--;
722 rcu_string_free(device
->name
);
726 if (fs_devices
->seed
) {
727 fs_devices
= fs_devices
->seed
;
731 fs_devices
->latest_bdev
= latest_dev
->bdev
;
733 mutex_unlock(&uuid_mutex
);
736 static void __free_device(struct work_struct
*work
)
738 struct btrfs_device
*device
;
740 device
= container_of(work
, struct btrfs_device
, rcu_work
);
743 blkdev_put(device
->bdev
, device
->mode
);
745 rcu_string_free(device
->name
);
749 static void free_device(struct rcu_head
*head
)
751 struct btrfs_device
*device
;
753 device
= container_of(head
, struct btrfs_device
, rcu
);
755 INIT_WORK(&device
->rcu_work
, __free_device
);
756 schedule_work(&device
->rcu_work
);
759 static int __btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
761 struct btrfs_device
*device
, *tmp
;
763 if (--fs_devices
->opened
> 0)
766 mutex_lock(&fs_devices
->device_list_mutex
);
767 list_for_each_entry_safe(device
, tmp
, &fs_devices
->devices
, dev_list
) {
768 struct btrfs_device
*new_device
;
769 struct rcu_string
*name
;
772 fs_devices
->open_devices
--;
774 if (device
->writeable
&&
775 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
776 list_del_init(&device
->dev_alloc_list
);
777 fs_devices
->rw_devices
--;
781 fs_devices
->missing_devices
--;
783 new_device
= btrfs_alloc_device(NULL
, &device
->devid
,
785 BUG_ON(IS_ERR(new_device
)); /* -ENOMEM */
787 /* Safe because we are under uuid_mutex */
789 name
= rcu_string_strdup(device
->name
->str
, GFP_NOFS
);
790 BUG_ON(!name
); /* -ENOMEM */
791 rcu_assign_pointer(new_device
->name
, name
);
794 list_replace_rcu(&device
->dev_list
, &new_device
->dev_list
);
795 new_device
->fs_devices
= device
->fs_devices
;
797 call_rcu(&device
->rcu
, free_device
);
799 mutex_unlock(&fs_devices
->device_list_mutex
);
801 WARN_ON(fs_devices
->open_devices
);
802 WARN_ON(fs_devices
->rw_devices
);
803 fs_devices
->opened
= 0;
804 fs_devices
->seeding
= 0;
809 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
811 struct btrfs_fs_devices
*seed_devices
= NULL
;
814 mutex_lock(&uuid_mutex
);
815 ret
= __btrfs_close_devices(fs_devices
);
816 if (!fs_devices
->opened
) {
817 seed_devices
= fs_devices
->seed
;
818 fs_devices
->seed
= NULL
;
820 mutex_unlock(&uuid_mutex
);
822 while (seed_devices
) {
823 fs_devices
= seed_devices
;
824 seed_devices
= fs_devices
->seed
;
825 __btrfs_close_devices(fs_devices
);
826 free_fs_devices(fs_devices
);
829 * Wait for rcu kworkers under __btrfs_close_devices
830 * to finish all blkdev_puts so device is really
831 * free when umount is done.
837 static int __btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
838 fmode_t flags
, void *holder
)
840 struct request_queue
*q
;
841 struct block_device
*bdev
;
842 struct list_head
*head
= &fs_devices
->devices
;
843 struct btrfs_device
*device
;
844 struct btrfs_device
*latest_dev
= NULL
;
845 struct buffer_head
*bh
;
846 struct btrfs_super_block
*disk_super
;
853 list_for_each_entry(device
, head
, dev_list
) {
859 /* Just open everything we can; ignore failures here */
860 if (btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
864 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
865 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
866 if (devid
!= device
->devid
)
869 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
,
873 device
->generation
= btrfs_super_generation(disk_super
);
875 device
->generation
> latest_dev
->generation
)
878 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
879 device
->writeable
= 0;
881 device
->writeable
= !bdev_read_only(bdev
);
885 q
= bdev_get_queue(bdev
);
886 if (blk_queue_discard(q
))
887 device
->can_discard
= 1;
890 device
->in_fs_metadata
= 0;
891 device
->mode
= flags
;
893 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
894 fs_devices
->rotating
= 1;
896 fs_devices
->open_devices
++;
897 if (device
->writeable
&&
898 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
899 fs_devices
->rw_devices
++;
900 list_add(&device
->dev_alloc_list
,
901 &fs_devices
->alloc_list
);
908 blkdev_put(bdev
, flags
);
911 if (fs_devices
->open_devices
== 0) {
915 fs_devices
->seeding
= seeding
;
916 fs_devices
->opened
= 1;
917 fs_devices
->latest_bdev
= latest_dev
->bdev
;
918 fs_devices
->total_rw_bytes
= 0;
923 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
924 fmode_t flags
, void *holder
)
928 mutex_lock(&uuid_mutex
);
929 if (fs_devices
->opened
) {
930 fs_devices
->opened
++;
933 ret
= __btrfs_open_devices(fs_devices
, flags
, holder
);
935 mutex_unlock(&uuid_mutex
);
940 * Look for a btrfs signature on a device. This may be called out of the mount path
941 * and we are not allowed to call set_blocksize during the scan. The superblock
942 * is read via pagecache
944 int btrfs_scan_one_device(const char *path
, fmode_t flags
, void *holder
,
945 struct btrfs_fs_devices
**fs_devices_ret
)
947 struct btrfs_super_block
*disk_super
;
948 struct block_device
*bdev
;
959 * we would like to check all the supers, but that would make
960 * a btrfs mount succeed after a mkfs from a different FS.
961 * So, we need to add a special mount option to scan for
962 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
964 bytenr
= btrfs_sb_offset(0);
966 mutex_lock(&uuid_mutex
);
968 bdev
= blkdev_get_by_path(path
, flags
, holder
);
975 /* make sure our super fits in the device */
976 if (bytenr
+ PAGE_CACHE_SIZE
>= i_size_read(bdev
->bd_inode
))
979 /* make sure our super fits in the page */
980 if (sizeof(*disk_super
) > PAGE_CACHE_SIZE
)
983 /* make sure our super doesn't straddle pages on disk */
984 index
= bytenr
>> PAGE_CACHE_SHIFT
;
985 if ((bytenr
+ sizeof(*disk_super
) - 1) >> PAGE_CACHE_SHIFT
!= index
)
988 /* pull in the page with our super */
989 page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
992 if (IS_ERR_OR_NULL(page
))
997 /* align our pointer to the offset of the super block */
998 disk_super
= p
+ (bytenr
& ~PAGE_CACHE_MASK
);
1000 if (btrfs_super_bytenr(disk_super
) != bytenr
||
1001 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
)
1004 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1005 transid
= btrfs_super_generation(disk_super
);
1006 total_devices
= btrfs_super_num_devices(disk_super
);
1008 ret
= device_list_add(path
, disk_super
, devid
, fs_devices_ret
);
1010 if (disk_super
->label
[0]) {
1011 if (disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
1012 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
1013 printk(KERN_INFO
"BTRFS: device label %s ", disk_super
->label
);
1015 printk(KERN_INFO
"BTRFS: device fsid %pU ", disk_super
->fsid
);
1018 printk(KERN_CONT
"devid %llu transid %llu %s\n", devid
, transid
, path
);
1021 if (!ret
&& fs_devices_ret
)
1022 (*fs_devices_ret
)->total_devices
= total_devices
;
1026 page_cache_release(page
);
1029 blkdev_put(bdev
, flags
);
1031 mutex_unlock(&uuid_mutex
);
1035 /* helper to account the used device space in the range */
1036 int btrfs_account_dev_extents_size(struct btrfs_device
*device
, u64 start
,
1037 u64 end
, u64
*length
)
1039 struct btrfs_key key
;
1040 struct btrfs_root
*root
= device
->dev_root
;
1041 struct btrfs_dev_extent
*dev_extent
;
1042 struct btrfs_path
*path
;
1046 struct extent_buffer
*l
;
1050 if (start
>= device
->total_bytes
|| device
->is_tgtdev_for_dev_replace
)
1053 path
= btrfs_alloc_path();
1058 key
.objectid
= device
->devid
;
1060 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1062 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1066 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1073 slot
= path
->slots
[0];
1074 if (slot
>= btrfs_header_nritems(l
)) {
1075 ret
= btrfs_next_leaf(root
, path
);
1083 btrfs_item_key_to_cpu(l
, &key
, slot
);
1085 if (key
.objectid
< device
->devid
)
1088 if (key
.objectid
> device
->devid
)
1091 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1094 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1095 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1097 if (key
.offset
<= start
&& extent_end
> end
) {
1098 *length
= end
- start
+ 1;
1100 } else if (key
.offset
<= start
&& extent_end
> start
)
1101 *length
+= extent_end
- start
;
1102 else if (key
.offset
> start
&& extent_end
<= end
)
1103 *length
+= extent_end
- key
.offset
;
1104 else if (key
.offset
> start
&& key
.offset
<= end
) {
1105 *length
+= end
- key
.offset
+ 1;
1107 } else if (key
.offset
> end
)
1115 btrfs_free_path(path
);
1119 static int contains_pending_extent(struct btrfs_trans_handle
*trans
,
1120 struct btrfs_device
*device
,
1121 u64
*start
, u64 len
)
1123 struct extent_map
*em
;
1124 struct list_head
*search_list
= &trans
->transaction
->pending_chunks
;
1126 u64 physical_start
= *start
;
1129 list_for_each_entry(em
, search_list
, list
) {
1130 struct map_lookup
*map
;
1133 map
= (struct map_lookup
*)em
->bdev
;
1134 for (i
= 0; i
< map
->num_stripes
; i
++) {
1137 if (map
->stripes
[i
].dev
!= device
)
1139 if (map
->stripes
[i
].physical
>= physical_start
+ len
||
1140 map
->stripes
[i
].physical
+ em
->orig_block_len
<=
1144 * Make sure that while processing the pinned list we do
1145 * not override our *start with a lower value, because
1146 * we can have pinned chunks that fall within this
1147 * device hole and that have lower physical addresses
1148 * than the pending chunks we processed before. If we
1149 * do not take this special care we can end up getting
1150 * 2 pending chunks that start at the same physical
1151 * device offsets because the end offset of a pinned
1152 * chunk can be equal to the start offset of some
1155 end
= map
->stripes
[i
].physical
+ em
->orig_block_len
;
1162 if (search_list
== &trans
->transaction
->pending_chunks
) {
1163 search_list
= &trans
->root
->fs_info
->pinned_chunks
;
1172 * find_free_dev_extent - find free space in the specified device
1173 * @device: the device which we search the free space in
1174 * @num_bytes: the size of the free space that we need
1175 * @start: store the start of the free space.
1176 * @len: the size of the free space. that we find, or the size of the max
1177 * free space if we don't find suitable free space
1179 * this uses a pretty simple search, the expectation is that it is
1180 * called very infrequently and that a given device has a small number
1183 * @start is used to store the start of the free space if we find. But if we
1184 * don't find suitable free space, it will be used to store the start position
1185 * of the max free space.
1187 * @len is used to store the size of the free space that we find.
1188 * But if we don't find suitable free space, it is used to store the size of
1189 * the max free space.
1191 int find_free_dev_extent(struct btrfs_trans_handle
*trans
,
1192 struct btrfs_device
*device
, u64 num_bytes
,
1193 u64
*start
, u64
*len
)
1195 struct btrfs_key key
;
1196 struct btrfs_root
*root
= device
->dev_root
;
1197 struct btrfs_dev_extent
*dev_extent
;
1198 struct btrfs_path
*path
;
1204 u64 search_end
= device
->total_bytes
;
1207 struct extent_buffer
*l
;
1209 /* FIXME use last free of some kind */
1211 /* we don't want to overwrite the superblock on the drive,
1212 * so we make sure to start at an offset of at least 1MB
1214 search_start
= max(root
->fs_info
->alloc_start
, 1024ull * 1024);
1216 path
= btrfs_alloc_path();
1220 max_hole_start
= search_start
;
1224 if (search_start
>= search_end
|| device
->is_tgtdev_for_dev_replace
) {
1230 path
->search_commit_root
= 1;
1231 path
->skip_locking
= 1;
1233 key
.objectid
= device
->devid
;
1234 key
.offset
= search_start
;
1235 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1237 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1241 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1248 slot
= path
->slots
[0];
1249 if (slot
>= btrfs_header_nritems(l
)) {
1250 ret
= btrfs_next_leaf(root
, path
);
1258 btrfs_item_key_to_cpu(l
, &key
, slot
);
1260 if (key
.objectid
< device
->devid
)
1263 if (key
.objectid
> device
->devid
)
1266 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1269 if (key
.offset
> search_start
) {
1270 hole_size
= key
.offset
- search_start
;
1273 * Have to check before we set max_hole_start, otherwise
1274 * we could end up sending back this offset anyway.
1276 if (contains_pending_extent(trans
, device
,
1279 if (key
.offset
>= search_start
) {
1280 hole_size
= key
.offset
- search_start
;
1287 if (hole_size
> max_hole_size
) {
1288 max_hole_start
= search_start
;
1289 max_hole_size
= hole_size
;
1293 * If this free space is greater than which we need,
1294 * it must be the max free space that we have found
1295 * until now, so max_hole_start must point to the start
1296 * of this free space and the length of this free space
1297 * is stored in max_hole_size. Thus, we return
1298 * max_hole_start and max_hole_size and go back to the
1301 if (hole_size
>= num_bytes
) {
1307 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1308 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1310 if (extent_end
> search_start
)
1311 search_start
= extent_end
;
1318 * At this point, search_start should be the end of
1319 * allocated dev extents, and when shrinking the device,
1320 * search_end may be smaller than search_start.
1322 if (search_end
> search_start
) {
1323 hole_size
= search_end
- search_start
;
1325 if (contains_pending_extent(trans
, device
, &search_start
,
1327 btrfs_release_path(path
);
1331 if (hole_size
> max_hole_size
) {
1332 max_hole_start
= search_start
;
1333 max_hole_size
= hole_size
;
1338 if (max_hole_size
< num_bytes
)
1344 btrfs_free_path(path
);
1345 *start
= max_hole_start
;
1347 *len
= max_hole_size
;
1351 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1352 struct btrfs_device
*device
,
1353 u64 start
, u64
*dev_extent_len
)
1356 struct btrfs_path
*path
;
1357 struct btrfs_root
*root
= device
->dev_root
;
1358 struct btrfs_key key
;
1359 struct btrfs_key found_key
;
1360 struct extent_buffer
*leaf
= NULL
;
1361 struct btrfs_dev_extent
*extent
= NULL
;
1363 path
= btrfs_alloc_path();
1367 key
.objectid
= device
->devid
;
1369 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1371 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1373 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1374 BTRFS_DEV_EXTENT_KEY
);
1377 leaf
= path
->nodes
[0];
1378 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1379 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1380 struct btrfs_dev_extent
);
1381 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1382 btrfs_dev_extent_length(leaf
, extent
) < start
);
1384 btrfs_release_path(path
);
1386 } else if (ret
== 0) {
1387 leaf
= path
->nodes
[0];
1388 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1389 struct btrfs_dev_extent
);
1391 btrfs_error(root
->fs_info
, ret
, "Slot search failed");
1395 *dev_extent_len
= btrfs_dev_extent_length(leaf
, extent
);
1397 ret
= btrfs_del_item(trans
, root
, path
);
1399 btrfs_error(root
->fs_info
, ret
,
1400 "Failed to remove dev extent item");
1402 trans
->transaction
->have_free_bgs
= 1;
1405 btrfs_free_path(path
);
1409 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1410 struct btrfs_device
*device
,
1411 u64 chunk_tree
, u64 chunk_objectid
,
1412 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1415 struct btrfs_path
*path
;
1416 struct btrfs_root
*root
= device
->dev_root
;
1417 struct btrfs_dev_extent
*extent
;
1418 struct extent_buffer
*leaf
;
1419 struct btrfs_key key
;
1421 WARN_ON(!device
->in_fs_metadata
);
1422 WARN_ON(device
->is_tgtdev_for_dev_replace
);
1423 path
= btrfs_alloc_path();
1427 key
.objectid
= device
->devid
;
1429 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1430 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1435 leaf
= path
->nodes
[0];
1436 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1437 struct btrfs_dev_extent
);
1438 btrfs_set_dev_extent_chunk_tree(leaf
, extent
, chunk_tree
);
1439 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
, chunk_objectid
);
1440 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1442 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
1443 btrfs_dev_extent_chunk_tree_uuid(extent
), BTRFS_UUID_SIZE
);
1445 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1446 btrfs_mark_buffer_dirty(leaf
);
1448 btrfs_free_path(path
);
1452 static u64
find_next_chunk(struct btrfs_fs_info
*fs_info
)
1454 struct extent_map_tree
*em_tree
;
1455 struct extent_map
*em
;
1459 em_tree
= &fs_info
->mapping_tree
.map_tree
;
1460 read_lock(&em_tree
->lock
);
1461 n
= rb_last(&em_tree
->map
);
1463 em
= rb_entry(n
, struct extent_map
, rb_node
);
1464 ret
= em
->start
+ em
->len
;
1466 read_unlock(&em_tree
->lock
);
1471 static noinline
int find_next_devid(struct btrfs_fs_info
*fs_info
,
1475 struct btrfs_key key
;
1476 struct btrfs_key found_key
;
1477 struct btrfs_path
*path
;
1479 path
= btrfs_alloc_path();
1483 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1484 key
.type
= BTRFS_DEV_ITEM_KEY
;
1485 key
.offset
= (u64
)-1;
1487 ret
= btrfs_search_slot(NULL
, fs_info
->chunk_root
, &key
, path
, 0, 0);
1491 BUG_ON(ret
== 0); /* Corruption */
1493 ret
= btrfs_previous_item(fs_info
->chunk_root
, path
,
1494 BTRFS_DEV_ITEMS_OBJECTID
,
1495 BTRFS_DEV_ITEM_KEY
);
1499 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1501 *devid_ret
= found_key
.offset
+ 1;
1505 btrfs_free_path(path
);
1510 * the device information is stored in the chunk root
1511 * the btrfs_device struct should be fully filled in
1513 static int btrfs_add_device(struct btrfs_trans_handle
*trans
,
1514 struct btrfs_root
*root
,
1515 struct btrfs_device
*device
)
1518 struct btrfs_path
*path
;
1519 struct btrfs_dev_item
*dev_item
;
1520 struct extent_buffer
*leaf
;
1521 struct btrfs_key key
;
1524 root
= root
->fs_info
->chunk_root
;
1526 path
= btrfs_alloc_path();
1530 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1531 key
.type
= BTRFS_DEV_ITEM_KEY
;
1532 key
.offset
= device
->devid
;
1534 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1539 leaf
= path
->nodes
[0];
1540 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1542 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1543 btrfs_set_device_generation(leaf
, dev_item
, 0);
1544 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1545 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1546 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1547 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1548 btrfs_set_device_total_bytes(leaf
, dev_item
,
1549 btrfs_device_get_disk_total_bytes(device
));
1550 btrfs_set_device_bytes_used(leaf
, dev_item
,
1551 btrfs_device_get_bytes_used(device
));
1552 btrfs_set_device_group(leaf
, dev_item
, 0);
1553 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1554 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1555 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1557 ptr
= btrfs_device_uuid(dev_item
);
1558 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1559 ptr
= btrfs_device_fsid(dev_item
);
1560 write_extent_buffer(leaf
, root
->fs_info
->fsid
, ptr
, BTRFS_UUID_SIZE
);
1561 btrfs_mark_buffer_dirty(leaf
);
1565 btrfs_free_path(path
);
1570 * Function to update ctime/mtime for a given device path.
1571 * Mainly used for ctime/mtime based probe like libblkid.
1573 static void update_dev_time(char *path_name
)
1577 filp
= filp_open(path_name
, O_RDWR
, 0);
1580 file_update_time(filp
);
1581 filp_close(filp
, NULL
);
1585 static int btrfs_rm_dev_item(struct btrfs_root
*root
,
1586 struct btrfs_device
*device
)
1589 struct btrfs_path
*path
;
1590 struct btrfs_key key
;
1591 struct btrfs_trans_handle
*trans
;
1593 root
= root
->fs_info
->chunk_root
;
1595 path
= btrfs_alloc_path();
1599 trans
= btrfs_start_transaction(root
, 0);
1600 if (IS_ERR(trans
)) {
1601 btrfs_free_path(path
);
1602 return PTR_ERR(trans
);
1604 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1605 key
.type
= BTRFS_DEV_ITEM_KEY
;
1606 key
.offset
= device
->devid
;
1608 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1617 ret
= btrfs_del_item(trans
, root
, path
);
1621 btrfs_free_path(path
);
1622 btrfs_commit_transaction(trans
, root
);
1626 int btrfs_rm_device(struct btrfs_root
*root
, char *device_path
)
1628 struct btrfs_device
*device
;
1629 struct btrfs_device
*next_device
;
1630 struct block_device
*bdev
;
1631 struct buffer_head
*bh
= NULL
;
1632 struct btrfs_super_block
*disk_super
;
1633 struct btrfs_fs_devices
*cur_devices
;
1640 bool clear_super
= false;
1642 mutex_lock(&uuid_mutex
);
1645 seq
= read_seqbegin(&root
->fs_info
->profiles_lock
);
1647 all_avail
= root
->fs_info
->avail_data_alloc_bits
|
1648 root
->fs_info
->avail_system_alloc_bits
|
1649 root
->fs_info
->avail_metadata_alloc_bits
;
1650 } while (read_seqretry(&root
->fs_info
->profiles_lock
, seq
));
1652 num_devices
= root
->fs_info
->fs_devices
->num_devices
;
1653 btrfs_dev_replace_lock(&root
->fs_info
->dev_replace
);
1654 if (btrfs_dev_replace_is_ongoing(&root
->fs_info
->dev_replace
)) {
1655 WARN_ON(num_devices
< 1);
1658 btrfs_dev_replace_unlock(&root
->fs_info
->dev_replace
);
1660 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID10
) && num_devices
<= 4) {
1661 ret
= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET
;
1665 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID1
) && num_devices
<= 2) {
1666 ret
= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET
;
1670 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID5
) &&
1671 root
->fs_info
->fs_devices
->rw_devices
<= 2) {
1672 ret
= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET
;
1675 if ((all_avail
& BTRFS_BLOCK_GROUP_RAID6
) &&
1676 root
->fs_info
->fs_devices
->rw_devices
<= 3) {
1677 ret
= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET
;
1681 if (strcmp(device_path
, "missing") == 0) {
1682 struct list_head
*devices
;
1683 struct btrfs_device
*tmp
;
1686 devices
= &root
->fs_info
->fs_devices
->devices
;
1688 * It is safe to read the devices since the volume_mutex
1691 list_for_each_entry(tmp
, devices
, dev_list
) {
1692 if (tmp
->in_fs_metadata
&&
1693 !tmp
->is_tgtdev_for_dev_replace
&&
1703 ret
= BTRFS_ERROR_DEV_MISSING_NOT_FOUND
;
1707 ret
= btrfs_get_bdev_and_sb(device_path
,
1708 FMODE_WRITE
| FMODE_EXCL
,
1709 root
->fs_info
->bdev_holder
, 0,
1713 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1714 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1715 dev_uuid
= disk_super
->dev_item
.uuid
;
1716 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
1724 if (device
->is_tgtdev_for_dev_replace
) {
1725 ret
= BTRFS_ERROR_DEV_TGT_REPLACE
;
1729 if (device
->writeable
&& root
->fs_info
->fs_devices
->rw_devices
== 1) {
1730 ret
= BTRFS_ERROR_DEV_ONLY_WRITABLE
;
1734 if (device
->writeable
) {
1736 list_del_init(&device
->dev_alloc_list
);
1737 device
->fs_devices
->rw_devices
--;
1738 unlock_chunks(root
);
1742 mutex_unlock(&uuid_mutex
);
1743 ret
= btrfs_shrink_device(device
, 0);
1744 mutex_lock(&uuid_mutex
);
1749 * TODO: the superblock still includes this device in its num_devices
1750 * counter although write_all_supers() is not locked out. This
1751 * could give a filesystem state which requires a degraded mount.
1753 ret
= btrfs_rm_dev_item(root
->fs_info
->chunk_root
, device
);
1757 device
->in_fs_metadata
= 0;
1758 btrfs_scrub_cancel_dev(root
->fs_info
, device
);
1761 * the device list mutex makes sure that we don't change
1762 * the device list while someone else is writing out all
1763 * the device supers. Whoever is writing all supers, should
1764 * lock the device list mutex before getting the number of
1765 * devices in the super block (super_copy). Conversely,
1766 * whoever updates the number of devices in the super block
1767 * (super_copy) should hold the device list mutex.
1770 cur_devices
= device
->fs_devices
;
1771 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1772 list_del_rcu(&device
->dev_list
);
1774 device
->fs_devices
->num_devices
--;
1775 device
->fs_devices
->total_devices
--;
1777 if (device
->missing
)
1778 device
->fs_devices
->missing_devices
--;
1780 next_device
= list_entry(root
->fs_info
->fs_devices
->devices
.next
,
1781 struct btrfs_device
, dev_list
);
1782 if (device
->bdev
== root
->fs_info
->sb
->s_bdev
)
1783 root
->fs_info
->sb
->s_bdev
= next_device
->bdev
;
1784 if (device
->bdev
== root
->fs_info
->fs_devices
->latest_bdev
)
1785 root
->fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1788 device
->fs_devices
->open_devices
--;
1789 /* remove sysfs entry */
1790 btrfs_kobj_rm_device(root
->fs_info
->fs_devices
, device
);
1793 call_rcu(&device
->rcu
, free_device
);
1795 num_devices
= btrfs_super_num_devices(root
->fs_info
->super_copy
) - 1;
1796 btrfs_set_super_num_devices(root
->fs_info
->super_copy
, num_devices
);
1797 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
1799 if (cur_devices
->open_devices
== 0) {
1800 struct btrfs_fs_devices
*fs_devices
;
1801 fs_devices
= root
->fs_info
->fs_devices
;
1802 while (fs_devices
) {
1803 if (fs_devices
->seed
== cur_devices
) {
1804 fs_devices
->seed
= cur_devices
->seed
;
1807 fs_devices
= fs_devices
->seed
;
1809 cur_devices
->seed
= NULL
;
1810 __btrfs_close_devices(cur_devices
);
1811 free_fs_devices(cur_devices
);
1814 root
->fs_info
->num_tolerated_disk_barrier_failures
=
1815 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
1818 * at this point, the device is zero sized. We want to
1819 * remove it from the devices list and zero out the old super
1821 if (clear_super
&& disk_super
) {
1825 /* make sure this device isn't detected as part of
1828 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
1829 set_buffer_dirty(bh
);
1830 sync_dirty_buffer(bh
);
1832 /* clear the mirror copies of super block on the disk
1833 * being removed, 0th copy is been taken care above and
1834 * the below would take of the rest
1836 for (i
= 1; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1837 bytenr
= btrfs_sb_offset(i
);
1838 if (bytenr
+ BTRFS_SUPER_INFO_SIZE
>=
1839 i_size_read(bdev
->bd_inode
))
1843 bh
= __bread(bdev
, bytenr
/ 4096,
1844 BTRFS_SUPER_INFO_SIZE
);
1848 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1850 if (btrfs_super_bytenr(disk_super
) != bytenr
||
1851 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
) {
1854 memset(&disk_super
->magic
, 0,
1855 sizeof(disk_super
->magic
));
1856 set_buffer_dirty(bh
);
1857 sync_dirty_buffer(bh
);
1864 /* Notify udev that device has changed */
1865 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
1867 /* Update ctime/mtime for device path for libblkid */
1868 update_dev_time(device_path
);
1874 blkdev_put(bdev
, FMODE_READ
| FMODE_EXCL
);
1876 mutex_unlock(&uuid_mutex
);
1879 if (device
->writeable
) {
1881 list_add(&device
->dev_alloc_list
,
1882 &root
->fs_info
->fs_devices
->alloc_list
);
1883 device
->fs_devices
->rw_devices
++;
1884 unlock_chunks(root
);
1889 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info
*fs_info
,
1890 struct btrfs_device
*srcdev
)
1892 struct btrfs_fs_devices
*fs_devices
;
1894 WARN_ON(!mutex_is_locked(&fs_info
->fs_devices
->device_list_mutex
));
1897 * in case of fs with no seed, srcdev->fs_devices will point
1898 * to fs_devices of fs_info. However when the dev being replaced is
1899 * a seed dev it will point to the seed's local fs_devices. In short
1900 * srcdev will have its correct fs_devices in both the cases.
1902 fs_devices
= srcdev
->fs_devices
;
1904 list_del_rcu(&srcdev
->dev_list
);
1905 list_del_rcu(&srcdev
->dev_alloc_list
);
1906 fs_devices
->num_devices
--;
1907 if (srcdev
->missing
)
1908 fs_devices
->missing_devices
--;
1910 if (srcdev
->writeable
) {
1911 fs_devices
->rw_devices
--;
1912 /* zero out the old super if it is writable */
1913 btrfs_scratch_superblock(srcdev
);
1917 fs_devices
->open_devices
--;
1920 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info
*fs_info
,
1921 struct btrfs_device
*srcdev
)
1923 struct btrfs_fs_devices
*fs_devices
= srcdev
->fs_devices
;
1925 call_rcu(&srcdev
->rcu
, free_device
);
1928 * unless fs_devices is seed fs, num_devices shouldn't go
1931 BUG_ON(!fs_devices
->num_devices
&& !fs_devices
->seeding
);
1933 /* if this is no devs we rather delete the fs_devices */
1934 if (!fs_devices
->num_devices
) {
1935 struct btrfs_fs_devices
*tmp_fs_devices
;
1937 tmp_fs_devices
= fs_info
->fs_devices
;
1938 while (tmp_fs_devices
) {
1939 if (tmp_fs_devices
->seed
== fs_devices
) {
1940 tmp_fs_devices
->seed
= fs_devices
->seed
;
1943 tmp_fs_devices
= tmp_fs_devices
->seed
;
1945 fs_devices
->seed
= NULL
;
1946 __btrfs_close_devices(fs_devices
);
1947 free_fs_devices(fs_devices
);
1951 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info
*fs_info
,
1952 struct btrfs_device
*tgtdev
)
1954 struct btrfs_device
*next_device
;
1956 mutex_lock(&uuid_mutex
);
1958 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
1960 btrfs_kobj_rm_device(fs_info
->fs_devices
, tgtdev
);
1963 btrfs_scratch_superblock(tgtdev
);
1964 fs_info
->fs_devices
->open_devices
--;
1966 fs_info
->fs_devices
->num_devices
--;
1968 next_device
= list_entry(fs_info
->fs_devices
->devices
.next
,
1969 struct btrfs_device
, dev_list
);
1970 if (tgtdev
->bdev
== fs_info
->sb
->s_bdev
)
1971 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1972 if (tgtdev
->bdev
== fs_info
->fs_devices
->latest_bdev
)
1973 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1974 list_del_rcu(&tgtdev
->dev_list
);
1976 call_rcu(&tgtdev
->rcu
, free_device
);
1978 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
1979 mutex_unlock(&uuid_mutex
);
1982 static int btrfs_find_device_by_path(struct btrfs_root
*root
, char *device_path
,
1983 struct btrfs_device
**device
)
1986 struct btrfs_super_block
*disk_super
;
1989 struct block_device
*bdev
;
1990 struct buffer_head
*bh
;
1993 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
1994 root
->fs_info
->bdev_holder
, 0, &bdev
, &bh
);
1997 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
1998 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
1999 dev_uuid
= disk_super
->dev_item
.uuid
;
2000 *device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
2005 blkdev_put(bdev
, FMODE_READ
);
2009 int btrfs_find_device_missing_or_by_path(struct btrfs_root
*root
,
2011 struct btrfs_device
**device
)
2014 if (strcmp(device_path
, "missing") == 0) {
2015 struct list_head
*devices
;
2016 struct btrfs_device
*tmp
;
2018 devices
= &root
->fs_info
->fs_devices
->devices
;
2020 * It is safe to read the devices since the volume_mutex
2021 * is held by the caller.
2023 list_for_each_entry(tmp
, devices
, dev_list
) {
2024 if (tmp
->in_fs_metadata
&& !tmp
->bdev
) {
2031 btrfs_err(root
->fs_info
, "no missing device found");
2037 return btrfs_find_device_by_path(root
, device_path
, device
);
2042 * does all the dirty work required for changing file system's UUID.
2044 static int btrfs_prepare_sprout(struct btrfs_root
*root
)
2046 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2047 struct btrfs_fs_devices
*old_devices
;
2048 struct btrfs_fs_devices
*seed_devices
;
2049 struct btrfs_super_block
*disk_super
= root
->fs_info
->super_copy
;
2050 struct btrfs_device
*device
;
2053 BUG_ON(!mutex_is_locked(&uuid_mutex
));
2054 if (!fs_devices
->seeding
)
2057 seed_devices
= __alloc_fs_devices();
2058 if (IS_ERR(seed_devices
))
2059 return PTR_ERR(seed_devices
);
2061 old_devices
= clone_fs_devices(fs_devices
);
2062 if (IS_ERR(old_devices
)) {
2063 kfree(seed_devices
);
2064 return PTR_ERR(old_devices
);
2067 list_add(&old_devices
->list
, &fs_uuids
);
2069 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
2070 seed_devices
->opened
= 1;
2071 INIT_LIST_HEAD(&seed_devices
->devices
);
2072 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
2073 mutex_init(&seed_devices
->device_list_mutex
);
2075 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2076 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
2078 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
)
2079 device
->fs_devices
= seed_devices
;
2082 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
2083 unlock_chunks(root
);
2085 fs_devices
->seeding
= 0;
2086 fs_devices
->num_devices
= 0;
2087 fs_devices
->open_devices
= 0;
2088 fs_devices
->missing_devices
= 0;
2089 fs_devices
->rotating
= 0;
2090 fs_devices
->seed
= seed_devices
;
2092 generate_random_uuid(fs_devices
->fsid
);
2093 memcpy(root
->fs_info
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2094 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2095 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2097 super_flags
= btrfs_super_flags(disk_super
) &
2098 ~BTRFS_SUPER_FLAG_SEEDING
;
2099 btrfs_set_super_flags(disk_super
, super_flags
);
2105 * strore the expected generation for seed devices in device items.
2107 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
,
2108 struct btrfs_root
*root
)
2110 struct btrfs_path
*path
;
2111 struct extent_buffer
*leaf
;
2112 struct btrfs_dev_item
*dev_item
;
2113 struct btrfs_device
*device
;
2114 struct btrfs_key key
;
2115 u8 fs_uuid
[BTRFS_UUID_SIZE
];
2116 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2120 path
= btrfs_alloc_path();
2124 root
= root
->fs_info
->chunk_root
;
2125 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2127 key
.type
= BTRFS_DEV_ITEM_KEY
;
2130 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2134 leaf
= path
->nodes
[0];
2136 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
2137 ret
= btrfs_next_leaf(root
, path
);
2142 leaf
= path
->nodes
[0];
2143 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2144 btrfs_release_path(path
);
2148 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2149 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
2150 key
.type
!= BTRFS_DEV_ITEM_KEY
)
2153 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2154 struct btrfs_dev_item
);
2155 devid
= btrfs_device_id(leaf
, dev_item
);
2156 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
2158 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
2160 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
,
2162 BUG_ON(!device
); /* Logic error */
2164 if (device
->fs_devices
->seeding
) {
2165 btrfs_set_device_generation(leaf
, dev_item
,
2166 device
->generation
);
2167 btrfs_mark_buffer_dirty(leaf
);
2175 btrfs_free_path(path
);
2179 int btrfs_init_new_device(struct btrfs_root
*root
, char *device_path
)
2181 struct request_queue
*q
;
2182 struct btrfs_trans_handle
*trans
;
2183 struct btrfs_device
*device
;
2184 struct block_device
*bdev
;
2185 struct list_head
*devices
;
2186 struct super_block
*sb
= root
->fs_info
->sb
;
2187 struct rcu_string
*name
;
2189 int seeding_dev
= 0;
2192 if ((sb
->s_flags
& MS_RDONLY
) && !root
->fs_info
->fs_devices
->seeding
)
2195 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2196 root
->fs_info
->bdev_holder
);
2198 return PTR_ERR(bdev
);
2200 if (root
->fs_info
->fs_devices
->seeding
) {
2202 down_write(&sb
->s_umount
);
2203 mutex_lock(&uuid_mutex
);
2206 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2208 devices
= &root
->fs_info
->fs_devices
->devices
;
2210 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2211 list_for_each_entry(device
, devices
, dev_list
) {
2212 if (device
->bdev
== bdev
) {
2215 &root
->fs_info
->fs_devices
->device_list_mutex
);
2219 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2221 device
= btrfs_alloc_device(root
->fs_info
, NULL
, NULL
);
2222 if (IS_ERR(device
)) {
2223 /* we can safely leave the fs_devices entry around */
2224 ret
= PTR_ERR(device
);
2228 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2234 rcu_assign_pointer(device
->name
, name
);
2236 trans
= btrfs_start_transaction(root
, 0);
2237 if (IS_ERR(trans
)) {
2238 rcu_string_free(device
->name
);
2240 ret
= PTR_ERR(trans
);
2244 q
= bdev_get_queue(bdev
);
2245 if (blk_queue_discard(q
))
2246 device
->can_discard
= 1;
2247 device
->writeable
= 1;
2248 device
->generation
= trans
->transid
;
2249 device
->io_width
= root
->sectorsize
;
2250 device
->io_align
= root
->sectorsize
;
2251 device
->sector_size
= root
->sectorsize
;
2252 device
->total_bytes
= i_size_read(bdev
->bd_inode
);
2253 device
->disk_total_bytes
= device
->total_bytes
;
2254 device
->commit_total_bytes
= device
->total_bytes
;
2255 device
->dev_root
= root
->fs_info
->dev_root
;
2256 device
->bdev
= bdev
;
2257 device
->in_fs_metadata
= 1;
2258 device
->is_tgtdev_for_dev_replace
= 0;
2259 device
->mode
= FMODE_EXCL
;
2260 device
->dev_stats_valid
= 1;
2261 set_blocksize(device
->bdev
, 4096);
2264 sb
->s_flags
&= ~MS_RDONLY
;
2265 ret
= btrfs_prepare_sprout(root
);
2266 BUG_ON(ret
); /* -ENOMEM */
2269 device
->fs_devices
= root
->fs_info
->fs_devices
;
2271 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2273 list_add_rcu(&device
->dev_list
, &root
->fs_info
->fs_devices
->devices
);
2274 list_add(&device
->dev_alloc_list
,
2275 &root
->fs_info
->fs_devices
->alloc_list
);
2276 root
->fs_info
->fs_devices
->num_devices
++;
2277 root
->fs_info
->fs_devices
->open_devices
++;
2278 root
->fs_info
->fs_devices
->rw_devices
++;
2279 root
->fs_info
->fs_devices
->total_devices
++;
2280 root
->fs_info
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2282 spin_lock(&root
->fs_info
->free_chunk_lock
);
2283 root
->fs_info
->free_chunk_space
+= device
->total_bytes
;
2284 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2286 if (!blk_queue_nonrot(bdev_get_queue(bdev
)))
2287 root
->fs_info
->fs_devices
->rotating
= 1;
2289 tmp
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
2290 btrfs_set_super_total_bytes(root
->fs_info
->super_copy
,
2291 tmp
+ device
->total_bytes
);
2293 tmp
= btrfs_super_num_devices(root
->fs_info
->super_copy
);
2294 btrfs_set_super_num_devices(root
->fs_info
->super_copy
,
2297 /* add sysfs device entry */
2298 btrfs_kobj_add_device(root
->fs_info
->fs_devices
, device
);
2301 * we've got more storage, clear any full flags on the space
2304 btrfs_clear_space_info_full(root
->fs_info
);
2306 unlock_chunks(root
);
2307 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2311 ret
= init_first_rw_device(trans
, root
, device
);
2312 unlock_chunks(root
);
2314 btrfs_abort_transaction(trans
, root
, ret
);
2319 ret
= btrfs_add_device(trans
, root
, device
);
2321 btrfs_abort_transaction(trans
, root
, ret
);
2326 char fsid_buf
[BTRFS_UUID_UNPARSED_SIZE
];
2328 ret
= btrfs_finish_sprout(trans
, root
);
2330 btrfs_abort_transaction(trans
, root
, ret
);
2334 /* Sprouting would change fsid of the mounted root,
2335 * so rename the fsid on the sysfs
2337 snprintf(fsid_buf
, BTRFS_UUID_UNPARSED_SIZE
, "%pU",
2338 root
->fs_info
->fsid
);
2339 if (kobject_rename(&root
->fs_info
->fs_devices
->super_kobj
,
2341 pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n");
2344 root
->fs_info
->num_tolerated_disk_barrier_failures
=
2345 btrfs_calc_num_tolerated_disk_barrier_failures(root
->fs_info
);
2346 ret
= btrfs_commit_transaction(trans
, root
);
2349 mutex_unlock(&uuid_mutex
);
2350 up_write(&sb
->s_umount
);
2352 if (ret
) /* transaction commit */
2355 ret
= btrfs_relocate_sys_chunks(root
);
2357 btrfs_error(root
->fs_info
, ret
,
2358 "Failed to relocate sys chunks after "
2359 "device initialization. This can be fixed "
2360 "using the \"btrfs balance\" command.");
2361 trans
= btrfs_attach_transaction(root
);
2362 if (IS_ERR(trans
)) {
2363 if (PTR_ERR(trans
) == -ENOENT
)
2365 return PTR_ERR(trans
);
2367 ret
= btrfs_commit_transaction(trans
, root
);
2370 /* Update ctime/mtime for libblkid */
2371 update_dev_time(device_path
);
2375 btrfs_end_transaction(trans
, root
);
2376 rcu_string_free(device
->name
);
2377 btrfs_kobj_rm_device(root
->fs_info
->fs_devices
, device
);
2380 blkdev_put(bdev
, FMODE_EXCL
);
2382 mutex_unlock(&uuid_mutex
);
2383 up_write(&sb
->s_umount
);
2388 int btrfs_init_dev_replace_tgtdev(struct btrfs_root
*root
, char *device_path
,
2389 struct btrfs_device
*srcdev
,
2390 struct btrfs_device
**device_out
)
2392 struct request_queue
*q
;
2393 struct btrfs_device
*device
;
2394 struct block_device
*bdev
;
2395 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2396 struct list_head
*devices
;
2397 struct rcu_string
*name
;
2398 u64 devid
= BTRFS_DEV_REPLACE_DEVID
;
2402 if (fs_info
->fs_devices
->seeding
) {
2403 btrfs_err(fs_info
, "the filesystem is a seed filesystem!");
2407 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2408 fs_info
->bdev_holder
);
2410 btrfs_err(fs_info
, "target device %s is invalid!", device_path
);
2411 return PTR_ERR(bdev
);
2414 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2416 devices
= &fs_info
->fs_devices
->devices
;
2417 list_for_each_entry(device
, devices
, dev_list
) {
2418 if (device
->bdev
== bdev
) {
2419 btrfs_err(fs_info
, "target device is in the filesystem!");
2426 if (i_size_read(bdev
->bd_inode
) <
2427 btrfs_device_get_total_bytes(srcdev
)) {
2428 btrfs_err(fs_info
, "target device is smaller than source device!");
2434 device
= btrfs_alloc_device(NULL
, &devid
, NULL
);
2435 if (IS_ERR(device
)) {
2436 ret
= PTR_ERR(device
);
2440 name
= rcu_string_strdup(device_path
, GFP_NOFS
);
2446 rcu_assign_pointer(device
->name
, name
);
2448 q
= bdev_get_queue(bdev
);
2449 if (blk_queue_discard(q
))
2450 device
->can_discard
= 1;
2451 mutex_lock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2452 device
->writeable
= 1;
2453 device
->generation
= 0;
2454 device
->io_width
= root
->sectorsize
;
2455 device
->io_align
= root
->sectorsize
;
2456 device
->sector_size
= root
->sectorsize
;
2457 device
->total_bytes
= btrfs_device_get_total_bytes(srcdev
);
2458 device
->disk_total_bytes
= btrfs_device_get_disk_total_bytes(srcdev
);
2459 device
->bytes_used
= btrfs_device_get_bytes_used(srcdev
);
2460 ASSERT(list_empty(&srcdev
->resized_list
));
2461 device
->commit_total_bytes
= srcdev
->commit_total_bytes
;
2462 device
->commit_bytes_used
= device
->bytes_used
;
2463 device
->dev_root
= fs_info
->dev_root
;
2464 device
->bdev
= bdev
;
2465 device
->in_fs_metadata
= 1;
2466 device
->is_tgtdev_for_dev_replace
= 1;
2467 device
->mode
= FMODE_EXCL
;
2468 device
->dev_stats_valid
= 1;
2469 set_blocksize(device
->bdev
, 4096);
2470 device
->fs_devices
= fs_info
->fs_devices
;
2471 list_add(&device
->dev_list
, &fs_info
->fs_devices
->devices
);
2472 fs_info
->fs_devices
->num_devices
++;
2473 fs_info
->fs_devices
->open_devices
++;
2474 mutex_unlock(&root
->fs_info
->fs_devices
->device_list_mutex
);
2476 *device_out
= device
;
2480 blkdev_put(bdev
, FMODE_EXCL
);
2484 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info
*fs_info
,
2485 struct btrfs_device
*tgtdev
)
2487 WARN_ON(fs_info
->fs_devices
->rw_devices
== 0);
2488 tgtdev
->io_width
= fs_info
->dev_root
->sectorsize
;
2489 tgtdev
->io_align
= fs_info
->dev_root
->sectorsize
;
2490 tgtdev
->sector_size
= fs_info
->dev_root
->sectorsize
;
2491 tgtdev
->dev_root
= fs_info
->dev_root
;
2492 tgtdev
->in_fs_metadata
= 1;
2495 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2496 struct btrfs_device
*device
)
2499 struct btrfs_path
*path
;
2500 struct btrfs_root
*root
;
2501 struct btrfs_dev_item
*dev_item
;
2502 struct extent_buffer
*leaf
;
2503 struct btrfs_key key
;
2505 root
= device
->dev_root
->fs_info
->chunk_root
;
2507 path
= btrfs_alloc_path();
2511 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2512 key
.type
= BTRFS_DEV_ITEM_KEY
;
2513 key
.offset
= device
->devid
;
2515 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2524 leaf
= path
->nodes
[0];
2525 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2527 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2528 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2529 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2530 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2531 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2532 btrfs_set_device_total_bytes(leaf
, dev_item
,
2533 btrfs_device_get_disk_total_bytes(device
));
2534 btrfs_set_device_bytes_used(leaf
, dev_item
,
2535 btrfs_device_get_bytes_used(device
));
2536 btrfs_mark_buffer_dirty(leaf
);
2539 btrfs_free_path(path
);
2543 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2544 struct btrfs_device
*device
, u64 new_size
)
2546 struct btrfs_super_block
*super_copy
=
2547 device
->dev_root
->fs_info
->super_copy
;
2548 struct btrfs_fs_devices
*fs_devices
;
2552 if (!device
->writeable
)
2555 lock_chunks(device
->dev_root
);
2556 old_total
= btrfs_super_total_bytes(super_copy
);
2557 diff
= new_size
- device
->total_bytes
;
2559 if (new_size
<= device
->total_bytes
||
2560 device
->is_tgtdev_for_dev_replace
) {
2561 unlock_chunks(device
->dev_root
);
2565 fs_devices
= device
->dev_root
->fs_info
->fs_devices
;
2567 btrfs_set_super_total_bytes(super_copy
, old_total
+ diff
);
2568 device
->fs_devices
->total_rw_bytes
+= diff
;
2570 btrfs_device_set_total_bytes(device
, new_size
);
2571 btrfs_device_set_disk_total_bytes(device
, new_size
);
2572 btrfs_clear_space_info_full(device
->dev_root
->fs_info
);
2573 if (list_empty(&device
->resized_list
))
2574 list_add_tail(&device
->resized_list
,
2575 &fs_devices
->resized_devices
);
2576 unlock_chunks(device
->dev_root
);
2578 return btrfs_update_device(trans
, device
);
2581 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
,
2582 struct btrfs_root
*root
, u64 chunk_objectid
,
2586 struct btrfs_path
*path
;
2587 struct btrfs_key key
;
2589 root
= root
->fs_info
->chunk_root
;
2590 path
= btrfs_alloc_path();
2594 key
.objectid
= chunk_objectid
;
2595 key
.offset
= chunk_offset
;
2596 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2598 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2601 else if (ret
> 0) { /* Logic error or corruption */
2602 btrfs_error(root
->fs_info
, -ENOENT
,
2603 "Failed lookup while freeing chunk.");
2608 ret
= btrfs_del_item(trans
, root
, path
);
2610 btrfs_error(root
->fs_info
, ret
,
2611 "Failed to delete chunk item.");
2613 btrfs_free_path(path
);
2617 static int btrfs_del_sys_chunk(struct btrfs_root
*root
, u64 chunk_objectid
, u64
2620 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
2621 struct btrfs_disk_key
*disk_key
;
2622 struct btrfs_chunk
*chunk
;
2629 struct btrfs_key key
;
2632 array_size
= btrfs_super_sys_array_size(super_copy
);
2634 ptr
= super_copy
->sys_chunk_array
;
2637 while (cur
< array_size
) {
2638 disk_key
= (struct btrfs_disk_key
*)ptr
;
2639 btrfs_disk_key_to_cpu(&key
, disk_key
);
2641 len
= sizeof(*disk_key
);
2643 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2644 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2645 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2646 len
+= btrfs_chunk_item_size(num_stripes
);
2651 if (key
.objectid
== chunk_objectid
&&
2652 key
.offset
== chunk_offset
) {
2653 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2655 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2661 unlock_chunks(root
);
2665 int btrfs_remove_chunk(struct btrfs_trans_handle
*trans
,
2666 struct btrfs_root
*root
, u64 chunk_offset
)
2668 struct extent_map_tree
*em_tree
;
2669 struct extent_map
*em
;
2670 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
2671 struct map_lookup
*map
;
2672 u64 dev_extent_len
= 0;
2673 u64 chunk_objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2677 root
= root
->fs_info
->chunk_root
;
2678 em_tree
= &root
->fs_info
->mapping_tree
.map_tree
;
2680 read_lock(&em_tree
->lock
);
2681 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
2682 read_unlock(&em_tree
->lock
);
2684 if (!em
|| em
->start
> chunk_offset
||
2685 em
->start
+ em
->len
< chunk_offset
) {
2687 * This is a logic error, but we don't want to just rely on the
2688 * user having built with ASSERT enabled, so if ASSERT doens't
2689 * do anything we still error out.
2693 free_extent_map(em
);
2696 map
= (struct map_lookup
*)em
->bdev
;
2697 lock_chunks(root
->fs_info
->chunk_root
);
2698 check_system_chunk(trans
, extent_root
, map
->type
);
2699 unlock_chunks(root
->fs_info
->chunk_root
);
2701 for (i
= 0; i
< map
->num_stripes
; i
++) {
2702 struct btrfs_device
*device
= map
->stripes
[i
].dev
;
2703 ret
= btrfs_free_dev_extent(trans
, device
,
2704 map
->stripes
[i
].physical
,
2707 btrfs_abort_transaction(trans
, root
, ret
);
2711 if (device
->bytes_used
> 0) {
2713 btrfs_device_set_bytes_used(device
,
2714 device
->bytes_used
- dev_extent_len
);
2715 spin_lock(&root
->fs_info
->free_chunk_lock
);
2716 root
->fs_info
->free_chunk_space
+= dev_extent_len
;
2717 spin_unlock(&root
->fs_info
->free_chunk_lock
);
2718 btrfs_clear_space_info_full(root
->fs_info
);
2719 unlock_chunks(root
);
2722 if (map
->stripes
[i
].dev
) {
2723 ret
= btrfs_update_device(trans
, map
->stripes
[i
].dev
);
2725 btrfs_abort_transaction(trans
, root
, ret
);
2730 ret
= btrfs_free_chunk(trans
, root
, chunk_objectid
, chunk_offset
);
2732 btrfs_abort_transaction(trans
, root
, ret
);
2736 trace_btrfs_chunk_free(root
, map
, chunk_offset
, em
->len
);
2738 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2739 ret
= btrfs_del_sys_chunk(root
, chunk_objectid
, chunk_offset
);
2741 btrfs_abort_transaction(trans
, root
, ret
);
2746 ret
= btrfs_remove_block_group(trans
, extent_root
, chunk_offset
, em
);
2748 btrfs_abort_transaction(trans
, extent_root
, ret
);
2754 free_extent_map(em
);
2758 static int btrfs_relocate_chunk(struct btrfs_root
*root
,
2762 struct btrfs_root
*extent_root
;
2763 struct btrfs_trans_handle
*trans
;
2766 root
= root
->fs_info
->chunk_root
;
2767 extent_root
= root
->fs_info
->extent_root
;
2769 ret
= btrfs_can_relocate(extent_root
, chunk_offset
);
2773 /* step one, relocate all the extents inside this chunk */
2774 ret
= btrfs_relocate_block_group(extent_root
, chunk_offset
);
2778 trans
= btrfs_start_transaction(root
, 0);
2779 if (IS_ERR(trans
)) {
2780 ret
= PTR_ERR(trans
);
2781 btrfs_std_error(root
->fs_info
, ret
);
2786 * step two, delete the device extents and the
2787 * chunk tree entries
2789 ret
= btrfs_remove_chunk(trans
, root
, chunk_offset
);
2790 btrfs_end_transaction(trans
, root
);
2794 static int btrfs_relocate_sys_chunks(struct btrfs_root
*root
)
2796 struct btrfs_root
*chunk_root
= root
->fs_info
->chunk_root
;
2797 struct btrfs_path
*path
;
2798 struct extent_buffer
*leaf
;
2799 struct btrfs_chunk
*chunk
;
2800 struct btrfs_key key
;
2801 struct btrfs_key found_key
;
2803 bool retried
= false;
2807 path
= btrfs_alloc_path();
2812 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2813 key
.offset
= (u64
)-1;
2814 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2817 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
2820 BUG_ON(ret
== 0); /* Corruption */
2822 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
2829 leaf
= path
->nodes
[0];
2830 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2832 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
2833 struct btrfs_chunk
);
2834 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
2835 btrfs_release_path(path
);
2837 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2838 ret
= btrfs_relocate_chunk(chunk_root
,
2847 if (found_key
.offset
== 0)
2849 key
.offset
= found_key
.offset
- 1;
2852 if (failed
&& !retried
) {
2856 } else if (WARN_ON(failed
&& retried
)) {
2860 btrfs_free_path(path
);
2864 static int insert_balance_item(struct btrfs_root
*root
,
2865 struct btrfs_balance_control
*bctl
)
2867 struct btrfs_trans_handle
*trans
;
2868 struct btrfs_balance_item
*item
;
2869 struct btrfs_disk_balance_args disk_bargs
;
2870 struct btrfs_path
*path
;
2871 struct extent_buffer
*leaf
;
2872 struct btrfs_key key
;
2875 path
= btrfs_alloc_path();
2879 trans
= btrfs_start_transaction(root
, 0);
2880 if (IS_ERR(trans
)) {
2881 btrfs_free_path(path
);
2882 return PTR_ERR(trans
);
2885 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2886 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2889 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2894 leaf
= path
->nodes
[0];
2895 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
2897 memset_extent_buffer(leaf
, 0, (unsigned long)item
, sizeof(*item
));
2899 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
2900 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
2901 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
2902 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
2903 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
2904 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
2906 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
2908 btrfs_mark_buffer_dirty(leaf
);
2910 btrfs_free_path(path
);
2911 err
= btrfs_commit_transaction(trans
, root
);
2917 static int del_balance_item(struct btrfs_root
*root
)
2919 struct btrfs_trans_handle
*trans
;
2920 struct btrfs_path
*path
;
2921 struct btrfs_key key
;
2924 path
= btrfs_alloc_path();
2928 trans
= btrfs_start_transaction(root
, 0);
2929 if (IS_ERR(trans
)) {
2930 btrfs_free_path(path
);
2931 return PTR_ERR(trans
);
2934 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
2935 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
2938 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2946 ret
= btrfs_del_item(trans
, root
, path
);
2948 btrfs_free_path(path
);
2949 err
= btrfs_commit_transaction(trans
, root
);
2956 * This is a heuristic used to reduce the number of chunks balanced on
2957 * resume after balance was interrupted.
2959 static void update_balance_args(struct btrfs_balance_control
*bctl
)
2962 * Turn on soft mode for chunk types that were being converted.
2964 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2965 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2966 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2967 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2968 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
2969 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
2972 * Turn on usage filter if is not already used. The idea is
2973 * that chunks that we have already balanced should be
2974 * reasonably full. Don't do it for chunks that are being
2975 * converted - that will keep us from relocating unconverted
2976 * (albeit full) chunks.
2978 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2979 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2980 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2981 bctl
->data
.usage
= 90;
2983 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2984 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2985 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2986 bctl
->sys
.usage
= 90;
2988 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
2989 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
2990 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
2991 bctl
->meta
.usage
= 90;
2996 * Should be called with both balance and volume mutexes held to
2997 * serialize other volume operations (add_dev/rm_dev/resize) with
2998 * restriper. Same goes for unset_balance_control.
3000 static void set_balance_control(struct btrfs_balance_control
*bctl
)
3002 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
3004 BUG_ON(fs_info
->balance_ctl
);
3006 spin_lock(&fs_info
->balance_lock
);
3007 fs_info
->balance_ctl
= bctl
;
3008 spin_unlock(&fs_info
->balance_lock
);
3011 static void unset_balance_control(struct btrfs_fs_info
*fs_info
)
3013 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3015 BUG_ON(!fs_info
->balance_ctl
);
3017 spin_lock(&fs_info
->balance_lock
);
3018 fs_info
->balance_ctl
= NULL
;
3019 spin_unlock(&fs_info
->balance_lock
);
3025 * Balance filters. Return 1 if chunk should be filtered out
3026 * (should not be balanced).
3028 static int chunk_profiles_filter(u64 chunk_type
,
3029 struct btrfs_balance_args
*bargs
)
3031 chunk_type
= chunk_to_extended(chunk_type
) &
3032 BTRFS_EXTENDED_PROFILE_MASK
;
3034 if (bargs
->profiles
& chunk_type
)
3040 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
3041 struct btrfs_balance_args
*bargs
)
3043 struct btrfs_block_group_cache
*cache
;
3044 u64 chunk_used
, user_thresh
;
3047 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3048 chunk_used
= btrfs_block_group_used(&cache
->item
);
3050 if (bargs
->usage
== 0)
3052 else if (bargs
->usage
> 100)
3053 user_thresh
= cache
->key
.offset
;
3055 user_thresh
= div_factor_fine(cache
->key
.offset
,
3058 if (chunk_used
< user_thresh
)
3061 btrfs_put_block_group(cache
);
3065 static int chunk_devid_filter(struct extent_buffer
*leaf
,
3066 struct btrfs_chunk
*chunk
,
3067 struct btrfs_balance_args
*bargs
)
3069 struct btrfs_stripe
*stripe
;
3070 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3073 for (i
= 0; i
< num_stripes
; i
++) {
3074 stripe
= btrfs_stripe_nr(chunk
, i
);
3075 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
3082 /* [pstart, pend) */
3083 static int chunk_drange_filter(struct extent_buffer
*leaf
,
3084 struct btrfs_chunk
*chunk
,
3086 struct btrfs_balance_args
*bargs
)
3088 struct btrfs_stripe
*stripe
;
3089 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3095 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
3098 if (btrfs_chunk_type(leaf
, chunk
) & (BTRFS_BLOCK_GROUP_DUP
|
3099 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)) {
3100 factor
= num_stripes
/ 2;
3101 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID5
) {
3102 factor
= num_stripes
- 1;
3103 } else if (btrfs_chunk_type(leaf
, chunk
) & BTRFS_BLOCK_GROUP_RAID6
) {
3104 factor
= num_stripes
- 2;
3106 factor
= num_stripes
;
3109 for (i
= 0; i
< num_stripes
; i
++) {
3110 stripe
= btrfs_stripe_nr(chunk
, i
);
3111 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
3114 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
3115 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
3116 stripe_length
= div_u64(stripe_length
, factor
);
3118 if (stripe_offset
< bargs
->pend
&&
3119 stripe_offset
+ stripe_length
> bargs
->pstart
)
3126 /* [vstart, vend) */
3127 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
3128 struct btrfs_chunk
*chunk
,
3130 struct btrfs_balance_args
*bargs
)
3132 if (chunk_offset
< bargs
->vend
&&
3133 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
3134 /* at least part of the chunk is inside this vrange */
3140 static int chunk_soft_convert_filter(u64 chunk_type
,
3141 struct btrfs_balance_args
*bargs
)
3143 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
3146 chunk_type
= chunk_to_extended(chunk_type
) &
3147 BTRFS_EXTENDED_PROFILE_MASK
;
3149 if (bargs
->target
== chunk_type
)
3155 static int should_balance_chunk(struct btrfs_root
*root
,
3156 struct extent_buffer
*leaf
,
3157 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
3159 struct btrfs_balance_control
*bctl
= root
->fs_info
->balance_ctl
;
3160 struct btrfs_balance_args
*bargs
= NULL
;
3161 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3164 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
3165 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
3169 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3170 bargs
= &bctl
->data
;
3171 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3173 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3174 bargs
= &bctl
->meta
;
3176 /* profiles filter */
3177 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
3178 chunk_profiles_filter(chunk_type
, bargs
)) {
3183 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3184 chunk_usage_filter(bctl
->fs_info
, chunk_offset
, bargs
)) {
3189 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
3190 chunk_devid_filter(leaf
, chunk
, bargs
)) {
3194 /* drange filter, makes sense only with devid filter */
3195 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
3196 chunk_drange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3201 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
3202 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3206 /* soft profile changing mode */
3207 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
3208 chunk_soft_convert_filter(chunk_type
, bargs
)) {
3213 * limited by count, must be the last filter
3215 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT
)) {
3216 if (bargs
->limit
== 0)
3225 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
3227 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3228 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3229 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
3230 struct list_head
*devices
;
3231 struct btrfs_device
*device
;
3234 struct btrfs_chunk
*chunk
;
3235 struct btrfs_path
*path
;
3236 struct btrfs_key key
;
3237 struct btrfs_key found_key
;
3238 struct btrfs_trans_handle
*trans
;
3239 struct extent_buffer
*leaf
;
3242 int enospc_errors
= 0;
3243 bool counting
= true;
3244 u64 limit_data
= bctl
->data
.limit
;
3245 u64 limit_meta
= bctl
->meta
.limit
;
3246 u64 limit_sys
= bctl
->sys
.limit
;
3248 /* step one make some room on all the devices */
3249 devices
= &fs_info
->fs_devices
->devices
;
3250 list_for_each_entry(device
, devices
, dev_list
) {
3251 old_size
= btrfs_device_get_total_bytes(device
);
3252 size_to_free
= div_factor(old_size
, 1);
3253 size_to_free
= min(size_to_free
, (u64
)1 * 1024 * 1024);
3254 if (!device
->writeable
||
3255 btrfs_device_get_total_bytes(device
) -
3256 btrfs_device_get_bytes_used(device
) > size_to_free
||
3257 device
->is_tgtdev_for_dev_replace
)
3260 ret
= btrfs_shrink_device(device
, old_size
- size_to_free
);
3265 trans
= btrfs_start_transaction(dev_root
, 0);
3266 BUG_ON(IS_ERR(trans
));
3268 ret
= btrfs_grow_device(trans
, device
, old_size
);
3271 btrfs_end_transaction(trans
, dev_root
);
3274 /* step two, relocate all the chunks */
3275 path
= btrfs_alloc_path();
3281 /* zero out stat counters */
3282 spin_lock(&fs_info
->balance_lock
);
3283 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
3284 spin_unlock(&fs_info
->balance_lock
);
3287 bctl
->data
.limit
= limit_data
;
3288 bctl
->meta
.limit
= limit_meta
;
3289 bctl
->sys
.limit
= limit_sys
;
3291 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3292 key
.offset
= (u64
)-1;
3293 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3296 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
3297 atomic_read(&fs_info
->balance_cancel_req
)) {
3302 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3307 * this shouldn't happen, it means the last relocate
3311 BUG(); /* FIXME break ? */
3313 ret
= btrfs_previous_item(chunk_root
, path
, 0,
3314 BTRFS_CHUNK_ITEM_KEY
);
3320 leaf
= path
->nodes
[0];
3321 slot
= path
->slots
[0];
3322 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3324 if (found_key
.objectid
!= key
.objectid
)
3327 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3330 spin_lock(&fs_info
->balance_lock
);
3331 bctl
->stat
.considered
++;
3332 spin_unlock(&fs_info
->balance_lock
);
3335 ret
= should_balance_chunk(chunk_root
, leaf
, chunk
,
3337 btrfs_release_path(path
);
3342 spin_lock(&fs_info
->balance_lock
);
3343 bctl
->stat
.expected
++;
3344 spin_unlock(&fs_info
->balance_lock
);
3348 ret
= btrfs_relocate_chunk(chunk_root
,
3351 if (ret
&& ret
!= -ENOSPC
)
3353 if (ret
== -ENOSPC
) {
3356 spin_lock(&fs_info
->balance_lock
);
3357 bctl
->stat
.completed
++;
3358 spin_unlock(&fs_info
->balance_lock
);
3361 if (found_key
.offset
== 0)
3363 key
.offset
= found_key
.offset
- 1;
3367 btrfs_release_path(path
);
3372 btrfs_free_path(path
);
3373 if (enospc_errors
) {
3374 btrfs_info(fs_info
, "%d enospc errors during balance",
3384 * alloc_profile_is_valid - see if a given profile is valid and reduced
3385 * @flags: profile to validate
3386 * @extended: if true @flags is treated as an extended profile
3388 static int alloc_profile_is_valid(u64 flags
, int extended
)
3390 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3391 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3393 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3395 /* 1) check that all other bits are zeroed */
3399 /* 2) see if profile is reduced */
3401 return !extended
; /* "0" is valid for usual profiles */
3403 /* true if exactly one bit set */
3404 return (flags
& (flags
- 1)) == 0;
3407 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3409 /* cancel requested || normal exit path */
3410 return atomic_read(&fs_info
->balance_cancel_req
) ||
3411 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3412 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3415 static void __cancel_balance(struct btrfs_fs_info
*fs_info
)
3419 unset_balance_control(fs_info
);
3420 ret
= del_balance_item(fs_info
->tree_root
);
3422 btrfs_std_error(fs_info
, ret
);
3424 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3428 * Should be called with both balance and volume mutexes held
3430 int btrfs_balance(struct btrfs_balance_control
*bctl
,
3431 struct btrfs_ioctl_balance_args
*bargs
)
3433 struct btrfs_fs_info
*fs_info
= bctl
->fs_info
;
3440 if (btrfs_fs_closing(fs_info
) ||
3441 atomic_read(&fs_info
->balance_pause_req
) ||
3442 atomic_read(&fs_info
->balance_cancel_req
)) {
3447 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
3448 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
3452 * In case of mixed groups both data and meta should be picked,
3453 * and identical options should be given for both of them.
3455 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
3456 if (mixed
&& (bctl
->flags
& allowed
)) {
3457 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
3458 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
3459 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
3460 btrfs_err(fs_info
, "with mixed groups data and "
3461 "metadata balance options must be the same");
3467 num_devices
= fs_info
->fs_devices
->num_devices
;
3468 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
3469 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
3470 BUG_ON(num_devices
< 1);
3473 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
3474 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
3475 if (num_devices
== 1)
3476 allowed
|= BTRFS_BLOCK_GROUP_DUP
;
3477 else if (num_devices
> 1)
3478 allowed
|= (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
);
3479 if (num_devices
> 2)
3480 allowed
|= BTRFS_BLOCK_GROUP_RAID5
;
3481 if (num_devices
> 3)
3482 allowed
|= (BTRFS_BLOCK_GROUP_RAID10
|
3483 BTRFS_BLOCK_GROUP_RAID6
);
3484 if ((bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3485 (!alloc_profile_is_valid(bctl
->data
.target
, 1) ||
3486 (bctl
->data
.target
& ~allowed
))) {
3487 btrfs_err(fs_info
, "unable to start balance with target "
3488 "data profile %llu",
3493 if ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3494 (!alloc_profile_is_valid(bctl
->meta
.target
, 1) ||
3495 (bctl
->meta
.target
& ~allowed
))) {
3497 "unable to start balance with target metadata profile %llu",
3502 if ((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3503 (!alloc_profile_is_valid(bctl
->sys
.target
, 1) ||
3504 (bctl
->sys
.target
& ~allowed
))) {
3506 "unable to start balance with target system profile %llu",
3512 /* allow dup'ed data chunks only in mixed mode */
3513 if (!mixed
&& (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3514 (bctl
->data
.target
& BTRFS_BLOCK_GROUP_DUP
)) {
3515 btrfs_err(fs_info
, "dup for data is not allowed");
3520 /* allow to reduce meta or sys integrity only if force set */
3521 allowed
= BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
|
3522 BTRFS_BLOCK_GROUP_RAID10
|
3523 BTRFS_BLOCK_GROUP_RAID5
|
3524 BTRFS_BLOCK_GROUP_RAID6
;
3526 seq
= read_seqbegin(&fs_info
->profiles_lock
);
3528 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3529 (fs_info
->avail_system_alloc_bits
& allowed
) &&
3530 !(bctl
->sys
.target
& allowed
)) ||
3531 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
3532 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
3533 !(bctl
->meta
.target
& allowed
))) {
3534 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
3535 btrfs_info(fs_info
, "force reducing metadata integrity");
3537 btrfs_err(fs_info
, "balance will reduce metadata "
3538 "integrity, use force if you want this");
3543 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
3545 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3546 int num_tolerated_disk_barrier_failures
;
3547 u64 target
= bctl
->sys
.target
;
3549 num_tolerated_disk_barrier_failures
=
3550 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3551 if (num_tolerated_disk_barrier_failures
> 0 &&
3553 (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID0
|
3554 BTRFS_AVAIL_ALLOC_BIT_SINGLE
)))
3555 num_tolerated_disk_barrier_failures
= 0;
3556 else if (num_tolerated_disk_barrier_failures
> 1 &&
3558 (BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
)))
3559 num_tolerated_disk_barrier_failures
= 1;
3561 fs_info
->num_tolerated_disk_barrier_failures
=
3562 num_tolerated_disk_barrier_failures
;
3565 ret
= insert_balance_item(fs_info
->tree_root
, bctl
);
3566 if (ret
&& ret
!= -EEXIST
)
3569 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
3570 BUG_ON(ret
== -EEXIST
);
3571 set_balance_control(bctl
);
3573 BUG_ON(ret
!= -EEXIST
);
3574 spin_lock(&fs_info
->balance_lock
);
3575 update_balance_args(bctl
);
3576 spin_unlock(&fs_info
->balance_lock
);
3579 atomic_inc(&fs_info
->balance_running
);
3580 mutex_unlock(&fs_info
->balance_mutex
);
3582 ret
= __btrfs_balance(fs_info
);
3584 mutex_lock(&fs_info
->balance_mutex
);
3585 atomic_dec(&fs_info
->balance_running
);
3587 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) {
3588 fs_info
->num_tolerated_disk_barrier_failures
=
3589 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info
);
3593 memset(bargs
, 0, sizeof(*bargs
));
3594 update_ioctl_balance_args(fs_info
, 0, bargs
);
3597 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
3598 balance_need_close(fs_info
)) {
3599 __cancel_balance(fs_info
);
3602 wake_up(&fs_info
->balance_wait_q
);
3606 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
3607 __cancel_balance(fs_info
);
3610 atomic_set(&fs_info
->mutually_exclusive_operation_running
, 0);
3615 static int balance_kthread(void *data
)
3617 struct btrfs_fs_info
*fs_info
= data
;
3620 mutex_lock(&fs_info
->volume_mutex
);
3621 mutex_lock(&fs_info
->balance_mutex
);
3623 if (fs_info
->balance_ctl
) {
3624 btrfs_info(fs_info
, "continuing balance");
3625 ret
= btrfs_balance(fs_info
->balance_ctl
, NULL
);
3628 mutex_unlock(&fs_info
->balance_mutex
);
3629 mutex_unlock(&fs_info
->volume_mutex
);
3634 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
3636 struct task_struct
*tsk
;
3638 spin_lock(&fs_info
->balance_lock
);
3639 if (!fs_info
->balance_ctl
) {
3640 spin_unlock(&fs_info
->balance_lock
);
3643 spin_unlock(&fs_info
->balance_lock
);
3645 if (btrfs_test_opt(fs_info
->tree_root
, SKIP_BALANCE
)) {
3646 btrfs_info(fs_info
, "force skipping balance");
3650 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
3651 return PTR_ERR_OR_ZERO(tsk
);
3654 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
3656 struct btrfs_balance_control
*bctl
;
3657 struct btrfs_balance_item
*item
;
3658 struct btrfs_disk_balance_args disk_bargs
;
3659 struct btrfs_path
*path
;
3660 struct extent_buffer
*leaf
;
3661 struct btrfs_key key
;
3664 path
= btrfs_alloc_path();
3668 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3669 key
.type
= BTRFS_BALANCE_ITEM_KEY
;
3672 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3675 if (ret
> 0) { /* ret = -ENOENT; */
3680 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3686 leaf
= path
->nodes
[0];
3687 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3689 bctl
->fs_info
= fs_info
;
3690 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
3691 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3693 btrfs_balance_data(leaf
, item
, &disk_bargs
);
3694 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
3695 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
3696 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
3697 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
3698 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
3700 WARN_ON(atomic_xchg(&fs_info
->mutually_exclusive_operation_running
, 1));
3702 mutex_lock(&fs_info
->volume_mutex
);
3703 mutex_lock(&fs_info
->balance_mutex
);
3705 set_balance_control(bctl
);
3707 mutex_unlock(&fs_info
->balance_mutex
);
3708 mutex_unlock(&fs_info
->volume_mutex
);
3710 btrfs_free_path(path
);
3714 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
3718 mutex_lock(&fs_info
->balance_mutex
);
3719 if (!fs_info
->balance_ctl
) {
3720 mutex_unlock(&fs_info
->balance_mutex
);
3724 if (atomic_read(&fs_info
->balance_running
)) {
3725 atomic_inc(&fs_info
->balance_pause_req
);
3726 mutex_unlock(&fs_info
->balance_mutex
);
3728 wait_event(fs_info
->balance_wait_q
,
3729 atomic_read(&fs_info
->balance_running
) == 0);
3731 mutex_lock(&fs_info
->balance_mutex
);
3732 /* we are good with balance_ctl ripped off from under us */
3733 BUG_ON(atomic_read(&fs_info
->balance_running
));
3734 atomic_dec(&fs_info
->balance_pause_req
);
3739 mutex_unlock(&fs_info
->balance_mutex
);
3743 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
3745 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
3748 mutex_lock(&fs_info
->balance_mutex
);
3749 if (!fs_info
->balance_ctl
) {
3750 mutex_unlock(&fs_info
->balance_mutex
);
3754 atomic_inc(&fs_info
->balance_cancel_req
);
3756 * if we are running just wait and return, balance item is
3757 * deleted in btrfs_balance in this case
3759 if (atomic_read(&fs_info
->balance_running
)) {
3760 mutex_unlock(&fs_info
->balance_mutex
);
3761 wait_event(fs_info
->balance_wait_q
,
3762 atomic_read(&fs_info
->balance_running
) == 0);
3763 mutex_lock(&fs_info
->balance_mutex
);
3765 /* __cancel_balance needs volume_mutex */
3766 mutex_unlock(&fs_info
->balance_mutex
);
3767 mutex_lock(&fs_info
->volume_mutex
);
3768 mutex_lock(&fs_info
->balance_mutex
);
3770 if (fs_info
->balance_ctl
)
3771 __cancel_balance(fs_info
);
3773 mutex_unlock(&fs_info
->volume_mutex
);
3776 BUG_ON(fs_info
->balance_ctl
|| atomic_read(&fs_info
->balance_running
));
3777 atomic_dec(&fs_info
->balance_cancel_req
);
3778 mutex_unlock(&fs_info
->balance_mutex
);
3782 static int btrfs_uuid_scan_kthread(void *data
)
3784 struct btrfs_fs_info
*fs_info
= data
;
3785 struct btrfs_root
*root
= fs_info
->tree_root
;
3786 struct btrfs_key key
;
3787 struct btrfs_key max_key
;
3788 struct btrfs_path
*path
= NULL
;
3790 struct extent_buffer
*eb
;
3792 struct btrfs_root_item root_item
;
3794 struct btrfs_trans_handle
*trans
= NULL
;
3796 path
= btrfs_alloc_path();
3803 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3806 max_key
.objectid
= (u64
)-1;
3807 max_key
.type
= BTRFS_ROOT_ITEM_KEY
;
3808 max_key
.offset
= (u64
)-1;
3811 ret
= btrfs_search_forward(root
, &key
, path
, 0);
3818 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
||
3819 (key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
3820 key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) ||
3821 key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
3824 eb
= path
->nodes
[0];
3825 slot
= path
->slots
[0];
3826 item_size
= btrfs_item_size_nr(eb
, slot
);
3827 if (item_size
< sizeof(root_item
))
3830 read_extent_buffer(eb
, &root_item
,
3831 btrfs_item_ptr_offset(eb
, slot
),
3832 (int)sizeof(root_item
));
3833 if (btrfs_root_refs(&root_item
) == 0)
3836 if (!btrfs_is_empty_uuid(root_item
.uuid
) ||
3837 !btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3841 btrfs_release_path(path
);
3843 * 1 - subvol uuid item
3844 * 1 - received_subvol uuid item
3846 trans
= btrfs_start_transaction(fs_info
->uuid_root
, 2);
3847 if (IS_ERR(trans
)) {
3848 ret
= PTR_ERR(trans
);
3856 if (!btrfs_is_empty_uuid(root_item
.uuid
)) {
3857 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3859 BTRFS_UUID_KEY_SUBVOL
,
3862 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3868 if (!btrfs_is_empty_uuid(root_item
.received_uuid
)) {
3869 ret
= btrfs_uuid_tree_add(trans
, fs_info
->uuid_root
,
3870 root_item
.received_uuid
,
3871 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
3874 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
3882 ret
= btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3888 btrfs_release_path(path
);
3889 if (key
.offset
< (u64
)-1) {
3891 } else if (key
.type
< BTRFS_ROOT_ITEM_KEY
) {
3893 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3894 } else if (key
.objectid
< (u64
)-1) {
3896 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3905 btrfs_free_path(path
);
3906 if (trans
&& !IS_ERR(trans
))
3907 btrfs_end_transaction(trans
, fs_info
->uuid_root
);
3909 btrfs_warn(fs_info
, "btrfs_uuid_scan_kthread failed %d", ret
);
3911 fs_info
->update_uuid_tree_gen
= 1;
3912 up(&fs_info
->uuid_tree_rescan_sem
);
3917 * Callback for btrfs_uuid_tree_iterate().
3919 * 0 check succeeded, the entry is not outdated.
3920 * < 0 if an error occured.
3921 * > 0 if the check failed, which means the caller shall remove the entry.
3923 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info
*fs_info
,
3924 u8
*uuid
, u8 type
, u64 subid
)
3926 struct btrfs_key key
;
3928 struct btrfs_root
*subvol_root
;
3930 if (type
!= BTRFS_UUID_KEY_SUBVOL
&&
3931 type
!= BTRFS_UUID_KEY_RECEIVED_SUBVOL
)
3934 key
.objectid
= subid
;
3935 key
.type
= BTRFS_ROOT_ITEM_KEY
;
3936 key
.offset
= (u64
)-1;
3937 subvol_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
3938 if (IS_ERR(subvol_root
)) {
3939 ret
= PTR_ERR(subvol_root
);
3946 case BTRFS_UUID_KEY_SUBVOL
:
3947 if (memcmp(uuid
, subvol_root
->root_item
.uuid
, BTRFS_UUID_SIZE
))
3950 case BTRFS_UUID_KEY_RECEIVED_SUBVOL
:
3951 if (memcmp(uuid
, subvol_root
->root_item
.received_uuid
,
3961 static int btrfs_uuid_rescan_kthread(void *data
)
3963 struct btrfs_fs_info
*fs_info
= (struct btrfs_fs_info
*)data
;
3967 * 1st step is to iterate through the existing UUID tree and
3968 * to delete all entries that contain outdated data.
3969 * 2nd step is to add all missing entries to the UUID tree.
3971 ret
= btrfs_uuid_tree_iterate(fs_info
, btrfs_check_uuid_tree_entry
);
3973 btrfs_warn(fs_info
, "iterating uuid_tree failed %d", ret
);
3974 up(&fs_info
->uuid_tree_rescan_sem
);
3977 return btrfs_uuid_scan_kthread(data
);
3980 int btrfs_create_uuid_tree(struct btrfs_fs_info
*fs_info
)
3982 struct btrfs_trans_handle
*trans
;
3983 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
3984 struct btrfs_root
*uuid_root
;
3985 struct task_struct
*task
;
3992 trans
= btrfs_start_transaction(tree_root
, 2);
3994 return PTR_ERR(trans
);
3996 uuid_root
= btrfs_create_tree(trans
, fs_info
,
3997 BTRFS_UUID_TREE_OBJECTID
);
3998 if (IS_ERR(uuid_root
)) {
3999 ret
= PTR_ERR(uuid_root
);
4000 btrfs_abort_transaction(trans
, tree_root
, ret
);
4004 fs_info
->uuid_root
= uuid_root
;
4006 ret
= btrfs_commit_transaction(trans
, tree_root
);
4010 down(&fs_info
->uuid_tree_rescan_sem
);
4011 task
= kthread_run(btrfs_uuid_scan_kthread
, fs_info
, "btrfs-uuid");
4013 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4014 btrfs_warn(fs_info
, "failed to start uuid_scan task");
4015 up(&fs_info
->uuid_tree_rescan_sem
);
4016 return PTR_ERR(task
);
4022 int btrfs_check_uuid_tree(struct btrfs_fs_info
*fs_info
)
4024 struct task_struct
*task
;
4026 down(&fs_info
->uuid_tree_rescan_sem
);
4027 task
= kthread_run(btrfs_uuid_rescan_kthread
, fs_info
, "btrfs-uuid");
4029 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4030 btrfs_warn(fs_info
, "failed to start uuid_rescan task");
4031 up(&fs_info
->uuid_tree_rescan_sem
);
4032 return PTR_ERR(task
);
4039 * shrinking a device means finding all of the device extents past
4040 * the new size, and then following the back refs to the chunks.
4041 * The chunk relocation code actually frees the device extent
4043 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
4045 struct btrfs_trans_handle
*trans
;
4046 struct btrfs_root
*root
= device
->dev_root
;
4047 struct btrfs_dev_extent
*dev_extent
= NULL
;
4048 struct btrfs_path
*path
;
4055 bool retried
= false;
4056 bool checked_pending_chunks
= false;
4057 struct extent_buffer
*l
;
4058 struct btrfs_key key
;
4059 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4060 u64 old_total
= btrfs_super_total_bytes(super_copy
);
4061 u64 old_size
= btrfs_device_get_total_bytes(device
);
4062 u64 diff
= old_size
- new_size
;
4064 if (device
->is_tgtdev_for_dev_replace
)
4067 path
= btrfs_alloc_path();
4075 btrfs_device_set_total_bytes(device
, new_size
);
4076 if (device
->writeable
) {
4077 device
->fs_devices
->total_rw_bytes
-= diff
;
4078 spin_lock(&root
->fs_info
->free_chunk_lock
);
4079 root
->fs_info
->free_chunk_space
-= diff
;
4080 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4082 unlock_chunks(root
);
4085 key
.objectid
= device
->devid
;
4086 key
.offset
= (u64
)-1;
4087 key
.type
= BTRFS_DEV_EXTENT_KEY
;
4090 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4094 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
4099 btrfs_release_path(path
);
4104 slot
= path
->slots
[0];
4105 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
4107 if (key
.objectid
!= device
->devid
) {
4108 btrfs_release_path(path
);
4112 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
4113 length
= btrfs_dev_extent_length(l
, dev_extent
);
4115 if (key
.offset
+ length
<= new_size
) {
4116 btrfs_release_path(path
);
4120 chunk_objectid
= btrfs_dev_extent_chunk_objectid(l
, dev_extent
);
4121 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
4122 btrfs_release_path(path
);
4124 ret
= btrfs_relocate_chunk(root
, chunk_objectid
, chunk_offset
);
4125 if (ret
&& ret
!= -ENOSPC
)
4129 } while (key
.offset
-- > 0);
4131 if (failed
&& !retried
) {
4135 } else if (failed
&& retried
) {
4140 /* Shrinking succeeded, else we would be at "done". */
4141 trans
= btrfs_start_transaction(root
, 0);
4142 if (IS_ERR(trans
)) {
4143 ret
= PTR_ERR(trans
);
4150 * We checked in the above loop all device extents that were already in
4151 * the device tree. However before we have updated the device's
4152 * total_bytes to the new size, we might have had chunk allocations that
4153 * have not complete yet (new block groups attached to transaction
4154 * handles), and therefore their device extents were not yet in the
4155 * device tree and we missed them in the loop above. So if we have any
4156 * pending chunk using a device extent that overlaps the device range
4157 * that we can not use anymore, commit the current transaction and
4158 * repeat the search on the device tree - this way we guarantee we will
4159 * not have chunks using device extents that end beyond 'new_size'.
4161 if (!checked_pending_chunks
) {
4162 u64 start
= new_size
;
4163 u64 len
= old_size
- new_size
;
4165 if (contains_pending_extent(trans
, device
, &start
, len
)) {
4166 unlock_chunks(root
);
4167 checked_pending_chunks
= true;
4170 ret
= btrfs_commit_transaction(trans
, root
);
4177 btrfs_device_set_disk_total_bytes(device
, new_size
);
4178 if (list_empty(&device
->resized_list
))
4179 list_add_tail(&device
->resized_list
,
4180 &root
->fs_info
->fs_devices
->resized_devices
);
4182 WARN_ON(diff
> old_total
);
4183 btrfs_set_super_total_bytes(super_copy
, old_total
- diff
);
4184 unlock_chunks(root
);
4186 /* Now btrfs_update_device() will change the on-disk size. */
4187 ret
= btrfs_update_device(trans
, device
);
4188 btrfs_end_transaction(trans
, root
);
4190 btrfs_free_path(path
);
4193 btrfs_device_set_total_bytes(device
, old_size
);
4194 if (device
->writeable
)
4195 device
->fs_devices
->total_rw_bytes
+= diff
;
4196 spin_lock(&root
->fs_info
->free_chunk_lock
);
4197 root
->fs_info
->free_chunk_space
+= diff
;
4198 spin_unlock(&root
->fs_info
->free_chunk_lock
);
4199 unlock_chunks(root
);
4204 static int btrfs_add_system_chunk(struct btrfs_root
*root
,
4205 struct btrfs_key
*key
,
4206 struct btrfs_chunk
*chunk
, int item_size
)
4208 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
4209 struct btrfs_disk_key disk_key
;
4214 array_size
= btrfs_super_sys_array_size(super_copy
);
4215 if (array_size
+ item_size
+ sizeof(disk_key
)
4216 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
) {
4217 unlock_chunks(root
);
4221 ptr
= super_copy
->sys_chunk_array
+ array_size
;
4222 btrfs_cpu_key_to_disk(&disk_key
, key
);
4223 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
4224 ptr
+= sizeof(disk_key
);
4225 memcpy(ptr
, chunk
, item_size
);
4226 item_size
+= sizeof(disk_key
);
4227 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
4228 unlock_chunks(root
);
4234 * sort the devices in descending order by max_avail, total_avail
4236 static int btrfs_cmp_device_info(const void *a
, const void *b
)
4238 const struct btrfs_device_info
*di_a
= a
;
4239 const struct btrfs_device_info
*di_b
= b
;
4241 if (di_a
->max_avail
> di_b
->max_avail
)
4243 if (di_a
->max_avail
< di_b
->max_avail
)
4245 if (di_a
->total_avail
> di_b
->total_avail
)
4247 if (di_a
->total_avail
< di_b
->total_avail
)
4252 static const struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
4253 [BTRFS_RAID_RAID10
] = {
4256 .devs_max
= 0, /* 0 == as many as possible */
4258 .devs_increment
= 2,
4261 [BTRFS_RAID_RAID1
] = {
4266 .devs_increment
= 2,
4269 [BTRFS_RAID_DUP
] = {
4274 .devs_increment
= 1,
4277 [BTRFS_RAID_RAID0
] = {
4282 .devs_increment
= 1,
4285 [BTRFS_RAID_SINGLE
] = {
4290 .devs_increment
= 1,
4293 [BTRFS_RAID_RAID5
] = {
4298 .devs_increment
= 1,
4301 [BTRFS_RAID_RAID6
] = {
4306 .devs_increment
= 1,
4311 static u32
find_raid56_stripe_len(u32 data_devices
, u32 dev_stripe_target
)
4313 /* TODO allow them to set a preferred stripe size */
4317 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4319 if (!(type
& BTRFS_BLOCK_GROUP_RAID56_MASK
))
4322 btrfs_set_fs_incompat(info
, RAID56
);
4325 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4326 - sizeof(struct btrfs_item) \
4327 - sizeof(struct btrfs_chunk)) \
4328 / sizeof(struct btrfs_stripe) + 1)
4330 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4331 - 2 * sizeof(struct btrfs_disk_key) \
4332 - 2 * sizeof(struct btrfs_chunk)) \
4333 / sizeof(struct btrfs_stripe) + 1)
4335 static int __btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4336 struct btrfs_root
*extent_root
, u64 start
,
4339 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
4340 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
4341 struct list_head
*cur
;
4342 struct map_lookup
*map
= NULL
;
4343 struct extent_map_tree
*em_tree
;
4344 struct extent_map
*em
;
4345 struct btrfs_device_info
*devices_info
= NULL
;
4347 int num_stripes
; /* total number of stripes to allocate */
4348 int data_stripes
; /* number of stripes that count for
4350 int sub_stripes
; /* sub_stripes info for map */
4351 int dev_stripes
; /* stripes per dev */
4352 int devs_max
; /* max devs to use */
4353 int devs_min
; /* min devs needed */
4354 int devs_increment
; /* ndevs has to be a multiple of this */
4355 int ncopies
; /* how many copies to data has */
4357 u64 max_stripe_size
;
4361 u64 raid_stripe_len
= BTRFS_STRIPE_LEN
;
4367 BUG_ON(!alloc_profile_is_valid(type
, 0));
4369 if (list_empty(&fs_devices
->alloc_list
))
4372 index
= __get_raid_index(type
);
4374 sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
4375 dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
4376 devs_max
= btrfs_raid_array
[index
].devs_max
;
4377 devs_min
= btrfs_raid_array
[index
].devs_min
;
4378 devs_increment
= btrfs_raid_array
[index
].devs_increment
;
4379 ncopies
= btrfs_raid_array
[index
].ncopies
;
4381 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
4382 max_stripe_size
= 1024 * 1024 * 1024;
4383 max_chunk_size
= 10 * max_stripe_size
;
4385 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4386 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
4387 /* for larger filesystems, use larger metadata chunks */
4388 if (fs_devices
->total_rw_bytes
> 50ULL * 1024 * 1024 * 1024)
4389 max_stripe_size
= 1024 * 1024 * 1024;
4391 max_stripe_size
= 256 * 1024 * 1024;
4392 max_chunk_size
= max_stripe_size
;
4394 devs_max
= BTRFS_MAX_DEVS(info
->chunk_root
);
4395 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4396 max_stripe_size
= 32 * 1024 * 1024;
4397 max_chunk_size
= 2 * max_stripe_size
;
4399 devs_max
= BTRFS_MAX_DEVS_SYS_CHUNK
;
4401 btrfs_err(info
, "invalid chunk type 0x%llx requested",
4406 /* we don't want a chunk larger than 10% of writeable space */
4407 max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
4410 devices_info
= kcalloc(fs_devices
->rw_devices
, sizeof(*devices_info
),
4415 cur
= fs_devices
->alloc_list
.next
;
4418 * in the first pass through the devices list, we gather information
4419 * about the available holes on each device.
4422 while (cur
!= &fs_devices
->alloc_list
) {
4423 struct btrfs_device
*device
;
4427 device
= list_entry(cur
, struct btrfs_device
, dev_alloc_list
);
4431 if (!device
->writeable
) {
4433 "BTRFS: read-only device in alloc_list\n");
4437 if (!device
->in_fs_metadata
||
4438 device
->is_tgtdev_for_dev_replace
)
4441 if (device
->total_bytes
> device
->bytes_used
)
4442 total_avail
= device
->total_bytes
- device
->bytes_used
;
4446 /* If there is no space on this device, skip it. */
4447 if (total_avail
== 0)
4450 ret
= find_free_dev_extent(trans
, device
,
4451 max_stripe_size
* dev_stripes
,
4452 &dev_offset
, &max_avail
);
4453 if (ret
&& ret
!= -ENOSPC
)
4457 max_avail
= max_stripe_size
* dev_stripes
;
4459 if (max_avail
< BTRFS_STRIPE_LEN
* dev_stripes
)
4462 if (ndevs
== fs_devices
->rw_devices
) {
4463 WARN(1, "%s: found more than %llu devices\n",
4464 __func__
, fs_devices
->rw_devices
);
4467 devices_info
[ndevs
].dev_offset
= dev_offset
;
4468 devices_info
[ndevs
].max_avail
= max_avail
;
4469 devices_info
[ndevs
].total_avail
= total_avail
;
4470 devices_info
[ndevs
].dev
= device
;
4475 * now sort the devices by hole size / available space
4477 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
4478 btrfs_cmp_device_info
, NULL
);
4480 /* round down to number of usable stripes */
4481 ndevs
-= ndevs
% devs_increment
;
4483 if (ndevs
< devs_increment
* sub_stripes
|| ndevs
< devs_min
) {
4488 if (devs_max
&& ndevs
> devs_max
)
4491 * the primary goal is to maximize the number of stripes, so use as many
4492 * devices as possible, even if the stripes are not maximum sized.
4494 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4495 num_stripes
= ndevs
* dev_stripes
;
4498 * this will have to be fixed for RAID1 and RAID10 over
4501 data_stripes
= num_stripes
/ ncopies
;
4503 if (type
& BTRFS_BLOCK_GROUP_RAID5
) {
4504 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 1,
4505 btrfs_super_stripesize(info
->super_copy
));
4506 data_stripes
= num_stripes
- 1;
4508 if (type
& BTRFS_BLOCK_GROUP_RAID6
) {
4509 raid_stripe_len
= find_raid56_stripe_len(ndevs
- 2,
4510 btrfs_super_stripesize(info
->super_copy
));
4511 data_stripes
= num_stripes
- 2;
4515 * Use the number of data stripes to figure out how big this chunk
4516 * is really going to be in terms of logical address space,
4517 * and compare that answer with the max chunk size
4519 if (stripe_size
* data_stripes
> max_chunk_size
) {
4520 u64 mask
= (1ULL << 24) - 1;
4522 stripe_size
= div_u64(max_chunk_size
, data_stripes
);
4524 /* bump the answer up to a 16MB boundary */
4525 stripe_size
= (stripe_size
+ mask
) & ~mask
;
4527 /* but don't go higher than the limits we found
4528 * while searching for free extents
4530 if (stripe_size
> devices_info
[ndevs
-1].max_avail
)
4531 stripe_size
= devices_info
[ndevs
-1].max_avail
;
4534 stripe_size
= div_u64(stripe_size
, dev_stripes
);
4536 /* align to BTRFS_STRIPE_LEN */
4537 stripe_size
= div_u64(stripe_size
, raid_stripe_len
);
4538 stripe_size
*= raid_stripe_len
;
4540 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
4545 map
->num_stripes
= num_stripes
;
4547 for (i
= 0; i
< ndevs
; ++i
) {
4548 for (j
= 0; j
< dev_stripes
; ++j
) {
4549 int s
= i
* dev_stripes
+ j
;
4550 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
4551 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
4555 map
->sector_size
= extent_root
->sectorsize
;
4556 map
->stripe_len
= raid_stripe_len
;
4557 map
->io_align
= raid_stripe_len
;
4558 map
->io_width
= raid_stripe_len
;
4560 map
->sub_stripes
= sub_stripes
;
4562 num_bytes
= stripe_size
* data_stripes
;
4564 trace_btrfs_chunk_alloc(info
->chunk_root
, map
, start
, num_bytes
);
4566 em
= alloc_extent_map();
4572 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
4573 em
->bdev
= (struct block_device
*)map
;
4575 em
->len
= num_bytes
;
4576 em
->block_start
= 0;
4577 em
->block_len
= em
->len
;
4578 em
->orig_block_len
= stripe_size
;
4580 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4581 write_lock(&em_tree
->lock
);
4582 ret
= add_extent_mapping(em_tree
, em
, 0);
4584 list_add_tail(&em
->list
, &trans
->transaction
->pending_chunks
);
4585 atomic_inc(&em
->refs
);
4587 write_unlock(&em_tree
->lock
);
4589 free_extent_map(em
);
4593 ret
= btrfs_make_block_group(trans
, extent_root
, 0, type
,
4594 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4597 goto error_del_extent
;
4599 for (i
= 0; i
< map
->num_stripes
; i
++) {
4600 num_bytes
= map
->stripes
[i
].dev
->bytes_used
+ stripe_size
;
4601 btrfs_device_set_bytes_used(map
->stripes
[i
].dev
, num_bytes
);
4604 spin_lock(&extent_root
->fs_info
->free_chunk_lock
);
4605 extent_root
->fs_info
->free_chunk_space
-= (stripe_size
*
4607 spin_unlock(&extent_root
->fs_info
->free_chunk_lock
);
4609 free_extent_map(em
);
4610 check_raid56_incompat_flag(extent_root
->fs_info
, type
);
4612 kfree(devices_info
);
4616 write_lock(&em_tree
->lock
);
4617 remove_extent_mapping(em_tree
, em
);
4618 write_unlock(&em_tree
->lock
);
4620 /* One for our allocation */
4621 free_extent_map(em
);
4622 /* One for the tree reference */
4623 free_extent_map(em
);
4624 /* One for the pending_chunks list reference */
4625 free_extent_map(em
);
4627 kfree(devices_info
);
4631 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
4632 struct btrfs_root
*extent_root
,
4633 u64 chunk_offset
, u64 chunk_size
)
4635 struct btrfs_key key
;
4636 struct btrfs_root
*chunk_root
= extent_root
->fs_info
->chunk_root
;
4637 struct btrfs_device
*device
;
4638 struct btrfs_chunk
*chunk
;
4639 struct btrfs_stripe
*stripe
;
4640 struct extent_map_tree
*em_tree
;
4641 struct extent_map
*em
;
4642 struct map_lookup
*map
;
4649 em_tree
= &extent_root
->fs_info
->mapping_tree
.map_tree
;
4650 read_lock(&em_tree
->lock
);
4651 em
= lookup_extent_mapping(em_tree
, chunk_offset
, chunk_size
);
4652 read_unlock(&em_tree
->lock
);
4655 btrfs_crit(extent_root
->fs_info
, "unable to find logical "
4656 "%Lu len %Lu", chunk_offset
, chunk_size
);
4660 if (em
->start
!= chunk_offset
|| em
->len
!= chunk_size
) {
4661 btrfs_crit(extent_root
->fs_info
, "found a bad mapping, wanted"
4662 " %Lu-%Lu, found %Lu-%Lu", chunk_offset
,
4663 chunk_size
, em
->start
, em
->len
);
4664 free_extent_map(em
);
4668 map
= (struct map_lookup
*)em
->bdev
;
4669 item_size
= btrfs_chunk_item_size(map
->num_stripes
);
4670 stripe_size
= em
->orig_block_len
;
4672 chunk
= kzalloc(item_size
, GFP_NOFS
);
4678 for (i
= 0; i
< map
->num_stripes
; i
++) {
4679 device
= map
->stripes
[i
].dev
;
4680 dev_offset
= map
->stripes
[i
].physical
;
4682 ret
= btrfs_update_device(trans
, device
);
4685 ret
= btrfs_alloc_dev_extent(trans
, device
,
4686 chunk_root
->root_key
.objectid
,
4687 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
4688 chunk_offset
, dev_offset
,
4694 stripe
= &chunk
->stripe
;
4695 for (i
= 0; i
< map
->num_stripes
; i
++) {
4696 device
= map
->stripes
[i
].dev
;
4697 dev_offset
= map
->stripes
[i
].physical
;
4699 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
4700 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
4701 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
4705 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
4706 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
4707 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
4708 btrfs_set_stack_chunk_type(chunk
, map
->type
);
4709 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
4710 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
4711 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
4712 btrfs_set_stack_chunk_sector_size(chunk
, extent_root
->sectorsize
);
4713 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
4715 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
4716 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
4717 key
.offset
= chunk_offset
;
4719 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
4720 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4722 * TODO: Cleanup of inserted chunk root in case of
4725 ret
= btrfs_add_system_chunk(chunk_root
, &key
, chunk
,
4731 free_extent_map(em
);
4736 * Chunk allocation falls into two parts. The first part does works
4737 * that make the new allocated chunk useable, but not do any operation
4738 * that modifies the chunk tree. The second part does the works that
4739 * require modifying the chunk tree. This division is important for the
4740 * bootstrap process of adding storage to a seed btrfs.
4742 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
,
4743 struct btrfs_root
*extent_root
, u64 type
)
4747 ASSERT(mutex_is_locked(&extent_root
->fs_info
->chunk_mutex
));
4748 chunk_offset
= find_next_chunk(extent_root
->fs_info
);
4749 return __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
, type
);
4752 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
,
4753 struct btrfs_root
*root
,
4754 struct btrfs_device
*device
)
4757 u64 sys_chunk_offset
;
4759 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4760 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
4763 chunk_offset
= find_next_chunk(fs_info
);
4764 alloc_profile
= btrfs_get_alloc_profile(extent_root
, 0);
4765 ret
= __btrfs_alloc_chunk(trans
, extent_root
, chunk_offset
,
4770 sys_chunk_offset
= find_next_chunk(root
->fs_info
);
4771 alloc_profile
= btrfs_get_alloc_profile(fs_info
->chunk_root
, 0);
4772 ret
= __btrfs_alloc_chunk(trans
, extent_root
, sys_chunk_offset
,
4777 static inline int btrfs_chunk_max_errors(struct map_lookup
*map
)
4781 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1
|
4782 BTRFS_BLOCK_GROUP_RAID10
|
4783 BTRFS_BLOCK_GROUP_RAID5
|
4784 BTRFS_BLOCK_GROUP_DUP
)) {
4786 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
) {
4795 int btrfs_chunk_readonly(struct btrfs_root
*root
, u64 chunk_offset
)
4797 struct extent_map
*em
;
4798 struct map_lookup
*map
;
4799 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
4804 read_lock(&map_tree
->map_tree
.lock
);
4805 em
= lookup_extent_mapping(&map_tree
->map_tree
, chunk_offset
, 1);
4806 read_unlock(&map_tree
->map_tree
.lock
);
4810 map
= (struct map_lookup
*)em
->bdev
;
4811 for (i
= 0; i
< map
->num_stripes
; i
++) {
4812 if (map
->stripes
[i
].dev
->missing
) {
4817 if (!map
->stripes
[i
].dev
->writeable
) {
4824 * If the number of missing devices is larger than max errors,
4825 * we can not write the data into that chunk successfully, so
4828 if (miss_ndevs
> btrfs_chunk_max_errors(map
))
4831 free_extent_map(em
);
4835 void btrfs_mapping_init(struct btrfs_mapping_tree
*tree
)
4837 extent_map_tree_init(&tree
->map_tree
);
4840 void btrfs_mapping_tree_free(struct btrfs_mapping_tree
*tree
)
4842 struct extent_map
*em
;
4845 write_lock(&tree
->map_tree
.lock
);
4846 em
= lookup_extent_mapping(&tree
->map_tree
, 0, (u64
)-1);
4848 remove_extent_mapping(&tree
->map_tree
, em
);
4849 write_unlock(&tree
->map_tree
.lock
);
4853 free_extent_map(em
);
4854 /* once for the tree */
4855 free_extent_map(em
);
4859 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
4861 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
4862 struct extent_map
*em
;
4863 struct map_lookup
*map
;
4864 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4867 read_lock(&em_tree
->lock
);
4868 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4869 read_unlock(&em_tree
->lock
);
4872 * We could return errors for these cases, but that could get ugly and
4873 * we'd probably do the same thing which is just not do anything else
4874 * and exit, so return 1 so the callers don't try to use other copies.
4877 btrfs_crit(fs_info
, "No mapping for %Lu-%Lu", logical
,
4882 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
4883 btrfs_crit(fs_info
, "Invalid mapping for %Lu-%Lu, got "
4884 "%Lu-%Lu", logical
, logical
+len
, em
->start
,
4885 em
->start
+ em
->len
);
4886 free_extent_map(em
);
4890 map
= (struct map_lookup
*)em
->bdev
;
4891 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1
))
4892 ret
= map
->num_stripes
;
4893 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
4894 ret
= map
->sub_stripes
;
4895 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
4897 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
4901 free_extent_map(em
);
4903 btrfs_dev_replace_lock(&fs_info
->dev_replace
);
4904 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
))
4906 btrfs_dev_replace_unlock(&fs_info
->dev_replace
);
4911 unsigned long btrfs_full_stripe_len(struct btrfs_root
*root
,
4912 struct btrfs_mapping_tree
*map_tree
,
4915 struct extent_map
*em
;
4916 struct map_lookup
*map
;
4917 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4918 unsigned long len
= root
->sectorsize
;
4920 read_lock(&em_tree
->lock
);
4921 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4922 read_unlock(&em_tree
->lock
);
4925 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4926 map
= (struct map_lookup
*)em
->bdev
;
4927 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
4928 len
= map
->stripe_len
* nr_data_stripes(map
);
4929 free_extent_map(em
);
4933 int btrfs_is_parity_mirror(struct btrfs_mapping_tree
*map_tree
,
4934 u64 logical
, u64 len
, int mirror_num
)
4936 struct extent_map
*em
;
4937 struct map_lookup
*map
;
4938 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
4941 read_lock(&em_tree
->lock
);
4942 em
= lookup_extent_mapping(em_tree
, logical
, len
);
4943 read_unlock(&em_tree
->lock
);
4946 BUG_ON(em
->start
> logical
|| em
->start
+ em
->len
< logical
);
4947 map
= (struct map_lookup
*)em
->bdev
;
4948 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
4950 free_extent_map(em
);
4954 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
4955 struct map_lookup
*map
, int first
, int num
,
4956 int optimal
, int dev_replace_is_ongoing
)
4960 struct btrfs_device
*srcdev
;
4962 if (dev_replace_is_ongoing
&&
4963 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
4964 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
4965 srcdev
= fs_info
->dev_replace
.srcdev
;
4970 * try to avoid the drive that is the source drive for a
4971 * dev-replace procedure, only choose it if no other non-missing
4972 * mirror is available
4974 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
4975 if (map
->stripes
[optimal
].dev
->bdev
&&
4976 (tolerance
|| map
->stripes
[optimal
].dev
!= srcdev
))
4978 for (i
= first
; i
< first
+ num
; i
++) {
4979 if (map
->stripes
[i
].dev
->bdev
&&
4980 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
4985 /* we couldn't find one that doesn't fail. Just return something
4986 * and the io error handling code will clean up eventually
4991 static inline int parity_smaller(u64 a
, u64 b
)
4996 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4997 static void sort_parity_stripes(struct btrfs_bio
*bbio
, int num_stripes
)
4999 struct btrfs_bio_stripe s
;
5006 for (i
= 0; i
< num_stripes
- 1; i
++) {
5007 if (parity_smaller(bbio
->raid_map
[i
],
5008 bbio
->raid_map
[i
+1])) {
5009 s
= bbio
->stripes
[i
];
5010 l
= bbio
->raid_map
[i
];
5011 bbio
->stripes
[i
] = bbio
->stripes
[i
+1];
5012 bbio
->raid_map
[i
] = bbio
->raid_map
[i
+1];
5013 bbio
->stripes
[i
+1] = s
;
5014 bbio
->raid_map
[i
+1] = l
;
5022 static struct btrfs_bio
*alloc_btrfs_bio(int total_stripes
, int real_stripes
)
5024 struct btrfs_bio
*bbio
= kzalloc(
5025 /* the size of the btrfs_bio */
5026 sizeof(struct btrfs_bio
) +
5027 /* plus the variable array for the stripes */
5028 sizeof(struct btrfs_bio_stripe
) * (total_stripes
) +
5029 /* plus the variable array for the tgt dev */
5030 sizeof(int) * (real_stripes
) +
5032 * plus the raid_map, which includes both the tgt dev
5035 sizeof(u64
) * (total_stripes
),
5040 atomic_set(&bbio
->error
, 0);
5041 atomic_set(&bbio
->refs
, 1);
5046 void btrfs_get_bbio(struct btrfs_bio
*bbio
)
5048 WARN_ON(!atomic_read(&bbio
->refs
));
5049 atomic_inc(&bbio
->refs
);
5052 void btrfs_put_bbio(struct btrfs_bio
*bbio
)
5056 if (atomic_dec_and_test(&bbio
->refs
))
5060 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
5061 u64 logical
, u64
*length
,
5062 struct btrfs_bio
**bbio_ret
,
5063 int mirror_num
, int need_raid_map
)
5065 struct extent_map
*em
;
5066 struct map_lookup
*map
;
5067 struct btrfs_mapping_tree
*map_tree
= &fs_info
->mapping_tree
;
5068 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
5071 u64 stripe_end_offset
;
5081 int tgtdev_indexes
= 0;
5082 struct btrfs_bio
*bbio
= NULL
;
5083 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
5084 int dev_replace_is_ongoing
= 0;
5085 int num_alloc_stripes
;
5086 int patch_the_first_stripe_for_dev_replace
= 0;
5087 u64 physical_to_patch_in_first_stripe
= 0;
5088 u64 raid56_full_stripe_start
= (u64
)-1;
5090 read_lock(&em_tree
->lock
);
5091 em
= lookup_extent_mapping(em_tree
, logical
, *length
);
5092 read_unlock(&em_tree
->lock
);
5095 btrfs_crit(fs_info
, "unable to find logical %llu len %llu",
5100 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
5101 btrfs_crit(fs_info
, "found a bad mapping, wanted %Lu, "
5102 "found %Lu-%Lu", logical
, em
->start
,
5103 em
->start
+ em
->len
);
5104 free_extent_map(em
);
5108 map
= (struct map_lookup
*)em
->bdev
;
5109 offset
= logical
- em
->start
;
5111 stripe_len
= map
->stripe_len
;
5114 * stripe_nr counts the total number of stripes we have to stride
5115 * to get to this block
5117 stripe_nr
= div64_u64(stripe_nr
, stripe_len
);
5119 stripe_offset
= stripe_nr
* stripe_len
;
5120 BUG_ON(offset
< stripe_offset
);
5122 /* stripe_offset is the offset of this block in its stripe*/
5123 stripe_offset
= offset
- stripe_offset
;
5125 /* if we're here for raid56, we need to know the stripe aligned start */
5126 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5127 unsigned long full_stripe_len
= stripe_len
* nr_data_stripes(map
);
5128 raid56_full_stripe_start
= offset
;
5130 /* allow a write of a full stripe, but make sure we don't
5131 * allow straddling of stripes
5133 raid56_full_stripe_start
= div64_u64(raid56_full_stripe_start
,
5135 raid56_full_stripe_start
*= full_stripe_len
;
5138 if (rw
& REQ_DISCARD
) {
5139 /* we don't discard raid56 yet */
5140 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5144 *length
= min_t(u64
, em
->len
- offset
, *length
);
5145 } else if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
5147 /* For writes to RAID[56], allow a full stripeset across all disks.
5148 For other RAID types and for RAID[56] reads, just allow a single
5149 stripe (on a single disk). */
5150 if ((map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) &&
5152 max_len
= stripe_len
* nr_data_stripes(map
) -
5153 (offset
- raid56_full_stripe_start
);
5155 /* we limit the length of each bio to what fits in a stripe */
5156 max_len
= stripe_len
- stripe_offset
;
5158 *length
= min_t(u64
, em
->len
- offset
, max_len
);
5160 *length
= em
->len
- offset
;
5163 /* This is for when we're called from btrfs_merge_bio_hook() and all
5164 it cares about is the length */
5168 btrfs_dev_replace_lock(dev_replace
);
5169 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
5170 if (!dev_replace_is_ongoing
)
5171 btrfs_dev_replace_unlock(dev_replace
);
5173 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
5174 !(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) &&
5175 dev_replace
->tgtdev
!= NULL
) {
5177 * in dev-replace case, for repair case (that's the only
5178 * case where the mirror is selected explicitly when
5179 * calling btrfs_map_block), blocks left of the left cursor
5180 * can also be read from the target drive.
5181 * For REQ_GET_READ_MIRRORS, the target drive is added as
5182 * the last one to the array of stripes. For READ, it also
5183 * needs to be supported using the same mirror number.
5184 * If the requested block is not left of the left cursor,
5185 * EIO is returned. This can happen because btrfs_num_copies()
5186 * returns one more in the dev-replace case.
5188 u64 tmp_length
= *length
;
5189 struct btrfs_bio
*tmp_bbio
= NULL
;
5190 int tmp_num_stripes
;
5191 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5192 int index_srcdev
= 0;
5194 u64 physical_of_found
= 0;
5196 ret
= __btrfs_map_block(fs_info
, REQ_GET_READ_MIRRORS
,
5197 logical
, &tmp_length
, &tmp_bbio
, 0, 0);
5199 WARN_ON(tmp_bbio
!= NULL
);
5203 tmp_num_stripes
= tmp_bbio
->num_stripes
;
5204 if (mirror_num
> tmp_num_stripes
) {
5206 * REQ_GET_READ_MIRRORS does not contain this
5207 * mirror, that means that the requested area
5208 * is not left of the left cursor
5211 btrfs_put_bbio(tmp_bbio
);
5216 * process the rest of the function using the mirror_num
5217 * of the source drive. Therefore look it up first.
5218 * At the end, patch the device pointer to the one of the
5221 for (i
= 0; i
< tmp_num_stripes
; i
++) {
5222 if (tmp_bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5224 * In case of DUP, in order to keep it
5225 * simple, only add the mirror with the
5226 * lowest physical address
5229 physical_of_found
<=
5230 tmp_bbio
->stripes
[i
].physical
)
5235 tmp_bbio
->stripes
[i
].physical
;
5240 mirror_num
= index_srcdev
+ 1;
5241 patch_the_first_stripe_for_dev_replace
= 1;
5242 physical_to_patch_in_first_stripe
= physical_of_found
;
5246 btrfs_put_bbio(tmp_bbio
);
5250 btrfs_put_bbio(tmp_bbio
);
5251 } else if (mirror_num
> map
->num_stripes
) {
5257 stripe_nr_orig
= stripe_nr
;
5258 stripe_nr_end
= ALIGN(offset
+ *length
, map
->stripe_len
);
5259 stripe_nr_end
= div_u64(stripe_nr_end
, map
->stripe_len
);
5260 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
5263 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5264 if (rw
& REQ_DISCARD
)
5265 num_stripes
= min_t(u64
, map
->num_stripes
,
5266 stripe_nr_end
- stripe_nr_orig
);
5267 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5269 if (!(rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)))
5271 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1
) {
5272 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
))
5273 num_stripes
= map
->num_stripes
;
5274 else if (mirror_num
)
5275 stripe_index
= mirror_num
- 1;
5277 stripe_index
= find_live_mirror(fs_info
, map
, 0,
5279 current
->pid
% map
->num_stripes
,
5280 dev_replace_is_ongoing
);
5281 mirror_num
= stripe_index
+ 1;
5284 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
5285 if (rw
& (REQ_WRITE
| REQ_DISCARD
| REQ_GET_READ_MIRRORS
)) {
5286 num_stripes
= map
->num_stripes
;
5287 } else if (mirror_num
) {
5288 stripe_index
= mirror_num
- 1;
5293 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5294 u32 factor
= map
->num_stripes
/ map
->sub_stripes
;
5296 stripe_nr
= div_u64_rem(stripe_nr
, factor
, &stripe_index
);
5297 stripe_index
*= map
->sub_stripes
;
5299 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
5300 num_stripes
= map
->sub_stripes
;
5301 else if (rw
& REQ_DISCARD
)
5302 num_stripes
= min_t(u64
, map
->sub_stripes
*
5303 (stripe_nr_end
- stripe_nr_orig
),
5305 else if (mirror_num
)
5306 stripe_index
+= mirror_num
- 1;
5308 int old_stripe_index
= stripe_index
;
5309 stripe_index
= find_live_mirror(fs_info
, map
,
5311 map
->sub_stripes
, stripe_index
+
5312 current
->pid
% map
->sub_stripes
,
5313 dev_replace_is_ongoing
);
5314 mirror_num
= stripe_index
- old_stripe_index
+ 1;
5317 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5318 if (need_raid_map
&&
5319 ((rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
)) ||
5321 /* push stripe_nr back to the start of the full stripe */
5322 stripe_nr
= div_u64(raid56_full_stripe_start
,
5323 stripe_len
* nr_data_stripes(map
));
5325 /* RAID[56] write or recovery. Return all stripes */
5326 num_stripes
= map
->num_stripes
;
5327 max_errors
= nr_parity_stripes(map
);
5329 *length
= map
->stripe_len
;
5334 * Mirror #0 or #1 means the original data block.
5335 * Mirror #2 is RAID5 parity block.
5336 * Mirror #3 is RAID6 Q block.
5338 stripe_nr
= div_u64_rem(stripe_nr
,
5339 nr_data_stripes(map
), &stripe_index
);
5341 stripe_index
= nr_data_stripes(map
) +
5344 /* We distribute the parity blocks across stripes */
5345 div_u64_rem(stripe_nr
+ stripe_index
, map
->num_stripes
,
5347 if (!(rw
& (REQ_WRITE
| REQ_DISCARD
|
5348 REQ_GET_READ_MIRRORS
)) && mirror_num
<= 1)
5353 * after this, stripe_nr is the number of stripes on this
5354 * device we have to walk to find the data, and stripe_index is
5355 * the number of our device in the stripe array
5357 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5359 mirror_num
= stripe_index
+ 1;
5361 BUG_ON(stripe_index
>= map
->num_stripes
);
5363 num_alloc_stripes
= num_stripes
;
5364 if (dev_replace_is_ongoing
) {
5365 if (rw
& (REQ_WRITE
| REQ_DISCARD
))
5366 num_alloc_stripes
<<= 1;
5367 if (rw
& REQ_GET_READ_MIRRORS
)
5368 num_alloc_stripes
++;
5369 tgtdev_indexes
= num_stripes
;
5372 bbio
= alloc_btrfs_bio(num_alloc_stripes
, tgtdev_indexes
);
5377 if (dev_replace_is_ongoing
)
5378 bbio
->tgtdev_map
= (int *)(bbio
->stripes
+ num_alloc_stripes
);
5380 /* build raid_map */
5381 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
&&
5382 need_raid_map
&& ((rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
)) ||
5387 bbio
->raid_map
= (u64
*)((void *)bbio
->stripes
+
5388 sizeof(struct btrfs_bio_stripe
) *
5390 sizeof(int) * tgtdev_indexes
);
5392 /* Work out the disk rotation on this stripe-set */
5393 div_u64_rem(stripe_nr
, num_stripes
, &rot
);
5395 /* Fill in the logical address of each stripe */
5396 tmp
= stripe_nr
* nr_data_stripes(map
);
5397 for (i
= 0; i
< nr_data_stripes(map
); i
++)
5398 bbio
->raid_map
[(i
+rot
) % num_stripes
] =
5399 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
5401 bbio
->raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
5402 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5403 bbio
->raid_map
[(i
+rot
+1) % num_stripes
] =
5407 if (rw
& REQ_DISCARD
) {
5409 u32 sub_stripes
= 0;
5410 u64 stripes_per_dev
= 0;
5411 u32 remaining_stripes
= 0;
5412 u32 last_stripe
= 0;
5415 (BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID10
)) {
5416 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5419 sub_stripes
= map
->sub_stripes
;
5421 factor
= map
->num_stripes
/ sub_stripes
;
5422 stripes_per_dev
= div_u64_rem(stripe_nr_end
-
5425 &remaining_stripes
);
5426 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
5427 last_stripe
*= sub_stripes
;
5430 for (i
= 0; i
< num_stripes
; i
++) {
5431 bbio
->stripes
[i
].physical
=
5432 map
->stripes
[stripe_index
].physical
+
5433 stripe_offset
+ stripe_nr
* map
->stripe_len
;
5434 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
5436 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5437 BTRFS_BLOCK_GROUP_RAID10
)) {
5438 bbio
->stripes
[i
].length
= stripes_per_dev
*
5441 if (i
/ sub_stripes
< remaining_stripes
)
5442 bbio
->stripes
[i
].length
+=
5446 * Special for the first stripe and
5449 * |-------|...|-------|
5453 if (i
< sub_stripes
)
5454 bbio
->stripes
[i
].length
-=
5457 if (stripe_index
>= last_stripe
&&
5458 stripe_index
<= (last_stripe
+
5460 bbio
->stripes
[i
].length
-=
5463 if (i
== sub_stripes
- 1)
5466 bbio
->stripes
[i
].length
= *length
;
5469 if (stripe_index
== map
->num_stripes
) {
5470 /* This could only happen for RAID0/10 */
5476 for (i
= 0; i
< num_stripes
; i
++) {
5477 bbio
->stripes
[i
].physical
=
5478 map
->stripes
[stripe_index
].physical
+
5480 stripe_nr
* map
->stripe_len
;
5481 bbio
->stripes
[i
].dev
=
5482 map
->stripes
[stripe_index
].dev
;
5487 if (rw
& (REQ_WRITE
| REQ_GET_READ_MIRRORS
))
5488 max_errors
= btrfs_chunk_max_errors(map
);
5491 sort_parity_stripes(bbio
, num_stripes
);
5494 if (dev_replace_is_ongoing
&& (rw
& (REQ_WRITE
| REQ_DISCARD
)) &&
5495 dev_replace
->tgtdev
!= NULL
) {
5496 int index_where_to_add
;
5497 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5500 * duplicate the write operations while the dev replace
5501 * procedure is running. Since the copying of the old disk
5502 * to the new disk takes place at run time while the
5503 * filesystem is mounted writable, the regular write
5504 * operations to the old disk have to be duplicated to go
5505 * to the new disk as well.
5506 * Note that device->missing is handled by the caller, and
5507 * that the write to the old disk is already set up in the
5510 index_where_to_add
= num_stripes
;
5511 for (i
= 0; i
< num_stripes
; i
++) {
5512 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5513 /* write to new disk, too */
5514 struct btrfs_bio_stripe
*new =
5515 bbio
->stripes
+ index_where_to_add
;
5516 struct btrfs_bio_stripe
*old
=
5519 new->physical
= old
->physical
;
5520 new->length
= old
->length
;
5521 new->dev
= dev_replace
->tgtdev
;
5522 bbio
->tgtdev_map
[i
] = index_where_to_add
;
5523 index_where_to_add
++;
5528 num_stripes
= index_where_to_add
;
5529 } else if (dev_replace_is_ongoing
&& (rw
& REQ_GET_READ_MIRRORS
) &&
5530 dev_replace
->tgtdev
!= NULL
) {
5531 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5532 int index_srcdev
= 0;
5534 u64 physical_of_found
= 0;
5537 * During the dev-replace procedure, the target drive can
5538 * also be used to read data in case it is needed to repair
5539 * a corrupt block elsewhere. This is possible if the
5540 * requested area is left of the left cursor. In this area,
5541 * the target drive is a full copy of the source drive.
5543 for (i
= 0; i
< num_stripes
; i
++) {
5544 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5546 * In case of DUP, in order to keep it
5547 * simple, only add the mirror with the
5548 * lowest physical address
5551 physical_of_found
<=
5552 bbio
->stripes
[i
].physical
)
5556 physical_of_found
= bbio
->stripes
[i
].physical
;
5560 if (physical_of_found
+ map
->stripe_len
<=
5561 dev_replace
->cursor_left
) {
5562 struct btrfs_bio_stripe
*tgtdev_stripe
=
5563 bbio
->stripes
+ num_stripes
;
5565 tgtdev_stripe
->physical
= physical_of_found
;
5566 tgtdev_stripe
->length
=
5567 bbio
->stripes
[index_srcdev
].length
;
5568 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
5569 bbio
->tgtdev_map
[index_srcdev
] = num_stripes
;
5578 bbio
->map_type
= map
->type
;
5579 bbio
->num_stripes
= num_stripes
;
5580 bbio
->max_errors
= max_errors
;
5581 bbio
->mirror_num
= mirror_num
;
5582 bbio
->num_tgtdevs
= tgtdev_indexes
;
5585 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5586 * mirror_num == num_stripes + 1 && dev_replace target drive is
5587 * available as a mirror
5589 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
5590 WARN_ON(num_stripes
> 1);
5591 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
5592 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
5593 bbio
->mirror_num
= map
->num_stripes
+ 1;
5596 if (dev_replace_is_ongoing
)
5597 btrfs_dev_replace_unlock(dev_replace
);
5598 free_extent_map(em
);
5602 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, int rw
,
5603 u64 logical
, u64
*length
,
5604 struct btrfs_bio
**bbio_ret
, int mirror_num
)
5606 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
5610 /* For Scrub/replace */
5611 int btrfs_map_sblock(struct btrfs_fs_info
*fs_info
, int rw
,
5612 u64 logical
, u64
*length
,
5613 struct btrfs_bio
**bbio_ret
, int mirror_num
,
5616 return __btrfs_map_block(fs_info
, rw
, logical
, length
, bbio_ret
,
5617 mirror_num
, need_raid_map
);
5620 int btrfs_rmap_block(struct btrfs_mapping_tree
*map_tree
,
5621 u64 chunk_start
, u64 physical
, u64 devid
,
5622 u64
**logical
, int *naddrs
, int *stripe_len
)
5624 struct extent_map_tree
*em_tree
= &map_tree
->map_tree
;
5625 struct extent_map
*em
;
5626 struct map_lookup
*map
;
5634 read_lock(&em_tree
->lock
);
5635 em
= lookup_extent_mapping(em_tree
, chunk_start
, 1);
5636 read_unlock(&em_tree
->lock
);
5639 printk(KERN_ERR
"BTRFS: couldn't find em for chunk %Lu\n",
5644 if (em
->start
!= chunk_start
) {
5645 printk(KERN_ERR
"BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
5646 em
->start
, chunk_start
);
5647 free_extent_map(em
);
5650 map
= (struct map_lookup
*)em
->bdev
;
5653 rmap_len
= map
->stripe_len
;
5655 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5656 length
= div_u64(length
, map
->num_stripes
/ map
->sub_stripes
);
5657 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5658 length
= div_u64(length
, map
->num_stripes
);
5659 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5660 length
= div_u64(length
, nr_data_stripes(map
));
5661 rmap_len
= map
->stripe_len
* nr_data_stripes(map
);
5664 buf
= kcalloc(map
->num_stripes
, sizeof(u64
), GFP_NOFS
);
5665 BUG_ON(!buf
); /* -ENOMEM */
5667 for (i
= 0; i
< map
->num_stripes
; i
++) {
5668 if (devid
&& map
->stripes
[i
].dev
->devid
!= devid
)
5670 if (map
->stripes
[i
].physical
> physical
||
5671 map
->stripes
[i
].physical
+ length
<= physical
)
5674 stripe_nr
= physical
- map
->stripes
[i
].physical
;
5675 stripe_nr
= div_u64(stripe_nr
, map
->stripe_len
);
5677 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
5678 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5679 stripe_nr
= div_u64(stripe_nr
, map
->sub_stripes
);
5680 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
5681 stripe_nr
= stripe_nr
* map
->num_stripes
+ i
;
5682 } /* else if RAID[56], multiply by nr_data_stripes().
5683 * Alternatively, just use rmap_len below instead of
5684 * map->stripe_len */
5686 bytenr
= chunk_start
+ stripe_nr
* rmap_len
;
5687 WARN_ON(nr
>= map
->num_stripes
);
5688 for (j
= 0; j
< nr
; j
++) {
5689 if (buf
[j
] == bytenr
)
5693 WARN_ON(nr
>= map
->num_stripes
);
5700 *stripe_len
= rmap_len
;
5702 free_extent_map(em
);
5706 static inline void btrfs_end_bbio(struct btrfs_bio
*bbio
, struct bio
*bio
, int err
)
5708 bio
->bi_private
= bbio
->private;
5709 bio
->bi_end_io
= bbio
->end_io
;
5710 bio_endio(bio
, err
);
5712 btrfs_put_bbio(bbio
);
5715 static void btrfs_end_bio(struct bio
*bio
, int err
)
5717 struct btrfs_bio
*bbio
= bio
->bi_private
;
5718 struct btrfs_device
*dev
= bbio
->stripes
[0].dev
;
5719 int is_orig_bio
= 0;
5722 atomic_inc(&bbio
->error
);
5723 if (err
== -EIO
|| err
== -EREMOTEIO
) {
5724 unsigned int stripe_index
=
5725 btrfs_io_bio(bio
)->stripe_index
;
5727 BUG_ON(stripe_index
>= bbio
->num_stripes
);
5728 dev
= bbio
->stripes
[stripe_index
].dev
;
5730 if (bio
->bi_rw
& WRITE
)
5731 btrfs_dev_stat_inc(dev
,
5732 BTRFS_DEV_STAT_WRITE_ERRS
);
5734 btrfs_dev_stat_inc(dev
,
5735 BTRFS_DEV_STAT_READ_ERRS
);
5736 if ((bio
->bi_rw
& WRITE_FLUSH
) == WRITE_FLUSH
)
5737 btrfs_dev_stat_inc(dev
,
5738 BTRFS_DEV_STAT_FLUSH_ERRS
);
5739 btrfs_dev_stat_print_on_error(dev
);
5744 if (bio
== bbio
->orig_bio
)
5747 btrfs_bio_counter_dec(bbio
->fs_info
);
5749 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5752 bio
= bbio
->orig_bio
;
5755 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5756 /* only send an error to the higher layers if it is
5757 * beyond the tolerance of the btrfs bio
5759 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
5763 * this bio is actually up to date, we didn't
5764 * go over the max number of errors
5766 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
5770 btrfs_end_bbio(bbio
, bio
, err
);
5771 } else if (!is_orig_bio
) {
5777 * see run_scheduled_bios for a description of why bios are collected for
5780 * This will add one bio to the pending list for a device and make sure
5781 * the work struct is scheduled.
5783 static noinline
void btrfs_schedule_bio(struct btrfs_root
*root
,
5784 struct btrfs_device
*device
,
5785 int rw
, struct bio
*bio
)
5787 int should_queue
= 1;
5788 struct btrfs_pending_bios
*pending_bios
;
5790 if (device
->missing
|| !device
->bdev
) {
5791 bio_endio(bio
, -EIO
);
5795 /* don't bother with additional async steps for reads, right now */
5796 if (!(rw
& REQ_WRITE
)) {
5798 btrfsic_submit_bio(rw
, bio
);
5804 * nr_async_bios allows us to reliably return congestion to the
5805 * higher layers. Otherwise, the async bio makes it appear we have
5806 * made progress against dirty pages when we've really just put it
5807 * on a queue for later
5809 atomic_inc(&root
->fs_info
->nr_async_bios
);
5810 WARN_ON(bio
->bi_next
);
5811 bio
->bi_next
= NULL
;
5814 spin_lock(&device
->io_lock
);
5815 if (bio
->bi_rw
& REQ_SYNC
)
5816 pending_bios
= &device
->pending_sync_bios
;
5818 pending_bios
= &device
->pending_bios
;
5820 if (pending_bios
->tail
)
5821 pending_bios
->tail
->bi_next
= bio
;
5823 pending_bios
->tail
= bio
;
5824 if (!pending_bios
->head
)
5825 pending_bios
->head
= bio
;
5826 if (device
->running_pending
)
5829 spin_unlock(&device
->io_lock
);
5832 btrfs_queue_work(root
->fs_info
->submit_workers
,
5836 static int bio_size_ok(struct block_device
*bdev
, struct bio
*bio
,
5839 struct bio_vec
*prev
;
5840 struct request_queue
*q
= bdev_get_queue(bdev
);
5841 unsigned int max_sectors
= queue_max_sectors(q
);
5842 struct bvec_merge_data bvm
= {
5844 .bi_sector
= sector
,
5845 .bi_rw
= bio
->bi_rw
,
5848 if (WARN_ON(bio
->bi_vcnt
== 0))
5851 prev
= &bio
->bi_io_vec
[bio
->bi_vcnt
- 1];
5852 if (bio_sectors(bio
) > max_sectors
)
5855 if (!q
->merge_bvec_fn
)
5858 bvm
.bi_size
= bio
->bi_iter
.bi_size
- prev
->bv_len
;
5859 if (q
->merge_bvec_fn(q
, &bvm
, prev
) < prev
->bv_len
)
5864 static void submit_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5865 struct bio
*bio
, u64 physical
, int dev_nr
,
5868 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
5870 bio
->bi_private
= bbio
;
5871 btrfs_io_bio(bio
)->stripe_index
= dev_nr
;
5872 bio
->bi_end_io
= btrfs_end_bio
;
5873 bio
->bi_iter
.bi_sector
= physical
>> 9;
5876 struct rcu_string
*name
;
5879 name
= rcu_dereference(dev
->name
);
5880 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5881 "(%s id %llu), size=%u\n", rw
,
5882 (u64
)bio
->bi_iter
.bi_sector
, (u_long
)dev
->bdev
->bd_dev
,
5883 name
->str
, dev
->devid
, bio
->bi_iter
.bi_size
);
5887 bio
->bi_bdev
= dev
->bdev
;
5889 btrfs_bio_counter_inc_noblocked(root
->fs_info
);
5892 btrfs_schedule_bio(root
, dev
, rw
, bio
);
5894 btrfsic_submit_bio(rw
, bio
);
5897 static int breakup_stripe_bio(struct btrfs_root
*root
, struct btrfs_bio
*bbio
,
5898 struct bio
*first_bio
, struct btrfs_device
*dev
,
5899 int dev_nr
, int rw
, int async
)
5901 struct bio_vec
*bvec
= first_bio
->bi_io_vec
;
5903 int nr_vecs
= bio_get_nr_vecs(dev
->bdev
);
5904 u64 physical
= bbio
->stripes
[dev_nr
].physical
;
5907 bio
= btrfs_bio_alloc(dev
->bdev
, physical
>> 9, nr_vecs
, GFP_NOFS
);
5911 while (bvec
<= (first_bio
->bi_io_vec
+ first_bio
->bi_vcnt
- 1)) {
5912 if (bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
5913 bvec
->bv_offset
) < bvec
->bv_len
) {
5914 u64 len
= bio
->bi_iter
.bi_size
;
5916 atomic_inc(&bbio
->stripes_pending
);
5917 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
,
5925 submit_stripe_bio(root
, bbio
, bio
, physical
, dev_nr
, rw
, async
);
5929 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
5931 atomic_inc(&bbio
->error
);
5932 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
5933 /* Shoud be the original bio. */
5934 WARN_ON(bio
!= bbio
->orig_bio
);
5936 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
5937 bio
->bi_iter
.bi_sector
= logical
>> 9;
5939 btrfs_end_bbio(bbio
, bio
, -EIO
);
5943 int btrfs_map_bio(struct btrfs_root
*root
, int rw
, struct bio
*bio
,
5944 int mirror_num
, int async_submit
)
5946 struct btrfs_device
*dev
;
5947 struct bio
*first_bio
= bio
;
5948 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
5954 struct btrfs_bio
*bbio
= NULL
;
5956 length
= bio
->bi_iter
.bi_size
;
5957 map_length
= length
;
5959 btrfs_bio_counter_inc_blocked(root
->fs_info
);
5960 ret
= __btrfs_map_block(root
->fs_info
, rw
, logical
, &map_length
, &bbio
,
5963 btrfs_bio_counter_dec(root
->fs_info
);
5967 total_devs
= bbio
->num_stripes
;
5968 bbio
->orig_bio
= first_bio
;
5969 bbio
->private = first_bio
->bi_private
;
5970 bbio
->end_io
= first_bio
->bi_end_io
;
5971 bbio
->fs_info
= root
->fs_info
;
5972 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
5974 if (bbio
->raid_map
) {
5975 /* In this case, map_length has been set to the length of
5976 a single stripe; not the whole write */
5978 ret
= raid56_parity_write(root
, bio
, bbio
, map_length
);
5980 ret
= raid56_parity_recover(root
, bio
, bbio
, map_length
,
5984 btrfs_bio_counter_dec(root
->fs_info
);
5988 if (map_length
< length
) {
5989 btrfs_crit(root
->fs_info
, "mapping failed logical %llu bio len %llu len %llu",
5990 logical
, length
, map_length
);
5994 for (dev_nr
= 0; dev_nr
< total_devs
; dev_nr
++) {
5995 dev
= bbio
->stripes
[dev_nr
].dev
;
5996 if (!dev
|| !dev
->bdev
|| (rw
& WRITE
&& !dev
->writeable
)) {
5997 bbio_error(bbio
, first_bio
, logical
);
6002 * Check and see if we're ok with this bio based on it's size
6003 * and offset with the given device.
6005 if (!bio_size_ok(dev
->bdev
, first_bio
,
6006 bbio
->stripes
[dev_nr
].physical
>> 9)) {
6007 ret
= breakup_stripe_bio(root
, bbio
, first_bio
, dev
,
6008 dev_nr
, rw
, async_submit
);
6013 if (dev_nr
< total_devs
- 1) {
6014 bio
= btrfs_bio_clone(first_bio
, GFP_NOFS
);
6015 BUG_ON(!bio
); /* -ENOMEM */
6019 submit_stripe_bio(root
, bbio
, bio
,
6020 bbio
->stripes
[dev_nr
].physical
, dev_nr
, rw
,
6023 btrfs_bio_counter_dec(root
->fs_info
);
6027 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_info
*fs_info
, u64 devid
,
6030 struct btrfs_device
*device
;
6031 struct btrfs_fs_devices
*cur_devices
;
6033 cur_devices
= fs_info
->fs_devices
;
6034 while (cur_devices
) {
6036 !memcmp(cur_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
)) {
6037 device
= __find_device(&cur_devices
->devices
,
6042 cur_devices
= cur_devices
->seed
;
6047 static struct btrfs_device
*add_missing_dev(struct btrfs_root
*root
,
6048 struct btrfs_fs_devices
*fs_devices
,
6049 u64 devid
, u8
*dev_uuid
)
6051 struct btrfs_device
*device
;
6053 device
= btrfs_alloc_device(NULL
, &devid
, dev_uuid
);
6057 list_add(&device
->dev_list
, &fs_devices
->devices
);
6058 device
->fs_devices
= fs_devices
;
6059 fs_devices
->num_devices
++;
6061 device
->missing
= 1;
6062 fs_devices
->missing_devices
++;
6068 * btrfs_alloc_device - allocate struct btrfs_device
6069 * @fs_info: used only for generating a new devid, can be NULL if
6070 * devid is provided (i.e. @devid != NULL).
6071 * @devid: a pointer to devid for this device. If NULL a new devid
6073 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6076 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6077 * on error. Returned struct is not linked onto any lists and can be
6078 * destroyed with kfree() right away.
6080 struct btrfs_device
*btrfs_alloc_device(struct btrfs_fs_info
*fs_info
,
6084 struct btrfs_device
*dev
;
6087 if (WARN_ON(!devid
&& !fs_info
))
6088 return ERR_PTR(-EINVAL
);
6090 dev
= __alloc_device();
6099 ret
= find_next_devid(fs_info
, &tmp
);
6102 return ERR_PTR(ret
);
6108 memcpy(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
);
6110 generate_random_uuid(dev
->uuid
);
6112 btrfs_init_work(&dev
->work
, btrfs_submit_helper
,
6113 pending_bios_fn
, NULL
, NULL
);
6118 static int read_one_chunk(struct btrfs_root
*root
, struct btrfs_key
*key
,
6119 struct extent_buffer
*leaf
,
6120 struct btrfs_chunk
*chunk
)
6122 struct btrfs_mapping_tree
*map_tree
= &root
->fs_info
->mapping_tree
;
6123 struct map_lookup
*map
;
6124 struct extent_map
*em
;
6128 u8 uuid
[BTRFS_UUID_SIZE
];
6133 logical
= key
->offset
;
6134 length
= btrfs_chunk_length(leaf
, chunk
);
6136 read_lock(&map_tree
->map_tree
.lock
);
6137 em
= lookup_extent_mapping(&map_tree
->map_tree
, logical
, 1);
6138 read_unlock(&map_tree
->map_tree
.lock
);
6140 /* already mapped? */
6141 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
6142 free_extent_map(em
);
6145 free_extent_map(em
);
6148 em
= alloc_extent_map();
6151 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
6152 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
6154 free_extent_map(em
);
6158 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
6159 em
->bdev
= (struct block_device
*)map
;
6160 em
->start
= logical
;
6163 em
->block_start
= 0;
6164 em
->block_len
= em
->len
;
6166 map
->num_stripes
= num_stripes
;
6167 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
6168 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
6169 map
->sector_size
= btrfs_chunk_sector_size(leaf
, chunk
);
6170 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
6171 map
->type
= btrfs_chunk_type(leaf
, chunk
);
6172 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
6173 for (i
= 0; i
< num_stripes
; i
++) {
6174 map
->stripes
[i
].physical
=
6175 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
6176 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
6177 read_extent_buffer(leaf
, uuid
, (unsigned long)
6178 btrfs_stripe_dev_uuid_nr(chunk
, i
),
6180 map
->stripes
[i
].dev
= btrfs_find_device(root
->fs_info
, devid
,
6182 if (!map
->stripes
[i
].dev
&& !btrfs_test_opt(root
, DEGRADED
)) {
6183 free_extent_map(em
);
6186 if (!map
->stripes
[i
].dev
) {
6187 map
->stripes
[i
].dev
=
6188 add_missing_dev(root
, root
->fs_info
->fs_devices
,
6190 if (!map
->stripes
[i
].dev
) {
6191 free_extent_map(em
);
6194 btrfs_warn(root
->fs_info
, "devid %llu uuid %pU is missing",
6197 map
->stripes
[i
].dev
->in_fs_metadata
= 1;
6200 write_lock(&map_tree
->map_tree
.lock
);
6201 ret
= add_extent_mapping(&map_tree
->map_tree
, em
, 0);
6202 write_unlock(&map_tree
->map_tree
.lock
);
6203 BUG_ON(ret
); /* Tree corruption */
6204 free_extent_map(em
);
6209 static void fill_device_from_item(struct extent_buffer
*leaf
,
6210 struct btrfs_dev_item
*dev_item
,
6211 struct btrfs_device
*device
)
6215 device
->devid
= btrfs_device_id(leaf
, dev_item
);
6216 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
6217 device
->total_bytes
= device
->disk_total_bytes
;
6218 device
->commit_total_bytes
= device
->disk_total_bytes
;
6219 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
6220 device
->commit_bytes_used
= device
->bytes_used
;
6221 device
->type
= btrfs_device_type(leaf
, dev_item
);
6222 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
6223 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
6224 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
6225 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
6226 device
->is_tgtdev_for_dev_replace
= 0;
6228 ptr
= btrfs_device_uuid(dev_item
);
6229 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
6232 static struct btrfs_fs_devices
*open_seed_devices(struct btrfs_root
*root
,
6235 struct btrfs_fs_devices
*fs_devices
;
6238 BUG_ON(!mutex_is_locked(&uuid_mutex
));
6240 fs_devices
= root
->fs_info
->fs_devices
->seed
;
6241 while (fs_devices
) {
6242 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_UUID_SIZE
))
6245 fs_devices
= fs_devices
->seed
;
6248 fs_devices
= find_fsid(fsid
);
6250 if (!btrfs_test_opt(root
, DEGRADED
))
6251 return ERR_PTR(-ENOENT
);
6253 fs_devices
= alloc_fs_devices(fsid
);
6254 if (IS_ERR(fs_devices
))
6257 fs_devices
->seeding
= 1;
6258 fs_devices
->opened
= 1;
6262 fs_devices
= clone_fs_devices(fs_devices
);
6263 if (IS_ERR(fs_devices
))
6266 ret
= __btrfs_open_devices(fs_devices
, FMODE_READ
,
6267 root
->fs_info
->bdev_holder
);
6269 free_fs_devices(fs_devices
);
6270 fs_devices
= ERR_PTR(ret
);
6274 if (!fs_devices
->seeding
) {
6275 __btrfs_close_devices(fs_devices
);
6276 free_fs_devices(fs_devices
);
6277 fs_devices
= ERR_PTR(-EINVAL
);
6281 fs_devices
->seed
= root
->fs_info
->fs_devices
->seed
;
6282 root
->fs_info
->fs_devices
->seed
= fs_devices
;
6287 static int read_one_dev(struct btrfs_root
*root
,
6288 struct extent_buffer
*leaf
,
6289 struct btrfs_dev_item
*dev_item
)
6291 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
6292 struct btrfs_device
*device
;
6295 u8 fs_uuid
[BTRFS_UUID_SIZE
];
6296 u8 dev_uuid
[BTRFS_UUID_SIZE
];
6298 devid
= btrfs_device_id(leaf
, dev_item
);
6299 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
6301 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
6304 if (memcmp(fs_uuid
, root
->fs_info
->fsid
, BTRFS_UUID_SIZE
)) {
6305 fs_devices
= open_seed_devices(root
, fs_uuid
);
6306 if (IS_ERR(fs_devices
))
6307 return PTR_ERR(fs_devices
);
6310 device
= btrfs_find_device(root
->fs_info
, devid
, dev_uuid
, fs_uuid
);
6312 if (!btrfs_test_opt(root
, DEGRADED
))
6315 device
= add_missing_dev(root
, fs_devices
, devid
, dev_uuid
);
6318 btrfs_warn(root
->fs_info
, "devid %llu uuid %pU missing",
6321 if (!device
->bdev
&& !btrfs_test_opt(root
, DEGRADED
))
6324 if(!device
->bdev
&& !device
->missing
) {
6326 * this happens when a device that was properly setup
6327 * in the device info lists suddenly goes bad.
6328 * device->bdev is NULL, and so we have to set
6329 * device->missing to one here
6331 device
->fs_devices
->missing_devices
++;
6332 device
->missing
= 1;
6335 /* Move the device to its own fs_devices */
6336 if (device
->fs_devices
!= fs_devices
) {
6337 ASSERT(device
->missing
);
6339 list_move(&device
->dev_list
, &fs_devices
->devices
);
6340 device
->fs_devices
->num_devices
--;
6341 fs_devices
->num_devices
++;
6343 device
->fs_devices
->missing_devices
--;
6344 fs_devices
->missing_devices
++;
6346 device
->fs_devices
= fs_devices
;
6350 if (device
->fs_devices
!= root
->fs_info
->fs_devices
) {
6351 BUG_ON(device
->writeable
);
6352 if (device
->generation
!=
6353 btrfs_device_generation(leaf
, dev_item
))
6357 fill_device_from_item(leaf
, dev_item
, device
);
6358 device
->in_fs_metadata
= 1;
6359 if (device
->writeable
&& !device
->is_tgtdev_for_dev_replace
) {
6360 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
6361 spin_lock(&root
->fs_info
->free_chunk_lock
);
6362 root
->fs_info
->free_chunk_space
+= device
->total_bytes
-
6364 spin_unlock(&root
->fs_info
->free_chunk_lock
);
6370 int btrfs_read_sys_array(struct btrfs_root
*root
)
6372 struct btrfs_super_block
*super_copy
= root
->fs_info
->super_copy
;
6373 struct extent_buffer
*sb
;
6374 struct btrfs_disk_key
*disk_key
;
6375 struct btrfs_chunk
*chunk
;
6377 unsigned long sb_array_offset
;
6383 struct btrfs_key key
;
6385 ASSERT(BTRFS_SUPER_INFO_SIZE
<= root
->nodesize
);
6387 * This will create extent buffer of nodesize, superblock size is
6388 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6389 * overallocate but we can keep it as-is, only the first page is used.
6391 sb
= btrfs_find_create_tree_block(root
, BTRFS_SUPER_INFO_OFFSET
);
6394 btrfs_set_buffer_uptodate(sb
);
6395 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
6397 * The sb extent buffer is artifical and just used to read the system array.
6398 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6399 * pages up-to-date when the page is larger: extent does not cover the
6400 * whole page and consequently check_page_uptodate does not find all
6401 * the page's extents up-to-date (the hole beyond sb),
6402 * write_extent_buffer then triggers a WARN_ON.
6404 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6405 * but sb spans only this function. Add an explicit SetPageUptodate call
6406 * to silence the warning eg. on PowerPC 64.
6408 if (PAGE_CACHE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
6409 SetPageUptodate(sb
->pages
[0]);
6411 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
6412 array_size
= btrfs_super_sys_array_size(super_copy
);
6414 array_ptr
= super_copy
->sys_chunk_array
;
6415 sb_array_offset
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
6418 while (cur_offset
< array_size
) {
6419 disk_key
= (struct btrfs_disk_key
*)array_ptr
;
6420 len
= sizeof(*disk_key
);
6421 if (cur_offset
+ len
> array_size
)
6422 goto out_short_read
;
6424 btrfs_disk_key_to_cpu(&key
, disk_key
);
6427 sb_array_offset
+= len
;
6430 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6431 chunk
= (struct btrfs_chunk
*)sb_array_offset
;
6433 * At least one btrfs_chunk with one stripe must be
6434 * present, exact stripe count check comes afterwards
6436 len
= btrfs_chunk_item_size(1);
6437 if (cur_offset
+ len
> array_size
)
6438 goto out_short_read
;
6440 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
6441 len
= btrfs_chunk_item_size(num_stripes
);
6442 if (cur_offset
+ len
> array_size
)
6443 goto out_short_read
;
6445 ret
= read_one_chunk(root
, &key
, sb
, chunk
);
6453 sb_array_offset
+= len
;
6456 free_extent_buffer(sb
);
6460 printk(KERN_ERR
"BTRFS: sys_array too short to read %u bytes at offset %u\n",
6462 free_extent_buffer(sb
);
6466 int btrfs_read_chunk_tree(struct btrfs_root
*root
)
6468 struct btrfs_path
*path
;
6469 struct extent_buffer
*leaf
;
6470 struct btrfs_key key
;
6471 struct btrfs_key found_key
;
6475 root
= root
->fs_info
->chunk_root
;
6477 path
= btrfs_alloc_path();
6481 mutex_lock(&uuid_mutex
);
6485 * Read all device items, and then all the chunk items. All
6486 * device items are found before any chunk item (their object id
6487 * is smaller than the lowest possible object id for a chunk
6488 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6490 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
6493 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6497 leaf
= path
->nodes
[0];
6498 slot
= path
->slots
[0];
6499 if (slot
>= btrfs_header_nritems(leaf
)) {
6500 ret
= btrfs_next_leaf(root
, path
);
6507 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
6508 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
6509 struct btrfs_dev_item
*dev_item
;
6510 dev_item
= btrfs_item_ptr(leaf
, slot
,
6511 struct btrfs_dev_item
);
6512 ret
= read_one_dev(root
, leaf
, dev_item
);
6515 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
6516 struct btrfs_chunk
*chunk
;
6517 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
6518 ret
= read_one_chunk(root
, &found_key
, leaf
, chunk
);
6526 unlock_chunks(root
);
6527 mutex_unlock(&uuid_mutex
);
6529 btrfs_free_path(path
);
6533 void btrfs_init_devices_late(struct btrfs_fs_info
*fs_info
)
6535 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6536 struct btrfs_device
*device
;
6538 while (fs_devices
) {
6539 mutex_lock(&fs_devices
->device_list_mutex
);
6540 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
6541 device
->dev_root
= fs_info
->dev_root
;
6542 mutex_unlock(&fs_devices
->device_list_mutex
);
6544 fs_devices
= fs_devices
->seed
;
6548 static void __btrfs_reset_dev_stats(struct btrfs_device
*dev
)
6552 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6553 btrfs_dev_stat_reset(dev
, i
);
6556 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
6558 struct btrfs_key key
;
6559 struct btrfs_key found_key
;
6560 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6561 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6562 struct extent_buffer
*eb
;
6565 struct btrfs_device
*device
;
6566 struct btrfs_path
*path
= NULL
;
6569 path
= btrfs_alloc_path();
6575 mutex_lock(&fs_devices
->device_list_mutex
);
6576 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6578 struct btrfs_dev_stats_item
*ptr
;
6581 key
.type
= BTRFS_DEV_STATS_KEY
;
6582 key
.offset
= device
->devid
;
6583 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
6585 __btrfs_reset_dev_stats(device
);
6586 device
->dev_stats_valid
= 1;
6587 btrfs_release_path(path
);
6590 slot
= path
->slots
[0];
6591 eb
= path
->nodes
[0];
6592 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
6593 item_size
= btrfs_item_size_nr(eb
, slot
);
6595 ptr
= btrfs_item_ptr(eb
, slot
,
6596 struct btrfs_dev_stats_item
);
6598 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6599 if (item_size
>= (1 + i
) * sizeof(__le64
))
6600 btrfs_dev_stat_set(device
, i
,
6601 btrfs_dev_stats_value(eb
, ptr
, i
));
6603 btrfs_dev_stat_reset(device
, i
);
6606 device
->dev_stats_valid
= 1;
6607 btrfs_dev_stat_print_on_load(device
);
6608 btrfs_release_path(path
);
6610 mutex_unlock(&fs_devices
->device_list_mutex
);
6613 btrfs_free_path(path
);
6614 return ret
< 0 ? ret
: 0;
6617 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
6618 struct btrfs_root
*dev_root
,
6619 struct btrfs_device
*device
)
6621 struct btrfs_path
*path
;
6622 struct btrfs_key key
;
6623 struct extent_buffer
*eb
;
6624 struct btrfs_dev_stats_item
*ptr
;
6629 key
.type
= BTRFS_DEV_STATS_KEY
;
6630 key
.offset
= device
->devid
;
6632 path
= btrfs_alloc_path();
6634 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
6636 printk_in_rcu(KERN_WARNING
"BTRFS: "
6637 "error %d while searching for dev_stats item for device %s!\n",
6638 ret
, rcu_str_deref(device
->name
));
6643 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
6644 /* need to delete old one and insert a new one */
6645 ret
= btrfs_del_item(trans
, dev_root
, path
);
6647 printk_in_rcu(KERN_WARNING
"BTRFS: "
6648 "delete too small dev_stats item for device %s failed %d!\n",
6649 rcu_str_deref(device
->name
), ret
);
6656 /* need to insert a new item */
6657 btrfs_release_path(path
);
6658 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
6659 &key
, sizeof(*ptr
));
6661 printk_in_rcu(KERN_WARNING
"BTRFS: "
6662 "insert dev_stats item for device %s failed %d!\n",
6663 rcu_str_deref(device
->name
), ret
);
6668 eb
= path
->nodes
[0];
6669 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
6670 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6671 btrfs_set_dev_stats_value(eb
, ptr
, i
,
6672 btrfs_dev_stat_read(device
, i
));
6673 btrfs_mark_buffer_dirty(eb
);
6676 btrfs_free_path(path
);
6681 * called from commit_transaction. Writes all changed device stats to disk.
6683 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
,
6684 struct btrfs_fs_info
*fs_info
)
6686 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
6687 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6688 struct btrfs_device
*device
;
6692 mutex_lock(&fs_devices
->device_list_mutex
);
6693 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6694 if (!device
->dev_stats_valid
|| !btrfs_dev_stats_dirty(device
))
6697 stats_cnt
= atomic_read(&device
->dev_stats_ccnt
);
6698 ret
= update_dev_stat_item(trans
, dev_root
, device
);
6700 atomic_sub(stats_cnt
, &device
->dev_stats_ccnt
);
6702 mutex_unlock(&fs_devices
->device_list_mutex
);
6707 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
6709 btrfs_dev_stat_inc(dev
, index
);
6710 btrfs_dev_stat_print_on_error(dev
);
6713 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
6715 if (!dev
->dev_stats_valid
)
6717 printk_ratelimited_in_rcu(KERN_ERR
"BTRFS: "
6718 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6719 rcu_str_deref(dev
->name
),
6720 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6721 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6722 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6723 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6724 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6727 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
6731 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6732 if (btrfs_dev_stat_read(dev
, i
) != 0)
6734 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
6735 return; /* all values == 0, suppress message */
6737 printk_in_rcu(KERN_INFO
"BTRFS: "
6738 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6739 rcu_str_deref(dev
->name
),
6740 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
6741 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
6742 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
6743 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
6744 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
6747 int btrfs_get_dev_stats(struct btrfs_root
*root
,
6748 struct btrfs_ioctl_get_dev_stats
*stats
)
6750 struct btrfs_device
*dev
;
6751 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
6754 mutex_lock(&fs_devices
->device_list_mutex
);
6755 dev
= btrfs_find_device(root
->fs_info
, stats
->devid
, NULL
, NULL
);
6756 mutex_unlock(&fs_devices
->device_list_mutex
);
6759 btrfs_warn(root
->fs_info
, "get dev_stats failed, device not found");
6761 } else if (!dev
->dev_stats_valid
) {
6762 btrfs_warn(root
->fs_info
, "get dev_stats failed, not yet valid");
6764 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
6765 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
6766 if (stats
->nr_items
> i
)
6768 btrfs_dev_stat_read_and_reset(dev
, i
);
6770 btrfs_dev_stat_reset(dev
, i
);
6773 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
6774 if (stats
->nr_items
> i
)
6775 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
6777 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
6778 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
6782 int btrfs_scratch_superblock(struct btrfs_device
*device
)
6784 struct buffer_head
*bh
;
6785 struct btrfs_super_block
*disk_super
;
6787 bh
= btrfs_read_dev_super(device
->bdev
);
6790 disk_super
= (struct btrfs_super_block
*)bh
->b_data
;
6792 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
6793 set_buffer_dirty(bh
);
6794 sync_dirty_buffer(bh
);
6801 * Update the size of all devices, which is used for writing out the
6804 void btrfs_update_commit_device_size(struct btrfs_fs_info
*fs_info
)
6806 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6807 struct btrfs_device
*curr
, *next
;
6809 if (list_empty(&fs_devices
->resized_devices
))
6812 mutex_lock(&fs_devices
->device_list_mutex
);
6813 lock_chunks(fs_info
->dev_root
);
6814 list_for_each_entry_safe(curr
, next
, &fs_devices
->resized_devices
,
6816 list_del_init(&curr
->resized_list
);
6817 curr
->commit_total_bytes
= curr
->disk_total_bytes
;
6819 unlock_chunks(fs_info
->dev_root
);
6820 mutex_unlock(&fs_devices
->device_list_mutex
);
6823 /* Must be invoked during the transaction commit */
6824 void btrfs_update_commit_device_bytes_used(struct btrfs_root
*root
,
6825 struct btrfs_transaction
*transaction
)
6827 struct extent_map
*em
;
6828 struct map_lookup
*map
;
6829 struct btrfs_device
*dev
;
6832 if (list_empty(&transaction
->pending_chunks
))
6835 /* In order to kick the device replace finish process */
6837 list_for_each_entry(em
, &transaction
->pending_chunks
, list
) {
6838 map
= (struct map_lookup
*)em
->bdev
;
6840 for (i
= 0; i
< map
->num_stripes
; i
++) {
6841 dev
= map
->stripes
[i
].dev
;
6842 dev
->commit_bytes_used
= dev
->bytes_used
;
6845 unlock_chunks(root
);
6848 void btrfs_set_fs_info_ptr(struct btrfs_fs_info
*fs_info
)
6850 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6851 while (fs_devices
) {
6852 fs_devices
->fs_info
= fs_info
;
6853 fs_devices
= fs_devices
->seed
;
6857 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info
*fs_info
)
6859 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6860 while (fs_devices
) {
6861 fs_devices
->fs_info
= NULL
;
6862 fs_devices
= fs_devices
->seed
;