raid56.c 68 KB

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