raid5.c 159 KB

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