dm-raid.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061
  1. /*
  2. * Copyright (C) 2010-2011 Neil Brown
  3. * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include "md.h"
  10. #include "raid1.h"
  11. #include "raid5.h"
  12. #include "raid10.h"
  13. #include "md-bitmap.h"
  14. #include <linux/device-mapper.h>
  15. #define DM_MSG_PREFIX "raid"
  16. #define MAX_RAID_DEVICES 253 /* md-raid kernel limit */
  17. /*
  18. * Minimum sectors of free reshape space per raid device
  19. */
  20. #define MIN_FREE_RESHAPE_SPACE to_sector(4*4096)
  21. /*
  22. * Minimum journal space 4 MiB in sectors.
  23. */
  24. #define MIN_RAID456_JOURNAL_SPACE (4*2048)
  25. static bool devices_handle_discard_safely = false;
  26. /*
  27. * The following flags are used by dm-raid.c to set up the array state.
  28. * They must be cleared before md_run is called.
  29. */
  30. #define FirstUse 10 /* rdev flag */
  31. struct raid_dev {
  32. /*
  33. * Two DM devices, one to hold metadata and one to hold the
  34. * actual data/parity. The reason for this is to not confuse
  35. * ti->len and give more flexibility in altering size and
  36. * characteristics.
  37. *
  38. * While it is possible for this device to be associated
  39. * with a different physical device than the data_dev, it
  40. * is intended for it to be the same.
  41. * |--------- Physical Device ---------|
  42. * |- meta_dev -|------ data_dev ------|
  43. */
  44. struct dm_dev *meta_dev;
  45. struct dm_dev *data_dev;
  46. struct md_rdev rdev;
  47. };
  48. /*
  49. * Bits for establishing rs->ctr_flags
  50. *
  51. * 1 = no flag value
  52. * 2 = flag with value
  53. */
  54. #define __CTR_FLAG_SYNC 0 /* 1 */ /* Not with raid0! */
  55. #define __CTR_FLAG_NOSYNC 1 /* 1 */ /* Not with raid0! */
  56. #define __CTR_FLAG_REBUILD 2 /* 2 */ /* Not with raid0! */
  57. #define __CTR_FLAG_DAEMON_SLEEP 3 /* 2 */ /* Not with raid0! */
  58. #define __CTR_FLAG_MIN_RECOVERY_RATE 4 /* 2 */ /* Not with raid0! */
  59. #define __CTR_FLAG_MAX_RECOVERY_RATE 5 /* 2 */ /* Not with raid0! */
  60. #define __CTR_FLAG_MAX_WRITE_BEHIND 6 /* 2 */ /* Only with raid1! */
  61. #define __CTR_FLAG_WRITE_MOSTLY 7 /* 2 */ /* Only with raid1! */
  62. #define __CTR_FLAG_STRIPE_CACHE 8 /* 2 */ /* Only with raid4/5/6! */
  63. #define __CTR_FLAG_REGION_SIZE 9 /* 2 */ /* Not with raid0! */
  64. #define __CTR_FLAG_RAID10_COPIES 10 /* 2 */ /* Only with raid10 */
  65. #define __CTR_FLAG_RAID10_FORMAT 11 /* 2 */ /* Only with raid10 */
  66. /* New for v1.9.0 */
  67. #define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid1/4/5/6/10! */
  68. #define __CTR_FLAG_DATA_OFFSET 13 /* 2 */ /* Only with reshapable raid4/5/6/10! */
  69. #define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */
  70. /* New for v1.10.0 */
  71. #define __CTR_FLAG_JOURNAL_DEV 15 /* 2 */ /* Only with raid4/5/6 (journal device)! */
  72. /* New for v1.11.1 */
  73. #define __CTR_FLAG_JOURNAL_MODE 16 /* 2 */ /* Only with raid4/5/6 (journal mode)! */
  74. /*
  75. * Flags for rs->ctr_flags field.
  76. */
  77. #define CTR_FLAG_SYNC (1 << __CTR_FLAG_SYNC)
  78. #define CTR_FLAG_NOSYNC (1 << __CTR_FLAG_NOSYNC)
  79. #define CTR_FLAG_REBUILD (1 << __CTR_FLAG_REBUILD)
  80. #define CTR_FLAG_DAEMON_SLEEP (1 << __CTR_FLAG_DAEMON_SLEEP)
  81. #define CTR_FLAG_MIN_RECOVERY_RATE (1 << __CTR_FLAG_MIN_RECOVERY_RATE)
  82. #define CTR_FLAG_MAX_RECOVERY_RATE (1 << __CTR_FLAG_MAX_RECOVERY_RATE)
  83. #define CTR_FLAG_MAX_WRITE_BEHIND (1 << __CTR_FLAG_MAX_WRITE_BEHIND)
  84. #define CTR_FLAG_WRITE_MOSTLY (1 << __CTR_FLAG_WRITE_MOSTLY)
  85. #define CTR_FLAG_STRIPE_CACHE (1 << __CTR_FLAG_STRIPE_CACHE)
  86. #define CTR_FLAG_REGION_SIZE (1 << __CTR_FLAG_REGION_SIZE)
  87. #define CTR_FLAG_RAID10_COPIES (1 << __CTR_FLAG_RAID10_COPIES)
  88. #define CTR_FLAG_RAID10_FORMAT (1 << __CTR_FLAG_RAID10_FORMAT)
  89. #define CTR_FLAG_DELTA_DISKS (1 << __CTR_FLAG_DELTA_DISKS)
  90. #define CTR_FLAG_DATA_OFFSET (1 << __CTR_FLAG_DATA_OFFSET)
  91. #define CTR_FLAG_RAID10_USE_NEAR_SETS (1 << __CTR_FLAG_RAID10_USE_NEAR_SETS)
  92. #define CTR_FLAG_JOURNAL_DEV (1 << __CTR_FLAG_JOURNAL_DEV)
  93. #define CTR_FLAG_JOURNAL_MODE (1 << __CTR_FLAG_JOURNAL_MODE)
  94. /*
  95. * Definitions of various constructor flags to
  96. * be used in checks of valid / invalid flags
  97. * per raid level.
  98. */
  99. /* Define all any sync flags */
  100. #define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
  101. /* Define flags for options without argument (e.g. 'nosync') */
  102. #define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \
  103. CTR_FLAG_RAID10_USE_NEAR_SETS)
  104. /* Define flags for options with one argument (e.g. 'delta_disks +2') */
  105. #define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
  106. CTR_FLAG_WRITE_MOSTLY | \
  107. CTR_FLAG_DAEMON_SLEEP | \
  108. CTR_FLAG_MIN_RECOVERY_RATE | \
  109. CTR_FLAG_MAX_RECOVERY_RATE | \
  110. CTR_FLAG_MAX_WRITE_BEHIND | \
  111. CTR_FLAG_STRIPE_CACHE | \
  112. CTR_FLAG_REGION_SIZE | \
  113. CTR_FLAG_RAID10_COPIES | \
  114. CTR_FLAG_RAID10_FORMAT | \
  115. CTR_FLAG_DELTA_DISKS | \
  116. CTR_FLAG_DATA_OFFSET)
  117. /* Valid options definitions per raid level... */
  118. /* "raid0" does only accept data offset */
  119. #define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET)
  120. /* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */
  121. #define RAID1_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
  122. CTR_FLAG_REBUILD | \
  123. CTR_FLAG_WRITE_MOSTLY | \
  124. CTR_FLAG_DAEMON_SLEEP | \
  125. CTR_FLAG_MIN_RECOVERY_RATE | \
  126. CTR_FLAG_MAX_RECOVERY_RATE | \
  127. CTR_FLAG_MAX_WRITE_BEHIND | \
  128. CTR_FLAG_REGION_SIZE | \
  129. CTR_FLAG_DELTA_DISKS | \
  130. CTR_FLAG_DATA_OFFSET)
  131. /* "raid10" does not accept any raid1 or stripe cache options */
  132. #define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
  133. CTR_FLAG_REBUILD | \
  134. CTR_FLAG_DAEMON_SLEEP | \
  135. CTR_FLAG_MIN_RECOVERY_RATE | \
  136. CTR_FLAG_MAX_RECOVERY_RATE | \
  137. CTR_FLAG_REGION_SIZE | \
  138. CTR_FLAG_RAID10_COPIES | \
  139. CTR_FLAG_RAID10_FORMAT | \
  140. CTR_FLAG_DELTA_DISKS | \
  141. CTR_FLAG_DATA_OFFSET | \
  142. CTR_FLAG_RAID10_USE_NEAR_SETS)
  143. /*
  144. * "raid4/5/6" do not accept any raid1 or raid10 specific options
  145. *
  146. * "raid6" does not accept "nosync", because it is not guaranteed
  147. * that both parity and q-syndrome are being written properly with
  148. * any writes
  149. */
  150. #define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \
  151. CTR_FLAG_REBUILD | \
  152. CTR_FLAG_DAEMON_SLEEP | \
  153. CTR_FLAG_MIN_RECOVERY_RATE | \
  154. CTR_FLAG_MAX_RECOVERY_RATE | \
  155. CTR_FLAG_STRIPE_CACHE | \
  156. CTR_FLAG_REGION_SIZE | \
  157. CTR_FLAG_DELTA_DISKS | \
  158. CTR_FLAG_DATA_OFFSET | \
  159. CTR_FLAG_JOURNAL_DEV | \
  160. CTR_FLAG_JOURNAL_MODE)
  161. #define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \
  162. CTR_FLAG_REBUILD | \
  163. CTR_FLAG_DAEMON_SLEEP | \
  164. CTR_FLAG_MIN_RECOVERY_RATE | \
  165. CTR_FLAG_MAX_RECOVERY_RATE | \
  166. CTR_FLAG_STRIPE_CACHE | \
  167. CTR_FLAG_REGION_SIZE | \
  168. CTR_FLAG_DELTA_DISKS | \
  169. CTR_FLAG_DATA_OFFSET | \
  170. CTR_FLAG_JOURNAL_DEV | \
  171. CTR_FLAG_JOURNAL_MODE)
  172. /* ...valid options definitions per raid level */
  173. /*
  174. * Flags for rs->runtime_flags field
  175. * (RT_FLAG prefix meaning "runtime flag")
  176. *
  177. * These are all internal and used to define runtime state,
  178. * e.g. to prevent another resume from preresume processing
  179. * the raid set all over again.
  180. */
  181. #define RT_FLAG_RS_PRERESUMED 0
  182. #define RT_FLAG_RS_RESUMED 1
  183. #define RT_FLAG_RS_BITMAP_LOADED 2
  184. #define RT_FLAG_UPDATE_SBS 3
  185. #define RT_FLAG_RESHAPE_RS 4
  186. #define RT_FLAG_RS_SUSPENDED 5
  187. #define RT_FLAG_RS_IN_SYNC 6
  188. #define RT_FLAG_RS_RESYNCING 7
  189. /* Array elements of 64 bit needed for rebuild/failed disk bits */
  190. #define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
  191. /*
  192. * raid set level, layout and chunk sectors backup/restore
  193. */
  194. struct rs_layout {
  195. int new_level;
  196. int new_layout;
  197. int new_chunk_sectors;
  198. };
  199. struct raid_set {
  200. struct dm_target *ti;
  201. uint32_t stripe_cache_entries;
  202. unsigned long ctr_flags;
  203. unsigned long runtime_flags;
  204. uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
  205. int raid_disks;
  206. int delta_disks;
  207. int data_offset;
  208. int raid10_copies;
  209. int requested_bitmap_chunk_sectors;
  210. struct mddev md;
  211. struct raid_type *raid_type;
  212. struct dm_target_callbacks callbacks;
  213. /* Optional raid4/5/6 journal device */
  214. struct journal_dev {
  215. struct dm_dev *dev;
  216. struct md_rdev rdev;
  217. int mode;
  218. } journal_dev;
  219. struct raid_dev dev[0];
  220. };
  221. static void rs_config_backup(struct raid_set *rs, struct rs_layout *l)
  222. {
  223. struct mddev *mddev = &rs->md;
  224. l->new_level = mddev->new_level;
  225. l->new_layout = mddev->new_layout;
  226. l->new_chunk_sectors = mddev->new_chunk_sectors;
  227. }
  228. static void rs_config_restore(struct raid_set *rs, struct rs_layout *l)
  229. {
  230. struct mddev *mddev = &rs->md;
  231. mddev->new_level = l->new_level;
  232. mddev->new_layout = l->new_layout;
  233. mddev->new_chunk_sectors = l->new_chunk_sectors;
  234. }
  235. /* raid10 algorithms (i.e. formats) */
  236. #define ALGORITHM_RAID10_DEFAULT 0
  237. #define ALGORITHM_RAID10_NEAR 1
  238. #define ALGORITHM_RAID10_OFFSET 2
  239. #define ALGORITHM_RAID10_FAR 3
  240. /* Supported raid types and properties. */
  241. static struct raid_type {
  242. const char *name; /* RAID algorithm. */
  243. const char *descr; /* Descriptor text for logging. */
  244. const unsigned int parity_devs; /* # of parity devices. */
  245. const unsigned int minimal_devs;/* minimal # of devices in set. */
  246. const unsigned int level; /* RAID level. */
  247. const unsigned int algorithm; /* RAID algorithm. */
  248. } raid_types[] = {
  249. {"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */},
  250. {"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */},
  251. {"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR},
  252. {"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET},
  253. {"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR},
  254. {"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT},
  255. {"raid4", "raid4 (dedicated first parity disk)", 1, 2, 5, ALGORITHM_PARITY_0}, /* raid4 layout = raid5_0 */
  256. {"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N},
  257. {"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
  258. {"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
  259. {"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
  260. {"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
  261. {"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
  262. {"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
  263. {"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE},
  264. {"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6},
  265. {"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6},
  266. {"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6},
  267. {"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6},
  268. {"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6}
  269. };
  270. /* True, if @v is in inclusive range [@min, @max] */
  271. static bool __within_range(long v, long min, long max)
  272. {
  273. return v >= min && v <= max;
  274. }
  275. /* All table line arguments are defined here */
  276. static struct arg_name_flag {
  277. const unsigned long flag;
  278. const char *name;
  279. } __arg_name_flags[] = {
  280. { CTR_FLAG_SYNC, "sync"},
  281. { CTR_FLAG_NOSYNC, "nosync"},
  282. { CTR_FLAG_REBUILD, "rebuild"},
  283. { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
  284. { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
  285. { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
  286. { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
  287. { CTR_FLAG_WRITE_MOSTLY, "write_mostly"},
  288. { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
  289. { CTR_FLAG_REGION_SIZE, "region_size"},
  290. { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
  291. { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
  292. { CTR_FLAG_DATA_OFFSET, "data_offset"},
  293. { CTR_FLAG_DELTA_DISKS, "delta_disks"},
  294. { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
  295. { CTR_FLAG_JOURNAL_DEV, "journal_dev" },
  296. { CTR_FLAG_JOURNAL_MODE, "journal_mode" },
  297. };
  298. /* Return argument name string for given @flag */
  299. static const char *dm_raid_arg_name_by_flag(const uint32_t flag)
  300. {
  301. if (hweight32(flag) == 1) {
  302. struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
  303. while (anf-- > __arg_name_flags)
  304. if (flag & anf->flag)
  305. return anf->name;
  306. } else
  307. DMERR("%s called with more than one flag!", __func__);
  308. return NULL;
  309. }
  310. /* Define correlation of raid456 journal cache modes and dm-raid target line parameters */
  311. static struct {
  312. const int mode;
  313. const char *param;
  314. } _raid456_journal_mode[] = {
  315. { R5C_JOURNAL_MODE_WRITE_THROUGH , "writethrough" },
  316. { R5C_JOURNAL_MODE_WRITE_BACK , "writeback" }
  317. };
  318. /* Return MD raid4/5/6 journal mode for dm @journal_mode one */
  319. static int dm_raid_journal_mode_to_md(const char *mode)
  320. {
  321. int m = ARRAY_SIZE(_raid456_journal_mode);
  322. while (m--)
  323. if (!strcasecmp(mode, _raid456_journal_mode[m].param))
  324. return _raid456_journal_mode[m].mode;
  325. return -EINVAL;
  326. }
  327. /* Return dm-raid raid4/5/6 journal mode string for @mode */
  328. static const char *md_journal_mode_to_dm_raid(const int mode)
  329. {
  330. int m = ARRAY_SIZE(_raid456_journal_mode);
  331. while (m--)
  332. if (mode == _raid456_journal_mode[m].mode)
  333. return _raid456_journal_mode[m].param;
  334. return "unknown";
  335. }
  336. /*
  337. * Bool helpers to test for various raid levels of a raid set.
  338. * It's level as reported by the superblock rather than
  339. * the requested raid_type passed to the constructor.
  340. */
  341. /* Return true, if raid set in @rs is raid0 */
  342. static bool rs_is_raid0(struct raid_set *rs)
  343. {
  344. return !rs->md.level;
  345. }
  346. /* Return true, if raid set in @rs is raid1 */
  347. static bool rs_is_raid1(struct raid_set *rs)
  348. {
  349. return rs->md.level == 1;
  350. }
  351. /* Return true, if raid set in @rs is raid10 */
  352. static bool rs_is_raid10(struct raid_set *rs)
  353. {
  354. return rs->md.level == 10;
  355. }
  356. /* Return true, if raid set in @rs is level 6 */
  357. static bool rs_is_raid6(struct raid_set *rs)
  358. {
  359. return rs->md.level == 6;
  360. }
  361. /* Return true, if raid set in @rs is level 4, 5 or 6 */
  362. static bool rs_is_raid456(struct raid_set *rs)
  363. {
  364. return __within_range(rs->md.level, 4, 6);
  365. }
  366. /* Return true, if raid set in @rs is reshapable */
  367. static bool __is_raid10_far(int layout);
  368. static bool rs_is_reshapable(struct raid_set *rs)
  369. {
  370. return rs_is_raid456(rs) ||
  371. (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout));
  372. }
  373. /* Return true, if raid set in @rs is recovering */
  374. static bool rs_is_recovering(struct raid_set *rs)
  375. {
  376. return rs->md.recovery_cp < rs->md.dev_sectors;
  377. }
  378. /* Return true, if raid set in @rs is reshaping */
  379. static bool rs_is_reshaping(struct raid_set *rs)
  380. {
  381. return rs->md.reshape_position != MaxSector;
  382. }
  383. /*
  384. * bool helpers to test for various raid levels of a raid type @rt
  385. */
  386. /* Return true, if raid type in @rt is raid0 */
  387. static bool rt_is_raid0(struct raid_type *rt)
  388. {
  389. return !rt->level;
  390. }
  391. /* Return true, if raid type in @rt is raid1 */
  392. static bool rt_is_raid1(struct raid_type *rt)
  393. {
  394. return rt->level == 1;
  395. }
  396. /* Return true, if raid type in @rt is raid10 */
  397. static bool rt_is_raid10(struct raid_type *rt)
  398. {
  399. return rt->level == 10;
  400. }
  401. /* Return true, if raid type in @rt is raid4/5 */
  402. static bool rt_is_raid45(struct raid_type *rt)
  403. {
  404. return __within_range(rt->level, 4, 5);
  405. }
  406. /* Return true, if raid type in @rt is raid6 */
  407. static bool rt_is_raid6(struct raid_type *rt)
  408. {
  409. return rt->level == 6;
  410. }
  411. /* Return true, if raid type in @rt is raid4/5/6 */
  412. static bool rt_is_raid456(struct raid_type *rt)
  413. {
  414. return __within_range(rt->level, 4, 6);
  415. }
  416. /* END: raid level bools */
  417. /* Return valid ctr flags for the raid level of @rs */
  418. static unsigned long __valid_flags(struct raid_set *rs)
  419. {
  420. if (rt_is_raid0(rs->raid_type))
  421. return RAID0_VALID_FLAGS;
  422. else if (rt_is_raid1(rs->raid_type))
  423. return RAID1_VALID_FLAGS;
  424. else if (rt_is_raid10(rs->raid_type))
  425. return RAID10_VALID_FLAGS;
  426. else if (rt_is_raid45(rs->raid_type))
  427. return RAID45_VALID_FLAGS;
  428. else if (rt_is_raid6(rs->raid_type))
  429. return RAID6_VALID_FLAGS;
  430. return 0;
  431. }
  432. /*
  433. * Check for valid flags set on @rs
  434. *
  435. * Has to be called after parsing of the ctr flags!
  436. */
  437. static int rs_check_for_valid_flags(struct raid_set *rs)
  438. {
  439. if (rs->ctr_flags & ~__valid_flags(rs)) {
  440. rs->ti->error = "Invalid flags combination";
  441. return -EINVAL;
  442. }
  443. return 0;
  444. }
  445. /* MD raid10 bit definitions and helpers */
  446. #define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */
  447. #define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
  448. #define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */
  449. #define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */
  450. /* Return md raid10 near copies for @layout */
  451. static unsigned int __raid10_near_copies(int layout)
  452. {
  453. return layout & 0xFF;
  454. }
  455. /* Return md raid10 far copies for @layout */
  456. static unsigned int __raid10_far_copies(int layout)
  457. {
  458. return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
  459. }
  460. /* Return true if md raid10 offset for @layout */
  461. static bool __is_raid10_offset(int layout)
  462. {
  463. return !!(layout & RAID10_OFFSET);
  464. }
  465. /* Return true if md raid10 near for @layout */
  466. static bool __is_raid10_near(int layout)
  467. {
  468. return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1;
  469. }
  470. /* Return true if md raid10 far for @layout */
  471. static bool __is_raid10_far(int layout)
  472. {
  473. return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
  474. }
  475. /* Return md raid10 layout string for @layout */
  476. static const char *raid10_md_layout_to_format(int layout)
  477. {
  478. /*
  479. * Bit 16 stands for "offset"
  480. * (i.e. adjacent stripes hold copies)
  481. *
  482. * Refer to MD's raid10.c for details
  483. */
  484. if (__is_raid10_offset(layout))
  485. return "offset";
  486. if (__raid10_near_copies(layout) > 1)
  487. return "near";
  488. if (__raid10_far_copies(layout) > 1)
  489. return "far";
  490. return "unknown";
  491. }
  492. /* Return md raid10 algorithm for @name */
  493. static int raid10_name_to_format(const char *name)
  494. {
  495. if (!strcasecmp(name, "near"))
  496. return ALGORITHM_RAID10_NEAR;
  497. else if (!strcasecmp(name, "offset"))
  498. return ALGORITHM_RAID10_OFFSET;
  499. else if (!strcasecmp(name, "far"))
  500. return ALGORITHM_RAID10_FAR;
  501. return -EINVAL;
  502. }
  503. /* Return md raid10 copies for @layout */
  504. static unsigned int raid10_md_layout_to_copies(int layout)
  505. {
  506. return max(__raid10_near_copies(layout), __raid10_far_copies(layout));
  507. }
  508. /* Return md raid10 format id for @format string */
  509. static int raid10_format_to_md_layout(struct raid_set *rs,
  510. unsigned int algorithm,
  511. unsigned int copies)
  512. {
  513. unsigned int n = 1, f = 1, r = 0;
  514. /*
  515. * MD resilienece flaw:
  516. *
  517. * enabling use_far_sets for far/offset formats causes copies
  518. * to be colocated on the same devs together with their origins!
  519. *
  520. * -> disable it for now in the definition above
  521. */
  522. if (algorithm == ALGORITHM_RAID10_DEFAULT ||
  523. algorithm == ALGORITHM_RAID10_NEAR)
  524. n = copies;
  525. else if (algorithm == ALGORITHM_RAID10_OFFSET) {
  526. f = copies;
  527. r = RAID10_OFFSET;
  528. if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
  529. r |= RAID10_USE_FAR_SETS;
  530. } else if (algorithm == ALGORITHM_RAID10_FAR) {
  531. f = copies;
  532. r = !RAID10_OFFSET;
  533. if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
  534. r |= RAID10_USE_FAR_SETS;
  535. } else
  536. return -EINVAL;
  537. return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
  538. }
  539. /* END: MD raid10 bit definitions and helpers */
  540. /* Check for any of the raid10 algorithms */
  541. static bool __got_raid10(struct raid_type *rtp, const int layout)
  542. {
  543. if (rtp->level == 10) {
  544. switch (rtp->algorithm) {
  545. case ALGORITHM_RAID10_DEFAULT:
  546. case ALGORITHM_RAID10_NEAR:
  547. return __is_raid10_near(layout);
  548. case ALGORITHM_RAID10_OFFSET:
  549. return __is_raid10_offset(layout);
  550. case ALGORITHM_RAID10_FAR:
  551. return __is_raid10_far(layout);
  552. default:
  553. break;
  554. }
  555. }
  556. return false;
  557. }
  558. /* Return raid_type for @name */
  559. static struct raid_type *get_raid_type(const char *name)
  560. {
  561. struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
  562. while (rtp-- > raid_types)
  563. if (!strcasecmp(rtp->name, name))
  564. return rtp;
  565. return NULL;
  566. }
  567. /* Return raid_type for @name based derived from @level and @layout */
  568. static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
  569. {
  570. struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
  571. while (rtp-- > raid_types) {
  572. /* RAID10 special checks based on @layout flags/properties */
  573. if (rtp->level == level &&
  574. (__got_raid10(rtp, layout) || rtp->algorithm == layout))
  575. return rtp;
  576. }
  577. return NULL;
  578. }
  579. /* Adjust rdev sectors */
  580. static void rs_set_rdev_sectors(struct raid_set *rs)
  581. {
  582. struct mddev *mddev = &rs->md;
  583. struct md_rdev *rdev;
  584. /*
  585. * raid10 sets rdev->sector to the device size, which
  586. * is unintended in case of out-of-place reshaping
  587. */
  588. rdev_for_each(rdev, mddev)
  589. if (!test_bit(Journal, &rdev->flags))
  590. rdev->sectors = mddev->dev_sectors;
  591. }
  592. /*
  593. * Change bdev capacity of @rs in case of a disk add/remove reshape
  594. */
  595. static void rs_set_capacity(struct raid_set *rs)
  596. {
  597. struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table));
  598. set_capacity(gendisk, rs->md.array_sectors);
  599. revalidate_disk(gendisk);
  600. }
  601. /*
  602. * Set the mddev properties in @rs to the current
  603. * ones retrieved from the freshest superblock
  604. */
  605. static void rs_set_cur(struct raid_set *rs)
  606. {
  607. struct mddev *mddev = &rs->md;
  608. mddev->new_level = mddev->level;
  609. mddev->new_layout = mddev->layout;
  610. mddev->new_chunk_sectors = mddev->chunk_sectors;
  611. }
  612. /*
  613. * Set the mddev properties in @rs to the new
  614. * ones requested by the ctr
  615. */
  616. static void rs_set_new(struct raid_set *rs)
  617. {
  618. struct mddev *mddev = &rs->md;
  619. mddev->level = mddev->new_level;
  620. mddev->layout = mddev->new_layout;
  621. mddev->chunk_sectors = mddev->new_chunk_sectors;
  622. mddev->raid_disks = rs->raid_disks;
  623. mddev->delta_disks = 0;
  624. }
  625. static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
  626. unsigned int raid_devs)
  627. {
  628. unsigned int i;
  629. struct raid_set *rs;
  630. if (raid_devs <= raid_type->parity_devs) {
  631. ti->error = "Insufficient number of devices";
  632. return ERR_PTR(-EINVAL);
  633. }
  634. rs = kzalloc(struct_size(rs, dev, raid_devs), GFP_KERNEL);
  635. if (!rs) {
  636. ti->error = "Cannot allocate raid context";
  637. return ERR_PTR(-ENOMEM);
  638. }
  639. mddev_init(&rs->md);
  640. rs->raid_disks = raid_devs;
  641. rs->delta_disks = 0;
  642. rs->ti = ti;
  643. rs->raid_type = raid_type;
  644. rs->stripe_cache_entries = 256;
  645. rs->md.raid_disks = raid_devs;
  646. rs->md.level = raid_type->level;
  647. rs->md.new_level = rs->md.level;
  648. rs->md.layout = raid_type->algorithm;
  649. rs->md.new_layout = rs->md.layout;
  650. rs->md.delta_disks = 0;
  651. rs->md.recovery_cp = MaxSector;
  652. for (i = 0; i < raid_devs; i++)
  653. md_rdev_init(&rs->dev[i].rdev);
  654. /*
  655. * Remaining items to be initialized by further RAID params:
  656. * rs->md.persistent
  657. * rs->md.external
  658. * rs->md.chunk_sectors
  659. * rs->md.new_chunk_sectors
  660. * rs->md.dev_sectors
  661. */
  662. return rs;
  663. }
  664. /* Free all @rs allocations */
  665. static void raid_set_free(struct raid_set *rs)
  666. {
  667. int i;
  668. if (rs->journal_dev.dev) {
  669. md_rdev_clear(&rs->journal_dev.rdev);
  670. dm_put_device(rs->ti, rs->journal_dev.dev);
  671. }
  672. for (i = 0; i < rs->raid_disks; i++) {
  673. if (rs->dev[i].meta_dev)
  674. dm_put_device(rs->ti, rs->dev[i].meta_dev);
  675. md_rdev_clear(&rs->dev[i].rdev);
  676. if (rs->dev[i].data_dev)
  677. dm_put_device(rs->ti, rs->dev[i].data_dev);
  678. }
  679. kfree(rs);
  680. }
  681. /*
  682. * For every device we have two words
  683. * <meta_dev>: meta device name or '-' if missing
  684. * <data_dev>: data device name or '-' if missing
  685. *
  686. * The following are permitted:
  687. * - -
  688. * - <data_dev>
  689. * <meta_dev> <data_dev>
  690. *
  691. * The following is not allowed:
  692. * <meta_dev> -
  693. *
  694. * This code parses those words. If there is a failure,
  695. * the caller must use raid_set_free() to unwind the operations.
  696. */
  697. static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
  698. {
  699. int i;
  700. int rebuild = 0;
  701. int metadata_available = 0;
  702. int r = 0;
  703. const char *arg;
  704. /* Put off the number of raid devices argument to get to dev pairs */
  705. arg = dm_shift_arg(as);
  706. if (!arg)
  707. return -EINVAL;
  708. for (i = 0; i < rs->raid_disks; i++) {
  709. rs->dev[i].rdev.raid_disk = i;
  710. rs->dev[i].meta_dev = NULL;
  711. rs->dev[i].data_dev = NULL;
  712. /*
  713. * There are no offsets initially.
  714. * Out of place reshape will set them accordingly.
  715. */
  716. rs->dev[i].rdev.data_offset = 0;
  717. rs->dev[i].rdev.new_data_offset = 0;
  718. rs->dev[i].rdev.mddev = &rs->md;
  719. arg = dm_shift_arg(as);
  720. if (!arg)
  721. return -EINVAL;
  722. if (strcmp(arg, "-")) {
  723. r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
  724. &rs->dev[i].meta_dev);
  725. if (r) {
  726. rs->ti->error = "RAID metadata device lookup failure";
  727. return r;
  728. }
  729. rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
  730. if (!rs->dev[i].rdev.sb_page) {
  731. rs->ti->error = "Failed to allocate superblock page";
  732. return -ENOMEM;
  733. }
  734. }
  735. arg = dm_shift_arg(as);
  736. if (!arg)
  737. return -EINVAL;
  738. if (!strcmp(arg, "-")) {
  739. if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
  740. (!rs->dev[i].rdev.recovery_offset)) {
  741. rs->ti->error = "Drive designated for rebuild not specified";
  742. return -EINVAL;
  743. }
  744. if (rs->dev[i].meta_dev) {
  745. rs->ti->error = "No data device supplied with metadata device";
  746. return -EINVAL;
  747. }
  748. continue;
  749. }
  750. r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
  751. &rs->dev[i].data_dev);
  752. if (r) {
  753. rs->ti->error = "RAID device lookup failure";
  754. return r;
  755. }
  756. if (rs->dev[i].meta_dev) {
  757. metadata_available = 1;
  758. rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
  759. }
  760. rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
  761. list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
  762. if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
  763. rebuild++;
  764. }
  765. if (rs->journal_dev.dev)
  766. list_add_tail(&rs->journal_dev.rdev.same_set, &rs->md.disks);
  767. if (metadata_available) {
  768. rs->md.external = 0;
  769. rs->md.persistent = 1;
  770. rs->md.major_version = 2;
  771. } else if (rebuild && !rs->md.recovery_cp) {
  772. /*
  773. * Without metadata, we will not be able to tell if the array
  774. * is in-sync or not - we must assume it is not. Therefore,
  775. * it is impossible to rebuild a drive.
  776. *
  777. * Even if there is metadata, the on-disk information may
  778. * indicate that the array is not in-sync and it will then
  779. * fail at that time.
  780. *
  781. * User could specify 'nosync' option if desperate.
  782. */
  783. rs->ti->error = "Unable to rebuild drive while array is not in-sync";
  784. return -EINVAL;
  785. }
  786. return 0;
  787. }
  788. /*
  789. * validate_region_size
  790. * @rs
  791. * @region_size: region size in sectors. If 0, pick a size (4MiB default).
  792. *
  793. * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
  794. * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
  795. *
  796. * Returns: 0 on success, -EINVAL on failure.
  797. */
  798. static int validate_region_size(struct raid_set *rs, unsigned long region_size)
  799. {
  800. unsigned long min_region_size = rs->ti->len / (1 << 21);
  801. if (rs_is_raid0(rs))
  802. return 0;
  803. if (!region_size) {
  804. /*
  805. * Choose a reasonable default. All figures in sectors.
  806. */
  807. if (min_region_size > (1 << 13)) {
  808. /* If not a power of 2, make it the next power of 2 */
  809. region_size = roundup_pow_of_two(min_region_size);
  810. DMINFO("Choosing default region size of %lu sectors",
  811. region_size);
  812. } else {
  813. DMINFO("Choosing default region size of 4MiB");
  814. region_size = 1 << 13; /* sectors */
  815. }
  816. } else {
  817. /*
  818. * Validate user-supplied value.
  819. */
  820. if (region_size > rs->ti->len) {
  821. rs->ti->error = "Supplied region size is too large";
  822. return -EINVAL;
  823. }
  824. if (region_size < min_region_size) {
  825. DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
  826. region_size, min_region_size);
  827. rs->ti->error = "Supplied region size is too small";
  828. return -EINVAL;
  829. }
  830. if (!is_power_of_2(region_size)) {
  831. rs->ti->error = "Region size is not a power of 2";
  832. return -EINVAL;
  833. }
  834. if (region_size < rs->md.chunk_sectors) {
  835. rs->ti->error = "Region size is smaller than the chunk size";
  836. return -EINVAL;
  837. }
  838. }
  839. /*
  840. * Convert sectors to bytes.
  841. */
  842. rs->md.bitmap_info.chunksize = to_bytes(region_size);
  843. return 0;
  844. }
  845. /*
  846. * validate_raid_redundancy
  847. * @rs
  848. *
  849. * Determine if there are enough devices in the array that haven't
  850. * failed (or are being rebuilt) to form a usable array.
  851. *
  852. * Returns: 0 on success, -EINVAL on failure.
  853. */
  854. static int validate_raid_redundancy(struct raid_set *rs)
  855. {
  856. unsigned int i, rebuild_cnt = 0;
  857. unsigned int rebuilds_per_group = 0, copies;
  858. unsigned int group_size, last_group_start;
  859. for (i = 0; i < rs->md.raid_disks; i++)
  860. if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
  861. !rs->dev[i].rdev.sb_page)
  862. rebuild_cnt++;
  863. switch (rs->md.level) {
  864. case 0:
  865. break;
  866. case 1:
  867. if (rebuild_cnt >= rs->md.raid_disks)
  868. goto too_many;
  869. break;
  870. case 4:
  871. case 5:
  872. case 6:
  873. if (rebuild_cnt > rs->raid_type->parity_devs)
  874. goto too_many;
  875. break;
  876. case 10:
  877. copies = raid10_md_layout_to_copies(rs->md.new_layout);
  878. if (copies < 2) {
  879. DMERR("Bogus raid10 data copies < 2!");
  880. return -EINVAL;
  881. }
  882. if (rebuild_cnt < copies)
  883. break;
  884. /*
  885. * It is possible to have a higher rebuild count for RAID10,
  886. * as long as the failed devices occur in different mirror
  887. * groups (i.e. different stripes).
  888. *
  889. * When checking "near" format, make sure no adjacent devices
  890. * have failed beyond what can be handled. In addition to the
  891. * simple case where the number of devices is a multiple of the
  892. * number of copies, we must also handle cases where the number
  893. * of devices is not a multiple of the number of copies.
  894. * E.g. dev1 dev2 dev3 dev4 dev5
  895. * A A B B C
  896. * C D D E E
  897. */
  898. if (__is_raid10_near(rs->md.new_layout)) {
  899. for (i = 0; i < rs->md.raid_disks; i++) {
  900. if (!(i % copies))
  901. rebuilds_per_group = 0;
  902. if ((!rs->dev[i].rdev.sb_page ||
  903. !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
  904. (++rebuilds_per_group >= copies))
  905. goto too_many;
  906. }
  907. break;
  908. }
  909. /*
  910. * When checking "far" and "offset" formats, we need to ensure
  911. * that the device that holds its copy is not also dead or
  912. * being rebuilt. (Note that "far" and "offset" formats only
  913. * support two copies right now. These formats also only ever
  914. * use the 'use_far_sets' variant.)
  915. *
  916. * This check is somewhat complicated by the need to account
  917. * for arrays that are not a multiple of (far) copies. This
  918. * results in the need to treat the last (potentially larger)
  919. * set differently.
  920. */
  921. group_size = (rs->md.raid_disks / copies);
  922. last_group_start = (rs->md.raid_disks / group_size) - 1;
  923. last_group_start *= group_size;
  924. for (i = 0; i < rs->md.raid_disks; i++) {
  925. if (!(i % copies) && !(i > last_group_start))
  926. rebuilds_per_group = 0;
  927. if ((!rs->dev[i].rdev.sb_page ||
  928. !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
  929. (++rebuilds_per_group >= copies))
  930. goto too_many;
  931. }
  932. break;
  933. default:
  934. if (rebuild_cnt)
  935. return -EINVAL;
  936. }
  937. return 0;
  938. too_many:
  939. return -EINVAL;
  940. }
  941. /*
  942. * Possible arguments are...
  943. * <chunk_size> [optional_args]
  944. *
  945. * Argument definitions
  946. * <chunk_size> The number of sectors per disk that
  947. * will form the "stripe"
  948. * [[no]sync] Force or prevent recovery of the
  949. * entire array
  950. * [rebuild <idx>] Rebuild the drive indicated by the index
  951. * [daemon_sleep <ms>] Time between bitmap daemon work to
  952. * clear bits
  953. * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
  954. * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
  955. * [write_mostly <idx>] Indicate a write mostly drive via index
  956. * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
  957. * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
  958. * [region_size <sectors>] Defines granularity of bitmap
  959. * [journal_dev <dev>] raid4/5/6 journaling deviice
  960. * (i.e. write hole closing log)
  961. *
  962. * RAID10-only options:
  963. * [raid10_copies <# copies>] Number of copies. (Default: 2)
  964. * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
  965. */
  966. static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
  967. unsigned int num_raid_params)
  968. {
  969. int value, raid10_format = ALGORITHM_RAID10_DEFAULT;
  970. unsigned int raid10_copies = 2;
  971. unsigned int i, write_mostly = 0;
  972. unsigned int region_size = 0;
  973. sector_t max_io_len;
  974. const char *arg, *key;
  975. struct raid_dev *rd;
  976. struct raid_type *rt = rs->raid_type;
  977. arg = dm_shift_arg(as);
  978. num_raid_params--; /* Account for chunk_size argument */
  979. if (kstrtoint(arg, 10, &value) < 0) {
  980. rs->ti->error = "Bad numerical argument given for chunk_size";
  981. return -EINVAL;
  982. }
  983. /*
  984. * First, parse the in-order required arguments
  985. * "chunk_size" is the only argument of this type.
  986. */
  987. if (rt_is_raid1(rt)) {
  988. if (value)
  989. DMERR("Ignoring chunk size parameter for RAID 1");
  990. value = 0;
  991. } else if (!is_power_of_2(value)) {
  992. rs->ti->error = "Chunk size must be a power of 2";
  993. return -EINVAL;
  994. } else if (value < 8) {
  995. rs->ti->error = "Chunk size value is too small";
  996. return -EINVAL;
  997. }
  998. rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
  999. /*
  1000. * We set each individual device as In_sync with a completed
  1001. * 'recovery_offset'. If there has been a device failure or
  1002. * replacement then one of the following cases applies:
  1003. *
  1004. * 1) User specifies 'rebuild'.
  1005. * - Device is reset when param is read.
  1006. * 2) A new device is supplied.
  1007. * - No matching superblock found, resets device.
  1008. * 3) Device failure was transient and returns on reload.
  1009. * - Failure noticed, resets device for bitmap replay.
  1010. * 4) Device hadn't completed recovery after previous failure.
  1011. * - Superblock is read and overrides recovery_offset.
  1012. *
  1013. * What is found in the superblocks of the devices is always
  1014. * authoritative, unless 'rebuild' or '[no]sync' was specified.
  1015. */
  1016. for (i = 0; i < rs->raid_disks; i++) {
  1017. set_bit(In_sync, &rs->dev[i].rdev.flags);
  1018. rs->dev[i].rdev.recovery_offset = MaxSector;
  1019. }
  1020. /*
  1021. * Second, parse the unordered optional arguments
  1022. */
  1023. for (i = 0; i < num_raid_params; i++) {
  1024. key = dm_shift_arg(as);
  1025. if (!key) {
  1026. rs->ti->error = "Not enough raid parameters given";
  1027. return -EINVAL;
  1028. }
  1029. if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) {
  1030. if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
  1031. rs->ti->error = "Only one 'nosync' argument allowed";
  1032. return -EINVAL;
  1033. }
  1034. continue;
  1035. }
  1036. if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) {
  1037. if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) {
  1038. rs->ti->error = "Only one 'sync' argument allowed";
  1039. return -EINVAL;
  1040. }
  1041. continue;
  1042. }
  1043. if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
  1044. if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
  1045. rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
  1046. return -EINVAL;
  1047. }
  1048. continue;
  1049. }
  1050. arg = dm_shift_arg(as);
  1051. i++; /* Account for the argument pairs */
  1052. if (!arg) {
  1053. rs->ti->error = "Wrong number of raid parameters given";
  1054. return -EINVAL;
  1055. }
  1056. /*
  1057. * Parameters that take a string value are checked here.
  1058. */
  1059. /* "raid10_format {near|offset|far} */
  1060. if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) {
  1061. if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) {
  1062. rs->ti->error = "Only one 'raid10_format' argument pair allowed";
  1063. return -EINVAL;
  1064. }
  1065. if (!rt_is_raid10(rt)) {
  1066. rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
  1067. return -EINVAL;
  1068. }
  1069. raid10_format = raid10_name_to_format(arg);
  1070. if (raid10_format < 0) {
  1071. rs->ti->error = "Invalid 'raid10_format' value given";
  1072. return raid10_format;
  1073. }
  1074. continue;
  1075. }
  1076. /* "journal_dev <dev>" */
  1077. if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV))) {
  1078. int r;
  1079. struct md_rdev *jdev;
  1080. if (test_and_set_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
  1081. rs->ti->error = "Only one raid4/5/6 set journaling device allowed";
  1082. return -EINVAL;
  1083. }
  1084. if (!rt_is_raid456(rt)) {
  1085. rs->ti->error = "'journal_dev' is an invalid parameter for this RAID type";
  1086. return -EINVAL;
  1087. }
  1088. r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
  1089. &rs->journal_dev.dev);
  1090. if (r) {
  1091. rs->ti->error = "raid4/5/6 journal device lookup failure";
  1092. return r;
  1093. }
  1094. jdev = &rs->journal_dev.rdev;
  1095. md_rdev_init(jdev);
  1096. jdev->mddev = &rs->md;
  1097. jdev->bdev = rs->journal_dev.dev->bdev;
  1098. jdev->sectors = to_sector(i_size_read(jdev->bdev->bd_inode));
  1099. if (jdev->sectors < MIN_RAID456_JOURNAL_SPACE) {
  1100. rs->ti->error = "No space for raid4/5/6 journal";
  1101. return -ENOSPC;
  1102. }
  1103. rs->journal_dev.mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
  1104. set_bit(Journal, &jdev->flags);
  1105. continue;
  1106. }
  1107. /* "journal_mode <mode>" ("journal_dev" mandatory!) */
  1108. if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE))) {
  1109. int r;
  1110. if (!test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
  1111. rs->ti->error = "raid4/5/6 'journal_mode' is invalid without 'journal_dev'";
  1112. return -EINVAL;
  1113. }
  1114. if (test_and_set_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
  1115. rs->ti->error = "Only one raid4/5/6 'journal_mode' argument allowed";
  1116. return -EINVAL;
  1117. }
  1118. r = dm_raid_journal_mode_to_md(arg);
  1119. if (r < 0) {
  1120. rs->ti->error = "Invalid 'journal_mode' argument";
  1121. return r;
  1122. }
  1123. rs->journal_dev.mode = r;
  1124. continue;
  1125. }
  1126. /*
  1127. * Parameters with number values from here on.
  1128. */
  1129. if (kstrtoint(arg, 10, &value) < 0) {
  1130. rs->ti->error = "Bad numerical argument given in raid params";
  1131. return -EINVAL;
  1132. }
  1133. if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
  1134. /*
  1135. * "rebuild" is being passed in by userspace to provide
  1136. * indexes of replaced devices and to set up additional
  1137. * devices on raid level takeover.
  1138. */
  1139. if (!__within_range(value, 0, rs->raid_disks - 1)) {
  1140. rs->ti->error = "Invalid rebuild index given";
  1141. return -EINVAL;
  1142. }
  1143. if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
  1144. rs->ti->error = "rebuild for this index already given";
  1145. return -EINVAL;
  1146. }
  1147. rd = rs->dev + value;
  1148. clear_bit(In_sync, &rd->rdev.flags);
  1149. clear_bit(Faulty, &rd->rdev.flags);
  1150. rd->rdev.recovery_offset = 0;
  1151. set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags);
  1152. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
  1153. if (!rt_is_raid1(rt)) {
  1154. rs->ti->error = "write_mostly option is only valid for RAID1";
  1155. return -EINVAL;
  1156. }
  1157. if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
  1158. rs->ti->error = "Invalid write_mostly index given";
  1159. return -EINVAL;
  1160. }
  1161. write_mostly++;
  1162. set_bit(WriteMostly, &rs->dev[value].rdev.flags);
  1163. set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
  1164. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
  1165. if (!rt_is_raid1(rt)) {
  1166. rs->ti->error = "max_write_behind option is only valid for RAID1";
  1167. return -EINVAL;
  1168. }
  1169. if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
  1170. rs->ti->error = "Only one max_write_behind argument pair allowed";
  1171. return -EINVAL;
  1172. }
  1173. /*
  1174. * In device-mapper, we specify things in sectors, but
  1175. * MD records this value in kB
  1176. */
  1177. if (value < 0 || value / 2 > COUNTER_MAX) {
  1178. rs->ti->error = "Max write-behind limit out of range";
  1179. return -EINVAL;
  1180. }
  1181. rs->md.bitmap_info.max_write_behind = value / 2;
  1182. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
  1183. if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) {
  1184. rs->ti->error = "Only one daemon_sleep argument pair allowed";
  1185. return -EINVAL;
  1186. }
  1187. if (value < 0) {
  1188. rs->ti->error = "daemon sleep period out of range";
  1189. return -EINVAL;
  1190. }
  1191. rs->md.bitmap_info.daemon_sleep = value;
  1192. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) {
  1193. /* Userspace passes new data_offset after having extended the the data image LV */
  1194. if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
  1195. rs->ti->error = "Only one data_offset argument pair allowed";
  1196. return -EINVAL;
  1197. }
  1198. /* Ensure sensible data offset */
  1199. if (value < 0 ||
  1200. (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) {
  1201. rs->ti->error = "Bogus data_offset value";
  1202. return -EINVAL;
  1203. }
  1204. rs->data_offset = value;
  1205. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) {
  1206. /* Define the +/-# of disks to add to/remove from the given raid set */
  1207. if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
  1208. rs->ti->error = "Only one delta_disks argument pair allowed";
  1209. return -EINVAL;
  1210. }
  1211. /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
  1212. if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
  1213. rs->ti->error = "Too many delta_disk requested";
  1214. return -EINVAL;
  1215. }
  1216. rs->delta_disks = value;
  1217. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) {
  1218. if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) {
  1219. rs->ti->error = "Only one stripe_cache argument pair allowed";
  1220. return -EINVAL;
  1221. }
  1222. if (!rt_is_raid456(rt)) {
  1223. rs->ti->error = "Inappropriate argument: stripe_cache";
  1224. return -EINVAL;
  1225. }
  1226. if (value < 0) {
  1227. rs->ti->error = "Bogus stripe cache entries value";
  1228. return -EINVAL;
  1229. }
  1230. rs->stripe_cache_entries = value;
  1231. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
  1232. if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) {
  1233. rs->ti->error = "Only one min_recovery_rate argument pair allowed";
  1234. return -EINVAL;
  1235. }
  1236. if (value < 0) {
  1237. rs->ti->error = "min_recovery_rate out of range";
  1238. return -EINVAL;
  1239. }
  1240. rs->md.sync_speed_min = value;
  1241. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
  1242. if (test_and_set_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags)) {
  1243. rs->ti->error = "Only one max_recovery_rate argument pair allowed";
  1244. return -EINVAL;
  1245. }
  1246. if (value < 0) {
  1247. rs->ti->error = "max_recovery_rate out of range";
  1248. return -EINVAL;
  1249. }
  1250. rs->md.sync_speed_max = value;
  1251. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) {
  1252. if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) {
  1253. rs->ti->error = "Only one region_size argument pair allowed";
  1254. return -EINVAL;
  1255. }
  1256. region_size = value;
  1257. rs->requested_bitmap_chunk_sectors = value;
  1258. } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) {
  1259. if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) {
  1260. rs->ti->error = "Only one raid10_copies argument pair allowed";
  1261. return -EINVAL;
  1262. }
  1263. if (!__within_range(value, 2, rs->md.raid_disks)) {
  1264. rs->ti->error = "Bad value for 'raid10_copies'";
  1265. return -EINVAL;
  1266. }
  1267. raid10_copies = value;
  1268. } else {
  1269. DMERR("Unable to parse RAID parameter: %s", key);
  1270. rs->ti->error = "Unable to parse RAID parameter";
  1271. return -EINVAL;
  1272. }
  1273. }
  1274. if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) &&
  1275. test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
  1276. rs->ti->error = "sync and nosync are mutually exclusive";
  1277. return -EINVAL;
  1278. }
  1279. if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) &&
  1280. (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ||
  1281. test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))) {
  1282. rs->ti->error = "sync/nosync and rebuild are mutually exclusive";
  1283. return -EINVAL;
  1284. }
  1285. if (write_mostly >= rs->md.raid_disks) {
  1286. rs->ti->error = "Can't set all raid1 devices to write_mostly";
  1287. return -EINVAL;
  1288. }
  1289. if (rs->md.sync_speed_max &&
  1290. rs->md.sync_speed_min > rs->md.sync_speed_max) {
  1291. rs->ti->error = "Bogus recovery rates";
  1292. return -EINVAL;
  1293. }
  1294. if (validate_region_size(rs, region_size))
  1295. return -EINVAL;
  1296. if (rs->md.chunk_sectors)
  1297. max_io_len = rs->md.chunk_sectors;
  1298. else
  1299. max_io_len = region_size;
  1300. if (dm_set_target_max_io_len(rs->ti, max_io_len))
  1301. return -EINVAL;
  1302. if (rt_is_raid10(rt)) {
  1303. if (raid10_copies > rs->md.raid_disks) {
  1304. rs->ti->error = "Not enough devices to satisfy specification";
  1305. return -EINVAL;
  1306. }
  1307. rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
  1308. if (rs->md.new_layout < 0) {
  1309. rs->ti->error = "Error getting raid10 format";
  1310. return rs->md.new_layout;
  1311. }
  1312. rt = get_raid_type_by_ll(10, rs->md.new_layout);
  1313. if (!rt) {
  1314. rs->ti->error = "Failed to recognize new raid10 layout";
  1315. return -EINVAL;
  1316. }
  1317. if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
  1318. rt->algorithm == ALGORITHM_RAID10_NEAR) &&
  1319. test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
  1320. rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
  1321. return -EINVAL;
  1322. }
  1323. }
  1324. rs->raid10_copies = raid10_copies;
  1325. /* Assume there are no metadata devices until the drives are parsed */
  1326. rs->md.persistent = 0;
  1327. rs->md.external = 1;
  1328. /* Check, if any invalid ctr arguments have been passed in for the raid level */
  1329. return rs_check_for_valid_flags(rs);
  1330. }
  1331. /* Set raid4/5/6 cache size */
  1332. static int rs_set_raid456_stripe_cache(struct raid_set *rs)
  1333. {
  1334. int r;
  1335. struct r5conf *conf;
  1336. struct mddev *mddev = &rs->md;
  1337. uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2;
  1338. uint32_t nr_stripes = rs->stripe_cache_entries;
  1339. if (!rt_is_raid456(rs->raid_type)) {
  1340. rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size";
  1341. return -EINVAL;
  1342. }
  1343. if (nr_stripes < min_stripes) {
  1344. DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size",
  1345. nr_stripes, min_stripes);
  1346. nr_stripes = min_stripes;
  1347. }
  1348. conf = mddev->private;
  1349. if (!conf) {
  1350. rs->ti->error = "Cannot change stripe_cache size on inactive RAID set";
  1351. return -EINVAL;
  1352. }
  1353. /* Try setting number of stripes in raid456 stripe cache */
  1354. if (conf->min_nr_stripes != nr_stripes) {
  1355. r = raid5_set_cache_size(mddev, nr_stripes);
  1356. if (r) {
  1357. rs->ti->error = "Failed to set raid4/5/6 stripe cache size";
  1358. return r;
  1359. }
  1360. DMINFO("%u stripe cache entries", nr_stripes);
  1361. }
  1362. return 0;
  1363. }
  1364. /* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
  1365. static unsigned int mddev_data_stripes(struct raid_set *rs)
  1366. {
  1367. return rs->md.raid_disks - rs->raid_type->parity_devs;
  1368. }
  1369. /* Return # of data stripes of @rs (i.e. as of ctr) */
  1370. static unsigned int rs_data_stripes(struct raid_set *rs)
  1371. {
  1372. return rs->raid_disks - rs->raid_type->parity_devs;
  1373. }
  1374. /*
  1375. * Retrieve rdev->sectors from any valid raid device of @rs
  1376. * to allow userpace to pass in arbitray "- -" device tupples.
  1377. */
  1378. static sector_t __rdev_sectors(struct raid_set *rs)
  1379. {
  1380. int i;
  1381. for (i = 0; i < rs->md.raid_disks; i++) {
  1382. struct md_rdev *rdev = &rs->dev[i].rdev;
  1383. if (!test_bit(Journal, &rdev->flags) &&
  1384. rdev->bdev && rdev->sectors)
  1385. return rdev->sectors;
  1386. }
  1387. return 0;
  1388. }
  1389. /* Check that calculated dev_sectors fits all component devices. */
  1390. static int _check_data_dev_sectors(struct raid_set *rs)
  1391. {
  1392. sector_t ds = ~0;
  1393. struct md_rdev *rdev;
  1394. rdev_for_each(rdev, &rs->md)
  1395. if (!test_bit(Journal, &rdev->flags) && rdev->bdev) {
  1396. ds = min(ds, to_sector(i_size_read(rdev->bdev->bd_inode)));
  1397. if (ds < rs->md.dev_sectors) {
  1398. rs->ti->error = "Component device(s) too small";
  1399. return -EINVAL;
  1400. }
  1401. }
  1402. return 0;
  1403. }
  1404. /* Calculate the sectors per device and per array used for @rs */
  1405. static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev)
  1406. {
  1407. int delta_disks;
  1408. unsigned int data_stripes;
  1409. struct mddev *mddev = &rs->md;
  1410. struct md_rdev *rdev;
  1411. sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len;
  1412. if (use_mddev) {
  1413. delta_disks = mddev->delta_disks;
  1414. data_stripes = mddev_data_stripes(rs);
  1415. } else {
  1416. delta_disks = rs->delta_disks;
  1417. data_stripes = rs_data_stripes(rs);
  1418. }
  1419. /* Special raid1 case w/o delta_disks support (yet) */
  1420. if (rt_is_raid1(rs->raid_type))
  1421. ;
  1422. else if (rt_is_raid10(rs->raid_type)) {
  1423. if (rs->raid10_copies < 2 ||
  1424. delta_disks < 0) {
  1425. rs->ti->error = "Bogus raid10 data copies or delta disks";
  1426. return -EINVAL;
  1427. }
  1428. dev_sectors *= rs->raid10_copies;
  1429. if (sector_div(dev_sectors, data_stripes))
  1430. goto bad;
  1431. array_sectors = (data_stripes + delta_disks) * dev_sectors;
  1432. if (sector_div(array_sectors, rs->raid10_copies))
  1433. goto bad;
  1434. } else if (sector_div(dev_sectors, data_stripes))
  1435. goto bad;
  1436. else
  1437. /* Striped layouts */
  1438. array_sectors = (data_stripes + delta_disks) * dev_sectors;
  1439. rdev_for_each(rdev, mddev)
  1440. if (!test_bit(Journal, &rdev->flags))
  1441. rdev->sectors = dev_sectors;
  1442. mddev->array_sectors = array_sectors;
  1443. mddev->dev_sectors = dev_sectors;
  1444. return _check_data_dev_sectors(rs);
  1445. bad:
  1446. rs->ti->error = "Target length not divisible by number of data devices";
  1447. return -EINVAL;
  1448. }
  1449. /* Setup recovery on @rs */
  1450. static void __rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
  1451. {
  1452. /* raid0 does not recover */
  1453. if (rs_is_raid0(rs))
  1454. rs->md.recovery_cp = MaxSector;
  1455. /*
  1456. * A raid6 set has to be recovered either
  1457. * completely or for the grown part to
  1458. * ensure proper parity and Q-Syndrome
  1459. */
  1460. else if (rs_is_raid6(rs))
  1461. rs->md.recovery_cp = dev_sectors;
  1462. /*
  1463. * Other raid set types may skip recovery
  1464. * depending on the 'nosync' flag.
  1465. */
  1466. else
  1467. rs->md.recovery_cp = test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)
  1468. ? MaxSector : dev_sectors;
  1469. }
  1470. /* Setup recovery on @rs based on raid type, device size and 'nosync' flag */
  1471. static void rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
  1472. {
  1473. if (!dev_sectors)
  1474. /* New raid set or 'sync' flag provided */
  1475. __rs_setup_recovery(rs, 0);
  1476. else if (dev_sectors == MaxSector)
  1477. /* Prevent recovery */
  1478. __rs_setup_recovery(rs, MaxSector);
  1479. else if (__rdev_sectors(rs) < dev_sectors)
  1480. /* Grown raid set */
  1481. __rs_setup_recovery(rs, __rdev_sectors(rs));
  1482. else
  1483. __rs_setup_recovery(rs, MaxSector);
  1484. }
  1485. static void do_table_event(struct work_struct *ws)
  1486. {
  1487. struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
  1488. smp_rmb(); /* Make sure we access most actual mddev properties */
  1489. if (!rs_is_reshaping(rs)) {
  1490. if (rs_is_raid10(rs))
  1491. rs_set_rdev_sectors(rs);
  1492. rs_set_capacity(rs);
  1493. }
  1494. dm_table_event(rs->ti->table);
  1495. }
  1496. static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
  1497. {
  1498. struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
  1499. return mddev_congested(&rs->md, bits);
  1500. }
  1501. /*
  1502. * Make sure a valid takover (level switch) is being requested on @rs
  1503. *
  1504. * Conversions of raid sets from one MD personality to another
  1505. * have to conform to restrictions which are enforced here.
  1506. */
  1507. static int rs_check_takeover(struct raid_set *rs)
  1508. {
  1509. struct mddev *mddev = &rs->md;
  1510. unsigned int near_copies;
  1511. if (rs->md.degraded) {
  1512. rs->ti->error = "Can't takeover degraded raid set";
  1513. return -EPERM;
  1514. }
  1515. if (rs_is_reshaping(rs)) {
  1516. rs->ti->error = "Can't takeover reshaping raid set";
  1517. return -EPERM;
  1518. }
  1519. switch (mddev->level) {
  1520. case 0:
  1521. /* raid0 -> raid1/5 with one disk */
  1522. if ((mddev->new_level == 1 || mddev->new_level == 5) &&
  1523. mddev->raid_disks == 1)
  1524. return 0;
  1525. /* raid0 -> raid10 */
  1526. if (mddev->new_level == 10 &&
  1527. !(rs->raid_disks % mddev->raid_disks))
  1528. return 0;
  1529. /* raid0 with multiple disks -> raid4/5/6 */
  1530. if (__within_range(mddev->new_level, 4, 6) &&
  1531. mddev->new_layout == ALGORITHM_PARITY_N &&
  1532. mddev->raid_disks > 1)
  1533. return 0;
  1534. break;
  1535. case 10:
  1536. /* Can't takeover raid10_offset! */
  1537. if (__is_raid10_offset(mddev->layout))
  1538. break;
  1539. near_copies = __raid10_near_copies(mddev->layout);
  1540. /* raid10* -> raid0 */
  1541. if (mddev->new_level == 0) {
  1542. /* Can takeover raid10_near with raid disks divisable by data copies! */
  1543. if (near_copies > 1 &&
  1544. !(mddev->raid_disks % near_copies)) {
  1545. mddev->raid_disks /= near_copies;
  1546. mddev->delta_disks = mddev->raid_disks;
  1547. return 0;
  1548. }
  1549. /* Can takeover raid10_far */
  1550. if (near_copies == 1 &&
  1551. __raid10_far_copies(mddev->layout) > 1)
  1552. return 0;
  1553. break;
  1554. }
  1555. /* raid10_{near,far} -> raid1 */
  1556. if (mddev->new_level == 1 &&
  1557. max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
  1558. return 0;
  1559. /* raid10_{near,far} with 2 disks -> raid4/5 */
  1560. if (__within_range(mddev->new_level, 4, 5) &&
  1561. mddev->raid_disks == 2)
  1562. return 0;
  1563. break;
  1564. case 1:
  1565. /* raid1 with 2 disks -> raid4/5 */
  1566. if (__within_range(mddev->new_level, 4, 5) &&
  1567. mddev->raid_disks == 2) {
  1568. mddev->degraded = 1;
  1569. return 0;
  1570. }
  1571. /* raid1 -> raid0 */
  1572. if (mddev->new_level == 0 &&
  1573. mddev->raid_disks == 1)
  1574. return 0;
  1575. /* raid1 -> raid10 */
  1576. if (mddev->new_level == 10)
  1577. return 0;
  1578. break;
  1579. case 4:
  1580. /* raid4 -> raid0 */
  1581. if (mddev->new_level == 0)
  1582. return 0;
  1583. /* raid4 -> raid1/5 with 2 disks */
  1584. if ((mddev->new_level == 1 || mddev->new_level == 5) &&
  1585. mddev->raid_disks == 2)
  1586. return 0;
  1587. /* raid4 -> raid5/6 with parity N */
  1588. if (__within_range(mddev->new_level, 5, 6) &&
  1589. mddev->layout == ALGORITHM_PARITY_N)
  1590. return 0;
  1591. break;
  1592. case 5:
  1593. /* raid5 with parity N -> raid0 */
  1594. if (mddev->new_level == 0 &&
  1595. mddev->layout == ALGORITHM_PARITY_N)
  1596. return 0;
  1597. /* raid5 with parity N -> raid4 */
  1598. if (mddev->new_level == 4 &&
  1599. mddev->layout == ALGORITHM_PARITY_N)
  1600. return 0;
  1601. /* raid5 with 2 disks -> raid1/4/10 */
  1602. if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
  1603. mddev->raid_disks == 2)
  1604. return 0;
  1605. /* raid5_* -> raid6_*_6 with Q-Syndrome N (e.g. raid5_ra -> raid6_ra_6 */
  1606. if (mddev->new_level == 6 &&
  1607. ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
  1608. __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6)))
  1609. return 0;
  1610. break;
  1611. case 6:
  1612. /* raid6 with parity N -> raid0 */
  1613. if (mddev->new_level == 0 &&
  1614. mddev->layout == ALGORITHM_PARITY_N)
  1615. return 0;
  1616. /* raid6 with parity N -> raid4 */
  1617. if (mddev->new_level == 4 &&
  1618. mddev->layout == ALGORITHM_PARITY_N)
  1619. return 0;
  1620. /* raid6_*_n with Q-Syndrome N -> raid5_* */
  1621. if (mddev->new_level == 5 &&
  1622. ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
  1623. __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
  1624. return 0;
  1625. default:
  1626. break;
  1627. }
  1628. rs->ti->error = "takeover not possible";
  1629. return -EINVAL;
  1630. }
  1631. /* True if @rs requested to be taken over */
  1632. static bool rs_takeover_requested(struct raid_set *rs)
  1633. {
  1634. return rs->md.new_level != rs->md.level;
  1635. }
  1636. /* True if @rs is requested to reshape by ctr */
  1637. static bool rs_reshape_requested(struct raid_set *rs)
  1638. {
  1639. bool change;
  1640. struct mddev *mddev = &rs->md;
  1641. if (rs_takeover_requested(rs))
  1642. return false;
  1643. if (rs_is_raid0(rs))
  1644. return false;
  1645. change = mddev->new_layout != mddev->layout ||
  1646. mddev->new_chunk_sectors != mddev->chunk_sectors ||
  1647. rs->delta_disks;
  1648. /* Historical case to support raid1 reshape without delta disks */
  1649. if (rs_is_raid1(rs)) {
  1650. if (rs->delta_disks)
  1651. return !!rs->delta_disks;
  1652. return !change &&
  1653. mddev->raid_disks != rs->raid_disks;
  1654. }
  1655. if (rs_is_raid10(rs))
  1656. return change &&
  1657. !__is_raid10_far(mddev->new_layout) &&
  1658. rs->delta_disks >= 0;
  1659. return change;
  1660. }
  1661. /* Features */
  1662. #define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */
  1663. /* State flags for sb->flags */
  1664. #define SB_FLAG_RESHAPE_ACTIVE 0x1
  1665. #define SB_FLAG_RESHAPE_BACKWARDS 0x2
  1666. /*
  1667. * This structure is never routinely used by userspace, unlike md superblocks.
  1668. * Devices with this superblock should only ever be accessed via device-mapper.
  1669. */
  1670. #define DM_RAID_MAGIC 0x64526D44
  1671. struct dm_raid_superblock {
  1672. __le32 magic; /* "DmRd" */
  1673. __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
  1674. __le32 num_devices; /* Number of devices in this raid set. (Max 64) */
  1675. __le32 array_position; /* The position of this drive in the raid set */
  1676. __le64 events; /* Incremented by md when superblock updated */
  1677. __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */
  1678. /* indicate failures (see extension below) */
  1679. /*
  1680. * This offset tracks the progress of the repair or replacement of
  1681. * an individual drive.
  1682. */
  1683. __le64 disk_recovery_offset;
  1684. /*
  1685. * This offset tracks the progress of the initial raid set
  1686. * synchronisation/parity calculation.
  1687. */
  1688. __le64 array_resync_offset;
  1689. /*
  1690. * raid characteristics
  1691. */
  1692. __le32 level;
  1693. __le32 layout;
  1694. __le32 stripe_sectors;
  1695. /********************************************************************
  1696. * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
  1697. *
  1698. * FEATURE_FLAG_SUPPORTS_V190 in the compat_features member indicates that those exist
  1699. */
  1700. __le32 flags; /* Flags defining array states for reshaping */
  1701. /*
  1702. * This offset tracks the progress of a raid
  1703. * set reshape in order to be able to restart it
  1704. */
  1705. __le64 reshape_position;
  1706. /*
  1707. * These define the properties of the array in case of an interrupted reshape
  1708. */
  1709. __le32 new_level;
  1710. __le32 new_layout;
  1711. __le32 new_stripe_sectors;
  1712. __le32 delta_disks;
  1713. __le64 array_sectors; /* Array size in sectors */
  1714. /*
  1715. * Sector offsets to data on devices (reshaping).
  1716. * Needed to support out of place reshaping, thus
  1717. * not writing over any stripes whilst converting
  1718. * them from old to new layout
  1719. */
  1720. __le64 data_offset;
  1721. __le64 new_data_offset;
  1722. __le64 sectors; /* Used device size in sectors */
  1723. /*
  1724. * Additonal Bit field of devices indicating failures to support
  1725. * up to 256 devices with the 1.9.0 on-disk metadata format
  1726. */
  1727. __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
  1728. __le32 incompat_features; /* Used to indicate any incompatible features */
  1729. /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
  1730. } __packed;
  1731. /*
  1732. * Check for reshape constraints on raid set @rs:
  1733. *
  1734. * - reshape function non-existent
  1735. * - degraded set
  1736. * - ongoing recovery
  1737. * - ongoing reshape
  1738. *
  1739. * Returns 0 if none or -EPERM if given constraint
  1740. * and error message reference in @errmsg
  1741. */
  1742. static int rs_check_reshape(struct raid_set *rs)
  1743. {
  1744. struct mddev *mddev = &rs->md;
  1745. if (!mddev->pers || !mddev->pers->check_reshape)
  1746. rs->ti->error = "Reshape not supported";
  1747. else if (mddev->degraded)
  1748. rs->ti->error = "Can't reshape degraded raid set";
  1749. else if (rs_is_recovering(rs))
  1750. rs->ti->error = "Convert request on recovering raid set prohibited";
  1751. else if (rs_is_reshaping(rs))
  1752. rs->ti->error = "raid set already reshaping!";
  1753. else if (!(rs_is_raid1(rs) || rs_is_raid10(rs) || rs_is_raid456(rs)))
  1754. rs->ti->error = "Reshaping only supported for raid1/4/5/6/10";
  1755. else
  1756. return 0;
  1757. return -EPERM;
  1758. }
  1759. static int read_disk_sb(struct md_rdev *rdev, int size, bool force_reload)
  1760. {
  1761. BUG_ON(!rdev->sb_page);
  1762. if (rdev->sb_loaded && !force_reload)
  1763. return 0;
  1764. rdev->sb_loaded = 0;
  1765. if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) {
  1766. DMERR("Failed to read superblock of device at position %d",
  1767. rdev->raid_disk);
  1768. md_error(rdev->mddev, rdev);
  1769. set_bit(Faulty, &rdev->flags);
  1770. return -EIO;
  1771. }
  1772. rdev->sb_loaded = 1;
  1773. return 0;
  1774. }
  1775. static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
  1776. {
  1777. failed_devices[0] = le64_to_cpu(sb->failed_devices);
  1778. memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
  1779. if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
  1780. int i = ARRAY_SIZE(sb->extended_failed_devices);
  1781. while (i--)
  1782. failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
  1783. }
  1784. }
  1785. static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
  1786. {
  1787. int i = ARRAY_SIZE(sb->extended_failed_devices);
  1788. sb->failed_devices = cpu_to_le64(failed_devices[0]);
  1789. while (i--)
  1790. sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
  1791. }
  1792. /*
  1793. * Synchronize the superblock members with the raid set properties
  1794. *
  1795. * All superblock data is little endian.
  1796. */
  1797. static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
  1798. {
  1799. bool update_failed_devices = false;
  1800. unsigned int i;
  1801. uint64_t failed_devices[DISKS_ARRAY_ELEMS];
  1802. struct dm_raid_superblock *sb;
  1803. struct raid_set *rs = container_of(mddev, struct raid_set, md);
  1804. /* No metadata device, no superblock */
  1805. if (!rdev->meta_bdev)
  1806. return;
  1807. BUG_ON(!rdev->sb_page);
  1808. sb = page_address(rdev->sb_page);
  1809. sb_retrieve_failed_devices(sb, failed_devices);
  1810. for (i = 0; i < rs->raid_disks; i++)
  1811. if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
  1812. update_failed_devices = true;
  1813. set_bit(i, (void *) failed_devices);
  1814. }
  1815. if (update_failed_devices)
  1816. sb_update_failed_devices(sb, failed_devices);
  1817. sb->magic = cpu_to_le32(DM_RAID_MAGIC);
  1818. sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
  1819. sb->num_devices = cpu_to_le32(mddev->raid_disks);
  1820. sb->array_position = cpu_to_le32(rdev->raid_disk);
  1821. sb->events = cpu_to_le64(mddev->events);
  1822. sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
  1823. sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
  1824. sb->level = cpu_to_le32(mddev->level);
  1825. sb->layout = cpu_to_le32(mddev->layout);
  1826. sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
  1827. /********************************************************************
  1828. * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
  1829. *
  1830. * FEATURE_FLAG_SUPPORTS_V190 in the compat_features member indicates that those exist
  1831. */
  1832. sb->new_level = cpu_to_le32(mddev->new_level);
  1833. sb->new_layout = cpu_to_le32(mddev->new_layout);
  1834. sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
  1835. sb->delta_disks = cpu_to_le32(mddev->delta_disks);
  1836. smp_rmb(); /* Make sure we access most recent reshape position */
  1837. sb->reshape_position = cpu_to_le64(mddev->reshape_position);
  1838. if (le64_to_cpu(sb->reshape_position) != MaxSector) {
  1839. /* Flag ongoing reshape */
  1840. sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
  1841. if (mddev->delta_disks < 0 || mddev->reshape_backwards)
  1842. sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
  1843. } else {
  1844. /* Clear reshape flags */
  1845. sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
  1846. }
  1847. sb->array_sectors = cpu_to_le64(mddev->array_sectors);
  1848. sb->data_offset = cpu_to_le64(rdev->data_offset);
  1849. sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
  1850. sb->sectors = cpu_to_le64(rdev->sectors);
  1851. sb->incompat_features = cpu_to_le32(0);
  1852. /* Zero out the rest of the payload after the size of the superblock */
  1853. memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
  1854. }
  1855. /*
  1856. * super_load
  1857. *
  1858. * This function creates a superblock if one is not found on the device
  1859. * and will decide which superblock to use if there's a choice.
  1860. *
  1861. * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
  1862. */
  1863. static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
  1864. {
  1865. int r;
  1866. struct dm_raid_superblock *sb;
  1867. struct dm_raid_superblock *refsb;
  1868. uint64_t events_sb, events_refsb;
  1869. r = read_disk_sb(rdev, rdev->sb_size, false);
  1870. if (r)
  1871. return r;
  1872. sb = page_address(rdev->sb_page);
  1873. /*
  1874. * Two cases that we want to write new superblocks and rebuild:
  1875. * 1) New device (no matching magic number)
  1876. * 2) Device specified for rebuild (!In_sync w/ offset == 0)
  1877. */
  1878. if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
  1879. (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
  1880. super_sync(rdev->mddev, rdev);
  1881. set_bit(FirstUse, &rdev->flags);
  1882. sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190);
  1883. /* Force writing of superblocks to disk */
  1884. set_bit(MD_SB_CHANGE_DEVS, &rdev->mddev->sb_flags);
  1885. /* Any superblock is better than none, choose that if given */
  1886. return refdev ? 0 : 1;
  1887. }
  1888. if (!refdev)
  1889. return 1;
  1890. events_sb = le64_to_cpu(sb->events);
  1891. refsb = page_address(refdev->sb_page);
  1892. events_refsb = le64_to_cpu(refsb->events);
  1893. return (events_sb > events_refsb) ? 1 : 0;
  1894. }
  1895. static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
  1896. {
  1897. int role;
  1898. unsigned int d;
  1899. struct mddev *mddev = &rs->md;
  1900. uint64_t events_sb;
  1901. uint64_t failed_devices[DISKS_ARRAY_ELEMS];
  1902. struct dm_raid_superblock *sb;
  1903. uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
  1904. struct md_rdev *r;
  1905. struct dm_raid_superblock *sb2;
  1906. sb = page_address(rdev->sb_page);
  1907. events_sb = le64_to_cpu(sb->events);
  1908. /*
  1909. * Initialise to 1 if this is a new superblock.
  1910. */
  1911. mddev->events = events_sb ? : 1;
  1912. mddev->reshape_position = MaxSector;
  1913. mddev->raid_disks = le32_to_cpu(sb->num_devices);
  1914. mddev->level = le32_to_cpu(sb->level);
  1915. mddev->layout = le32_to_cpu(sb->layout);
  1916. mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
  1917. /*
  1918. * Reshaping is supported, e.g. reshape_position is valid
  1919. * in superblock and superblock content is authoritative.
  1920. */
  1921. if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
  1922. /* Superblock is authoritative wrt given raid set layout! */
  1923. mddev->new_level = le32_to_cpu(sb->new_level);
  1924. mddev->new_layout = le32_to_cpu(sb->new_layout);
  1925. mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
  1926. mddev->delta_disks = le32_to_cpu(sb->delta_disks);
  1927. mddev->array_sectors = le64_to_cpu(sb->array_sectors);
  1928. /* raid was reshaping and got interrupted */
  1929. if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
  1930. if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
  1931. DMERR("Reshape requested but raid set is still reshaping");
  1932. return -EINVAL;
  1933. }
  1934. if (mddev->delta_disks < 0 ||
  1935. (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
  1936. mddev->reshape_backwards = 1;
  1937. else
  1938. mddev->reshape_backwards = 0;
  1939. mddev->reshape_position = le64_to_cpu(sb->reshape_position);
  1940. rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
  1941. }
  1942. } else {
  1943. /*
  1944. * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
  1945. */
  1946. struct raid_type *rt_cur = get_raid_type_by_ll(mddev->level, mddev->layout);
  1947. struct raid_type *rt_new = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
  1948. if (rs_takeover_requested(rs)) {
  1949. if (rt_cur && rt_new)
  1950. DMERR("Takeover raid sets from %s to %s not yet supported by metadata. (raid level change)",
  1951. rt_cur->name, rt_new->name);
  1952. else
  1953. DMERR("Takeover raid sets not yet supported by metadata. (raid level change)");
  1954. return -EINVAL;
  1955. } else if (rs_reshape_requested(rs)) {
  1956. DMERR("Reshaping raid sets not yet supported by metadata. (raid layout change keeping level)");
  1957. if (mddev->layout != mddev->new_layout) {
  1958. if (rt_cur && rt_new)
  1959. DMERR(" current layout %s vs new layout %s",
  1960. rt_cur->name, rt_new->name);
  1961. else
  1962. DMERR(" current layout 0x%X vs new layout 0x%X",
  1963. le32_to_cpu(sb->layout), mddev->new_layout);
  1964. }
  1965. if (mddev->chunk_sectors != mddev->new_chunk_sectors)
  1966. DMERR(" current stripe sectors %u vs new stripe sectors %u",
  1967. mddev->chunk_sectors, mddev->new_chunk_sectors);
  1968. if (rs->delta_disks)
  1969. DMERR(" current %u disks vs new %u disks",
  1970. mddev->raid_disks, mddev->raid_disks + rs->delta_disks);
  1971. if (rs_is_raid10(rs)) {
  1972. DMERR(" Old layout: %s w/ %u copies",
  1973. raid10_md_layout_to_format(mddev->layout),
  1974. raid10_md_layout_to_copies(mddev->layout));
  1975. DMERR(" New layout: %s w/ %u copies",
  1976. raid10_md_layout_to_format(mddev->new_layout),
  1977. raid10_md_layout_to_copies(mddev->new_layout));
  1978. }
  1979. return -EINVAL;
  1980. }
  1981. DMINFO("Discovered old metadata format; upgrading to extended metadata format");
  1982. }
  1983. if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
  1984. mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
  1985. /*
  1986. * During load, we set FirstUse if a new superblock was written.
  1987. * There are two reasons we might not have a superblock:
  1988. * 1) The raid set is brand new - in which case, all of the
  1989. * devices must have their In_sync bit set. Also,
  1990. * recovery_cp must be 0, unless forced.
  1991. * 2) This is a new device being added to an old raid set
  1992. * and the new device needs to be rebuilt - in which
  1993. * case the In_sync bit will /not/ be set and
  1994. * recovery_cp must be MaxSector.
  1995. * 3) This is/are a new device(s) being added to an old
  1996. * raid set during takeover to a higher raid level
  1997. * to provide capacity for redundancy or during reshape
  1998. * to add capacity to grow the raid set.
  1999. */
  2000. d = 0;
  2001. rdev_for_each(r, mddev) {
  2002. if (test_bit(Journal, &rdev->flags))
  2003. continue;
  2004. if (test_bit(FirstUse, &r->flags))
  2005. new_devs++;
  2006. if (!test_bit(In_sync, &r->flags)) {
  2007. DMINFO("Device %d specified for rebuild; clearing superblock",
  2008. r->raid_disk);
  2009. rebuilds++;
  2010. if (test_bit(FirstUse, &r->flags))
  2011. rebuild_and_new++;
  2012. }
  2013. d++;
  2014. }
  2015. if (new_devs == rs->raid_disks || !rebuilds) {
  2016. /* Replace a broken device */
  2017. if (new_devs == 1 && !rs->delta_disks)
  2018. ;
  2019. if (new_devs == rs->raid_disks) {
  2020. DMINFO("Superblocks created for new raid set");
  2021. set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
  2022. } else if (new_devs != rebuilds &&
  2023. new_devs != rs->delta_disks) {
  2024. DMERR("New device injected into existing raid set without "
  2025. "'delta_disks' or 'rebuild' parameter specified");
  2026. return -EINVAL;
  2027. }
  2028. } else if (new_devs && new_devs != rebuilds) {
  2029. DMERR("%u 'rebuild' devices cannot be injected into"
  2030. " a raid set with %u other first-time devices",
  2031. rebuilds, new_devs);
  2032. return -EINVAL;
  2033. } else if (rebuilds) {
  2034. if (rebuild_and_new && rebuilds != rebuild_and_new) {
  2035. DMERR("new device%s provided without 'rebuild'",
  2036. new_devs > 1 ? "s" : "");
  2037. return -EINVAL;
  2038. } else if (!test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) && rs_is_recovering(rs)) {
  2039. DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
  2040. (unsigned long long) mddev->recovery_cp);
  2041. return -EINVAL;
  2042. } else if (rs_is_reshaping(rs)) {
  2043. DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)",
  2044. (unsigned long long) mddev->reshape_position);
  2045. return -EINVAL;
  2046. }
  2047. }
  2048. /*
  2049. * Now we set the Faulty bit for those devices that are
  2050. * recorded in the superblock as failed.
  2051. */
  2052. sb_retrieve_failed_devices(sb, failed_devices);
  2053. rdev_for_each(r, mddev) {
  2054. if (test_bit(Journal, &rdev->flags) ||
  2055. !r->sb_page)
  2056. continue;
  2057. sb2 = page_address(r->sb_page);
  2058. sb2->failed_devices = 0;
  2059. memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
  2060. /*
  2061. * Check for any device re-ordering.
  2062. */
  2063. if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
  2064. role = le32_to_cpu(sb2->array_position);
  2065. if (role < 0)
  2066. continue;
  2067. if (role != r->raid_disk) {
  2068. if (rs_is_raid10(rs) && __is_raid10_near(mddev->layout)) {
  2069. if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
  2070. rs->raid_disks % rs->raid10_copies) {
  2071. rs->ti->error =
  2072. "Cannot change raid10 near set to odd # of devices!";
  2073. return -EINVAL;
  2074. }
  2075. sb2->array_position = cpu_to_le32(r->raid_disk);
  2076. } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
  2077. !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
  2078. !rt_is_raid1(rs->raid_type)) {
  2079. rs->ti->error = "Cannot change device positions in raid set";
  2080. return -EINVAL;
  2081. }
  2082. DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
  2083. }
  2084. /*
  2085. * Partial recovery is performed on
  2086. * returning failed devices.
  2087. */
  2088. if (test_bit(role, (void *) failed_devices))
  2089. set_bit(Faulty, &r->flags);
  2090. }
  2091. }
  2092. return 0;
  2093. }
  2094. static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
  2095. {
  2096. struct mddev *mddev = &rs->md;
  2097. struct dm_raid_superblock *sb;
  2098. if (rs_is_raid0(rs) || !rdev->sb_page || rdev->raid_disk < 0)
  2099. return 0;
  2100. sb = page_address(rdev->sb_page);
  2101. /*
  2102. * If mddev->events is not set, we know we have not yet initialized
  2103. * the array.
  2104. */
  2105. if (!mddev->events && super_init_validation(rs, rdev))
  2106. return -EINVAL;
  2107. if (le32_to_cpu(sb->compat_features) &&
  2108. le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
  2109. rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
  2110. return -EINVAL;
  2111. }
  2112. if (sb->incompat_features) {
  2113. rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
  2114. return -EINVAL;
  2115. }
  2116. /* Enable bitmap creation for RAID levels != 0 */
  2117. mddev->bitmap_info.offset = (rt_is_raid0(rs->raid_type) || rs->journal_dev.dev) ? 0 : to_sector(4096);
  2118. mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
  2119. if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
  2120. /*
  2121. * Retrieve rdev size stored in superblock to be prepared for shrink.
  2122. * Check extended superblock members are present otherwise the size
  2123. * will not be set!
  2124. */
  2125. if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190)
  2126. rdev->sectors = le64_to_cpu(sb->sectors);
  2127. rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
  2128. if (rdev->recovery_offset == MaxSector)
  2129. set_bit(In_sync, &rdev->flags);
  2130. /*
  2131. * If no reshape in progress -> we're recovering single
  2132. * disk(s) and have to set the device(s) to out-of-sync
  2133. */
  2134. else if (!rs_is_reshaping(rs))
  2135. clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
  2136. }
  2137. /*
  2138. * If a device comes back, set it as not In_sync and no longer faulty.
  2139. */
  2140. if (test_and_clear_bit(Faulty, &rdev->flags)) {
  2141. rdev->recovery_offset = 0;
  2142. clear_bit(In_sync, &rdev->flags);
  2143. rdev->saved_raid_disk = rdev->raid_disk;
  2144. }
  2145. /* Reshape support -> restore repective data offsets */
  2146. rdev->data_offset = le64_to_cpu(sb->data_offset);
  2147. rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
  2148. return 0;
  2149. }
  2150. /*
  2151. * Analyse superblocks and select the freshest.
  2152. */
  2153. static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
  2154. {
  2155. int r;
  2156. struct md_rdev *rdev, *freshest;
  2157. struct mddev *mddev = &rs->md;
  2158. freshest = NULL;
  2159. rdev_for_each(rdev, mddev) {
  2160. if (test_bit(Journal, &rdev->flags))
  2161. continue;
  2162. if (!rdev->meta_bdev)
  2163. continue;
  2164. /* Set superblock offset/size for metadata device. */
  2165. rdev->sb_start = 0;
  2166. rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
  2167. if (rdev->sb_size < sizeof(struct dm_raid_superblock) || rdev->sb_size > PAGE_SIZE) {
  2168. DMERR("superblock size of a logical block is no longer valid");
  2169. return -EINVAL;
  2170. }
  2171. /*
  2172. * Skipping super_load due to CTR_FLAG_SYNC will cause
  2173. * the array to undergo initialization again as
  2174. * though it were new. This is the intended effect
  2175. * of the "sync" directive.
  2176. *
  2177. * With reshaping capability added, we must ensure that
  2178. * that the "sync" directive is disallowed during the reshape.
  2179. */
  2180. if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
  2181. continue;
  2182. r = super_load(rdev, freshest);
  2183. switch (r) {
  2184. case 1:
  2185. freshest = rdev;
  2186. break;
  2187. case 0:
  2188. break;
  2189. default:
  2190. /* This is a failure to read the superblock from the metadata device. */
  2191. /*
  2192. * We have to keep any raid0 data/metadata device pairs or
  2193. * the MD raid0 personality will fail to start the array.
  2194. */
  2195. if (rs_is_raid0(rs))
  2196. continue;
  2197. /*
  2198. * We keep the dm_devs to be able to emit the device tuple
  2199. * properly on the table line in raid_status() (rather than
  2200. * mistakenly acting as if '- -' got passed into the constructor).
  2201. *
  2202. * The rdev has to stay on the same_set list to allow for
  2203. * the attempt to restore faulty devices on second resume.
  2204. */
  2205. rdev->raid_disk = rdev->saved_raid_disk = -1;
  2206. break;
  2207. }
  2208. }
  2209. if (!freshest)
  2210. return 0;
  2211. /*
  2212. * Validation of the freshest device provides the source of
  2213. * validation for the remaining devices.
  2214. */
  2215. rs->ti->error = "Unable to assemble array: Invalid superblocks";
  2216. if (super_validate(rs, freshest))
  2217. return -EINVAL;
  2218. if (validate_raid_redundancy(rs)) {
  2219. rs->ti->error = "Insufficient redundancy to activate array";
  2220. return -EINVAL;
  2221. }
  2222. rdev_for_each(rdev, mddev)
  2223. if (!test_bit(Journal, &rdev->flags) &&
  2224. rdev != freshest &&
  2225. super_validate(rs, rdev))
  2226. return -EINVAL;
  2227. return 0;
  2228. }
  2229. /*
  2230. * Adjust data_offset and new_data_offset on all disk members of @rs
  2231. * for out of place reshaping if requested by contructor
  2232. *
  2233. * We need free space at the beginning of each raid disk for forward
  2234. * and at the end for backward reshapes which userspace has to provide
  2235. * via remapping/reordering of space.
  2236. */
  2237. static int rs_adjust_data_offsets(struct raid_set *rs)
  2238. {
  2239. sector_t data_offset = 0, new_data_offset = 0;
  2240. struct md_rdev *rdev;
  2241. /* Constructor did not request data offset change */
  2242. if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
  2243. if (!rs_is_reshapable(rs))
  2244. goto out;
  2245. return 0;
  2246. }
  2247. /* HM FIXME: get In_Sync raid_dev? */
  2248. rdev = &rs->dev[0].rdev;
  2249. if (rs->delta_disks < 0) {
  2250. /*
  2251. * Removing disks (reshaping backwards):
  2252. *
  2253. * - before reshape: data is at offset 0 and free space
  2254. * is at end of each component LV
  2255. *
  2256. * - after reshape: data is at offset rs->data_offset != 0 on each component LV
  2257. */
  2258. data_offset = 0;
  2259. new_data_offset = rs->data_offset;
  2260. } else if (rs->delta_disks > 0) {
  2261. /*
  2262. * Adding disks (reshaping forwards):
  2263. *
  2264. * - before reshape: data is at offset rs->data_offset != 0 and
  2265. * free space is at begin of each component LV
  2266. *
  2267. * - after reshape: data is at offset 0 on each component LV
  2268. */
  2269. data_offset = rs->data_offset;
  2270. new_data_offset = 0;
  2271. } else {
  2272. /*
  2273. * User space passes in 0 for data offset after having removed reshape space
  2274. *
  2275. * - or - (data offset != 0)
  2276. *
  2277. * Changing RAID layout or chunk size -> toggle offsets
  2278. *
  2279. * - before reshape: data is at offset rs->data_offset 0 and
  2280. * free space is at end of each component LV
  2281. * -or-
  2282. * data is at offset rs->data_offset != 0 and
  2283. * free space is at begin of each component LV
  2284. *
  2285. * - after reshape: data is at offset 0 if it was at offset != 0
  2286. * or at offset != 0 if it was at offset 0
  2287. * on each component LV
  2288. *
  2289. */
  2290. data_offset = rs->data_offset ? rdev->data_offset : 0;
  2291. new_data_offset = data_offset ? 0 : rs->data_offset;
  2292. set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
  2293. }
  2294. /*
  2295. * Make sure we got a minimum amount of free sectors per device
  2296. */
  2297. if (rs->data_offset &&
  2298. to_sector(i_size_read(rdev->bdev->bd_inode)) - rs->md.dev_sectors < MIN_FREE_RESHAPE_SPACE) {
  2299. rs->ti->error = data_offset ? "No space for forward reshape" :
  2300. "No space for backward reshape";
  2301. return -ENOSPC;
  2302. }
  2303. out:
  2304. /*
  2305. * Raise recovery_cp in case data_offset != 0 to
  2306. * avoid false recovery positives in the constructor.
  2307. */
  2308. if (rs->md.recovery_cp < rs->md.dev_sectors)
  2309. rs->md.recovery_cp += rs->dev[0].rdev.data_offset;
  2310. /* Adjust data offsets on all rdevs but on any raid4/5/6 journal device */
  2311. rdev_for_each(rdev, &rs->md) {
  2312. if (!test_bit(Journal, &rdev->flags)) {
  2313. rdev->data_offset = data_offset;
  2314. rdev->new_data_offset = new_data_offset;
  2315. }
  2316. }
  2317. return 0;
  2318. }
  2319. /* Userpace reordered disks -> adjust raid_disk indexes in @rs */
  2320. static void __reorder_raid_disk_indexes(struct raid_set *rs)
  2321. {
  2322. int i = 0;
  2323. struct md_rdev *rdev;
  2324. rdev_for_each(rdev, &rs->md) {
  2325. if (!test_bit(Journal, &rdev->flags)) {
  2326. rdev->raid_disk = i++;
  2327. rdev->saved_raid_disk = rdev->new_raid_disk = -1;
  2328. }
  2329. }
  2330. }
  2331. /*
  2332. * Setup @rs for takeover by a different raid level
  2333. */
  2334. static int rs_setup_takeover(struct raid_set *rs)
  2335. {
  2336. struct mddev *mddev = &rs->md;
  2337. struct md_rdev *rdev;
  2338. unsigned int d = mddev->raid_disks = rs->raid_disks;
  2339. sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;
  2340. if (rt_is_raid10(rs->raid_type)) {
  2341. if (rs_is_raid0(rs)) {
  2342. /* Userpace reordered disks -> adjust raid_disk indexes */
  2343. __reorder_raid_disk_indexes(rs);
  2344. /* raid0 -> raid10_far layout */
  2345. mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
  2346. rs->raid10_copies);
  2347. } else if (rs_is_raid1(rs))
  2348. /* raid1 -> raid10_near layout */
  2349. mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
  2350. rs->raid_disks);
  2351. else
  2352. return -EINVAL;
  2353. }
  2354. clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
  2355. mddev->recovery_cp = MaxSector;
  2356. while (d--) {
  2357. rdev = &rs->dev[d].rdev;
  2358. if (test_bit(d, (void *) rs->rebuild_disks)) {
  2359. clear_bit(In_sync, &rdev->flags);
  2360. clear_bit(Faulty, &rdev->flags);
  2361. mddev->recovery_cp = rdev->recovery_offset = 0;
  2362. /* Bitmap has to be created when we do an "up" takeover */
  2363. set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
  2364. }
  2365. rdev->new_data_offset = new_data_offset;
  2366. }
  2367. return 0;
  2368. }
  2369. /* Prepare @rs for reshape */
  2370. static int rs_prepare_reshape(struct raid_set *rs)
  2371. {
  2372. bool reshape;
  2373. struct mddev *mddev = &rs->md;
  2374. if (rs_is_raid10(rs)) {
  2375. if (rs->raid_disks != mddev->raid_disks &&
  2376. __is_raid10_near(mddev->layout) &&
  2377. rs->raid10_copies &&
  2378. rs->raid10_copies != __raid10_near_copies(mddev->layout)) {
  2379. /*
  2380. * raid disk have to be multiple of data copies to allow this conversion,
  2381. *
  2382. * This is actually not a reshape it is a
  2383. * rebuild of any additional mirrors per group
  2384. */
  2385. if (rs->raid_disks % rs->raid10_copies) {
  2386. rs->ti->error = "Can't reshape raid10 mirror groups";
  2387. return -EINVAL;
  2388. }
  2389. /* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */
  2390. __reorder_raid_disk_indexes(rs);
  2391. mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
  2392. rs->raid10_copies);
  2393. mddev->new_layout = mddev->layout;
  2394. reshape = false;
  2395. } else
  2396. reshape = true;
  2397. } else if (rs_is_raid456(rs))
  2398. reshape = true;
  2399. else if (rs_is_raid1(rs)) {
  2400. if (rs->delta_disks) {
  2401. /* Process raid1 via delta_disks */
  2402. mddev->degraded = rs->delta_disks < 0 ? -rs->delta_disks : rs->delta_disks;
  2403. reshape = true;
  2404. } else {
  2405. /* Process raid1 without delta_disks */
  2406. mddev->raid_disks = rs->raid_disks;
  2407. reshape = false;
  2408. }
  2409. } else {
  2410. rs->ti->error = "Called with bogus raid type";
  2411. return -EINVAL;
  2412. }
  2413. if (reshape) {
  2414. set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags);
  2415. set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
  2416. } else if (mddev->raid_disks < rs->raid_disks)
  2417. /* Create new superblocks and bitmaps, if any new disks */
  2418. set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
  2419. return 0;
  2420. }
  2421. /* Get reshape sectors from data_offsets or raid set */
  2422. static sector_t _get_reshape_sectors(struct raid_set *rs)
  2423. {
  2424. struct md_rdev *rdev;
  2425. sector_t reshape_sectors = 0;
  2426. rdev_for_each(rdev, &rs->md)
  2427. if (!test_bit(Journal, &rdev->flags)) {
  2428. reshape_sectors = (rdev->data_offset > rdev->new_data_offset) ?
  2429. rdev->data_offset - rdev->new_data_offset :
  2430. rdev->new_data_offset - rdev->data_offset;
  2431. break;
  2432. }
  2433. return max(reshape_sectors, (sector_t) rs->data_offset);
  2434. }
  2435. /*
  2436. *
  2437. * - change raid layout
  2438. * - change chunk size
  2439. * - add disks
  2440. * - remove disks
  2441. */
  2442. static int rs_setup_reshape(struct raid_set *rs)
  2443. {
  2444. int r = 0;
  2445. unsigned int cur_raid_devs, d;
  2446. sector_t reshape_sectors = _get_reshape_sectors(rs);
  2447. struct mddev *mddev = &rs->md;
  2448. struct md_rdev *rdev;
  2449. mddev->delta_disks = rs->delta_disks;
  2450. cur_raid_devs = mddev->raid_disks;
  2451. /* Ignore impossible layout change whilst adding/removing disks */
  2452. if (mddev->delta_disks &&
  2453. mddev->layout != mddev->new_layout) {
  2454. DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks);
  2455. mddev->new_layout = mddev->layout;
  2456. }
  2457. /*
  2458. * Adjust array size:
  2459. *
  2460. * - in case of adding disk(s), array size has
  2461. * to grow after the disk adding reshape,
  2462. * which'll hapen in the event handler;
  2463. * reshape will happen forward, so space has to
  2464. * be available at the beginning of each disk
  2465. *
  2466. * - in case of removing disk(s), array size
  2467. * has to shrink before starting the reshape,
  2468. * which'll happen here;
  2469. * reshape will happen backward, so space has to
  2470. * be available at the end of each disk
  2471. *
  2472. * - data_offset and new_data_offset are
  2473. * adjusted for aforementioned out of place
  2474. * reshaping based on userspace passing in
  2475. * the "data_offset <sectors>" key/value
  2476. * pair via the constructor
  2477. */
  2478. /* Add disk(s) */
  2479. if (rs->delta_disks > 0) {
  2480. /* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */
  2481. for (d = cur_raid_devs; d < rs->raid_disks; d++) {
  2482. rdev = &rs->dev[d].rdev;
  2483. clear_bit(In_sync, &rdev->flags);
  2484. /*
  2485. * save_raid_disk needs to be -1, or recovery_offset will be set to 0
  2486. * by md, which'll store that erroneously in the superblock on reshape
  2487. */
  2488. rdev->saved_raid_disk = -1;
  2489. rdev->raid_disk = d;
  2490. rdev->sectors = mddev->dev_sectors;
  2491. rdev->recovery_offset = rs_is_raid1(rs) ? 0 : MaxSector;
  2492. }
  2493. mddev->reshape_backwards = 0; /* adding disk(s) -> forward reshape */
  2494. /* Remove disk(s) */
  2495. } else if (rs->delta_disks < 0) {
  2496. r = rs_set_dev_and_array_sectors(rs, true);
  2497. mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */
  2498. /* Change layout and/or chunk size */
  2499. } else {
  2500. /*
  2501. * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size:
  2502. *
  2503. * keeping number of disks and do layout change ->
  2504. *
  2505. * toggle reshape_backward depending on data_offset:
  2506. *
  2507. * - free space upfront -> reshape forward
  2508. *
  2509. * - free space at the end -> reshape backward
  2510. *
  2511. *
  2512. * This utilizes free reshape space avoiding the need
  2513. * for userspace to move (parts of) LV segments in
  2514. * case of layout/chunksize change (for disk
  2515. * adding/removing reshape space has to be at
  2516. * the proper address (see above with delta_disks):
  2517. *
  2518. * add disk(s) -> begin
  2519. * remove disk(s)-> end
  2520. */
  2521. mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1;
  2522. }
  2523. /*
  2524. * Adjust device size for forward reshape
  2525. * because md_finish_reshape() reduces it.
  2526. */
  2527. if (!mddev->reshape_backwards)
  2528. rdev_for_each(rdev, &rs->md)
  2529. if (!test_bit(Journal, &rdev->flags))
  2530. rdev->sectors += reshape_sectors;
  2531. return r;
  2532. }
  2533. /*
  2534. * Enable/disable discard support on RAID set depending on
  2535. * RAID level and discard properties of underlying RAID members.
  2536. */
  2537. static void configure_discard_support(struct raid_set *rs)
  2538. {
  2539. int i;
  2540. bool raid456;
  2541. struct dm_target *ti = rs->ti;
  2542. /*
  2543. * XXX: RAID level 4,5,6 require zeroing for safety.
  2544. */
  2545. raid456 = rs_is_raid456(rs);
  2546. for (i = 0; i < rs->raid_disks; i++) {
  2547. struct request_queue *q;
  2548. if (!rs->dev[i].rdev.bdev)
  2549. continue;
  2550. q = bdev_get_queue(rs->dev[i].rdev.bdev);
  2551. if (!q || !blk_queue_discard(q))
  2552. return;
  2553. if (raid456) {
  2554. if (!devices_handle_discard_safely) {
  2555. DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
  2556. DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
  2557. return;
  2558. }
  2559. }
  2560. }
  2561. /*
  2562. * RAID1 and RAID10 personalities require bio splitting,
  2563. * RAID0/4/5/6 don't and process large discard bios properly.
  2564. */
  2565. ti->split_discard_bios = !!(rs_is_raid1(rs) || rs_is_raid10(rs));
  2566. ti->num_discard_bios = 1;
  2567. }
  2568. /*
  2569. * Construct a RAID0/1/10/4/5/6 mapping:
  2570. * Args:
  2571. * <raid_type> <#raid_params> <raid_params>{0,} \
  2572. * <#raid_devs> [<meta_dev1> <dev1>]{1,}
  2573. *
  2574. * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
  2575. * details on possible <raid_params>.
  2576. *
  2577. * Userspace is free to initialize the metadata devices, hence the superblocks to
  2578. * enforce recreation based on the passed in table parameters.
  2579. *
  2580. */
  2581. static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  2582. {
  2583. int r;
  2584. bool resize = false;
  2585. struct raid_type *rt;
  2586. unsigned int num_raid_params, num_raid_devs;
  2587. sector_t calculated_dev_sectors, rdev_sectors, reshape_sectors;
  2588. struct raid_set *rs = NULL;
  2589. const char *arg;
  2590. struct rs_layout rs_layout;
  2591. struct dm_arg_set as = { argc, argv }, as_nrd;
  2592. struct dm_arg _args[] = {
  2593. { 0, as.argc, "Cannot understand number of raid parameters" },
  2594. { 1, 254, "Cannot understand number of raid devices parameters" }
  2595. };
  2596. /* Must have <raid_type> */
  2597. arg = dm_shift_arg(&as);
  2598. if (!arg) {
  2599. ti->error = "No arguments";
  2600. return -EINVAL;
  2601. }
  2602. rt = get_raid_type(arg);
  2603. if (!rt) {
  2604. ti->error = "Unrecognised raid_type";
  2605. return -EINVAL;
  2606. }
  2607. /* Must have <#raid_params> */
  2608. if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
  2609. return -EINVAL;
  2610. /* number of raid device tupples <meta_dev data_dev> */
  2611. as_nrd = as;
  2612. dm_consume_args(&as_nrd, num_raid_params);
  2613. _args[1].max = (as_nrd.argc - 1) / 2;
  2614. if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
  2615. return -EINVAL;
  2616. if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
  2617. ti->error = "Invalid number of supplied raid devices";
  2618. return -EINVAL;
  2619. }
  2620. rs = raid_set_alloc(ti, rt, num_raid_devs);
  2621. if (IS_ERR(rs))
  2622. return PTR_ERR(rs);
  2623. r = parse_raid_params(rs, &as, num_raid_params);
  2624. if (r)
  2625. goto bad;
  2626. r = parse_dev_params(rs, &as);
  2627. if (r)
  2628. goto bad;
  2629. rs->md.sync_super = super_sync;
  2630. /*
  2631. * Calculate ctr requested array and device sizes to allow
  2632. * for superblock analysis needing device sizes defined.
  2633. *
  2634. * Any existing superblock will overwrite the array and device sizes
  2635. */
  2636. r = rs_set_dev_and_array_sectors(rs, false);
  2637. if (r)
  2638. goto bad;
  2639. calculated_dev_sectors = rs->md.dev_sectors;
  2640. /*
  2641. * Backup any new raid set level, layout, ...
  2642. * requested to be able to compare to superblock
  2643. * members for conversion decisions.
  2644. */
  2645. rs_config_backup(rs, &rs_layout);
  2646. r = analyse_superblocks(ti, rs);
  2647. if (r)
  2648. goto bad;
  2649. rdev_sectors = __rdev_sectors(rs);
  2650. if (!rdev_sectors) {
  2651. ti->error = "Invalid rdev size";
  2652. r = -EINVAL;
  2653. goto bad;
  2654. }
  2655. reshape_sectors = _get_reshape_sectors(rs);
  2656. if (calculated_dev_sectors != rdev_sectors)
  2657. resize = calculated_dev_sectors != (reshape_sectors ? rdev_sectors - reshape_sectors : rdev_sectors);
  2658. INIT_WORK(&rs->md.event_work, do_table_event);
  2659. ti->private = rs;
  2660. ti->num_flush_bios = 1;
  2661. /* Restore any requested new layout for conversion decision */
  2662. rs_config_restore(rs, &rs_layout);
  2663. /*
  2664. * Now that we have any superblock metadata available,
  2665. * check for new, recovering, reshaping, to be taken over,
  2666. * to be reshaped or an existing, unchanged raid set to
  2667. * run in sequence.
  2668. */
  2669. if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) {
  2670. /* A new raid6 set has to be recovered to ensure proper parity and Q-Syndrome */
  2671. if (rs_is_raid6(rs) &&
  2672. test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
  2673. ti->error = "'nosync' not allowed for new raid6 set";
  2674. r = -EINVAL;
  2675. goto bad;
  2676. }
  2677. rs_setup_recovery(rs, 0);
  2678. set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
  2679. rs_set_new(rs);
  2680. } else if (rs_is_recovering(rs)) {
  2681. /* Rebuild particular devices */
  2682. if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
  2683. set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
  2684. rs_setup_recovery(rs, MaxSector);
  2685. }
  2686. /* A recovering raid set may be resized */
  2687. ; /* skip setup rs */
  2688. } else if (rs_is_reshaping(rs)) {
  2689. /* Have to reject size change request during reshape */
  2690. if (resize) {
  2691. ti->error = "Can't resize a reshaping raid set";
  2692. r = -EPERM;
  2693. goto bad;
  2694. }
  2695. /* skip setup rs */
  2696. } else if (rs_takeover_requested(rs)) {
  2697. if (rs_is_reshaping(rs)) {
  2698. ti->error = "Can't takeover a reshaping raid set";
  2699. r = -EPERM;
  2700. goto bad;
  2701. }
  2702. /* We can't takeover a journaled raid4/5/6 */
  2703. if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
  2704. ti->error = "Can't takeover a journaled raid4/5/6 set";
  2705. r = -EPERM;
  2706. goto bad;
  2707. }
  2708. /*
  2709. * If a takeover is needed, userspace sets any additional
  2710. * devices to rebuild and we can check for a valid request here.
  2711. *
  2712. * If acceptible, set the level to the new requested
  2713. * one, prohibit requesting recovery, allow the raid
  2714. * set to run and store superblocks during resume.
  2715. */
  2716. r = rs_check_takeover(rs);
  2717. if (r)
  2718. goto bad;
  2719. r = rs_setup_takeover(rs);
  2720. if (r)
  2721. goto bad;
  2722. set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
  2723. /* Takeover ain't recovery, so disable recovery */
  2724. rs_setup_recovery(rs, MaxSector);
  2725. rs_set_new(rs);
  2726. } else if (rs_reshape_requested(rs)) {
  2727. /*
  2728. * No need to check for 'ongoing' takeover here, because takeover
  2729. * is an instant operation as oposed to an ongoing reshape.
  2730. */
  2731. /* We can't reshape a journaled raid4/5/6 */
  2732. if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
  2733. ti->error = "Can't reshape a journaled raid4/5/6 set";
  2734. r = -EPERM;
  2735. goto bad;
  2736. }
  2737. /* Out-of-place space has to be available to allow for a reshape unless raid1! */
  2738. if (reshape_sectors || rs_is_raid1(rs)) {
  2739. /*
  2740. * We can only prepare for a reshape here, because the
  2741. * raid set needs to run to provide the repective reshape
  2742. * check functions via its MD personality instance.
  2743. *
  2744. * So do the reshape check after md_run() succeeded.
  2745. */
  2746. r = rs_prepare_reshape(rs);
  2747. if (r)
  2748. goto bad;
  2749. /* Reshaping ain't recovery, so disable recovery */
  2750. rs_setup_recovery(rs, MaxSector);
  2751. }
  2752. rs_set_cur(rs);
  2753. } else {
  2754. /* May not set recovery when a device rebuild is requested */
  2755. if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
  2756. rs_setup_recovery(rs, MaxSector);
  2757. set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
  2758. } else
  2759. rs_setup_recovery(rs, test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ?
  2760. 0 : (resize ? calculated_dev_sectors : MaxSector));
  2761. rs_set_cur(rs);
  2762. }
  2763. /* If constructor requested it, change data and new_data offsets */
  2764. r = rs_adjust_data_offsets(rs);
  2765. if (r)
  2766. goto bad;
  2767. /* Start raid set read-only and assumed clean to change in raid_resume() */
  2768. rs->md.ro = 1;
  2769. rs->md.in_sync = 1;
  2770. /* Keep array frozen */
  2771. set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
  2772. /* Has to be held on running the array */
  2773. mddev_lock_nointr(&rs->md);
  2774. r = md_run(&rs->md);
  2775. rs->md.in_sync = 0; /* Assume already marked dirty */
  2776. if (r) {
  2777. ti->error = "Failed to run raid array";
  2778. mddev_unlock(&rs->md);
  2779. goto bad;
  2780. }
  2781. r = md_start(&rs->md);
  2782. if (r) {
  2783. ti->error = "Failed to start raid array";
  2784. mddev_unlock(&rs->md);
  2785. goto bad_md_start;
  2786. }
  2787. rs->callbacks.congested_fn = raid_is_congested;
  2788. dm_table_add_target_callbacks(ti->table, &rs->callbacks);
  2789. /* If raid4/5/6 journal mode explicitly requested (only possible with journal dev) -> set it */
  2790. if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
  2791. r = r5c_journal_mode_set(&rs->md, rs->journal_dev.mode);
  2792. if (r) {
  2793. ti->error = "Failed to set raid4/5/6 journal mode";
  2794. mddev_unlock(&rs->md);
  2795. goto bad_journal_mode_set;
  2796. }
  2797. }
  2798. mddev_suspend(&rs->md);
  2799. set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags);
  2800. /* Try to adjust the raid4/5/6 stripe cache size to the stripe size */
  2801. if (rs_is_raid456(rs)) {
  2802. r = rs_set_raid456_stripe_cache(rs);
  2803. if (r)
  2804. goto bad_stripe_cache;
  2805. }
  2806. /* Now do an early reshape check */
  2807. if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
  2808. r = rs_check_reshape(rs);
  2809. if (r)
  2810. goto bad_check_reshape;
  2811. /* Restore new, ctr requested layout to perform check */
  2812. rs_config_restore(rs, &rs_layout);
  2813. if (rs->md.pers->start_reshape) {
  2814. r = rs->md.pers->check_reshape(&rs->md);
  2815. if (r) {
  2816. ti->error = "Reshape check failed";
  2817. goto bad_check_reshape;
  2818. }
  2819. }
  2820. }
  2821. /* Disable/enable discard support on raid set. */
  2822. configure_discard_support(rs);
  2823. mddev_unlock(&rs->md);
  2824. return 0;
  2825. bad_md_start:
  2826. bad_journal_mode_set:
  2827. bad_stripe_cache:
  2828. bad_check_reshape:
  2829. md_stop(&rs->md);
  2830. bad:
  2831. raid_set_free(rs);
  2832. return r;
  2833. }
  2834. static void raid_dtr(struct dm_target *ti)
  2835. {
  2836. struct raid_set *rs = ti->private;
  2837. list_del_init(&rs->callbacks.list);
  2838. md_stop(&rs->md);
  2839. raid_set_free(rs);
  2840. }
  2841. static int raid_map(struct dm_target *ti, struct bio *bio)
  2842. {
  2843. struct raid_set *rs = ti->private;
  2844. struct mddev *mddev = &rs->md;
  2845. /*
  2846. * If we're reshaping to add disk(s)), ti->len and
  2847. * mddev->array_sectors will differ during the process
  2848. * (ti->len > mddev->array_sectors), so we have to requeue
  2849. * bios with addresses > mddev->array_sectors here or
  2850. * there will occur accesses past EOD of the component
  2851. * data images thus erroring the raid set.
  2852. */
  2853. if (unlikely(bio_end_sector(bio) > mddev->array_sectors))
  2854. return DM_MAPIO_REQUEUE;
  2855. md_handle_request(mddev, bio);
  2856. return DM_MAPIO_SUBMITTED;
  2857. }
  2858. /* Return sync state string for @state */
  2859. enum sync_state { st_frozen, st_reshape, st_resync, st_check, st_repair, st_recover, st_idle };
  2860. static const char *sync_str(enum sync_state state)
  2861. {
  2862. /* Has to be in above sync_state order! */
  2863. static const char *sync_strs[] = {
  2864. "frozen",
  2865. "reshape",
  2866. "resync",
  2867. "check",
  2868. "repair",
  2869. "recover",
  2870. "idle"
  2871. };
  2872. return __within_range(state, 0, ARRAY_SIZE(sync_strs) - 1) ? sync_strs[state] : "undef";
  2873. };
  2874. /* Return enum sync_state for @mddev derived from @recovery flags */
  2875. static enum sync_state decipher_sync_action(struct mddev *mddev, unsigned long recovery)
  2876. {
  2877. if (test_bit(MD_RECOVERY_FROZEN, &recovery))
  2878. return st_frozen;
  2879. /* The MD sync thread can be done with io or be interrupted but still be running */
  2880. if (!test_bit(MD_RECOVERY_DONE, &recovery) &&
  2881. (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
  2882. (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery)))) {
  2883. if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
  2884. return st_reshape;
  2885. if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
  2886. if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
  2887. return st_resync;
  2888. if (test_bit(MD_RECOVERY_CHECK, &recovery))
  2889. return st_check;
  2890. return st_repair;
  2891. }
  2892. if (test_bit(MD_RECOVERY_RECOVER, &recovery))
  2893. return st_recover;
  2894. if (mddev->reshape_position != MaxSector)
  2895. return st_reshape;
  2896. }
  2897. return st_idle;
  2898. }
  2899. /*
  2900. * Return status string for @rdev
  2901. *
  2902. * Status characters:
  2903. *
  2904. * 'D' = Dead/Failed raid set component or raid4/5/6 journal device
  2905. * 'a' = Alive but not in-sync raid set component _or_ alive raid4/5/6 'write_back' journal device
  2906. * 'A' = Alive and in-sync raid set component _or_ alive raid4/5/6 'write_through' journal device
  2907. * '-' = Non-existing device (i.e. uspace passed '- -' into the ctr)
  2908. */
  2909. static const char *__raid_dev_status(struct raid_set *rs, struct md_rdev *rdev)
  2910. {
  2911. if (!rdev->bdev)
  2912. return "-";
  2913. else if (test_bit(Faulty, &rdev->flags))
  2914. return "D";
  2915. else if (test_bit(Journal, &rdev->flags))
  2916. return (rs->journal_dev.mode == R5C_JOURNAL_MODE_WRITE_THROUGH) ? "A" : "a";
  2917. else if (test_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags) ||
  2918. (!test_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags) &&
  2919. !test_bit(In_sync, &rdev->flags)))
  2920. return "a";
  2921. else
  2922. return "A";
  2923. }
  2924. /* Helper to return resync/reshape progress for @rs and runtime flags for raid set in sync / resynching */
  2925. static sector_t rs_get_progress(struct raid_set *rs, unsigned long recovery,
  2926. sector_t resync_max_sectors)
  2927. {
  2928. sector_t r;
  2929. enum sync_state state;
  2930. struct mddev *mddev = &rs->md;
  2931. clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
  2932. clear_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
  2933. if (rs_is_raid0(rs)) {
  2934. r = resync_max_sectors;
  2935. set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
  2936. } else {
  2937. state = decipher_sync_action(mddev, recovery);
  2938. if (state == st_idle && !test_bit(MD_RECOVERY_INTR, &recovery))
  2939. r = mddev->recovery_cp;
  2940. else
  2941. r = mddev->curr_resync_completed;
  2942. if (state == st_idle && r >= resync_max_sectors) {
  2943. /*
  2944. * Sync complete.
  2945. */
  2946. /* In case we have finished recovering, the array is in sync. */
  2947. if (test_bit(MD_RECOVERY_RECOVER, &recovery))
  2948. set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
  2949. } else if (state == st_recover)
  2950. /*
  2951. * In case we are recovering, the array is not in sync
  2952. * and health chars should show the recovering legs.
  2953. */
  2954. ;
  2955. else if (state == st_resync)
  2956. /*
  2957. * If "resync" is occurring, the raid set
  2958. * is or may be out of sync hence the health
  2959. * characters shall be 'a'.
  2960. */
  2961. set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
  2962. else if (state == st_reshape)
  2963. /*
  2964. * If "reshape" is occurring, the raid set
  2965. * is or may be out of sync hence the health
  2966. * characters shall be 'a'.
  2967. */
  2968. set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
  2969. else if (state == st_check || state == st_repair)
  2970. /*
  2971. * If "check" or "repair" is occurring, the raid set has
  2972. * undergone an initial sync and the health characters
  2973. * should not be 'a' anymore.
  2974. */
  2975. set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
  2976. else {
  2977. struct md_rdev *rdev;
  2978. /*
  2979. * We are idle and recovery is needed, prevent 'A' chars race
  2980. * caused by components still set to in-sync by constructor.
  2981. */
  2982. if (test_bit(MD_RECOVERY_NEEDED, &recovery))
  2983. set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
  2984. /*
  2985. * The raid set may be doing an initial sync, or it may
  2986. * be rebuilding individual components. If all the
  2987. * devices are In_sync, then it is the raid set that is
  2988. * being initialized.
  2989. */
  2990. set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
  2991. rdev_for_each(rdev, mddev)
  2992. if (!test_bit(Journal, &rdev->flags) &&
  2993. !test_bit(In_sync, &rdev->flags)) {
  2994. clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
  2995. break;
  2996. }
  2997. }
  2998. }
  2999. return min(r, resync_max_sectors);
  3000. }
  3001. /* Helper to return @dev name or "-" if !@dev */
  3002. static const char *__get_dev_name(struct dm_dev *dev)
  3003. {
  3004. return dev ? dev->name : "-";
  3005. }
  3006. static void raid_status(struct dm_target *ti, status_type_t type,
  3007. unsigned int status_flags, char *result, unsigned int maxlen)
  3008. {
  3009. struct raid_set *rs = ti->private;
  3010. struct mddev *mddev = &rs->md;
  3011. struct r5conf *conf = mddev->private;
  3012. int i, max_nr_stripes = conf ? conf->max_nr_stripes : 0;
  3013. unsigned long recovery;
  3014. unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
  3015. unsigned int sz = 0;
  3016. unsigned int rebuild_disks;
  3017. unsigned int write_mostly_params = 0;
  3018. sector_t progress, resync_max_sectors, resync_mismatches;
  3019. const char *sync_action;
  3020. struct raid_type *rt;
  3021. switch (type) {
  3022. case STATUSTYPE_INFO:
  3023. /* *Should* always succeed */
  3024. rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
  3025. if (!rt)
  3026. return;
  3027. DMEMIT("%s %d ", rt->name, mddev->raid_disks);
  3028. /* Access most recent mddev properties for status output */
  3029. smp_rmb();
  3030. recovery = rs->md.recovery;
  3031. /* Get sensible max sectors even if raid set not yet started */
  3032. resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
  3033. mddev->resync_max_sectors : mddev->dev_sectors;
  3034. progress = rs_get_progress(rs, recovery, resync_max_sectors);
  3035. resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
  3036. atomic64_read(&mddev->resync_mismatches) : 0;
  3037. sync_action = sync_str(decipher_sync_action(&rs->md, recovery));
  3038. /* HM FIXME: do we want another state char for raid0? It shows 'D'/'A'/'-' now */
  3039. for (i = 0; i < rs->raid_disks; i++)
  3040. DMEMIT(__raid_dev_status(rs, &rs->dev[i].rdev));
  3041. /*
  3042. * In-sync/Reshape ratio:
  3043. * The in-sync ratio shows the progress of:
  3044. * - Initializing the raid set
  3045. * - Rebuilding a subset of devices of the raid set
  3046. * The user can distinguish between the two by referring
  3047. * to the status characters.
  3048. *
  3049. * The reshape ratio shows the progress of
  3050. * changing the raid layout or the number of
  3051. * disks of a raid set
  3052. */
  3053. DMEMIT(" %llu/%llu", (unsigned long long) progress,
  3054. (unsigned long long) resync_max_sectors);
  3055. /*
  3056. * v1.5.0+:
  3057. *
  3058. * Sync action:
  3059. * See Documentation/device-mapper/dm-raid.txt for
  3060. * information on each of these states.
  3061. */
  3062. DMEMIT(" %s", sync_action);
  3063. /*
  3064. * v1.5.0+:
  3065. *
  3066. * resync_mismatches/mismatch_cnt
  3067. * This field shows the number of discrepancies found when
  3068. * performing a "check" of the raid set.
  3069. */
  3070. DMEMIT(" %llu", (unsigned long long) resync_mismatches);
  3071. /*
  3072. * v1.9.0+:
  3073. *
  3074. * data_offset (needed for out of space reshaping)
  3075. * This field shows the data offset into the data
  3076. * image LV where the first stripes data starts.
  3077. *
  3078. * We keep data_offset equal on all raid disks of the set,
  3079. * so retrieving it from the first raid disk is sufficient.
  3080. */
  3081. DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
  3082. /*
  3083. * v1.10.0+:
  3084. */
  3085. DMEMIT(" %s", test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ?
  3086. __raid_dev_status(rs, &rs->journal_dev.rdev) : "-");
  3087. break;
  3088. case STATUSTYPE_TABLE:
  3089. /* Report the table line string you would use to construct this raid set */
  3090. /* Calculate raid parameter count */
  3091. for (i = 0; i < rs->raid_disks; i++)
  3092. if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
  3093. write_mostly_params += 2;
  3094. rebuild_disks = memweight(rs->rebuild_disks, DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks));
  3095. raid_param_cnt += rebuild_disks * 2 +
  3096. write_mostly_params +
  3097. hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
  3098. hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2 +
  3099. (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ? 2 : 0) +
  3100. (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags) ? 2 : 0);
  3101. /* Emit table line */
  3102. /* This has to be in the documented order for userspace! */
  3103. DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
  3104. if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
  3105. DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
  3106. if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
  3107. DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
  3108. if (rebuild_disks)
  3109. for (i = 0; i < rs->raid_disks; i++)
  3110. if (test_bit(rs->dev[i].rdev.raid_disk, (void *) rs->rebuild_disks))
  3111. DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD),
  3112. rs->dev[i].rdev.raid_disk);
  3113. if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
  3114. DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
  3115. mddev->bitmap_info.daemon_sleep);
  3116. if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
  3117. DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
  3118. mddev->sync_speed_min);
  3119. if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
  3120. DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
  3121. mddev->sync_speed_max);
  3122. if (write_mostly_params)
  3123. for (i = 0; i < rs->raid_disks; i++)
  3124. if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
  3125. DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
  3126. rs->dev[i].rdev.raid_disk);
  3127. if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
  3128. DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
  3129. mddev->bitmap_info.max_write_behind);
  3130. if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
  3131. DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
  3132. max_nr_stripes);
  3133. if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
  3134. DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
  3135. (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
  3136. if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
  3137. DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
  3138. raid10_md_layout_to_copies(mddev->layout));
  3139. if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
  3140. DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
  3141. raid10_md_layout_to_format(mddev->layout));
  3142. if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
  3143. DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
  3144. max(rs->delta_disks, mddev->delta_disks));
  3145. if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
  3146. DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
  3147. (unsigned long long) rs->data_offset);
  3148. if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags))
  3149. DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV),
  3150. __get_dev_name(rs->journal_dev.dev));
  3151. if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags))
  3152. DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE),
  3153. md_journal_mode_to_dm_raid(rs->journal_dev.mode));
  3154. DMEMIT(" %d", rs->raid_disks);
  3155. for (i = 0; i < rs->raid_disks; i++)
  3156. DMEMIT(" %s %s", __get_dev_name(rs->dev[i].meta_dev),
  3157. __get_dev_name(rs->dev[i].data_dev));
  3158. }
  3159. }
  3160. static int raid_message(struct dm_target *ti, unsigned int argc, char **argv,
  3161. char *result, unsigned maxlen)
  3162. {
  3163. struct raid_set *rs = ti->private;
  3164. struct mddev *mddev = &rs->md;
  3165. if (!mddev->pers || !mddev->pers->sync_request)
  3166. return -EINVAL;
  3167. if (!strcasecmp(argv[0], "frozen"))
  3168. set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  3169. else
  3170. clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  3171. if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
  3172. if (mddev->sync_thread) {
  3173. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  3174. md_reap_sync_thread(mddev);
  3175. }
  3176. } else if (decipher_sync_action(mddev, mddev->recovery) != st_idle)
  3177. return -EBUSY;
  3178. else if (!strcasecmp(argv[0], "resync"))
  3179. ; /* MD_RECOVERY_NEEDED set below */
  3180. else if (!strcasecmp(argv[0], "recover"))
  3181. set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
  3182. else {
  3183. if (!strcasecmp(argv[0], "check")) {
  3184. set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3185. set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
  3186. set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3187. } else if (!strcasecmp(argv[0], "repair")) {
  3188. set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
  3189. set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3190. } else
  3191. return -EINVAL;
  3192. }
  3193. if (mddev->ro == 2) {
  3194. /* A write to sync_action is enough to justify
  3195. * canceling read-auto mode
  3196. */
  3197. mddev->ro = 0;
  3198. if (!mddev->suspended && mddev->sync_thread)
  3199. md_wakeup_thread(mddev->sync_thread);
  3200. }
  3201. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3202. if (!mddev->suspended && mddev->thread)
  3203. md_wakeup_thread(mddev->thread);
  3204. return 0;
  3205. }
  3206. static int raid_iterate_devices(struct dm_target *ti,
  3207. iterate_devices_callout_fn fn, void *data)
  3208. {
  3209. struct raid_set *rs = ti->private;
  3210. unsigned int i;
  3211. int r = 0;
  3212. for (i = 0; !r && i < rs->md.raid_disks; i++)
  3213. if (rs->dev[i].data_dev)
  3214. r = fn(ti,
  3215. rs->dev[i].data_dev,
  3216. 0, /* No offset on data devs */
  3217. rs->md.dev_sectors,
  3218. data);
  3219. return r;
  3220. }
  3221. static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
  3222. {
  3223. struct raid_set *rs = ti->private;
  3224. unsigned int chunk_size = to_bytes(rs->md.chunk_sectors);
  3225. blk_limits_io_min(limits, chunk_size);
  3226. blk_limits_io_opt(limits, chunk_size * mddev_data_stripes(rs));
  3227. }
  3228. static void raid_postsuspend(struct dm_target *ti)
  3229. {
  3230. struct raid_set *rs = ti->private;
  3231. if (!test_and_set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
  3232. /* Writes have to be stopped before suspending to avoid deadlocks. */
  3233. if (!test_bit(MD_RECOVERY_FROZEN, &rs->md.recovery))
  3234. md_stop_writes(&rs->md);
  3235. mddev_lock_nointr(&rs->md);
  3236. mddev_suspend(&rs->md);
  3237. mddev_unlock(&rs->md);
  3238. }
  3239. }
  3240. static void attempt_restore_of_faulty_devices(struct raid_set *rs)
  3241. {
  3242. int i;
  3243. uint64_t cleared_failed_devices[DISKS_ARRAY_ELEMS];
  3244. unsigned long flags;
  3245. bool cleared = false;
  3246. struct dm_raid_superblock *sb;
  3247. struct mddev *mddev = &rs->md;
  3248. struct md_rdev *r;
  3249. /* RAID personalities have to provide hot add/remove methods or we need to bail out. */
  3250. if (!mddev->pers || !mddev->pers->hot_add_disk || !mddev->pers->hot_remove_disk)
  3251. return;
  3252. memset(cleared_failed_devices, 0, sizeof(cleared_failed_devices));
  3253. for (i = 0; i < mddev->raid_disks; i++) {
  3254. r = &rs->dev[i].rdev;
  3255. /* HM FIXME: enhance journal device recovery processing */
  3256. if (test_bit(Journal, &r->flags))
  3257. continue;
  3258. if (test_bit(Faulty, &r->flags) &&
  3259. r->meta_bdev && !read_disk_sb(r, r->sb_size, true)) {
  3260. DMINFO("Faulty %s device #%d has readable super block."
  3261. " Attempting to revive it.",
  3262. rs->raid_type->name, i);
  3263. /*
  3264. * Faulty bit may be set, but sometimes the array can
  3265. * be suspended before the personalities can respond
  3266. * by removing the device from the array (i.e. calling
  3267. * 'hot_remove_disk'). If they haven't yet removed
  3268. * the failed device, its 'raid_disk' number will be
  3269. * '>= 0' - meaning we must call this function
  3270. * ourselves.
  3271. */
  3272. flags = r->flags;
  3273. clear_bit(In_sync, &r->flags); /* Mandatory for hot remove. */
  3274. if (r->raid_disk >= 0) {
  3275. if (mddev->pers->hot_remove_disk(mddev, r)) {
  3276. /* Failed to revive this device, try next */
  3277. r->flags = flags;
  3278. continue;
  3279. }
  3280. } else
  3281. r->raid_disk = r->saved_raid_disk = i;
  3282. clear_bit(Faulty, &r->flags);
  3283. clear_bit(WriteErrorSeen, &r->flags);
  3284. if (mddev->pers->hot_add_disk(mddev, r)) {
  3285. /* Failed to revive this device, try next */
  3286. r->raid_disk = r->saved_raid_disk = -1;
  3287. r->flags = flags;
  3288. } else {
  3289. clear_bit(In_sync, &r->flags);
  3290. r->recovery_offset = 0;
  3291. set_bit(i, (void *) cleared_failed_devices);
  3292. cleared = true;
  3293. }
  3294. }
  3295. }
  3296. /* If any failed devices could be cleared, update all sbs failed_devices bits */
  3297. if (cleared) {
  3298. uint64_t failed_devices[DISKS_ARRAY_ELEMS];
  3299. rdev_for_each(r, &rs->md) {
  3300. if (test_bit(Journal, &r->flags))
  3301. continue;
  3302. sb = page_address(r->sb_page);
  3303. sb_retrieve_failed_devices(sb, failed_devices);
  3304. for (i = 0; i < DISKS_ARRAY_ELEMS; i++)
  3305. failed_devices[i] &= ~cleared_failed_devices[i];
  3306. sb_update_failed_devices(sb, failed_devices);
  3307. }
  3308. }
  3309. }
  3310. static int __load_dirty_region_bitmap(struct raid_set *rs)
  3311. {
  3312. int r = 0;
  3313. /* Try loading the bitmap unless "raid0", which does not have one */
  3314. if (!rs_is_raid0(rs) &&
  3315. !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
  3316. r = md_bitmap_load(&rs->md);
  3317. if (r)
  3318. DMERR("Failed to load bitmap");
  3319. }
  3320. return r;
  3321. }
  3322. /* Enforce updating all superblocks */
  3323. static void rs_update_sbs(struct raid_set *rs)
  3324. {
  3325. struct mddev *mddev = &rs->md;
  3326. int ro = mddev->ro;
  3327. set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
  3328. mddev->ro = 0;
  3329. md_update_sb(mddev, 1);
  3330. mddev->ro = ro;
  3331. }
  3332. /*
  3333. * Reshape changes raid algorithm of @rs to new one within personality
  3334. * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes
  3335. * disks from a raid set thus growing/shrinking it or resizes the set
  3336. *
  3337. * Call mddev_lock_nointr() before!
  3338. */
  3339. static int rs_start_reshape(struct raid_set *rs)
  3340. {
  3341. int r;
  3342. struct mddev *mddev = &rs->md;
  3343. struct md_personality *pers = mddev->pers;
  3344. /* Don't allow the sync thread to work until the table gets reloaded. */
  3345. set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
  3346. r = rs_setup_reshape(rs);
  3347. if (r)
  3348. return r;
  3349. /*
  3350. * Check any reshape constraints enforced by the personalility
  3351. *
  3352. * May as well already kick the reshape off so that * pers->start_reshape() becomes optional.
  3353. */
  3354. r = pers->check_reshape(mddev);
  3355. if (r) {
  3356. rs->ti->error = "pers->check_reshape() failed";
  3357. return r;
  3358. }
  3359. /*
  3360. * Personality may not provide start reshape method in which
  3361. * case check_reshape above has already covered everything
  3362. */
  3363. if (pers->start_reshape) {
  3364. r = pers->start_reshape(mddev);
  3365. if (r) {
  3366. rs->ti->error = "pers->start_reshape() failed";
  3367. return r;
  3368. }
  3369. }
  3370. /*
  3371. * Now reshape got set up, update superblocks to
  3372. * reflect the fact so that a table reload will
  3373. * access proper superblock content in the ctr.
  3374. */
  3375. rs_update_sbs(rs);
  3376. return 0;
  3377. }
  3378. static int raid_preresume(struct dm_target *ti)
  3379. {
  3380. int r;
  3381. struct raid_set *rs = ti->private;
  3382. struct mddev *mddev = &rs->md;
  3383. /* This is a resume after a suspend of the set -> it's already started. */
  3384. if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
  3385. return 0;
  3386. /*
  3387. * The superblocks need to be updated on disk if the
  3388. * array is new or new devices got added (thus zeroed
  3389. * out by userspace) or __load_dirty_region_bitmap
  3390. * will overwrite them in core with old data or fail.
  3391. */
  3392. if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags))
  3393. rs_update_sbs(rs);
  3394. /* Load the bitmap from disk unless raid0 */
  3395. r = __load_dirty_region_bitmap(rs);
  3396. if (r)
  3397. return r;
  3398. /* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */
  3399. if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) && mddev->bitmap &&
  3400. mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) {
  3401. r = md_bitmap_resize(mddev->bitmap, mddev->dev_sectors,
  3402. to_bytes(rs->requested_bitmap_chunk_sectors), 0);
  3403. if (r)
  3404. DMERR("Failed to resize bitmap");
  3405. }
  3406. /* Check for any resize/reshape on @rs and adjust/initiate */
  3407. /* Be prepared for mddev_resume() in raid_resume() */
  3408. set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  3409. if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) {
  3410. set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3411. mddev->resync_min = mddev->recovery_cp;
  3412. }
  3413. /* Check for any reshape request unless new raid set */
  3414. if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
  3415. /* Initiate a reshape. */
  3416. rs_set_rdev_sectors(rs);
  3417. mddev_lock_nointr(mddev);
  3418. r = rs_start_reshape(rs);
  3419. mddev_unlock(mddev);
  3420. if (r)
  3421. DMWARN("Failed to check/start reshape, continuing without change");
  3422. r = 0;
  3423. }
  3424. return r;
  3425. }
  3426. static void raid_resume(struct dm_target *ti)
  3427. {
  3428. struct raid_set *rs = ti->private;
  3429. struct mddev *mddev = &rs->md;
  3430. if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
  3431. /*
  3432. * A secondary resume while the device is active.
  3433. * Take this opportunity to check whether any failed
  3434. * devices are reachable again.
  3435. */
  3436. attempt_restore_of_faulty_devices(rs);
  3437. }
  3438. if (test_and_clear_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
  3439. /* Only reduce raid set size before running a disk removing reshape. */
  3440. if (mddev->delta_disks < 0)
  3441. rs_set_capacity(rs);
  3442. mddev_lock_nointr(mddev);
  3443. clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
  3444. mddev->ro = 0;
  3445. mddev->in_sync = 0;
  3446. mddev_resume(mddev);
  3447. mddev_unlock(mddev);
  3448. }
  3449. }
  3450. static struct target_type raid_target = {
  3451. .name = "raid",
  3452. .version = {1, 14, 0},
  3453. .module = THIS_MODULE,
  3454. .ctr = raid_ctr,
  3455. .dtr = raid_dtr,
  3456. .map = raid_map,
  3457. .status = raid_status,
  3458. .message = raid_message,
  3459. .iterate_devices = raid_iterate_devices,
  3460. .io_hints = raid_io_hints,
  3461. .postsuspend = raid_postsuspend,
  3462. .preresume = raid_preresume,
  3463. .resume = raid_resume,
  3464. };
  3465. static int __init dm_raid_init(void)
  3466. {
  3467. DMINFO("Loading target version %u.%u.%u",
  3468. raid_target.version[0],
  3469. raid_target.version[1],
  3470. raid_target.version[2]);
  3471. return dm_register_target(&raid_target);
  3472. }
  3473. static void __exit dm_raid_exit(void)
  3474. {
  3475. dm_unregister_target(&raid_target);
  3476. }
  3477. module_init(dm_raid_init);
  3478. module_exit(dm_raid_exit);
  3479. module_param(devices_handle_discard_safely, bool, 0644);
  3480. MODULE_PARM_DESC(devices_handle_discard_safely,
  3481. "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
  3482. MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
  3483. MODULE_ALIAS("dm-raid0");
  3484. MODULE_ALIAS("dm-raid1");
  3485. MODULE_ALIAS("dm-raid10");
  3486. MODULE_ALIAS("dm-raid4");
  3487. MODULE_ALIAS("dm-raid5");
  3488. MODULE_ALIAS("dm-raid6");
  3489. MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
  3490. MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
  3491. MODULE_LICENSE("GPL");