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