raid56.c 66 KB

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