raid56.c 65 KB

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