raid56.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2012 Fusion-io All rights reserved.
  4. * Copyright (C) 2012 Intel Corp. All rights reserved.
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/bio.h>
  8. #include <linux/slab.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/raid/pq.h>
  11. #include <linux/hash.h>
  12. #include <linux/list_sort.h>
  13. #include <linux/raid/xor.h>
  14. #include <linux/mm.h>
  15. #include "messages.h"
  16. #include "ctree.h"
  17. #include "disk-io.h"
  18. #include "volumes.h"
  19. #include "raid56.h"
  20. #include "async-thread.h"
  21. #include "file-item.h"
  22. #include "btrfs_inode.h"
  23. /* set when additional merges to this rbio are not allowed */
  24. #define RBIO_RMW_LOCKED_BIT 1
  25. /*
  26. * set when this rbio is sitting in the hash, but it is just a cache
  27. * of past RMW
  28. */
  29. #define RBIO_CACHE_BIT 2
  30. /*
  31. * set when it is safe to trust the stripe_pages for caching
  32. */
  33. #define RBIO_CACHE_READY_BIT 3
  34. #define RBIO_CACHE_SIZE 1024
  35. #define BTRFS_STRIPE_HASH_TABLE_BITS 11
  36. static void dump_bioc(const struct btrfs_fs_info *fs_info, const struct btrfs_io_context *bioc)
  37. {
  38. if (unlikely(!bioc)) {
  39. btrfs_crit(fs_info, "bioc=NULL");
  40. return;
  41. }
  42. btrfs_crit(fs_info,
  43. "bioc logical=%llu full_stripe=%llu size=%llu map_type=0x%llx mirror=%u replace_nr_stripes=%u replace_stripe_src=%d num_stripes=%u",
  44. bioc->logical, bioc->full_stripe_logical, bioc->size,
  45. bioc->map_type, bioc->mirror_num, bioc->replace_nr_stripes,
  46. bioc->replace_stripe_src, bioc->num_stripes);
  47. for (int i = 0; i < bioc->num_stripes; i++) {
  48. btrfs_crit(fs_info, " nr=%d devid=%llu physical=%llu",
  49. i, bioc->stripes[i].dev->devid,
  50. bioc->stripes[i].physical);
  51. }
  52. }
  53. static void btrfs_dump_rbio(const struct btrfs_fs_info *fs_info,
  54. const struct btrfs_raid_bio *rbio)
  55. {
  56. if (!IS_ENABLED(CONFIG_BTRFS_ASSERT))
  57. return;
  58. dump_bioc(fs_info, rbio->bioc);
  59. btrfs_crit(fs_info,
  60. "rbio flags=0x%lx nr_sectors=%u nr_data=%u real_stripes=%u stripe_nsectors=%u scrubp=%u dbitmap=0x%lx",
  61. rbio->flags, rbio->nr_sectors, rbio->nr_data,
  62. rbio->real_stripes, rbio->stripe_nsectors,
  63. rbio->scrubp, rbio->dbitmap);
  64. }
  65. #define ASSERT_RBIO(expr, rbio) \
  66. ({ \
  67. if (IS_ENABLED(CONFIG_BTRFS_ASSERT) && unlikely(!(expr))) { \
  68. const struct btrfs_fs_info *__fs_info = (rbio)->bioc ? \
  69. (rbio)->bioc->fs_info : NULL; \
  70. \
  71. btrfs_dump_rbio(__fs_info, (rbio)); \
  72. } \
  73. ASSERT((expr)); \
  74. })
  75. #define ASSERT_RBIO_STRIPE(expr, rbio, stripe_nr) \
  76. ({ \
  77. if (IS_ENABLED(CONFIG_BTRFS_ASSERT) && unlikely(!(expr))) { \
  78. const struct btrfs_fs_info *__fs_info = (rbio)->bioc ? \
  79. (rbio)->bioc->fs_info : NULL; \
  80. \
  81. btrfs_dump_rbio(__fs_info, (rbio)); \
  82. btrfs_crit(__fs_info, "stripe_nr=%d", (stripe_nr)); \
  83. } \
  84. ASSERT((expr)); \
  85. })
  86. #define ASSERT_RBIO_SECTOR(expr, rbio, sector_nr) \
  87. ({ \
  88. if (IS_ENABLED(CONFIG_BTRFS_ASSERT) && unlikely(!(expr))) { \
  89. const struct btrfs_fs_info *__fs_info = (rbio)->bioc ? \
  90. (rbio)->bioc->fs_info : NULL; \
  91. \
  92. btrfs_dump_rbio(__fs_info, (rbio)); \
  93. btrfs_crit(__fs_info, "sector_nr=%d", (sector_nr)); \
  94. } \
  95. ASSERT((expr)); \
  96. })
  97. #define ASSERT_RBIO_LOGICAL(expr, rbio, logical) \
  98. ({ \
  99. if (IS_ENABLED(CONFIG_BTRFS_ASSERT) && unlikely(!(expr))) { \
  100. const struct btrfs_fs_info *__fs_info = (rbio)->bioc ? \
  101. (rbio)->bioc->fs_info : NULL; \
  102. \
  103. btrfs_dump_rbio(__fs_info, (rbio)); \
  104. btrfs_crit(__fs_info, "logical=%llu", (logical)); \
  105. } \
  106. ASSERT((expr)); \
  107. })
  108. /* Used by the raid56 code to lock stripes for read/modify/write */
  109. struct btrfs_stripe_hash {
  110. struct list_head hash_list;
  111. spinlock_t lock;
  112. };
  113. /* Used by the raid56 code to lock stripes for read/modify/write */
  114. struct btrfs_stripe_hash_table {
  115. struct list_head stripe_cache;
  116. spinlock_t cache_lock;
  117. int cache_size;
  118. struct btrfs_stripe_hash table[];
  119. };
  120. /*
  121. * A bvec like structure to present a sector inside a page.
  122. *
  123. * Unlike bvec we don't need bvlen, as it's fixed to sectorsize.
  124. */
  125. struct sector_ptr {
  126. struct page *page;
  127. unsigned int pgoff:24;
  128. unsigned int uptodate:8;
  129. };
  130. static void rmw_rbio_work(struct work_struct *work);
  131. static void rmw_rbio_work_locked(struct work_struct *work);
  132. static void index_rbio_pages(struct btrfs_raid_bio *rbio);
  133. static int alloc_rbio_pages(struct btrfs_raid_bio *rbio);
  134. static int finish_parity_scrub(struct btrfs_raid_bio *rbio);
  135. static void scrub_rbio_work_locked(struct work_struct *work);
  136. static void free_raid_bio_pointers(struct btrfs_raid_bio *rbio)
  137. {
  138. bitmap_free(rbio->error_bitmap);
  139. kfree(rbio->stripe_pages);
  140. kfree(rbio->bio_sectors);
  141. kfree(rbio->stripe_sectors);
  142. kfree(rbio->finish_pointers);
  143. }
  144. static void free_raid_bio(struct btrfs_raid_bio *rbio)
  145. {
  146. int i;
  147. if (!refcount_dec_and_test(&rbio->refs))
  148. return;
  149. WARN_ON(!list_empty(&rbio->stripe_cache));
  150. WARN_ON(!list_empty(&rbio->hash_list));
  151. WARN_ON(!bio_list_empty(&rbio->bio_list));
  152. for (i = 0; i < rbio->nr_pages; i++) {
  153. if (rbio->stripe_pages[i]) {
  154. __free_page(rbio->stripe_pages[i]);
  155. rbio->stripe_pages[i] = NULL;
  156. }
  157. }
  158. btrfs_put_bioc(rbio->bioc);
  159. free_raid_bio_pointers(rbio);
  160. kfree(rbio);
  161. }
  162. static void start_async_work(struct btrfs_raid_bio *rbio, work_func_t work_func)
  163. {
  164. INIT_WORK(&rbio->work, work_func);
  165. queue_work(rbio->bioc->fs_info->rmw_workers, &rbio->work);
  166. }
  167. /*
  168. * the stripe hash table is used for locking, and to collect
  169. * bios in hopes of making a full stripe
  170. */
  171. int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
  172. {
  173. struct btrfs_stripe_hash_table *table;
  174. struct btrfs_stripe_hash_table *x;
  175. struct btrfs_stripe_hash *cur;
  176. struct btrfs_stripe_hash *h;
  177. unsigned int num_entries = 1U << BTRFS_STRIPE_HASH_TABLE_BITS;
  178. if (info->stripe_hash_table)
  179. return 0;
  180. /*
  181. * The table is large, starting with order 4 and can go as high as
  182. * order 7 in case lock debugging is turned on.
  183. *
  184. * Try harder to allocate and fallback to vmalloc to lower the chance
  185. * of a failing mount.
  186. */
  187. table = kvzalloc(struct_size(table, table, num_entries), GFP_KERNEL);
  188. if (!table)
  189. return -ENOMEM;
  190. spin_lock_init(&table->cache_lock);
  191. INIT_LIST_HEAD(&table->stripe_cache);
  192. h = table->table;
  193. for (unsigned int i = 0; i < num_entries; i++) {
  194. cur = h + i;
  195. INIT_LIST_HEAD(&cur->hash_list);
  196. spin_lock_init(&cur->lock);
  197. }
  198. x = cmpxchg(&info->stripe_hash_table, NULL, table);
  199. kvfree(x);
  200. return 0;
  201. }
  202. /*
  203. * caching an rbio means to copy anything from the
  204. * bio_sectors array into the stripe_pages array. We
  205. * use the page uptodate bit in the stripe cache array
  206. * to indicate if it has valid data
  207. *
  208. * once the caching is done, we set the cache ready
  209. * bit.
  210. */
  211. static void cache_rbio_pages(struct btrfs_raid_bio *rbio)
  212. {
  213. int i;
  214. int ret;
  215. ret = alloc_rbio_pages(rbio);
  216. if (ret)
  217. return;
  218. for (i = 0; i < rbio->nr_sectors; i++) {
  219. /* Some range not covered by bio (partial write), skip it */
  220. if (!rbio->bio_sectors[i].page) {
  221. /*
  222. * Even if the sector is not covered by bio, if it is
  223. * a data sector it should still be uptodate as it is
  224. * read from disk.
  225. */
  226. if (i < rbio->nr_data * rbio->stripe_nsectors)
  227. ASSERT(rbio->stripe_sectors[i].uptodate);
  228. continue;
  229. }
  230. ASSERT(rbio->stripe_sectors[i].page);
  231. memcpy_page(rbio->stripe_sectors[i].page,
  232. rbio->stripe_sectors[i].pgoff,
  233. rbio->bio_sectors[i].page,
  234. rbio->bio_sectors[i].pgoff,
  235. rbio->bioc->fs_info->sectorsize);
  236. rbio->stripe_sectors[i].uptodate = 1;
  237. }
  238. set_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  239. }
  240. /*
  241. * we hash on the first logical address of the stripe
  242. */
  243. static int rbio_bucket(struct btrfs_raid_bio *rbio)
  244. {
  245. u64 num = rbio->bioc->full_stripe_logical;
  246. /*
  247. * we shift down quite a bit. We're using byte
  248. * addressing, and most of the lower bits are zeros.
  249. * This tends to upset hash_64, and it consistently
  250. * returns just one or two different values.
  251. *
  252. * shifting off the lower bits fixes things.
  253. */
  254. return hash_64(num >> 16, BTRFS_STRIPE_HASH_TABLE_BITS);
  255. }
  256. static bool full_page_sectors_uptodate(struct btrfs_raid_bio *rbio,
  257. unsigned int page_nr)
  258. {
  259. const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
  260. const u32 sectors_per_page = PAGE_SIZE / sectorsize;
  261. int i;
  262. ASSERT(page_nr < rbio->nr_pages);
  263. for (i = sectors_per_page * page_nr;
  264. i < sectors_per_page * page_nr + sectors_per_page;
  265. i++) {
  266. if (!rbio->stripe_sectors[i].uptodate)
  267. return false;
  268. }
  269. return true;
  270. }
  271. /*
  272. * Update the stripe_sectors[] array to use correct page and pgoff
  273. *
  274. * Should be called every time any page pointer in stripes_pages[] got modified.
  275. */
  276. static void index_stripe_sectors(struct btrfs_raid_bio *rbio)
  277. {
  278. const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
  279. u32 offset;
  280. int i;
  281. for (i = 0, offset = 0; i < rbio->nr_sectors; i++, offset += sectorsize) {
  282. int page_index = offset >> PAGE_SHIFT;
  283. ASSERT(page_index < rbio->nr_pages);
  284. rbio->stripe_sectors[i].page = rbio->stripe_pages[page_index];
  285. rbio->stripe_sectors[i].pgoff = offset_in_page(offset);
  286. }
  287. }
  288. static void steal_rbio_page(struct btrfs_raid_bio *src,
  289. struct btrfs_raid_bio *dest, int page_nr)
  290. {
  291. const u32 sectorsize = src->bioc->fs_info->sectorsize;
  292. const u32 sectors_per_page = PAGE_SIZE / sectorsize;
  293. int i;
  294. if (dest->stripe_pages[page_nr])
  295. __free_page(dest->stripe_pages[page_nr]);
  296. dest->stripe_pages[page_nr] = src->stripe_pages[page_nr];
  297. src->stripe_pages[page_nr] = NULL;
  298. /* Also update the sector->uptodate bits. */
  299. for (i = sectors_per_page * page_nr;
  300. i < sectors_per_page * page_nr + sectors_per_page; i++)
  301. dest->stripe_sectors[i].uptodate = true;
  302. }
  303. static bool is_data_stripe_page(struct btrfs_raid_bio *rbio, int page_nr)
  304. {
  305. const int sector_nr = (page_nr << PAGE_SHIFT) >>
  306. rbio->bioc->fs_info->sectorsize_bits;
  307. /*
  308. * We have ensured PAGE_SIZE is aligned with sectorsize, thus
  309. * we won't have a page which is half data half parity.
  310. *
  311. * Thus if the first sector of the page belongs to data stripes, then
  312. * the full page belongs to data stripes.
  313. */
  314. return (sector_nr < rbio->nr_data * rbio->stripe_nsectors);
  315. }
  316. /*
  317. * Stealing an rbio means taking all the uptodate pages from the stripe array
  318. * in the source rbio and putting them into the destination rbio.
  319. *
  320. * This will also update the involved stripe_sectors[] which are referring to
  321. * the old pages.
  322. */
  323. static void steal_rbio(struct btrfs_raid_bio *src, struct btrfs_raid_bio *dest)
  324. {
  325. int i;
  326. if (!test_bit(RBIO_CACHE_READY_BIT, &src->flags))
  327. return;
  328. for (i = 0; i < dest->nr_pages; i++) {
  329. struct page *p = src->stripe_pages[i];
  330. /*
  331. * We don't need to steal P/Q pages as they will always be
  332. * regenerated for RMW or full write anyway.
  333. */
  334. if (!is_data_stripe_page(src, i))
  335. continue;
  336. /*
  337. * If @src already has RBIO_CACHE_READY_BIT, it should have
  338. * all data stripe pages present and uptodate.
  339. */
  340. ASSERT(p);
  341. ASSERT(full_page_sectors_uptodate(src, i));
  342. steal_rbio_page(src, dest, i);
  343. }
  344. index_stripe_sectors(dest);
  345. index_stripe_sectors(src);
  346. }
  347. /*
  348. * merging means we take the bio_list from the victim and
  349. * splice it into the destination. The victim should
  350. * be discarded afterwards.
  351. *
  352. * must be called with dest->rbio_list_lock held
  353. */
  354. static void merge_rbio(struct btrfs_raid_bio *dest,
  355. struct btrfs_raid_bio *victim)
  356. {
  357. bio_list_merge_init(&dest->bio_list, &victim->bio_list);
  358. dest->bio_list_bytes += victim->bio_list_bytes;
  359. /* Also inherit the bitmaps from @victim. */
  360. bitmap_or(&dest->dbitmap, &victim->dbitmap, &dest->dbitmap,
  361. dest->stripe_nsectors);
  362. }
  363. /*
  364. * used to prune items that are in the cache. The caller
  365. * must hold the hash table lock.
  366. */
  367. static void __remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
  368. {
  369. int bucket = rbio_bucket(rbio);
  370. struct btrfs_stripe_hash_table *table;
  371. struct btrfs_stripe_hash *h;
  372. int freeit = 0;
  373. /*
  374. * check the bit again under the hash table lock.
  375. */
  376. if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
  377. return;
  378. table = rbio->bioc->fs_info->stripe_hash_table;
  379. h = table->table + bucket;
  380. /* hold the lock for the bucket because we may be
  381. * removing it from the hash table
  382. */
  383. spin_lock(&h->lock);
  384. /*
  385. * hold the lock for the bio list because we need
  386. * to make sure the bio list is empty
  387. */
  388. spin_lock(&rbio->bio_list_lock);
  389. if (test_and_clear_bit(RBIO_CACHE_BIT, &rbio->flags)) {
  390. list_del_init(&rbio->stripe_cache);
  391. table->cache_size -= 1;
  392. freeit = 1;
  393. /* if the bio list isn't empty, this rbio is
  394. * still involved in an IO. We take it out
  395. * of the cache list, and drop the ref that
  396. * was held for the list.
  397. *
  398. * If the bio_list was empty, we also remove
  399. * the rbio from the hash_table, and drop
  400. * the corresponding ref
  401. */
  402. if (bio_list_empty(&rbio->bio_list)) {
  403. if (!list_empty(&rbio->hash_list)) {
  404. list_del_init(&rbio->hash_list);
  405. refcount_dec(&rbio->refs);
  406. BUG_ON(!list_empty(&rbio->plug_list));
  407. }
  408. }
  409. }
  410. spin_unlock(&rbio->bio_list_lock);
  411. spin_unlock(&h->lock);
  412. if (freeit)
  413. free_raid_bio(rbio);
  414. }
  415. /*
  416. * prune a given rbio from the cache
  417. */
  418. static void remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
  419. {
  420. struct btrfs_stripe_hash_table *table;
  421. if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
  422. return;
  423. table = rbio->bioc->fs_info->stripe_hash_table;
  424. spin_lock(&table->cache_lock);
  425. __remove_rbio_from_cache(rbio);
  426. spin_unlock(&table->cache_lock);
  427. }
  428. /*
  429. * remove everything in the cache
  430. */
  431. static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
  432. {
  433. struct btrfs_stripe_hash_table *table;
  434. struct btrfs_raid_bio *rbio;
  435. table = info->stripe_hash_table;
  436. spin_lock(&table->cache_lock);
  437. while (!list_empty(&table->stripe_cache)) {
  438. rbio = list_entry(table->stripe_cache.next,
  439. struct btrfs_raid_bio,
  440. stripe_cache);
  441. __remove_rbio_from_cache(rbio);
  442. }
  443. spin_unlock(&table->cache_lock);
  444. }
  445. /*
  446. * remove all cached entries and free the hash table
  447. * used by unmount
  448. */
  449. void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
  450. {
  451. if (!info->stripe_hash_table)
  452. return;
  453. btrfs_clear_rbio_cache(info);
  454. kvfree(info->stripe_hash_table);
  455. info->stripe_hash_table = NULL;
  456. }
  457. /*
  458. * insert an rbio into the stripe cache. It
  459. * must have already been prepared by calling
  460. * cache_rbio_pages
  461. *
  462. * If this rbio was already cached, it gets
  463. * moved to the front of the lru.
  464. *
  465. * If the size of the rbio cache is too big, we
  466. * prune an item.
  467. */
  468. static void cache_rbio(struct btrfs_raid_bio *rbio)
  469. {
  470. struct btrfs_stripe_hash_table *table;
  471. if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
  472. return;
  473. table = rbio->bioc->fs_info->stripe_hash_table;
  474. spin_lock(&table->cache_lock);
  475. spin_lock(&rbio->bio_list_lock);
  476. /* bump our ref if we were not in the list before */
  477. if (!test_and_set_bit(RBIO_CACHE_BIT, &rbio->flags))
  478. refcount_inc(&rbio->refs);
  479. if (!list_empty(&rbio->stripe_cache)){
  480. list_move(&rbio->stripe_cache, &table->stripe_cache);
  481. } else {
  482. list_add(&rbio->stripe_cache, &table->stripe_cache);
  483. table->cache_size += 1;
  484. }
  485. spin_unlock(&rbio->bio_list_lock);
  486. if (table->cache_size > RBIO_CACHE_SIZE) {
  487. struct btrfs_raid_bio *found;
  488. found = list_entry(table->stripe_cache.prev,
  489. struct btrfs_raid_bio,
  490. stripe_cache);
  491. if (found != rbio)
  492. __remove_rbio_from_cache(found);
  493. }
  494. spin_unlock(&table->cache_lock);
  495. }
  496. /*
  497. * helper function to run the xor_blocks api. It is only
  498. * able to do MAX_XOR_BLOCKS at a time, so we need to
  499. * loop through.
  500. */
  501. static void run_xor(void **pages, int src_cnt, ssize_t len)
  502. {
  503. int src_off = 0;
  504. int xor_src_cnt = 0;
  505. void *dest = pages[src_cnt];
  506. while(src_cnt > 0) {
  507. xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
  508. xor_blocks(xor_src_cnt, len, dest, pages + src_off);
  509. src_cnt -= xor_src_cnt;
  510. src_off += xor_src_cnt;
  511. }
  512. }
  513. /*
  514. * Returns true if the bio list inside this rbio covers an entire stripe (no
  515. * rmw required).
  516. */
  517. static int rbio_is_full(struct btrfs_raid_bio *rbio)
  518. {
  519. unsigned long size = rbio->bio_list_bytes;
  520. int ret = 1;
  521. spin_lock(&rbio->bio_list_lock);
  522. if (size != rbio->nr_data * BTRFS_STRIPE_LEN)
  523. ret = 0;
  524. BUG_ON(size > rbio->nr_data * BTRFS_STRIPE_LEN);
  525. spin_unlock(&rbio->bio_list_lock);
  526. return ret;
  527. }
  528. /*
  529. * returns 1 if it is safe to merge two rbios together.
  530. * The merging is safe if the two rbios correspond to
  531. * the same stripe and if they are both going in the same
  532. * direction (read vs write), and if neither one is
  533. * locked for final IO
  534. *
  535. * The caller is responsible for locking such that
  536. * rmw_locked is safe to test
  537. */
  538. static int rbio_can_merge(struct btrfs_raid_bio *last,
  539. struct btrfs_raid_bio *cur)
  540. {
  541. if (test_bit(RBIO_RMW_LOCKED_BIT, &last->flags) ||
  542. test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags))
  543. return 0;
  544. /*
  545. * we can't merge with cached rbios, since the
  546. * idea is that when we merge the destination
  547. * rbio is going to run our IO for us. We can
  548. * steal from cached rbios though, other functions
  549. * handle that.
  550. */
  551. if (test_bit(RBIO_CACHE_BIT, &last->flags) ||
  552. test_bit(RBIO_CACHE_BIT, &cur->flags))
  553. return 0;
  554. if (last->bioc->full_stripe_logical != cur->bioc->full_stripe_logical)
  555. return 0;
  556. /* we can't merge with different operations */
  557. if (last->operation != cur->operation)
  558. return 0;
  559. /*
  560. * We've need read the full stripe from the drive.
  561. * check and repair the parity and write the new results.
  562. *
  563. * We're not allowed to add any new bios to the
  564. * bio list here, anyone else that wants to
  565. * change this stripe needs to do their own rmw.
  566. */
  567. if (last->operation == BTRFS_RBIO_PARITY_SCRUB)
  568. return 0;
  569. if (last->operation == BTRFS_RBIO_READ_REBUILD)
  570. return 0;
  571. return 1;
  572. }
  573. static unsigned int rbio_stripe_sector_index(const struct btrfs_raid_bio *rbio,
  574. unsigned int stripe_nr,
  575. unsigned int sector_nr)
  576. {
  577. ASSERT_RBIO_STRIPE(stripe_nr < rbio->real_stripes, rbio, stripe_nr);
  578. ASSERT_RBIO_SECTOR(sector_nr < rbio->stripe_nsectors, rbio, sector_nr);
  579. return stripe_nr * rbio->stripe_nsectors + sector_nr;
  580. }
  581. /* Return a sector from rbio->stripe_sectors, not from the bio list */
  582. static struct sector_ptr *rbio_stripe_sector(const struct btrfs_raid_bio *rbio,
  583. unsigned int stripe_nr,
  584. unsigned int sector_nr)
  585. {
  586. return &rbio->stripe_sectors[rbio_stripe_sector_index(rbio, stripe_nr,
  587. sector_nr)];
  588. }
  589. /* Grab a sector inside P stripe */
  590. static struct sector_ptr *rbio_pstripe_sector(const struct btrfs_raid_bio *rbio,
  591. unsigned int sector_nr)
  592. {
  593. return rbio_stripe_sector(rbio, rbio->nr_data, sector_nr);
  594. }
  595. /* Grab a sector inside Q stripe, return NULL if not RAID6 */
  596. static struct sector_ptr *rbio_qstripe_sector(const struct btrfs_raid_bio *rbio,
  597. unsigned int sector_nr)
  598. {
  599. if (rbio->nr_data + 1 == rbio->real_stripes)
  600. return NULL;
  601. return rbio_stripe_sector(rbio, rbio->nr_data + 1, sector_nr);
  602. }
  603. /*
  604. * The first stripe in the table for a logical address
  605. * has the lock. rbios are added in one of three ways:
  606. *
  607. * 1) Nobody has the stripe locked yet. The rbio is given
  608. * the lock and 0 is returned. The caller must start the IO
  609. * themselves.
  610. *
  611. * 2) Someone has the stripe locked, but we're able to merge
  612. * with the lock owner. The rbio is freed and the IO will
  613. * start automatically along with the existing rbio. 1 is returned.
  614. *
  615. * 3) Someone has the stripe locked, but we're not able to merge.
  616. * The rbio is added to the lock owner's plug list, or merged into
  617. * an rbio already on the plug list. When the lock owner unlocks,
  618. * the next rbio on the list is run and the IO is started automatically.
  619. * 1 is returned
  620. *
  621. * If we return 0, the caller still owns the rbio and must continue with
  622. * IO submission. If we return 1, the caller must assume the rbio has
  623. * already been freed.
  624. */
  625. static noinline int lock_stripe_add(struct btrfs_raid_bio *rbio)
  626. {
  627. struct btrfs_stripe_hash *h;
  628. struct btrfs_raid_bio *cur;
  629. struct btrfs_raid_bio *pending;
  630. struct btrfs_raid_bio *freeit = NULL;
  631. struct btrfs_raid_bio *cache_drop = NULL;
  632. int ret = 0;
  633. h = rbio->bioc->fs_info->stripe_hash_table->table + rbio_bucket(rbio);
  634. spin_lock(&h->lock);
  635. list_for_each_entry(cur, &h->hash_list, hash_list) {
  636. if (cur->bioc->full_stripe_logical != rbio->bioc->full_stripe_logical)
  637. continue;
  638. spin_lock(&cur->bio_list_lock);
  639. /* Can we steal this cached rbio's pages? */
  640. if (bio_list_empty(&cur->bio_list) &&
  641. list_empty(&cur->plug_list) &&
  642. test_bit(RBIO_CACHE_BIT, &cur->flags) &&
  643. !test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags)) {
  644. list_del_init(&cur->hash_list);
  645. refcount_dec(&cur->refs);
  646. steal_rbio(cur, rbio);
  647. cache_drop = cur;
  648. spin_unlock(&cur->bio_list_lock);
  649. goto lockit;
  650. }
  651. /* Can we merge into the lock owner? */
  652. if (rbio_can_merge(cur, rbio)) {
  653. merge_rbio(cur, rbio);
  654. spin_unlock(&cur->bio_list_lock);
  655. freeit = rbio;
  656. ret = 1;
  657. goto out;
  658. }
  659. /*
  660. * We couldn't merge with the running rbio, see if we can merge
  661. * with the pending ones. We don't have to check for rmw_locked
  662. * because there is no way they are inside finish_rmw right now
  663. */
  664. list_for_each_entry(pending, &cur->plug_list, plug_list) {
  665. if (rbio_can_merge(pending, rbio)) {
  666. merge_rbio(pending, rbio);
  667. spin_unlock(&cur->bio_list_lock);
  668. freeit = rbio;
  669. ret = 1;
  670. goto out;
  671. }
  672. }
  673. /*
  674. * No merging, put us on the tail of the plug list, our rbio
  675. * will be started with the currently running rbio unlocks
  676. */
  677. list_add_tail(&rbio->plug_list, &cur->plug_list);
  678. spin_unlock(&cur->bio_list_lock);
  679. ret = 1;
  680. goto out;
  681. }
  682. lockit:
  683. refcount_inc(&rbio->refs);
  684. list_add(&rbio->hash_list, &h->hash_list);
  685. out:
  686. spin_unlock(&h->lock);
  687. if (cache_drop)
  688. remove_rbio_from_cache(cache_drop);
  689. if (freeit)
  690. free_raid_bio(freeit);
  691. return ret;
  692. }
  693. static void recover_rbio_work_locked(struct work_struct *work);
  694. /*
  695. * called as rmw or parity rebuild is completed. If the plug list has more
  696. * rbios waiting for this stripe, the next one on the list will be started
  697. */
  698. static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
  699. {
  700. int bucket;
  701. struct btrfs_stripe_hash *h;
  702. int keep_cache = 0;
  703. bucket = rbio_bucket(rbio);
  704. h = rbio->bioc->fs_info->stripe_hash_table->table + bucket;
  705. if (list_empty(&rbio->plug_list))
  706. cache_rbio(rbio);
  707. spin_lock(&h->lock);
  708. spin_lock(&rbio->bio_list_lock);
  709. if (!list_empty(&rbio->hash_list)) {
  710. /*
  711. * if we're still cached and there is no other IO
  712. * to perform, just leave this rbio here for others
  713. * to steal from later
  714. */
  715. if (list_empty(&rbio->plug_list) &&
  716. test_bit(RBIO_CACHE_BIT, &rbio->flags)) {
  717. keep_cache = 1;
  718. clear_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  719. BUG_ON(!bio_list_empty(&rbio->bio_list));
  720. goto done;
  721. }
  722. list_del_init(&rbio->hash_list);
  723. refcount_dec(&rbio->refs);
  724. /*
  725. * we use the plug list to hold all the rbios
  726. * waiting for the chance to lock this stripe.
  727. * hand the lock over to one of them.
  728. */
  729. if (!list_empty(&rbio->plug_list)) {
  730. struct btrfs_raid_bio *next;
  731. struct list_head *head = rbio->plug_list.next;
  732. next = list_entry(head, struct btrfs_raid_bio,
  733. plug_list);
  734. list_del_init(&rbio->plug_list);
  735. list_add(&next->hash_list, &h->hash_list);
  736. refcount_inc(&next->refs);
  737. spin_unlock(&rbio->bio_list_lock);
  738. spin_unlock(&h->lock);
  739. if (next->operation == BTRFS_RBIO_READ_REBUILD) {
  740. start_async_work(next, recover_rbio_work_locked);
  741. } else if (next->operation == BTRFS_RBIO_WRITE) {
  742. steal_rbio(rbio, next);
  743. start_async_work(next, rmw_rbio_work_locked);
  744. } else if (next->operation == BTRFS_RBIO_PARITY_SCRUB) {
  745. steal_rbio(rbio, next);
  746. start_async_work(next, scrub_rbio_work_locked);
  747. }
  748. goto done_nolock;
  749. }
  750. }
  751. done:
  752. spin_unlock(&rbio->bio_list_lock);
  753. spin_unlock(&h->lock);
  754. done_nolock:
  755. if (!keep_cache)
  756. remove_rbio_from_cache(rbio);
  757. }
  758. static void rbio_endio_bio_list(struct bio *cur, blk_status_t err)
  759. {
  760. struct bio *next;
  761. while (cur) {
  762. next = cur->bi_next;
  763. cur->bi_next = NULL;
  764. cur->bi_status = err;
  765. bio_endio(cur);
  766. cur = next;
  767. }
  768. }
  769. /*
  770. * this frees the rbio and runs through all the bios in the
  771. * bio_list and calls end_io on them
  772. */
  773. static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, blk_status_t err)
  774. {
  775. struct bio *cur = bio_list_get(&rbio->bio_list);
  776. struct bio *extra;
  777. kfree(rbio->csum_buf);
  778. bitmap_free(rbio->csum_bitmap);
  779. rbio->csum_buf = NULL;
  780. rbio->csum_bitmap = NULL;
  781. /*
  782. * Clear the data bitmap, as the rbio may be cached for later usage.
  783. * do this before before unlock_stripe() so there will be no new bio
  784. * for this bio.
  785. */
  786. bitmap_clear(&rbio->dbitmap, 0, rbio->stripe_nsectors);
  787. /*
  788. * At this moment, rbio->bio_list is empty, however since rbio does not
  789. * always have RBIO_RMW_LOCKED_BIT set and rbio is still linked on the
  790. * hash list, rbio may be merged with others so that rbio->bio_list
  791. * becomes non-empty.
  792. * Once unlock_stripe() is done, rbio->bio_list will not be updated any
  793. * more and we can call bio_endio() on all queued bios.
  794. */
  795. unlock_stripe(rbio);
  796. extra = bio_list_get(&rbio->bio_list);
  797. free_raid_bio(rbio);
  798. rbio_endio_bio_list(cur, err);
  799. if (extra)
  800. rbio_endio_bio_list(extra, err);
  801. }
  802. /*
  803. * Get a sector pointer specified by its @stripe_nr and @sector_nr.
  804. *
  805. * @rbio: The raid bio
  806. * @stripe_nr: Stripe number, valid range [0, real_stripe)
  807. * @sector_nr: Sector number inside the stripe,
  808. * valid range [0, stripe_nsectors)
  809. * @bio_list_only: Whether to use sectors inside the bio list only.
  810. *
  811. * The read/modify/write code wants to reuse the original bio page as much
  812. * as possible, and only use stripe_sectors as fallback.
  813. */
  814. static struct sector_ptr *sector_in_rbio(struct btrfs_raid_bio *rbio,
  815. int stripe_nr, int sector_nr,
  816. bool bio_list_only)
  817. {
  818. struct sector_ptr *sector;
  819. int index;
  820. ASSERT_RBIO_STRIPE(stripe_nr >= 0 && stripe_nr < rbio->real_stripes,
  821. rbio, stripe_nr);
  822. ASSERT_RBIO_SECTOR(sector_nr >= 0 && sector_nr < rbio->stripe_nsectors,
  823. rbio, sector_nr);
  824. index = stripe_nr * rbio->stripe_nsectors + sector_nr;
  825. ASSERT(index >= 0 && index < rbio->nr_sectors);
  826. spin_lock(&rbio->bio_list_lock);
  827. sector = &rbio->bio_sectors[index];
  828. if (sector->page || bio_list_only) {
  829. /* Don't return sector without a valid page pointer */
  830. if (!sector->page)
  831. sector = NULL;
  832. spin_unlock(&rbio->bio_list_lock);
  833. return sector;
  834. }
  835. spin_unlock(&rbio->bio_list_lock);
  836. return &rbio->stripe_sectors[index];
  837. }
  838. /*
  839. * allocation and initial setup for the btrfs_raid_bio. Not
  840. * this does not allocate any pages for rbio->pages.
  841. */
  842. static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
  843. struct btrfs_io_context *bioc)
  844. {
  845. const unsigned int real_stripes = bioc->num_stripes - bioc->replace_nr_stripes;
  846. const unsigned int stripe_npages = BTRFS_STRIPE_LEN >> PAGE_SHIFT;
  847. const unsigned int num_pages = stripe_npages * real_stripes;
  848. const unsigned int stripe_nsectors =
  849. BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits;
  850. const unsigned int num_sectors = stripe_nsectors * real_stripes;
  851. struct btrfs_raid_bio *rbio;
  852. /* PAGE_SIZE must also be aligned to sectorsize for subpage support */
  853. ASSERT(IS_ALIGNED(PAGE_SIZE, fs_info->sectorsize));
  854. /*
  855. * Our current stripe len should be fixed to 64k thus stripe_nsectors
  856. * (at most 16) should be no larger than BITS_PER_LONG.
  857. */
  858. ASSERT(stripe_nsectors <= BITS_PER_LONG);
  859. /*
  860. * Real stripes must be between 2 (2 disks RAID5, aka RAID1) and 256
  861. * (limited by u8).
  862. */
  863. ASSERT(real_stripes >= 2);
  864. ASSERT(real_stripes <= U8_MAX);
  865. rbio = kzalloc(sizeof(*rbio), GFP_NOFS);
  866. if (!rbio)
  867. return ERR_PTR(-ENOMEM);
  868. rbio->stripe_pages = kcalloc(num_pages, sizeof(struct page *),
  869. GFP_NOFS);
  870. rbio->bio_sectors = kcalloc(num_sectors, sizeof(struct sector_ptr),
  871. GFP_NOFS);
  872. rbio->stripe_sectors = kcalloc(num_sectors, sizeof(struct sector_ptr),
  873. GFP_NOFS);
  874. rbio->finish_pointers = kcalloc(real_stripes, sizeof(void *), GFP_NOFS);
  875. rbio->error_bitmap = bitmap_zalloc(num_sectors, GFP_NOFS);
  876. if (!rbio->stripe_pages || !rbio->bio_sectors || !rbio->stripe_sectors ||
  877. !rbio->finish_pointers || !rbio->error_bitmap) {
  878. free_raid_bio_pointers(rbio);
  879. kfree(rbio);
  880. return ERR_PTR(-ENOMEM);
  881. }
  882. bio_list_init(&rbio->bio_list);
  883. init_waitqueue_head(&rbio->io_wait);
  884. INIT_LIST_HEAD(&rbio->plug_list);
  885. spin_lock_init(&rbio->bio_list_lock);
  886. INIT_LIST_HEAD(&rbio->stripe_cache);
  887. INIT_LIST_HEAD(&rbio->hash_list);
  888. btrfs_get_bioc(bioc);
  889. rbio->bioc = bioc;
  890. rbio->nr_pages = num_pages;
  891. rbio->nr_sectors = num_sectors;
  892. rbio->real_stripes = real_stripes;
  893. rbio->stripe_npages = stripe_npages;
  894. rbio->stripe_nsectors = stripe_nsectors;
  895. refcount_set(&rbio->refs, 1);
  896. atomic_set(&rbio->stripes_pending, 0);
  897. ASSERT(btrfs_nr_parity_stripes(bioc->map_type));
  898. rbio->nr_data = real_stripes - btrfs_nr_parity_stripes(bioc->map_type);
  899. ASSERT(rbio->nr_data > 0);
  900. return rbio;
  901. }
  902. /* allocate pages for all the stripes in the bio, including parity */
  903. static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
  904. {
  905. int ret;
  906. ret = btrfs_alloc_page_array(rbio->nr_pages, rbio->stripe_pages, false);
  907. if (ret < 0)
  908. return ret;
  909. /* Mapping all sectors */
  910. index_stripe_sectors(rbio);
  911. return 0;
  912. }
  913. /* only allocate pages for p/q stripes */
  914. static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
  915. {
  916. const int data_pages = rbio->nr_data * rbio->stripe_npages;
  917. int ret;
  918. ret = btrfs_alloc_page_array(rbio->nr_pages - data_pages,
  919. rbio->stripe_pages + data_pages, false);
  920. if (ret < 0)
  921. return ret;
  922. index_stripe_sectors(rbio);
  923. return 0;
  924. }
  925. /*
  926. * Return the total number of errors found in the vertical stripe of @sector_nr.
  927. *
  928. * @faila and @failb will also be updated to the first and second stripe
  929. * number of the errors.
  930. */
  931. static int get_rbio_veritical_errors(struct btrfs_raid_bio *rbio, int sector_nr,
  932. int *faila, int *failb)
  933. {
  934. int stripe_nr;
  935. int found_errors = 0;
  936. if (faila || failb) {
  937. /*
  938. * Both @faila and @failb should be valid pointers if any of
  939. * them is specified.
  940. */
  941. ASSERT(faila && failb);
  942. *faila = -1;
  943. *failb = -1;
  944. }
  945. for (stripe_nr = 0; stripe_nr < rbio->real_stripes; stripe_nr++) {
  946. int total_sector_nr = stripe_nr * rbio->stripe_nsectors + sector_nr;
  947. if (test_bit(total_sector_nr, rbio->error_bitmap)) {
  948. found_errors++;
  949. if (faila) {
  950. /* Update faila and failb. */
  951. if (*faila < 0)
  952. *faila = stripe_nr;
  953. else if (*failb < 0)
  954. *failb = stripe_nr;
  955. }
  956. }
  957. }
  958. return found_errors;
  959. }
  960. /*
  961. * Add a single sector @sector into our list of bios for IO.
  962. *
  963. * Return 0 if everything went well.
  964. * Return <0 for error.
  965. */
  966. static int rbio_add_io_sector(struct btrfs_raid_bio *rbio,
  967. struct bio_list *bio_list,
  968. struct sector_ptr *sector,
  969. unsigned int stripe_nr,
  970. unsigned int sector_nr,
  971. enum req_op op)
  972. {
  973. const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
  974. struct bio *last = bio_list->tail;
  975. int ret;
  976. struct bio *bio;
  977. struct btrfs_io_stripe *stripe;
  978. u64 disk_start;
  979. /*
  980. * Note: here stripe_nr has taken device replace into consideration,
  981. * thus it can be larger than rbio->real_stripe.
  982. * So here we check against bioc->num_stripes, not rbio->real_stripes.
  983. */
  984. ASSERT_RBIO_STRIPE(stripe_nr >= 0 && stripe_nr < rbio->bioc->num_stripes,
  985. rbio, stripe_nr);
  986. ASSERT_RBIO_SECTOR(sector_nr >= 0 && sector_nr < rbio->stripe_nsectors,
  987. rbio, sector_nr);
  988. ASSERT(sector->page);
  989. stripe = &rbio->bioc->stripes[stripe_nr];
  990. disk_start = stripe->physical + sector_nr * sectorsize;
  991. /* if the device is missing, just fail this stripe */
  992. if (!stripe->dev->bdev) {
  993. int found_errors;
  994. set_bit(stripe_nr * rbio->stripe_nsectors + sector_nr,
  995. rbio->error_bitmap);
  996. /* Check if we have reached tolerance early. */
  997. found_errors = get_rbio_veritical_errors(rbio, sector_nr,
  998. NULL, NULL);
  999. if (found_errors > rbio->bioc->max_errors)
  1000. return -EIO;
  1001. return 0;
  1002. }
  1003. /* see if we can add this page onto our existing bio */
  1004. if (last) {
  1005. u64 last_end = last->bi_iter.bi_sector << SECTOR_SHIFT;
  1006. last_end += last->bi_iter.bi_size;
  1007. /*
  1008. * we can't merge these if they are from different
  1009. * devices or if they are not contiguous
  1010. */
  1011. if (last_end == disk_start && !last->bi_status &&
  1012. last->bi_bdev == stripe->dev->bdev) {
  1013. ret = bio_add_page(last, sector->page, sectorsize,
  1014. sector->pgoff);
  1015. if (ret == sectorsize)
  1016. return 0;
  1017. }
  1018. }
  1019. /* put a new bio on the list */
  1020. bio = bio_alloc(stripe->dev->bdev,
  1021. max(BTRFS_STRIPE_LEN >> PAGE_SHIFT, 1),
  1022. op, GFP_NOFS);
  1023. bio->bi_iter.bi_sector = disk_start >> SECTOR_SHIFT;
  1024. bio->bi_private = rbio;
  1025. __bio_add_page(bio, sector->page, sectorsize, sector->pgoff);
  1026. bio_list_add(bio_list, bio);
  1027. return 0;
  1028. }
  1029. static void index_one_bio(struct btrfs_raid_bio *rbio, struct bio *bio)
  1030. {
  1031. const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
  1032. struct bio_vec bvec;
  1033. struct bvec_iter iter;
  1034. u32 offset = (bio->bi_iter.bi_sector << SECTOR_SHIFT) -
  1035. rbio->bioc->full_stripe_logical;
  1036. bio_for_each_segment(bvec, bio, iter) {
  1037. u32 bvec_offset;
  1038. for (bvec_offset = 0; bvec_offset < bvec.bv_len;
  1039. bvec_offset += sectorsize, offset += sectorsize) {
  1040. int index = offset / sectorsize;
  1041. struct sector_ptr *sector = &rbio->bio_sectors[index];
  1042. sector->page = bvec.bv_page;
  1043. sector->pgoff = bvec.bv_offset + bvec_offset;
  1044. ASSERT(sector->pgoff < PAGE_SIZE);
  1045. }
  1046. }
  1047. }
  1048. /*
  1049. * helper function to walk our bio list and populate the bio_pages array with
  1050. * the result. This seems expensive, but it is faster than constantly
  1051. * searching through the bio list as we setup the IO in finish_rmw or stripe
  1052. * reconstruction.
  1053. *
  1054. * This must be called before you trust the answers from page_in_rbio
  1055. */
  1056. static void index_rbio_pages(struct btrfs_raid_bio *rbio)
  1057. {
  1058. struct bio *bio;
  1059. spin_lock(&rbio->bio_list_lock);
  1060. bio_list_for_each(bio, &rbio->bio_list)
  1061. index_one_bio(rbio, bio);
  1062. spin_unlock(&rbio->bio_list_lock);
  1063. }
  1064. static void bio_get_trace_info(struct btrfs_raid_bio *rbio, struct bio *bio,
  1065. struct raid56_bio_trace_info *trace_info)
  1066. {
  1067. const struct btrfs_io_context *bioc = rbio->bioc;
  1068. int i;
  1069. ASSERT(bioc);
  1070. /* We rely on bio->bi_bdev to find the stripe number. */
  1071. if (!bio->bi_bdev)
  1072. goto not_found;
  1073. for (i = 0; i < bioc->num_stripes; i++) {
  1074. if (bio->bi_bdev != bioc->stripes[i].dev->bdev)
  1075. continue;
  1076. trace_info->stripe_nr = i;
  1077. trace_info->devid = bioc->stripes[i].dev->devid;
  1078. trace_info->offset = (bio->bi_iter.bi_sector << SECTOR_SHIFT) -
  1079. bioc->stripes[i].physical;
  1080. return;
  1081. }
  1082. not_found:
  1083. trace_info->devid = -1;
  1084. trace_info->offset = -1;
  1085. trace_info->stripe_nr = -1;
  1086. }
  1087. static inline void bio_list_put(struct bio_list *bio_list)
  1088. {
  1089. struct bio *bio;
  1090. while ((bio = bio_list_pop(bio_list)))
  1091. bio_put(bio);
  1092. }
  1093. static void assert_rbio(struct btrfs_raid_bio *rbio)
  1094. {
  1095. if (!IS_ENABLED(CONFIG_BTRFS_DEBUG) ||
  1096. !IS_ENABLED(CONFIG_BTRFS_ASSERT))
  1097. return;
  1098. /*
  1099. * At least two stripes (2 disks RAID5), and since real_stripes is U8,
  1100. * we won't go beyond 256 disks anyway.
  1101. */
  1102. ASSERT_RBIO(rbio->real_stripes >= 2, rbio);
  1103. ASSERT_RBIO(rbio->nr_data > 0, rbio);
  1104. /*
  1105. * This is another check to make sure nr data stripes is smaller
  1106. * than total stripes.
  1107. */
  1108. ASSERT_RBIO(rbio->nr_data < rbio->real_stripes, rbio);
  1109. }
  1110. /* Generate PQ for one vertical stripe. */
  1111. static void generate_pq_vertical(struct btrfs_raid_bio *rbio, int sectornr)
  1112. {
  1113. void **pointers = rbio->finish_pointers;
  1114. const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
  1115. struct sector_ptr *sector;
  1116. int stripe;
  1117. const bool has_qstripe = rbio->bioc->map_type & BTRFS_BLOCK_GROUP_RAID6;
  1118. /* First collect one sector from each data stripe */
  1119. for (stripe = 0; stripe < rbio->nr_data; stripe++) {
  1120. sector = sector_in_rbio(rbio, stripe, sectornr, 0);
  1121. pointers[stripe] = kmap_local_page(sector->page) +
  1122. sector->pgoff;
  1123. }
  1124. /* Then add the parity stripe */
  1125. sector = rbio_pstripe_sector(rbio, sectornr);
  1126. sector->uptodate = 1;
  1127. pointers[stripe++] = kmap_local_page(sector->page) + sector->pgoff;
  1128. if (has_qstripe) {
  1129. /*
  1130. * RAID6, add the qstripe and call the library function
  1131. * to fill in our p/q
  1132. */
  1133. sector = rbio_qstripe_sector(rbio, sectornr);
  1134. sector->uptodate = 1;
  1135. pointers[stripe++] = kmap_local_page(sector->page) +
  1136. sector->pgoff;
  1137. assert_rbio(rbio);
  1138. raid6_call.gen_syndrome(rbio->real_stripes, sectorsize,
  1139. pointers);
  1140. } else {
  1141. /* raid5 */
  1142. memcpy(pointers[rbio->nr_data], pointers[0], sectorsize);
  1143. run_xor(pointers + 1, rbio->nr_data - 1, sectorsize);
  1144. }
  1145. for (stripe = stripe - 1; stripe >= 0; stripe--)
  1146. kunmap_local(pointers[stripe]);
  1147. }
  1148. static int rmw_assemble_write_bios(struct btrfs_raid_bio *rbio,
  1149. struct bio_list *bio_list)
  1150. {
  1151. /* The total sector number inside the full stripe. */
  1152. int total_sector_nr;
  1153. int sectornr;
  1154. int stripe;
  1155. int ret;
  1156. ASSERT(bio_list_size(bio_list) == 0);
  1157. /* We should have at least one data sector. */
  1158. ASSERT(bitmap_weight(&rbio->dbitmap, rbio->stripe_nsectors));
  1159. /*
  1160. * Reset errors, as we may have errors inherited from from degraded
  1161. * write.
  1162. */
  1163. bitmap_clear(rbio->error_bitmap, 0, rbio->nr_sectors);
  1164. /*
  1165. * Start assembly. Make bios for everything from the higher layers (the
  1166. * bio_list in our rbio) and our P/Q. Ignore everything else.
  1167. */
  1168. for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
  1169. total_sector_nr++) {
  1170. struct sector_ptr *sector;
  1171. stripe = total_sector_nr / rbio->stripe_nsectors;
  1172. sectornr = total_sector_nr % rbio->stripe_nsectors;
  1173. /* This vertical stripe has no data, skip it. */
  1174. if (!test_bit(sectornr, &rbio->dbitmap))
  1175. continue;
  1176. if (stripe < rbio->nr_data) {
  1177. sector = sector_in_rbio(rbio, stripe, sectornr, 1);
  1178. if (!sector)
  1179. continue;
  1180. } else {
  1181. sector = rbio_stripe_sector(rbio, stripe, sectornr);
  1182. }
  1183. ret = rbio_add_io_sector(rbio, bio_list, sector, stripe,
  1184. sectornr, REQ_OP_WRITE);
  1185. if (ret)
  1186. goto error;
  1187. }
  1188. if (likely(!rbio->bioc->replace_nr_stripes))
  1189. return 0;
  1190. /*
  1191. * Make a copy for the replace target device.
  1192. *
  1193. * Thus the source stripe number (in replace_stripe_src) should be valid.
  1194. */
  1195. ASSERT(rbio->bioc->replace_stripe_src >= 0);
  1196. for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
  1197. total_sector_nr++) {
  1198. struct sector_ptr *sector;
  1199. stripe = total_sector_nr / rbio->stripe_nsectors;
  1200. sectornr = total_sector_nr % rbio->stripe_nsectors;
  1201. /*
  1202. * For RAID56, there is only one device that can be replaced,
  1203. * and replace_stripe_src[0] indicates the stripe number we
  1204. * need to copy from.
  1205. */
  1206. if (stripe != rbio->bioc->replace_stripe_src) {
  1207. /*
  1208. * We can skip the whole stripe completely, note
  1209. * total_sector_nr will be increased by one anyway.
  1210. */
  1211. ASSERT(sectornr == 0);
  1212. total_sector_nr += rbio->stripe_nsectors - 1;
  1213. continue;
  1214. }
  1215. /* This vertical stripe has no data, skip it. */
  1216. if (!test_bit(sectornr, &rbio->dbitmap))
  1217. continue;
  1218. if (stripe < rbio->nr_data) {
  1219. sector = sector_in_rbio(rbio, stripe, sectornr, 1);
  1220. if (!sector)
  1221. continue;
  1222. } else {
  1223. sector = rbio_stripe_sector(rbio, stripe, sectornr);
  1224. }
  1225. ret = rbio_add_io_sector(rbio, bio_list, sector,
  1226. rbio->real_stripes,
  1227. sectornr, REQ_OP_WRITE);
  1228. if (ret)
  1229. goto error;
  1230. }
  1231. return 0;
  1232. error:
  1233. bio_list_put(bio_list);
  1234. return -EIO;
  1235. }
  1236. static void set_rbio_range_error(struct btrfs_raid_bio *rbio, struct bio *bio)
  1237. {
  1238. struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
  1239. u32 offset = (bio->bi_iter.bi_sector << SECTOR_SHIFT) -
  1240. rbio->bioc->full_stripe_logical;
  1241. int total_nr_sector = offset >> fs_info->sectorsize_bits;
  1242. ASSERT(total_nr_sector < rbio->nr_data * rbio->stripe_nsectors);
  1243. bitmap_set(rbio->error_bitmap, total_nr_sector,
  1244. bio->bi_iter.bi_size >> fs_info->sectorsize_bits);
  1245. /*
  1246. * Special handling for raid56_alloc_missing_rbio() used by
  1247. * scrub/replace. Unlike call path in raid56_parity_recover(), they
  1248. * pass an empty bio here. Thus we have to find out the missing device
  1249. * and mark the stripe error instead.
  1250. */
  1251. if (bio->bi_iter.bi_size == 0) {
  1252. bool found_missing = false;
  1253. int stripe_nr;
  1254. for (stripe_nr = 0; stripe_nr < rbio->real_stripes; stripe_nr++) {
  1255. if (!rbio->bioc->stripes[stripe_nr].dev->bdev) {
  1256. found_missing = true;
  1257. bitmap_set(rbio->error_bitmap,
  1258. stripe_nr * rbio->stripe_nsectors,
  1259. rbio->stripe_nsectors);
  1260. }
  1261. }
  1262. ASSERT(found_missing);
  1263. }
  1264. }
  1265. /*
  1266. * For subpage case, we can no longer set page Up-to-date directly for
  1267. * stripe_pages[], thus we need to locate the sector.
  1268. */
  1269. static struct sector_ptr *find_stripe_sector(struct btrfs_raid_bio *rbio,
  1270. struct page *page,
  1271. unsigned int pgoff)
  1272. {
  1273. int i;
  1274. for (i = 0; i < rbio->nr_sectors; i++) {
  1275. struct sector_ptr *sector = &rbio->stripe_sectors[i];
  1276. if (sector->page == page && sector->pgoff == pgoff)
  1277. return sector;
  1278. }
  1279. return NULL;
  1280. }
  1281. /*
  1282. * this sets each page in the bio uptodate. It should only be used on private
  1283. * rbio pages, nothing that comes in from the higher layers
  1284. */
  1285. static void set_bio_pages_uptodate(struct btrfs_raid_bio *rbio, struct bio *bio)
  1286. {
  1287. const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
  1288. struct bio_vec *bvec;
  1289. struct bvec_iter_all iter_all;
  1290. ASSERT(!bio_flagged(bio, BIO_CLONED));
  1291. bio_for_each_segment_all(bvec, bio, iter_all) {
  1292. struct sector_ptr *sector;
  1293. int pgoff;
  1294. for (pgoff = bvec->bv_offset; pgoff - bvec->bv_offset < bvec->bv_len;
  1295. pgoff += sectorsize) {
  1296. sector = find_stripe_sector(rbio, bvec->bv_page, pgoff);
  1297. ASSERT(sector);
  1298. if (sector)
  1299. sector->uptodate = 1;
  1300. }
  1301. }
  1302. }
  1303. static int get_bio_sector_nr(struct btrfs_raid_bio *rbio, struct bio *bio)
  1304. {
  1305. struct bio_vec *bv = bio_first_bvec_all(bio);
  1306. int i;
  1307. for (i = 0; i < rbio->nr_sectors; i++) {
  1308. struct sector_ptr *sector;
  1309. sector = &rbio->stripe_sectors[i];
  1310. if (sector->page == bv->bv_page && sector->pgoff == bv->bv_offset)
  1311. break;
  1312. sector = &rbio->bio_sectors[i];
  1313. if (sector->page == bv->bv_page && sector->pgoff == bv->bv_offset)
  1314. break;
  1315. }
  1316. ASSERT(i < rbio->nr_sectors);
  1317. return i;
  1318. }
  1319. static void rbio_update_error_bitmap(struct btrfs_raid_bio *rbio, struct bio *bio)
  1320. {
  1321. int total_sector_nr = get_bio_sector_nr(rbio, bio);
  1322. u32 bio_size = 0;
  1323. struct bio_vec *bvec;
  1324. int i;
  1325. bio_for_each_bvec_all(bvec, bio, i)
  1326. bio_size += bvec->bv_len;
  1327. /*
  1328. * Since we can have multiple bios touching the error_bitmap, we cannot
  1329. * call bitmap_set() without protection.
  1330. *
  1331. * Instead use set_bit() for each bit, as set_bit() itself is atomic.
  1332. */
  1333. for (i = total_sector_nr; i < total_sector_nr +
  1334. (bio_size >> rbio->bioc->fs_info->sectorsize_bits); i++)
  1335. set_bit(i, rbio->error_bitmap);
  1336. }
  1337. /* Verify the data sectors at read time. */
  1338. static void verify_bio_data_sectors(struct btrfs_raid_bio *rbio,
  1339. struct bio *bio)
  1340. {
  1341. struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
  1342. int total_sector_nr = get_bio_sector_nr(rbio, bio);
  1343. struct bio_vec *bvec;
  1344. struct bvec_iter_all iter_all;
  1345. /* No data csum for the whole stripe, no need to verify. */
  1346. if (!rbio->csum_bitmap || !rbio->csum_buf)
  1347. return;
  1348. /* P/Q stripes, they have no data csum to verify against. */
  1349. if (total_sector_nr >= rbio->nr_data * rbio->stripe_nsectors)
  1350. return;
  1351. bio_for_each_segment_all(bvec, bio, iter_all) {
  1352. int bv_offset;
  1353. for (bv_offset = bvec->bv_offset;
  1354. bv_offset < bvec->bv_offset + bvec->bv_len;
  1355. bv_offset += fs_info->sectorsize, total_sector_nr++) {
  1356. u8 csum_buf[BTRFS_CSUM_SIZE];
  1357. u8 *expected_csum = rbio->csum_buf +
  1358. total_sector_nr * fs_info->csum_size;
  1359. int ret;
  1360. /* No csum for this sector, skip to the next sector. */
  1361. if (!test_bit(total_sector_nr, rbio->csum_bitmap))
  1362. continue;
  1363. ret = btrfs_check_sector_csum(fs_info, bvec->bv_page,
  1364. bv_offset, csum_buf, expected_csum);
  1365. if (ret < 0)
  1366. set_bit(total_sector_nr, rbio->error_bitmap);
  1367. }
  1368. }
  1369. }
  1370. static void raid_wait_read_end_io(struct bio *bio)
  1371. {
  1372. struct btrfs_raid_bio *rbio = bio->bi_private;
  1373. if (bio->bi_status) {
  1374. rbio_update_error_bitmap(rbio, bio);
  1375. } else {
  1376. set_bio_pages_uptodate(rbio, bio);
  1377. verify_bio_data_sectors(rbio, bio);
  1378. }
  1379. bio_put(bio);
  1380. if (atomic_dec_and_test(&rbio->stripes_pending))
  1381. wake_up(&rbio->io_wait);
  1382. }
  1383. static void submit_read_wait_bio_list(struct btrfs_raid_bio *rbio,
  1384. struct bio_list *bio_list)
  1385. {
  1386. struct bio *bio;
  1387. atomic_set(&rbio->stripes_pending, bio_list_size(bio_list));
  1388. while ((bio = bio_list_pop(bio_list))) {
  1389. bio->bi_end_io = raid_wait_read_end_io;
  1390. if (trace_raid56_read_enabled()) {
  1391. struct raid56_bio_trace_info trace_info = { 0 };
  1392. bio_get_trace_info(rbio, bio, &trace_info);
  1393. trace_raid56_read(rbio, bio, &trace_info);
  1394. }
  1395. submit_bio(bio);
  1396. }
  1397. wait_event(rbio->io_wait, atomic_read(&rbio->stripes_pending) == 0);
  1398. }
  1399. static int alloc_rbio_data_pages(struct btrfs_raid_bio *rbio)
  1400. {
  1401. const int data_pages = rbio->nr_data * rbio->stripe_npages;
  1402. int ret;
  1403. ret = btrfs_alloc_page_array(data_pages, rbio->stripe_pages, false);
  1404. if (ret < 0)
  1405. return ret;
  1406. index_stripe_sectors(rbio);
  1407. return 0;
  1408. }
  1409. /*
  1410. * We use plugging call backs to collect full stripes.
  1411. * Any time we get a partial stripe write while plugged
  1412. * we collect it into a list. When the unplug comes down,
  1413. * we sort the list by logical block number and merge
  1414. * everything we can into the same rbios
  1415. */
  1416. struct btrfs_plug_cb {
  1417. struct blk_plug_cb cb;
  1418. struct btrfs_fs_info *info;
  1419. struct list_head rbio_list;
  1420. };
  1421. /*
  1422. * rbios on the plug list are sorted for easier merging.
  1423. */
  1424. static int plug_cmp(void *priv, const struct list_head *a,
  1425. const struct list_head *b)
  1426. {
  1427. const struct btrfs_raid_bio *ra = container_of(a, struct btrfs_raid_bio,
  1428. plug_list);
  1429. const struct btrfs_raid_bio *rb = container_of(b, struct btrfs_raid_bio,
  1430. plug_list);
  1431. u64 a_sector = ra->bio_list.head->bi_iter.bi_sector;
  1432. u64 b_sector = rb->bio_list.head->bi_iter.bi_sector;
  1433. if (a_sector < b_sector)
  1434. return -1;
  1435. if (a_sector > b_sector)
  1436. return 1;
  1437. return 0;
  1438. }
  1439. static void raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
  1440. {
  1441. struct btrfs_plug_cb *plug = container_of(cb, struct btrfs_plug_cb, cb);
  1442. struct btrfs_raid_bio *cur;
  1443. struct btrfs_raid_bio *last = NULL;
  1444. list_sort(NULL, &plug->rbio_list, plug_cmp);
  1445. while (!list_empty(&plug->rbio_list)) {
  1446. cur = list_entry(plug->rbio_list.next,
  1447. struct btrfs_raid_bio, plug_list);
  1448. list_del_init(&cur->plug_list);
  1449. if (rbio_is_full(cur)) {
  1450. /* We have a full stripe, queue it down. */
  1451. start_async_work(cur, rmw_rbio_work);
  1452. continue;
  1453. }
  1454. if (last) {
  1455. if (rbio_can_merge(last, cur)) {
  1456. merge_rbio(last, cur);
  1457. free_raid_bio(cur);
  1458. continue;
  1459. }
  1460. start_async_work(last, rmw_rbio_work);
  1461. }
  1462. last = cur;
  1463. }
  1464. if (last)
  1465. start_async_work(last, rmw_rbio_work);
  1466. kfree(plug);
  1467. }
  1468. /* Add the original bio into rbio->bio_list, and update rbio::dbitmap. */
  1469. static void rbio_add_bio(struct btrfs_raid_bio *rbio, struct bio *orig_bio)
  1470. {
  1471. const struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
  1472. const u64 orig_logical = orig_bio->bi_iter.bi_sector << SECTOR_SHIFT;
  1473. const u64 full_stripe_start = rbio->bioc->full_stripe_logical;
  1474. const u32 orig_len = orig_bio->bi_iter.bi_size;
  1475. const u32 sectorsize = fs_info->sectorsize;
  1476. u64 cur_logical;
  1477. ASSERT_RBIO_LOGICAL(orig_logical >= full_stripe_start &&
  1478. orig_logical + orig_len <= full_stripe_start +
  1479. rbio->nr_data * BTRFS_STRIPE_LEN,
  1480. rbio, orig_logical);
  1481. bio_list_add(&rbio->bio_list, orig_bio);
  1482. rbio->bio_list_bytes += orig_bio->bi_iter.bi_size;
  1483. /* Update the dbitmap. */
  1484. for (cur_logical = orig_logical; cur_logical < orig_logical + orig_len;
  1485. cur_logical += sectorsize) {
  1486. int bit = ((u32)(cur_logical - full_stripe_start) >>
  1487. fs_info->sectorsize_bits) % rbio->stripe_nsectors;
  1488. set_bit(bit, &rbio->dbitmap);
  1489. }
  1490. }
  1491. /*
  1492. * our main entry point for writes from the rest of the FS.
  1493. */
  1494. void raid56_parity_write(struct bio *bio, struct btrfs_io_context *bioc)
  1495. {
  1496. struct btrfs_fs_info *fs_info = bioc->fs_info;
  1497. struct btrfs_raid_bio *rbio;
  1498. struct btrfs_plug_cb *plug = NULL;
  1499. struct blk_plug_cb *cb;
  1500. rbio = alloc_rbio(fs_info, bioc);
  1501. if (IS_ERR(rbio)) {
  1502. bio->bi_status = errno_to_blk_status(PTR_ERR(rbio));
  1503. bio_endio(bio);
  1504. return;
  1505. }
  1506. rbio->operation = BTRFS_RBIO_WRITE;
  1507. rbio_add_bio(rbio, bio);
  1508. /*
  1509. * Don't plug on full rbios, just get them out the door
  1510. * as quickly as we can
  1511. */
  1512. if (!rbio_is_full(rbio)) {
  1513. cb = blk_check_plugged(raid_unplug, fs_info, sizeof(*plug));
  1514. if (cb) {
  1515. plug = container_of(cb, struct btrfs_plug_cb, cb);
  1516. if (!plug->info) {
  1517. plug->info = fs_info;
  1518. INIT_LIST_HEAD(&plug->rbio_list);
  1519. }
  1520. list_add_tail(&rbio->plug_list, &plug->rbio_list);
  1521. return;
  1522. }
  1523. }
  1524. /*
  1525. * Either we don't have any existing plug, or we're doing a full stripe,
  1526. * queue the rmw work now.
  1527. */
  1528. start_async_work(rbio, rmw_rbio_work);
  1529. }
  1530. static int verify_one_sector(struct btrfs_raid_bio *rbio,
  1531. int stripe_nr, int sector_nr)
  1532. {
  1533. struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
  1534. struct sector_ptr *sector;
  1535. u8 csum_buf[BTRFS_CSUM_SIZE];
  1536. u8 *csum_expected;
  1537. int ret;
  1538. if (!rbio->csum_bitmap || !rbio->csum_buf)
  1539. return 0;
  1540. /* No way to verify P/Q as they are not covered by data csum. */
  1541. if (stripe_nr >= rbio->nr_data)
  1542. return 0;
  1543. /*
  1544. * If we're rebuilding a read, we have to use pages from the
  1545. * bio list if possible.
  1546. */
  1547. if (rbio->operation == BTRFS_RBIO_READ_REBUILD) {
  1548. sector = sector_in_rbio(rbio, stripe_nr, sector_nr, 0);
  1549. } else {
  1550. sector = rbio_stripe_sector(rbio, stripe_nr, sector_nr);
  1551. }
  1552. ASSERT(sector->page);
  1553. csum_expected = rbio->csum_buf +
  1554. (stripe_nr * rbio->stripe_nsectors + sector_nr) *
  1555. fs_info->csum_size;
  1556. ret = btrfs_check_sector_csum(fs_info, sector->page, sector->pgoff,
  1557. csum_buf, csum_expected);
  1558. return ret;
  1559. }
  1560. /*
  1561. * Recover a vertical stripe specified by @sector_nr.
  1562. * @*pointers are the pre-allocated pointers by the caller, so we don't
  1563. * need to allocate/free the pointers again and again.
  1564. */
  1565. static int recover_vertical(struct btrfs_raid_bio *rbio, int sector_nr,
  1566. void **pointers, void **unmap_array)
  1567. {
  1568. struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
  1569. struct sector_ptr *sector;
  1570. const u32 sectorsize = fs_info->sectorsize;
  1571. int found_errors;
  1572. int faila;
  1573. int failb;
  1574. int stripe_nr;
  1575. int ret = 0;
  1576. /*
  1577. * Now we just use bitmap to mark the horizontal stripes in
  1578. * which we have data when doing parity scrub.
  1579. */
  1580. if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB &&
  1581. !test_bit(sector_nr, &rbio->dbitmap))
  1582. return 0;
  1583. found_errors = get_rbio_veritical_errors(rbio, sector_nr, &faila,
  1584. &failb);
  1585. /*
  1586. * No errors in the vertical stripe, skip it. Can happen for recovery
  1587. * which only part of a stripe failed csum check.
  1588. */
  1589. if (!found_errors)
  1590. return 0;
  1591. if (found_errors > rbio->bioc->max_errors)
  1592. return -EIO;
  1593. /*
  1594. * Setup our array of pointers with sectors from each stripe
  1595. *
  1596. * NOTE: store a duplicate array of pointers to preserve the
  1597. * pointer order.
  1598. */
  1599. for (stripe_nr = 0; stripe_nr < rbio->real_stripes; stripe_nr++) {
  1600. /*
  1601. * If we're rebuilding a read, we have to use pages from the
  1602. * bio list if possible.
  1603. */
  1604. if (rbio->operation == BTRFS_RBIO_READ_REBUILD) {
  1605. sector = sector_in_rbio(rbio, stripe_nr, sector_nr, 0);
  1606. } else {
  1607. sector = rbio_stripe_sector(rbio, stripe_nr, sector_nr);
  1608. }
  1609. ASSERT(sector->page);
  1610. pointers[stripe_nr] = kmap_local_page(sector->page) +
  1611. sector->pgoff;
  1612. unmap_array[stripe_nr] = pointers[stripe_nr];
  1613. }
  1614. /* All raid6 handling here */
  1615. if (rbio->bioc->map_type & BTRFS_BLOCK_GROUP_RAID6) {
  1616. /* Single failure, rebuild from parity raid5 style */
  1617. if (failb < 0) {
  1618. if (faila == rbio->nr_data)
  1619. /*
  1620. * Just the P stripe has failed, without
  1621. * a bad data or Q stripe.
  1622. * We have nothing to do, just skip the
  1623. * recovery for this stripe.
  1624. */
  1625. goto cleanup;
  1626. /*
  1627. * a single failure in raid6 is rebuilt
  1628. * in the pstripe code below
  1629. */
  1630. goto pstripe;
  1631. }
  1632. /*
  1633. * If the q stripe is failed, do a pstripe reconstruction from
  1634. * the xors.
  1635. * If both the q stripe and the P stripe are failed, we're
  1636. * here due to a crc mismatch and we can't give them the
  1637. * data they want.
  1638. */
  1639. if (failb == rbio->real_stripes - 1) {
  1640. if (faila == rbio->real_stripes - 2)
  1641. /*
  1642. * Only P and Q are corrupted.
  1643. * We only care about data stripes recovery,
  1644. * can skip this vertical stripe.
  1645. */
  1646. goto cleanup;
  1647. /*
  1648. * Otherwise we have one bad data stripe and
  1649. * a good P stripe. raid5!
  1650. */
  1651. goto pstripe;
  1652. }
  1653. if (failb == rbio->real_stripes - 2) {
  1654. raid6_datap_recov(rbio->real_stripes, sectorsize,
  1655. faila, pointers);
  1656. } else {
  1657. raid6_2data_recov(rbio->real_stripes, sectorsize,
  1658. faila, failb, pointers);
  1659. }
  1660. } else {
  1661. void *p;
  1662. /* Rebuild from P stripe here (raid5 or raid6). */
  1663. ASSERT(failb == -1);
  1664. pstripe:
  1665. /* Copy parity block into failed block to start with */
  1666. memcpy(pointers[faila], pointers[rbio->nr_data], sectorsize);
  1667. /* Rearrange the pointer array */
  1668. p = pointers[faila];
  1669. for (stripe_nr = faila; stripe_nr < rbio->nr_data - 1;
  1670. stripe_nr++)
  1671. pointers[stripe_nr] = pointers[stripe_nr + 1];
  1672. pointers[rbio->nr_data - 1] = p;
  1673. /* Xor in the rest */
  1674. run_xor(pointers, rbio->nr_data - 1, sectorsize);
  1675. }
  1676. /*
  1677. * No matter if this is a RMW or recovery, we should have all
  1678. * failed sectors repaired in the vertical stripe, thus they are now
  1679. * uptodate.
  1680. * Especially if we determine to cache the rbio, we need to
  1681. * have at least all data sectors uptodate.
  1682. *
  1683. * If possible, also check if the repaired sector matches its data
  1684. * checksum.
  1685. */
  1686. if (faila >= 0) {
  1687. ret = verify_one_sector(rbio, faila, sector_nr);
  1688. if (ret < 0)
  1689. goto cleanup;
  1690. sector = rbio_stripe_sector(rbio, faila, sector_nr);
  1691. sector->uptodate = 1;
  1692. }
  1693. if (failb >= 0) {
  1694. ret = verify_one_sector(rbio, failb, sector_nr);
  1695. if (ret < 0)
  1696. goto cleanup;
  1697. sector = rbio_stripe_sector(rbio, failb, sector_nr);
  1698. sector->uptodate = 1;
  1699. }
  1700. cleanup:
  1701. for (stripe_nr = rbio->real_stripes - 1; stripe_nr >= 0; stripe_nr--)
  1702. kunmap_local(unmap_array[stripe_nr]);
  1703. return ret;
  1704. }
  1705. static int recover_sectors(struct btrfs_raid_bio *rbio)
  1706. {
  1707. void **pointers = NULL;
  1708. void **unmap_array = NULL;
  1709. int sectornr;
  1710. int ret = 0;
  1711. /*
  1712. * @pointers array stores the pointer for each sector.
  1713. *
  1714. * @unmap_array stores copy of pointers that does not get reordered
  1715. * during reconstruction so that kunmap_local works.
  1716. */
  1717. pointers = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
  1718. unmap_array = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
  1719. if (!pointers || !unmap_array) {
  1720. ret = -ENOMEM;
  1721. goto out;
  1722. }
  1723. if (rbio->operation == BTRFS_RBIO_READ_REBUILD) {
  1724. spin_lock(&rbio->bio_list_lock);
  1725. set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  1726. spin_unlock(&rbio->bio_list_lock);
  1727. }
  1728. index_rbio_pages(rbio);
  1729. for (sectornr = 0; sectornr < rbio->stripe_nsectors; sectornr++) {
  1730. ret = recover_vertical(rbio, sectornr, pointers, unmap_array);
  1731. if (ret < 0)
  1732. break;
  1733. }
  1734. out:
  1735. kfree(pointers);
  1736. kfree(unmap_array);
  1737. return ret;
  1738. }
  1739. static void recover_rbio(struct btrfs_raid_bio *rbio)
  1740. {
  1741. struct bio_list bio_list = BIO_EMPTY_LIST;
  1742. int total_sector_nr;
  1743. int ret = 0;
  1744. /*
  1745. * Either we're doing recover for a read failure or degraded write,
  1746. * caller should have set error bitmap correctly.
  1747. */
  1748. ASSERT(bitmap_weight(rbio->error_bitmap, rbio->nr_sectors));
  1749. /* For recovery, we need to read all sectors including P/Q. */
  1750. ret = alloc_rbio_pages(rbio);
  1751. if (ret < 0)
  1752. goto out;
  1753. index_rbio_pages(rbio);
  1754. /*
  1755. * Read everything that hasn't failed. However this time we will
  1756. * not trust any cached sector.
  1757. * As we may read out some stale data but higher layer is not reading
  1758. * that stale part.
  1759. *
  1760. * So here we always re-read everything in recovery path.
  1761. */
  1762. for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
  1763. total_sector_nr++) {
  1764. int stripe = total_sector_nr / rbio->stripe_nsectors;
  1765. int sectornr = total_sector_nr % rbio->stripe_nsectors;
  1766. struct sector_ptr *sector;
  1767. /*
  1768. * Skip the range which has error. It can be a range which is
  1769. * marked error (for csum mismatch), or it can be a missing
  1770. * device.
  1771. */
  1772. if (!rbio->bioc->stripes[stripe].dev->bdev ||
  1773. test_bit(total_sector_nr, rbio->error_bitmap)) {
  1774. /*
  1775. * Also set the error bit for missing device, which
  1776. * may not yet have its error bit set.
  1777. */
  1778. set_bit(total_sector_nr, rbio->error_bitmap);
  1779. continue;
  1780. }
  1781. sector = rbio_stripe_sector(rbio, stripe, sectornr);
  1782. ret = rbio_add_io_sector(rbio, &bio_list, sector, stripe,
  1783. sectornr, REQ_OP_READ);
  1784. if (ret < 0) {
  1785. bio_list_put(&bio_list);
  1786. goto out;
  1787. }
  1788. }
  1789. submit_read_wait_bio_list(rbio, &bio_list);
  1790. ret = recover_sectors(rbio);
  1791. out:
  1792. rbio_orig_end_io(rbio, errno_to_blk_status(ret));
  1793. }
  1794. static void recover_rbio_work(struct work_struct *work)
  1795. {
  1796. struct btrfs_raid_bio *rbio;
  1797. rbio = container_of(work, struct btrfs_raid_bio, work);
  1798. if (!lock_stripe_add(rbio))
  1799. recover_rbio(rbio);
  1800. }
  1801. static void recover_rbio_work_locked(struct work_struct *work)
  1802. {
  1803. recover_rbio(container_of(work, struct btrfs_raid_bio, work));
  1804. }
  1805. static void set_rbio_raid6_extra_error(struct btrfs_raid_bio *rbio, int mirror_num)
  1806. {
  1807. bool found = false;
  1808. int sector_nr;
  1809. /*
  1810. * This is for RAID6 extra recovery tries, thus mirror number should
  1811. * be large than 2.
  1812. * Mirror 1 means read from data stripes. Mirror 2 means rebuild using
  1813. * RAID5 methods.
  1814. */
  1815. ASSERT(mirror_num > 2);
  1816. for (sector_nr = 0; sector_nr < rbio->stripe_nsectors; sector_nr++) {
  1817. int found_errors;
  1818. int faila;
  1819. int failb;
  1820. found_errors = get_rbio_veritical_errors(rbio, sector_nr,
  1821. &faila, &failb);
  1822. /* This vertical stripe doesn't have errors. */
  1823. if (!found_errors)
  1824. continue;
  1825. /*
  1826. * If we found errors, there should be only one error marked
  1827. * by previous set_rbio_range_error().
  1828. */
  1829. ASSERT(found_errors == 1);
  1830. found = true;
  1831. /* Now select another stripe to mark as error. */
  1832. failb = rbio->real_stripes - (mirror_num - 1);
  1833. if (failb <= faila)
  1834. failb--;
  1835. /* Set the extra bit in error bitmap. */
  1836. if (failb >= 0)
  1837. set_bit(failb * rbio->stripe_nsectors + sector_nr,
  1838. rbio->error_bitmap);
  1839. }
  1840. /* We should found at least one vertical stripe with error.*/
  1841. ASSERT(found);
  1842. }
  1843. /*
  1844. * the main entry point for reads from the higher layers. This
  1845. * is really only called when the normal read path had a failure,
  1846. * so we assume the bio they send down corresponds to a failed part
  1847. * of the drive.
  1848. */
  1849. void raid56_parity_recover(struct bio *bio, struct btrfs_io_context *bioc,
  1850. int mirror_num)
  1851. {
  1852. struct btrfs_fs_info *fs_info = bioc->fs_info;
  1853. struct btrfs_raid_bio *rbio;
  1854. rbio = alloc_rbio(fs_info, bioc);
  1855. if (IS_ERR(rbio)) {
  1856. bio->bi_status = errno_to_blk_status(PTR_ERR(rbio));
  1857. bio_endio(bio);
  1858. return;
  1859. }
  1860. rbio->operation = BTRFS_RBIO_READ_REBUILD;
  1861. rbio_add_bio(rbio, bio);
  1862. set_rbio_range_error(rbio, bio);
  1863. /*
  1864. * Loop retry:
  1865. * for 'mirror == 2', reconstruct from all other stripes.
  1866. * for 'mirror_num > 2', select a stripe to fail on every retry.
  1867. */
  1868. if (mirror_num > 2)
  1869. set_rbio_raid6_extra_error(rbio, mirror_num);
  1870. start_async_work(rbio, recover_rbio_work);
  1871. }
  1872. static void fill_data_csums(struct btrfs_raid_bio *rbio)
  1873. {
  1874. struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
  1875. struct btrfs_root *csum_root = btrfs_csum_root(fs_info,
  1876. rbio->bioc->full_stripe_logical);
  1877. const u64 start = rbio->bioc->full_stripe_logical;
  1878. const u32 len = (rbio->nr_data * rbio->stripe_nsectors) <<
  1879. fs_info->sectorsize_bits;
  1880. int ret;
  1881. /* The rbio should not have its csum buffer initialized. */
  1882. ASSERT(!rbio->csum_buf && !rbio->csum_bitmap);
  1883. /*
  1884. * Skip the csum search if:
  1885. *
  1886. * - The rbio doesn't belong to data block groups
  1887. * Then we are doing IO for tree blocks, no need to search csums.
  1888. *
  1889. * - The rbio belongs to mixed block groups
  1890. * This is to avoid deadlock, as we're already holding the full
  1891. * stripe lock, if we trigger a metadata read, and it needs to do
  1892. * raid56 recovery, we will deadlock.
  1893. */
  1894. if (!(rbio->bioc->map_type & BTRFS_BLOCK_GROUP_DATA) ||
  1895. rbio->bioc->map_type & BTRFS_BLOCK_GROUP_METADATA)
  1896. return;
  1897. rbio->csum_buf = kzalloc(rbio->nr_data * rbio->stripe_nsectors *
  1898. fs_info->csum_size, GFP_NOFS);
  1899. rbio->csum_bitmap = bitmap_zalloc(rbio->nr_data * rbio->stripe_nsectors,
  1900. GFP_NOFS);
  1901. if (!rbio->csum_buf || !rbio->csum_bitmap) {
  1902. ret = -ENOMEM;
  1903. goto error;
  1904. }
  1905. ret = btrfs_lookup_csums_bitmap(csum_root, NULL, start, start + len - 1,
  1906. rbio->csum_buf, rbio->csum_bitmap);
  1907. if (ret < 0)
  1908. goto error;
  1909. if (bitmap_empty(rbio->csum_bitmap, len >> fs_info->sectorsize_bits))
  1910. goto no_csum;
  1911. return;
  1912. error:
  1913. /*
  1914. * We failed to allocate memory or grab the csum, but it's not fatal,
  1915. * we can still continue. But better to warn users that RMW is no
  1916. * longer safe for this particular sub-stripe write.
  1917. */
  1918. btrfs_warn_rl(fs_info,
  1919. "sub-stripe write for full stripe %llu is not safe, failed to get csum: %d",
  1920. rbio->bioc->full_stripe_logical, ret);
  1921. no_csum:
  1922. kfree(rbio->csum_buf);
  1923. bitmap_free(rbio->csum_bitmap);
  1924. rbio->csum_buf = NULL;
  1925. rbio->csum_bitmap = NULL;
  1926. }
  1927. static int rmw_read_wait_recover(struct btrfs_raid_bio *rbio)
  1928. {
  1929. struct bio_list bio_list = BIO_EMPTY_LIST;
  1930. int total_sector_nr;
  1931. int ret = 0;
  1932. /*
  1933. * Fill the data csums we need for data verification. We need to fill
  1934. * the csum_bitmap/csum_buf first, as our endio function will try to
  1935. * verify the data sectors.
  1936. */
  1937. fill_data_csums(rbio);
  1938. /*
  1939. * Build a list of bios to read all sectors (including data and P/Q).
  1940. *
  1941. * This behavior is to compensate the later csum verification and recovery.
  1942. */
  1943. for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
  1944. total_sector_nr++) {
  1945. struct sector_ptr *sector;
  1946. int stripe = total_sector_nr / rbio->stripe_nsectors;
  1947. int sectornr = total_sector_nr % rbio->stripe_nsectors;
  1948. sector = rbio_stripe_sector(rbio, stripe, sectornr);
  1949. ret = rbio_add_io_sector(rbio, &bio_list, sector,
  1950. stripe, sectornr, REQ_OP_READ);
  1951. if (ret) {
  1952. bio_list_put(&bio_list);
  1953. return ret;
  1954. }
  1955. }
  1956. /*
  1957. * We may or may not have any corrupted sectors (including missing dev
  1958. * and csum mismatch), just let recover_sectors() to handle them all.
  1959. */
  1960. submit_read_wait_bio_list(rbio, &bio_list);
  1961. return recover_sectors(rbio);
  1962. }
  1963. static void raid_wait_write_end_io(struct bio *bio)
  1964. {
  1965. struct btrfs_raid_bio *rbio = bio->bi_private;
  1966. blk_status_t err = bio->bi_status;
  1967. if (err)
  1968. rbio_update_error_bitmap(rbio, bio);
  1969. bio_put(bio);
  1970. if (atomic_dec_and_test(&rbio->stripes_pending))
  1971. wake_up(&rbio->io_wait);
  1972. }
  1973. static void submit_write_bios(struct btrfs_raid_bio *rbio,
  1974. struct bio_list *bio_list)
  1975. {
  1976. struct bio *bio;
  1977. atomic_set(&rbio->stripes_pending, bio_list_size(bio_list));
  1978. while ((bio = bio_list_pop(bio_list))) {
  1979. bio->bi_end_io = raid_wait_write_end_io;
  1980. if (trace_raid56_write_enabled()) {
  1981. struct raid56_bio_trace_info trace_info = { 0 };
  1982. bio_get_trace_info(rbio, bio, &trace_info);
  1983. trace_raid56_write(rbio, bio, &trace_info);
  1984. }
  1985. submit_bio(bio);
  1986. }
  1987. }
  1988. /*
  1989. * To determine if we need to read any sector from the disk.
  1990. * Should only be utilized in RMW path, to skip cached rbio.
  1991. */
  1992. static bool need_read_stripe_sectors(struct btrfs_raid_bio *rbio)
  1993. {
  1994. int i;
  1995. for (i = 0; i < rbio->nr_data * rbio->stripe_nsectors; i++) {
  1996. struct sector_ptr *sector = &rbio->stripe_sectors[i];
  1997. /*
  1998. * We have a sector which doesn't have page nor uptodate,
  1999. * thus this rbio can not be cached one, as cached one must
  2000. * have all its data sectors present and uptodate.
  2001. */
  2002. if (!sector->page || !sector->uptodate)
  2003. return true;
  2004. }
  2005. return false;
  2006. }
  2007. static void rmw_rbio(struct btrfs_raid_bio *rbio)
  2008. {
  2009. struct bio_list bio_list;
  2010. int sectornr;
  2011. int ret = 0;
  2012. /*
  2013. * Allocate the pages for parity first, as P/Q pages will always be
  2014. * needed for both full-stripe and sub-stripe writes.
  2015. */
  2016. ret = alloc_rbio_parity_pages(rbio);
  2017. if (ret < 0)
  2018. goto out;
  2019. /*
  2020. * Either full stripe write, or we have every data sector already
  2021. * cached, can go to write path immediately.
  2022. */
  2023. if (!rbio_is_full(rbio) && need_read_stripe_sectors(rbio)) {
  2024. /*
  2025. * Now we're doing sub-stripe write, also need all data stripes
  2026. * to do the full RMW.
  2027. */
  2028. ret = alloc_rbio_data_pages(rbio);
  2029. if (ret < 0)
  2030. goto out;
  2031. index_rbio_pages(rbio);
  2032. ret = rmw_read_wait_recover(rbio);
  2033. if (ret < 0)
  2034. goto out;
  2035. }
  2036. /*
  2037. * At this stage we're not allowed to add any new bios to the
  2038. * bio list any more, anyone else that wants to change this stripe
  2039. * needs to do their own rmw.
  2040. */
  2041. spin_lock(&rbio->bio_list_lock);
  2042. set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  2043. spin_unlock(&rbio->bio_list_lock);
  2044. bitmap_clear(rbio->error_bitmap, 0, rbio->nr_sectors);
  2045. index_rbio_pages(rbio);
  2046. /*
  2047. * We don't cache full rbios because we're assuming
  2048. * the higher layers are unlikely to use this area of
  2049. * the disk again soon. If they do use it again,
  2050. * hopefully they will send another full bio.
  2051. */
  2052. if (!rbio_is_full(rbio))
  2053. cache_rbio_pages(rbio);
  2054. else
  2055. clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  2056. for (sectornr = 0; sectornr < rbio->stripe_nsectors; sectornr++)
  2057. generate_pq_vertical(rbio, sectornr);
  2058. bio_list_init(&bio_list);
  2059. ret = rmw_assemble_write_bios(rbio, &bio_list);
  2060. if (ret < 0)
  2061. goto out;
  2062. /* We should have at least one bio assembled. */
  2063. ASSERT(bio_list_size(&bio_list));
  2064. submit_write_bios(rbio, &bio_list);
  2065. wait_event(rbio->io_wait, atomic_read(&rbio->stripes_pending) == 0);
  2066. /* We may have more errors than our tolerance during the read. */
  2067. for (sectornr = 0; sectornr < rbio->stripe_nsectors; sectornr++) {
  2068. int found_errors;
  2069. found_errors = get_rbio_veritical_errors(rbio, sectornr, NULL, NULL);
  2070. if (found_errors > rbio->bioc->max_errors) {
  2071. ret = -EIO;
  2072. break;
  2073. }
  2074. }
  2075. out:
  2076. rbio_orig_end_io(rbio, errno_to_blk_status(ret));
  2077. }
  2078. static void rmw_rbio_work(struct work_struct *work)
  2079. {
  2080. struct btrfs_raid_bio *rbio;
  2081. rbio = container_of(work, struct btrfs_raid_bio, work);
  2082. if (lock_stripe_add(rbio) == 0)
  2083. rmw_rbio(rbio);
  2084. }
  2085. static void rmw_rbio_work_locked(struct work_struct *work)
  2086. {
  2087. rmw_rbio(container_of(work, struct btrfs_raid_bio, work));
  2088. }
  2089. /*
  2090. * The following code is used to scrub/replace the parity stripe
  2091. *
  2092. * Caller must have already increased bio_counter for getting @bioc.
  2093. *
  2094. * Note: We need make sure all the pages that add into the scrub/replace
  2095. * raid bio are correct and not be changed during the scrub/replace. That
  2096. * is those pages just hold metadata or file data with checksum.
  2097. */
  2098. struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
  2099. struct btrfs_io_context *bioc,
  2100. struct btrfs_device *scrub_dev,
  2101. unsigned long *dbitmap, int stripe_nsectors)
  2102. {
  2103. struct btrfs_fs_info *fs_info = bioc->fs_info;
  2104. struct btrfs_raid_bio *rbio;
  2105. int i;
  2106. rbio = alloc_rbio(fs_info, bioc);
  2107. if (IS_ERR(rbio))
  2108. return NULL;
  2109. bio_list_add(&rbio->bio_list, bio);
  2110. /*
  2111. * This is a special bio which is used to hold the completion handler
  2112. * and make the scrub rbio is similar to the other types
  2113. */
  2114. ASSERT(!bio->bi_iter.bi_size);
  2115. rbio->operation = BTRFS_RBIO_PARITY_SCRUB;
  2116. /*
  2117. * After mapping bioc with BTRFS_MAP_WRITE, parities have been sorted
  2118. * to the end position, so this search can start from the first parity
  2119. * stripe.
  2120. */
  2121. for (i = rbio->nr_data; i < rbio->real_stripes; i++) {
  2122. if (bioc->stripes[i].dev == scrub_dev) {
  2123. rbio->scrubp = i;
  2124. break;
  2125. }
  2126. }
  2127. ASSERT_RBIO_STRIPE(i < rbio->real_stripes, rbio, i);
  2128. bitmap_copy(&rbio->dbitmap, dbitmap, stripe_nsectors);
  2129. return rbio;
  2130. }
  2131. /*
  2132. * We just scrub the parity that we have correct data on the same horizontal,
  2133. * so we needn't allocate all pages for all the stripes.
  2134. */
  2135. static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
  2136. {
  2137. const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
  2138. int total_sector_nr;
  2139. for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
  2140. total_sector_nr++) {
  2141. struct page *page;
  2142. int sectornr = total_sector_nr % rbio->stripe_nsectors;
  2143. int index = (total_sector_nr * sectorsize) >> PAGE_SHIFT;
  2144. if (!test_bit(sectornr, &rbio->dbitmap))
  2145. continue;
  2146. if (rbio->stripe_pages[index])
  2147. continue;
  2148. page = alloc_page(GFP_NOFS);
  2149. if (!page)
  2150. return -ENOMEM;
  2151. rbio->stripe_pages[index] = page;
  2152. }
  2153. index_stripe_sectors(rbio);
  2154. return 0;
  2155. }
  2156. static int finish_parity_scrub(struct btrfs_raid_bio *rbio)
  2157. {
  2158. struct btrfs_io_context *bioc = rbio->bioc;
  2159. const u32 sectorsize = bioc->fs_info->sectorsize;
  2160. void **pointers = rbio->finish_pointers;
  2161. unsigned long *pbitmap = &rbio->finish_pbitmap;
  2162. int nr_data = rbio->nr_data;
  2163. int stripe;
  2164. int sectornr;
  2165. bool has_qstripe;
  2166. struct sector_ptr p_sector = { 0 };
  2167. struct sector_ptr q_sector = { 0 };
  2168. struct bio_list bio_list;
  2169. int is_replace = 0;
  2170. int ret;
  2171. bio_list_init(&bio_list);
  2172. if (rbio->real_stripes - rbio->nr_data == 1)
  2173. has_qstripe = false;
  2174. else if (rbio->real_stripes - rbio->nr_data == 2)
  2175. has_qstripe = true;
  2176. else
  2177. BUG();
  2178. /*
  2179. * Replace is running and our P/Q stripe is being replaced, then we
  2180. * need to duplicate the final write to replace target.
  2181. */
  2182. if (bioc->replace_nr_stripes && bioc->replace_stripe_src == rbio->scrubp) {
  2183. is_replace = 1;
  2184. bitmap_copy(pbitmap, &rbio->dbitmap, rbio->stripe_nsectors);
  2185. }
  2186. /*
  2187. * Because the higher layers(scrubber) are unlikely to
  2188. * use this area of the disk again soon, so don't cache
  2189. * it.
  2190. */
  2191. clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  2192. p_sector.page = alloc_page(GFP_NOFS);
  2193. if (!p_sector.page)
  2194. return -ENOMEM;
  2195. p_sector.pgoff = 0;
  2196. p_sector.uptodate = 1;
  2197. if (has_qstripe) {
  2198. /* RAID6, allocate and map temp space for the Q stripe */
  2199. q_sector.page = alloc_page(GFP_NOFS);
  2200. if (!q_sector.page) {
  2201. __free_page(p_sector.page);
  2202. p_sector.page = NULL;
  2203. return -ENOMEM;
  2204. }
  2205. q_sector.pgoff = 0;
  2206. q_sector.uptodate = 1;
  2207. pointers[rbio->real_stripes - 1] = kmap_local_page(q_sector.page);
  2208. }
  2209. bitmap_clear(rbio->error_bitmap, 0, rbio->nr_sectors);
  2210. /* Map the parity stripe just once */
  2211. pointers[nr_data] = kmap_local_page(p_sector.page);
  2212. for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors) {
  2213. struct sector_ptr *sector;
  2214. void *parity;
  2215. /* first collect one page from each data stripe */
  2216. for (stripe = 0; stripe < nr_data; stripe++) {
  2217. sector = sector_in_rbio(rbio, stripe, sectornr, 0);
  2218. pointers[stripe] = kmap_local_page(sector->page) +
  2219. sector->pgoff;
  2220. }
  2221. if (has_qstripe) {
  2222. assert_rbio(rbio);
  2223. /* RAID6, call the library function to fill in our P/Q */
  2224. raid6_call.gen_syndrome(rbio->real_stripes, sectorsize,
  2225. pointers);
  2226. } else {
  2227. /* raid5 */
  2228. memcpy(pointers[nr_data], pointers[0], sectorsize);
  2229. run_xor(pointers + 1, nr_data - 1, sectorsize);
  2230. }
  2231. /* Check scrubbing parity and repair it */
  2232. sector = rbio_stripe_sector(rbio, rbio->scrubp, sectornr);
  2233. parity = kmap_local_page(sector->page) + sector->pgoff;
  2234. if (memcmp(parity, pointers[rbio->scrubp], sectorsize) != 0)
  2235. memcpy(parity, pointers[rbio->scrubp], sectorsize);
  2236. else
  2237. /* Parity is right, needn't writeback */
  2238. bitmap_clear(&rbio->dbitmap, sectornr, 1);
  2239. kunmap_local(parity);
  2240. for (stripe = nr_data - 1; stripe >= 0; stripe--)
  2241. kunmap_local(pointers[stripe]);
  2242. }
  2243. kunmap_local(pointers[nr_data]);
  2244. __free_page(p_sector.page);
  2245. p_sector.page = NULL;
  2246. if (q_sector.page) {
  2247. kunmap_local(pointers[rbio->real_stripes - 1]);
  2248. __free_page(q_sector.page);
  2249. q_sector.page = NULL;
  2250. }
  2251. /*
  2252. * time to start writing. Make bios for everything from the
  2253. * higher layers (the bio_list in our rbio) and our p/q. Ignore
  2254. * everything else.
  2255. */
  2256. for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors) {
  2257. struct sector_ptr *sector;
  2258. sector = rbio_stripe_sector(rbio, rbio->scrubp, sectornr);
  2259. ret = rbio_add_io_sector(rbio, &bio_list, sector, rbio->scrubp,
  2260. sectornr, REQ_OP_WRITE);
  2261. if (ret)
  2262. goto cleanup;
  2263. }
  2264. if (!is_replace)
  2265. goto submit_write;
  2266. /*
  2267. * Replace is running and our parity stripe needs to be duplicated to
  2268. * the target device. Check we have a valid source stripe number.
  2269. */
  2270. ASSERT_RBIO(rbio->bioc->replace_stripe_src >= 0, rbio);
  2271. for_each_set_bit(sectornr, pbitmap, rbio->stripe_nsectors) {
  2272. struct sector_ptr *sector;
  2273. sector = rbio_stripe_sector(rbio, rbio->scrubp, sectornr);
  2274. ret = rbio_add_io_sector(rbio, &bio_list, sector,
  2275. rbio->real_stripes,
  2276. sectornr, REQ_OP_WRITE);
  2277. if (ret)
  2278. goto cleanup;
  2279. }
  2280. submit_write:
  2281. submit_write_bios(rbio, &bio_list);
  2282. return 0;
  2283. cleanup:
  2284. bio_list_put(&bio_list);
  2285. return ret;
  2286. }
  2287. static inline int is_data_stripe(struct btrfs_raid_bio *rbio, int stripe)
  2288. {
  2289. if (stripe >= 0 && stripe < rbio->nr_data)
  2290. return 1;
  2291. return 0;
  2292. }
  2293. static int recover_scrub_rbio(struct btrfs_raid_bio *rbio)
  2294. {
  2295. void **pointers = NULL;
  2296. void **unmap_array = NULL;
  2297. int sector_nr;
  2298. int ret = 0;
  2299. /*
  2300. * @pointers array stores the pointer for each sector.
  2301. *
  2302. * @unmap_array stores copy of pointers that does not get reordered
  2303. * during reconstruction so that kunmap_local works.
  2304. */
  2305. pointers = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
  2306. unmap_array = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
  2307. if (!pointers || !unmap_array) {
  2308. ret = -ENOMEM;
  2309. goto out;
  2310. }
  2311. for (sector_nr = 0; sector_nr < rbio->stripe_nsectors; sector_nr++) {
  2312. int dfail = 0, failp = -1;
  2313. int faila;
  2314. int failb;
  2315. int found_errors;
  2316. found_errors = get_rbio_veritical_errors(rbio, sector_nr,
  2317. &faila, &failb);
  2318. if (found_errors > rbio->bioc->max_errors) {
  2319. ret = -EIO;
  2320. goto out;
  2321. }
  2322. if (found_errors == 0)
  2323. continue;
  2324. /* We should have at least one error here. */
  2325. ASSERT(faila >= 0 || failb >= 0);
  2326. if (is_data_stripe(rbio, faila))
  2327. dfail++;
  2328. else if (is_parity_stripe(faila))
  2329. failp = faila;
  2330. if (is_data_stripe(rbio, failb))
  2331. dfail++;
  2332. else if (is_parity_stripe(failb))
  2333. failp = failb;
  2334. /*
  2335. * Because we can not use a scrubbing parity to repair the
  2336. * data, so the capability of the repair is declined. (In the
  2337. * case of RAID5, we can not repair anything.)
  2338. */
  2339. if (dfail > rbio->bioc->max_errors - 1) {
  2340. ret = -EIO;
  2341. goto out;
  2342. }
  2343. /*
  2344. * If all data is good, only parity is correctly, just repair
  2345. * the parity, no need to recover data stripes.
  2346. */
  2347. if (dfail == 0)
  2348. continue;
  2349. /*
  2350. * Here means we got one corrupted data stripe and one
  2351. * corrupted parity on RAID6, if the corrupted parity is
  2352. * scrubbing parity, luckily, use the other one to repair the
  2353. * data, or we can not repair the data stripe.
  2354. */
  2355. if (failp != rbio->scrubp) {
  2356. ret = -EIO;
  2357. goto out;
  2358. }
  2359. ret = recover_vertical(rbio, sector_nr, pointers, unmap_array);
  2360. if (ret < 0)
  2361. goto out;
  2362. }
  2363. out:
  2364. kfree(pointers);
  2365. kfree(unmap_array);
  2366. return ret;
  2367. }
  2368. static int scrub_assemble_read_bios(struct btrfs_raid_bio *rbio)
  2369. {
  2370. struct bio_list bio_list = BIO_EMPTY_LIST;
  2371. int total_sector_nr;
  2372. int ret = 0;
  2373. /* Build a list of bios to read all the missing parts. */
  2374. for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
  2375. total_sector_nr++) {
  2376. int sectornr = total_sector_nr % rbio->stripe_nsectors;
  2377. int stripe = total_sector_nr / rbio->stripe_nsectors;
  2378. struct sector_ptr *sector;
  2379. /* No data in the vertical stripe, no need to read. */
  2380. if (!test_bit(sectornr, &rbio->dbitmap))
  2381. continue;
  2382. /*
  2383. * We want to find all the sectors missing from the rbio and
  2384. * read them from the disk. If sector_in_rbio() finds a sector
  2385. * in the bio list we don't need to read it off the stripe.
  2386. */
  2387. sector = sector_in_rbio(rbio, stripe, sectornr, 1);
  2388. if (sector)
  2389. continue;
  2390. sector = rbio_stripe_sector(rbio, stripe, sectornr);
  2391. /*
  2392. * The bio cache may have handed us an uptodate sector. If so,
  2393. * use it.
  2394. */
  2395. if (sector->uptodate)
  2396. continue;
  2397. ret = rbio_add_io_sector(rbio, &bio_list, sector, stripe,
  2398. sectornr, REQ_OP_READ);
  2399. if (ret) {
  2400. bio_list_put(&bio_list);
  2401. return ret;
  2402. }
  2403. }
  2404. submit_read_wait_bio_list(rbio, &bio_list);
  2405. return 0;
  2406. }
  2407. static void scrub_rbio(struct btrfs_raid_bio *rbio)
  2408. {
  2409. int sector_nr;
  2410. int ret;
  2411. ret = alloc_rbio_essential_pages(rbio);
  2412. if (ret)
  2413. goto out;
  2414. bitmap_clear(rbio->error_bitmap, 0, rbio->nr_sectors);
  2415. ret = scrub_assemble_read_bios(rbio);
  2416. if (ret < 0)
  2417. goto out;
  2418. /* We may have some failures, recover the failed sectors first. */
  2419. ret = recover_scrub_rbio(rbio);
  2420. if (ret < 0)
  2421. goto out;
  2422. /*
  2423. * We have every sector properly prepared. Can finish the scrub
  2424. * and writeback the good content.
  2425. */
  2426. ret = finish_parity_scrub(rbio);
  2427. wait_event(rbio->io_wait, atomic_read(&rbio->stripes_pending) == 0);
  2428. for (sector_nr = 0; sector_nr < rbio->stripe_nsectors; sector_nr++) {
  2429. int found_errors;
  2430. found_errors = get_rbio_veritical_errors(rbio, sector_nr, NULL, NULL);
  2431. if (found_errors > rbio->bioc->max_errors) {
  2432. ret = -EIO;
  2433. break;
  2434. }
  2435. }
  2436. out:
  2437. rbio_orig_end_io(rbio, errno_to_blk_status(ret));
  2438. }
  2439. static void scrub_rbio_work_locked(struct work_struct *work)
  2440. {
  2441. scrub_rbio(container_of(work, struct btrfs_raid_bio, work));
  2442. }
  2443. void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio)
  2444. {
  2445. if (!lock_stripe_add(rbio))
  2446. start_async_work(rbio, scrub_rbio_work_locked);
  2447. }
  2448. /*
  2449. * This is for scrub call sites where we already have correct data contents.
  2450. * This allows us to avoid reading data stripes again.
  2451. *
  2452. * Unfortunately here we have to do page copy, other than reusing the pages.
  2453. * This is due to the fact rbio has its own page management for its cache.
  2454. */
  2455. void raid56_parity_cache_data_pages(struct btrfs_raid_bio *rbio,
  2456. struct page **data_pages, u64 data_logical)
  2457. {
  2458. const u64 offset_in_full_stripe = data_logical -
  2459. rbio->bioc->full_stripe_logical;
  2460. const int page_index = offset_in_full_stripe >> PAGE_SHIFT;
  2461. const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
  2462. const u32 sectors_per_page = PAGE_SIZE / sectorsize;
  2463. int ret;
  2464. /*
  2465. * If we hit ENOMEM temporarily, but later at
  2466. * raid56_parity_submit_scrub_rbio() time it succeeded, we just do
  2467. * the extra read, not a big deal.
  2468. *
  2469. * If we hit ENOMEM later at raid56_parity_submit_scrub_rbio() time,
  2470. * the bio would got proper error number set.
  2471. */
  2472. ret = alloc_rbio_data_pages(rbio);
  2473. if (ret < 0)
  2474. return;
  2475. /* data_logical must be at stripe boundary and inside the full stripe. */
  2476. ASSERT(IS_ALIGNED(offset_in_full_stripe, BTRFS_STRIPE_LEN));
  2477. ASSERT(offset_in_full_stripe < (rbio->nr_data << BTRFS_STRIPE_LEN_SHIFT));
  2478. for (int page_nr = 0; page_nr < (BTRFS_STRIPE_LEN >> PAGE_SHIFT); page_nr++) {
  2479. struct page *dst = rbio->stripe_pages[page_nr + page_index];
  2480. struct page *src = data_pages[page_nr];
  2481. memcpy_page(dst, 0, src, 0, PAGE_SIZE);
  2482. for (int sector_nr = sectors_per_page * page_index;
  2483. sector_nr < sectors_per_page * (page_index + 1);
  2484. sector_nr++)
  2485. rbio->stripe_sectors[sector_nr].uptodate = true;
  2486. }
  2487. }