raid5.c 174 KB

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