raid56.c 66 KB

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