raid5.c 159 KB

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