raid5.c 180 KB

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