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