reada.c 24 KB

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