reada.c 24 KB

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