GNU Linux-libre 4.14.290-gnu1
[releases.git] / fs / btrfs / reada.c
1 /*
2  * Copyright (C) 2011 STRATO.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <linux/sched.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/rbtree.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include "ctree.h"
27 #include "volumes.h"
28 #include "disk-io.h"
29 #include "transaction.h"
30 #include "dev-replace.h"
31
32 #undef DEBUG
33
34 /*
35  * This is the implementation for the generic read ahead framework.
36  *
37  * To trigger a readahead, btrfs_reada_add must be called. It will start
38  * a read ahead for the given range [start, end) on tree root. The returned
39  * handle can either be used to wait on the readahead to finish
40  * (btrfs_reada_wait), or to send it to the background (btrfs_reada_detach).
41  *
42  * The read ahead works as follows:
43  * On btrfs_reada_add, the root of the tree is inserted into a radix_tree.
44  * reada_start_machine will then search for extents to prefetch and trigger
45  * some reads. When a read finishes for a node, all contained node/leaf
46  * pointers that lie in the given range will also be enqueued. The reads will
47  * be triggered in sequential order, thus giving a big win over a naive
48  * enumeration. It will also make use of multi-device layouts. Each disk
49  * will have its on read pointer and all disks will by utilized in parallel.
50  * Also will no two disks read both sides of a mirror simultaneously, as this
51  * would waste seeking capacity. Instead both disks will read different parts
52  * of the filesystem.
53  * Any number of readaheads can be started in parallel. The read order will be
54  * determined globally, i.e. 2 parallel readaheads will normally finish faster
55  * than the 2 started one after another.
56  */
57
58 #define MAX_IN_FLIGHT 6
59
60 struct reada_extctl {
61         struct list_head        list;
62         struct reada_control    *rc;
63         u64                     generation;
64 };
65
66 struct reada_extent {
67         u64                     logical;
68         struct btrfs_key        top;
69         struct list_head        extctl;
70         int                     refcnt;
71         spinlock_t              lock;
72         struct reada_zone       *zones[BTRFS_MAX_MIRRORS];
73         int                     nzones;
74         int                     scheduled;
75 };
76
77 struct reada_zone {
78         u64                     start;
79         u64                     end;
80         u64                     elems;
81         struct list_head        list;
82         spinlock_t              lock;
83         int                     locked;
84         struct btrfs_device     *device;
85         struct btrfs_device     *devs[BTRFS_MAX_MIRRORS]; /* full list, incl
86                                                            * self */
87         int                     ndevs;
88         struct kref             refcnt;
89 };
90
91 struct reada_machine_work {
92         struct btrfs_work       work;
93         struct btrfs_fs_info    *fs_info;
94 };
95
96 static void reada_extent_put(struct btrfs_fs_info *, struct reada_extent *);
97 static void reada_control_release(struct kref *kref);
98 static void reada_zone_release(struct kref *kref);
99 static void reada_start_machine(struct btrfs_fs_info *fs_info);
100 static void __reada_start_machine(struct btrfs_fs_info *fs_info);
101
102 static int reada_add_block(struct reada_control *rc, u64 logical,
103                            struct btrfs_key *top, u64 generation);
104
105 /* recurses */
106 /* in case of err, eb might be NULL */
107 static void __readahead_hook(struct btrfs_fs_info *fs_info,
108                              struct reada_extent *re, struct extent_buffer *eb,
109                              int err)
110 {
111         int nritems;
112         int i;
113         u64 bytenr;
114         u64 generation;
115         struct list_head list;
116
117         spin_lock(&re->lock);
118         /*
119          * just take the full list from the extent. afterwards we
120          * don't need the lock anymore
121          */
122         list_replace_init(&re->extctl, &list);
123         re->scheduled = 0;
124         spin_unlock(&re->lock);
125
126         /*
127          * this is the error case, the extent buffer has not been
128          * read correctly. We won't access anything from it and
129          * just cleanup our data structures. Effectively this will
130          * cut the branch below this node from read ahead.
131          */
132         if (err)
133                 goto cleanup;
134
135         /*
136          * FIXME: currently we just set nritems to 0 if this is a leaf,
137          * effectively ignoring the content. In a next step we could
138          * trigger more readahead depending from the content, e.g.
139          * fetch the checksums for the extents in the leaf.
140          */
141         if (!btrfs_header_level(eb))
142                 goto cleanup;
143
144         nritems = btrfs_header_nritems(eb);
145         generation = btrfs_header_generation(eb);
146         for (i = 0; i < nritems; i++) {
147                 struct reada_extctl *rec;
148                 u64 n_gen;
149                 struct btrfs_key key;
150                 struct btrfs_key next_key;
151
152                 btrfs_node_key_to_cpu(eb, &key, i);
153                 if (i + 1 < nritems)
154                         btrfs_node_key_to_cpu(eb, &next_key, i + 1);
155                 else
156                         next_key = re->top;
157                 bytenr = btrfs_node_blockptr(eb, i);
158                 n_gen = btrfs_node_ptr_generation(eb, i);
159
160                 list_for_each_entry(rec, &list, list) {
161                         struct reada_control *rc = rec->rc;
162
163                         /*
164                          * if the generation doesn't match, just ignore this
165                          * extctl. This will probably cut off a branch from
166                          * prefetch. Alternatively one could start a new (sub-)
167                          * prefetch for this branch, starting again from root.
168                          * FIXME: move the generation check out of this loop
169                          */
170 #ifdef DEBUG
171                         if (rec->generation != generation) {
172                                 btrfs_debug(fs_info,
173                                             "generation mismatch for (%llu,%d,%llu) %llu != %llu",
174                                             key.objectid, key.type, key.offset,
175                                             rec->generation, generation);
176                         }
177 #endif
178                         if (rec->generation == generation &&
179                             btrfs_comp_cpu_keys(&key, &rc->key_end) < 0 &&
180                             btrfs_comp_cpu_keys(&next_key, &rc->key_start) > 0)
181                                 reada_add_block(rc, bytenr, &next_key, n_gen);
182                 }
183         }
184
185 cleanup:
186         /*
187          * free extctl records
188          */
189         while (!list_empty(&list)) {
190                 struct reada_control *rc;
191                 struct reada_extctl *rec;
192
193                 rec = list_first_entry(&list, struct reada_extctl, list);
194                 list_del(&rec->list);
195                 rc = rec->rc;
196                 kfree(rec);
197
198                 kref_get(&rc->refcnt);
199                 if (atomic_dec_and_test(&rc->elems)) {
200                         kref_put(&rc->refcnt, reada_control_release);
201                         wake_up(&rc->wait);
202                 }
203                 kref_put(&rc->refcnt, reada_control_release);
204
205                 reada_extent_put(fs_info, re);  /* one ref for each entry */
206         }
207
208         return;
209 }
210
211 int btree_readahead_hook(struct extent_buffer *eb, int err)
212 {
213         struct btrfs_fs_info *fs_info = eb->fs_info;
214         int ret = 0;
215         struct reada_extent *re;
216
217         /* find extent */
218         spin_lock(&fs_info->reada_lock);
219         re = radix_tree_lookup(&fs_info->reada_tree,
220                                eb->start >> PAGE_SHIFT);
221         if (re)
222                 re->refcnt++;
223         spin_unlock(&fs_info->reada_lock);
224         if (!re) {
225                 ret = -1;
226                 goto start_machine;
227         }
228
229         __readahead_hook(fs_info, re, eb, err);
230         reada_extent_put(fs_info, re);  /* our ref */
231
232 start_machine:
233         reada_start_machine(fs_info);
234         return ret;
235 }
236
237 static struct reada_zone *reada_find_zone(struct btrfs_device *dev, u64 logical,
238                                           struct btrfs_bio *bbio)
239 {
240         struct btrfs_fs_info *fs_info = dev->fs_info;
241         int ret;
242         struct reada_zone *zone;
243         struct btrfs_block_group_cache *cache = NULL;
244         u64 start;
245         u64 end;
246         int i;
247
248         zone = NULL;
249         spin_lock(&fs_info->reada_lock);
250         ret = radix_tree_gang_lookup(&dev->reada_zones, (void **)&zone,
251                                      logical >> PAGE_SHIFT, 1);
252         if (ret == 1 && logical >= zone->start && logical <= zone->end) {
253                 kref_get(&zone->refcnt);
254                 spin_unlock(&fs_info->reada_lock);
255                 return zone;
256         }
257
258         spin_unlock(&fs_info->reada_lock);
259
260         cache = btrfs_lookup_block_group(fs_info, logical);
261         if (!cache)
262                 return NULL;
263
264         start = cache->key.objectid;
265         end = start + cache->key.offset - 1;
266         btrfs_put_block_group(cache);
267
268         zone = kzalloc(sizeof(*zone), GFP_KERNEL);
269         if (!zone)
270                 return NULL;
271
272         ret = radix_tree_preload(GFP_KERNEL);
273         if (ret) {
274                 kfree(zone);
275                 return NULL;
276         }
277
278         zone->start = start;
279         zone->end = end;
280         INIT_LIST_HEAD(&zone->list);
281         spin_lock_init(&zone->lock);
282         zone->locked = 0;
283         kref_init(&zone->refcnt);
284         zone->elems = 0;
285         zone->device = dev; /* our device always sits at index 0 */
286         for (i = 0; i < bbio->num_stripes; ++i) {
287                 /* bounds have already been checked */
288                 zone->devs[i] = bbio->stripes[i].dev;
289         }
290         zone->ndevs = bbio->num_stripes;
291
292         spin_lock(&fs_info->reada_lock);
293         ret = radix_tree_insert(&dev->reada_zones,
294                                 (unsigned long)(zone->end >> PAGE_SHIFT),
295                                 zone);
296
297         if (ret == -EEXIST) {
298                 kfree(zone);
299                 ret = radix_tree_gang_lookup(&dev->reada_zones, (void **)&zone,
300                                              logical >> PAGE_SHIFT, 1);
301                 if (ret == 1 && logical >= zone->start && logical <= zone->end)
302                         kref_get(&zone->refcnt);
303                 else
304                         zone = NULL;
305         }
306         spin_unlock(&fs_info->reada_lock);
307         radix_tree_preload_end();
308
309         return zone;
310 }
311
312 static struct reada_extent *reada_find_extent(struct btrfs_fs_info *fs_info,
313                                               u64 logical,
314                                               struct btrfs_key *top)
315 {
316         int ret;
317         struct reada_extent *re = NULL;
318         struct reada_extent *re_exist = NULL;
319         struct btrfs_bio *bbio = NULL;
320         struct btrfs_device *dev;
321         struct btrfs_device *prev_dev;
322         u64 length;
323         int real_stripes;
324         int nzones = 0;
325         unsigned long index = logical >> PAGE_SHIFT;
326         int dev_replace_is_ongoing;
327         int have_zone = 0;
328
329         spin_lock(&fs_info->reada_lock);
330         re = radix_tree_lookup(&fs_info->reada_tree, index);
331         if (re)
332                 re->refcnt++;
333         spin_unlock(&fs_info->reada_lock);
334
335         if (re)
336                 return re;
337
338         re = kzalloc(sizeof(*re), GFP_KERNEL);
339         if (!re)
340                 return NULL;
341
342         re->logical = logical;
343         re->top = *top;
344         INIT_LIST_HEAD(&re->extctl);
345         spin_lock_init(&re->lock);
346         re->refcnt = 1;
347
348         /*
349          * map block
350          */
351         length = fs_info->nodesize;
352         ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
353                         &length, &bbio, 0);
354         if (ret || !bbio || length < fs_info->nodesize)
355                 goto error;
356
357         if (bbio->num_stripes > BTRFS_MAX_MIRRORS) {
358                 btrfs_err(fs_info,
359                            "readahead: more than %d copies not supported",
360                            BTRFS_MAX_MIRRORS);
361                 goto error;
362         }
363
364         real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
365         for (nzones = 0; nzones < real_stripes; ++nzones) {
366                 struct reada_zone *zone;
367
368                 dev = bbio->stripes[nzones].dev;
369
370                 /* cannot read ahead on missing device. */
371                  if (!dev->bdev)
372                         continue;
373
374                 zone = reada_find_zone(dev, logical, bbio);
375                 if (!zone)
376                         continue;
377
378                 re->zones[re->nzones++] = zone;
379                 spin_lock(&zone->lock);
380                 if (!zone->elems)
381                         kref_get(&zone->refcnt);
382                 ++zone->elems;
383                 spin_unlock(&zone->lock);
384                 spin_lock(&fs_info->reada_lock);
385                 kref_put(&zone->refcnt, reada_zone_release);
386                 spin_unlock(&fs_info->reada_lock);
387         }
388         if (re->nzones == 0) {
389                 /* not a single zone found, error and out */
390                 goto error;
391         }
392
393         ret = radix_tree_preload(GFP_KERNEL);
394         if (ret)
395                 goto error;
396
397         /* insert extent in reada_tree + all per-device trees, all or nothing */
398         btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
399         spin_lock(&fs_info->reada_lock);
400         ret = radix_tree_insert(&fs_info->reada_tree, index, re);
401         if (ret == -EEXIST) {
402                 re_exist = radix_tree_lookup(&fs_info->reada_tree, index);
403                 re_exist->refcnt++;
404                 spin_unlock(&fs_info->reada_lock);
405                 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
406                 radix_tree_preload_end();
407                 goto error;
408         }
409         if (ret) {
410                 spin_unlock(&fs_info->reada_lock);
411                 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
412                 radix_tree_preload_end();
413                 goto error;
414         }
415         radix_tree_preload_end();
416         prev_dev = NULL;
417         dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(
418                         &fs_info->dev_replace);
419         for (nzones = 0; nzones < re->nzones; ++nzones) {
420                 dev = re->zones[nzones]->device;
421
422                 if (dev == prev_dev) {
423                         /*
424                          * in case of DUP, just add the first zone. As both
425                          * are on the same device, there's nothing to gain
426                          * from adding both.
427                          * Also, it wouldn't work, as the tree is per device
428                          * and adding would fail with EEXIST
429                          */
430                         continue;
431                 }
432                 if (!dev->bdev)
433                         continue;
434
435                 if (dev_replace_is_ongoing &&
436                     dev == fs_info->dev_replace.tgtdev) {
437                         /*
438                          * as this device is selected for reading only as
439                          * a last resort, skip it for read ahead.
440                          */
441                         continue;
442                 }
443                 prev_dev = dev;
444                 ret = radix_tree_insert(&dev->reada_extents, index, re);
445                 if (ret) {
446                         while (--nzones >= 0) {
447                                 dev = re->zones[nzones]->device;
448                                 BUG_ON(dev == NULL);
449                                 /* ignore whether the entry was inserted */
450                                 radix_tree_delete(&dev->reada_extents, index);
451                         }
452                         radix_tree_delete(&fs_info->reada_tree, index);
453                         spin_unlock(&fs_info->reada_lock);
454                         btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
455                         goto error;
456                 }
457                 have_zone = 1;
458         }
459         if (!have_zone)
460                 radix_tree_delete(&fs_info->reada_tree, index);
461         spin_unlock(&fs_info->reada_lock);
462         btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
463
464         if (!have_zone)
465                 goto error;
466
467         btrfs_put_bbio(bbio);
468         return re;
469
470 error:
471         for (nzones = 0; nzones < re->nzones; ++nzones) {
472                 struct reada_zone *zone;
473
474                 zone = re->zones[nzones];
475                 kref_get(&zone->refcnt);
476                 spin_lock(&zone->lock);
477                 --zone->elems;
478                 if (zone->elems == 0) {
479                         /*
480                          * no fs_info->reada_lock needed, as this can't be
481                          * the last ref
482                          */
483                         kref_put(&zone->refcnt, reada_zone_release);
484                 }
485                 spin_unlock(&zone->lock);
486
487                 spin_lock(&fs_info->reada_lock);
488                 kref_put(&zone->refcnt, reada_zone_release);
489                 spin_unlock(&fs_info->reada_lock);
490         }
491         btrfs_put_bbio(bbio);
492         kfree(re);
493         return re_exist;
494 }
495
496 static void reada_extent_put(struct btrfs_fs_info *fs_info,
497                              struct reada_extent *re)
498 {
499         int i;
500         unsigned long index = re->logical >> PAGE_SHIFT;
501
502         spin_lock(&fs_info->reada_lock);
503         if (--re->refcnt) {
504                 spin_unlock(&fs_info->reada_lock);
505                 return;
506         }
507
508         radix_tree_delete(&fs_info->reada_tree, index);
509         for (i = 0; i < re->nzones; ++i) {
510                 struct reada_zone *zone = re->zones[i];
511
512                 radix_tree_delete(&zone->device->reada_extents, index);
513         }
514
515         spin_unlock(&fs_info->reada_lock);
516
517         for (i = 0; i < re->nzones; ++i) {
518                 struct reada_zone *zone = re->zones[i];
519
520                 kref_get(&zone->refcnt);
521                 spin_lock(&zone->lock);
522                 --zone->elems;
523                 if (zone->elems == 0) {
524                         /* no fs_info->reada_lock needed, as this can't be
525                          * the last ref */
526                         kref_put(&zone->refcnt, reada_zone_release);
527                 }
528                 spin_unlock(&zone->lock);
529
530                 spin_lock(&fs_info->reada_lock);
531                 kref_put(&zone->refcnt, reada_zone_release);
532                 spin_unlock(&fs_info->reada_lock);
533         }
534
535         kfree(re);
536 }
537
538 static void reada_zone_release(struct kref *kref)
539 {
540         struct reada_zone *zone = container_of(kref, struct reada_zone, refcnt);
541
542         radix_tree_delete(&zone->device->reada_zones,
543                           zone->end >> PAGE_SHIFT);
544
545         kfree(zone);
546 }
547
548 static void reada_control_release(struct kref *kref)
549 {
550         struct reada_control *rc = container_of(kref, struct reada_control,
551                                                 refcnt);
552
553         kfree(rc);
554 }
555
556 static int reada_add_block(struct reada_control *rc, u64 logical,
557                            struct btrfs_key *top, u64 generation)
558 {
559         struct btrfs_fs_info *fs_info = rc->fs_info;
560         struct reada_extent *re;
561         struct reada_extctl *rec;
562
563         /* takes one ref */
564         re = reada_find_extent(fs_info, logical, top);
565         if (!re)
566                 return -1;
567
568         rec = kzalloc(sizeof(*rec), GFP_KERNEL);
569         if (!rec) {
570                 reada_extent_put(fs_info, re);
571                 return -ENOMEM;
572         }
573
574         rec->rc = rc;
575         rec->generation = generation;
576         atomic_inc(&rc->elems);
577
578         spin_lock(&re->lock);
579         list_add_tail(&rec->list, &re->extctl);
580         spin_unlock(&re->lock);
581
582         /* leave the ref on the extent */
583
584         return 0;
585 }
586
587 /*
588  * called with fs_info->reada_lock held
589  */
590 static void reada_peer_zones_set_lock(struct reada_zone *zone, int lock)
591 {
592         int i;
593         unsigned long index = zone->end >> PAGE_SHIFT;
594
595         for (i = 0; i < zone->ndevs; ++i) {
596                 struct reada_zone *peer;
597                 peer = radix_tree_lookup(&zone->devs[i]->reada_zones, index);
598                 if (peer && peer->device != zone->device)
599                         peer->locked = lock;
600         }
601 }
602
603 /*
604  * called with fs_info->reada_lock held
605  */
606 static int reada_pick_zone(struct btrfs_device *dev)
607 {
608         struct reada_zone *top_zone = NULL;
609         struct reada_zone *top_locked_zone = NULL;
610         u64 top_elems = 0;
611         u64 top_locked_elems = 0;
612         unsigned long index = 0;
613         int ret;
614
615         if (dev->reada_curr_zone) {
616                 reada_peer_zones_set_lock(dev->reada_curr_zone, 0);
617                 kref_put(&dev->reada_curr_zone->refcnt, reada_zone_release);
618                 dev->reada_curr_zone = NULL;
619         }
620         /* pick the zone with the most elements */
621         while (1) {
622                 struct reada_zone *zone;
623
624                 ret = radix_tree_gang_lookup(&dev->reada_zones,
625                                              (void **)&zone, index, 1);
626                 if (ret == 0)
627                         break;
628                 index = (zone->end >> PAGE_SHIFT) + 1;
629                 if (zone->locked) {
630                         if (zone->elems > top_locked_elems) {
631                                 top_locked_elems = zone->elems;
632                                 top_locked_zone = zone;
633                         }
634                 } else {
635                         if (zone->elems > top_elems) {
636                                 top_elems = zone->elems;
637                                 top_zone = zone;
638                         }
639                 }
640         }
641         if (top_zone)
642                 dev->reada_curr_zone = top_zone;
643         else if (top_locked_zone)
644                 dev->reada_curr_zone = top_locked_zone;
645         else
646                 return 0;
647
648         dev->reada_next = dev->reada_curr_zone->start;
649         kref_get(&dev->reada_curr_zone->refcnt);
650         reada_peer_zones_set_lock(dev->reada_curr_zone, 1);
651
652         return 1;
653 }
654
655 static int reada_start_machine_dev(struct btrfs_device *dev)
656 {
657         struct btrfs_fs_info *fs_info = dev->fs_info;
658         struct reada_extent *re = NULL;
659         int mirror_num = 0;
660         struct extent_buffer *eb = NULL;
661         u64 logical;
662         int ret;
663         int i;
664
665         spin_lock(&fs_info->reada_lock);
666         if (dev->reada_curr_zone == NULL) {
667                 ret = reada_pick_zone(dev);
668                 if (!ret) {
669                         spin_unlock(&fs_info->reada_lock);
670                         return 0;
671                 }
672         }
673         /*
674          * FIXME currently we issue the reads one extent at a time. If we have
675          * a contiguous block of extents, we could also coagulate them or use
676          * plugging to speed things up
677          */
678         ret = radix_tree_gang_lookup(&dev->reada_extents, (void **)&re,
679                                      dev->reada_next >> PAGE_SHIFT, 1);
680         if (ret == 0 || re->logical > dev->reada_curr_zone->end) {
681                 ret = reada_pick_zone(dev);
682                 if (!ret) {
683                         spin_unlock(&fs_info->reada_lock);
684                         return 0;
685                 }
686                 re = NULL;
687                 ret = radix_tree_gang_lookup(&dev->reada_extents, (void **)&re,
688                                         dev->reada_next >> PAGE_SHIFT, 1);
689         }
690         if (ret == 0) {
691                 spin_unlock(&fs_info->reada_lock);
692                 return 0;
693         }
694         dev->reada_next = re->logical + fs_info->nodesize;
695         re->refcnt++;
696
697         spin_unlock(&fs_info->reada_lock);
698
699         spin_lock(&re->lock);
700         if (re->scheduled || list_empty(&re->extctl)) {
701                 spin_unlock(&re->lock);
702                 reada_extent_put(fs_info, re);
703                 return 0;
704         }
705         re->scheduled = 1;
706         spin_unlock(&re->lock);
707
708         /*
709          * find mirror num
710          */
711         for (i = 0; i < re->nzones; ++i) {
712                 if (re->zones[i]->device == dev) {
713                         mirror_num = i + 1;
714                         break;
715                 }
716         }
717         logical = re->logical;
718
719         atomic_inc(&dev->reada_in_flight);
720         ret = reada_tree_block_flagged(fs_info, logical, mirror_num, &eb);
721         if (ret)
722                 __readahead_hook(fs_info, re, NULL, ret);
723         else if (eb)
724                 __readahead_hook(fs_info, re, eb, ret);
725
726         if (eb)
727                 free_extent_buffer(eb);
728
729         atomic_dec(&dev->reada_in_flight);
730         reada_extent_put(fs_info, re);
731
732         return 1;
733
734 }
735
736 static void reada_start_machine_worker(struct btrfs_work *work)
737 {
738         struct reada_machine_work *rmw;
739         int old_ioprio;
740
741         rmw = container_of(work, struct reada_machine_work, work);
742
743         old_ioprio = IOPRIO_PRIO_VALUE(task_nice_ioclass(current),
744                                        task_nice_ioprio(current));
745         set_task_ioprio(current, BTRFS_IOPRIO_READA);
746         __reada_start_machine(rmw->fs_info);
747         set_task_ioprio(current, old_ioprio);
748
749         atomic_dec(&rmw->fs_info->reada_works_cnt);
750
751         kfree(rmw);
752 }
753
754 static void __reada_start_machine(struct btrfs_fs_info *fs_info)
755 {
756         struct btrfs_device *device;
757         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
758         u64 enqueued;
759         u64 total = 0;
760         int i;
761
762 again:
763         do {
764                 enqueued = 0;
765                 mutex_lock(&fs_devices->device_list_mutex);
766                 list_for_each_entry(device, &fs_devices->devices, dev_list) {
767                         if (atomic_read(&device->reada_in_flight) <
768                             MAX_IN_FLIGHT)
769                                 enqueued += reada_start_machine_dev(device);
770                 }
771                 mutex_unlock(&fs_devices->device_list_mutex);
772                 total += enqueued;
773         } while (enqueued && total < 10000);
774         if (fs_devices->seed) {
775                 fs_devices = fs_devices->seed;
776                 goto again;
777         }
778
779         if (enqueued == 0)
780                 return;
781
782         /*
783          * If everything is already in the cache, this is effectively single
784          * threaded. To a) not hold the caller for too long and b) to utilize
785          * more cores, we broke the loop above after 10000 iterations and now
786          * enqueue to workers to finish it. This will distribute the load to
787          * the cores.
788          */
789         for (i = 0; i < 2; ++i) {
790                 reada_start_machine(fs_info);
791                 if (atomic_read(&fs_info->reada_works_cnt) >
792                     BTRFS_MAX_MIRRORS * 2)
793                         break;
794         }
795 }
796
797 static void reada_start_machine(struct btrfs_fs_info *fs_info)
798 {
799         struct reada_machine_work *rmw;
800
801         rmw = kzalloc(sizeof(*rmw), GFP_KERNEL);
802         if (!rmw) {
803                 /* FIXME we cannot handle this properly right now */
804                 BUG();
805         }
806         btrfs_init_work(&rmw->work, btrfs_readahead_helper,
807                         reada_start_machine_worker, NULL, NULL);
808         rmw->fs_info = fs_info;
809
810         btrfs_queue_work(fs_info->readahead_workers, &rmw->work);
811         atomic_inc(&fs_info->reada_works_cnt);
812 }
813
814 #ifdef DEBUG
815 static void dump_devs(struct btrfs_fs_info *fs_info, int all)
816 {
817         struct btrfs_device *device;
818         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
819         unsigned long index;
820         int ret;
821         int i;
822         int j;
823         int cnt;
824
825         spin_lock(&fs_info->reada_lock);
826         list_for_each_entry(device, &fs_devices->devices, dev_list) {
827                 btrfs_debug(fs_info, "dev %lld has %d in flight", device->devid,
828                         atomic_read(&device->reada_in_flight));
829                 index = 0;
830                 while (1) {
831                         struct reada_zone *zone;
832                         ret = radix_tree_gang_lookup(&device->reada_zones,
833                                                      (void **)&zone, index, 1);
834                         if (ret == 0)
835                                 break;
836                         pr_debug("  zone %llu-%llu elems %llu locked %d devs",
837                                     zone->start, zone->end, zone->elems,
838                                     zone->locked);
839                         for (j = 0; j < zone->ndevs; ++j) {
840                                 pr_cont(" %lld",
841                                         zone->devs[j]->devid);
842                         }
843                         if (device->reada_curr_zone == zone)
844                                 pr_cont(" curr off %llu",
845                                         device->reada_next - zone->start);
846                         pr_cont("\n");
847                         index = (zone->end >> PAGE_SHIFT) + 1;
848                 }
849                 cnt = 0;
850                 index = 0;
851                 while (all) {
852                         struct reada_extent *re = NULL;
853
854                         ret = radix_tree_gang_lookup(&device->reada_extents,
855                                                      (void **)&re, index, 1);
856                         if (ret == 0)
857                                 break;
858                         pr_debug("  re: logical %llu size %u empty %d scheduled %d",
859                                 re->logical, fs_info->nodesize,
860                                 list_empty(&re->extctl), re->scheduled);
861
862                         for (i = 0; i < re->nzones; ++i) {
863                                 pr_cont(" zone %llu-%llu devs",
864                                         re->zones[i]->start,
865                                         re->zones[i]->end);
866                                 for (j = 0; j < re->zones[i]->ndevs; ++j) {
867                                         pr_cont(" %lld",
868                                                 re->zones[i]->devs[j]->devid);
869                                 }
870                         }
871                         pr_cont("\n");
872                         index = (re->logical >> PAGE_SHIFT) + 1;
873                         if (++cnt > 15)
874                                 break;
875                 }
876         }
877
878         index = 0;
879         cnt = 0;
880         while (all) {
881                 struct reada_extent *re = NULL;
882
883                 ret = radix_tree_gang_lookup(&fs_info->reada_tree, (void **)&re,
884                                              index, 1);
885                 if (ret == 0)
886                         break;
887                 if (!re->scheduled) {
888                         index = (re->logical >> PAGE_SHIFT) + 1;
889                         continue;
890                 }
891                 pr_debug("re: logical %llu size %u list empty %d scheduled %d",
892                         re->logical, fs_info->nodesize,
893                         list_empty(&re->extctl), re->scheduled);
894                 for (i = 0; i < re->nzones; ++i) {
895                         pr_cont(" zone %llu-%llu devs",
896                                 re->zones[i]->start,
897                                 re->zones[i]->end);
898                         for (j = 0; j < re->zones[i]->ndevs; ++j) {
899                                 pr_cont(" %lld",
900                                        re->zones[i]->devs[j]->devid);
901                         }
902                 }
903                 pr_cont("\n");
904                 index = (re->logical >> PAGE_SHIFT) + 1;
905         }
906         spin_unlock(&fs_info->reada_lock);
907 }
908 #endif
909
910 /*
911  * interface
912  */
913 struct reada_control *btrfs_reada_add(struct btrfs_root *root,
914                         struct btrfs_key *key_start, struct btrfs_key *key_end)
915 {
916         struct reada_control *rc;
917         u64 start;
918         u64 generation;
919         int ret;
920         struct extent_buffer *node;
921         static struct btrfs_key max_key = {
922                 .objectid = (u64)-1,
923                 .type = (u8)-1,
924                 .offset = (u64)-1
925         };
926
927         rc = kzalloc(sizeof(*rc), GFP_KERNEL);
928         if (!rc)
929                 return ERR_PTR(-ENOMEM);
930
931         rc->fs_info = root->fs_info;
932         rc->key_start = *key_start;
933         rc->key_end = *key_end;
934         atomic_set(&rc->elems, 0);
935         init_waitqueue_head(&rc->wait);
936         kref_init(&rc->refcnt);
937         kref_get(&rc->refcnt); /* one ref for having elements */
938
939         node = btrfs_root_node(root);
940         start = node->start;
941         generation = btrfs_header_generation(node);
942         free_extent_buffer(node);
943
944         ret = reada_add_block(rc, start, &max_key, generation);
945         if (ret) {
946                 kfree(rc);
947                 return ERR_PTR(ret);
948         }
949
950         reada_start_machine(root->fs_info);
951
952         return rc;
953 }
954
955 #ifdef DEBUG
956 int btrfs_reada_wait(void *handle)
957 {
958         struct reada_control *rc = handle;
959         struct btrfs_fs_info *fs_info = rc->fs_info;
960
961         while (atomic_read(&rc->elems)) {
962                 if (!atomic_read(&fs_info->reada_works_cnt))
963                         reada_start_machine(fs_info);
964                 wait_event_timeout(rc->wait, atomic_read(&rc->elems) == 0,
965                                    5 * HZ);
966                 dump_devs(fs_info, atomic_read(&rc->elems) < 10 ? 1 : 0);
967         }
968
969         dump_devs(fs_info, atomic_read(&rc->elems) < 10 ? 1 : 0);
970
971         kref_put(&rc->refcnt, reada_control_release);
972
973         return 0;
974 }
975 #else
976 int btrfs_reada_wait(void *handle)
977 {
978         struct reada_control *rc = handle;
979         struct btrfs_fs_info *fs_info = rc->fs_info;
980
981         while (atomic_read(&rc->elems)) {
982                 if (!atomic_read(&fs_info->reada_works_cnt))
983                         reada_start_machine(fs_info);
984                 wait_event_timeout(rc->wait, atomic_read(&rc->elems) == 0,
985                                    (HZ + 9) / 10);
986         }
987
988         kref_put(&rc->refcnt, reada_control_release);
989
990         return 0;
991 }
992 #endif
993
994 void btrfs_reada_detach(void *handle)
995 {
996         struct reada_control *rc = handle;
997
998         kref_put(&rc->refcnt, reada_control_release);
999 }