raid5.c 168 KB

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