19 #include <linux/sched.h>
23 #include <linux/rbtree.h>
24 #include <linux/slab.h>
57 #define MAX_IN_FLIGHT 6
98 static void reada_control_release(
struct kref *
kref);
99 static void reada_zone_release(
struct kref *
kref);
123 level = btrfs_header_level(eb);
135 spin_lock(&re->
lock);
143 spin_unlock(&re->
lock);
146 nritems = level ? btrfs_header_nritems(eb) : 0;
147 generation = btrfs_header_generation(eb);
165 for (i = 0; i <
nritems; i++) {
171 btrfs_node_key_to_cpu(eb, &
key, i);
173 btrfs_node_key_to_cpu(eb, &next_key, i + 1);
176 bytenr = btrfs_node_blockptr(eb, i);
177 n_gen = btrfs_node_ptr_generation(eb, i);
192 "(%llu,%d,%llu) %llu != %llu\n",
200 reada_add_block(rc, bytenr, &next_key,
207 while (!list_empty(&
list)) {
218 kref_put(&rc->
refcnt, reada_control_release);
221 kref_put(&rc->
refcnt, reada_control_release);
223 reada_extent_put(fs_info, re);
225 reada_extent_put(fs_info, re);
241 ret = __readahead_hook(root, eb, start, err);
243 reada_start_machine(root->
fs_info);
262 logical >> PAGE_CACHE_SHIFT, 1);
268 if (logical >= zone->
start && logical < zone->end)
271 kref_put(&zone->
refcnt, reada_zone_release);
279 start = cache->
key.objectid;
280 end = start + cache->
key.offset - 1;
283 zone = kzalloc(
sizeof(*zone),
GFP_NOFS);
289 INIT_LIST_HEAD(&zone->
list);
303 (
unsigned long)(zone->
end >> PAGE_CACHE_SHIFT),
309 logical >> PAGE_CACHE_SHIFT, 1);
345 re = kzalloc(
sizeof(*re),
GFP_NOFS);
349 blocksize = btrfs_level_size(root, level);
353 INIT_LIST_HEAD(&re->
extctl);
362 if (ret || !bbio || length < blocksize)
371 for (nzones = 0; nzones < bbio->
num_stripes; ++nzones) {
374 dev = bbio->
stripes[nzones].dev;
375 zone = reada_find_zone(fs_info, dev, logical, bbio);
379 re->
zones[nzones] = zone;
380 spin_lock(&zone->
lock);
384 spin_unlock(&zone->
lock);
386 kref_put(&zone->
refcnt, reada_zone_release);
410 for (i = 0; i < nzones; ++
i) {
412 if (dev == prev_dev) {
446 zone = re->
zones[nzones];
448 spin_lock(&zone->
lock);
450 if (zone->
elems == 0) {
455 kref_put(&zone->
refcnt, reada_zone_release);
457 spin_unlock(&zone->
lock);
460 kref_put(&zone->
refcnt, reada_zone_release);
481 for (i = 0; i < re->
nzones; ++
i) {
489 for (i = 0; i < re->
nzones; ++
i) {
493 spin_lock(&zone->
lock);
495 if (zone->
elems == 0) {
498 kref_put(&zone->
refcnt, reada_zone_release);
500 spin_unlock(&zone->
lock);
503 kref_put(&zone->
refcnt, reada_zone_release);
512 static void reada_zone_release(
struct kref *
kref)
517 zone->
end >> PAGE_CACHE_SHIFT);
522 static void reada_control_release(
struct kref *
kref)
537 re = reada_find_extent(root, logical, top, level);
541 rec = kzalloc(
sizeof(*rec),
GFP_NOFS);
543 reada_extent_put(root->
fs_info, re);
551 spin_lock(&re->
lock);
553 spin_unlock(&re->
lock);
563 static void reada_peer_zones_set_lock(
struct reada_zone *zone,
int lock)
568 for (i = 0; i < zone->
ndevs; ++
i) {
584 u64 top_locked_elems = 0;
585 unsigned long index = 0;
598 (
void **)&zone, index, 1);
603 if (zone->
elems > top_locked_elems) {
604 top_locked_elems = zone->
elems;
605 top_locked_zone = zone;
608 if (zone->
elems > top_elems) {
609 top_elems = zone->
elems;
616 else if (top_locked_zone)
628 static int reada_start_machine_dev(
struct btrfs_fs_info *fs_info,
642 ret = reada_pick_zone(dev);
656 ret = reada_pick_zone(dev);
677 for (i = 0; i < re->
nzones; ++
i) {
678 if (re->
zones[i]->device == dev) {
686 spin_lock(&re->
lock);
691 spin_unlock(&re->
lock);
693 reada_extent_put(fs_info, re);
727 __reada_start_machine(fs_info);
731 static void __reada_start_machine(
struct btrfs_fs_info *fs_info)
744 enqueued += reada_start_machine_dev(fs_info,
748 }
while (enqueued && total < 10000);
760 for (i = 0; i < 2; ++
i)
761 reada_start_machine(fs_info);
764 static void reada_start_machine(
struct btrfs_fs_info *fs_info)
768 rmw = kzalloc(
sizeof(*rmw),
GFP_NOFS);
773 rmw->
work.func = reada_start_machine_worker;
780 static void dump_devs(
struct btrfs_fs_info *fs_info,
int all)
798 (
void **)&zone, index, 1);
804 for (j = 0; j < zone->
ndevs; ++
j) {
806 zone->
devs[j]->devid);
820 (
void **)&re, index, 1);
824 " re: logical %llu size %u empty %d for %lld",
829 for (i = 0; i < re->
nzones; ++
i) {
833 for (j = 0; j < re->
zones[
i]->ndevs; ++
j) {
835 re->
zones[i]->devs[j]->devid);
859 "re: logical %llu size %u list empty %d for %lld",
862 for (i = 0; i < re->
nzones; ++
i) {
866 for (i = 0; i < re->
nzones; ++
i) {
870 for (j = 0; j < re->
zones[
i]->ndevs; ++
j) {
872 re->
zones[i]->devs[j]->devid);
900 rc = kzalloc(
sizeof(*rc),
GFP_NOFS);
914 level = btrfs_header_level(node);
915 generation = btrfs_header_generation(node);
918 reada_add_block(rc, start, &max_key, level, generation);
920 reada_start_machine(root->
fs_info);
933 dump_devs(rc->
root->fs_info, rc->
elems < 10 ? 1 : 0);
936 dump_devs(rc->
root->fs_info, rc->
elems < 10 ? 1 : 0);
938 kref_put(&rc->
refcnt, reada_control_release);
951 kref_put(&rc->
refcnt, reada_control_release);
961 kref_put(&rc->
refcnt, reada_control_release);