raid5.c 133 KB

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