raid5.c 167 KB

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