reada.c 24 KB

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