raid56.c 67 KB

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