raid5.c 165 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941
  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/blkdev.h>
  45. #include <linux/kthread.h>
  46. #include <linux/raid/pq.h>
  47. #include <linux/async_tx.h>
  48. #include <linux/async.h>
  49. #include <linux/seq_file.h>
  50. #include <linux/cpu.h>
  51. #include "md.h"
  52. #include "raid5.h"
  53. #include "raid0.h"
  54. #include "bitmap.h"
  55. /*
  56. * Stripe cache
  57. */
  58. #define NR_STRIPES 256
  59. #define STRIPE_SIZE PAGE_SIZE
  60. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  61. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  62. #define IO_THRESHOLD 1
  63. #define BYPASS_THRESHOLD 1
  64. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  65. #define HASH_MASK (NR_HASH - 1)
  66. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  67. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  68. * order without overlap. There may be several bio's per stripe+device, and
  69. * a bio could span several devices.
  70. * When walking this list for a particular stripe+device, we must never proceed
  71. * beyond a bio that extends past this device, as the next bio might no longer
  72. * be valid.
  73. * This macro is used to determine the 'next' bio in the list, given the sector
  74. * of the current stripe+device
  75. */
  76. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  77. /*
  78. * The following can be used to debug the driver
  79. */
  80. #define RAID5_PARANOIA 1
  81. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  82. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  83. #else
  84. # define CHECK_DEVLOCK()
  85. #endif
  86. #ifdef DEBUG
  87. #define inline
  88. #define __inline__
  89. #endif
  90. #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
  91. /*
  92. * We maintain a biased count of active stripes in the bottom 16 bits of
  93. * bi_phys_segments, and a count of processed stripes in the upper 16 bits
  94. */
  95. static inline int raid5_bi_phys_segments(struct bio *bio)
  96. {
  97. return bio->bi_phys_segments & 0xffff;
  98. }
  99. static inline int raid5_bi_hw_segments(struct bio *bio)
  100. {
  101. return (bio->bi_phys_segments >> 16) & 0xffff;
  102. }
  103. static inline int raid5_dec_bi_phys_segments(struct bio *bio)
  104. {
  105. --bio->bi_phys_segments;
  106. return raid5_bi_phys_segments(bio);
  107. }
  108. static inline int raid5_dec_bi_hw_segments(struct bio *bio)
  109. {
  110. unsigned short val = raid5_bi_hw_segments(bio);
  111. --val;
  112. bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
  113. return val;
  114. }
  115. static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
  116. {
  117. bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
  118. }
  119. /* Find first data disk in a raid6 stripe */
  120. static inline int raid6_d0(struct stripe_head *sh)
  121. {
  122. if (sh->ddf_layout)
  123. /* ddf always start from first device */
  124. return 0;
  125. /* md starts just after Q block */
  126. if (sh->qd_idx == sh->disks - 1)
  127. return 0;
  128. else
  129. return sh->qd_idx + 1;
  130. }
  131. static inline int raid6_next_disk(int disk, int raid_disks)
  132. {
  133. disk++;
  134. return (disk < raid_disks) ? disk : 0;
  135. }
  136. /* When walking through the disks in a raid5, starting at raid6_d0,
  137. * We need to map each disk to a 'slot', where the data disks are slot
  138. * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
  139. * is raid_disks-1. This help does that mapping.
  140. */
  141. static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
  142. int *count, int syndrome_disks)
  143. {
  144. int slot = *count;
  145. if (sh->ddf_layout)
  146. (*count)++;
  147. if (idx == sh->pd_idx)
  148. return syndrome_disks;
  149. if (idx == sh->qd_idx)
  150. return syndrome_disks + 1;
  151. if (!sh->ddf_layout)
  152. (*count)++;
  153. return slot;
  154. }
  155. static void return_io(struct bio *return_bi)
  156. {
  157. struct bio *bi = return_bi;
  158. while (bi) {
  159. return_bi = bi->bi_next;
  160. bi->bi_next = NULL;
  161. bi->bi_size = 0;
  162. bio_endio(bi, 0);
  163. bi = return_bi;
  164. }
  165. }
  166. static void print_raid5_conf (raid5_conf_t *conf);
  167. static int stripe_operations_active(struct stripe_head *sh)
  168. {
  169. return sh->check_state || sh->reconstruct_state ||
  170. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  171. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  172. }
  173. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  174. {
  175. if (atomic_dec_and_test(&sh->count)) {
  176. BUG_ON(!list_empty(&sh->lru));
  177. BUG_ON(atomic_read(&conf->active_stripes)==0);
  178. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  179. if (test_bit(STRIPE_DELAYED, &sh->state)) {
  180. list_add_tail(&sh->lru, &conf->delayed_list);
  181. blk_plug_device(conf->mddev->queue);
  182. } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  183. sh->bm_seq - conf->seq_write > 0) {
  184. list_add_tail(&sh->lru, &conf->bitmap_list);
  185. blk_plug_device(conf->mddev->queue);
  186. } else {
  187. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  188. list_add_tail(&sh->lru, &conf->handle_list);
  189. }
  190. md_wakeup_thread(conf->mddev->thread);
  191. } else {
  192. BUG_ON(stripe_operations_active(sh));
  193. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  194. atomic_dec(&conf->preread_active_stripes);
  195. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  196. md_wakeup_thread(conf->mddev->thread);
  197. }
  198. atomic_dec(&conf->active_stripes);
  199. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  200. list_add_tail(&sh->lru, &conf->inactive_list);
  201. wake_up(&conf->wait_for_stripe);
  202. if (conf->retry_read_aligned)
  203. md_wakeup_thread(conf->mddev->thread);
  204. }
  205. }
  206. }
  207. }
  208. static void release_stripe(struct stripe_head *sh)
  209. {
  210. raid5_conf_t *conf = sh->raid_conf;
  211. unsigned long flags;
  212. spin_lock_irqsave(&conf->device_lock, flags);
  213. __release_stripe(conf, sh);
  214. spin_unlock_irqrestore(&conf->device_lock, flags);
  215. }
  216. static inline void remove_hash(struct stripe_head *sh)
  217. {
  218. pr_debug("remove_hash(), stripe %llu\n",
  219. (unsigned long long)sh->sector);
  220. hlist_del_init(&sh->hash);
  221. }
  222. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  223. {
  224. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  225. pr_debug("insert_hash(), stripe %llu\n",
  226. (unsigned long long)sh->sector);
  227. CHECK_DEVLOCK();
  228. hlist_add_head(&sh->hash, hp);
  229. }
  230. /* find an idle stripe, make sure it is unhashed, and return it. */
  231. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  232. {
  233. struct stripe_head *sh = NULL;
  234. struct list_head *first;
  235. CHECK_DEVLOCK();
  236. if (list_empty(&conf->inactive_list))
  237. goto out;
  238. first = conf->inactive_list.next;
  239. sh = list_entry(first, struct stripe_head, lru);
  240. list_del_init(first);
  241. remove_hash(sh);
  242. atomic_inc(&conf->active_stripes);
  243. out:
  244. return sh;
  245. }
  246. static void shrink_buffers(struct stripe_head *sh, int num)
  247. {
  248. struct page *p;
  249. int i;
  250. for (i=0; i<num ; i++) {
  251. p = sh->dev[i].page;
  252. if (!p)
  253. continue;
  254. sh->dev[i].page = NULL;
  255. put_page(p);
  256. }
  257. }
  258. static int grow_buffers(struct stripe_head *sh, int num)
  259. {
  260. int i;
  261. for (i=0; i<num; i++) {
  262. struct page *page;
  263. if (!(page = alloc_page(GFP_KERNEL))) {
  264. return 1;
  265. }
  266. sh->dev[i].page = page;
  267. }
  268. return 0;
  269. }
  270. static void raid5_build_block(struct stripe_head *sh, int i, int previous);
  271. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  272. struct stripe_head *sh);
  273. static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
  274. {
  275. raid5_conf_t *conf = sh->raid_conf;
  276. int i;
  277. BUG_ON(atomic_read(&sh->count) != 0);
  278. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  279. BUG_ON(stripe_operations_active(sh));
  280. CHECK_DEVLOCK();
  281. pr_debug("init_stripe called, stripe %llu\n",
  282. (unsigned long long)sh->sector);
  283. remove_hash(sh);
  284. sh->generation = conf->generation - previous;
  285. sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  286. sh->sector = sector;
  287. stripe_set_idx(sector, conf, previous, sh);
  288. sh->state = 0;
  289. for (i = sh->disks; i--; ) {
  290. struct r5dev *dev = &sh->dev[i];
  291. if (dev->toread || dev->read || dev->towrite || dev->written ||
  292. test_bit(R5_LOCKED, &dev->flags)) {
  293. printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
  294. (unsigned long long)sh->sector, i, dev->toread,
  295. dev->read, dev->towrite, dev->written,
  296. test_bit(R5_LOCKED, &dev->flags));
  297. BUG();
  298. }
  299. dev->flags = 0;
  300. raid5_build_block(sh, i, previous);
  301. }
  302. insert_hash(conf, sh);
  303. }
  304. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
  305. short generation)
  306. {
  307. struct stripe_head *sh;
  308. struct hlist_node *hn;
  309. CHECK_DEVLOCK();
  310. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  311. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  312. if (sh->sector == sector && sh->generation == generation)
  313. return sh;
  314. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  315. return NULL;
  316. }
  317. static void unplug_slaves(mddev_t *mddev);
  318. static void raid5_unplug_device(struct request_queue *q);
  319. static struct stripe_head *
  320. get_active_stripe(raid5_conf_t *conf, sector_t sector,
  321. int previous, int noblock, int noquiesce)
  322. {
  323. struct stripe_head *sh;
  324. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  325. spin_lock_irq(&conf->device_lock);
  326. do {
  327. wait_event_lock_irq(conf->wait_for_stripe,
  328. conf->quiesce == 0 || noquiesce,
  329. conf->device_lock, /* nothing */);
  330. sh = __find_stripe(conf, sector, conf->generation - previous);
  331. if (!sh) {
  332. if (!conf->inactive_blocked)
  333. sh = get_free_stripe(conf);
  334. if (noblock && sh == NULL)
  335. break;
  336. if (!sh) {
  337. conf->inactive_blocked = 1;
  338. wait_event_lock_irq(conf->wait_for_stripe,
  339. !list_empty(&conf->inactive_list) &&
  340. (atomic_read(&conf->active_stripes)
  341. < (conf->max_nr_stripes *3/4)
  342. || !conf->inactive_blocked),
  343. conf->device_lock,
  344. raid5_unplug_device(conf->mddev->queue)
  345. );
  346. conf->inactive_blocked = 0;
  347. } else
  348. init_stripe(sh, sector, previous);
  349. } else {
  350. if (atomic_read(&sh->count)) {
  351. BUG_ON(!list_empty(&sh->lru)
  352. && !test_bit(STRIPE_EXPANDING, &sh->state));
  353. } else {
  354. if (!test_bit(STRIPE_HANDLE, &sh->state))
  355. atomic_inc(&conf->active_stripes);
  356. if (list_empty(&sh->lru) &&
  357. !test_bit(STRIPE_EXPANDING, &sh->state))
  358. BUG();
  359. list_del_init(&sh->lru);
  360. }
  361. }
  362. } while (sh == NULL);
  363. if (sh)
  364. atomic_inc(&sh->count);
  365. spin_unlock_irq(&conf->device_lock);
  366. return sh;
  367. }
  368. static void
  369. raid5_end_read_request(struct bio *bi, int error);
  370. static void
  371. raid5_end_write_request(struct bio *bi, int error);
  372. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  373. {
  374. raid5_conf_t *conf = sh->raid_conf;
  375. int i, disks = sh->disks;
  376. might_sleep();
  377. for (i = disks; i--; ) {
  378. int rw;
  379. struct bio *bi;
  380. mdk_rdev_t *rdev;
  381. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  382. rw = WRITE;
  383. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  384. rw = READ;
  385. else
  386. continue;
  387. bi = &sh->dev[i].req;
  388. bi->bi_rw = rw;
  389. if (rw == WRITE)
  390. bi->bi_end_io = raid5_end_write_request;
  391. else
  392. bi->bi_end_io = raid5_end_read_request;
  393. rcu_read_lock();
  394. rdev = rcu_dereference(conf->disks[i].rdev);
  395. if (rdev && test_bit(Faulty, &rdev->flags))
  396. rdev = NULL;
  397. if (rdev)
  398. atomic_inc(&rdev->nr_pending);
  399. rcu_read_unlock();
  400. if (rdev) {
  401. if (s->syncing || s->expanding || s->expanded)
  402. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  403. set_bit(STRIPE_IO_STARTED, &sh->state);
  404. bi->bi_bdev = rdev->bdev;
  405. pr_debug("%s: for %llu schedule op %ld on disc %d\n",
  406. __func__, (unsigned long long)sh->sector,
  407. bi->bi_rw, i);
  408. atomic_inc(&sh->count);
  409. bi->bi_sector = sh->sector + rdev->data_offset;
  410. bi->bi_flags = 1 << BIO_UPTODATE;
  411. bi->bi_vcnt = 1;
  412. bi->bi_max_vecs = 1;
  413. bi->bi_idx = 0;
  414. bi->bi_io_vec = &sh->dev[i].vec;
  415. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  416. bi->bi_io_vec[0].bv_offset = 0;
  417. bi->bi_size = STRIPE_SIZE;
  418. bi->bi_next = NULL;
  419. if (rw == WRITE &&
  420. test_bit(R5_ReWrite, &sh->dev[i].flags))
  421. atomic_add(STRIPE_SECTORS,
  422. &rdev->corrected_errors);
  423. generic_make_request(bi);
  424. } else {
  425. if (rw == WRITE)
  426. set_bit(STRIPE_DEGRADED, &sh->state);
  427. pr_debug("skip op %ld on disc %d for sector %llu\n",
  428. bi->bi_rw, i, (unsigned long long)sh->sector);
  429. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  430. set_bit(STRIPE_HANDLE, &sh->state);
  431. }
  432. }
  433. }
  434. static struct dma_async_tx_descriptor *
  435. async_copy_data(int frombio, struct bio *bio, struct page *page,
  436. sector_t sector, struct dma_async_tx_descriptor *tx)
  437. {
  438. struct bio_vec *bvl;
  439. struct page *bio_page;
  440. int i;
  441. int page_offset;
  442. struct async_submit_ctl submit;
  443. enum async_tx_flags flags = 0;
  444. if (bio->bi_sector >= sector)
  445. page_offset = (signed)(bio->bi_sector - sector) * 512;
  446. else
  447. page_offset = (signed)(sector - bio->bi_sector) * -512;
  448. if (frombio)
  449. flags |= ASYNC_TX_FENCE;
  450. init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
  451. bio_for_each_segment(bvl, bio, i) {
  452. int len = bio_iovec_idx(bio, i)->bv_len;
  453. int clen;
  454. int b_offset = 0;
  455. if (page_offset < 0) {
  456. b_offset = -page_offset;
  457. page_offset += b_offset;
  458. len -= b_offset;
  459. }
  460. if (len > 0 && page_offset + len > STRIPE_SIZE)
  461. clen = STRIPE_SIZE - page_offset;
  462. else
  463. clen = len;
  464. if (clen > 0) {
  465. b_offset += bio_iovec_idx(bio, i)->bv_offset;
  466. bio_page = bio_iovec_idx(bio, i)->bv_page;
  467. if (frombio)
  468. tx = async_memcpy(page, bio_page, page_offset,
  469. b_offset, clen, &submit);
  470. else
  471. tx = async_memcpy(bio_page, page, b_offset,
  472. page_offset, clen, &submit);
  473. }
  474. /* chain the operations */
  475. submit.depend_tx = tx;
  476. if (clen < len) /* hit end of page */
  477. break;
  478. page_offset += len;
  479. }
  480. return tx;
  481. }
  482. static void ops_complete_biofill(void *stripe_head_ref)
  483. {
  484. struct stripe_head *sh = stripe_head_ref;
  485. struct bio *return_bi = NULL;
  486. raid5_conf_t *conf = sh->raid_conf;
  487. int i;
  488. pr_debug("%s: stripe %llu\n", __func__,
  489. (unsigned long long)sh->sector);
  490. /* clear completed biofills */
  491. spin_lock_irq(&conf->device_lock);
  492. for (i = sh->disks; i--; ) {
  493. struct r5dev *dev = &sh->dev[i];
  494. /* acknowledge completion of a biofill operation */
  495. /* and check if we need to reply to a read request,
  496. * new R5_Wantfill requests are held off until
  497. * !STRIPE_BIOFILL_RUN
  498. */
  499. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  500. struct bio *rbi, *rbi2;
  501. BUG_ON(!dev->read);
  502. rbi = dev->read;
  503. dev->read = NULL;
  504. while (rbi && rbi->bi_sector <
  505. dev->sector + STRIPE_SECTORS) {
  506. rbi2 = r5_next_bio(rbi, dev->sector);
  507. if (!raid5_dec_bi_phys_segments(rbi)) {
  508. rbi->bi_next = return_bi;
  509. return_bi = rbi;
  510. }
  511. rbi = rbi2;
  512. }
  513. }
  514. }
  515. spin_unlock_irq(&conf->device_lock);
  516. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  517. return_io(return_bi);
  518. set_bit(STRIPE_HANDLE, &sh->state);
  519. release_stripe(sh);
  520. }
  521. static void ops_run_biofill(struct stripe_head *sh)
  522. {
  523. struct dma_async_tx_descriptor *tx = NULL;
  524. raid5_conf_t *conf = sh->raid_conf;
  525. struct async_submit_ctl submit;
  526. int i;
  527. pr_debug("%s: stripe %llu\n", __func__,
  528. (unsigned long long)sh->sector);
  529. for (i = sh->disks; i--; ) {
  530. struct r5dev *dev = &sh->dev[i];
  531. if (test_bit(R5_Wantfill, &dev->flags)) {
  532. struct bio *rbi;
  533. spin_lock_irq(&conf->device_lock);
  534. dev->read = rbi = dev->toread;
  535. dev->toread = NULL;
  536. spin_unlock_irq(&conf->device_lock);
  537. while (rbi && rbi->bi_sector <
  538. dev->sector + STRIPE_SECTORS) {
  539. tx = async_copy_data(0, rbi, dev->page,
  540. dev->sector, tx);
  541. rbi = r5_next_bio(rbi, dev->sector);
  542. }
  543. }
  544. }
  545. atomic_inc(&sh->count);
  546. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
  547. async_trigger_callback(&submit);
  548. }
  549. static void mark_target_uptodate(struct stripe_head *sh, int target)
  550. {
  551. struct r5dev *tgt;
  552. if (target < 0)
  553. return;
  554. tgt = &sh->dev[target];
  555. set_bit(R5_UPTODATE, &tgt->flags);
  556. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  557. clear_bit(R5_Wantcompute, &tgt->flags);
  558. }
  559. static void ops_complete_compute(void *stripe_head_ref)
  560. {
  561. struct stripe_head *sh = stripe_head_ref;
  562. pr_debug("%s: stripe %llu\n", __func__,
  563. (unsigned long long)sh->sector);
  564. /* mark the computed target(s) as uptodate */
  565. mark_target_uptodate(sh, sh->ops.target);
  566. mark_target_uptodate(sh, sh->ops.target2);
  567. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  568. if (sh->check_state == check_state_compute_run)
  569. sh->check_state = check_state_compute_result;
  570. set_bit(STRIPE_HANDLE, &sh->state);
  571. release_stripe(sh);
  572. }
  573. /* return a pointer to the address conversion region of the scribble buffer */
  574. static addr_conv_t *to_addr_conv(struct stripe_head *sh,
  575. struct raid5_percpu *percpu)
  576. {
  577. return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
  578. }
  579. static struct dma_async_tx_descriptor *
  580. ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
  581. {
  582. int disks = sh->disks;
  583. struct page **xor_srcs = percpu->scribble;
  584. int target = sh->ops.target;
  585. struct r5dev *tgt = &sh->dev[target];
  586. struct page *xor_dest = tgt->page;
  587. int count = 0;
  588. struct dma_async_tx_descriptor *tx;
  589. struct async_submit_ctl submit;
  590. int i;
  591. pr_debug("%s: stripe %llu block: %d\n",
  592. __func__, (unsigned long long)sh->sector, target);
  593. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  594. for (i = disks; i--; )
  595. if (i != target)
  596. xor_srcs[count++] = sh->dev[i].page;
  597. atomic_inc(&sh->count);
  598. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
  599. ops_complete_compute, sh, to_addr_conv(sh, percpu));
  600. if (unlikely(count == 1))
  601. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  602. else
  603. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  604. return tx;
  605. }
  606. /* set_syndrome_sources - populate source buffers for gen_syndrome
  607. * @srcs - (struct page *) array of size sh->disks
  608. * @sh - stripe_head to parse
  609. *
  610. * Populates srcs in proper layout order for the stripe and returns the
  611. * 'count' of sources to be used in a call to async_gen_syndrome. The P
  612. * destination buffer is recorded in srcs[count] and the Q destination
  613. * is recorded in srcs[count+1]].
  614. */
  615. static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
  616. {
  617. int disks = sh->disks;
  618. int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
  619. int d0_idx = raid6_d0(sh);
  620. int count;
  621. int i;
  622. for (i = 0; i < disks; i++)
  623. srcs[i] = NULL;
  624. count = 0;
  625. i = d0_idx;
  626. do {
  627. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  628. srcs[slot] = sh->dev[i].page;
  629. i = raid6_next_disk(i, disks);
  630. } while (i != d0_idx);
  631. return syndrome_disks;
  632. }
  633. static struct dma_async_tx_descriptor *
  634. ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
  635. {
  636. int disks = sh->disks;
  637. struct page **blocks = percpu->scribble;
  638. int target;
  639. int qd_idx = sh->qd_idx;
  640. struct dma_async_tx_descriptor *tx;
  641. struct async_submit_ctl submit;
  642. struct r5dev *tgt;
  643. struct page *dest;
  644. int i;
  645. int count;
  646. if (sh->ops.target < 0)
  647. target = sh->ops.target2;
  648. else if (sh->ops.target2 < 0)
  649. target = sh->ops.target;
  650. else
  651. /* we should only have one valid target */
  652. BUG();
  653. BUG_ON(target < 0);
  654. pr_debug("%s: stripe %llu block: %d\n",
  655. __func__, (unsigned long long)sh->sector, target);
  656. tgt = &sh->dev[target];
  657. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  658. dest = tgt->page;
  659. atomic_inc(&sh->count);
  660. if (target == qd_idx) {
  661. count = set_syndrome_sources(blocks, sh);
  662. blocks[count] = NULL; /* regenerating p is not necessary */
  663. BUG_ON(blocks[count+1] != dest); /* q should already be set */
  664. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  665. ops_complete_compute, sh,
  666. to_addr_conv(sh, percpu));
  667. tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  668. } else {
  669. /* Compute any data- or p-drive using XOR */
  670. count = 0;
  671. for (i = disks; i-- ; ) {
  672. if (i == target || i == qd_idx)
  673. continue;
  674. blocks[count++] = sh->dev[i].page;
  675. }
  676. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  677. NULL, ops_complete_compute, sh,
  678. to_addr_conv(sh, percpu));
  679. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
  680. }
  681. return tx;
  682. }
  683. static struct dma_async_tx_descriptor *
  684. ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
  685. {
  686. int i, count, disks = sh->disks;
  687. int syndrome_disks = sh->ddf_layout ? disks : disks-2;
  688. int d0_idx = raid6_d0(sh);
  689. int faila = -1, failb = -1;
  690. int target = sh->ops.target;
  691. int target2 = sh->ops.target2;
  692. struct r5dev *tgt = &sh->dev[target];
  693. struct r5dev *tgt2 = &sh->dev[target2];
  694. struct dma_async_tx_descriptor *tx;
  695. struct page **blocks = percpu->scribble;
  696. struct async_submit_ctl submit;
  697. pr_debug("%s: stripe %llu block1: %d block2: %d\n",
  698. __func__, (unsigned long long)sh->sector, target, target2);
  699. BUG_ON(target < 0 || target2 < 0);
  700. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  701. BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
  702. /* we need to open-code set_syndrome_sources to handle the
  703. * slot number conversion for 'faila' and 'failb'
  704. */
  705. for (i = 0; i < disks ; i++)
  706. blocks[i] = NULL;
  707. count = 0;
  708. i = d0_idx;
  709. do {
  710. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  711. blocks[slot] = sh->dev[i].page;
  712. if (i == target)
  713. faila = slot;
  714. if (i == target2)
  715. failb = slot;
  716. i = raid6_next_disk(i, disks);
  717. } while (i != d0_idx);
  718. BUG_ON(faila == failb);
  719. if (failb < faila)
  720. swap(faila, failb);
  721. pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
  722. __func__, (unsigned long long)sh->sector, faila, failb);
  723. atomic_inc(&sh->count);
  724. if (failb == syndrome_disks+1) {
  725. /* Q disk is one of the missing disks */
  726. if (faila == syndrome_disks) {
  727. /* Missing P+Q, just recompute */
  728. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  729. ops_complete_compute, sh,
  730. to_addr_conv(sh, percpu));
  731. return async_gen_syndrome(blocks, 0, syndrome_disks+2,
  732. STRIPE_SIZE, &submit);
  733. } else {
  734. struct page *dest;
  735. int data_target;
  736. int qd_idx = sh->qd_idx;
  737. /* Missing D+Q: recompute D from P, then recompute Q */
  738. if (target == qd_idx)
  739. data_target = target2;
  740. else
  741. data_target = target;
  742. count = 0;
  743. for (i = disks; i-- ; ) {
  744. if (i == data_target || i == qd_idx)
  745. continue;
  746. blocks[count++] = sh->dev[i].page;
  747. }
  748. dest = sh->dev[data_target].page;
  749. init_async_submit(&submit,
  750. ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
  751. NULL, NULL, NULL,
  752. to_addr_conv(sh, percpu));
  753. tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
  754. &submit);
  755. count = set_syndrome_sources(blocks, sh);
  756. init_async_submit(&submit, ASYNC_TX_FENCE, tx,
  757. ops_complete_compute, sh,
  758. to_addr_conv(sh, percpu));
  759. return async_gen_syndrome(blocks, 0, count+2,
  760. STRIPE_SIZE, &submit);
  761. }
  762. } else {
  763. init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
  764. ops_complete_compute, sh,
  765. to_addr_conv(sh, percpu));
  766. if (failb == syndrome_disks) {
  767. /* We're missing D+P. */
  768. return async_raid6_datap_recov(syndrome_disks+2,
  769. STRIPE_SIZE, faila,
  770. blocks, &submit);
  771. } else {
  772. /* We're missing D+D. */
  773. return async_raid6_2data_recov(syndrome_disks+2,
  774. STRIPE_SIZE, faila, failb,
  775. blocks, &submit);
  776. }
  777. }
  778. }
  779. static void ops_complete_prexor(void *stripe_head_ref)
  780. {
  781. struct stripe_head *sh = stripe_head_ref;
  782. pr_debug("%s: stripe %llu\n", __func__,
  783. (unsigned long long)sh->sector);
  784. }
  785. static struct dma_async_tx_descriptor *
  786. ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
  787. struct dma_async_tx_descriptor *tx)
  788. {
  789. int disks = sh->disks;
  790. struct page **xor_srcs = percpu->scribble;
  791. int count = 0, pd_idx = sh->pd_idx, i;
  792. struct async_submit_ctl submit;
  793. /* existing parity data subtracted */
  794. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  795. pr_debug("%s: stripe %llu\n", __func__,
  796. (unsigned long long)sh->sector);
  797. for (i = disks; i--; ) {
  798. struct r5dev *dev = &sh->dev[i];
  799. /* Only process blocks that are known to be uptodate */
  800. if (test_bit(R5_Wantdrain, &dev->flags))
  801. xor_srcs[count++] = dev->page;
  802. }
  803. init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  804. ops_complete_prexor, sh, to_addr_conv(sh, percpu));
  805. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  806. return tx;
  807. }
  808. static struct dma_async_tx_descriptor *
  809. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  810. {
  811. int disks = sh->disks;
  812. int i;
  813. pr_debug("%s: stripe %llu\n", __func__,
  814. (unsigned long long)sh->sector);
  815. for (i = disks; i--; ) {
  816. struct r5dev *dev = &sh->dev[i];
  817. struct bio *chosen;
  818. if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
  819. struct bio *wbi;
  820. spin_lock(&sh->lock);
  821. chosen = dev->towrite;
  822. dev->towrite = NULL;
  823. BUG_ON(dev->written);
  824. wbi = dev->written = chosen;
  825. spin_unlock(&sh->lock);
  826. while (wbi && wbi->bi_sector <
  827. dev->sector + STRIPE_SECTORS) {
  828. tx = async_copy_data(1, wbi, dev->page,
  829. dev->sector, tx);
  830. wbi = r5_next_bio(wbi, dev->sector);
  831. }
  832. }
  833. }
  834. return tx;
  835. }
  836. static void ops_complete_reconstruct(void *stripe_head_ref)
  837. {
  838. struct stripe_head *sh = stripe_head_ref;
  839. int disks = sh->disks;
  840. int pd_idx = sh->pd_idx;
  841. int qd_idx = sh->qd_idx;
  842. int i;
  843. pr_debug("%s: stripe %llu\n", __func__,
  844. (unsigned long long)sh->sector);
  845. for (i = disks; i--; ) {
  846. struct r5dev *dev = &sh->dev[i];
  847. if (dev->written || i == pd_idx || i == qd_idx)
  848. set_bit(R5_UPTODATE, &dev->flags);
  849. }
  850. if (sh->reconstruct_state == reconstruct_state_drain_run)
  851. sh->reconstruct_state = reconstruct_state_drain_result;
  852. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  853. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  854. else {
  855. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  856. sh->reconstruct_state = reconstruct_state_result;
  857. }
  858. set_bit(STRIPE_HANDLE, &sh->state);
  859. release_stripe(sh);
  860. }
  861. static void
  862. ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
  863. struct dma_async_tx_descriptor *tx)
  864. {
  865. int disks = sh->disks;
  866. struct page **xor_srcs = percpu->scribble;
  867. struct async_submit_ctl submit;
  868. int count = 0, pd_idx = sh->pd_idx, i;
  869. struct page *xor_dest;
  870. int prexor = 0;
  871. unsigned long flags;
  872. pr_debug("%s: stripe %llu\n", __func__,
  873. (unsigned long long)sh->sector);
  874. /* check if prexor is active which means only process blocks
  875. * that are part of a read-modify-write (written)
  876. */
  877. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  878. prexor = 1;
  879. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  880. for (i = disks; i--; ) {
  881. struct r5dev *dev = &sh->dev[i];
  882. if (dev->written)
  883. xor_srcs[count++] = dev->page;
  884. }
  885. } else {
  886. xor_dest = sh->dev[pd_idx].page;
  887. for (i = disks; i--; ) {
  888. struct r5dev *dev = &sh->dev[i];
  889. if (i != pd_idx)
  890. xor_srcs[count++] = dev->page;
  891. }
  892. }
  893. /* 1/ if we prexor'd then the dest is reused as a source
  894. * 2/ if we did not prexor then we are redoing the parity
  895. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  896. * for the synchronous xor case
  897. */
  898. flags = ASYNC_TX_ACK |
  899. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  900. atomic_inc(&sh->count);
  901. init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
  902. to_addr_conv(sh, percpu));
  903. if (unlikely(count == 1))
  904. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
  905. else
  906. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
  907. }
  908. static void
  909. ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
  910. struct dma_async_tx_descriptor *tx)
  911. {
  912. struct async_submit_ctl submit;
  913. struct page **blocks = percpu->scribble;
  914. int count;
  915. pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
  916. count = set_syndrome_sources(blocks, sh);
  917. atomic_inc(&sh->count);
  918. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
  919. sh, to_addr_conv(sh, percpu));
  920. async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
  921. }
  922. static void ops_complete_check(void *stripe_head_ref)
  923. {
  924. struct stripe_head *sh = stripe_head_ref;
  925. pr_debug("%s: stripe %llu\n", __func__,
  926. (unsigned long long)sh->sector);
  927. sh->check_state = check_state_check_result;
  928. set_bit(STRIPE_HANDLE, &sh->state);
  929. release_stripe(sh);
  930. }
  931. static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
  932. {
  933. int disks = sh->disks;
  934. int pd_idx = sh->pd_idx;
  935. int qd_idx = sh->qd_idx;
  936. struct page *xor_dest;
  937. struct page **xor_srcs = percpu->scribble;
  938. struct dma_async_tx_descriptor *tx;
  939. struct async_submit_ctl submit;
  940. int count;
  941. int i;
  942. pr_debug("%s: stripe %llu\n", __func__,
  943. (unsigned long long)sh->sector);
  944. count = 0;
  945. xor_dest = sh->dev[pd_idx].page;
  946. xor_srcs[count++] = xor_dest;
  947. for (i = disks; i--; ) {
  948. if (i == pd_idx || i == qd_idx)
  949. continue;
  950. xor_srcs[count++] = sh->dev[i].page;
  951. }
  952. init_async_submit(&submit, 0, NULL, NULL, NULL,
  953. to_addr_conv(sh, percpu));
  954. tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  955. &sh->ops.zero_sum_result, &submit);
  956. atomic_inc(&sh->count);
  957. init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
  958. tx = async_trigger_callback(&submit);
  959. }
  960. static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
  961. {
  962. struct page **srcs = percpu->scribble;
  963. struct async_submit_ctl submit;
  964. int count;
  965. pr_debug("%s: stripe %llu checkp: %d\n", __func__,
  966. (unsigned long long)sh->sector, checkp);
  967. count = set_syndrome_sources(srcs, sh);
  968. if (!checkp)
  969. srcs[count] = NULL;
  970. atomic_inc(&sh->count);
  971. init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
  972. sh, to_addr_conv(sh, percpu));
  973. async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
  974. &sh->ops.zero_sum_result, percpu->spare_page, &submit);
  975. }
  976. static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  977. {
  978. int overlap_clear = 0, i, disks = sh->disks;
  979. struct dma_async_tx_descriptor *tx = NULL;
  980. raid5_conf_t *conf = sh->raid_conf;
  981. int level = conf->level;
  982. struct raid5_percpu *percpu;
  983. unsigned long cpu;
  984. cpu = get_cpu();
  985. percpu = per_cpu_ptr(conf->percpu, cpu);
  986. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  987. ops_run_biofill(sh);
  988. overlap_clear++;
  989. }
  990. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  991. if (level < 6)
  992. tx = ops_run_compute5(sh, percpu);
  993. else {
  994. if (sh->ops.target2 < 0 || sh->ops.target < 0)
  995. tx = ops_run_compute6_1(sh, percpu);
  996. else
  997. tx = ops_run_compute6_2(sh, percpu);
  998. }
  999. /* terminate the chain if reconstruct is not set to be run */
  1000. if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
  1001. async_tx_ack(tx);
  1002. }
  1003. if (test_bit(STRIPE_OP_PREXOR, &ops_request))
  1004. tx = ops_run_prexor(sh, percpu, tx);
  1005. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  1006. tx = ops_run_biodrain(sh, tx);
  1007. overlap_clear++;
  1008. }
  1009. if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
  1010. if (level < 6)
  1011. ops_run_reconstruct5(sh, percpu, tx);
  1012. else
  1013. ops_run_reconstruct6(sh, percpu, tx);
  1014. }
  1015. if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
  1016. if (sh->check_state == check_state_run)
  1017. ops_run_check_p(sh, percpu);
  1018. else if (sh->check_state == check_state_run_q)
  1019. ops_run_check_pq(sh, percpu, 0);
  1020. else if (sh->check_state == check_state_run_pq)
  1021. ops_run_check_pq(sh, percpu, 1);
  1022. else
  1023. BUG();
  1024. }
  1025. if (overlap_clear)
  1026. for (i = disks; i--; ) {
  1027. struct r5dev *dev = &sh->dev[i];
  1028. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1029. wake_up(&sh->raid_conf->wait_for_overlap);
  1030. }
  1031. put_cpu();
  1032. }
  1033. #ifdef CONFIG_MULTICORE_RAID456
  1034. static void async_run_ops(void *param, async_cookie_t cookie)
  1035. {
  1036. struct stripe_head *sh = param;
  1037. unsigned long ops_request = sh->ops.request;
  1038. clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
  1039. wake_up(&sh->ops.wait_for_ops);
  1040. __raid_run_ops(sh, ops_request);
  1041. release_stripe(sh);
  1042. }
  1043. static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  1044. {
  1045. /* since handle_stripe can be called outside of raid5d context
  1046. * we need to ensure sh->ops.request is de-staged before another
  1047. * request arrives
  1048. */
  1049. wait_event(sh->ops.wait_for_ops,
  1050. !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
  1051. sh->ops.request = ops_request;
  1052. atomic_inc(&sh->count);
  1053. async_schedule(async_run_ops, sh);
  1054. }
  1055. #else
  1056. #define raid_run_ops __raid_run_ops
  1057. #endif
  1058. static int grow_one_stripe(raid5_conf_t *conf)
  1059. {
  1060. struct stripe_head *sh;
  1061. int disks = max(conf->raid_disks, conf->previous_raid_disks);
  1062. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  1063. if (!sh)
  1064. return 0;
  1065. memset(sh, 0, sizeof(*sh) + (disks-1)*sizeof(struct r5dev));
  1066. sh->raid_conf = conf;
  1067. spin_lock_init(&sh->lock);
  1068. #ifdef CONFIG_MULTICORE_RAID456
  1069. init_waitqueue_head(&sh->ops.wait_for_ops);
  1070. #endif
  1071. if (grow_buffers(sh, disks)) {
  1072. shrink_buffers(sh, disks);
  1073. kmem_cache_free(conf->slab_cache, sh);
  1074. return 0;
  1075. }
  1076. /* we just created an active stripe so... */
  1077. atomic_set(&sh->count, 1);
  1078. atomic_inc(&conf->active_stripes);
  1079. INIT_LIST_HEAD(&sh->lru);
  1080. release_stripe(sh);
  1081. return 1;
  1082. }
  1083. static int grow_stripes(raid5_conf_t *conf, int num)
  1084. {
  1085. struct kmem_cache *sc;
  1086. int devs = max(conf->raid_disks, conf->previous_raid_disks);
  1087. sprintf(conf->cache_name[0],
  1088. "raid%d-%s", conf->level, mdname(conf->mddev));
  1089. sprintf(conf->cache_name[1],
  1090. "raid%d-%s-alt", conf->level, mdname(conf->mddev));
  1091. conf->active_name = 0;
  1092. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  1093. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  1094. 0, 0, NULL);
  1095. if (!sc)
  1096. return 1;
  1097. conf->slab_cache = sc;
  1098. conf->pool_size = devs;
  1099. while (num--)
  1100. if (!grow_one_stripe(conf))
  1101. return 1;
  1102. return 0;
  1103. }
  1104. /**
  1105. * scribble_len - return the required size of the scribble region
  1106. * @num - total number of disks in the array
  1107. *
  1108. * The size must be enough to contain:
  1109. * 1/ a struct page pointer for each device in the array +2
  1110. * 2/ room to convert each entry in (1) to its corresponding dma
  1111. * (dma_map_page()) or page (page_address()) address.
  1112. *
  1113. * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
  1114. * calculate over all devices (not just the data blocks), using zeros in place
  1115. * of the P and Q blocks.
  1116. */
  1117. static size_t scribble_len(int num)
  1118. {
  1119. size_t len;
  1120. len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
  1121. return len;
  1122. }
  1123. static int resize_stripes(raid5_conf_t *conf, int newsize)
  1124. {
  1125. /* Make all the stripes able to hold 'newsize' devices.
  1126. * New slots in each stripe get 'page' set to a new page.
  1127. *
  1128. * This happens in stages:
  1129. * 1/ create a new kmem_cache and allocate the required number of
  1130. * stripe_heads.
  1131. * 2/ gather all the old stripe_heads and tranfer the pages across
  1132. * to the new stripe_heads. This will have the side effect of
  1133. * freezing the array as once all stripe_heads have been collected,
  1134. * no IO will be possible. Old stripe heads are freed once their
  1135. * pages have been transferred over, and the old kmem_cache is
  1136. * freed when all stripes are done.
  1137. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  1138. * we simple return a failre status - no need to clean anything up.
  1139. * 4/ allocate new pages for the new slots in the new stripe_heads.
  1140. * If this fails, we don't bother trying the shrink the
  1141. * stripe_heads down again, we just leave them as they are.
  1142. * As each stripe_head is processed the new one is released into
  1143. * active service.
  1144. *
  1145. * Once step2 is started, we cannot afford to wait for a write,
  1146. * so we use GFP_NOIO allocations.
  1147. */
  1148. struct stripe_head *osh, *nsh;
  1149. LIST_HEAD(newstripes);
  1150. struct disk_info *ndisks;
  1151. unsigned long cpu;
  1152. int err;
  1153. struct kmem_cache *sc;
  1154. int i;
  1155. if (newsize <= conf->pool_size)
  1156. return 0; /* never bother to shrink */
  1157. err = md_allow_write(conf->mddev);
  1158. if (err)
  1159. return err;
  1160. /* Step 1 */
  1161. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  1162. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  1163. 0, 0, NULL);
  1164. if (!sc)
  1165. return -ENOMEM;
  1166. for (i = conf->max_nr_stripes; i; i--) {
  1167. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  1168. if (!nsh)
  1169. break;
  1170. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  1171. nsh->raid_conf = conf;
  1172. spin_lock_init(&nsh->lock);
  1173. #ifdef CONFIG_MULTICORE_RAID456
  1174. init_waitqueue_head(&nsh->ops.wait_for_ops);
  1175. #endif
  1176. list_add(&nsh->lru, &newstripes);
  1177. }
  1178. if (i) {
  1179. /* didn't get enough, give up */
  1180. while (!list_empty(&newstripes)) {
  1181. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  1182. list_del(&nsh->lru);
  1183. kmem_cache_free(sc, nsh);
  1184. }
  1185. kmem_cache_destroy(sc);
  1186. return -ENOMEM;
  1187. }
  1188. /* Step 2 - Must use GFP_NOIO now.
  1189. * OK, we have enough stripes, start collecting inactive
  1190. * stripes and copying them over
  1191. */
  1192. list_for_each_entry(nsh, &newstripes, lru) {
  1193. spin_lock_irq(&conf->device_lock);
  1194. wait_event_lock_irq(conf->wait_for_stripe,
  1195. !list_empty(&conf->inactive_list),
  1196. conf->device_lock,
  1197. unplug_slaves(conf->mddev)
  1198. );
  1199. osh = get_free_stripe(conf);
  1200. spin_unlock_irq(&conf->device_lock);
  1201. atomic_set(&nsh->count, 1);
  1202. for(i=0; i<conf->pool_size; i++)
  1203. nsh->dev[i].page = osh->dev[i].page;
  1204. for( ; i<newsize; i++)
  1205. nsh->dev[i].page = NULL;
  1206. kmem_cache_free(conf->slab_cache, osh);
  1207. }
  1208. kmem_cache_destroy(conf->slab_cache);
  1209. /* Step 3.
  1210. * At this point, we are holding all the stripes so the array
  1211. * is completely stalled, so now is a good time to resize
  1212. * conf->disks and the scribble region
  1213. */
  1214. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  1215. if (ndisks) {
  1216. for (i=0; i<conf->raid_disks; i++)
  1217. ndisks[i] = conf->disks[i];
  1218. kfree(conf->disks);
  1219. conf->disks = ndisks;
  1220. } else
  1221. err = -ENOMEM;
  1222. get_online_cpus();
  1223. conf->scribble_len = scribble_len(newsize);
  1224. for_each_present_cpu(cpu) {
  1225. struct raid5_percpu *percpu;
  1226. void *scribble;
  1227. percpu = per_cpu_ptr(conf->percpu, cpu);
  1228. scribble = kmalloc(conf->scribble_len, GFP_NOIO);
  1229. if (scribble) {
  1230. kfree(percpu->scribble);
  1231. percpu->scribble = scribble;
  1232. } else {
  1233. err = -ENOMEM;
  1234. break;
  1235. }
  1236. }
  1237. put_online_cpus();
  1238. /* Step 4, return new stripes to service */
  1239. while(!list_empty(&newstripes)) {
  1240. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  1241. list_del_init(&nsh->lru);
  1242. for (i=conf->raid_disks; i < newsize; i++)
  1243. if (nsh->dev[i].page == NULL) {
  1244. struct page *p = alloc_page(GFP_NOIO);
  1245. nsh->dev[i].page = p;
  1246. if (!p)
  1247. err = -ENOMEM;
  1248. }
  1249. release_stripe(nsh);
  1250. }
  1251. /* critical section pass, GFP_NOIO no longer needed */
  1252. conf->slab_cache = sc;
  1253. conf->active_name = 1-conf->active_name;
  1254. conf->pool_size = newsize;
  1255. return err;
  1256. }
  1257. static int drop_one_stripe(raid5_conf_t *conf)
  1258. {
  1259. struct stripe_head *sh;
  1260. spin_lock_irq(&conf->device_lock);
  1261. sh = get_free_stripe(conf);
  1262. spin_unlock_irq(&conf->device_lock);
  1263. if (!sh)
  1264. return 0;
  1265. BUG_ON(atomic_read(&sh->count));
  1266. shrink_buffers(sh, conf->pool_size);
  1267. kmem_cache_free(conf->slab_cache, sh);
  1268. atomic_dec(&conf->active_stripes);
  1269. return 1;
  1270. }
  1271. static void shrink_stripes(raid5_conf_t *conf)
  1272. {
  1273. while (drop_one_stripe(conf))
  1274. ;
  1275. if (conf->slab_cache)
  1276. kmem_cache_destroy(conf->slab_cache);
  1277. conf->slab_cache = NULL;
  1278. }
  1279. static void raid5_end_read_request(struct bio * bi, int error)
  1280. {
  1281. struct stripe_head *sh = bi->bi_private;
  1282. raid5_conf_t *conf = sh->raid_conf;
  1283. int disks = sh->disks, i;
  1284. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1285. char b[BDEVNAME_SIZE];
  1286. mdk_rdev_t *rdev;
  1287. for (i=0 ; i<disks; i++)
  1288. if (bi == &sh->dev[i].req)
  1289. break;
  1290. pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  1291. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1292. uptodate);
  1293. if (i == disks) {
  1294. BUG();
  1295. return;
  1296. }
  1297. if (uptodate) {
  1298. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1299. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1300. rdev = conf->disks[i].rdev;
  1301. printk_rl(KERN_INFO "raid5:%s: read error corrected"
  1302. " (%lu sectors at %llu on %s)\n",
  1303. mdname(conf->mddev), STRIPE_SECTORS,
  1304. (unsigned long long)(sh->sector
  1305. + rdev->data_offset),
  1306. bdevname(rdev->bdev, b));
  1307. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1308. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1309. }
  1310. if (atomic_read(&conf->disks[i].rdev->read_errors))
  1311. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  1312. } else {
  1313. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  1314. int retry = 0;
  1315. rdev = conf->disks[i].rdev;
  1316. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  1317. atomic_inc(&rdev->read_errors);
  1318. if (conf->mddev->degraded)
  1319. printk_rl(KERN_WARNING
  1320. "raid5:%s: read error not correctable "
  1321. "(sector %llu on %s).\n",
  1322. mdname(conf->mddev),
  1323. (unsigned long long)(sh->sector
  1324. + rdev->data_offset),
  1325. bdn);
  1326. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  1327. /* Oh, no!!! */
  1328. printk_rl(KERN_WARNING
  1329. "raid5:%s: read error NOT corrected!! "
  1330. "(sector %llu on %s).\n",
  1331. mdname(conf->mddev),
  1332. (unsigned long long)(sh->sector
  1333. + rdev->data_offset),
  1334. bdn);
  1335. else if (atomic_read(&rdev->read_errors)
  1336. > conf->max_nr_stripes)
  1337. printk(KERN_WARNING
  1338. "raid5:%s: Too many read errors, failing device %s.\n",
  1339. mdname(conf->mddev), bdn);
  1340. else
  1341. retry = 1;
  1342. if (retry)
  1343. set_bit(R5_ReadError, &sh->dev[i].flags);
  1344. else {
  1345. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1346. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1347. md_error(conf->mddev, rdev);
  1348. }
  1349. }
  1350. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1351. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1352. set_bit(STRIPE_HANDLE, &sh->state);
  1353. release_stripe(sh);
  1354. }
  1355. static void raid5_end_write_request(struct bio *bi, int error)
  1356. {
  1357. struct stripe_head *sh = bi->bi_private;
  1358. raid5_conf_t *conf = sh->raid_conf;
  1359. int disks = sh->disks, i;
  1360. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1361. for (i=0 ; i<disks; i++)
  1362. if (bi == &sh->dev[i].req)
  1363. break;
  1364. pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  1365. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1366. uptodate);
  1367. if (i == disks) {
  1368. BUG();
  1369. return;
  1370. }
  1371. if (!uptodate)
  1372. md_error(conf->mddev, conf->disks[i].rdev);
  1373. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1374. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1375. set_bit(STRIPE_HANDLE, &sh->state);
  1376. release_stripe(sh);
  1377. }
  1378. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
  1379. static void raid5_build_block(struct stripe_head *sh, int i, int previous)
  1380. {
  1381. struct r5dev *dev = &sh->dev[i];
  1382. bio_init(&dev->req);
  1383. dev->req.bi_io_vec = &dev->vec;
  1384. dev->req.bi_vcnt++;
  1385. dev->req.bi_max_vecs++;
  1386. dev->vec.bv_page = dev->page;
  1387. dev->vec.bv_len = STRIPE_SIZE;
  1388. dev->vec.bv_offset = 0;
  1389. dev->req.bi_sector = sh->sector;
  1390. dev->req.bi_private = sh;
  1391. dev->flags = 0;
  1392. dev->sector = compute_blocknr(sh, i, previous);
  1393. }
  1394. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  1395. {
  1396. char b[BDEVNAME_SIZE];
  1397. raid5_conf_t *conf = mddev->private;
  1398. pr_debug("raid5: error called\n");
  1399. if (!test_bit(Faulty, &rdev->flags)) {
  1400. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1401. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1402. unsigned long flags;
  1403. spin_lock_irqsave(&conf->device_lock, flags);
  1404. mddev->degraded++;
  1405. spin_unlock_irqrestore(&conf->device_lock, flags);
  1406. /*
  1407. * if recovery was running, make sure it aborts.
  1408. */
  1409. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1410. }
  1411. set_bit(Faulty, &rdev->flags);
  1412. printk(KERN_ALERT
  1413. "raid5: Disk failure on %s, disabling device.\n"
  1414. "raid5: Operation continuing on %d devices.\n",
  1415. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  1416. }
  1417. }
  1418. /*
  1419. * Input: a 'big' sector number,
  1420. * Output: index of the data and parity disk, and the sector # in them.
  1421. */
  1422. static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
  1423. int previous, int *dd_idx,
  1424. struct stripe_head *sh)
  1425. {
  1426. long stripe;
  1427. unsigned long chunk_number;
  1428. unsigned int chunk_offset;
  1429. int pd_idx, qd_idx;
  1430. int ddf_layout = 0;
  1431. sector_t new_sector;
  1432. int algorithm = previous ? conf->prev_algo
  1433. : conf->algorithm;
  1434. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1435. : conf->chunk_sectors;
  1436. int raid_disks = previous ? conf->previous_raid_disks
  1437. : conf->raid_disks;
  1438. int data_disks = raid_disks - conf->max_degraded;
  1439. /* First compute the information on this sector */
  1440. /*
  1441. * Compute the chunk number and the sector offset inside the chunk
  1442. */
  1443. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  1444. chunk_number = r_sector;
  1445. BUG_ON(r_sector != chunk_number);
  1446. /*
  1447. * Compute the stripe number
  1448. */
  1449. stripe = chunk_number / data_disks;
  1450. /*
  1451. * Compute the data disk and parity disk indexes inside the stripe
  1452. */
  1453. *dd_idx = chunk_number % data_disks;
  1454. /*
  1455. * Select the parity disk based on the user selected algorithm.
  1456. */
  1457. pd_idx = qd_idx = ~0;
  1458. switch(conf->level) {
  1459. case 4:
  1460. pd_idx = data_disks;
  1461. break;
  1462. case 5:
  1463. switch (algorithm) {
  1464. case ALGORITHM_LEFT_ASYMMETRIC:
  1465. pd_idx = data_disks - stripe % raid_disks;
  1466. if (*dd_idx >= pd_idx)
  1467. (*dd_idx)++;
  1468. break;
  1469. case ALGORITHM_RIGHT_ASYMMETRIC:
  1470. pd_idx = stripe % raid_disks;
  1471. if (*dd_idx >= pd_idx)
  1472. (*dd_idx)++;
  1473. break;
  1474. case ALGORITHM_LEFT_SYMMETRIC:
  1475. pd_idx = data_disks - stripe % raid_disks;
  1476. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1477. break;
  1478. case ALGORITHM_RIGHT_SYMMETRIC:
  1479. pd_idx = stripe % raid_disks;
  1480. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1481. break;
  1482. case ALGORITHM_PARITY_0:
  1483. pd_idx = 0;
  1484. (*dd_idx)++;
  1485. break;
  1486. case ALGORITHM_PARITY_N:
  1487. pd_idx = data_disks;
  1488. break;
  1489. default:
  1490. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1491. algorithm);
  1492. BUG();
  1493. }
  1494. break;
  1495. case 6:
  1496. switch (algorithm) {
  1497. case ALGORITHM_LEFT_ASYMMETRIC:
  1498. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1499. qd_idx = pd_idx + 1;
  1500. if (pd_idx == raid_disks-1) {
  1501. (*dd_idx)++; /* Q D D D P */
  1502. qd_idx = 0;
  1503. } else if (*dd_idx >= pd_idx)
  1504. (*dd_idx) += 2; /* D D P Q D */
  1505. break;
  1506. case ALGORITHM_RIGHT_ASYMMETRIC:
  1507. pd_idx = stripe % raid_disks;
  1508. qd_idx = pd_idx + 1;
  1509. if (pd_idx == raid_disks-1) {
  1510. (*dd_idx)++; /* Q D D D P */
  1511. qd_idx = 0;
  1512. } else if (*dd_idx >= pd_idx)
  1513. (*dd_idx) += 2; /* D D P Q D */
  1514. break;
  1515. case ALGORITHM_LEFT_SYMMETRIC:
  1516. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1517. qd_idx = (pd_idx + 1) % raid_disks;
  1518. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1519. break;
  1520. case ALGORITHM_RIGHT_SYMMETRIC:
  1521. pd_idx = stripe % raid_disks;
  1522. qd_idx = (pd_idx + 1) % raid_disks;
  1523. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1524. break;
  1525. case ALGORITHM_PARITY_0:
  1526. pd_idx = 0;
  1527. qd_idx = 1;
  1528. (*dd_idx) += 2;
  1529. break;
  1530. case ALGORITHM_PARITY_N:
  1531. pd_idx = data_disks;
  1532. qd_idx = data_disks + 1;
  1533. break;
  1534. case ALGORITHM_ROTATING_ZERO_RESTART:
  1535. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  1536. * of blocks for computing Q is different.
  1537. */
  1538. pd_idx = stripe % raid_disks;
  1539. qd_idx = pd_idx + 1;
  1540. if (pd_idx == raid_disks-1) {
  1541. (*dd_idx)++; /* Q D D D P */
  1542. qd_idx = 0;
  1543. } else if (*dd_idx >= pd_idx)
  1544. (*dd_idx) += 2; /* D D P Q D */
  1545. ddf_layout = 1;
  1546. break;
  1547. case ALGORITHM_ROTATING_N_RESTART:
  1548. /* Same a left_asymmetric, by first stripe is
  1549. * D D D P Q rather than
  1550. * Q D D D P
  1551. */
  1552. pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
  1553. qd_idx = pd_idx + 1;
  1554. if (pd_idx == raid_disks-1) {
  1555. (*dd_idx)++; /* Q D D D P */
  1556. qd_idx = 0;
  1557. } else if (*dd_idx >= pd_idx)
  1558. (*dd_idx) += 2; /* D D P Q D */
  1559. ddf_layout = 1;
  1560. break;
  1561. case ALGORITHM_ROTATING_N_CONTINUE:
  1562. /* Same as left_symmetric but Q is before P */
  1563. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1564. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  1565. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1566. ddf_layout = 1;
  1567. break;
  1568. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1569. /* RAID5 left_asymmetric, with Q on last device */
  1570. pd_idx = data_disks - stripe % (raid_disks-1);
  1571. if (*dd_idx >= pd_idx)
  1572. (*dd_idx)++;
  1573. qd_idx = raid_disks - 1;
  1574. break;
  1575. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1576. pd_idx = stripe % (raid_disks-1);
  1577. if (*dd_idx >= pd_idx)
  1578. (*dd_idx)++;
  1579. qd_idx = raid_disks - 1;
  1580. break;
  1581. case ALGORITHM_LEFT_SYMMETRIC_6:
  1582. pd_idx = data_disks - stripe % (raid_disks-1);
  1583. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1584. qd_idx = raid_disks - 1;
  1585. break;
  1586. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1587. pd_idx = stripe % (raid_disks-1);
  1588. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1589. qd_idx = raid_disks - 1;
  1590. break;
  1591. case ALGORITHM_PARITY_0_6:
  1592. pd_idx = 0;
  1593. (*dd_idx)++;
  1594. qd_idx = raid_disks - 1;
  1595. break;
  1596. default:
  1597. printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
  1598. algorithm);
  1599. BUG();
  1600. }
  1601. break;
  1602. }
  1603. if (sh) {
  1604. sh->pd_idx = pd_idx;
  1605. sh->qd_idx = qd_idx;
  1606. sh->ddf_layout = ddf_layout;
  1607. }
  1608. /*
  1609. * Finally, compute the new sector number
  1610. */
  1611. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  1612. return new_sector;
  1613. }
  1614. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
  1615. {
  1616. raid5_conf_t *conf = sh->raid_conf;
  1617. int raid_disks = sh->disks;
  1618. int data_disks = raid_disks - conf->max_degraded;
  1619. sector_t new_sector = sh->sector, check;
  1620. int sectors_per_chunk = previous ? conf->prev_chunk_sectors
  1621. : conf->chunk_sectors;
  1622. int algorithm = previous ? conf->prev_algo
  1623. : conf->algorithm;
  1624. sector_t stripe;
  1625. int chunk_offset;
  1626. int chunk_number, dummy1, dd_idx = i;
  1627. sector_t r_sector;
  1628. struct stripe_head sh2;
  1629. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  1630. stripe = new_sector;
  1631. BUG_ON(new_sector != stripe);
  1632. if (i == sh->pd_idx)
  1633. return 0;
  1634. switch(conf->level) {
  1635. case 4: break;
  1636. case 5:
  1637. switch (algorithm) {
  1638. case ALGORITHM_LEFT_ASYMMETRIC:
  1639. case ALGORITHM_RIGHT_ASYMMETRIC:
  1640. if (i > sh->pd_idx)
  1641. i--;
  1642. break;
  1643. case ALGORITHM_LEFT_SYMMETRIC:
  1644. case ALGORITHM_RIGHT_SYMMETRIC:
  1645. if (i < sh->pd_idx)
  1646. i += raid_disks;
  1647. i -= (sh->pd_idx + 1);
  1648. break;
  1649. case ALGORITHM_PARITY_0:
  1650. i -= 1;
  1651. break;
  1652. case ALGORITHM_PARITY_N:
  1653. break;
  1654. default:
  1655. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1656. algorithm);
  1657. BUG();
  1658. }
  1659. break;
  1660. case 6:
  1661. if (i == sh->qd_idx)
  1662. return 0; /* It is the Q disk */
  1663. switch (algorithm) {
  1664. case ALGORITHM_LEFT_ASYMMETRIC:
  1665. case ALGORITHM_RIGHT_ASYMMETRIC:
  1666. case ALGORITHM_ROTATING_ZERO_RESTART:
  1667. case ALGORITHM_ROTATING_N_RESTART:
  1668. if (sh->pd_idx == raid_disks-1)
  1669. i--; /* Q D D D P */
  1670. else if (i > sh->pd_idx)
  1671. i -= 2; /* D D P Q D */
  1672. break;
  1673. case ALGORITHM_LEFT_SYMMETRIC:
  1674. case ALGORITHM_RIGHT_SYMMETRIC:
  1675. if (sh->pd_idx == raid_disks-1)
  1676. i--; /* Q D D D P */
  1677. else {
  1678. /* D D P Q D */
  1679. if (i < sh->pd_idx)
  1680. i += raid_disks;
  1681. i -= (sh->pd_idx + 2);
  1682. }
  1683. break;
  1684. case ALGORITHM_PARITY_0:
  1685. i -= 2;
  1686. break;
  1687. case ALGORITHM_PARITY_N:
  1688. break;
  1689. case ALGORITHM_ROTATING_N_CONTINUE:
  1690. /* Like left_symmetric, but P is before Q */
  1691. if (sh->pd_idx == 0)
  1692. i--; /* P D D D Q */
  1693. else {
  1694. /* D D Q P D */
  1695. if (i < sh->pd_idx)
  1696. i += raid_disks;
  1697. i -= (sh->pd_idx + 1);
  1698. }
  1699. break;
  1700. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1701. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1702. if (i > sh->pd_idx)
  1703. i--;
  1704. break;
  1705. case ALGORITHM_LEFT_SYMMETRIC_6:
  1706. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1707. if (i < sh->pd_idx)
  1708. i += data_disks + 1;
  1709. i -= (sh->pd_idx + 1);
  1710. break;
  1711. case ALGORITHM_PARITY_0_6:
  1712. i -= 1;
  1713. break;
  1714. default:
  1715. printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
  1716. algorithm);
  1717. BUG();
  1718. }
  1719. break;
  1720. }
  1721. chunk_number = stripe * data_disks + i;
  1722. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  1723. check = raid5_compute_sector(conf, r_sector,
  1724. previous, &dummy1, &sh2);
  1725. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  1726. || sh2.qd_idx != sh->qd_idx) {
  1727. printk(KERN_ERR "compute_blocknr: map not correct\n");
  1728. return 0;
  1729. }
  1730. return r_sector;
  1731. }
  1732. static void
  1733. schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
  1734. int rcw, int expand)
  1735. {
  1736. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  1737. raid5_conf_t *conf = sh->raid_conf;
  1738. int level = conf->level;
  1739. if (rcw) {
  1740. /* if we are not expanding this is a proper write request, and
  1741. * there will be bios with new data to be drained into the
  1742. * stripe cache
  1743. */
  1744. if (!expand) {
  1745. sh->reconstruct_state = reconstruct_state_drain_run;
  1746. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1747. } else
  1748. sh->reconstruct_state = reconstruct_state_run;
  1749. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  1750. for (i = disks; i--; ) {
  1751. struct r5dev *dev = &sh->dev[i];
  1752. if (dev->towrite) {
  1753. set_bit(R5_LOCKED, &dev->flags);
  1754. set_bit(R5_Wantdrain, &dev->flags);
  1755. if (!expand)
  1756. clear_bit(R5_UPTODATE, &dev->flags);
  1757. s->locked++;
  1758. }
  1759. }
  1760. if (s->locked + conf->max_degraded == disks)
  1761. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  1762. atomic_inc(&conf->pending_full_writes);
  1763. } else {
  1764. BUG_ON(level == 6);
  1765. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  1766. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  1767. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  1768. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  1769. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1770. set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
  1771. for (i = disks; i--; ) {
  1772. struct r5dev *dev = &sh->dev[i];
  1773. if (i == pd_idx)
  1774. continue;
  1775. if (dev->towrite &&
  1776. (test_bit(R5_UPTODATE, &dev->flags) ||
  1777. test_bit(R5_Wantcompute, &dev->flags))) {
  1778. set_bit(R5_Wantdrain, &dev->flags);
  1779. set_bit(R5_LOCKED, &dev->flags);
  1780. clear_bit(R5_UPTODATE, &dev->flags);
  1781. s->locked++;
  1782. }
  1783. }
  1784. }
  1785. /* keep the parity disk(s) locked while asynchronous operations
  1786. * are in flight
  1787. */
  1788. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1789. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1790. s->locked++;
  1791. if (level == 6) {
  1792. int qd_idx = sh->qd_idx;
  1793. struct r5dev *dev = &sh->dev[qd_idx];
  1794. set_bit(R5_LOCKED, &dev->flags);
  1795. clear_bit(R5_UPTODATE, &dev->flags);
  1796. s->locked++;
  1797. }
  1798. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  1799. __func__, (unsigned long long)sh->sector,
  1800. s->locked, s->ops_request);
  1801. }
  1802. /*
  1803. * Each stripe/dev can have one or more bion attached.
  1804. * toread/towrite point to the first in a chain.
  1805. * The bi_next chain must be in order.
  1806. */
  1807. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1808. {
  1809. struct bio **bip;
  1810. raid5_conf_t *conf = sh->raid_conf;
  1811. int firstwrite=0;
  1812. pr_debug("adding bh b#%llu to stripe s#%llu\n",
  1813. (unsigned long long)bi->bi_sector,
  1814. (unsigned long long)sh->sector);
  1815. spin_lock(&sh->lock);
  1816. spin_lock_irq(&conf->device_lock);
  1817. if (forwrite) {
  1818. bip = &sh->dev[dd_idx].towrite;
  1819. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1820. firstwrite = 1;
  1821. } else
  1822. bip = &sh->dev[dd_idx].toread;
  1823. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1824. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1825. goto overlap;
  1826. bip = & (*bip)->bi_next;
  1827. }
  1828. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1829. goto overlap;
  1830. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1831. if (*bip)
  1832. bi->bi_next = *bip;
  1833. *bip = bi;
  1834. bi->bi_phys_segments++;
  1835. spin_unlock_irq(&conf->device_lock);
  1836. spin_unlock(&sh->lock);
  1837. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1838. (unsigned long long)bi->bi_sector,
  1839. (unsigned long long)sh->sector, dd_idx);
  1840. if (conf->mddev->bitmap && firstwrite) {
  1841. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1842. STRIPE_SECTORS, 0);
  1843. sh->bm_seq = conf->seq_flush+1;
  1844. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1845. }
  1846. if (forwrite) {
  1847. /* check if page is covered */
  1848. sector_t sector = sh->dev[dd_idx].sector;
  1849. for (bi=sh->dev[dd_idx].towrite;
  1850. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1851. bi && bi->bi_sector <= sector;
  1852. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1853. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1854. sector = bi->bi_sector + (bi->bi_size>>9);
  1855. }
  1856. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1857. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1858. }
  1859. return 1;
  1860. overlap:
  1861. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1862. spin_unlock_irq(&conf->device_lock);
  1863. spin_unlock(&sh->lock);
  1864. return 0;
  1865. }
  1866. static void end_reshape(raid5_conf_t *conf);
  1867. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  1868. struct stripe_head *sh)
  1869. {
  1870. int sectors_per_chunk =
  1871. previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
  1872. int dd_idx;
  1873. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1874. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  1875. raid5_compute_sector(conf,
  1876. stripe * (disks - conf->max_degraded)
  1877. *sectors_per_chunk + chunk_offset,
  1878. previous,
  1879. &dd_idx, sh);
  1880. }
  1881. static void
  1882. handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
  1883. struct stripe_head_state *s, int disks,
  1884. struct bio **return_bi)
  1885. {
  1886. int i;
  1887. for (i = disks; i--; ) {
  1888. struct bio *bi;
  1889. int bitmap_end = 0;
  1890. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1891. mdk_rdev_t *rdev;
  1892. rcu_read_lock();
  1893. rdev = rcu_dereference(conf->disks[i].rdev);
  1894. if (rdev && test_bit(In_sync, &rdev->flags))
  1895. /* multiple read failures in one stripe */
  1896. md_error(conf->mddev, rdev);
  1897. rcu_read_unlock();
  1898. }
  1899. spin_lock_irq(&conf->device_lock);
  1900. /* fail all writes first */
  1901. bi = sh->dev[i].towrite;
  1902. sh->dev[i].towrite = NULL;
  1903. if (bi) {
  1904. s->to_write--;
  1905. bitmap_end = 1;
  1906. }
  1907. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1908. wake_up(&conf->wait_for_overlap);
  1909. while (bi && bi->bi_sector <
  1910. sh->dev[i].sector + STRIPE_SECTORS) {
  1911. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1912. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1913. if (!raid5_dec_bi_phys_segments(bi)) {
  1914. md_write_end(conf->mddev);
  1915. bi->bi_next = *return_bi;
  1916. *return_bi = bi;
  1917. }
  1918. bi = nextbi;
  1919. }
  1920. /* and fail all 'written' */
  1921. bi = sh->dev[i].written;
  1922. sh->dev[i].written = NULL;
  1923. if (bi) bitmap_end = 1;
  1924. while (bi && bi->bi_sector <
  1925. sh->dev[i].sector + STRIPE_SECTORS) {
  1926. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1927. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1928. if (!raid5_dec_bi_phys_segments(bi)) {
  1929. md_write_end(conf->mddev);
  1930. bi->bi_next = *return_bi;
  1931. *return_bi = bi;
  1932. }
  1933. bi = bi2;
  1934. }
  1935. /* fail any reads if this device is non-operational and
  1936. * the data has not reached the cache yet.
  1937. */
  1938. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  1939. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1940. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  1941. bi = sh->dev[i].toread;
  1942. sh->dev[i].toread = NULL;
  1943. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1944. wake_up(&conf->wait_for_overlap);
  1945. if (bi) s->to_read--;
  1946. while (bi && bi->bi_sector <
  1947. sh->dev[i].sector + STRIPE_SECTORS) {
  1948. struct bio *nextbi =
  1949. r5_next_bio(bi, sh->dev[i].sector);
  1950. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1951. if (!raid5_dec_bi_phys_segments(bi)) {
  1952. bi->bi_next = *return_bi;
  1953. *return_bi = bi;
  1954. }
  1955. bi = nextbi;
  1956. }
  1957. }
  1958. spin_unlock_irq(&conf->device_lock);
  1959. if (bitmap_end)
  1960. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1961. STRIPE_SECTORS, 0, 0);
  1962. }
  1963. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  1964. if (atomic_dec_and_test(&conf->pending_full_writes))
  1965. md_wakeup_thread(conf->mddev->thread);
  1966. }
  1967. /* fetch_block5 - checks the given member device to see if its data needs
  1968. * to be read or computed to satisfy a request.
  1969. *
  1970. * Returns 1 when no more member devices need to be checked, otherwise returns
  1971. * 0 to tell the loop in handle_stripe_fill5 to continue
  1972. */
  1973. static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
  1974. int disk_idx, int disks)
  1975. {
  1976. struct r5dev *dev = &sh->dev[disk_idx];
  1977. struct r5dev *failed_dev = &sh->dev[s->failed_num];
  1978. /* is the data in this block needed, and can we get it? */
  1979. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1980. !test_bit(R5_UPTODATE, &dev->flags) &&
  1981. (dev->toread ||
  1982. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1983. s->syncing || s->expanding ||
  1984. (s->failed &&
  1985. (failed_dev->toread ||
  1986. (failed_dev->towrite &&
  1987. !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
  1988. /* We would like to get this block, possibly by computing it,
  1989. * otherwise read it if the backing disk is insync
  1990. */
  1991. if ((s->uptodate == disks - 1) &&
  1992. (s->failed && disk_idx == s->failed_num)) {
  1993. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  1994. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  1995. set_bit(R5_Wantcompute, &dev->flags);
  1996. sh->ops.target = disk_idx;
  1997. sh->ops.target2 = -1;
  1998. s->req_compute = 1;
  1999. /* Careful: from this point on 'uptodate' is in the eye
  2000. * of raid_run_ops which services 'compute' operations
  2001. * before writes. R5_Wantcompute flags a block that will
  2002. * be R5_UPTODATE by the time it is needed for a
  2003. * subsequent operation.
  2004. */
  2005. s->uptodate++;
  2006. return 1; /* uptodate + compute == disks */
  2007. } else if (test_bit(R5_Insync, &dev->flags)) {
  2008. set_bit(R5_LOCKED, &dev->flags);
  2009. set_bit(R5_Wantread, &dev->flags);
  2010. s->locked++;
  2011. pr_debug("Reading block %d (sync=%d)\n", disk_idx,
  2012. s->syncing);
  2013. }
  2014. }
  2015. return 0;
  2016. }
  2017. /**
  2018. * handle_stripe_fill5 - read or compute data to satisfy pending requests.
  2019. */
  2020. static void handle_stripe_fill5(struct stripe_head *sh,
  2021. struct stripe_head_state *s, int disks)
  2022. {
  2023. int i;
  2024. /* look for blocks to read/compute, skip this if a compute
  2025. * is already in flight, or if the stripe contents are in the
  2026. * midst of changing due to a write
  2027. */
  2028. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  2029. !sh->reconstruct_state)
  2030. for (i = disks; i--; )
  2031. if (fetch_block5(sh, s, i, disks))
  2032. break;
  2033. set_bit(STRIPE_HANDLE, &sh->state);
  2034. }
  2035. /* fetch_block6 - checks the given member device to see if its data needs
  2036. * to be read or computed to satisfy a request.
  2037. *
  2038. * Returns 1 when no more member devices need to be checked, otherwise returns
  2039. * 0 to tell the loop in handle_stripe_fill6 to continue
  2040. */
  2041. static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
  2042. struct r6_state *r6s, int disk_idx, int disks)
  2043. {
  2044. struct r5dev *dev = &sh->dev[disk_idx];
  2045. struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
  2046. &sh->dev[r6s->failed_num[1]] };
  2047. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2048. !test_bit(R5_UPTODATE, &dev->flags) &&
  2049. (dev->toread ||
  2050. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  2051. s->syncing || s->expanding ||
  2052. (s->failed >= 1 &&
  2053. (fdev[0]->toread || s->to_write)) ||
  2054. (s->failed >= 2 &&
  2055. (fdev[1]->toread || s->to_write)))) {
  2056. /* we would like to get this block, possibly by computing it,
  2057. * otherwise read it if the backing disk is insync
  2058. */
  2059. BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
  2060. BUG_ON(test_bit(R5_Wantread, &dev->flags));
  2061. if ((s->uptodate == disks - 1) &&
  2062. (s->failed && (disk_idx == r6s->failed_num[0] ||
  2063. disk_idx == r6s->failed_num[1]))) {
  2064. /* have disk failed, and we're requested to fetch it;
  2065. * do compute it
  2066. */
  2067. pr_debug("Computing stripe %llu block %d\n",
  2068. (unsigned long long)sh->sector, disk_idx);
  2069. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2070. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2071. set_bit(R5_Wantcompute, &dev->flags);
  2072. sh->ops.target = disk_idx;
  2073. sh->ops.target2 = -1; /* no 2nd target */
  2074. s->req_compute = 1;
  2075. s->uptodate++;
  2076. return 1;
  2077. } else if (s->uptodate == disks-2 && s->failed >= 2) {
  2078. /* Computing 2-failure is *very* expensive; only
  2079. * do it if failed >= 2
  2080. */
  2081. int other;
  2082. for (other = disks; other--; ) {
  2083. if (other == disk_idx)
  2084. continue;
  2085. if (!test_bit(R5_UPTODATE,
  2086. &sh->dev[other].flags))
  2087. break;
  2088. }
  2089. BUG_ON(other < 0);
  2090. pr_debug("Computing stripe %llu blocks %d,%d\n",
  2091. (unsigned long long)sh->sector,
  2092. disk_idx, other);
  2093. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2094. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2095. set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
  2096. set_bit(R5_Wantcompute, &sh->dev[other].flags);
  2097. sh->ops.target = disk_idx;
  2098. sh->ops.target2 = other;
  2099. s->uptodate += 2;
  2100. s->req_compute = 1;
  2101. return 1;
  2102. } else if (test_bit(R5_Insync, &dev->flags)) {
  2103. set_bit(R5_LOCKED, &dev->flags);
  2104. set_bit(R5_Wantread, &dev->flags);
  2105. s->locked++;
  2106. pr_debug("Reading block %d (sync=%d)\n",
  2107. disk_idx, s->syncing);
  2108. }
  2109. }
  2110. return 0;
  2111. }
  2112. /**
  2113. * handle_stripe_fill6 - read or compute data to satisfy pending requests.
  2114. */
  2115. static void handle_stripe_fill6(struct stripe_head *sh,
  2116. struct stripe_head_state *s, struct r6_state *r6s,
  2117. int disks)
  2118. {
  2119. int i;
  2120. /* look for blocks to read/compute, skip this if a compute
  2121. * is already in flight, or if the stripe contents are in the
  2122. * midst of changing due to a write
  2123. */
  2124. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  2125. !sh->reconstruct_state)
  2126. for (i = disks; i--; )
  2127. if (fetch_block6(sh, s, r6s, i, disks))
  2128. break;
  2129. set_bit(STRIPE_HANDLE, &sh->state);
  2130. }
  2131. /* handle_stripe_clean_event
  2132. * any written block on an uptodate or failed drive can be returned.
  2133. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  2134. * never LOCKED, so we don't need to test 'failed' directly.
  2135. */
  2136. static void handle_stripe_clean_event(raid5_conf_t *conf,
  2137. struct stripe_head *sh, int disks, struct bio **return_bi)
  2138. {
  2139. int i;
  2140. struct r5dev *dev;
  2141. for (i = disks; i--; )
  2142. if (sh->dev[i].written) {
  2143. dev = &sh->dev[i];
  2144. if (!test_bit(R5_LOCKED, &dev->flags) &&
  2145. test_bit(R5_UPTODATE, &dev->flags)) {
  2146. /* We can return any write requests */
  2147. struct bio *wbi, *wbi2;
  2148. int bitmap_end = 0;
  2149. pr_debug("Return write for disc %d\n", i);
  2150. spin_lock_irq(&conf->device_lock);
  2151. wbi = dev->written;
  2152. dev->written = NULL;
  2153. while (wbi && wbi->bi_sector <
  2154. dev->sector + STRIPE_SECTORS) {
  2155. wbi2 = r5_next_bio(wbi, dev->sector);
  2156. if (!raid5_dec_bi_phys_segments(wbi)) {
  2157. md_write_end(conf->mddev);
  2158. wbi->bi_next = *return_bi;
  2159. *return_bi = wbi;
  2160. }
  2161. wbi = wbi2;
  2162. }
  2163. if (dev->towrite == NULL)
  2164. bitmap_end = 1;
  2165. spin_unlock_irq(&conf->device_lock);
  2166. if (bitmap_end)
  2167. bitmap_endwrite(conf->mddev->bitmap,
  2168. sh->sector,
  2169. STRIPE_SECTORS,
  2170. !test_bit(STRIPE_DEGRADED, &sh->state),
  2171. 0);
  2172. }
  2173. }
  2174. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2175. if (atomic_dec_and_test(&conf->pending_full_writes))
  2176. md_wakeup_thread(conf->mddev->thread);
  2177. }
  2178. static void handle_stripe_dirtying5(raid5_conf_t *conf,
  2179. struct stripe_head *sh, struct stripe_head_state *s, int disks)
  2180. {
  2181. int rmw = 0, rcw = 0, i;
  2182. for (i = disks; i--; ) {
  2183. /* would I have to read this buffer for read_modify_write */
  2184. struct r5dev *dev = &sh->dev[i];
  2185. if ((dev->towrite || i == sh->pd_idx) &&
  2186. !test_bit(R5_LOCKED, &dev->flags) &&
  2187. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2188. test_bit(R5_Wantcompute, &dev->flags))) {
  2189. if (test_bit(R5_Insync, &dev->flags))
  2190. rmw++;
  2191. else
  2192. rmw += 2*disks; /* cannot read it */
  2193. }
  2194. /* Would I have to read this buffer for reconstruct_write */
  2195. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  2196. !test_bit(R5_LOCKED, &dev->flags) &&
  2197. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2198. test_bit(R5_Wantcompute, &dev->flags))) {
  2199. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2200. else
  2201. rcw += 2*disks;
  2202. }
  2203. }
  2204. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  2205. (unsigned long long)sh->sector, rmw, rcw);
  2206. set_bit(STRIPE_HANDLE, &sh->state);
  2207. if (rmw < rcw && rmw > 0)
  2208. /* prefer read-modify-write, but need to get some data */
  2209. for (i = disks; i--; ) {
  2210. struct r5dev *dev = &sh->dev[i];
  2211. if ((dev->towrite || i == sh->pd_idx) &&
  2212. !test_bit(R5_LOCKED, &dev->flags) &&
  2213. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2214. test_bit(R5_Wantcompute, &dev->flags)) &&
  2215. test_bit(R5_Insync, &dev->flags)) {
  2216. if (
  2217. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2218. pr_debug("Read_old block "
  2219. "%d for r-m-w\n", i);
  2220. set_bit(R5_LOCKED, &dev->flags);
  2221. set_bit(R5_Wantread, &dev->flags);
  2222. s->locked++;
  2223. } else {
  2224. set_bit(STRIPE_DELAYED, &sh->state);
  2225. set_bit(STRIPE_HANDLE, &sh->state);
  2226. }
  2227. }
  2228. }
  2229. if (rcw <= rmw && rcw > 0)
  2230. /* want reconstruct write, but need to get some data */
  2231. for (i = disks; i--; ) {
  2232. struct r5dev *dev = &sh->dev[i];
  2233. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2234. i != sh->pd_idx &&
  2235. !test_bit(R5_LOCKED, &dev->flags) &&
  2236. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2237. test_bit(R5_Wantcompute, &dev->flags)) &&
  2238. test_bit(R5_Insync, &dev->flags)) {
  2239. if (
  2240. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2241. pr_debug("Read_old block "
  2242. "%d for Reconstruct\n", i);
  2243. set_bit(R5_LOCKED, &dev->flags);
  2244. set_bit(R5_Wantread, &dev->flags);
  2245. s->locked++;
  2246. } else {
  2247. set_bit(STRIPE_DELAYED, &sh->state);
  2248. set_bit(STRIPE_HANDLE, &sh->state);
  2249. }
  2250. }
  2251. }
  2252. /* now if nothing is locked, and if we have enough data,
  2253. * we can start a write request
  2254. */
  2255. /* since handle_stripe can be called at any time we need to handle the
  2256. * case where a compute block operation has been submitted and then a
  2257. * subsequent call wants to start a write request. raid_run_ops only
  2258. * handles the case where compute block and reconstruct are requested
  2259. * simultaneously. If this is not the case then new writes need to be
  2260. * held off until the compute completes.
  2261. */
  2262. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2263. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  2264. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  2265. schedule_reconstruction(sh, s, rcw == 0, 0);
  2266. }
  2267. static void handle_stripe_dirtying6(raid5_conf_t *conf,
  2268. struct stripe_head *sh, struct stripe_head_state *s,
  2269. struct r6_state *r6s, int disks)
  2270. {
  2271. int rcw = 0, pd_idx = sh->pd_idx, i;
  2272. int qd_idx = sh->qd_idx;
  2273. set_bit(STRIPE_HANDLE, &sh->state);
  2274. for (i = disks; i--; ) {
  2275. struct r5dev *dev = &sh->dev[i];
  2276. /* check if we haven't enough data */
  2277. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2278. i != pd_idx && i != qd_idx &&
  2279. !test_bit(R5_LOCKED, &dev->flags) &&
  2280. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2281. test_bit(R5_Wantcompute, &dev->flags))) {
  2282. rcw++;
  2283. if (!test_bit(R5_Insync, &dev->flags))
  2284. continue; /* it's a failed drive */
  2285. if (
  2286. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2287. pr_debug("Read_old stripe %llu "
  2288. "block %d for Reconstruct\n",
  2289. (unsigned long long)sh->sector, i);
  2290. set_bit(R5_LOCKED, &dev->flags);
  2291. set_bit(R5_Wantread, &dev->flags);
  2292. s->locked++;
  2293. } else {
  2294. pr_debug("Request delayed stripe %llu "
  2295. "block %d for Reconstruct\n",
  2296. (unsigned long long)sh->sector, i);
  2297. set_bit(STRIPE_DELAYED, &sh->state);
  2298. set_bit(STRIPE_HANDLE, &sh->state);
  2299. }
  2300. }
  2301. }
  2302. /* now if nothing is locked, and if we have enough data, we can start a
  2303. * write request
  2304. */
  2305. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2306. s->locked == 0 && rcw == 0 &&
  2307. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  2308. schedule_reconstruction(sh, s, 1, 0);
  2309. }
  2310. }
  2311. static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
  2312. struct stripe_head_state *s, int disks)
  2313. {
  2314. struct r5dev *dev = NULL;
  2315. set_bit(STRIPE_HANDLE, &sh->state);
  2316. switch (sh->check_state) {
  2317. case check_state_idle:
  2318. /* start a new check operation if there are no failures */
  2319. if (s->failed == 0) {
  2320. BUG_ON(s->uptodate != disks);
  2321. sh->check_state = check_state_run;
  2322. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2323. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2324. s->uptodate--;
  2325. break;
  2326. }
  2327. dev = &sh->dev[s->failed_num];
  2328. /* fall through */
  2329. case check_state_compute_result:
  2330. sh->check_state = check_state_idle;
  2331. if (!dev)
  2332. dev = &sh->dev[sh->pd_idx];
  2333. /* check that a write has not made the stripe insync */
  2334. if (test_bit(STRIPE_INSYNC, &sh->state))
  2335. break;
  2336. /* either failed parity check, or recovery is happening */
  2337. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2338. BUG_ON(s->uptodate != disks);
  2339. set_bit(R5_LOCKED, &dev->flags);
  2340. s->locked++;
  2341. set_bit(R5_Wantwrite, &dev->flags);
  2342. clear_bit(STRIPE_DEGRADED, &sh->state);
  2343. set_bit(STRIPE_INSYNC, &sh->state);
  2344. break;
  2345. case check_state_run:
  2346. break; /* we will be called again upon completion */
  2347. case check_state_check_result:
  2348. sh->check_state = check_state_idle;
  2349. /* if a failure occurred during the check operation, leave
  2350. * STRIPE_INSYNC not set and let the stripe be handled again
  2351. */
  2352. if (s->failed)
  2353. break;
  2354. /* handle a successful check operation, if parity is correct
  2355. * we are done. Otherwise update the mismatch count and repair
  2356. * parity if !MD_RECOVERY_CHECK
  2357. */
  2358. if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
  2359. /* parity is correct (on disc,
  2360. * not in buffer any more)
  2361. */
  2362. set_bit(STRIPE_INSYNC, &sh->state);
  2363. else {
  2364. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2365. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2366. /* don't try to repair!! */
  2367. set_bit(STRIPE_INSYNC, &sh->state);
  2368. else {
  2369. sh->check_state = check_state_compute_run;
  2370. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2371. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2372. set_bit(R5_Wantcompute,
  2373. &sh->dev[sh->pd_idx].flags);
  2374. sh->ops.target = sh->pd_idx;
  2375. sh->ops.target2 = -1;
  2376. s->uptodate++;
  2377. }
  2378. }
  2379. break;
  2380. case check_state_compute_run:
  2381. break;
  2382. default:
  2383. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2384. __func__, sh->check_state,
  2385. (unsigned long long) sh->sector);
  2386. BUG();
  2387. }
  2388. }
  2389. static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
  2390. struct stripe_head_state *s,
  2391. struct r6_state *r6s, int disks)
  2392. {
  2393. int pd_idx = sh->pd_idx;
  2394. int qd_idx = sh->qd_idx;
  2395. struct r5dev *dev;
  2396. set_bit(STRIPE_HANDLE, &sh->state);
  2397. BUG_ON(s->failed > 2);
  2398. /* Want to check and possibly repair P and Q.
  2399. * However there could be one 'failed' device, in which
  2400. * case we can only check one of them, possibly using the
  2401. * other to generate missing data
  2402. */
  2403. switch (sh->check_state) {
  2404. case check_state_idle:
  2405. /* start a new check operation if there are < 2 failures */
  2406. if (s->failed == r6s->q_failed) {
  2407. /* The only possible failed device holds Q, so it
  2408. * makes sense to check P (If anything else were failed,
  2409. * we would have used P to recreate it).
  2410. */
  2411. sh->check_state = check_state_run;
  2412. }
  2413. if (!r6s->q_failed && s->failed < 2) {
  2414. /* Q is not failed, and we didn't use it to generate
  2415. * anything, so it makes sense to check it
  2416. */
  2417. if (sh->check_state == check_state_run)
  2418. sh->check_state = check_state_run_pq;
  2419. else
  2420. sh->check_state = check_state_run_q;
  2421. }
  2422. /* discard potentially stale zero_sum_result */
  2423. sh->ops.zero_sum_result = 0;
  2424. if (sh->check_state == check_state_run) {
  2425. /* async_xor_zero_sum destroys the contents of P */
  2426. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  2427. s->uptodate--;
  2428. }
  2429. if (sh->check_state >= check_state_run &&
  2430. sh->check_state <= check_state_run_pq) {
  2431. /* async_syndrome_zero_sum preserves P and Q, so
  2432. * no need to mark them !uptodate here
  2433. */
  2434. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2435. break;
  2436. }
  2437. /* we have 2-disk failure */
  2438. BUG_ON(s->failed != 2);
  2439. /* fall through */
  2440. case check_state_compute_result:
  2441. sh->check_state = check_state_idle;
  2442. /* check that a write has not made the stripe insync */
  2443. if (test_bit(STRIPE_INSYNC, &sh->state))
  2444. break;
  2445. /* now write out any block on a failed drive,
  2446. * or P or Q if they were recomputed
  2447. */
  2448. BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
  2449. if (s->failed == 2) {
  2450. dev = &sh->dev[r6s->failed_num[1]];
  2451. s->locked++;
  2452. set_bit(R5_LOCKED, &dev->flags);
  2453. set_bit(R5_Wantwrite, &dev->flags);
  2454. }
  2455. if (s->failed >= 1) {
  2456. dev = &sh->dev[r6s->failed_num[0]];
  2457. s->locked++;
  2458. set_bit(R5_LOCKED, &dev->flags);
  2459. set_bit(R5_Wantwrite, &dev->flags);
  2460. }
  2461. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2462. dev = &sh->dev[pd_idx];
  2463. s->locked++;
  2464. set_bit(R5_LOCKED, &dev->flags);
  2465. set_bit(R5_Wantwrite, &dev->flags);
  2466. }
  2467. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2468. dev = &sh->dev[qd_idx];
  2469. s->locked++;
  2470. set_bit(R5_LOCKED, &dev->flags);
  2471. set_bit(R5_Wantwrite, &dev->flags);
  2472. }
  2473. clear_bit(STRIPE_DEGRADED, &sh->state);
  2474. set_bit(STRIPE_INSYNC, &sh->state);
  2475. break;
  2476. case check_state_run:
  2477. case check_state_run_q:
  2478. case check_state_run_pq:
  2479. break; /* we will be called again upon completion */
  2480. case check_state_check_result:
  2481. sh->check_state = check_state_idle;
  2482. /* handle a successful check operation, if parity is correct
  2483. * we are done. Otherwise update the mismatch count and repair
  2484. * parity if !MD_RECOVERY_CHECK
  2485. */
  2486. if (sh->ops.zero_sum_result == 0) {
  2487. /* both parities are correct */
  2488. if (!s->failed)
  2489. set_bit(STRIPE_INSYNC, &sh->state);
  2490. else {
  2491. /* in contrast to the raid5 case we can validate
  2492. * parity, but still have a failure to write
  2493. * back
  2494. */
  2495. sh->check_state = check_state_compute_result;
  2496. /* Returning at this point means that we may go
  2497. * off and bring p and/or q uptodate again so
  2498. * we make sure to check zero_sum_result again
  2499. * to verify if p or q need writeback
  2500. */
  2501. }
  2502. } else {
  2503. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2504. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2505. /* don't try to repair!! */
  2506. set_bit(STRIPE_INSYNC, &sh->state);
  2507. else {
  2508. int *target = &sh->ops.target;
  2509. sh->ops.target = -1;
  2510. sh->ops.target2 = -1;
  2511. sh->check_state = check_state_compute_run;
  2512. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2513. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2514. if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
  2515. set_bit(R5_Wantcompute,
  2516. &sh->dev[pd_idx].flags);
  2517. *target = pd_idx;
  2518. target = &sh->ops.target2;
  2519. s->uptodate++;
  2520. }
  2521. if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
  2522. set_bit(R5_Wantcompute,
  2523. &sh->dev[qd_idx].flags);
  2524. *target = qd_idx;
  2525. s->uptodate++;
  2526. }
  2527. }
  2528. }
  2529. break;
  2530. case check_state_compute_run:
  2531. break;
  2532. default:
  2533. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2534. __func__, sh->check_state,
  2535. (unsigned long long) sh->sector);
  2536. BUG();
  2537. }
  2538. }
  2539. static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
  2540. struct r6_state *r6s)
  2541. {
  2542. int i;
  2543. /* We have read all the blocks in this stripe and now we need to
  2544. * copy some of them into a target stripe for expand.
  2545. */
  2546. struct dma_async_tx_descriptor *tx = NULL;
  2547. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2548. for (i = 0; i < sh->disks; i++)
  2549. if (i != sh->pd_idx && i != sh->qd_idx) {
  2550. int dd_idx, j;
  2551. struct stripe_head *sh2;
  2552. struct async_submit_ctl submit;
  2553. sector_t bn = compute_blocknr(sh, i, 1);
  2554. sector_t s = raid5_compute_sector(conf, bn, 0,
  2555. &dd_idx, NULL);
  2556. sh2 = get_active_stripe(conf, s, 0, 1, 1);
  2557. if (sh2 == NULL)
  2558. /* so far only the early blocks of this stripe
  2559. * have been requested. When later blocks
  2560. * get requested, we will try again
  2561. */
  2562. continue;
  2563. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2564. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2565. /* must have already done this block */
  2566. release_stripe(sh2);
  2567. continue;
  2568. }
  2569. /* place all the copies on one channel */
  2570. init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
  2571. tx = async_memcpy(sh2->dev[dd_idx].page,
  2572. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2573. &submit);
  2574. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2575. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2576. for (j = 0; j < conf->raid_disks; j++)
  2577. if (j != sh2->pd_idx &&
  2578. (!r6s || j != sh2->qd_idx) &&
  2579. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2580. break;
  2581. if (j == conf->raid_disks) {
  2582. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2583. set_bit(STRIPE_HANDLE, &sh2->state);
  2584. }
  2585. release_stripe(sh2);
  2586. }
  2587. /* done submitting copies, wait for them to complete */
  2588. if (tx) {
  2589. async_tx_ack(tx);
  2590. dma_wait_for_async_tx(tx);
  2591. }
  2592. }
  2593. /*
  2594. * handle_stripe - do things to a stripe.
  2595. *
  2596. * We lock the stripe and then examine the state of various bits
  2597. * to see what needs to be done.
  2598. * Possible results:
  2599. * return some read request which now have data
  2600. * return some write requests which are safely on disc
  2601. * schedule a read on some buffers
  2602. * schedule a write of some buffers
  2603. * return confirmation of parity correctness
  2604. *
  2605. * buffers are taken off read_list or write_list, and bh_cache buffers
  2606. * get BH_Lock set before the stripe lock is released.
  2607. *
  2608. */
  2609. static void handle_stripe5(struct stripe_head *sh)
  2610. {
  2611. raid5_conf_t *conf = sh->raid_conf;
  2612. int disks = sh->disks, i;
  2613. struct bio *return_bi = NULL;
  2614. struct stripe_head_state s;
  2615. struct r5dev *dev;
  2616. mdk_rdev_t *blocked_rdev = NULL;
  2617. int prexor;
  2618. int dec_preread_active = 0;
  2619. memset(&s, 0, sizeof(s));
  2620. pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
  2621. "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
  2622. atomic_read(&sh->count), sh->pd_idx, sh->check_state,
  2623. sh->reconstruct_state);
  2624. spin_lock(&sh->lock);
  2625. clear_bit(STRIPE_HANDLE, &sh->state);
  2626. clear_bit(STRIPE_DELAYED, &sh->state);
  2627. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2628. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2629. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2630. /* Now to look around and see what can be done */
  2631. rcu_read_lock();
  2632. for (i=disks; i--; ) {
  2633. mdk_rdev_t *rdev;
  2634. dev = &sh->dev[i];
  2635. clear_bit(R5_Insync, &dev->flags);
  2636. pr_debug("check %d: state 0x%lx toread %p read %p write %p "
  2637. "written %p\n", i, dev->flags, dev->toread, dev->read,
  2638. dev->towrite, dev->written);
  2639. /* maybe we can request a biofill operation
  2640. *
  2641. * new wantfill requests are only permitted while
  2642. * ops_complete_biofill is guaranteed to be inactive
  2643. */
  2644. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2645. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2646. set_bit(R5_Wantfill, &dev->flags);
  2647. /* now count some things */
  2648. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2649. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2650. if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
  2651. if (test_bit(R5_Wantfill, &dev->flags))
  2652. s.to_fill++;
  2653. else if (dev->toread)
  2654. s.to_read++;
  2655. if (dev->towrite) {
  2656. s.to_write++;
  2657. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2658. s.non_overwrite++;
  2659. }
  2660. if (dev->written)
  2661. s.written++;
  2662. rdev = rcu_dereference(conf->disks[i].rdev);
  2663. if (blocked_rdev == NULL &&
  2664. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2665. blocked_rdev = rdev;
  2666. atomic_inc(&rdev->nr_pending);
  2667. }
  2668. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2669. /* The ReadError flag will just be confusing now */
  2670. clear_bit(R5_ReadError, &dev->flags);
  2671. clear_bit(R5_ReWrite, &dev->flags);
  2672. }
  2673. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2674. || test_bit(R5_ReadError, &dev->flags)) {
  2675. s.failed++;
  2676. s.failed_num = i;
  2677. } else
  2678. set_bit(R5_Insync, &dev->flags);
  2679. }
  2680. rcu_read_unlock();
  2681. if (unlikely(blocked_rdev)) {
  2682. if (s.syncing || s.expanding || s.expanded ||
  2683. s.to_write || s.written) {
  2684. set_bit(STRIPE_HANDLE, &sh->state);
  2685. goto unlock;
  2686. }
  2687. /* There is nothing for the blocked_rdev to block */
  2688. rdev_dec_pending(blocked_rdev, conf->mddev);
  2689. blocked_rdev = NULL;
  2690. }
  2691. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  2692. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  2693. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  2694. }
  2695. pr_debug("locked=%d uptodate=%d to_read=%d"
  2696. " to_write=%d failed=%d failed_num=%d\n",
  2697. s.locked, s.uptodate, s.to_read, s.to_write,
  2698. s.failed, s.failed_num);
  2699. /* check if the array has lost two devices and, if so, some requests might
  2700. * need to be failed
  2701. */
  2702. if (s.failed > 1 && s.to_read+s.to_write+s.written)
  2703. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2704. if (s.failed > 1 && s.syncing) {
  2705. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2706. clear_bit(STRIPE_SYNCING, &sh->state);
  2707. s.syncing = 0;
  2708. }
  2709. /* might be able to return some write requests if the parity block
  2710. * is safe, or on a failed drive
  2711. */
  2712. dev = &sh->dev[sh->pd_idx];
  2713. if ( s.written &&
  2714. ((test_bit(R5_Insync, &dev->flags) &&
  2715. !test_bit(R5_LOCKED, &dev->flags) &&
  2716. test_bit(R5_UPTODATE, &dev->flags)) ||
  2717. (s.failed == 1 && s.failed_num == sh->pd_idx)))
  2718. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2719. /* Now we might consider reading some blocks, either to check/generate
  2720. * parity, or to satisfy requests
  2721. * or to load a block that is being partially written.
  2722. */
  2723. if (s.to_read || s.non_overwrite ||
  2724. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
  2725. handle_stripe_fill5(sh, &s, disks);
  2726. /* Now we check to see if any write operations have recently
  2727. * completed
  2728. */
  2729. prexor = 0;
  2730. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  2731. prexor = 1;
  2732. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  2733. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  2734. sh->reconstruct_state = reconstruct_state_idle;
  2735. /* All the 'written' buffers and the parity block are ready to
  2736. * be written back to disk
  2737. */
  2738. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  2739. for (i = disks; i--; ) {
  2740. dev = &sh->dev[i];
  2741. if (test_bit(R5_LOCKED, &dev->flags) &&
  2742. (i == sh->pd_idx || dev->written)) {
  2743. pr_debug("Writing block %d\n", i);
  2744. set_bit(R5_Wantwrite, &dev->flags);
  2745. if (prexor)
  2746. continue;
  2747. if (!test_bit(R5_Insync, &dev->flags) ||
  2748. (i == sh->pd_idx && s.failed == 0))
  2749. set_bit(STRIPE_INSYNC, &sh->state);
  2750. }
  2751. }
  2752. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2753. dec_preread_active = 1;
  2754. }
  2755. /* Now to consider new write requests and what else, if anything
  2756. * should be read. We do not handle new writes when:
  2757. * 1/ A 'write' operation (copy+xor) is already in flight.
  2758. * 2/ A 'check' operation is in flight, as it may clobber the parity
  2759. * block.
  2760. */
  2761. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  2762. handle_stripe_dirtying5(conf, sh, &s, disks);
  2763. /* maybe we need to check and possibly fix the parity for this stripe
  2764. * Any reads will already have been scheduled, so we just see if enough
  2765. * data is available. The parity check is held off while parity
  2766. * dependent operations are in flight.
  2767. */
  2768. if (sh->check_state ||
  2769. (s.syncing && s.locked == 0 &&
  2770. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  2771. !test_bit(STRIPE_INSYNC, &sh->state)))
  2772. handle_parity_checks5(conf, sh, &s, disks);
  2773. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2774. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2775. clear_bit(STRIPE_SYNCING, &sh->state);
  2776. }
  2777. /* If the failed drive is just a ReadError, then we might need to progress
  2778. * the repair/check process
  2779. */
  2780. if (s.failed == 1 && !conf->mddev->ro &&
  2781. test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
  2782. && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
  2783. && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
  2784. ) {
  2785. dev = &sh->dev[s.failed_num];
  2786. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2787. set_bit(R5_Wantwrite, &dev->flags);
  2788. set_bit(R5_ReWrite, &dev->flags);
  2789. set_bit(R5_LOCKED, &dev->flags);
  2790. s.locked++;
  2791. } else {
  2792. /* let's read it back */
  2793. set_bit(R5_Wantread, &dev->flags);
  2794. set_bit(R5_LOCKED, &dev->flags);
  2795. s.locked++;
  2796. }
  2797. }
  2798. /* Finish reconstruct operations initiated by the expansion process */
  2799. if (sh->reconstruct_state == reconstruct_state_result) {
  2800. struct stripe_head *sh2
  2801. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  2802. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  2803. /* sh cannot be written until sh2 has been read.
  2804. * so arrange for sh to be delayed a little
  2805. */
  2806. set_bit(STRIPE_DELAYED, &sh->state);
  2807. set_bit(STRIPE_HANDLE, &sh->state);
  2808. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  2809. &sh2->state))
  2810. atomic_inc(&conf->preread_active_stripes);
  2811. release_stripe(sh2);
  2812. goto unlock;
  2813. }
  2814. if (sh2)
  2815. release_stripe(sh2);
  2816. sh->reconstruct_state = reconstruct_state_idle;
  2817. clear_bit(STRIPE_EXPANDING, &sh->state);
  2818. for (i = conf->raid_disks; i--; ) {
  2819. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2820. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2821. s.locked++;
  2822. }
  2823. }
  2824. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  2825. !sh->reconstruct_state) {
  2826. /* Need to write out all blocks after computing parity */
  2827. sh->disks = conf->raid_disks;
  2828. stripe_set_idx(sh->sector, conf, 0, sh);
  2829. schedule_reconstruction(sh, &s, 1, 1);
  2830. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  2831. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2832. atomic_dec(&conf->reshape_stripes);
  2833. wake_up(&conf->wait_for_overlap);
  2834. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2835. }
  2836. if (s.expanding && s.locked == 0 &&
  2837. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2838. handle_stripe_expansion(conf, sh, NULL);
  2839. unlock:
  2840. spin_unlock(&sh->lock);
  2841. /* wait for this device to become unblocked */
  2842. if (unlikely(blocked_rdev))
  2843. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2844. if (s.ops_request)
  2845. raid_run_ops(sh, s.ops_request);
  2846. ops_run_io(sh, &s);
  2847. if (dec_preread_active) {
  2848. /* We delay this until after ops_run_io so that if make_request
  2849. * is waiting on a barrier, it won't continue until the writes
  2850. * have actually been submitted.
  2851. */
  2852. atomic_dec(&conf->preread_active_stripes);
  2853. if (atomic_read(&conf->preread_active_stripes) <
  2854. IO_THRESHOLD)
  2855. md_wakeup_thread(conf->mddev->thread);
  2856. }
  2857. return_io(return_bi);
  2858. }
  2859. static void handle_stripe6(struct stripe_head *sh)
  2860. {
  2861. raid5_conf_t *conf = sh->raid_conf;
  2862. int disks = sh->disks;
  2863. struct bio *return_bi = NULL;
  2864. int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
  2865. struct stripe_head_state s;
  2866. struct r6_state r6s;
  2867. struct r5dev *dev, *pdev, *qdev;
  2868. mdk_rdev_t *blocked_rdev = NULL;
  2869. int dec_preread_active = 0;
  2870. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  2871. "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
  2872. (unsigned long long)sh->sector, sh->state,
  2873. atomic_read(&sh->count), pd_idx, qd_idx,
  2874. sh->check_state, sh->reconstruct_state);
  2875. memset(&s, 0, sizeof(s));
  2876. spin_lock(&sh->lock);
  2877. clear_bit(STRIPE_HANDLE, &sh->state);
  2878. clear_bit(STRIPE_DELAYED, &sh->state);
  2879. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2880. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2881. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2882. /* Now to look around and see what can be done */
  2883. rcu_read_lock();
  2884. for (i=disks; i--; ) {
  2885. mdk_rdev_t *rdev;
  2886. dev = &sh->dev[i];
  2887. clear_bit(R5_Insync, &dev->flags);
  2888. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2889. i, dev->flags, dev->toread, dev->towrite, dev->written);
  2890. /* maybe we can reply to a read
  2891. *
  2892. * new wantfill requests are only permitted while
  2893. * ops_complete_biofill is guaranteed to be inactive
  2894. */
  2895. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2896. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2897. set_bit(R5_Wantfill, &dev->flags);
  2898. /* now count some things */
  2899. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2900. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2901. if (test_bit(R5_Wantcompute, &dev->flags)) {
  2902. s.compute++;
  2903. BUG_ON(s.compute > 2);
  2904. }
  2905. if (test_bit(R5_Wantfill, &dev->flags)) {
  2906. s.to_fill++;
  2907. } else if (dev->toread)
  2908. s.to_read++;
  2909. if (dev->towrite) {
  2910. s.to_write++;
  2911. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2912. s.non_overwrite++;
  2913. }
  2914. if (dev->written)
  2915. s.written++;
  2916. rdev = rcu_dereference(conf->disks[i].rdev);
  2917. if (blocked_rdev == NULL &&
  2918. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2919. blocked_rdev = rdev;
  2920. atomic_inc(&rdev->nr_pending);
  2921. }
  2922. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2923. /* The ReadError flag will just be confusing now */
  2924. clear_bit(R5_ReadError, &dev->flags);
  2925. clear_bit(R5_ReWrite, &dev->flags);
  2926. }
  2927. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2928. || test_bit(R5_ReadError, &dev->flags)) {
  2929. if (s.failed < 2)
  2930. r6s.failed_num[s.failed] = i;
  2931. s.failed++;
  2932. } else
  2933. set_bit(R5_Insync, &dev->flags);
  2934. }
  2935. rcu_read_unlock();
  2936. if (unlikely(blocked_rdev)) {
  2937. if (s.syncing || s.expanding || s.expanded ||
  2938. s.to_write || s.written) {
  2939. set_bit(STRIPE_HANDLE, &sh->state);
  2940. goto unlock;
  2941. }
  2942. /* There is nothing for the blocked_rdev to block */
  2943. rdev_dec_pending(blocked_rdev, conf->mddev);
  2944. blocked_rdev = NULL;
  2945. }
  2946. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  2947. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  2948. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  2949. }
  2950. pr_debug("locked=%d uptodate=%d to_read=%d"
  2951. " to_write=%d failed=%d failed_num=%d,%d\n",
  2952. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  2953. r6s.failed_num[0], r6s.failed_num[1]);
  2954. /* check if the array has lost >2 devices and, if so, some requests
  2955. * might need to be failed
  2956. */
  2957. if (s.failed > 2 && s.to_read+s.to_write+s.written)
  2958. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2959. if (s.failed > 2 && s.syncing) {
  2960. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2961. clear_bit(STRIPE_SYNCING, &sh->state);
  2962. s.syncing = 0;
  2963. }
  2964. /*
  2965. * might be able to return some write requests if the parity blocks
  2966. * are safe, or on a failed drive
  2967. */
  2968. pdev = &sh->dev[pd_idx];
  2969. r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
  2970. || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
  2971. qdev = &sh->dev[qd_idx];
  2972. r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
  2973. || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
  2974. if ( s.written &&
  2975. ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  2976. && !test_bit(R5_LOCKED, &pdev->flags)
  2977. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  2978. ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  2979. && !test_bit(R5_LOCKED, &qdev->flags)
  2980. && test_bit(R5_UPTODATE, &qdev->flags)))))
  2981. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2982. /* Now we might consider reading some blocks, either to check/generate
  2983. * parity, or to satisfy requests
  2984. * or to load a block that is being partially written.
  2985. */
  2986. if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
  2987. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
  2988. handle_stripe_fill6(sh, &s, &r6s, disks);
  2989. /* Now we check to see if any write operations have recently
  2990. * completed
  2991. */
  2992. if (sh->reconstruct_state == reconstruct_state_drain_result) {
  2993. sh->reconstruct_state = reconstruct_state_idle;
  2994. /* All the 'written' buffers and the parity blocks are ready to
  2995. * be written back to disk
  2996. */
  2997. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  2998. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags));
  2999. for (i = disks; i--; ) {
  3000. dev = &sh->dev[i];
  3001. if (test_bit(R5_LOCKED, &dev->flags) &&
  3002. (i == sh->pd_idx || i == qd_idx ||
  3003. dev->written)) {
  3004. pr_debug("Writing block %d\n", i);
  3005. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  3006. set_bit(R5_Wantwrite, &dev->flags);
  3007. if (!test_bit(R5_Insync, &dev->flags) ||
  3008. ((i == sh->pd_idx || i == qd_idx) &&
  3009. s.failed == 0))
  3010. set_bit(STRIPE_INSYNC, &sh->state);
  3011. }
  3012. }
  3013. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3014. dec_preread_active = 1;
  3015. }
  3016. /* Now to consider new write requests and what else, if anything
  3017. * should be read. We do not handle new writes when:
  3018. * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
  3019. * 2/ A 'check' operation is in flight, as it may clobber the parity
  3020. * block.
  3021. */
  3022. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  3023. handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
  3024. /* maybe we need to check and possibly fix the parity for this stripe
  3025. * Any reads will already have been scheduled, so we just see if enough
  3026. * data is available. The parity check is held off while parity
  3027. * dependent operations are in flight.
  3028. */
  3029. if (sh->check_state ||
  3030. (s.syncing && s.locked == 0 &&
  3031. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  3032. !test_bit(STRIPE_INSYNC, &sh->state)))
  3033. handle_parity_checks6(conf, sh, &s, &r6s, disks);
  3034. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  3035. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  3036. clear_bit(STRIPE_SYNCING, &sh->state);
  3037. }
  3038. /* If the failed drives are just a ReadError, then we might need
  3039. * to progress the repair/check process
  3040. */
  3041. if (s.failed <= 2 && !conf->mddev->ro)
  3042. for (i = 0; i < s.failed; i++) {
  3043. dev = &sh->dev[r6s.failed_num[i]];
  3044. if (test_bit(R5_ReadError, &dev->flags)
  3045. && !test_bit(R5_LOCKED, &dev->flags)
  3046. && test_bit(R5_UPTODATE, &dev->flags)
  3047. ) {
  3048. if (!test_bit(R5_ReWrite, &dev->flags)) {
  3049. set_bit(R5_Wantwrite, &dev->flags);
  3050. set_bit(R5_ReWrite, &dev->flags);
  3051. set_bit(R5_LOCKED, &dev->flags);
  3052. s.locked++;
  3053. } else {
  3054. /* let's read it back */
  3055. set_bit(R5_Wantread, &dev->flags);
  3056. set_bit(R5_LOCKED, &dev->flags);
  3057. s.locked++;
  3058. }
  3059. }
  3060. }
  3061. /* Finish reconstruct operations initiated by the expansion process */
  3062. if (sh->reconstruct_state == reconstruct_state_result) {
  3063. sh->reconstruct_state = reconstruct_state_idle;
  3064. clear_bit(STRIPE_EXPANDING, &sh->state);
  3065. for (i = conf->raid_disks; i--; ) {
  3066. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  3067. set_bit(R5_LOCKED, &sh->dev[i].flags);
  3068. s.locked++;
  3069. }
  3070. }
  3071. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  3072. !sh->reconstruct_state) {
  3073. struct stripe_head *sh2
  3074. = get_active_stripe(conf, sh->sector, 1, 1, 1);
  3075. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  3076. /* sh cannot be written until sh2 has been read.
  3077. * so arrange for sh to be delayed a little
  3078. */
  3079. set_bit(STRIPE_DELAYED, &sh->state);
  3080. set_bit(STRIPE_HANDLE, &sh->state);
  3081. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  3082. &sh2->state))
  3083. atomic_inc(&conf->preread_active_stripes);
  3084. release_stripe(sh2);
  3085. goto unlock;
  3086. }
  3087. if (sh2)
  3088. release_stripe(sh2);
  3089. /* Need to write out all blocks after computing P&Q */
  3090. sh->disks = conf->raid_disks;
  3091. stripe_set_idx(sh->sector, conf, 0, sh);
  3092. schedule_reconstruction(sh, &s, 1, 1);
  3093. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  3094. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  3095. atomic_dec(&conf->reshape_stripes);
  3096. wake_up(&conf->wait_for_overlap);
  3097. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  3098. }
  3099. if (s.expanding && s.locked == 0 &&
  3100. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  3101. handle_stripe_expansion(conf, sh, &r6s);
  3102. unlock:
  3103. spin_unlock(&sh->lock);
  3104. /* wait for this device to become unblocked */
  3105. if (unlikely(blocked_rdev))
  3106. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  3107. if (s.ops_request)
  3108. raid_run_ops(sh, s.ops_request);
  3109. ops_run_io(sh, &s);
  3110. if (dec_preread_active) {
  3111. /* We delay this until after ops_run_io so that if make_request
  3112. * is waiting on a barrier, it won't continue until the writes
  3113. * have actually been submitted.
  3114. */
  3115. atomic_dec(&conf->preread_active_stripes);
  3116. if (atomic_read(&conf->preread_active_stripes) <
  3117. IO_THRESHOLD)
  3118. md_wakeup_thread(conf->mddev->thread);
  3119. }
  3120. return_io(return_bi);
  3121. }
  3122. static void handle_stripe(struct stripe_head *sh)
  3123. {
  3124. if (sh->raid_conf->level == 6)
  3125. handle_stripe6(sh);
  3126. else
  3127. handle_stripe5(sh);
  3128. }
  3129. static void raid5_activate_delayed(raid5_conf_t *conf)
  3130. {
  3131. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  3132. while (!list_empty(&conf->delayed_list)) {
  3133. struct list_head *l = conf->delayed_list.next;
  3134. struct stripe_head *sh;
  3135. sh = list_entry(l, struct stripe_head, lru);
  3136. list_del_init(l);
  3137. clear_bit(STRIPE_DELAYED, &sh->state);
  3138. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3139. atomic_inc(&conf->preread_active_stripes);
  3140. list_add_tail(&sh->lru, &conf->hold_list);
  3141. }
  3142. } else
  3143. blk_plug_device(conf->mddev->queue);
  3144. }
  3145. static void activate_bit_delay(raid5_conf_t *conf)
  3146. {
  3147. /* device_lock is held */
  3148. struct list_head head;
  3149. list_add(&head, &conf->bitmap_list);
  3150. list_del_init(&conf->bitmap_list);
  3151. while (!list_empty(&head)) {
  3152. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  3153. list_del_init(&sh->lru);
  3154. atomic_inc(&sh->count);
  3155. __release_stripe(conf, sh);
  3156. }
  3157. }
  3158. static void unplug_slaves(mddev_t *mddev)
  3159. {
  3160. raid5_conf_t *conf = mddev->private;
  3161. int i;
  3162. int devs = max(conf->raid_disks, conf->previous_raid_disks);
  3163. rcu_read_lock();
  3164. for (i = 0; i < devs; i++) {
  3165. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  3166. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  3167. struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
  3168. atomic_inc(&rdev->nr_pending);
  3169. rcu_read_unlock();
  3170. blk_unplug(r_queue);
  3171. rdev_dec_pending(rdev, mddev);
  3172. rcu_read_lock();
  3173. }
  3174. }
  3175. rcu_read_unlock();
  3176. }
  3177. static void raid5_unplug_device(struct request_queue *q)
  3178. {
  3179. mddev_t *mddev = q->queuedata;
  3180. raid5_conf_t *conf = mddev->private;
  3181. unsigned long flags;
  3182. spin_lock_irqsave(&conf->device_lock, flags);
  3183. if (blk_remove_plug(q)) {
  3184. conf->seq_flush++;
  3185. raid5_activate_delayed(conf);
  3186. }
  3187. md_wakeup_thread(mddev->thread);
  3188. spin_unlock_irqrestore(&conf->device_lock, flags);
  3189. unplug_slaves(mddev);
  3190. }
  3191. static int raid5_congested(void *data, int bits)
  3192. {
  3193. mddev_t *mddev = data;
  3194. raid5_conf_t *conf = mddev->private;
  3195. /* No difference between reads and writes. Just check
  3196. * how busy the stripe_cache is
  3197. */
  3198. if (mddev_congested(mddev, bits))
  3199. return 1;
  3200. if (conf->inactive_blocked)
  3201. return 1;
  3202. if (conf->quiesce)
  3203. return 1;
  3204. if (list_empty_careful(&conf->inactive_list))
  3205. return 1;
  3206. return 0;
  3207. }
  3208. /* We want read requests to align with chunks where possible,
  3209. * but write requests don't need to.
  3210. */
  3211. static int raid5_mergeable_bvec(struct request_queue *q,
  3212. struct bvec_merge_data *bvm,
  3213. struct bio_vec *biovec)
  3214. {
  3215. mddev_t *mddev = q->queuedata;
  3216. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  3217. int max;
  3218. unsigned int chunk_sectors = mddev->chunk_sectors;
  3219. unsigned int bio_sectors = bvm->bi_size >> 9;
  3220. if ((bvm->bi_rw & 1) == WRITE)
  3221. return biovec->bv_len; /* always allow writes to be mergeable */
  3222. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3223. chunk_sectors = mddev->new_chunk_sectors;
  3224. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  3225. if (max < 0) max = 0;
  3226. if (max <= biovec->bv_len && bio_sectors == 0)
  3227. return biovec->bv_len;
  3228. else
  3229. return max;
  3230. }
  3231. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  3232. {
  3233. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  3234. unsigned int chunk_sectors = mddev->chunk_sectors;
  3235. unsigned int bio_sectors = bio->bi_size >> 9;
  3236. if (mddev->new_chunk_sectors < mddev->chunk_sectors)
  3237. chunk_sectors = mddev->new_chunk_sectors;
  3238. return chunk_sectors >=
  3239. ((sector & (chunk_sectors - 1)) + bio_sectors);
  3240. }
  3241. /*
  3242. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  3243. * later sampled by raid5d.
  3244. */
  3245. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  3246. {
  3247. unsigned long flags;
  3248. spin_lock_irqsave(&conf->device_lock, flags);
  3249. bi->bi_next = conf->retry_read_aligned_list;
  3250. conf->retry_read_aligned_list = bi;
  3251. spin_unlock_irqrestore(&conf->device_lock, flags);
  3252. md_wakeup_thread(conf->mddev->thread);
  3253. }
  3254. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  3255. {
  3256. struct bio *bi;
  3257. bi = conf->retry_read_aligned;
  3258. if (bi) {
  3259. conf->retry_read_aligned = NULL;
  3260. return bi;
  3261. }
  3262. bi = conf->retry_read_aligned_list;
  3263. if(bi) {
  3264. conf->retry_read_aligned_list = bi->bi_next;
  3265. bi->bi_next = NULL;
  3266. /*
  3267. * this sets the active strip count to 1 and the processed
  3268. * strip count to zero (upper 8 bits)
  3269. */
  3270. bi->bi_phys_segments = 1; /* biased count of active stripes */
  3271. }
  3272. return bi;
  3273. }
  3274. /*
  3275. * The "raid5_align_endio" should check if the read succeeded and if it
  3276. * did, call bio_endio on the original bio (having bio_put the new bio
  3277. * first).
  3278. * If the read failed..
  3279. */
  3280. static void raid5_align_endio(struct bio *bi, int error)
  3281. {
  3282. struct bio* raid_bi = bi->bi_private;
  3283. mddev_t *mddev;
  3284. raid5_conf_t *conf;
  3285. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  3286. mdk_rdev_t *rdev;
  3287. bio_put(bi);
  3288. rdev = (void*)raid_bi->bi_next;
  3289. raid_bi->bi_next = NULL;
  3290. mddev = rdev->mddev;
  3291. conf = mddev->private;
  3292. rdev_dec_pending(rdev, conf->mddev);
  3293. if (!error && uptodate) {
  3294. bio_endio(raid_bi, 0);
  3295. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3296. wake_up(&conf->wait_for_stripe);
  3297. return;
  3298. }
  3299. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  3300. add_bio_to_retry(raid_bi, conf);
  3301. }
  3302. static int bio_fits_rdev(struct bio *bi)
  3303. {
  3304. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  3305. if ((bi->bi_size>>9) > queue_max_sectors(q))
  3306. return 0;
  3307. blk_recount_segments(q, bi);
  3308. if (bi->bi_phys_segments > queue_max_segments(q))
  3309. return 0;
  3310. if (q->merge_bvec_fn)
  3311. /* it's too hard to apply the merge_bvec_fn at this stage,
  3312. * just just give up
  3313. */
  3314. return 0;
  3315. return 1;
  3316. }
  3317. static int chunk_aligned_read(mddev_t *mddev, struct bio * raid_bio)
  3318. {
  3319. raid5_conf_t *conf = mddev->private;
  3320. int dd_idx;
  3321. struct bio* align_bi;
  3322. mdk_rdev_t *rdev;
  3323. if (!in_chunk_boundary(mddev, raid_bio)) {
  3324. pr_debug("chunk_aligned_read : non aligned\n");
  3325. return 0;
  3326. }
  3327. /*
  3328. * use bio_clone to make a copy of the bio
  3329. */
  3330. align_bi = bio_clone(raid_bio, GFP_NOIO);
  3331. if (!align_bi)
  3332. return 0;
  3333. /*
  3334. * set bi_end_io to a new function, and set bi_private to the
  3335. * original bio.
  3336. */
  3337. align_bi->bi_end_io = raid5_align_endio;
  3338. align_bi->bi_private = raid_bio;
  3339. /*
  3340. * compute position
  3341. */
  3342. align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
  3343. 0,
  3344. &dd_idx, NULL);
  3345. rcu_read_lock();
  3346. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3347. if (rdev && test_bit(In_sync, &rdev->flags)) {
  3348. atomic_inc(&rdev->nr_pending);
  3349. rcu_read_unlock();
  3350. raid_bio->bi_next = (void*)rdev;
  3351. align_bi->bi_bdev = rdev->bdev;
  3352. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3353. align_bi->bi_sector += rdev->data_offset;
  3354. if (!bio_fits_rdev(align_bi)) {
  3355. /* too big in some way */
  3356. bio_put(align_bi);
  3357. rdev_dec_pending(rdev, mddev);
  3358. return 0;
  3359. }
  3360. spin_lock_irq(&conf->device_lock);
  3361. wait_event_lock_irq(conf->wait_for_stripe,
  3362. conf->quiesce == 0,
  3363. conf->device_lock, /* nothing */);
  3364. atomic_inc(&conf->active_aligned_reads);
  3365. spin_unlock_irq(&conf->device_lock);
  3366. generic_make_request(align_bi);
  3367. return 1;
  3368. } else {
  3369. rcu_read_unlock();
  3370. bio_put(align_bi);
  3371. return 0;
  3372. }
  3373. }
  3374. /* __get_priority_stripe - get the next stripe to process
  3375. *
  3376. * Full stripe writes are allowed to pass preread active stripes up until
  3377. * the bypass_threshold is exceeded. In general the bypass_count
  3378. * increments when the handle_list is handled before the hold_list; however, it
  3379. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  3380. * stripe with in flight i/o. The bypass_count will be reset when the
  3381. * head of the hold_list has changed, i.e. the head was promoted to the
  3382. * handle_list.
  3383. */
  3384. static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
  3385. {
  3386. struct stripe_head *sh;
  3387. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  3388. __func__,
  3389. list_empty(&conf->handle_list) ? "empty" : "busy",
  3390. list_empty(&conf->hold_list) ? "empty" : "busy",
  3391. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  3392. if (!list_empty(&conf->handle_list)) {
  3393. sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
  3394. if (list_empty(&conf->hold_list))
  3395. conf->bypass_count = 0;
  3396. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  3397. if (conf->hold_list.next == conf->last_hold)
  3398. conf->bypass_count++;
  3399. else {
  3400. conf->last_hold = conf->hold_list.next;
  3401. conf->bypass_count -= conf->bypass_threshold;
  3402. if (conf->bypass_count < 0)
  3403. conf->bypass_count = 0;
  3404. }
  3405. }
  3406. } else if (!list_empty(&conf->hold_list) &&
  3407. ((conf->bypass_threshold &&
  3408. conf->bypass_count > conf->bypass_threshold) ||
  3409. atomic_read(&conf->pending_full_writes) == 0)) {
  3410. sh = list_entry(conf->hold_list.next,
  3411. typeof(*sh), lru);
  3412. conf->bypass_count -= conf->bypass_threshold;
  3413. if (conf->bypass_count < 0)
  3414. conf->bypass_count = 0;
  3415. } else
  3416. return NULL;
  3417. list_del_init(&sh->lru);
  3418. atomic_inc(&sh->count);
  3419. BUG_ON(atomic_read(&sh->count) != 1);
  3420. return sh;
  3421. }
  3422. static int make_request(mddev_t *mddev, struct bio * bi)
  3423. {
  3424. raid5_conf_t *conf = mddev->private;
  3425. int dd_idx;
  3426. sector_t new_sector;
  3427. sector_t logical_sector, last_sector;
  3428. struct stripe_head *sh;
  3429. const int rw = bio_data_dir(bi);
  3430. int remaining;
  3431. if (unlikely(bio_rw_flagged(bi, BIO_RW_BARRIER))) {
  3432. /* Drain all pending writes. We only really need
  3433. * to ensure they have been submitted, but this is
  3434. * easier.
  3435. */
  3436. mddev->pers->quiesce(mddev, 1);
  3437. mddev->pers->quiesce(mddev, 0);
  3438. md_barrier_request(mddev, bi);
  3439. return 0;
  3440. }
  3441. md_write_start(mddev, bi);
  3442. if (rw == READ &&
  3443. mddev->reshape_position == MaxSector &&
  3444. chunk_aligned_read(mddev,bi))
  3445. return 0;
  3446. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3447. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3448. bi->bi_next = NULL;
  3449. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3450. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3451. DEFINE_WAIT(w);
  3452. int disks, data_disks;
  3453. int previous;
  3454. retry:
  3455. previous = 0;
  3456. disks = conf->raid_disks;
  3457. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3458. if (unlikely(conf->reshape_progress != MaxSector)) {
  3459. /* spinlock is needed as reshape_progress may be
  3460. * 64bit on a 32bit platform, and so it might be
  3461. * possible to see a half-updated value
  3462. * Ofcourse reshape_progress could change after
  3463. * the lock is dropped, so once we get a reference
  3464. * to the stripe that we think it is, we will have
  3465. * to check again.
  3466. */
  3467. spin_lock_irq(&conf->device_lock);
  3468. if (mddev->delta_disks < 0
  3469. ? logical_sector < conf->reshape_progress
  3470. : logical_sector >= conf->reshape_progress) {
  3471. disks = conf->previous_raid_disks;
  3472. previous = 1;
  3473. } else {
  3474. if (mddev->delta_disks < 0
  3475. ? logical_sector < conf->reshape_safe
  3476. : logical_sector >= conf->reshape_safe) {
  3477. spin_unlock_irq(&conf->device_lock);
  3478. schedule();
  3479. goto retry;
  3480. }
  3481. }
  3482. spin_unlock_irq(&conf->device_lock);
  3483. }
  3484. data_disks = disks - conf->max_degraded;
  3485. new_sector = raid5_compute_sector(conf, logical_sector,
  3486. previous,
  3487. &dd_idx, NULL);
  3488. pr_debug("raid5: make_request, sector %llu logical %llu\n",
  3489. (unsigned long long)new_sector,
  3490. (unsigned long long)logical_sector);
  3491. sh = get_active_stripe(conf, new_sector, previous,
  3492. (bi->bi_rw&RWA_MASK), 0);
  3493. if (sh) {
  3494. if (unlikely(previous)) {
  3495. /* expansion might have moved on while waiting for a
  3496. * stripe, so we must do the range check again.
  3497. * Expansion could still move past after this
  3498. * test, but as we are holding a reference to
  3499. * 'sh', we know that if that happens,
  3500. * STRIPE_EXPANDING will get set and the expansion
  3501. * won't proceed until we finish with the stripe.
  3502. */
  3503. int must_retry = 0;
  3504. spin_lock_irq(&conf->device_lock);
  3505. if (mddev->delta_disks < 0
  3506. ? logical_sector >= conf->reshape_progress
  3507. : logical_sector < conf->reshape_progress)
  3508. /* mismatch, need to try again */
  3509. must_retry = 1;
  3510. spin_unlock_irq(&conf->device_lock);
  3511. if (must_retry) {
  3512. release_stripe(sh);
  3513. schedule();
  3514. goto retry;
  3515. }
  3516. }
  3517. if (bio_data_dir(bi) == WRITE &&
  3518. logical_sector >= mddev->suspend_lo &&
  3519. logical_sector < mddev->suspend_hi) {
  3520. release_stripe(sh);
  3521. /* As the suspend_* range is controlled by
  3522. * userspace, we want an interruptible
  3523. * wait.
  3524. */
  3525. flush_signals(current);
  3526. prepare_to_wait(&conf->wait_for_overlap,
  3527. &w, TASK_INTERRUPTIBLE);
  3528. if (logical_sector >= mddev->suspend_lo &&
  3529. logical_sector < mddev->suspend_hi)
  3530. schedule();
  3531. goto retry;
  3532. }
  3533. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3534. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  3535. /* Stripe is busy expanding or
  3536. * add failed due to overlap. Flush everything
  3537. * and wait a while
  3538. */
  3539. raid5_unplug_device(mddev->queue);
  3540. release_stripe(sh);
  3541. schedule();
  3542. goto retry;
  3543. }
  3544. finish_wait(&conf->wait_for_overlap, &w);
  3545. set_bit(STRIPE_HANDLE, &sh->state);
  3546. clear_bit(STRIPE_DELAYED, &sh->state);
  3547. if (mddev->barrier &&
  3548. !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  3549. atomic_inc(&conf->preread_active_stripes);
  3550. release_stripe(sh);
  3551. } else {
  3552. /* cannot get stripe for read-ahead, just give-up */
  3553. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3554. finish_wait(&conf->wait_for_overlap, &w);
  3555. break;
  3556. }
  3557. }
  3558. spin_lock_irq(&conf->device_lock);
  3559. remaining = raid5_dec_bi_phys_segments(bi);
  3560. spin_unlock_irq(&conf->device_lock);
  3561. if (remaining == 0) {
  3562. if ( rw == WRITE )
  3563. md_write_end(mddev);
  3564. bio_endio(bi, 0);
  3565. }
  3566. if (mddev->barrier) {
  3567. /* We need to wait for the stripes to all be handled.
  3568. * So: wait for preread_active_stripes to drop to 0.
  3569. */
  3570. wait_event(mddev->thread->wqueue,
  3571. atomic_read(&conf->preread_active_stripes) == 0);
  3572. }
  3573. return 0;
  3574. }
  3575. static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
  3576. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  3577. {
  3578. /* reshaping is quite different to recovery/resync so it is
  3579. * handled quite separately ... here.
  3580. *
  3581. * On each call to sync_request, we gather one chunk worth of
  3582. * destination stripes and flag them as expanding.
  3583. * Then we find all the source stripes and request reads.
  3584. * As the reads complete, handle_stripe will copy the data
  3585. * into the destination stripe and release that stripe.
  3586. */
  3587. raid5_conf_t *conf = mddev->private;
  3588. struct stripe_head *sh;
  3589. sector_t first_sector, last_sector;
  3590. int raid_disks = conf->previous_raid_disks;
  3591. int data_disks = raid_disks - conf->max_degraded;
  3592. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3593. int i;
  3594. int dd_idx;
  3595. sector_t writepos, readpos, safepos;
  3596. sector_t stripe_addr;
  3597. int reshape_sectors;
  3598. struct list_head stripes;
  3599. if (sector_nr == 0) {
  3600. /* If restarting in the middle, skip the initial sectors */
  3601. if (mddev->delta_disks < 0 &&
  3602. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  3603. sector_nr = raid5_size(mddev, 0, 0)
  3604. - conf->reshape_progress;
  3605. } else if (mddev->delta_disks >= 0 &&
  3606. conf->reshape_progress > 0)
  3607. sector_nr = conf->reshape_progress;
  3608. sector_div(sector_nr, new_data_disks);
  3609. if (sector_nr) {
  3610. mddev->curr_resync_completed = sector_nr;
  3611. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3612. *skipped = 1;
  3613. return sector_nr;
  3614. }
  3615. }
  3616. /* We need to process a full chunk at a time.
  3617. * If old and new chunk sizes differ, we need to process the
  3618. * largest of these
  3619. */
  3620. if (mddev->new_chunk_sectors > mddev->chunk_sectors)
  3621. reshape_sectors = mddev->new_chunk_sectors;
  3622. else
  3623. reshape_sectors = mddev->chunk_sectors;
  3624. /* we update the metadata when there is more than 3Meg
  3625. * in the block range (that is rather arbitrary, should
  3626. * probably be time based) or when the data about to be
  3627. * copied would over-write the source of the data at
  3628. * the front of the range.
  3629. * i.e. one new_stripe along from reshape_progress new_maps
  3630. * to after where reshape_safe old_maps to
  3631. */
  3632. writepos = conf->reshape_progress;
  3633. sector_div(writepos, new_data_disks);
  3634. readpos = conf->reshape_progress;
  3635. sector_div(readpos, data_disks);
  3636. safepos = conf->reshape_safe;
  3637. sector_div(safepos, data_disks);
  3638. if (mddev->delta_disks < 0) {
  3639. writepos -= min_t(sector_t, reshape_sectors, writepos);
  3640. readpos += reshape_sectors;
  3641. safepos += reshape_sectors;
  3642. } else {
  3643. writepos += reshape_sectors;
  3644. readpos -= min_t(sector_t, reshape_sectors, readpos);
  3645. safepos -= min_t(sector_t, reshape_sectors, safepos);
  3646. }
  3647. /* 'writepos' is the most advanced device address we might write.
  3648. * 'readpos' is the least advanced device address we might read.
  3649. * 'safepos' is the least address recorded in the metadata as having
  3650. * been reshaped.
  3651. * If 'readpos' is behind 'writepos', then there is no way that we can
  3652. * ensure safety in the face of a crash - that must be done by userspace
  3653. * making a backup of the data. So in that case there is no particular
  3654. * rush to update metadata.
  3655. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  3656. * update the metadata to advance 'safepos' to match 'readpos' so that
  3657. * we can be safe in the event of a crash.
  3658. * So we insist on updating metadata if safepos is behind writepos and
  3659. * readpos is beyond writepos.
  3660. * In any case, update the metadata every 10 seconds.
  3661. * Maybe that number should be configurable, but I'm not sure it is
  3662. * worth it.... maybe it could be a multiple of safemode_delay???
  3663. */
  3664. if ((mddev->delta_disks < 0
  3665. ? (safepos > writepos && readpos < writepos)
  3666. : (safepos < writepos && readpos > writepos)) ||
  3667. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3668. /* Cannot proceed until we've updated the superblock... */
  3669. wait_event(conf->wait_for_overlap,
  3670. atomic_read(&conf->reshape_stripes)==0);
  3671. mddev->reshape_position = conf->reshape_progress;
  3672. mddev->curr_resync_completed = mddev->curr_resync;
  3673. conf->reshape_checkpoint = jiffies;
  3674. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3675. md_wakeup_thread(mddev->thread);
  3676. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3677. kthread_should_stop());
  3678. spin_lock_irq(&conf->device_lock);
  3679. conf->reshape_safe = mddev->reshape_position;
  3680. spin_unlock_irq(&conf->device_lock);
  3681. wake_up(&conf->wait_for_overlap);
  3682. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3683. }
  3684. if (mddev->delta_disks < 0) {
  3685. BUG_ON(conf->reshape_progress == 0);
  3686. stripe_addr = writepos;
  3687. BUG_ON((mddev->dev_sectors &
  3688. ~((sector_t)reshape_sectors - 1))
  3689. - reshape_sectors - stripe_addr
  3690. != sector_nr);
  3691. } else {
  3692. BUG_ON(writepos != sector_nr + reshape_sectors);
  3693. stripe_addr = sector_nr;
  3694. }
  3695. INIT_LIST_HEAD(&stripes);
  3696. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  3697. int j;
  3698. int skipped_disk = 0;
  3699. sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
  3700. set_bit(STRIPE_EXPANDING, &sh->state);
  3701. atomic_inc(&conf->reshape_stripes);
  3702. /* If any of this stripe is beyond the end of the old
  3703. * array, then we need to zero those blocks
  3704. */
  3705. for (j=sh->disks; j--;) {
  3706. sector_t s;
  3707. if (j == sh->pd_idx)
  3708. continue;
  3709. if (conf->level == 6 &&
  3710. j == sh->qd_idx)
  3711. continue;
  3712. s = compute_blocknr(sh, j, 0);
  3713. if (s < raid5_size(mddev, 0, 0)) {
  3714. skipped_disk = 1;
  3715. continue;
  3716. }
  3717. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  3718. set_bit(R5_Expanded, &sh->dev[j].flags);
  3719. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  3720. }
  3721. if (!skipped_disk) {
  3722. set_bit(STRIPE_EXPAND_READY, &sh->state);
  3723. set_bit(STRIPE_HANDLE, &sh->state);
  3724. }
  3725. list_add(&sh->lru, &stripes);
  3726. }
  3727. spin_lock_irq(&conf->device_lock);
  3728. if (mddev->delta_disks < 0)
  3729. conf->reshape_progress -= reshape_sectors * new_data_disks;
  3730. else
  3731. conf->reshape_progress += reshape_sectors * new_data_disks;
  3732. spin_unlock_irq(&conf->device_lock);
  3733. /* Ok, those stripe are ready. We can start scheduling
  3734. * reads on the source stripes.
  3735. * The source stripes are determined by mapping the first and last
  3736. * block on the destination stripes.
  3737. */
  3738. first_sector =
  3739. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  3740. 1, &dd_idx, NULL);
  3741. last_sector =
  3742. raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
  3743. * new_data_disks - 1),
  3744. 1, &dd_idx, NULL);
  3745. if (last_sector >= mddev->dev_sectors)
  3746. last_sector = mddev->dev_sectors - 1;
  3747. while (first_sector <= last_sector) {
  3748. sh = get_active_stripe(conf, first_sector, 1, 0, 1);
  3749. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3750. set_bit(STRIPE_HANDLE, &sh->state);
  3751. release_stripe(sh);
  3752. first_sector += STRIPE_SECTORS;
  3753. }
  3754. /* Now that the sources are clearly marked, we can release
  3755. * the destination stripes
  3756. */
  3757. while (!list_empty(&stripes)) {
  3758. sh = list_entry(stripes.next, struct stripe_head, lru);
  3759. list_del_init(&sh->lru);
  3760. release_stripe(sh);
  3761. }
  3762. /* If this takes us to the resync_max point where we have to pause,
  3763. * then we need to write out the superblock.
  3764. */
  3765. sector_nr += reshape_sectors;
  3766. if ((sector_nr - mddev->curr_resync_completed) * 2
  3767. >= mddev->resync_max - mddev->curr_resync_completed) {
  3768. /* Cannot proceed until we've updated the superblock... */
  3769. wait_event(conf->wait_for_overlap,
  3770. atomic_read(&conf->reshape_stripes) == 0);
  3771. mddev->reshape_position = conf->reshape_progress;
  3772. mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors;
  3773. conf->reshape_checkpoint = jiffies;
  3774. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3775. md_wakeup_thread(mddev->thread);
  3776. wait_event(mddev->sb_wait,
  3777. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  3778. || kthread_should_stop());
  3779. spin_lock_irq(&conf->device_lock);
  3780. conf->reshape_safe = mddev->reshape_position;
  3781. spin_unlock_irq(&conf->device_lock);
  3782. wake_up(&conf->wait_for_overlap);
  3783. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3784. }
  3785. return reshape_sectors;
  3786. }
  3787. /* FIXME go_faster isn't used */
  3788. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  3789. {
  3790. raid5_conf_t *conf = mddev->private;
  3791. struct stripe_head *sh;
  3792. sector_t max_sector = mddev->dev_sectors;
  3793. int sync_blocks;
  3794. int still_degraded = 0;
  3795. int i;
  3796. if (sector_nr >= max_sector) {
  3797. /* just being told to finish up .. nothing much to do */
  3798. unplug_slaves(mddev);
  3799. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  3800. end_reshape(conf);
  3801. return 0;
  3802. }
  3803. if (mddev->curr_resync < max_sector) /* aborted */
  3804. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  3805. &sync_blocks, 1);
  3806. else /* completed sync */
  3807. conf->fullsync = 0;
  3808. bitmap_close_sync(mddev->bitmap);
  3809. return 0;
  3810. }
  3811. /* Allow raid5_quiesce to complete */
  3812. wait_event(conf->wait_for_overlap, conf->quiesce != 2);
  3813. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3814. return reshape_request(mddev, sector_nr, skipped);
  3815. /* No need to check resync_max as we never do more than one
  3816. * stripe, and as resync_max will always be on a chunk boundary,
  3817. * if the check in md_do_sync didn't fire, there is no chance
  3818. * of overstepping resync_max here
  3819. */
  3820. /* if there is too many failed drives and we are trying
  3821. * to resync, then assert that we are finished, because there is
  3822. * nothing we can do.
  3823. */
  3824. if (mddev->degraded >= conf->max_degraded &&
  3825. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3826. sector_t rv = mddev->dev_sectors - sector_nr;
  3827. *skipped = 1;
  3828. return rv;
  3829. }
  3830. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  3831. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  3832. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  3833. /* we can skip this block, and probably more */
  3834. sync_blocks /= STRIPE_SECTORS;
  3835. *skipped = 1;
  3836. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  3837. }
  3838. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  3839. sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
  3840. if (sh == NULL) {
  3841. sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
  3842. /* make sure we don't swamp the stripe cache if someone else
  3843. * is trying to get access
  3844. */
  3845. schedule_timeout_uninterruptible(1);
  3846. }
  3847. /* Need to check if array will still be degraded after recovery/resync
  3848. * We don't need to check the 'failed' flag as when that gets set,
  3849. * recovery aborts.
  3850. */
  3851. for (i = 0; i < conf->raid_disks; i++)
  3852. if (conf->disks[i].rdev == NULL)
  3853. still_degraded = 1;
  3854. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  3855. spin_lock(&sh->lock);
  3856. set_bit(STRIPE_SYNCING, &sh->state);
  3857. clear_bit(STRIPE_INSYNC, &sh->state);
  3858. spin_unlock(&sh->lock);
  3859. handle_stripe(sh);
  3860. release_stripe(sh);
  3861. return STRIPE_SECTORS;
  3862. }
  3863. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  3864. {
  3865. /* We may not be able to submit a whole bio at once as there
  3866. * may not be enough stripe_heads available.
  3867. * We cannot pre-allocate enough stripe_heads as we may need
  3868. * more than exist in the cache (if we allow ever large chunks).
  3869. * So we do one stripe head at a time and record in
  3870. * ->bi_hw_segments how many have been done.
  3871. *
  3872. * We *know* that this entire raid_bio is in one chunk, so
  3873. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  3874. */
  3875. struct stripe_head *sh;
  3876. int dd_idx;
  3877. sector_t sector, logical_sector, last_sector;
  3878. int scnt = 0;
  3879. int remaining;
  3880. int handled = 0;
  3881. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3882. sector = raid5_compute_sector(conf, logical_sector,
  3883. 0, &dd_idx, NULL);
  3884. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  3885. for (; logical_sector < last_sector;
  3886. logical_sector += STRIPE_SECTORS,
  3887. sector += STRIPE_SECTORS,
  3888. scnt++) {
  3889. if (scnt < raid5_bi_hw_segments(raid_bio))
  3890. /* already done this stripe */
  3891. continue;
  3892. sh = get_active_stripe(conf, sector, 0, 1, 0);
  3893. if (!sh) {
  3894. /* failed to get a stripe - must wait */
  3895. raid5_set_bi_hw_segments(raid_bio, scnt);
  3896. conf->retry_read_aligned = raid_bio;
  3897. return handled;
  3898. }
  3899. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  3900. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  3901. release_stripe(sh);
  3902. raid5_set_bi_hw_segments(raid_bio, scnt);
  3903. conf->retry_read_aligned = raid_bio;
  3904. return handled;
  3905. }
  3906. handle_stripe(sh);
  3907. release_stripe(sh);
  3908. handled++;
  3909. }
  3910. spin_lock_irq(&conf->device_lock);
  3911. remaining = raid5_dec_bi_phys_segments(raid_bio);
  3912. spin_unlock_irq(&conf->device_lock);
  3913. if (remaining == 0)
  3914. bio_endio(raid_bio, 0);
  3915. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3916. wake_up(&conf->wait_for_stripe);
  3917. return handled;
  3918. }
  3919. /*
  3920. * This is our raid5 kernel thread.
  3921. *
  3922. * We scan the hash table for stripes which can be handled now.
  3923. * During the scan, completed stripes are saved for us by the interrupt
  3924. * handler, so that they will not have to wait for our next wakeup.
  3925. */
  3926. static void raid5d(mddev_t *mddev)
  3927. {
  3928. struct stripe_head *sh;
  3929. raid5_conf_t *conf = mddev->private;
  3930. int handled;
  3931. pr_debug("+++ raid5d active\n");
  3932. md_check_recovery(mddev);
  3933. handled = 0;
  3934. spin_lock_irq(&conf->device_lock);
  3935. while (1) {
  3936. struct bio *bio;
  3937. if (conf->seq_flush != conf->seq_write) {
  3938. int seq = conf->seq_flush;
  3939. spin_unlock_irq(&conf->device_lock);
  3940. bitmap_unplug(mddev->bitmap);
  3941. spin_lock_irq(&conf->device_lock);
  3942. conf->seq_write = seq;
  3943. activate_bit_delay(conf);
  3944. }
  3945. while ((bio = remove_bio_from_retry(conf))) {
  3946. int ok;
  3947. spin_unlock_irq(&conf->device_lock);
  3948. ok = retry_aligned_read(conf, bio);
  3949. spin_lock_irq(&conf->device_lock);
  3950. if (!ok)
  3951. break;
  3952. handled++;
  3953. }
  3954. sh = __get_priority_stripe(conf);
  3955. if (!sh)
  3956. break;
  3957. spin_unlock_irq(&conf->device_lock);
  3958. handled++;
  3959. handle_stripe(sh);
  3960. release_stripe(sh);
  3961. cond_resched();
  3962. spin_lock_irq(&conf->device_lock);
  3963. }
  3964. pr_debug("%d stripes handled\n", handled);
  3965. spin_unlock_irq(&conf->device_lock);
  3966. async_tx_issue_pending_all();
  3967. unplug_slaves(mddev);
  3968. pr_debug("--- raid5d inactive\n");
  3969. }
  3970. static ssize_t
  3971. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  3972. {
  3973. raid5_conf_t *conf = mddev->private;
  3974. if (conf)
  3975. return sprintf(page, "%d\n", conf->max_nr_stripes);
  3976. else
  3977. return 0;
  3978. }
  3979. static ssize_t
  3980. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  3981. {
  3982. raid5_conf_t *conf = mddev->private;
  3983. unsigned long new;
  3984. int err;
  3985. if (len >= PAGE_SIZE)
  3986. return -EINVAL;
  3987. if (!conf)
  3988. return -ENODEV;
  3989. if (strict_strtoul(page, 10, &new))
  3990. return -EINVAL;
  3991. if (new <= 16 || new > 32768)
  3992. return -EINVAL;
  3993. while (new < conf->max_nr_stripes) {
  3994. if (drop_one_stripe(conf))
  3995. conf->max_nr_stripes--;
  3996. else
  3997. break;
  3998. }
  3999. err = md_allow_write(mddev);
  4000. if (err)
  4001. return err;
  4002. while (new > conf->max_nr_stripes) {
  4003. if (grow_one_stripe(conf))
  4004. conf->max_nr_stripes++;
  4005. else break;
  4006. }
  4007. return len;
  4008. }
  4009. static struct md_sysfs_entry
  4010. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  4011. raid5_show_stripe_cache_size,
  4012. raid5_store_stripe_cache_size);
  4013. static ssize_t
  4014. raid5_show_preread_threshold(mddev_t *mddev, char *page)
  4015. {
  4016. raid5_conf_t *conf = mddev->private;
  4017. if (conf)
  4018. return sprintf(page, "%d\n", conf->bypass_threshold);
  4019. else
  4020. return 0;
  4021. }
  4022. static ssize_t
  4023. raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
  4024. {
  4025. raid5_conf_t *conf = mddev->private;
  4026. unsigned long new;
  4027. if (len >= PAGE_SIZE)
  4028. return -EINVAL;
  4029. if (!conf)
  4030. return -ENODEV;
  4031. if (strict_strtoul(page, 10, &new))
  4032. return -EINVAL;
  4033. if (new > conf->max_nr_stripes)
  4034. return -EINVAL;
  4035. conf->bypass_threshold = new;
  4036. return len;
  4037. }
  4038. static struct md_sysfs_entry
  4039. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  4040. S_IRUGO | S_IWUSR,
  4041. raid5_show_preread_threshold,
  4042. raid5_store_preread_threshold);
  4043. static ssize_t
  4044. stripe_cache_active_show(mddev_t *mddev, char *page)
  4045. {
  4046. raid5_conf_t *conf = mddev->private;
  4047. if (conf)
  4048. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  4049. else
  4050. return 0;
  4051. }
  4052. static struct md_sysfs_entry
  4053. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  4054. static struct attribute *raid5_attrs[] = {
  4055. &raid5_stripecache_size.attr,
  4056. &raid5_stripecache_active.attr,
  4057. &raid5_preread_bypass_threshold.attr,
  4058. NULL,
  4059. };
  4060. static struct attribute_group raid5_attrs_group = {
  4061. .name = NULL,
  4062. .attrs = raid5_attrs,
  4063. };
  4064. static sector_t
  4065. raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  4066. {
  4067. raid5_conf_t *conf = mddev->private;
  4068. if (!sectors)
  4069. sectors = mddev->dev_sectors;
  4070. if (!raid_disks)
  4071. /* size is defined by the smallest of previous and new size */
  4072. raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
  4073. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4074. sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
  4075. return sectors * (raid_disks - conf->max_degraded);
  4076. }
  4077. static void raid5_free_percpu(raid5_conf_t *conf)
  4078. {
  4079. struct raid5_percpu *percpu;
  4080. unsigned long cpu;
  4081. if (!conf->percpu)
  4082. return;
  4083. get_online_cpus();
  4084. for_each_possible_cpu(cpu) {
  4085. percpu = per_cpu_ptr(conf->percpu, cpu);
  4086. safe_put_page(percpu->spare_page);
  4087. kfree(percpu->scribble);
  4088. }
  4089. #ifdef CONFIG_HOTPLUG_CPU
  4090. unregister_cpu_notifier(&conf->cpu_notify);
  4091. #endif
  4092. put_online_cpus();
  4093. free_percpu(conf->percpu);
  4094. }
  4095. static void free_conf(raid5_conf_t *conf)
  4096. {
  4097. shrink_stripes(conf);
  4098. raid5_free_percpu(conf);
  4099. kfree(conf->disks);
  4100. kfree(conf->stripe_hashtbl);
  4101. kfree(conf);
  4102. }
  4103. #ifdef CONFIG_HOTPLUG_CPU
  4104. static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
  4105. void *hcpu)
  4106. {
  4107. raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
  4108. long cpu = (long)hcpu;
  4109. struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
  4110. switch (action) {
  4111. case CPU_UP_PREPARE:
  4112. case CPU_UP_PREPARE_FROZEN:
  4113. if (conf->level == 6 && !percpu->spare_page)
  4114. percpu->spare_page = alloc_page(GFP_KERNEL);
  4115. if (!percpu->scribble)
  4116. percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4117. if (!percpu->scribble ||
  4118. (conf->level == 6 && !percpu->spare_page)) {
  4119. safe_put_page(percpu->spare_page);
  4120. kfree(percpu->scribble);
  4121. pr_err("%s: failed memory allocation for cpu%ld\n",
  4122. __func__, cpu);
  4123. return NOTIFY_BAD;
  4124. }
  4125. break;
  4126. case CPU_DEAD:
  4127. case CPU_DEAD_FROZEN:
  4128. safe_put_page(percpu->spare_page);
  4129. kfree(percpu->scribble);
  4130. percpu->spare_page = NULL;
  4131. percpu->scribble = NULL;
  4132. break;
  4133. default:
  4134. break;
  4135. }
  4136. return NOTIFY_OK;
  4137. }
  4138. #endif
  4139. static int raid5_alloc_percpu(raid5_conf_t *conf)
  4140. {
  4141. unsigned long cpu;
  4142. struct page *spare_page;
  4143. struct raid5_percpu __percpu *allcpus;
  4144. void *scribble;
  4145. int err;
  4146. allcpus = alloc_percpu(struct raid5_percpu);
  4147. if (!allcpus)
  4148. return -ENOMEM;
  4149. conf->percpu = allcpus;
  4150. get_online_cpus();
  4151. err = 0;
  4152. for_each_present_cpu(cpu) {
  4153. if (conf->level == 6) {
  4154. spare_page = alloc_page(GFP_KERNEL);
  4155. if (!spare_page) {
  4156. err = -ENOMEM;
  4157. break;
  4158. }
  4159. per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
  4160. }
  4161. scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
  4162. if (!scribble) {
  4163. err = -ENOMEM;
  4164. break;
  4165. }
  4166. per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
  4167. }
  4168. #ifdef CONFIG_HOTPLUG_CPU
  4169. conf->cpu_notify.notifier_call = raid456_cpu_notify;
  4170. conf->cpu_notify.priority = 0;
  4171. if (err == 0)
  4172. err = register_cpu_notifier(&conf->cpu_notify);
  4173. #endif
  4174. put_online_cpus();
  4175. return err;
  4176. }
  4177. static raid5_conf_t *setup_conf(mddev_t *mddev)
  4178. {
  4179. raid5_conf_t *conf;
  4180. int raid_disk, memory, max_disks;
  4181. mdk_rdev_t *rdev;
  4182. struct disk_info *disk;
  4183. if (mddev->new_level != 5
  4184. && mddev->new_level != 4
  4185. && mddev->new_level != 6) {
  4186. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  4187. mdname(mddev), mddev->new_level);
  4188. return ERR_PTR(-EIO);
  4189. }
  4190. if ((mddev->new_level == 5
  4191. && !algorithm_valid_raid5(mddev->new_layout)) ||
  4192. (mddev->new_level == 6
  4193. && !algorithm_valid_raid6(mddev->new_layout))) {
  4194. printk(KERN_ERR "raid5: %s: layout %d not supported\n",
  4195. mdname(mddev), mddev->new_layout);
  4196. return ERR_PTR(-EIO);
  4197. }
  4198. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  4199. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  4200. mdname(mddev), mddev->raid_disks);
  4201. return ERR_PTR(-EINVAL);
  4202. }
  4203. if (!mddev->new_chunk_sectors ||
  4204. (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
  4205. !is_power_of_2(mddev->new_chunk_sectors)) {
  4206. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  4207. mddev->new_chunk_sectors << 9, mdname(mddev));
  4208. return ERR_PTR(-EINVAL);
  4209. }
  4210. conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
  4211. if (conf == NULL)
  4212. goto abort;
  4213. spin_lock_init(&conf->device_lock);
  4214. init_waitqueue_head(&conf->wait_for_stripe);
  4215. init_waitqueue_head(&conf->wait_for_overlap);
  4216. INIT_LIST_HEAD(&conf->handle_list);
  4217. INIT_LIST_HEAD(&conf->hold_list);
  4218. INIT_LIST_HEAD(&conf->delayed_list);
  4219. INIT_LIST_HEAD(&conf->bitmap_list);
  4220. INIT_LIST_HEAD(&conf->inactive_list);
  4221. atomic_set(&conf->active_stripes, 0);
  4222. atomic_set(&conf->preread_active_stripes, 0);
  4223. atomic_set(&conf->active_aligned_reads, 0);
  4224. conf->bypass_threshold = BYPASS_THRESHOLD;
  4225. conf->raid_disks = mddev->raid_disks;
  4226. if (mddev->reshape_position == MaxSector)
  4227. conf->previous_raid_disks = mddev->raid_disks;
  4228. else
  4229. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  4230. max_disks = max(conf->raid_disks, conf->previous_raid_disks);
  4231. conf->scribble_len = scribble_len(max_disks);
  4232. conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
  4233. GFP_KERNEL);
  4234. if (!conf->disks)
  4235. goto abort;
  4236. conf->mddev = mddev;
  4237. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  4238. goto abort;
  4239. conf->level = mddev->new_level;
  4240. if (raid5_alloc_percpu(conf) != 0)
  4241. goto abort;
  4242. pr_debug("raid5: run(%s) called.\n", mdname(mddev));
  4243. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4244. raid_disk = rdev->raid_disk;
  4245. if (raid_disk >= max_disks
  4246. || raid_disk < 0)
  4247. continue;
  4248. disk = conf->disks + raid_disk;
  4249. disk->rdev = rdev;
  4250. if (test_bit(In_sync, &rdev->flags)) {
  4251. char b[BDEVNAME_SIZE];
  4252. printk(KERN_INFO "raid5: device %s operational as raid"
  4253. " disk %d\n", bdevname(rdev->bdev,b),
  4254. raid_disk);
  4255. } else
  4256. /* Cannot rely on bitmap to complete recovery */
  4257. conf->fullsync = 1;
  4258. }
  4259. conf->chunk_sectors = mddev->new_chunk_sectors;
  4260. conf->level = mddev->new_level;
  4261. if (conf->level == 6)
  4262. conf->max_degraded = 2;
  4263. else
  4264. conf->max_degraded = 1;
  4265. conf->algorithm = mddev->new_layout;
  4266. conf->max_nr_stripes = NR_STRIPES;
  4267. conf->reshape_progress = mddev->reshape_position;
  4268. if (conf->reshape_progress != MaxSector) {
  4269. conf->prev_chunk_sectors = mddev->chunk_sectors;
  4270. conf->prev_algo = mddev->layout;
  4271. }
  4272. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  4273. max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  4274. if (grow_stripes(conf, conf->max_nr_stripes)) {
  4275. printk(KERN_ERR
  4276. "raid5: couldn't allocate %dkB for buffers\n", memory);
  4277. goto abort;
  4278. } else
  4279. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  4280. memory, mdname(mddev));
  4281. conf->thread = md_register_thread(raid5d, mddev, NULL);
  4282. if (!conf->thread) {
  4283. printk(KERN_ERR
  4284. "raid5: couldn't allocate thread for %s\n",
  4285. mdname(mddev));
  4286. goto abort;
  4287. }
  4288. return conf;
  4289. abort:
  4290. if (conf) {
  4291. free_conf(conf);
  4292. return ERR_PTR(-EIO);
  4293. } else
  4294. return ERR_PTR(-ENOMEM);
  4295. }
  4296. static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
  4297. {
  4298. switch (algo) {
  4299. case ALGORITHM_PARITY_0:
  4300. if (raid_disk < max_degraded)
  4301. return 1;
  4302. break;
  4303. case ALGORITHM_PARITY_N:
  4304. if (raid_disk >= raid_disks - max_degraded)
  4305. return 1;
  4306. break;
  4307. case ALGORITHM_PARITY_0_6:
  4308. if (raid_disk == 0 ||
  4309. raid_disk == raid_disks - 1)
  4310. return 1;
  4311. break;
  4312. case ALGORITHM_LEFT_ASYMMETRIC_6:
  4313. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  4314. case ALGORITHM_LEFT_SYMMETRIC_6:
  4315. case ALGORITHM_RIGHT_SYMMETRIC_6:
  4316. if (raid_disk == raid_disks - 1)
  4317. return 1;
  4318. }
  4319. return 0;
  4320. }
  4321. static int run(mddev_t *mddev)
  4322. {
  4323. raid5_conf_t *conf;
  4324. int working_disks = 0, chunk_size;
  4325. int dirty_parity_disks = 0;
  4326. mdk_rdev_t *rdev;
  4327. sector_t reshape_offset = 0;
  4328. if (mddev->recovery_cp != MaxSector)
  4329. printk(KERN_NOTICE "raid5: %s is not clean"
  4330. " -- starting background reconstruction\n",
  4331. mdname(mddev));
  4332. if (mddev->reshape_position != MaxSector) {
  4333. /* Check that we can continue the reshape.
  4334. * Currently only disks can change, it must
  4335. * increase, and we must be past the point where
  4336. * a stripe over-writes itself
  4337. */
  4338. sector_t here_new, here_old;
  4339. int old_disks;
  4340. int max_degraded = (mddev->level == 6 ? 2 : 1);
  4341. if (mddev->new_level != mddev->level) {
  4342. printk(KERN_ERR "raid5: %s: unsupported reshape "
  4343. "required - aborting.\n",
  4344. mdname(mddev));
  4345. return -EINVAL;
  4346. }
  4347. old_disks = mddev->raid_disks - mddev->delta_disks;
  4348. /* reshape_position must be on a new-stripe boundary, and one
  4349. * further up in new geometry must map after here in old
  4350. * geometry.
  4351. */
  4352. here_new = mddev->reshape_position;
  4353. if (sector_div(here_new, mddev->new_chunk_sectors *
  4354. (mddev->raid_disks - max_degraded))) {
  4355. printk(KERN_ERR "raid5: reshape_position not "
  4356. "on a stripe boundary\n");
  4357. return -EINVAL;
  4358. }
  4359. reshape_offset = here_new * mddev->new_chunk_sectors;
  4360. /* here_new is the stripe we will write to */
  4361. here_old = mddev->reshape_position;
  4362. sector_div(here_old, mddev->chunk_sectors *
  4363. (old_disks-max_degraded));
  4364. /* here_old is the first stripe that we might need to read
  4365. * from */
  4366. if (mddev->delta_disks == 0) {
  4367. /* We cannot be sure it is safe to start an in-place
  4368. * reshape. It is only safe if user-space if monitoring
  4369. * and taking constant backups.
  4370. * mdadm always starts a situation like this in
  4371. * readonly mode so it can take control before
  4372. * allowing any writes. So just check for that.
  4373. */
  4374. if ((here_new * mddev->new_chunk_sectors !=
  4375. here_old * mddev->chunk_sectors) ||
  4376. mddev->ro == 0) {
  4377. printk(KERN_ERR "raid5: in-place reshape must be started"
  4378. " in read-only mode - aborting\n");
  4379. return -EINVAL;
  4380. }
  4381. } else if (mddev->delta_disks < 0
  4382. ? (here_new * mddev->new_chunk_sectors <=
  4383. here_old * mddev->chunk_sectors)
  4384. : (here_new * mddev->new_chunk_sectors >=
  4385. here_old * mddev->chunk_sectors)) {
  4386. /* Reading from the same stripe as writing to - bad */
  4387. printk(KERN_ERR "raid5: reshape_position too early for "
  4388. "auto-recovery - aborting.\n");
  4389. return -EINVAL;
  4390. }
  4391. printk(KERN_INFO "raid5: reshape will continue\n");
  4392. /* OK, we should be able to continue; */
  4393. } else {
  4394. BUG_ON(mddev->level != mddev->new_level);
  4395. BUG_ON(mddev->layout != mddev->new_layout);
  4396. BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
  4397. BUG_ON(mddev->delta_disks != 0);
  4398. }
  4399. if (mddev->private == NULL)
  4400. conf = setup_conf(mddev);
  4401. else
  4402. conf = mddev->private;
  4403. if (IS_ERR(conf))
  4404. return PTR_ERR(conf);
  4405. mddev->thread = conf->thread;
  4406. conf->thread = NULL;
  4407. mddev->private = conf;
  4408. /*
  4409. * 0 for a fully functional array, 1 or 2 for a degraded array.
  4410. */
  4411. list_for_each_entry(rdev, &mddev->disks, same_set) {
  4412. if (rdev->raid_disk < 0)
  4413. continue;
  4414. if (test_bit(In_sync, &rdev->flags))
  4415. working_disks++;
  4416. /* This disc is not fully in-sync. However if it
  4417. * just stored parity (beyond the recovery_offset),
  4418. * when we don't need to be concerned about the
  4419. * array being dirty.
  4420. * When reshape goes 'backwards', we never have
  4421. * partially completed devices, so we only need
  4422. * to worry about reshape going forwards.
  4423. */
  4424. /* Hack because v0.91 doesn't store recovery_offset properly. */
  4425. if (mddev->major_version == 0 &&
  4426. mddev->minor_version > 90)
  4427. rdev->recovery_offset = reshape_offset;
  4428. printk("%d: w=%d pa=%d pr=%d m=%d a=%d r=%d op1=%d op2=%d\n",
  4429. rdev->raid_disk, working_disks, conf->prev_algo,
  4430. conf->previous_raid_disks, conf->max_degraded,
  4431. conf->algorithm, conf->raid_disks,
  4432. only_parity(rdev->raid_disk,
  4433. conf->prev_algo,
  4434. conf->previous_raid_disks,
  4435. conf->max_degraded),
  4436. only_parity(rdev->raid_disk,
  4437. conf->algorithm,
  4438. conf->raid_disks,
  4439. conf->max_degraded));
  4440. if (rdev->recovery_offset < reshape_offset) {
  4441. /* We need to check old and new layout */
  4442. if (!only_parity(rdev->raid_disk,
  4443. conf->algorithm,
  4444. conf->raid_disks,
  4445. conf->max_degraded))
  4446. continue;
  4447. }
  4448. if (!only_parity(rdev->raid_disk,
  4449. conf->prev_algo,
  4450. conf->previous_raid_disks,
  4451. conf->max_degraded))
  4452. continue;
  4453. dirty_parity_disks++;
  4454. }
  4455. mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks)
  4456. - working_disks);
  4457. if (mddev->degraded > conf->max_degraded) {
  4458. printk(KERN_ERR "raid5: not enough operational devices for %s"
  4459. " (%d/%d failed)\n",
  4460. mdname(mddev), mddev->degraded, conf->raid_disks);
  4461. goto abort;
  4462. }
  4463. /* device size must be a multiple of chunk size */
  4464. mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
  4465. mddev->resync_max_sectors = mddev->dev_sectors;
  4466. if (mddev->degraded > dirty_parity_disks &&
  4467. mddev->recovery_cp != MaxSector) {
  4468. if (mddev->ok_start_degraded)
  4469. printk(KERN_WARNING
  4470. "raid5: starting dirty degraded array: %s"
  4471. "- data corruption possible.\n",
  4472. mdname(mddev));
  4473. else {
  4474. printk(KERN_ERR
  4475. "raid5: cannot start dirty degraded array for %s\n",
  4476. mdname(mddev));
  4477. goto abort;
  4478. }
  4479. }
  4480. if (mddev->degraded == 0)
  4481. printk("raid5: raid level %d set %s active with %d out of %d"
  4482. " devices, algorithm %d\n", conf->level, mdname(mddev),
  4483. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  4484. mddev->new_layout);
  4485. else
  4486. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  4487. " out of %d devices, algorithm %d\n", conf->level,
  4488. mdname(mddev), mddev->raid_disks - mddev->degraded,
  4489. mddev->raid_disks, mddev->new_layout);
  4490. print_raid5_conf(conf);
  4491. if (conf->reshape_progress != MaxSector) {
  4492. printk("...ok start reshape thread\n");
  4493. conf->reshape_safe = conf->reshape_progress;
  4494. atomic_set(&conf->reshape_stripes, 0);
  4495. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4496. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4497. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4498. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4499. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4500. "reshape");
  4501. }
  4502. /* read-ahead size must cover two whole stripes, which is
  4503. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4504. */
  4505. {
  4506. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4507. int stripe = data_disks *
  4508. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  4509. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4510. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4511. }
  4512. /* Ok, everything is just fine now */
  4513. if (mddev->to_remove == &raid5_attrs_group)
  4514. mddev->to_remove = NULL;
  4515. else if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  4516. printk(KERN_WARNING
  4517. "raid5: failed to create sysfs attributes for %s\n",
  4518. mdname(mddev));
  4519. mddev->queue->queue_lock = &conf->device_lock;
  4520. mddev->queue->unplug_fn = raid5_unplug_device;
  4521. mddev->queue->backing_dev_info.congested_data = mddev;
  4522. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  4523. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4524. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  4525. chunk_size = mddev->chunk_sectors << 9;
  4526. blk_queue_io_min(mddev->queue, chunk_size);
  4527. blk_queue_io_opt(mddev->queue, chunk_size *
  4528. (conf->raid_disks - conf->max_degraded));
  4529. list_for_each_entry(rdev, &mddev->disks, same_set)
  4530. disk_stack_limits(mddev->gendisk, rdev->bdev,
  4531. rdev->data_offset << 9);
  4532. return 0;
  4533. abort:
  4534. md_unregister_thread(mddev->thread);
  4535. mddev->thread = NULL;
  4536. if (conf) {
  4537. print_raid5_conf(conf);
  4538. free_conf(conf);
  4539. }
  4540. mddev->private = NULL;
  4541. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  4542. return -EIO;
  4543. }
  4544. static int stop(mddev_t *mddev)
  4545. {
  4546. raid5_conf_t *conf = mddev->private;
  4547. md_unregister_thread(mddev->thread);
  4548. mddev->thread = NULL;
  4549. mddev->queue->backing_dev_info.congested_fn = NULL;
  4550. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  4551. free_conf(conf);
  4552. mddev->private = NULL;
  4553. mddev->to_remove = &raid5_attrs_group;
  4554. return 0;
  4555. }
  4556. #ifdef DEBUG
  4557. static void print_sh(struct seq_file *seq, struct stripe_head *sh)
  4558. {
  4559. int i;
  4560. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  4561. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  4562. seq_printf(seq, "sh %llu, count %d.\n",
  4563. (unsigned long long)sh->sector, atomic_read(&sh->count));
  4564. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  4565. for (i = 0; i < sh->disks; i++) {
  4566. seq_printf(seq, "(cache%d: %p %ld) ",
  4567. i, sh->dev[i].page, sh->dev[i].flags);
  4568. }
  4569. seq_printf(seq, "\n");
  4570. }
  4571. static void printall(struct seq_file *seq, raid5_conf_t *conf)
  4572. {
  4573. struct stripe_head *sh;
  4574. struct hlist_node *hn;
  4575. int i;
  4576. spin_lock_irq(&conf->device_lock);
  4577. for (i = 0; i < NR_HASH; i++) {
  4578. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  4579. if (sh->raid_conf != conf)
  4580. continue;
  4581. print_sh(seq, sh);
  4582. }
  4583. }
  4584. spin_unlock_irq(&conf->device_lock);
  4585. }
  4586. #endif
  4587. static void status(struct seq_file *seq, mddev_t *mddev)
  4588. {
  4589. raid5_conf_t *conf = mddev->private;
  4590. int i;
  4591. seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
  4592. mddev->chunk_sectors / 2, mddev->layout);
  4593. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  4594. for (i = 0; i < conf->raid_disks; i++)
  4595. seq_printf (seq, "%s",
  4596. conf->disks[i].rdev &&
  4597. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  4598. seq_printf (seq, "]");
  4599. #ifdef DEBUG
  4600. seq_printf (seq, "\n");
  4601. printall(seq, conf);
  4602. #endif
  4603. }
  4604. static void print_raid5_conf (raid5_conf_t *conf)
  4605. {
  4606. int i;
  4607. struct disk_info *tmp;
  4608. printk("RAID5 conf printout:\n");
  4609. if (!conf) {
  4610. printk("(conf==NULL)\n");
  4611. return;
  4612. }
  4613. printk(" --- rd:%d wd:%d\n", conf->raid_disks,
  4614. conf->raid_disks - conf->mddev->degraded);
  4615. for (i = 0; i < conf->raid_disks; i++) {
  4616. char b[BDEVNAME_SIZE];
  4617. tmp = conf->disks + i;
  4618. if (tmp->rdev)
  4619. printk(" disk %d, o:%d, dev:%s\n",
  4620. i, !test_bit(Faulty, &tmp->rdev->flags),
  4621. bdevname(tmp->rdev->bdev,b));
  4622. }
  4623. }
  4624. static int raid5_spare_active(mddev_t *mddev)
  4625. {
  4626. int i;
  4627. raid5_conf_t *conf = mddev->private;
  4628. struct disk_info *tmp;
  4629. for (i = 0; i < conf->raid_disks; i++) {
  4630. tmp = conf->disks + i;
  4631. if (tmp->rdev
  4632. && !test_bit(Faulty, &tmp->rdev->flags)
  4633. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  4634. unsigned long flags;
  4635. spin_lock_irqsave(&conf->device_lock, flags);
  4636. mddev->degraded--;
  4637. spin_unlock_irqrestore(&conf->device_lock, flags);
  4638. }
  4639. }
  4640. print_raid5_conf(conf);
  4641. return 0;
  4642. }
  4643. static int raid5_remove_disk(mddev_t *mddev, int number)
  4644. {
  4645. raid5_conf_t *conf = mddev->private;
  4646. int err = 0;
  4647. mdk_rdev_t *rdev;
  4648. struct disk_info *p = conf->disks + number;
  4649. print_raid5_conf(conf);
  4650. rdev = p->rdev;
  4651. if (rdev) {
  4652. if (number >= conf->raid_disks &&
  4653. conf->reshape_progress == MaxSector)
  4654. clear_bit(In_sync, &rdev->flags);
  4655. if (test_bit(In_sync, &rdev->flags) ||
  4656. atomic_read(&rdev->nr_pending)) {
  4657. err = -EBUSY;
  4658. goto abort;
  4659. }
  4660. /* Only remove non-faulty devices if recovery
  4661. * isn't possible.
  4662. */
  4663. if (!test_bit(Faulty, &rdev->flags) &&
  4664. mddev->degraded <= conf->max_degraded &&
  4665. number < conf->raid_disks) {
  4666. err = -EBUSY;
  4667. goto abort;
  4668. }
  4669. p->rdev = NULL;
  4670. synchronize_rcu();
  4671. if (atomic_read(&rdev->nr_pending)) {
  4672. /* lost the race, try later */
  4673. err = -EBUSY;
  4674. p->rdev = rdev;
  4675. }
  4676. }
  4677. abort:
  4678. print_raid5_conf(conf);
  4679. return err;
  4680. }
  4681. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  4682. {
  4683. raid5_conf_t *conf = mddev->private;
  4684. int err = -EEXIST;
  4685. int disk;
  4686. struct disk_info *p;
  4687. int first = 0;
  4688. int last = conf->raid_disks - 1;
  4689. if (mddev->degraded > conf->max_degraded)
  4690. /* no point adding a device */
  4691. return -EINVAL;
  4692. if (rdev->raid_disk >= 0)
  4693. first = last = rdev->raid_disk;
  4694. /*
  4695. * find the disk ... but prefer rdev->saved_raid_disk
  4696. * if possible.
  4697. */
  4698. if (rdev->saved_raid_disk >= 0 &&
  4699. rdev->saved_raid_disk >= first &&
  4700. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  4701. disk = rdev->saved_raid_disk;
  4702. else
  4703. disk = first;
  4704. for ( ; disk <= last ; disk++)
  4705. if ((p=conf->disks + disk)->rdev == NULL) {
  4706. clear_bit(In_sync, &rdev->flags);
  4707. rdev->raid_disk = disk;
  4708. err = 0;
  4709. if (rdev->saved_raid_disk != disk)
  4710. conf->fullsync = 1;
  4711. rcu_assign_pointer(p->rdev, rdev);
  4712. break;
  4713. }
  4714. print_raid5_conf(conf);
  4715. return err;
  4716. }
  4717. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  4718. {
  4719. /* no resync is happening, and there is enough space
  4720. * on all devices, so we can resize.
  4721. * We need to make sure resync covers any new space.
  4722. * If the array is shrinking we should possibly wait until
  4723. * any io in the removed space completes, but it hardly seems
  4724. * worth it.
  4725. */
  4726. sectors &= ~((sector_t)mddev->chunk_sectors - 1);
  4727. md_set_array_sectors(mddev, raid5_size(mddev, sectors,
  4728. mddev->raid_disks));
  4729. if (mddev->array_sectors >
  4730. raid5_size(mddev, sectors, mddev->raid_disks))
  4731. return -EINVAL;
  4732. set_capacity(mddev->gendisk, mddev->array_sectors);
  4733. revalidate_disk(mddev->gendisk);
  4734. if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
  4735. mddev->recovery_cp = mddev->dev_sectors;
  4736. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4737. }
  4738. mddev->dev_sectors = sectors;
  4739. mddev->resync_max_sectors = sectors;
  4740. return 0;
  4741. }
  4742. static int check_stripe_cache(mddev_t *mddev)
  4743. {
  4744. /* Can only proceed if there are plenty of stripe_heads.
  4745. * We need a minimum of one full stripe,, and for sensible progress
  4746. * it is best to have about 4 times that.
  4747. * If we require 4 times, then the default 256 4K stripe_heads will
  4748. * allow for chunk sizes up to 256K, which is probably OK.
  4749. * If the chunk size is greater, user-space should request more
  4750. * stripe_heads first.
  4751. */
  4752. raid5_conf_t *conf = mddev->private;
  4753. if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
  4754. > conf->max_nr_stripes ||
  4755. ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
  4756. > conf->max_nr_stripes) {
  4757. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  4758. ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
  4759. / STRIPE_SIZE)*4);
  4760. return 0;
  4761. }
  4762. return 1;
  4763. }
  4764. static int check_reshape(mddev_t *mddev)
  4765. {
  4766. raid5_conf_t *conf = mddev->private;
  4767. if (mddev->delta_disks == 0 &&
  4768. mddev->new_layout == mddev->layout &&
  4769. mddev->new_chunk_sectors == mddev->chunk_sectors)
  4770. return 0; /* nothing to do */
  4771. if (mddev->bitmap)
  4772. /* Cannot grow a bitmap yet */
  4773. return -EBUSY;
  4774. if (mddev->degraded > conf->max_degraded)
  4775. return -EINVAL;
  4776. if (mddev->delta_disks < 0) {
  4777. /* We might be able to shrink, but the devices must
  4778. * be made bigger first.
  4779. * For raid6, 4 is the minimum size.
  4780. * Otherwise 2 is the minimum
  4781. */
  4782. int min = 2;
  4783. if (mddev->level == 6)
  4784. min = 4;
  4785. if (mddev->raid_disks + mddev->delta_disks < min)
  4786. return -EINVAL;
  4787. }
  4788. if (!check_stripe_cache(mddev))
  4789. return -ENOSPC;
  4790. return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  4791. }
  4792. static int raid5_start_reshape(mddev_t *mddev)
  4793. {
  4794. raid5_conf_t *conf = mddev->private;
  4795. mdk_rdev_t *rdev;
  4796. int spares = 0;
  4797. int added_devices = 0;
  4798. unsigned long flags;
  4799. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  4800. return -EBUSY;
  4801. if (!check_stripe_cache(mddev))
  4802. return -ENOSPC;
  4803. list_for_each_entry(rdev, &mddev->disks, same_set)
  4804. if (rdev->raid_disk < 0 &&
  4805. !test_bit(Faulty, &rdev->flags))
  4806. spares++;
  4807. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  4808. /* Not enough devices even to make a degraded array
  4809. * of that size
  4810. */
  4811. return -EINVAL;
  4812. /* Refuse to reduce size of the array. Any reductions in
  4813. * array size must be through explicit setting of array_size
  4814. * attribute.
  4815. */
  4816. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  4817. < mddev->array_sectors) {
  4818. printk(KERN_ERR "md: %s: array size must be reduced "
  4819. "before number of disks\n", mdname(mddev));
  4820. return -EINVAL;
  4821. }
  4822. atomic_set(&conf->reshape_stripes, 0);
  4823. spin_lock_irq(&conf->device_lock);
  4824. conf->previous_raid_disks = conf->raid_disks;
  4825. conf->raid_disks += mddev->delta_disks;
  4826. conf->prev_chunk_sectors = conf->chunk_sectors;
  4827. conf->chunk_sectors = mddev->new_chunk_sectors;
  4828. conf->prev_algo = conf->algorithm;
  4829. conf->algorithm = mddev->new_layout;
  4830. if (mddev->delta_disks < 0)
  4831. conf->reshape_progress = raid5_size(mddev, 0, 0);
  4832. else
  4833. conf->reshape_progress = 0;
  4834. conf->reshape_safe = conf->reshape_progress;
  4835. conf->generation++;
  4836. spin_unlock_irq(&conf->device_lock);
  4837. /* Add some new drives, as many as will fit.
  4838. * We know there are enough to make the newly sized array work.
  4839. */
  4840. list_for_each_entry(rdev, &mddev->disks, same_set)
  4841. if (rdev->raid_disk < 0 &&
  4842. !test_bit(Faulty, &rdev->flags)) {
  4843. if (raid5_add_disk(mddev, rdev) == 0) {
  4844. char nm[20];
  4845. if (rdev->raid_disk >= conf->previous_raid_disks) {
  4846. set_bit(In_sync, &rdev->flags);
  4847. added_devices++;
  4848. } else
  4849. rdev->recovery_offset = 0;
  4850. sprintf(nm, "rd%d", rdev->raid_disk);
  4851. if (sysfs_create_link(&mddev->kobj,
  4852. &rdev->kobj, nm))
  4853. printk(KERN_WARNING
  4854. "raid5: failed to create "
  4855. " link %s for %s\n",
  4856. nm, mdname(mddev));
  4857. } else
  4858. break;
  4859. }
  4860. /* When a reshape changes the number of devices, ->degraded
  4861. * is measured against the large of the pre and post number of
  4862. * devices.*/
  4863. if (mddev->delta_disks > 0) {
  4864. spin_lock_irqsave(&conf->device_lock, flags);
  4865. mddev->degraded += (conf->raid_disks - conf->previous_raid_disks)
  4866. - added_devices;
  4867. spin_unlock_irqrestore(&conf->device_lock, flags);
  4868. }
  4869. mddev->raid_disks = conf->raid_disks;
  4870. mddev->reshape_position = conf->reshape_progress;
  4871. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4872. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4873. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4874. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4875. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4876. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4877. "reshape");
  4878. if (!mddev->sync_thread) {
  4879. mddev->recovery = 0;
  4880. spin_lock_irq(&conf->device_lock);
  4881. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  4882. conf->reshape_progress = MaxSector;
  4883. spin_unlock_irq(&conf->device_lock);
  4884. return -EAGAIN;
  4885. }
  4886. conf->reshape_checkpoint = jiffies;
  4887. md_wakeup_thread(mddev->sync_thread);
  4888. md_new_event(mddev);
  4889. return 0;
  4890. }
  4891. /* This is called from the reshape thread and should make any
  4892. * changes needed in 'conf'
  4893. */
  4894. static void end_reshape(raid5_conf_t *conf)
  4895. {
  4896. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  4897. spin_lock_irq(&conf->device_lock);
  4898. conf->previous_raid_disks = conf->raid_disks;
  4899. conf->reshape_progress = MaxSector;
  4900. spin_unlock_irq(&conf->device_lock);
  4901. wake_up(&conf->wait_for_overlap);
  4902. /* read-ahead size must cover two whole stripes, which is
  4903. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4904. */
  4905. {
  4906. int data_disks = conf->raid_disks - conf->max_degraded;
  4907. int stripe = data_disks * ((conf->chunk_sectors << 9)
  4908. / PAGE_SIZE);
  4909. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4910. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4911. }
  4912. }
  4913. }
  4914. /* This is called from the raid5d thread with mddev_lock held.
  4915. * It makes config changes to the device.
  4916. */
  4917. static void raid5_finish_reshape(mddev_t *mddev)
  4918. {
  4919. raid5_conf_t *conf = mddev->private;
  4920. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  4921. if (mddev->delta_disks > 0) {
  4922. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4923. set_capacity(mddev->gendisk, mddev->array_sectors);
  4924. revalidate_disk(mddev->gendisk);
  4925. } else {
  4926. int d;
  4927. mddev->degraded = conf->raid_disks;
  4928. for (d = 0; d < conf->raid_disks ; d++)
  4929. if (conf->disks[d].rdev &&
  4930. test_bit(In_sync,
  4931. &conf->disks[d].rdev->flags))
  4932. mddev->degraded--;
  4933. for (d = conf->raid_disks ;
  4934. d < conf->raid_disks - mddev->delta_disks;
  4935. d++) {
  4936. mdk_rdev_t *rdev = conf->disks[d].rdev;
  4937. if (rdev && raid5_remove_disk(mddev, d) == 0) {
  4938. char nm[20];
  4939. sprintf(nm, "rd%d", rdev->raid_disk);
  4940. sysfs_remove_link(&mddev->kobj, nm);
  4941. rdev->raid_disk = -1;
  4942. }
  4943. }
  4944. }
  4945. mddev->layout = conf->algorithm;
  4946. mddev->chunk_sectors = conf->chunk_sectors;
  4947. mddev->reshape_position = MaxSector;
  4948. mddev->delta_disks = 0;
  4949. }
  4950. }
  4951. static void raid5_quiesce(mddev_t *mddev, int state)
  4952. {
  4953. raid5_conf_t *conf = mddev->private;
  4954. switch(state) {
  4955. case 2: /* resume for a suspend */
  4956. wake_up(&conf->wait_for_overlap);
  4957. break;
  4958. case 1: /* stop all writes */
  4959. spin_lock_irq(&conf->device_lock);
  4960. /* '2' tells resync/reshape to pause so that all
  4961. * active stripes can drain
  4962. */
  4963. conf->quiesce = 2;
  4964. wait_event_lock_irq(conf->wait_for_stripe,
  4965. atomic_read(&conf->active_stripes) == 0 &&
  4966. atomic_read(&conf->active_aligned_reads) == 0,
  4967. conf->device_lock, /* nothing */);
  4968. conf->quiesce = 1;
  4969. spin_unlock_irq(&conf->device_lock);
  4970. /* allow reshape to continue */
  4971. wake_up(&conf->wait_for_overlap);
  4972. break;
  4973. case 0: /* re-enable writes */
  4974. spin_lock_irq(&conf->device_lock);
  4975. conf->quiesce = 0;
  4976. wake_up(&conf->wait_for_stripe);
  4977. wake_up(&conf->wait_for_overlap);
  4978. spin_unlock_irq(&conf->device_lock);
  4979. break;
  4980. }
  4981. }
  4982. static void *raid5_takeover_raid0(mddev_t *mddev)
  4983. {
  4984. mddev->new_level = 5;
  4985. mddev->new_layout = ALGORITHM_PARITY_N;
  4986. mddev->new_chunk_sectors = mddev->chunk_sectors;
  4987. mddev->raid_disks += 1;
  4988. mddev->delta_disks = 1;
  4989. /* make sure it will be not marked as dirty */
  4990. mddev->recovery_cp = MaxSector;
  4991. return setup_conf(mddev);
  4992. }
  4993. static void *raid5_takeover_raid1(mddev_t *mddev)
  4994. {
  4995. int chunksect;
  4996. if (mddev->raid_disks != 2 ||
  4997. mddev->degraded > 1)
  4998. return ERR_PTR(-EINVAL);
  4999. /* Should check if there are write-behind devices? */
  5000. chunksect = 64*2; /* 64K by default */
  5001. /* The array must be an exact multiple of chunksize */
  5002. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  5003. chunksect >>= 1;
  5004. if ((chunksect<<9) < STRIPE_SIZE)
  5005. /* array size does not allow a suitable chunk size */
  5006. return ERR_PTR(-EINVAL);
  5007. mddev->new_level = 5;
  5008. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5009. mddev->new_chunk_sectors = chunksect;
  5010. return setup_conf(mddev);
  5011. }
  5012. static void *raid5_takeover_raid6(mddev_t *mddev)
  5013. {
  5014. int new_layout;
  5015. switch (mddev->layout) {
  5016. case ALGORITHM_LEFT_ASYMMETRIC_6:
  5017. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  5018. break;
  5019. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  5020. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  5021. break;
  5022. case ALGORITHM_LEFT_SYMMETRIC_6:
  5023. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  5024. break;
  5025. case ALGORITHM_RIGHT_SYMMETRIC_6:
  5026. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  5027. break;
  5028. case ALGORITHM_PARITY_0_6:
  5029. new_layout = ALGORITHM_PARITY_0;
  5030. break;
  5031. case ALGORITHM_PARITY_N:
  5032. new_layout = ALGORITHM_PARITY_N;
  5033. break;
  5034. default:
  5035. return ERR_PTR(-EINVAL);
  5036. }
  5037. mddev->new_level = 5;
  5038. mddev->new_layout = new_layout;
  5039. mddev->delta_disks = -1;
  5040. mddev->raid_disks -= 1;
  5041. return setup_conf(mddev);
  5042. }
  5043. static int raid5_check_reshape(mddev_t *mddev)
  5044. {
  5045. /* For a 2-drive array, the layout and chunk size can be changed
  5046. * immediately as not restriping is needed.
  5047. * For larger arrays we record the new value - after validation
  5048. * to be used by a reshape pass.
  5049. */
  5050. raid5_conf_t *conf = mddev->private;
  5051. int new_chunk = mddev->new_chunk_sectors;
  5052. if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
  5053. return -EINVAL;
  5054. if (new_chunk > 0) {
  5055. if (!is_power_of_2(new_chunk))
  5056. return -EINVAL;
  5057. if (new_chunk < (PAGE_SIZE>>9))
  5058. return -EINVAL;
  5059. if (mddev->array_sectors & (new_chunk-1))
  5060. /* not factor of array size */
  5061. return -EINVAL;
  5062. }
  5063. /* They look valid */
  5064. if (mddev->raid_disks == 2) {
  5065. /* can make the change immediately */
  5066. if (mddev->new_layout >= 0) {
  5067. conf->algorithm = mddev->new_layout;
  5068. mddev->layout = mddev->new_layout;
  5069. }
  5070. if (new_chunk > 0) {
  5071. conf->chunk_sectors = new_chunk ;
  5072. mddev->chunk_sectors = new_chunk;
  5073. }
  5074. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  5075. md_wakeup_thread(mddev->thread);
  5076. }
  5077. return check_reshape(mddev);
  5078. }
  5079. static int raid6_check_reshape(mddev_t *mddev)
  5080. {
  5081. int new_chunk = mddev->new_chunk_sectors;
  5082. if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
  5083. return -EINVAL;
  5084. if (new_chunk > 0) {
  5085. if (!is_power_of_2(new_chunk))
  5086. return -EINVAL;
  5087. if (new_chunk < (PAGE_SIZE >> 9))
  5088. return -EINVAL;
  5089. if (mddev->array_sectors & (new_chunk-1))
  5090. /* not factor of array size */
  5091. return -EINVAL;
  5092. }
  5093. /* They look valid */
  5094. return check_reshape(mddev);
  5095. }
  5096. static void *raid5_takeover(mddev_t *mddev)
  5097. {
  5098. /* raid5 can take over:
  5099. * raid0 - if all devices are the same - make it a raid4 layout
  5100. * raid1 - if there are two drives. We need to know the chunk size
  5101. * raid4 - trivial - just use a raid4 layout.
  5102. * raid6 - Providing it is a *_6 layout
  5103. */
  5104. if (mddev->level == 0) {
  5105. /* for raid0 takeover only one zone is supported */
  5106. struct raid0_private_data *raid0_priv
  5107. = mddev->private;
  5108. if (raid0_priv->nr_strip_zones > 1) {
  5109. printk(KERN_ERR "md: cannot takeover raid 0 with more than one zone.\n");
  5110. return ERR_PTR(-EINVAL);
  5111. }
  5112. return raid5_takeover_raid0(mddev);
  5113. }
  5114. if (mddev->level == 1)
  5115. return raid5_takeover_raid1(mddev);
  5116. if (mddev->level == 4) {
  5117. mddev->new_layout = ALGORITHM_PARITY_N;
  5118. mddev->new_level = 5;
  5119. return setup_conf(mddev);
  5120. }
  5121. if (mddev->level == 6)
  5122. return raid5_takeover_raid6(mddev);
  5123. return ERR_PTR(-EINVAL);
  5124. }
  5125. static void *raid4_takeover(mddev_t *mddev)
  5126. {
  5127. /* raid4 can take over raid5 if layout is right.
  5128. */
  5129. if (mddev->level == 5 &&
  5130. mddev->layout == ALGORITHM_PARITY_N) {
  5131. mddev->new_layout = 0;
  5132. mddev->new_level = 4;
  5133. return setup_conf(mddev);
  5134. }
  5135. return ERR_PTR(-EINVAL);
  5136. }
  5137. static struct mdk_personality raid5_personality;
  5138. static void *raid6_takeover(mddev_t *mddev)
  5139. {
  5140. /* Currently can only take over a raid5. We map the
  5141. * personality to an equivalent raid6 personality
  5142. * with the Q block at the end.
  5143. */
  5144. int new_layout;
  5145. if (mddev->pers != &raid5_personality)
  5146. return ERR_PTR(-EINVAL);
  5147. if (mddev->degraded > 1)
  5148. return ERR_PTR(-EINVAL);
  5149. if (mddev->raid_disks > 253)
  5150. return ERR_PTR(-EINVAL);
  5151. if (mddev->raid_disks < 3)
  5152. return ERR_PTR(-EINVAL);
  5153. switch (mddev->layout) {
  5154. case ALGORITHM_LEFT_ASYMMETRIC:
  5155. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  5156. break;
  5157. case ALGORITHM_RIGHT_ASYMMETRIC:
  5158. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  5159. break;
  5160. case ALGORITHM_LEFT_SYMMETRIC:
  5161. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  5162. break;
  5163. case ALGORITHM_RIGHT_SYMMETRIC:
  5164. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  5165. break;
  5166. case ALGORITHM_PARITY_0:
  5167. new_layout = ALGORITHM_PARITY_0_6;
  5168. break;
  5169. case ALGORITHM_PARITY_N:
  5170. new_layout = ALGORITHM_PARITY_N;
  5171. break;
  5172. default:
  5173. return ERR_PTR(-EINVAL);
  5174. }
  5175. mddev->new_level = 6;
  5176. mddev->new_layout = new_layout;
  5177. mddev->delta_disks = 1;
  5178. mddev->raid_disks += 1;
  5179. return setup_conf(mddev);
  5180. }
  5181. static struct mdk_personality raid6_personality =
  5182. {
  5183. .name = "raid6",
  5184. .level = 6,
  5185. .owner = THIS_MODULE,
  5186. .make_request = make_request,
  5187. .run = run,
  5188. .stop = stop,
  5189. .status = status,
  5190. .error_handler = error,
  5191. .hot_add_disk = raid5_add_disk,
  5192. .hot_remove_disk= raid5_remove_disk,
  5193. .spare_active = raid5_spare_active,
  5194. .sync_request = sync_request,
  5195. .resize = raid5_resize,
  5196. .size = raid5_size,
  5197. .check_reshape = raid6_check_reshape,
  5198. .start_reshape = raid5_start_reshape,
  5199. .finish_reshape = raid5_finish_reshape,
  5200. .quiesce = raid5_quiesce,
  5201. .takeover = raid6_takeover,
  5202. };
  5203. static struct mdk_personality raid5_personality =
  5204. {
  5205. .name = "raid5",
  5206. .level = 5,
  5207. .owner = THIS_MODULE,
  5208. .make_request = make_request,
  5209. .run = run,
  5210. .stop = stop,
  5211. .status = status,
  5212. .error_handler = error,
  5213. .hot_add_disk = raid5_add_disk,
  5214. .hot_remove_disk= raid5_remove_disk,
  5215. .spare_active = raid5_spare_active,
  5216. .sync_request = sync_request,
  5217. .resize = raid5_resize,
  5218. .size = raid5_size,
  5219. .check_reshape = raid5_check_reshape,
  5220. .start_reshape = raid5_start_reshape,
  5221. .finish_reshape = raid5_finish_reshape,
  5222. .quiesce = raid5_quiesce,
  5223. .takeover = raid5_takeover,
  5224. };
  5225. static struct mdk_personality raid4_personality =
  5226. {
  5227. .name = "raid4",
  5228. .level = 4,
  5229. .owner = THIS_MODULE,
  5230. .make_request = make_request,
  5231. .run = run,
  5232. .stop = stop,
  5233. .status = status,
  5234. .error_handler = error,
  5235. .hot_add_disk = raid5_add_disk,
  5236. .hot_remove_disk= raid5_remove_disk,
  5237. .spare_active = raid5_spare_active,
  5238. .sync_request = sync_request,
  5239. .resize = raid5_resize,
  5240. .size = raid5_size,
  5241. .check_reshape = raid5_check_reshape,
  5242. .start_reshape = raid5_start_reshape,
  5243. .finish_reshape = raid5_finish_reshape,
  5244. .quiesce = raid5_quiesce,
  5245. .takeover = raid4_takeover,
  5246. };
  5247. static int __init raid5_init(void)
  5248. {
  5249. register_md_personality(&raid6_personality);
  5250. register_md_personality(&raid5_personality);
  5251. register_md_personality(&raid4_personality);
  5252. return 0;
  5253. }
  5254. static void raid5_exit(void)
  5255. {
  5256. unregister_md_personality(&raid6_personality);
  5257. unregister_md_personality(&raid5_personality);
  5258. unregister_md_personality(&raid4_personality);
  5259. }
  5260. module_init(raid5_init);
  5261. module_exit(raid5_exit);
  5262. MODULE_LICENSE("GPL");
  5263. MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
  5264. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  5265. MODULE_ALIAS("md-raid5");
  5266. MODULE_ALIAS("md-raid4");
  5267. MODULE_ALIAS("md-level-5");
  5268. MODULE_ALIAS("md-level-4");
  5269. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  5270. MODULE_ALIAS("md-raid6");
  5271. MODULE_ALIAS("md-level-6");
  5272. /* This used to be two separate modules, they were: */
  5273. MODULE_ALIAS("raid5");
  5274. MODULE_ALIAS("raid6");