raid56.c 66 KB

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