1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 STRATO. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/pagemap.h>
8 #include <linux/writeback.h>
9 #include <linux/blkdev.h>
10 #include <linux/slab.h>
11 #include <linux/workqueue.h>
15 #include "transaction.h"
16 #include "dev-replace.h"
21 * This is the implementation for the generic read ahead framework.
23 * To trigger a readahead, btrfs_reada_add must be called. It will start
24 * a read ahead for the given range [start, end) on tree root. The returned
25 * handle can either be used to wait on the readahead to finish
26 * (btrfs_reada_wait), or to send it to the background (btrfs_reada_detach).
28 * The read ahead works as follows:
29 * On btrfs_reada_add, the root of the tree is inserted into a radix_tree.
30 * reada_start_machine will then search for extents to prefetch and trigger
31 * some reads. When a read finishes for a node, all contained node/leaf
32 * pointers that lie in the given range will also be enqueued. The reads will
33 * be triggered in sequential order, thus giving a big win over a naive
34 * enumeration. It will also make use of multi-device layouts. Each disk
35 * will have its on read pointer and all disks will by utilized in parallel.
36 * Also will no two disks read both sides of a mirror simultaneously, as this
37 * would waste seeking capacity. Instead both disks will read different parts
39 * Any number of readaheads can be started in parallel. The read order will be
40 * determined globally, i.e. 2 parallel readaheads will normally finish faster
41 * than the 2 started one after another.
44 #define MAX_IN_FLIGHT 6
47 struct list_head list
;
48 struct reada_control
*rc
;
55 struct list_head extctl
;
58 struct reada_zone
*zones
[BTRFS_MAX_MIRRORS
];
67 struct list_head list
;
70 struct btrfs_device
*device
;
71 struct btrfs_device
*devs
[BTRFS_MAX_MIRRORS
]; /* full list, incl
77 struct reada_machine_work
{
78 struct btrfs_work work
;
79 struct btrfs_fs_info
*fs_info
;
82 static void reada_extent_put(struct btrfs_fs_info
*, struct reada_extent
*);
83 static void reada_control_release(struct kref
*kref
);
84 static void reada_zone_release(struct kref
*kref
);
85 static void reada_start_machine(struct btrfs_fs_info
*fs_info
);
86 static void __reada_start_machine(struct btrfs_fs_info
*fs_info
);
88 static int reada_add_block(struct reada_control
*rc
, u64 logical
,
89 struct btrfs_key
*top
, u64 generation
);
92 /* in case of err, eb might be NULL */
93 static void __readahead_hook(struct btrfs_fs_info
*fs_info
,
94 struct reada_extent
*re
, struct extent_buffer
*eb
,
101 struct list_head list
;
103 spin_lock(&re
->lock
);
105 * just take the full list from the extent. afterwards we
106 * don't need the lock anymore
108 list_replace_init(&re
->extctl
, &list
);
110 spin_unlock(&re
->lock
);
113 * this is the error case, the extent buffer has not been
114 * read correctly. We won't access anything from it and
115 * just cleanup our data structures. Effectively this will
116 * cut the branch below this node from read ahead.
122 * FIXME: currently we just set nritems to 0 if this is a leaf,
123 * effectively ignoring the content. In a next step we could
124 * trigger more readahead depending from the content, e.g.
125 * fetch the checksums for the extents in the leaf.
127 if (!btrfs_header_level(eb
))
130 nritems
= btrfs_header_nritems(eb
);
131 generation
= btrfs_header_generation(eb
);
132 for (i
= 0; i
< nritems
; i
++) {
133 struct reada_extctl
*rec
;
135 struct btrfs_key key
;
136 struct btrfs_key next_key
;
138 btrfs_node_key_to_cpu(eb
, &key
, i
);
140 btrfs_node_key_to_cpu(eb
, &next_key
, i
+ 1);
143 bytenr
= btrfs_node_blockptr(eb
, i
);
144 n_gen
= btrfs_node_ptr_generation(eb
, i
);
146 list_for_each_entry(rec
, &list
, list
) {
147 struct reada_control
*rc
= rec
->rc
;
150 * if the generation doesn't match, just ignore this
151 * extctl. This will probably cut off a branch from
152 * prefetch. Alternatively one could start a new (sub-)
153 * prefetch for this branch, starting again from root.
154 * FIXME: move the generation check out of this loop
157 if (rec
->generation
!= generation
) {
159 "generation mismatch for (%llu,%d,%llu) %llu != %llu",
160 key
.objectid
, key
.type
, key
.offset
,
161 rec
->generation
, generation
);
164 if (rec
->generation
== generation
&&
165 btrfs_comp_cpu_keys(&key
, &rc
->key_end
) < 0 &&
166 btrfs_comp_cpu_keys(&next_key
, &rc
->key_start
) > 0)
167 reada_add_block(rc
, bytenr
, &next_key
, n_gen
);
173 * free extctl records
175 while (!list_empty(&list
)) {
176 struct reada_control
*rc
;
177 struct reada_extctl
*rec
;
179 rec
= list_first_entry(&list
, struct reada_extctl
, list
);
180 list_del(&rec
->list
);
184 kref_get(&rc
->refcnt
);
185 if (atomic_dec_and_test(&rc
->elems
)) {
186 kref_put(&rc
->refcnt
, reada_control_release
);
189 kref_put(&rc
->refcnt
, reada_control_release
);
191 reada_extent_put(fs_info
, re
); /* one ref for each entry */
197 int btree_readahead_hook(struct extent_buffer
*eb
, int err
)
199 struct btrfs_fs_info
*fs_info
= eb
->fs_info
;
201 struct reada_extent
*re
;
204 spin_lock(&fs_info
->reada_lock
);
205 re
= radix_tree_lookup(&fs_info
->reada_tree
,
206 eb
->start
>> PAGE_SHIFT
);
209 spin_unlock(&fs_info
->reada_lock
);
215 __readahead_hook(fs_info
, re
, eb
, err
);
216 reada_extent_put(fs_info
, re
); /* our ref */
219 reada_start_machine(fs_info
);
223 static struct reada_zone
*reada_find_zone(struct btrfs_device
*dev
, u64 logical
,
224 struct btrfs_bio
*bbio
)
226 struct btrfs_fs_info
*fs_info
= dev
->fs_info
;
228 struct reada_zone
*zone
;
229 struct btrfs_block_group_cache
*cache
= NULL
;
235 spin_lock(&fs_info
->reada_lock
);
236 ret
= radix_tree_gang_lookup(&dev
->reada_zones
, (void **)&zone
,
237 logical
>> PAGE_SHIFT
, 1);
238 if (ret
== 1 && logical
>= zone
->start
&& logical
<= zone
->end
) {
239 kref_get(&zone
->refcnt
);
240 spin_unlock(&fs_info
->reada_lock
);
244 spin_unlock(&fs_info
->reada_lock
);
246 cache
= btrfs_lookup_block_group(fs_info
, logical
);
250 start
= cache
->key
.objectid
;
251 end
= start
+ cache
->key
.offset
- 1;
252 btrfs_put_block_group(cache
);
254 zone
= kzalloc(sizeof(*zone
), GFP_KERNEL
);
258 ret
= radix_tree_preload(GFP_KERNEL
);
266 INIT_LIST_HEAD(&zone
->list
);
267 spin_lock_init(&zone
->lock
);
269 kref_init(&zone
->refcnt
);
271 zone
->device
= dev
; /* our device always sits at index 0 */
272 for (i
= 0; i
< bbio
->num_stripes
; ++i
) {
273 /* bounds have already been checked */
274 zone
->devs
[i
] = bbio
->stripes
[i
].dev
;
276 zone
->ndevs
= bbio
->num_stripes
;
278 spin_lock(&fs_info
->reada_lock
);
279 ret
= radix_tree_insert(&dev
->reada_zones
,
280 (unsigned long)(zone
->end
>> PAGE_SHIFT
),
283 if (ret
== -EEXIST
) {
285 ret
= radix_tree_gang_lookup(&dev
->reada_zones
, (void **)&zone
,
286 logical
>> PAGE_SHIFT
, 1);
287 if (ret
== 1 && logical
>= zone
->start
&& logical
<= zone
->end
)
288 kref_get(&zone
->refcnt
);
292 spin_unlock(&fs_info
->reada_lock
);
293 radix_tree_preload_end();
298 static struct reada_extent
*reada_find_extent(struct btrfs_fs_info
*fs_info
,
300 struct btrfs_key
*top
)
303 struct reada_extent
*re
= NULL
;
304 struct reada_extent
*re_exist
= NULL
;
305 struct btrfs_bio
*bbio
= NULL
;
306 struct btrfs_device
*dev
;
307 struct btrfs_device
*prev_dev
;
311 unsigned long index
= logical
>> PAGE_SHIFT
;
312 int dev_replace_is_ongoing
;
315 spin_lock(&fs_info
->reada_lock
);
316 re
= radix_tree_lookup(&fs_info
->reada_tree
, index
);
319 spin_unlock(&fs_info
->reada_lock
);
324 re
= kzalloc(sizeof(*re
), GFP_KERNEL
);
328 re
->logical
= logical
;
330 INIT_LIST_HEAD(&re
->extctl
);
331 spin_lock_init(&re
->lock
);
337 length
= fs_info
->nodesize
;
338 ret
= btrfs_map_block(fs_info
, BTRFS_MAP_GET_READ_MIRRORS
, logical
,
340 if (ret
|| !bbio
|| length
< fs_info
->nodesize
)
343 if (bbio
->num_stripes
> BTRFS_MAX_MIRRORS
) {
345 "readahead: more than %d copies not supported",
350 real_stripes
= bbio
->num_stripes
- bbio
->num_tgtdevs
;
351 for (nzones
= 0; nzones
< real_stripes
; ++nzones
) {
352 struct reada_zone
*zone
;
354 dev
= bbio
->stripes
[nzones
].dev
;
356 /* cannot read ahead on missing device. */
360 zone
= reada_find_zone(dev
, logical
, bbio
);
364 re
->zones
[re
->nzones
++] = zone
;
365 spin_lock(&zone
->lock
);
367 kref_get(&zone
->refcnt
);
369 spin_unlock(&zone
->lock
);
370 spin_lock(&fs_info
->reada_lock
);
371 kref_put(&zone
->refcnt
, reada_zone_release
);
372 spin_unlock(&fs_info
->reada_lock
);
374 if (re
->nzones
== 0) {
375 /* not a single zone found, error and out */
379 ret
= radix_tree_preload(GFP_KERNEL
);
383 /* insert extent in reada_tree + all per-device trees, all or nothing */
384 btrfs_dev_replace_read_lock(&fs_info
->dev_replace
);
385 spin_lock(&fs_info
->reada_lock
);
386 ret
= radix_tree_insert(&fs_info
->reada_tree
, index
, re
);
387 if (ret
== -EEXIST
) {
388 re_exist
= radix_tree_lookup(&fs_info
->reada_tree
, index
);
390 spin_unlock(&fs_info
->reada_lock
);
391 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
392 radix_tree_preload_end();
396 spin_unlock(&fs_info
->reada_lock
);
397 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
398 radix_tree_preload_end();
401 radix_tree_preload_end();
403 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(
404 &fs_info
->dev_replace
);
405 for (nzones
= 0; nzones
< re
->nzones
; ++nzones
) {
406 dev
= re
->zones
[nzones
]->device
;
408 if (dev
== prev_dev
) {
410 * in case of DUP, just add the first zone. As both
411 * are on the same device, there's nothing to gain
413 * Also, it wouldn't work, as the tree is per device
414 * and adding would fail with EEXIST
421 if (dev_replace_is_ongoing
&&
422 dev
== fs_info
->dev_replace
.tgtdev
) {
424 * as this device is selected for reading only as
425 * a last resort, skip it for read ahead.
430 ret
= radix_tree_insert(&dev
->reada_extents
, index
, re
);
432 while (--nzones
>= 0) {
433 dev
= re
->zones
[nzones
]->device
;
435 /* ignore whether the entry was inserted */
436 radix_tree_delete(&dev
->reada_extents
, index
);
438 radix_tree_delete(&fs_info
->reada_tree
, index
);
439 spin_unlock(&fs_info
->reada_lock
);
440 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
445 spin_unlock(&fs_info
->reada_lock
);
446 btrfs_dev_replace_read_unlock(&fs_info
->dev_replace
);
451 btrfs_put_bbio(bbio
);
455 for (nzones
= 0; nzones
< re
->nzones
; ++nzones
) {
456 struct reada_zone
*zone
;
458 zone
= re
->zones
[nzones
];
459 kref_get(&zone
->refcnt
);
460 spin_lock(&zone
->lock
);
462 if (zone
->elems
== 0) {
464 * no fs_info->reada_lock needed, as this can't be
467 kref_put(&zone
->refcnt
, reada_zone_release
);
469 spin_unlock(&zone
->lock
);
471 spin_lock(&fs_info
->reada_lock
);
472 kref_put(&zone
->refcnt
, reada_zone_release
);
473 spin_unlock(&fs_info
->reada_lock
);
475 btrfs_put_bbio(bbio
);
480 static void reada_extent_put(struct btrfs_fs_info
*fs_info
,
481 struct reada_extent
*re
)
484 unsigned long index
= re
->logical
>> PAGE_SHIFT
;
486 spin_lock(&fs_info
->reada_lock
);
488 spin_unlock(&fs_info
->reada_lock
);
492 radix_tree_delete(&fs_info
->reada_tree
, index
);
493 for (i
= 0; i
< re
->nzones
; ++i
) {
494 struct reada_zone
*zone
= re
->zones
[i
];
496 radix_tree_delete(&zone
->device
->reada_extents
, index
);
499 spin_unlock(&fs_info
->reada_lock
);
501 for (i
= 0; i
< re
->nzones
; ++i
) {
502 struct reada_zone
*zone
= re
->zones
[i
];
504 kref_get(&zone
->refcnt
);
505 spin_lock(&zone
->lock
);
507 if (zone
->elems
== 0) {
508 /* no fs_info->reada_lock needed, as this can't be
510 kref_put(&zone
->refcnt
, reada_zone_release
);
512 spin_unlock(&zone
->lock
);
514 spin_lock(&fs_info
->reada_lock
);
515 kref_put(&zone
->refcnt
, reada_zone_release
);
516 spin_unlock(&fs_info
->reada_lock
);
522 static void reada_zone_release(struct kref
*kref
)
524 struct reada_zone
*zone
= container_of(kref
, struct reada_zone
, refcnt
);
526 radix_tree_delete(&zone
->device
->reada_zones
,
527 zone
->end
>> PAGE_SHIFT
);
532 static void reada_control_release(struct kref
*kref
)
534 struct reada_control
*rc
= container_of(kref
, struct reada_control
,
540 static int reada_add_block(struct reada_control
*rc
, u64 logical
,
541 struct btrfs_key
*top
, u64 generation
)
543 struct btrfs_fs_info
*fs_info
= rc
->fs_info
;
544 struct reada_extent
*re
;
545 struct reada_extctl
*rec
;
548 re
= reada_find_extent(fs_info
, logical
, top
);
552 rec
= kzalloc(sizeof(*rec
), GFP_KERNEL
);
554 reada_extent_put(fs_info
, re
);
559 rec
->generation
= generation
;
560 atomic_inc(&rc
->elems
);
562 spin_lock(&re
->lock
);
563 list_add_tail(&rec
->list
, &re
->extctl
);
564 spin_unlock(&re
->lock
);
566 /* leave the ref on the extent */
572 * called with fs_info->reada_lock held
574 static void reada_peer_zones_set_lock(struct reada_zone
*zone
, int lock
)
577 unsigned long index
= zone
->end
>> PAGE_SHIFT
;
579 for (i
= 0; i
< zone
->ndevs
; ++i
) {
580 struct reada_zone
*peer
;
581 peer
= radix_tree_lookup(&zone
->devs
[i
]->reada_zones
, index
);
582 if (peer
&& peer
->device
!= zone
->device
)
588 * called with fs_info->reada_lock held
590 static int reada_pick_zone(struct btrfs_device
*dev
)
592 struct reada_zone
*top_zone
= NULL
;
593 struct reada_zone
*top_locked_zone
= NULL
;
595 u64 top_locked_elems
= 0;
596 unsigned long index
= 0;
599 if (dev
->reada_curr_zone
) {
600 reada_peer_zones_set_lock(dev
->reada_curr_zone
, 0);
601 kref_put(&dev
->reada_curr_zone
->refcnt
, reada_zone_release
);
602 dev
->reada_curr_zone
= NULL
;
604 /* pick the zone with the most elements */
606 struct reada_zone
*zone
;
608 ret
= radix_tree_gang_lookup(&dev
->reada_zones
,
609 (void **)&zone
, index
, 1);
612 index
= (zone
->end
>> PAGE_SHIFT
) + 1;
614 if (zone
->elems
> top_locked_elems
) {
615 top_locked_elems
= zone
->elems
;
616 top_locked_zone
= zone
;
619 if (zone
->elems
> top_elems
) {
620 top_elems
= zone
->elems
;
626 dev
->reada_curr_zone
= top_zone
;
627 else if (top_locked_zone
)
628 dev
->reada_curr_zone
= top_locked_zone
;
632 dev
->reada_next
= dev
->reada_curr_zone
->start
;
633 kref_get(&dev
->reada_curr_zone
->refcnt
);
634 reada_peer_zones_set_lock(dev
->reada_curr_zone
, 1);
639 static int reada_start_machine_dev(struct btrfs_device
*dev
)
641 struct btrfs_fs_info
*fs_info
= dev
->fs_info
;
642 struct reada_extent
*re
= NULL
;
644 struct extent_buffer
*eb
= NULL
;
649 spin_lock(&fs_info
->reada_lock
);
650 if (dev
->reada_curr_zone
== NULL
) {
651 ret
= reada_pick_zone(dev
);
653 spin_unlock(&fs_info
->reada_lock
);
658 * FIXME currently we issue the reads one extent at a time. If we have
659 * a contiguous block of extents, we could also coagulate them or use
660 * plugging to speed things up
662 ret
= radix_tree_gang_lookup(&dev
->reada_extents
, (void **)&re
,
663 dev
->reada_next
>> PAGE_SHIFT
, 1);
664 if (ret
== 0 || re
->logical
> dev
->reada_curr_zone
->end
) {
665 ret
= reada_pick_zone(dev
);
667 spin_unlock(&fs_info
->reada_lock
);
671 ret
= radix_tree_gang_lookup(&dev
->reada_extents
, (void **)&re
,
672 dev
->reada_next
>> PAGE_SHIFT
, 1);
675 spin_unlock(&fs_info
->reada_lock
);
678 dev
->reada_next
= re
->logical
+ fs_info
->nodesize
;
681 spin_unlock(&fs_info
->reada_lock
);
683 spin_lock(&re
->lock
);
684 if (re
->scheduled
|| list_empty(&re
->extctl
)) {
685 spin_unlock(&re
->lock
);
686 reada_extent_put(fs_info
, re
);
690 spin_unlock(&re
->lock
);
695 for (i
= 0; i
< re
->nzones
; ++i
) {
696 if (re
->zones
[i
]->device
== dev
) {
701 logical
= re
->logical
;
703 atomic_inc(&dev
->reada_in_flight
);
704 ret
= reada_tree_block_flagged(fs_info
, logical
, mirror_num
, &eb
);
706 __readahead_hook(fs_info
, re
, NULL
, ret
);
708 __readahead_hook(fs_info
, re
, eb
, ret
);
711 free_extent_buffer(eb
);
713 atomic_dec(&dev
->reada_in_flight
);
714 reada_extent_put(fs_info
, re
);
720 static void reada_start_machine_worker(struct btrfs_work
*work
)
722 struct reada_machine_work
*rmw
;
725 rmw
= container_of(work
, struct reada_machine_work
, work
);
727 old_ioprio
= IOPRIO_PRIO_VALUE(task_nice_ioclass(current
),
728 task_nice_ioprio(current
));
729 set_task_ioprio(current
, BTRFS_IOPRIO_READA
);
730 __reada_start_machine(rmw
->fs_info
);
731 set_task_ioprio(current
, old_ioprio
);
733 atomic_dec(&rmw
->fs_info
->reada_works_cnt
);
738 static void __reada_start_machine(struct btrfs_fs_info
*fs_info
)
740 struct btrfs_device
*device
;
741 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
749 mutex_lock(&fs_devices
->device_list_mutex
);
750 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
751 if (atomic_read(&device
->reada_in_flight
) <
753 enqueued
+= reada_start_machine_dev(device
);
755 mutex_unlock(&fs_devices
->device_list_mutex
);
757 } while (enqueued
&& total
< 10000);
758 if (fs_devices
->seed
) {
759 fs_devices
= fs_devices
->seed
;
767 * If everything is already in the cache, this is effectively single
768 * threaded. To a) not hold the caller for too long and b) to utilize
769 * more cores, we broke the loop above after 10000 iterations and now
770 * enqueue to workers to finish it. This will distribute the load to
773 for (i
= 0; i
< 2; ++i
) {
774 reada_start_machine(fs_info
);
775 if (atomic_read(&fs_info
->reada_works_cnt
) >
776 BTRFS_MAX_MIRRORS
* 2)
781 static void reada_start_machine(struct btrfs_fs_info
*fs_info
)
783 struct reada_machine_work
*rmw
;
785 rmw
= kzalloc(sizeof(*rmw
), GFP_KERNEL
);
787 /* FIXME we cannot handle this properly right now */
790 btrfs_init_work(&rmw
->work
, btrfs_readahead_helper
,
791 reada_start_machine_worker
, NULL
, NULL
);
792 rmw
->fs_info
= fs_info
;
794 btrfs_queue_work(fs_info
->readahead_workers
, &rmw
->work
);
795 atomic_inc(&fs_info
->reada_works_cnt
);
799 static void dump_devs(struct btrfs_fs_info
*fs_info
, int all
)
801 struct btrfs_device
*device
;
802 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
809 spin_lock(&fs_info
->reada_lock
);
810 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
811 btrfs_debug(fs_info
, "dev %lld has %d in flight", device
->devid
,
812 atomic_read(&device
->reada_in_flight
));
815 struct reada_zone
*zone
;
816 ret
= radix_tree_gang_lookup(&device
->reada_zones
,
817 (void **)&zone
, index
, 1);
820 pr_debug(" zone %llu-%llu elems %llu locked %d devs",
821 zone
->start
, zone
->end
, zone
->elems
,
823 for (j
= 0; j
< zone
->ndevs
; ++j
) {
825 zone
->devs
[j
]->devid
);
827 if (device
->reada_curr_zone
== zone
)
828 pr_cont(" curr off %llu",
829 device
->reada_next
- zone
->start
);
831 index
= (zone
->end
>> PAGE_SHIFT
) + 1;
836 struct reada_extent
*re
= NULL
;
838 ret
= radix_tree_gang_lookup(&device
->reada_extents
,
839 (void **)&re
, index
, 1);
842 pr_debug(" re: logical %llu size %u empty %d scheduled %d",
843 re
->logical
, fs_info
->nodesize
,
844 list_empty(&re
->extctl
), re
->scheduled
);
846 for (i
= 0; i
< re
->nzones
; ++i
) {
847 pr_cont(" zone %llu-%llu devs",
850 for (j
= 0; j
< re
->zones
[i
]->ndevs
; ++j
) {
852 re
->zones
[i
]->devs
[j
]->devid
);
856 index
= (re
->logical
>> PAGE_SHIFT
) + 1;
865 struct reada_extent
*re
= NULL
;
867 ret
= radix_tree_gang_lookup(&fs_info
->reada_tree
, (void **)&re
,
871 if (!re
->scheduled
) {
872 index
= (re
->logical
>> PAGE_SHIFT
) + 1;
875 pr_debug("re: logical %llu size %u list empty %d scheduled %d",
876 re
->logical
, fs_info
->nodesize
,
877 list_empty(&re
->extctl
), re
->scheduled
);
878 for (i
= 0; i
< re
->nzones
; ++i
) {
879 pr_cont(" zone %llu-%llu devs",
882 for (j
= 0; j
< re
->zones
[i
]->ndevs
; ++j
) {
884 re
->zones
[i
]->devs
[j
]->devid
);
888 index
= (re
->logical
>> PAGE_SHIFT
) + 1;
890 spin_unlock(&fs_info
->reada_lock
);
897 struct reada_control
*btrfs_reada_add(struct btrfs_root
*root
,
898 struct btrfs_key
*key_start
, struct btrfs_key
*key_end
)
900 struct reada_control
*rc
;
904 struct extent_buffer
*node
;
905 static struct btrfs_key max_key
= {
911 rc
= kzalloc(sizeof(*rc
), GFP_KERNEL
);
913 return ERR_PTR(-ENOMEM
);
915 rc
->fs_info
= root
->fs_info
;
916 rc
->key_start
= *key_start
;
917 rc
->key_end
= *key_end
;
918 atomic_set(&rc
->elems
, 0);
919 init_waitqueue_head(&rc
->wait
);
920 kref_init(&rc
->refcnt
);
921 kref_get(&rc
->refcnt
); /* one ref for having elements */
923 node
= btrfs_root_node(root
);
925 generation
= btrfs_header_generation(node
);
926 free_extent_buffer(node
);
928 ret
= reada_add_block(rc
, start
, &max_key
, generation
);
934 reada_start_machine(root
->fs_info
);
940 int btrfs_reada_wait(void *handle
)
942 struct reada_control
*rc
= handle
;
943 struct btrfs_fs_info
*fs_info
= rc
->fs_info
;
945 while (atomic_read(&rc
->elems
)) {
946 if (!atomic_read(&fs_info
->reada_works_cnt
))
947 reada_start_machine(fs_info
);
948 wait_event_timeout(rc
->wait
, atomic_read(&rc
->elems
) == 0,
950 dump_devs(fs_info
, atomic_read(&rc
->elems
) < 10 ? 1 : 0);
953 dump_devs(fs_info
, atomic_read(&rc
->elems
) < 10 ? 1 : 0);
955 kref_put(&rc
->refcnt
, reada_control_release
);
960 int btrfs_reada_wait(void *handle
)
962 struct reada_control
*rc
= handle
;
963 struct btrfs_fs_info
*fs_info
= rc
->fs_info
;
965 while (atomic_read(&rc
->elems
)) {
966 if (!atomic_read(&fs_info
->reada_works_cnt
))
967 reada_start_machine(fs_info
);
968 wait_event_timeout(rc
->wait
, atomic_read(&rc
->elems
) == 0,
972 kref_put(&rc
->refcnt
, reada_control_release
);
978 void btrfs_reada_detach(void *handle
)
980 struct reada_control
*rc
= handle
;
982 kref_put(&rc
->refcnt
, reada_control_release
);