raid56.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
  1. /*
  2. * Copyright (C) 2012 Fusion-io All rights reserved.
  3. * Copyright (C) 2012 Intel Corp. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public
  7. * License v2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public
  15. * License along with this program; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 021110-1307, USA.
  18. */
  19. #include <linux/sched.h>
  20. #include <linux/wait.h>
  21. #include <linux/bio.h>
  22. #include <linux/slab.h>
  23. #include <linux/buffer_head.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/random.h>
  26. #include <linux/iocontext.h>
  27. #include <linux/capability.h>
  28. #include <linux/ratelimit.h>
  29. #include <linux/kthread.h>
  30. #include <linux/raid/pq.h>
  31. #include <linux/hash.h>
  32. #include <linux/list_sort.h>
  33. #include <linux/raid/xor.h>
  34. #include <linux/vmalloc.h>
  35. #include <asm/div64.h>
  36. #include "compat.h"
  37. #include "ctree.h"
  38. #include "extent_map.h"
  39. #include "disk-io.h"
  40. #include "transaction.h"
  41. #include "print-tree.h"
  42. #include "volumes.h"
  43. #include "raid56.h"
  44. #include "async-thread.h"
  45. #include "check-integrity.h"
  46. #include "rcu-string.h"
  47. /* set when additional merges to this rbio are not allowed */
  48. #define RBIO_RMW_LOCKED_BIT 1
  49. /*
  50. * set when this rbio is sitting in the hash, but it is just a cache
  51. * of past RMW
  52. */
  53. #define RBIO_CACHE_BIT 2
  54. /*
  55. * set when it is safe to trust the stripe_pages for caching
  56. */
  57. #define RBIO_CACHE_READY_BIT 3
  58. #define RBIO_CACHE_SIZE 1024
  59. struct btrfs_raid_bio {
  60. struct btrfs_fs_info *fs_info;
  61. struct btrfs_bio *bbio;
  62. /*
  63. * logical block numbers for the start of each stripe
  64. * The last one or two are p/q. These are sorted,
  65. * so raid_map[0] is the start of our full stripe
  66. */
  67. u64 *raid_map;
  68. /* while we're doing rmw on a stripe
  69. * we put it into a hash table so we can
  70. * lock the stripe and merge more rbios
  71. * into it.
  72. */
  73. struct list_head hash_list;
  74. /*
  75. * LRU list for the stripe cache
  76. */
  77. struct list_head stripe_cache;
  78. /*
  79. * for scheduling work in the helper threads
  80. */
  81. struct btrfs_work work;
  82. /*
  83. * bio list and bio_list_lock are used
  84. * to add more bios into the stripe
  85. * in hopes of avoiding the full rmw
  86. */
  87. struct bio_list bio_list;
  88. spinlock_t bio_list_lock;
  89. /* also protected by the bio_list_lock, the
  90. * plug list is used by the plugging code
  91. * to collect partial bios while plugged. The
  92. * stripe locking code also uses it to hand off
  93. * the stripe lock to the next pending IO
  94. */
  95. struct list_head plug_list;
  96. /*
  97. * flags that tell us if it is safe to
  98. * merge with this bio
  99. */
  100. unsigned long flags;
  101. /* size of each individual stripe on disk */
  102. int stripe_len;
  103. /* number of data stripes (no p/q) */
  104. int nr_data;
  105. /*
  106. * set if we're doing a parity rebuild
  107. * for a read from higher up, which is handled
  108. * differently from a parity rebuild as part of
  109. * rmw
  110. */
  111. int read_rebuild;
  112. /* first bad stripe */
  113. int faila;
  114. /* second bad stripe (for raid6 use) */
  115. int failb;
  116. /*
  117. * number of pages needed to represent the full
  118. * stripe
  119. */
  120. int nr_pages;
  121. /*
  122. * size of all the bios in the bio_list. This
  123. * helps us decide if the rbio maps to a full
  124. * stripe or not
  125. */
  126. int bio_list_bytes;
  127. atomic_t refs;
  128. /*
  129. * these are two arrays of pointers. We allocate the
  130. * rbio big enough to hold them both and setup their
  131. * locations when the rbio is allocated
  132. */
  133. /* pointers to pages that we allocated for
  134. * reading/writing stripes directly from the disk (including P/Q)
  135. */
  136. struct page **stripe_pages;
  137. /*
  138. * pointers to the pages in the bio_list. Stored
  139. * here for faster lookup
  140. */
  141. struct page **bio_pages;
  142. };
  143. static int __raid56_parity_recover(struct btrfs_raid_bio *rbio);
  144. static noinline void finish_rmw(struct btrfs_raid_bio *rbio);
  145. static void rmw_work(struct btrfs_work *work);
  146. static void read_rebuild_work(struct btrfs_work *work);
  147. static void async_rmw_stripe(struct btrfs_raid_bio *rbio);
  148. static void async_read_rebuild(struct btrfs_raid_bio *rbio);
  149. static int fail_bio_stripe(struct btrfs_raid_bio *rbio, struct bio *bio);
  150. static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed);
  151. static void __free_raid_bio(struct btrfs_raid_bio *rbio);
  152. static void index_rbio_pages(struct btrfs_raid_bio *rbio);
  153. static int alloc_rbio_pages(struct btrfs_raid_bio *rbio);
  154. /*
  155. * the stripe hash table is used for locking, and to collect
  156. * bios in hopes of making a full stripe
  157. */
  158. int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
  159. {
  160. struct btrfs_stripe_hash_table *table;
  161. struct btrfs_stripe_hash_table *x;
  162. struct btrfs_stripe_hash *cur;
  163. struct btrfs_stripe_hash *h;
  164. int num_entries = 1 << BTRFS_STRIPE_HASH_TABLE_BITS;
  165. int i;
  166. int table_size;
  167. if (info->stripe_hash_table)
  168. return 0;
  169. /*
  170. * The table is large, starting with order 4 and can go as high as
  171. * order 7 in case lock debugging is turned on.
  172. *
  173. * Try harder to allocate and fallback to vmalloc to lower the chance
  174. * of a failing mount.
  175. */
  176. table_size = sizeof(*table) + sizeof(*h) * num_entries;
  177. table = kzalloc(table_size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  178. if (!table) {
  179. table = vzalloc(table_size);
  180. if (!table)
  181. return -ENOMEM;
  182. }
  183. spin_lock_init(&table->cache_lock);
  184. INIT_LIST_HEAD(&table->stripe_cache);
  185. h = table->table;
  186. for (i = 0; i < num_entries; i++) {
  187. cur = h + i;
  188. INIT_LIST_HEAD(&cur->hash_list);
  189. spin_lock_init(&cur->lock);
  190. init_waitqueue_head(&cur->wait);
  191. }
  192. x = cmpxchg(&info->stripe_hash_table, NULL, table);
  193. if (x) {
  194. if (is_vmalloc_addr(x))
  195. vfree(x);
  196. else
  197. kfree(x);
  198. }
  199. return 0;
  200. }
  201. /*
  202. * caching an rbio means to copy anything from the
  203. * bio_pages array into the stripe_pages array. We
  204. * use the page uptodate bit in the stripe cache array
  205. * to indicate if it has valid data
  206. *
  207. * once the caching is done, we set the cache ready
  208. * bit.
  209. */
  210. static void cache_rbio_pages(struct btrfs_raid_bio *rbio)
  211. {
  212. int i;
  213. char *s;
  214. char *d;
  215. int ret;
  216. ret = alloc_rbio_pages(rbio);
  217. if (ret)
  218. return;
  219. for (i = 0; i < rbio->nr_pages; i++) {
  220. if (!rbio->bio_pages[i])
  221. continue;
  222. s = kmap(rbio->bio_pages[i]);
  223. d = kmap(rbio->stripe_pages[i]);
  224. memcpy(d, s, PAGE_CACHE_SIZE);
  225. kunmap(rbio->bio_pages[i]);
  226. kunmap(rbio->stripe_pages[i]);
  227. SetPageUptodate(rbio->stripe_pages[i]);
  228. }
  229. set_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  230. }
  231. /*
  232. * we hash on the first logical address of the stripe
  233. */
  234. static int rbio_bucket(struct btrfs_raid_bio *rbio)
  235. {
  236. u64 num = rbio->raid_map[0];
  237. /*
  238. * we shift down quite a bit. We're using byte
  239. * addressing, and most of the lower bits are zeros.
  240. * This tends to upset hash_64, and it consistently
  241. * returns just one or two different values.
  242. *
  243. * shifting off the lower bits fixes things.
  244. */
  245. return hash_64(num >> 16, BTRFS_STRIPE_HASH_TABLE_BITS);
  246. }
  247. /*
  248. * stealing an rbio means taking all the uptodate pages from the stripe
  249. * array in the source rbio and putting them into the destination rbio
  250. */
  251. static void steal_rbio(struct btrfs_raid_bio *src, struct btrfs_raid_bio *dest)
  252. {
  253. int i;
  254. struct page *s;
  255. struct page *d;
  256. if (!test_bit(RBIO_CACHE_READY_BIT, &src->flags))
  257. return;
  258. for (i = 0; i < dest->nr_pages; i++) {
  259. s = src->stripe_pages[i];
  260. if (!s || !PageUptodate(s)) {
  261. continue;
  262. }
  263. d = dest->stripe_pages[i];
  264. if (d)
  265. __free_page(d);
  266. dest->stripe_pages[i] = s;
  267. src->stripe_pages[i] = NULL;
  268. }
  269. }
  270. /*
  271. * merging means we take the bio_list from the victim and
  272. * splice it into the destination. The victim should
  273. * be discarded afterwards.
  274. *
  275. * must be called with dest->rbio_list_lock held
  276. */
  277. static void merge_rbio(struct btrfs_raid_bio *dest,
  278. struct btrfs_raid_bio *victim)
  279. {
  280. bio_list_merge(&dest->bio_list, &victim->bio_list);
  281. dest->bio_list_bytes += victim->bio_list_bytes;
  282. bio_list_init(&victim->bio_list);
  283. }
  284. /*
  285. * used to prune items that are in the cache. The caller
  286. * must hold the hash table lock.
  287. */
  288. static void __remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
  289. {
  290. int bucket = rbio_bucket(rbio);
  291. struct btrfs_stripe_hash_table *table;
  292. struct btrfs_stripe_hash *h;
  293. int freeit = 0;
  294. /*
  295. * check the bit again under the hash table lock.
  296. */
  297. if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
  298. return;
  299. table = rbio->fs_info->stripe_hash_table;
  300. h = table->table + bucket;
  301. /* hold the lock for the bucket because we may be
  302. * removing it from the hash table
  303. */
  304. spin_lock(&h->lock);
  305. /*
  306. * hold the lock for the bio list because we need
  307. * to make sure the bio list is empty
  308. */
  309. spin_lock(&rbio->bio_list_lock);
  310. if (test_and_clear_bit(RBIO_CACHE_BIT, &rbio->flags)) {
  311. list_del_init(&rbio->stripe_cache);
  312. table->cache_size -= 1;
  313. freeit = 1;
  314. /* if the bio list isn't empty, this rbio is
  315. * still involved in an IO. We take it out
  316. * of the cache list, and drop the ref that
  317. * was held for the list.
  318. *
  319. * If the bio_list was empty, we also remove
  320. * the rbio from the hash_table, and drop
  321. * the corresponding ref
  322. */
  323. if (bio_list_empty(&rbio->bio_list)) {
  324. if (!list_empty(&rbio->hash_list)) {
  325. list_del_init(&rbio->hash_list);
  326. atomic_dec(&rbio->refs);
  327. BUG_ON(!list_empty(&rbio->plug_list));
  328. }
  329. }
  330. }
  331. spin_unlock(&rbio->bio_list_lock);
  332. spin_unlock(&h->lock);
  333. if (freeit)
  334. __free_raid_bio(rbio);
  335. }
  336. /*
  337. * prune a given rbio from the cache
  338. */
  339. static void remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
  340. {
  341. struct btrfs_stripe_hash_table *table;
  342. unsigned long flags;
  343. if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
  344. return;
  345. table = rbio->fs_info->stripe_hash_table;
  346. spin_lock_irqsave(&table->cache_lock, flags);
  347. __remove_rbio_from_cache(rbio);
  348. spin_unlock_irqrestore(&table->cache_lock, flags);
  349. }
  350. /*
  351. * remove everything in the cache
  352. */
  353. static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
  354. {
  355. struct btrfs_stripe_hash_table *table;
  356. unsigned long flags;
  357. struct btrfs_raid_bio *rbio;
  358. table = info->stripe_hash_table;
  359. spin_lock_irqsave(&table->cache_lock, flags);
  360. while (!list_empty(&table->stripe_cache)) {
  361. rbio = list_entry(table->stripe_cache.next,
  362. struct btrfs_raid_bio,
  363. stripe_cache);
  364. __remove_rbio_from_cache(rbio);
  365. }
  366. spin_unlock_irqrestore(&table->cache_lock, flags);
  367. }
  368. /*
  369. * remove all cached entries and free the hash table
  370. * used by unmount
  371. */
  372. void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
  373. {
  374. if (!info->stripe_hash_table)
  375. return;
  376. btrfs_clear_rbio_cache(info);
  377. if (is_vmalloc_addr(info->stripe_hash_table))
  378. vfree(info->stripe_hash_table);
  379. else
  380. kfree(info->stripe_hash_table);
  381. info->stripe_hash_table = NULL;
  382. }
  383. /*
  384. * insert an rbio into the stripe cache. It
  385. * must have already been prepared by calling
  386. * cache_rbio_pages
  387. *
  388. * If this rbio was already cached, it gets
  389. * moved to the front of the lru.
  390. *
  391. * If the size of the rbio cache is too big, we
  392. * prune an item.
  393. */
  394. static void cache_rbio(struct btrfs_raid_bio *rbio)
  395. {
  396. struct btrfs_stripe_hash_table *table;
  397. unsigned long flags;
  398. if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
  399. return;
  400. table = rbio->fs_info->stripe_hash_table;
  401. spin_lock_irqsave(&table->cache_lock, flags);
  402. spin_lock(&rbio->bio_list_lock);
  403. /* bump our ref if we were not in the list before */
  404. if (!test_and_set_bit(RBIO_CACHE_BIT, &rbio->flags))
  405. atomic_inc(&rbio->refs);
  406. if (!list_empty(&rbio->stripe_cache)){
  407. list_move(&rbio->stripe_cache, &table->stripe_cache);
  408. } else {
  409. list_add(&rbio->stripe_cache, &table->stripe_cache);
  410. table->cache_size += 1;
  411. }
  412. spin_unlock(&rbio->bio_list_lock);
  413. if (table->cache_size > RBIO_CACHE_SIZE) {
  414. struct btrfs_raid_bio *found;
  415. found = list_entry(table->stripe_cache.prev,
  416. struct btrfs_raid_bio,
  417. stripe_cache);
  418. if (found != rbio)
  419. __remove_rbio_from_cache(found);
  420. }
  421. spin_unlock_irqrestore(&table->cache_lock, flags);
  422. return;
  423. }
  424. /*
  425. * helper function to run the xor_blocks api. It is only
  426. * able to do MAX_XOR_BLOCKS at a time, so we need to
  427. * loop through.
  428. */
  429. static void run_xor(void **pages, int src_cnt, ssize_t len)
  430. {
  431. int src_off = 0;
  432. int xor_src_cnt = 0;
  433. void *dest = pages[src_cnt];
  434. while(src_cnt > 0) {
  435. xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
  436. xor_blocks(xor_src_cnt, len, dest, pages + src_off);
  437. src_cnt -= xor_src_cnt;
  438. src_off += xor_src_cnt;
  439. }
  440. }
  441. /*
  442. * returns true if the bio list inside this rbio
  443. * covers an entire stripe (no rmw required).
  444. * Must be called with the bio list lock held, or
  445. * at a time when you know it is impossible to add
  446. * new bios into the list
  447. */
  448. static int __rbio_is_full(struct btrfs_raid_bio *rbio)
  449. {
  450. unsigned long size = rbio->bio_list_bytes;
  451. int ret = 1;
  452. if (size != rbio->nr_data * rbio->stripe_len)
  453. ret = 0;
  454. BUG_ON(size > rbio->nr_data * rbio->stripe_len);
  455. return ret;
  456. }
  457. static int rbio_is_full(struct btrfs_raid_bio *rbio)
  458. {
  459. unsigned long flags;
  460. int ret;
  461. spin_lock_irqsave(&rbio->bio_list_lock, flags);
  462. ret = __rbio_is_full(rbio);
  463. spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
  464. return ret;
  465. }
  466. /*
  467. * returns 1 if it is safe to merge two rbios together.
  468. * The merging is safe if the two rbios correspond to
  469. * the same stripe and if they are both going in the same
  470. * direction (read vs write), and if neither one is
  471. * locked for final IO
  472. *
  473. * The caller is responsible for locking such that
  474. * rmw_locked is safe to test
  475. */
  476. static int rbio_can_merge(struct btrfs_raid_bio *last,
  477. struct btrfs_raid_bio *cur)
  478. {
  479. if (test_bit(RBIO_RMW_LOCKED_BIT, &last->flags) ||
  480. test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags))
  481. return 0;
  482. /*
  483. * we can't merge with cached rbios, since the
  484. * idea is that when we merge the destination
  485. * rbio is going to run our IO for us. We can
  486. * steal from cached rbio's though, other functions
  487. * handle that.
  488. */
  489. if (test_bit(RBIO_CACHE_BIT, &last->flags) ||
  490. test_bit(RBIO_CACHE_BIT, &cur->flags))
  491. return 0;
  492. if (last->raid_map[0] !=
  493. cur->raid_map[0])
  494. return 0;
  495. /* reads can't merge with writes */
  496. if (last->read_rebuild !=
  497. cur->read_rebuild) {
  498. return 0;
  499. }
  500. return 1;
  501. }
  502. /*
  503. * helper to index into the pstripe
  504. */
  505. static struct page *rbio_pstripe_page(struct btrfs_raid_bio *rbio, int index)
  506. {
  507. index += (rbio->nr_data * rbio->stripe_len) >> PAGE_CACHE_SHIFT;
  508. return rbio->stripe_pages[index];
  509. }
  510. /*
  511. * helper to index into the qstripe, returns null
  512. * if there is no qstripe
  513. */
  514. static struct page *rbio_qstripe_page(struct btrfs_raid_bio *rbio, int index)
  515. {
  516. if (rbio->nr_data + 1 == rbio->bbio->num_stripes)
  517. return NULL;
  518. index += ((rbio->nr_data + 1) * rbio->stripe_len) >>
  519. PAGE_CACHE_SHIFT;
  520. return rbio->stripe_pages[index];
  521. }
  522. /*
  523. * The first stripe in the table for a logical address
  524. * has the lock. rbios are added in one of three ways:
  525. *
  526. * 1) Nobody has the stripe locked yet. The rbio is given
  527. * the lock and 0 is returned. The caller must start the IO
  528. * themselves.
  529. *
  530. * 2) Someone has the stripe locked, but we're able to merge
  531. * with the lock owner. The rbio is freed and the IO will
  532. * start automatically along with the existing rbio. 1 is returned.
  533. *
  534. * 3) Someone has the stripe locked, but we're not able to merge.
  535. * The rbio is added to the lock owner's plug list, or merged into
  536. * an rbio already on the plug list. When the lock owner unlocks,
  537. * the next rbio on the list is run and the IO is started automatically.
  538. * 1 is returned
  539. *
  540. * If we return 0, the caller still owns the rbio and must continue with
  541. * IO submission. If we return 1, the caller must assume the rbio has
  542. * already been freed.
  543. */
  544. static noinline int lock_stripe_add(struct btrfs_raid_bio *rbio)
  545. {
  546. int bucket = rbio_bucket(rbio);
  547. struct btrfs_stripe_hash *h = rbio->fs_info->stripe_hash_table->table + bucket;
  548. struct btrfs_raid_bio *cur;
  549. struct btrfs_raid_bio *pending;
  550. unsigned long flags;
  551. DEFINE_WAIT(wait);
  552. struct btrfs_raid_bio *freeit = NULL;
  553. struct btrfs_raid_bio *cache_drop = NULL;
  554. int ret = 0;
  555. int walk = 0;
  556. spin_lock_irqsave(&h->lock, flags);
  557. list_for_each_entry(cur, &h->hash_list, hash_list) {
  558. walk++;
  559. if (cur->raid_map[0] == rbio->raid_map[0]) {
  560. spin_lock(&cur->bio_list_lock);
  561. /* can we steal this cached rbio's pages? */
  562. if (bio_list_empty(&cur->bio_list) &&
  563. list_empty(&cur->plug_list) &&
  564. test_bit(RBIO_CACHE_BIT, &cur->flags) &&
  565. !test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags)) {
  566. list_del_init(&cur->hash_list);
  567. atomic_dec(&cur->refs);
  568. steal_rbio(cur, rbio);
  569. cache_drop = cur;
  570. spin_unlock(&cur->bio_list_lock);
  571. goto lockit;
  572. }
  573. /* can we merge into the lock owner? */
  574. if (rbio_can_merge(cur, rbio)) {
  575. merge_rbio(cur, rbio);
  576. spin_unlock(&cur->bio_list_lock);
  577. freeit = rbio;
  578. ret = 1;
  579. goto out;
  580. }
  581. /*
  582. * we couldn't merge with the running
  583. * rbio, see if we can merge with the
  584. * pending ones. We don't have to
  585. * check for rmw_locked because there
  586. * is no way they are inside finish_rmw
  587. * right now
  588. */
  589. list_for_each_entry(pending, &cur->plug_list,
  590. plug_list) {
  591. if (rbio_can_merge(pending, rbio)) {
  592. merge_rbio(pending, rbio);
  593. spin_unlock(&cur->bio_list_lock);
  594. freeit = rbio;
  595. ret = 1;
  596. goto out;
  597. }
  598. }
  599. /* no merging, put us on the tail of the plug list,
  600. * our rbio will be started with the currently
  601. * running rbio unlocks
  602. */
  603. list_add_tail(&rbio->plug_list, &cur->plug_list);
  604. spin_unlock(&cur->bio_list_lock);
  605. ret = 1;
  606. goto out;
  607. }
  608. }
  609. lockit:
  610. atomic_inc(&rbio->refs);
  611. list_add(&rbio->hash_list, &h->hash_list);
  612. out:
  613. spin_unlock_irqrestore(&h->lock, flags);
  614. if (cache_drop)
  615. remove_rbio_from_cache(cache_drop);
  616. if (freeit)
  617. __free_raid_bio(freeit);
  618. return ret;
  619. }
  620. /*
  621. * called as rmw or parity rebuild is completed. If the plug list has more
  622. * rbios waiting for this stripe, the next one on the list will be started
  623. */
  624. static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
  625. {
  626. int bucket;
  627. struct btrfs_stripe_hash *h;
  628. unsigned long flags;
  629. int keep_cache = 0;
  630. bucket = rbio_bucket(rbio);
  631. h = rbio->fs_info->stripe_hash_table->table + bucket;
  632. if (list_empty(&rbio->plug_list))
  633. cache_rbio(rbio);
  634. spin_lock_irqsave(&h->lock, flags);
  635. spin_lock(&rbio->bio_list_lock);
  636. if (!list_empty(&rbio->hash_list)) {
  637. /*
  638. * if we're still cached and there is no other IO
  639. * to perform, just leave this rbio here for others
  640. * to steal from later
  641. */
  642. if (list_empty(&rbio->plug_list) &&
  643. test_bit(RBIO_CACHE_BIT, &rbio->flags)) {
  644. keep_cache = 1;
  645. clear_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  646. BUG_ON(!bio_list_empty(&rbio->bio_list));
  647. goto done;
  648. }
  649. list_del_init(&rbio->hash_list);
  650. atomic_dec(&rbio->refs);
  651. /*
  652. * we use the plug list to hold all the rbios
  653. * waiting for the chance to lock this stripe.
  654. * hand the lock over to one of them.
  655. */
  656. if (!list_empty(&rbio->plug_list)) {
  657. struct btrfs_raid_bio *next;
  658. struct list_head *head = rbio->plug_list.next;
  659. next = list_entry(head, struct btrfs_raid_bio,
  660. plug_list);
  661. list_del_init(&rbio->plug_list);
  662. list_add(&next->hash_list, &h->hash_list);
  663. atomic_inc(&next->refs);
  664. spin_unlock(&rbio->bio_list_lock);
  665. spin_unlock_irqrestore(&h->lock, flags);
  666. if (next->read_rebuild)
  667. async_read_rebuild(next);
  668. else {
  669. steal_rbio(rbio, next);
  670. async_rmw_stripe(next);
  671. }
  672. goto done_nolock;
  673. } else if (waitqueue_active(&h->wait)) {
  674. spin_unlock(&rbio->bio_list_lock);
  675. spin_unlock_irqrestore(&h->lock, flags);
  676. wake_up(&h->wait);
  677. goto done_nolock;
  678. }
  679. }
  680. done:
  681. spin_unlock(&rbio->bio_list_lock);
  682. spin_unlock_irqrestore(&h->lock, flags);
  683. done_nolock:
  684. if (!keep_cache)
  685. remove_rbio_from_cache(rbio);
  686. }
  687. static void __free_raid_bio(struct btrfs_raid_bio *rbio)
  688. {
  689. int i;
  690. WARN_ON(atomic_read(&rbio->refs) < 0);
  691. if (!atomic_dec_and_test(&rbio->refs))
  692. return;
  693. WARN_ON(!list_empty(&rbio->stripe_cache));
  694. WARN_ON(!list_empty(&rbio->hash_list));
  695. WARN_ON(!bio_list_empty(&rbio->bio_list));
  696. for (i = 0; i < rbio->nr_pages; i++) {
  697. if (rbio->stripe_pages[i]) {
  698. __free_page(rbio->stripe_pages[i]);
  699. rbio->stripe_pages[i] = NULL;
  700. }
  701. }
  702. kfree(rbio->raid_map);
  703. kfree(rbio->bbio);
  704. kfree(rbio);
  705. }
  706. static void free_raid_bio(struct btrfs_raid_bio *rbio)
  707. {
  708. unlock_stripe(rbio);
  709. __free_raid_bio(rbio);
  710. }
  711. /*
  712. * this frees the rbio and runs through all the bios in the
  713. * bio_list and calls end_io on them
  714. */
  715. static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, int err, int uptodate)
  716. {
  717. struct bio *cur = bio_list_get(&rbio->bio_list);
  718. struct bio *next;
  719. free_raid_bio(rbio);
  720. while (cur) {
  721. next = cur->bi_next;
  722. cur->bi_next = NULL;
  723. if (uptodate)
  724. set_bit(BIO_UPTODATE, &cur->bi_flags);
  725. bio_endio(cur, err);
  726. cur = next;
  727. }
  728. }
  729. /*
  730. * end io function used by finish_rmw. When we finally
  731. * get here, we've written a full stripe
  732. */
  733. static void raid_write_end_io(struct bio *bio, int err)
  734. {
  735. struct btrfs_raid_bio *rbio = bio->bi_private;
  736. if (err)
  737. fail_bio_stripe(rbio, bio);
  738. bio_put(bio);
  739. if (!atomic_dec_and_test(&rbio->bbio->stripes_pending))
  740. return;
  741. err = 0;
  742. /* OK, we have read all the stripes we need to. */
  743. if (atomic_read(&rbio->bbio->error) > rbio->bbio->max_errors)
  744. err = -EIO;
  745. rbio_orig_end_io(rbio, err, 0);
  746. return;
  747. }
  748. /*
  749. * the read/modify/write code wants to use the original bio for
  750. * any pages it included, and then use the rbio for everything
  751. * else. This function decides if a given index (stripe number)
  752. * and page number in that stripe fall inside the original bio
  753. * or the rbio.
  754. *
  755. * if you set bio_list_only, you'll get a NULL back for any ranges
  756. * that are outside the bio_list
  757. *
  758. * This doesn't take any refs on anything, you get a bare page pointer
  759. * and the caller must bump refs as required.
  760. *
  761. * You must call index_rbio_pages once before you can trust
  762. * the answers from this function.
  763. */
  764. static struct page *page_in_rbio(struct btrfs_raid_bio *rbio,
  765. int index, int pagenr, int bio_list_only)
  766. {
  767. int chunk_page;
  768. struct page *p = NULL;
  769. chunk_page = index * (rbio->stripe_len >> PAGE_SHIFT) + pagenr;
  770. spin_lock_irq(&rbio->bio_list_lock);
  771. p = rbio->bio_pages[chunk_page];
  772. spin_unlock_irq(&rbio->bio_list_lock);
  773. if (p || bio_list_only)
  774. return p;
  775. return rbio->stripe_pages[chunk_page];
  776. }
  777. /*
  778. * number of pages we need for the entire stripe across all the
  779. * drives
  780. */
  781. static unsigned long rbio_nr_pages(unsigned long stripe_len, int nr_stripes)
  782. {
  783. unsigned long nr = stripe_len * nr_stripes;
  784. return (nr + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  785. }
  786. /*
  787. * allocation and initial setup for the btrfs_raid_bio. Not
  788. * this does not allocate any pages for rbio->pages.
  789. */
  790. static struct btrfs_raid_bio *alloc_rbio(struct btrfs_root *root,
  791. struct btrfs_bio *bbio, u64 *raid_map,
  792. u64 stripe_len)
  793. {
  794. struct btrfs_raid_bio *rbio;
  795. int nr_data = 0;
  796. int num_pages = rbio_nr_pages(stripe_len, bbio->num_stripes);
  797. void *p;
  798. rbio = kzalloc(sizeof(*rbio) + num_pages * sizeof(struct page *) * 2,
  799. GFP_NOFS);
  800. if (!rbio) {
  801. kfree(raid_map);
  802. kfree(bbio);
  803. return ERR_PTR(-ENOMEM);
  804. }
  805. bio_list_init(&rbio->bio_list);
  806. INIT_LIST_HEAD(&rbio->plug_list);
  807. spin_lock_init(&rbio->bio_list_lock);
  808. INIT_LIST_HEAD(&rbio->stripe_cache);
  809. INIT_LIST_HEAD(&rbio->hash_list);
  810. rbio->bbio = bbio;
  811. rbio->raid_map = raid_map;
  812. rbio->fs_info = root->fs_info;
  813. rbio->stripe_len = stripe_len;
  814. rbio->nr_pages = num_pages;
  815. rbio->faila = -1;
  816. rbio->failb = -1;
  817. atomic_set(&rbio->refs, 1);
  818. /*
  819. * the stripe_pages and bio_pages array point to the extra
  820. * memory we allocated past the end of the rbio
  821. */
  822. p = rbio + 1;
  823. rbio->stripe_pages = p;
  824. rbio->bio_pages = p + sizeof(struct page *) * num_pages;
  825. if (raid_map[bbio->num_stripes - 1] == RAID6_Q_STRIPE)
  826. nr_data = bbio->num_stripes - 2;
  827. else
  828. nr_data = bbio->num_stripes - 1;
  829. rbio->nr_data = nr_data;
  830. return rbio;
  831. }
  832. /* allocate pages for all the stripes in the bio, including parity */
  833. static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
  834. {
  835. int i;
  836. struct page *page;
  837. for (i = 0; i < rbio->nr_pages; i++) {
  838. if (rbio->stripe_pages[i])
  839. continue;
  840. page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  841. if (!page)
  842. return -ENOMEM;
  843. rbio->stripe_pages[i] = page;
  844. ClearPageUptodate(page);
  845. }
  846. return 0;
  847. }
  848. /* allocate pages for just the p/q stripes */
  849. static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
  850. {
  851. int i;
  852. struct page *page;
  853. i = (rbio->nr_data * rbio->stripe_len) >> PAGE_CACHE_SHIFT;
  854. for (; i < rbio->nr_pages; i++) {
  855. if (rbio->stripe_pages[i])
  856. continue;
  857. page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  858. if (!page)
  859. return -ENOMEM;
  860. rbio->stripe_pages[i] = page;
  861. }
  862. return 0;
  863. }
  864. /*
  865. * add a single page from a specific stripe into our list of bios for IO
  866. * this will try to merge into existing bios if possible, and returns
  867. * zero if all went well.
  868. */
  869. static int rbio_add_io_page(struct btrfs_raid_bio *rbio,
  870. struct bio_list *bio_list,
  871. struct page *page,
  872. int stripe_nr,
  873. unsigned long page_index,
  874. unsigned long bio_max_len)
  875. {
  876. struct bio *last = bio_list->tail;
  877. u64 last_end = 0;
  878. int ret;
  879. struct bio *bio;
  880. struct btrfs_bio_stripe *stripe;
  881. u64 disk_start;
  882. stripe = &rbio->bbio->stripes[stripe_nr];
  883. disk_start = stripe->physical + (page_index << PAGE_CACHE_SHIFT);
  884. /* if the device is missing, just fail this stripe */
  885. if (!stripe->dev->bdev)
  886. return fail_rbio_index(rbio, stripe_nr);
  887. /* see if we can add this page onto our existing bio */
  888. if (last) {
  889. last_end = (u64)last->bi_sector << 9;
  890. last_end += last->bi_size;
  891. /*
  892. * we can't merge these if they are from different
  893. * devices or if they are not contiguous
  894. */
  895. if (last_end == disk_start && stripe->dev->bdev &&
  896. test_bit(BIO_UPTODATE, &last->bi_flags) &&
  897. last->bi_bdev == stripe->dev->bdev) {
  898. ret = bio_add_page(last, page, PAGE_CACHE_SIZE, 0);
  899. if (ret == PAGE_CACHE_SIZE)
  900. return 0;
  901. }
  902. }
  903. /* put a new bio on the list */
  904. bio = btrfs_io_bio_alloc(GFP_NOFS, bio_max_len >> PAGE_SHIFT?:1);
  905. if (!bio)
  906. return -ENOMEM;
  907. bio->bi_size = 0;
  908. bio->bi_bdev = stripe->dev->bdev;
  909. bio->bi_sector = disk_start >> 9;
  910. set_bit(BIO_UPTODATE, &bio->bi_flags);
  911. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  912. bio_list_add(bio_list, bio);
  913. return 0;
  914. }
  915. /*
  916. * while we're doing the read/modify/write cycle, we could
  917. * have errors in reading pages off the disk. This checks
  918. * for errors and if we're not able to read the page it'll
  919. * trigger parity reconstruction. The rmw will be finished
  920. * after we've reconstructed the failed stripes
  921. */
  922. static void validate_rbio_for_rmw(struct btrfs_raid_bio *rbio)
  923. {
  924. if (rbio->faila >= 0 || rbio->failb >= 0) {
  925. BUG_ON(rbio->faila == rbio->bbio->num_stripes - 1);
  926. __raid56_parity_recover(rbio);
  927. } else {
  928. finish_rmw(rbio);
  929. }
  930. }
  931. /*
  932. * these are just the pages from the rbio array, not from anything
  933. * the FS sent down to us
  934. */
  935. static struct page *rbio_stripe_page(struct btrfs_raid_bio *rbio, int stripe, int page)
  936. {
  937. int index;
  938. index = stripe * (rbio->stripe_len >> PAGE_CACHE_SHIFT);
  939. index += page;
  940. return rbio->stripe_pages[index];
  941. }
  942. /*
  943. * helper function to walk our bio list and populate the bio_pages array with
  944. * the result. This seems expensive, but it is faster than constantly
  945. * searching through the bio list as we setup the IO in finish_rmw or stripe
  946. * reconstruction.
  947. *
  948. * This must be called before you trust the answers from page_in_rbio
  949. */
  950. static void index_rbio_pages(struct btrfs_raid_bio *rbio)
  951. {
  952. struct bio *bio;
  953. u64 start;
  954. unsigned long stripe_offset;
  955. unsigned long page_index;
  956. struct page *p;
  957. int i;
  958. spin_lock_irq(&rbio->bio_list_lock);
  959. bio_list_for_each(bio, &rbio->bio_list) {
  960. start = (u64)bio->bi_sector << 9;
  961. stripe_offset = start - rbio->raid_map[0];
  962. page_index = stripe_offset >> PAGE_CACHE_SHIFT;
  963. for (i = 0; i < bio->bi_vcnt; i++) {
  964. p = bio->bi_io_vec[i].bv_page;
  965. rbio->bio_pages[page_index + i] = p;
  966. }
  967. }
  968. spin_unlock_irq(&rbio->bio_list_lock);
  969. }
  970. /*
  971. * this is called from one of two situations. We either
  972. * have a full stripe from the higher layers, or we've read all
  973. * the missing bits off disk.
  974. *
  975. * This will calculate the parity and then send down any
  976. * changed blocks.
  977. */
  978. static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
  979. {
  980. struct btrfs_bio *bbio = rbio->bbio;
  981. void *pointers[bbio->num_stripes];
  982. int stripe_len = rbio->stripe_len;
  983. int nr_data = rbio->nr_data;
  984. int stripe;
  985. int pagenr;
  986. int p_stripe = -1;
  987. int q_stripe = -1;
  988. struct bio_list bio_list;
  989. struct bio *bio;
  990. int pages_per_stripe = stripe_len >> PAGE_CACHE_SHIFT;
  991. int ret;
  992. bio_list_init(&bio_list);
  993. if (bbio->num_stripes - rbio->nr_data == 1) {
  994. p_stripe = bbio->num_stripes - 1;
  995. } else if (bbio->num_stripes - rbio->nr_data == 2) {
  996. p_stripe = bbio->num_stripes - 2;
  997. q_stripe = bbio->num_stripes - 1;
  998. } else {
  999. BUG();
  1000. }
  1001. /* at this point we either have a full stripe,
  1002. * or we've read the full stripe from the drive.
  1003. * recalculate the parity and write the new results.
  1004. *
  1005. * We're not allowed to add any new bios to the
  1006. * bio list here, anyone else that wants to
  1007. * change this stripe needs to do their own rmw.
  1008. */
  1009. spin_lock_irq(&rbio->bio_list_lock);
  1010. set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  1011. spin_unlock_irq(&rbio->bio_list_lock);
  1012. atomic_set(&rbio->bbio->error, 0);
  1013. /*
  1014. * now that we've set rmw_locked, run through the
  1015. * bio list one last time and map the page pointers
  1016. *
  1017. * We don't cache full rbios because we're assuming
  1018. * the higher layers are unlikely to use this area of
  1019. * the disk again soon. If they do use it again,
  1020. * hopefully they will send another full bio.
  1021. */
  1022. index_rbio_pages(rbio);
  1023. if (!rbio_is_full(rbio))
  1024. cache_rbio_pages(rbio);
  1025. else
  1026. clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  1027. for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) {
  1028. struct page *p;
  1029. /* first collect one page from each data stripe */
  1030. for (stripe = 0; stripe < nr_data; stripe++) {
  1031. p = page_in_rbio(rbio, stripe, pagenr, 0);
  1032. pointers[stripe] = kmap(p);
  1033. }
  1034. /* then add the parity stripe */
  1035. p = rbio_pstripe_page(rbio, pagenr);
  1036. SetPageUptodate(p);
  1037. pointers[stripe++] = kmap(p);
  1038. if (q_stripe != -1) {
  1039. /*
  1040. * raid6, add the qstripe and call the
  1041. * library function to fill in our p/q
  1042. */
  1043. p = rbio_qstripe_page(rbio, pagenr);
  1044. SetPageUptodate(p);
  1045. pointers[stripe++] = kmap(p);
  1046. raid6_call.gen_syndrome(bbio->num_stripes, PAGE_SIZE,
  1047. pointers);
  1048. } else {
  1049. /* raid5 */
  1050. memcpy(pointers[nr_data], pointers[0], PAGE_SIZE);
  1051. run_xor(pointers + 1, nr_data - 1, PAGE_CACHE_SIZE);
  1052. }
  1053. for (stripe = 0; stripe < bbio->num_stripes; stripe++)
  1054. kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
  1055. }
  1056. /*
  1057. * time to start writing. Make bios for everything from the
  1058. * higher layers (the bio_list in our rbio) and our p/q. Ignore
  1059. * everything else.
  1060. */
  1061. for (stripe = 0; stripe < bbio->num_stripes; stripe++) {
  1062. for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) {
  1063. struct page *page;
  1064. if (stripe < rbio->nr_data) {
  1065. page = page_in_rbio(rbio, stripe, pagenr, 1);
  1066. if (!page)
  1067. continue;
  1068. } else {
  1069. page = rbio_stripe_page(rbio, stripe, pagenr);
  1070. }
  1071. ret = rbio_add_io_page(rbio, &bio_list,
  1072. page, stripe, pagenr, rbio->stripe_len);
  1073. if (ret)
  1074. goto cleanup;
  1075. }
  1076. }
  1077. atomic_set(&bbio->stripes_pending, bio_list_size(&bio_list));
  1078. BUG_ON(atomic_read(&bbio->stripes_pending) == 0);
  1079. while (1) {
  1080. bio = bio_list_pop(&bio_list);
  1081. if (!bio)
  1082. break;
  1083. bio->bi_private = rbio;
  1084. bio->bi_end_io = raid_write_end_io;
  1085. BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
  1086. submit_bio(WRITE, bio);
  1087. }
  1088. return;
  1089. cleanup:
  1090. rbio_orig_end_io(rbio, -EIO, 0);
  1091. }
  1092. /*
  1093. * helper to find the stripe number for a given bio. Used to figure out which
  1094. * stripe has failed. This expects the bio to correspond to a physical disk,
  1095. * so it looks up based on physical sector numbers.
  1096. */
  1097. static int find_bio_stripe(struct btrfs_raid_bio *rbio,
  1098. struct bio *bio)
  1099. {
  1100. u64 physical = bio->bi_sector;
  1101. u64 stripe_start;
  1102. int i;
  1103. struct btrfs_bio_stripe *stripe;
  1104. physical <<= 9;
  1105. for (i = 0; i < rbio->bbio->num_stripes; i++) {
  1106. stripe = &rbio->bbio->stripes[i];
  1107. stripe_start = stripe->physical;
  1108. if (physical >= stripe_start &&
  1109. physical < stripe_start + rbio->stripe_len) {
  1110. return i;
  1111. }
  1112. }
  1113. return -1;
  1114. }
  1115. /*
  1116. * helper to find the stripe number for a given
  1117. * bio (before mapping). Used to figure out which stripe has
  1118. * failed. This looks up based on logical block numbers.
  1119. */
  1120. static int find_logical_bio_stripe(struct btrfs_raid_bio *rbio,
  1121. struct bio *bio)
  1122. {
  1123. u64 logical = bio->bi_sector;
  1124. u64 stripe_start;
  1125. int i;
  1126. logical <<= 9;
  1127. for (i = 0; i < rbio->nr_data; i++) {
  1128. stripe_start = rbio->raid_map[i];
  1129. if (logical >= stripe_start &&
  1130. logical < stripe_start + rbio->stripe_len) {
  1131. return i;
  1132. }
  1133. }
  1134. return -1;
  1135. }
  1136. /*
  1137. * returns -EIO if we had too many failures
  1138. */
  1139. static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed)
  1140. {
  1141. unsigned long flags;
  1142. int ret = 0;
  1143. spin_lock_irqsave(&rbio->bio_list_lock, flags);
  1144. /* we already know this stripe is bad, move on */
  1145. if (rbio->faila == failed || rbio->failb == failed)
  1146. goto out;
  1147. if (rbio->faila == -1) {
  1148. /* first failure on this rbio */
  1149. rbio->faila = failed;
  1150. atomic_inc(&rbio->bbio->error);
  1151. } else if (rbio->failb == -1) {
  1152. /* second failure on this rbio */
  1153. rbio->failb = failed;
  1154. atomic_inc(&rbio->bbio->error);
  1155. } else {
  1156. ret = -EIO;
  1157. }
  1158. out:
  1159. spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
  1160. return ret;
  1161. }
  1162. /*
  1163. * helper to fail a stripe based on a physical disk
  1164. * bio.
  1165. */
  1166. static int fail_bio_stripe(struct btrfs_raid_bio *rbio,
  1167. struct bio *bio)
  1168. {
  1169. int failed = find_bio_stripe(rbio, bio);
  1170. if (failed < 0)
  1171. return -EIO;
  1172. return fail_rbio_index(rbio, failed);
  1173. }
  1174. /*
  1175. * this sets each page in the bio uptodate. It should only be used on private
  1176. * rbio pages, nothing that comes in from the higher layers
  1177. */
  1178. static void set_bio_pages_uptodate(struct bio *bio)
  1179. {
  1180. int i;
  1181. struct page *p;
  1182. for (i = 0; i < bio->bi_vcnt; i++) {
  1183. p = bio->bi_io_vec[i].bv_page;
  1184. SetPageUptodate(p);
  1185. }
  1186. }
  1187. /*
  1188. * end io for the read phase of the rmw cycle. All the bios here are physical
  1189. * stripe bios we've read from the disk so we can recalculate the parity of the
  1190. * stripe.
  1191. *
  1192. * This will usually kick off finish_rmw once all the bios are read in, but it
  1193. * may trigger parity reconstruction if we had any errors along the way
  1194. */
  1195. static void raid_rmw_end_io(struct bio *bio, int err)
  1196. {
  1197. struct btrfs_raid_bio *rbio = bio->bi_private;
  1198. if (err)
  1199. fail_bio_stripe(rbio, bio);
  1200. else
  1201. set_bio_pages_uptodate(bio);
  1202. bio_put(bio);
  1203. if (!atomic_dec_and_test(&rbio->bbio->stripes_pending))
  1204. return;
  1205. err = 0;
  1206. if (atomic_read(&rbio->bbio->error) > rbio->bbio->max_errors)
  1207. goto cleanup;
  1208. /*
  1209. * this will normally call finish_rmw to start our write
  1210. * but if there are any failed stripes we'll reconstruct
  1211. * from parity first
  1212. */
  1213. validate_rbio_for_rmw(rbio);
  1214. return;
  1215. cleanup:
  1216. rbio_orig_end_io(rbio, -EIO, 0);
  1217. }
  1218. static void async_rmw_stripe(struct btrfs_raid_bio *rbio)
  1219. {
  1220. rbio->work.flags = 0;
  1221. rbio->work.func = rmw_work;
  1222. btrfs_queue_worker(&rbio->fs_info->rmw_workers,
  1223. &rbio->work);
  1224. }
  1225. static void async_read_rebuild(struct btrfs_raid_bio *rbio)
  1226. {
  1227. rbio->work.flags = 0;
  1228. rbio->work.func = read_rebuild_work;
  1229. btrfs_queue_worker(&rbio->fs_info->rmw_workers,
  1230. &rbio->work);
  1231. }
  1232. /*
  1233. * the stripe must be locked by the caller. It will
  1234. * unlock after all the writes are done
  1235. */
  1236. static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
  1237. {
  1238. int bios_to_read = 0;
  1239. struct btrfs_bio *bbio = rbio->bbio;
  1240. struct bio_list bio_list;
  1241. int ret;
  1242. int nr_pages = (rbio->stripe_len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1243. int pagenr;
  1244. int stripe;
  1245. struct bio *bio;
  1246. bio_list_init(&bio_list);
  1247. ret = alloc_rbio_pages(rbio);
  1248. if (ret)
  1249. goto cleanup;
  1250. index_rbio_pages(rbio);
  1251. atomic_set(&rbio->bbio->error, 0);
  1252. /*
  1253. * build a list of bios to read all the missing parts of this
  1254. * stripe
  1255. */
  1256. for (stripe = 0; stripe < rbio->nr_data; stripe++) {
  1257. for (pagenr = 0; pagenr < nr_pages; pagenr++) {
  1258. struct page *page;
  1259. /*
  1260. * we want to find all the pages missing from
  1261. * the rbio and read them from the disk. If
  1262. * page_in_rbio finds a page in the bio list
  1263. * we don't need to read it off the stripe.
  1264. */
  1265. page = page_in_rbio(rbio, stripe, pagenr, 1);
  1266. if (page)
  1267. continue;
  1268. page = rbio_stripe_page(rbio, stripe, pagenr);
  1269. /*
  1270. * the bio cache may have handed us an uptodate
  1271. * page. If so, be happy and use it
  1272. */
  1273. if (PageUptodate(page))
  1274. continue;
  1275. ret = rbio_add_io_page(rbio, &bio_list, page,
  1276. stripe, pagenr, rbio->stripe_len);
  1277. if (ret)
  1278. goto cleanup;
  1279. }
  1280. }
  1281. bios_to_read = bio_list_size(&bio_list);
  1282. if (!bios_to_read) {
  1283. /*
  1284. * this can happen if others have merged with
  1285. * us, it means there is nothing left to read.
  1286. * But if there are missing devices it may not be
  1287. * safe to do the full stripe write yet.
  1288. */
  1289. goto finish;
  1290. }
  1291. /*
  1292. * the bbio may be freed once we submit the last bio. Make sure
  1293. * not to touch it after that
  1294. */
  1295. atomic_set(&bbio->stripes_pending, bios_to_read);
  1296. while (1) {
  1297. bio = bio_list_pop(&bio_list);
  1298. if (!bio)
  1299. break;
  1300. bio->bi_private = rbio;
  1301. bio->bi_end_io = raid_rmw_end_io;
  1302. btrfs_bio_wq_end_io(rbio->fs_info, bio,
  1303. BTRFS_WQ_ENDIO_RAID56);
  1304. BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
  1305. submit_bio(READ, bio);
  1306. }
  1307. /* the actual write will happen once the reads are done */
  1308. return 0;
  1309. cleanup:
  1310. rbio_orig_end_io(rbio, -EIO, 0);
  1311. return -EIO;
  1312. finish:
  1313. validate_rbio_for_rmw(rbio);
  1314. return 0;
  1315. }
  1316. /*
  1317. * if the upper layers pass in a full stripe, we thank them by only allocating
  1318. * enough pages to hold the parity, and sending it all down quickly.
  1319. */
  1320. static int full_stripe_write(struct btrfs_raid_bio *rbio)
  1321. {
  1322. int ret;
  1323. ret = alloc_rbio_parity_pages(rbio);
  1324. if (ret) {
  1325. __free_raid_bio(rbio);
  1326. return ret;
  1327. }
  1328. ret = lock_stripe_add(rbio);
  1329. if (ret == 0)
  1330. finish_rmw(rbio);
  1331. return 0;
  1332. }
  1333. /*
  1334. * partial stripe writes get handed over to async helpers.
  1335. * We're really hoping to merge a few more writes into this
  1336. * rbio before calculating new parity
  1337. */
  1338. static int partial_stripe_write(struct btrfs_raid_bio *rbio)
  1339. {
  1340. int ret;
  1341. ret = lock_stripe_add(rbio);
  1342. if (ret == 0)
  1343. async_rmw_stripe(rbio);
  1344. return 0;
  1345. }
  1346. /*
  1347. * sometimes while we were reading from the drive to
  1348. * recalculate parity, enough new bios come into create
  1349. * a full stripe. So we do a check here to see if we can
  1350. * go directly to finish_rmw
  1351. */
  1352. static int __raid56_parity_write(struct btrfs_raid_bio *rbio)
  1353. {
  1354. /* head off into rmw land if we don't have a full stripe */
  1355. if (!rbio_is_full(rbio))
  1356. return partial_stripe_write(rbio);
  1357. return full_stripe_write(rbio);
  1358. }
  1359. /*
  1360. * We use plugging call backs to collect full stripes.
  1361. * Any time we get a partial stripe write while plugged
  1362. * we collect it into a list. When the unplug comes down,
  1363. * we sort the list by logical block number and merge
  1364. * everything we can into the same rbios
  1365. */
  1366. struct btrfs_plug_cb {
  1367. struct blk_plug_cb cb;
  1368. struct btrfs_fs_info *info;
  1369. struct list_head rbio_list;
  1370. struct btrfs_work work;
  1371. };
  1372. /*
  1373. * rbios on the plug list are sorted for easier merging.
  1374. */
  1375. static int plug_cmp(void *priv, struct list_head *a, struct list_head *b)
  1376. {
  1377. struct btrfs_raid_bio *ra = container_of(a, struct btrfs_raid_bio,
  1378. plug_list);
  1379. struct btrfs_raid_bio *rb = container_of(b, struct btrfs_raid_bio,
  1380. plug_list);
  1381. u64 a_sector = ra->bio_list.head->bi_sector;
  1382. u64 b_sector = rb->bio_list.head->bi_sector;
  1383. if (a_sector < b_sector)
  1384. return -1;
  1385. if (a_sector > b_sector)
  1386. return 1;
  1387. return 0;
  1388. }
  1389. static void run_plug(struct btrfs_plug_cb *plug)
  1390. {
  1391. struct btrfs_raid_bio *cur;
  1392. struct btrfs_raid_bio *last = NULL;
  1393. /*
  1394. * sort our plug list then try to merge
  1395. * everything we can in hopes of creating full
  1396. * stripes.
  1397. */
  1398. list_sort(NULL, &plug->rbio_list, plug_cmp);
  1399. while (!list_empty(&plug->rbio_list)) {
  1400. cur = list_entry(plug->rbio_list.next,
  1401. struct btrfs_raid_bio, plug_list);
  1402. list_del_init(&cur->plug_list);
  1403. if (rbio_is_full(cur)) {
  1404. /* we have a full stripe, send it down */
  1405. full_stripe_write(cur);
  1406. continue;
  1407. }
  1408. if (last) {
  1409. if (rbio_can_merge(last, cur)) {
  1410. merge_rbio(last, cur);
  1411. __free_raid_bio(cur);
  1412. continue;
  1413. }
  1414. __raid56_parity_write(last);
  1415. }
  1416. last = cur;
  1417. }
  1418. if (last) {
  1419. __raid56_parity_write(last);
  1420. }
  1421. kfree(plug);
  1422. }
  1423. /*
  1424. * if the unplug comes from schedule, we have to push the
  1425. * work off to a helper thread
  1426. */
  1427. static void unplug_work(struct btrfs_work *work)
  1428. {
  1429. struct btrfs_plug_cb *plug;
  1430. plug = container_of(work, struct btrfs_plug_cb, work);
  1431. run_plug(plug);
  1432. }
  1433. static void btrfs_raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
  1434. {
  1435. struct btrfs_plug_cb *plug;
  1436. plug = container_of(cb, struct btrfs_plug_cb, cb);
  1437. if (from_schedule) {
  1438. plug->work.flags = 0;
  1439. plug->work.func = unplug_work;
  1440. btrfs_queue_worker(&plug->info->rmw_workers,
  1441. &plug->work);
  1442. return;
  1443. }
  1444. run_plug(plug);
  1445. }
  1446. /*
  1447. * our main entry point for writes from the rest of the FS.
  1448. */
  1449. int raid56_parity_write(struct btrfs_root *root, struct bio *bio,
  1450. struct btrfs_bio *bbio, u64 *raid_map,
  1451. u64 stripe_len)
  1452. {
  1453. struct btrfs_raid_bio *rbio;
  1454. struct btrfs_plug_cb *plug = NULL;
  1455. struct blk_plug_cb *cb;
  1456. rbio = alloc_rbio(root, bbio, raid_map, stripe_len);
  1457. if (IS_ERR(rbio))
  1458. return PTR_ERR(rbio);
  1459. bio_list_add(&rbio->bio_list, bio);
  1460. rbio->bio_list_bytes = bio->bi_size;
  1461. /*
  1462. * don't plug on full rbios, just get them out the door
  1463. * as quickly as we can
  1464. */
  1465. if (rbio_is_full(rbio))
  1466. return full_stripe_write(rbio);
  1467. cb = blk_check_plugged(btrfs_raid_unplug, root->fs_info,
  1468. sizeof(*plug));
  1469. if (cb) {
  1470. plug = container_of(cb, struct btrfs_plug_cb, cb);
  1471. if (!plug->info) {
  1472. plug->info = root->fs_info;
  1473. INIT_LIST_HEAD(&plug->rbio_list);
  1474. }
  1475. list_add_tail(&rbio->plug_list, &plug->rbio_list);
  1476. } else {
  1477. return __raid56_parity_write(rbio);
  1478. }
  1479. return 0;
  1480. }
  1481. /*
  1482. * all parity reconstruction happens here. We've read in everything
  1483. * we can find from the drives and this does the heavy lifting of
  1484. * sorting the good from the bad.
  1485. */
  1486. static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
  1487. {
  1488. int pagenr, stripe;
  1489. void **pointers;
  1490. int faila = -1, failb = -1;
  1491. int nr_pages = (rbio->stripe_len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1492. struct page *page;
  1493. int err;
  1494. int i;
  1495. pointers = kzalloc(rbio->bbio->num_stripes * sizeof(void *),
  1496. GFP_NOFS);
  1497. if (!pointers) {
  1498. err = -ENOMEM;
  1499. goto cleanup_io;
  1500. }
  1501. faila = rbio->faila;
  1502. failb = rbio->failb;
  1503. if (rbio->read_rebuild) {
  1504. spin_lock_irq(&rbio->bio_list_lock);
  1505. set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  1506. spin_unlock_irq(&rbio->bio_list_lock);
  1507. }
  1508. index_rbio_pages(rbio);
  1509. for (pagenr = 0; pagenr < nr_pages; pagenr++) {
  1510. /* setup our array of pointers with pages
  1511. * from each stripe
  1512. */
  1513. for (stripe = 0; stripe < rbio->bbio->num_stripes; stripe++) {
  1514. /*
  1515. * if we're rebuilding a read, we have to use
  1516. * pages from the bio list
  1517. */
  1518. if (rbio->read_rebuild &&
  1519. (stripe == faila || stripe == failb)) {
  1520. page = page_in_rbio(rbio, stripe, pagenr, 0);
  1521. } else {
  1522. page = rbio_stripe_page(rbio, stripe, pagenr);
  1523. }
  1524. pointers[stripe] = kmap(page);
  1525. }
  1526. /* all raid6 handling here */
  1527. if (rbio->raid_map[rbio->bbio->num_stripes - 1] ==
  1528. RAID6_Q_STRIPE) {
  1529. /*
  1530. * single failure, rebuild from parity raid5
  1531. * style
  1532. */
  1533. if (failb < 0) {
  1534. if (faila == rbio->nr_data) {
  1535. /*
  1536. * Just the P stripe has failed, without
  1537. * a bad data or Q stripe.
  1538. * TODO, we should redo the xor here.
  1539. */
  1540. err = -EIO;
  1541. goto cleanup;
  1542. }
  1543. /*
  1544. * a single failure in raid6 is rebuilt
  1545. * in the pstripe code below
  1546. */
  1547. goto pstripe;
  1548. }
  1549. /* make sure our ps and qs are in order */
  1550. if (faila > failb) {
  1551. int tmp = failb;
  1552. failb = faila;
  1553. faila = tmp;
  1554. }
  1555. /* if the q stripe is failed, do a pstripe reconstruction
  1556. * from the xors.
  1557. * If both the q stripe and the P stripe are failed, we're
  1558. * here due to a crc mismatch and we can't give them the
  1559. * data they want
  1560. */
  1561. if (rbio->raid_map[failb] == RAID6_Q_STRIPE) {
  1562. if (rbio->raid_map[faila] == RAID5_P_STRIPE) {
  1563. err = -EIO;
  1564. goto cleanup;
  1565. }
  1566. /*
  1567. * otherwise we have one bad data stripe and
  1568. * a good P stripe. raid5!
  1569. */
  1570. goto pstripe;
  1571. }
  1572. if (rbio->raid_map[failb] == RAID5_P_STRIPE) {
  1573. raid6_datap_recov(rbio->bbio->num_stripes,
  1574. PAGE_SIZE, faila, pointers);
  1575. } else {
  1576. raid6_2data_recov(rbio->bbio->num_stripes,
  1577. PAGE_SIZE, faila, failb,
  1578. pointers);
  1579. }
  1580. } else {
  1581. void *p;
  1582. /* rebuild from P stripe here (raid5 or raid6) */
  1583. BUG_ON(failb != -1);
  1584. pstripe:
  1585. /* Copy parity block into failed block to start with */
  1586. memcpy(pointers[faila],
  1587. pointers[rbio->nr_data],
  1588. PAGE_CACHE_SIZE);
  1589. /* rearrange the pointer array */
  1590. p = pointers[faila];
  1591. for (stripe = faila; stripe < rbio->nr_data - 1; stripe++)
  1592. pointers[stripe] = pointers[stripe + 1];
  1593. pointers[rbio->nr_data - 1] = p;
  1594. /* xor in the rest */
  1595. run_xor(pointers, rbio->nr_data - 1, PAGE_CACHE_SIZE);
  1596. }
  1597. /* if we're doing this rebuild as part of an rmw, go through
  1598. * and set all of our private rbio pages in the
  1599. * failed stripes as uptodate. This way finish_rmw will
  1600. * know they can be trusted. If this was a read reconstruction,
  1601. * other endio functions will fiddle the uptodate bits
  1602. */
  1603. if (!rbio->read_rebuild) {
  1604. for (i = 0; i < nr_pages; i++) {
  1605. if (faila != -1) {
  1606. page = rbio_stripe_page(rbio, faila, i);
  1607. SetPageUptodate(page);
  1608. }
  1609. if (failb != -1) {
  1610. page = rbio_stripe_page(rbio, failb, i);
  1611. SetPageUptodate(page);
  1612. }
  1613. }
  1614. }
  1615. for (stripe = 0; stripe < rbio->bbio->num_stripes; stripe++) {
  1616. /*
  1617. * if we're rebuilding a read, we have to use
  1618. * pages from the bio list
  1619. */
  1620. if (rbio->read_rebuild &&
  1621. (stripe == faila || stripe == failb)) {
  1622. page = page_in_rbio(rbio, stripe, pagenr, 0);
  1623. } else {
  1624. page = rbio_stripe_page(rbio, stripe, pagenr);
  1625. }
  1626. kunmap(page);
  1627. }
  1628. }
  1629. err = 0;
  1630. cleanup:
  1631. kfree(pointers);
  1632. cleanup_io:
  1633. if (rbio->read_rebuild) {
  1634. if (err == 0)
  1635. cache_rbio_pages(rbio);
  1636. else
  1637. clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  1638. rbio_orig_end_io(rbio, err, err == 0);
  1639. } else if (err == 0) {
  1640. rbio->faila = -1;
  1641. rbio->failb = -1;
  1642. finish_rmw(rbio);
  1643. } else {
  1644. rbio_orig_end_io(rbio, err, 0);
  1645. }
  1646. }
  1647. /*
  1648. * This is called only for stripes we've read from disk to
  1649. * reconstruct the parity.
  1650. */
  1651. static void raid_recover_end_io(struct bio *bio, int err)
  1652. {
  1653. struct btrfs_raid_bio *rbio = bio->bi_private;
  1654. /*
  1655. * we only read stripe pages off the disk, set them
  1656. * up to date if there were no errors
  1657. */
  1658. if (err)
  1659. fail_bio_stripe(rbio, bio);
  1660. else
  1661. set_bio_pages_uptodate(bio);
  1662. bio_put(bio);
  1663. if (!atomic_dec_and_test(&rbio->bbio->stripes_pending))
  1664. return;
  1665. if (atomic_read(&rbio->bbio->error) > rbio->bbio->max_errors)
  1666. rbio_orig_end_io(rbio, -EIO, 0);
  1667. else
  1668. __raid_recover_end_io(rbio);
  1669. }
  1670. /*
  1671. * reads everything we need off the disk to reconstruct
  1672. * the parity. endio handlers trigger final reconstruction
  1673. * when the IO is done.
  1674. *
  1675. * This is used both for reads from the higher layers and for
  1676. * parity construction required to finish a rmw cycle.
  1677. */
  1678. static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
  1679. {
  1680. int bios_to_read = 0;
  1681. struct btrfs_bio *bbio = rbio->bbio;
  1682. struct bio_list bio_list;
  1683. int ret;
  1684. int nr_pages = (rbio->stripe_len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1685. int pagenr;
  1686. int stripe;
  1687. struct bio *bio;
  1688. bio_list_init(&bio_list);
  1689. ret = alloc_rbio_pages(rbio);
  1690. if (ret)
  1691. goto cleanup;
  1692. atomic_set(&rbio->bbio->error, 0);
  1693. /*
  1694. * read everything that hasn't failed. Thanks to the
  1695. * stripe cache, it is possible that some or all of these
  1696. * pages are going to be uptodate.
  1697. */
  1698. for (stripe = 0; stripe < bbio->num_stripes; stripe++) {
  1699. if (rbio->faila == stripe ||
  1700. rbio->failb == stripe)
  1701. continue;
  1702. for (pagenr = 0; pagenr < nr_pages; pagenr++) {
  1703. struct page *p;
  1704. /*
  1705. * the rmw code may have already read this
  1706. * page in
  1707. */
  1708. p = rbio_stripe_page(rbio, stripe, pagenr);
  1709. if (PageUptodate(p))
  1710. continue;
  1711. ret = rbio_add_io_page(rbio, &bio_list,
  1712. rbio_stripe_page(rbio, stripe, pagenr),
  1713. stripe, pagenr, rbio->stripe_len);
  1714. if (ret < 0)
  1715. goto cleanup;
  1716. }
  1717. }
  1718. bios_to_read = bio_list_size(&bio_list);
  1719. if (!bios_to_read) {
  1720. /*
  1721. * we might have no bios to read just because the pages
  1722. * were up to date, or we might have no bios to read because
  1723. * the devices were gone.
  1724. */
  1725. if (atomic_read(&rbio->bbio->error) <= rbio->bbio->max_errors) {
  1726. __raid_recover_end_io(rbio);
  1727. goto out;
  1728. } else {
  1729. goto cleanup;
  1730. }
  1731. }
  1732. /*
  1733. * the bbio may be freed once we submit the last bio. Make sure
  1734. * not to touch it after that
  1735. */
  1736. atomic_set(&bbio->stripes_pending, bios_to_read);
  1737. while (1) {
  1738. bio = bio_list_pop(&bio_list);
  1739. if (!bio)
  1740. break;
  1741. bio->bi_private = rbio;
  1742. bio->bi_end_io = raid_recover_end_io;
  1743. btrfs_bio_wq_end_io(rbio->fs_info, bio,
  1744. BTRFS_WQ_ENDIO_RAID56);
  1745. BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
  1746. submit_bio(READ, bio);
  1747. }
  1748. out:
  1749. return 0;
  1750. cleanup:
  1751. if (rbio->read_rebuild)
  1752. rbio_orig_end_io(rbio, -EIO, 0);
  1753. return -EIO;
  1754. }
  1755. /*
  1756. * the main entry point for reads from the higher layers. This
  1757. * is really only called when the normal read path had a failure,
  1758. * so we assume the bio they send down corresponds to a failed part
  1759. * of the drive.
  1760. */
  1761. int raid56_parity_recover(struct btrfs_root *root, struct bio *bio,
  1762. struct btrfs_bio *bbio, u64 *raid_map,
  1763. u64 stripe_len, int mirror_num)
  1764. {
  1765. struct btrfs_raid_bio *rbio;
  1766. int ret;
  1767. rbio = alloc_rbio(root, bbio, raid_map, stripe_len);
  1768. if (IS_ERR(rbio))
  1769. return PTR_ERR(rbio);
  1770. rbio->read_rebuild = 1;
  1771. bio_list_add(&rbio->bio_list, bio);
  1772. rbio->bio_list_bytes = bio->bi_size;
  1773. rbio->faila = find_logical_bio_stripe(rbio, bio);
  1774. if (rbio->faila == -1) {
  1775. BUG();
  1776. kfree(raid_map);
  1777. kfree(bbio);
  1778. kfree(rbio);
  1779. return -EIO;
  1780. }
  1781. /*
  1782. * reconstruct from the q stripe if they are
  1783. * asking for mirror 3
  1784. */
  1785. if (mirror_num == 3)
  1786. rbio->failb = bbio->num_stripes - 2;
  1787. ret = lock_stripe_add(rbio);
  1788. /*
  1789. * __raid56_parity_recover will end the bio with
  1790. * any errors it hits. We don't want to return
  1791. * its error value up the stack because our caller
  1792. * will end up calling bio_endio with any nonzero
  1793. * return
  1794. */
  1795. if (ret == 0)
  1796. __raid56_parity_recover(rbio);
  1797. /*
  1798. * our rbio has been added to the list of
  1799. * rbios that will be handled after the
  1800. * currently lock owner is done
  1801. */
  1802. return 0;
  1803. }
  1804. static void rmw_work(struct btrfs_work *work)
  1805. {
  1806. struct btrfs_raid_bio *rbio;
  1807. rbio = container_of(work, struct btrfs_raid_bio, work);
  1808. raid56_rmw_stripe(rbio);
  1809. }
  1810. static void read_rebuild_work(struct btrfs_work *work)
  1811. {
  1812. struct btrfs_raid_bio *rbio;
  1813. rbio = container_of(work, struct btrfs_raid_bio, work);
  1814. __raid56_parity_recover(rbio);
  1815. }