dm-raid.c 105 KB

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