reada.c 24 KB

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