raid56.c 67 KB

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