dm-integrity.c 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670
  1. /*
  2. * Copyright (C) 2016-2017 Red Hat, Inc. All rights reserved.
  3. * Copyright (C) 2016-2017 Milan Broz
  4. * Copyright (C) 2016-2017 Mikulas Patocka
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include "dm-bio-record.h"
  9. #include <linux/compiler.h>
  10. #include <linux/module.h>
  11. #include <linux/device-mapper.h>
  12. #include <linux/dm-io.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/sort.h>
  15. #include <linux/rbtree.h>
  16. #include <linux/delay.h>
  17. #include <linux/random.h>
  18. #include <linux/reboot.h>
  19. #include <crypto/hash.h>
  20. #include <crypto/skcipher.h>
  21. #include <linux/async_tx.h>
  22. #include <linux/dm-bufio.h>
  23. #include "dm-audit.h"
  24. #define DM_MSG_PREFIX "integrity"
  25. #define DEFAULT_INTERLEAVE_SECTORS 32768
  26. #define DEFAULT_JOURNAL_SIZE_FACTOR 7
  27. #define DEFAULT_SECTORS_PER_BITMAP_BIT 32768
  28. #define DEFAULT_BUFFER_SECTORS 128
  29. #define DEFAULT_JOURNAL_WATERMARK 50
  30. #define DEFAULT_SYNC_MSEC 10000
  31. #define DEFAULT_MAX_JOURNAL_SECTORS (IS_ENABLED(CONFIG_64BIT) ? 131072 : 8192)
  32. #define MIN_LOG2_INTERLEAVE_SECTORS 3
  33. #define MAX_LOG2_INTERLEAVE_SECTORS 31
  34. #define METADATA_WORKQUEUE_MAX_ACTIVE 16
  35. #define RECALC_SECTORS (IS_ENABLED(CONFIG_64BIT) ? 32768 : 2048)
  36. #define RECALC_WRITE_SUPER 16
  37. #define BITMAP_BLOCK_SIZE 4096 /* don't change it */
  38. #define BITMAP_FLUSH_INTERVAL (10 * HZ)
  39. #define DISCARD_FILLER 0xf6
  40. #define SALT_SIZE 16
  41. /*
  42. * Warning - DEBUG_PRINT prints security-sensitive data to the log,
  43. * so it should not be enabled in the official kernel
  44. */
  45. //#define DEBUG_PRINT
  46. //#define INTERNAL_VERIFY
  47. /*
  48. * On disk structures
  49. */
  50. #define SB_MAGIC "integrt"
  51. #define SB_VERSION_1 1
  52. #define SB_VERSION_2 2
  53. #define SB_VERSION_3 3
  54. #define SB_VERSION_4 4
  55. #define SB_VERSION_5 5
  56. #define SB_SECTORS 8
  57. #define MAX_SECTORS_PER_BLOCK 8
  58. struct superblock {
  59. __u8 magic[8];
  60. __u8 version;
  61. __u8 log2_interleave_sectors;
  62. __le16 integrity_tag_size;
  63. __le32 journal_sections;
  64. __le64 provided_data_sectors; /* userspace uses this value */
  65. __le32 flags;
  66. __u8 log2_sectors_per_block;
  67. __u8 log2_blocks_per_bitmap_bit;
  68. __u8 pad[2];
  69. __le64 recalc_sector;
  70. __u8 pad2[8];
  71. __u8 salt[SALT_SIZE];
  72. };
  73. #define SB_FLAG_HAVE_JOURNAL_MAC 0x1
  74. #define SB_FLAG_RECALCULATING 0x2
  75. #define SB_FLAG_DIRTY_BITMAP 0x4
  76. #define SB_FLAG_FIXED_PADDING 0x8
  77. #define SB_FLAG_FIXED_HMAC 0x10
  78. #define JOURNAL_ENTRY_ROUNDUP 8
  79. typedef __le64 commit_id_t;
  80. #define JOURNAL_MAC_PER_SECTOR 8
  81. struct journal_entry {
  82. union {
  83. struct {
  84. __le32 sector_lo;
  85. __le32 sector_hi;
  86. } s;
  87. __le64 sector;
  88. } u;
  89. commit_id_t last_bytes[];
  90. /* __u8 tag[0]; */
  91. };
  92. #define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
  93. #if BITS_PER_LONG == 64
  94. #define journal_entry_set_sector(je, x) do { smp_wmb(); WRITE_ONCE((je)->u.sector, cpu_to_le64(x)); } while (0)
  95. #else
  96. #define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); WRITE_ONCE((je)->u.s.sector_hi, cpu_to_le32((x) >> 32)); } while (0)
  97. #endif
  98. #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
  99. #define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
  100. #define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
  101. #define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
  102. #define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
  103. #define JOURNAL_BLOCK_SECTORS 8
  104. #define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
  105. #define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
  106. struct journal_sector {
  107. struct_group(sectors,
  108. __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR];
  109. __u8 mac[JOURNAL_MAC_PER_SECTOR];
  110. );
  111. commit_id_t commit_id;
  112. };
  113. #define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK]))
  114. #define METADATA_PADDING_SECTORS 8
  115. #define N_COMMIT_IDS 4
  116. static unsigned char prev_commit_seq(unsigned char seq)
  117. {
  118. return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS;
  119. }
  120. static unsigned char next_commit_seq(unsigned char seq)
  121. {
  122. return (seq + 1) % N_COMMIT_IDS;
  123. }
  124. /*
  125. * In-memory structures
  126. */
  127. struct journal_node {
  128. struct rb_node node;
  129. sector_t sector;
  130. };
  131. struct alg_spec {
  132. char *alg_string;
  133. char *key_string;
  134. __u8 *key;
  135. unsigned int key_size;
  136. };
  137. struct dm_integrity_c {
  138. struct dm_dev *dev;
  139. struct dm_dev *meta_dev;
  140. unsigned int tag_size;
  141. __s8 log2_tag_size;
  142. sector_t start;
  143. mempool_t journal_io_mempool;
  144. struct dm_io_client *io;
  145. struct dm_bufio_client *bufio;
  146. struct workqueue_struct *metadata_wq;
  147. struct superblock *sb;
  148. unsigned int journal_pages;
  149. unsigned int n_bitmap_blocks;
  150. struct page_list *journal;
  151. struct page_list *journal_io;
  152. struct page_list *journal_xor;
  153. struct page_list *recalc_bitmap;
  154. struct page_list *may_write_bitmap;
  155. struct bitmap_block_status *bbs;
  156. unsigned int bitmap_flush_interval;
  157. int synchronous_mode;
  158. struct bio_list synchronous_bios;
  159. struct delayed_work bitmap_flush_work;
  160. struct crypto_skcipher *journal_crypt;
  161. struct scatterlist **journal_scatterlist;
  162. struct scatterlist **journal_io_scatterlist;
  163. struct skcipher_request **sk_requests;
  164. struct crypto_shash *journal_mac;
  165. struct journal_node *journal_tree;
  166. struct rb_root journal_tree_root;
  167. sector_t provided_data_sectors;
  168. unsigned short journal_entry_size;
  169. unsigned char journal_entries_per_sector;
  170. unsigned char journal_section_entries;
  171. unsigned short journal_section_sectors;
  172. unsigned int journal_sections;
  173. unsigned int journal_entries;
  174. sector_t data_device_sectors;
  175. sector_t meta_device_sectors;
  176. unsigned int initial_sectors;
  177. unsigned int metadata_run;
  178. __s8 log2_metadata_run;
  179. __u8 log2_buffer_sectors;
  180. __u8 sectors_per_block;
  181. __u8 log2_blocks_per_bitmap_bit;
  182. unsigned char mode;
  183. int failed;
  184. struct crypto_shash *internal_hash;
  185. struct dm_target *ti;
  186. /* these variables are locked with endio_wait.lock */
  187. struct rb_root in_progress;
  188. struct list_head wait_list;
  189. wait_queue_head_t endio_wait;
  190. struct workqueue_struct *wait_wq;
  191. struct workqueue_struct *offload_wq;
  192. unsigned char commit_seq;
  193. commit_id_t commit_ids[N_COMMIT_IDS];
  194. unsigned int committed_section;
  195. unsigned int n_committed_sections;
  196. unsigned int uncommitted_section;
  197. unsigned int n_uncommitted_sections;
  198. unsigned int free_section;
  199. unsigned char free_section_entry;
  200. unsigned int free_sectors;
  201. unsigned int free_sectors_threshold;
  202. struct workqueue_struct *commit_wq;
  203. struct work_struct commit_work;
  204. struct workqueue_struct *writer_wq;
  205. struct work_struct writer_work;
  206. struct workqueue_struct *recalc_wq;
  207. struct work_struct recalc_work;
  208. u8 *recalc_buffer;
  209. u8 *recalc_tags;
  210. struct bio_list flush_bio_list;
  211. unsigned long autocommit_jiffies;
  212. struct timer_list autocommit_timer;
  213. unsigned int autocommit_msec;
  214. wait_queue_head_t copy_to_journal_wait;
  215. struct completion crypto_backoff;
  216. bool wrote_to_journal;
  217. bool journal_uptodate;
  218. bool just_formatted;
  219. bool recalculate_flag;
  220. bool reset_recalculate_flag;
  221. bool discard;
  222. bool fix_padding;
  223. bool fix_hmac;
  224. bool legacy_recalculate;
  225. struct alg_spec internal_hash_alg;
  226. struct alg_spec journal_crypt_alg;
  227. struct alg_spec journal_mac_alg;
  228. atomic64_t number_of_mismatches;
  229. struct notifier_block reboot_notifier;
  230. };
  231. struct dm_integrity_range {
  232. sector_t logical_sector;
  233. sector_t n_sectors;
  234. bool waiting;
  235. union {
  236. struct rb_node node;
  237. struct {
  238. struct task_struct *task;
  239. struct list_head wait_entry;
  240. };
  241. };
  242. };
  243. struct dm_integrity_io {
  244. struct work_struct work;
  245. struct dm_integrity_c *ic;
  246. enum req_op op;
  247. bool fua;
  248. struct dm_integrity_range range;
  249. sector_t metadata_block;
  250. unsigned int metadata_offset;
  251. atomic_t in_flight;
  252. blk_status_t bi_status;
  253. struct completion *completion;
  254. struct dm_bio_details bio_details;
  255. };
  256. struct journal_completion {
  257. struct dm_integrity_c *ic;
  258. atomic_t in_flight;
  259. struct completion comp;
  260. };
  261. struct journal_io {
  262. struct dm_integrity_range range;
  263. struct journal_completion *comp;
  264. };
  265. struct bitmap_block_status {
  266. struct work_struct work;
  267. struct dm_integrity_c *ic;
  268. unsigned int idx;
  269. unsigned long *bitmap;
  270. struct bio_list bio_queue;
  271. spinlock_t bio_queue_lock;
  272. };
  273. static struct kmem_cache *journal_io_cache;
  274. #define JOURNAL_IO_MEMPOOL 32
  275. #ifdef DEBUG_PRINT
  276. #define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
  277. static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
  278. {
  279. va_list args;
  280. va_start(args, msg);
  281. vprintk(msg, args);
  282. va_end(args);
  283. if (len)
  284. pr_cont(":");
  285. while (len) {
  286. pr_cont(" %02x", *bytes);
  287. bytes++;
  288. len--;
  289. }
  290. pr_cont("\n");
  291. }
  292. #define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
  293. #else
  294. #define DEBUG_print(x, ...) do { } while (0)
  295. #define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
  296. #endif
  297. static void dm_integrity_prepare(struct request *rq)
  298. {
  299. }
  300. static void dm_integrity_complete(struct request *rq, unsigned int nr_bytes)
  301. {
  302. }
  303. /*
  304. * DM Integrity profile, protection is performed layer above (dm-crypt)
  305. */
  306. static const struct blk_integrity_profile dm_integrity_profile = {
  307. .name = "DM-DIF-EXT-TAG",
  308. .generate_fn = NULL,
  309. .verify_fn = NULL,
  310. .prepare_fn = dm_integrity_prepare,
  311. .complete_fn = dm_integrity_complete,
  312. };
  313. static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);
  314. static void integrity_bio_wait(struct work_struct *w);
  315. static void dm_integrity_dtr(struct dm_target *ti);
  316. static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err)
  317. {
  318. if (err == -EILSEQ)
  319. atomic64_inc(&ic->number_of_mismatches);
  320. if (!cmpxchg(&ic->failed, 0, err))
  321. DMERR("Error on %s: %d", msg, err);
  322. }
  323. static int dm_integrity_failed(struct dm_integrity_c *ic)
  324. {
  325. return READ_ONCE(ic->failed);
  326. }
  327. static bool dm_integrity_disable_recalculate(struct dm_integrity_c *ic)
  328. {
  329. if (ic->legacy_recalculate)
  330. return false;
  331. if (!(ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_HMAC)) ?
  332. ic->internal_hash_alg.key || ic->journal_mac_alg.key :
  333. ic->internal_hash_alg.key && !ic->journal_mac_alg.key)
  334. return true;
  335. return false;
  336. }
  337. static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned int i,
  338. unsigned int j, unsigned char seq)
  339. {
  340. /*
  341. * Xor the number with section and sector, so that if a piece of
  342. * journal is written at wrong place, it is detected.
  343. */
  344. return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j);
  345. }
  346. static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector,
  347. sector_t *area, sector_t *offset)
  348. {
  349. if (!ic->meta_dev) {
  350. __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors;
  351. *area = data_sector >> log2_interleave_sectors;
  352. *offset = (unsigned int)data_sector & ((1U << log2_interleave_sectors) - 1);
  353. } else {
  354. *area = 0;
  355. *offset = data_sector;
  356. }
  357. }
  358. #define sector_to_block(ic, n) \
  359. do { \
  360. BUG_ON((n) & (unsigned int)((ic)->sectors_per_block - 1)); \
  361. (n) >>= (ic)->sb->log2_sectors_per_block; \
  362. } while (0)
  363. static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area,
  364. sector_t offset, unsigned int *metadata_offset)
  365. {
  366. __u64 ms;
  367. unsigned int mo;
  368. ms = area << ic->sb->log2_interleave_sectors;
  369. if (likely(ic->log2_metadata_run >= 0))
  370. ms += area << ic->log2_metadata_run;
  371. else
  372. ms += area * ic->metadata_run;
  373. ms >>= ic->log2_buffer_sectors;
  374. sector_to_block(ic, offset);
  375. if (likely(ic->log2_tag_size >= 0)) {
  376. ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size);
  377. mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
  378. } else {
  379. ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors);
  380. mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
  381. }
  382. *metadata_offset = mo;
  383. return ms;
  384. }
  385. static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset)
  386. {
  387. sector_t result;
  388. if (ic->meta_dev)
  389. return offset;
  390. result = area << ic->sb->log2_interleave_sectors;
  391. if (likely(ic->log2_metadata_run >= 0))
  392. result += (area + 1) << ic->log2_metadata_run;
  393. else
  394. result += (area + 1) * ic->metadata_run;
  395. result += (sector_t)ic->initial_sectors + offset;
  396. result += ic->start;
  397. return result;
  398. }
  399. static void wraparound_section(struct dm_integrity_c *ic, unsigned int *sec_ptr)
  400. {
  401. if (unlikely(*sec_ptr >= ic->journal_sections))
  402. *sec_ptr -= ic->journal_sections;
  403. }
  404. static void sb_set_version(struct dm_integrity_c *ic)
  405. {
  406. if (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_HMAC))
  407. ic->sb->version = SB_VERSION_5;
  408. else if (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING))
  409. ic->sb->version = SB_VERSION_4;
  410. else if (ic->mode == 'B' || ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP))
  411. ic->sb->version = SB_VERSION_3;
  412. else if (ic->meta_dev || ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
  413. ic->sb->version = SB_VERSION_2;
  414. else
  415. ic->sb->version = SB_VERSION_1;
  416. }
  417. static int sb_mac(struct dm_integrity_c *ic, bool wr)
  418. {
  419. SHASH_DESC_ON_STACK(desc, ic->journal_mac);
  420. int r;
  421. unsigned int size = crypto_shash_digestsize(ic->journal_mac);
  422. if (sizeof(struct superblock) + size > 1 << SECTOR_SHIFT) {
  423. dm_integrity_io_error(ic, "digest is too long", -EINVAL);
  424. return -EINVAL;
  425. }
  426. desc->tfm = ic->journal_mac;
  427. r = crypto_shash_init(desc);
  428. if (unlikely(r < 0)) {
  429. dm_integrity_io_error(ic, "crypto_shash_init", r);
  430. return r;
  431. }
  432. r = crypto_shash_update(desc, (__u8 *)ic->sb, (1 << SECTOR_SHIFT) - size);
  433. if (unlikely(r < 0)) {
  434. dm_integrity_io_error(ic, "crypto_shash_update", r);
  435. return r;
  436. }
  437. if (likely(wr)) {
  438. r = crypto_shash_final(desc, (__u8 *)ic->sb + (1 << SECTOR_SHIFT) - size);
  439. if (unlikely(r < 0)) {
  440. dm_integrity_io_error(ic, "crypto_shash_final", r);
  441. return r;
  442. }
  443. } else {
  444. __u8 result[HASH_MAX_DIGESTSIZE];
  445. r = crypto_shash_final(desc, result);
  446. if (unlikely(r < 0)) {
  447. dm_integrity_io_error(ic, "crypto_shash_final", r);
  448. return r;
  449. }
  450. if (memcmp((__u8 *)ic->sb + (1 << SECTOR_SHIFT) - size, result, size)) {
  451. dm_integrity_io_error(ic, "superblock mac", -EILSEQ);
  452. dm_audit_log_target(DM_MSG_PREFIX, "mac-superblock", ic->ti, 0);
  453. return -EILSEQ;
  454. }
  455. }
  456. return 0;
  457. }
  458. static int sync_rw_sb(struct dm_integrity_c *ic, blk_opf_t opf)
  459. {
  460. struct dm_io_request io_req;
  461. struct dm_io_region io_loc;
  462. const enum req_op op = opf & REQ_OP_MASK;
  463. int r;
  464. io_req.bi_opf = opf;
  465. io_req.mem.type = DM_IO_KMEM;
  466. io_req.mem.ptr.addr = ic->sb;
  467. io_req.notify.fn = NULL;
  468. io_req.client = ic->io;
  469. io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
  470. io_loc.sector = ic->start;
  471. io_loc.count = SB_SECTORS;
  472. if (op == REQ_OP_WRITE) {
  473. sb_set_version(ic);
  474. if (ic->journal_mac && ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_HMAC)) {
  475. r = sb_mac(ic, true);
  476. if (unlikely(r))
  477. return r;
  478. }
  479. }
  480. r = dm_io(&io_req, 1, &io_loc, NULL);
  481. if (unlikely(r))
  482. return r;
  483. if (op == REQ_OP_READ) {
  484. if (ic->mode != 'R' && ic->journal_mac && ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_HMAC)) {
  485. r = sb_mac(ic, false);
  486. if (unlikely(r))
  487. return r;
  488. }
  489. }
  490. return 0;
  491. }
  492. #define BITMAP_OP_TEST_ALL_SET 0
  493. #define BITMAP_OP_TEST_ALL_CLEAR 1
  494. #define BITMAP_OP_SET 2
  495. #define BITMAP_OP_CLEAR 3
  496. static bool block_bitmap_op(struct dm_integrity_c *ic, struct page_list *bitmap,
  497. sector_t sector, sector_t n_sectors, int mode)
  498. {
  499. unsigned long bit, end_bit, this_end_bit, page, end_page;
  500. unsigned long *data;
  501. if (unlikely(((sector | n_sectors) & ((1 << ic->sb->log2_sectors_per_block) - 1)) != 0)) {
  502. DMCRIT("invalid bitmap access (%llx,%llx,%d,%d,%d)",
  503. sector,
  504. n_sectors,
  505. ic->sb->log2_sectors_per_block,
  506. ic->log2_blocks_per_bitmap_bit,
  507. mode);
  508. BUG();
  509. }
  510. if (unlikely(!n_sectors))
  511. return true;
  512. bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
  513. end_bit = (sector + n_sectors - 1) >>
  514. (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
  515. page = bit / (PAGE_SIZE * 8);
  516. bit %= PAGE_SIZE * 8;
  517. end_page = end_bit / (PAGE_SIZE * 8);
  518. end_bit %= PAGE_SIZE * 8;
  519. repeat:
  520. if (page < end_page) {
  521. this_end_bit = PAGE_SIZE * 8 - 1;
  522. } else {
  523. this_end_bit = end_bit;
  524. }
  525. data = lowmem_page_address(bitmap[page].page);
  526. if (mode == BITMAP_OP_TEST_ALL_SET) {
  527. while (bit <= this_end_bit) {
  528. if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
  529. do {
  530. if (data[bit / BITS_PER_LONG] != -1)
  531. return false;
  532. bit += BITS_PER_LONG;
  533. } while (this_end_bit >= bit + BITS_PER_LONG - 1);
  534. continue;
  535. }
  536. if (!test_bit(bit, data))
  537. return false;
  538. bit++;
  539. }
  540. } else if (mode == BITMAP_OP_TEST_ALL_CLEAR) {
  541. while (bit <= this_end_bit) {
  542. if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
  543. do {
  544. if (data[bit / BITS_PER_LONG] != 0)
  545. return false;
  546. bit += BITS_PER_LONG;
  547. } while (this_end_bit >= bit + BITS_PER_LONG - 1);
  548. continue;
  549. }
  550. if (test_bit(bit, data))
  551. return false;
  552. bit++;
  553. }
  554. } else if (mode == BITMAP_OP_SET) {
  555. while (bit <= this_end_bit) {
  556. if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
  557. do {
  558. data[bit / BITS_PER_LONG] = -1;
  559. bit += BITS_PER_LONG;
  560. } while (this_end_bit >= bit + BITS_PER_LONG - 1);
  561. continue;
  562. }
  563. __set_bit(bit, data);
  564. bit++;
  565. }
  566. } else if (mode == BITMAP_OP_CLEAR) {
  567. if (!bit && this_end_bit == PAGE_SIZE * 8 - 1)
  568. clear_page(data);
  569. else while (bit <= this_end_bit) {
  570. if (!(bit % BITS_PER_LONG) && this_end_bit >= bit + BITS_PER_LONG - 1) {
  571. do {
  572. data[bit / BITS_PER_LONG] = 0;
  573. bit += BITS_PER_LONG;
  574. } while (this_end_bit >= bit + BITS_PER_LONG - 1);
  575. continue;
  576. }
  577. __clear_bit(bit, data);
  578. bit++;
  579. }
  580. } else {
  581. BUG();
  582. }
  583. if (unlikely(page < end_page)) {
  584. bit = 0;
  585. page++;
  586. goto repeat;
  587. }
  588. return true;
  589. }
  590. static void block_bitmap_copy(struct dm_integrity_c *ic, struct page_list *dst, struct page_list *src)
  591. {
  592. unsigned int n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
  593. unsigned int i;
  594. for (i = 0; i < n_bitmap_pages; i++) {
  595. unsigned long *dst_data = lowmem_page_address(dst[i].page);
  596. unsigned long *src_data = lowmem_page_address(src[i].page);
  597. copy_page(dst_data, src_data);
  598. }
  599. }
  600. static struct bitmap_block_status *sector_to_bitmap_block(struct dm_integrity_c *ic, sector_t sector)
  601. {
  602. unsigned int bit = sector >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
  603. unsigned int bitmap_block = bit / (BITMAP_BLOCK_SIZE * 8);
  604. BUG_ON(bitmap_block >= ic->n_bitmap_blocks);
  605. return &ic->bbs[bitmap_block];
  606. }
  607. static void access_journal_check(struct dm_integrity_c *ic, unsigned int section, unsigned int offset,
  608. bool e, const char *function)
  609. {
  610. #if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
  611. unsigned int limit = e ? ic->journal_section_entries : ic->journal_section_sectors;
  612. if (unlikely(section >= ic->journal_sections) ||
  613. unlikely(offset >= limit)) {
  614. DMCRIT("%s: invalid access at (%u,%u), limit (%u,%u)",
  615. function, section, offset, ic->journal_sections, limit);
  616. BUG();
  617. }
  618. #endif
  619. }
  620. static void page_list_location(struct dm_integrity_c *ic, unsigned int section, unsigned int offset,
  621. unsigned int *pl_index, unsigned int *pl_offset)
  622. {
  623. unsigned int sector;
  624. access_journal_check(ic, section, offset, false, "page_list_location");
  625. sector = section * ic->journal_section_sectors + offset;
  626. *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
  627. *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
  628. }
  629. static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl,
  630. unsigned int section, unsigned int offset, unsigned int *n_sectors)
  631. {
  632. unsigned int pl_index, pl_offset;
  633. char *va;
  634. page_list_location(ic, section, offset, &pl_index, &pl_offset);
  635. if (n_sectors)
  636. *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT;
  637. va = lowmem_page_address(pl[pl_index].page);
  638. return (struct journal_sector *)(va + pl_offset);
  639. }
  640. static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned int section, unsigned int offset)
  641. {
  642. return access_page_list(ic, ic->journal, section, offset, NULL);
  643. }
  644. static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned int section, unsigned int n)
  645. {
  646. unsigned int rel_sector, offset;
  647. struct journal_sector *js;
  648. access_journal_check(ic, section, n, true, "access_journal_entry");
  649. rel_sector = n % JOURNAL_BLOCK_SECTORS;
  650. offset = n / JOURNAL_BLOCK_SECTORS;
  651. js = access_journal(ic, section, rel_sector);
  652. return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size);
  653. }
  654. static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned int section, unsigned int n)
  655. {
  656. n <<= ic->sb->log2_sectors_per_block;
  657. n += JOURNAL_BLOCK_SECTORS;
  658. access_journal_check(ic, section, n, false, "access_journal_data");
  659. return access_journal(ic, section, n);
  660. }
  661. static void section_mac(struct dm_integrity_c *ic, unsigned int section, __u8 result[JOURNAL_MAC_SIZE])
  662. {
  663. SHASH_DESC_ON_STACK(desc, ic->journal_mac);
  664. int r;
  665. unsigned int j, size;
  666. desc->tfm = ic->journal_mac;
  667. r = crypto_shash_init(desc);
  668. if (unlikely(r < 0)) {
  669. dm_integrity_io_error(ic, "crypto_shash_init", r);
  670. goto err;
  671. }
  672. if (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_HMAC)) {
  673. __le64 section_le;
  674. r = crypto_shash_update(desc, (__u8 *)&ic->sb->salt, SALT_SIZE);
  675. if (unlikely(r < 0)) {
  676. dm_integrity_io_error(ic, "crypto_shash_update", r);
  677. goto err;
  678. }
  679. section_le = cpu_to_le64(section);
  680. r = crypto_shash_update(desc, (__u8 *)&section_le, sizeof section_le);
  681. if (unlikely(r < 0)) {
  682. dm_integrity_io_error(ic, "crypto_shash_update", r);
  683. goto err;
  684. }
  685. }
  686. for (j = 0; j < ic->journal_section_entries; j++) {
  687. struct journal_entry *je = access_journal_entry(ic, section, j);
  688. r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector);
  689. if (unlikely(r < 0)) {
  690. dm_integrity_io_error(ic, "crypto_shash_update", r);
  691. goto err;
  692. }
  693. }
  694. size = crypto_shash_digestsize(ic->journal_mac);
  695. if (likely(size <= JOURNAL_MAC_SIZE)) {
  696. r = crypto_shash_final(desc, result);
  697. if (unlikely(r < 0)) {
  698. dm_integrity_io_error(ic, "crypto_shash_final", r);
  699. goto err;
  700. }
  701. memset(result + size, 0, JOURNAL_MAC_SIZE - size);
  702. } else {
  703. __u8 digest[HASH_MAX_DIGESTSIZE];
  704. if (WARN_ON(size > sizeof(digest))) {
  705. dm_integrity_io_error(ic, "digest_size", -EINVAL);
  706. goto err;
  707. }
  708. r = crypto_shash_final(desc, digest);
  709. if (unlikely(r < 0)) {
  710. dm_integrity_io_error(ic, "crypto_shash_final", r);
  711. goto err;
  712. }
  713. memcpy(result, digest, JOURNAL_MAC_SIZE);
  714. }
  715. return;
  716. err:
  717. memset(result, 0, JOURNAL_MAC_SIZE);
  718. }
  719. static void rw_section_mac(struct dm_integrity_c *ic, unsigned int section, bool wr)
  720. {
  721. __u8 result[JOURNAL_MAC_SIZE];
  722. unsigned int j;
  723. if (!ic->journal_mac)
  724. return;
  725. section_mac(ic, section, result);
  726. for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) {
  727. struct journal_sector *js = access_journal(ic, section, j);
  728. if (likely(wr))
  729. memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
  730. else {
  731. if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR)) {
  732. dm_integrity_io_error(ic, "journal mac", -EILSEQ);
  733. dm_audit_log_target(DM_MSG_PREFIX, "mac-journal", ic->ti, 0);
  734. }
  735. }
  736. }
  737. }
  738. static void complete_journal_op(void *context)
  739. {
  740. struct journal_completion *comp = context;
  741. BUG_ON(!atomic_read(&comp->in_flight));
  742. if (likely(atomic_dec_and_test(&comp->in_flight)))
  743. complete(&comp->comp);
  744. }
  745. static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned int section,
  746. unsigned int n_sections, struct journal_completion *comp)
  747. {
  748. struct async_submit_ctl submit;
  749. size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT;
  750. unsigned int pl_index, pl_offset, section_index;
  751. struct page_list *source_pl, *target_pl;
  752. if (likely(encrypt)) {
  753. source_pl = ic->journal;
  754. target_pl = ic->journal_io;
  755. } else {
  756. source_pl = ic->journal_io;
  757. target_pl = ic->journal;
  758. }
  759. page_list_location(ic, section, 0, &pl_index, &pl_offset);
  760. atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight);
  761. init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL);
  762. section_index = pl_index;
  763. do {
  764. size_t this_step;
  765. struct page *src_pages[2];
  766. struct page *dst_page;
  767. while (unlikely(pl_index == section_index)) {
  768. unsigned int dummy;
  769. if (likely(encrypt))
  770. rw_section_mac(ic, section, true);
  771. section++;
  772. n_sections--;
  773. if (!n_sections)
  774. break;
  775. page_list_location(ic, section, 0, &section_index, &dummy);
  776. }
  777. this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset);
  778. dst_page = target_pl[pl_index].page;
  779. src_pages[0] = source_pl[pl_index].page;
  780. src_pages[1] = ic->journal_xor[pl_index].page;
  781. async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit);
  782. pl_index++;
  783. pl_offset = 0;
  784. n_bytes -= this_step;
  785. } while (n_bytes);
  786. BUG_ON(n_sections);
  787. async_tx_issue_pending_all();
  788. }
  789. static void complete_journal_encrypt(struct crypto_async_request *req, int err)
  790. {
  791. struct journal_completion *comp = req->data;
  792. if (unlikely(err)) {
  793. if (likely(err == -EINPROGRESS)) {
  794. complete(&comp->ic->crypto_backoff);
  795. return;
  796. }
  797. dm_integrity_io_error(comp->ic, "asynchronous encrypt", err);
  798. }
  799. complete_journal_op(comp);
  800. }
  801. static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp)
  802. {
  803. int r;
  804. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  805. complete_journal_encrypt, comp);
  806. if (likely(encrypt))
  807. r = crypto_skcipher_encrypt(req);
  808. else
  809. r = crypto_skcipher_decrypt(req);
  810. if (likely(!r))
  811. return false;
  812. if (likely(r == -EINPROGRESS))
  813. return true;
  814. if (likely(r == -EBUSY)) {
  815. wait_for_completion(&comp->ic->crypto_backoff);
  816. reinit_completion(&comp->ic->crypto_backoff);
  817. return true;
  818. }
  819. dm_integrity_io_error(comp->ic, "encrypt", r);
  820. return false;
  821. }
  822. static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned int section,
  823. unsigned int n_sections, struct journal_completion *comp)
  824. {
  825. struct scatterlist **source_sg;
  826. struct scatterlist **target_sg;
  827. atomic_add(2, &comp->in_flight);
  828. if (likely(encrypt)) {
  829. source_sg = ic->journal_scatterlist;
  830. target_sg = ic->journal_io_scatterlist;
  831. } else {
  832. source_sg = ic->journal_io_scatterlist;
  833. target_sg = ic->journal_scatterlist;
  834. }
  835. do {
  836. struct skcipher_request *req;
  837. unsigned int ivsize;
  838. char *iv;
  839. if (likely(encrypt))
  840. rw_section_mac(ic, section, true);
  841. req = ic->sk_requests[section];
  842. ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
  843. iv = req->iv;
  844. memcpy(iv, iv + ivsize, ivsize);
  845. req->src = source_sg[section];
  846. req->dst = target_sg[section];
  847. if (unlikely(do_crypt(encrypt, req, comp)))
  848. atomic_inc(&comp->in_flight);
  849. section++;
  850. n_sections--;
  851. } while (n_sections);
  852. atomic_dec(&comp->in_flight);
  853. complete_journal_op(comp);
  854. }
  855. static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned int section,
  856. unsigned int n_sections, struct journal_completion *comp)
  857. {
  858. if (ic->journal_xor)
  859. return xor_journal(ic, encrypt, section, n_sections, comp);
  860. else
  861. return crypt_journal(ic, encrypt, section, n_sections, comp);
  862. }
  863. static void complete_journal_io(unsigned long error, void *context)
  864. {
  865. struct journal_completion *comp = context;
  866. if (unlikely(error != 0))
  867. dm_integrity_io_error(comp->ic, "writing journal", -EIO);
  868. complete_journal_op(comp);
  869. }
  870. static void rw_journal_sectors(struct dm_integrity_c *ic, blk_opf_t opf,
  871. unsigned int sector, unsigned int n_sectors,
  872. struct journal_completion *comp)
  873. {
  874. struct dm_io_request io_req;
  875. struct dm_io_region io_loc;
  876. unsigned int pl_index, pl_offset;
  877. int r;
  878. if (unlikely(dm_integrity_failed(ic))) {
  879. if (comp)
  880. complete_journal_io(-1UL, comp);
  881. return;
  882. }
  883. pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
  884. pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
  885. io_req.bi_opf = opf;
  886. io_req.mem.type = DM_IO_PAGE_LIST;
  887. if (ic->journal_io)
  888. io_req.mem.ptr.pl = &ic->journal_io[pl_index];
  889. else
  890. io_req.mem.ptr.pl = &ic->journal[pl_index];
  891. io_req.mem.offset = pl_offset;
  892. if (likely(comp != NULL)) {
  893. io_req.notify.fn = complete_journal_io;
  894. io_req.notify.context = comp;
  895. } else {
  896. io_req.notify.fn = NULL;
  897. }
  898. io_req.client = ic->io;
  899. io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
  900. io_loc.sector = ic->start + SB_SECTORS + sector;
  901. io_loc.count = n_sectors;
  902. r = dm_io(&io_req, 1, &io_loc, NULL);
  903. if (unlikely(r)) {
  904. dm_integrity_io_error(ic, (opf & REQ_OP_MASK) == REQ_OP_READ ?
  905. "reading journal" : "writing journal", r);
  906. if (comp) {
  907. WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
  908. complete_journal_io(-1UL, comp);
  909. }
  910. }
  911. }
  912. static void rw_journal(struct dm_integrity_c *ic, blk_opf_t opf,
  913. unsigned int section, unsigned int n_sections,
  914. struct journal_completion *comp)
  915. {
  916. unsigned int sector, n_sectors;
  917. sector = section * ic->journal_section_sectors;
  918. n_sectors = n_sections * ic->journal_section_sectors;
  919. rw_journal_sectors(ic, opf, sector, n_sectors, comp);
  920. }
  921. static void write_journal(struct dm_integrity_c *ic, unsigned int commit_start, unsigned int commit_sections)
  922. {
  923. struct journal_completion io_comp;
  924. struct journal_completion crypt_comp_1;
  925. struct journal_completion crypt_comp_2;
  926. unsigned int i;
  927. io_comp.ic = ic;
  928. init_completion(&io_comp.comp);
  929. if (commit_start + commit_sections <= ic->journal_sections) {
  930. io_comp.in_flight = (atomic_t)ATOMIC_INIT(1);
  931. if (ic->journal_io) {
  932. crypt_comp_1.ic = ic;
  933. init_completion(&crypt_comp_1.comp);
  934. crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
  935. encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1);
  936. wait_for_completion_io(&crypt_comp_1.comp);
  937. } else {
  938. for (i = 0; i < commit_sections; i++)
  939. rw_section_mac(ic, commit_start + i, true);
  940. }
  941. rw_journal(ic, REQ_OP_WRITE | REQ_FUA | REQ_SYNC, commit_start,
  942. commit_sections, &io_comp);
  943. } else {
  944. unsigned int to_end;
  945. io_comp.in_flight = (atomic_t)ATOMIC_INIT(2);
  946. to_end = ic->journal_sections - commit_start;
  947. if (ic->journal_io) {
  948. crypt_comp_1.ic = ic;
  949. init_completion(&crypt_comp_1.comp);
  950. crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
  951. encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1);
  952. if (try_wait_for_completion(&crypt_comp_1.comp)) {
  953. rw_journal(ic, REQ_OP_WRITE | REQ_FUA,
  954. commit_start, to_end, &io_comp);
  955. reinit_completion(&crypt_comp_1.comp);
  956. crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
  957. encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1);
  958. wait_for_completion_io(&crypt_comp_1.comp);
  959. } else {
  960. crypt_comp_2.ic = ic;
  961. init_completion(&crypt_comp_2.comp);
  962. crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0);
  963. encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2);
  964. wait_for_completion_io(&crypt_comp_1.comp);
  965. rw_journal(ic, REQ_OP_WRITE | REQ_FUA, commit_start, to_end, &io_comp);
  966. wait_for_completion_io(&crypt_comp_2.comp);
  967. }
  968. } else {
  969. for (i = 0; i < to_end; i++)
  970. rw_section_mac(ic, commit_start + i, true);
  971. rw_journal(ic, REQ_OP_WRITE | REQ_FUA, commit_start, to_end, &io_comp);
  972. for (i = 0; i < commit_sections - to_end; i++)
  973. rw_section_mac(ic, i, true);
  974. }
  975. rw_journal(ic, REQ_OP_WRITE | REQ_FUA, 0, commit_sections - to_end, &io_comp);
  976. }
  977. wait_for_completion_io(&io_comp.comp);
  978. }
  979. static void copy_from_journal(struct dm_integrity_c *ic, unsigned int section, unsigned int offset,
  980. unsigned int n_sectors, sector_t target, io_notify_fn fn, void *data)
  981. {
  982. struct dm_io_request io_req;
  983. struct dm_io_region io_loc;
  984. int r;
  985. unsigned int sector, pl_index, pl_offset;
  986. BUG_ON((target | n_sectors | offset) & (unsigned int)(ic->sectors_per_block - 1));
  987. if (unlikely(dm_integrity_failed(ic))) {
  988. fn(-1UL, data);
  989. return;
  990. }
  991. sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
  992. pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
  993. pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
  994. io_req.bi_opf = REQ_OP_WRITE;
  995. io_req.mem.type = DM_IO_PAGE_LIST;
  996. io_req.mem.ptr.pl = &ic->journal[pl_index];
  997. io_req.mem.offset = pl_offset;
  998. io_req.notify.fn = fn;
  999. io_req.notify.context = data;
  1000. io_req.client = ic->io;
  1001. io_loc.bdev = ic->dev->bdev;
  1002. io_loc.sector = target;
  1003. io_loc.count = n_sectors;
  1004. r = dm_io(&io_req, 1, &io_loc, NULL);
  1005. if (unlikely(r)) {
  1006. WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
  1007. fn(-1UL, data);
  1008. }
  1009. }
  1010. static bool ranges_overlap(struct dm_integrity_range *range1, struct dm_integrity_range *range2)
  1011. {
  1012. return range1->logical_sector < range2->logical_sector + range2->n_sectors &&
  1013. range1->logical_sector + range1->n_sectors > range2->logical_sector;
  1014. }
  1015. static bool add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range, bool check_waiting)
  1016. {
  1017. struct rb_node **n = &ic->in_progress.rb_node;
  1018. struct rb_node *parent;
  1019. BUG_ON((new_range->logical_sector | new_range->n_sectors) & (unsigned int)(ic->sectors_per_block - 1));
  1020. if (likely(check_waiting)) {
  1021. struct dm_integrity_range *range;
  1022. list_for_each_entry(range, &ic->wait_list, wait_entry) {
  1023. if (unlikely(ranges_overlap(range, new_range)))
  1024. return false;
  1025. }
  1026. }
  1027. parent = NULL;
  1028. while (*n) {
  1029. struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node);
  1030. parent = *n;
  1031. if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) {
  1032. n = &range->node.rb_left;
  1033. } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) {
  1034. n = &range->node.rb_right;
  1035. } else {
  1036. return false;
  1037. }
  1038. }
  1039. rb_link_node(&new_range->node, parent, n);
  1040. rb_insert_color(&new_range->node, &ic->in_progress);
  1041. return true;
  1042. }
  1043. static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range)
  1044. {
  1045. rb_erase(&range->node, &ic->in_progress);
  1046. while (unlikely(!list_empty(&ic->wait_list))) {
  1047. struct dm_integrity_range *last_range =
  1048. list_first_entry(&ic->wait_list, struct dm_integrity_range, wait_entry);
  1049. struct task_struct *last_range_task;
  1050. last_range_task = last_range->task;
  1051. list_del(&last_range->wait_entry);
  1052. if (!add_new_range(ic, last_range, false)) {
  1053. last_range->task = last_range_task;
  1054. list_add(&last_range->wait_entry, &ic->wait_list);
  1055. break;
  1056. }
  1057. last_range->waiting = false;
  1058. wake_up_process(last_range_task);
  1059. }
  1060. }
  1061. static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range)
  1062. {
  1063. unsigned long flags;
  1064. spin_lock_irqsave(&ic->endio_wait.lock, flags);
  1065. remove_range_unlocked(ic, range);
  1066. spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
  1067. }
  1068. static void wait_and_add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
  1069. {
  1070. new_range->waiting = true;
  1071. list_add_tail(&new_range->wait_entry, &ic->wait_list);
  1072. new_range->task = current;
  1073. do {
  1074. __set_current_state(TASK_UNINTERRUPTIBLE);
  1075. spin_unlock_irq(&ic->endio_wait.lock);
  1076. io_schedule();
  1077. spin_lock_irq(&ic->endio_wait.lock);
  1078. } while (unlikely(new_range->waiting));
  1079. }
  1080. static void add_new_range_and_wait(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
  1081. {
  1082. if (unlikely(!add_new_range(ic, new_range, true)))
  1083. wait_and_add_new_range(ic, new_range);
  1084. }
  1085. static void init_journal_node(struct journal_node *node)
  1086. {
  1087. RB_CLEAR_NODE(&node->node);
  1088. node->sector = (sector_t)-1;
  1089. }
  1090. static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector)
  1091. {
  1092. struct rb_node **link;
  1093. struct rb_node *parent;
  1094. node->sector = sector;
  1095. BUG_ON(!RB_EMPTY_NODE(&node->node));
  1096. link = &ic->journal_tree_root.rb_node;
  1097. parent = NULL;
  1098. while (*link) {
  1099. struct journal_node *j;
  1100. parent = *link;
  1101. j = container_of(parent, struct journal_node, node);
  1102. if (sector < j->sector)
  1103. link = &j->node.rb_left;
  1104. else
  1105. link = &j->node.rb_right;
  1106. }
  1107. rb_link_node(&node->node, parent, link);
  1108. rb_insert_color(&node->node, &ic->journal_tree_root);
  1109. }
  1110. static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node)
  1111. {
  1112. BUG_ON(RB_EMPTY_NODE(&node->node));
  1113. rb_erase(&node->node, &ic->journal_tree_root);
  1114. init_journal_node(node);
  1115. }
  1116. #define NOT_FOUND (-1U)
  1117. static unsigned int find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector)
  1118. {
  1119. struct rb_node *n = ic->journal_tree_root.rb_node;
  1120. unsigned int found = NOT_FOUND;
  1121. *next_sector = (sector_t)-1;
  1122. while (n) {
  1123. struct journal_node *j = container_of(n, struct journal_node, node);
  1124. if (sector == j->sector) {
  1125. found = j - ic->journal_tree;
  1126. }
  1127. if (sector < j->sector) {
  1128. *next_sector = j->sector;
  1129. n = j->node.rb_left;
  1130. } else {
  1131. n = j->node.rb_right;
  1132. }
  1133. }
  1134. return found;
  1135. }
  1136. static bool test_journal_node(struct dm_integrity_c *ic, unsigned int pos, sector_t sector)
  1137. {
  1138. struct journal_node *node, *next_node;
  1139. struct rb_node *next;
  1140. if (unlikely(pos >= ic->journal_entries))
  1141. return false;
  1142. node = &ic->journal_tree[pos];
  1143. if (unlikely(RB_EMPTY_NODE(&node->node)))
  1144. return false;
  1145. if (unlikely(node->sector != sector))
  1146. return false;
  1147. next = rb_next(&node->node);
  1148. if (unlikely(!next))
  1149. return true;
  1150. next_node = container_of(next, struct journal_node, node);
  1151. return next_node->sector != sector;
  1152. }
  1153. static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node)
  1154. {
  1155. struct rb_node *next;
  1156. struct journal_node *next_node;
  1157. unsigned int next_section;
  1158. BUG_ON(RB_EMPTY_NODE(&node->node));
  1159. next = rb_next(&node->node);
  1160. if (unlikely(!next))
  1161. return false;
  1162. next_node = container_of(next, struct journal_node, node);
  1163. if (next_node->sector != node->sector)
  1164. return false;
  1165. next_section = (unsigned int)(next_node - ic->journal_tree) / ic->journal_section_entries;
  1166. if (next_section >= ic->committed_section &&
  1167. next_section < ic->committed_section + ic->n_committed_sections)
  1168. return true;
  1169. if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections)
  1170. return true;
  1171. return false;
  1172. }
  1173. #define TAG_READ 0
  1174. #define TAG_WRITE 1
  1175. #define TAG_CMP 2
  1176. static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block,
  1177. unsigned int *metadata_offset, unsigned int total_size, int op)
  1178. {
  1179. #define MAY_BE_FILLER 1
  1180. #define MAY_BE_HASH 2
  1181. unsigned int hash_offset = 0;
  1182. unsigned int may_be = MAY_BE_HASH | (ic->discard ? MAY_BE_FILLER : 0);
  1183. do {
  1184. unsigned char *data, *dp;
  1185. struct dm_buffer *b;
  1186. unsigned int to_copy;
  1187. int r;
  1188. r = dm_integrity_failed(ic);
  1189. if (unlikely(r))
  1190. return r;
  1191. data = dm_bufio_read(ic->bufio, *metadata_block, &b);
  1192. if (IS_ERR(data))
  1193. return PTR_ERR(data);
  1194. to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size);
  1195. dp = data + *metadata_offset;
  1196. if (op == TAG_READ) {
  1197. memcpy(tag, dp, to_copy);
  1198. } else if (op == TAG_WRITE) {
  1199. if (memcmp(dp, tag, to_copy)) {
  1200. memcpy(dp, tag, to_copy);
  1201. dm_bufio_mark_partial_buffer_dirty(b, *metadata_offset, *metadata_offset + to_copy);
  1202. }
  1203. } else {
  1204. /* e.g.: op == TAG_CMP */
  1205. if (likely(is_power_of_2(ic->tag_size))) {
  1206. if (unlikely(memcmp(dp, tag, to_copy)))
  1207. if (unlikely(!ic->discard) ||
  1208. unlikely(memchr_inv(dp, DISCARD_FILLER, to_copy) != NULL)) {
  1209. goto thorough_test;
  1210. }
  1211. } else {
  1212. unsigned int i, ts;
  1213. thorough_test:
  1214. ts = total_size;
  1215. for (i = 0; i < to_copy; i++, ts--) {
  1216. if (unlikely(dp[i] != tag[i]))
  1217. may_be &= ~MAY_BE_HASH;
  1218. if (likely(dp[i] != DISCARD_FILLER))
  1219. may_be &= ~MAY_BE_FILLER;
  1220. hash_offset++;
  1221. if (unlikely(hash_offset == ic->tag_size)) {
  1222. if (unlikely(!may_be)) {
  1223. dm_bufio_release(b);
  1224. return ts;
  1225. }
  1226. hash_offset = 0;
  1227. may_be = MAY_BE_HASH | (ic->discard ? MAY_BE_FILLER : 0);
  1228. }
  1229. }
  1230. }
  1231. }
  1232. dm_bufio_release(b);
  1233. tag += to_copy;
  1234. *metadata_offset += to_copy;
  1235. if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) {
  1236. (*metadata_block)++;
  1237. *metadata_offset = 0;
  1238. }
  1239. if (unlikely(!is_power_of_2(ic->tag_size))) {
  1240. hash_offset = (hash_offset + to_copy) % ic->tag_size;
  1241. }
  1242. total_size -= to_copy;
  1243. } while (unlikely(total_size));
  1244. return 0;
  1245. #undef MAY_BE_FILLER
  1246. #undef MAY_BE_HASH
  1247. }
  1248. struct flush_request {
  1249. struct dm_io_request io_req;
  1250. struct dm_io_region io_reg;
  1251. struct dm_integrity_c *ic;
  1252. struct completion comp;
  1253. };
  1254. static void flush_notify(unsigned long error, void *fr_)
  1255. {
  1256. struct flush_request *fr = fr_;
  1257. if (unlikely(error != 0))
  1258. dm_integrity_io_error(fr->ic, "flushing disk cache", -EIO);
  1259. complete(&fr->comp);
  1260. }
  1261. static void dm_integrity_flush_buffers(struct dm_integrity_c *ic, bool flush_data)
  1262. {
  1263. int r;
  1264. struct flush_request fr;
  1265. if (!ic->meta_dev)
  1266. flush_data = false;
  1267. if (flush_data) {
  1268. fr.io_req.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC,
  1269. fr.io_req.mem.type = DM_IO_KMEM,
  1270. fr.io_req.mem.ptr.addr = NULL,
  1271. fr.io_req.notify.fn = flush_notify,
  1272. fr.io_req.notify.context = &fr;
  1273. fr.io_req.client = dm_bufio_get_dm_io_client(ic->bufio),
  1274. fr.io_reg.bdev = ic->dev->bdev,
  1275. fr.io_reg.sector = 0,
  1276. fr.io_reg.count = 0,
  1277. fr.ic = ic;
  1278. init_completion(&fr.comp);
  1279. r = dm_io(&fr.io_req, 1, &fr.io_reg, NULL);
  1280. BUG_ON(r);
  1281. }
  1282. r = dm_bufio_write_dirty_buffers(ic->bufio);
  1283. if (unlikely(r))
  1284. dm_integrity_io_error(ic, "writing tags", r);
  1285. if (flush_data)
  1286. wait_for_completion(&fr.comp);
  1287. }
  1288. static void sleep_on_endio_wait(struct dm_integrity_c *ic)
  1289. {
  1290. DECLARE_WAITQUEUE(wait, current);
  1291. __add_wait_queue(&ic->endio_wait, &wait);
  1292. __set_current_state(TASK_UNINTERRUPTIBLE);
  1293. spin_unlock_irq(&ic->endio_wait.lock);
  1294. io_schedule();
  1295. spin_lock_irq(&ic->endio_wait.lock);
  1296. __remove_wait_queue(&ic->endio_wait, &wait);
  1297. }
  1298. static void autocommit_fn(struct timer_list *t)
  1299. {
  1300. struct dm_integrity_c *ic = from_timer(ic, t, autocommit_timer);
  1301. if (likely(!dm_integrity_failed(ic)))
  1302. queue_work(ic->commit_wq, &ic->commit_work);
  1303. }
  1304. static void schedule_autocommit(struct dm_integrity_c *ic)
  1305. {
  1306. if (!timer_pending(&ic->autocommit_timer))
  1307. mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies);
  1308. }
  1309. static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
  1310. {
  1311. struct bio *bio;
  1312. unsigned long flags;
  1313. spin_lock_irqsave(&ic->endio_wait.lock, flags);
  1314. bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  1315. bio_list_add(&ic->flush_bio_list, bio);
  1316. spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
  1317. queue_work(ic->commit_wq, &ic->commit_work);
  1318. }
  1319. static void do_endio(struct dm_integrity_c *ic, struct bio *bio)
  1320. {
  1321. int r = dm_integrity_failed(ic);
  1322. if (unlikely(r) && !bio->bi_status)
  1323. bio->bi_status = errno_to_blk_status(r);
  1324. if (unlikely(ic->synchronous_mode) && bio_op(bio) == REQ_OP_WRITE) {
  1325. unsigned long flags;
  1326. spin_lock_irqsave(&ic->endio_wait.lock, flags);
  1327. bio_list_add(&ic->synchronous_bios, bio);
  1328. queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
  1329. spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
  1330. return;
  1331. }
  1332. bio_endio(bio);
  1333. }
  1334. static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
  1335. {
  1336. struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  1337. if (unlikely(dio->fua) && likely(!bio->bi_status) && likely(!dm_integrity_failed(ic)))
  1338. submit_flush_bio(ic, dio);
  1339. else
  1340. do_endio(ic, bio);
  1341. }
  1342. static void dec_in_flight(struct dm_integrity_io *dio)
  1343. {
  1344. if (atomic_dec_and_test(&dio->in_flight)) {
  1345. struct dm_integrity_c *ic = dio->ic;
  1346. struct bio *bio;
  1347. remove_range(ic, &dio->range);
  1348. if (dio->op == REQ_OP_WRITE || unlikely(dio->op == REQ_OP_DISCARD))
  1349. schedule_autocommit(ic);
  1350. bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  1351. if (unlikely(dio->bi_status) && !bio->bi_status)
  1352. bio->bi_status = dio->bi_status;
  1353. if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
  1354. dio->range.logical_sector += dio->range.n_sectors;
  1355. bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
  1356. INIT_WORK(&dio->work, integrity_bio_wait);
  1357. queue_work(ic->offload_wq, &dio->work);
  1358. return;
  1359. }
  1360. do_endio_flush(ic, dio);
  1361. }
  1362. }
  1363. static void integrity_end_io(struct bio *bio)
  1364. {
  1365. struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
  1366. dm_bio_restore(&dio->bio_details, bio);
  1367. if (bio->bi_integrity)
  1368. bio->bi_opf |= REQ_INTEGRITY;
  1369. if (dio->completion)
  1370. complete(dio->completion);
  1371. dec_in_flight(dio);
  1372. }
  1373. static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector,
  1374. const char *data, char *result)
  1375. {
  1376. __le64 sector_le = cpu_to_le64(sector);
  1377. SHASH_DESC_ON_STACK(req, ic->internal_hash);
  1378. int r;
  1379. unsigned int digest_size;
  1380. req->tfm = ic->internal_hash;
  1381. r = crypto_shash_init(req);
  1382. if (unlikely(r < 0)) {
  1383. dm_integrity_io_error(ic, "crypto_shash_init", r);
  1384. goto failed;
  1385. }
  1386. if (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_HMAC)) {
  1387. r = crypto_shash_update(req, (__u8 *)&ic->sb->salt, SALT_SIZE);
  1388. if (unlikely(r < 0)) {
  1389. dm_integrity_io_error(ic, "crypto_shash_update", r);
  1390. goto failed;
  1391. }
  1392. }
  1393. r = crypto_shash_update(req, (const __u8 *)&sector_le, sizeof sector_le);
  1394. if (unlikely(r < 0)) {
  1395. dm_integrity_io_error(ic, "crypto_shash_update", r);
  1396. goto failed;
  1397. }
  1398. r = crypto_shash_update(req, data, ic->sectors_per_block << SECTOR_SHIFT);
  1399. if (unlikely(r < 0)) {
  1400. dm_integrity_io_error(ic, "crypto_shash_update", r);
  1401. goto failed;
  1402. }
  1403. r = crypto_shash_final(req, result);
  1404. if (unlikely(r < 0)) {
  1405. dm_integrity_io_error(ic, "crypto_shash_final", r);
  1406. goto failed;
  1407. }
  1408. digest_size = crypto_shash_digestsize(ic->internal_hash);
  1409. if (unlikely(digest_size < ic->tag_size))
  1410. memset(result + digest_size, 0, ic->tag_size - digest_size);
  1411. return;
  1412. failed:
  1413. /* this shouldn't happen anyway, the hash functions have no reason to fail */
  1414. get_random_bytes(result, ic->tag_size);
  1415. }
  1416. static void integrity_metadata(struct work_struct *w)
  1417. {
  1418. struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
  1419. struct dm_integrity_c *ic = dio->ic;
  1420. int r;
  1421. if (ic->internal_hash) {
  1422. struct bvec_iter iter;
  1423. struct bio_vec bv;
  1424. unsigned int digest_size = crypto_shash_digestsize(ic->internal_hash);
  1425. struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  1426. char *checksums;
  1427. unsigned int extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0;
  1428. char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
  1429. sector_t sector;
  1430. unsigned int sectors_to_process;
  1431. if (unlikely(ic->mode == 'R'))
  1432. goto skip_io;
  1433. if (likely(dio->op != REQ_OP_DISCARD))
  1434. checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space,
  1435. GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
  1436. else
  1437. checksums = kmalloc(PAGE_SIZE, GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
  1438. if (!checksums) {
  1439. checksums = checksums_onstack;
  1440. if (WARN_ON(extra_space &&
  1441. digest_size > sizeof(checksums_onstack))) {
  1442. r = -EINVAL;
  1443. goto error;
  1444. }
  1445. }
  1446. if (unlikely(dio->op == REQ_OP_DISCARD)) {
  1447. unsigned int bi_size = dio->bio_details.bi_iter.bi_size;
  1448. unsigned int max_size = likely(checksums != checksums_onstack) ? PAGE_SIZE : HASH_MAX_DIGESTSIZE;
  1449. unsigned int max_blocks = max_size / ic->tag_size;
  1450. memset(checksums, DISCARD_FILLER, max_size);
  1451. while (bi_size) {
  1452. unsigned int this_step_blocks = bi_size >> (SECTOR_SHIFT + ic->sb->log2_sectors_per_block);
  1453. this_step_blocks = min(this_step_blocks, max_blocks);
  1454. r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
  1455. this_step_blocks * ic->tag_size, TAG_WRITE);
  1456. if (unlikely(r)) {
  1457. if (likely(checksums != checksums_onstack))
  1458. kfree(checksums);
  1459. goto error;
  1460. }
  1461. bi_size -= this_step_blocks << (SECTOR_SHIFT + ic->sb->log2_sectors_per_block);
  1462. }
  1463. if (likely(checksums != checksums_onstack))
  1464. kfree(checksums);
  1465. goto skip_io;
  1466. }
  1467. sector = dio->range.logical_sector;
  1468. sectors_to_process = dio->range.n_sectors;
  1469. __bio_for_each_segment(bv, bio, iter, dio->bio_details.bi_iter) {
  1470. unsigned int pos;
  1471. char *mem, *checksums_ptr;
  1472. again:
  1473. mem = bvec_kmap_local(&bv);
  1474. pos = 0;
  1475. checksums_ptr = checksums;
  1476. do {
  1477. integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
  1478. checksums_ptr += ic->tag_size;
  1479. sectors_to_process -= ic->sectors_per_block;
  1480. pos += ic->sectors_per_block << SECTOR_SHIFT;
  1481. sector += ic->sectors_per_block;
  1482. } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
  1483. kunmap_local(mem);
  1484. r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
  1485. checksums_ptr - checksums, dio->op == REQ_OP_READ ? TAG_CMP : TAG_WRITE);
  1486. if (unlikely(r)) {
  1487. if (r > 0) {
  1488. sector_t s;
  1489. s = sector - ((r + ic->tag_size - 1) / ic->tag_size);
  1490. DMERR_LIMIT("%pg: Checksum failed at sector 0x%llx",
  1491. bio->bi_bdev, s);
  1492. r = -EILSEQ;
  1493. atomic64_inc(&ic->number_of_mismatches);
  1494. dm_audit_log_bio(DM_MSG_PREFIX, "integrity-checksum",
  1495. bio, s, 0);
  1496. }
  1497. if (likely(checksums != checksums_onstack))
  1498. kfree(checksums);
  1499. goto error;
  1500. }
  1501. if (!sectors_to_process)
  1502. break;
  1503. if (unlikely(pos < bv.bv_len)) {
  1504. bv.bv_offset += pos;
  1505. bv.bv_len -= pos;
  1506. goto again;
  1507. }
  1508. }
  1509. if (likely(checksums != checksums_onstack))
  1510. kfree(checksums);
  1511. } else {
  1512. struct bio_integrity_payload *bip = dio->bio_details.bi_integrity;
  1513. if (bip) {
  1514. struct bio_vec biv;
  1515. struct bvec_iter iter;
  1516. unsigned int data_to_process = dio->range.n_sectors;
  1517. sector_to_block(ic, data_to_process);
  1518. data_to_process *= ic->tag_size;
  1519. bip_for_each_vec(biv, bip, iter) {
  1520. unsigned char *tag;
  1521. unsigned int this_len;
  1522. BUG_ON(PageHighMem(biv.bv_page));
  1523. tag = bvec_virt(&biv);
  1524. this_len = min(biv.bv_len, data_to_process);
  1525. r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
  1526. this_len, dio->op == REQ_OP_READ ? TAG_READ : TAG_WRITE);
  1527. if (unlikely(r))
  1528. goto error;
  1529. data_to_process -= this_len;
  1530. if (!data_to_process)
  1531. break;
  1532. }
  1533. }
  1534. }
  1535. skip_io:
  1536. dec_in_flight(dio);
  1537. return;
  1538. error:
  1539. dio->bi_status = errno_to_blk_status(r);
  1540. dec_in_flight(dio);
  1541. }
  1542. static int dm_integrity_map(struct dm_target *ti, struct bio *bio)
  1543. {
  1544. struct dm_integrity_c *ic = ti->private;
  1545. struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
  1546. struct bio_integrity_payload *bip;
  1547. sector_t area, offset;
  1548. dio->ic = ic;
  1549. dio->bi_status = 0;
  1550. dio->op = bio_op(bio);
  1551. if (unlikely(dio->op == REQ_OP_DISCARD)) {
  1552. if (ti->max_io_len) {
  1553. sector_t sec = dm_target_offset(ti, bio->bi_iter.bi_sector);
  1554. unsigned int log2_max_io_len = __fls(ti->max_io_len);
  1555. sector_t start_boundary = sec >> log2_max_io_len;
  1556. sector_t end_boundary = (sec + bio_sectors(bio) - 1) >> log2_max_io_len;
  1557. if (start_boundary < end_boundary) {
  1558. sector_t len = ti->max_io_len - (sec & (ti->max_io_len - 1));
  1559. dm_accept_partial_bio(bio, len);
  1560. }
  1561. }
  1562. }
  1563. if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
  1564. submit_flush_bio(ic, dio);
  1565. return DM_MAPIO_SUBMITTED;
  1566. }
  1567. dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
  1568. dio->fua = dio->op == REQ_OP_WRITE && bio->bi_opf & REQ_FUA;
  1569. if (unlikely(dio->fua)) {
  1570. /*
  1571. * Don't pass down the FUA flag because we have to flush
  1572. * disk cache anyway.
  1573. */
  1574. bio->bi_opf &= ~REQ_FUA;
  1575. }
  1576. if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) {
  1577. DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
  1578. dio->range.logical_sector, bio_sectors(bio),
  1579. ic->provided_data_sectors);
  1580. return DM_MAPIO_KILL;
  1581. }
  1582. if (unlikely((dio->range.logical_sector | bio_sectors(bio)) & (unsigned int)(ic->sectors_per_block - 1))) {
  1583. DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
  1584. ic->sectors_per_block,
  1585. dio->range.logical_sector, bio_sectors(bio));
  1586. return DM_MAPIO_KILL;
  1587. }
  1588. if (ic->sectors_per_block > 1 && likely(dio->op != REQ_OP_DISCARD)) {
  1589. struct bvec_iter iter;
  1590. struct bio_vec bv;
  1591. bio_for_each_segment(bv, bio, iter) {
  1592. if (unlikely(bv.bv_len & ((ic->sectors_per_block << SECTOR_SHIFT) - 1))) {
  1593. DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
  1594. bv.bv_offset, bv.bv_len, ic->sectors_per_block);
  1595. return DM_MAPIO_KILL;
  1596. }
  1597. }
  1598. }
  1599. bip = bio_integrity(bio);
  1600. if (!ic->internal_hash) {
  1601. if (bip) {
  1602. unsigned int wanted_tag_size = bio_sectors(bio) >> ic->sb->log2_sectors_per_block;
  1603. if (ic->log2_tag_size >= 0)
  1604. wanted_tag_size <<= ic->log2_tag_size;
  1605. else
  1606. wanted_tag_size *= ic->tag_size;
  1607. if (unlikely(wanted_tag_size != bip->bip_iter.bi_size)) {
  1608. DMERR("Invalid integrity data size %u, expected %u",
  1609. bip->bip_iter.bi_size, wanted_tag_size);
  1610. return DM_MAPIO_KILL;
  1611. }
  1612. }
  1613. } else {
  1614. if (unlikely(bip != NULL)) {
  1615. DMERR("Unexpected integrity data when using internal hash");
  1616. return DM_MAPIO_KILL;
  1617. }
  1618. }
  1619. if (unlikely(ic->mode == 'R') && unlikely(dio->op != REQ_OP_READ))
  1620. return DM_MAPIO_KILL;
  1621. get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
  1622. dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
  1623. bio->bi_iter.bi_sector = get_data_sector(ic, area, offset);
  1624. dm_integrity_map_continue(dio, true);
  1625. return DM_MAPIO_SUBMITTED;
  1626. }
  1627. static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
  1628. unsigned int journal_section, unsigned int journal_entry)
  1629. {
  1630. struct dm_integrity_c *ic = dio->ic;
  1631. sector_t logical_sector;
  1632. unsigned int n_sectors;
  1633. logical_sector = dio->range.logical_sector;
  1634. n_sectors = dio->range.n_sectors;
  1635. do {
  1636. struct bio_vec bv = bio_iovec(bio);
  1637. char *mem;
  1638. if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors))
  1639. bv.bv_len = n_sectors << SECTOR_SHIFT;
  1640. n_sectors -= bv.bv_len >> SECTOR_SHIFT;
  1641. bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
  1642. retry_kmap:
  1643. mem = kmap_local_page(bv.bv_page);
  1644. if (likely(dio->op == REQ_OP_WRITE))
  1645. flush_dcache_page(bv.bv_page);
  1646. do {
  1647. struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry);
  1648. if (unlikely(dio->op == REQ_OP_READ)) {
  1649. struct journal_sector *js;
  1650. char *mem_ptr;
  1651. unsigned int s;
  1652. if (unlikely(journal_entry_is_inprogress(je))) {
  1653. flush_dcache_page(bv.bv_page);
  1654. kunmap_local(mem);
  1655. __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
  1656. goto retry_kmap;
  1657. }
  1658. smp_rmb();
  1659. BUG_ON(journal_entry_get_sector(je) != logical_sector);
  1660. js = access_journal_data(ic, journal_section, journal_entry);
  1661. mem_ptr = mem + bv.bv_offset;
  1662. s = 0;
  1663. do {
  1664. memcpy(mem_ptr, js, JOURNAL_SECTOR_DATA);
  1665. *(commit_id_t *)(mem_ptr + JOURNAL_SECTOR_DATA) = je->last_bytes[s];
  1666. js++;
  1667. mem_ptr += 1 << SECTOR_SHIFT;
  1668. } while (++s < ic->sectors_per_block);
  1669. #ifdef INTERNAL_VERIFY
  1670. if (ic->internal_hash) {
  1671. char checksums_onstack[max((size_t)HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
  1672. integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack);
  1673. if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) {
  1674. DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx",
  1675. logical_sector);
  1676. dm_audit_log_bio(DM_MSG_PREFIX, "journal-checksum",
  1677. bio, logical_sector, 0);
  1678. }
  1679. }
  1680. #endif
  1681. }
  1682. if (!ic->internal_hash) {
  1683. struct bio_integrity_payload *bip = bio_integrity(bio);
  1684. unsigned int tag_todo = ic->tag_size;
  1685. char *tag_ptr = journal_entry_tag(ic, je);
  1686. if (bip) do {
  1687. struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
  1688. unsigned int tag_now = min(biv.bv_len, tag_todo);
  1689. char *tag_addr;
  1690. BUG_ON(PageHighMem(biv.bv_page));
  1691. tag_addr = bvec_virt(&biv);
  1692. if (likely(dio->op == REQ_OP_WRITE))
  1693. memcpy(tag_ptr, tag_addr, tag_now);
  1694. else
  1695. memcpy(tag_addr, tag_ptr, tag_now);
  1696. bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now);
  1697. tag_ptr += tag_now;
  1698. tag_todo -= tag_now;
  1699. } while (unlikely(tag_todo)); else {
  1700. if (likely(dio->op == REQ_OP_WRITE))
  1701. memset(tag_ptr, 0, tag_todo);
  1702. }
  1703. }
  1704. if (likely(dio->op == REQ_OP_WRITE)) {
  1705. struct journal_sector *js;
  1706. unsigned int s;
  1707. js = access_journal_data(ic, journal_section, journal_entry);
  1708. memcpy(js, mem + bv.bv_offset, ic->sectors_per_block << SECTOR_SHIFT);
  1709. s = 0;
  1710. do {
  1711. je->last_bytes[s] = js[s].commit_id;
  1712. } while (++s < ic->sectors_per_block);
  1713. if (ic->internal_hash) {
  1714. unsigned int digest_size = crypto_shash_digestsize(ic->internal_hash);
  1715. if (unlikely(digest_size > ic->tag_size)) {
  1716. char checksums_onstack[HASH_MAX_DIGESTSIZE];
  1717. integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack);
  1718. memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size);
  1719. } else
  1720. integrity_sector_checksum(ic, logical_sector, (char *)js, journal_entry_tag(ic, je));
  1721. }
  1722. journal_entry_set_sector(je, logical_sector);
  1723. }
  1724. logical_sector += ic->sectors_per_block;
  1725. journal_entry++;
  1726. if (unlikely(journal_entry == ic->journal_section_entries)) {
  1727. journal_entry = 0;
  1728. journal_section++;
  1729. wraparound_section(ic, &journal_section);
  1730. }
  1731. bv.bv_offset += ic->sectors_per_block << SECTOR_SHIFT;
  1732. } while (bv.bv_len -= ic->sectors_per_block << SECTOR_SHIFT);
  1733. if (unlikely(dio->op == REQ_OP_READ))
  1734. flush_dcache_page(bv.bv_page);
  1735. kunmap_local(mem);
  1736. } while (n_sectors);
  1737. if (likely(dio->op == REQ_OP_WRITE)) {
  1738. smp_mb();
  1739. if (unlikely(waitqueue_active(&ic->copy_to_journal_wait)))
  1740. wake_up(&ic->copy_to_journal_wait);
  1741. if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) {
  1742. queue_work(ic->commit_wq, &ic->commit_work);
  1743. } else {
  1744. schedule_autocommit(ic);
  1745. }
  1746. } else {
  1747. remove_range(ic, &dio->range);
  1748. }
  1749. if (unlikely(bio->bi_iter.bi_size)) {
  1750. sector_t area, offset;
  1751. dio->range.logical_sector = logical_sector;
  1752. get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
  1753. dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
  1754. return true;
  1755. }
  1756. return false;
  1757. }
  1758. static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map)
  1759. {
  1760. struct dm_integrity_c *ic = dio->ic;
  1761. struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  1762. unsigned int journal_section, journal_entry;
  1763. unsigned int journal_read_pos;
  1764. struct completion read_comp;
  1765. bool discard_retried = false;
  1766. bool need_sync_io = ic->internal_hash && dio->op == REQ_OP_READ;
  1767. if (unlikely(dio->op == REQ_OP_DISCARD) && ic->mode != 'D')
  1768. need_sync_io = true;
  1769. if (need_sync_io && from_map) {
  1770. INIT_WORK(&dio->work, integrity_bio_wait);
  1771. queue_work(ic->offload_wq, &dio->work);
  1772. return;
  1773. }
  1774. lock_retry:
  1775. spin_lock_irq(&ic->endio_wait.lock);
  1776. retry:
  1777. if (unlikely(dm_integrity_failed(ic))) {
  1778. spin_unlock_irq(&ic->endio_wait.lock);
  1779. do_endio(ic, bio);
  1780. return;
  1781. }
  1782. dio->range.n_sectors = bio_sectors(bio);
  1783. journal_read_pos = NOT_FOUND;
  1784. if (ic->mode == 'J' && likely(dio->op != REQ_OP_DISCARD)) {
  1785. if (dio->op == REQ_OP_WRITE) {
  1786. unsigned int next_entry, i, pos;
  1787. unsigned int ws, we, range_sectors;
  1788. dio->range.n_sectors = min(dio->range.n_sectors,
  1789. (sector_t)ic->free_sectors << ic->sb->log2_sectors_per_block);
  1790. if (unlikely(!dio->range.n_sectors)) {
  1791. if (from_map)
  1792. goto offload_to_thread;
  1793. sleep_on_endio_wait(ic);
  1794. goto retry;
  1795. }
  1796. range_sectors = dio->range.n_sectors >> ic->sb->log2_sectors_per_block;
  1797. ic->free_sectors -= range_sectors;
  1798. journal_section = ic->free_section;
  1799. journal_entry = ic->free_section_entry;
  1800. next_entry = ic->free_section_entry + range_sectors;
  1801. ic->free_section_entry = next_entry % ic->journal_section_entries;
  1802. ic->free_section += next_entry / ic->journal_section_entries;
  1803. ic->n_uncommitted_sections += next_entry / ic->journal_section_entries;
  1804. wraparound_section(ic, &ic->free_section);
  1805. pos = journal_section * ic->journal_section_entries + journal_entry;
  1806. ws = journal_section;
  1807. we = journal_entry;
  1808. i = 0;
  1809. do {
  1810. struct journal_entry *je;
  1811. add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i);
  1812. pos++;
  1813. if (unlikely(pos >= ic->journal_entries))
  1814. pos = 0;
  1815. je = access_journal_entry(ic, ws, we);
  1816. BUG_ON(!journal_entry_is_unused(je));
  1817. journal_entry_set_inprogress(je);
  1818. we++;
  1819. if (unlikely(we == ic->journal_section_entries)) {
  1820. we = 0;
  1821. ws++;
  1822. wraparound_section(ic, &ws);
  1823. }
  1824. } while ((i += ic->sectors_per_block) < dio->range.n_sectors);
  1825. spin_unlock_irq(&ic->endio_wait.lock);
  1826. goto journal_read_write;
  1827. } else {
  1828. sector_t next_sector;
  1829. journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
  1830. if (likely(journal_read_pos == NOT_FOUND)) {
  1831. if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector))
  1832. dio->range.n_sectors = next_sector - dio->range.logical_sector;
  1833. } else {
  1834. unsigned int i;
  1835. unsigned int jp = journal_read_pos + 1;
  1836. for (i = ic->sectors_per_block; i < dio->range.n_sectors; i += ic->sectors_per_block, jp++) {
  1837. if (!test_journal_node(ic, jp, dio->range.logical_sector + i))
  1838. break;
  1839. }
  1840. dio->range.n_sectors = i;
  1841. }
  1842. }
  1843. }
  1844. if (unlikely(!add_new_range(ic, &dio->range, true))) {
  1845. /*
  1846. * We must not sleep in the request routine because it could
  1847. * stall bios on current->bio_list.
  1848. * So, we offload the bio to a workqueue if we have to sleep.
  1849. */
  1850. if (from_map) {
  1851. offload_to_thread:
  1852. spin_unlock_irq(&ic->endio_wait.lock);
  1853. INIT_WORK(&dio->work, integrity_bio_wait);
  1854. queue_work(ic->wait_wq, &dio->work);
  1855. return;
  1856. }
  1857. if (journal_read_pos != NOT_FOUND)
  1858. dio->range.n_sectors = ic->sectors_per_block;
  1859. wait_and_add_new_range(ic, &dio->range);
  1860. /*
  1861. * wait_and_add_new_range drops the spinlock, so the journal
  1862. * may have been changed arbitrarily. We need to recheck.
  1863. * To simplify the code, we restrict I/O size to just one block.
  1864. */
  1865. if (journal_read_pos != NOT_FOUND) {
  1866. sector_t next_sector;
  1867. unsigned int new_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
  1868. if (unlikely(new_pos != journal_read_pos)) {
  1869. remove_range_unlocked(ic, &dio->range);
  1870. goto retry;
  1871. }
  1872. }
  1873. }
  1874. if (ic->mode == 'J' && likely(dio->op == REQ_OP_DISCARD) && !discard_retried) {
  1875. sector_t next_sector;
  1876. unsigned int new_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
  1877. if (unlikely(new_pos != NOT_FOUND) ||
  1878. unlikely(next_sector < dio->range.logical_sector - dio->range.n_sectors)) {
  1879. remove_range_unlocked(ic, &dio->range);
  1880. spin_unlock_irq(&ic->endio_wait.lock);
  1881. queue_work(ic->commit_wq, &ic->commit_work);
  1882. flush_workqueue(ic->commit_wq);
  1883. queue_work(ic->writer_wq, &ic->writer_work);
  1884. flush_workqueue(ic->writer_wq);
  1885. discard_retried = true;
  1886. goto lock_retry;
  1887. }
  1888. }
  1889. spin_unlock_irq(&ic->endio_wait.lock);
  1890. if (unlikely(journal_read_pos != NOT_FOUND)) {
  1891. journal_section = journal_read_pos / ic->journal_section_entries;
  1892. journal_entry = journal_read_pos % ic->journal_section_entries;
  1893. goto journal_read_write;
  1894. }
  1895. if (ic->mode == 'B' && (dio->op == REQ_OP_WRITE || unlikely(dio->op == REQ_OP_DISCARD))) {
  1896. if (!block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
  1897. dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
  1898. struct bitmap_block_status *bbs;
  1899. bbs = sector_to_bitmap_block(ic, dio->range.logical_sector);
  1900. spin_lock(&bbs->bio_queue_lock);
  1901. bio_list_add(&bbs->bio_queue, bio);
  1902. spin_unlock(&bbs->bio_queue_lock);
  1903. queue_work(ic->writer_wq, &bbs->work);
  1904. return;
  1905. }
  1906. }
  1907. dio->in_flight = (atomic_t)ATOMIC_INIT(2);
  1908. if (need_sync_io) {
  1909. init_completion(&read_comp);
  1910. dio->completion = &read_comp;
  1911. } else
  1912. dio->completion = NULL;
  1913. dm_bio_record(&dio->bio_details, bio);
  1914. bio_set_dev(bio, ic->dev->bdev);
  1915. bio->bi_integrity = NULL;
  1916. bio->bi_opf &= ~REQ_INTEGRITY;
  1917. bio->bi_end_io = integrity_end_io;
  1918. bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT;
  1919. if (unlikely(dio->op == REQ_OP_DISCARD) && likely(ic->mode != 'D')) {
  1920. integrity_metadata(&dio->work);
  1921. dm_integrity_flush_buffers(ic, false);
  1922. dio->in_flight = (atomic_t)ATOMIC_INIT(1);
  1923. dio->completion = NULL;
  1924. submit_bio_noacct(bio);
  1925. return;
  1926. }
  1927. submit_bio_noacct(bio);
  1928. if (need_sync_io) {
  1929. wait_for_completion_io(&read_comp);
  1930. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
  1931. dio->range.logical_sector + dio->range.n_sectors > le64_to_cpu(ic->sb->recalc_sector))
  1932. goto skip_check;
  1933. if (ic->mode == 'B') {
  1934. if (!block_bitmap_op(ic, ic->recalc_bitmap, dio->range.logical_sector,
  1935. dio->range.n_sectors, BITMAP_OP_TEST_ALL_CLEAR))
  1936. goto skip_check;
  1937. }
  1938. if (likely(!bio->bi_status))
  1939. integrity_metadata(&dio->work);
  1940. else
  1941. skip_check:
  1942. dec_in_flight(dio);
  1943. } else {
  1944. INIT_WORK(&dio->work, integrity_metadata);
  1945. queue_work(ic->metadata_wq, &dio->work);
  1946. }
  1947. return;
  1948. journal_read_write:
  1949. if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry)))
  1950. goto lock_retry;
  1951. do_endio_flush(ic, dio);
  1952. }
  1953. static void integrity_bio_wait(struct work_struct *w)
  1954. {
  1955. struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
  1956. dm_integrity_map_continue(dio, false);
  1957. }
  1958. static void pad_uncommitted(struct dm_integrity_c *ic)
  1959. {
  1960. if (ic->free_section_entry) {
  1961. ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry;
  1962. ic->free_section_entry = 0;
  1963. ic->free_section++;
  1964. wraparound_section(ic, &ic->free_section);
  1965. ic->n_uncommitted_sections++;
  1966. }
  1967. if (WARN_ON(ic->journal_sections * ic->journal_section_entries !=
  1968. (ic->n_uncommitted_sections + ic->n_committed_sections) *
  1969. ic->journal_section_entries + ic->free_sectors)) {
  1970. DMCRIT("journal_sections %u, journal_section_entries %u, "
  1971. "n_uncommitted_sections %u, n_committed_sections %u, "
  1972. "journal_section_entries %u, free_sectors %u",
  1973. ic->journal_sections, ic->journal_section_entries,
  1974. ic->n_uncommitted_sections, ic->n_committed_sections,
  1975. ic->journal_section_entries, ic->free_sectors);
  1976. }
  1977. }
  1978. static void integrity_commit(struct work_struct *w)
  1979. {
  1980. struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work);
  1981. unsigned int commit_start, commit_sections;
  1982. unsigned int i, j, n;
  1983. struct bio *flushes;
  1984. del_timer(&ic->autocommit_timer);
  1985. spin_lock_irq(&ic->endio_wait.lock);
  1986. flushes = bio_list_get(&ic->flush_bio_list);
  1987. if (unlikely(ic->mode != 'J')) {
  1988. spin_unlock_irq(&ic->endio_wait.lock);
  1989. dm_integrity_flush_buffers(ic, true);
  1990. goto release_flush_bios;
  1991. }
  1992. pad_uncommitted(ic);
  1993. commit_start = ic->uncommitted_section;
  1994. commit_sections = ic->n_uncommitted_sections;
  1995. spin_unlock_irq(&ic->endio_wait.lock);
  1996. if (!commit_sections)
  1997. goto release_flush_bios;
  1998. ic->wrote_to_journal = true;
  1999. i = commit_start;
  2000. for (n = 0; n < commit_sections; n++) {
  2001. for (j = 0; j < ic->journal_section_entries; j++) {
  2002. struct journal_entry *je;
  2003. je = access_journal_entry(ic, i, j);
  2004. io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
  2005. }
  2006. for (j = 0; j < ic->journal_section_sectors; j++) {
  2007. struct journal_sector *js;
  2008. js = access_journal(ic, i, j);
  2009. js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq);
  2010. }
  2011. i++;
  2012. if (unlikely(i >= ic->journal_sections))
  2013. ic->commit_seq = next_commit_seq(ic->commit_seq);
  2014. wraparound_section(ic, &i);
  2015. }
  2016. smp_rmb();
  2017. write_journal(ic, commit_start, commit_sections);
  2018. spin_lock_irq(&ic->endio_wait.lock);
  2019. ic->uncommitted_section += commit_sections;
  2020. wraparound_section(ic, &ic->uncommitted_section);
  2021. ic->n_uncommitted_sections -= commit_sections;
  2022. ic->n_committed_sections += commit_sections;
  2023. spin_unlock_irq(&ic->endio_wait.lock);
  2024. if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold)
  2025. queue_work(ic->writer_wq, &ic->writer_work);
  2026. release_flush_bios:
  2027. while (flushes) {
  2028. struct bio *next = flushes->bi_next;
  2029. flushes->bi_next = NULL;
  2030. do_endio(ic, flushes);
  2031. flushes = next;
  2032. }
  2033. }
  2034. static void complete_copy_from_journal(unsigned long error, void *context)
  2035. {
  2036. struct journal_io *io = context;
  2037. struct journal_completion *comp = io->comp;
  2038. struct dm_integrity_c *ic = comp->ic;
  2039. remove_range(ic, &io->range);
  2040. mempool_free(io, &ic->journal_io_mempool);
  2041. if (unlikely(error != 0))
  2042. dm_integrity_io_error(ic, "copying from journal", -EIO);
  2043. complete_journal_op(comp);
  2044. }
  2045. static void restore_last_bytes(struct dm_integrity_c *ic, struct journal_sector *js,
  2046. struct journal_entry *je)
  2047. {
  2048. unsigned int s = 0;
  2049. do {
  2050. js->commit_id = je->last_bytes[s];
  2051. js++;
  2052. } while (++s < ic->sectors_per_block);
  2053. }
  2054. static void do_journal_write(struct dm_integrity_c *ic, unsigned int write_start,
  2055. unsigned int write_sections, bool from_replay)
  2056. {
  2057. unsigned int i, j, n;
  2058. struct journal_completion comp;
  2059. struct blk_plug plug;
  2060. blk_start_plug(&plug);
  2061. comp.ic = ic;
  2062. comp.in_flight = (atomic_t)ATOMIC_INIT(1);
  2063. init_completion(&comp.comp);
  2064. i = write_start;
  2065. for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) {
  2066. #ifndef INTERNAL_VERIFY
  2067. if (unlikely(from_replay))
  2068. #endif
  2069. rw_section_mac(ic, i, false);
  2070. for (j = 0; j < ic->journal_section_entries; j++) {
  2071. struct journal_entry *je = access_journal_entry(ic, i, j);
  2072. sector_t sec, area, offset;
  2073. unsigned int k, l, next_loop;
  2074. sector_t metadata_block;
  2075. unsigned int metadata_offset;
  2076. struct journal_io *io;
  2077. if (journal_entry_is_unused(je))
  2078. continue;
  2079. BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay);
  2080. sec = journal_entry_get_sector(je);
  2081. if (unlikely(from_replay)) {
  2082. if (unlikely(sec & (unsigned int)(ic->sectors_per_block - 1))) {
  2083. dm_integrity_io_error(ic, "invalid sector in journal", -EIO);
  2084. sec &= ~(sector_t)(ic->sectors_per_block - 1);
  2085. }
  2086. if (unlikely(sec >= ic->provided_data_sectors)) {
  2087. journal_entry_set_unused(je);
  2088. continue;
  2089. }
  2090. }
  2091. get_area_and_offset(ic, sec, &area, &offset);
  2092. restore_last_bytes(ic, access_journal_data(ic, i, j), je);
  2093. for (k = j + 1; k < ic->journal_section_entries; k++) {
  2094. struct journal_entry *je2 = access_journal_entry(ic, i, k);
  2095. sector_t sec2, area2, offset2;
  2096. if (journal_entry_is_unused(je2))
  2097. break;
  2098. BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay);
  2099. sec2 = journal_entry_get_sector(je2);
  2100. if (unlikely(sec2 >= ic->provided_data_sectors))
  2101. break;
  2102. get_area_and_offset(ic, sec2, &area2, &offset2);
  2103. if (area2 != area || offset2 != offset + ((k - j) << ic->sb->log2_sectors_per_block))
  2104. break;
  2105. restore_last_bytes(ic, access_journal_data(ic, i, k), je2);
  2106. }
  2107. next_loop = k - 1;
  2108. io = mempool_alloc(&ic->journal_io_mempool, GFP_NOIO);
  2109. io->comp = &comp;
  2110. io->range.logical_sector = sec;
  2111. io->range.n_sectors = (k - j) << ic->sb->log2_sectors_per_block;
  2112. spin_lock_irq(&ic->endio_wait.lock);
  2113. add_new_range_and_wait(ic, &io->range);
  2114. if (likely(!from_replay)) {
  2115. struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries];
  2116. /* don't write if there is newer committed sector */
  2117. while (j < k && find_newer_committed_node(ic, &section_node[j])) {
  2118. struct journal_entry *je2 = access_journal_entry(ic, i, j);
  2119. journal_entry_set_unused(je2);
  2120. remove_journal_node(ic, &section_node[j]);
  2121. j++;
  2122. sec += ic->sectors_per_block;
  2123. offset += ic->sectors_per_block;
  2124. }
  2125. while (j < k && find_newer_committed_node(ic, &section_node[k - 1])) {
  2126. struct journal_entry *je2 = access_journal_entry(ic, i, k - 1);
  2127. journal_entry_set_unused(je2);
  2128. remove_journal_node(ic, &section_node[k - 1]);
  2129. k--;
  2130. }
  2131. if (j == k) {
  2132. remove_range_unlocked(ic, &io->range);
  2133. spin_unlock_irq(&ic->endio_wait.lock);
  2134. mempool_free(io, &ic->journal_io_mempool);
  2135. goto skip_io;
  2136. }
  2137. for (l = j; l < k; l++) {
  2138. remove_journal_node(ic, &section_node[l]);
  2139. }
  2140. }
  2141. spin_unlock_irq(&ic->endio_wait.lock);
  2142. metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
  2143. for (l = j; l < k; l++) {
  2144. int r;
  2145. struct journal_entry *je2 = access_journal_entry(ic, i, l);
  2146. if (
  2147. #ifndef INTERNAL_VERIFY
  2148. unlikely(from_replay) &&
  2149. #endif
  2150. ic->internal_hash) {
  2151. char test_tag[max_t(size_t, HASH_MAX_DIGESTSIZE, MAX_TAG_SIZE)];
  2152. integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block),
  2153. (char *)access_journal_data(ic, i, l), test_tag);
  2154. if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size))) {
  2155. dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
  2156. dm_audit_log_target(DM_MSG_PREFIX, "integrity-replay-journal", ic->ti, 0);
  2157. }
  2158. }
  2159. journal_entry_set_unused(je2);
  2160. r = dm_integrity_rw_tag(ic, journal_entry_tag(ic, je2), &metadata_block, &metadata_offset,
  2161. ic->tag_size, TAG_WRITE);
  2162. if (unlikely(r)) {
  2163. dm_integrity_io_error(ic, "reading tags", r);
  2164. }
  2165. }
  2166. atomic_inc(&comp.in_flight);
  2167. copy_from_journal(ic, i, j << ic->sb->log2_sectors_per_block,
  2168. (k - j) << ic->sb->log2_sectors_per_block,
  2169. get_data_sector(ic, area, offset),
  2170. complete_copy_from_journal, io);
  2171. skip_io:
  2172. j = next_loop;
  2173. }
  2174. }
  2175. dm_bufio_write_dirty_buffers_async(ic->bufio);
  2176. blk_finish_plug(&plug);
  2177. complete_journal_op(&comp);
  2178. wait_for_completion_io(&comp.comp);
  2179. dm_integrity_flush_buffers(ic, true);
  2180. }
  2181. static void integrity_writer(struct work_struct *w)
  2182. {
  2183. struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work);
  2184. unsigned int write_start, write_sections;
  2185. unsigned int prev_free_sectors;
  2186. spin_lock_irq(&ic->endio_wait.lock);
  2187. write_start = ic->committed_section;
  2188. write_sections = ic->n_committed_sections;
  2189. spin_unlock_irq(&ic->endio_wait.lock);
  2190. if (!write_sections)
  2191. return;
  2192. do_journal_write(ic, write_start, write_sections, false);
  2193. spin_lock_irq(&ic->endio_wait.lock);
  2194. ic->committed_section += write_sections;
  2195. wraparound_section(ic, &ic->committed_section);
  2196. ic->n_committed_sections -= write_sections;
  2197. prev_free_sectors = ic->free_sectors;
  2198. ic->free_sectors += write_sections * ic->journal_section_entries;
  2199. if (unlikely(!prev_free_sectors))
  2200. wake_up_locked(&ic->endio_wait);
  2201. spin_unlock_irq(&ic->endio_wait.lock);
  2202. }
  2203. static void recalc_write_super(struct dm_integrity_c *ic)
  2204. {
  2205. int r;
  2206. dm_integrity_flush_buffers(ic, false);
  2207. if (dm_integrity_failed(ic))
  2208. return;
  2209. r = sync_rw_sb(ic, REQ_OP_WRITE);
  2210. if (unlikely(r))
  2211. dm_integrity_io_error(ic, "writing superblock", r);
  2212. }
  2213. static void integrity_recalc(struct work_struct *w)
  2214. {
  2215. struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, recalc_work);
  2216. struct dm_integrity_range range;
  2217. struct dm_io_request io_req;
  2218. struct dm_io_region io_loc;
  2219. sector_t area, offset;
  2220. sector_t metadata_block;
  2221. unsigned int metadata_offset;
  2222. sector_t logical_sector, n_sectors;
  2223. __u8 *t;
  2224. unsigned int i;
  2225. int r;
  2226. unsigned int super_counter = 0;
  2227. DEBUG_print("start recalculation... (position %llx)\n", le64_to_cpu(ic->sb->recalc_sector));
  2228. spin_lock_irq(&ic->endio_wait.lock);
  2229. next_chunk:
  2230. if (unlikely(dm_post_suspending(ic->ti)))
  2231. goto unlock_ret;
  2232. range.logical_sector = le64_to_cpu(ic->sb->recalc_sector);
  2233. if (unlikely(range.logical_sector >= ic->provided_data_sectors)) {
  2234. if (ic->mode == 'B') {
  2235. block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
  2236. DEBUG_print("queue_delayed_work: bitmap_flush_work\n");
  2237. queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
  2238. }
  2239. goto unlock_ret;
  2240. }
  2241. get_area_and_offset(ic, range.logical_sector, &area, &offset);
  2242. range.n_sectors = min((sector_t)RECALC_SECTORS, ic->provided_data_sectors - range.logical_sector);
  2243. if (!ic->meta_dev)
  2244. range.n_sectors = min(range.n_sectors, ((sector_t)1U << ic->sb->log2_interleave_sectors) - (unsigned int)offset);
  2245. add_new_range_and_wait(ic, &range);
  2246. spin_unlock_irq(&ic->endio_wait.lock);
  2247. logical_sector = range.logical_sector;
  2248. n_sectors = range.n_sectors;
  2249. if (ic->mode == 'B') {
  2250. if (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector, n_sectors, BITMAP_OP_TEST_ALL_CLEAR)) {
  2251. goto advance_and_next;
  2252. }
  2253. while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector,
  2254. ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
  2255. logical_sector += ic->sectors_per_block;
  2256. n_sectors -= ic->sectors_per_block;
  2257. cond_resched();
  2258. }
  2259. while (block_bitmap_op(ic, ic->recalc_bitmap, logical_sector + n_sectors - ic->sectors_per_block,
  2260. ic->sectors_per_block, BITMAP_OP_TEST_ALL_CLEAR)) {
  2261. n_sectors -= ic->sectors_per_block;
  2262. cond_resched();
  2263. }
  2264. get_area_and_offset(ic, logical_sector, &area, &offset);
  2265. }
  2266. DEBUG_print("recalculating: %llx, %llx\n", logical_sector, n_sectors);
  2267. if (unlikely(++super_counter == RECALC_WRITE_SUPER)) {
  2268. recalc_write_super(ic);
  2269. if (ic->mode == 'B') {
  2270. queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
  2271. }
  2272. super_counter = 0;
  2273. }
  2274. if (unlikely(dm_integrity_failed(ic)))
  2275. goto err;
  2276. io_req.bi_opf = REQ_OP_READ;
  2277. io_req.mem.type = DM_IO_VMA;
  2278. io_req.mem.ptr.addr = ic->recalc_buffer;
  2279. io_req.notify.fn = NULL;
  2280. io_req.client = ic->io;
  2281. io_loc.bdev = ic->dev->bdev;
  2282. io_loc.sector = get_data_sector(ic, area, offset);
  2283. io_loc.count = n_sectors;
  2284. r = dm_io(&io_req, 1, &io_loc, NULL);
  2285. if (unlikely(r)) {
  2286. dm_integrity_io_error(ic, "reading data", r);
  2287. goto err;
  2288. }
  2289. t = ic->recalc_tags;
  2290. for (i = 0; i < n_sectors; i += ic->sectors_per_block) {
  2291. integrity_sector_checksum(ic, logical_sector + i, ic->recalc_buffer + (i << SECTOR_SHIFT), t);
  2292. t += ic->tag_size;
  2293. }
  2294. metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
  2295. r = dm_integrity_rw_tag(ic, ic->recalc_tags, &metadata_block, &metadata_offset, t - ic->recalc_tags, TAG_WRITE);
  2296. if (unlikely(r)) {
  2297. dm_integrity_io_error(ic, "writing tags", r);
  2298. goto err;
  2299. }
  2300. if (ic->mode == 'B') {
  2301. sector_t start, end;
  2302. start = (range.logical_sector >>
  2303. (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)) <<
  2304. (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
  2305. end = ((range.logical_sector + range.n_sectors) >>
  2306. (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)) <<
  2307. (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
  2308. block_bitmap_op(ic, ic->recalc_bitmap, start, end - start, BITMAP_OP_CLEAR);
  2309. }
  2310. advance_and_next:
  2311. cond_resched();
  2312. spin_lock_irq(&ic->endio_wait.lock);
  2313. remove_range_unlocked(ic, &range);
  2314. ic->sb->recalc_sector = cpu_to_le64(range.logical_sector + range.n_sectors);
  2315. goto next_chunk;
  2316. err:
  2317. remove_range(ic, &range);
  2318. return;
  2319. unlock_ret:
  2320. spin_unlock_irq(&ic->endio_wait.lock);
  2321. recalc_write_super(ic);
  2322. }
  2323. static void bitmap_block_work(struct work_struct *w)
  2324. {
  2325. struct bitmap_block_status *bbs = container_of(w, struct bitmap_block_status, work);
  2326. struct dm_integrity_c *ic = bbs->ic;
  2327. struct bio *bio;
  2328. struct bio_list bio_queue;
  2329. struct bio_list waiting;
  2330. bio_list_init(&waiting);
  2331. spin_lock(&bbs->bio_queue_lock);
  2332. bio_queue = bbs->bio_queue;
  2333. bio_list_init(&bbs->bio_queue);
  2334. spin_unlock(&bbs->bio_queue_lock);
  2335. while ((bio = bio_list_pop(&bio_queue))) {
  2336. struct dm_integrity_io *dio;
  2337. dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
  2338. if (block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
  2339. dio->range.n_sectors, BITMAP_OP_TEST_ALL_SET)) {
  2340. remove_range(ic, &dio->range);
  2341. INIT_WORK(&dio->work, integrity_bio_wait);
  2342. queue_work(ic->offload_wq, &dio->work);
  2343. } else {
  2344. block_bitmap_op(ic, ic->journal, dio->range.logical_sector,
  2345. dio->range.n_sectors, BITMAP_OP_SET);
  2346. bio_list_add(&waiting, bio);
  2347. }
  2348. }
  2349. if (bio_list_empty(&waiting))
  2350. return;
  2351. rw_journal_sectors(ic, REQ_OP_WRITE | REQ_FUA | REQ_SYNC,
  2352. bbs->idx * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT),
  2353. BITMAP_BLOCK_SIZE >> SECTOR_SHIFT, NULL);
  2354. while ((bio = bio_list_pop(&waiting))) {
  2355. struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
  2356. block_bitmap_op(ic, ic->may_write_bitmap, dio->range.logical_sector,
  2357. dio->range.n_sectors, BITMAP_OP_SET);
  2358. remove_range(ic, &dio->range);
  2359. INIT_WORK(&dio->work, integrity_bio_wait);
  2360. queue_work(ic->offload_wq, &dio->work);
  2361. }
  2362. queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, ic->bitmap_flush_interval);
  2363. }
  2364. static void bitmap_flush_work(struct work_struct *work)
  2365. {
  2366. struct dm_integrity_c *ic = container_of(work, struct dm_integrity_c, bitmap_flush_work.work);
  2367. struct dm_integrity_range range;
  2368. unsigned long limit;
  2369. struct bio *bio;
  2370. dm_integrity_flush_buffers(ic, false);
  2371. range.logical_sector = 0;
  2372. range.n_sectors = ic->provided_data_sectors;
  2373. spin_lock_irq(&ic->endio_wait.lock);
  2374. add_new_range_and_wait(ic, &range);
  2375. spin_unlock_irq(&ic->endio_wait.lock);
  2376. dm_integrity_flush_buffers(ic, true);
  2377. limit = ic->provided_data_sectors;
  2378. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
  2379. limit = le64_to_cpu(ic->sb->recalc_sector)
  2380. >> (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit)
  2381. << (ic->sb->log2_sectors_per_block + ic->log2_blocks_per_bitmap_bit);
  2382. }
  2383. /*DEBUG_print("zeroing journal\n");*/
  2384. block_bitmap_op(ic, ic->journal, 0, limit, BITMAP_OP_CLEAR);
  2385. block_bitmap_op(ic, ic->may_write_bitmap, 0, limit, BITMAP_OP_CLEAR);
  2386. rw_journal_sectors(ic, REQ_OP_WRITE | REQ_FUA | REQ_SYNC, 0,
  2387. ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
  2388. spin_lock_irq(&ic->endio_wait.lock);
  2389. remove_range_unlocked(ic, &range);
  2390. while (unlikely((bio = bio_list_pop(&ic->synchronous_bios)) != NULL)) {
  2391. bio_endio(bio);
  2392. spin_unlock_irq(&ic->endio_wait.lock);
  2393. spin_lock_irq(&ic->endio_wait.lock);
  2394. }
  2395. spin_unlock_irq(&ic->endio_wait.lock);
  2396. }
  2397. static void init_journal(struct dm_integrity_c *ic, unsigned int start_section,
  2398. unsigned int n_sections, unsigned char commit_seq)
  2399. {
  2400. unsigned int i, j, n;
  2401. if (!n_sections)
  2402. return;
  2403. for (n = 0; n < n_sections; n++) {
  2404. i = start_section + n;
  2405. wraparound_section(ic, &i);
  2406. for (j = 0; j < ic->journal_section_sectors; j++) {
  2407. struct journal_sector *js = access_journal(ic, i, j);
  2408. BUILD_BUG_ON(sizeof(js->sectors) != JOURNAL_SECTOR_DATA);
  2409. memset(&js->sectors, 0, sizeof(js->sectors));
  2410. js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq);
  2411. }
  2412. for (j = 0; j < ic->journal_section_entries; j++) {
  2413. struct journal_entry *je = access_journal_entry(ic, i, j);
  2414. journal_entry_set_unused(je);
  2415. }
  2416. }
  2417. write_journal(ic, start_section, n_sections);
  2418. }
  2419. static int find_commit_seq(struct dm_integrity_c *ic, unsigned int i, unsigned int j, commit_id_t id)
  2420. {
  2421. unsigned char k;
  2422. for (k = 0; k < N_COMMIT_IDS; k++) {
  2423. if (dm_integrity_commit_id(ic, i, j, k) == id)
  2424. return k;
  2425. }
  2426. dm_integrity_io_error(ic, "journal commit id", -EIO);
  2427. return -EIO;
  2428. }
  2429. static void replay_journal(struct dm_integrity_c *ic)
  2430. {
  2431. unsigned int i, j;
  2432. bool used_commit_ids[N_COMMIT_IDS];
  2433. unsigned int max_commit_id_sections[N_COMMIT_IDS];
  2434. unsigned int write_start, write_sections;
  2435. unsigned int continue_section;
  2436. bool journal_empty;
  2437. unsigned char unused, last_used, want_commit_seq;
  2438. if (ic->mode == 'R')
  2439. return;
  2440. if (ic->journal_uptodate)
  2441. return;
  2442. last_used = 0;
  2443. write_start = 0;
  2444. if (!ic->just_formatted) {
  2445. DEBUG_print("reading journal\n");
  2446. rw_journal(ic, REQ_OP_READ, 0, ic->journal_sections, NULL);
  2447. if (ic->journal_io)
  2448. DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal");
  2449. if (ic->journal_io) {
  2450. struct journal_completion crypt_comp;
  2451. crypt_comp.ic = ic;
  2452. init_completion(&crypt_comp.comp);
  2453. crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0);
  2454. encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp);
  2455. wait_for_completion(&crypt_comp.comp);
  2456. }
  2457. DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal");
  2458. }
  2459. if (dm_integrity_failed(ic))
  2460. goto clear_journal;
  2461. journal_empty = true;
  2462. memset(used_commit_ids, 0, sizeof used_commit_ids);
  2463. memset(max_commit_id_sections, 0, sizeof max_commit_id_sections);
  2464. for (i = 0; i < ic->journal_sections; i++) {
  2465. for (j = 0; j < ic->journal_section_sectors; j++) {
  2466. int k;
  2467. struct journal_sector *js = access_journal(ic, i, j);
  2468. k = find_commit_seq(ic, i, j, js->commit_id);
  2469. if (k < 0)
  2470. goto clear_journal;
  2471. used_commit_ids[k] = true;
  2472. max_commit_id_sections[k] = i;
  2473. }
  2474. if (journal_empty) {
  2475. for (j = 0; j < ic->journal_section_entries; j++) {
  2476. struct journal_entry *je = access_journal_entry(ic, i, j);
  2477. if (!journal_entry_is_unused(je)) {
  2478. journal_empty = false;
  2479. break;
  2480. }
  2481. }
  2482. }
  2483. }
  2484. if (!used_commit_ids[N_COMMIT_IDS - 1]) {
  2485. unused = N_COMMIT_IDS - 1;
  2486. while (unused && !used_commit_ids[unused - 1])
  2487. unused--;
  2488. } else {
  2489. for (unused = 0; unused < N_COMMIT_IDS; unused++)
  2490. if (!used_commit_ids[unused])
  2491. break;
  2492. if (unused == N_COMMIT_IDS) {
  2493. dm_integrity_io_error(ic, "journal commit ids", -EIO);
  2494. goto clear_journal;
  2495. }
  2496. }
  2497. DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
  2498. unused, used_commit_ids[0], used_commit_ids[1],
  2499. used_commit_ids[2], used_commit_ids[3]);
  2500. last_used = prev_commit_seq(unused);
  2501. want_commit_seq = prev_commit_seq(last_used);
  2502. if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)])
  2503. journal_empty = true;
  2504. write_start = max_commit_id_sections[last_used] + 1;
  2505. if (unlikely(write_start >= ic->journal_sections))
  2506. want_commit_seq = next_commit_seq(want_commit_seq);
  2507. wraparound_section(ic, &write_start);
  2508. i = write_start;
  2509. for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) {
  2510. for (j = 0; j < ic->journal_section_sectors; j++) {
  2511. struct journal_sector *js = access_journal(ic, i, j);
  2512. if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) {
  2513. /*
  2514. * This could be caused by crash during writing.
  2515. * We won't replay the inconsistent part of the
  2516. * journal.
  2517. */
  2518. DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
  2519. i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq);
  2520. goto brk;
  2521. }
  2522. }
  2523. i++;
  2524. if (unlikely(i >= ic->journal_sections))
  2525. want_commit_seq = next_commit_seq(want_commit_seq);
  2526. wraparound_section(ic, &i);
  2527. }
  2528. brk:
  2529. if (!journal_empty) {
  2530. DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
  2531. write_sections, write_start, want_commit_seq);
  2532. do_journal_write(ic, write_start, write_sections, true);
  2533. }
  2534. if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) {
  2535. continue_section = write_start;
  2536. ic->commit_seq = want_commit_seq;
  2537. DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq);
  2538. } else {
  2539. unsigned int s;
  2540. unsigned char erase_seq;
  2541. clear_journal:
  2542. DEBUG_print("clearing journal\n");
  2543. erase_seq = prev_commit_seq(prev_commit_seq(last_used));
  2544. s = write_start;
  2545. init_journal(ic, s, 1, erase_seq);
  2546. s++;
  2547. wraparound_section(ic, &s);
  2548. if (ic->journal_sections >= 2) {
  2549. init_journal(ic, s, ic->journal_sections - 2, erase_seq);
  2550. s += ic->journal_sections - 2;
  2551. wraparound_section(ic, &s);
  2552. init_journal(ic, s, 1, erase_seq);
  2553. }
  2554. continue_section = 0;
  2555. ic->commit_seq = next_commit_seq(erase_seq);
  2556. }
  2557. ic->committed_section = continue_section;
  2558. ic->n_committed_sections = 0;
  2559. ic->uncommitted_section = continue_section;
  2560. ic->n_uncommitted_sections = 0;
  2561. ic->free_section = continue_section;
  2562. ic->free_section_entry = 0;
  2563. ic->free_sectors = ic->journal_entries;
  2564. ic->journal_tree_root = RB_ROOT;
  2565. for (i = 0; i < ic->journal_entries; i++)
  2566. init_journal_node(&ic->journal_tree[i]);
  2567. }
  2568. static void dm_integrity_enter_synchronous_mode(struct dm_integrity_c *ic)
  2569. {
  2570. DEBUG_print("dm_integrity_enter_synchronous_mode\n");
  2571. if (ic->mode == 'B') {
  2572. ic->bitmap_flush_interval = msecs_to_jiffies(10) + 1;
  2573. ic->synchronous_mode = 1;
  2574. cancel_delayed_work_sync(&ic->bitmap_flush_work);
  2575. queue_delayed_work(ic->commit_wq, &ic->bitmap_flush_work, 0);
  2576. flush_workqueue(ic->commit_wq);
  2577. }
  2578. }
  2579. static int dm_integrity_reboot(struct notifier_block *n, unsigned long code, void *x)
  2580. {
  2581. struct dm_integrity_c *ic = container_of(n, struct dm_integrity_c, reboot_notifier);
  2582. DEBUG_print("dm_integrity_reboot\n");
  2583. dm_integrity_enter_synchronous_mode(ic);
  2584. return NOTIFY_DONE;
  2585. }
  2586. static void dm_integrity_postsuspend(struct dm_target *ti)
  2587. {
  2588. struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
  2589. int r;
  2590. WARN_ON(unregister_reboot_notifier(&ic->reboot_notifier));
  2591. del_timer_sync(&ic->autocommit_timer);
  2592. if (ic->recalc_wq)
  2593. drain_workqueue(ic->recalc_wq);
  2594. if (ic->mode == 'B')
  2595. cancel_delayed_work_sync(&ic->bitmap_flush_work);
  2596. queue_work(ic->commit_wq, &ic->commit_work);
  2597. drain_workqueue(ic->commit_wq);
  2598. if (ic->mode == 'J') {
  2599. queue_work(ic->writer_wq, &ic->writer_work);
  2600. drain_workqueue(ic->writer_wq);
  2601. dm_integrity_flush_buffers(ic, true);
  2602. if (ic->wrote_to_journal) {
  2603. init_journal(ic, ic->free_section,
  2604. ic->journal_sections - ic->free_section, ic->commit_seq);
  2605. if (ic->free_section) {
  2606. init_journal(ic, 0, ic->free_section,
  2607. next_commit_seq(ic->commit_seq));
  2608. }
  2609. }
  2610. }
  2611. if (ic->mode == 'B') {
  2612. dm_integrity_flush_buffers(ic, true);
  2613. #if 1
  2614. /* set to 0 to test bitmap replay code */
  2615. init_journal(ic, 0, ic->journal_sections, 0);
  2616. ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
  2617. r = sync_rw_sb(ic, REQ_OP_WRITE | REQ_FUA);
  2618. if (unlikely(r))
  2619. dm_integrity_io_error(ic, "writing superblock", r);
  2620. #endif
  2621. }
  2622. BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
  2623. ic->journal_uptodate = true;
  2624. }
  2625. static void dm_integrity_resume(struct dm_target *ti)
  2626. {
  2627. struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
  2628. __u64 old_provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
  2629. int r;
  2630. DEBUG_print("resume\n");
  2631. ic->wrote_to_journal = false;
  2632. if (ic->provided_data_sectors != old_provided_data_sectors) {
  2633. if (ic->provided_data_sectors > old_provided_data_sectors &&
  2634. ic->mode == 'B' &&
  2635. ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit) {
  2636. rw_journal_sectors(ic, REQ_OP_READ, 0,
  2637. ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
  2638. block_bitmap_op(ic, ic->journal, old_provided_data_sectors,
  2639. ic->provided_data_sectors - old_provided_data_sectors, BITMAP_OP_SET);
  2640. rw_journal_sectors(ic, REQ_OP_WRITE | REQ_FUA | REQ_SYNC, 0,
  2641. ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
  2642. }
  2643. ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
  2644. r = sync_rw_sb(ic, REQ_OP_WRITE | REQ_FUA);
  2645. if (unlikely(r))
  2646. dm_integrity_io_error(ic, "writing superblock", r);
  2647. }
  2648. if (ic->sb->flags & cpu_to_le32(SB_FLAG_DIRTY_BITMAP)) {
  2649. DEBUG_print("resume dirty_bitmap\n");
  2650. rw_journal_sectors(ic, REQ_OP_READ, 0,
  2651. ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
  2652. if (ic->mode == 'B') {
  2653. if (ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit &&
  2654. !ic->reset_recalculate_flag) {
  2655. block_bitmap_copy(ic, ic->recalc_bitmap, ic->journal);
  2656. block_bitmap_copy(ic, ic->may_write_bitmap, ic->journal);
  2657. if (!block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors,
  2658. BITMAP_OP_TEST_ALL_CLEAR)) {
  2659. ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
  2660. ic->sb->recalc_sector = cpu_to_le64(0);
  2661. }
  2662. } else {
  2663. DEBUG_print("non-matching blocks_per_bitmap_bit: %u, %u\n",
  2664. ic->sb->log2_blocks_per_bitmap_bit, ic->log2_blocks_per_bitmap_bit);
  2665. ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
  2666. block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
  2667. block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_SET);
  2668. block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_SET);
  2669. rw_journal_sectors(ic, REQ_OP_WRITE | REQ_FUA | REQ_SYNC, 0,
  2670. ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
  2671. ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
  2672. ic->sb->recalc_sector = cpu_to_le64(0);
  2673. }
  2674. } else {
  2675. if (!(ic->sb->log2_blocks_per_bitmap_bit == ic->log2_blocks_per_bitmap_bit &&
  2676. block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_TEST_ALL_CLEAR)) ||
  2677. ic->reset_recalculate_flag) {
  2678. ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
  2679. ic->sb->recalc_sector = cpu_to_le64(0);
  2680. }
  2681. init_journal(ic, 0, ic->journal_sections, 0);
  2682. replay_journal(ic);
  2683. ic->sb->flags &= ~cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
  2684. }
  2685. r = sync_rw_sb(ic, REQ_OP_WRITE | REQ_FUA);
  2686. if (unlikely(r))
  2687. dm_integrity_io_error(ic, "writing superblock", r);
  2688. } else {
  2689. replay_journal(ic);
  2690. if (ic->reset_recalculate_flag) {
  2691. ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
  2692. ic->sb->recalc_sector = cpu_to_le64(0);
  2693. }
  2694. if (ic->mode == 'B') {
  2695. ic->sb->flags |= cpu_to_le32(SB_FLAG_DIRTY_BITMAP);
  2696. ic->sb->log2_blocks_per_bitmap_bit = ic->log2_blocks_per_bitmap_bit;
  2697. r = sync_rw_sb(ic, REQ_OP_WRITE | REQ_FUA);
  2698. if (unlikely(r))
  2699. dm_integrity_io_error(ic, "writing superblock", r);
  2700. block_bitmap_op(ic, ic->journal, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
  2701. block_bitmap_op(ic, ic->recalc_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
  2702. block_bitmap_op(ic, ic->may_write_bitmap, 0, ic->provided_data_sectors, BITMAP_OP_CLEAR);
  2703. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
  2704. le64_to_cpu(ic->sb->recalc_sector) < ic->provided_data_sectors) {
  2705. block_bitmap_op(ic, ic->journal, le64_to_cpu(ic->sb->recalc_sector),
  2706. ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
  2707. block_bitmap_op(ic, ic->recalc_bitmap, le64_to_cpu(ic->sb->recalc_sector),
  2708. ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
  2709. block_bitmap_op(ic, ic->may_write_bitmap, le64_to_cpu(ic->sb->recalc_sector),
  2710. ic->provided_data_sectors - le64_to_cpu(ic->sb->recalc_sector), BITMAP_OP_SET);
  2711. }
  2712. rw_journal_sectors(ic, REQ_OP_WRITE | REQ_FUA | REQ_SYNC, 0,
  2713. ic->n_bitmap_blocks * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT), NULL);
  2714. }
  2715. }
  2716. DEBUG_print("testing recalc: %x\n", ic->sb->flags);
  2717. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
  2718. __u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);
  2719. DEBUG_print("recalc pos: %llx / %llx\n", recalc_pos, ic->provided_data_sectors);
  2720. if (recalc_pos < ic->provided_data_sectors) {
  2721. queue_work(ic->recalc_wq, &ic->recalc_work);
  2722. } else if (recalc_pos > ic->provided_data_sectors) {
  2723. ic->sb->recalc_sector = cpu_to_le64(ic->provided_data_sectors);
  2724. recalc_write_super(ic);
  2725. }
  2726. }
  2727. ic->reboot_notifier.notifier_call = dm_integrity_reboot;
  2728. ic->reboot_notifier.next = NULL;
  2729. ic->reboot_notifier.priority = INT_MAX - 1; /* be notified after md and before hardware drivers */
  2730. WARN_ON(register_reboot_notifier(&ic->reboot_notifier));
  2731. #if 0
  2732. /* set to 1 to stress test synchronous mode */
  2733. dm_integrity_enter_synchronous_mode(ic);
  2734. #endif
  2735. }
  2736. static void dm_integrity_status(struct dm_target *ti, status_type_t type,
  2737. unsigned int status_flags, char *result, unsigned int maxlen)
  2738. {
  2739. struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
  2740. unsigned int arg_count;
  2741. size_t sz = 0;
  2742. switch (type) {
  2743. case STATUSTYPE_INFO:
  2744. DMEMIT("%llu %llu",
  2745. (unsigned long long)atomic64_read(&ic->number_of_mismatches),
  2746. ic->provided_data_sectors);
  2747. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
  2748. DMEMIT(" %llu", le64_to_cpu(ic->sb->recalc_sector));
  2749. else
  2750. DMEMIT(" -");
  2751. break;
  2752. case STATUSTYPE_TABLE: {
  2753. __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100;
  2754. watermark_percentage += ic->journal_entries / 2;
  2755. do_div(watermark_percentage, ic->journal_entries);
  2756. arg_count = 3;
  2757. arg_count += !!ic->meta_dev;
  2758. arg_count += ic->sectors_per_block != 1;
  2759. arg_count += !!(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING));
  2760. arg_count += ic->reset_recalculate_flag;
  2761. arg_count += ic->discard;
  2762. arg_count += ic->mode == 'J';
  2763. arg_count += ic->mode == 'J';
  2764. arg_count += ic->mode == 'B';
  2765. arg_count += ic->mode == 'B';
  2766. arg_count += !!ic->internal_hash_alg.alg_string;
  2767. arg_count += !!ic->journal_crypt_alg.alg_string;
  2768. arg_count += !!ic->journal_mac_alg.alg_string;
  2769. arg_count += (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING)) != 0;
  2770. arg_count += (ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_HMAC)) != 0;
  2771. arg_count += ic->legacy_recalculate;
  2772. DMEMIT("%s %llu %u %c %u", ic->dev->name, ic->start,
  2773. ic->tag_size, ic->mode, arg_count);
  2774. if (ic->meta_dev)
  2775. DMEMIT(" meta_device:%s", ic->meta_dev->name);
  2776. if (ic->sectors_per_block != 1)
  2777. DMEMIT(" block_size:%u", ic->sectors_per_block << SECTOR_SHIFT);
  2778. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
  2779. DMEMIT(" recalculate");
  2780. if (ic->reset_recalculate_flag)
  2781. DMEMIT(" reset_recalculate");
  2782. if (ic->discard)
  2783. DMEMIT(" allow_discards");
  2784. DMEMIT(" journal_sectors:%u", ic->initial_sectors - SB_SECTORS);
  2785. DMEMIT(" interleave_sectors:%u", 1U << ic->sb->log2_interleave_sectors);
  2786. DMEMIT(" buffer_sectors:%u", 1U << ic->log2_buffer_sectors);
  2787. if (ic->mode == 'J') {
  2788. DMEMIT(" journal_watermark:%u", (unsigned int)watermark_percentage);
  2789. DMEMIT(" commit_time:%u", ic->autocommit_msec);
  2790. }
  2791. if (ic->mode == 'B') {
  2792. DMEMIT(" sectors_per_bit:%llu", (sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit);
  2793. DMEMIT(" bitmap_flush_interval:%u", jiffies_to_msecs(ic->bitmap_flush_interval));
  2794. }
  2795. if ((ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING)) != 0)
  2796. DMEMIT(" fix_padding");
  2797. if ((ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_HMAC)) != 0)
  2798. DMEMIT(" fix_hmac");
  2799. if (ic->legacy_recalculate)
  2800. DMEMIT(" legacy_recalculate");
  2801. #define EMIT_ALG(a, n) \
  2802. do { \
  2803. if (ic->a.alg_string) { \
  2804. DMEMIT(" %s:%s", n, ic->a.alg_string); \
  2805. if (ic->a.key_string) \
  2806. DMEMIT(":%s", ic->a.key_string);\
  2807. } \
  2808. } while (0)
  2809. EMIT_ALG(internal_hash_alg, "internal_hash");
  2810. EMIT_ALG(journal_crypt_alg, "journal_crypt");
  2811. EMIT_ALG(journal_mac_alg, "journal_mac");
  2812. break;
  2813. }
  2814. case STATUSTYPE_IMA:
  2815. DMEMIT_TARGET_NAME_VERSION(ti->type);
  2816. DMEMIT(",dev_name=%s,start=%llu,tag_size=%u,mode=%c",
  2817. ic->dev->name, ic->start, ic->tag_size, ic->mode);
  2818. if (ic->meta_dev)
  2819. DMEMIT(",meta_device=%s", ic->meta_dev->name);
  2820. if (ic->sectors_per_block != 1)
  2821. DMEMIT(",block_size=%u", ic->sectors_per_block << SECTOR_SHIFT);
  2822. DMEMIT(",recalculate=%c", (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) ?
  2823. 'y' : 'n');
  2824. DMEMIT(",allow_discards=%c", ic->discard ? 'y' : 'n');
  2825. DMEMIT(",fix_padding=%c",
  2826. ((ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING)) != 0) ? 'y' : 'n');
  2827. DMEMIT(",fix_hmac=%c",
  2828. ((ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_HMAC)) != 0) ? 'y' : 'n');
  2829. DMEMIT(",legacy_recalculate=%c", ic->legacy_recalculate ? 'y' : 'n');
  2830. DMEMIT(",journal_sectors=%u", ic->initial_sectors - SB_SECTORS);
  2831. DMEMIT(",interleave_sectors=%u", 1U << ic->sb->log2_interleave_sectors);
  2832. DMEMIT(",buffer_sectors=%u", 1U << ic->log2_buffer_sectors);
  2833. DMEMIT(";");
  2834. break;
  2835. }
  2836. }
  2837. static int dm_integrity_iterate_devices(struct dm_target *ti,
  2838. iterate_devices_callout_fn fn, void *data)
  2839. {
  2840. struct dm_integrity_c *ic = ti->private;
  2841. if (!ic->meta_dev)
  2842. return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data);
  2843. else
  2844. return fn(ti, ic->dev, 0, ti->len, data);
  2845. }
  2846. static void dm_integrity_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2847. {
  2848. struct dm_integrity_c *ic = ti->private;
  2849. if (ic->sectors_per_block > 1) {
  2850. limits->logical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
  2851. limits->physical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
  2852. blk_limits_io_min(limits, ic->sectors_per_block << SECTOR_SHIFT);
  2853. limits->dma_alignment = limits->logical_block_size - 1;
  2854. }
  2855. }
  2856. static void calculate_journal_section_size(struct dm_integrity_c *ic)
  2857. {
  2858. unsigned int sector_space = JOURNAL_SECTOR_DATA;
  2859. ic->journal_sections = le32_to_cpu(ic->sb->journal_sections);
  2860. ic->journal_entry_size = roundup(offsetof(struct journal_entry, last_bytes[ic->sectors_per_block]) + ic->tag_size,
  2861. JOURNAL_ENTRY_ROUNDUP);
  2862. if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC))
  2863. sector_space -= JOURNAL_MAC_PER_SECTOR;
  2864. ic->journal_entries_per_sector = sector_space / ic->journal_entry_size;
  2865. ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS;
  2866. ic->journal_section_sectors = (ic->journal_section_entries << ic->sb->log2_sectors_per_block) + JOURNAL_BLOCK_SECTORS;
  2867. ic->journal_entries = ic->journal_section_entries * ic->journal_sections;
  2868. }
  2869. static int calculate_device_limits(struct dm_integrity_c *ic)
  2870. {
  2871. __u64 initial_sectors;
  2872. calculate_journal_section_size(ic);
  2873. initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections;
  2874. if (initial_sectors + METADATA_PADDING_SECTORS >= ic->meta_device_sectors || initial_sectors > UINT_MAX)
  2875. return -EINVAL;
  2876. ic->initial_sectors = initial_sectors;
  2877. if (!ic->meta_dev) {
  2878. sector_t last_sector, last_area, last_offset;
  2879. /* we have to maintain excessive padding for compatibility with existing volumes */
  2880. __u64 metadata_run_padding =
  2881. ic->sb->flags & cpu_to_le32(SB_FLAG_FIXED_PADDING) ?
  2882. (__u64)(METADATA_PADDING_SECTORS << SECTOR_SHIFT) :
  2883. (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS);
  2884. ic->metadata_run = round_up((__u64)ic->tag_size << (ic->sb->log2_interleave_sectors - ic->sb->log2_sectors_per_block),
  2885. metadata_run_padding) >> SECTOR_SHIFT;
  2886. if (!(ic->metadata_run & (ic->metadata_run - 1)))
  2887. ic->log2_metadata_run = __ffs(ic->metadata_run);
  2888. else
  2889. ic->log2_metadata_run = -1;
  2890. get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset);
  2891. last_sector = get_data_sector(ic, last_area, last_offset);
  2892. if (last_sector < ic->start || last_sector >= ic->meta_device_sectors)
  2893. return -EINVAL;
  2894. } else {
  2895. __u64 meta_size = (ic->provided_data_sectors >> ic->sb->log2_sectors_per_block) * ic->tag_size;
  2896. meta_size = (meta_size + ((1U << (ic->log2_buffer_sectors + SECTOR_SHIFT)) - 1))
  2897. >> (ic->log2_buffer_sectors + SECTOR_SHIFT);
  2898. meta_size <<= ic->log2_buffer_sectors;
  2899. if (ic->initial_sectors + meta_size < ic->initial_sectors ||
  2900. ic->initial_sectors + meta_size > ic->meta_device_sectors)
  2901. return -EINVAL;
  2902. ic->metadata_run = 1;
  2903. ic->log2_metadata_run = 0;
  2904. }
  2905. return 0;
  2906. }
  2907. static void get_provided_data_sectors(struct dm_integrity_c *ic)
  2908. {
  2909. if (!ic->meta_dev) {
  2910. int test_bit;
  2911. ic->provided_data_sectors = 0;
  2912. for (test_bit = fls64(ic->meta_device_sectors) - 1; test_bit >= 3; test_bit--) {
  2913. __u64 prev_data_sectors = ic->provided_data_sectors;
  2914. ic->provided_data_sectors |= (sector_t)1 << test_bit;
  2915. if (calculate_device_limits(ic))
  2916. ic->provided_data_sectors = prev_data_sectors;
  2917. }
  2918. } else {
  2919. ic->provided_data_sectors = ic->data_device_sectors;
  2920. ic->provided_data_sectors &= ~(sector_t)(ic->sectors_per_block - 1);
  2921. }
  2922. }
  2923. static int initialize_superblock(struct dm_integrity_c *ic,
  2924. unsigned int journal_sectors, unsigned int interleave_sectors)
  2925. {
  2926. unsigned int journal_sections;
  2927. int test_bit;
  2928. memset(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT);
  2929. memcpy(ic->sb->magic, SB_MAGIC, 8);
  2930. ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size);
  2931. ic->sb->log2_sectors_per_block = __ffs(ic->sectors_per_block);
  2932. if (ic->journal_mac_alg.alg_string)
  2933. ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC);
  2934. calculate_journal_section_size(ic);
  2935. journal_sections = journal_sectors / ic->journal_section_sectors;
  2936. if (!journal_sections)
  2937. journal_sections = 1;
  2938. if (ic->fix_hmac && (ic->internal_hash_alg.alg_string || ic->journal_mac_alg.alg_string)) {
  2939. ic->sb->flags |= cpu_to_le32(SB_FLAG_FIXED_HMAC);
  2940. get_random_bytes(ic->sb->salt, SALT_SIZE);
  2941. }
  2942. if (!ic->meta_dev) {
  2943. if (ic->fix_padding)
  2944. ic->sb->flags |= cpu_to_le32(SB_FLAG_FIXED_PADDING);
  2945. ic->sb->journal_sections = cpu_to_le32(journal_sections);
  2946. if (!interleave_sectors)
  2947. interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
  2948. ic->sb->log2_interleave_sectors = __fls(interleave_sectors);
  2949. ic->sb->log2_interleave_sectors = max((__u8)MIN_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
  2950. ic->sb->log2_interleave_sectors = min((__u8)MAX_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
  2951. get_provided_data_sectors(ic);
  2952. if (!ic->provided_data_sectors)
  2953. return -EINVAL;
  2954. } else {
  2955. ic->sb->log2_interleave_sectors = 0;
  2956. get_provided_data_sectors(ic);
  2957. if (!ic->provided_data_sectors)
  2958. return -EINVAL;
  2959. try_smaller_buffer:
  2960. ic->sb->journal_sections = cpu_to_le32(0);
  2961. for (test_bit = fls(journal_sections) - 1; test_bit >= 0; test_bit--) {
  2962. __u32 prev_journal_sections = le32_to_cpu(ic->sb->journal_sections);
  2963. __u32 test_journal_sections = prev_journal_sections | (1U << test_bit);
  2964. if (test_journal_sections > journal_sections)
  2965. continue;
  2966. ic->sb->journal_sections = cpu_to_le32(test_journal_sections);
  2967. if (calculate_device_limits(ic))
  2968. ic->sb->journal_sections = cpu_to_le32(prev_journal_sections);
  2969. }
  2970. if (!le32_to_cpu(ic->sb->journal_sections)) {
  2971. if (ic->log2_buffer_sectors > 3) {
  2972. ic->log2_buffer_sectors--;
  2973. goto try_smaller_buffer;
  2974. }
  2975. return -EINVAL;
  2976. }
  2977. }
  2978. ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
  2979. sb_set_version(ic);
  2980. return 0;
  2981. }
  2982. static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic)
  2983. {
  2984. struct gendisk *disk = dm_disk(dm_table_get_md(ti->table));
  2985. struct blk_integrity bi;
  2986. memset(&bi, 0, sizeof(bi));
  2987. bi.profile = &dm_integrity_profile;
  2988. bi.tuple_size = ic->tag_size;
  2989. bi.tag_size = bi.tuple_size;
  2990. bi.interval_exp = ic->sb->log2_sectors_per_block + SECTOR_SHIFT;
  2991. blk_integrity_register(disk, &bi);
  2992. blk_queue_max_integrity_segments(disk->queue, UINT_MAX);
  2993. }
  2994. static void dm_integrity_free_page_list(struct page_list *pl)
  2995. {
  2996. unsigned int i;
  2997. if (!pl)
  2998. return;
  2999. for (i = 0; pl[i].page; i++)
  3000. __free_page(pl[i].page);
  3001. kvfree(pl);
  3002. }
  3003. static struct page_list *dm_integrity_alloc_page_list(unsigned int n_pages)
  3004. {
  3005. struct page_list *pl;
  3006. unsigned int i;
  3007. pl = kvmalloc_array(n_pages + 1, sizeof(struct page_list), GFP_KERNEL | __GFP_ZERO);
  3008. if (!pl)
  3009. return NULL;
  3010. for (i = 0; i < n_pages; i++) {
  3011. pl[i].page = alloc_page(GFP_KERNEL);
  3012. if (!pl[i].page) {
  3013. dm_integrity_free_page_list(pl);
  3014. return NULL;
  3015. }
  3016. if (i)
  3017. pl[i - 1].next = &pl[i];
  3018. }
  3019. pl[i].page = NULL;
  3020. pl[i].next = NULL;
  3021. return pl;
  3022. }
  3023. static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl)
  3024. {
  3025. unsigned int i;
  3026. for (i = 0; i < ic->journal_sections; i++)
  3027. kvfree(sl[i]);
  3028. kvfree(sl);
  3029. }
  3030. static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic,
  3031. struct page_list *pl)
  3032. {
  3033. struct scatterlist **sl;
  3034. unsigned int i;
  3035. sl = kvmalloc_array(ic->journal_sections,
  3036. sizeof(struct scatterlist *),
  3037. GFP_KERNEL | __GFP_ZERO);
  3038. if (!sl)
  3039. return NULL;
  3040. for (i = 0; i < ic->journal_sections; i++) {
  3041. struct scatterlist *s;
  3042. unsigned int start_index, start_offset;
  3043. unsigned int end_index, end_offset;
  3044. unsigned int n_pages;
  3045. unsigned int idx;
  3046. page_list_location(ic, i, 0, &start_index, &start_offset);
  3047. page_list_location(ic, i, ic->journal_section_sectors - 1,
  3048. &end_index, &end_offset);
  3049. n_pages = (end_index - start_index + 1);
  3050. s = kvmalloc_array(n_pages, sizeof(struct scatterlist),
  3051. GFP_KERNEL);
  3052. if (!s) {
  3053. dm_integrity_free_journal_scatterlist(ic, sl);
  3054. return NULL;
  3055. }
  3056. sg_init_table(s, n_pages);
  3057. for (idx = start_index; idx <= end_index; idx++) {
  3058. char *va = lowmem_page_address(pl[idx].page);
  3059. unsigned int start = 0, end = PAGE_SIZE;
  3060. if (idx == start_index)
  3061. start = start_offset;
  3062. if (idx == end_index)
  3063. end = end_offset + (1 << SECTOR_SHIFT);
  3064. sg_set_buf(&s[idx - start_index], va + start, end - start);
  3065. }
  3066. sl[i] = s;
  3067. }
  3068. return sl;
  3069. }
  3070. static void free_alg(struct alg_spec *a)
  3071. {
  3072. kfree_sensitive(a->alg_string);
  3073. kfree_sensitive(a->key);
  3074. memset(a, 0, sizeof *a);
  3075. }
  3076. static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval)
  3077. {
  3078. char *k;
  3079. free_alg(a);
  3080. a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL);
  3081. if (!a->alg_string)
  3082. goto nomem;
  3083. k = strchr(a->alg_string, ':');
  3084. if (k) {
  3085. *k = 0;
  3086. a->key_string = k + 1;
  3087. if (strlen(a->key_string) & 1)
  3088. goto inval;
  3089. a->key_size = strlen(a->key_string) / 2;
  3090. a->key = kmalloc(a->key_size, GFP_KERNEL);
  3091. if (!a->key)
  3092. goto nomem;
  3093. if (hex2bin(a->key, a->key_string, a->key_size))
  3094. goto inval;
  3095. }
  3096. return 0;
  3097. inval:
  3098. *error = error_inval;
  3099. return -EINVAL;
  3100. nomem:
  3101. *error = "Out of memory for an argument";
  3102. return -ENOMEM;
  3103. }
  3104. static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error,
  3105. char *error_alg, char *error_key)
  3106. {
  3107. int r;
  3108. if (a->alg_string) {
  3109. *hash = crypto_alloc_shash(a->alg_string, 0, CRYPTO_ALG_ALLOCATES_MEMORY);
  3110. if (IS_ERR(*hash)) {
  3111. *error = error_alg;
  3112. r = PTR_ERR(*hash);
  3113. *hash = NULL;
  3114. return r;
  3115. }
  3116. if (a->key) {
  3117. r = crypto_shash_setkey(*hash, a->key, a->key_size);
  3118. if (r) {
  3119. *error = error_key;
  3120. return r;
  3121. }
  3122. } else if (crypto_shash_get_flags(*hash) & CRYPTO_TFM_NEED_KEY) {
  3123. *error = error_key;
  3124. return -ENOKEY;
  3125. }
  3126. }
  3127. return 0;
  3128. }
  3129. static int create_journal(struct dm_integrity_c *ic, char **error)
  3130. {
  3131. int r = 0;
  3132. unsigned int i;
  3133. __u64 journal_pages, journal_desc_size, journal_tree_size;
  3134. unsigned char *crypt_data = NULL, *crypt_iv = NULL;
  3135. struct skcipher_request *req = NULL;
  3136. ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL);
  3137. ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL);
  3138. ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL);
  3139. ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
  3140. journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
  3141. PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
  3142. journal_desc_size = journal_pages * sizeof(struct page_list);
  3143. if (journal_pages >= totalram_pages() - totalhigh_pages() || journal_desc_size > ULONG_MAX) {
  3144. *error = "Journal doesn't fit into memory";
  3145. r = -ENOMEM;
  3146. goto bad;
  3147. }
  3148. ic->journal_pages = journal_pages;
  3149. ic->journal = dm_integrity_alloc_page_list(ic->journal_pages);
  3150. if (!ic->journal) {
  3151. *error = "Could not allocate memory for journal";
  3152. r = -ENOMEM;
  3153. goto bad;
  3154. }
  3155. if (ic->journal_crypt_alg.alg_string) {
  3156. unsigned int ivsize, blocksize;
  3157. struct journal_completion comp;
  3158. comp.ic = ic;
  3159. ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, CRYPTO_ALG_ALLOCATES_MEMORY);
  3160. if (IS_ERR(ic->journal_crypt)) {
  3161. *error = "Invalid journal cipher";
  3162. r = PTR_ERR(ic->journal_crypt);
  3163. ic->journal_crypt = NULL;
  3164. goto bad;
  3165. }
  3166. ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
  3167. blocksize = crypto_skcipher_blocksize(ic->journal_crypt);
  3168. if (ic->journal_crypt_alg.key) {
  3169. r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key,
  3170. ic->journal_crypt_alg.key_size);
  3171. if (r) {
  3172. *error = "Error setting encryption key";
  3173. goto bad;
  3174. }
  3175. }
  3176. DEBUG_print("cipher %s, block size %u iv size %u\n",
  3177. ic->journal_crypt_alg.alg_string, blocksize, ivsize);
  3178. ic->journal_io = dm_integrity_alloc_page_list(ic->journal_pages);
  3179. if (!ic->journal_io) {
  3180. *error = "Could not allocate memory for journal io";
  3181. r = -ENOMEM;
  3182. goto bad;
  3183. }
  3184. if (blocksize == 1) {
  3185. struct scatterlist *sg;
  3186. req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
  3187. if (!req) {
  3188. *error = "Could not allocate crypt request";
  3189. r = -ENOMEM;
  3190. goto bad;
  3191. }
  3192. crypt_iv = kzalloc(ivsize, GFP_KERNEL);
  3193. if (!crypt_iv) {
  3194. *error = "Could not allocate iv";
  3195. r = -ENOMEM;
  3196. goto bad;
  3197. }
  3198. ic->journal_xor = dm_integrity_alloc_page_list(ic->journal_pages);
  3199. if (!ic->journal_xor) {
  3200. *error = "Could not allocate memory for journal xor";
  3201. r = -ENOMEM;
  3202. goto bad;
  3203. }
  3204. sg = kvmalloc_array(ic->journal_pages + 1,
  3205. sizeof(struct scatterlist),
  3206. GFP_KERNEL);
  3207. if (!sg) {
  3208. *error = "Unable to allocate sg list";
  3209. r = -ENOMEM;
  3210. goto bad;
  3211. }
  3212. sg_init_table(sg, ic->journal_pages + 1);
  3213. for (i = 0; i < ic->journal_pages; i++) {
  3214. char *va = lowmem_page_address(ic->journal_xor[i].page);
  3215. clear_page(va);
  3216. sg_set_buf(&sg[i], va, PAGE_SIZE);
  3217. }
  3218. sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids);
  3219. skcipher_request_set_crypt(req, sg, sg,
  3220. PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, crypt_iv);
  3221. init_completion(&comp.comp);
  3222. comp.in_flight = (atomic_t)ATOMIC_INIT(1);
  3223. if (do_crypt(true, req, &comp))
  3224. wait_for_completion(&comp.comp);
  3225. kvfree(sg);
  3226. r = dm_integrity_failed(ic);
  3227. if (r) {
  3228. *error = "Unable to encrypt journal";
  3229. goto bad;
  3230. }
  3231. DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data");
  3232. crypto_free_skcipher(ic->journal_crypt);
  3233. ic->journal_crypt = NULL;
  3234. } else {
  3235. unsigned int crypt_len = roundup(ivsize, blocksize);
  3236. req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
  3237. if (!req) {
  3238. *error = "Could not allocate crypt request";
  3239. r = -ENOMEM;
  3240. goto bad;
  3241. }
  3242. crypt_iv = kmalloc(ivsize, GFP_KERNEL);
  3243. if (!crypt_iv) {
  3244. *error = "Could not allocate iv";
  3245. r = -ENOMEM;
  3246. goto bad;
  3247. }
  3248. crypt_data = kmalloc(crypt_len, GFP_KERNEL);
  3249. if (!crypt_data) {
  3250. *error = "Unable to allocate crypt data";
  3251. r = -ENOMEM;
  3252. goto bad;
  3253. }
  3254. ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal);
  3255. if (!ic->journal_scatterlist) {
  3256. *error = "Unable to allocate sg list";
  3257. r = -ENOMEM;
  3258. goto bad;
  3259. }
  3260. ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io);
  3261. if (!ic->journal_io_scatterlist) {
  3262. *error = "Unable to allocate sg list";
  3263. r = -ENOMEM;
  3264. goto bad;
  3265. }
  3266. ic->sk_requests = kvmalloc_array(ic->journal_sections,
  3267. sizeof(struct skcipher_request *),
  3268. GFP_KERNEL | __GFP_ZERO);
  3269. if (!ic->sk_requests) {
  3270. *error = "Unable to allocate sk requests";
  3271. r = -ENOMEM;
  3272. goto bad;
  3273. }
  3274. for (i = 0; i < ic->journal_sections; i++) {
  3275. struct scatterlist sg;
  3276. struct skcipher_request *section_req;
  3277. __le32 section_le = cpu_to_le32(i);
  3278. memset(crypt_iv, 0x00, ivsize);
  3279. memset(crypt_data, 0x00, crypt_len);
  3280. memcpy(crypt_data, &section_le, min((size_t)crypt_len, sizeof(section_le)));
  3281. sg_init_one(&sg, crypt_data, crypt_len);
  3282. skcipher_request_set_crypt(req, &sg, &sg, crypt_len, crypt_iv);
  3283. init_completion(&comp.comp);
  3284. comp.in_flight = (atomic_t)ATOMIC_INIT(1);
  3285. if (do_crypt(true, req, &comp))
  3286. wait_for_completion(&comp.comp);
  3287. r = dm_integrity_failed(ic);
  3288. if (r) {
  3289. *error = "Unable to generate iv";
  3290. goto bad;
  3291. }
  3292. section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
  3293. if (!section_req) {
  3294. *error = "Unable to allocate crypt request";
  3295. r = -ENOMEM;
  3296. goto bad;
  3297. }
  3298. section_req->iv = kmalloc_array(ivsize, 2,
  3299. GFP_KERNEL);
  3300. if (!section_req->iv) {
  3301. skcipher_request_free(section_req);
  3302. *error = "Unable to allocate iv";
  3303. r = -ENOMEM;
  3304. goto bad;
  3305. }
  3306. memcpy(section_req->iv + ivsize, crypt_data, ivsize);
  3307. section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT;
  3308. ic->sk_requests[i] = section_req;
  3309. DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i);
  3310. }
  3311. }
  3312. }
  3313. for (i = 0; i < N_COMMIT_IDS; i++) {
  3314. unsigned int j;
  3315. retest_commit_id:
  3316. for (j = 0; j < i; j++) {
  3317. if (ic->commit_ids[j] == ic->commit_ids[i]) {
  3318. ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1);
  3319. goto retest_commit_id;
  3320. }
  3321. }
  3322. DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]);
  3323. }
  3324. journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node);
  3325. if (journal_tree_size > ULONG_MAX) {
  3326. *error = "Journal doesn't fit into memory";
  3327. r = -ENOMEM;
  3328. goto bad;
  3329. }
  3330. ic->journal_tree = kvmalloc(journal_tree_size, GFP_KERNEL);
  3331. if (!ic->journal_tree) {
  3332. *error = "Could not allocate memory for journal tree";
  3333. r = -ENOMEM;
  3334. }
  3335. bad:
  3336. kfree(crypt_data);
  3337. kfree(crypt_iv);
  3338. skcipher_request_free(req);
  3339. return r;
  3340. }
  3341. /*
  3342. * Construct a integrity mapping
  3343. *
  3344. * Arguments:
  3345. * device
  3346. * offset from the start of the device
  3347. * tag size
  3348. * D - direct writes, J - journal writes, B - bitmap mode, R - recovery mode
  3349. * number of optional arguments
  3350. * optional arguments:
  3351. * journal_sectors
  3352. * interleave_sectors
  3353. * buffer_sectors
  3354. * journal_watermark
  3355. * commit_time
  3356. * meta_device
  3357. * block_size
  3358. * sectors_per_bit
  3359. * bitmap_flush_interval
  3360. * internal_hash
  3361. * journal_crypt
  3362. * journal_mac
  3363. * recalculate
  3364. */
  3365. static int dm_integrity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  3366. {
  3367. struct dm_integrity_c *ic;
  3368. char dummy;
  3369. int r;
  3370. unsigned int extra_args;
  3371. struct dm_arg_set as;
  3372. static const struct dm_arg _args[] = {
  3373. {0, 18, "Invalid number of feature args"},
  3374. };
  3375. unsigned int journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec;
  3376. bool should_write_sb;
  3377. __u64 threshold;
  3378. unsigned long long start;
  3379. __s8 log2_sectors_per_bitmap_bit = -1;
  3380. __s8 log2_blocks_per_bitmap_bit;
  3381. __u64 bits_in_journal;
  3382. __u64 n_bitmap_bits;
  3383. #define DIRECT_ARGUMENTS 4
  3384. if (argc <= DIRECT_ARGUMENTS) {
  3385. ti->error = "Invalid argument count";
  3386. return -EINVAL;
  3387. }
  3388. ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL);
  3389. if (!ic) {
  3390. ti->error = "Cannot allocate integrity context";
  3391. return -ENOMEM;
  3392. }
  3393. ti->private = ic;
  3394. ti->per_io_data_size = sizeof(struct dm_integrity_io);
  3395. ic->ti = ti;
  3396. ic->in_progress = RB_ROOT;
  3397. INIT_LIST_HEAD(&ic->wait_list);
  3398. init_waitqueue_head(&ic->endio_wait);
  3399. bio_list_init(&ic->flush_bio_list);
  3400. init_waitqueue_head(&ic->copy_to_journal_wait);
  3401. init_completion(&ic->crypto_backoff);
  3402. atomic64_set(&ic->number_of_mismatches, 0);
  3403. ic->bitmap_flush_interval = BITMAP_FLUSH_INTERVAL;
  3404. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
  3405. if (r) {
  3406. ti->error = "Device lookup failed";
  3407. goto bad;
  3408. }
  3409. if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
  3410. ti->error = "Invalid starting offset";
  3411. r = -EINVAL;
  3412. goto bad;
  3413. }
  3414. ic->start = start;
  3415. if (strcmp(argv[2], "-")) {
  3416. if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) {
  3417. ti->error = "Invalid tag size";
  3418. r = -EINVAL;
  3419. goto bad;
  3420. }
  3421. }
  3422. if (!strcmp(argv[3], "J") || !strcmp(argv[3], "B") ||
  3423. !strcmp(argv[3], "D") || !strcmp(argv[3], "R")) {
  3424. ic->mode = argv[3][0];
  3425. } else {
  3426. ti->error = "Invalid mode (expecting J, B, D, R)";
  3427. r = -EINVAL;
  3428. goto bad;
  3429. }
  3430. journal_sectors = 0;
  3431. interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
  3432. buffer_sectors = DEFAULT_BUFFER_SECTORS;
  3433. journal_watermark = DEFAULT_JOURNAL_WATERMARK;
  3434. sync_msec = DEFAULT_SYNC_MSEC;
  3435. ic->sectors_per_block = 1;
  3436. as.argc = argc - DIRECT_ARGUMENTS;
  3437. as.argv = argv + DIRECT_ARGUMENTS;
  3438. r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
  3439. if (r)
  3440. goto bad;
  3441. while (extra_args--) {
  3442. const char *opt_string;
  3443. unsigned int val;
  3444. unsigned long long llval;
  3445. opt_string = dm_shift_arg(&as);
  3446. if (!opt_string) {
  3447. r = -EINVAL;
  3448. ti->error = "Not enough feature arguments";
  3449. goto bad;
  3450. }
  3451. if (sscanf(opt_string, "journal_sectors:%u%c", &val, &dummy) == 1)
  3452. journal_sectors = val ? val : 1;
  3453. else if (sscanf(opt_string, "interleave_sectors:%u%c", &val, &dummy) == 1)
  3454. interleave_sectors = val;
  3455. else if (sscanf(opt_string, "buffer_sectors:%u%c", &val, &dummy) == 1)
  3456. buffer_sectors = val;
  3457. else if (sscanf(opt_string, "journal_watermark:%u%c", &val, &dummy) == 1 && val <= 100)
  3458. journal_watermark = val;
  3459. else if (sscanf(opt_string, "commit_time:%u%c", &val, &dummy) == 1)
  3460. sync_msec = val;
  3461. else if (!strncmp(opt_string, "meta_device:", strlen("meta_device:"))) {
  3462. if (ic->meta_dev) {
  3463. dm_put_device(ti, ic->meta_dev);
  3464. ic->meta_dev = NULL;
  3465. }
  3466. r = dm_get_device(ti, strchr(opt_string, ':') + 1,
  3467. dm_table_get_mode(ti->table), &ic->meta_dev);
  3468. if (r) {
  3469. ti->error = "Device lookup failed";
  3470. goto bad;
  3471. }
  3472. } else if (sscanf(opt_string, "block_size:%u%c", &val, &dummy) == 1) {
  3473. if (val < 1 << SECTOR_SHIFT ||
  3474. val > MAX_SECTORS_PER_BLOCK << SECTOR_SHIFT ||
  3475. (val & (val -1))) {
  3476. r = -EINVAL;
  3477. ti->error = "Invalid block_size argument";
  3478. goto bad;
  3479. }
  3480. ic->sectors_per_block = val >> SECTOR_SHIFT;
  3481. } else if (sscanf(opt_string, "sectors_per_bit:%llu%c", &llval, &dummy) == 1) {
  3482. log2_sectors_per_bitmap_bit = !llval ? 0 : __ilog2_u64(llval);
  3483. } else if (sscanf(opt_string, "bitmap_flush_interval:%u%c", &val, &dummy) == 1) {
  3484. if (val >= (uint64_t)UINT_MAX * 1000 / HZ) {
  3485. r = -EINVAL;
  3486. ti->error = "Invalid bitmap_flush_interval argument";
  3487. goto bad;
  3488. }
  3489. ic->bitmap_flush_interval = msecs_to_jiffies(val);
  3490. } else if (!strncmp(opt_string, "internal_hash:", strlen("internal_hash:"))) {
  3491. r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error,
  3492. "Invalid internal_hash argument");
  3493. if (r)
  3494. goto bad;
  3495. } else if (!strncmp(opt_string, "journal_crypt:", strlen("journal_crypt:"))) {
  3496. r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error,
  3497. "Invalid journal_crypt argument");
  3498. if (r)
  3499. goto bad;
  3500. } else if (!strncmp(opt_string, "journal_mac:", strlen("journal_mac:"))) {
  3501. r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error,
  3502. "Invalid journal_mac argument");
  3503. if (r)
  3504. goto bad;
  3505. } else if (!strcmp(opt_string, "recalculate")) {
  3506. ic->recalculate_flag = true;
  3507. } else if (!strcmp(opt_string, "reset_recalculate")) {
  3508. ic->recalculate_flag = true;
  3509. ic->reset_recalculate_flag = true;
  3510. } else if (!strcmp(opt_string, "allow_discards")) {
  3511. ic->discard = true;
  3512. } else if (!strcmp(opt_string, "fix_padding")) {
  3513. ic->fix_padding = true;
  3514. } else if (!strcmp(opt_string, "fix_hmac")) {
  3515. ic->fix_hmac = true;
  3516. } else if (!strcmp(opt_string, "legacy_recalculate")) {
  3517. ic->legacy_recalculate = true;
  3518. } else {
  3519. r = -EINVAL;
  3520. ti->error = "Invalid argument";
  3521. goto bad;
  3522. }
  3523. }
  3524. ic->data_device_sectors = bdev_nr_sectors(ic->dev->bdev);
  3525. if (!ic->meta_dev)
  3526. ic->meta_device_sectors = ic->data_device_sectors;
  3527. else
  3528. ic->meta_device_sectors = bdev_nr_sectors(ic->meta_dev->bdev);
  3529. if (!journal_sectors) {
  3530. journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS,
  3531. ic->data_device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR);
  3532. }
  3533. if (!buffer_sectors)
  3534. buffer_sectors = 1;
  3535. ic->log2_buffer_sectors = min((int)__fls(buffer_sectors), 31 - SECTOR_SHIFT);
  3536. r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error,
  3537. "Invalid internal hash", "Error setting internal hash key");
  3538. if (r)
  3539. goto bad;
  3540. r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error,
  3541. "Invalid journal mac", "Error setting journal mac key");
  3542. if (r)
  3543. goto bad;
  3544. if (!ic->tag_size) {
  3545. if (!ic->internal_hash) {
  3546. ti->error = "Unknown tag size";
  3547. r = -EINVAL;
  3548. goto bad;
  3549. }
  3550. ic->tag_size = crypto_shash_digestsize(ic->internal_hash);
  3551. }
  3552. if (ic->tag_size > MAX_TAG_SIZE) {
  3553. ti->error = "Too big tag size";
  3554. r = -EINVAL;
  3555. goto bad;
  3556. }
  3557. if (!(ic->tag_size & (ic->tag_size - 1)))
  3558. ic->log2_tag_size = __ffs(ic->tag_size);
  3559. else
  3560. ic->log2_tag_size = -1;
  3561. if (ic->mode == 'B' && !ic->internal_hash) {
  3562. r = -EINVAL;
  3563. ti->error = "Bitmap mode can be only used with internal hash";
  3564. goto bad;
  3565. }
  3566. if (ic->discard && !ic->internal_hash) {
  3567. r = -EINVAL;
  3568. ti->error = "Discard can be only used with internal hash";
  3569. goto bad;
  3570. }
  3571. ic->autocommit_jiffies = msecs_to_jiffies(sync_msec);
  3572. ic->autocommit_msec = sync_msec;
  3573. timer_setup(&ic->autocommit_timer, autocommit_fn, 0);
  3574. ic->io = dm_io_client_create();
  3575. if (IS_ERR(ic->io)) {
  3576. r = PTR_ERR(ic->io);
  3577. ic->io = NULL;
  3578. ti->error = "Cannot allocate dm io";
  3579. goto bad;
  3580. }
  3581. r = mempool_init_slab_pool(&ic->journal_io_mempool, JOURNAL_IO_MEMPOOL, journal_io_cache);
  3582. if (r) {
  3583. ti->error = "Cannot allocate mempool";
  3584. goto bad;
  3585. }
  3586. ic->metadata_wq = alloc_workqueue("dm-integrity-metadata",
  3587. WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
  3588. if (!ic->metadata_wq) {
  3589. ti->error = "Cannot allocate workqueue";
  3590. r = -ENOMEM;
  3591. goto bad;
  3592. }
  3593. /*
  3594. * If this workqueue were percpu, it would cause bio reordering
  3595. * and reduced performance.
  3596. */
  3597. ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  3598. if (!ic->wait_wq) {
  3599. ti->error = "Cannot allocate workqueue";
  3600. r = -ENOMEM;
  3601. goto bad;
  3602. }
  3603. ic->offload_wq = alloc_workqueue("dm-integrity-offload", WQ_MEM_RECLAIM,
  3604. METADATA_WORKQUEUE_MAX_ACTIVE);
  3605. if (!ic->offload_wq) {
  3606. ti->error = "Cannot allocate workqueue";
  3607. r = -ENOMEM;
  3608. goto bad;
  3609. }
  3610. ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
  3611. if (!ic->commit_wq) {
  3612. ti->error = "Cannot allocate workqueue";
  3613. r = -ENOMEM;
  3614. goto bad;
  3615. }
  3616. INIT_WORK(&ic->commit_work, integrity_commit);
  3617. if (ic->mode == 'J' || ic->mode == 'B') {
  3618. ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
  3619. if (!ic->writer_wq) {
  3620. ti->error = "Cannot allocate workqueue";
  3621. r = -ENOMEM;
  3622. goto bad;
  3623. }
  3624. INIT_WORK(&ic->writer_work, integrity_writer);
  3625. }
  3626. ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
  3627. if (!ic->sb) {
  3628. r = -ENOMEM;
  3629. ti->error = "Cannot allocate superblock area";
  3630. goto bad;
  3631. }
  3632. r = sync_rw_sb(ic, REQ_OP_READ);
  3633. if (r) {
  3634. ti->error = "Error reading superblock";
  3635. goto bad;
  3636. }
  3637. should_write_sb = false;
  3638. if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
  3639. if (ic->mode != 'R') {
  3640. if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) {
  3641. r = -EINVAL;
  3642. ti->error = "The device is not initialized";
  3643. goto bad;
  3644. }
  3645. }
  3646. r = initialize_superblock(ic, journal_sectors, interleave_sectors);
  3647. if (r) {
  3648. ti->error = "Could not initialize superblock";
  3649. goto bad;
  3650. }
  3651. if (ic->mode != 'R')
  3652. should_write_sb = true;
  3653. }
  3654. if (!ic->sb->version || ic->sb->version > SB_VERSION_5) {
  3655. r = -EINVAL;
  3656. ti->error = "Unknown version";
  3657. goto bad;
  3658. }
  3659. if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
  3660. r = -EINVAL;
  3661. ti->error = "Tag size doesn't match the information in superblock";
  3662. goto bad;
  3663. }
  3664. if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) {
  3665. r = -EINVAL;
  3666. ti->error = "Block size doesn't match the information in superblock";
  3667. goto bad;
  3668. }
  3669. if (!le32_to_cpu(ic->sb->journal_sections)) {
  3670. r = -EINVAL;
  3671. ti->error = "Corrupted superblock, journal_sections is 0";
  3672. goto bad;
  3673. }
  3674. /* make sure that ti->max_io_len doesn't overflow */
  3675. if (!ic->meta_dev) {
  3676. if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS ||
  3677. ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) {
  3678. r = -EINVAL;
  3679. ti->error = "Invalid interleave_sectors in the superblock";
  3680. goto bad;
  3681. }
  3682. } else {
  3683. if (ic->sb->log2_interleave_sectors) {
  3684. r = -EINVAL;
  3685. ti->error = "Invalid interleave_sectors in the superblock";
  3686. goto bad;
  3687. }
  3688. }
  3689. if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
  3690. r = -EINVAL;
  3691. ti->error = "Journal mac mismatch";
  3692. goto bad;
  3693. }
  3694. get_provided_data_sectors(ic);
  3695. if (!ic->provided_data_sectors) {
  3696. r = -EINVAL;
  3697. ti->error = "The device is too small";
  3698. goto bad;
  3699. }
  3700. try_smaller_buffer:
  3701. r = calculate_device_limits(ic);
  3702. if (r) {
  3703. if (ic->meta_dev) {
  3704. if (ic->log2_buffer_sectors > 3) {
  3705. ic->log2_buffer_sectors--;
  3706. goto try_smaller_buffer;
  3707. }
  3708. }
  3709. ti->error = "The device is too small";
  3710. goto bad;
  3711. }
  3712. if (log2_sectors_per_bitmap_bit < 0)
  3713. log2_sectors_per_bitmap_bit = __fls(DEFAULT_SECTORS_PER_BITMAP_BIT);
  3714. if (log2_sectors_per_bitmap_bit < ic->sb->log2_sectors_per_block)
  3715. log2_sectors_per_bitmap_bit = ic->sb->log2_sectors_per_block;
  3716. bits_in_journal = ((__u64)ic->journal_section_sectors * ic->journal_sections) << (SECTOR_SHIFT + 3);
  3717. if (bits_in_journal > UINT_MAX)
  3718. bits_in_journal = UINT_MAX;
  3719. while (bits_in_journal < (ic->provided_data_sectors + ((sector_t)1 << log2_sectors_per_bitmap_bit) - 1) >> log2_sectors_per_bitmap_bit)
  3720. log2_sectors_per_bitmap_bit++;
  3721. log2_blocks_per_bitmap_bit = log2_sectors_per_bitmap_bit - ic->sb->log2_sectors_per_block;
  3722. ic->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
  3723. if (should_write_sb) {
  3724. ic->sb->log2_blocks_per_bitmap_bit = log2_blocks_per_bitmap_bit;
  3725. }
  3726. n_bitmap_bits = ((ic->provided_data_sectors >> ic->sb->log2_sectors_per_block)
  3727. + (((sector_t)1 << log2_blocks_per_bitmap_bit) - 1)) >> log2_blocks_per_bitmap_bit;
  3728. ic->n_bitmap_blocks = DIV_ROUND_UP(n_bitmap_bits, BITMAP_BLOCK_SIZE * 8);
  3729. if (!ic->meta_dev)
  3730. ic->log2_buffer_sectors = min(ic->log2_buffer_sectors, (__u8)__ffs(ic->metadata_run));
  3731. if (ti->len > ic->provided_data_sectors) {
  3732. r = -EINVAL;
  3733. ti->error = "Not enough provided sectors for requested mapping size";
  3734. goto bad;
  3735. }
  3736. threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
  3737. threshold += 50;
  3738. do_div(threshold, 100);
  3739. ic->free_sectors_threshold = threshold;
  3740. DEBUG_print("initialized:\n");
  3741. DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
  3742. DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
  3743. DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
  3744. DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
  3745. DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
  3746. DEBUG_print(" journal_sections %u\n", (unsigned int)le32_to_cpu(ic->sb->journal_sections));
  3747. DEBUG_print(" journal_entries %u\n", ic->journal_entries);
  3748. DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
  3749. DEBUG_print(" data_device_sectors 0x%llx\n", bdev_nr_sectors(ic->dev->bdev));
  3750. DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
  3751. DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
  3752. DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
  3753. DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", ic->provided_data_sectors, ic->provided_data_sectors);
  3754. DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
  3755. DEBUG_print(" bits_in_journal %llu\n", bits_in_journal);
  3756. if (ic->recalculate_flag && !(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))) {
  3757. ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
  3758. ic->sb->recalc_sector = cpu_to_le64(0);
  3759. }
  3760. if (ic->internal_hash) {
  3761. size_t recalc_tags_size;
  3762. ic->recalc_wq = alloc_workqueue("dm-integrity-recalc", WQ_MEM_RECLAIM, 1);
  3763. if (!ic->recalc_wq ) {
  3764. ti->error = "Cannot allocate workqueue";
  3765. r = -ENOMEM;
  3766. goto bad;
  3767. }
  3768. INIT_WORK(&ic->recalc_work, integrity_recalc);
  3769. ic->recalc_buffer = vmalloc(RECALC_SECTORS << SECTOR_SHIFT);
  3770. if (!ic->recalc_buffer) {
  3771. ti->error = "Cannot allocate buffer for recalculating";
  3772. r = -ENOMEM;
  3773. goto bad;
  3774. }
  3775. recalc_tags_size = (RECALC_SECTORS >> ic->sb->log2_sectors_per_block) * ic->tag_size;
  3776. if (crypto_shash_digestsize(ic->internal_hash) > ic->tag_size)
  3777. recalc_tags_size += crypto_shash_digestsize(ic->internal_hash) - ic->tag_size;
  3778. ic->recalc_tags = kvmalloc(recalc_tags_size, GFP_KERNEL);
  3779. if (!ic->recalc_tags) {
  3780. ti->error = "Cannot allocate tags for recalculating";
  3781. r = -ENOMEM;
  3782. goto bad;
  3783. }
  3784. } else {
  3785. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
  3786. ti->error = "Recalculate can only be specified with internal_hash";
  3787. r = -EINVAL;
  3788. goto bad;
  3789. }
  3790. }
  3791. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
  3792. le64_to_cpu(ic->sb->recalc_sector) < ic->provided_data_sectors &&
  3793. dm_integrity_disable_recalculate(ic)) {
  3794. ti->error = "Recalculating with HMAC is disabled for security reasons - if you really need it, use the argument \"legacy_recalculate\"";
  3795. r = -EOPNOTSUPP;
  3796. goto bad;
  3797. }
  3798. ic->bufio = dm_bufio_client_create(ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev,
  3799. 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors), 1, 0, NULL, NULL, 0);
  3800. if (IS_ERR(ic->bufio)) {
  3801. r = PTR_ERR(ic->bufio);
  3802. ti->error = "Cannot initialize dm-bufio";
  3803. ic->bufio = NULL;
  3804. goto bad;
  3805. }
  3806. dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
  3807. if (ic->mode != 'R') {
  3808. r = create_journal(ic, &ti->error);
  3809. if (r)
  3810. goto bad;
  3811. }
  3812. if (ic->mode == 'B') {
  3813. unsigned int i;
  3814. unsigned int n_bitmap_pages = DIV_ROUND_UP(ic->n_bitmap_blocks, PAGE_SIZE / BITMAP_BLOCK_SIZE);
  3815. ic->recalc_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
  3816. if (!ic->recalc_bitmap) {
  3817. r = -ENOMEM;
  3818. goto bad;
  3819. }
  3820. ic->may_write_bitmap = dm_integrity_alloc_page_list(n_bitmap_pages);
  3821. if (!ic->may_write_bitmap) {
  3822. r = -ENOMEM;
  3823. goto bad;
  3824. }
  3825. ic->bbs = kvmalloc_array(ic->n_bitmap_blocks, sizeof(struct bitmap_block_status), GFP_KERNEL);
  3826. if (!ic->bbs) {
  3827. r = -ENOMEM;
  3828. goto bad;
  3829. }
  3830. INIT_DELAYED_WORK(&ic->bitmap_flush_work, bitmap_flush_work);
  3831. for (i = 0; i < ic->n_bitmap_blocks; i++) {
  3832. struct bitmap_block_status *bbs = &ic->bbs[i];
  3833. unsigned int sector, pl_index, pl_offset;
  3834. INIT_WORK(&bbs->work, bitmap_block_work);
  3835. bbs->ic = ic;
  3836. bbs->idx = i;
  3837. bio_list_init(&bbs->bio_queue);
  3838. spin_lock_init(&bbs->bio_queue_lock);
  3839. sector = i * (BITMAP_BLOCK_SIZE >> SECTOR_SHIFT);
  3840. pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
  3841. pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
  3842. bbs->bitmap = lowmem_page_address(ic->journal[pl_index].page) + pl_offset;
  3843. }
  3844. }
  3845. if (should_write_sb) {
  3846. init_journal(ic, 0, ic->journal_sections, 0);
  3847. r = dm_integrity_failed(ic);
  3848. if (unlikely(r)) {
  3849. ti->error = "Error initializing journal";
  3850. goto bad;
  3851. }
  3852. r = sync_rw_sb(ic, REQ_OP_WRITE | REQ_FUA);
  3853. if (r) {
  3854. ti->error = "Error initializing superblock";
  3855. goto bad;
  3856. }
  3857. ic->just_formatted = true;
  3858. }
  3859. if (!ic->meta_dev) {
  3860. r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
  3861. if (r)
  3862. goto bad;
  3863. }
  3864. if (ic->mode == 'B') {
  3865. unsigned int max_io_len = ((sector_t)ic->sectors_per_block << ic->log2_blocks_per_bitmap_bit) * (BITMAP_BLOCK_SIZE * 8);
  3866. if (!max_io_len)
  3867. max_io_len = 1U << 31;
  3868. DEBUG_print("max_io_len: old %u, new %u\n", ti->max_io_len, max_io_len);
  3869. if (!ti->max_io_len || ti->max_io_len > max_io_len) {
  3870. r = dm_set_target_max_io_len(ti, max_io_len);
  3871. if (r)
  3872. goto bad;
  3873. }
  3874. }
  3875. if (!ic->internal_hash)
  3876. dm_integrity_set(ti, ic);
  3877. ti->num_flush_bios = 1;
  3878. ti->flush_supported = true;
  3879. if (ic->discard)
  3880. ti->num_discard_bios = 1;
  3881. dm_audit_log_ctr(DM_MSG_PREFIX, ti, 1);
  3882. return 0;
  3883. bad:
  3884. dm_audit_log_ctr(DM_MSG_PREFIX, ti, 0);
  3885. dm_integrity_dtr(ti);
  3886. return r;
  3887. }
  3888. static void dm_integrity_dtr(struct dm_target *ti)
  3889. {
  3890. struct dm_integrity_c *ic = ti->private;
  3891. BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
  3892. BUG_ON(!list_empty(&ic->wait_list));
  3893. if (ic->mode == 'B')
  3894. cancel_delayed_work_sync(&ic->bitmap_flush_work);
  3895. if (ic->metadata_wq)
  3896. destroy_workqueue(ic->metadata_wq);
  3897. if (ic->wait_wq)
  3898. destroy_workqueue(ic->wait_wq);
  3899. if (ic->offload_wq)
  3900. destroy_workqueue(ic->offload_wq);
  3901. if (ic->commit_wq)
  3902. destroy_workqueue(ic->commit_wq);
  3903. if (ic->writer_wq)
  3904. destroy_workqueue(ic->writer_wq);
  3905. if (ic->recalc_wq)
  3906. destroy_workqueue(ic->recalc_wq);
  3907. vfree(ic->recalc_buffer);
  3908. kvfree(ic->recalc_tags);
  3909. kvfree(ic->bbs);
  3910. if (ic->bufio)
  3911. dm_bufio_client_destroy(ic->bufio);
  3912. mempool_exit(&ic->journal_io_mempool);
  3913. if (ic->io)
  3914. dm_io_client_destroy(ic->io);
  3915. if (ic->dev)
  3916. dm_put_device(ti, ic->dev);
  3917. if (ic->meta_dev)
  3918. dm_put_device(ti, ic->meta_dev);
  3919. dm_integrity_free_page_list(ic->journal);
  3920. dm_integrity_free_page_list(ic->journal_io);
  3921. dm_integrity_free_page_list(ic->journal_xor);
  3922. dm_integrity_free_page_list(ic->recalc_bitmap);
  3923. dm_integrity_free_page_list(ic->may_write_bitmap);
  3924. if (ic->journal_scatterlist)
  3925. dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
  3926. if (ic->journal_io_scatterlist)
  3927. dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
  3928. if (ic->sk_requests) {
  3929. unsigned int i;
  3930. for (i = 0; i < ic->journal_sections; i++) {
  3931. struct skcipher_request *req = ic->sk_requests[i];
  3932. if (req) {
  3933. kfree_sensitive(req->iv);
  3934. skcipher_request_free(req);
  3935. }
  3936. }
  3937. kvfree(ic->sk_requests);
  3938. }
  3939. kvfree(ic->journal_tree);
  3940. if (ic->sb)
  3941. free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
  3942. if (ic->internal_hash)
  3943. crypto_free_shash(ic->internal_hash);
  3944. free_alg(&ic->internal_hash_alg);
  3945. if (ic->journal_crypt)
  3946. crypto_free_skcipher(ic->journal_crypt);
  3947. free_alg(&ic->journal_crypt_alg);
  3948. if (ic->journal_mac)
  3949. crypto_free_shash(ic->journal_mac);
  3950. free_alg(&ic->journal_mac_alg);
  3951. kfree(ic);
  3952. dm_audit_log_dtr(DM_MSG_PREFIX, ti, 1);
  3953. }
  3954. static struct target_type integrity_target = {
  3955. .name = "integrity",
  3956. .version = {1, 10, 0},
  3957. .module = THIS_MODULE,
  3958. .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
  3959. .ctr = dm_integrity_ctr,
  3960. .dtr = dm_integrity_dtr,
  3961. .map = dm_integrity_map,
  3962. .postsuspend = dm_integrity_postsuspend,
  3963. .resume = dm_integrity_resume,
  3964. .status = dm_integrity_status,
  3965. .iterate_devices = dm_integrity_iterate_devices,
  3966. .io_hints = dm_integrity_io_hints,
  3967. };
  3968. static int __init dm_integrity_init(void)
  3969. {
  3970. int r;
  3971. journal_io_cache = kmem_cache_create("integrity_journal_io",
  3972. sizeof(struct journal_io), 0, 0, NULL);
  3973. if (!journal_io_cache) {
  3974. DMERR("can't allocate journal io cache");
  3975. return -ENOMEM;
  3976. }
  3977. r = dm_register_target(&integrity_target);
  3978. if (r < 0) {
  3979. DMERR("register failed %d", r);
  3980. kmem_cache_destroy(journal_io_cache);
  3981. return r;
  3982. }
  3983. return 0;
  3984. }
  3985. static void __exit dm_integrity_exit(void)
  3986. {
  3987. dm_unregister_target(&integrity_target);
  3988. kmem_cache_destroy(journal_io_cache);
  3989. }
  3990. module_init(dm_integrity_init);
  3991. module_exit(dm_integrity_exit);
  3992. MODULE_AUTHOR("Milan Broz");
  3993. MODULE_AUTHOR("Mikulas Patocka");
  3994. MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
  3995. MODULE_LICENSE("GPL");