raid56.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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 <asm/div64.h>
  35. #include "compat.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. struct btrfs_raid_bio {
  59. struct btrfs_fs_info *fs_info;
  60. struct btrfs_bio *bbio;
  61. /*
  62. * logical block numbers for the start of each stripe
  63. * The last one or two are p/q. These are sorted,
  64. * so raid_map[0] is the start of our full stripe
  65. */
  66. u64 *raid_map;
  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. /*
  105. * set if we're doing a parity rebuild
  106. * for a read from higher up, which is handled
  107. * differently from a parity rebuild as part of
  108. * rmw
  109. */
  110. int read_rebuild;
  111. /* first bad stripe */
  112. int faila;
  113. /* second bad stripe (for raid6 use) */
  114. int failb;
  115. /*
  116. * number of pages needed to represent the full
  117. * stripe
  118. */
  119. int nr_pages;
  120. /*
  121. * size of all the bios in the bio_list. This
  122. * helps us decide if the rbio maps to a full
  123. * stripe or not
  124. */
  125. int bio_list_bytes;
  126. atomic_t refs;
  127. /*
  128. * these are two arrays of pointers. We allocate the
  129. * rbio big enough to hold them both and setup their
  130. * locations when the rbio is allocated
  131. */
  132. /* pointers to pages that we allocated for
  133. * reading/writing stripes directly from the disk (including P/Q)
  134. */
  135. struct page **stripe_pages;
  136. /*
  137. * pointers to the pages in the bio_list. Stored
  138. * here for faster lookup
  139. */
  140. struct page **bio_pages;
  141. };
  142. static int __raid56_parity_recover(struct btrfs_raid_bio *rbio);
  143. static noinline void finish_rmw(struct btrfs_raid_bio *rbio);
  144. static void rmw_work(struct btrfs_work *work);
  145. static void read_rebuild_work(struct btrfs_work *work);
  146. static void async_rmw_stripe(struct btrfs_raid_bio *rbio);
  147. static void async_read_rebuild(struct btrfs_raid_bio *rbio);
  148. static int fail_bio_stripe(struct btrfs_raid_bio *rbio, struct bio *bio);
  149. static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed);
  150. static void __free_raid_bio(struct btrfs_raid_bio *rbio);
  151. static void index_rbio_pages(struct btrfs_raid_bio *rbio);
  152. static int alloc_rbio_pages(struct btrfs_raid_bio *rbio);
  153. /*
  154. * the stripe hash table is used for locking, and to collect
  155. * bios in hopes of making a full stripe
  156. */
  157. int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
  158. {
  159. struct btrfs_stripe_hash_table *table;
  160. struct btrfs_stripe_hash_table *x;
  161. struct btrfs_stripe_hash *cur;
  162. struct btrfs_stripe_hash *h;
  163. int num_entries = 1 << BTRFS_STRIPE_HASH_TABLE_BITS;
  164. int i;
  165. if (info->stripe_hash_table)
  166. return 0;
  167. table = kzalloc(sizeof(*table) + sizeof(*h) * num_entries, GFP_NOFS);
  168. if (!table)
  169. return -ENOMEM;
  170. spin_lock_init(&table->cache_lock);
  171. INIT_LIST_HEAD(&table->stripe_cache);
  172. h = table->table;
  173. for (i = 0; i < num_entries; i++) {
  174. cur = h + i;
  175. INIT_LIST_HEAD(&cur->hash_list);
  176. spin_lock_init(&cur->lock);
  177. init_waitqueue_head(&cur->wait);
  178. }
  179. x = cmpxchg(&info->stripe_hash_table, NULL, table);
  180. if (x)
  181. kfree(x);
  182. return 0;
  183. }
  184. /*
  185. * caching an rbio means to copy anything from the
  186. * bio_pages array into the stripe_pages array. We
  187. * use the page uptodate bit in the stripe cache array
  188. * to indicate if it has valid data
  189. *
  190. * once the caching is done, we set the cache ready
  191. * bit.
  192. */
  193. static void cache_rbio_pages(struct btrfs_raid_bio *rbio)
  194. {
  195. int i;
  196. char *s;
  197. char *d;
  198. int ret;
  199. ret = alloc_rbio_pages(rbio);
  200. if (ret)
  201. return;
  202. for (i = 0; i < rbio->nr_pages; i++) {
  203. if (!rbio->bio_pages[i])
  204. continue;
  205. s = kmap(rbio->bio_pages[i]);
  206. d = kmap(rbio->stripe_pages[i]);
  207. memcpy(d, s, PAGE_CACHE_SIZE);
  208. kunmap(rbio->bio_pages[i]);
  209. kunmap(rbio->stripe_pages[i]);
  210. SetPageUptodate(rbio->stripe_pages[i]);
  211. }
  212. set_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  213. }
  214. /*
  215. * we hash on the first logical address of the stripe
  216. */
  217. static int rbio_bucket(struct btrfs_raid_bio *rbio)
  218. {
  219. u64 num = rbio->raid_map[0];
  220. /*
  221. * we shift down quite a bit. We're using byte
  222. * addressing, and most of the lower bits are zeros.
  223. * This tends to upset hash_64, and it consistently
  224. * returns just one or two different values.
  225. *
  226. * shifting off the lower bits fixes things.
  227. */
  228. return hash_64(num >> 16, BTRFS_STRIPE_HASH_TABLE_BITS);
  229. }
  230. /*
  231. * stealing an rbio means taking all the uptodate pages from the stripe
  232. * array in the source rbio and putting them into the destination rbio
  233. */
  234. static void steal_rbio(struct btrfs_raid_bio *src, struct btrfs_raid_bio *dest)
  235. {
  236. int i;
  237. struct page *s;
  238. struct page *d;
  239. if (!test_bit(RBIO_CACHE_READY_BIT, &src->flags))
  240. return;
  241. for (i = 0; i < dest->nr_pages; i++) {
  242. s = src->stripe_pages[i];
  243. if (!s || !PageUptodate(s)) {
  244. continue;
  245. }
  246. d = dest->stripe_pages[i];
  247. if (d)
  248. __free_page(d);
  249. dest->stripe_pages[i] = s;
  250. src->stripe_pages[i] = NULL;
  251. }
  252. }
  253. /*
  254. * merging means we take the bio_list from the victim and
  255. * splice it into the destination. The victim should
  256. * be discarded afterwards.
  257. *
  258. * must be called with dest->rbio_list_lock held
  259. */
  260. static void merge_rbio(struct btrfs_raid_bio *dest,
  261. struct btrfs_raid_bio *victim)
  262. {
  263. bio_list_merge(&dest->bio_list, &victim->bio_list);
  264. dest->bio_list_bytes += victim->bio_list_bytes;
  265. bio_list_init(&victim->bio_list);
  266. }
  267. /*
  268. * used to prune items that are in the cache. The caller
  269. * must hold the hash table lock.
  270. */
  271. static void __remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
  272. {
  273. int bucket = rbio_bucket(rbio);
  274. struct btrfs_stripe_hash_table *table;
  275. struct btrfs_stripe_hash *h;
  276. int freeit = 0;
  277. /*
  278. * check the bit again under the hash table lock.
  279. */
  280. if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
  281. return;
  282. table = rbio->fs_info->stripe_hash_table;
  283. h = table->table + bucket;
  284. /* hold the lock for the bucket because we may be
  285. * removing it from the hash table
  286. */
  287. spin_lock(&h->lock);
  288. /*
  289. * hold the lock for the bio list because we need
  290. * to make sure the bio list is empty
  291. */
  292. spin_lock(&rbio->bio_list_lock);
  293. if (test_and_clear_bit(RBIO_CACHE_BIT, &rbio->flags)) {
  294. list_del_init(&rbio->stripe_cache);
  295. table->cache_size -= 1;
  296. freeit = 1;
  297. /* if the bio list isn't empty, this rbio is
  298. * still involved in an IO. We take it out
  299. * of the cache list, and drop the ref that
  300. * was held for the list.
  301. *
  302. * If the bio_list was empty, we also remove
  303. * the rbio from the hash_table, and drop
  304. * the corresponding ref
  305. */
  306. if (bio_list_empty(&rbio->bio_list)) {
  307. if (!list_empty(&rbio->hash_list)) {
  308. list_del_init(&rbio->hash_list);
  309. atomic_dec(&rbio->refs);
  310. BUG_ON(!list_empty(&rbio->plug_list));
  311. }
  312. }
  313. }
  314. spin_unlock(&rbio->bio_list_lock);
  315. spin_unlock(&h->lock);
  316. if (freeit)
  317. __free_raid_bio(rbio);
  318. }
  319. /*
  320. * prune a given rbio from the cache
  321. */
  322. static void remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
  323. {
  324. struct btrfs_stripe_hash_table *table;
  325. unsigned long flags;
  326. if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
  327. return;
  328. table = rbio->fs_info->stripe_hash_table;
  329. spin_lock_irqsave(&table->cache_lock, flags);
  330. __remove_rbio_from_cache(rbio);
  331. spin_unlock_irqrestore(&table->cache_lock, flags);
  332. }
  333. /*
  334. * remove everything in the cache
  335. */
  336. void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
  337. {
  338. struct btrfs_stripe_hash_table *table;
  339. unsigned long flags;
  340. struct btrfs_raid_bio *rbio;
  341. table = info->stripe_hash_table;
  342. spin_lock_irqsave(&table->cache_lock, flags);
  343. while (!list_empty(&table->stripe_cache)) {
  344. rbio = list_entry(table->stripe_cache.next,
  345. struct btrfs_raid_bio,
  346. stripe_cache);
  347. __remove_rbio_from_cache(rbio);
  348. }
  349. spin_unlock_irqrestore(&table->cache_lock, flags);
  350. }
  351. /*
  352. * remove all cached entries and free the hash table
  353. * used by unmount
  354. */
  355. void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
  356. {
  357. if (!info->stripe_hash_table)
  358. return;
  359. btrfs_clear_rbio_cache(info);
  360. kfree(info->stripe_hash_table);
  361. info->stripe_hash_table = NULL;
  362. }
  363. /*
  364. * insert an rbio into the stripe cache. It
  365. * must have already been prepared by calling
  366. * cache_rbio_pages
  367. *
  368. * If this rbio was already cached, it gets
  369. * moved to the front of the lru.
  370. *
  371. * If the size of the rbio cache is too big, we
  372. * prune an item.
  373. */
  374. static void cache_rbio(struct btrfs_raid_bio *rbio)
  375. {
  376. struct btrfs_stripe_hash_table *table;
  377. unsigned long flags;
  378. if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
  379. return;
  380. table = rbio->fs_info->stripe_hash_table;
  381. spin_lock_irqsave(&table->cache_lock, flags);
  382. spin_lock(&rbio->bio_list_lock);
  383. /* bump our ref if we were not in the list before */
  384. if (!test_and_set_bit(RBIO_CACHE_BIT, &rbio->flags))
  385. atomic_inc(&rbio->refs);
  386. if (!list_empty(&rbio->stripe_cache)){
  387. list_move(&rbio->stripe_cache, &table->stripe_cache);
  388. } else {
  389. list_add(&rbio->stripe_cache, &table->stripe_cache);
  390. table->cache_size += 1;
  391. }
  392. spin_unlock(&rbio->bio_list_lock);
  393. if (table->cache_size > RBIO_CACHE_SIZE) {
  394. struct btrfs_raid_bio *found;
  395. found = list_entry(table->stripe_cache.prev,
  396. struct btrfs_raid_bio,
  397. stripe_cache);
  398. if (found != rbio)
  399. __remove_rbio_from_cache(found);
  400. }
  401. spin_unlock_irqrestore(&table->cache_lock, flags);
  402. return;
  403. }
  404. /*
  405. * helper function to run the xor_blocks api. It is only
  406. * able to do MAX_XOR_BLOCKS at a time, so we need to
  407. * loop through.
  408. */
  409. static void run_xor(void **pages, int src_cnt, ssize_t len)
  410. {
  411. int src_off = 0;
  412. int xor_src_cnt = 0;
  413. void *dest = pages[src_cnt];
  414. while(src_cnt > 0) {
  415. xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
  416. xor_blocks(xor_src_cnt, len, dest, pages + src_off);
  417. src_cnt -= xor_src_cnt;
  418. src_off += xor_src_cnt;
  419. }
  420. }
  421. /*
  422. * returns true if the bio list inside this rbio
  423. * covers an entire stripe (no rmw required).
  424. * Must be called with the bio list lock held, or
  425. * at a time when you know it is impossible to add
  426. * new bios into the list
  427. */
  428. static int __rbio_is_full(struct btrfs_raid_bio *rbio)
  429. {
  430. unsigned long size = rbio->bio_list_bytes;
  431. int ret = 1;
  432. if (size != rbio->nr_data * rbio->stripe_len)
  433. ret = 0;
  434. BUG_ON(size > rbio->nr_data * rbio->stripe_len);
  435. return ret;
  436. }
  437. static int rbio_is_full(struct btrfs_raid_bio *rbio)
  438. {
  439. unsigned long flags;
  440. int ret;
  441. spin_lock_irqsave(&rbio->bio_list_lock, flags);
  442. ret = __rbio_is_full(rbio);
  443. spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
  444. return ret;
  445. }
  446. /*
  447. * returns 1 if it is safe to merge two rbios together.
  448. * The merging is safe if the two rbios correspond to
  449. * the same stripe and if they are both going in the same
  450. * direction (read vs write), and if neither one is
  451. * locked for final IO
  452. *
  453. * The caller is responsible for locking such that
  454. * rmw_locked is safe to test
  455. */
  456. static int rbio_can_merge(struct btrfs_raid_bio *last,
  457. struct btrfs_raid_bio *cur)
  458. {
  459. if (test_bit(RBIO_RMW_LOCKED_BIT, &last->flags) ||
  460. test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags))
  461. return 0;
  462. /*
  463. * we can't merge with cached rbios, since the
  464. * idea is that when we merge the destination
  465. * rbio is going to run our IO for us. We can
  466. * steal from cached rbio's though, other functions
  467. * handle that.
  468. */
  469. if (test_bit(RBIO_CACHE_BIT, &last->flags) ||
  470. test_bit(RBIO_CACHE_BIT, &cur->flags))
  471. return 0;
  472. if (last->raid_map[0] !=
  473. cur->raid_map[0])
  474. return 0;
  475. /* reads can't merge with writes */
  476. if (last->read_rebuild !=
  477. cur->read_rebuild) {
  478. return 0;
  479. }
  480. return 1;
  481. }
  482. /*
  483. * helper to index into the pstripe
  484. */
  485. static struct page *rbio_pstripe_page(struct btrfs_raid_bio *rbio, int index)
  486. {
  487. index += (rbio->nr_data * rbio->stripe_len) >> PAGE_CACHE_SHIFT;
  488. return rbio->stripe_pages[index];
  489. }
  490. /*
  491. * helper to index into the qstripe, returns null
  492. * if there is no qstripe
  493. */
  494. static struct page *rbio_qstripe_page(struct btrfs_raid_bio *rbio, int index)
  495. {
  496. if (rbio->nr_data + 1 == rbio->bbio->num_stripes)
  497. return NULL;
  498. index += ((rbio->nr_data + 1) * rbio->stripe_len) >>
  499. PAGE_CACHE_SHIFT;
  500. return rbio->stripe_pages[index];
  501. }
  502. /*
  503. * The first stripe in the table for a logical address
  504. * has the lock. rbios are added in one of three ways:
  505. *
  506. * 1) Nobody has the stripe locked yet. The rbio is given
  507. * the lock and 0 is returned. The caller must start the IO
  508. * themselves.
  509. *
  510. * 2) Someone has the stripe locked, but we're able to merge
  511. * with the lock owner. The rbio is freed and the IO will
  512. * start automatically along with the existing rbio. 1 is returned.
  513. *
  514. * 3) Someone has the stripe locked, but we're not able to merge.
  515. * The rbio is added to the lock owner's plug list, or merged into
  516. * an rbio already on the plug list. When the lock owner unlocks,
  517. * the next rbio on the list is run and the IO is started automatically.
  518. * 1 is returned
  519. *
  520. * If we return 0, the caller still owns the rbio and must continue with
  521. * IO submission. If we return 1, the caller must assume the rbio has
  522. * already been freed.
  523. */
  524. static noinline int lock_stripe_add(struct btrfs_raid_bio *rbio)
  525. {
  526. int bucket = rbio_bucket(rbio);
  527. struct btrfs_stripe_hash *h = rbio->fs_info->stripe_hash_table->table + bucket;
  528. struct btrfs_raid_bio *cur;
  529. struct btrfs_raid_bio *pending;
  530. unsigned long flags;
  531. DEFINE_WAIT(wait);
  532. struct btrfs_raid_bio *freeit = NULL;
  533. struct btrfs_raid_bio *cache_drop = NULL;
  534. int ret = 0;
  535. int walk = 0;
  536. spin_lock_irqsave(&h->lock, flags);
  537. list_for_each_entry(cur, &h->hash_list, hash_list) {
  538. walk++;
  539. if (cur->raid_map[0] == rbio->raid_map[0]) {
  540. spin_lock(&cur->bio_list_lock);
  541. /* can we steal this cached rbio's pages? */
  542. if (bio_list_empty(&cur->bio_list) &&
  543. list_empty(&cur->plug_list) &&
  544. test_bit(RBIO_CACHE_BIT, &cur->flags) &&
  545. !test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags)) {
  546. list_del_init(&cur->hash_list);
  547. atomic_dec(&cur->refs);
  548. steal_rbio(cur, rbio);
  549. cache_drop = cur;
  550. spin_unlock(&cur->bio_list_lock);
  551. goto lockit;
  552. }
  553. /* can we merge into the lock owner? */
  554. if (rbio_can_merge(cur, rbio)) {
  555. merge_rbio(cur, rbio);
  556. spin_unlock(&cur->bio_list_lock);
  557. freeit = rbio;
  558. ret = 1;
  559. goto out;
  560. }
  561. /*
  562. * we couldn't merge with the running
  563. * rbio, see if we can merge with the
  564. * pending ones. We don't have to
  565. * check for rmw_locked because there
  566. * is no way they are inside finish_rmw
  567. * right now
  568. */
  569. list_for_each_entry(pending, &cur->plug_list,
  570. plug_list) {
  571. if (rbio_can_merge(pending, rbio)) {
  572. merge_rbio(pending, rbio);
  573. spin_unlock(&cur->bio_list_lock);
  574. freeit = rbio;
  575. ret = 1;
  576. goto out;
  577. }
  578. }
  579. /* no merging, put us on the tail of the plug list,
  580. * our rbio will be started with the currently
  581. * running rbio unlocks
  582. */
  583. list_add_tail(&rbio->plug_list, &cur->plug_list);
  584. spin_unlock(&cur->bio_list_lock);
  585. ret = 1;
  586. goto out;
  587. }
  588. }
  589. lockit:
  590. atomic_inc(&rbio->refs);
  591. list_add(&rbio->hash_list, &h->hash_list);
  592. out:
  593. spin_unlock_irqrestore(&h->lock, flags);
  594. if (cache_drop)
  595. remove_rbio_from_cache(cache_drop);
  596. if (freeit)
  597. __free_raid_bio(freeit);
  598. return ret;
  599. }
  600. /*
  601. * called as rmw or parity rebuild is completed. If the plug list has more
  602. * rbios waiting for this stripe, the next one on the list will be started
  603. */
  604. static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
  605. {
  606. int bucket;
  607. struct btrfs_stripe_hash *h;
  608. unsigned long flags;
  609. int keep_cache = 0;
  610. bucket = rbio_bucket(rbio);
  611. h = rbio->fs_info->stripe_hash_table->table + bucket;
  612. if (list_empty(&rbio->plug_list))
  613. cache_rbio(rbio);
  614. spin_lock_irqsave(&h->lock, flags);
  615. spin_lock(&rbio->bio_list_lock);
  616. if (!list_empty(&rbio->hash_list)) {
  617. /*
  618. * if we're still cached and there is no other IO
  619. * to perform, just leave this rbio here for others
  620. * to steal from later
  621. */
  622. if (list_empty(&rbio->plug_list) &&
  623. test_bit(RBIO_CACHE_BIT, &rbio->flags)) {
  624. keep_cache = 1;
  625. clear_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  626. BUG_ON(!bio_list_empty(&rbio->bio_list));
  627. goto done;
  628. }
  629. list_del_init(&rbio->hash_list);
  630. atomic_dec(&rbio->refs);
  631. /*
  632. * we use the plug list to hold all the rbios
  633. * waiting for the chance to lock this stripe.
  634. * hand the lock over to one of them.
  635. */
  636. if (!list_empty(&rbio->plug_list)) {
  637. struct btrfs_raid_bio *next;
  638. struct list_head *head = rbio->plug_list.next;
  639. next = list_entry(head, struct btrfs_raid_bio,
  640. plug_list);
  641. list_del_init(&rbio->plug_list);
  642. list_add(&next->hash_list, &h->hash_list);
  643. atomic_inc(&next->refs);
  644. spin_unlock(&rbio->bio_list_lock);
  645. spin_unlock_irqrestore(&h->lock, flags);
  646. if (next->read_rebuild)
  647. async_read_rebuild(next);
  648. else {
  649. steal_rbio(rbio, next);
  650. async_rmw_stripe(next);
  651. }
  652. goto done_nolock;
  653. } else if (waitqueue_active(&h->wait)) {
  654. spin_unlock(&rbio->bio_list_lock);
  655. spin_unlock_irqrestore(&h->lock, flags);
  656. wake_up(&h->wait);
  657. goto done_nolock;
  658. }
  659. }
  660. done:
  661. spin_unlock(&rbio->bio_list_lock);
  662. spin_unlock_irqrestore(&h->lock, flags);
  663. done_nolock:
  664. if (!keep_cache)
  665. remove_rbio_from_cache(rbio);
  666. }
  667. static void __free_raid_bio(struct btrfs_raid_bio *rbio)
  668. {
  669. int i;
  670. WARN_ON(atomic_read(&rbio->refs) < 0);
  671. if (!atomic_dec_and_test(&rbio->refs))
  672. return;
  673. WARN_ON(!list_empty(&rbio->stripe_cache));
  674. WARN_ON(!list_empty(&rbio->hash_list));
  675. WARN_ON(!bio_list_empty(&rbio->bio_list));
  676. for (i = 0; i < rbio->nr_pages; i++) {
  677. if (rbio->stripe_pages[i]) {
  678. __free_page(rbio->stripe_pages[i]);
  679. rbio->stripe_pages[i] = NULL;
  680. }
  681. }
  682. kfree(rbio->raid_map);
  683. kfree(rbio->bbio);
  684. kfree(rbio);
  685. }
  686. static void free_raid_bio(struct btrfs_raid_bio *rbio)
  687. {
  688. unlock_stripe(rbio);
  689. __free_raid_bio(rbio);
  690. }
  691. /*
  692. * this frees the rbio and runs through all the bios in the
  693. * bio_list and calls end_io on them
  694. */
  695. static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, int err, int uptodate)
  696. {
  697. struct bio *cur = bio_list_get(&rbio->bio_list);
  698. struct bio *next;
  699. free_raid_bio(rbio);
  700. while (cur) {
  701. next = cur->bi_next;
  702. cur->bi_next = NULL;
  703. if (uptodate)
  704. set_bit(BIO_UPTODATE, &cur->bi_flags);
  705. bio_endio(cur, err);
  706. cur = next;
  707. }
  708. }
  709. /*
  710. * end io function used by finish_rmw. When we finally
  711. * get here, we've written a full stripe
  712. */
  713. static void raid_write_end_io(struct bio *bio, int err)
  714. {
  715. struct btrfs_raid_bio *rbio = bio->bi_private;
  716. if (err)
  717. fail_bio_stripe(rbio, bio);
  718. bio_put(bio);
  719. if (!atomic_dec_and_test(&rbio->bbio->stripes_pending))
  720. return;
  721. err = 0;
  722. /* OK, we have read all the stripes we need to. */
  723. if (atomic_read(&rbio->bbio->error) > rbio->bbio->max_errors)
  724. err = -EIO;
  725. rbio_orig_end_io(rbio, err, 0);
  726. return;
  727. }
  728. /*
  729. * the read/modify/write code wants to use the original bio for
  730. * any pages it included, and then use the rbio for everything
  731. * else. This function decides if a given index (stripe number)
  732. * and page number in that stripe fall inside the original bio
  733. * or the rbio.
  734. *
  735. * if you set bio_list_only, you'll get a NULL back for any ranges
  736. * that are outside the bio_list
  737. *
  738. * This doesn't take any refs on anything, you get a bare page pointer
  739. * and the caller must bump refs as required.
  740. *
  741. * You must call index_rbio_pages once before you can trust
  742. * the answers from this function.
  743. */
  744. static struct page *page_in_rbio(struct btrfs_raid_bio *rbio,
  745. int index, int pagenr, int bio_list_only)
  746. {
  747. int chunk_page;
  748. struct page *p = NULL;
  749. chunk_page = index * (rbio->stripe_len >> PAGE_SHIFT) + pagenr;
  750. spin_lock_irq(&rbio->bio_list_lock);
  751. p = rbio->bio_pages[chunk_page];
  752. spin_unlock_irq(&rbio->bio_list_lock);
  753. if (p || bio_list_only)
  754. return p;
  755. return rbio->stripe_pages[chunk_page];
  756. }
  757. /*
  758. * number of pages we need for the entire stripe across all the
  759. * drives
  760. */
  761. static unsigned long rbio_nr_pages(unsigned long stripe_len, int nr_stripes)
  762. {
  763. unsigned long nr = stripe_len * nr_stripes;
  764. return (nr + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  765. }
  766. /*
  767. * allocation and initial setup for the btrfs_raid_bio. Not
  768. * this does not allocate any pages for rbio->pages.
  769. */
  770. static struct btrfs_raid_bio *alloc_rbio(struct btrfs_root *root,
  771. struct btrfs_bio *bbio, u64 *raid_map,
  772. u64 stripe_len)
  773. {
  774. struct btrfs_raid_bio *rbio;
  775. int nr_data = 0;
  776. int num_pages = rbio_nr_pages(stripe_len, bbio->num_stripes);
  777. void *p;
  778. rbio = kzalloc(sizeof(*rbio) + num_pages * sizeof(struct page *) * 2,
  779. GFP_NOFS);
  780. if (!rbio) {
  781. kfree(raid_map);
  782. kfree(bbio);
  783. return ERR_PTR(-ENOMEM);
  784. }
  785. bio_list_init(&rbio->bio_list);
  786. INIT_LIST_HEAD(&rbio->plug_list);
  787. spin_lock_init(&rbio->bio_list_lock);
  788. INIT_LIST_HEAD(&rbio->stripe_cache);
  789. INIT_LIST_HEAD(&rbio->hash_list);
  790. rbio->bbio = bbio;
  791. rbio->raid_map = raid_map;
  792. rbio->fs_info = root->fs_info;
  793. rbio->stripe_len = stripe_len;
  794. rbio->nr_pages = num_pages;
  795. rbio->faila = -1;
  796. rbio->failb = -1;
  797. atomic_set(&rbio->refs, 1);
  798. /*
  799. * the stripe_pages and bio_pages array point to the extra
  800. * memory we allocated past the end of the rbio
  801. */
  802. p = rbio + 1;
  803. rbio->stripe_pages = p;
  804. rbio->bio_pages = p + sizeof(struct page *) * num_pages;
  805. if (raid_map[bbio->num_stripes - 1] == RAID6_Q_STRIPE)
  806. nr_data = bbio->num_stripes - 2;
  807. else
  808. nr_data = bbio->num_stripes - 1;
  809. rbio->nr_data = nr_data;
  810. return rbio;
  811. }
  812. /* allocate pages for all the stripes in the bio, including parity */
  813. static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
  814. {
  815. int i;
  816. struct page *page;
  817. for (i = 0; i < rbio->nr_pages; i++) {
  818. if (rbio->stripe_pages[i])
  819. continue;
  820. page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  821. if (!page)
  822. return -ENOMEM;
  823. rbio->stripe_pages[i] = page;
  824. ClearPageUptodate(page);
  825. }
  826. return 0;
  827. }
  828. /* allocate pages for just the p/q stripes */
  829. static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
  830. {
  831. int i;
  832. struct page *page;
  833. i = (rbio->nr_data * rbio->stripe_len) >> PAGE_CACHE_SHIFT;
  834. for (; i < rbio->nr_pages; i++) {
  835. if (rbio->stripe_pages[i])
  836. continue;
  837. page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
  838. if (!page)
  839. return -ENOMEM;
  840. rbio->stripe_pages[i] = page;
  841. }
  842. return 0;
  843. }
  844. /*
  845. * add a single page from a specific stripe into our list of bios for IO
  846. * this will try to merge into existing bios if possible, and returns
  847. * zero if all went well.
  848. */
  849. int rbio_add_io_page(struct btrfs_raid_bio *rbio,
  850. struct bio_list *bio_list,
  851. struct page *page,
  852. int stripe_nr,
  853. unsigned long page_index,
  854. unsigned long bio_max_len)
  855. {
  856. struct bio *last = bio_list->tail;
  857. u64 last_end = 0;
  858. int ret;
  859. struct bio *bio;
  860. struct btrfs_bio_stripe *stripe;
  861. u64 disk_start;
  862. stripe = &rbio->bbio->stripes[stripe_nr];
  863. disk_start = stripe->physical + (page_index << PAGE_CACHE_SHIFT);
  864. /* if the device is missing, just fail this stripe */
  865. if (!stripe->dev->bdev)
  866. return fail_rbio_index(rbio, stripe_nr);
  867. /* see if we can add this page onto our existing bio */
  868. if (last) {
  869. last_end = (u64)last->bi_sector << 9;
  870. last_end += last->bi_size;
  871. /*
  872. * we can't merge these if they are from different
  873. * devices or if they are not contiguous
  874. */
  875. if (last_end == disk_start && stripe->dev->bdev &&
  876. test_bit(BIO_UPTODATE, &last->bi_flags) &&
  877. last->bi_bdev == stripe->dev->bdev) {
  878. ret = bio_add_page(last, page, PAGE_CACHE_SIZE, 0);
  879. if (ret == PAGE_CACHE_SIZE)
  880. return 0;
  881. }
  882. }
  883. /* put a new bio on the list */
  884. bio = bio_alloc(GFP_NOFS, bio_max_len >> PAGE_SHIFT?:1);
  885. if (!bio)
  886. return -ENOMEM;
  887. bio->bi_size = 0;
  888. bio->bi_bdev = stripe->dev->bdev;
  889. bio->bi_sector = disk_start >> 9;
  890. set_bit(BIO_UPTODATE, &bio->bi_flags);
  891. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  892. bio_list_add(bio_list, bio);
  893. return 0;
  894. }
  895. /*
  896. * while we're doing the read/modify/write cycle, we could
  897. * have errors in reading pages off the disk. This checks
  898. * for errors and if we're not able to read the page it'll
  899. * trigger parity reconstruction. The rmw will be finished
  900. * after we've reconstructed the failed stripes
  901. */
  902. static void validate_rbio_for_rmw(struct btrfs_raid_bio *rbio)
  903. {
  904. if (rbio->faila >= 0 || rbio->failb >= 0) {
  905. BUG_ON(rbio->faila == rbio->bbio->num_stripes - 1);
  906. __raid56_parity_recover(rbio);
  907. } else {
  908. finish_rmw(rbio);
  909. }
  910. }
  911. /*
  912. * these are just the pages from the rbio array, not from anything
  913. * the FS sent down to us
  914. */
  915. static struct page *rbio_stripe_page(struct btrfs_raid_bio *rbio, int stripe, int page)
  916. {
  917. int index;
  918. index = stripe * (rbio->stripe_len >> PAGE_CACHE_SHIFT);
  919. index += page;
  920. return rbio->stripe_pages[index];
  921. }
  922. /*
  923. * helper function to walk our bio list and populate the bio_pages array with
  924. * the result. This seems expensive, but it is faster than constantly
  925. * searching through the bio list as we setup the IO in finish_rmw or stripe
  926. * reconstruction.
  927. *
  928. * This must be called before you trust the answers from page_in_rbio
  929. */
  930. static void index_rbio_pages(struct btrfs_raid_bio *rbio)
  931. {
  932. struct bio *bio;
  933. u64 start;
  934. unsigned long stripe_offset;
  935. unsigned long page_index;
  936. struct page *p;
  937. int i;
  938. spin_lock_irq(&rbio->bio_list_lock);
  939. bio_list_for_each(bio, &rbio->bio_list) {
  940. start = (u64)bio->bi_sector << 9;
  941. stripe_offset = start - rbio->raid_map[0];
  942. page_index = stripe_offset >> PAGE_CACHE_SHIFT;
  943. for (i = 0; i < bio->bi_vcnt; i++) {
  944. p = bio->bi_io_vec[i].bv_page;
  945. rbio->bio_pages[page_index + i] = p;
  946. }
  947. }
  948. spin_unlock_irq(&rbio->bio_list_lock);
  949. }
  950. /*
  951. * this is called from one of two situations. We either
  952. * have a full stripe from the higher layers, or we've read all
  953. * the missing bits off disk.
  954. *
  955. * This will calculate the parity and then send down any
  956. * changed blocks.
  957. */
  958. static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
  959. {
  960. struct btrfs_bio *bbio = rbio->bbio;
  961. void *pointers[bbio->num_stripes];
  962. int stripe_len = rbio->stripe_len;
  963. int nr_data = rbio->nr_data;
  964. int stripe;
  965. int pagenr;
  966. int p_stripe = -1;
  967. int q_stripe = -1;
  968. struct bio_list bio_list;
  969. struct bio *bio;
  970. int pages_per_stripe = stripe_len >> PAGE_CACHE_SHIFT;
  971. int ret;
  972. bio_list_init(&bio_list);
  973. if (bbio->num_stripes - rbio->nr_data == 1) {
  974. p_stripe = bbio->num_stripes - 1;
  975. } else if (bbio->num_stripes - rbio->nr_data == 2) {
  976. p_stripe = bbio->num_stripes - 2;
  977. q_stripe = bbio->num_stripes - 1;
  978. } else {
  979. BUG();
  980. }
  981. /* at this point we either have a full stripe,
  982. * or we've read the full stripe from the drive.
  983. * recalculate the parity and write the new results.
  984. *
  985. * We're not allowed to add any new bios to the
  986. * bio list here, anyone else that wants to
  987. * change this stripe needs to do their own rmw.
  988. */
  989. spin_lock_irq(&rbio->bio_list_lock);
  990. set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  991. spin_unlock_irq(&rbio->bio_list_lock);
  992. atomic_set(&rbio->bbio->error, 0);
  993. /*
  994. * now that we've set rmw_locked, run through the
  995. * bio list one last time and map the page pointers
  996. *
  997. * We don't cache full rbios because we're assuming
  998. * the higher layers are unlikely to use this area of
  999. * the disk again soon. If they do use it again,
  1000. * hopefully they will send another full bio.
  1001. */
  1002. index_rbio_pages(rbio);
  1003. if (!rbio_is_full(rbio))
  1004. cache_rbio_pages(rbio);
  1005. else
  1006. clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  1007. for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) {
  1008. struct page *p;
  1009. /* first collect one page from each data stripe */
  1010. for (stripe = 0; stripe < nr_data; stripe++) {
  1011. p = page_in_rbio(rbio, stripe, pagenr, 0);
  1012. pointers[stripe] = kmap(p);
  1013. }
  1014. /* then add the parity stripe */
  1015. p = rbio_pstripe_page(rbio, pagenr);
  1016. SetPageUptodate(p);
  1017. pointers[stripe++] = kmap(p);
  1018. if (q_stripe != -1) {
  1019. /*
  1020. * raid6, add the qstripe and call the
  1021. * library function to fill in our p/q
  1022. */
  1023. p = rbio_qstripe_page(rbio, pagenr);
  1024. SetPageUptodate(p);
  1025. pointers[stripe++] = kmap(p);
  1026. raid6_call.gen_syndrome(bbio->num_stripes, PAGE_SIZE,
  1027. pointers);
  1028. } else {
  1029. /* raid5 */
  1030. memcpy(pointers[nr_data], pointers[0], PAGE_SIZE);
  1031. run_xor(pointers + 1, nr_data - 1, PAGE_CACHE_SIZE);
  1032. }
  1033. for (stripe = 0; stripe < bbio->num_stripes; stripe++)
  1034. kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
  1035. }
  1036. /*
  1037. * time to start writing. Make bios for everything from the
  1038. * higher layers (the bio_list in our rbio) and our p/q. Ignore
  1039. * everything else.
  1040. */
  1041. for (stripe = 0; stripe < bbio->num_stripes; stripe++) {
  1042. for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) {
  1043. struct page *page;
  1044. if (stripe < rbio->nr_data) {
  1045. page = page_in_rbio(rbio, stripe, pagenr, 1);
  1046. if (!page)
  1047. continue;
  1048. } else {
  1049. page = rbio_stripe_page(rbio, stripe, pagenr);
  1050. }
  1051. ret = rbio_add_io_page(rbio, &bio_list,
  1052. page, stripe, pagenr, rbio->stripe_len);
  1053. if (ret)
  1054. goto cleanup;
  1055. }
  1056. }
  1057. atomic_set(&bbio->stripes_pending, bio_list_size(&bio_list));
  1058. BUG_ON(atomic_read(&bbio->stripes_pending) == 0);
  1059. while (1) {
  1060. bio = bio_list_pop(&bio_list);
  1061. if (!bio)
  1062. break;
  1063. bio->bi_private = rbio;
  1064. bio->bi_end_io = raid_write_end_io;
  1065. BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
  1066. submit_bio(WRITE, bio);
  1067. }
  1068. return;
  1069. cleanup:
  1070. rbio_orig_end_io(rbio, -EIO, 0);
  1071. }
  1072. /*
  1073. * helper to find the stripe number for a given bio. Used to figure out which
  1074. * stripe has failed. This expects the bio to correspond to a physical disk,
  1075. * so it looks up based on physical sector numbers.
  1076. */
  1077. static int find_bio_stripe(struct btrfs_raid_bio *rbio,
  1078. struct bio *bio)
  1079. {
  1080. u64 physical = bio->bi_sector;
  1081. u64 stripe_start;
  1082. int i;
  1083. struct btrfs_bio_stripe *stripe;
  1084. physical <<= 9;
  1085. for (i = 0; i < rbio->bbio->num_stripes; i++) {
  1086. stripe = &rbio->bbio->stripes[i];
  1087. stripe_start = stripe->physical;
  1088. if (physical >= stripe_start &&
  1089. physical < stripe_start + rbio->stripe_len) {
  1090. return i;
  1091. }
  1092. }
  1093. return -1;
  1094. }
  1095. /*
  1096. * helper to find the stripe number for a given
  1097. * bio (before mapping). Used to figure out which stripe has
  1098. * failed. This looks up based on logical block numbers.
  1099. */
  1100. static int find_logical_bio_stripe(struct btrfs_raid_bio *rbio,
  1101. struct bio *bio)
  1102. {
  1103. u64 logical = bio->bi_sector;
  1104. u64 stripe_start;
  1105. int i;
  1106. logical <<= 9;
  1107. for (i = 0; i < rbio->nr_data; i++) {
  1108. stripe_start = rbio->raid_map[i];
  1109. if (logical >= stripe_start &&
  1110. logical < stripe_start + rbio->stripe_len) {
  1111. return i;
  1112. }
  1113. }
  1114. return -1;
  1115. }
  1116. /*
  1117. * returns -EIO if we had too many failures
  1118. */
  1119. static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed)
  1120. {
  1121. unsigned long flags;
  1122. int ret = 0;
  1123. spin_lock_irqsave(&rbio->bio_list_lock, flags);
  1124. /* we already know this stripe is bad, move on */
  1125. if (rbio->faila == failed || rbio->failb == failed)
  1126. goto out;
  1127. if (rbio->faila == -1) {
  1128. /* first failure on this rbio */
  1129. rbio->faila = failed;
  1130. atomic_inc(&rbio->bbio->error);
  1131. } else if (rbio->failb == -1) {
  1132. /* second failure on this rbio */
  1133. rbio->failb = failed;
  1134. atomic_inc(&rbio->bbio->error);
  1135. } else {
  1136. ret = -EIO;
  1137. }
  1138. out:
  1139. spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
  1140. return ret;
  1141. }
  1142. /*
  1143. * helper to fail a stripe based on a physical disk
  1144. * bio.
  1145. */
  1146. static int fail_bio_stripe(struct btrfs_raid_bio *rbio,
  1147. struct bio *bio)
  1148. {
  1149. int failed = find_bio_stripe(rbio, bio);
  1150. if (failed < 0)
  1151. return -EIO;
  1152. return fail_rbio_index(rbio, failed);
  1153. }
  1154. /*
  1155. * this sets each page in the bio uptodate. It should only be used on private
  1156. * rbio pages, nothing that comes in from the higher layers
  1157. */
  1158. static void set_bio_pages_uptodate(struct bio *bio)
  1159. {
  1160. int i;
  1161. struct page *p;
  1162. for (i = 0; i < bio->bi_vcnt; i++) {
  1163. p = bio->bi_io_vec[i].bv_page;
  1164. SetPageUptodate(p);
  1165. }
  1166. }
  1167. /*
  1168. * end io for the read phase of the rmw cycle. All the bios here are physical
  1169. * stripe bios we've read from the disk so we can recalculate the parity of the
  1170. * stripe.
  1171. *
  1172. * This will usually kick off finish_rmw once all the bios are read in, but it
  1173. * may trigger parity reconstruction if we had any errors along the way
  1174. */
  1175. static void raid_rmw_end_io(struct bio *bio, int err)
  1176. {
  1177. struct btrfs_raid_bio *rbio = bio->bi_private;
  1178. if (err)
  1179. fail_bio_stripe(rbio, bio);
  1180. else
  1181. set_bio_pages_uptodate(bio);
  1182. bio_put(bio);
  1183. if (!atomic_dec_and_test(&rbio->bbio->stripes_pending))
  1184. return;
  1185. err = 0;
  1186. if (atomic_read(&rbio->bbio->error) > rbio->bbio->max_errors)
  1187. goto cleanup;
  1188. /*
  1189. * this will normally call finish_rmw to start our write
  1190. * but if there are any failed stripes we'll reconstruct
  1191. * from parity first
  1192. */
  1193. validate_rbio_for_rmw(rbio);
  1194. return;
  1195. cleanup:
  1196. rbio_orig_end_io(rbio, -EIO, 0);
  1197. }
  1198. static void async_rmw_stripe(struct btrfs_raid_bio *rbio)
  1199. {
  1200. rbio->work.flags = 0;
  1201. rbio->work.func = rmw_work;
  1202. btrfs_queue_worker(&rbio->fs_info->rmw_workers,
  1203. &rbio->work);
  1204. }
  1205. static void async_read_rebuild(struct btrfs_raid_bio *rbio)
  1206. {
  1207. rbio->work.flags = 0;
  1208. rbio->work.func = read_rebuild_work;
  1209. btrfs_queue_worker(&rbio->fs_info->rmw_workers,
  1210. &rbio->work);
  1211. }
  1212. /*
  1213. * the stripe must be locked by the caller. It will
  1214. * unlock after all the writes are done
  1215. */
  1216. static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
  1217. {
  1218. int bios_to_read = 0;
  1219. struct btrfs_bio *bbio = rbio->bbio;
  1220. struct bio_list bio_list;
  1221. int ret;
  1222. int nr_pages = (rbio->stripe_len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1223. int pagenr;
  1224. int stripe;
  1225. struct bio *bio;
  1226. bio_list_init(&bio_list);
  1227. ret = alloc_rbio_pages(rbio);
  1228. if (ret)
  1229. goto cleanup;
  1230. index_rbio_pages(rbio);
  1231. atomic_set(&rbio->bbio->error, 0);
  1232. /*
  1233. * build a list of bios to read all the missing parts of this
  1234. * stripe
  1235. */
  1236. for (stripe = 0; stripe < rbio->nr_data; stripe++) {
  1237. for (pagenr = 0; pagenr < nr_pages; pagenr++) {
  1238. struct page *page;
  1239. /*
  1240. * we want to find all the pages missing from
  1241. * the rbio and read them from the disk. If
  1242. * page_in_rbio finds a page in the bio list
  1243. * we don't need to read it off the stripe.
  1244. */
  1245. page = page_in_rbio(rbio, stripe, pagenr, 1);
  1246. if (page)
  1247. continue;
  1248. page = rbio_stripe_page(rbio, stripe, pagenr);
  1249. /*
  1250. * the bio cache may have handed us an uptodate
  1251. * page. If so, be happy and use it
  1252. */
  1253. if (PageUptodate(page))
  1254. continue;
  1255. ret = rbio_add_io_page(rbio, &bio_list, page,
  1256. stripe, pagenr, rbio->stripe_len);
  1257. if (ret)
  1258. goto cleanup;
  1259. }
  1260. }
  1261. bios_to_read = bio_list_size(&bio_list);
  1262. if (!bios_to_read) {
  1263. /*
  1264. * this can happen if others have merged with
  1265. * us, it means there is nothing left to read.
  1266. * But if there are missing devices it may not be
  1267. * safe to do the full stripe write yet.
  1268. */
  1269. goto finish;
  1270. }
  1271. /*
  1272. * the bbio may be freed once we submit the last bio. Make sure
  1273. * not to touch it after that
  1274. */
  1275. atomic_set(&bbio->stripes_pending, bios_to_read);
  1276. while (1) {
  1277. bio = bio_list_pop(&bio_list);
  1278. if (!bio)
  1279. break;
  1280. bio->bi_private = rbio;
  1281. bio->bi_end_io = raid_rmw_end_io;
  1282. btrfs_bio_wq_end_io(rbio->fs_info, bio,
  1283. BTRFS_WQ_ENDIO_RAID56);
  1284. BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
  1285. submit_bio(READ, bio);
  1286. }
  1287. /* the actual write will happen once the reads are done */
  1288. return 0;
  1289. cleanup:
  1290. rbio_orig_end_io(rbio, -EIO, 0);
  1291. return -EIO;
  1292. finish:
  1293. validate_rbio_for_rmw(rbio);
  1294. return 0;
  1295. }
  1296. /*
  1297. * if the upper layers pass in a full stripe, we thank them by only allocating
  1298. * enough pages to hold the parity, and sending it all down quickly.
  1299. */
  1300. static int full_stripe_write(struct btrfs_raid_bio *rbio)
  1301. {
  1302. int ret;
  1303. ret = alloc_rbio_parity_pages(rbio);
  1304. if (ret)
  1305. return ret;
  1306. ret = lock_stripe_add(rbio);
  1307. if (ret == 0)
  1308. finish_rmw(rbio);
  1309. return 0;
  1310. }
  1311. /*
  1312. * partial stripe writes get handed over to async helpers.
  1313. * We're really hoping to merge a few more writes into this
  1314. * rbio before calculating new parity
  1315. */
  1316. static int partial_stripe_write(struct btrfs_raid_bio *rbio)
  1317. {
  1318. int ret;
  1319. ret = lock_stripe_add(rbio);
  1320. if (ret == 0)
  1321. async_rmw_stripe(rbio);
  1322. return 0;
  1323. }
  1324. /*
  1325. * sometimes while we were reading from the drive to
  1326. * recalculate parity, enough new bios come into create
  1327. * a full stripe. So we do a check here to see if we can
  1328. * go directly to finish_rmw
  1329. */
  1330. static int __raid56_parity_write(struct btrfs_raid_bio *rbio)
  1331. {
  1332. /* head off into rmw land if we don't have a full stripe */
  1333. if (!rbio_is_full(rbio))
  1334. return partial_stripe_write(rbio);
  1335. return full_stripe_write(rbio);
  1336. }
  1337. /*
  1338. * We use plugging call backs to collect full stripes.
  1339. * Any time we get a partial stripe write while plugged
  1340. * we collect it into a list. When the unplug comes down,
  1341. * we sort the list by logical block number and merge
  1342. * everything we can into the same rbios
  1343. */
  1344. struct btrfs_plug_cb {
  1345. struct blk_plug_cb cb;
  1346. struct btrfs_fs_info *info;
  1347. struct list_head rbio_list;
  1348. struct btrfs_work work;
  1349. };
  1350. /*
  1351. * rbios on the plug list are sorted for easier merging.
  1352. */
  1353. static int plug_cmp(void *priv, struct list_head *a, struct list_head *b)
  1354. {
  1355. struct btrfs_raid_bio *ra = container_of(a, struct btrfs_raid_bio,
  1356. plug_list);
  1357. struct btrfs_raid_bio *rb = container_of(b, struct btrfs_raid_bio,
  1358. plug_list);
  1359. u64 a_sector = ra->bio_list.head->bi_sector;
  1360. u64 b_sector = rb->bio_list.head->bi_sector;
  1361. if (a_sector < b_sector)
  1362. return -1;
  1363. if (a_sector > b_sector)
  1364. return 1;
  1365. return 0;
  1366. }
  1367. static void run_plug(struct btrfs_plug_cb *plug)
  1368. {
  1369. struct btrfs_raid_bio *cur;
  1370. struct btrfs_raid_bio *last = NULL;
  1371. /*
  1372. * sort our plug list then try to merge
  1373. * everything we can in hopes of creating full
  1374. * stripes.
  1375. */
  1376. list_sort(NULL, &plug->rbio_list, plug_cmp);
  1377. while (!list_empty(&plug->rbio_list)) {
  1378. cur = list_entry(plug->rbio_list.next,
  1379. struct btrfs_raid_bio, plug_list);
  1380. list_del_init(&cur->plug_list);
  1381. if (rbio_is_full(cur)) {
  1382. /* we have a full stripe, send it down */
  1383. full_stripe_write(cur);
  1384. continue;
  1385. }
  1386. if (last) {
  1387. if (rbio_can_merge(last, cur)) {
  1388. merge_rbio(last, cur);
  1389. __free_raid_bio(cur);
  1390. continue;
  1391. }
  1392. __raid56_parity_write(last);
  1393. }
  1394. last = cur;
  1395. }
  1396. if (last) {
  1397. __raid56_parity_write(last);
  1398. }
  1399. kfree(plug);
  1400. }
  1401. /*
  1402. * if the unplug comes from schedule, we have to push the
  1403. * work off to a helper thread
  1404. */
  1405. static void unplug_work(struct btrfs_work *work)
  1406. {
  1407. struct btrfs_plug_cb *plug;
  1408. plug = container_of(work, struct btrfs_plug_cb, work);
  1409. run_plug(plug);
  1410. }
  1411. static void btrfs_raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
  1412. {
  1413. struct btrfs_plug_cb *plug;
  1414. plug = container_of(cb, struct btrfs_plug_cb, cb);
  1415. if (from_schedule) {
  1416. plug->work.flags = 0;
  1417. plug->work.func = unplug_work;
  1418. btrfs_queue_worker(&plug->info->rmw_workers,
  1419. &plug->work);
  1420. return;
  1421. }
  1422. run_plug(plug);
  1423. }
  1424. /*
  1425. * our main entry point for writes from the rest of the FS.
  1426. */
  1427. int raid56_parity_write(struct btrfs_root *root, struct bio *bio,
  1428. struct btrfs_bio *bbio, u64 *raid_map,
  1429. u64 stripe_len)
  1430. {
  1431. struct btrfs_raid_bio *rbio;
  1432. struct btrfs_plug_cb *plug = NULL;
  1433. struct blk_plug_cb *cb;
  1434. rbio = alloc_rbio(root, bbio, raid_map, stripe_len);
  1435. if (IS_ERR(rbio)) {
  1436. kfree(raid_map);
  1437. kfree(bbio);
  1438. return PTR_ERR(rbio);
  1439. }
  1440. bio_list_add(&rbio->bio_list, bio);
  1441. rbio->bio_list_bytes = bio->bi_size;
  1442. /*
  1443. * don't plug on full rbios, just get them out the door
  1444. * as quickly as we can
  1445. */
  1446. if (rbio_is_full(rbio))
  1447. return full_stripe_write(rbio);
  1448. cb = blk_check_plugged(btrfs_raid_unplug, root->fs_info,
  1449. sizeof(*plug));
  1450. if (cb) {
  1451. plug = container_of(cb, struct btrfs_plug_cb, cb);
  1452. if (!plug->info) {
  1453. plug->info = root->fs_info;
  1454. INIT_LIST_HEAD(&plug->rbio_list);
  1455. }
  1456. list_add_tail(&rbio->plug_list, &plug->rbio_list);
  1457. } else {
  1458. return __raid56_parity_write(rbio);
  1459. }
  1460. return 0;
  1461. }
  1462. /*
  1463. * all parity reconstruction happens here. We've read in everything
  1464. * we can find from the drives and this does the heavy lifting of
  1465. * sorting the good from the bad.
  1466. */
  1467. static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
  1468. {
  1469. int pagenr, stripe;
  1470. void **pointers;
  1471. int faila = -1, failb = -1;
  1472. int nr_pages = (rbio->stripe_len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1473. struct page *page;
  1474. int err;
  1475. int i;
  1476. pointers = kzalloc(rbio->bbio->num_stripes * sizeof(void *),
  1477. GFP_NOFS);
  1478. if (!pointers) {
  1479. err = -ENOMEM;
  1480. goto cleanup_io;
  1481. }
  1482. faila = rbio->faila;
  1483. failb = rbio->failb;
  1484. if (rbio->read_rebuild) {
  1485. spin_lock_irq(&rbio->bio_list_lock);
  1486. set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
  1487. spin_unlock_irq(&rbio->bio_list_lock);
  1488. }
  1489. index_rbio_pages(rbio);
  1490. for (pagenr = 0; pagenr < nr_pages; pagenr++) {
  1491. /* setup our array of pointers with pages
  1492. * from each stripe
  1493. */
  1494. for (stripe = 0; stripe < rbio->bbio->num_stripes; stripe++) {
  1495. /*
  1496. * if we're rebuilding a read, we have to use
  1497. * pages from the bio list
  1498. */
  1499. if (rbio->read_rebuild &&
  1500. (stripe == faila || stripe == failb)) {
  1501. page = page_in_rbio(rbio, stripe, pagenr, 0);
  1502. } else {
  1503. page = rbio_stripe_page(rbio, stripe, pagenr);
  1504. }
  1505. pointers[stripe] = kmap(page);
  1506. }
  1507. /* all raid6 handling here */
  1508. if (rbio->raid_map[rbio->bbio->num_stripes - 1] ==
  1509. RAID6_Q_STRIPE) {
  1510. /*
  1511. * single failure, rebuild from parity raid5
  1512. * style
  1513. */
  1514. if (failb < 0) {
  1515. if (faila == rbio->nr_data) {
  1516. /*
  1517. * Just the P stripe has failed, without
  1518. * a bad data or Q stripe.
  1519. * TODO, we should redo the xor here.
  1520. */
  1521. err = -EIO;
  1522. goto cleanup;
  1523. }
  1524. /*
  1525. * a single failure in raid6 is rebuilt
  1526. * in the pstripe code below
  1527. */
  1528. goto pstripe;
  1529. }
  1530. /* make sure our ps and qs are in order */
  1531. if (faila > failb) {
  1532. int tmp = failb;
  1533. failb = faila;
  1534. faila = tmp;
  1535. }
  1536. /* if the q stripe is failed, do a pstripe reconstruction
  1537. * from the xors.
  1538. * If both the q stripe and the P stripe are failed, we're
  1539. * here due to a crc mismatch and we can't give them the
  1540. * data they want
  1541. */
  1542. if (rbio->raid_map[failb] == RAID6_Q_STRIPE) {
  1543. if (rbio->raid_map[faila] == RAID5_P_STRIPE) {
  1544. err = -EIO;
  1545. goto cleanup;
  1546. }
  1547. /*
  1548. * otherwise we have one bad data stripe and
  1549. * a good P stripe. raid5!
  1550. */
  1551. goto pstripe;
  1552. }
  1553. if (rbio->raid_map[failb] == RAID5_P_STRIPE) {
  1554. raid6_datap_recov(rbio->bbio->num_stripes,
  1555. PAGE_SIZE, faila, pointers);
  1556. } else {
  1557. raid6_2data_recov(rbio->bbio->num_stripes,
  1558. PAGE_SIZE, faila, failb,
  1559. pointers);
  1560. }
  1561. } else {
  1562. void *p;
  1563. /* rebuild from P stripe here (raid5 or raid6) */
  1564. BUG_ON(failb != -1);
  1565. pstripe:
  1566. /* Copy parity block into failed block to start with */
  1567. memcpy(pointers[faila],
  1568. pointers[rbio->nr_data],
  1569. PAGE_CACHE_SIZE);
  1570. /* rearrange the pointer array */
  1571. p = pointers[faila];
  1572. for (stripe = faila; stripe < rbio->nr_data - 1; stripe++)
  1573. pointers[stripe] = pointers[stripe + 1];
  1574. pointers[rbio->nr_data - 1] = p;
  1575. /* xor in the rest */
  1576. run_xor(pointers, rbio->nr_data - 1, PAGE_CACHE_SIZE);
  1577. }
  1578. /* if we're doing this rebuild as part of an rmw, go through
  1579. * and set all of our private rbio pages in the
  1580. * failed stripes as uptodate. This way finish_rmw will
  1581. * know they can be trusted. If this was a read reconstruction,
  1582. * other endio functions will fiddle the uptodate bits
  1583. */
  1584. if (!rbio->read_rebuild) {
  1585. for (i = 0; i < nr_pages; i++) {
  1586. if (faila != -1) {
  1587. page = rbio_stripe_page(rbio, faila, i);
  1588. SetPageUptodate(page);
  1589. }
  1590. if (failb != -1) {
  1591. page = rbio_stripe_page(rbio, failb, i);
  1592. SetPageUptodate(page);
  1593. }
  1594. }
  1595. }
  1596. for (stripe = 0; stripe < rbio->bbio->num_stripes; stripe++) {
  1597. /*
  1598. * if we're rebuilding a read, we have to use
  1599. * pages from the bio list
  1600. */
  1601. if (rbio->read_rebuild &&
  1602. (stripe == faila || stripe == failb)) {
  1603. page = page_in_rbio(rbio, stripe, pagenr, 0);
  1604. } else {
  1605. page = rbio_stripe_page(rbio, stripe, pagenr);
  1606. }
  1607. kunmap(page);
  1608. }
  1609. }
  1610. err = 0;
  1611. cleanup:
  1612. kfree(pointers);
  1613. cleanup_io:
  1614. if (rbio->read_rebuild) {
  1615. if (err == 0)
  1616. cache_rbio_pages(rbio);
  1617. else
  1618. clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
  1619. rbio_orig_end_io(rbio, err, err == 0);
  1620. } else if (err == 0) {
  1621. rbio->faila = -1;
  1622. rbio->failb = -1;
  1623. finish_rmw(rbio);
  1624. } else {
  1625. rbio_orig_end_io(rbio, err, 0);
  1626. }
  1627. }
  1628. /*
  1629. * This is called only for stripes we've read from disk to
  1630. * reconstruct the parity.
  1631. */
  1632. static void raid_recover_end_io(struct bio *bio, int err)
  1633. {
  1634. struct btrfs_raid_bio *rbio = bio->bi_private;
  1635. /*
  1636. * we only read stripe pages off the disk, set them
  1637. * up to date if there were no errors
  1638. */
  1639. if (err)
  1640. fail_bio_stripe(rbio, bio);
  1641. else
  1642. set_bio_pages_uptodate(bio);
  1643. bio_put(bio);
  1644. if (!atomic_dec_and_test(&rbio->bbio->stripes_pending))
  1645. return;
  1646. if (atomic_read(&rbio->bbio->error) > rbio->bbio->max_errors)
  1647. rbio_orig_end_io(rbio, -EIO, 0);
  1648. else
  1649. __raid_recover_end_io(rbio);
  1650. }
  1651. /*
  1652. * reads everything we need off the disk to reconstruct
  1653. * the parity. endio handlers trigger final reconstruction
  1654. * when the IO is done.
  1655. *
  1656. * This is used both for reads from the higher layers and for
  1657. * parity construction required to finish a rmw cycle.
  1658. */
  1659. static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
  1660. {
  1661. int bios_to_read = 0;
  1662. struct btrfs_bio *bbio = rbio->bbio;
  1663. struct bio_list bio_list;
  1664. int ret;
  1665. int nr_pages = (rbio->stripe_len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1666. int pagenr;
  1667. int stripe;
  1668. struct bio *bio;
  1669. bio_list_init(&bio_list);
  1670. ret = alloc_rbio_pages(rbio);
  1671. if (ret)
  1672. goto cleanup;
  1673. atomic_set(&rbio->bbio->error, 0);
  1674. /*
  1675. * read everything that hasn't failed. Thanks to the
  1676. * stripe cache, it is possible that some or all of these
  1677. * pages are going to be uptodate.
  1678. */
  1679. for (stripe = 0; stripe < bbio->num_stripes; stripe++) {
  1680. if (rbio->faila == stripe ||
  1681. rbio->failb == stripe)
  1682. continue;
  1683. for (pagenr = 0; pagenr < nr_pages; pagenr++) {
  1684. struct page *p;
  1685. /*
  1686. * the rmw code may have already read this
  1687. * page in
  1688. */
  1689. p = rbio_stripe_page(rbio, stripe, pagenr);
  1690. if (PageUptodate(p))
  1691. continue;
  1692. ret = rbio_add_io_page(rbio, &bio_list,
  1693. rbio_stripe_page(rbio, stripe, pagenr),
  1694. stripe, pagenr, rbio->stripe_len);
  1695. if (ret < 0)
  1696. goto cleanup;
  1697. }
  1698. }
  1699. bios_to_read = bio_list_size(&bio_list);
  1700. if (!bios_to_read) {
  1701. /*
  1702. * we might have no bios to read just because the pages
  1703. * were up to date, or we might have no bios to read because
  1704. * the devices were gone.
  1705. */
  1706. if (atomic_read(&rbio->bbio->error) <= rbio->bbio->max_errors) {
  1707. __raid_recover_end_io(rbio);
  1708. goto out;
  1709. } else {
  1710. goto cleanup;
  1711. }
  1712. }
  1713. /*
  1714. * the bbio may be freed once we submit the last bio. Make sure
  1715. * not to touch it after that
  1716. */
  1717. atomic_set(&bbio->stripes_pending, bios_to_read);
  1718. while (1) {
  1719. bio = bio_list_pop(&bio_list);
  1720. if (!bio)
  1721. break;
  1722. bio->bi_private = rbio;
  1723. bio->bi_end_io = raid_recover_end_io;
  1724. btrfs_bio_wq_end_io(rbio->fs_info, bio,
  1725. BTRFS_WQ_ENDIO_RAID56);
  1726. BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
  1727. submit_bio(READ, bio);
  1728. }
  1729. out:
  1730. return 0;
  1731. cleanup:
  1732. if (rbio->read_rebuild)
  1733. rbio_orig_end_io(rbio, -EIO, 0);
  1734. return -EIO;
  1735. }
  1736. /*
  1737. * the main entry point for reads from the higher layers. This
  1738. * is really only called when the normal read path had a failure,
  1739. * so we assume the bio they send down corresponds to a failed part
  1740. * of the drive.
  1741. */
  1742. int raid56_parity_recover(struct btrfs_root *root, struct bio *bio,
  1743. struct btrfs_bio *bbio, u64 *raid_map,
  1744. u64 stripe_len, int mirror_num)
  1745. {
  1746. struct btrfs_raid_bio *rbio;
  1747. int ret;
  1748. rbio = alloc_rbio(root, bbio, raid_map, stripe_len);
  1749. if (IS_ERR(rbio)) {
  1750. return PTR_ERR(rbio);
  1751. }
  1752. rbio->read_rebuild = 1;
  1753. bio_list_add(&rbio->bio_list, bio);
  1754. rbio->bio_list_bytes = bio->bi_size;
  1755. rbio->faila = find_logical_bio_stripe(rbio, bio);
  1756. if (rbio->faila == -1) {
  1757. BUG();
  1758. kfree(rbio);
  1759. return -EIO;
  1760. }
  1761. /*
  1762. * reconstruct from the q stripe if they are
  1763. * asking for mirror 3
  1764. */
  1765. if (mirror_num == 3)
  1766. rbio->failb = bbio->num_stripes - 2;
  1767. ret = lock_stripe_add(rbio);
  1768. /*
  1769. * __raid56_parity_recover will end the bio with
  1770. * any errors it hits. We don't want to return
  1771. * its error value up the stack because our caller
  1772. * will end up calling bio_endio with any nonzero
  1773. * return
  1774. */
  1775. if (ret == 0)
  1776. __raid56_parity_recover(rbio);
  1777. /*
  1778. * our rbio has been added to the list of
  1779. * rbios that will be handled after the
  1780. * currently lock owner is done
  1781. */
  1782. return 0;
  1783. }
  1784. static void rmw_work(struct btrfs_work *work)
  1785. {
  1786. struct btrfs_raid_bio *rbio;
  1787. rbio = container_of(work, struct btrfs_raid_bio, work);
  1788. raid56_rmw_stripe(rbio);
  1789. }
  1790. static void read_rebuild_work(struct btrfs_work *work)
  1791. {
  1792. struct btrfs_raid_bio *rbio;
  1793. rbio = container_of(work, struct btrfs_raid_bio, work);
  1794. __raid56_parity_recover(rbio);
  1795. }