raid56.c 66 KB

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