raid5.c 197 KB

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