dm-integrity.c 153 KB

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