reada.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. if (!have_zone)
  392. radix_tree_delete(&fs_info->reada_tree, index);
  393. spin_unlock(&fs_info->reada_lock);
  394. btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
  395. if (!have_zone)
  396. goto error;
  397. btrfs_put_bbio(bbio);
  398. return re;
  399. error:
  400. for (nzones = 0; nzones < re->nzones; ++nzones) {
  401. struct reada_zone *zone;
  402. zone = re->zones[nzones];
  403. kref_get(&zone->refcnt);
  404. spin_lock(&zone->lock);
  405. --zone->elems;
  406. if (zone->elems == 0) {
  407. /*
  408. * no fs_info->reada_lock needed, as this can't be
  409. * the last ref
  410. */
  411. kref_put(&zone->refcnt, reada_zone_release);
  412. }
  413. spin_unlock(&zone->lock);
  414. spin_lock(&fs_info->reada_lock);
  415. kref_put(&zone->refcnt, reada_zone_release);
  416. spin_unlock(&fs_info->reada_lock);
  417. }
  418. btrfs_put_bbio(bbio);
  419. kfree(re);
  420. return re_exist;
  421. }
  422. static void reada_extent_put(struct btrfs_fs_info *fs_info,
  423. struct reada_extent *re)
  424. {
  425. int i;
  426. unsigned long index = re->logical >> PAGE_SHIFT;
  427. spin_lock(&fs_info->reada_lock);
  428. if (--re->refcnt) {
  429. spin_unlock(&fs_info->reada_lock);
  430. return;
  431. }
  432. radix_tree_delete(&fs_info->reada_tree, index);
  433. for (i = 0; i < re->nzones; ++i) {
  434. struct reada_zone *zone = re->zones[i];
  435. radix_tree_delete(&zone->device->reada_extents, index);
  436. }
  437. spin_unlock(&fs_info->reada_lock);
  438. for (i = 0; i < re->nzones; ++i) {
  439. struct reada_zone *zone = re->zones[i];
  440. kref_get(&zone->refcnt);
  441. spin_lock(&zone->lock);
  442. --zone->elems;
  443. if (zone->elems == 0) {
  444. /* no fs_info->reada_lock needed, as this can't be
  445. * the last ref */
  446. kref_put(&zone->refcnt, reada_zone_release);
  447. }
  448. spin_unlock(&zone->lock);
  449. spin_lock(&fs_info->reada_lock);
  450. kref_put(&zone->refcnt, reada_zone_release);
  451. spin_unlock(&fs_info->reada_lock);
  452. }
  453. kfree(re);
  454. }
  455. static void reada_zone_release(struct kref *kref)
  456. {
  457. struct reada_zone *zone = container_of(kref, struct reada_zone, refcnt);
  458. radix_tree_delete(&zone->device->reada_zones,
  459. zone->end >> PAGE_SHIFT);
  460. kfree(zone);
  461. }
  462. static void reada_control_release(struct kref *kref)
  463. {
  464. struct reada_control *rc = container_of(kref, struct reada_control,
  465. refcnt);
  466. kfree(rc);
  467. }
  468. static int reada_add_block(struct reada_control *rc, u64 logical,
  469. struct btrfs_key *top, u64 generation)
  470. {
  471. struct btrfs_fs_info *fs_info = rc->fs_info;
  472. struct reada_extent *re;
  473. struct reada_extctl *rec;
  474. /* takes one ref */
  475. re = reada_find_extent(fs_info, logical, top);
  476. if (!re)
  477. return -1;
  478. rec = kzalloc(sizeof(*rec), GFP_KERNEL);
  479. if (!rec) {
  480. reada_extent_put(fs_info, re);
  481. return -ENOMEM;
  482. }
  483. rec->rc = rc;
  484. rec->generation = generation;
  485. atomic_inc(&rc->elems);
  486. spin_lock(&re->lock);
  487. list_add_tail(&rec->list, &re->extctl);
  488. spin_unlock(&re->lock);
  489. /* leave the ref on the extent */
  490. return 0;
  491. }
  492. /*
  493. * called with fs_info->reada_lock held
  494. */
  495. static void reada_peer_zones_set_lock(struct reada_zone *zone, int lock)
  496. {
  497. int i;
  498. unsigned long index = zone->end >> PAGE_SHIFT;
  499. for (i = 0; i < zone->ndevs; ++i) {
  500. struct reada_zone *peer;
  501. peer = radix_tree_lookup(&zone->devs[i]->reada_zones, index);
  502. if (peer && peer->device != zone->device)
  503. peer->locked = lock;
  504. }
  505. }
  506. /*
  507. * called with fs_info->reada_lock held
  508. */
  509. static int reada_pick_zone(struct btrfs_device *dev)
  510. {
  511. struct reada_zone *top_zone = NULL;
  512. struct reada_zone *top_locked_zone = NULL;
  513. u64 top_elems = 0;
  514. u64 top_locked_elems = 0;
  515. unsigned long index = 0;
  516. int ret;
  517. if (dev->reada_curr_zone) {
  518. reada_peer_zones_set_lock(dev->reada_curr_zone, 0);
  519. kref_put(&dev->reada_curr_zone->refcnt, reada_zone_release);
  520. dev->reada_curr_zone = NULL;
  521. }
  522. /* pick the zone with the most elements */
  523. while (1) {
  524. struct reada_zone *zone;
  525. ret = radix_tree_gang_lookup(&dev->reada_zones,
  526. (void **)&zone, index, 1);
  527. if (ret == 0)
  528. break;
  529. index = (zone->end >> PAGE_SHIFT) + 1;
  530. if (zone->locked) {
  531. if (zone->elems > top_locked_elems) {
  532. top_locked_elems = zone->elems;
  533. top_locked_zone = zone;
  534. }
  535. } else {
  536. if (zone->elems > top_elems) {
  537. top_elems = zone->elems;
  538. top_zone = zone;
  539. }
  540. }
  541. }
  542. if (top_zone)
  543. dev->reada_curr_zone = top_zone;
  544. else if (top_locked_zone)
  545. dev->reada_curr_zone = top_locked_zone;
  546. else
  547. return 0;
  548. dev->reada_next = dev->reada_curr_zone->start;
  549. kref_get(&dev->reada_curr_zone->refcnt);
  550. reada_peer_zones_set_lock(dev->reada_curr_zone, 1);
  551. return 1;
  552. }
  553. static int reada_start_machine_dev(struct btrfs_device *dev)
  554. {
  555. struct btrfs_fs_info *fs_info = dev->fs_info;
  556. struct reada_extent *re = NULL;
  557. int mirror_num = 0;
  558. struct extent_buffer *eb = NULL;
  559. u64 logical;
  560. int ret;
  561. int i;
  562. spin_lock(&fs_info->reada_lock);
  563. if (dev->reada_curr_zone == NULL) {
  564. ret = reada_pick_zone(dev);
  565. if (!ret) {
  566. spin_unlock(&fs_info->reada_lock);
  567. return 0;
  568. }
  569. }
  570. /*
  571. * FIXME currently we issue the reads one extent at a time. If we have
  572. * a contiguous block of extents, we could also coagulate them or use
  573. * plugging to speed things up
  574. */
  575. ret = radix_tree_gang_lookup(&dev->reada_extents, (void **)&re,
  576. dev->reada_next >> PAGE_SHIFT, 1);
  577. if (ret == 0 || re->logical > dev->reada_curr_zone->end) {
  578. ret = reada_pick_zone(dev);
  579. if (!ret) {
  580. spin_unlock(&fs_info->reada_lock);
  581. return 0;
  582. }
  583. re = NULL;
  584. ret = radix_tree_gang_lookup(&dev->reada_extents, (void **)&re,
  585. dev->reada_next >> PAGE_SHIFT, 1);
  586. }
  587. if (ret == 0) {
  588. spin_unlock(&fs_info->reada_lock);
  589. return 0;
  590. }
  591. dev->reada_next = re->logical + fs_info->nodesize;
  592. re->refcnt++;
  593. spin_unlock(&fs_info->reada_lock);
  594. spin_lock(&re->lock);
  595. if (re->scheduled || list_empty(&re->extctl)) {
  596. spin_unlock(&re->lock);
  597. reada_extent_put(fs_info, re);
  598. return 0;
  599. }
  600. re->scheduled = 1;
  601. spin_unlock(&re->lock);
  602. /*
  603. * find mirror num
  604. */
  605. for (i = 0; i < re->nzones; ++i) {
  606. if (re->zones[i]->device == dev) {
  607. mirror_num = i + 1;
  608. break;
  609. }
  610. }
  611. logical = re->logical;
  612. atomic_inc(&dev->reada_in_flight);
  613. ret = reada_tree_block_flagged(fs_info, logical, mirror_num, &eb);
  614. if (ret)
  615. __readahead_hook(fs_info, re, NULL, ret);
  616. else if (eb)
  617. __readahead_hook(fs_info, re, eb, ret);
  618. if (eb)
  619. free_extent_buffer(eb);
  620. atomic_dec(&dev->reada_in_flight);
  621. reada_extent_put(fs_info, re);
  622. return 1;
  623. }
  624. static void reada_start_machine_worker(struct btrfs_work *work)
  625. {
  626. struct reada_machine_work *rmw;
  627. int old_ioprio;
  628. rmw = container_of(work, struct reada_machine_work, work);
  629. old_ioprio = IOPRIO_PRIO_VALUE(task_nice_ioclass(current),
  630. task_nice_ioprio(current));
  631. set_task_ioprio(current, BTRFS_IOPRIO_READA);
  632. __reada_start_machine(rmw->fs_info);
  633. set_task_ioprio(current, old_ioprio);
  634. atomic_dec(&rmw->fs_info->reada_works_cnt);
  635. kfree(rmw);
  636. }
  637. static void __reada_start_machine(struct btrfs_fs_info *fs_info)
  638. {
  639. struct btrfs_device *device;
  640. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  641. u64 enqueued;
  642. u64 total = 0;
  643. int i;
  644. again:
  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 (fs_devices->seed) {
  657. fs_devices = fs_devices->seed;
  658. goto again;
  659. }
  660. if (enqueued == 0)
  661. return;
  662. /*
  663. * If everything is already in the cache, this is effectively single
  664. * threaded. To a) not hold the caller for too long and b) to utilize
  665. * more cores, we broke the loop above after 10000 iterations and now
  666. * enqueue to workers to finish it. This will distribute the load to
  667. * the cores.
  668. */
  669. for (i = 0; i < 2; ++i) {
  670. reada_start_machine(fs_info);
  671. if (atomic_read(&fs_info->reada_works_cnt) >
  672. BTRFS_MAX_MIRRORS * 2)
  673. break;
  674. }
  675. }
  676. static void reada_start_machine(struct btrfs_fs_info *fs_info)
  677. {
  678. struct reada_machine_work *rmw;
  679. rmw = kzalloc(sizeof(*rmw), GFP_KERNEL);
  680. if (!rmw) {
  681. /* FIXME we cannot handle this properly right now */
  682. BUG();
  683. }
  684. btrfs_init_work(&rmw->work, btrfs_readahead_helper,
  685. reada_start_machine_worker, NULL, NULL);
  686. rmw->fs_info = fs_info;
  687. btrfs_queue_work(fs_info->readahead_workers, &rmw->work);
  688. atomic_inc(&fs_info->reada_works_cnt);
  689. }
  690. #ifdef DEBUG
  691. static void dump_devs(struct btrfs_fs_info *fs_info, int all)
  692. {
  693. struct btrfs_device *device;
  694. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  695. unsigned long index;
  696. int ret;
  697. int i;
  698. int j;
  699. int cnt;
  700. spin_lock(&fs_info->reada_lock);
  701. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  702. btrfs_debug(fs_info, "dev %lld has %d in flight", device->devid,
  703. atomic_read(&device->reada_in_flight));
  704. index = 0;
  705. while (1) {
  706. struct reada_zone *zone;
  707. ret = radix_tree_gang_lookup(&device->reada_zones,
  708. (void **)&zone, index, 1);
  709. if (ret == 0)
  710. break;
  711. pr_debug(" zone %llu-%llu elems %llu locked %d devs",
  712. zone->start, zone->end, zone->elems,
  713. zone->locked);
  714. for (j = 0; j < zone->ndevs; ++j) {
  715. pr_cont(" %lld",
  716. zone->devs[j]->devid);
  717. }
  718. if (device->reada_curr_zone == zone)
  719. pr_cont(" curr off %llu",
  720. device->reada_next - zone->start);
  721. pr_cont("\n");
  722. index = (zone->end >> PAGE_SHIFT) + 1;
  723. }
  724. cnt = 0;
  725. index = 0;
  726. while (all) {
  727. struct reada_extent *re = NULL;
  728. ret = radix_tree_gang_lookup(&device->reada_extents,
  729. (void **)&re, index, 1);
  730. if (ret == 0)
  731. break;
  732. pr_debug(" re: logical %llu size %u empty %d scheduled %d",
  733. re->logical, fs_info->nodesize,
  734. list_empty(&re->extctl), re->scheduled);
  735. for (i = 0; i < re->nzones; ++i) {
  736. pr_cont(" zone %llu-%llu devs",
  737. re->zones[i]->start,
  738. re->zones[i]->end);
  739. for (j = 0; j < re->zones[i]->ndevs; ++j) {
  740. pr_cont(" %lld",
  741. re->zones[i]->devs[j]->devid);
  742. }
  743. }
  744. pr_cont("\n");
  745. index = (re->logical >> PAGE_SHIFT) + 1;
  746. if (++cnt > 15)
  747. break;
  748. }
  749. }
  750. index = 0;
  751. cnt = 0;
  752. while (all) {
  753. struct reada_extent *re = NULL;
  754. ret = radix_tree_gang_lookup(&fs_info->reada_tree, (void **)&re,
  755. index, 1);
  756. if (ret == 0)
  757. break;
  758. if (!re->scheduled) {
  759. index = (re->logical >> PAGE_SHIFT) + 1;
  760. continue;
  761. }
  762. pr_debug("re: logical %llu size %u list empty %d scheduled %d",
  763. re->logical, fs_info->nodesize,
  764. list_empty(&re->extctl), re->scheduled);
  765. for (i = 0; i < re->nzones; ++i) {
  766. pr_cont(" zone %llu-%llu devs",
  767. re->zones[i]->start,
  768. re->zones[i]->end);
  769. for (j = 0; j < re->zones[i]->ndevs; ++j) {
  770. pr_cont(" %lld",
  771. re->zones[i]->devs[j]->devid);
  772. }
  773. }
  774. pr_cont("\n");
  775. index = (re->logical >> PAGE_SHIFT) + 1;
  776. }
  777. spin_unlock(&fs_info->reada_lock);
  778. }
  779. #endif
  780. /*
  781. * interface
  782. */
  783. struct reada_control *btrfs_reada_add(struct btrfs_root *root,
  784. struct btrfs_key *key_start, struct btrfs_key *key_end)
  785. {
  786. struct reada_control *rc;
  787. u64 start;
  788. u64 generation;
  789. int ret;
  790. struct extent_buffer *node;
  791. static struct btrfs_key max_key = {
  792. .objectid = (u64)-1,
  793. .type = (u8)-1,
  794. .offset = (u64)-1
  795. };
  796. rc = kzalloc(sizeof(*rc), GFP_KERNEL);
  797. if (!rc)
  798. return ERR_PTR(-ENOMEM);
  799. rc->fs_info = root->fs_info;
  800. rc->key_start = *key_start;
  801. rc->key_end = *key_end;
  802. atomic_set(&rc->elems, 0);
  803. init_waitqueue_head(&rc->wait);
  804. kref_init(&rc->refcnt);
  805. kref_get(&rc->refcnt); /* one ref for having elements */
  806. node = btrfs_root_node(root);
  807. start = node->start;
  808. generation = btrfs_header_generation(node);
  809. free_extent_buffer(node);
  810. ret = reada_add_block(rc, start, &max_key, generation);
  811. if (ret) {
  812. kfree(rc);
  813. return ERR_PTR(ret);
  814. }
  815. reada_start_machine(root->fs_info);
  816. return rc;
  817. }
  818. #ifdef DEBUG
  819. int btrfs_reada_wait(void *handle)
  820. {
  821. struct reada_control *rc = handle;
  822. struct btrfs_fs_info *fs_info = rc->fs_info;
  823. while (atomic_read(&rc->elems)) {
  824. if (!atomic_read(&fs_info->reada_works_cnt))
  825. reada_start_machine(fs_info);
  826. wait_event_timeout(rc->wait, atomic_read(&rc->elems) == 0,
  827. 5 * HZ);
  828. dump_devs(fs_info, atomic_read(&rc->elems) < 10 ? 1 : 0);
  829. }
  830. dump_devs(fs_info, atomic_read(&rc->elems) < 10 ? 1 : 0);
  831. kref_put(&rc->refcnt, reada_control_release);
  832. return 0;
  833. }
  834. #else
  835. int btrfs_reada_wait(void *handle)
  836. {
  837. struct reada_control *rc = handle;
  838. struct btrfs_fs_info *fs_info = rc->fs_info;
  839. while (atomic_read(&rc->elems)) {
  840. if (!atomic_read(&fs_info->reada_works_cnt))
  841. reada_start_machine(fs_info);
  842. wait_event_timeout(rc->wait, atomic_read(&rc->elems) == 0,
  843. (HZ + 9) / 10);
  844. }
  845. kref_put(&rc->refcnt, reada_control_release);
  846. return 0;
  847. }
  848. #endif
  849. void btrfs_reada_detach(void *handle)
  850. {
  851. struct reada_control *rc = handle;
  852. kref_put(&rc->refcnt, reada_control_release);
  853. }