reada.c 24 KB

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