raid56.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721
  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/mm.h>
  35. #include <asm/div64.h>
  36. #include "ctree.h"
  37. #include "extent_map.h"
  38. #include "disk-io.h"
  39. #include "transaction.h"
  40. #include "print-tree.h"
  41. #include "volumes.h"
  42. #include "raid56.h"
  43. #include "async-thread.h"
  44. #include "check-integrity.h"
  45. #include "rcu-string.h"
  46. /* set when additional merges to this rbio are not allowed */
  47. #define RBIO_RMW_LOCKED_BIT 1
  48. /*
  49. * set when this rbio is sitting in the hash, but it is just a cache
  50. * of past RMW
  51. */
  52. #define RBIO_CACHE_BIT 2
  53. /*
  54. * set when it is safe to trust the stripe_pages for caching
  55. */
  56. #define RBIO_CACHE_READY_BIT 3
  57. #define RBIO_CACHE_SIZE 1024
  58. enum btrfs_rbio_ops {
  59. BTRFS_RBIO_WRITE,
  60. BTRFS_RBIO_READ_REBUILD,
  61. BTRFS_RBIO_PARITY_SCRUB,
  62. BTRFS_RBIO_REBUILD_MISSING,
  63. };
  64. struct btrfs_raid_bio {
  65. struct btrfs_fs_info *fs_info;
  66. struct btrfs_bio *bbio;
  67. /* while we're doing rmw on a stripe
  68. * we put it into a hash table so we can
  69. * lock the stripe and merge more rbios
  70. * into it.
  71. */
  72. struct list_head hash_list;
  73. /*
  74. * LRU list for the stripe cache
  75. */
  76. struct list_head stripe_cache;
  77. /*
  78. * for scheduling work in the helper threads
  79. */
  80. struct btrfs_work work;
  81. /*
  82. * bio list and bio_list_lock are used
  83. * to add more bios into the stripe
  84. * in hopes of avoiding the full rmw
  85. */
  86. struct bio_list bio_list;
  87. spinlock_t bio_list_lock;
  88. /* also protected by the bio_list_lock, the
  89. * plug list is used by the plugging code
  90. * to collect partial bios while plugged. The
  91. * stripe locking code also uses it to hand off
  92. * the stripe lock to the next pending IO
  93. */
  94. struct list_head plug_list;
  95. /*
  96. * flags that tell us if it is safe to
  97. * merge with this bio
  98. */
  99. unsigned long flags;
  100. /* size of each individual stripe on disk */
  101. int stripe_len;
  102. /* number of data stripes (no p/q) */
  103. int nr_data;
  104. int real_stripes;
  105. int stripe_npages;
  106. /*
  107. * set if we're doing a parity rebuild
  108. * for a read from higher up, which is handled
  109. * differently from a parity rebuild as part of
  110. * rmw
  111. */
  112. enum btrfs_rbio_ops operation;
  113. /* first bad stripe */
  114. int faila;
  115. /* second bad stripe (for raid6 use) */
  116. int failb;
  117. int scrubp;
  118. /*
  119. * number of pages needed to represent the full
  120. * stripe
  121. */
  122. int nr_pages;
  123. /*
  124. * size of all the bios in the bio_list. This
  125. * helps us decide if the rbio maps to a full
  126. * stripe or not
  127. */
  128. int bio_list_bytes;
  129. int generic_bio_cnt;
  130. refcount_t refs;
  131. atomic_t stripes_pending;
  132. atomic_t error;
  133. /*
  134. * these are two arrays of pointers. We allocate the
  135. * rbio big enough to hold them both and setup their
  136. * locations when the rbio is allocated
  137. */
  138. /* pointers to pages that we allocated for
  139. * reading/writing stripes directly from the disk (including P/Q)
  140. */
  141. struct page **stripe_pages;
  142. /*
  143. * pointers to the pages in the bio_list. Stored
  144. * here for faster lookup
  145. */
  146. struct page **bio_pages;
  147. /*
  148. * bitmap to record which horizontal stripe has data
  149. */
  150. unsigned long *dbitmap;
  151. };
  152. static int __raid56_parity_recover(struct btrfs_raid_bio *rbio);
  153. static noinline void finish_rmw(struct btrfs_raid_bio *rbio);
  154. static void rmw_work(struct btrfs_work *work);
  155. static void read_rebuild_work(struct btrfs_work *work);
  156. static void async_rmw_stripe(struct btrfs_raid_bio *rbio);
  157. static void async_read_rebuild(struct btrfs_raid_bio *rbio);
  158. static int fail_bio_stripe(struct btrfs_raid_bio *rbio, struct bio *bio);
  159. static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed);
  160. static void __free_raid_bio(struct btrfs_raid_bio *rbio);
  161. static void index_rbio_pages(struct btrfs_raid_bio *rbio);
  162. static int alloc_rbio_pages(struct btrfs_raid_bio *rbio);
  163. static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
  164. int need_check);
  165. static void async_scrub_parity(struct btrfs_raid_bio *rbio);
  166. /*
  167. * the stripe hash table is used for locking, and to collect
  168. * bios in hopes of making a full stripe
  169. */
  170. int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
  171. {
  172. struct btrfs_stripe_hash_table *table;
  173. struct btrfs_stripe_hash_table *x;
  174. struct btrfs_stripe_hash *cur;
  175. struct btrfs_stripe_hash *h;
  176. int num_entries = 1 << BTRFS_STRIPE_HASH_TABLE_BITS;
  177. int i;
  178. int table_size;
  179. if (info->stripe_hash_table)
  180. return 0;
  181. /*
  182. * The table is large, starting with order 4 and can go as high as
  183. * order 7 in case lock debugging is turned on.
  184. *
  185. * Try harder to allocate and fallback to vmalloc to lower the chance
  186. * of a failing mount.
  187. */
  188. table_size = sizeof(*table) + sizeof(*h) * num_entries;
  189. table = kvzalloc(table_size, GFP_KERNEL);
  190. if (!table)
  191. return -ENOMEM;
  192. spin_lock_init(&table->cache_lock);
  193. INIT_LIST_HEAD(&table->stripe_cache);
  194. h = table->table;
  195. for (i = 0; i < num_entries; i++) {
  196. cur = h + i;
  197. INIT_LIST_HEAD(&cur->hash_list);
  198. spin_lock_init(&cur->lock);
  199. init_waitqueue_head(&cur->wait);
  200. }
  201. x = cmpxchg(&info->stripe_hash_table, NULL, table);
  202. if (x)
  203. kvfree(x);
  204. return 0;
  205. }
  206. /*
  207. * caching an rbio means to copy anything from the
  208. * bio_pages array into the stripe_pages array. We
  209. * use the page uptodate bit in the stripe cache array
  210. * to indicate if it has valid data
  211. *
  212. * once the caching is done, we set the cache ready
  213. * bit.
  214. */
  215. static void cache_rbio_pages(struct btrfs_raid_bio *rbio)
  216. {
  217. int i;
  218. char *s;
  219. char *d;
  220. int ret;
  221. ret = alloc_rbio_pages(rbio);
  222. if (ret)
  223. return;
  224. for (i = 0; i < rbio->nr_pages; i++) {
  225. if (!rbio->bio_pages[i])
  226. continue;
  227. s = kmap(rbio->bio_pages[i]);
  228. d = kmap(rbio->stripe_pages[i]);
  229. memcpy(d, s, PAGE_SIZE);
  230. kunmap(rbio->bio_pages[i]);
  231. kunmap(rbio->stripe_pages[i]);
  232. SetPageUptodate(rbio->stripe_pages[i]);
  233. }
  234. set_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  235. }
  236. /*
  237. * we hash on the first logical address of the stripe
  238. */
  239. static int rbio_bucket(struct btrfs_raid_bio *rbio)
  240. {
  241. u64 num = rbio->bbio->raid_map[0];
  242. /*
  243. * we shift down quite a bit. We're using byte
  244. * addressing, and most of the lower bits are zeros.
  245. * This tends to upset hash_64, and it consistently
  246. * returns just one or two different values.
  247. *
  248. * shifting off the lower bits fixes things.
  249. */
  250. return hash_64(num >> 16, BTRFS_STRIPE_HASH_TABLE_BITS);
  251. }
  252. /*
  253. * stealing an rbio means taking all the uptodate pages from the stripe
  254. * array in the source rbio and putting them into the destination rbio
  255. */
  256. static void steal_rbio(struct btrfs_raid_bio *src, struct btrfs_raid_bio *dest)
  257. {
  258. int i;
  259. struct page *s;
  260. struct page *d;
  261. if (!test_bit(RBIO_CACHE_READY_BIT, &src->flags))
  262. return;
  263. for (i = 0; i < dest->nr_pages; i++) {
  264. s = src->stripe_pages[i];
  265. if (!s || !PageUptodate(s)) {
  266. continue;
  267. }
  268. d = dest->stripe_pages[i];
  269. if (d)
  270. __free_page(d);
  271. dest->stripe_pages[i] = s;
  272. src->stripe_pages[i] = NULL;
  273. }
  274. }
  275. /*
  276. * merging means we take the bio_list from the victim and
  277. * splice it into the destination. The victim should
  278. * be discarded afterwards.
  279. *
  280. * must be called with dest->rbio_list_lock held
  281. */
  282. static void merge_rbio(struct btrfs_raid_bio *dest,
  283. struct btrfs_raid_bio *victim)
  284. {
  285. bio_list_merge(&dest->bio_list, &victim->bio_list);
  286. dest->bio_list_bytes += victim->bio_list_bytes;
  287. dest->generic_bio_cnt += victim->generic_bio_cnt;
  288. bio_list_init(&victim->bio_list);
  289. }
  290. /*
  291. * used to prune items that are in the cache. The caller
  292. * must hold the hash table lock.
  293. */
  294. static void __remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
  295. {
  296. int bucket = rbio_bucket(rbio);
  297. struct btrfs_stripe_hash_table *table;
  298. struct btrfs_stripe_hash *h;
  299. int freeit = 0;
  300. /*
  301. * check the bit again under the hash table lock.
  302. */
  303. if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
  304. return;
  305. table = rbio->fs_info->stripe_hash_table;
  306. h = table->table + bucket;
  307. /* hold the lock for the bucket because we may be
  308. * removing it from the hash table
  309. */
  310. spin_lock(&h->lock);
  311. /*
  312. * hold the lock for the bio list because we need
  313. * to make sure the bio list is empty
  314. */
  315. spin_lock(&rbio->bio_list_lock);
  316. if (test_and_clear_bit(RBIO_CACHE_BIT, &rbio->flags)) {
  317. list_del_init(&rbio->stripe_cache);
  318. table->cache_size -= 1;
  319. freeit = 1;
  320. /* if the bio list isn't empty, this rbio is
  321. * still involved in an IO. We take it out
  322. * of the cache list, and drop the ref that
  323. * was held for the list.
  324. *
  325. * If the bio_list was empty, we also remove
  326. * the rbio from the hash_table, and drop
  327. * the corresponding ref
  328. */
  329. if (bio_list_empty(&rbio->bio_list)) {
  330. if (!list_empty(&rbio->hash_list)) {
  331. list_del_init(&rbio->hash_list);
  332. refcount_dec(&rbio->refs);
  333. BUG_ON(!list_empty(&rbio->plug_list));
  334. }
  335. }
  336. }
  337. spin_unlock(&rbio->bio_list_lock);
  338. spin_unlock(&h->lock);
  339. if (freeit)
  340. __free_raid_bio(rbio);
  341. }
  342. /*
  343. * prune a given rbio from the cache
  344. */
  345. static void remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
  346. {
  347. struct btrfs_stripe_hash_table *table;
  348. unsigned long flags;
  349. if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
  350. return;
  351. table = rbio->fs_info->stripe_hash_table;
  352. spin_lock_irqsave(&table->cache_lock, flags);
  353. __remove_rbio_from_cache(rbio);
  354. spin_unlock_irqrestore(&table->cache_lock, flags);
  355. }
  356. /*
  357. * remove everything in the cache
  358. */
  359. static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
  360. {
  361. struct btrfs_stripe_hash_table *table;
  362. unsigned long flags;
  363. struct btrfs_raid_bio *rbio;
  364. table = info->stripe_hash_table;
  365. spin_lock_irqsave(&table->cache_lock, flags);
  366. while (!list_empty(&table->stripe_cache)) {
  367. rbio = list_entry(table->stripe_cache.next,
  368. struct btrfs_raid_bio,
  369. stripe_cache);
  370. __remove_rbio_from_cache(rbio);
  371. }
  372. spin_unlock_irqrestore(&table->cache_lock, flags);
  373. }
  374. /*
  375. * remove all cached entries and free the hash table
  376. * used by unmount
  377. */
  378. void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
  379. {
  380. if (!info->stripe_hash_table)
  381. return;
  382. btrfs_clear_rbio_cache(info);
  383. kvfree(info->stripe_hash_table);
  384. info->stripe_hash_table = NULL;
  385. }
  386. /*
  387. * insert an rbio into the stripe cache. It
  388. * must have already been prepared by calling
  389. * cache_rbio_pages
  390. *
  391. * If this rbio was already cached, it gets
  392. * moved to the front of the lru.
  393. *
  394. * If the size of the rbio cache is too big, we
  395. * prune an item.
  396. */
  397. static void cache_rbio(struct btrfs_raid_bio *rbio)
  398. {
  399. struct btrfs_stripe_hash_table *table;
  400. unsigned long flags;
  401. if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
  402. return;
  403. table = rbio->fs_info->stripe_hash_table;
  404. spin_lock_irqsave(&table->cache_lock, flags);
  405. spin_lock(&rbio->bio_list_lock);
  406. /* bump our ref if we were not in the list before */
  407. if (!test_and_set_bit(RBIO_CACHE_BIT, &rbio->flags))
  408. refcount_inc(&rbio->refs);
  409. if (!list_empty(&rbio->stripe_cache)){
  410. list_move(&rbio->stripe_cache, &table->stripe_cache);
  411. } else {
  412. list_add(&rbio->stripe_cache, &table->stripe_cache);
  413. table->cache_size += 1;
  414. }
  415. spin_unlock(&rbio->bio_list_lock);
  416. if (table->cache_size > RBIO_CACHE_SIZE) {
  417. struct btrfs_raid_bio *found;
  418. found = list_entry(table->stripe_cache.prev,
  419. struct btrfs_raid_bio,
  420. stripe_cache);
  421. if (found != rbio)
  422. __remove_rbio_from_cache(found);
  423. }
  424. spin_unlock_irqrestore(&table->cache_lock, flags);
  425. }
  426. /*
  427. * helper function to run the xor_blocks api. It is only
  428. * able to do MAX_XOR_BLOCKS at a time, so we need to
  429. * loop through.
  430. */
  431. static void run_xor(void **pages, int src_cnt, ssize_t len)
  432. {
  433. int src_off = 0;
  434. int xor_src_cnt = 0;
  435. void *dest = pages[src_cnt];
  436. while(src_cnt > 0) {
  437. xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
  438. xor_blocks(xor_src_cnt, len, dest, pages + src_off);
  439. src_cnt -= xor_src_cnt;
  440. src_off += xor_src_cnt;
  441. }
  442. }
  443. /*
  444. * returns true if the bio list inside this rbio
  445. * covers an entire stripe (no rmw required).
  446. * Must be called with the bio list lock held, or
  447. * at a time when you know it is impossible to add
  448. * new bios into the list
  449. */
  450. static int __rbio_is_full(struct btrfs_raid_bio *rbio)
  451. {
  452. unsigned long size = rbio->bio_list_bytes;
  453. int ret = 1;
  454. if (size != rbio->nr_data * rbio->stripe_len)
  455. ret = 0;
  456. BUG_ON(size > rbio->nr_data * rbio->stripe_len);
  457. return ret;
  458. }
  459. static int rbio_is_full(struct btrfs_raid_bio *rbio)
  460. {
  461. unsigned long flags;
  462. int ret;
  463. spin_lock_irqsave(&rbio->bio_list_lock, flags);
  464. ret = __rbio_is_full(rbio);
  465. spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
  466. return ret;
  467. }
  468. /*
  469. * returns 1 if it is safe to merge two rbios together.
  470. * The merging is safe if the two rbios correspond to
  471. * the same stripe and if they are both going in the same
  472. * direction (read vs write), and if neither one is
  473. * locked for final IO
  474. *
  475. * The caller is responsible for locking such that
  476. * rmw_locked is safe to test
  477. */
  478. static int rbio_can_merge(struct btrfs_raid_bio *last,
  479. struct btrfs_raid_bio *cur)
  480. {
  481. if (test_bit(RBIO_RMW_LOCKED_BIT, &last->flags) ||
  482. test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags))
  483. return 0;
  484. /*
  485. * we can't merge with cached rbios, since the
  486. * idea is that when we merge the destination
  487. * rbio is going to run our IO for us. We can
  488. * steal from cached rbios though, other functions
  489. * handle that.
  490. */
  491. if (test_bit(RBIO_CACHE_BIT, &last->flags) ||
  492. test_bit(RBIO_CACHE_BIT, &cur->flags))
  493. return 0;
  494. if (last->bbio->raid_map[0] !=
  495. cur->bbio->raid_map[0])
  496. return 0;
  497. /* we can't merge with different operations */
  498. if (last->operation != cur->operation)
  499. return 0;
  500. /*
  501. * We've need read the full stripe from the drive.
  502. * check and repair the parity and write the new results.
  503. *
  504. * We're not allowed to add any new bios to the
  505. * bio list here, anyone else that wants to
  506. * change this stripe needs to do their own rmw.
  507. */
  508. if (last->operation == BTRFS_RBIO_PARITY_SCRUB ||
  509. cur->operation == BTRFS_RBIO_PARITY_SCRUB)
  510. return 0;
  511. if (last->operation == BTRFS_RBIO_REBUILD_MISSING ||
  512. cur->operation == BTRFS_RBIO_REBUILD_MISSING)
  513. return 0;
  514. return 1;
  515. }
  516. static int rbio_stripe_page_index(struct btrfs_raid_bio *rbio, int stripe,
  517. int index)
  518. {
  519. return stripe * rbio->stripe_npages + index;
  520. }
  521. /*
  522. * these are just the pages from the rbio array, not from anything
  523. * the FS sent down to us
  524. */
  525. static struct page *rbio_stripe_page(struct btrfs_raid_bio *rbio, int stripe,
  526. int index)
  527. {
  528. return rbio->stripe_pages[rbio_stripe_page_index(rbio, stripe, index)];
  529. }
  530. /*
  531. * helper to index into the pstripe
  532. */
  533. static struct page *rbio_pstripe_page(struct btrfs_raid_bio *rbio, int index)
  534. {
  535. return rbio_stripe_page(rbio, rbio->nr_data, index);
  536. }
  537. /*
  538. * helper to index into the qstripe, returns null
  539. * if there is no qstripe
  540. */
  541. static struct page *rbio_qstripe_page(struct btrfs_raid_bio *rbio, int index)
  542. {
  543. if (rbio->nr_data + 1 == rbio->real_stripes)
  544. return NULL;
  545. return rbio_stripe_page(rbio, rbio->nr_data + 1, index);
  546. }
  547. /*
  548. * The first stripe in the table for a logical address
  549. * has the lock. rbios are added in one of three ways:
  550. *
  551. * 1) Nobody has the stripe locked yet. The rbio is given
  552. * the lock and 0 is returned. The caller must start the IO
  553. * themselves.
  554. *
  555. * 2) Someone has the stripe locked, but we're able to merge
  556. * with the lock owner. The rbio is freed and the IO will
  557. * start automatically along with the existing rbio. 1 is returned.
  558. *
  559. * 3) Someone has the stripe locked, but we're not able to merge.
  560. * The rbio is added to the lock owner's plug list, or merged into
  561. * an rbio already on the plug list. When the lock owner unlocks,
  562. * the next rbio on the list is run and the IO is started automatically.
  563. * 1 is returned
  564. *
  565. * If we return 0, the caller still owns the rbio and must continue with
  566. * IO submission. If we return 1, the caller must assume the rbio has
  567. * already been freed.
  568. */
  569. static noinline int lock_stripe_add(struct btrfs_raid_bio *rbio)
  570. {
  571. int bucket = rbio_bucket(rbio);
  572. struct btrfs_stripe_hash *h = rbio->fs_info->stripe_hash_table->table + bucket;
  573. struct btrfs_raid_bio *cur;
  574. struct btrfs_raid_bio *pending;
  575. unsigned long flags;
  576. DEFINE_WAIT(wait);
  577. struct btrfs_raid_bio *freeit = NULL;
  578. struct btrfs_raid_bio *cache_drop = NULL;
  579. int ret = 0;
  580. spin_lock_irqsave(&h->lock, flags);
  581. list_for_each_entry(cur, &h->hash_list, hash_list) {
  582. if (cur->bbio->raid_map[0] == rbio->bbio->raid_map[0]) {
  583. spin_lock(&cur->bio_list_lock);
  584. /* can we steal this cached rbio's pages? */
  585. if (bio_list_empty(&cur->bio_list) &&
  586. list_empty(&cur->plug_list) &&
  587. test_bit(RBIO_CACHE_BIT, &cur->flags) &&
  588. !test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags)) {
  589. list_del_init(&cur->hash_list);
  590. refcount_dec(&cur->refs);
  591. steal_rbio(cur, rbio);
  592. cache_drop = cur;
  593. spin_unlock(&cur->bio_list_lock);
  594. goto lockit;
  595. }
  596. /* can we merge into the lock owner? */
  597. if (rbio_can_merge(cur, rbio)) {
  598. merge_rbio(cur, rbio);
  599. spin_unlock(&cur->bio_list_lock);
  600. freeit = rbio;
  601. ret = 1;
  602. goto out;
  603. }
  604. /*
  605. * we couldn't merge with the running
  606. * rbio, see if we can merge with the
  607. * pending ones. We don't have to
  608. * check for rmw_locked because there
  609. * is no way they are inside finish_rmw
  610. * right now
  611. */
  612. list_for_each_entry(pending, &cur->plug_list,
  613. plug_list) {
  614. if (rbio_can_merge(pending, rbio)) {
  615. merge_rbio(pending, rbio);
  616. spin_unlock(&cur->bio_list_lock);
  617. freeit = rbio;
  618. ret = 1;
  619. goto out;
  620. }
  621. }
  622. /* no merging, put us on the tail of the plug list,
  623. * our rbio will be started with the currently
  624. * running rbio unlocks
  625. */
  626. list_add_tail(&rbio->plug_list, &cur->plug_list);
  627. spin_unlock(&cur->bio_list_lock);
  628. ret = 1;
  629. goto out;
  630. }
  631. }
  632. lockit:
  633. refcount_inc(&rbio->refs);
  634. list_add(&rbio->hash_list, &h->hash_list);
  635. out:
  636. spin_unlock_irqrestore(&h->lock, flags);
  637. if (cache_drop)
  638. remove_rbio_from_cache(cache_drop);
  639. if (freeit)
  640. __free_raid_bio(freeit);
  641. return ret;
  642. }
  643. /*
  644. * called as rmw or parity rebuild is completed. If the plug list has more
  645. * rbios waiting for this stripe, the next one on the list will be started
  646. */
  647. static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
  648. {
  649. int bucket;
  650. struct btrfs_stripe_hash *h;
  651. unsigned long flags;
  652. int keep_cache = 0;
  653. bucket = rbio_bucket(rbio);
  654. h = rbio->fs_info->stripe_hash_table->table + bucket;
  655. if (list_empty(&rbio->plug_list))
  656. cache_rbio(rbio);
  657. spin_lock_irqsave(&h->lock, flags);
  658. spin_lock(&rbio->bio_list_lock);
  659. if (!list_empty(&rbio->hash_list)) {
  660. /*
  661. * if we're still cached and there is no other IO
  662. * to perform, just leave this rbio here for others
  663. * to steal from later
  664. */
  665. if (list_empty(&rbio->plug_list) &&
  666. test_bit(RBIO_CACHE_BIT, &rbio->flags)) {
  667. keep_cache = 1;
  668. clear_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  669. BUG_ON(!bio_list_empty(&rbio->bio_list));
  670. goto done;
  671. }
  672. list_del_init(&rbio->hash_list);
  673. refcount_dec(&rbio->refs);
  674. /*
  675. * we use the plug list to hold all the rbios
  676. * waiting for the chance to lock this stripe.
  677. * hand the lock over to one of them.
  678. */
  679. if (!list_empty(&rbio->plug_list)) {
  680. struct btrfs_raid_bio *next;
  681. struct list_head *head = rbio->plug_list.next;
  682. next = list_entry(head, struct btrfs_raid_bio,
  683. plug_list);
  684. list_del_init(&rbio->plug_list);
  685. list_add(&next->hash_list, &h->hash_list);
  686. refcount_inc(&next->refs);
  687. spin_unlock(&rbio->bio_list_lock);
  688. spin_unlock_irqrestore(&h->lock, flags);
  689. if (next->operation == BTRFS_RBIO_READ_REBUILD)
  690. async_read_rebuild(next);
  691. else if (next->operation == BTRFS_RBIO_REBUILD_MISSING) {
  692. steal_rbio(rbio, next);
  693. async_read_rebuild(next);
  694. } else if (next->operation == BTRFS_RBIO_WRITE) {
  695. steal_rbio(rbio, next);
  696. async_rmw_stripe(next);
  697. } else if (next->operation == BTRFS_RBIO_PARITY_SCRUB) {
  698. steal_rbio(rbio, next);
  699. async_scrub_parity(next);
  700. }
  701. goto done_nolock;
  702. /*
  703. * The barrier for this waitqueue_active is not needed,
  704. * we're protected by h->lock and can't miss a wakeup.
  705. */
  706. } else if (waitqueue_active(&h->wait)) {
  707. spin_unlock(&rbio->bio_list_lock);
  708. spin_unlock_irqrestore(&h->lock, flags);
  709. wake_up(&h->wait);
  710. goto done_nolock;
  711. }
  712. }
  713. done:
  714. spin_unlock(&rbio->bio_list_lock);
  715. spin_unlock_irqrestore(&h->lock, flags);
  716. done_nolock:
  717. if (!keep_cache)
  718. remove_rbio_from_cache(rbio);
  719. }
  720. static void __free_raid_bio(struct btrfs_raid_bio *rbio)
  721. {
  722. int i;
  723. if (!refcount_dec_and_test(&rbio->refs))
  724. return;
  725. WARN_ON(!list_empty(&rbio->stripe_cache));
  726. WARN_ON(!list_empty(&rbio->hash_list));
  727. WARN_ON(!bio_list_empty(&rbio->bio_list));
  728. for (i = 0; i < rbio->nr_pages; i++) {
  729. if (rbio->stripe_pages[i]) {
  730. __free_page(rbio->stripe_pages[i]);
  731. rbio->stripe_pages[i] = NULL;
  732. }
  733. }
  734. btrfs_put_bbio(rbio->bbio);
  735. kfree(rbio);
  736. }
  737. static void free_raid_bio(struct btrfs_raid_bio *rbio)
  738. {
  739. unlock_stripe(rbio);
  740. __free_raid_bio(rbio);
  741. }
  742. /*
  743. * this frees the rbio and runs through all the bios in the
  744. * bio_list and calls end_io on them
  745. */
  746. static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, blk_status_t err)
  747. {
  748. struct bio *cur = bio_list_get(&rbio->bio_list);
  749. struct bio *next;
  750. if (rbio->generic_bio_cnt)
  751. btrfs_bio_counter_sub(rbio->fs_info, rbio->generic_bio_cnt);
  752. free_raid_bio(rbio);
  753. while (cur) {
  754. next = cur->bi_next;
  755. cur->bi_next = NULL;
  756. cur->bi_status = err;
  757. bio_endio(cur);
  758. cur = next;
  759. }
  760. }
  761. /*
  762. * end io function used by finish_rmw. When we finally
  763. * get here, we've written a full stripe
  764. */
  765. static void raid_write_end_io(struct bio *bio)
  766. {
  767. struct btrfs_raid_bio *rbio = bio->bi_private;
  768. blk_status_t err = bio->bi_status;
  769. int max_errors;
  770. if (err)
  771. fail_bio_stripe(rbio, bio);
  772. bio_put(bio);
  773. if (!atomic_dec_and_test(&rbio->stripes_pending))
  774. return;
  775. err = 0;
  776. /* OK, we have read all the stripes we need to. */
  777. max_errors = (rbio->operation == BTRFS_RBIO_PARITY_SCRUB) ?
  778. 0 : rbio->bbio->max_errors;
  779. if (atomic_read(&rbio->error) > max_errors)
  780. err = BLK_STS_IOERR;
  781. rbio_orig_end_io(rbio, err);
  782. }
  783. /*
  784. * the read/modify/write code wants to use the original bio for
  785. * any pages it included, and then use the rbio for everything
  786. * else. This function decides if a given index (stripe number)
  787. * and page number in that stripe fall inside the original bio
  788. * or the rbio.
  789. *
  790. * if you set bio_list_only, you'll get a NULL back for any ranges
  791. * that are outside the bio_list
  792. *
  793. * This doesn't take any refs on anything, you get a bare page pointer
  794. * and the caller must bump refs as required.
  795. *
  796. * You must call index_rbio_pages once before you can trust
  797. * the answers from this function.
  798. */
  799. static struct page *page_in_rbio(struct btrfs_raid_bio *rbio,
  800. int index, int pagenr, int bio_list_only)
  801. {
  802. int chunk_page;
  803. struct page *p = NULL;
  804. chunk_page = index * (rbio->stripe_len >> PAGE_SHIFT) + pagenr;
  805. spin_lock_irq(&rbio->bio_list_lock);
  806. p = rbio->bio_pages[chunk_page];
  807. spin_unlock_irq(&rbio->bio_list_lock);
  808. if (p || bio_list_only)
  809. return p;
  810. return rbio->stripe_pages[chunk_page];
  811. }
  812. /*
  813. * number of pages we need for the entire stripe across all the
  814. * drives
  815. */
  816. static unsigned long rbio_nr_pages(unsigned long stripe_len, int nr_stripes)
  817. {
  818. return DIV_ROUND_UP(stripe_len, PAGE_SIZE) * nr_stripes;
  819. }
  820. /*
  821. * allocation and initial setup for the btrfs_raid_bio. Not
  822. * this does not allocate any pages for rbio->pages.
  823. */
  824. static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
  825. struct btrfs_bio *bbio,
  826. u64 stripe_len)
  827. {
  828. struct btrfs_raid_bio *rbio;
  829. int nr_data = 0;
  830. int real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
  831. int num_pages = rbio_nr_pages(stripe_len, real_stripes);
  832. int stripe_npages = DIV_ROUND_UP(stripe_len, PAGE_SIZE);
  833. void *p;
  834. rbio = kzalloc(sizeof(*rbio) + num_pages * sizeof(struct page *) * 2 +
  835. DIV_ROUND_UP(stripe_npages, BITS_PER_LONG) *
  836. sizeof(long), GFP_NOFS);
  837. if (!rbio)
  838. return ERR_PTR(-ENOMEM);
  839. bio_list_init(&rbio->bio_list);
  840. INIT_LIST_HEAD(&rbio->plug_list);
  841. spin_lock_init(&rbio->bio_list_lock);
  842. INIT_LIST_HEAD(&rbio->stripe_cache);
  843. INIT_LIST_HEAD(&rbio->hash_list);
  844. rbio->bbio = bbio;
  845. rbio->fs_info = fs_info;
  846. rbio->stripe_len = stripe_len;
  847. rbio->nr_pages = num_pages;
  848. rbio->real_stripes = real_stripes;
  849. rbio->stripe_npages = stripe_npages;
  850. rbio->faila = -1;
  851. rbio->failb = -1;
  852. refcount_set(&rbio->refs, 1);
  853. atomic_set(&rbio->error, 0);
  854. atomic_set(&rbio->stripes_pending, 0);
  855. /*
  856. * the stripe_pages and bio_pages array point to the extra
  857. * memory we allocated past the end of the rbio
  858. */
  859. p = rbio + 1;
  860. rbio->stripe_pages = p;
  861. rbio->bio_pages = p + sizeof(struct page *) * num_pages;
  862. rbio->dbitmap = p + sizeof(struct page *) * num_pages * 2;
  863. if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
  864. nr_data = real_stripes - 1;
  865. else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
  866. nr_data = real_stripes - 2;
  867. else
  868. BUG();
  869. rbio->nr_data = nr_data;
  870. return rbio;
  871. }
  872. /* allocate pages for all the stripes in the bio, including parity */
  873. static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
  874. {
  875. int i;
  876. struct page *page;
  877. for (i = 0; i < rbio->nr_pages; i++) {
  878. if (rbio->stripe_pages[i])
  879. continue;
  880. page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  881. if (!page)
  882. return -ENOMEM;
  883. rbio->stripe_pages[i] = page;
  884. }
  885. return 0;
  886. }
  887. /* only allocate pages for p/q stripes */
  888. static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
  889. {
  890. int i;
  891. struct page *page;
  892. i = rbio_stripe_page_index(rbio, rbio->nr_data, 0);
  893. for (; i < rbio->nr_pages; i++) {
  894. if (rbio->stripe_pages[i])
  895. continue;
  896. page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  897. if (!page)
  898. return -ENOMEM;
  899. rbio->stripe_pages[i] = page;
  900. }
  901. return 0;
  902. }
  903. /*
  904. * add a single page from a specific stripe into our list of bios for IO
  905. * this will try to merge into existing bios if possible, and returns
  906. * zero if all went well.
  907. */
  908. static int rbio_add_io_page(struct btrfs_raid_bio *rbio,
  909. struct bio_list *bio_list,
  910. struct page *page,
  911. int stripe_nr,
  912. unsigned long page_index,
  913. unsigned long bio_max_len)
  914. {
  915. struct bio *last = bio_list->tail;
  916. u64 last_end = 0;
  917. int ret;
  918. struct bio *bio;
  919. struct btrfs_bio_stripe *stripe;
  920. u64 disk_start;
  921. stripe = &rbio->bbio->stripes[stripe_nr];
  922. disk_start = stripe->physical + (page_index << PAGE_SHIFT);
  923. /* if the device is missing, just fail this stripe */
  924. if (!stripe->dev->bdev)
  925. return fail_rbio_index(rbio, stripe_nr);
  926. /* see if we can add this page onto our existing bio */
  927. if (last) {
  928. last_end = (u64)last->bi_iter.bi_sector << 9;
  929. last_end += last->bi_iter.bi_size;
  930. /*
  931. * we can't merge these if they are from different
  932. * devices or if they are not contiguous
  933. */
  934. if (last_end == disk_start && stripe->dev->bdev &&
  935. !last->bi_status &&
  936. last->bi_bdev == stripe->dev->bdev) {
  937. ret = bio_add_page(last, page, PAGE_SIZE, 0);
  938. if (ret == PAGE_SIZE)
  939. return 0;
  940. }
  941. }
  942. /* put a new bio on the list */
  943. bio = btrfs_io_bio_alloc(bio_max_len >> PAGE_SHIFT ?: 1);
  944. bio->bi_iter.bi_size = 0;
  945. bio->bi_bdev = stripe->dev->bdev;
  946. bio->bi_iter.bi_sector = disk_start >> 9;
  947. bio_add_page(bio, page, PAGE_SIZE, 0);
  948. bio_list_add(bio_list, bio);
  949. return 0;
  950. }
  951. /*
  952. * while we're doing the read/modify/write cycle, we could
  953. * have errors in reading pages off the disk. This checks
  954. * for errors and if we're not able to read the page it'll
  955. * trigger parity reconstruction. The rmw will be finished
  956. * after we've reconstructed the failed stripes
  957. */
  958. static void validate_rbio_for_rmw(struct btrfs_raid_bio *rbio)
  959. {
  960. if (rbio->faila >= 0 || rbio->failb >= 0) {
  961. BUG_ON(rbio->faila == rbio->real_stripes - 1);
  962. __raid56_parity_recover(rbio);
  963. } else {
  964. finish_rmw(rbio);
  965. }
  966. }
  967. /*
  968. * helper function to walk our bio list and populate the bio_pages array with
  969. * the result. This seems expensive, but it is faster than constantly
  970. * searching through the bio list as we setup the IO in finish_rmw or stripe
  971. * reconstruction.
  972. *
  973. * This must be called before you trust the answers from page_in_rbio
  974. */
  975. static void index_rbio_pages(struct btrfs_raid_bio *rbio)
  976. {
  977. struct bio *bio;
  978. u64 start;
  979. unsigned long stripe_offset;
  980. unsigned long page_index;
  981. spin_lock_irq(&rbio->bio_list_lock);
  982. bio_list_for_each(bio, &rbio->bio_list) {
  983. struct bio_vec bvec;
  984. struct bvec_iter iter;
  985. int i = 0;
  986. start = (u64)bio->bi_iter.bi_sector << 9;
  987. stripe_offset = start - rbio->bbio->raid_map[0];
  988. page_index = stripe_offset >> PAGE_SHIFT;
  989. if (bio_flagged(bio, BIO_CLONED))
  990. bio->bi_iter = btrfs_io_bio(bio)->iter;
  991. bio_for_each_segment(bvec, bio, iter) {
  992. rbio->bio_pages[page_index + i] = bvec.bv_page;
  993. i++;
  994. }
  995. }
  996. spin_unlock_irq(&rbio->bio_list_lock);
  997. }
  998. /*
  999. * this is called from one of two situations. We either
  1000. * have a full stripe from the higher layers, or we've read all
  1001. * the missing bits off disk.
  1002. *
  1003. * This will calculate the parity and then send down any
  1004. * changed blocks.
  1005. */
  1006. static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
  1007. {
  1008. struct btrfs_bio *bbio = rbio->bbio;
  1009. void *pointers[rbio->real_stripes];
  1010. int nr_data = rbio->nr_data;
  1011. int stripe;
  1012. int pagenr;
  1013. int p_stripe = -1;
  1014. int q_stripe = -1;
  1015. struct bio_list bio_list;
  1016. struct bio *bio;
  1017. int ret;
  1018. bio_list_init(&bio_list);
  1019. if (rbio->real_stripes - rbio->nr_data == 1) {
  1020. p_stripe = rbio->real_stripes - 1;
  1021. } else if (rbio->real_stripes - rbio->nr_data == 2) {
  1022. p_stripe = rbio->real_stripes - 2;
  1023. q_stripe = rbio->real_stripes - 1;
  1024. } else {
  1025. BUG();
  1026. }
  1027. /* at this point we either have a full stripe,
  1028. * or we've read the full stripe from the drive.
  1029. * recalculate the parity and write the new results.
  1030. *
  1031. * We're not allowed to add any new bios to the
  1032. * bio list here, anyone else that wants to
  1033. * change this stripe needs to do their own rmw.
  1034. */
  1035. spin_lock_irq(&rbio->bio_list_lock);
  1036. set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  1037. spin_unlock_irq(&rbio->bio_list_lock);
  1038. atomic_set(&rbio->error, 0);
  1039. /*
  1040. * now that we've set rmw_locked, run through the
  1041. * bio list one last time and map the page pointers
  1042. *
  1043. * We don't cache full rbios because we're assuming
  1044. * the higher layers are unlikely to use this area of
  1045. * the disk again soon. If they do use it again,
  1046. * hopefully they will send another full bio.
  1047. */
  1048. index_rbio_pages(rbio);
  1049. if (!rbio_is_full(rbio))
  1050. cache_rbio_pages(rbio);
  1051. else
  1052. clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  1053. for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
  1054. struct page *p;
  1055. /* first collect one page from each data stripe */
  1056. for (stripe = 0; stripe < nr_data; stripe++) {
  1057. p = page_in_rbio(rbio, stripe, pagenr, 0);
  1058. pointers[stripe] = kmap(p);
  1059. }
  1060. /* then add the parity stripe */
  1061. p = rbio_pstripe_page(rbio, pagenr);
  1062. SetPageUptodate(p);
  1063. pointers[stripe++] = kmap(p);
  1064. if (q_stripe != -1) {
  1065. /*
  1066. * raid6, add the qstripe and call the
  1067. * library function to fill in our p/q
  1068. */
  1069. p = rbio_qstripe_page(rbio, pagenr);
  1070. SetPageUptodate(p);
  1071. pointers[stripe++] = kmap(p);
  1072. raid6_call.gen_syndrome(rbio->real_stripes, PAGE_SIZE,
  1073. pointers);
  1074. } else {
  1075. /* raid5 */
  1076. memcpy(pointers[nr_data], pointers[0], PAGE_SIZE);
  1077. run_xor(pointers + 1, nr_data - 1, PAGE_SIZE);
  1078. }
  1079. for (stripe = 0; stripe < rbio->real_stripes; stripe++)
  1080. kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
  1081. }
  1082. /*
  1083. * time to start writing. Make bios for everything from the
  1084. * higher layers (the bio_list in our rbio) and our p/q. Ignore
  1085. * everything else.
  1086. */
  1087. for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
  1088. for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
  1089. struct page *page;
  1090. if (stripe < rbio->nr_data) {
  1091. page = page_in_rbio(rbio, stripe, pagenr, 1);
  1092. if (!page)
  1093. continue;
  1094. } else {
  1095. page = rbio_stripe_page(rbio, stripe, pagenr);
  1096. }
  1097. ret = rbio_add_io_page(rbio, &bio_list,
  1098. page, stripe, pagenr, rbio->stripe_len);
  1099. if (ret)
  1100. goto cleanup;
  1101. }
  1102. }
  1103. if (likely(!bbio->num_tgtdevs))
  1104. goto write_data;
  1105. for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
  1106. if (!bbio->tgtdev_map[stripe])
  1107. continue;
  1108. for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
  1109. struct page *page;
  1110. if (stripe < rbio->nr_data) {
  1111. page = page_in_rbio(rbio, stripe, pagenr, 1);
  1112. if (!page)
  1113. continue;
  1114. } else {
  1115. page = rbio_stripe_page(rbio, stripe, pagenr);
  1116. }
  1117. ret = rbio_add_io_page(rbio, &bio_list, page,
  1118. rbio->bbio->tgtdev_map[stripe],
  1119. pagenr, rbio->stripe_len);
  1120. if (ret)
  1121. goto cleanup;
  1122. }
  1123. }
  1124. write_data:
  1125. atomic_set(&rbio->stripes_pending, bio_list_size(&bio_list));
  1126. BUG_ON(atomic_read(&rbio->stripes_pending) == 0);
  1127. while (1) {
  1128. bio = bio_list_pop(&bio_list);
  1129. if (!bio)
  1130. break;
  1131. bio->bi_private = rbio;
  1132. bio->bi_end_io = raid_write_end_io;
  1133. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  1134. submit_bio(bio);
  1135. }
  1136. return;
  1137. cleanup:
  1138. rbio_orig_end_io(rbio, -EIO);
  1139. }
  1140. /*
  1141. * helper to find the stripe number for a given bio. Used to figure out which
  1142. * stripe has failed. This expects the bio to correspond to a physical disk,
  1143. * so it looks up based on physical sector numbers.
  1144. */
  1145. static int find_bio_stripe(struct btrfs_raid_bio *rbio,
  1146. struct bio *bio)
  1147. {
  1148. u64 physical = bio->bi_iter.bi_sector;
  1149. u64 stripe_start;
  1150. int i;
  1151. struct btrfs_bio_stripe *stripe;
  1152. physical <<= 9;
  1153. for (i = 0; i < rbio->bbio->num_stripes; i++) {
  1154. stripe = &rbio->bbio->stripes[i];
  1155. stripe_start = stripe->physical;
  1156. if (physical >= stripe_start &&
  1157. physical < stripe_start + rbio->stripe_len &&
  1158. bio->bi_bdev == stripe->dev->bdev) {
  1159. return i;
  1160. }
  1161. }
  1162. return -1;
  1163. }
  1164. /*
  1165. * helper to find the stripe number for a given
  1166. * bio (before mapping). Used to figure out which stripe has
  1167. * failed. This looks up based on logical block numbers.
  1168. */
  1169. static int find_logical_bio_stripe(struct btrfs_raid_bio *rbio,
  1170. struct bio *bio)
  1171. {
  1172. u64 logical = bio->bi_iter.bi_sector;
  1173. u64 stripe_start;
  1174. int i;
  1175. logical <<= 9;
  1176. for (i = 0; i < rbio->nr_data; i++) {
  1177. stripe_start = rbio->bbio->raid_map[i];
  1178. if (logical >= stripe_start &&
  1179. logical < stripe_start + rbio->stripe_len) {
  1180. return i;
  1181. }
  1182. }
  1183. return -1;
  1184. }
  1185. /*
  1186. * returns -EIO if we had too many failures
  1187. */
  1188. static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed)
  1189. {
  1190. unsigned long flags;
  1191. int ret = 0;
  1192. spin_lock_irqsave(&rbio->bio_list_lock, flags);
  1193. /* we already know this stripe is bad, move on */
  1194. if (rbio->faila == failed || rbio->failb == failed)
  1195. goto out;
  1196. if (rbio->faila == -1) {
  1197. /* first failure on this rbio */
  1198. rbio->faila = failed;
  1199. atomic_inc(&rbio->error);
  1200. } else if (rbio->failb == -1) {
  1201. /* second failure on this rbio */
  1202. rbio->failb = failed;
  1203. atomic_inc(&rbio->error);
  1204. } else {
  1205. ret = -EIO;
  1206. }
  1207. out:
  1208. spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
  1209. return ret;
  1210. }
  1211. /*
  1212. * helper to fail a stripe based on a physical disk
  1213. * bio.
  1214. */
  1215. static int fail_bio_stripe(struct btrfs_raid_bio *rbio,
  1216. struct bio *bio)
  1217. {
  1218. int failed = find_bio_stripe(rbio, bio);
  1219. if (failed < 0)
  1220. return -EIO;
  1221. return fail_rbio_index(rbio, failed);
  1222. }
  1223. /*
  1224. * this sets each page in the bio uptodate. It should only be used on private
  1225. * rbio pages, nothing that comes in from the higher layers
  1226. */
  1227. static void set_bio_pages_uptodate(struct bio *bio)
  1228. {
  1229. struct bio_vec bvec;
  1230. struct bvec_iter iter;
  1231. if (bio_flagged(bio, BIO_CLONED))
  1232. bio->bi_iter = btrfs_io_bio(bio)->iter;
  1233. bio_for_each_segment(bvec, bio, iter)
  1234. SetPageUptodate(bvec.bv_page);
  1235. }
  1236. /*
  1237. * end io for the read phase of the rmw cycle. All the bios here are physical
  1238. * stripe bios we've read from the disk so we can recalculate the parity of the
  1239. * stripe.
  1240. *
  1241. * This will usually kick off finish_rmw once all the bios are read in, but it
  1242. * may trigger parity reconstruction if we had any errors along the way
  1243. */
  1244. static void raid_rmw_end_io(struct bio *bio)
  1245. {
  1246. struct btrfs_raid_bio *rbio = bio->bi_private;
  1247. if (bio->bi_status)
  1248. fail_bio_stripe(rbio, bio);
  1249. else
  1250. set_bio_pages_uptodate(bio);
  1251. bio_put(bio);
  1252. if (!atomic_dec_and_test(&rbio->stripes_pending))
  1253. return;
  1254. if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
  1255. goto cleanup;
  1256. /*
  1257. * this will normally call finish_rmw to start our write
  1258. * but if there are any failed stripes we'll reconstruct
  1259. * from parity first
  1260. */
  1261. validate_rbio_for_rmw(rbio);
  1262. return;
  1263. cleanup:
  1264. rbio_orig_end_io(rbio, -EIO);
  1265. }
  1266. static void async_rmw_stripe(struct btrfs_raid_bio *rbio)
  1267. {
  1268. btrfs_init_work(&rbio->work, btrfs_rmw_helper, rmw_work, NULL, NULL);
  1269. btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
  1270. }
  1271. static void async_read_rebuild(struct btrfs_raid_bio *rbio)
  1272. {
  1273. btrfs_init_work(&rbio->work, btrfs_rmw_helper,
  1274. read_rebuild_work, NULL, NULL);
  1275. btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
  1276. }
  1277. /*
  1278. * the stripe must be locked by the caller. It will
  1279. * unlock after all the writes are done
  1280. */
  1281. static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
  1282. {
  1283. int bios_to_read = 0;
  1284. struct bio_list bio_list;
  1285. int ret;
  1286. int pagenr;
  1287. int stripe;
  1288. struct bio *bio;
  1289. bio_list_init(&bio_list);
  1290. ret = alloc_rbio_pages(rbio);
  1291. if (ret)
  1292. goto cleanup;
  1293. index_rbio_pages(rbio);
  1294. atomic_set(&rbio->error, 0);
  1295. /*
  1296. * build a list of bios to read all the missing parts of this
  1297. * stripe
  1298. */
  1299. for (stripe = 0; stripe < rbio->nr_data; stripe++) {
  1300. for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
  1301. struct page *page;
  1302. /*
  1303. * we want to find all the pages missing from
  1304. * the rbio and read them from the disk. If
  1305. * page_in_rbio finds a page in the bio list
  1306. * we don't need to read it off the stripe.
  1307. */
  1308. page = page_in_rbio(rbio, stripe, pagenr, 1);
  1309. if (page)
  1310. continue;
  1311. page = rbio_stripe_page(rbio, stripe, pagenr);
  1312. /*
  1313. * the bio cache may have handed us an uptodate
  1314. * page. If so, be happy and use it
  1315. */
  1316. if (PageUptodate(page))
  1317. continue;
  1318. ret = rbio_add_io_page(rbio, &bio_list, page,
  1319. stripe, pagenr, rbio->stripe_len);
  1320. if (ret)
  1321. goto cleanup;
  1322. }
  1323. }
  1324. bios_to_read = bio_list_size(&bio_list);
  1325. if (!bios_to_read) {
  1326. /*
  1327. * this can happen if others have merged with
  1328. * us, it means there is nothing left to read.
  1329. * But if there are missing devices it may not be
  1330. * safe to do the full stripe write yet.
  1331. */
  1332. goto finish;
  1333. }
  1334. /*
  1335. * the bbio may be freed once we submit the last bio. Make sure
  1336. * not to touch it after that
  1337. */
  1338. atomic_set(&rbio->stripes_pending, bios_to_read);
  1339. while (1) {
  1340. bio = bio_list_pop(&bio_list);
  1341. if (!bio)
  1342. break;
  1343. bio->bi_private = rbio;
  1344. bio->bi_end_io = raid_rmw_end_io;
  1345. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  1346. btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
  1347. submit_bio(bio);
  1348. }
  1349. /* the actual write will happen once the reads are done */
  1350. return 0;
  1351. cleanup:
  1352. rbio_orig_end_io(rbio, -EIO);
  1353. return -EIO;
  1354. finish:
  1355. validate_rbio_for_rmw(rbio);
  1356. return 0;
  1357. }
  1358. /*
  1359. * if the upper layers pass in a full stripe, we thank them by only allocating
  1360. * enough pages to hold the parity, and sending it all down quickly.
  1361. */
  1362. static int full_stripe_write(struct btrfs_raid_bio *rbio)
  1363. {
  1364. int ret;
  1365. ret = alloc_rbio_parity_pages(rbio);
  1366. if (ret) {
  1367. __free_raid_bio(rbio);
  1368. return ret;
  1369. }
  1370. ret = lock_stripe_add(rbio);
  1371. if (ret == 0)
  1372. finish_rmw(rbio);
  1373. return 0;
  1374. }
  1375. /*
  1376. * partial stripe writes get handed over to async helpers.
  1377. * We're really hoping to merge a few more writes into this
  1378. * rbio before calculating new parity
  1379. */
  1380. static int partial_stripe_write(struct btrfs_raid_bio *rbio)
  1381. {
  1382. int ret;
  1383. ret = lock_stripe_add(rbio);
  1384. if (ret == 0)
  1385. async_rmw_stripe(rbio);
  1386. return 0;
  1387. }
  1388. /*
  1389. * sometimes while we were reading from the drive to
  1390. * recalculate parity, enough new bios come into create
  1391. * a full stripe. So we do a check here to see if we can
  1392. * go directly to finish_rmw
  1393. */
  1394. static int __raid56_parity_write(struct btrfs_raid_bio *rbio)
  1395. {
  1396. /* head off into rmw land if we don't have a full stripe */
  1397. if (!rbio_is_full(rbio))
  1398. return partial_stripe_write(rbio);
  1399. return full_stripe_write(rbio);
  1400. }
  1401. /*
  1402. * We use plugging call backs to collect full stripes.
  1403. * Any time we get a partial stripe write while plugged
  1404. * we collect it into a list. When the unplug comes down,
  1405. * we sort the list by logical block number and merge
  1406. * everything we can into the same rbios
  1407. */
  1408. struct btrfs_plug_cb {
  1409. struct blk_plug_cb cb;
  1410. struct btrfs_fs_info *info;
  1411. struct list_head rbio_list;
  1412. struct btrfs_work work;
  1413. };
  1414. /*
  1415. * rbios on the plug list are sorted for easier merging.
  1416. */
  1417. static int plug_cmp(void *priv, struct list_head *a, struct list_head *b)
  1418. {
  1419. struct btrfs_raid_bio *ra = container_of(a, struct btrfs_raid_bio,
  1420. plug_list);
  1421. struct btrfs_raid_bio *rb = container_of(b, struct btrfs_raid_bio,
  1422. plug_list);
  1423. u64 a_sector = ra->bio_list.head->bi_iter.bi_sector;
  1424. u64 b_sector = rb->bio_list.head->bi_iter.bi_sector;
  1425. if (a_sector < b_sector)
  1426. return -1;
  1427. if (a_sector > b_sector)
  1428. return 1;
  1429. return 0;
  1430. }
  1431. static void run_plug(struct btrfs_plug_cb *plug)
  1432. {
  1433. struct btrfs_raid_bio *cur;
  1434. struct btrfs_raid_bio *last = NULL;
  1435. /*
  1436. * sort our plug list then try to merge
  1437. * everything we can in hopes of creating full
  1438. * stripes.
  1439. */
  1440. list_sort(NULL, &plug->rbio_list, plug_cmp);
  1441. while (!list_empty(&plug->rbio_list)) {
  1442. cur = list_entry(plug->rbio_list.next,
  1443. struct btrfs_raid_bio, plug_list);
  1444. list_del_init(&cur->plug_list);
  1445. if (rbio_is_full(cur)) {
  1446. /* we have a full stripe, send it down */
  1447. full_stripe_write(cur);
  1448. continue;
  1449. }
  1450. if (last) {
  1451. if (rbio_can_merge(last, cur)) {
  1452. merge_rbio(last, cur);
  1453. __free_raid_bio(cur);
  1454. continue;
  1455. }
  1456. __raid56_parity_write(last);
  1457. }
  1458. last = cur;
  1459. }
  1460. if (last) {
  1461. __raid56_parity_write(last);
  1462. }
  1463. kfree(plug);
  1464. }
  1465. /*
  1466. * if the unplug comes from schedule, we have to push the
  1467. * work off to a helper thread
  1468. */
  1469. static void unplug_work(struct btrfs_work *work)
  1470. {
  1471. struct btrfs_plug_cb *plug;
  1472. plug = container_of(work, struct btrfs_plug_cb, work);
  1473. run_plug(plug);
  1474. }
  1475. static void btrfs_raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
  1476. {
  1477. struct btrfs_plug_cb *plug;
  1478. plug = container_of(cb, struct btrfs_plug_cb, cb);
  1479. if (from_schedule) {
  1480. btrfs_init_work(&plug->work, btrfs_rmw_helper,
  1481. unplug_work, NULL, NULL);
  1482. btrfs_queue_work(plug->info->rmw_workers,
  1483. &plug->work);
  1484. return;
  1485. }
  1486. run_plug(plug);
  1487. }
  1488. /*
  1489. * our main entry point for writes from the rest of the FS.
  1490. */
  1491. int raid56_parity_write(struct btrfs_fs_info *fs_info, struct bio *bio,
  1492. struct btrfs_bio *bbio, u64 stripe_len)
  1493. {
  1494. struct btrfs_raid_bio *rbio;
  1495. struct btrfs_plug_cb *plug = NULL;
  1496. struct blk_plug_cb *cb;
  1497. int ret;
  1498. rbio = alloc_rbio(fs_info, bbio, stripe_len);
  1499. if (IS_ERR(rbio)) {
  1500. btrfs_put_bbio(bbio);
  1501. return PTR_ERR(rbio);
  1502. }
  1503. bio_list_add(&rbio->bio_list, bio);
  1504. rbio->bio_list_bytes = bio->bi_iter.bi_size;
  1505. rbio->operation = BTRFS_RBIO_WRITE;
  1506. btrfs_bio_counter_inc_noblocked(fs_info);
  1507. rbio->generic_bio_cnt = 1;
  1508. /*
  1509. * don't plug on full rbios, just get them out the door
  1510. * as quickly as we can
  1511. */
  1512. if (rbio_is_full(rbio)) {
  1513. ret = full_stripe_write(rbio);
  1514. if (ret)
  1515. btrfs_bio_counter_dec(fs_info);
  1516. return ret;
  1517. }
  1518. cb = blk_check_plugged(btrfs_raid_unplug, fs_info, sizeof(*plug));
  1519. if (cb) {
  1520. plug = container_of(cb, struct btrfs_plug_cb, cb);
  1521. if (!plug->info) {
  1522. plug->info = fs_info;
  1523. INIT_LIST_HEAD(&plug->rbio_list);
  1524. }
  1525. list_add_tail(&rbio->plug_list, &plug->rbio_list);
  1526. ret = 0;
  1527. } else {
  1528. ret = __raid56_parity_write(rbio);
  1529. if (ret)
  1530. btrfs_bio_counter_dec(fs_info);
  1531. }
  1532. return ret;
  1533. }
  1534. /*
  1535. * all parity reconstruction happens here. We've read in everything
  1536. * we can find from the drives and this does the heavy lifting of
  1537. * sorting the good from the bad.
  1538. */
  1539. static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
  1540. {
  1541. int pagenr, stripe;
  1542. void **pointers;
  1543. int faila = -1, failb = -1;
  1544. struct page *page;
  1545. int err;
  1546. int i;
  1547. pointers = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
  1548. if (!pointers) {
  1549. err = -ENOMEM;
  1550. goto cleanup_io;
  1551. }
  1552. faila = rbio->faila;
  1553. failb = rbio->failb;
  1554. if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
  1555. rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
  1556. spin_lock_irq(&rbio->bio_list_lock);
  1557. set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  1558. spin_unlock_irq(&rbio->bio_list_lock);
  1559. }
  1560. index_rbio_pages(rbio);
  1561. for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
  1562. /*
  1563. * Now we just use bitmap to mark the horizontal stripes in
  1564. * which we have data when doing parity scrub.
  1565. */
  1566. if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB &&
  1567. !test_bit(pagenr, rbio->dbitmap))
  1568. continue;
  1569. /* setup our array of pointers with pages
  1570. * from each stripe
  1571. */
  1572. for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
  1573. /*
  1574. * if we're rebuilding a read, we have to use
  1575. * pages from the bio list
  1576. */
  1577. if ((rbio->operation == BTRFS_RBIO_READ_REBUILD ||
  1578. rbio->operation == BTRFS_RBIO_REBUILD_MISSING) &&
  1579. (stripe == faila || stripe == failb)) {
  1580. page = page_in_rbio(rbio, stripe, pagenr, 0);
  1581. } else {
  1582. page = rbio_stripe_page(rbio, stripe, pagenr);
  1583. }
  1584. pointers[stripe] = kmap(page);
  1585. }
  1586. /* all raid6 handling here */
  1587. if (rbio->bbio->map_type & BTRFS_BLOCK_GROUP_RAID6) {
  1588. /*
  1589. * single failure, rebuild from parity raid5
  1590. * style
  1591. */
  1592. if (failb < 0) {
  1593. if (faila == rbio->nr_data) {
  1594. /*
  1595. * Just the P stripe has failed, without
  1596. * a bad data or Q stripe.
  1597. * TODO, we should redo the xor here.
  1598. */
  1599. err = -EIO;
  1600. goto cleanup;
  1601. }
  1602. /*
  1603. * a single failure in raid6 is rebuilt
  1604. * in the pstripe code below
  1605. */
  1606. goto pstripe;
  1607. }
  1608. /* make sure our ps and qs are in order */
  1609. if (faila > failb) {
  1610. int tmp = failb;
  1611. failb = faila;
  1612. faila = tmp;
  1613. }
  1614. /* if the q stripe is failed, do a pstripe reconstruction
  1615. * from the xors.
  1616. * If both the q stripe and the P stripe are failed, we're
  1617. * here due to a crc mismatch and we can't give them the
  1618. * data they want
  1619. */
  1620. if (rbio->bbio->raid_map[failb] == RAID6_Q_STRIPE) {
  1621. if (rbio->bbio->raid_map[faila] ==
  1622. RAID5_P_STRIPE) {
  1623. err = -EIO;
  1624. goto cleanup;
  1625. }
  1626. /*
  1627. * otherwise we have one bad data stripe and
  1628. * a good P stripe. raid5!
  1629. */
  1630. goto pstripe;
  1631. }
  1632. if (rbio->bbio->raid_map[failb] == RAID5_P_STRIPE) {
  1633. raid6_datap_recov(rbio->real_stripes,
  1634. PAGE_SIZE, faila, pointers);
  1635. } else {
  1636. raid6_2data_recov(rbio->real_stripes,
  1637. PAGE_SIZE, faila, failb,
  1638. pointers);
  1639. }
  1640. } else {
  1641. void *p;
  1642. /* rebuild from P stripe here (raid5 or raid6) */
  1643. BUG_ON(failb != -1);
  1644. pstripe:
  1645. /* Copy parity block into failed block to start with */
  1646. memcpy(pointers[faila],
  1647. pointers[rbio->nr_data],
  1648. PAGE_SIZE);
  1649. /* rearrange the pointer array */
  1650. p = pointers[faila];
  1651. for (stripe = faila; stripe < rbio->nr_data - 1; stripe++)
  1652. pointers[stripe] = pointers[stripe + 1];
  1653. pointers[rbio->nr_data - 1] = p;
  1654. /* xor in the rest */
  1655. run_xor(pointers, rbio->nr_data - 1, PAGE_SIZE);
  1656. }
  1657. /* if we're doing this rebuild as part of an rmw, go through
  1658. * and set all of our private rbio pages in the
  1659. * failed stripes as uptodate. This way finish_rmw will
  1660. * know they can be trusted. If this was a read reconstruction,
  1661. * other endio functions will fiddle the uptodate bits
  1662. */
  1663. if (rbio->operation == BTRFS_RBIO_WRITE) {
  1664. for (i = 0; i < rbio->stripe_npages; i++) {
  1665. if (faila != -1) {
  1666. page = rbio_stripe_page(rbio, faila, i);
  1667. SetPageUptodate(page);
  1668. }
  1669. if (failb != -1) {
  1670. page = rbio_stripe_page(rbio, failb, i);
  1671. SetPageUptodate(page);
  1672. }
  1673. }
  1674. }
  1675. for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
  1676. /*
  1677. * if we're rebuilding a read, we have to use
  1678. * pages from the bio list
  1679. */
  1680. if ((rbio->operation == BTRFS_RBIO_READ_REBUILD ||
  1681. rbio->operation == BTRFS_RBIO_REBUILD_MISSING) &&
  1682. (stripe == faila || stripe == failb)) {
  1683. page = page_in_rbio(rbio, stripe, pagenr, 0);
  1684. } else {
  1685. page = rbio_stripe_page(rbio, stripe, pagenr);
  1686. }
  1687. kunmap(page);
  1688. }
  1689. }
  1690. err = 0;
  1691. cleanup:
  1692. kfree(pointers);
  1693. cleanup_io:
  1694. if (rbio->operation == BTRFS_RBIO_READ_REBUILD) {
  1695. if (err == 0)
  1696. cache_rbio_pages(rbio);
  1697. else
  1698. clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  1699. rbio_orig_end_io(rbio, err);
  1700. } else if (rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
  1701. rbio_orig_end_io(rbio, err);
  1702. } else if (err == 0) {
  1703. rbio->faila = -1;
  1704. rbio->failb = -1;
  1705. if (rbio->operation == BTRFS_RBIO_WRITE)
  1706. finish_rmw(rbio);
  1707. else if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB)
  1708. finish_parity_scrub(rbio, 0);
  1709. else
  1710. BUG();
  1711. } else {
  1712. rbio_orig_end_io(rbio, err);
  1713. }
  1714. }
  1715. /*
  1716. * This is called only for stripes we've read from disk to
  1717. * reconstruct the parity.
  1718. */
  1719. static void raid_recover_end_io(struct bio *bio)
  1720. {
  1721. struct btrfs_raid_bio *rbio = bio->bi_private;
  1722. /*
  1723. * we only read stripe pages off the disk, set them
  1724. * up to date if there were no errors
  1725. */
  1726. if (bio->bi_status)
  1727. fail_bio_stripe(rbio, bio);
  1728. else
  1729. set_bio_pages_uptodate(bio);
  1730. bio_put(bio);
  1731. if (!atomic_dec_and_test(&rbio->stripes_pending))
  1732. return;
  1733. if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
  1734. rbio_orig_end_io(rbio, -EIO);
  1735. else
  1736. __raid_recover_end_io(rbio);
  1737. }
  1738. /*
  1739. * reads everything we need off the disk to reconstruct
  1740. * the parity. endio handlers trigger final reconstruction
  1741. * when the IO is done.
  1742. *
  1743. * This is used both for reads from the higher layers and for
  1744. * parity construction required to finish a rmw cycle.
  1745. */
  1746. static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
  1747. {
  1748. int bios_to_read = 0;
  1749. struct bio_list bio_list;
  1750. int ret;
  1751. int pagenr;
  1752. int stripe;
  1753. struct bio *bio;
  1754. bio_list_init(&bio_list);
  1755. ret = alloc_rbio_pages(rbio);
  1756. if (ret)
  1757. goto cleanup;
  1758. atomic_set(&rbio->error, 0);
  1759. /*
  1760. * read everything that hasn't failed. Thanks to the
  1761. * stripe cache, it is possible that some or all of these
  1762. * pages are going to be uptodate.
  1763. */
  1764. for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
  1765. if (rbio->faila == stripe || rbio->failb == stripe) {
  1766. atomic_inc(&rbio->error);
  1767. continue;
  1768. }
  1769. for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
  1770. struct page *p;
  1771. /*
  1772. * the rmw code may have already read this
  1773. * page in
  1774. */
  1775. p = rbio_stripe_page(rbio, stripe, pagenr);
  1776. if (PageUptodate(p))
  1777. continue;
  1778. ret = rbio_add_io_page(rbio, &bio_list,
  1779. rbio_stripe_page(rbio, stripe, pagenr),
  1780. stripe, pagenr, rbio->stripe_len);
  1781. if (ret < 0)
  1782. goto cleanup;
  1783. }
  1784. }
  1785. bios_to_read = bio_list_size(&bio_list);
  1786. if (!bios_to_read) {
  1787. /*
  1788. * we might have no bios to read just because the pages
  1789. * were up to date, or we might have no bios to read because
  1790. * the devices were gone.
  1791. */
  1792. if (atomic_read(&rbio->error) <= rbio->bbio->max_errors) {
  1793. __raid_recover_end_io(rbio);
  1794. goto out;
  1795. } else {
  1796. goto cleanup;
  1797. }
  1798. }
  1799. /*
  1800. * the bbio may be freed once we submit the last bio. Make sure
  1801. * not to touch it after that
  1802. */
  1803. atomic_set(&rbio->stripes_pending, bios_to_read);
  1804. while (1) {
  1805. bio = bio_list_pop(&bio_list);
  1806. if (!bio)
  1807. break;
  1808. bio->bi_private = rbio;
  1809. bio->bi_end_io = raid_recover_end_io;
  1810. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  1811. btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
  1812. submit_bio(bio);
  1813. }
  1814. out:
  1815. return 0;
  1816. cleanup:
  1817. if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
  1818. rbio->operation == BTRFS_RBIO_REBUILD_MISSING)
  1819. rbio_orig_end_io(rbio, -EIO);
  1820. return -EIO;
  1821. }
  1822. /*
  1823. * the main entry point for reads from the higher layers. This
  1824. * is really only called when the normal read path had a failure,
  1825. * so we assume the bio they send down corresponds to a failed part
  1826. * of the drive.
  1827. */
  1828. int raid56_parity_recover(struct btrfs_fs_info *fs_info, struct bio *bio,
  1829. struct btrfs_bio *bbio, u64 stripe_len,
  1830. int mirror_num, int generic_io)
  1831. {
  1832. struct btrfs_raid_bio *rbio;
  1833. int ret;
  1834. if (generic_io) {
  1835. ASSERT(bbio->mirror_num == mirror_num);
  1836. btrfs_io_bio(bio)->mirror_num = mirror_num;
  1837. }
  1838. rbio = alloc_rbio(fs_info, bbio, stripe_len);
  1839. if (IS_ERR(rbio)) {
  1840. if (generic_io)
  1841. btrfs_put_bbio(bbio);
  1842. return PTR_ERR(rbio);
  1843. }
  1844. rbio->operation = BTRFS_RBIO_READ_REBUILD;
  1845. bio_list_add(&rbio->bio_list, bio);
  1846. rbio->bio_list_bytes = bio->bi_iter.bi_size;
  1847. rbio->faila = find_logical_bio_stripe(rbio, bio);
  1848. if (rbio->faila == -1) {
  1849. btrfs_warn(fs_info,
  1850. "%s could not find the bad stripe in raid56 so that we cannot recover any more (bio has logical %llu len %llu, bbio has map_type %llu)",
  1851. __func__, (u64)bio->bi_iter.bi_sector << 9,
  1852. (u64)bio->bi_iter.bi_size, bbio->map_type);
  1853. if (generic_io)
  1854. btrfs_put_bbio(bbio);
  1855. kfree(rbio);
  1856. return -EIO;
  1857. }
  1858. if (generic_io) {
  1859. btrfs_bio_counter_inc_noblocked(fs_info);
  1860. rbio->generic_bio_cnt = 1;
  1861. } else {
  1862. btrfs_get_bbio(bbio);
  1863. }
  1864. /*
  1865. * reconstruct from the q stripe if they are
  1866. * asking for mirror 3
  1867. */
  1868. if (mirror_num == 3)
  1869. rbio->failb = rbio->real_stripes - 2;
  1870. ret = lock_stripe_add(rbio);
  1871. /*
  1872. * __raid56_parity_recover will end the bio with
  1873. * any errors it hits. We don't want to return
  1874. * its error value up the stack because our caller
  1875. * will end up calling bio_endio with any nonzero
  1876. * return
  1877. */
  1878. if (ret == 0)
  1879. __raid56_parity_recover(rbio);
  1880. /*
  1881. * our rbio has been added to the list of
  1882. * rbios that will be handled after the
  1883. * currently lock owner is done
  1884. */
  1885. return 0;
  1886. }
  1887. static void rmw_work(struct btrfs_work *work)
  1888. {
  1889. struct btrfs_raid_bio *rbio;
  1890. rbio = container_of(work, struct btrfs_raid_bio, work);
  1891. raid56_rmw_stripe(rbio);
  1892. }
  1893. static void read_rebuild_work(struct btrfs_work *work)
  1894. {
  1895. struct btrfs_raid_bio *rbio;
  1896. rbio = container_of(work, struct btrfs_raid_bio, work);
  1897. __raid56_parity_recover(rbio);
  1898. }
  1899. /*
  1900. * The following code is used to scrub/replace the parity stripe
  1901. *
  1902. * Caller must have already increased bio_counter for getting @bbio.
  1903. *
  1904. * Note: We need make sure all the pages that add into the scrub/replace
  1905. * raid bio are correct and not be changed during the scrub/replace. That
  1906. * is those pages just hold metadata or file data with checksum.
  1907. */
  1908. struct btrfs_raid_bio *
  1909. raid56_parity_alloc_scrub_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
  1910. struct btrfs_bio *bbio, u64 stripe_len,
  1911. struct btrfs_device *scrub_dev,
  1912. unsigned long *dbitmap, int stripe_nsectors)
  1913. {
  1914. struct btrfs_raid_bio *rbio;
  1915. int i;
  1916. rbio = alloc_rbio(fs_info, bbio, stripe_len);
  1917. if (IS_ERR(rbio))
  1918. return NULL;
  1919. bio_list_add(&rbio->bio_list, bio);
  1920. /*
  1921. * This is a special bio which is used to hold the completion handler
  1922. * and make the scrub rbio is similar to the other types
  1923. */
  1924. ASSERT(!bio->bi_iter.bi_size);
  1925. rbio->operation = BTRFS_RBIO_PARITY_SCRUB;
  1926. for (i = 0; i < rbio->real_stripes; i++) {
  1927. if (bbio->stripes[i].dev == scrub_dev) {
  1928. rbio->scrubp = i;
  1929. break;
  1930. }
  1931. }
  1932. /* Now we just support the sectorsize equals to page size */
  1933. ASSERT(fs_info->sectorsize == PAGE_SIZE);
  1934. ASSERT(rbio->stripe_npages == stripe_nsectors);
  1935. bitmap_copy(rbio->dbitmap, dbitmap, stripe_nsectors);
  1936. /*
  1937. * We have already increased bio_counter when getting bbio, record it
  1938. * so we can free it at rbio_orig_end_io().
  1939. */
  1940. rbio->generic_bio_cnt = 1;
  1941. return rbio;
  1942. }
  1943. /* Used for both parity scrub and missing. */
  1944. void raid56_add_scrub_pages(struct btrfs_raid_bio *rbio, struct page *page,
  1945. u64 logical)
  1946. {
  1947. int stripe_offset;
  1948. int index;
  1949. ASSERT(logical >= rbio->bbio->raid_map[0]);
  1950. ASSERT(logical + PAGE_SIZE <= rbio->bbio->raid_map[0] +
  1951. rbio->stripe_len * rbio->nr_data);
  1952. stripe_offset = (int)(logical - rbio->bbio->raid_map[0]);
  1953. index = stripe_offset >> PAGE_SHIFT;
  1954. rbio->bio_pages[index] = page;
  1955. }
  1956. /*
  1957. * We just scrub the parity that we have correct data on the same horizontal,
  1958. * so we needn't allocate all pages for all the stripes.
  1959. */
  1960. static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
  1961. {
  1962. int i;
  1963. int bit;
  1964. int index;
  1965. struct page *page;
  1966. for_each_set_bit(bit, rbio->dbitmap, rbio->stripe_npages) {
  1967. for (i = 0; i < rbio->real_stripes; i++) {
  1968. index = i * rbio->stripe_npages + bit;
  1969. if (rbio->stripe_pages[index])
  1970. continue;
  1971. page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  1972. if (!page)
  1973. return -ENOMEM;
  1974. rbio->stripe_pages[index] = page;
  1975. }
  1976. }
  1977. return 0;
  1978. }
  1979. static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
  1980. int need_check)
  1981. {
  1982. struct btrfs_bio *bbio = rbio->bbio;
  1983. void *pointers[rbio->real_stripes];
  1984. DECLARE_BITMAP(pbitmap, rbio->stripe_npages);
  1985. int nr_data = rbio->nr_data;
  1986. int stripe;
  1987. int pagenr;
  1988. int p_stripe = -1;
  1989. int q_stripe = -1;
  1990. struct page *p_page = NULL;
  1991. struct page *q_page = NULL;
  1992. struct bio_list bio_list;
  1993. struct bio *bio;
  1994. int is_replace = 0;
  1995. int ret;
  1996. bio_list_init(&bio_list);
  1997. if (rbio->real_stripes - rbio->nr_data == 1) {
  1998. p_stripe = rbio->real_stripes - 1;
  1999. } else if (rbio->real_stripes - rbio->nr_data == 2) {
  2000. p_stripe = rbio->real_stripes - 2;
  2001. q_stripe = rbio->real_stripes - 1;
  2002. } else {
  2003. BUG();
  2004. }
  2005. if (bbio->num_tgtdevs && bbio->tgtdev_map[rbio->scrubp]) {
  2006. is_replace = 1;
  2007. bitmap_copy(pbitmap, rbio->dbitmap, rbio->stripe_npages);
  2008. }
  2009. /*
  2010. * Because the higher layers(scrubber) are unlikely to
  2011. * use this area of the disk again soon, so don't cache
  2012. * it.
  2013. */
  2014. clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  2015. if (!need_check)
  2016. goto writeback;
  2017. p_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  2018. if (!p_page)
  2019. goto cleanup;
  2020. SetPageUptodate(p_page);
  2021. if (q_stripe != -1) {
  2022. q_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  2023. if (!q_page) {
  2024. __free_page(p_page);
  2025. goto cleanup;
  2026. }
  2027. SetPageUptodate(q_page);
  2028. }
  2029. atomic_set(&rbio->error, 0);
  2030. for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
  2031. struct page *p;
  2032. void *parity;
  2033. /* first collect one page from each data stripe */
  2034. for (stripe = 0; stripe < nr_data; stripe++) {
  2035. p = page_in_rbio(rbio, stripe, pagenr, 0);
  2036. pointers[stripe] = kmap(p);
  2037. }
  2038. /* then add the parity stripe */
  2039. pointers[stripe++] = kmap(p_page);
  2040. if (q_stripe != -1) {
  2041. /*
  2042. * raid6, add the qstripe and call the
  2043. * library function to fill in our p/q
  2044. */
  2045. pointers[stripe++] = kmap(q_page);
  2046. raid6_call.gen_syndrome(rbio->real_stripes, PAGE_SIZE,
  2047. pointers);
  2048. } else {
  2049. /* raid5 */
  2050. memcpy(pointers[nr_data], pointers[0], PAGE_SIZE);
  2051. run_xor(pointers + 1, nr_data - 1, PAGE_SIZE);
  2052. }
  2053. /* Check scrubbing parity and repair it */
  2054. p = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
  2055. parity = kmap(p);
  2056. if (memcmp(parity, pointers[rbio->scrubp], PAGE_SIZE))
  2057. memcpy(parity, pointers[rbio->scrubp], PAGE_SIZE);
  2058. else
  2059. /* Parity is right, needn't writeback */
  2060. bitmap_clear(rbio->dbitmap, pagenr, 1);
  2061. kunmap(p);
  2062. for (stripe = 0; stripe < rbio->real_stripes; stripe++)
  2063. kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
  2064. }
  2065. __free_page(p_page);
  2066. if (q_page)
  2067. __free_page(q_page);
  2068. writeback:
  2069. /*
  2070. * time to start writing. Make bios for everything from the
  2071. * higher layers (the bio_list in our rbio) and our p/q. Ignore
  2072. * everything else.
  2073. */
  2074. for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
  2075. struct page *page;
  2076. page = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
  2077. ret = rbio_add_io_page(rbio, &bio_list,
  2078. page, rbio->scrubp, pagenr, rbio->stripe_len);
  2079. if (ret)
  2080. goto cleanup;
  2081. }
  2082. if (!is_replace)
  2083. goto submit_write;
  2084. for_each_set_bit(pagenr, pbitmap, rbio->stripe_npages) {
  2085. struct page *page;
  2086. page = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
  2087. ret = rbio_add_io_page(rbio, &bio_list, page,
  2088. bbio->tgtdev_map[rbio->scrubp],
  2089. pagenr, rbio->stripe_len);
  2090. if (ret)
  2091. goto cleanup;
  2092. }
  2093. submit_write:
  2094. nr_data = bio_list_size(&bio_list);
  2095. if (!nr_data) {
  2096. /* Every parity is right */
  2097. rbio_orig_end_io(rbio, 0);
  2098. return;
  2099. }
  2100. atomic_set(&rbio->stripes_pending, nr_data);
  2101. while (1) {
  2102. bio = bio_list_pop(&bio_list);
  2103. if (!bio)
  2104. break;
  2105. bio->bi_private = rbio;
  2106. bio->bi_end_io = raid_write_end_io;
  2107. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  2108. submit_bio(bio);
  2109. }
  2110. return;
  2111. cleanup:
  2112. rbio_orig_end_io(rbio, -EIO);
  2113. }
  2114. static inline int is_data_stripe(struct btrfs_raid_bio *rbio, int stripe)
  2115. {
  2116. if (stripe >= 0 && stripe < rbio->nr_data)
  2117. return 1;
  2118. return 0;
  2119. }
  2120. /*
  2121. * While we're doing the parity check and repair, we could have errors
  2122. * in reading pages off the disk. This checks for errors and if we're
  2123. * not able to read the page it'll trigger parity reconstruction. The
  2124. * parity scrub will be finished after we've reconstructed the failed
  2125. * stripes
  2126. */
  2127. static void validate_rbio_for_parity_scrub(struct btrfs_raid_bio *rbio)
  2128. {
  2129. if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
  2130. goto cleanup;
  2131. if (rbio->faila >= 0 || rbio->failb >= 0) {
  2132. int dfail = 0, failp = -1;
  2133. if (is_data_stripe(rbio, rbio->faila))
  2134. dfail++;
  2135. else if (is_parity_stripe(rbio->faila))
  2136. failp = rbio->faila;
  2137. if (is_data_stripe(rbio, rbio->failb))
  2138. dfail++;
  2139. else if (is_parity_stripe(rbio->failb))
  2140. failp = rbio->failb;
  2141. /*
  2142. * Because we can not use a scrubbing parity to repair
  2143. * the data, so the capability of the repair is declined.
  2144. * (In the case of RAID5, we can not repair anything)
  2145. */
  2146. if (dfail > rbio->bbio->max_errors - 1)
  2147. goto cleanup;
  2148. /*
  2149. * If all data is good, only parity is correctly, just
  2150. * repair the parity.
  2151. */
  2152. if (dfail == 0) {
  2153. finish_parity_scrub(rbio, 0);
  2154. return;
  2155. }
  2156. /*
  2157. * Here means we got one corrupted data stripe and one
  2158. * corrupted parity on RAID6, if the corrupted parity
  2159. * is scrubbing parity, luckily, use the other one to repair
  2160. * the data, or we can not repair the data stripe.
  2161. */
  2162. if (failp != rbio->scrubp)
  2163. goto cleanup;
  2164. __raid_recover_end_io(rbio);
  2165. } else {
  2166. finish_parity_scrub(rbio, 1);
  2167. }
  2168. return;
  2169. cleanup:
  2170. rbio_orig_end_io(rbio, -EIO);
  2171. }
  2172. /*
  2173. * end io for the read phase of the rmw cycle. All the bios here are physical
  2174. * stripe bios we've read from the disk so we can recalculate the parity of the
  2175. * stripe.
  2176. *
  2177. * This will usually kick off finish_rmw once all the bios are read in, but it
  2178. * may trigger parity reconstruction if we had any errors along the way
  2179. */
  2180. static void raid56_parity_scrub_end_io(struct bio *bio)
  2181. {
  2182. struct btrfs_raid_bio *rbio = bio->bi_private;
  2183. if (bio->bi_status)
  2184. fail_bio_stripe(rbio, bio);
  2185. else
  2186. set_bio_pages_uptodate(bio);
  2187. bio_put(bio);
  2188. if (!atomic_dec_and_test(&rbio->stripes_pending))
  2189. return;
  2190. /*
  2191. * this will normally call finish_rmw to start our write
  2192. * but if there are any failed stripes we'll reconstruct
  2193. * from parity first
  2194. */
  2195. validate_rbio_for_parity_scrub(rbio);
  2196. }
  2197. static void raid56_parity_scrub_stripe(struct btrfs_raid_bio *rbio)
  2198. {
  2199. int bios_to_read = 0;
  2200. struct bio_list bio_list;
  2201. int ret;
  2202. int pagenr;
  2203. int stripe;
  2204. struct bio *bio;
  2205. ret = alloc_rbio_essential_pages(rbio);
  2206. if (ret)
  2207. goto cleanup;
  2208. bio_list_init(&bio_list);
  2209. atomic_set(&rbio->error, 0);
  2210. /*
  2211. * build a list of bios to read all the missing parts of this
  2212. * stripe
  2213. */
  2214. for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
  2215. for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
  2216. struct page *page;
  2217. /*
  2218. * we want to find all the pages missing from
  2219. * the rbio and read them from the disk. If
  2220. * page_in_rbio finds a page in the bio list
  2221. * we don't need to read it off the stripe.
  2222. */
  2223. page = page_in_rbio(rbio, stripe, pagenr, 1);
  2224. if (page)
  2225. continue;
  2226. page = rbio_stripe_page(rbio, stripe, pagenr);
  2227. /*
  2228. * the bio cache may have handed us an uptodate
  2229. * page. If so, be happy and use it
  2230. */
  2231. if (PageUptodate(page))
  2232. continue;
  2233. ret = rbio_add_io_page(rbio, &bio_list, page,
  2234. stripe, pagenr, rbio->stripe_len);
  2235. if (ret)
  2236. goto cleanup;
  2237. }
  2238. }
  2239. bios_to_read = bio_list_size(&bio_list);
  2240. if (!bios_to_read) {
  2241. /*
  2242. * this can happen if others have merged with
  2243. * us, it means there is nothing left to read.
  2244. * But if there are missing devices it may not be
  2245. * safe to do the full stripe write yet.
  2246. */
  2247. goto finish;
  2248. }
  2249. /*
  2250. * the bbio may be freed once we submit the last bio. Make sure
  2251. * not to touch it after that
  2252. */
  2253. atomic_set(&rbio->stripes_pending, bios_to_read);
  2254. while (1) {
  2255. bio = bio_list_pop(&bio_list);
  2256. if (!bio)
  2257. break;
  2258. bio->bi_private = rbio;
  2259. bio->bi_end_io = raid56_parity_scrub_end_io;
  2260. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  2261. btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
  2262. submit_bio(bio);
  2263. }
  2264. /* the actual write will happen once the reads are done */
  2265. return;
  2266. cleanup:
  2267. rbio_orig_end_io(rbio, -EIO);
  2268. return;
  2269. finish:
  2270. validate_rbio_for_parity_scrub(rbio);
  2271. }
  2272. static void scrub_parity_work(struct btrfs_work *work)
  2273. {
  2274. struct btrfs_raid_bio *rbio;
  2275. rbio = container_of(work, struct btrfs_raid_bio, work);
  2276. raid56_parity_scrub_stripe(rbio);
  2277. }
  2278. static void async_scrub_parity(struct btrfs_raid_bio *rbio)
  2279. {
  2280. btrfs_init_work(&rbio->work, btrfs_rmw_helper,
  2281. scrub_parity_work, NULL, NULL);
  2282. btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
  2283. }
  2284. void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio)
  2285. {
  2286. if (!lock_stripe_add(rbio))
  2287. async_scrub_parity(rbio);
  2288. }
  2289. /* The following code is used for dev replace of a missing RAID 5/6 device. */
  2290. struct btrfs_raid_bio *
  2291. raid56_alloc_missing_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
  2292. struct btrfs_bio *bbio, u64 length)
  2293. {
  2294. struct btrfs_raid_bio *rbio;
  2295. rbio = alloc_rbio(fs_info, bbio, length);
  2296. if (IS_ERR(rbio))
  2297. return NULL;
  2298. rbio->operation = BTRFS_RBIO_REBUILD_MISSING;
  2299. bio_list_add(&rbio->bio_list, bio);
  2300. /*
  2301. * This is a special bio which is used to hold the completion handler
  2302. * and make the scrub rbio is similar to the other types
  2303. */
  2304. ASSERT(!bio->bi_iter.bi_size);
  2305. rbio->faila = find_logical_bio_stripe(rbio, bio);
  2306. if (rbio->faila == -1) {
  2307. BUG();
  2308. kfree(rbio);
  2309. return NULL;
  2310. }
  2311. /*
  2312. * When we get bbio, we have already increased bio_counter, record it
  2313. * so we can free it at rbio_orig_end_io()
  2314. */
  2315. rbio->generic_bio_cnt = 1;
  2316. return rbio;
  2317. }
  2318. static void missing_raid56_work(struct btrfs_work *work)
  2319. {
  2320. struct btrfs_raid_bio *rbio;
  2321. rbio = container_of(work, struct btrfs_raid_bio, work);
  2322. __raid56_parity_recover(rbio);
  2323. }
  2324. static void async_missing_raid56(struct btrfs_raid_bio *rbio)
  2325. {
  2326. btrfs_init_work(&rbio->work, btrfs_rmw_helper,
  2327. missing_raid56_work, NULL, NULL);
  2328. btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
  2329. }
  2330. void raid56_submit_missing_rbio(struct btrfs_raid_bio *rbio)
  2331. {
  2332. if (!lock_stripe_add(rbio))
  2333. async_missing_raid56(rbio);
  2334. }