dm-raid.c 117 KB

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