raid5.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051
  1. /*
  2. * raid5.c : Multiple Devices driver for Linux
  3. * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  4. * Copyright (C) 1999, 2000 Ingo Molnar
  5. * Copyright (C) 2002, 2003 H. Peter Anvin
  6. *
  7. * RAID-4/5/6 management functions.
  8. * Thanks to Penguin Computing for making the RAID-6 development possible
  9. * by donating a test server!
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. * BITMAP UNPLUGGING:
  22. *
  23. * The sequencing for updating the bitmap reliably is a little
  24. * subtle (and I got it wrong the first time) so it deserves some
  25. * explanation.
  26. *
  27. * We group bitmap updates into batches. Each batch has a number.
  28. * We may write out several batches at once, but that isn't very important.
  29. * conf->bm_write is the number of the last batch successfully written.
  30. * conf->bm_flush is the number of the last batch that was closed to
  31. * new additions.
  32. * When we discover that we will need to write to any block in a stripe
  33. * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
  34. * the number of the batch it will be in. This is bm_flush+1.
  35. * When we are ready to do a write, if that batch hasn't been written yet,
  36. * we plug the array and queue the stripe for later.
  37. * When an unplug happens, we increment bm_flush, thus closing the current
  38. * batch.
  39. * When we notice that bm_flush > bm_write, we write out all pending updates
  40. * to the bitmap, and advance bm_write to where bm_flush was.
  41. * This may occasionally write a bit out twice, but is sure never to
  42. * miss any bits.
  43. */
  44. #include <linux/module.h>
  45. #include <linux/slab.h>
  46. #include <linux/highmem.h>
  47. #include <linux/bitops.h>
  48. #include <linux/kthread.h>
  49. #include <asm/atomic.h>
  50. #include "raid6.h"
  51. #include <linux/raid/bitmap.h>
  52. /*
  53. * Stripe cache
  54. */
  55. #define NR_STRIPES 256
  56. #define STRIPE_SIZE PAGE_SIZE
  57. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  58. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  59. #define IO_THRESHOLD 1
  60. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  61. #define HASH_MASK (NR_HASH - 1)
  62. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  63. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  64. * order without overlap. There may be several bio's per stripe+device, and
  65. * a bio could span several devices.
  66. * When walking this list for a particular stripe+device, we must never proceed
  67. * beyond a bio that extends past this device, as the next bio might no longer
  68. * be valid.
  69. * This macro is used to determine the 'next' bio in the list, given the sector
  70. * of the current stripe+device
  71. */
  72. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  73. /*
  74. * The following can be used to debug the driver
  75. */
  76. #define RAID5_DEBUG 0
  77. #define RAID5_PARANOIA 1
  78. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  79. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  80. #else
  81. # define CHECK_DEVLOCK()
  82. #endif
  83. #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
  84. #if RAID5_DEBUG
  85. #define inline
  86. #define __inline__
  87. #endif
  88. #if !RAID6_USE_EMPTY_ZERO_PAGE
  89. /* In .bss so it's zeroed */
  90. const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
  91. #endif
  92. static inline int raid6_next_disk(int disk, int raid_disks)
  93. {
  94. disk++;
  95. return (disk < raid_disks) ? disk : 0;
  96. }
  97. static void print_raid5_conf (raid5_conf_t *conf);
  98. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  99. {
  100. if (atomic_dec_and_test(&sh->count)) {
  101. BUG_ON(!list_empty(&sh->lru));
  102. BUG_ON(atomic_read(&conf->active_stripes)==0);
  103. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  104. if (test_bit(STRIPE_DELAYED, &sh->state)) {
  105. list_add_tail(&sh->lru, &conf->delayed_list);
  106. blk_plug_device(conf->mddev->queue);
  107. } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  108. sh->bm_seq - conf->seq_write > 0) {
  109. list_add_tail(&sh->lru, &conf->bitmap_list);
  110. blk_plug_device(conf->mddev->queue);
  111. } else {
  112. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  113. list_add_tail(&sh->lru, &conf->handle_list);
  114. }
  115. md_wakeup_thread(conf->mddev->thread);
  116. } else {
  117. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  118. atomic_dec(&conf->preread_active_stripes);
  119. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  120. md_wakeup_thread(conf->mddev->thread);
  121. }
  122. atomic_dec(&conf->active_stripes);
  123. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  124. list_add_tail(&sh->lru, &conf->inactive_list);
  125. wake_up(&conf->wait_for_stripe);
  126. if (conf->retry_read_aligned)
  127. md_wakeup_thread(conf->mddev->thread);
  128. }
  129. }
  130. }
  131. }
  132. static void release_stripe(struct stripe_head *sh)
  133. {
  134. raid5_conf_t *conf = sh->raid_conf;
  135. unsigned long flags;
  136. spin_lock_irqsave(&conf->device_lock, flags);
  137. __release_stripe(conf, sh);
  138. spin_unlock_irqrestore(&conf->device_lock, flags);
  139. }
  140. static inline void remove_hash(struct stripe_head *sh)
  141. {
  142. PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  143. hlist_del_init(&sh->hash);
  144. }
  145. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  146. {
  147. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  148. PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  149. CHECK_DEVLOCK();
  150. hlist_add_head(&sh->hash, hp);
  151. }
  152. /* find an idle stripe, make sure it is unhashed, and return it. */
  153. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  154. {
  155. struct stripe_head *sh = NULL;
  156. struct list_head *first;
  157. CHECK_DEVLOCK();
  158. if (list_empty(&conf->inactive_list))
  159. goto out;
  160. first = conf->inactive_list.next;
  161. sh = list_entry(first, struct stripe_head, lru);
  162. list_del_init(first);
  163. remove_hash(sh);
  164. atomic_inc(&conf->active_stripes);
  165. out:
  166. return sh;
  167. }
  168. static void shrink_buffers(struct stripe_head *sh, int num)
  169. {
  170. struct page *p;
  171. int i;
  172. for (i=0; i<num ; i++) {
  173. p = sh->dev[i].page;
  174. if (!p)
  175. continue;
  176. sh->dev[i].page = NULL;
  177. put_page(p);
  178. }
  179. }
  180. static int grow_buffers(struct stripe_head *sh, int num)
  181. {
  182. int i;
  183. for (i=0; i<num; i++) {
  184. struct page *page;
  185. if (!(page = alloc_page(GFP_KERNEL))) {
  186. return 1;
  187. }
  188. sh->dev[i].page = page;
  189. }
  190. return 0;
  191. }
  192. static void raid5_build_block (struct stripe_head *sh, int i);
  193. static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
  194. {
  195. raid5_conf_t *conf = sh->raid_conf;
  196. int i;
  197. BUG_ON(atomic_read(&sh->count) != 0);
  198. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  199. CHECK_DEVLOCK();
  200. PRINTK("init_stripe called, stripe %llu\n",
  201. (unsigned long long)sh->sector);
  202. remove_hash(sh);
  203. sh->sector = sector;
  204. sh->pd_idx = pd_idx;
  205. sh->state = 0;
  206. sh->disks = disks;
  207. for (i = sh->disks; i--; ) {
  208. struct r5dev *dev = &sh->dev[i];
  209. if (dev->toread || dev->towrite || dev->written ||
  210. test_bit(R5_LOCKED, &dev->flags)) {
  211. printk("sector=%llx i=%d %p %p %p %d\n",
  212. (unsigned long long)sh->sector, i, dev->toread,
  213. dev->towrite, dev->written,
  214. test_bit(R5_LOCKED, &dev->flags));
  215. BUG();
  216. }
  217. dev->flags = 0;
  218. raid5_build_block(sh, i);
  219. }
  220. insert_hash(conf, sh);
  221. }
  222. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
  223. {
  224. struct stripe_head *sh;
  225. struct hlist_node *hn;
  226. CHECK_DEVLOCK();
  227. PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
  228. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  229. if (sh->sector == sector && sh->disks == disks)
  230. return sh;
  231. PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
  232. return NULL;
  233. }
  234. static void unplug_slaves(mddev_t *mddev);
  235. static void raid5_unplug_device(request_queue_t *q);
  236. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
  237. int pd_idx, int noblock)
  238. {
  239. struct stripe_head *sh;
  240. PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
  241. spin_lock_irq(&conf->device_lock);
  242. do {
  243. wait_event_lock_irq(conf->wait_for_stripe,
  244. conf->quiesce == 0,
  245. conf->device_lock, /* nothing */);
  246. sh = __find_stripe(conf, sector, disks);
  247. if (!sh) {
  248. if (!conf->inactive_blocked)
  249. sh = get_free_stripe(conf);
  250. if (noblock && sh == NULL)
  251. break;
  252. if (!sh) {
  253. conf->inactive_blocked = 1;
  254. wait_event_lock_irq(conf->wait_for_stripe,
  255. !list_empty(&conf->inactive_list) &&
  256. (atomic_read(&conf->active_stripes)
  257. < (conf->max_nr_stripes *3/4)
  258. || !conf->inactive_blocked),
  259. conf->device_lock,
  260. raid5_unplug_device(conf->mddev->queue)
  261. );
  262. conf->inactive_blocked = 0;
  263. } else
  264. init_stripe(sh, sector, pd_idx, disks);
  265. } else {
  266. if (atomic_read(&sh->count)) {
  267. BUG_ON(!list_empty(&sh->lru));
  268. } else {
  269. if (!test_bit(STRIPE_HANDLE, &sh->state))
  270. atomic_inc(&conf->active_stripes);
  271. if (list_empty(&sh->lru) &&
  272. !test_bit(STRIPE_EXPANDING, &sh->state))
  273. BUG();
  274. list_del_init(&sh->lru);
  275. }
  276. }
  277. } while (sh == NULL);
  278. if (sh)
  279. atomic_inc(&sh->count);
  280. spin_unlock_irq(&conf->device_lock);
  281. return sh;
  282. }
  283. static int grow_one_stripe(raid5_conf_t *conf)
  284. {
  285. struct stripe_head *sh;
  286. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  287. if (!sh)
  288. return 0;
  289. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  290. sh->raid_conf = conf;
  291. spin_lock_init(&sh->lock);
  292. if (grow_buffers(sh, conf->raid_disks)) {
  293. shrink_buffers(sh, conf->raid_disks);
  294. kmem_cache_free(conf->slab_cache, sh);
  295. return 0;
  296. }
  297. sh->disks = conf->raid_disks;
  298. /* we just created an active stripe so... */
  299. atomic_set(&sh->count, 1);
  300. atomic_inc(&conf->active_stripes);
  301. INIT_LIST_HEAD(&sh->lru);
  302. release_stripe(sh);
  303. return 1;
  304. }
  305. static int grow_stripes(raid5_conf_t *conf, int num)
  306. {
  307. struct kmem_cache *sc;
  308. int devs = conf->raid_disks;
  309. sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
  310. sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
  311. conf->active_name = 0;
  312. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  313. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  314. 0, 0, NULL, NULL);
  315. if (!sc)
  316. return 1;
  317. conf->slab_cache = sc;
  318. conf->pool_size = devs;
  319. while (num--)
  320. if (!grow_one_stripe(conf))
  321. return 1;
  322. return 0;
  323. }
  324. #ifdef CONFIG_MD_RAID5_RESHAPE
  325. static int resize_stripes(raid5_conf_t *conf, int newsize)
  326. {
  327. /* Make all the stripes able to hold 'newsize' devices.
  328. * New slots in each stripe get 'page' set to a new page.
  329. *
  330. * This happens in stages:
  331. * 1/ create a new kmem_cache and allocate the required number of
  332. * stripe_heads.
  333. * 2/ gather all the old stripe_heads and tranfer the pages across
  334. * to the new stripe_heads. This will have the side effect of
  335. * freezing the array as once all stripe_heads have been collected,
  336. * no IO will be possible. Old stripe heads are freed once their
  337. * pages have been transferred over, and the old kmem_cache is
  338. * freed when all stripes are done.
  339. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  340. * we simple return a failre status - no need to clean anything up.
  341. * 4/ allocate new pages for the new slots in the new stripe_heads.
  342. * If this fails, we don't bother trying the shrink the
  343. * stripe_heads down again, we just leave them as they are.
  344. * As each stripe_head is processed the new one is released into
  345. * active service.
  346. *
  347. * Once step2 is started, we cannot afford to wait for a write,
  348. * so we use GFP_NOIO allocations.
  349. */
  350. struct stripe_head *osh, *nsh;
  351. LIST_HEAD(newstripes);
  352. struct disk_info *ndisks;
  353. int err = 0;
  354. struct kmem_cache *sc;
  355. int i;
  356. if (newsize <= conf->pool_size)
  357. return 0; /* never bother to shrink */
  358. md_allow_write(conf->mddev);
  359. /* Step 1 */
  360. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  361. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  362. 0, 0, NULL, NULL);
  363. if (!sc)
  364. return -ENOMEM;
  365. for (i = conf->max_nr_stripes; i; i--) {
  366. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  367. if (!nsh)
  368. break;
  369. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  370. nsh->raid_conf = conf;
  371. spin_lock_init(&nsh->lock);
  372. list_add(&nsh->lru, &newstripes);
  373. }
  374. if (i) {
  375. /* didn't get enough, give up */
  376. while (!list_empty(&newstripes)) {
  377. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  378. list_del(&nsh->lru);
  379. kmem_cache_free(sc, nsh);
  380. }
  381. kmem_cache_destroy(sc);
  382. return -ENOMEM;
  383. }
  384. /* Step 2 - Must use GFP_NOIO now.
  385. * OK, we have enough stripes, start collecting inactive
  386. * stripes and copying them over
  387. */
  388. list_for_each_entry(nsh, &newstripes, lru) {
  389. spin_lock_irq(&conf->device_lock);
  390. wait_event_lock_irq(conf->wait_for_stripe,
  391. !list_empty(&conf->inactive_list),
  392. conf->device_lock,
  393. unplug_slaves(conf->mddev)
  394. );
  395. osh = get_free_stripe(conf);
  396. spin_unlock_irq(&conf->device_lock);
  397. atomic_set(&nsh->count, 1);
  398. for(i=0; i<conf->pool_size; i++)
  399. nsh->dev[i].page = osh->dev[i].page;
  400. for( ; i<newsize; i++)
  401. nsh->dev[i].page = NULL;
  402. kmem_cache_free(conf->slab_cache, osh);
  403. }
  404. kmem_cache_destroy(conf->slab_cache);
  405. /* Step 3.
  406. * At this point, we are holding all the stripes so the array
  407. * is completely stalled, so now is a good time to resize
  408. * conf->disks.
  409. */
  410. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  411. if (ndisks) {
  412. for (i=0; i<conf->raid_disks; i++)
  413. ndisks[i] = conf->disks[i];
  414. kfree(conf->disks);
  415. conf->disks = ndisks;
  416. } else
  417. err = -ENOMEM;
  418. /* Step 4, return new stripes to service */
  419. while(!list_empty(&newstripes)) {
  420. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  421. list_del_init(&nsh->lru);
  422. for (i=conf->raid_disks; i < newsize; i++)
  423. if (nsh->dev[i].page == NULL) {
  424. struct page *p = alloc_page(GFP_NOIO);
  425. nsh->dev[i].page = p;
  426. if (!p)
  427. err = -ENOMEM;
  428. }
  429. release_stripe(nsh);
  430. }
  431. /* critical section pass, GFP_NOIO no longer needed */
  432. conf->slab_cache = sc;
  433. conf->active_name = 1-conf->active_name;
  434. conf->pool_size = newsize;
  435. return err;
  436. }
  437. #endif
  438. static int drop_one_stripe(raid5_conf_t *conf)
  439. {
  440. struct stripe_head *sh;
  441. spin_lock_irq(&conf->device_lock);
  442. sh = get_free_stripe(conf);
  443. spin_unlock_irq(&conf->device_lock);
  444. if (!sh)
  445. return 0;
  446. BUG_ON(atomic_read(&sh->count));
  447. shrink_buffers(sh, conf->pool_size);
  448. kmem_cache_free(conf->slab_cache, sh);
  449. atomic_dec(&conf->active_stripes);
  450. return 1;
  451. }
  452. static void shrink_stripes(raid5_conf_t *conf)
  453. {
  454. while (drop_one_stripe(conf))
  455. ;
  456. if (conf->slab_cache)
  457. kmem_cache_destroy(conf->slab_cache);
  458. conf->slab_cache = NULL;
  459. }
  460. static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
  461. int error)
  462. {
  463. struct stripe_head *sh = bi->bi_private;
  464. raid5_conf_t *conf = sh->raid_conf;
  465. int disks = sh->disks, i;
  466. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  467. char b[BDEVNAME_SIZE];
  468. mdk_rdev_t *rdev;
  469. if (bi->bi_size)
  470. return 1;
  471. for (i=0 ; i<disks; i++)
  472. if (bi == &sh->dev[i].req)
  473. break;
  474. PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  475. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  476. uptodate);
  477. if (i == disks) {
  478. BUG();
  479. return 0;
  480. }
  481. if (uptodate) {
  482. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  483. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  484. rdev = conf->disks[i].rdev;
  485. printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
  486. mdname(conf->mddev), STRIPE_SECTORS,
  487. (unsigned long long)sh->sector + rdev->data_offset,
  488. bdevname(rdev->bdev, b));
  489. clear_bit(R5_ReadError, &sh->dev[i].flags);
  490. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  491. }
  492. if (atomic_read(&conf->disks[i].rdev->read_errors))
  493. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  494. } else {
  495. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  496. int retry = 0;
  497. rdev = conf->disks[i].rdev;
  498. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  499. atomic_inc(&rdev->read_errors);
  500. if (conf->mddev->degraded)
  501. printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
  502. mdname(conf->mddev),
  503. (unsigned long long)sh->sector + rdev->data_offset,
  504. bdn);
  505. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  506. /* Oh, no!!! */
  507. printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
  508. mdname(conf->mddev),
  509. (unsigned long long)sh->sector + rdev->data_offset,
  510. bdn);
  511. else if (atomic_read(&rdev->read_errors)
  512. > conf->max_nr_stripes)
  513. printk(KERN_WARNING
  514. "raid5:%s: Too many read errors, failing device %s.\n",
  515. mdname(conf->mddev), bdn);
  516. else
  517. retry = 1;
  518. if (retry)
  519. set_bit(R5_ReadError, &sh->dev[i].flags);
  520. else {
  521. clear_bit(R5_ReadError, &sh->dev[i].flags);
  522. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  523. md_error(conf->mddev, rdev);
  524. }
  525. }
  526. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  527. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  528. set_bit(STRIPE_HANDLE, &sh->state);
  529. release_stripe(sh);
  530. return 0;
  531. }
  532. static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
  533. int error)
  534. {
  535. struct stripe_head *sh = bi->bi_private;
  536. raid5_conf_t *conf = sh->raid_conf;
  537. int disks = sh->disks, i;
  538. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  539. if (bi->bi_size)
  540. return 1;
  541. for (i=0 ; i<disks; i++)
  542. if (bi == &sh->dev[i].req)
  543. break;
  544. PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  545. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  546. uptodate);
  547. if (i == disks) {
  548. BUG();
  549. return 0;
  550. }
  551. if (!uptodate)
  552. md_error(conf->mddev, conf->disks[i].rdev);
  553. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  554. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  555. set_bit(STRIPE_HANDLE, &sh->state);
  556. release_stripe(sh);
  557. return 0;
  558. }
  559. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  560. static void raid5_build_block (struct stripe_head *sh, int i)
  561. {
  562. struct r5dev *dev = &sh->dev[i];
  563. bio_init(&dev->req);
  564. dev->req.bi_io_vec = &dev->vec;
  565. dev->req.bi_vcnt++;
  566. dev->req.bi_max_vecs++;
  567. dev->vec.bv_page = dev->page;
  568. dev->vec.bv_len = STRIPE_SIZE;
  569. dev->vec.bv_offset = 0;
  570. dev->req.bi_sector = sh->sector;
  571. dev->req.bi_private = sh;
  572. dev->flags = 0;
  573. dev->sector = compute_blocknr(sh, i);
  574. }
  575. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  576. {
  577. char b[BDEVNAME_SIZE];
  578. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  579. PRINTK("raid5: error called\n");
  580. if (!test_bit(Faulty, &rdev->flags)) {
  581. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  582. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  583. unsigned long flags;
  584. spin_lock_irqsave(&conf->device_lock, flags);
  585. mddev->degraded++;
  586. spin_unlock_irqrestore(&conf->device_lock, flags);
  587. /*
  588. * if recovery was running, make sure it aborts.
  589. */
  590. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  591. }
  592. set_bit(Faulty, &rdev->flags);
  593. printk (KERN_ALERT
  594. "raid5: Disk failure on %s, disabling device."
  595. " Operation continuing on %d devices\n",
  596. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  597. }
  598. }
  599. /*
  600. * Input: a 'big' sector number,
  601. * Output: index of the data and parity disk, and the sector # in them.
  602. */
  603. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  604. unsigned int data_disks, unsigned int * dd_idx,
  605. unsigned int * pd_idx, raid5_conf_t *conf)
  606. {
  607. long stripe;
  608. unsigned long chunk_number;
  609. unsigned int chunk_offset;
  610. sector_t new_sector;
  611. int sectors_per_chunk = conf->chunk_size >> 9;
  612. /* First compute the information on this sector */
  613. /*
  614. * Compute the chunk number and the sector offset inside the chunk
  615. */
  616. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  617. chunk_number = r_sector;
  618. BUG_ON(r_sector != chunk_number);
  619. /*
  620. * Compute the stripe number
  621. */
  622. stripe = chunk_number / data_disks;
  623. /*
  624. * Compute the data disk and parity disk indexes inside the stripe
  625. */
  626. *dd_idx = chunk_number % data_disks;
  627. /*
  628. * Select the parity disk based on the user selected algorithm.
  629. */
  630. switch(conf->level) {
  631. case 4:
  632. *pd_idx = data_disks;
  633. break;
  634. case 5:
  635. switch (conf->algorithm) {
  636. case ALGORITHM_LEFT_ASYMMETRIC:
  637. *pd_idx = data_disks - stripe % raid_disks;
  638. if (*dd_idx >= *pd_idx)
  639. (*dd_idx)++;
  640. break;
  641. case ALGORITHM_RIGHT_ASYMMETRIC:
  642. *pd_idx = stripe % raid_disks;
  643. if (*dd_idx >= *pd_idx)
  644. (*dd_idx)++;
  645. break;
  646. case ALGORITHM_LEFT_SYMMETRIC:
  647. *pd_idx = data_disks - stripe % raid_disks;
  648. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  649. break;
  650. case ALGORITHM_RIGHT_SYMMETRIC:
  651. *pd_idx = stripe % raid_disks;
  652. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  653. break;
  654. default:
  655. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  656. conf->algorithm);
  657. }
  658. break;
  659. case 6:
  660. /**** FIX THIS ****/
  661. switch (conf->algorithm) {
  662. case ALGORITHM_LEFT_ASYMMETRIC:
  663. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  664. if (*pd_idx == raid_disks-1)
  665. (*dd_idx)++; /* Q D D D P */
  666. else if (*dd_idx >= *pd_idx)
  667. (*dd_idx) += 2; /* D D P Q D */
  668. break;
  669. case ALGORITHM_RIGHT_ASYMMETRIC:
  670. *pd_idx = stripe % raid_disks;
  671. if (*pd_idx == raid_disks-1)
  672. (*dd_idx)++; /* Q D D D P */
  673. else if (*dd_idx >= *pd_idx)
  674. (*dd_idx) += 2; /* D D P Q D */
  675. break;
  676. case ALGORITHM_LEFT_SYMMETRIC:
  677. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  678. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  679. break;
  680. case ALGORITHM_RIGHT_SYMMETRIC:
  681. *pd_idx = stripe % raid_disks;
  682. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  683. break;
  684. default:
  685. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  686. conf->algorithm);
  687. }
  688. break;
  689. }
  690. /*
  691. * Finally, compute the new sector number
  692. */
  693. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  694. return new_sector;
  695. }
  696. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  697. {
  698. raid5_conf_t *conf = sh->raid_conf;
  699. int raid_disks = sh->disks;
  700. int data_disks = raid_disks - conf->max_degraded;
  701. sector_t new_sector = sh->sector, check;
  702. int sectors_per_chunk = conf->chunk_size >> 9;
  703. sector_t stripe;
  704. int chunk_offset;
  705. int chunk_number, dummy1, dummy2, dd_idx = i;
  706. sector_t r_sector;
  707. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  708. stripe = new_sector;
  709. BUG_ON(new_sector != stripe);
  710. if (i == sh->pd_idx)
  711. return 0;
  712. switch(conf->level) {
  713. case 4: break;
  714. case 5:
  715. switch (conf->algorithm) {
  716. case ALGORITHM_LEFT_ASYMMETRIC:
  717. case ALGORITHM_RIGHT_ASYMMETRIC:
  718. if (i > sh->pd_idx)
  719. i--;
  720. break;
  721. case ALGORITHM_LEFT_SYMMETRIC:
  722. case ALGORITHM_RIGHT_SYMMETRIC:
  723. if (i < sh->pd_idx)
  724. i += raid_disks;
  725. i -= (sh->pd_idx + 1);
  726. break;
  727. default:
  728. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  729. conf->algorithm);
  730. }
  731. break;
  732. case 6:
  733. if (i == raid6_next_disk(sh->pd_idx, raid_disks))
  734. return 0; /* It is the Q disk */
  735. switch (conf->algorithm) {
  736. case ALGORITHM_LEFT_ASYMMETRIC:
  737. case ALGORITHM_RIGHT_ASYMMETRIC:
  738. if (sh->pd_idx == raid_disks-1)
  739. i--; /* Q D D D P */
  740. else if (i > sh->pd_idx)
  741. i -= 2; /* D D P Q D */
  742. break;
  743. case ALGORITHM_LEFT_SYMMETRIC:
  744. case ALGORITHM_RIGHT_SYMMETRIC:
  745. if (sh->pd_idx == raid_disks-1)
  746. i--; /* Q D D D P */
  747. else {
  748. /* D D P Q D */
  749. if (i < sh->pd_idx)
  750. i += raid_disks;
  751. i -= (sh->pd_idx + 2);
  752. }
  753. break;
  754. default:
  755. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  756. conf->algorithm);
  757. }
  758. break;
  759. }
  760. chunk_number = stripe * data_disks + i;
  761. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  762. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  763. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  764. printk(KERN_ERR "compute_blocknr: map not correct\n");
  765. return 0;
  766. }
  767. return r_sector;
  768. }
  769. /*
  770. * Copy data between a page in the stripe cache, and one or more bion
  771. * The page could align with the middle of the bio, or there could be
  772. * several bion, each with several bio_vecs, which cover part of the page
  773. * Multiple bion are linked together on bi_next. There may be extras
  774. * at the end of this list. We ignore them.
  775. */
  776. static void copy_data(int frombio, struct bio *bio,
  777. struct page *page,
  778. sector_t sector)
  779. {
  780. char *pa = page_address(page);
  781. struct bio_vec *bvl;
  782. int i;
  783. int page_offset;
  784. if (bio->bi_sector >= sector)
  785. page_offset = (signed)(bio->bi_sector - sector) * 512;
  786. else
  787. page_offset = (signed)(sector - bio->bi_sector) * -512;
  788. bio_for_each_segment(bvl, bio, i) {
  789. int len = bio_iovec_idx(bio,i)->bv_len;
  790. int clen;
  791. int b_offset = 0;
  792. if (page_offset < 0) {
  793. b_offset = -page_offset;
  794. page_offset += b_offset;
  795. len -= b_offset;
  796. }
  797. if (len > 0 && page_offset + len > STRIPE_SIZE)
  798. clen = STRIPE_SIZE - page_offset;
  799. else clen = len;
  800. if (clen > 0) {
  801. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  802. if (frombio)
  803. memcpy(pa+page_offset, ba+b_offset, clen);
  804. else
  805. memcpy(ba+b_offset, pa+page_offset, clen);
  806. __bio_kunmap_atomic(ba, KM_USER0);
  807. }
  808. if (clen < len) /* hit end of page */
  809. break;
  810. page_offset += len;
  811. }
  812. }
  813. #define check_xor() do { \
  814. if (count == MAX_XOR_BLOCKS) { \
  815. xor_block(count, STRIPE_SIZE, ptr); \
  816. count = 1; \
  817. } \
  818. } while(0)
  819. static void compute_block(struct stripe_head *sh, int dd_idx)
  820. {
  821. int i, count, disks = sh->disks;
  822. void *ptr[MAX_XOR_BLOCKS], *p;
  823. PRINTK("compute_block, stripe %llu, idx %d\n",
  824. (unsigned long long)sh->sector, dd_idx);
  825. ptr[0] = page_address(sh->dev[dd_idx].page);
  826. memset(ptr[0], 0, STRIPE_SIZE);
  827. count = 1;
  828. for (i = disks ; i--; ) {
  829. if (i == dd_idx)
  830. continue;
  831. p = page_address(sh->dev[i].page);
  832. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  833. ptr[count++] = p;
  834. else
  835. printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
  836. " not present\n", dd_idx,
  837. (unsigned long long)sh->sector, i);
  838. check_xor();
  839. }
  840. if (count != 1)
  841. xor_block(count, STRIPE_SIZE, ptr);
  842. set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  843. }
  844. static void compute_parity5(struct stripe_head *sh, int method)
  845. {
  846. raid5_conf_t *conf = sh->raid_conf;
  847. int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
  848. void *ptr[MAX_XOR_BLOCKS];
  849. struct bio *chosen;
  850. PRINTK("compute_parity5, stripe %llu, method %d\n",
  851. (unsigned long long)sh->sector, method);
  852. count = 1;
  853. ptr[0] = page_address(sh->dev[pd_idx].page);
  854. switch(method) {
  855. case READ_MODIFY_WRITE:
  856. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
  857. for (i=disks ; i-- ;) {
  858. if (i==pd_idx)
  859. continue;
  860. if (sh->dev[i].towrite &&
  861. test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  862. ptr[count++] = page_address(sh->dev[i].page);
  863. chosen = sh->dev[i].towrite;
  864. sh->dev[i].towrite = NULL;
  865. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  866. wake_up(&conf->wait_for_overlap);
  867. BUG_ON(sh->dev[i].written);
  868. sh->dev[i].written = chosen;
  869. check_xor();
  870. }
  871. }
  872. break;
  873. case RECONSTRUCT_WRITE:
  874. memset(ptr[0], 0, STRIPE_SIZE);
  875. for (i= disks; i-- ;)
  876. if (i!=pd_idx && sh->dev[i].towrite) {
  877. chosen = sh->dev[i].towrite;
  878. sh->dev[i].towrite = NULL;
  879. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  880. wake_up(&conf->wait_for_overlap);
  881. BUG_ON(sh->dev[i].written);
  882. sh->dev[i].written = chosen;
  883. }
  884. break;
  885. case CHECK_PARITY:
  886. break;
  887. }
  888. if (count>1) {
  889. xor_block(count, STRIPE_SIZE, ptr);
  890. count = 1;
  891. }
  892. for (i = disks; i--;)
  893. if (sh->dev[i].written) {
  894. sector_t sector = sh->dev[i].sector;
  895. struct bio *wbi = sh->dev[i].written;
  896. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  897. copy_data(1, wbi, sh->dev[i].page, sector);
  898. wbi = r5_next_bio(wbi, sector);
  899. }
  900. set_bit(R5_LOCKED, &sh->dev[i].flags);
  901. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  902. }
  903. switch(method) {
  904. case RECONSTRUCT_WRITE:
  905. case CHECK_PARITY:
  906. for (i=disks; i--;)
  907. if (i != pd_idx) {
  908. ptr[count++] = page_address(sh->dev[i].page);
  909. check_xor();
  910. }
  911. break;
  912. case READ_MODIFY_WRITE:
  913. for (i = disks; i--;)
  914. if (sh->dev[i].written) {
  915. ptr[count++] = page_address(sh->dev[i].page);
  916. check_xor();
  917. }
  918. }
  919. if (count != 1)
  920. xor_block(count, STRIPE_SIZE, ptr);
  921. if (method != CHECK_PARITY) {
  922. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  923. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  924. } else
  925. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  926. }
  927. static void compute_parity6(struct stripe_head *sh, int method)
  928. {
  929. raid6_conf_t *conf = sh->raid_conf;
  930. int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
  931. struct bio *chosen;
  932. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  933. void *ptrs[disks];
  934. qd_idx = raid6_next_disk(pd_idx, disks);
  935. d0_idx = raid6_next_disk(qd_idx, disks);
  936. PRINTK("compute_parity, stripe %llu, method %d\n",
  937. (unsigned long long)sh->sector, method);
  938. switch(method) {
  939. case READ_MODIFY_WRITE:
  940. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  941. case RECONSTRUCT_WRITE:
  942. for (i= disks; i-- ;)
  943. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  944. chosen = sh->dev[i].towrite;
  945. sh->dev[i].towrite = NULL;
  946. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  947. wake_up(&conf->wait_for_overlap);
  948. BUG_ON(sh->dev[i].written);
  949. sh->dev[i].written = chosen;
  950. }
  951. break;
  952. case CHECK_PARITY:
  953. BUG(); /* Not implemented yet */
  954. }
  955. for (i = disks; i--;)
  956. if (sh->dev[i].written) {
  957. sector_t sector = sh->dev[i].sector;
  958. struct bio *wbi = sh->dev[i].written;
  959. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  960. copy_data(1, wbi, sh->dev[i].page, sector);
  961. wbi = r5_next_bio(wbi, sector);
  962. }
  963. set_bit(R5_LOCKED, &sh->dev[i].flags);
  964. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  965. }
  966. // switch(method) {
  967. // case RECONSTRUCT_WRITE:
  968. // case CHECK_PARITY:
  969. // case UPDATE_PARITY:
  970. /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
  971. /* FIX: Is this ordering of drives even remotely optimal? */
  972. count = 0;
  973. i = d0_idx;
  974. do {
  975. ptrs[count++] = page_address(sh->dev[i].page);
  976. if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  977. printk("block %d/%d not uptodate on parity calc\n", i,count);
  978. i = raid6_next_disk(i, disks);
  979. } while ( i != d0_idx );
  980. // break;
  981. // }
  982. raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
  983. switch(method) {
  984. case RECONSTRUCT_WRITE:
  985. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  986. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  987. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  988. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  989. break;
  990. case UPDATE_PARITY:
  991. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  992. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  993. break;
  994. }
  995. }
  996. /* Compute one missing block */
  997. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  998. {
  999. raid6_conf_t *conf = sh->raid_conf;
  1000. int i, count, disks = conf->raid_disks;
  1001. void *ptr[MAX_XOR_BLOCKS], *p;
  1002. int pd_idx = sh->pd_idx;
  1003. int qd_idx = raid6_next_disk(pd_idx, disks);
  1004. PRINTK("compute_block_1, stripe %llu, idx %d\n",
  1005. (unsigned long long)sh->sector, dd_idx);
  1006. if ( dd_idx == qd_idx ) {
  1007. /* We're actually computing the Q drive */
  1008. compute_parity6(sh, UPDATE_PARITY);
  1009. } else {
  1010. ptr[0] = page_address(sh->dev[dd_idx].page);
  1011. if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
  1012. count = 1;
  1013. for (i = disks ; i--; ) {
  1014. if (i == dd_idx || i == qd_idx)
  1015. continue;
  1016. p = page_address(sh->dev[i].page);
  1017. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1018. ptr[count++] = p;
  1019. else
  1020. printk("compute_block() %d, stripe %llu, %d"
  1021. " not present\n", dd_idx,
  1022. (unsigned long long)sh->sector, i);
  1023. check_xor();
  1024. }
  1025. if (count != 1)
  1026. xor_block(count, STRIPE_SIZE, ptr);
  1027. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1028. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1029. }
  1030. }
  1031. /* Compute two missing blocks */
  1032. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1033. {
  1034. raid6_conf_t *conf = sh->raid_conf;
  1035. int i, count, disks = conf->raid_disks;
  1036. int pd_idx = sh->pd_idx;
  1037. int qd_idx = raid6_next_disk(pd_idx, disks);
  1038. int d0_idx = raid6_next_disk(qd_idx, disks);
  1039. int faila, failb;
  1040. /* faila and failb are disk numbers relative to d0_idx */
  1041. /* pd_idx become disks-2 and qd_idx become disks-1 */
  1042. faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
  1043. failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
  1044. BUG_ON(faila == failb);
  1045. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1046. PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1047. (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
  1048. if ( failb == disks-1 ) {
  1049. /* Q disk is one of the missing disks */
  1050. if ( faila == disks-2 ) {
  1051. /* Missing P+Q, just recompute */
  1052. compute_parity6(sh, UPDATE_PARITY);
  1053. return;
  1054. } else {
  1055. /* We're missing D+Q; recompute D from P */
  1056. compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
  1057. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1058. return;
  1059. }
  1060. }
  1061. /* We're missing D+P or D+D; build pointer table */
  1062. {
  1063. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1064. void *ptrs[disks];
  1065. count = 0;
  1066. i = d0_idx;
  1067. do {
  1068. ptrs[count++] = page_address(sh->dev[i].page);
  1069. i = raid6_next_disk(i, disks);
  1070. if (i != dd_idx1 && i != dd_idx2 &&
  1071. !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1072. printk("compute_2 with missing block %d/%d\n", count, i);
  1073. } while ( i != d0_idx );
  1074. if ( failb == disks-2 ) {
  1075. /* We're missing D+P. */
  1076. raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
  1077. } else {
  1078. /* We're missing D+D. */
  1079. raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
  1080. }
  1081. /* Both the above update both missing blocks */
  1082. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1083. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1084. }
  1085. }
  1086. /*
  1087. * Each stripe/dev can have one or more bion attached.
  1088. * toread/towrite point to the first in a chain.
  1089. * The bi_next chain must be in order.
  1090. */
  1091. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1092. {
  1093. struct bio **bip;
  1094. raid5_conf_t *conf = sh->raid_conf;
  1095. int firstwrite=0;
  1096. PRINTK("adding bh b#%llu to stripe s#%llu\n",
  1097. (unsigned long long)bi->bi_sector,
  1098. (unsigned long long)sh->sector);
  1099. spin_lock(&sh->lock);
  1100. spin_lock_irq(&conf->device_lock);
  1101. if (forwrite) {
  1102. bip = &sh->dev[dd_idx].towrite;
  1103. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1104. firstwrite = 1;
  1105. } else
  1106. bip = &sh->dev[dd_idx].toread;
  1107. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1108. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1109. goto overlap;
  1110. bip = & (*bip)->bi_next;
  1111. }
  1112. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1113. goto overlap;
  1114. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1115. if (*bip)
  1116. bi->bi_next = *bip;
  1117. *bip = bi;
  1118. bi->bi_phys_segments ++;
  1119. spin_unlock_irq(&conf->device_lock);
  1120. spin_unlock(&sh->lock);
  1121. PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1122. (unsigned long long)bi->bi_sector,
  1123. (unsigned long long)sh->sector, dd_idx);
  1124. if (conf->mddev->bitmap && firstwrite) {
  1125. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1126. STRIPE_SECTORS, 0);
  1127. sh->bm_seq = conf->seq_flush+1;
  1128. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1129. }
  1130. if (forwrite) {
  1131. /* check if page is covered */
  1132. sector_t sector = sh->dev[dd_idx].sector;
  1133. for (bi=sh->dev[dd_idx].towrite;
  1134. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1135. bi && bi->bi_sector <= sector;
  1136. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1137. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1138. sector = bi->bi_sector + (bi->bi_size>>9);
  1139. }
  1140. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1141. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1142. }
  1143. return 1;
  1144. overlap:
  1145. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1146. spin_unlock_irq(&conf->device_lock);
  1147. spin_unlock(&sh->lock);
  1148. return 0;
  1149. }
  1150. static void end_reshape(raid5_conf_t *conf);
  1151. static int page_is_zero(struct page *p)
  1152. {
  1153. char *a = page_address(p);
  1154. return ((*(u32*)a) == 0 &&
  1155. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1156. }
  1157. static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  1158. {
  1159. int sectors_per_chunk = conf->chunk_size >> 9;
  1160. int pd_idx, dd_idx;
  1161. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1162. raid5_compute_sector(stripe * (disks - conf->max_degraded)
  1163. *sectors_per_chunk + chunk_offset,
  1164. disks, disks - conf->max_degraded,
  1165. &dd_idx, &pd_idx, conf);
  1166. return pd_idx;
  1167. }
  1168. /*
  1169. * handle_stripe - do things to a stripe.
  1170. *
  1171. * We lock the stripe and then examine the state of various bits
  1172. * to see what needs to be done.
  1173. * Possible results:
  1174. * return some read request which now have data
  1175. * return some write requests which are safely on disc
  1176. * schedule a read on some buffers
  1177. * schedule a write of some buffers
  1178. * return confirmation of parity correctness
  1179. *
  1180. * Parity calculations are done inside the stripe lock
  1181. * buffers are taken off read_list or write_list, and bh_cache buffers
  1182. * get BH_Lock set before the stripe lock is released.
  1183. *
  1184. */
  1185. static void handle_stripe5(struct stripe_head *sh)
  1186. {
  1187. raid5_conf_t *conf = sh->raid_conf;
  1188. int disks = sh->disks;
  1189. struct bio *return_bi= NULL;
  1190. struct bio *bi;
  1191. int i;
  1192. int syncing, expanding, expanded;
  1193. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  1194. int non_overwrite = 0;
  1195. int failed_num=0;
  1196. struct r5dev *dev;
  1197. PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
  1198. (unsigned long long)sh->sector, atomic_read(&sh->count),
  1199. sh->pd_idx);
  1200. spin_lock(&sh->lock);
  1201. clear_bit(STRIPE_HANDLE, &sh->state);
  1202. clear_bit(STRIPE_DELAYED, &sh->state);
  1203. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  1204. expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1205. expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  1206. /* Now to look around and see what can be done */
  1207. rcu_read_lock();
  1208. for (i=disks; i--; ) {
  1209. mdk_rdev_t *rdev;
  1210. dev = &sh->dev[i];
  1211. clear_bit(R5_Insync, &dev->flags);
  1212. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  1213. i, dev->flags, dev->toread, dev->towrite, dev->written);
  1214. /* maybe we can reply to a read */
  1215. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  1216. struct bio *rbi, *rbi2;
  1217. PRINTK("Return read for disc %d\n", i);
  1218. spin_lock_irq(&conf->device_lock);
  1219. rbi = dev->toread;
  1220. dev->toread = NULL;
  1221. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1222. wake_up(&conf->wait_for_overlap);
  1223. spin_unlock_irq(&conf->device_lock);
  1224. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1225. copy_data(0, rbi, dev->page, dev->sector);
  1226. rbi2 = r5_next_bio(rbi, dev->sector);
  1227. spin_lock_irq(&conf->device_lock);
  1228. if (--rbi->bi_phys_segments == 0) {
  1229. rbi->bi_next = return_bi;
  1230. return_bi = rbi;
  1231. }
  1232. spin_unlock_irq(&conf->device_lock);
  1233. rbi = rbi2;
  1234. }
  1235. }
  1236. /* now count some things */
  1237. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  1238. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  1239. if (dev->toread) to_read++;
  1240. if (dev->towrite) {
  1241. to_write++;
  1242. if (!test_bit(R5_OVERWRITE, &dev->flags))
  1243. non_overwrite++;
  1244. }
  1245. if (dev->written) written++;
  1246. rdev = rcu_dereference(conf->disks[i].rdev);
  1247. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1248. /* The ReadError flag will just be confusing now */
  1249. clear_bit(R5_ReadError, &dev->flags);
  1250. clear_bit(R5_ReWrite, &dev->flags);
  1251. }
  1252. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1253. || test_bit(R5_ReadError, &dev->flags)) {
  1254. failed++;
  1255. failed_num = i;
  1256. } else
  1257. set_bit(R5_Insync, &dev->flags);
  1258. }
  1259. rcu_read_unlock();
  1260. PRINTK("locked=%d uptodate=%d to_read=%d"
  1261. " to_write=%d failed=%d failed_num=%d\n",
  1262. locked, uptodate, to_read, to_write, failed, failed_num);
  1263. /* check if the array has lost two devices and, if so, some requests might
  1264. * need to be failed
  1265. */
  1266. if (failed > 1 && to_read+to_write+written) {
  1267. for (i=disks; i--; ) {
  1268. int bitmap_end = 0;
  1269. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1270. mdk_rdev_t *rdev;
  1271. rcu_read_lock();
  1272. rdev = rcu_dereference(conf->disks[i].rdev);
  1273. if (rdev && test_bit(In_sync, &rdev->flags))
  1274. /* multiple read failures in one stripe */
  1275. md_error(conf->mddev, rdev);
  1276. rcu_read_unlock();
  1277. }
  1278. spin_lock_irq(&conf->device_lock);
  1279. /* fail all writes first */
  1280. bi = sh->dev[i].towrite;
  1281. sh->dev[i].towrite = NULL;
  1282. if (bi) { to_write--; bitmap_end = 1; }
  1283. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1284. wake_up(&conf->wait_for_overlap);
  1285. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1286. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1287. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1288. if (--bi->bi_phys_segments == 0) {
  1289. md_write_end(conf->mddev);
  1290. bi->bi_next = return_bi;
  1291. return_bi = bi;
  1292. }
  1293. bi = nextbi;
  1294. }
  1295. /* and fail all 'written' */
  1296. bi = sh->dev[i].written;
  1297. sh->dev[i].written = NULL;
  1298. if (bi) bitmap_end = 1;
  1299. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1300. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1301. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1302. if (--bi->bi_phys_segments == 0) {
  1303. md_write_end(conf->mddev);
  1304. bi->bi_next = return_bi;
  1305. return_bi = bi;
  1306. }
  1307. bi = bi2;
  1308. }
  1309. /* fail any reads if this device is non-operational */
  1310. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1311. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1312. bi = sh->dev[i].toread;
  1313. sh->dev[i].toread = NULL;
  1314. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1315. wake_up(&conf->wait_for_overlap);
  1316. if (bi) to_read--;
  1317. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1318. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1319. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1320. if (--bi->bi_phys_segments == 0) {
  1321. bi->bi_next = return_bi;
  1322. return_bi = bi;
  1323. }
  1324. bi = nextbi;
  1325. }
  1326. }
  1327. spin_unlock_irq(&conf->device_lock);
  1328. if (bitmap_end)
  1329. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1330. STRIPE_SECTORS, 0, 0);
  1331. }
  1332. }
  1333. if (failed > 1 && syncing) {
  1334. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1335. clear_bit(STRIPE_SYNCING, &sh->state);
  1336. syncing = 0;
  1337. }
  1338. /* might be able to return some write requests if the parity block
  1339. * is safe, or on a failed drive
  1340. */
  1341. dev = &sh->dev[sh->pd_idx];
  1342. if ( written &&
  1343. ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
  1344. test_bit(R5_UPTODATE, &dev->flags))
  1345. || (failed == 1 && failed_num == sh->pd_idx))
  1346. ) {
  1347. /* any written block on an uptodate or failed drive can be returned.
  1348. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1349. * never LOCKED, so we don't need to test 'failed' directly.
  1350. */
  1351. for (i=disks; i--; )
  1352. if (sh->dev[i].written) {
  1353. dev = &sh->dev[i];
  1354. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1355. test_bit(R5_UPTODATE, &dev->flags) ) {
  1356. /* We can return any write requests */
  1357. struct bio *wbi, *wbi2;
  1358. int bitmap_end = 0;
  1359. PRINTK("Return write for disc %d\n", i);
  1360. spin_lock_irq(&conf->device_lock);
  1361. wbi = dev->written;
  1362. dev->written = NULL;
  1363. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1364. wbi2 = r5_next_bio(wbi, dev->sector);
  1365. if (--wbi->bi_phys_segments == 0) {
  1366. md_write_end(conf->mddev);
  1367. wbi->bi_next = return_bi;
  1368. return_bi = wbi;
  1369. }
  1370. wbi = wbi2;
  1371. }
  1372. if (dev->towrite == NULL)
  1373. bitmap_end = 1;
  1374. spin_unlock_irq(&conf->device_lock);
  1375. if (bitmap_end)
  1376. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1377. STRIPE_SECTORS,
  1378. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1379. }
  1380. }
  1381. }
  1382. /* Now we might consider reading some blocks, either to check/generate
  1383. * parity, or to satisfy requests
  1384. * or to load a block that is being partially written.
  1385. */
  1386. if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
  1387. for (i=disks; i--;) {
  1388. dev = &sh->dev[i];
  1389. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1390. (dev->toread ||
  1391. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1392. syncing ||
  1393. expanding ||
  1394. (failed && (sh->dev[failed_num].toread ||
  1395. (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
  1396. )
  1397. ) {
  1398. /* we would like to get this block, possibly
  1399. * by computing it, but we might not be able to
  1400. */
  1401. if (uptodate == disks-1) {
  1402. PRINTK("Computing block %d\n", i);
  1403. compute_block(sh, i);
  1404. uptodate++;
  1405. } else if (test_bit(R5_Insync, &dev->flags)) {
  1406. set_bit(R5_LOCKED, &dev->flags);
  1407. set_bit(R5_Wantread, &dev->flags);
  1408. locked++;
  1409. PRINTK("Reading block %d (sync=%d)\n",
  1410. i, syncing);
  1411. }
  1412. }
  1413. }
  1414. set_bit(STRIPE_HANDLE, &sh->state);
  1415. }
  1416. /* now to consider writing and what else, if anything should be read */
  1417. if (to_write) {
  1418. int rmw=0, rcw=0;
  1419. for (i=disks ; i--;) {
  1420. /* would I have to read this buffer for read_modify_write */
  1421. dev = &sh->dev[i];
  1422. if ((dev->towrite || i == sh->pd_idx) &&
  1423. (!test_bit(R5_LOCKED, &dev->flags)
  1424. ) &&
  1425. !test_bit(R5_UPTODATE, &dev->flags)) {
  1426. if (test_bit(R5_Insync, &dev->flags)
  1427. /* && !(!mddev->insync && i == sh->pd_idx) */
  1428. )
  1429. rmw++;
  1430. else rmw += 2*disks; /* cannot read it */
  1431. }
  1432. /* Would I have to read this buffer for reconstruct_write */
  1433. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1434. (!test_bit(R5_LOCKED, &dev->flags)
  1435. ) &&
  1436. !test_bit(R5_UPTODATE, &dev->flags)) {
  1437. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1438. else rcw += 2*disks;
  1439. }
  1440. }
  1441. PRINTK("for sector %llu, rmw=%d rcw=%d\n",
  1442. (unsigned long long)sh->sector, rmw, rcw);
  1443. set_bit(STRIPE_HANDLE, &sh->state);
  1444. if (rmw < rcw && rmw > 0)
  1445. /* prefer read-modify-write, but need to get some data */
  1446. for (i=disks; i--;) {
  1447. dev = &sh->dev[i];
  1448. if ((dev->towrite || i == sh->pd_idx) &&
  1449. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1450. test_bit(R5_Insync, &dev->flags)) {
  1451. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1452. {
  1453. PRINTK("Read_old block %d for r-m-w\n", i);
  1454. set_bit(R5_LOCKED, &dev->flags);
  1455. set_bit(R5_Wantread, &dev->flags);
  1456. locked++;
  1457. } else {
  1458. set_bit(STRIPE_DELAYED, &sh->state);
  1459. set_bit(STRIPE_HANDLE, &sh->state);
  1460. }
  1461. }
  1462. }
  1463. if (rcw <= rmw && rcw > 0)
  1464. /* want reconstruct write, but need to get some data */
  1465. for (i=disks; i--;) {
  1466. dev = &sh->dev[i];
  1467. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1468. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1469. test_bit(R5_Insync, &dev->flags)) {
  1470. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1471. {
  1472. PRINTK("Read_old block %d for Reconstruct\n", i);
  1473. set_bit(R5_LOCKED, &dev->flags);
  1474. set_bit(R5_Wantread, &dev->flags);
  1475. locked++;
  1476. } else {
  1477. set_bit(STRIPE_DELAYED, &sh->state);
  1478. set_bit(STRIPE_HANDLE, &sh->state);
  1479. }
  1480. }
  1481. }
  1482. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1483. if (locked == 0 && (rcw == 0 ||rmw == 0) &&
  1484. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1485. PRINTK("Computing parity...\n");
  1486. compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
  1487. /* now every locked buffer is ready to be written */
  1488. for (i=disks; i--;)
  1489. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1490. PRINTK("Writing block %d\n", i);
  1491. locked++;
  1492. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1493. if (!test_bit(R5_Insync, &sh->dev[i].flags)
  1494. || (i==sh->pd_idx && failed == 0))
  1495. set_bit(STRIPE_INSYNC, &sh->state);
  1496. }
  1497. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1498. atomic_dec(&conf->preread_active_stripes);
  1499. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  1500. md_wakeup_thread(conf->mddev->thread);
  1501. }
  1502. }
  1503. }
  1504. /* maybe we need to check and possibly fix the parity for this stripe
  1505. * Any reads will already have been scheduled, so we just see if enough data
  1506. * is available
  1507. */
  1508. if (syncing && locked == 0 &&
  1509. !test_bit(STRIPE_INSYNC, &sh->state)) {
  1510. set_bit(STRIPE_HANDLE, &sh->state);
  1511. if (failed == 0) {
  1512. BUG_ON(uptodate != disks);
  1513. compute_parity5(sh, CHECK_PARITY);
  1514. uptodate--;
  1515. if (page_is_zero(sh->dev[sh->pd_idx].page)) {
  1516. /* parity is correct (on disc, not in buffer any more) */
  1517. set_bit(STRIPE_INSYNC, &sh->state);
  1518. } else {
  1519. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  1520. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  1521. /* don't try to repair!! */
  1522. set_bit(STRIPE_INSYNC, &sh->state);
  1523. else {
  1524. compute_block(sh, sh->pd_idx);
  1525. uptodate++;
  1526. }
  1527. }
  1528. }
  1529. if (!test_bit(STRIPE_INSYNC, &sh->state)) {
  1530. /* either failed parity check, or recovery is happening */
  1531. if (failed==0)
  1532. failed_num = sh->pd_idx;
  1533. dev = &sh->dev[failed_num];
  1534. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  1535. BUG_ON(uptodate != disks);
  1536. set_bit(R5_LOCKED, &dev->flags);
  1537. set_bit(R5_Wantwrite, &dev->flags);
  1538. clear_bit(STRIPE_DEGRADED, &sh->state);
  1539. locked++;
  1540. set_bit(STRIPE_INSYNC, &sh->state);
  1541. }
  1542. }
  1543. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  1544. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  1545. clear_bit(STRIPE_SYNCING, &sh->state);
  1546. }
  1547. /* If the failed drive is just a ReadError, then we might need to progress
  1548. * the repair/check process
  1549. */
  1550. if (failed == 1 && ! conf->mddev->ro &&
  1551. test_bit(R5_ReadError, &sh->dev[failed_num].flags)
  1552. && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
  1553. && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
  1554. ) {
  1555. dev = &sh->dev[failed_num];
  1556. if (!test_bit(R5_ReWrite, &dev->flags)) {
  1557. set_bit(R5_Wantwrite, &dev->flags);
  1558. set_bit(R5_ReWrite, &dev->flags);
  1559. set_bit(R5_LOCKED, &dev->flags);
  1560. locked++;
  1561. } else {
  1562. /* let's read it back */
  1563. set_bit(R5_Wantread, &dev->flags);
  1564. set_bit(R5_LOCKED, &dev->flags);
  1565. locked++;
  1566. }
  1567. }
  1568. if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  1569. /* Need to write out all blocks after computing parity */
  1570. sh->disks = conf->raid_disks;
  1571. sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
  1572. compute_parity5(sh, RECONSTRUCT_WRITE);
  1573. for (i= conf->raid_disks; i--;) {
  1574. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1575. locked++;
  1576. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1577. }
  1578. clear_bit(STRIPE_EXPANDING, &sh->state);
  1579. } else if (expanded) {
  1580. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  1581. atomic_dec(&conf->reshape_stripes);
  1582. wake_up(&conf->wait_for_overlap);
  1583. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  1584. }
  1585. if (expanding && locked == 0) {
  1586. /* We have read all the blocks in this stripe and now we need to
  1587. * copy some of them into a target stripe for expand.
  1588. */
  1589. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1590. for (i=0; i< sh->disks; i++)
  1591. if (i != sh->pd_idx) {
  1592. int dd_idx, pd_idx, j;
  1593. struct stripe_head *sh2;
  1594. sector_t bn = compute_blocknr(sh, i);
  1595. sector_t s = raid5_compute_sector(bn, conf->raid_disks,
  1596. conf->raid_disks-1,
  1597. &dd_idx, &pd_idx, conf);
  1598. sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
  1599. if (sh2 == NULL)
  1600. /* so far only the early blocks of this stripe
  1601. * have been requested. When later blocks
  1602. * get requested, we will try again
  1603. */
  1604. continue;
  1605. if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  1606. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  1607. /* must have already done this block */
  1608. release_stripe(sh2);
  1609. continue;
  1610. }
  1611. memcpy(page_address(sh2->dev[dd_idx].page),
  1612. page_address(sh->dev[i].page),
  1613. STRIPE_SIZE);
  1614. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  1615. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  1616. for (j=0; j<conf->raid_disks; j++)
  1617. if (j != sh2->pd_idx &&
  1618. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  1619. break;
  1620. if (j == conf->raid_disks) {
  1621. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  1622. set_bit(STRIPE_HANDLE, &sh2->state);
  1623. }
  1624. release_stripe(sh2);
  1625. }
  1626. }
  1627. spin_unlock(&sh->lock);
  1628. while ((bi=return_bi)) {
  1629. int bytes = bi->bi_size;
  1630. return_bi = bi->bi_next;
  1631. bi->bi_next = NULL;
  1632. bi->bi_size = 0;
  1633. bi->bi_end_io(bi, bytes,
  1634. test_bit(BIO_UPTODATE, &bi->bi_flags)
  1635. ? 0 : -EIO);
  1636. }
  1637. for (i=disks; i-- ;) {
  1638. int rw;
  1639. struct bio *bi;
  1640. mdk_rdev_t *rdev;
  1641. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  1642. rw = WRITE;
  1643. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  1644. rw = READ;
  1645. else
  1646. continue;
  1647. bi = &sh->dev[i].req;
  1648. bi->bi_rw = rw;
  1649. if (rw == WRITE)
  1650. bi->bi_end_io = raid5_end_write_request;
  1651. else
  1652. bi->bi_end_io = raid5_end_read_request;
  1653. rcu_read_lock();
  1654. rdev = rcu_dereference(conf->disks[i].rdev);
  1655. if (rdev && test_bit(Faulty, &rdev->flags))
  1656. rdev = NULL;
  1657. if (rdev)
  1658. atomic_inc(&rdev->nr_pending);
  1659. rcu_read_unlock();
  1660. if (rdev) {
  1661. if (syncing || expanding || expanded)
  1662. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  1663. bi->bi_bdev = rdev->bdev;
  1664. PRINTK("for %llu schedule op %ld on disc %d\n",
  1665. (unsigned long long)sh->sector, bi->bi_rw, i);
  1666. atomic_inc(&sh->count);
  1667. bi->bi_sector = sh->sector + rdev->data_offset;
  1668. bi->bi_flags = 1 << BIO_UPTODATE;
  1669. bi->bi_vcnt = 1;
  1670. bi->bi_max_vecs = 1;
  1671. bi->bi_idx = 0;
  1672. bi->bi_io_vec = &sh->dev[i].vec;
  1673. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1674. bi->bi_io_vec[0].bv_offset = 0;
  1675. bi->bi_size = STRIPE_SIZE;
  1676. bi->bi_next = NULL;
  1677. if (rw == WRITE &&
  1678. test_bit(R5_ReWrite, &sh->dev[i].flags))
  1679. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  1680. generic_make_request(bi);
  1681. } else {
  1682. if (rw == WRITE)
  1683. set_bit(STRIPE_DEGRADED, &sh->state);
  1684. PRINTK("skip op %ld on disc %d for sector %llu\n",
  1685. bi->bi_rw, i, (unsigned long long)sh->sector);
  1686. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1687. set_bit(STRIPE_HANDLE, &sh->state);
  1688. }
  1689. }
  1690. }
  1691. static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
  1692. {
  1693. raid6_conf_t *conf = sh->raid_conf;
  1694. int disks = conf->raid_disks;
  1695. struct bio *return_bi= NULL;
  1696. struct bio *bi;
  1697. int i;
  1698. int syncing;
  1699. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  1700. int non_overwrite = 0;
  1701. int failed_num[2] = {0, 0};
  1702. struct r5dev *dev, *pdev, *qdev;
  1703. int pd_idx = sh->pd_idx;
  1704. int qd_idx = raid6_next_disk(pd_idx, disks);
  1705. int p_failed, q_failed;
  1706. PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
  1707. (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
  1708. pd_idx, qd_idx);
  1709. spin_lock(&sh->lock);
  1710. clear_bit(STRIPE_HANDLE, &sh->state);
  1711. clear_bit(STRIPE_DELAYED, &sh->state);
  1712. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  1713. /* Now to look around and see what can be done */
  1714. rcu_read_lock();
  1715. for (i=disks; i--; ) {
  1716. mdk_rdev_t *rdev;
  1717. dev = &sh->dev[i];
  1718. clear_bit(R5_Insync, &dev->flags);
  1719. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  1720. i, dev->flags, dev->toread, dev->towrite, dev->written);
  1721. /* maybe we can reply to a read */
  1722. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  1723. struct bio *rbi, *rbi2;
  1724. PRINTK("Return read for disc %d\n", i);
  1725. spin_lock_irq(&conf->device_lock);
  1726. rbi = dev->toread;
  1727. dev->toread = NULL;
  1728. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1729. wake_up(&conf->wait_for_overlap);
  1730. spin_unlock_irq(&conf->device_lock);
  1731. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1732. copy_data(0, rbi, dev->page, dev->sector);
  1733. rbi2 = r5_next_bio(rbi, dev->sector);
  1734. spin_lock_irq(&conf->device_lock);
  1735. if (--rbi->bi_phys_segments == 0) {
  1736. rbi->bi_next = return_bi;
  1737. return_bi = rbi;
  1738. }
  1739. spin_unlock_irq(&conf->device_lock);
  1740. rbi = rbi2;
  1741. }
  1742. }
  1743. /* now count some things */
  1744. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  1745. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  1746. if (dev->toread) to_read++;
  1747. if (dev->towrite) {
  1748. to_write++;
  1749. if (!test_bit(R5_OVERWRITE, &dev->flags))
  1750. non_overwrite++;
  1751. }
  1752. if (dev->written) written++;
  1753. rdev = rcu_dereference(conf->disks[i].rdev);
  1754. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1755. /* The ReadError flag will just be confusing now */
  1756. clear_bit(R5_ReadError, &dev->flags);
  1757. clear_bit(R5_ReWrite, &dev->flags);
  1758. }
  1759. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1760. || test_bit(R5_ReadError, &dev->flags)) {
  1761. if ( failed < 2 )
  1762. failed_num[failed] = i;
  1763. failed++;
  1764. } else
  1765. set_bit(R5_Insync, &dev->flags);
  1766. }
  1767. rcu_read_unlock();
  1768. PRINTK("locked=%d uptodate=%d to_read=%d"
  1769. " to_write=%d failed=%d failed_num=%d,%d\n",
  1770. locked, uptodate, to_read, to_write, failed,
  1771. failed_num[0], failed_num[1]);
  1772. /* check if the array has lost >2 devices and, if so, some requests might
  1773. * need to be failed
  1774. */
  1775. if (failed > 2 && to_read+to_write+written) {
  1776. for (i=disks; i--; ) {
  1777. int bitmap_end = 0;
  1778. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1779. mdk_rdev_t *rdev;
  1780. rcu_read_lock();
  1781. rdev = rcu_dereference(conf->disks[i].rdev);
  1782. if (rdev && test_bit(In_sync, &rdev->flags))
  1783. /* multiple read failures in one stripe */
  1784. md_error(conf->mddev, rdev);
  1785. rcu_read_unlock();
  1786. }
  1787. spin_lock_irq(&conf->device_lock);
  1788. /* fail all writes first */
  1789. bi = sh->dev[i].towrite;
  1790. sh->dev[i].towrite = NULL;
  1791. if (bi) { to_write--; bitmap_end = 1; }
  1792. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1793. wake_up(&conf->wait_for_overlap);
  1794. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1795. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1796. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1797. if (--bi->bi_phys_segments == 0) {
  1798. md_write_end(conf->mddev);
  1799. bi->bi_next = return_bi;
  1800. return_bi = bi;
  1801. }
  1802. bi = nextbi;
  1803. }
  1804. /* and fail all 'written' */
  1805. bi = sh->dev[i].written;
  1806. sh->dev[i].written = NULL;
  1807. if (bi) bitmap_end = 1;
  1808. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1809. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1810. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1811. if (--bi->bi_phys_segments == 0) {
  1812. md_write_end(conf->mddev);
  1813. bi->bi_next = return_bi;
  1814. return_bi = bi;
  1815. }
  1816. bi = bi2;
  1817. }
  1818. /* fail any reads if this device is non-operational */
  1819. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1820. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1821. bi = sh->dev[i].toread;
  1822. sh->dev[i].toread = NULL;
  1823. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1824. wake_up(&conf->wait_for_overlap);
  1825. if (bi) to_read--;
  1826. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1827. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1828. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1829. if (--bi->bi_phys_segments == 0) {
  1830. bi->bi_next = return_bi;
  1831. return_bi = bi;
  1832. }
  1833. bi = nextbi;
  1834. }
  1835. }
  1836. spin_unlock_irq(&conf->device_lock);
  1837. if (bitmap_end)
  1838. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1839. STRIPE_SECTORS, 0, 0);
  1840. }
  1841. }
  1842. if (failed > 2 && syncing) {
  1843. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1844. clear_bit(STRIPE_SYNCING, &sh->state);
  1845. syncing = 0;
  1846. }
  1847. /*
  1848. * might be able to return some write requests if the parity blocks
  1849. * are safe, or on a failed drive
  1850. */
  1851. pdev = &sh->dev[pd_idx];
  1852. p_failed = (failed >= 1 && failed_num[0] == pd_idx)
  1853. || (failed >= 2 && failed_num[1] == pd_idx);
  1854. qdev = &sh->dev[qd_idx];
  1855. q_failed = (failed >= 1 && failed_num[0] == qd_idx)
  1856. || (failed >= 2 && failed_num[1] == qd_idx);
  1857. if ( written &&
  1858. ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
  1859. && !test_bit(R5_LOCKED, &pdev->flags)
  1860. && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
  1861. ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
  1862. && !test_bit(R5_LOCKED, &qdev->flags)
  1863. && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
  1864. /* any written block on an uptodate or failed drive can be
  1865. * returned. Note that if we 'wrote' to a failed drive,
  1866. * it will be UPTODATE, but never LOCKED, so we don't need
  1867. * to test 'failed' directly.
  1868. */
  1869. for (i=disks; i--; )
  1870. if (sh->dev[i].written) {
  1871. dev = &sh->dev[i];
  1872. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1873. test_bit(R5_UPTODATE, &dev->flags) ) {
  1874. /* We can return any write requests */
  1875. int bitmap_end = 0;
  1876. struct bio *wbi, *wbi2;
  1877. PRINTK("Return write for stripe %llu disc %d\n",
  1878. (unsigned long long)sh->sector, i);
  1879. spin_lock_irq(&conf->device_lock);
  1880. wbi = dev->written;
  1881. dev->written = NULL;
  1882. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1883. wbi2 = r5_next_bio(wbi, dev->sector);
  1884. if (--wbi->bi_phys_segments == 0) {
  1885. md_write_end(conf->mddev);
  1886. wbi->bi_next = return_bi;
  1887. return_bi = wbi;
  1888. }
  1889. wbi = wbi2;
  1890. }
  1891. if (dev->towrite == NULL)
  1892. bitmap_end = 1;
  1893. spin_unlock_irq(&conf->device_lock);
  1894. if (bitmap_end)
  1895. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1896. STRIPE_SECTORS,
  1897. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1898. }
  1899. }
  1900. }
  1901. /* Now we might consider reading some blocks, either to check/generate
  1902. * parity, or to satisfy requests
  1903. * or to load a block that is being partially written.
  1904. */
  1905. if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
  1906. for (i=disks; i--;) {
  1907. dev = &sh->dev[i];
  1908. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1909. (dev->toread ||
  1910. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1911. syncing ||
  1912. (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
  1913. (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
  1914. )
  1915. ) {
  1916. /* we would like to get this block, possibly
  1917. * by computing it, but we might not be able to
  1918. */
  1919. if (uptodate == disks-1) {
  1920. PRINTK("Computing stripe %llu block %d\n",
  1921. (unsigned long long)sh->sector, i);
  1922. compute_block_1(sh, i, 0);
  1923. uptodate++;
  1924. } else if ( uptodate == disks-2 && failed >= 2 ) {
  1925. /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
  1926. int other;
  1927. for (other=disks; other--;) {
  1928. if ( other == i )
  1929. continue;
  1930. if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
  1931. break;
  1932. }
  1933. BUG_ON(other < 0);
  1934. PRINTK("Computing stripe %llu blocks %d,%d\n",
  1935. (unsigned long long)sh->sector, i, other);
  1936. compute_block_2(sh, i, other);
  1937. uptodate += 2;
  1938. } else if (test_bit(R5_Insync, &dev->flags)) {
  1939. set_bit(R5_LOCKED, &dev->flags);
  1940. set_bit(R5_Wantread, &dev->flags);
  1941. locked++;
  1942. PRINTK("Reading block %d (sync=%d)\n",
  1943. i, syncing);
  1944. }
  1945. }
  1946. }
  1947. set_bit(STRIPE_HANDLE, &sh->state);
  1948. }
  1949. /* now to consider writing and what else, if anything should be read */
  1950. if (to_write) {
  1951. int rcw=0, must_compute=0;
  1952. for (i=disks ; i--;) {
  1953. dev = &sh->dev[i];
  1954. /* Would I have to read this buffer for reconstruct_write */
  1955. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1956. && i != pd_idx && i != qd_idx
  1957. && (!test_bit(R5_LOCKED, &dev->flags)
  1958. ) &&
  1959. !test_bit(R5_UPTODATE, &dev->flags)) {
  1960. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1961. else {
  1962. PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
  1963. must_compute++;
  1964. }
  1965. }
  1966. }
  1967. PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
  1968. (unsigned long long)sh->sector, rcw, must_compute);
  1969. set_bit(STRIPE_HANDLE, &sh->state);
  1970. if (rcw > 0)
  1971. /* want reconstruct write, but need to get some data */
  1972. for (i=disks; i--;) {
  1973. dev = &sh->dev[i];
  1974. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1975. && !(failed == 0 && (i == pd_idx || i == qd_idx))
  1976. && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1977. test_bit(R5_Insync, &dev->flags)) {
  1978. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1979. {
  1980. PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
  1981. (unsigned long long)sh->sector, i);
  1982. set_bit(R5_LOCKED, &dev->flags);
  1983. set_bit(R5_Wantread, &dev->flags);
  1984. locked++;
  1985. } else {
  1986. PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
  1987. (unsigned long long)sh->sector, i);
  1988. set_bit(STRIPE_DELAYED, &sh->state);
  1989. set_bit(STRIPE_HANDLE, &sh->state);
  1990. }
  1991. }
  1992. }
  1993. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1994. if (locked == 0 && rcw == 0 &&
  1995. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1996. if ( must_compute > 0 ) {
  1997. /* We have failed blocks and need to compute them */
  1998. switch ( failed ) {
  1999. case 0: BUG();
  2000. case 1: compute_block_1(sh, failed_num[0], 0); break;
  2001. case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
  2002. default: BUG(); /* This request should have been failed? */
  2003. }
  2004. }
  2005. PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
  2006. compute_parity6(sh, RECONSTRUCT_WRITE);
  2007. /* now every locked buffer is ready to be written */
  2008. for (i=disks; i--;)
  2009. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  2010. PRINTK("Writing stripe %llu block %d\n",
  2011. (unsigned long long)sh->sector, i);
  2012. locked++;
  2013. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2014. }
  2015. /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
  2016. set_bit(STRIPE_INSYNC, &sh->state);
  2017. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2018. atomic_dec(&conf->preread_active_stripes);
  2019. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  2020. md_wakeup_thread(conf->mddev->thread);
  2021. }
  2022. }
  2023. }
  2024. /* maybe we need to check and possibly fix the parity for this stripe
  2025. * Any reads will already have been scheduled, so we just see if enough data
  2026. * is available
  2027. */
  2028. if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
  2029. int update_p = 0, update_q = 0;
  2030. struct r5dev *dev;
  2031. set_bit(STRIPE_HANDLE, &sh->state);
  2032. BUG_ON(failed>2);
  2033. BUG_ON(uptodate < disks);
  2034. /* Want to check and possibly repair P and Q.
  2035. * However there could be one 'failed' device, in which
  2036. * case we can only check one of them, possibly using the
  2037. * other to generate missing data
  2038. */
  2039. /* If !tmp_page, we cannot do the calculations,
  2040. * but as we have set STRIPE_HANDLE, we will soon be called
  2041. * by stripe_handle with a tmp_page - just wait until then.
  2042. */
  2043. if (tmp_page) {
  2044. if (failed == q_failed) {
  2045. /* The only possible failed device holds 'Q', so it makes
  2046. * sense to check P (If anything else were failed, we would
  2047. * have used P to recreate it).
  2048. */
  2049. compute_block_1(sh, pd_idx, 1);
  2050. if (!page_is_zero(sh->dev[pd_idx].page)) {
  2051. compute_block_1(sh,pd_idx,0);
  2052. update_p = 1;
  2053. }
  2054. }
  2055. if (!q_failed && failed < 2) {
  2056. /* q is not failed, and we didn't use it to generate
  2057. * anything, so it makes sense to check it
  2058. */
  2059. memcpy(page_address(tmp_page),
  2060. page_address(sh->dev[qd_idx].page),
  2061. STRIPE_SIZE);
  2062. compute_parity6(sh, UPDATE_PARITY);
  2063. if (memcmp(page_address(tmp_page),
  2064. page_address(sh->dev[qd_idx].page),
  2065. STRIPE_SIZE)!= 0) {
  2066. clear_bit(STRIPE_INSYNC, &sh->state);
  2067. update_q = 1;
  2068. }
  2069. }
  2070. if (update_p || update_q) {
  2071. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2072. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2073. /* don't try to repair!! */
  2074. update_p = update_q = 0;
  2075. }
  2076. /* now write out any block on a failed drive,
  2077. * or P or Q if they need it
  2078. */
  2079. if (failed == 2) {
  2080. dev = &sh->dev[failed_num[1]];
  2081. locked++;
  2082. set_bit(R5_LOCKED, &dev->flags);
  2083. set_bit(R5_Wantwrite, &dev->flags);
  2084. }
  2085. if (failed >= 1) {
  2086. dev = &sh->dev[failed_num[0]];
  2087. locked++;
  2088. set_bit(R5_LOCKED, &dev->flags);
  2089. set_bit(R5_Wantwrite, &dev->flags);
  2090. }
  2091. if (update_p) {
  2092. dev = &sh->dev[pd_idx];
  2093. locked ++;
  2094. set_bit(R5_LOCKED, &dev->flags);
  2095. set_bit(R5_Wantwrite, &dev->flags);
  2096. }
  2097. if (update_q) {
  2098. dev = &sh->dev[qd_idx];
  2099. locked++;
  2100. set_bit(R5_LOCKED, &dev->flags);
  2101. set_bit(R5_Wantwrite, &dev->flags);
  2102. }
  2103. clear_bit(STRIPE_DEGRADED, &sh->state);
  2104. set_bit(STRIPE_INSYNC, &sh->state);
  2105. }
  2106. }
  2107. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2108. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2109. clear_bit(STRIPE_SYNCING, &sh->state);
  2110. }
  2111. /* If the failed drives are just a ReadError, then we might need
  2112. * to progress the repair/check process
  2113. */
  2114. if (failed <= 2 && ! conf->mddev->ro)
  2115. for (i=0; i<failed;i++) {
  2116. dev = &sh->dev[failed_num[i]];
  2117. if (test_bit(R5_ReadError, &dev->flags)
  2118. && !test_bit(R5_LOCKED, &dev->flags)
  2119. && test_bit(R5_UPTODATE, &dev->flags)
  2120. ) {
  2121. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2122. set_bit(R5_Wantwrite, &dev->flags);
  2123. set_bit(R5_ReWrite, &dev->flags);
  2124. set_bit(R5_LOCKED, &dev->flags);
  2125. } else {
  2126. /* let's read it back */
  2127. set_bit(R5_Wantread, &dev->flags);
  2128. set_bit(R5_LOCKED, &dev->flags);
  2129. }
  2130. }
  2131. }
  2132. spin_unlock(&sh->lock);
  2133. while ((bi=return_bi)) {
  2134. int bytes = bi->bi_size;
  2135. return_bi = bi->bi_next;
  2136. bi->bi_next = NULL;
  2137. bi->bi_size = 0;
  2138. bi->bi_end_io(bi, bytes,
  2139. test_bit(BIO_UPTODATE, &bi->bi_flags)
  2140. ? 0 : -EIO);
  2141. }
  2142. for (i=disks; i-- ;) {
  2143. int rw;
  2144. struct bio *bi;
  2145. mdk_rdev_t *rdev;
  2146. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  2147. rw = WRITE;
  2148. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  2149. rw = READ;
  2150. else
  2151. continue;
  2152. bi = &sh->dev[i].req;
  2153. bi->bi_rw = rw;
  2154. if (rw == WRITE)
  2155. bi->bi_end_io = raid5_end_write_request;
  2156. else
  2157. bi->bi_end_io = raid5_end_read_request;
  2158. rcu_read_lock();
  2159. rdev = rcu_dereference(conf->disks[i].rdev);
  2160. if (rdev && test_bit(Faulty, &rdev->flags))
  2161. rdev = NULL;
  2162. if (rdev)
  2163. atomic_inc(&rdev->nr_pending);
  2164. rcu_read_unlock();
  2165. if (rdev) {
  2166. if (syncing)
  2167. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  2168. bi->bi_bdev = rdev->bdev;
  2169. PRINTK("for %llu schedule op %ld on disc %d\n",
  2170. (unsigned long long)sh->sector, bi->bi_rw, i);
  2171. atomic_inc(&sh->count);
  2172. bi->bi_sector = sh->sector + rdev->data_offset;
  2173. bi->bi_flags = 1 << BIO_UPTODATE;
  2174. bi->bi_vcnt = 1;
  2175. bi->bi_max_vecs = 1;
  2176. bi->bi_idx = 0;
  2177. bi->bi_io_vec = &sh->dev[i].vec;
  2178. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  2179. bi->bi_io_vec[0].bv_offset = 0;
  2180. bi->bi_size = STRIPE_SIZE;
  2181. bi->bi_next = NULL;
  2182. if (rw == WRITE &&
  2183. test_bit(R5_ReWrite, &sh->dev[i].flags))
  2184. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  2185. generic_make_request(bi);
  2186. } else {
  2187. if (rw == WRITE)
  2188. set_bit(STRIPE_DEGRADED, &sh->state);
  2189. PRINTK("skip op %ld on disc %d for sector %llu\n",
  2190. bi->bi_rw, i, (unsigned long long)sh->sector);
  2191. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2192. set_bit(STRIPE_HANDLE, &sh->state);
  2193. }
  2194. }
  2195. }
  2196. static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2197. {
  2198. if (sh->raid_conf->level == 6)
  2199. handle_stripe6(sh, tmp_page);
  2200. else
  2201. handle_stripe5(sh);
  2202. }
  2203. static void raid5_activate_delayed(raid5_conf_t *conf)
  2204. {
  2205. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2206. while (!list_empty(&conf->delayed_list)) {
  2207. struct list_head *l = conf->delayed_list.next;
  2208. struct stripe_head *sh;
  2209. sh = list_entry(l, struct stripe_head, lru);
  2210. list_del_init(l);
  2211. clear_bit(STRIPE_DELAYED, &sh->state);
  2212. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2213. atomic_inc(&conf->preread_active_stripes);
  2214. list_add_tail(&sh->lru, &conf->handle_list);
  2215. }
  2216. }
  2217. }
  2218. static void activate_bit_delay(raid5_conf_t *conf)
  2219. {
  2220. /* device_lock is held */
  2221. struct list_head head;
  2222. list_add(&head, &conf->bitmap_list);
  2223. list_del_init(&conf->bitmap_list);
  2224. while (!list_empty(&head)) {
  2225. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2226. list_del_init(&sh->lru);
  2227. atomic_inc(&sh->count);
  2228. __release_stripe(conf, sh);
  2229. }
  2230. }
  2231. static void unplug_slaves(mddev_t *mddev)
  2232. {
  2233. raid5_conf_t *conf = mddev_to_conf(mddev);
  2234. int i;
  2235. rcu_read_lock();
  2236. for (i=0; i<mddev->raid_disks; i++) {
  2237. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2238. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2239. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  2240. atomic_inc(&rdev->nr_pending);
  2241. rcu_read_unlock();
  2242. if (r_queue->unplug_fn)
  2243. r_queue->unplug_fn(r_queue);
  2244. rdev_dec_pending(rdev, mddev);
  2245. rcu_read_lock();
  2246. }
  2247. }
  2248. rcu_read_unlock();
  2249. }
  2250. static void raid5_unplug_device(request_queue_t *q)
  2251. {
  2252. mddev_t *mddev = q->queuedata;
  2253. raid5_conf_t *conf = mddev_to_conf(mddev);
  2254. unsigned long flags;
  2255. spin_lock_irqsave(&conf->device_lock, flags);
  2256. if (blk_remove_plug(q)) {
  2257. conf->seq_flush++;
  2258. raid5_activate_delayed(conf);
  2259. }
  2260. md_wakeup_thread(mddev->thread);
  2261. spin_unlock_irqrestore(&conf->device_lock, flags);
  2262. unplug_slaves(mddev);
  2263. }
  2264. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  2265. sector_t *error_sector)
  2266. {
  2267. mddev_t *mddev = q->queuedata;
  2268. raid5_conf_t *conf = mddev_to_conf(mddev);
  2269. int i, ret = 0;
  2270. rcu_read_lock();
  2271. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  2272. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2273. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  2274. struct block_device *bdev = rdev->bdev;
  2275. request_queue_t *r_queue = bdev_get_queue(bdev);
  2276. if (!r_queue->issue_flush_fn)
  2277. ret = -EOPNOTSUPP;
  2278. else {
  2279. atomic_inc(&rdev->nr_pending);
  2280. rcu_read_unlock();
  2281. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  2282. error_sector);
  2283. rdev_dec_pending(rdev, mddev);
  2284. rcu_read_lock();
  2285. }
  2286. }
  2287. }
  2288. rcu_read_unlock();
  2289. return ret;
  2290. }
  2291. static int raid5_congested(void *data, int bits)
  2292. {
  2293. mddev_t *mddev = data;
  2294. raid5_conf_t *conf = mddev_to_conf(mddev);
  2295. /* No difference between reads and writes. Just check
  2296. * how busy the stripe_cache is
  2297. */
  2298. if (conf->inactive_blocked)
  2299. return 1;
  2300. if (conf->quiesce)
  2301. return 1;
  2302. if (list_empty_careful(&conf->inactive_list))
  2303. return 1;
  2304. return 0;
  2305. }
  2306. /* We want read requests to align with chunks where possible,
  2307. * but write requests don't need to.
  2308. */
  2309. static int raid5_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
  2310. {
  2311. mddev_t *mddev = q->queuedata;
  2312. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2313. int max;
  2314. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2315. unsigned int bio_sectors = bio->bi_size >> 9;
  2316. if (bio_data_dir(bio) == WRITE)
  2317. return biovec->bv_len; /* always allow writes to be mergeable */
  2318. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  2319. if (max < 0) max = 0;
  2320. if (max <= biovec->bv_len && bio_sectors == 0)
  2321. return biovec->bv_len;
  2322. else
  2323. return max;
  2324. }
  2325. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  2326. {
  2327. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2328. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2329. unsigned int bio_sectors = bio->bi_size >> 9;
  2330. return chunk_sectors >=
  2331. ((sector & (chunk_sectors - 1)) + bio_sectors);
  2332. }
  2333. /*
  2334. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  2335. * later sampled by raid5d.
  2336. */
  2337. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  2338. {
  2339. unsigned long flags;
  2340. spin_lock_irqsave(&conf->device_lock, flags);
  2341. bi->bi_next = conf->retry_read_aligned_list;
  2342. conf->retry_read_aligned_list = bi;
  2343. spin_unlock_irqrestore(&conf->device_lock, flags);
  2344. md_wakeup_thread(conf->mddev->thread);
  2345. }
  2346. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  2347. {
  2348. struct bio *bi;
  2349. bi = conf->retry_read_aligned;
  2350. if (bi) {
  2351. conf->retry_read_aligned = NULL;
  2352. return bi;
  2353. }
  2354. bi = conf->retry_read_aligned_list;
  2355. if(bi) {
  2356. conf->retry_read_aligned_list = bi->bi_next;
  2357. bi->bi_next = NULL;
  2358. bi->bi_phys_segments = 1; /* biased count of active stripes */
  2359. bi->bi_hw_segments = 0; /* count of processed stripes */
  2360. }
  2361. return bi;
  2362. }
  2363. /*
  2364. * The "raid5_align_endio" should check if the read succeeded and if it
  2365. * did, call bio_endio on the original bio (having bio_put the new bio
  2366. * first).
  2367. * If the read failed..
  2368. */
  2369. static int raid5_align_endio(struct bio *bi, unsigned int bytes, int error)
  2370. {
  2371. struct bio* raid_bi = bi->bi_private;
  2372. mddev_t *mddev;
  2373. raid5_conf_t *conf;
  2374. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  2375. mdk_rdev_t *rdev;
  2376. if (bi->bi_size)
  2377. return 1;
  2378. bio_put(bi);
  2379. mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
  2380. conf = mddev_to_conf(mddev);
  2381. rdev = (void*)raid_bi->bi_next;
  2382. raid_bi->bi_next = NULL;
  2383. rdev_dec_pending(rdev, conf->mddev);
  2384. if (!error && uptodate) {
  2385. bio_endio(raid_bi, bytes, 0);
  2386. if (atomic_dec_and_test(&conf->active_aligned_reads))
  2387. wake_up(&conf->wait_for_stripe);
  2388. return 0;
  2389. }
  2390. PRINTK("raid5_align_endio : io error...handing IO for a retry\n");
  2391. add_bio_to_retry(raid_bi, conf);
  2392. return 0;
  2393. }
  2394. static int bio_fits_rdev(struct bio *bi)
  2395. {
  2396. request_queue_t *q = bdev_get_queue(bi->bi_bdev);
  2397. if ((bi->bi_size>>9) > q->max_sectors)
  2398. return 0;
  2399. blk_recount_segments(q, bi);
  2400. if (bi->bi_phys_segments > q->max_phys_segments ||
  2401. bi->bi_hw_segments > q->max_hw_segments)
  2402. return 0;
  2403. if (q->merge_bvec_fn)
  2404. /* it's too hard to apply the merge_bvec_fn at this stage,
  2405. * just just give up
  2406. */
  2407. return 0;
  2408. return 1;
  2409. }
  2410. static int chunk_aligned_read(request_queue_t *q, struct bio * raid_bio)
  2411. {
  2412. mddev_t *mddev = q->queuedata;
  2413. raid5_conf_t *conf = mddev_to_conf(mddev);
  2414. const unsigned int raid_disks = conf->raid_disks;
  2415. const unsigned int data_disks = raid_disks - conf->max_degraded;
  2416. unsigned int dd_idx, pd_idx;
  2417. struct bio* align_bi;
  2418. mdk_rdev_t *rdev;
  2419. if (!in_chunk_boundary(mddev, raid_bio)) {
  2420. PRINTK("chunk_aligned_read : non aligned\n");
  2421. return 0;
  2422. }
  2423. /*
  2424. * use bio_clone to make a copy of the bio
  2425. */
  2426. align_bi = bio_clone(raid_bio, GFP_NOIO);
  2427. if (!align_bi)
  2428. return 0;
  2429. /*
  2430. * set bi_end_io to a new function, and set bi_private to the
  2431. * original bio.
  2432. */
  2433. align_bi->bi_end_io = raid5_align_endio;
  2434. align_bi->bi_private = raid_bio;
  2435. /*
  2436. * compute position
  2437. */
  2438. align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
  2439. raid_disks,
  2440. data_disks,
  2441. &dd_idx,
  2442. &pd_idx,
  2443. conf);
  2444. rcu_read_lock();
  2445. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  2446. if (rdev && test_bit(In_sync, &rdev->flags)) {
  2447. atomic_inc(&rdev->nr_pending);
  2448. rcu_read_unlock();
  2449. raid_bio->bi_next = (void*)rdev;
  2450. align_bi->bi_bdev = rdev->bdev;
  2451. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  2452. align_bi->bi_sector += rdev->data_offset;
  2453. if (!bio_fits_rdev(align_bi)) {
  2454. /* too big in some way */
  2455. bio_put(align_bi);
  2456. rdev_dec_pending(rdev, mddev);
  2457. return 0;
  2458. }
  2459. spin_lock_irq(&conf->device_lock);
  2460. wait_event_lock_irq(conf->wait_for_stripe,
  2461. conf->quiesce == 0,
  2462. conf->device_lock, /* nothing */);
  2463. atomic_inc(&conf->active_aligned_reads);
  2464. spin_unlock_irq(&conf->device_lock);
  2465. generic_make_request(align_bi);
  2466. return 1;
  2467. } else {
  2468. rcu_read_unlock();
  2469. bio_put(align_bi);
  2470. return 0;
  2471. }
  2472. }
  2473. static int make_request(request_queue_t *q, struct bio * bi)
  2474. {
  2475. mddev_t *mddev = q->queuedata;
  2476. raid5_conf_t *conf = mddev_to_conf(mddev);
  2477. unsigned int dd_idx, pd_idx;
  2478. sector_t new_sector;
  2479. sector_t logical_sector, last_sector;
  2480. struct stripe_head *sh;
  2481. const int rw = bio_data_dir(bi);
  2482. int remaining;
  2483. if (unlikely(bio_barrier(bi))) {
  2484. bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
  2485. return 0;
  2486. }
  2487. md_write_start(mddev, bi);
  2488. disk_stat_inc(mddev->gendisk, ios[rw]);
  2489. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
  2490. if (rw == READ &&
  2491. mddev->reshape_position == MaxSector &&
  2492. chunk_aligned_read(q,bi))
  2493. return 0;
  2494. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  2495. last_sector = bi->bi_sector + (bi->bi_size>>9);
  2496. bi->bi_next = NULL;
  2497. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  2498. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  2499. DEFINE_WAIT(w);
  2500. int disks, data_disks;
  2501. retry:
  2502. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  2503. if (likely(conf->expand_progress == MaxSector))
  2504. disks = conf->raid_disks;
  2505. else {
  2506. /* spinlock is needed as expand_progress may be
  2507. * 64bit on a 32bit platform, and so it might be
  2508. * possible to see a half-updated value
  2509. * Ofcourse expand_progress could change after
  2510. * the lock is dropped, so once we get a reference
  2511. * to the stripe that we think it is, we will have
  2512. * to check again.
  2513. */
  2514. spin_lock_irq(&conf->device_lock);
  2515. disks = conf->raid_disks;
  2516. if (logical_sector >= conf->expand_progress)
  2517. disks = conf->previous_raid_disks;
  2518. else {
  2519. if (logical_sector >= conf->expand_lo) {
  2520. spin_unlock_irq(&conf->device_lock);
  2521. schedule();
  2522. goto retry;
  2523. }
  2524. }
  2525. spin_unlock_irq(&conf->device_lock);
  2526. }
  2527. data_disks = disks - conf->max_degraded;
  2528. new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
  2529. &dd_idx, &pd_idx, conf);
  2530. PRINTK("raid5: make_request, sector %llu logical %llu\n",
  2531. (unsigned long long)new_sector,
  2532. (unsigned long long)logical_sector);
  2533. sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
  2534. if (sh) {
  2535. if (unlikely(conf->expand_progress != MaxSector)) {
  2536. /* expansion might have moved on while waiting for a
  2537. * stripe, so we must do the range check again.
  2538. * Expansion could still move past after this
  2539. * test, but as we are holding a reference to
  2540. * 'sh', we know that if that happens,
  2541. * STRIPE_EXPANDING will get set and the expansion
  2542. * won't proceed until we finish with the stripe.
  2543. */
  2544. int must_retry = 0;
  2545. spin_lock_irq(&conf->device_lock);
  2546. if (logical_sector < conf->expand_progress &&
  2547. disks == conf->previous_raid_disks)
  2548. /* mismatch, need to try again */
  2549. must_retry = 1;
  2550. spin_unlock_irq(&conf->device_lock);
  2551. if (must_retry) {
  2552. release_stripe(sh);
  2553. goto retry;
  2554. }
  2555. }
  2556. /* FIXME what if we get a false positive because these
  2557. * are being updated.
  2558. */
  2559. if (logical_sector >= mddev->suspend_lo &&
  2560. logical_sector < mddev->suspend_hi) {
  2561. release_stripe(sh);
  2562. schedule();
  2563. goto retry;
  2564. }
  2565. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  2566. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  2567. /* Stripe is busy expanding or
  2568. * add failed due to overlap. Flush everything
  2569. * and wait a while
  2570. */
  2571. raid5_unplug_device(mddev->queue);
  2572. release_stripe(sh);
  2573. schedule();
  2574. goto retry;
  2575. }
  2576. finish_wait(&conf->wait_for_overlap, &w);
  2577. handle_stripe(sh, NULL);
  2578. release_stripe(sh);
  2579. } else {
  2580. /* cannot get stripe for read-ahead, just give-up */
  2581. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2582. finish_wait(&conf->wait_for_overlap, &w);
  2583. break;
  2584. }
  2585. }
  2586. spin_lock_irq(&conf->device_lock);
  2587. remaining = --bi->bi_phys_segments;
  2588. spin_unlock_irq(&conf->device_lock);
  2589. if (remaining == 0) {
  2590. int bytes = bi->bi_size;
  2591. if ( rw == WRITE )
  2592. md_write_end(mddev);
  2593. bi->bi_size = 0;
  2594. bi->bi_end_io(bi, bytes,
  2595. test_bit(BIO_UPTODATE, &bi->bi_flags)
  2596. ? 0 : -EIO);
  2597. }
  2598. return 0;
  2599. }
  2600. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  2601. {
  2602. /* reshaping is quite different to recovery/resync so it is
  2603. * handled quite separately ... here.
  2604. *
  2605. * On each call to sync_request, we gather one chunk worth of
  2606. * destination stripes and flag them as expanding.
  2607. * Then we find all the source stripes and request reads.
  2608. * As the reads complete, handle_stripe will copy the data
  2609. * into the destination stripe and release that stripe.
  2610. */
  2611. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2612. struct stripe_head *sh;
  2613. int pd_idx;
  2614. sector_t first_sector, last_sector;
  2615. int raid_disks;
  2616. int data_disks;
  2617. int i;
  2618. int dd_idx;
  2619. sector_t writepos, safepos, gap;
  2620. if (sector_nr == 0 &&
  2621. conf->expand_progress != 0) {
  2622. /* restarting in the middle, skip the initial sectors */
  2623. sector_nr = conf->expand_progress;
  2624. sector_div(sector_nr, conf->raid_disks-1);
  2625. *skipped = 1;
  2626. return sector_nr;
  2627. }
  2628. /* we update the metadata when there is more than 3Meg
  2629. * in the block range (that is rather arbitrary, should
  2630. * probably be time based) or when the data about to be
  2631. * copied would over-write the source of the data at
  2632. * the front of the range.
  2633. * i.e. one new_stripe forward from expand_progress new_maps
  2634. * to after where expand_lo old_maps to
  2635. */
  2636. writepos = conf->expand_progress +
  2637. conf->chunk_size/512*(conf->raid_disks-1);
  2638. sector_div(writepos, conf->raid_disks-1);
  2639. safepos = conf->expand_lo;
  2640. sector_div(safepos, conf->previous_raid_disks-1);
  2641. gap = conf->expand_progress - conf->expand_lo;
  2642. if (writepos >= safepos ||
  2643. gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
  2644. /* Cannot proceed until we've updated the superblock... */
  2645. wait_event(conf->wait_for_overlap,
  2646. atomic_read(&conf->reshape_stripes)==0);
  2647. mddev->reshape_position = conf->expand_progress;
  2648. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  2649. md_wakeup_thread(mddev->thread);
  2650. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  2651. kthread_should_stop());
  2652. spin_lock_irq(&conf->device_lock);
  2653. conf->expand_lo = mddev->reshape_position;
  2654. spin_unlock_irq(&conf->device_lock);
  2655. wake_up(&conf->wait_for_overlap);
  2656. }
  2657. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  2658. int j;
  2659. int skipped = 0;
  2660. pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
  2661. sh = get_active_stripe(conf, sector_nr+i,
  2662. conf->raid_disks, pd_idx, 0);
  2663. set_bit(STRIPE_EXPANDING, &sh->state);
  2664. atomic_inc(&conf->reshape_stripes);
  2665. /* If any of this stripe is beyond the end of the old
  2666. * array, then we need to zero those blocks
  2667. */
  2668. for (j=sh->disks; j--;) {
  2669. sector_t s;
  2670. if (j == sh->pd_idx)
  2671. continue;
  2672. s = compute_blocknr(sh, j);
  2673. if (s < (mddev->array_size<<1)) {
  2674. skipped = 1;
  2675. continue;
  2676. }
  2677. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  2678. set_bit(R5_Expanded, &sh->dev[j].flags);
  2679. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  2680. }
  2681. if (!skipped) {
  2682. set_bit(STRIPE_EXPAND_READY, &sh->state);
  2683. set_bit(STRIPE_HANDLE, &sh->state);
  2684. }
  2685. release_stripe(sh);
  2686. }
  2687. spin_lock_irq(&conf->device_lock);
  2688. conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
  2689. spin_unlock_irq(&conf->device_lock);
  2690. /* Ok, those stripe are ready. We can start scheduling
  2691. * reads on the source stripes.
  2692. * The source stripes are determined by mapping the first and last
  2693. * block on the destination stripes.
  2694. */
  2695. raid_disks = conf->previous_raid_disks;
  2696. data_disks = raid_disks - 1;
  2697. first_sector =
  2698. raid5_compute_sector(sector_nr*(conf->raid_disks-1),
  2699. raid_disks, data_disks,
  2700. &dd_idx, &pd_idx, conf);
  2701. last_sector =
  2702. raid5_compute_sector((sector_nr+conf->chunk_size/512)
  2703. *(conf->raid_disks-1) -1,
  2704. raid_disks, data_disks,
  2705. &dd_idx, &pd_idx, conf);
  2706. if (last_sector >= (mddev->size<<1))
  2707. last_sector = (mddev->size<<1)-1;
  2708. while (first_sector <= last_sector) {
  2709. pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
  2710. sh = get_active_stripe(conf, first_sector,
  2711. conf->previous_raid_disks, pd_idx, 0);
  2712. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2713. set_bit(STRIPE_HANDLE, &sh->state);
  2714. release_stripe(sh);
  2715. first_sector += STRIPE_SECTORS;
  2716. }
  2717. return conf->chunk_size>>9;
  2718. }
  2719. /* FIXME go_faster isn't used */
  2720. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  2721. {
  2722. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2723. struct stripe_head *sh;
  2724. int pd_idx;
  2725. int raid_disks = conf->raid_disks;
  2726. sector_t max_sector = mddev->size << 1;
  2727. int sync_blocks;
  2728. int still_degraded = 0;
  2729. int i;
  2730. if (sector_nr >= max_sector) {
  2731. /* just being told to finish up .. nothing much to do */
  2732. unplug_slaves(mddev);
  2733. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2734. end_reshape(conf);
  2735. return 0;
  2736. }
  2737. if (mddev->curr_resync < max_sector) /* aborted */
  2738. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2739. &sync_blocks, 1);
  2740. else /* completed sync */
  2741. conf->fullsync = 0;
  2742. bitmap_close_sync(mddev->bitmap);
  2743. return 0;
  2744. }
  2745. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2746. return reshape_request(mddev, sector_nr, skipped);
  2747. /* if there is too many failed drives and we are trying
  2748. * to resync, then assert that we are finished, because there is
  2749. * nothing we can do.
  2750. */
  2751. if (mddev->degraded >= conf->max_degraded &&
  2752. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2753. sector_t rv = (mddev->size << 1) - sector_nr;
  2754. *skipped = 1;
  2755. return rv;
  2756. }
  2757. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  2758. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2759. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  2760. /* we can skip this block, and probably more */
  2761. sync_blocks /= STRIPE_SECTORS;
  2762. *skipped = 1;
  2763. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  2764. }
  2765. pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
  2766. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
  2767. if (sh == NULL) {
  2768. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
  2769. /* make sure we don't swamp the stripe cache if someone else
  2770. * is trying to get access
  2771. */
  2772. schedule_timeout_uninterruptible(1);
  2773. }
  2774. /* Need to check if array will still be degraded after recovery/resync
  2775. * We don't need to check the 'failed' flag as when that gets set,
  2776. * recovery aborts.
  2777. */
  2778. for (i=0; i<mddev->raid_disks; i++)
  2779. if (conf->disks[i].rdev == NULL)
  2780. still_degraded = 1;
  2781. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  2782. spin_lock(&sh->lock);
  2783. set_bit(STRIPE_SYNCING, &sh->state);
  2784. clear_bit(STRIPE_INSYNC, &sh->state);
  2785. spin_unlock(&sh->lock);
  2786. handle_stripe(sh, NULL);
  2787. release_stripe(sh);
  2788. return STRIPE_SECTORS;
  2789. }
  2790. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  2791. {
  2792. /* We may not be able to submit a whole bio at once as there
  2793. * may not be enough stripe_heads available.
  2794. * We cannot pre-allocate enough stripe_heads as we may need
  2795. * more than exist in the cache (if we allow ever large chunks).
  2796. * So we do one stripe head at a time and record in
  2797. * ->bi_hw_segments how many have been done.
  2798. *
  2799. * We *know* that this entire raid_bio is in one chunk, so
  2800. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  2801. */
  2802. struct stripe_head *sh;
  2803. int dd_idx, pd_idx;
  2804. sector_t sector, logical_sector, last_sector;
  2805. int scnt = 0;
  2806. int remaining;
  2807. int handled = 0;
  2808. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  2809. sector = raid5_compute_sector( logical_sector,
  2810. conf->raid_disks,
  2811. conf->raid_disks - conf->max_degraded,
  2812. &dd_idx,
  2813. &pd_idx,
  2814. conf);
  2815. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  2816. for (; logical_sector < last_sector;
  2817. logical_sector += STRIPE_SECTORS,
  2818. sector += STRIPE_SECTORS,
  2819. scnt++) {
  2820. if (scnt < raid_bio->bi_hw_segments)
  2821. /* already done this stripe */
  2822. continue;
  2823. sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
  2824. if (!sh) {
  2825. /* failed to get a stripe - must wait */
  2826. raid_bio->bi_hw_segments = scnt;
  2827. conf->retry_read_aligned = raid_bio;
  2828. return handled;
  2829. }
  2830. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  2831. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  2832. release_stripe(sh);
  2833. raid_bio->bi_hw_segments = scnt;
  2834. conf->retry_read_aligned = raid_bio;
  2835. return handled;
  2836. }
  2837. handle_stripe(sh, NULL);
  2838. release_stripe(sh);
  2839. handled++;
  2840. }
  2841. spin_lock_irq(&conf->device_lock);
  2842. remaining = --raid_bio->bi_phys_segments;
  2843. spin_unlock_irq(&conf->device_lock);
  2844. if (remaining == 0) {
  2845. int bytes = raid_bio->bi_size;
  2846. raid_bio->bi_size = 0;
  2847. raid_bio->bi_end_io(raid_bio, bytes,
  2848. test_bit(BIO_UPTODATE, &raid_bio->bi_flags)
  2849. ? 0 : -EIO);
  2850. }
  2851. if (atomic_dec_and_test(&conf->active_aligned_reads))
  2852. wake_up(&conf->wait_for_stripe);
  2853. return handled;
  2854. }
  2855. /*
  2856. * This is our raid5 kernel thread.
  2857. *
  2858. * We scan the hash table for stripes which can be handled now.
  2859. * During the scan, completed stripes are saved for us by the interrupt
  2860. * handler, so that they will not have to wait for our next wakeup.
  2861. */
  2862. static void raid5d (mddev_t *mddev)
  2863. {
  2864. struct stripe_head *sh;
  2865. raid5_conf_t *conf = mddev_to_conf(mddev);
  2866. int handled;
  2867. PRINTK("+++ raid5d active\n");
  2868. md_check_recovery(mddev);
  2869. handled = 0;
  2870. spin_lock_irq(&conf->device_lock);
  2871. while (1) {
  2872. struct list_head *first;
  2873. struct bio *bio;
  2874. if (conf->seq_flush != conf->seq_write) {
  2875. int seq = conf->seq_flush;
  2876. spin_unlock_irq(&conf->device_lock);
  2877. bitmap_unplug(mddev->bitmap);
  2878. spin_lock_irq(&conf->device_lock);
  2879. conf->seq_write = seq;
  2880. activate_bit_delay(conf);
  2881. }
  2882. if (list_empty(&conf->handle_list) &&
  2883. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  2884. !blk_queue_plugged(mddev->queue) &&
  2885. !list_empty(&conf->delayed_list))
  2886. raid5_activate_delayed(conf);
  2887. while ((bio = remove_bio_from_retry(conf))) {
  2888. int ok;
  2889. spin_unlock_irq(&conf->device_lock);
  2890. ok = retry_aligned_read(conf, bio);
  2891. spin_lock_irq(&conf->device_lock);
  2892. if (!ok)
  2893. break;
  2894. handled++;
  2895. }
  2896. if (list_empty(&conf->handle_list))
  2897. break;
  2898. first = conf->handle_list.next;
  2899. sh = list_entry(first, struct stripe_head, lru);
  2900. list_del_init(first);
  2901. atomic_inc(&sh->count);
  2902. BUG_ON(atomic_read(&sh->count)!= 1);
  2903. spin_unlock_irq(&conf->device_lock);
  2904. handled++;
  2905. handle_stripe(sh, conf->spare_page);
  2906. release_stripe(sh);
  2907. spin_lock_irq(&conf->device_lock);
  2908. }
  2909. PRINTK("%d stripes handled\n", handled);
  2910. spin_unlock_irq(&conf->device_lock);
  2911. unplug_slaves(mddev);
  2912. PRINTK("--- raid5d inactive\n");
  2913. }
  2914. static ssize_t
  2915. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  2916. {
  2917. raid5_conf_t *conf = mddev_to_conf(mddev);
  2918. if (conf)
  2919. return sprintf(page, "%d\n", conf->max_nr_stripes);
  2920. else
  2921. return 0;
  2922. }
  2923. static ssize_t
  2924. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  2925. {
  2926. raid5_conf_t *conf = mddev_to_conf(mddev);
  2927. char *end;
  2928. int new;
  2929. if (len >= PAGE_SIZE)
  2930. return -EINVAL;
  2931. if (!conf)
  2932. return -ENODEV;
  2933. new = simple_strtoul(page, &end, 10);
  2934. if (!*page || (*end && *end != '\n') )
  2935. return -EINVAL;
  2936. if (new <= 16 || new > 32768)
  2937. return -EINVAL;
  2938. while (new < conf->max_nr_stripes) {
  2939. if (drop_one_stripe(conf))
  2940. conf->max_nr_stripes--;
  2941. else
  2942. break;
  2943. }
  2944. md_allow_write(mddev);
  2945. while (new > conf->max_nr_stripes) {
  2946. if (grow_one_stripe(conf))
  2947. conf->max_nr_stripes++;
  2948. else break;
  2949. }
  2950. return len;
  2951. }
  2952. static struct md_sysfs_entry
  2953. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  2954. raid5_show_stripe_cache_size,
  2955. raid5_store_stripe_cache_size);
  2956. static ssize_t
  2957. stripe_cache_active_show(mddev_t *mddev, char *page)
  2958. {
  2959. raid5_conf_t *conf = mddev_to_conf(mddev);
  2960. if (conf)
  2961. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  2962. else
  2963. return 0;
  2964. }
  2965. static struct md_sysfs_entry
  2966. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  2967. static struct attribute *raid5_attrs[] = {
  2968. &raid5_stripecache_size.attr,
  2969. &raid5_stripecache_active.attr,
  2970. NULL,
  2971. };
  2972. static struct attribute_group raid5_attrs_group = {
  2973. .name = NULL,
  2974. .attrs = raid5_attrs,
  2975. };
  2976. static int run(mddev_t *mddev)
  2977. {
  2978. raid5_conf_t *conf;
  2979. int raid_disk, memory;
  2980. mdk_rdev_t *rdev;
  2981. struct disk_info *disk;
  2982. struct list_head *tmp;
  2983. int working_disks = 0;
  2984. if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
  2985. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  2986. mdname(mddev), mddev->level);
  2987. return -EIO;
  2988. }
  2989. if (mddev->reshape_position != MaxSector) {
  2990. /* Check that we can continue the reshape.
  2991. * Currently only disks can change, it must
  2992. * increase, and we must be past the point where
  2993. * a stripe over-writes itself
  2994. */
  2995. sector_t here_new, here_old;
  2996. int old_disks;
  2997. if (mddev->new_level != mddev->level ||
  2998. mddev->new_layout != mddev->layout ||
  2999. mddev->new_chunk != mddev->chunk_size) {
  3000. printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
  3001. mdname(mddev));
  3002. return -EINVAL;
  3003. }
  3004. if (mddev->delta_disks <= 0) {
  3005. printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
  3006. mdname(mddev));
  3007. return -EINVAL;
  3008. }
  3009. old_disks = mddev->raid_disks - mddev->delta_disks;
  3010. /* reshape_position must be on a new-stripe boundary, and one
  3011. * further up in new geometry must map after here in old geometry.
  3012. */
  3013. here_new = mddev->reshape_position;
  3014. if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
  3015. printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
  3016. return -EINVAL;
  3017. }
  3018. /* here_new is the stripe we will write to */
  3019. here_old = mddev->reshape_position;
  3020. sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
  3021. /* here_old is the first stripe that we might need to read from */
  3022. if (here_new >= here_old) {
  3023. /* Reading from the same stripe as writing to - bad */
  3024. printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
  3025. return -EINVAL;
  3026. }
  3027. printk(KERN_INFO "raid5: reshape will continue\n");
  3028. /* OK, we should be able to continue; */
  3029. }
  3030. mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
  3031. if ((conf = mddev->private) == NULL)
  3032. goto abort;
  3033. if (mddev->reshape_position == MaxSector) {
  3034. conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
  3035. } else {
  3036. conf->raid_disks = mddev->raid_disks;
  3037. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  3038. }
  3039. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  3040. GFP_KERNEL);
  3041. if (!conf->disks)
  3042. goto abort;
  3043. conf->mddev = mddev;
  3044. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  3045. goto abort;
  3046. if (mddev->level == 6) {
  3047. conf->spare_page = alloc_page(GFP_KERNEL);
  3048. if (!conf->spare_page)
  3049. goto abort;
  3050. }
  3051. spin_lock_init(&conf->device_lock);
  3052. init_waitqueue_head(&conf->wait_for_stripe);
  3053. init_waitqueue_head(&conf->wait_for_overlap);
  3054. INIT_LIST_HEAD(&conf->handle_list);
  3055. INIT_LIST_HEAD(&conf->delayed_list);
  3056. INIT_LIST_HEAD(&conf->bitmap_list);
  3057. INIT_LIST_HEAD(&conf->inactive_list);
  3058. atomic_set(&conf->active_stripes, 0);
  3059. atomic_set(&conf->preread_active_stripes, 0);
  3060. atomic_set(&conf->active_aligned_reads, 0);
  3061. PRINTK("raid5: run(%s) called.\n", mdname(mddev));
  3062. ITERATE_RDEV(mddev,rdev,tmp) {
  3063. raid_disk = rdev->raid_disk;
  3064. if (raid_disk >= conf->raid_disks
  3065. || raid_disk < 0)
  3066. continue;
  3067. disk = conf->disks + raid_disk;
  3068. disk->rdev = rdev;
  3069. if (test_bit(In_sync, &rdev->flags)) {
  3070. char b[BDEVNAME_SIZE];
  3071. printk(KERN_INFO "raid5: device %s operational as raid"
  3072. " disk %d\n", bdevname(rdev->bdev,b),
  3073. raid_disk);
  3074. working_disks++;
  3075. }
  3076. }
  3077. /*
  3078. * 0 for a fully functional array, 1 or 2 for a degraded array.
  3079. */
  3080. mddev->degraded = conf->raid_disks - working_disks;
  3081. conf->mddev = mddev;
  3082. conf->chunk_size = mddev->chunk_size;
  3083. conf->level = mddev->level;
  3084. if (conf->level == 6)
  3085. conf->max_degraded = 2;
  3086. else
  3087. conf->max_degraded = 1;
  3088. conf->algorithm = mddev->layout;
  3089. conf->max_nr_stripes = NR_STRIPES;
  3090. conf->expand_progress = mddev->reshape_position;
  3091. /* device size must be a multiple of chunk size */
  3092. mddev->size &= ~(mddev->chunk_size/1024 -1);
  3093. mddev->resync_max_sectors = mddev->size << 1;
  3094. if (conf->level == 6 && conf->raid_disks < 4) {
  3095. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  3096. mdname(mddev), conf->raid_disks);
  3097. goto abort;
  3098. }
  3099. if (!conf->chunk_size || conf->chunk_size % 4) {
  3100. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  3101. conf->chunk_size, mdname(mddev));
  3102. goto abort;
  3103. }
  3104. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  3105. printk(KERN_ERR
  3106. "raid5: unsupported parity algorithm %d for %s\n",
  3107. conf->algorithm, mdname(mddev));
  3108. goto abort;
  3109. }
  3110. if (mddev->degraded > conf->max_degraded) {
  3111. printk(KERN_ERR "raid5: not enough operational devices for %s"
  3112. " (%d/%d failed)\n",
  3113. mdname(mddev), mddev->degraded, conf->raid_disks);
  3114. goto abort;
  3115. }
  3116. if (mddev->degraded > 0 &&
  3117. mddev->recovery_cp != MaxSector) {
  3118. if (mddev->ok_start_degraded)
  3119. printk(KERN_WARNING
  3120. "raid5: starting dirty degraded array: %s"
  3121. "- data corruption possible.\n",
  3122. mdname(mddev));
  3123. else {
  3124. printk(KERN_ERR
  3125. "raid5: cannot start dirty degraded array for %s\n",
  3126. mdname(mddev));
  3127. goto abort;
  3128. }
  3129. }
  3130. {
  3131. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  3132. if (!mddev->thread) {
  3133. printk(KERN_ERR
  3134. "raid5: couldn't allocate thread for %s\n",
  3135. mdname(mddev));
  3136. goto abort;
  3137. }
  3138. }
  3139. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  3140. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  3141. if (grow_stripes(conf, conf->max_nr_stripes)) {
  3142. printk(KERN_ERR
  3143. "raid5: couldn't allocate %dkB for buffers\n", memory);
  3144. shrink_stripes(conf);
  3145. md_unregister_thread(mddev->thread);
  3146. goto abort;
  3147. } else
  3148. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  3149. memory, mdname(mddev));
  3150. if (mddev->degraded == 0)
  3151. printk("raid5: raid level %d set %s active with %d out of %d"
  3152. " devices, algorithm %d\n", conf->level, mdname(mddev),
  3153. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  3154. conf->algorithm);
  3155. else
  3156. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  3157. " out of %d devices, algorithm %d\n", conf->level,
  3158. mdname(mddev), mddev->raid_disks - mddev->degraded,
  3159. mddev->raid_disks, conf->algorithm);
  3160. print_raid5_conf(conf);
  3161. if (conf->expand_progress != MaxSector) {
  3162. printk("...ok start reshape thread\n");
  3163. conf->expand_lo = conf->expand_progress;
  3164. atomic_set(&conf->reshape_stripes, 0);
  3165. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3166. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3167. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3168. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3169. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3170. "%s_reshape");
  3171. }
  3172. /* read-ahead size must cover two whole stripes, which is
  3173. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3174. */
  3175. {
  3176. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3177. int stripe = data_disks *
  3178. (mddev->chunk_size / PAGE_SIZE);
  3179. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3180. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3181. }
  3182. /* Ok, everything is just fine now */
  3183. sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
  3184. mddev->queue->unplug_fn = raid5_unplug_device;
  3185. mddev->queue->issue_flush_fn = raid5_issue_flush;
  3186. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  3187. mddev->queue->backing_dev_info.congested_data = mddev;
  3188. mddev->array_size = mddev->size * (conf->previous_raid_disks -
  3189. conf->max_degraded);
  3190. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  3191. return 0;
  3192. abort:
  3193. if (conf) {
  3194. print_raid5_conf(conf);
  3195. safe_put_page(conf->spare_page);
  3196. kfree(conf->disks);
  3197. kfree(conf->stripe_hashtbl);
  3198. kfree(conf);
  3199. }
  3200. mddev->private = NULL;
  3201. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  3202. return -EIO;
  3203. }
  3204. static int stop(mddev_t *mddev)
  3205. {
  3206. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3207. md_unregister_thread(mddev->thread);
  3208. mddev->thread = NULL;
  3209. shrink_stripes(conf);
  3210. kfree(conf->stripe_hashtbl);
  3211. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3212. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  3213. kfree(conf->disks);
  3214. kfree(conf);
  3215. mddev->private = NULL;
  3216. return 0;
  3217. }
  3218. #if RAID5_DEBUG
  3219. static void print_sh (struct seq_file *seq, struct stripe_head *sh)
  3220. {
  3221. int i;
  3222. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  3223. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  3224. seq_printf(seq, "sh %llu, count %d.\n",
  3225. (unsigned long long)sh->sector, atomic_read(&sh->count));
  3226. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  3227. for (i = 0; i < sh->disks; i++) {
  3228. seq_printf(seq, "(cache%d: %p %ld) ",
  3229. i, sh->dev[i].page, sh->dev[i].flags);
  3230. }
  3231. seq_printf(seq, "\n");
  3232. }
  3233. static void printall (struct seq_file *seq, raid5_conf_t *conf)
  3234. {
  3235. struct stripe_head *sh;
  3236. struct hlist_node *hn;
  3237. int i;
  3238. spin_lock_irq(&conf->device_lock);
  3239. for (i = 0; i < NR_HASH; i++) {
  3240. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  3241. if (sh->raid_conf != conf)
  3242. continue;
  3243. print_sh(seq, sh);
  3244. }
  3245. }
  3246. spin_unlock_irq(&conf->device_lock);
  3247. }
  3248. #endif
  3249. static void status (struct seq_file *seq, mddev_t *mddev)
  3250. {
  3251. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3252. int i;
  3253. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  3254. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  3255. for (i = 0; i < conf->raid_disks; i++)
  3256. seq_printf (seq, "%s",
  3257. conf->disks[i].rdev &&
  3258. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  3259. seq_printf (seq, "]");
  3260. #if RAID5_DEBUG
  3261. seq_printf (seq, "\n");
  3262. printall(seq, conf);
  3263. #endif
  3264. }
  3265. static void print_raid5_conf (raid5_conf_t *conf)
  3266. {
  3267. int i;
  3268. struct disk_info *tmp;
  3269. printk("RAID5 conf printout:\n");
  3270. if (!conf) {
  3271. printk("(conf==NULL)\n");
  3272. return;
  3273. }
  3274. printk(" --- rd:%d wd:%d\n", conf->raid_disks,
  3275. conf->raid_disks - conf->mddev->degraded);
  3276. for (i = 0; i < conf->raid_disks; i++) {
  3277. char b[BDEVNAME_SIZE];
  3278. tmp = conf->disks + i;
  3279. if (tmp->rdev)
  3280. printk(" disk %d, o:%d, dev:%s\n",
  3281. i, !test_bit(Faulty, &tmp->rdev->flags),
  3282. bdevname(tmp->rdev->bdev,b));
  3283. }
  3284. }
  3285. static int raid5_spare_active(mddev_t *mddev)
  3286. {
  3287. int i;
  3288. raid5_conf_t *conf = mddev->private;
  3289. struct disk_info *tmp;
  3290. for (i = 0; i < conf->raid_disks; i++) {
  3291. tmp = conf->disks + i;
  3292. if (tmp->rdev
  3293. && !test_bit(Faulty, &tmp->rdev->flags)
  3294. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  3295. unsigned long flags;
  3296. spin_lock_irqsave(&conf->device_lock, flags);
  3297. mddev->degraded--;
  3298. spin_unlock_irqrestore(&conf->device_lock, flags);
  3299. }
  3300. }
  3301. print_raid5_conf(conf);
  3302. return 0;
  3303. }
  3304. static int raid5_remove_disk(mddev_t *mddev, int number)
  3305. {
  3306. raid5_conf_t *conf = mddev->private;
  3307. int err = 0;
  3308. mdk_rdev_t *rdev;
  3309. struct disk_info *p = conf->disks + number;
  3310. print_raid5_conf(conf);
  3311. rdev = p->rdev;
  3312. if (rdev) {
  3313. if (test_bit(In_sync, &rdev->flags) ||
  3314. atomic_read(&rdev->nr_pending)) {
  3315. err = -EBUSY;
  3316. goto abort;
  3317. }
  3318. p->rdev = NULL;
  3319. synchronize_rcu();
  3320. if (atomic_read(&rdev->nr_pending)) {
  3321. /* lost the race, try later */
  3322. err = -EBUSY;
  3323. p->rdev = rdev;
  3324. }
  3325. }
  3326. abort:
  3327. print_raid5_conf(conf);
  3328. return err;
  3329. }
  3330. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  3331. {
  3332. raid5_conf_t *conf = mddev->private;
  3333. int found = 0;
  3334. int disk;
  3335. struct disk_info *p;
  3336. if (mddev->degraded > conf->max_degraded)
  3337. /* no point adding a device */
  3338. return 0;
  3339. /*
  3340. * find the disk ... but prefer rdev->saved_raid_disk
  3341. * if possible.
  3342. */
  3343. if (rdev->saved_raid_disk >= 0 &&
  3344. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  3345. disk = rdev->saved_raid_disk;
  3346. else
  3347. disk = 0;
  3348. for ( ; disk < conf->raid_disks; disk++)
  3349. if ((p=conf->disks + disk)->rdev == NULL) {
  3350. clear_bit(In_sync, &rdev->flags);
  3351. rdev->raid_disk = disk;
  3352. found = 1;
  3353. if (rdev->saved_raid_disk != disk)
  3354. conf->fullsync = 1;
  3355. rcu_assign_pointer(p->rdev, rdev);
  3356. break;
  3357. }
  3358. print_raid5_conf(conf);
  3359. return found;
  3360. }
  3361. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  3362. {
  3363. /* no resync is happening, and there is enough space
  3364. * on all devices, so we can resize.
  3365. * We need to make sure resync covers any new space.
  3366. * If the array is shrinking we should possibly wait until
  3367. * any io in the removed space completes, but it hardly seems
  3368. * worth it.
  3369. */
  3370. raid5_conf_t *conf = mddev_to_conf(mddev);
  3371. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  3372. mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
  3373. set_capacity(mddev->gendisk, mddev->array_size << 1);
  3374. mddev->changed = 1;
  3375. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  3376. mddev->recovery_cp = mddev->size << 1;
  3377. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3378. }
  3379. mddev->size = sectors /2;
  3380. mddev->resync_max_sectors = sectors;
  3381. return 0;
  3382. }
  3383. #ifdef CONFIG_MD_RAID5_RESHAPE
  3384. static int raid5_check_reshape(mddev_t *mddev)
  3385. {
  3386. raid5_conf_t *conf = mddev_to_conf(mddev);
  3387. int err;
  3388. if (mddev->delta_disks < 0 ||
  3389. mddev->new_level != mddev->level)
  3390. return -EINVAL; /* Cannot shrink array or change level yet */
  3391. if (mddev->delta_disks == 0)
  3392. return 0; /* nothing to do */
  3393. /* Can only proceed if there are plenty of stripe_heads.
  3394. * We need a minimum of one full stripe,, and for sensible progress
  3395. * it is best to have about 4 times that.
  3396. * If we require 4 times, then the default 256 4K stripe_heads will
  3397. * allow for chunk sizes up to 256K, which is probably OK.
  3398. * If the chunk size is greater, user-space should request more
  3399. * stripe_heads first.
  3400. */
  3401. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  3402. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  3403. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  3404. (mddev->chunk_size / STRIPE_SIZE)*4);
  3405. return -ENOSPC;
  3406. }
  3407. err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  3408. if (err)
  3409. return err;
  3410. /* looks like we might be able to manage this */
  3411. return 0;
  3412. }
  3413. static int raid5_start_reshape(mddev_t *mddev)
  3414. {
  3415. raid5_conf_t *conf = mddev_to_conf(mddev);
  3416. mdk_rdev_t *rdev;
  3417. struct list_head *rtmp;
  3418. int spares = 0;
  3419. int added_devices = 0;
  3420. unsigned long flags;
  3421. if (mddev->degraded ||
  3422. test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3423. return -EBUSY;
  3424. ITERATE_RDEV(mddev, rdev, rtmp)
  3425. if (rdev->raid_disk < 0 &&
  3426. !test_bit(Faulty, &rdev->flags))
  3427. spares++;
  3428. if (spares < mddev->delta_disks-1)
  3429. /* Not enough devices even to make a degraded array
  3430. * of that size
  3431. */
  3432. return -EINVAL;
  3433. atomic_set(&conf->reshape_stripes, 0);
  3434. spin_lock_irq(&conf->device_lock);
  3435. conf->previous_raid_disks = conf->raid_disks;
  3436. conf->raid_disks += mddev->delta_disks;
  3437. conf->expand_progress = 0;
  3438. conf->expand_lo = 0;
  3439. spin_unlock_irq(&conf->device_lock);
  3440. /* Add some new drives, as many as will fit.
  3441. * We know there are enough to make the newly sized array work.
  3442. */
  3443. ITERATE_RDEV(mddev, rdev, rtmp)
  3444. if (rdev->raid_disk < 0 &&
  3445. !test_bit(Faulty, &rdev->flags)) {
  3446. if (raid5_add_disk(mddev, rdev)) {
  3447. char nm[20];
  3448. set_bit(In_sync, &rdev->flags);
  3449. added_devices++;
  3450. rdev->recovery_offset = 0;
  3451. sprintf(nm, "rd%d", rdev->raid_disk);
  3452. sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
  3453. } else
  3454. break;
  3455. }
  3456. spin_lock_irqsave(&conf->device_lock, flags);
  3457. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
  3458. spin_unlock_irqrestore(&conf->device_lock, flags);
  3459. mddev->raid_disks = conf->raid_disks;
  3460. mddev->reshape_position = 0;
  3461. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3462. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3463. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3464. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3465. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3466. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3467. "%s_reshape");
  3468. if (!mddev->sync_thread) {
  3469. mddev->recovery = 0;
  3470. spin_lock_irq(&conf->device_lock);
  3471. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  3472. conf->expand_progress = MaxSector;
  3473. spin_unlock_irq(&conf->device_lock);
  3474. return -EAGAIN;
  3475. }
  3476. md_wakeup_thread(mddev->sync_thread);
  3477. md_new_event(mddev);
  3478. return 0;
  3479. }
  3480. #endif
  3481. static void end_reshape(raid5_conf_t *conf)
  3482. {
  3483. struct block_device *bdev;
  3484. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  3485. conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
  3486. set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
  3487. conf->mddev->changed = 1;
  3488. bdev = bdget_disk(conf->mddev->gendisk, 0);
  3489. if (bdev) {
  3490. mutex_lock(&bdev->bd_inode->i_mutex);
  3491. i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
  3492. mutex_unlock(&bdev->bd_inode->i_mutex);
  3493. bdput(bdev);
  3494. }
  3495. spin_lock_irq(&conf->device_lock);
  3496. conf->expand_progress = MaxSector;
  3497. spin_unlock_irq(&conf->device_lock);
  3498. conf->mddev->reshape_position = MaxSector;
  3499. /* read-ahead size must cover two whole stripes, which is
  3500. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3501. */
  3502. {
  3503. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3504. int stripe = data_disks *
  3505. (conf->mddev->chunk_size / PAGE_SIZE);
  3506. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3507. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3508. }
  3509. }
  3510. }
  3511. static void raid5_quiesce(mddev_t *mddev, int state)
  3512. {
  3513. raid5_conf_t *conf = mddev_to_conf(mddev);
  3514. switch(state) {
  3515. case 2: /* resume for a suspend */
  3516. wake_up(&conf->wait_for_overlap);
  3517. break;
  3518. case 1: /* stop all writes */
  3519. spin_lock_irq(&conf->device_lock);
  3520. conf->quiesce = 1;
  3521. wait_event_lock_irq(conf->wait_for_stripe,
  3522. atomic_read(&conf->active_stripes) == 0 &&
  3523. atomic_read(&conf->active_aligned_reads) == 0,
  3524. conf->device_lock, /* nothing */);
  3525. spin_unlock_irq(&conf->device_lock);
  3526. break;
  3527. case 0: /* re-enable writes */
  3528. spin_lock_irq(&conf->device_lock);
  3529. conf->quiesce = 0;
  3530. wake_up(&conf->wait_for_stripe);
  3531. wake_up(&conf->wait_for_overlap);
  3532. spin_unlock_irq(&conf->device_lock);
  3533. break;
  3534. }
  3535. }
  3536. static struct mdk_personality raid6_personality =
  3537. {
  3538. .name = "raid6",
  3539. .level = 6,
  3540. .owner = THIS_MODULE,
  3541. .make_request = make_request,
  3542. .run = run,
  3543. .stop = stop,
  3544. .status = status,
  3545. .error_handler = error,
  3546. .hot_add_disk = raid5_add_disk,
  3547. .hot_remove_disk= raid5_remove_disk,
  3548. .spare_active = raid5_spare_active,
  3549. .sync_request = sync_request,
  3550. .resize = raid5_resize,
  3551. .quiesce = raid5_quiesce,
  3552. };
  3553. static struct mdk_personality raid5_personality =
  3554. {
  3555. .name = "raid5",
  3556. .level = 5,
  3557. .owner = THIS_MODULE,
  3558. .make_request = make_request,
  3559. .run = run,
  3560. .stop = stop,
  3561. .status = status,
  3562. .error_handler = error,
  3563. .hot_add_disk = raid5_add_disk,
  3564. .hot_remove_disk= raid5_remove_disk,
  3565. .spare_active = raid5_spare_active,
  3566. .sync_request = sync_request,
  3567. .resize = raid5_resize,
  3568. #ifdef CONFIG_MD_RAID5_RESHAPE
  3569. .check_reshape = raid5_check_reshape,
  3570. .start_reshape = raid5_start_reshape,
  3571. #endif
  3572. .quiesce = raid5_quiesce,
  3573. };
  3574. static struct mdk_personality raid4_personality =
  3575. {
  3576. .name = "raid4",
  3577. .level = 4,
  3578. .owner = THIS_MODULE,
  3579. .make_request = make_request,
  3580. .run = run,
  3581. .stop = stop,
  3582. .status = status,
  3583. .error_handler = error,
  3584. .hot_add_disk = raid5_add_disk,
  3585. .hot_remove_disk= raid5_remove_disk,
  3586. .spare_active = raid5_spare_active,
  3587. .sync_request = sync_request,
  3588. .resize = raid5_resize,
  3589. .quiesce = raid5_quiesce,
  3590. };
  3591. static int __init raid5_init(void)
  3592. {
  3593. int e;
  3594. e = raid6_select_algo();
  3595. if ( e )
  3596. return e;
  3597. register_md_personality(&raid6_personality);
  3598. register_md_personality(&raid5_personality);
  3599. register_md_personality(&raid4_personality);
  3600. return 0;
  3601. }
  3602. static void raid5_exit(void)
  3603. {
  3604. unregister_md_personality(&raid6_personality);
  3605. unregister_md_personality(&raid5_personality);
  3606. unregister_md_personality(&raid4_personality);
  3607. }
  3608. module_init(raid5_init);
  3609. module_exit(raid5_exit);
  3610. MODULE_LICENSE("GPL");
  3611. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  3612. MODULE_ALIAS("md-raid5");
  3613. MODULE_ALIAS("md-raid4");
  3614. MODULE_ALIAS("md-level-5");
  3615. MODULE_ALIAS("md-level-4");
  3616. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  3617. MODULE_ALIAS("md-raid6");
  3618. MODULE_ALIAS("md-level-6");
  3619. /* This used to be two separate modules, they were: */
  3620. MODULE_ALIAS("raid5");
  3621. MODULE_ALIAS("raid6");