raid56.c 68 KB

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