reada.c 24 KB

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