blob: 5871306a5a3b89e626e69204b836d639c4f9c9c5 [file] [log] [blame]
Arne Jansen7414a032011-05-23 14:33:49 +02001/*
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"
Stefan Behrens8dabb742012-11-06 13:15:27 +010030#include "dev-replace.h"
Arne Jansen7414a032011-05-23 14:33:49 +020031
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
Arne Jansen7414a032011-05-23 14:33:49 +020058#define MAX_IN_FLIGHT 6
59
60struct reada_extctl {
61 struct list_head list;
62 struct reada_control *rc;
63 u64 generation;
64};
65
66struct reada_extent {
67 u64 logical;
68 struct btrfs_key top;
Arne Jansen7414a032011-05-23 14:33:49 +020069 int err;
70 struct list_head extctl;
Al Viro99621b42012-08-29 16:31:33 -040071 int refcnt;
Arne Jansen7414a032011-05-23 14:33:49 +020072 spinlock_t lock;
Stefan Behrens94598ba2012-03-27 14:21:26 -040073 struct reada_zone *zones[BTRFS_MAX_MIRRORS];
Arne Jansen7414a032011-05-23 14:33:49 +020074 int nzones;
75 struct btrfs_device *scheduled_for;
76};
77
78struct reada_zone {
79 u64 start;
80 u64 end;
81 u64 elems;
82 struct list_head list;
83 spinlock_t lock;
84 int locked;
85 struct btrfs_device *device;
Stefan Behrens94598ba2012-03-27 14:21:26 -040086 struct btrfs_device *devs[BTRFS_MAX_MIRRORS]; /* full list, incl
87 * self */
Arne Jansen7414a032011-05-23 14:33:49 +020088 int ndevs;
89 struct kref refcnt;
90};
91
92struct reada_machine_work {
Qu Wenruod458b052014-02-28 10:46:19 +080093 struct btrfs_work work;
Arne Jansen7414a032011-05-23 14:33:49 +020094 struct btrfs_fs_info *fs_info;
95};
96
97static void reada_extent_put(struct btrfs_fs_info *, struct reada_extent *);
98static void reada_control_release(struct kref *kref);
99static void reada_zone_release(struct kref *kref);
100static void reada_start_machine(struct btrfs_fs_info *fs_info);
101static void __reada_start_machine(struct btrfs_fs_info *fs_info);
102
103static int reada_add_block(struct reada_control *rc, u64 logical,
Zhao Lei1e7970c2015-12-31 20:30:00 +0800104 struct btrfs_key *top, u64 generation);
Arne Jansen7414a032011-05-23 14:33:49 +0200105
106/* recurses */
107/* in case of err, eb might be NULL */
Zhao Lei02873e42015-12-31 22:46:45 +0800108static void __readahead_hook(struct btrfs_fs_info *fs_info,
109 struct reada_extent *re, struct extent_buffer *eb,
110 u64 start, int err)
Arne Jansen7414a032011-05-23 14:33:49 +0200111{
112 int level = 0;
113 int nritems;
114 int i;
115 u64 bytenr;
116 u64 generation;
Arne Jansen7414a032011-05-23 14:33:49 +0200117 struct list_head list;
Arne Jansen7414a032011-05-23 14:33:49 +0200118 struct btrfs_device *for_dev;
119
120 if (eb)
121 level = btrfs_header_level(eb);
122
Arne Jansen7414a032011-05-23 14:33:49 +0200123 spin_lock(&re->lock);
124 /*
125 * just take the full list from the extent. afterwards we
126 * don't need the lock anymore
127 */
128 list_replace_init(&re->extctl, &list);
129 for_dev = re->scheduled_for;
130 re->scheduled_for = NULL;
131 spin_unlock(&re->lock);
132
Zhao Lei57f16e02015-12-31 22:20:59 +0800133 /*
134 * this is the error case, the extent buffer has not been
135 * read correctly. We won't access anything from it and
136 * just cleanup our data structures. Effectively this will
137 * cut the branch below this node from read ahead.
138 */
139 if (err)
140 goto cleanup;
Arne Jansen7414a032011-05-23 14:33:49 +0200141
Zhao Lei57f16e02015-12-31 22:20:59 +0800142 /*
143 * FIXME: currently we just set nritems to 0 if this is a leaf,
144 * effectively ignoring the content. In a next step we could
145 * trigger more readahead depending from the content, e.g.
146 * fetch the checksums for the extents in the leaf.
147 */
148 if (!level)
149 goto cleanup;
150
151 nritems = btrfs_header_nritems(eb);
152 generation = btrfs_header_generation(eb);
Arne Jansen7414a032011-05-23 14:33:49 +0200153 for (i = 0; i < nritems; i++) {
154 struct reada_extctl *rec;
155 u64 n_gen;
156 struct btrfs_key key;
157 struct btrfs_key next_key;
158
159 btrfs_node_key_to_cpu(eb, &key, i);
160 if (i + 1 < nritems)
161 btrfs_node_key_to_cpu(eb, &next_key, i + 1);
162 else
163 next_key = re->top;
164 bytenr = btrfs_node_blockptr(eb, i);
165 n_gen = btrfs_node_ptr_generation(eb, i);
166
167 list_for_each_entry(rec, &list, list) {
168 struct reada_control *rc = rec->rc;
169
170 /*
171 * if the generation doesn't match, just ignore this
172 * extctl. This will probably cut off a branch from
173 * prefetch. Alternatively one could start a new (sub-)
174 * prefetch for this branch, starting again from root.
175 * FIXME: move the generation check out of this loop
176 */
177#ifdef DEBUG
178 if (rec->generation != generation) {
Zhao Lei02873e42015-12-31 22:46:45 +0800179 btrfs_debug(fs_info,
180 "generation mismatch for (%llu,%d,%llu) %llu != %llu",
181 key.objectid, key.type, key.offset,
182 rec->generation, generation);
Arne Jansen7414a032011-05-23 14:33:49 +0200183 }
184#endif
185 if (rec->generation == generation &&
186 btrfs_comp_cpu_keys(&key, &rc->key_end) < 0 &&
187 btrfs_comp_cpu_keys(&next_key, &rc->key_start) > 0)
Zhao Lei1e7970c2015-12-31 20:30:00 +0800188 reada_add_block(rc, bytenr, &next_key, n_gen);
Arne Jansen7414a032011-05-23 14:33:49 +0200189 }
190 }
Zhao Lei57f16e02015-12-31 22:20:59 +0800191
192cleanup:
Arne Jansen7414a032011-05-23 14:33:49 +0200193 /*
194 * free extctl records
195 */
196 while (!list_empty(&list)) {
197 struct reada_control *rc;
198 struct reada_extctl *rec;
199
200 rec = list_first_entry(&list, struct reada_extctl, list);
201 list_del(&rec->list);
202 rc = rec->rc;
203 kfree(rec);
204
205 kref_get(&rc->refcnt);
206 if (atomic_dec_and_test(&rc->elems)) {
207 kref_put(&rc->refcnt, reada_control_release);
208 wake_up(&rc->wait);
209 }
210 kref_put(&rc->refcnt, reada_control_release);
211
212 reada_extent_put(fs_info, re); /* one ref for each entry */
213 }
Zhao Lei6e39dbe2015-12-31 22:09:05 +0800214
Arne Jansen7414a032011-05-23 14:33:49 +0200215 if (for_dev)
216 atomic_dec(&for_dev->reada_in_flight);
217
Zhao Lei6e39dbe2015-12-31 22:09:05 +0800218 return;
Arne Jansen7414a032011-05-23 14:33:49 +0200219}
220
221/*
222 * start is passed separately in case eb in NULL, which may be the case with
223 * failed I/O
224 */
Zhao Lei02873e42015-12-31 22:46:45 +0800225int btree_readahead_hook(struct btrfs_fs_info *fs_info,
226 struct extent_buffer *eb, u64 start, int err)
Arne Jansen7414a032011-05-23 14:33:49 +0200227{
Zhao Lei6e39dbe2015-12-31 22:09:05 +0800228 int ret = 0;
229 struct reada_extent *re;
Arne Jansen7414a032011-05-23 14:33:49 +0200230
Zhao Lei6e39dbe2015-12-31 22:09:05 +0800231 /* find extent */
232 spin_lock(&fs_info->reada_lock);
233 re = radix_tree_lookup(&fs_info->reada_tree,
234 start >> PAGE_CACHE_SHIFT);
235 if (re)
236 re->refcnt++;
237 spin_unlock(&fs_info->reada_lock);
238 if (!re) {
239 ret = -1;
240 goto start_machine;
241 }
Arne Jansen7414a032011-05-23 14:33:49 +0200242
Zhao Lei6e39dbe2015-12-31 22:09:05 +0800243 __readahead_hook(fs_info, re, eb, start, err);
244 reada_extent_put(fs_info, re); /* our ref */
Arne Jansen7414a032011-05-23 14:33:49 +0200245
Zhao Lei6e39dbe2015-12-31 22:09:05 +0800246start_machine:
247 reada_start_machine(fs_info);
Arne Jansen7414a032011-05-23 14:33:49 +0200248 return ret;
249}
250
251static struct reada_zone *reada_find_zone(struct btrfs_fs_info *fs_info,
252 struct btrfs_device *dev, u64 logical,
Ilya Dryomov21ca5432011-11-04 09:41:02 -0400253 struct btrfs_bio *bbio)
Arne Jansen7414a032011-05-23 14:33:49 +0200254{
255 int ret;
Arne Jansen7414a032011-05-23 14:33:49 +0200256 struct reada_zone *zone;
257 struct btrfs_block_group_cache *cache = NULL;
258 u64 start;
259 u64 end;
260 int i;
261
Arne Jansen7414a032011-05-23 14:33:49 +0200262 zone = NULL;
263 spin_lock(&fs_info->reada_lock);
264 ret = radix_tree_gang_lookup(&dev->reada_zones, (void **)&zone,
265 logical >> PAGE_CACHE_SHIFT, 1);
Zhao Leic37f49c2015-12-18 21:48:48 +0800266 if (ret == 1 && logical >= zone->start && logical <= zone->end) {
Arne Jansen7414a032011-05-23 14:33:49 +0200267 kref_get(&zone->refcnt);
Arne Jansen7414a032011-05-23 14:33:49 +0200268 spin_unlock(&fs_info->reada_lock);
Zhao Leic37f49c2015-12-18 21:48:48 +0800269 return zone;
Arne Jansen7414a032011-05-23 14:33:49 +0200270 }
271
Zhao Leic37f49c2015-12-18 21:48:48 +0800272 spin_unlock(&fs_info->reada_lock);
273
Arne Jansen7414a032011-05-23 14:33:49 +0200274 cache = btrfs_lookup_block_group(fs_info, logical);
275 if (!cache)
276 return NULL;
277
278 start = cache->key.objectid;
279 end = start + cache->key.offset - 1;
280 btrfs_put_block_group(cache);
281
282 zone = kzalloc(sizeof(*zone), GFP_NOFS);
283 if (!zone)
284 return NULL;
285
286 zone->start = start;
287 zone->end = end;
288 INIT_LIST_HEAD(&zone->list);
289 spin_lock_init(&zone->lock);
290 zone->locked = 0;
291 kref_init(&zone->refcnt);
292 zone->elems = 0;
293 zone->device = dev; /* our device always sits at index 0 */
Ilya Dryomov21ca5432011-11-04 09:41:02 -0400294 for (i = 0; i < bbio->num_stripes; ++i) {
Arne Jansen7414a032011-05-23 14:33:49 +0200295 /* bounds have already been checked */
Ilya Dryomov21ca5432011-11-04 09:41:02 -0400296 zone->devs[i] = bbio->stripes[i].dev;
Arne Jansen7414a032011-05-23 14:33:49 +0200297 }
Ilya Dryomov21ca5432011-11-04 09:41:02 -0400298 zone->ndevs = bbio->num_stripes;
Arne Jansen7414a032011-05-23 14:33:49 +0200299
300 spin_lock(&fs_info->reada_lock);
301 ret = radix_tree_insert(&dev->reada_zones,
Chris Masona1754232012-02-28 12:42:44 -0500302 (unsigned long)(zone->end >> PAGE_CACHE_SHIFT),
Arne Jansen7414a032011-05-23 14:33:49 +0200303 zone);
Arne Jansen7414a032011-05-23 14:33:49 +0200304
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100305 if (ret == -EEXIST) {
Arne Jansen7414a032011-05-23 14:33:49 +0200306 kfree(zone);
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100307 ret = radix_tree_gang_lookup(&dev->reada_zones, (void **)&zone,
308 logical >> PAGE_CACHE_SHIFT, 1);
Zhao Lei8e9aa512015-12-18 21:56:08 +0800309 if (ret == 1 && logical >= zone->start && logical <= zone->end)
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100310 kref_get(&zone->refcnt);
Zhao Lei8e9aa512015-12-18 21:56:08 +0800311 else
312 zone = NULL;
Arne Jansen7414a032011-05-23 14:33:49 +0200313 }
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100314 spin_unlock(&fs_info->reada_lock);
Arne Jansen7414a032011-05-23 14:33:49 +0200315
316 return zone;
317}
318
319static struct reada_extent *reada_find_extent(struct btrfs_root *root,
320 u64 logical,
Zhao Lei1e7970c2015-12-31 20:30:00 +0800321 struct btrfs_key *top)
Arne Jansen7414a032011-05-23 14:33:49 +0200322{
323 int ret;
Arne Jansen7414a032011-05-23 14:33:49 +0200324 struct reada_extent *re = NULL;
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100325 struct reada_extent *re_exist = NULL;
Arne Jansen7414a032011-05-23 14:33:49 +0200326 struct btrfs_fs_info *fs_info = root->fs_info;
Ilya Dryomov21ca5432011-11-04 09:41:02 -0400327 struct btrfs_bio *bbio = NULL;
Arne Jansen7414a032011-05-23 14:33:49 +0200328 struct btrfs_device *dev;
Arne Jansen207a2322012-02-25 09:09:47 +0100329 struct btrfs_device *prev_dev;
Arne Jansen7414a032011-05-23 14:33:49 +0200330 u32 blocksize;
331 u64 length;
Omar Sandoval7cb2c422015-06-19 11:52:49 -0700332 int real_stripes;
Arne Jansen7414a032011-05-23 14:33:49 +0200333 int nzones = 0;
Arne Jansen7414a032011-05-23 14:33:49 +0200334 unsigned long index = logical >> PAGE_CACHE_SHIFT;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100335 int dev_replace_is_ongoing;
Zhao Lei31945022015-12-31 18:48:54 +0800336 int have_zone = 0;
Arne Jansen7414a032011-05-23 14:33:49 +0200337
Arne Jansen7414a032011-05-23 14:33:49 +0200338 spin_lock(&fs_info->reada_lock);
339 re = radix_tree_lookup(&fs_info->reada_tree, index);
340 if (re)
Al Viro99621b42012-08-29 16:31:33 -0400341 re->refcnt++;
Arne Jansen7414a032011-05-23 14:33:49 +0200342 spin_unlock(&fs_info->reada_lock);
343
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100344 if (re)
Arne Jansen7414a032011-05-23 14:33:49 +0200345 return re;
346
347 re = kzalloc(sizeof(*re), GFP_NOFS);
348 if (!re)
349 return NULL;
350
David Sterba707e8a02014-06-04 19:22:26 +0200351 blocksize = root->nodesize;
Arne Jansen7414a032011-05-23 14:33:49 +0200352 re->logical = logical;
Arne Jansen7414a032011-05-23 14:33:49 +0200353 re->top = *top;
354 INIT_LIST_HEAD(&re->extctl);
355 spin_lock_init(&re->lock);
Al Viro99621b42012-08-29 16:31:33 -0400356 re->refcnt = 1;
Arne Jansen7414a032011-05-23 14:33:49 +0200357
358 /*
359 * map block
360 */
361 length = blocksize;
Stefan Behrens29a8d9a2012-11-06 14:16:24 +0100362 ret = btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, logical, &length,
363 &bbio, 0);
Ilya Dryomov21ca5432011-11-04 09:41:02 -0400364 if (ret || !bbio || length < blocksize)
Arne Jansen7414a032011-05-23 14:33:49 +0200365 goto error;
366
Stefan Behrens94598ba2012-03-27 14:21:26 -0400367 if (bbio->num_stripes > BTRFS_MAX_MIRRORS) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500368 btrfs_err(root->fs_info,
369 "readahead: more than %d copies not supported",
370 BTRFS_MAX_MIRRORS);
Arne Jansen7414a032011-05-23 14:33:49 +0200371 goto error;
372 }
373
Omar Sandoval7cb2c422015-06-19 11:52:49 -0700374 real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
375 for (nzones = 0; nzones < real_stripes; ++nzones) {
Arne Jansen7414a032011-05-23 14:33:49 +0200376 struct reada_zone *zone;
377
Ilya Dryomov21ca5432011-11-04 09:41:02 -0400378 dev = bbio->stripes[nzones].dev;
379 zone = reada_find_zone(fs_info, dev, logical, bbio);
Arne Jansen7414a032011-05-23 14:33:49 +0200380 if (!zone)
Zhao Lei6a159d22015-12-31 18:15:47 +0800381 continue;
Arne Jansen7414a032011-05-23 14:33:49 +0200382
Zhao Lei6a159d22015-12-31 18:15:47 +0800383 re->zones[re->nzones++] = zone;
Arne Jansen7414a032011-05-23 14:33:49 +0200384 spin_lock(&zone->lock);
385 if (!zone->elems)
386 kref_get(&zone->refcnt);
387 ++zone->elems;
388 spin_unlock(&zone->lock);
389 spin_lock(&fs_info->reada_lock);
390 kref_put(&zone->refcnt, reada_zone_release);
391 spin_unlock(&fs_info->reada_lock);
392 }
Zhao Lei6a159d22015-12-31 18:15:47 +0800393 if (re->nzones == 0) {
Arne Jansen7414a032011-05-23 14:33:49 +0200394 /* not a single zone found, error and out */
395 goto error;
396 }
397
398 /* insert extent in reada_tree + all per-device trees, all or nothing */
Stefan Behrens8dabb742012-11-06 13:15:27 +0100399 btrfs_dev_replace_lock(&fs_info->dev_replace);
Arne Jansen7414a032011-05-23 14:33:49 +0200400 spin_lock(&fs_info->reada_lock);
401 ret = radix_tree_insert(&fs_info->reada_tree, index, re);
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100402 if (ret == -EEXIST) {
403 re_exist = radix_tree_lookup(&fs_info->reada_tree, index);
404 BUG_ON(!re_exist);
Al Viro99621b42012-08-29 16:31:33 -0400405 re_exist->refcnt++;
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100406 spin_unlock(&fs_info->reada_lock);
Stefan Behrens8dabb742012-11-06 13:15:27 +0100407 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100408 goto error;
409 }
Arne Jansen7414a032011-05-23 14:33:49 +0200410 if (ret) {
411 spin_unlock(&fs_info->reada_lock);
Stefan Behrens8dabb742012-11-06 13:15:27 +0100412 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansen7414a032011-05-23 14:33:49 +0200413 goto error;
414 }
Arne Jansen207a2322012-02-25 09:09:47 +0100415 prev_dev = NULL;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100416 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(
417 &fs_info->dev_replace);
Zhao Lei6a159d22015-12-31 18:15:47 +0800418 for (nzones = 0; nzones < re->nzones; ++nzones) {
419 dev = re->zones[nzones]->device;
420
Arne Jansen207a2322012-02-25 09:09:47 +0100421 if (dev == prev_dev) {
422 /*
423 * in case of DUP, just add the first zone. As both
424 * are on the same device, there's nothing to gain
425 * from adding both.
426 * Also, it wouldn't work, as the tree is per device
427 * and adding would fail with EEXIST
428 */
429 continue;
430 }
Stefan Behrensff023aa2012-11-06 11:43:11 +0100431 if (!dev->bdev) {
Wang Shilong5fbc7c52014-06-11 10:55:22 +0800432 /*
433 * cannot read ahead on missing device, but for RAID5/6,
434 * REQ_GET_READ_MIRRORS return 1. So don't skip missing
435 * device for such case.
436 */
437 if (nzones > 1)
438 continue;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100439 }
Stefan Behrens8dabb742012-11-06 13:15:27 +0100440 if (dev_replace_is_ongoing &&
441 dev == fs_info->dev_replace.tgtdev) {
442 /*
443 * as this device is selected for reading only as
444 * a last resort, skip it for read ahead.
445 */
446 continue;
447 }
Arne Jansen207a2322012-02-25 09:09:47 +0100448 prev_dev = dev;
Arne Jansen7414a032011-05-23 14:33:49 +0200449 ret = radix_tree_insert(&dev->reada_extents, index, re);
450 if (ret) {
Zhao Lei6a159d22015-12-31 18:15:47 +0800451 while (--nzones >= 0) {
452 dev = re->zones[nzones]->device;
Arne Jansen7414a032011-05-23 14:33:49 +0200453 BUG_ON(dev == NULL);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100454 /* ignore whether the entry was inserted */
Arne Jansen7414a032011-05-23 14:33:49 +0200455 radix_tree_delete(&dev->reada_extents, index);
456 }
457 BUG_ON(fs_info == NULL);
458 radix_tree_delete(&fs_info->reada_tree, index);
459 spin_unlock(&fs_info->reada_lock);
Stefan Behrens8dabb742012-11-06 13:15:27 +0100460 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansen7414a032011-05-23 14:33:49 +0200461 goto error;
462 }
Zhao Lei31945022015-12-31 18:48:54 +0800463 have_zone = 1;
Arne Jansen7414a032011-05-23 14:33:49 +0200464 }
465 spin_unlock(&fs_info->reada_lock);
Stefan Behrens8dabb742012-11-06 13:15:27 +0100466 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansen7414a032011-05-23 14:33:49 +0200467
Zhao Lei31945022015-12-31 18:48:54 +0800468 if (!have_zone)
469 goto error;
470
Zhao Lei6e9606d2015-01-20 15:11:34 +0800471 btrfs_put_bbio(bbio);
Arne Jansen7414a032011-05-23 14:33:49 +0200472 return re;
473
474error:
Zhao Lei6a159d22015-12-31 18:15:47 +0800475 for (nzones = 0; nzones < re->nzones; ++nzones) {
Arne Jansen7414a032011-05-23 14:33:49 +0200476 struct reada_zone *zone;
477
Arne Jansen7414a032011-05-23 14:33:49 +0200478 zone = re->zones[nzones];
479 kref_get(&zone->refcnt);
480 spin_lock(&zone->lock);
481 --zone->elems;
482 if (zone->elems == 0) {
483 /*
484 * no fs_info->reada_lock needed, as this can't be
485 * the last ref
486 */
487 kref_put(&zone->refcnt, reada_zone_release);
488 }
489 spin_unlock(&zone->lock);
490
491 spin_lock(&fs_info->reada_lock);
492 kref_put(&zone->refcnt, reada_zone_release);
493 spin_unlock(&fs_info->reada_lock);
494 }
Zhao Lei6e9606d2015-01-20 15:11:34 +0800495 btrfs_put_bbio(bbio);
Arne Jansen7414a032011-05-23 14:33:49 +0200496 kfree(re);
Arne Jansen8c9c2bf2012-02-25 09:09:30 +0100497 return re_exist;
Arne Jansen7414a032011-05-23 14:33:49 +0200498}
499
Arne Jansen7414a032011-05-23 14:33:49 +0200500static void reada_extent_put(struct btrfs_fs_info *fs_info,
501 struct reada_extent *re)
502{
503 int i;
504 unsigned long index = re->logical >> PAGE_CACHE_SHIFT;
505
506 spin_lock(&fs_info->reada_lock);
Al Viro99621b42012-08-29 16:31:33 -0400507 if (--re->refcnt) {
Arne Jansen7414a032011-05-23 14:33:49 +0200508 spin_unlock(&fs_info->reada_lock);
509 return;
510 }
511
512 radix_tree_delete(&fs_info->reada_tree, index);
513 for (i = 0; i < re->nzones; ++i) {
514 struct reada_zone *zone = re->zones[i];
515
516 radix_tree_delete(&zone->device->reada_extents, index);
517 }
518
519 spin_unlock(&fs_info->reada_lock);
520
521 for (i = 0; i < re->nzones; ++i) {
522 struct reada_zone *zone = re->zones[i];
523
524 kref_get(&zone->refcnt);
525 spin_lock(&zone->lock);
526 --zone->elems;
527 if (zone->elems == 0) {
528 /* no fs_info->reada_lock needed, as this can't be
529 * the last ref */
530 kref_put(&zone->refcnt, reada_zone_release);
531 }
532 spin_unlock(&zone->lock);
533
534 spin_lock(&fs_info->reada_lock);
535 kref_put(&zone->refcnt, reada_zone_release);
536 spin_unlock(&fs_info->reada_lock);
537 }
538 if (re->scheduled_for)
539 atomic_dec(&re->scheduled_for->reada_in_flight);
540
541 kfree(re);
542}
543
544static void reada_zone_release(struct kref *kref)
545{
546 struct reada_zone *zone = container_of(kref, struct reada_zone, refcnt);
547
548 radix_tree_delete(&zone->device->reada_zones,
549 zone->end >> PAGE_CACHE_SHIFT);
550
551 kfree(zone);
552}
553
554static void reada_control_release(struct kref *kref)
555{
556 struct reada_control *rc = container_of(kref, struct reada_control,
557 refcnt);
558
559 kfree(rc);
560}
561
562static int reada_add_block(struct reada_control *rc, u64 logical,
Zhao Lei1e7970c2015-12-31 20:30:00 +0800563 struct btrfs_key *top, u64 generation)
Arne Jansen7414a032011-05-23 14:33:49 +0200564{
565 struct btrfs_root *root = rc->root;
566 struct reada_extent *re;
567 struct reada_extctl *rec;
568
Zhao Lei1e7970c2015-12-31 20:30:00 +0800569 re = reada_find_extent(root, logical, top); /* takes one ref */
Arne Jansen7414a032011-05-23 14:33:49 +0200570 if (!re)
571 return -1;
572
573 rec = kzalloc(sizeof(*rec), GFP_NOFS);
574 if (!rec) {
575 reada_extent_put(root->fs_info, re);
Luis de Bethencourtddd664f2015-10-20 14:56:23 +0100576 return -ENOMEM;
Arne Jansen7414a032011-05-23 14:33:49 +0200577 }
578
579 rec->rc = rc;
580 rec->generation = generation;
581 atomic_inc(&rc->elems);
582
583 spin_lock(&re->lock);
584 list_add_tail(&rec->list, &re->extctl);
585 spin_unlock(&re->lock);
586
587 /* leave the ref on the extent */
588
589 return 0;
590}
591
592/*
593 * called with fs_info->reada_lock held
594 */
595static void reada_peer_zones_set_lock(struct reada_zone *zone, int lock)
596{
597 int i;
598 unsigned long index = zone->end >> PAGE_CACHE_SHIFT;
599
600 for (i = 0; i < zone->ndevs; ++i) {
601 struct reada_zone *peer;
602 peer = radix_tree_lookup(&zone->devs[i]->reada_zones, index);
603 if (peer && peer->device != zone->device)
604 peer->locked = lock;
605 }
606}
607
608/*
609 * called with fs_info->reada_lock held
610 */
611static int reada_pick_zone(struct btrfs_device *dev)
612{
613 struct reada_zone *top_zone = NULL;
614 struct reada_zone *top_locked_zone = NULL;
615 u64 top_elems = 0;
616 u64 top_locked_elems = 0;
617 unsigned long index = 0;
618 int ret;
619
620 if (dev->reada_curr_zone) {
621 reada_peer_zones_set_lock(dev->reada_curr_zone, 0);
622 kref_put(&dev->reada_curr_zone->refcnt, reada_zone_release);
623 dev->reada_curr_zone = NULL;
624 }
625 /* pick the zone with the most elements */
626 while (1) {
627 struct reada_zone *zone;
628
629 ret = radix_tree_gang_lookup(&dev->reada_zones,
630 (void **)&zone, index, 1);
631 if (ret == 0)
632 break;
633 index = (zone->end >> PAGE_CACHE_SHIFT) + 1;
634 if (zone->locked) {
635 if (zone->elems > top_locked_elems) {
636 top_locked_elems = zone->elems;
637 top_locked_zone = zone;
638 }
639 } else {
640 if (zone->elems > top_elems) {
641 top_elems = zone->elems;
642 top_zone = zone;
643 }
644 }
645 }
646 if (top_zone)
647 dev->reada_curr_zone = top_zone;
648 else if (top_locked_zone)
649 dev->reada_curr_zone = top_locked_zone;
650 else
651 return 0;
652
653 dev->reada_next = dev->reada_curr_zone->start;
654 kref_get(&dev->reada_curr_zone->refcnt);
655 reada_peer_zones_set_lock(dev->reada_curr_zone, 1);
656
657 return 1;
658}
659
660static int reada_start_machine_dev(struct btrfs_fs_info *fs_info,
661 struct btrfs_device *dev)
662{
663 struct reada_extent *re = NULL;
664 int mirror_num = 0;
665 struct extent_buffer *eb = NULL;
666 u64 logical;
Arne Jansen7414a032011-05-23 14:33:49 +0200667 int ret;
668 int i;
Arne Jansen7414a032011-05-23 14:33:49 +0200669
670 spin_lock(&fs_info->reada_lock);
671 if (dev->reada_curr_zone == NULL) {
672 ret = reada_pick_zone(dev);
673 if (!ret) {
674 spin_unlock(&fs_info->reada_lock);
675 return 0;
676 }
677 }
678 /*
679 * FIXME currently we issue the reads one extent at a time. If we have
680 * a contiguous block of extents, we could also coagulate them or use
681 * plugging to speed things up
682 */
683 ret = radix_tree_gang_lookup(&dev->reada_extents, (void **)&re,
684 dev->reada_next >> PAGE_CACHE_SHIFT, 1);
Zhao Lei50378532015-12-18 21:33:05 +0800685 if (ret == 0 || re->logical > dev->reada_curr_zone->end) {
Arne Jansen7414a032011-05-23 14:33:49 +0200686 ret = reada_pick_zone(dev);
687 if (!ret) {
688 spin_unlock(&fs_info->reada_lock);
689 return 0;
690 }
691 re = NULL;
692 ret = radix_tree_gang_lookup(&dev->reada_extents, (void **)&re,
693 dev->reada_next >> PAGE_CACHE_SHIFT, 1);
694 }
695 if (ret == 0) {
696 spin_unlock(&fs_info->reada_lock);
697 return 0;
698 }
David Sterbab6ae40e2014-06-15 02:18:25 +0200699 dev->reada_next = re->logical + fs_info->tree_root->nodesize;
Al Viro99621b42012-08-29 16:31:33 -0400700 re->refcnt++;
Arne Jansen7414a032011-05-23 14:33:49 +0200701
702 spin_unlock(&fs_info->reada_lock);
703
Zhao Leia3f7fde2015-12-31 22:57:52 +0800704 spin_lock(&re->lock);
705 if (re->scheduled_for || list_empty(&re->extctl)) {
706 spin_unlock(&re->lock);
707 reada_extent_put(fs_info, re);
708 return 0;
709 }
710 re->scheduled_for = dev;
711 spin_unlock(&re->lock);
712
Arne Jansen7414a032011-05-23 14:33:49 +0200713 /*
714 * find mirror num
715 */
716 for (i = 0; i < re->nzones; ++i) {
717 if (re->zones[i]->device == dev) {
718 mirror_num = i + 1;
719 break;
720 }
721 }
722 logical = re->logical;
Arne Jansen7414a032011-05-23 14:33:49 +0200723
Arne Jansen7414a032011-05-23 14:33:49 +0200724 atomic_inc(&dev->reada_in_flight);
David Sterbab6ae40e2014-06-15 02:18:25 +0200725 ret = reada_tree_block_flagged(fs_info->extent_root, logical,
David Sterbac0dcaa42014-06-15 02:24:21 +0200726 mirror_num, &eb);
Arne Jansen7414a032011-05-23 14:33:49 +0200727 if (ret)
Zhao Lei02873e42015-12-31 22:46:45 +0800728 __readahead_hook(fs_info, re, NULL, logical, ret);
Arne Jansen7414a032011-05-23 14:33:49 +0200729 else if (eb)
Zhao Lei02873e42015-12-31 22:46:45 +0800730 __readahead_hook(fs_info, re, eb, eb->start, ret);
Arne Jansen7414a032011-05-23 14:33:49 +0200731
732 if (eb)
733 free_extent_buffer(eb);
734
Zhao Leib257cf52015-12-31 21:07:17 +0800735 reada_extent_put(fs_info, re);
736
Arne Jansen7414a032011-05-23 14:33:49 +0200737 return 1;
738
739}
740
Qu Wenruod458b052014-02-28 10:46:19 +0800741static void reada_start_machine_worker(struct btrfs_work *work)
Arne Jansen7414a032011-05-23 14:33:49 +0200742{
743 struct reada_machine_work *rmw;
744 struct btrfs_fs_info *fs_info;
Stefan Behrens3d136a12012-02-03 11:20:04 +0100745 int old_ioprio;
Arne Jansen7414a032011-05-23 14:33:49 +0200746
747 rmw = container_of(work, struct reada_machine_work, work);
748 fs_info = rmw->fs_info;
749
750 kfree(rmw);
751
Stefan Behrens3d136a12012-02-03 11:20:04 +0100752 old_ioprio = IOPRIO_PRIO_VALUE(task_nice_ioclass(current),
753 task_nice_ioprio(current));
754 set_task_ioprio(current, BTRFS_IOPRIO_READA);
Arne Jansen7414a032011-05-23 14:33:49 +0200755 __reada_start_machine(fs_info);
Stefan Behrens3d136a12012-02-03 11:20:04 +0100756 set_task_ioprio(current, old_ioprio);
Arne Jansen7414a032011-05-23 14:33:49 +0200757}
758
759static void __reada_start_machine(struct btrfs_fs_info *fs_info)
760{
761 struct btrfs_device *device;
762 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
763 u64 enqueued;
764 u64 total = 0;
765 int i;
766
767 do {
768 enqueued = 0;
769 list_for_each_entry(device, &fs_devices->devices, dev_list) {
770 if (atomic_read(&device->reada_in_flight) <
771 MAX_IN_FLIGHT)
772 enqueued += reada_start_machine_dev(fs_info,
773 device);
774 }
775 total += enqueued;
776 } while (enqueued && total < 10000);
777
778 if (enqueued == 0)
779 return;
780
781 /*
782 * If everything is already in the cache, this is effectively single
783 * threaded. To a) not hold the caller for too long and b) to utilize
784 * more cores, we broke the loop above after 10000 iterations and now
785 * enqueue to workers to finish it. This will distribute the load to
786 * the cores.
787 */
788 for (i = 0; i < 2; ++i)
789 reada_start_machine(fs_info);
790}
791
792static void reada_start_machine(struct btrfs_fs_info *fs_info)
793{
794 struct reada_machine_work *rmw;
795
796 rmw = kzalloc(sizeof(*rmw), GFP_NOFS);
797 if (!rmw) {
798 /* FIXME we cannot handle this properly right now */
799 BUG();
800 }
Liu Bo9e0af232014-08-15 23:36:53 +0800801 btrfs_init_work(&rmw->work, btrfs_readahead_helper,
802 reada_start_machine_worker, NULL, NULL);
Arne Jansen7414a032011-05-23 14:33:49 +0200803 rmw->fs_info = fs_info;
804
Qu Wenruo736cfa12014-02-28 10:46:13 +0800805 btrfs_queue_work(fs_info->readahead_workers, &rmw->work);
Arne Jansen7414a032011-05-23 14:33:49 +0200806}
807
808#ifdef DEBUG
809static void dump_devs(struct btrfs_fs_info *fs_info, int all)
810{
811 struct btrfs_device *device;
812 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
813 unsigned long index;
814 int ret;
815 int i;
816 int j;
817 int cnt;
818
819 spin_lock(&fs_info->reada_lock);
820 list_for_each_entry(device, &fs_devices->devices, dev_list) {
821 printk(KERN_DEBUG "dev %lld has %d in flight\n", device->devid,
822 atomic_read(&device->reada_in_flight));
823 index = 0;
824 while (1) {
825 struct reada_zone *zone;
826 ret = radix_tree_gang_lookup(&device->reada_zones,
827 (void **)&zone, index, 1);
828 if (ret == 0)
829 break;
830 printk(KERN_DEBUG " zone %llu-%llu elems %llu locked "
831 "%d devs", zone->start, zone->end, zone->elems,
832 zone->locked);
833 for (j = 0; j < zone->ndevs; ++j) {
834 printk(KERN_CONT " %lld",
835 zone->devs[j]->devid);
836 }
837 if (device->reada_curr_zone == zone)
838 printk(KERN_CONT " curr off %llu",
839 device->reada_next - zone->start);
840 printk(KERN_CONT "\n");
841 index = (zone->end >> PAGE_CACHE_SHIFT) + 1;
842 }
843 cnt = 0;
844 index = 0;
845 while (all) {
846 struct reada_extent *re = NULL;
847
848 ret = radix_tree_gang_lookup(&device->reada_extents,
849 (void **)&re, index, 1);
850 if (ret == 0)
851 break;
852 printk(KERN_DEBUG
853 " re: logical %llu size %u empty %d for %lld",
David Sterbab6ae40e2014-06-15 02:18:25 +0200854 re->logical, fs_info->tree_root->nodesize,
Arne Jansen7414a032011-05-23 14:33:49 +0200855 list_empty(&re->extctl), re->scheduled_for ?
856 re->scheduled_for->devid : -1);
857
858 for (i = 0; i < re->nzones; ++i) {
859 printk(KERN_CONT " zone %llu-%llu devs",
860 re->zones[i]->start,
861 re->zones[i]->end);
862 for (j = 0; j < re->zones[i]->ndevs; ++j) {
863 printk(KERN_CONT " %lld",
864 re->zones[i]->devs[j]->devid);
865 }
866 }
867 printk(KERN_CONT "\n");
868 index = (re->logical >> PAGE_CACHE_SHIFT) + 1;
869 if (++cnt > 15)
870 break;
871 }
872 }
873
874 index = 0;
875 cnt = 0;
876 while (all) {
877 struct reada_extent *re = NULL;
878
879 ret = radix_tree_gang_lookup(&fs_info->reada_tree, (void **)&re,
880 index, 1);
881 if (ret == 0)
882 break;
883 if (!re->scheduled_for) {
884 index = (re->logical >> PAGE_CACHE_SHIFT) + 1;
885 continue;
886 }
887 printk(KERN_DEBUG
888 "re: logical %llu size %u list empty %d for %lld",
David Sterbab6ae40e2014-06-15 02:18:25 +0200889 re->logical, fs_info->tree_root->nodesize,
890 list_empty(&re->extctl),
Arne Jansen7414a032011-05-23 14:33:49 +0200891 re->scheduled_for ? re->scheduled_for->devid : -1);
892 for (i = 0; i < re->nzones; ++i) {
893 printk(KERN_CONT " zone %llu-%llu devs",
894 re->zones[i]->start,
895 re->zones[i]->end);
Zhao Lei8afd6842015-12-31 22:28:51 +0800896 for (j = 0; j < re->zones[i]->ndevs; ++j) {
897 printk(KERN_CONT " %lld",
898 re->zones[i]->devs[j]->devid);
Arne Jansen7414a032011-05-23 14:33:49 +0200899 }
900 }
901 printk(KERN_CONT "\n");
902 index = (re->logical >> PAGE_CACHE_SHIFT) + 1;
903 }
904 spin_unlock(&fs_info->reada_lock);
905}
906#endif
907
908/*
909 * interface
910 */
911struct reada_control *btrfs_reada_add(struct btrfs_root *root,
912 struct btrfs_key *key_start, struct btrfs_key *key_end)
913{
914 struct reada_control *rc;
915 u64 start;
916 u64 generation;
Luis de Bethencourtddd664f2015-10-20 14:56:23 +0100917 int ret;
Arne Jansen7414a032011-05-23 14:33:49 +0200918 struct extent_buffer *node;
919 static struct btrfs_key max_key = {
920 .objectid = (u64)-1,
921 .type = (u8)-1,
922 .offset = (u64)-1
923 };
924
925 rc = kzalloc(sizeof(*rc), GFP_NOFS);
926 if (!rc)
927 return ERR_PTR(-ENOMEM);
928
929 rc->root = root;
930 rc->key_start = *key_start;
931 rc->key_end = *key_end;
932 atomic_set(&rc->elems, 0);
933 init_waitqueue_head(&rc->wait);
934 kref_init(&rc->refcnt);
935 kref_get(&rc->refcnt); /* one ref for having elements */
936
937 node = btrfs_root_node(root);
938 start = node->start;
Arne Jansen7414a032011-05-23 14:33:49 +0200939 generation = btrfs_header_generation(node);
940 free_extent_buffer(node);
941
Zhao Lei1e7970c2015-12-31 20:30:00 +0800942 ret = reada_add_block(rc, start, &max_key, generation);
Luis de Bethencourtddd664f2015-10-20 14:56:23 +0100943 if (ret) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100944 kfree(rc);
Luis de Bethencourtddd664f2015-10-20 14:56:23 +0100945 return ERR_PTR(ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100946 }
Arne Jansen7414a032011-05-23 14:33:49 +0200947
948 reada_start_machine(root->fs_info);
949
950 return rc;
951}
952
953#ifdef DEBUG
954int btrfs_reada_wait(void *handle)
955{
956 struct reada_control *rc = handle;
957
958 while (atomic_read(&rc->elems)) {
959 wait_event_timeout(rc->wait, atomic_read(&rc->elems) == 0,
960 5 * HZ);
Vincent3c59ccd2013-04-16 08:15:25 +0000961 dump_devs(rc->root->fs_info,
962 atomic_read(&rc->elems) < 10 ? 1 : 0);
Arne Jansen7414a032011-05-23 14:33:49 +0200963 }
964
Vincent3c59ccd2013-04-16 08:15:25 +0000965 dump_devs(rc->root->fs_info, atomic_read(&rc->elems) < 10 ? 1 : 0);
Arne Jansen7414a032011-05-23 14:33:49 +0200966
967 kref_put(&rc->refcnt, reada_control_release);
968
969 return 0;
970}
971#else
972int btrfs_reada_wait(void *handle)
973{
974 struct reada_control *rc = handle;
975
976 while (atomic_read(&rc->elems)) {
977 wait_event(rc->wait, atomic_read(&rc->elems) == 0);
978 }
979
980 kref_put(&rc->refcnt, reada_control_release);
981
982 return 0;
983}
984#endif
985
986void btrfs_reada_detach(void *handle)
987{
988 struct reada_control *rc = handle;
989
990 kref_put(&rc->refcnt, reada_control_release);
991}