reada.c 24 KB

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