reada.c 24 KB

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