raid56.c 65 KB

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