reada.c 24 KB

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