reada.c 25 KB

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