raid5.c 165 KB

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