dm-integrity.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638
  1. /*
  2. * Copyright (C) 2016-2017 Red Hat, Inc. All rights reserved.
  3. * Copyright (C) 2016-2017 Milan Broz
  4. * Copyright (C) 2016-2017 Mikulas Patocka
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include <linux/compiler.h>
  9. #include <linux/module.h>
  10. #include <linux/device-mapper.h>
  11. #include <linux/dm-io.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/sort.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/delay.h>
  16. #include <linux/random.h>
  17. #include <crypto/hash.h>
  18. #include <crypto/skcipher.h>
  19. #include <linux/async_tx.h>
  20. #include <linux/dm-bufio.h>
  21. #define DM_MSG_PREFIX "integrity"
  22. #define DEFAULT_INTERLEAVE_SECTORS 32768
  23. #define DEFAULT_JOURNAL_SIZE_FACTOR 7
  24. #define DEFAULT_BUFFER_SECTORS 128
  25. #define DEFAULT_JOURNAL_WATERMARK 50
  26. #define DEFAULT_SYNC_MSEC 10000
  27. #define DEFAULT_MAX_JOURNAL_SECTORS 131072
  28. #define MIN_LOG2_INTERLEAVE_SECTORS 3
  29. #define MAX_LOG2_INTERLEAVE_SECTORS 31
  30. #define METADATA_WORKQUEUE_MAX_ACTIVE 16
  31. #define RECALC_SECTORS 8192
  32. #define RECALC_WRITE_SUPER 16
  33. /*
  34. * Warning - DEBUG_PRINT prints security-sensitive data to the log,
  35. * so it should not be enabled in the official kernel
  36. */
  37. //#define DEBUG_PRINT
  38. //#define INTERNAL_VERIFY
  39. /*
  40. * On disk structures
  41. */
  42. #define SB_MAGIC "integrt"
  43. #define SB_VERSION_1 1
  44. #define SB_VERSION_2 2
  45. #define SB_SECTORS 8
  46. #define MAX_SECTORS_PER_BLOCK 8
  47. struct superblock {
  48. __u8 magic[8];
  49. __u8 version;
  50. __u8 log2_interleave_sectors;
  51. __u16 integrity_tag_size;
  52. __u32 journal_sections;
  53. __u64 provided_data_sectors; /* userspace uses this value */
  54. __u32 flags;
  55. __u8 log2_sectors_per_block;
  56. __u8 pad[3];
  57. __u64 recalc_sector;
  58. };
  59. #define SB_FLAG_HAVE_JOURNAL_MAC 0x1
  60. #define SB_FLAG_RECALCULATING 0x2
  61. #define JOURNAL_ENTRY_ROUNDUP 8
  62. typedef __u64 commit_id_t;
  63. #define JOURNAL_MAC_PER_SECTOR 8
  64. struct journal_entry {
  65. union {
  66. struct {
  67. __u32 sector_lo;
  68. __u32 sector_hi;
  69. } s;
  70. __u64 sector;
  71. } u;
  72. commit_id_t last_bytes[0];
  73. /* __u8 tag[0]; */
  74. };
  75. #define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
  76. #if BITS_PER_LONG == 64
  77. #define journal_entry_set_sector(je, x) do { smp_wmb(); WRITE_ONCE((je)->u.sector, cpu_to_le64(x)); } while (0)
  78. #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
  79. #elif defined(CONFIG_LBDAF)
  80. #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)
  81. #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
  82. #else
  83. #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(0)); } while (0)
  84. #define journal_entry_get_sector(je) le32_to_cpu((je)->u.s.sector_lo)
  85. #endif
  86. #define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
  87. #define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
  88. #define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
  89. #define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
  90. #define JOURNAL_BLOCK_SECTORS 8
  91. #define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
  92. #define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
  93. struct journal_sector {
  94. __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR];
  95. __u8 mac[JOURNAL_MAC_PER_SECTOR];
  96. commit_id_t commit_id;
  97. };
  98. #define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK]))
  99. #define METADATA_PADDING_SECTORS 8
  100. #define N_COMMIT_IDS 4
  101. static unsigned char prev_commit_seq(unsigned char seq)
  102. {
  103. return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS;
  104. }
  105. static unsigned char next_commit_seq(unsigned char seq)
  106. {
  107. return (seq + 1) % N_COMMIT_IDS;
  108. }
  109. /*
  110. * In-memory structures
  111. */
  112. struct journal_node {
  113. struct rb_node node;
  114. sector_t sector;
  115. };
  116. struct alg_spec {
  117. char *alg_string;
  118. char *key_string;
  119. __u8 *key;
  120. unsigned key_size;
  121. };
  122. struct dm_integrity_c {
  123. struct dm_dev *dev;
  124. struct dm_dev *meta_dev;
  125. unsigned tag_size;
  126. __s8 log2_tag_size;
  127. sector_t start;
  128. mempool_t journal_io_mempool;
  129. struct dm_io_client *io;
  130. struct dm_bufio_client *bufio;
  131. struct workqueue_struct *metadata_wq;
  132. struct superblock *sb;
  133. unsigned journal_pages;
  134. struct page_list *journal;
  135. struct page_list *journal_io;
  136. struct page_list *journal_xor;
  137. struct crypto_skcipher *journal_crypt;
  138. struct scatterlist **journal_scatterlist;
  139. struct scatterlist **journal_io_scatterlist;
  140. struct skcipher_request **sk_requests;
  141. struct crypto_shash *journal_mac;
  142. struct journal_node *journal_tree;
  143. struct rb_root journal_tree_root;
  144. sector_t provided_data_sectors;
  145. unsigned short journal_entry_size;
  146. unsigned char journal_entries_per_sector;
  147. unsigned char journal_section_entries;
  148. unsigned short journal_section_sectors;
  149. unsigned journal_sections;
  150. unsigned journal_entries;
  151. sector_t data_device_sectors;
  152. sector_t meta_device_sectors;
  153. unsigned initial_sectors;
  154. unsigned metadata_run;
  155. __s8 log2_metadata_run;
  156. __u8 log2_buffer_sectors;
  157. __u8 sectors_per_block;
  158. unsigned char mode;
  159. int suspending;
  160. int failed;
  161. struct crypto_shash *internal_hash;
  162. /* these variables are locked with endio_wait.lock */
  163. struct rb_root in_progress;
  164. struct list_head wait_list;
  165. wait_queue_head_t endio_wait;
  166. struct workqueue_struct *wait_wq;
  167. unsigned char commit_seq;
  168. commit_id_t commit_ids[N_COMMIT_IDS];
  169. unsigned committed_section;
  170. unsigned n_committed_sections;
  171. unsigned uncommitted_section;
  172. unsigned n_uncommitted_sections;
  173. unsigned free_section;
  174. unsigned char free_section_entry;
  175. unsigned free_sectors;
  176. unsigned free_sectors_threshold;
  177. struct workqueue_struct *commit_wq;
  178. struct work_struct commit_work;
  179. struct workqueue_struct *writer_wq;
  180. struct work_struct writer_work;
  181. struct workqueue_struct *recalc_wq;
  182. struct work_struct recalc_work;
  183. u8 *recalc_buffer;
  184. u8 *recalc_tags;
  185. struct bio_list flush_bio_list;
  186. unsigned long autocommit_jiffies;
  187. struct timer_list autocommit_timer;
  188. unsigned autocommit_msec;
  189. wait_queue_head_t copy_to_journal_wait;
  190. struct completion crypto_backoff;
  191. bool journal_uptodate;
  192. bool just_formatted;
  193. struct alg_spec internal_hash_alg;
  194. struct alg_spec journal_crypt_alg;
  195. struct alg_spec journal_mac_alg;
  196. atomic64_t number_of_mismatches;
  197. };
  198. struct dm_integrity_range {
  199. sector_t logical_sector;
  200. unsigned n_sectors;
  201. bool waiting;
  202. union {
  203. struct rb_node node;
  204. struct {
  205. struct task_struct *task;
  206. struct list_head wait_entry;
  207. };
  208. };
  209. };
  210. struct dm_integrity_io {
  211. struct work_struct work;
  212. struct dm_integrity_c *ic;
  213. bool write;
  214. bool fua;
  215. struct dm_integrity_range range;
  216. sector_t metadata_block;
  217. unsigned metadata_offset;
  218. atomic_t in_flight;
  219. blk_status_t bi_status;
  220. struct completion *completion;
  221. struct gendisk *orig_bi_disk;
  222. u8 orig_bi_partno;
  223. bio_end_io_t *orig_bi_end_io;
  224. struct bio_integrity_payload *orig_bi_integrity;
  225. struct bvec_iter orig_bi_iter;
  226. };
  227. struct journal_completion {
  228. struct dm_integrity_c *ic;
  229. atomic_t in_flight;
  230. struct completion comp;
  231. };
  232. struct journal_io {
  233. struct dm_integrity_range range;
  234. struct journal_completion *comp;
  235. };
  236. static struct kmem_cache *journal_io_cache;
  237. #define JOURNAL_IO_MEMPOOL 32
  238. #ifdef DEBUG_PRINT
  239. #define DEBUG_print(x, ...) printk(KERN_DEBUG x, ##__VA_ARGS__)
  240. static void __DEBUG_bytes(__u8 *bytes, size_t len, const char *msg, ...)
  241. {
  242. va_list args;
  243. va_start(args, msg);
  244. vprintk(msg, args);
  245. va_end(args);
  246. if (len)
  247. pr_cont(":");
  248. while (len) {
  249. pr_cont(" %02x", *bytes);
  250. bytes++;
  251. len--;
  252. }
  253. pr_cont("\n");
  254. }
  255. #define DEBUG_bytes(bytes, len, msg, ...) __DEBUG_bytes(bytes, len, KERN_DEBUG msg, ##__VA_ARGS__)
  256. #else
  257. #define DEBUG_print(x, ...) do { } while (0)
  258. #define DEBUG_bytes(bytes, len, msg, ...) do { } while (0)
  259. #endif
  260. /*
  261. * DM Integrity profile, protection is performed layer above (dm-crypt)
  262. */
  263. static const struct blk_integrity_profile dm_integrity_profile = {
  264. .name = "DM-DIF-EXT-TAG",
  265. .generate_fn = NULL,
  266. .verify_fn = NULL,
  267. };
  268. static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map);
  269. static void integrity_bio_wait(struct work_struct *w);
  270. static void dm_integrity_dtr(struct dm_target *ti);
  271. static void dm_integrity_io_error(struct dm_integrity_c *ic, const char *msg, int err)
  272. {
  273. if (err == -EILSEQ)
  274. atomic64_inc(&ic->number_of_mismatches);
  275. if (!cmpxchg(&ic->failed, 0, err))
  276. DMERR("Error on %s: %d", msg, err);
  277. }
  278. static int dm_integrity_failed(struct dm_integrity_c *ic)
  279. {
  280. return READ_ONCE(ic->failed);
  281. }
  282. static commit_id_t dm_integrity_commit_id(struct dm_integrity_c *ic, unsigned i,
  283. unsigned j, unsigned char seq)
  284. {
  285. /*
  286. * Xor the number with section and sector, so that if a piece of
  287. * journal is written at wrong place, it is detected.
  288. */
  289. return ic->commit_ids[seq] ^ cpu_to_le64(((__u64)i << 32) ^ j);
  290. }
  291. static void get_area_and_offset(struct dm_integrity_c *ic, sector_t data_sector,
  292. sector_t *area, sector_t *offset)
  293. {
  294. if (!ic->meta_dev) {
  295. __u8 log2_interleave_sectors = ic->sb->log2_interleave_sectors;
  296. *area = data_sector >> log2_interleave_sectors;
  297. *offset = (unsigned)data_sector & ((1U << log2_interleave_sectors) - 1);
  298. } else {
  299. *area = 0;
  300. *offset = data_sector;
  301. }
  302. }
  303. #define sector_to_block(ic, n) \
  304. do { \
  305. BUG_ON((n) & (unsigned)((ic)->sectors_per_block - 1)); \
  306. (n) >>= (ic)->sb->log2_sectors_per_block; \
  307. } while (0)
  308. static __u64 get_metadata_sector_and_offset(struct dm_integrity_c *ic, sector_t area,
  309. sector_t offset, unsigned *metadata_offset)
  310. {
  311. __u64 ms;
  312. unsigned mo;
  313. ms = area << ic->sb->log2_interleave_sectors;
  314. if (likely(ic->log2_metadata_run >= 0))
  315. ms += area << ic->log2_metadata_run;
  316. else
  317. ms += area * ic->metadata_run;
  318. ms >>= ic->log2_buffer_sectors;
  319. sector_to_block(ic, offset);
  320. if (likely(ic->log2_tag_size >= 0)) {
  321. ms += offset >> (SECTOR_SHIFT + ic->log2_buffer_sectors - ic->log2_tag_size);
  322. mo = (offset << ic->log2_tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
  323. } else {
  324. ms += (__u64)offset * ic->tag_size >> (SECTOR_SHIFT + ic->log2_buffer_sectors);
  325. mo = (offset * ic->tag_size) & ((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - 1);
  326. }
  327. *metadata_offset = mo;
  328. return ms;
  329. }
  330. static sector_t get_data_sector(struct dm_integrity_c *ic, sector_t area, sector_t offset)
  331. {
  332. sector_t result;
  333. if (ic->meta_dev)
  334. return offset;
  335. result = area << ic->sb->log2_interleave_sectors;
  336. if (likely(ic->log2_metadata_run >= 0))
  337. result += (area + 1) << ic->log2_metadata_run;
  338. else
  339. result += (area + 1) * ic->metadata_run;
  340. result += (sector_t)ic->initial_sectors + offset;
  341. result += ic->start;
  342. return result;
  343. }
  344. static void wraparound_section(struct dm_integrity_c *ic, unsigned *sec_ptr)
  345. {
  346. if (unlikely(*sec_ptr >= ic->journal_sections))
  347. *sec_ptr -= ic->journal_sections;
  348. }
  349. static void sb_set_version(struct dm_integrity_c *ic)
  350. {
  351. if (ic->meta_dev || ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
  352. ic->sb->version = SB_VERSION_2;
  353. else
  354. ic->sb->version = SB_VERSION_1;
  355. }
  356. static int sync_rw_sb(struct dm_integrity_c *ic, int op, int op_flags)
  357. {
  358. struct dm_io_request io_req;
  359. struct dm_io_region io_loc;
  360. io_req.bi_op = op;
  361. io_req.bi_op_flags = op_flags;
  362. io_req.mem.type = DM_IO_KMEM;
  363. io_req.mem.ptr.addr = ic->sb;
  364. io_req.notify.fn = NULL;
  365. io_req.client = ic->io;
  366. io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
  367. io_loc.sector = ic->start;
  368. io_loc.count = SB_SECTORS;
  369. return dm_io(&io_req, 1, &io_loc, NULL);
  370. }
  371. static void access_journal_check(struct dm_integrity_c *ic, unsigned section, unsigned offset,
  372. bool e, const char *function)
  373. {
  374. #if defined(CONFIG_DM_DEBUG) || defined(INTERNAL_VERIFY)
  375. unsigned limit = e ? ic->journal_section_entries : ic->journal_section_sectors;
  376. if (unlikely(section >= ic->journal_sections) ||
  377. unlikely(offset >= limit)) {
  378. printk(KERN_CRIT "%s: invalid access at (%u,%u), limit (%u,%u)\n",
  379. function, section, offset, ic->journal_sections, limit);
  380. BUG();
  381. }
  382. #endif
  383. }
  384. static void page_list_location(struct dm_integrity_c *ic, unsigned section, unsigned offset,
  385. unsigned *pl_index, unsigned *pl_offset)
  386. {
  387. unsigned sector;
  388. access_journal_check(ic, section, offset, false, "page_list_location");
  389. sector = section * ic->journal_section_sectors + offset;
  390. *pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
  391. *pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
  392. }
  393. static struct journal_sector *access_page_list(struct dm_integrity_c *ic, struct page_list *pl,
  394. unsigned section, unsigned offset, unsigned *n_sectors)
  395. {
  396. unsigned pl_index, pl_offset;
  397. char *va;
  398. page_list_location(ic, section, offset, &pl_index, &pl_offset);
  399. if (n_sectors)
  400. *n_sectors = (PAGE_SIZE - pl_offset) >> SECTOR_SHIFT;
  401. va = lowmem_page_address(pl[pl_index].page);
  402. return (struct journal_sector *)(va + pl_offset);
  403. }
  404. static struct journal_sector *access_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset)
  405. {
  406. return access_page_list(ic, ic->journal, section, offset, NULL);
  407. }
  408. static struct journal_entry *access_journal_entry(struct dm_integrity_c *ic, unsigned section, unsigned n)
  409. {
  410. unsigned rel_sector, offset;
  411. struct journal_sector *js;
  412. access_journal_check(ic, section, n, true, "access_journal_entry");
  413. rel_sector = n % JOURNAL_BLOCK_SECTORS;
  414. offset = n / JOURNAL_BLOCK_SECTORS;
  415. js = access_journal(ic, section, rel_sector);
  416. return (struct journal_entry *)((char *)js + offset * ic->journal_entry_size);
  417. }
  418. static struct journal_sector *access_journal_data(struct dm_integrity_c *ic, unsigned section, unsigned n)
  419. {
  420. n <<= ic->sb->log2_sectors_per_block;
  421. n += JOURNAL_BLOCK_SECTORS;
  422. access_journal_check(ic, section, n, false, "access_journal_data");
  423. return access_journal(ic, section, n);
  424. }
  425. static void section_mac(struct dm_integrity_c *ic, unsigned section, __u8 result[JOURNAL_MAC_SIZE])
  426. {
  427. SHASH_DESC_ON_STACK(desc, ic->journal_mac);
  428. int r;
  429. unsigned j, size;
  430. desc->tfm = ic->journal_mac;
  431. desc->flags = 0;
  432. r = crypto_shash_init(desc);
  433. if (unlikely(r)) {
  434. dm_integrity_io_error(ic, "crypto_shash_init", r);
  435. goto err;
  436. }
  437. for (j = 0; j < ic->journal_section_entries; j++) {
  438. struct journal_entry *je = access_journal_entry(ic, section, j);
  439. r = crypto_shash_update(desc, (__u8 *)&je->u.sector, sizeof je->u.sector);
  440. if (unlikely(r)) {
  441. dm_integrity_io_error(ic, "crypto_shash_update", r);
  442. goto err;
  443. }
  444. }
  445. size = crypto_shash_digestsize(ic->journal_mac);
  446. if (likely(size <= JOURNAL_MAC_SIZE)) {
  447. r = crypto_shash_final(desc, result);
  448. if (unlikely(r)) {
  449. dm_integrity_io_error(ic, "crypto_shash_final", r);
  450. goto err;
  451. }
  452. memset(result + size, 0, JOURNAL_MAC_SIZE - size);
  453. } else {
  454. __u8 digest[size];
  455. r = crypto_shash_final(desc, digest);
  456. if (unlikely(r)) {
  457. dm_integrity_io_error(ic, "crypto_shash_final", r);
  458. goto err;
  459. }
  460. memcpy(result, digest, JOURNAL_MAC_SIZE);
  461. }
  462. return;
  463. err:
  464. memset(result, 0, JOURNAL_MAC_SIZE);
  465. }
  466. static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr)
  467. {
  468. __u8 result[JOURNAL_MAC_SIZE];
  469. unsigned j;
  470. if (!ic->journal_mac)
  471. return;
  472. section_mac(ic, section, result);
  473. for (j = 0; j < JOURNAL_BLOCK_SECTORS; j++) {
  474. struct journal_sector *js = access_journal(ic, section, j);
  475. if (likely(wr))
  476. memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
  477. else {
  478. if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR))
  479. dm_integrity_io_error(ic, "journal mac", -EILSEQ);
  480. }
  481. }
  482. }
  483. static void complete_journal_op(void *context)
  484. {
  485. struct journal_completion *comp = context;
  486. BUG_ON(!atomic_read(&comp->in_flight));
  487. if (likely(atomic_dec_and_test(&comp->in_flight)))
  488. complete(&comp->comp);
  489. }
  490. static void xor_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
  491. unsigned n_sections, struct journal_completion *comp)
  492. {
  493. struct async_submit_ctl submit;
  494. size_t n_bytes = (size_t)(n_sections * ic->journal_section_sectors) << SECTOR_SHIFT;
  495. unsigned pl_index, pl_offset, section_index;
  496. struct page_list *source_pl, *target_pl;
  497. if (likely(encrypt)) {
  498. source_pl = ic->journal;
  499. target_pl = ic->journal_io;
  500. } else {
  501. source_pl = ic->journal_io;
  502. target_pl = ic->journal;
  503. }
  504. page_list_location(ic, section, 0, &pl_index, &pl_offset);
  505. atomic_add(roundup(pl_offset + n_bytes, PAGE_SIZE) >> PAGE_SHIFT, &comp->in_flight);
  506. init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL, complete_journal_op, comp, NULL);
  507. section_index = pl_index;
  508. do {
  509. size_t this_step;
  510. struct page *src_pages[2];
  511. struct page *dst_page;
  512. while (unlikely(pl_index == section_index)) {
  513. unsigned dummy;
  514. if (likely(encrypt))
  515. rw_section_mac(ic, section, true);
  516. section++;
  517. n_sections--;
  518. if (!n_sections)
  519. break;
  520. page_list_location(ic, section, 0, &section_index, &dummy);
  521. }
  522. this_step = min(n_bytes, (size_t)PAGE_SIZE - pl_offset);
  523. dst_page = target_pl[pl_index].page;
  524. src_pages[0] = source_pl[pl_index].page;
  525. src_pages[1] = ic->journal_xor[pl_index].page;
  526. async_xor(dst_page, src_pages, pl_offset, 2, this_step, &submit);
  527. pl_index++;
  528. pl_offset = 0;
  529. n_bytes -= this_step;
  530. } while (n_bytes);
  531. BUG_ON(n_sections);
  532. async_tx_issue_pending_all();
  533. }
  534. static void complete_journal_encrypt(struct crypto_async_request *req, int err)
  535. {
  536. struct journal_completion *comp = req->data;
  537. if (unlikely(err)) {
  538. if (likely(err == -EINPROGRESS)) {
  539. complete(&comp->ic->crypto_backoff);
  540. return;
  541. }
  542. dm_integrity_io_error(comp->ic, "asynchronous encrypt", err);
  543. }
  544. complete_journal_op(comp);
  545. }
  546. static bool do_crypt(bool encrypt, struct skcipher_request *req, struct journal_completion *comp)
  547. {
  548. int r;
  549. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  550. complete_journal_encrypt, comp);
  551. if (likely(encrypt))
  552. r = crypto_skcipher_encrypt(req);
  553. else
  554. r = crypto_skcipher_decrypt(req);
  555. if (likely(!r))
  556. return false;
  557. if (likely(r == -EINPROGRESS))
  558. return true;
  559. if (likely(r == -EBUSY)) {
  560. wait_for_completion(&comp->ic->crypto_backoff);
  561. reinit_completion(&comp->ic->crypto_backoff);
  562. return true;
  563. }
  564. dm_integrity_io_error(comp->ic, "encrypt", r);
  565. return false;
  566. }
  567. static void crypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
  568. unsigned n_sections, struct journal_completion *comp)
  569. {
  570. struct scatterlist **source_sg;
  571. struct scatterlist **target_sg;
  572. atomic_add(2, &comp->in_flight);
  573. if (likely(encrypt)) {
  574. source_sg = ic->journal_scatterlist;
  575. target_sg = ic->journal_io_scatterlist;
  576. } else {
  577. source_sg = ic->journal_io_scatterlist;
  578. target_sg = ic->journal_scatterlist;
  579. }
  580. do {
  581. struct skcipher_request *req;
  582. unsigned ivsize;
  583. char *iv;
  584. if (likely(encrypt))
  585. rw_section_mac(ic, section, true);
  586. req = ic->sk_requests[section];
  587. ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
  588. iv = req->iv;
  589. memcpy(iv, iv + ivsize, ivsize);
  590. req->src = source_sg[section];
  591. req->dst = target_sg[section];
  592. if (unlikely(do_crypt(encrypt, req, comp)))
  593. atomic_inc(&comp->in_flight);
  594. section++;
  595. n_sections--;
  596. } while (n_sections);
  597. atomic_dec(&comp->in_flight);
  598. complete_journal_op(comp);
  599. }
  600. static void encrypt_journal(struct dm_integrity_c *ic, bool encrypt, unsigned section,
  601. unsigned n_sections, struct journal_completion *comp)
  602. {
  603. if (ic->journal_xor)
  604. return xor_journal(ic, encrypt, section, n_sections, comp);
  605. else
  606. return crypt_journal(ic, encrypt, section, n_sections, comp);
  607. }
  608. static void complete_journal_io(unsigned long error, void *context)
  609. {
  610. struct journal_completion *comp = context;
  611. if (unlikely(error != 0))
  612. dm_integrity_io_error(comp->ic, "writing journal", -EIO);
  613. complete_journal_op(comp);
  614. }
  615. static void rw_journal(struct dm_integrity_c *ic, int op, int op_flags, unsigned section,
  616. unsigned n_sections, struct journal_completion *comp)
  617. {
  618. struct dm_io_request io_req;
  619. struct dm_io_region io_loc;
  620. unsigned sector, n_sectors, pl_index, pl_offset;
  621. int r;
  622. if (unlikely(dm_integrity_failed(ic))) {
  623. if (comp)
  624. complete_journal_io(-1UL, comp);
  625. return;
  626. }
  627. sector = section * ic->journal_section_sectors;
  628. n_sectors = n_sections * ic->journal_section_sectors;
  629. pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
  630. pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
  631. io_req.bi_op = op;
  632. io_req.bi_op_flags = op_flags;
  633. io_req.mem.type = DM_IO_PAGE_LIST;
  634. if (ic->journal_io)
  635. io_req.mem.ptr.pl = &ic->journal_io[pl_index];
  636. else
  637. io_req.mem.ptr.pl = &ic->journal[pl_index];
  638. io_req.mem.offset = pl_offset;
  639. if (likely(comp != NULL)) {
  640. io_req.notify.fn = complete_journal_io;
  641. io_req.notify.context = comp;
  642. } else {
  643. io_req.notify.fn = NULL;
  644. }
  645. io_req.client = ic->io;
  646. io_loc.bdev = ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev;
  647. io_loc.sector = ic->start + SB_SECTORS + sector;
  648. io_loc.count = n_sectors;
  649. r = dm_io(&io_req, 1, &io_loc, NULL);
  650. if (unlikely(r)) {
  651. dm_integrity_io_error(ic, op == REQ_OP_READ ? "reading journal" : "writing journal", r);
  652. if (comp) {
  653. WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
  654. complete_journal_io(-1UL, comp);
  655. }
  656. }
  657. }
  658. static void write_journal(struct dm_integrity_c *ic, unsigned commit_start, unsigned commit_sections)
  659. {
  660. struct journal_completion io_comp;
  661. struct journal_completion crypt_comp_1;
  662. struct journal_completion crypt_comp_2;
  663. unsigned i;
  664. io_comp.ic = ic;
  665. init_completion(&io_comp.comp);
  666. if (commit_start + commit_sections <= ic->journal_sections) {
  667. io_comp.in_flight = (atomic_t)ATOMIC_INIT(1);
  668. if (ic->journal_io) {
  669. crypt_comp_1.ic = ic;
  670. init_completion(&crypt_comp_1.comp);
  671. crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
  672. encrypt_journal(ic, true, commit_start, commit_sections, &crypt_comp_1);
  673. wait_for_completion_io(&crypt_comp_1.comp);
  674. } else {
  675. for (i = 0; i < commit_sections; i++)
  676. rw_section_mac(ic, commit_start + i, true);
  677. }
  678. rw_journal(ic, REQ_OP_WRITE, REQ_FUA | REQ_SYNC, commit_start,
  679. commit_sections, &io_comp);
  680. } else {
  681. unsigned to_end;
  682. io_comp.in_flight = (atomic_t)ATOMIC_INIT(2);
  683. to_end = ic->journal_sections - commit_start;
  684. if (ic->journal_io) {
  685. crypt_comp_1.ic = ic;
  686. init_completion(&crypt_comp_1.comp);
  687. crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
  688. encrypt_journal(ic, true, commit_start, to_end, &crypt_comp_1);
  689. if (try_wait_for_completion(&crypt_comp_1.comp)) {
  690. rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
  691. reinit_completion(&crypt_comp_1.comp);
  692. crypt_comp_1.in_flight = (atomic_t)ATOMIC_INIT(0);
  693. encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_1);
  694. wait_for_completion_io(&crypt_comp_1.comp);
  695. } else {
  696. crypt_comp_2.ic = ic;
  697. init_completion(&crypt_comp_2.comp);
  698. crypt_comp_2.in_flight = (atomic_t)ATOMIC_INIT(0);
  699. encrypt_journal(ic, true, 0, commit_sections - to_end, &crypt_comp_2);
  700. wait_for_completion_io(&crypt_comp_1.comp);
  701. rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
  702. wait_for_completion_io(&crypt_comp_2.comp);
  703. }
  704. } else {
  705. for (i = 0; i < to_end; i++)
  706. rw_section_mac(ic, commit_start + i, true);
  707. rw_journal(ic, REQ_OP_WRITE, REQ_FUA, commit_start, to_end, &io_comp);
  708. for (i = 0; i < commit_sections - to_end; i++)
  709. rw_section_mac(ic, i, true);
  710. }
  711. rw_journal(ic, REQ_OP_WRITE, REQ_FUA, 0, commit_sections - to_end, &io_comp);
  712. }
  713. wait_for_completion_io(&io_comp.comp);
  714. }
  715. static void copy_from_journal(struct dm_integrity_c *ic, unsigned section, unsigned offset,
  716. unsigned n_sectors, sector_t target, io_notify_fn fn, void *data)
  717. {
  718. struct dm_io_request io_req;
  719. struct dm_io_region io_loc;
  720. int r;
  721. unsigned sector, pl_index, pl_offset;
  722. BUG_ON((target | n_sectors | offset) & (unsigned)(ic->sectors_per_block - 1));
  723. if (unlikely(dm_integrity_failed(ic))) {
  724. fn(-1UL, data);
  725. return;
  726. }
  727. sector = section * ic->journal_section_sectors + JOURNAL_BLOCK_SECTORS + offset;
  728. pl_index = sector >> (PAGE_SHIFT - SECTOR_SHIFT);
  729. pl_offset = (sector << SECTOR_SHIFT) & (PAGE_SIZE - 1);
  730. io_req.bi_op = REQ_OP_WRITE;
  731. io_req.bi_op_flags = 0;
  732. io_req.mem.type = DM_IO_PAGE_LIST;
  733. io_req.mem.ptr.pl = &ic->journal[pl_index];
  734. io_req.mem.offset = pl_offset;
  735. io_req.notify.fn = fn;
  736. io_req.notify.context = data;
  737. io_req.client = ic->io;
  738. io_loc.bdev = ic->dev->bdev;
  739. io_loc.sector = target;
  740. io_loc.count = n_sectors;
  741. r = dm_io(&io_req, 1, &io_loc, NULL);
  742. if (unlikely(r)) {
  743. WARN_ONCE(1, "asynchronous dm_io failed: %d", r);
  744. fn(-1UL, data);
  745. }
  746. }
  747. static bool ranges_overlap(struct dm_integrity_range *range1, struct dm_integrity_range *range2)
  748. {
  749. return range1->logical_sector < range2->logical_sector + range2->n_sectors &&
  750. range2->logical_sector + range2->n_sectors > range2->logical_sector;
  751. }
  752. static bool add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range, bool check_waiting)
  753. {
  754. struct rb_node **n = &ic->in_progress.rb_node;
  755. struct rb_node *parent;
  756. BUG_ON((new_range->logical_sector | new_range->n_sectors) & (unsigned)(ic->sectors_per_block - 1));
  757. if (likely(check_waiting)) {
  758. struct dm_integrity_range *range;
  759. list_for_each_entry(range, &ic->wait_list, wait_entry) {
  760. if (unlikely(ranges_overlap(range, new_range)))
  761. return false;
  762. }
  763. }
  764. parent = NULL;
  765. while (*n) {
  766. struct dm_integrity_range *range = container_of(*n, struct dm_integrity_range, node);
  767. parent = *n;
  768. if (new_range->logical_sector + new_range->n_sectors <= range->logical_sector) {
  769. n = &range->node.rb_left;
  770. } else if (new_range->logical_sector >= range->logical_sector + range->n_sectors) {
  771. n = &range->node.rb_right;
  772. } else {
  773. return false;
  774. }
  775. }
  776. rb_link_node(&new_range->node, parent, n);
  777. rb_insert_color(&new_range->node, &ic->in_progress);
  778. return true;
  779. }
  780. static void remove_range_unlocked(struct dm_integrity_c *ic, struct dm_integrity_range *range)
  781. {
  782. rb_erase(&range->node, &ic->in_progress);
  783. while (unlikely(!list_empty(&ic->wait_list))) {
  784. struct dm_integrity_range *last_range =
  785. list_first_entry(&ic->wait_list, struct dm_integrity_range, wait_entry);
  786. struct task_struct *last_range_task;
  787. if (!ranges_overlap(range, last_range))
  788. break;
  789. last_range_task = last_range->task;
  790. list_del(&last_range->wait_entry);
  791. if (!add_new_range(ic, last_range, false)) {
  792. last_range->task = last_range_task;
  793. list_add(&last_range->wait_entry, &ic->wait_list);
  794. break;
  795. }
  796. last_range->waiting = false;
  797. wake_up_process(last_range_task);
  798. }
  799. }
  800. static void remove_range(struct dm_integrity_c *ic, struct dm_integrity_range *range)
  801. {
  802. unsigned long flags;
  803. spin_lock_irqsave(&ic->endio_wait.lock, flags);
  804. remove_range_unlocked(ic, range);
  805. spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
  806. }
  807. static void wait_and_add_new_range(struct dm_integrity_c *ic, struct dm_integrity_range *new_range)
  808. {
  809. new_range->waiting = true;
  810. list_add_tail(&new_range->wait_entry, &ic->wait_list);
  811. new_range->task = current;
  812. do {
  813. __set_current_state(TASK_UNINTERRUPTIBLE);
  814. spin_unlock_irq(&ic->endio_wait.lock);
  815. io_schedule();
  816. spin_lock_irq(&ic->endio_wait.lock);
  817. } while (unlikely(new_range->waiting));
  818. }
  819. static void init_journal_node(struct journal_node *node)
  820. {
  821. RB_CLEAR_NODE(&node->node);
  822. node->sector = (sector_t)-1;
  823. }
  824. static void add_journal_node(struct dm_integrity_c *ic, struct journal_node *node, sector_t sector)
  825. {
  826. struct rb_node **link;
  827. struct rb_node *parent;
  828. node->sector = sector;
  829. BUG_ON(!RB_EMPTY_NODE(&node->node));
  830. link = &ic->journal_tree_root.rb_node;
  831. parent = NULL;
  832. while (*link) {
  833. struct journal_node *j;
  834. parent = *link;
  835. j = container_of(parent, struct journal_node, node);
  836. if (sector < j->sector)
  837. link = &j->node.rb_left;
  838. else
  839. link = &j->node.rb_right;
  840. }
  841. rb_link_node(&node->node, parent, link);
  842. rb_insert_color(&node->node, &ic->journal_tree_root);
  843. }
  844. static void remove_journal_node(struct dm_integrity_c *ic, struct journal_node *node)
  845. {
  846. BUG_ON(RB_EMPTY_NODE(&node->node));
  847. rb_erase(&node->node, &ic->journal_tree_root);
  848. init_journal_node(node);
  849. }
  850. #define NOT_FOUND (-1U)
  851. static unsigned find_journal_node(struct dm_integrity_c *ic, sector_t sector, sector_t *next_sector)
  852. {
  853. struct rb_node *n = ic->journal_tree_root.rb_node;
  854. unsigned found = NOT_FOUND;
  855. *next_sector = (sector_t)-1;
  856. while (n) {
  857. struct journal_node *j = container_of(n, struct journal_node, node);
  858. if (sector == j->sector) {
  859. found = j - ic->journal_tree;
  860. }
  861. if (sector < j->sector) {
  862. *next_sector = j->sector;
  863. n = j->node.rb_left;
  864. } else {
  865. n = j->node.rb_right;
  866. }
  867. }
  868. return found;
  869. }
  870. static bool test_journal_node(struct dm_integrity_c *ic, unsigned pos, sector_t sector)
  871. {
  872. struct journal_node *node, *next_node;
  873. struct rb_node *next;
  874. if (unlikely(pos >= ic->journal_entries))
  875. return false;
  876. node = &ic->journal_tree[pos];
  877. if (unlikely(RB_EMPTY_NODE(&node->node)))
  878. return false;
  879. if (unlikely(node->sector != sector))
  880. return false;
  881. next = rb_next(&node->node);
  882. if (unlikely(!next))
  883. return true;
  884. next_node = container_of(next, struct journal_node, node);
  885. return next_node->sector != sector;
  886. }
  887. static bool find_newer_committed_node(struct dm_integrity_c *ic, struct journal_node *node)
  888. {
  889. struct rb_node *next;
  890. struct journal_node *next_node;
  891. unsigned next_section;
  892. BUG_ON(RB_EMPTY_NODE(&node->node));
  893. next = rb_next(&node->node);
  894. if (unlikely(!next))
  895. return false;
  896. next_node = container_of(next, struct journal_node, node);
  897. if (next_node->sector != node->sector)
  898. return false;
  899. next_section = (unsigned)(next_node - ic->journal_tree) / ic->journal_section_entries;
  900. if (next_section >= ic->committed_section &&
  901. next_section < ic->committed_section + ic->n_committed_sections)
  902. return true;
  903. if (next_section + ic->journal_sections < ic->committed_section + ic->n_committed_sections)
  904. return true;
  905. return false;
  906. }
  907. #define TAG_READ 0
  908. #define TAG_WRITE 1
  909. #define TAG_CMP 2
  910. static int dm_integrity_rw_tag(struct dm_integrity_c *ic, unsigned char *tag, sector_t *metadata_block,
  911. unsigned *metadata_offset, unsigned total_size, int op)
  912. {
  913. do {
  914. unsigned char *data, *dp;
  915. struct dm_buffer *b;
  916. unsigned to_copy;
  917. int r;
  918. r = dm_integrity_failed(ic);
  919. if (unlikely(r))
  920. return r;
  921. data = dm_bufio_read(ic->bufio, *metadata_block, &b);
  922. if (unlikely(IS_ERR(data)))
  923. return PTR_ERR(data);
  924. to_copy = min((1U << SECTOR_SHIFT << ic->log2_buffer_sectors) - *metadata_offset, total_size);
  925. dp = data + *metadata_offset;
  926. if (op == TAG_READ) {
  927. memcpy(tag, dp, to_copy);
  928. } else if (op == TAG_WRITE) {
  929. memcpy(dp, tag, to_copy);
  930. dm_bufio_mark_partial_buffer_dirty(b, *metadata_offset, *metadata_offset + to_copy);
  931. } else {
  932. /* e.g.: op == TAG_CMP */
  933. if (unlikely(memcmp(dp, tag, to_copy))) {
  934. unsigned i;
  935. for (i = 0; i < to_copy; i++) {
  936. if (dp[i] != tag[i])
  937. break;
  938. total_size--;
  939. }
  940. dm_bufio_release(b);
  941. return total_size;
  942. }
  943. }
  944. dm_bufio_release(b);
  945. tag += to_copy;
  946. *metadata_offset += to_copy;
  947. if (unlikely(*metadata_offset == 1U << SECTOR_SHIFT << ic->log2_buffer_sectors)) {
  948. (*metadata_block)++;
  949. *metadata_offset = 0;
  950. }
  951. total_size -= to_copy;
  952. } while (unlikely(total_size));
  953. return 0;
  954. }
  955. static void dm_integrity_flush_buffers(struct dm_integrity_c *ic)
  956. {
  957. int r;
  958. r = dm_bufio_write_dirty_buffers(ic->bufio);
  959. if (unlikely(r))
  960. dm_integrity_io_error(ic, "writing tags", r);
  961. }
  962. static void sleep_on_endio_wait(struct dm_integrity_c *ic)
  963. {
  964. DECLARE_WAITQUEUE(wait, current);
  965. __add_wait_queue(&ic->endio_wait, &wait);
  966. __set_current_state(TASK_UNINTERRUPTIBLE);
  967. spin_unlock_irq(&ic->endio_wait.lock);
  968. io_schedule();
  969. spin_lock_irq(&ic->endio_wait.lock);
  970. __remove_wait_queue(&ic->endio_wait, &wait);
  971. }
  972. static void autocommit_fn(struct timer_list *t)
  973. {
  974. struct dm_integrity_c *ic = from_timer(ic, t, autocommit_timer);
  975. if (likely(!dm_integrity_failed(ic)))
  976. queue_work(ic->commit_wq, &ic->commit_work);
  977. }
  978. static void schedule_autocommit(struct dm_integrity_c *ic)
  979. {
  980. if (!timer_pending(&ic->autocommit_timer))
  981. mod_timer(&ic->autocommit_timer, jiffies + ic->autocommit_jiffies);
  982. }
  983. static void submit_flush_bio(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
  984. {
  985. struct bio *bio;
  986. unsigned long flags;
  987. spin_lock_irqsave(&ic->endio_wait.lock, flags);
  988. bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  989. bio_list_add(&ic->flush_bio_list, bio);
  990. spin_unlock_irqrestore(&ic->endio_wait.lock, flags);
  991. queue_work(ic->commit_wq, &ic->commit_work);
  992. }
  993. static void do_endio(struct dm_integrity_c *ic, struct bio *bio)
  994. {
  995. int r = dm_integrity_failed(ic);
  996. if (unlikely(r) && !bio->bi_status)
  997. bio->bi_status = errno_to_blk_status(r);
  998. bio_endio(bio);
  999. }
  1000. static void do_endio_flush(struct dm_integrity_c *ic, struct dm_integrity_io *dio)
  1001. {
  1002. struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  1003. if (unlikely(dio->fua) && likely(!bio->bi_status) && likely(!dm_integrity_failed(ic)))
  1004. submit_flush_bio(ic, dio);
  1005. else
  1006. do_endio(ic, bio);
  1007. }
  1008. static void dec_in_flight(struct dm_integrity_io *dio)
  1009. {
  1010. if (atomic_dec_and_test(&dio->in_flight)) {
  1011. struct dm_integrity_c *ic = dio->ic;
  1012. struct bio *bio;
  1013. remove_range(ic, &dio->range);
  1014. if (unlikely(dio->write))
  1015. schedule_autocommit(ic);
  1016. bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  1017. if (unlikely(dio->bi_status) && !bio->bi_status)
  1018. bio->bi_status = dio->bi_status;
  1019. if (likely(!bio->bi_status) && unlikely(bio_sectors(bio) != dio->range.n_sectors)) {
  1020. dio->range.logical_sector += dio->range.n_sectors;
  1021. bio_advance(bio, dio->range.n_sectors << SECTOR_SHIFT);
  1022. INIT_WORK(&dio->work, integrity_bio_wait);
  1023. queue_work(ic->wait_wq, &dio->work);
  1024. return;
  1025. }
  1026. do_endio_flush(ic, dio);
  1027. }
  1028. }
  1029. static void integrity_end_io(struct bio *bio)
  1030. {
  1031. struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
  1032. bio->bi_iter = dio->orig_bi_iter;
  1033. bio->bi_disk = dio->orig_bi_disk;
  1034. bio->bi_partno = dio->orig_bi_partno;
  1035. if (dio->orig_bi_integrity) {
  1036. bio->bi_integrity = dio->orig_bi_integrity;
  1037. bio->bi_opf |= REQ_INTEGRITY;
  1038. }
  1039. bio->bi_end_io = dio->orig_bi_end_io;
  1040. if (dio->completion)
  1041. complete(dio->completion);
  1042. dec_in_flight(dio);
  1043. }
  1044. static void integrity_sector_checksum(struct dm_integrity_c *ic, sector_t sector,
  1045. const char *data, char *result)
  1046. {
  1047. __u64 sector_le = cpu_to_le64(sector);
  1048. SHASH_DESC_ON_STACK(req, ic->internal_hash);
  1049. int r;
  1050. unsigned digest_size;
  1051. req->tfm = ic->internal_hash;
  1052. req->flags = 0;
  1053. r = crypto_shash_init(req);
  1054. if (unlikely(r < 0)) {
  1055. dm_integrity_io_error(ic, "crypto_shash_init", r);
  1056. goto failed;
  1057. }
  1058. r = crypto_shash_update(req, (const __u8 *)&sector_le, sizeof sector_le);
  1059. if (unlikely(r < 0)) {
  1060. dm_integrity_io_error(ic, "crypto_shash_update", r);
  1061. goto failed;
  1062. }
  1063. r = crypto_shash_update(req, data, ic->sectors_per_block << SECTOR_SHIFT);
  1064. if (unlikely(r < 0)) {
  1065. dm_integrity_io_error(ic, "crypto_shash_update", r);
  1066. goto failed;
  1067. }
  1068. r = crypto_shash_final(req, result);
  1069. if (unlikely(r < 0)) {
  1070. dm_integrity_io_error(ic, "crypto_shash_final", r);
  1071. goto failed;
  1072. }
  1073. digest_size = crypto_shash_digestsize(ic->internal_hash);
  1074. if (unlikely(digest_size < ic->tag_size))
  1075. memset(result + digest_size, 0, ic->tag_size - digest_size);
  1076. return;
  1077. failed:
  1078. /* this shouldn't happen anyway, the hash functions have no reason to fail */
  1079. get_random_bytes(result, ic->tag_size);
  1080. }
  1081. static void integrity_metadata(struct work_struct *w)
  1082. {
  1083. struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
  1084. struct dm_integrity_c *ic = dio->ic;
  1085. int r;
  1086. if (ic->internal_hash) {
  1087. struct bvec_iter iter;
  1088. struct bio_vec bv;
  1089. unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
  1090. struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  1091. char *checksums;
  1092. unsigned extra_space = unlikely(digest_size > ic->tag_size) ? digest_size - ic->tag_size : 0;
  1093. char checksums_onstack[ic->tag_size + extra_space];
  1094. unsigned sectors_to_process = dio->range.n_sectors;
  1095. sector_t sector = dio->range.logical_sector;
  1096. if (unlikely(ic->mode == 'R'))
  1097. goto skip_io;
  1098. checksums = kmalloc((PAGE_SIZE >> SECTOR_SHIFT >> ic->sb->log2_sectors_per_block) * ic->tag_size + extra_space,
  1099. GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN);
  1100. if (!checksums)
  1101. checksums = checksums_onstack;
  1102. __bio_for_each_segment(bv, bio, iter, dio->orig_bi_iter) {
  1103. unsigned pos;
  1104. char *mem, *checksums_ptr;
  1105. again:
  1106. mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
  1107. pos = 0;
  1108. checksums_ptr = checksums;
  1109. do {
  1110. integrity_sector_checksum(ic, sector, mem + pos, checksums_ptr);
  1111. checksums_ptr += ic->tag_size;
  1112. sectors_to_process -= ic->sectors_per_block;
  1113. pos += ic->sectors_per_block << SECTOR_SHIFT;
  1114. sector += ic->sectors_per_block;
  1115. } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
  1116. kunmap_atomic(mem);
  1117. r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
  1118. checksums_ptr - checksums, !dio->write ? TAG_CMP : TAG_WRITE);
  1119. if (unlikely(r)) {
  1120. if (r > 0) {
  1121. DMERR("Checksum failed at sector 0x%llx",
  1122. (unsigned long long)(sector - ((r + ic->tag_size - 1) / ic->tag_size)));
  1123. r = -EILSEQ;
  1124. atomic64_inc(&ic->number_of_mismatches);
  1125. }
  1126. if (likely(checksums != checksums_onstack))
  1127. kfree(checksums);
  1128. goto error;
  1129. }
  1130. if (!sectors_to_process)
  1131. break;
  1132. if (unlikely(pos < bv.bv_len)) {
  1133. bv.bv_offset += pos;
  1134. bv.bv_len -= pos;
  1135. goto again;
  1136. }
  1137. }
  1138. if (likely(checksums != checksums_onstack))
  1139. kfree(checksums);
  1140. } else {
  1141. struct bio_integrity_payload *bip = dio->orig_bi_integrity;
  1142. if (bip) {
  1143. struct bio_vec biv;
  1144. struct bvec_iter iter;
  1145. unsigned data_to_process = dio->range.n_sectors;
  1146. sector_to_block(ic, data_to_process);
  1147. data_to_process *= ic->tag_size;
  1148. bip_for_each_vec(biv, bip, iter) {
  1149. unsigned char *tag;
  1150. unsigned this_len;
  1151. BUG_ON(PageHighMem(biv.bv_page));
  1152. tag = lowmem_page_address(biv.bv_page) + biv.bv_offset;
  1153. this_len = min(biv.bv_len, data_to_process);
  1154. r = dm_integrity_rw_tag(ic, tag, &dio->metadata_block, &dio->metadata_offset,
  1155. this_len, !dio->write ? TAG_READ : TAG_WRITE);
  1156. if (unlikely(r))
  1157. goto error;
  1158. data_to_process -= this_len;
  1159. if (!data_to_process)
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. skip_io:
  1165. dec_in_flight(dio);
  1166. return;
  1167. error:
  1168. dio->bi_status = errno_to_blk_status(r);
  1169. dec_in_flight(dio);
  1170. }
  1171. static int dm_integrity_map(struct dm_target *ti, struct bio *bio)
  1172. {
  1173. struct dm_integrity_c *ic = ti->private;
  1174. struct dm_integrity_io *dio = dm_per_bio_data(bio, sizeof(struct dm_integrity_io));
  1175. struct bio_integrity_payload *bip;
  1176. sector_t area, offset;
  1177. dio->ic = ic;
  1178. dio->bi_status = 0;
  1179. if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
  1180. submit_flush_bio(ic, dio);
  1181. return DM_MAPIO_SUBMITTED;
  1182. }
  1183. dio->range.logical_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
  1184. dio->write = bio_op(bio) == REQ_OP_WRITE;
  1185. dio->fua = dio->write && bio->bi_opf & REQ_FUA;
  1186. if (unlikely(dio->fua)) {
  1187. /*
  1188. * Don't pass down the FUA flag because we have to flush
  1189. * disk cache anyway.
  1190. */
  1191. bio->bi_opf &= ~REQ_FUA;
  1192. }
  1193. if (unlikely(dio->range.logical_sector + bio_sectors(bio) > ic->provided_data_sectors)) {
  1194. DMERR("Too big sector number: 0x%llx + 0x%x > 0x%llx",
  1195. (unsigned long long)dio->range.logical_sector, bio_sectors(bio),
  1196. (unsigned long long)ic->provided_data_sectors);
  1197. return DM_MAPIO_KILL;
  1198. }
  1199. if (unlikely((dio->range.logical_sector | bio_sectors(bio)) & (unsigned)(ic->sectors_per_block - 1))) {
  1200. DMERR("Bio not aligned on %u sectors: 0x%llx, 0x%x",
  1201. ic->sectors_per_block,
  1202. (unsigned long long)dio->range.logical_sector, bio_sectors(bio));
  1203. return DM_MAPIO_KILL;
  1204. }
  1205. if (ic->sectors_per_block > 1) {
  1206. struct bvec_iter iter;
  1207. struct bio_vec bv;
  1208. bio_for_each_segment(bv, bio, iter) {
  1209. if (unlikely(bv.bv_len & ((ic->sectors_per_block << SECTOR_SHIFT) - 1))) {
  1210. DMERR("Bio vector (%u,%u) is not aligned on %u-sector boundary",
  1211. bv.bv_offset, bv.bv_len, ic->sectors_per_block);
  1212. return DM_MAPIO_KILL;
  1213. }
  1214. }
  1215. }
  1216. bip = bio_integrity(bio);
  1217. if (!ic->internal_hash) {
  1218. if (bip) {
  1219. unsigned wanted_tag_size = bio_sectors(bio) >> ic->sb->log2_sectors_per_block;
  1220. if (ic->log2_tag_size >= 0)
  1221. wanted_tag_size <<= ic->log2_tag_size;
  1222. else
  1223. wanted_tag_size *= ic->tag_size;
  1224. if (unlikely(wanted_tag_size != bip->bip_iter.bi_size)) {
  1225. DMERR("Invalid integrity data size %u, expected %u", bip->bip_iter.bi_size, wanted_tag_size);
  1226. return DM_MAPIO_KILL;
  1227. }
  1228. }
  1229. } else {
  1230. if (unlikely(bip != NULL)) {
  1231. DMERR("Unexpected integrity data when using internal hash");
  1232. return DM_MAPIO_KILL;
  1233. }
  1234. }
  1235. if (unlikely(ic->mode == 'R') && unlikely(dio->write))
  1236. return DM_MAPIO_KILL;
  1237. get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
  1238. dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
  1239. bio->bi_iter.bi_sector = get_data_sector(ic, area, offset);
  1240. dm_integrity_map_continue(dio, true);
  1241. return DM_MAPIO_SUBMITTED;
  1242. }
  1243. static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
  1244. unsigned journal_section, unsigned journal_entry)
  1245. {
  1246. struct dm_integrity_c *ic = dio->ic;
  1247. sector_t logical_sector;
  1248. unsigned n_sectors;
  1249. logical_sector = dio->range.logical_sector;
  1250. n_sectors = dio->range.n_sectors;
  1251. do {
  1252. struct bio_vec bv = bio_iovec(bio);
  1253. char *mem;
  1254. if (unlikely(bv.bv_len >> SECTOR_SHIFT > n_sectors))
  1255. bv.bv_len = n_sectors << SECTOR_SHIFT;
  1256. n_sectors -= bv.bv_len >> SECTOR_SHIFT;
  1257. bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
  1258. retry_kmap:
  1259. mem = kmap_atomic(bv.bv_page);
  1260. if (likely(dio->write))
  1261. flush_dcache_page(bv.bv_page);
  1262. do {
  1263. struct journal_entry *je = access_journal_entry(ic, journal_section, journal_entry);
  1264. if (unlikely(!dio->write)) {
  1265. struct journal_sector *js;
  1266. char *mem_ptr;
  1267. unsigned s;
  1268. if (unlikely(journal_entry_is_inprogress(je))) {
  1269. flush_dcache_page(bv.bv_page);
  1270. kunmap_atomic(mem);
  1271. __io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
  1272. goto retry_kmap;
  1273. }
  1274. smp_rmb();
  1275. BUG_ON(journal_entry_get_sector(je) != logical_sector);
  1276. js = access_journal_data(ic, journal_section, journal_entry);
  1277. mem_ptr = mem + bv.bv_offset;
  1278. s = 0;
  1279. do {
  1280. memcpy(mem_ptr, js, JOURNAL_SECTOR_DATA);
  1281. *(commit_id_t *)(mem_ptr + JOURNAL_SECTOR_DATA) = je->last_bytes[s];
  1282. js++;
  1283. mem_ptr += 1 << SECTOR_SHIFT;
  1284. } while (++s < ic->sectors_per_block);
  1285. #ifdef INTERNAL_VERIFY
  1286. if (ic->internal_hash) {
  1287. char checksums_onstack[max(crypto_shash_digestsize(ic->internal_hash), ic->tag_size)];
  1288. integrity_sector_checksum(ic, logical_sector, mem + bv.bv_offset, checksums_onstack);
  1289. if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) {
  1290. DMERR("Checksum failed when reading from journal, at sector 0x%llx",
  1291. (unsigned long long)logical_sector);
  1292. }
  1293. }
  1294. #endif
  1295. }
  1296. if (!ic->internal_hash) {
  1297. struct bio_integrity_payload *bip = bio_integrity(bio);
  1298. unsigned tag_todo = ic->tag_size;
  1299. char *tag_ptr = journal_entry_tag(ic, je);
  1300. if (bip) do {
  1301. struct bio_vec biv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
  1302. unsigned tag_now = min(biv.bv_len, tag_todo);
  1303. char *tag_addr;
  1304. BUG_ON(PageHighMem(biv.bv_page));
  1305. tag_addr = lowmem_page_address(biv.bv_page) + biv.bv_offset;
  1306. if (likely(dio->write))
  1307. memcpy(tag_ptr, tag_addr, tag_now);
  1308. else
  1309. memcpy(tag_addr, tag_ptr, tag_now);
  1310. bvec_iter_advance(bip->bip_vec, &bip->bip_iter, tag_now);
  1311. tag_ptr += tag_now;
  1312. tag_todo -= tag_now;
  1313. } while (unlikely(tag_todo)); else {
  1314. if (likely(dio->write))
  1315. memset(tag_ptr, 0, tag_todo);
  1316. }
  1317. }
  1318. if (likely(dio->write)) {
  1319. struct journal_sector *js;
  1320. unsigned s;
  1321. js = access_journal_data(ic, journal_section, journal_entry);
  1322. memcpy(js, mem + bv.bv_offset, ic->sectors_per_block << SECTOR_SHIFT);
  1323. s = 0;
  1324. do {
  1325. je->last_bytes[s] = js[s].commit_id;
  1326. } while (++s < ic->sectors_per_block);
  1327. if (ic->internal_hash) {
  1328. unsigned digest_size = crypto_shash_digestsize(ic->internal_hash);
  1329. if (unlikely(digest_size > ic->tag_size)) {
  1330. char checksums_onstack[digest_size];
  1331. integrity_sector_checksum(ic, logical_sector, (char *)js, checksums_onstack);
  1332. memcpy(journal_entry_tag(ic, je), checksums_onstack, ic->tag_size);
  1333. } else
  1334. integrity_sector_checksum(ic, logical_sector, (char *)js, journal_entry_tag(ic, je));
  1335. }
  1336. journal_entry_set_sector(je, logical_sector);
  1337. }
  1338. logical_sector += ic->sectors_per_block;
  1339. journal_entry++;
  1340. if (unlikely(journal_entry == ic->journal_section_entries)) {
  1341. journal_entry = 0;
  1342. journal_section++;
  1343. wraparound_section(ic, &journal_section);
  1344. }
  1345. bv.bv_offset += ic->sectors_per_block << SECTOR_SHIFT;
  1346. } while (bv.bv_len -= ic->sectors_per_block << SECTOR_SHIFT);
  1347. if (unlikely(!dio->write))
  1348. flush_dcache_page(bv.bv_page);
  1349. kunmap_atomic(mem);
  1350. } while (n_sectors);
  1351. if (likely(dio->write)) {
  1352. smp_mb();
  1353. if (unlikely(waitqueue_active(&ic->copy_to_journal_wait)))
  1354. wake_up(&ic->copy_to_journal_wait);
  1355. if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold) {
  1356. queue_work(ic->commit_wq, &ic->commit_work);
  1357. } else {
  1358. schedule_autocommit(ic);
  1359. }
  1360. } else {
  1361. remove_range(ic, &dio->range);
  1362. }
  1363. if (unlikely(bio->bi_iter.bi_size)) {
  1364. sector_t area, offset;
  1365. dio->range.logical_sector = logical_sector;
  1366. get_area_and_offset(ic, dio->range.logical_sector, &area, &offset);
  1367. dio->metadata_block = get_metadata_sector_and_offset(ic, area, offset, &dio->metadata_offset);
  1368. return true;
  1369. }
  1370. return false;
  1371. }
  1372. static void dm_integrity_map_continue(struct dm_integrity_io *dio, bool from_map)
  1373. {
  1374. struct dm_integrity_c *ic = dio->ic;
  1375. struct bio *bio = dm_bio_from_per_bio_data(dio, sizeof(struct dm_integrity_io));
  1376. unsigned journal_section, journal_entry;
  1377. unsigned journal_read_pos;
  1378. struct completion read_comp;
  1379. bool need_sync_io = ic->internal_hash && !dio->write;
  1380. if (need_sync_io && from_map) {
  1381. INIT_WORK(&dio->work, integrity_bio_wait);
  1382. queue_work(ic->metadata_wq, &dio->work);
  1383. return;
  1384. }
  1385. lock_retry:
  1386. spin_lock_irq(&ic->endio_wait.lock);
  1387. retry:
  1388. if (unlikely(dm_integrity_failed(ic))) {
  1389. spin_unlock_irq(&ic->endio_wait.lock);
  1390. do_endio(ic, bio);
  1391. return;
  1392. }
  1393. dio->range.n_sectors = bio_sectors(bio);
  1394. journal_read_pos = NOT_FOUND;
  1395. if (likely(ic->mode == 'J')) {
  1396. if (dio->write) {
  1397. unsigned next_entry, i, pos;
  1398. unsigned ws, we, range_sectors;
  1399. dio->range.n_sectors = min(dio->range.n_sectors,
  1400. ic->free_sectors << ic->sb->log2_sectors_per_block);
  1401. if (unlikely(!dio->range.n_sectors)) {
  1402. if (from_map)
  1403. goto offload_to_thread;
  1404. sleep_on_endio_wait(ic);
  1405. goto retry;
  1406. }
  1407. range_sectors = dio->range.n_sectors >> ic->sb->log2_sectors_per_block;
  1408. ic->free_sectors -= range_sectors;
  1409. journal_section = ic->free_section;
  1410. journal_entry = ic->free_section_entry;
  1411. next_entry = ic->free_section_entry + range_sectors;
  1412. ic->free_section_entry = next_entry % ic->journal_section_entries;
  1413. ic->free_section += next_entry / ic->journal_section_entries;
  1414. ic->n_uncommitted_sections += next_entry / ic->journal_section_entries;
  1415. wraparound_section(ic, &ic->free_section);
  1416. pos = journal_section * ic->journal_section_entries + journal_entry;
  1417. ws = journal_section;
  1418. we = journal_entry;
  1419. i = 0;
  1420. do {
  1421. struct journal_entry *je;
  1422. add_journal_node(ic, &ic->journal_tree[pos], dio->range.logical_sector + i);
  1423. pos++;
  1424. if (unlikely(pos >= ic->journal_entries))
  1425. pos = 0;
  1426. je = access_journal_entry(ic, ws, we);
  1427. BUG_ON(!journal_entry_is_unused(je));
  1428. journal_entry_set_inprogress(je);
  1429. we++;
  1430. if (unlikely(we == ic->journal_section_entries)) {
  1431. we = 0;
  1432. ws++;
  1433. wraparound_section(ic, &ws);
  1434. }
  1435. } while ((i += ic->sectors_per_block) < dio->range.n_sectors);
  1436. spin_unlock_irq(&ic->endio_wait.lock);
  1437. goto journal_read_write;
  1438. } else {
  1439. sector_t next_sector;
  1440. journal_read_pos = find_journal_node(ic, dio->range.logical_sector, &next_sector);
  1441. if (likely(journal_read_pos == NOT_FOUND)) {
  1442. if (unlikely(dio->range.n_sectors > next_sector - dio->range.logical_sector))
  1443. dio->range.n_sectors = next_sector - dio->range.logical_sector;
  1444. } else {
  1445. unsigned i;
  1446. unsigned jp = journal_read_pos + 1;
  1447. for (i = ic->sectors_per_block; i < dio->range.n_sectors; i += ic->sectors_per_block, jp++) {
  1448. if (!test_journal_node(ic, jp, dio->range.logical_sector + i))
  1449. break;
  1450. }
  1451. dio->range.n_sectors = i;
  1452. }
  1453. }
  1454. }
  1455. if (unlikely(!add_new_range(ic, &dio->range, true))) {
  1456. /*
  1457. * We must not sleep in the request routine because it could
  1458. * stall bios on current->bio_list.
  1459. * So, we offload the bio to a workqueue if we have to sleep.
  1460. */
  1461. if (from_map) {
  1462. offload_to_thread:
  1463. spin_unlock_irq(&ic->endio_wait.lock);
  1464. INIT_WORK(&dio->work, integrity_bio_wait);
  1465. queue_work(ic->wait_wq, &dio->work);
  1466. return;
  1467. }
  1468. wait_and_add_new_range(ic, &dio->range);
  1469. }
  1470. spin_unlock_irq(&ic->endio_wait.lock);
  1471. if (unlikely(journal_read_pos != NOT_FOUND)) {
  1472. journal_section = journal_read_pos / ic->journal_section_entries;
  1473. journal_entry = journal_read_pos % ic->journal_section_entries;
  1474. goto journal_read_write;
  1475. }
  1476. dio->in_flight = (atomic_t)ATOMIC_INIT(2);
  1477. if (need_sync_io) {
  1478. init_completion(&read_comp);
  1479. dio->completion = &read_comp;
  1480. } else
  1481. dio->completion = NULL;
  1482. dio->orig_bi_iter = bio->bi_iter;
  1483. dio->orig_bi_disk = bio->bi_disk;
  1484. dio->orig_bi_partno = bio->bi_partno;
  1485. bio_set_dev(bio, ic->dev->bdev);
  1486. dio->orig_bi_integrity = bio_integrity(bio);
  1487. bio->bi_integrity = NULL;
  1488. bio->bi_opf &= ~REQ_INTEGRITY;
  1489. dio->orig_bi_end_io = bio->bi_end_io;
  1490. bio->bi_end_io = integrity_end_io;
  1491. bio->bi_iter.bi_size = dio->range.n_sectors << SECTOR_SHIFT;
  1492. generic_make_request(bio);
  1493. if (need_sync_io) {
  1494. wait_for_completion_io(&read_comp);
  1495. if (unlikely(ic->recalc_wq != NULL) &&
  1496. ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING) &&
  1497. dio->range.logical_sector + dio->range.n_sectors > le64_to_cpu(ic->sb->recalc_sector))
  1498. goto skip_check;
  1499. if (likely(!bio->bi_status))
  1500. integrity_metadata(&dio->work);
  1501. else
  1502. skip_check:
  1503. dec_in_flight(dio);
  1504. } else {
  1505. INIT_WORK(&dio->work, integrity_metadata);
  1506. queue_work(ic->metadata_wq, &dio->work);
  1507. }
  1508. return;
  1509. journal_read_write:
  1510. if (unlikely(__journal_read_write(dio, bio, journal_section, journal_entry)))
  1511. goto lock_retry;
  1512. do_endio_flush(ic, dio);
  1513. }
  1514. static void integrity_bio_wait(struct work_struct *w)
  1515. {
  1516. struct dm_integrity_io *dio = container_of(w, struct dm_integrity_io, work);
  1517. dm_integrity_map_continue(dio, false);
  1518. }
  1519. static void pad_uncommitted(struct dm_integrity_c *ic)
  1520. {
  1521. if (ic->free_section_entry) {
  1522. ic->free_sectors -= ic->journal_section_entries - ic->free_section_entry;
  1523. ic->free_section_entry = 0;
  1524. ic->free_section++;
  1525. wraparound_section(ic, &ic->free_section);
  1526. ic->n_uncommitted_sections++;
  1527. }
  1528. WARN_ON(ic->journal_sections * ic->journal_section_entries !=
  1529. (ic->n_uncommitted_sections + ic->n_committed_sections) * ic->journal_section_entries + ic->free_sectors);
  1530. }
  1531. static void integrity_commit(struct work_struct *w)
  1532. {
  1533. struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, commit_work);
  1534. unsigned commit_start, commit_sections;
  1535. unsigned i, j, n;
  1536. struct bio *flushes;
  1537. del_timer(&ic->autocommit_timer);
  1538. spin_lock_irq(&ic->endio_wait.lock);
  1539. flushes = bio_list_get(&ic->flush_bio_list);
  1540. if (unlikely(ic->mode != 'J')) {
  1541. spin_unlock_irq(&ic->endio_wait.lock);
  1542. dm_integrity_flush_buffers(ic);
  1543. goto release_flush_bios;
  1544. }
  1545. pad_uncommitted(ic);
  1546. commit_start = ic->uncommitted_section;
  1547. commit_sections = ic->n_uncommitted_sections;
  1548. spin_unlock_irq(&ic->endio_wait.lock);
  1549. if (!commit_sections)
  1550. goto release_flush_bios;
  1551. i = commit_start;
  1552. for (n = 0; n < commit_sections; n++) {
  1553. for (j = 0; j < ic->journal_section_entries; j++) {
  1554. struct journal_entry *je;
  1555. je = access_journal_entry(ic, i, j);
  1556. io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
  1557. }
  1558. for (j = 0; j < ic->journal_section_sectors; j++) {
  1559. struct journal_sector *js;
  1560. js = access_journal(ic, i, j);
  1561. js->commit_id = dm_integrity_commit_id(ic, i, j, ic->commit_seq);
  1562. }
  1563. i++;
  1564. if (unlikely(i >= ic->journal_sections))
  1565. ic->commit_seq = next_commit_seq(ic->commit_seq);
  1566. wraparound_section(ic, &i);
  1567. }
  1568. smp_rmb();
  1569. write_journal(ic, commit_start, commit_sections);
  1570. spin_lock_irq(&ic->endio_wait.lock);
  1571. ic->uncommitted_section += commit_sections;
  1572. wraparound_section(ic, &ic->uncommitted_section);
  1573. ic->n_uncommitted_sections -= commit_sections;
  1574. ic->n_committed_sections += commit_sections;
  1575. spin_unlock_irq(&ic->endio_wait.lock);
  1576. if (READ_ONCE(ic->free_sectors) <= ic->free_sectors_threshold)
  1577. queue_work(ic->writer_wq, &ic->writer_work);
  1578. release_flush_bios:
  1579. while (flushes) {
  1580. struct bio *next = flushes->bi_next;
  1581. flushes->bi_next = NULL;
  1582. do_endio(ic, flushes);
  1583. flushes = next;
  1584. }
  1585. }
  1586. static void complete_copy_from_journal(unsigned long error, void *context)
  1587. {
  1588. struct journal_io *io = context;
  1589. struct journal_completion *comp = io->comp;
  1590. struct dm_integrity_c *ic = comp->ic;
  1591. remove_range(ic, &io->range);
  1592. mempool_free(io, &ic->journal_io_mempool);
  1593. if (unlikely(error != 0))
  1594. dm_integrity_io_error(ic, "copying from journal", -EIO);
  1595. complete_journal_op(comp);
  1596. }
  1597. static void restore_last_bytes(struct dm_integrity_c *ic, struct journal_sector *js,
  1598. struct journal_entry *je)
  1599. {
  1600. unsigned s = 0;
  1601. do {
  1602. js->commit_id = je->last_bytes[s];
  1603. js++;
  1604. } while (++s < ic->sectors_per_block);
  1605. }
  1606. static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start,
  1607. unsigned write_sections, bool from_replay)
  1608. {
  1609. unsigned i, j, n;
  1610. struct journal_completion comp;
  1611. struct blk_plug plug;
  1612. blk_start_plug(&plug);
  1613. comp.ic = ic;
  1614. comp.in_flight = (atomic_t)ATOMIC_INIT(1);
  1615. init_completion(&comp.comp);
  1616. i = write_start;
  1617. for (n = 0; n < write_sections; n++, i++, wraparound_section(ic, &i)) {
  1618. #ifndef INTERNAL_VERIFY
  1619. if (unlikely(from_replay))
  1620. #endif
  1621. rw_section_mac(ic, i, false);
  1622. for (j = 0; j < ic->journal_section_entries; j++) {
  1623. struct journal_entry *je = access_journal_entry(ic, i, j);
  1624. sector_t sec, area, offset;
  1625. unsigned k, l, next_loop;
  1626. sector_t metadata_block;
  1627. unsigned metadata_offset;
  1628. struct journal_io *io;
  1629. if (journal_entry_is_unused(je))
  1630. continue;
  1631. BUG_ON(unlikely(journal_entry_is_inprogress(je)) && !from_replay);
  1632. sec = journal_entry_get_sector(je);
  1633. if (unlikely(from_replay)) {
  1634. if (unlikely(sec & (unsigned)(ic->sectors_per_block - 1))) {
  1635. dm_integrity_io_error(ic, "invalid sector in journal", -EIO);
  1636. sec &= ~(sector_t)(ic->sectors_per_block - 1);
  1637. }
  1638. }
  1639. get_area_and_offset(ic, sec, &area, &offset);
  1640. restore_last_bytes(ic, access_journal_data(ic, i, j), je);
  1641. for (k = j + 1; k < ic->journal_section_entries; k++) {
  1642. struct journal_entry *je2 = access_journal_entry(ic, i, k);
  1643. sector_t sec2, area2, offset2;
  1644. if (journal_entry_is_unused(je2))
  1645. break;
  1646. BUG_ON(unlikely(journal_entry_is_inprogress(je2)) && !from_replay);
  1647. sec2 = journal_entry_get_sector(je2);
  1648. get_area_and_offset(ic, sec2, &area2, &offset2);
  1649. if (area2 != area || offset2 != offset + ((k - j) << ic->sb->log2_sectors_per_block))
  1650. break;
  1651. restore_last_bytes(ic, access_journal_data(ic, i, k), je2);
  1652. }
  1653. next_loop = k - 1;
  1654. io = mempool_alloc(&ic->journal_io_mempool, GFP_NOIO);
  1655. io->comp = &comp;
  1656. io->range.logical_sector = sec;
  1657. io->range.n_sectors = (k - j) << ic->sb->log2_sectors_per_block;
  1658. spin_lock_irq(&ic->endio_wait.lock);
  1659. if (unlikely(!add_new_range(ic, &io->range, true)))
  1660. wait_and_add_new_range(ic, &io->range);
  1661. if (likely(!from_replay)) {
  1662. struct journal_node *section_node = &ic->journal_tree[i * ic->journal_section_entries];
  1663. /* don't write if there is newer committed sector */
  1664. while (j < k && find_newer_committed_node(ic, &section_node[j])) {
  1665. struct journal_entry *je2 = access_journal_entry(ic, i, j);
  1666. journal_entry_set_unused(je2);
  1667. remove_journal_node(ic, &section_node[j]);
  1668. j++;
  1669. sec += ic->sectors_per_block;
  1670. offset += ic->sectors_per_block;
  1671. }
  1672. while (j < k && find_newer_committed_node(ic, &section_node[k - 1])) {
  1673. struct journal_entry *je2 = access_journal_entry(ic, i, k - 1);
  1674. journal_entry_set_unused(je2);
  1675. remove_journal_node(ic, &section_node[k - 1]);
  1676. k--;
  1677. }
  1678. if (j == k) {
  1679. remove_range_unlocked(ic, &io->range);
  1680. spin_unlock_irq(&ic->endio_wait.lock);
  1681. mempool_free(io, &ic->journal_io_mempool);
  1682. goto skip_io;
  1683. }
  1684. for (l = j; l < k; l++) {
  1685. remove_journal_node(ic, &section_node[l]);
  1686. }
  1687. }
  1688. spin_unlock_irq(&ic->endio_wait.lock);
  1689. metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
  1690. for (l = j; l < k; l++) {
  1691. int r;
  1692. struct journal_entry *je2 = access_journal_entry(ic, i, l);
  1693. if (
  1694. #ifndef INTERNAL_VERIFY
  1695. unlikely(from_replay) &&
  1696. #endif
  1697. ic->internal_hash) {
  1698. char test_tag[max(crypto_shash_digestsize(ic->internal_hash), ic->tag_size)];
  1699. integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block),
  1700. (char *)access_journal_data(ic, i, l), test_tag);
  1701. if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size)))
  1702. dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
  1703. }
  1704. journal_entry_set_unused(je2);
  1705. r = dm_integrity_rw_tag(ic, journal_entry_tag(ic, je2), &metadata_block, &metadata_offset,
  1706. ic->tag_size, TAG_WRITE);
  1707. if (unlikely(r)) {
  1708. dm_integrity_io_error(ic, "reading tags", r);
  1709. }
  1710. }
  1711. atomic_inc(&comp.in_flight);
  1712. copy_from_journal(ic, i, j << ic->sb->log2_sectors_per_block,
  1713. (k - j) << ic->sb->log2_sectors_per_block,
  1714. get_data_sector(ic, area, offset),
  1715. complete_copy_from_journal, io);
  1716. skip_io:
  1717. j = next_loop;
  1718. }
  1719. }
  1720. dm_bufio_write_dirty_buffers_async(ic->bufio);
  1721. blk_finish_plug(&plug);
  1722. complete_journal_op(&comp);
  1723. wait_for_completion_io(&comp.comp);
  1724. dm_integrity_flush_buffers(ic);
  1725. }
  1726. static void integrity_writer(struct work_struct *w)
  1727. {
  1728. struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, writer_work);
  1729. unsigned write_start, write_sections;
  1730. unsigned prev_free_sectors;
  1731. /* the following test is not needed, but it tests the replay code */
  1732. if (READ_ONCE(ic->suspending) && !ic->meta_dev)
  1733. return;
  1734. spin_lock_irq(&ic->endio_wait.lock);
  1735. write_start = ic->committed_section;
  1736. write_sections = ic->n_committed_sections;
  1737. spin_unlock_irq(&ic->endio_wait.lock);
  1738. if (!write_sections)
  1739. return;
  1740. do_journal_write(ic, write_start, write_sections, false);
  1741. spin_lock_irq(&ic->endio_wait.lock);
  1742. ic->committed_section += write_sections;
  1743. wraparound_section(ic, &ic->committed_section);
  1744. ic->n_committed_sections -= write_sections;
  1745. prev_free_sectors = ic->free_sectors;
  1746. ic->free_sectors += write_sections * ic->journal_section_entries;
  1747. if (unlikely(!prev_free_sectors))
  1748. wake_up_locked(&ic->endio_wait);
  1749. spin_unlock_irq(&ic->endio_wait.lock);
  1750. }
  1751. static void recalc_write_super(struct dm_integrity_c *ic)
  1752. {
  1753. int r;
  1754. dm_integrity_flush_buffers(ic);
  1755. if (dm_integrity_failed(ic))
  1756. return;
  1757. sb_set_version(ic);
  1758. r = sync_rw_sb(ic, REQ_OP_WRITE, 0);
  1759. if (unlikely(r))
  1760. dm_integrity_io_error(ic, "writing superblock", r);
  1761. }
  1762. static void integrity_recalc(struct work_struct *w)
  1763. {
  1764. struct dm_integrity_c *ic = container_of(w, struct dm_integrity_c, recalc_work);
  1765. struct dm_integrity_range range;
  1766. struct dm_io_request io_req;
  1767. struct dm_io_region io_loc;
  1768. sector_t area, offset;
  1769. sector_t metadata_block;
  1770. unsigned metadata_offset;
  1771. __u8 *t;
  1772. unsigned i;
  1773. int r;
  1774. unsigned super_counter = 0;
  1775. spin_lock_irq(&ic->endio_wait.lock);
  1776. next_chunk:
  1777. if (unlikely(READ_ONCE(ic->suspending)))
  1778. goto unlock_ret;
  1779. range.logical_sector = le64_to_cpu(ic->sb->recalc_sector);
  1780. if (unlikely(range.logical_sector >= ic->provided_data_sectors))
  1781. goto unlock_ret;
  1782. get_area_and_offset(ic, range.logical_sector, &area, &offset);
  1783. range.n_sectors = min((sector_t)RECALC_SECTORS, ic->provided_data_sectors - range.logical_sector);
  1784. if (!ic->meta_dev)
  1785. range.n_sectors = min(range.n_sectors, (1U << ic->sb->log2_interleave_sectors) - (unsigned)offset);
  1786. if (unlikely(!add_new_range(ic, &range, true)))
  1787. wait_and_add_new_range(ic, &range);
  1788. spin_unlock_irq(&ic->endio_wait.lock);
  1789. if (unlikely(++super_counter == RECALC_WRITE_SUPER)) {
  1790. recalc_write_super(ic);
  1791. super_counter = 0;
  1792. }
  1793. if (unlikely(dm_integrity_failed(ic)))
  1794. goto err;
  1795. io_req.bi_op = REQ_OP_READ;
  1796. io_req.bi_op_flags = 0;
  1797. io_req.mem.type = DM_IO_VMA;
  1798. io_req.mem.ptr.addr = ic->recalc_buffer;
  1799. io_req.notify.fn = NULL;
  1800. io_req.client = ic->io;
  1801. io_loc.bdev = ic->dev->bdev;
  1802. io_loc.sector = get_data_sector(ic, area, offset);
  1803. io_loc.count = range.n_sectors;
  1804. r = dm_io(&io_req, 1, &io_loc, NULL);
  1805. if (unlikely(r)) {
  1806. dm_integrity_io_error(ic, "reading data", r);
  1807. goto err;
  1808. }
  1809. t = ic->recalc_tags;
  1810. for (i = 0; i < range.n_sectors; i += ic->sectors_per_block) {
  1811. integrity_sector_checksum(ic, range.logical_sector + i, ic->recalc_buffer + (i << SECTOR_SHIFT), t);
  1812. t += ic->tag_size;
  1813. }
  1814. metadata_block = get_metadata_sector_and_offset(ic, area, offset, &metadata_offset);
  1815. r = dm_integrity_rw_tag(ic, ic->recalc_tags, &metadata_block, &metadata_offset, t - ic->recalc_tags, TAG_WRITE);
  1816. if (unlikely(r)) {
  1817. dm_integrity_io_error(ic, "writing tags", r);
  1818. goto err;
  1819. }
  1820. spin_lock_irq(&ic->endio_wait.lock);
  1821. remove_range_unlocked(ic, &range);
  1822. ic->sb->recalc_sector = cpu_to_le64(range.logical_sector + range.n_sectors);
  1823. goto next_chunk;
  1824. err:
  1825. remove_range(ic, &range);
  1826. return;
  1827. unlock_ret:
  1828. spin_unlock_irq(&ic->endio_wait.lock);
  1829. recalc_write_super(ic);
  1830. }
  1831. static void init_journal(struct dm_integrity_c *ic, unsigned start_section,
  1832. unsigned n_sections, unsigned char commit_seq)
  1833. {
  1834. unsigned i, j, n;
  1835. if (!n_sections)
  1836. return;
  1837. for (n = 0; n < n_sections; n++) {
  1838. i = start_section + n;
  1839. wraparound_section(ic, &i);
  1840. for (j = 0; j < ic->journal_section_sectors; j++) {
  1841. struct journal_sector *js = access_journal(ic, i, j);
  1842. memset(&js->entries, 0, JOURNAL_SECTOR_DATA);
  1843. js->commit_id = dm_integrity_commit_id(ic, i, j, commit_seq);
  1844. }
  1845. for (j = 0; j < ic->journal_section_entries; j++) {
  1846. struct journal_entry *je = access_journal_entry(ic, i, j);
  1847. journal_entry_set_unused(je);
  1848. }
  1849. }
  1850. write_journal(ic, start_section, n_sections);
  1851. }
  1852. static int find_commit_seq(struct dm_integrity_c *ic, unsigned i, unsigned j, commit_id_t id)
  1853. {
  1854. unsigned char k;
  1855. for (k = 0; k < N_COMMIT_IDS; k++) {
  1856. if (dm_integrity_commit_id(ic, i, j, k) == id)
  1857. return k;
  1858. }
  1859. dm_integrity_io_error(ic, "journal commit id", -EIO);
  1860. return -EIO;
  1861. }
  1862. static void replay_journal(struct dm_integrity_c *ic)
  1863. {
  1864. unsigned i, j;
  1865. bool used_commit_ids[N_COMMIT_IDS];
  1866. unsigned max_commit_id_sections[N_COMMIT_IDS];
  1867. unsigned write_start, write_sections;
  1868. unsigned continue_section;
  1869. bool journal_empty;
  1870. unsigned char unused, last_used, want_commit_seq;
  1871. if (ic->mode == 'R')
  1872. return;
  1873. if (ic->journal_uptodate)
  1874. return;
  1875. last_used = 0;
  1876. write_start = 0;
  1877. if (!ic->just_formatted) {
  1878. DEBUG_print("reading journal\n");
  1879. rw_journal(ic, REQ_OP_READ, 0, 0, ic->journal_sections, NULL);
  1880. if (ic->journal_io)
  1881. DEBUG_bytes(lowmem_page_address(ic->journal_io[0].page), 64, "read journal");
  1882. if (ic->journal_io) {
  1883. struct journal_completion crypt_comp;
  1884. crypt_comp.ic = ic;
  1885. init_completion(&crypt_comp.comp);
  1886. crypt_comp.in_flight = (atomic_t)ATOMIC_INIT(0);
  1887. encrypt_journal(ic, false, 0, ic->journal_sections, &crypt_comp);
  1888. wait_for_completion(&crypt_comp.comp);
  1889. }
  1890. DEBUG_bytes(lowmem_page_address(ic->journal[0].page), 64, "decrypted journal");
  1891. }
  1892. if (dm_integrity_failed(ic))
  1893. goto clear_journal;
  1894. journal_empty = true;
  1895. memset(used_commit_ids, 0, sizeof used_commit_ids);
  1896. memset(max_commit_id_sections, 0, sizeof max_commit_id_sections);
  1897. for (i = 0; i < ic->journal_sections; i++) {
  1898. for (j = 0; j < ic->journal_section_sectors; j++) {
  1899. int k;
  1900. struct journal_sector *js = access_journal(ic, i, j);
  1901. k = find_commit_seq(ic, i, j, js->commit_id);
  1902. if (k < 0)
  1903. goto clear_journal;
  1904. used_commit_ids[k] = true;
  1905. max_commit_id_sections[k] = i;
  1906. }
  1907. if (journal_empty) {
  1908. for (j = 0; j < ic->journal_section_entries; j++) {
  1909. struct journal_entry *je = access_journal_entry(ic, i, j);
  1910. if (!journal_entry_is_unused(je)) {
  1911. journal_empty = false;
  1912. break;
  1913. }
  1914. }
  1915. }
  1916. }
  1917. if (!used_commit_ids[N_COMMIT_IDS - 1]) {
  1918. unused = N_COMMIT_IDS - 1;
  1919. while (unused && !used_commit_ids[unused - 1])
  1920. unused--;
  1921. } else {
  1922. for (unused = 0; unused < N_COMMIT_IDS; unused++)
  1923. if (!used_commit_ids[unused])
  1924. break;
  1925. if (unused == N_COMMIT_IDS) {
  1926. dm_integrity_io_error(ic, "journal commit ids", -EIO);
  1927. goto clear_journal;
  1928. }
  1929. }
  1930. DEBUG_print("first unused commit seq %d [%d,%d,%d,%d]\n",
  1931. unused, used_commit_ids[0], used_commit_ids[1],
  1932. used_commit_ids[2], used_commit_ids[3]);
  1933. last_used = prev_commit_seq(unused);
  1934. want_commit_seq = prev_commit_seq(last_used);
  1935. if (!used_commit_ids[want_commit_seq] && used_commit_ids[prev_commit_seq(want_commit_seq)])
  1936. journal_empty = true;
  1937. write_start = max_commit_id_sections[last_used] + 1;
  1938. if (unlikely(write_start >= ic->journal_sections))
  1939. want_commit_seq = next_commit_seq(want_commit_seq);
  1940. wraparound_section(ic, &write_start);
  1941. i = write_start;
  1942. for (write_sections = 0; write_sections < ic->journal_sections; write_sections++) {
  1943. for (j = 0; j < ic->journal_section_sectors; j++) {
  1944. struct journal_sector *js = access_journal(ic, i, j);
  1945. if (js->commit_id != dm_integrity_commit_id(ic, i, j, want_commit_seq)) {
  1946. /*
  1947. * This could be caused by crash during writing.
  1948. * We won't replay the inconsistent part of the
  1949. * journal.
  1950. */
  1951. DEBUG_print("commit id mismatch at position (%u, %u): %d != %d\n",
  1952. i, j, find_commit_seq(ic, i, j, js->commit_id), want_commit_seq);
  1953. goto brk;
  1954. }
  1955. }
  1956. i++;
  1957. if (unlikely(i >= ic->journal_sections))
  1958. want_commit_seq = next_commit_seq(want_commit_seq);
  1959. wraparound_section(ic, &i);
  1960. }
  1961. brk:
  1962. if (!journal_empty) {
  1963. DEBUG_print("replaying %u sections, starting at %u, commit seq %d\n",
  1964. write_sections, write_start, want_commit_seq);
  1965. do_journal_write(ic, write_start, write_sections, true);
  1966. }
  1967. if (write_sections == ic->journal_sections && (ic->mode == 'J' || journal_empty)) {
  1968. continue_section = write_start;
  1969. ic->commit_seq = want_commit_seq;
  1970. DEBUG_print("continuing from section %u, commit seq %d\n", write_start, ic->commit_seq);
  1971. } else {
  1972. unsigned s;
  1973. unsigned char erase_seq;
  1974. clear_journal:
  1975. DEBUG_print("clearing journal\n");
  1976. erase_seq = prev_commit_seq(prev_commit_seq(last_used));
  1977. s = write_start;
  1978. init_journal(ic, s, 1, erase_seq);
  1979. s++;
  1980. wraparound_section(ic, &s);
  1981. if (ic->journal_sections >= 2) {
  1982. init_journal(ic, s, ic->journal_sections - 2, erase_seq);
  1983. s += ic->journal_sections - 2;
  1984. wraparound_section(ic, &s);
  1985. init_journal(ic, s, 1, erase_seq);
  1986. }
  1987. continue_section = 0;
  1988. ic->commit_seq = next_commit_seq(erase_seq);
  1989. }
  1990. ic->committed_section = continue_section;
  1991. ic->n_committed_sections = 0;
  1992. ic->uncommitted_section = continue_section;
  1993. ic->n_uncommitted_sections = 0;
  1994. ic->free_section = continue_section;
  1995. ic->free_section_entry = 0;
  1996. ic->free_sectors = ic->journal_entries;
  1997. ic->journal_tree_root = RB_ROOT;
  1998. for (i = 0; i < ic->journal_entries; i++)
  1999. init_journal_node(&ic->journal_tree[i]);
  2000. }
  2001. static void dm_integrity_postsuspend(struct dm_target *ti)
  2002. {
  2003. struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
  2004. del_timer_sync(&ic->autocommit_timer);
  2005. WRITE_ONCE(ic->suspending, 1);
  2006. if (ic->recalc_wq)
  2007. drain_workqueue(ic->recalc_wq);
  2008. queue_work(ic->commit_wq, &ic->commit_work);
  2009. drain_workqueue(ic->commit_wq);
  2010. if (ic->mode == 'J') {
  2011. if (ic->meta_dev)
  2012. queue_work(ic->writer_wq, &ic->writer_work);
  2013. drain_workqueue(ic->writer_wq);
  2014. dm_integrity_flush_buffers(ic);
  2015. }
  2016. WRITE_ONCE(ic->suspending, 0);
  2017. BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
  2018. ic->journal_uptodate = true;
  2019. }
  2020. static void dm_integrity_resume(struct dm_target *ti)
  2021. {
  2022. struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
  2023. replay_journal(ic);
  2024. if (ic->recalc_wq && ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
  2025. __u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);
  2026. if (recalc_pos < ic->provided_data_sectors) {
  2027. queue_work(ic->recalc_wq, &ic->recalc_work);
  2028. } else if (recalc_pos > ic->provided_data_sectors) {
  2029. ic->sb->recalc_sector = cpu_to_le64(ic->provided_data_sectors);
  2030. recalc_write_super(ic);
  2031. }
  2032. }
  2033. }
  2034. static void dm_integrity_status(struct dm_target *ti, status_type_t type,
  2035. unsigned status_flags, char *result, unsigned maxlen)
  2036. {
  2037. struct dm_integrity_c *ic = (struct dm_integrity_c *)ti->private;
  2038. unsigned arg_count;
  2039. size_t sz = 0;
  2040. switch (type) {
  2041. case STATUSTYPE_INFO:
  2042. DMEMIT("%llu %llu",
  2043. (unsigned long long)atomic64_read(&ic->number_of_mismatches),
  2044. (unsigned long long)ic->provided_data_sectors);
  2045. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
  2046. DMEMIT(" %llu", (unsigned long long)le64_to_cpu(ic->sb->recalc_sector));
  2047. else
  2048. DMEMIT(" -");
  2049. break;
  2050. case STATUSTYPE_TABLE: {
  2051. __u64 watermark_percentage = (__u64)(ic->journal_entries - ic->free_sectors_threshold) * 100;
  2052. watermark_percentage += ic->journal_entries / 2;
  2053. do_div(watermark_percentage, ic->journal_entries);
  2054. arg_count = 5;
  2055. arg_count += !!ic->meta_dev;
  2056. arg_count += ic->sectors_per_block != 1;
  2057. arg_count += !!(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING));
  2058. arg_count += !!ic->internal_hash_alg.alg_string;
  2059. arg_count += !!ic->journal_crypt_alg.alg_string;
  2060. arg_count += !!ic->journal_mac_alg.alg_string;
  2061. DMEMIT("%s %llu %u %c %u", ic->dev->name, (unsigned long long)ic->start,
  2062. ic->tag_size, ic->mode, arg_count);
  2063. if (ic->meta_dev)
  2064. DMEMIT(" meta_device:%s", ic->meta_dev->name);
  2065. if (ic->sectors_per_block != 1)
  2066. DMEMIT(" block_size:%u", ic->sectors_per_block << SECTOR_SHIFT);
  2067. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))
  2068. DMEMIT(" recalculate");
  2069. DMEMIT(" journal_sectors:%u", ic->initial_sectors - SB_SECTORS);
  2070. DMEMIT(" interleave_sectors:%u", 1U << ic->sb->log2_interleave_sectors);
  2071. DMEMIT(" buffer_sectors:%u", 1U << ic->log2_buffer_sectors);
  2072. DMEMIT(" journal_watermark:%u", (unsigned)watermark_percentage);
  2073. DMEMIT(" commit_time:%u", ic->autocommit_msec);
  2074. #define EMIT_ALG(a, n) \
  2075. do { \
  2076. if (ic->a.alg_string) { \
  2077. DMEMIT(" %s:%s", n, ic->a.alg_string); \
  2078. if (ic->a.key_string) \
  2079. DMEMIT(":%s", ic->a.key_string);\
  2080. } \
  2081. } while (0)
  2082. EMIT_ALG(internal_hash_alg, "internal_hash");
  2083. EMIT_ALG(journal_crypt_alg, "journal_crypt");
  2084. EMIT_ALG(journal_mac_alg, "journal_mac");
  2085. break;
  2086. }
  2087. }
  2088. }
  2089. static int dm_integrity_iterate_devices(struct dm_target *ti,
  2090. iterate_devices_callout_fn fn, void *data)
  2091. {
  2092. struct dm_integrity_c *ic = ti->private;
  2093. if (!ic->meta_dev)
  2094. return fn(ti, ic->dev, ic->start + ic->initial_sectors + ic->metadata_run, ti->len, data);
  2095. else
  2096. return fn(ti, ic->dev, 0, ti->len, data);
  2097. }
  2098. static void dm_integrity_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2099. {
  2100. struct dm_integrity_c *ic = ti->private;
  2101. if (ic->sectors_per_block > 1) {
  2102. limits->logical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
  2103. limits->physical_block_size = ic->sectors_per_block << SECTOR_SHIFT;
  2104. blk_limits_io_min(limits, ic->sectors_per_block << SECTOR_SHIFT);
  2105. }
  2106. }
  2107. static void calculate_journal_section_size(struct dm_integrity_c *ic)
  2108. {
  2109. unsigned sector_space = JOURNAL_SECTOR_DATA;
  2110. ic->journal_sections = le32_to_cpu(ic->sb->journal_sections);
  2111. ic->journal_entry_size = roundup(offsetof(struct journal_entry, last_bytes[ic->sectors_per_block]) + ic->tag_size,
  2112. JOURNAL_ENTRY_ROUNDUP);
  2113. if (ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC))
  2114. sector_space -= JOURNAL_MAC_PER_SECTOR;
  2115. ic->journal_entries_per_sector = sector_space / ic->journal_entry_size;
  2116. ic->journal_section_entries = ic->journal_entries_per_sector * JOURNAL_BLOCK_SECTORS;
  2117. ic->journal_section_sectors = (ic->journal_section_entries << ic->sb->log2_sectors_per_block) + JOURNAL_BLOCK_SECTORS;
  2118. ic->journal_entries = ic->journal_section_entries * ic->journal_sections;
  2119. }
  2120. static int calculate_device_limits(struct dm_integrity_c *ic)
  2121. {
  2122. __u64 initial_sectors;
  2123. calculate_journal_section_size(ic);
  2124. initial_sectors = SB_SECTORS + (__u64)ic->journal_section_sectors * ic->journal_sections;
  2125. if (initial_sectors + METADATA_PADDING_SECTORS >= ic->meta_device_sectors || initial_sectors > UINT_MAX)
  2126. return -EINVAL;
  2127. ic->initial_sectors = initial_sectors;
  2128. if (!ic->meta_dev) {
  2129. sector_t last_sector, last_area, last_offset;
  2130. ic->metadata_run = roundup((__u64)ic->tag_size << (ic->sb->log2_interleave_sectors - ic->sb->log2_sectors_per_block),
  2131. (__u64)(1 << SECTOR_SHIFT << METADATA_PADDING_SECTORS)) >> SECTOR_SHIFT;
  2132. if (!(ic->metadata_run & (ic->metadata_run - 1)))
  2133. ic->log2_metadata_run = __ffs(ic->metadata_run);
  2134. else
  2135. ic->log2_metadata_run = -1;
  2136. get_area_and_offset(ic, ic->provided_data_sectors - 1, &last_area, &last_offset);
  2137. last_sector = get_data_sector(ic, last_area, last_offset);
  2138. if (last_sector < ic->start || last_sector >= ic->meta_device_sectors)
  2139. return -EINVAL;
  2140. } else {
  2141. __u64 meta_size = ic->provided_data_sectors * ic->tag_size;
  2142. meta_size = (meta_size + ((1U << (ic->log2_buffer_sectors + SECTOR_SHIFT)) - 1))
  2143. >> (ic->log2_buffer_sectors + SECTOR_SHIFT);
  2144. meta_size <<= ic->log2_buffer_sectors;
  2145. if (ic->initial_sectors + meta_size < ic->initial_sectors ||
  2146. ic->initial_sectors + meta_size > ic->meta_device_sectors)
  2147. return -EINVAL;
  2148. ic->metadata_run = 1;
  2149. ic->log2_metadata_run = 0;
  2150. }
  2151. return 0;
  2152. }
  2153. static int initialize_superblock(struct dm_integrity_c *ic, unsigned journal_sectors, unsigned interleave_sectors)
  2154. {
  2155. unsigned journal_sections;
  2156. int test_bit;
  2157. memset(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT);
  2158. memcpy(ic->sb->magic, SB_MAGIC, 8);
  2159. ic->sb->integrity_tag_size = cpu_to_le16(ic->tag_size);
  2160. ic->sb->log2_sectors_per_block = __ffs(ic->sectors_per_block);
  2161. if (ic->journal_mac_alg.alg_string)
  2162. ic->sb->flags |= cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC);
  2163. calculate_journal_section_size(ic);
  2164. journal_sections = journal_sectors / ic->journal_section_sectors;
  2165. if (!journal_sections)
  2166. journal_sections = 1;
  2167. if (!ic->meta_dev) {
  2168. ic->sb->journal_sections = cpu_to_le32(journal_sections);
  2169. if (!interleave_sectors)
  2170. interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
  2171. ic->sb->log2_interleave_sectors = __fls(interleave_sectors);
  2172. ic->sb->log2_interleave_sectors = max((__u8)MIN_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
  2173. ic->sb->log2_interleave_sectors = min((__u8)MAX_LOG2_INTERLEAVE_SECTORS, ic->sb->log2_interleave_sectors);
  2174. ic->provided_data_sectors = 0;
  2175. for (test_bit = fls64(ic->meta_device_sectors) - 1; test_bit >= 3; test_bit--) {
  2176. __u64 prev_data_sectors = ic->provided_data_sectors;
  2177. ic->provided_data_sectors |= (sector_t)1 << test_bit;
  2178. if (calculate_device_limits(ic))
  2179. ic->provided_data_sectors = prev_data_sectors;
  2180. }
  2181. if (!ic->provided_data_sectors)
  2182. return -EINVAL;
  2183. } else {
  2184. ic->sb->log2_interleave_sectors = 0;
  2185. ic->provided_data_sectors = ic->data_device_sectors;
  2186. ic->provided_data_sectors &= ~(sector_t)(ic->sectors_per_block - 1);
  2187. try_smaller_buffer:
  2188. ic->sb->journal_sections = cpu_to_le32(0);
  2189. for (test_bit = fls(journal_sections) - 1; test_bit >= 0; test_bit--) {
  2190. __u32 prev_journal_sections = le32_to_cpu(ic->sb->journal_sections);
  2191. __u32 test_journal_sections = prev_journal_sections | (1U << test_bit);
  2192. if (test_journal_sections > journal_sections)
  2193. continue;
  2194. ic->sb->journal_sections = cpu_to_le32(test_journal_sections);
  2195. if (calculate_device_limits(ic))
  2196. ic->sb->journal_sections = cpu_to_le32(prev_journal_sections);
  2197. }
  2198. if (!le32_to_cpu(ic->sb->journal_sections)) {
  2199. if (ic->log2_buffer_sectors > 3) {
  2200. ic->log2_buffer_sectors--;
  2201. goto try_smaller_buffer;
  2202. }
  2203. return -EINVAL;
  2204. }
  2205. }
  2206. ic->sb->provided_data_sectors = cpu_to_le64(ic->provided_data_sectors);
  2207. sb_set_version(ic);
  2208. return 0;
  2209. }
  2210. static void dm_integrity_set(struct dm_target *ti, struct dm_integrity_c *ic)
  2211. {
  2212. struct gendisk *disk = dm_disk(dm_table_get_md(ti->table));
  2213. struct blk_integrity bi;
  2214. memset(&bi, 0, sizeof(bi));
  2215. bi.profile = &dm_integrity_profile;
  2216. bi.tuple_size = ic->tag_size;
  2217. bi.tag_size = bi.tuple_size;
  2218. bi.interval_exp = ic->sb->log2_sectors_per_block + SECTOR_SHIFT;
  2219. blk_integrity_register(disk, &bi);
  2220. blk_queue_max_integrity_segments(disk->queue, UINT_MAX);
  2221. }
  2222. static void dm_integrity_free_page_list(struct dm_integrity_c *ic, struct page_list *pl)
  2223. {
  2224. unsigned i;
  2225. if (!pl)
  2226. return;
  2227. for (i = 0; i < ic->journal_pages; i++)
  2228. if (pl[i].page)
  2229. __free_page(pl[i].page);
  2230. kvfree(pl);
  2231. }
  2232. static struct page_list *dm_integrity_alloc_page_list(struct dm_integrity_c *ic)
  2233. {
  2234. size_t page_list_desc_size = ic->journal_pages * sizeof(struct page_list);
  2235. struct page_list *pl;
  2236. unsigned i;
  2237. pl = kvmalloc(page_list_desc_size, GFP_KERNEL | __GFP_ZERO);
  2238. if (!pl)
  2239. return NULL;
  2240. for (i = 0; i < ic->journal_pages; i++) {
  2241. pl[i].page = alloc_page(GFP_KERNEL);
  2242. if (!pl[i].page) {
  2243. dm_integrity_free_page_list(ic, pl);
  2244. return NULL;
  2245. }
  2246. if (i)
  2247. pl[i - 1].next = &pl[i];
  2248. }
  2249. return pl;
  2250. }
  2251. static void dm_integrity_free_journal_scatterlist(struct dm_integrity_c *ic, struct scatterlist **sl)
  2252. {
  2253. unsigned i;
  2254. for (i = 0; i < ic->journal_sections; i++)
  2255. kvfree(sl[i]);
  2256. kvfree(sl);
  2257. }
  2258. static struct scatterlist **dm_integrity_alloc_journal_scatterlist(struct dm_integrity_c *ic, struct page_list *pl)
  2259. {
  2260. struct scatterlist **sl;
  2261. unsigned i;
  2262. sl = kvmalloc_array(ic->journal_sections,
  2263. sizeof(struct scatterlist *),
  2264. GFP_KERNEL | __GFP_ZERO);
  2265. if (!sl)
  2266. return NULL;
  2267. for (i = 0; i < ic->journal_sections; i++) {
  2268. struct scatterlist *s;
  2269. unsigned start_index, start_offset;
  2270. unsigned end_index, end_offset;
  2271. unsigned n_pages;
  2272. unsigned idx;
  2273. page_list_location(ic, i, 0, &start_index, &start_offset);
  2274. page_list_location(ic, i, ic->journal_section_sectors - 1, &end_index, &end_offset);
  2275. n_pages = (end_index - start_index + 1);
  2276. s = kvmalloc_array(n_pages, sizeof(struct scatterlist),
  2277. GFP_KERNEL);
  2278. if (!s) {
  2279. dm_integrity_free_journal_scatterlist(ic, sl);
  2280. return NULL;
  2281. }
  2282. sg_init_table(s, n_pages);
  2283. for (idx = start_index; idx <= end_index; idx++) {
  2284. char *va = lowmem_page_address(pl[idx].page);
  2285. unsigned start = 0, end = PAGE_SIZE;
  2286. if (idx == start_index)
  2287. start = start_offset;
  2288. if (idx == end_index)
  2289. end = end_offset + (1 << SECTOR_SHIFT);
  2290. sg_set_buf(&s[idx - start_index], va + start, end - start);
  2291. }
  2292. sl[i] = s;
  2293. }
  2294. return sl;
  2295. }
  2296. static void free_alg(struct alg_spec *a)
  2297. {
  2298. kzfree(a->alg_string);
  2299. kzfree(a->key);
  2300. memset(a, 0, sizeof *a);
  2301. }
  2302. static int get_alg_and_key(const char *arg, struct alg_spec *a, char **error, char *error_inval)
  2303. {
  2304. char *k;
  2305. free_alg(a);
  2306. a->alg_string = kstrdup(strchr(arg, ':') + 1, GFP_KERNEL);
  2307. if (!a->alg_string)
  2308. goto nomem;
  2309. k = strchr(a->alg_string, ':');
  2310. if (k) {
  2311. *k = 0;
  2312. a->key_string = k + 1;
  2313. if (strlen(a->key_string) & 1)
  2314. goto inval;
  2315. a->key_size = strlen(a->key_string) / 2;
  2316. a->key = kmalloc(a->key_size, GFP_KERNEL);
  2317. if (!a->key)
  2318. goto nomem;
  2319. if (hex2bin(a->key, a->key_string, a->key_size))
  2320. goto inval;
  2321. }
  2322. return 0;
  2323. inval:
  2324. *error = error_inval;
  2325. return -EINVAL;
  2326. nomem:
  2327. *error = "Out of memory for an argument";
  2328. return -ENOMEM;
  2329. }
  2330. static int get_mac(struct crypto_shash **hash, struct alg_spec *a, char **error,
  2331. char *error_alg, char *error_key)
  2332. {
  2333. int r;
  2334. if (a->alg_string) {
  2335. *hash = crypto_alloc_shash(a->alg_string, 0, CRYPTO_ALG_ASYNC);
  2336. if (IS_ERR(*hash)) {
  2337. *error = error_alg;
  2338. r = PTR_ERR(*hash);
  2339. *hash = NULL;
  2340. return r;
  2341. }
  2342. if (a->key) {
  2343. r = crypto_shash_setkey(*hash, a->key, a->key_size);
  2344. if (r) {
  2345. *error = error_key;
  2346. return r;
  2347. }
  2348. } else if (crypto_shash_get_flags(*hash) & CRYPTO_TFM_NEED_KEY) {
  2349. *error = error_key;
  2350. return -ENOKEY;
  2351. }
  2352. }
  2353. return 0;
  2354. }
  2355. static int create_journal(struct dm_integrity_c *ic, char **error)
  2356. {
  2357. int r = 0;
  2358. unsigned i;
  2359. __u64 journal_pages, journal_desc_size, journal_tree_size;
  2360. unsigned char *crypt_data = NULL, *crypt_iv = NULL;
  2361. struct skcipher_request *req = NULL;
  2362. ic->commit_ids[0] = cpu_to_le64(0x1111111111111111ULL);
  2363. ic->commit_ids[1] = cpu_to_le64(0x2222222222222222ULL);
  2364. ic->commit_ids[2] = cpu_to_le64(0x3333333333333333ULL);
  2365. ic->commit_ids[3] = cpu_to_le64(0x4444444444444444ULL);
  2366. journal_pages = roundup((__u64)ic->journal_sections * ic->journal_section_sectors,
  2367. PAGE_SIZE >> SECTOR_SHIFT) >> (PAGE_SHIFT - SECTOR_SHIFT);
  2368. journal_desc_size = journal_pages * sizeof(struct page_list);
  2369. if (journal_pages >= totalram_pages - totalhigh_pages || journal_desc_size > ULONG_MAX) {
  2370. *error = "Journal doesn't fit into memory";
  2371. r = -ENOMEM;
  2372. goto bad;
  2373. }
  2374. ic->journal_pages = journal_pages;
  2375. ic->journal = dm_integrity_alloc_page_list(ic);
  2376. if (!ic->journal) {
  2377. *error = "Could not allocate memory for journal";
  2378. r = -ENOMEM;
  2379. goto bad;
  2380. }
  2381. if (ic->journal_crypt_alg.alg_string) {
  2382. unsigned ivsize, blocksize;
  2383. struct journal_completion comp;
  2384. comp.ic = ic;
  2385. ic->journal_crypt = crypto_alloc_skcipher(ic->journal_crypt_alg.alg_string, 0, 0);
  2386. if (IS_ERR(ic->journal_crypt)) {
  2387. *error = "Invalid journal cipher";
  2388. r = PTR_ERR(ic->journal_crypt);
  2389. ic->journal_crypt = NULL;
  2390. goto bad;
  2391. }
  2392. ivsize = crypto_skcipher_ivsize(ic->journal_crypt);
  2393. blocksize = crypto_skcipher_blocksize(ic->journal_crypt);
  2394. if (ic->journal_crypt_alg.key) {
  2395. r = crypto_skcipher_setkey(ic->journal_crypt, ic->journal_crypt_alg.key,
  2396. ic->journal_crypt_alg.key_size);
  2397. if (r) {
  2398. *error = "Error setting encryption key";
  2399. goto bad;
  2400. }
  2401. }
  2402. DEBUG_print("cipher %s, block size %u iv size %u\n",
  2403. ic->journal_crypt_alg.alg_string, blocksize, ivsize);
  2404. ic->journal_io = dm_integrity_alloc_page_list(ic);
  2405. if (!ic->journal_io) {
  2406. *error = "Could not allocate memory for journal io";
  2407. r = -ENOMEM;
  2408. goto bad;
  2409. }
  2410. if (blocksize == 1) {
  2411. struct scatterlist *sg;
  2412. req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
  2413. if (!req) {
  2414. *error = "Could not allocate crypt request";
  2415. r = -ENOMEM;
  2416. goto bad;
  2417. }
  2418. crypt_iv = kmalloc(ivsize, GFP_KERNEL);
  2419. if (!crypt_iv) {
  2420. *error = "Could not allocate iv";
  2421. r = -ENOMEM;
  2422. goto bad;
  2423. }
  2424. ic->journal_xor = dm_integrity_alloc_page_list(ic);
  2425. if (!ic->journal_xor) {
  2426. *error = "Could not allocate memory for journal xor";
  2427. r = -ENOMEM;
  2428. goto bad;
  2429. }
  2430. sg = kvmalloc_array(ic->journal_pages + 1,
  2431. sizeof(struct scatterlist),
  2432. GFP_KERNEL);
  2433. if (!sg) {
  2434. *error = "Unable to allocate sg list";
  2435. r = -ENOMEM;
  2436. goto bad;
  2437. }
  2438. sg_init_table(sg, ic->journal_pages + 1);
  2439. for (i = 0; i < ic->journal_pages; i++) {
  2440. char *va = lowmem_page_address(ic->journal_xor[i].page);
  2441. clear_page(va);
  2442. sg_set_buf(&sg[i], va, PAGE_SIZE);
  2443. }
  2444. sg_set_buf(&sg[i], &ic->commit_ids, sizeof ic->commit_ids);
  2445. memset(crypt_iv, 0x00, ivsize);
  2446. skcipher_request_set_crypt(req, sg, sg, PAGE_SIZE * ic->journal_pages + sizeof ic->commit_ids, crypt_iv);
  2447. init_completion(&comp.comp);
  2448. comp.in_flight = (atomic_t)ATOMIC_INIT(1);
  2449. if (do_crypt(true, req, &comp))
  2450. wait_for_completion(&comp.comp);
  2451. kvfree(sg);
  2452. r = dm_integrity_failed(ic);
  2453. if (r) {
  2454. *error = "Unable to encrypt journal";
  2455. goto bad;
  2456. }
  2457. DEBUG_bytes(lowmem_page_address(ic->journal_xor[0].page), 64, "xor data");
  2458. crypto_free_skcipher(ic->journal_crypt);
  2459. ic->journal_crypt = NULL;
  2460. } else {
  2461. unsigned crypt_len = roundup(ivsize, blocksize);
  2462. req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
  2463. if (!req) {
  2464. *error = "Could not allocate crypt request";
  2465. r = -ENOMEM;
  2466. goto bad;
  2467. }
  2468. crypt_iv = kmalloc(ivsize, GFP_KERNEL);
  2469. if (!crypt_iv) {
  2470. *error = "Could not allocate iv";
  2471. r = -ENOMEM;
  2472. goto bad;
  2473. }
  2474. crypt_data = kmalloc(crypt_len, GFP_KERNEL);
  2475. if (!crypt_data) {
  2476. *error = "Unable to allocate crypt data";
  2477. r = -ENOMEM;
  2478. goto bad;
  2479. }
  2480. ic->journal_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal);
  2481. if (!ic->journal_scatterlist) {
  2482. *error = "Unable to allocate sg list";
  2483. r = -ENOMEM;
  2484. goto bad;
  2485. }
  2486. ic->journal_io_scatterlist = dm_integrity_alloc_journal_scatterlist(ic, ic->journal_io);
  2487. if (!ic->journal_io_scatterlist) {
  2488. *error = "Unable to allocate sg list";
  2489. r = -ENOMEM;
  2490. goto bad;
  2491. }
  2492. ic->sk_requests = kvmalloc_array(ic->journal_sections,
  2493. sizeof(struct skcipher_request *),
  2494. GFP_KERNEL | __GFP_ZERO);
  2495. if (!ic->sk_requests) {
  2496. *error = "Unable to allocate sk requests";
  2497. r = -ENOMEM;
  2498. goto bad;
  2499. }
  2500. for (i = 0; i < ic->journal_sections; i++) {
  2501. struct scatterlist sg;
  2502. struct skcipher_request *section_req;
  2503. __u32 section_le = cpu_to_le32(i);
  2504. memset(crypt_iv, 0x00, ivsize);
  2505. memset(crypt_data, 0x00, crypt_len);
  2506. memcpy(crypt_data, &section_le, min((size_t)crypt_len, sizeof(section_le)));
  2507. sg_init_one(&sg, crypt_data, crypt_len);
  2508. skcipher_request_set_crypt(req, &sg, &sg, crypt_len, crypt_iv);
  2509. init_completion(&comp.comp);
  2510. comp.in_flight = (atomic_t)ATOMIC_INIT(1);
  2511. if (do_crypt(true, req, &comp))
  2512. wait_for_completion(&comp.comp);
  2513. r = dm_integrity_failed(ic);
  2514. if (r) {
  2515. *error = "Unable to generate iv";
  2516. goto bad;
  2517. }
  2518. section_req = skcipher_request_alloc(ic->journal_crypt, GFP_KERNEL);
  2519. if (!section_req) {
  2520. *error = "Unable to allocate crypt request";
  2521. r = -ENOMEM;
  2522. goto bad;
  2523. }
  2524. section_req->iv = kmalloc_array(ivsize, 2,
  2525. GFP_KERNEL);
  2526. if (!section_req->iv) {
  2527. skcipher_request_free(section_req);
  2528. *error = "Unable to allocate iv";
  2529. r = -ENOMEM;
  2530. goto bad;
  2531. }
  2532. memcpy(section_req->iv + ivsize, crypt_data, ivsize);
  2533. section_req->cryptlen = (size_t)ic->journal_section_sectors << SECTOR_SHIFT;
  2534. ic->sk_requests[i] = section_req;
  2535. DEBUG_bytes(crypt_data, ivsize, "iv(%u)", i);
  2536. }
  2537. }
  2538. }
  2539. for (i = 0; i < N_COMMIT_IDS; i++) {
  2540. unsigned j;
  2541. retest_commit_id:
  2542. for (j = 0; j < i; j++) {
  2543. if (ic->commit_ids[j] == ic->commit_ids[i]) {
  2544. ic->commit_ids[i] = cpu_to_le64(le64_to_cpu(ic->commit_ids[i]) + 1);
  2545. goto retest_commit_id;
  2546. }
  2547. }
  2548. DEBUG_print("commit id %u: %016llx\n", i, ic->commit_ids[i]);
  2549. }
  2550. journal_tree_size = (__u64)ic->journal_entries * sizeof(struct journal_node);
  2551. if (journal_tree_size > ULONG_MAX) {
  2552. *error = "Journal doesn't fit into memory";
  2553. r = -ENOMEM;
  2554. goto bad;
  2555. }
  2556. ic->journal_tree = kvmalloc(journal_tree_size, GFP_KERNEL);
  2557. if (!ic->journal_tree) {
  2558. *error = "Could not allocate memory for journal tree";
  2559. r = -ENOMEM;
  2560. }
  2561. bad:
  2562. kfree(crypt_data);
  2563. kfree(crypt_iv);
  2564. skcipher_request_free(req);
  2565. return r;
  2566. }
  2567. /*
  2568. * Construct a integrity mapping
  2569. *
  2570. * Arguments:
  2571. * device
  2572. * offset from the start of the device
  2573. * tag size
  2574. * D - direct writes, J - journal writes, R - recovery mode
  2575. * number of optional arguments
  2576. * optional arguments:
  2577. * journal_sectors
  2578. * interleave_sectors
  2579. * buffer_sectors
  2580. * journal_watermark
  2581. * commit_time
  2582. * internal_hash
  2583. * journal_crypt
  2584. * journal_mac
  2585. * block_size
  2586. */
  2587. static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2588. {
  2589. struct dm_integrity_c *ic;
  2590. char dummy;
  2591. int r;
  2592. unsigned extra_args;
  2593. struct dm_arg_set as;
  2594. static const struct dm_arg _args[] = {
  2595. {0, 9, "Invalid number of feature args"},
  2596. };
  2597. unsigned journal_sectors, interleave_sectors, buffer_sectors, journal_watermark, sync_msec;
  2598. bool recalculate;
  2599. bool should_write_sb;
  2600. __u64 threshold;
  2601. unsigned long long start;
  2602. #define DIRECT_ARGUMENTS 4
  2603. if (argc <= DIRECT_ARGUMENTS) {
  2604. ti->error = "Invalid argument count";
  2605. return -EINVAL;
  2606. }
  2607. ic = kzalloc(sizeof(struct dm_integrity_c), GFP_KERNEL);
  2608. if (!ic) {
  2609. ti->error = "Cannot allocate integrity context";
  2610. return -ENOMEM;
  2611. }
  2612. ti->private = ic;
  2613. ti->per_io_data_size = sizeof(struct dm_integrity_io);
  2614. ic->in_progress = RB_ROOT;
  2615. INIT_LIST_HEAD(&ic->wait_list);
  2616. init_waitqueue_head(&ic->endio_wait);
  2617. bio_list_init(&ic->flush_bio_list);
  2618. init_waitqueue_head(&ic->copy_to_journal_wait);
  2619. init_completion(&ic->crypto_backoff);
  2620. atomic64_set(&ic->number_of_mismatches, 0);
  2621. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ic->dev);
  2622. if (r) {
  2623. ti->error = "Device lookup failed";
  2624. goto bad;
  2625. }
  2626. if (sscanf(argv[1], "%llu%c", &start, &dummy) != 1 || start != (sector_t)start) {
  2627. ti->error = "Invalid starting offset";
  2628. r = -EINVAL;
  2629. goto bad;
  2630. }
  2631. ic->start = start;
  2632. if (strcmp(argv[2], "-")) {
  2633. if (sscanf(argv[2], "%u%c", &ic->tag_size, &dummy) != 1 || !ic->tag_size) {
  2634. ti->error = "Invalid tag size";
  2635. r = -EINVAL;
  2636. goto bad;
  2637. }
  2638. }
  2639. if (!strcmp(argv[3], "J") || !strcmp(argv[3], "D") || !strcmp(argv[3], "R"))
  2640. ic->mode = argv[3][0];
  2641. else {
  2642. ti->error = "Invalid mode (expecting J, D, R)";
  2643. r = -EINVAL;
  2644. goto bad;
  2645. }
  2646. journal_sectors = 0;
  2647. interleave_sectors = DEFAULT_INTERLEAVE_SECTORS;
  2648. buffer_sectors = DEFAULT_BUFFER_SECTORS;
  2649. journal_watermark = DEFAULT_JOURNAL_WATERMARK;
  2650. sync_msec = DEFAULT_SYNC_MSEC;
  2651. recalculate = false;
  2652. ic->sectors_per_block = 1;
  2653. as.argc = argc - DIRECT_ARGUMENTS;
  2654. as.argv = argv + DIRECT_ARGUMENTS;
  2655. r = dm_read_arg_group(_args, &as, &extra_args, &ti->error);
  2656. if (r)
  2657. goto bad;
  2658. while (extra_args--) {
  2659. const char *opt_string;
  2660. unsigned val;
  2661. opt_string = dm_shift_arg(&as);
  2662. if (!opt_string) {
  2663. r = -EINVAL;
  2664. ti->error = "Not enough feature arguments";
  2665. goto bad;
  2666. }
  2667. if (sscanf(opt_string, "journal_sectors:%u%c", &val, &dummy) == 1)
  2668. journal_sectors = val ? val : 1;
  2669. else if (sscanf(opt_string, "interleave_sectors:%u%c", &val, &dummy) == 1)
  2670. interleave_sectors = val;
  2671. else if (sscanf(opt_string, "buffer_sectors:%u%c", &val, &dummy) == 1)
  2672. buffer_sectors = val;
  2673. else if (sscanf(opt_string, "journal_watermark:%u%c", &val, &dummy) == 1 && val <= 100)
  2674. journal_watermark = val;
  2675. else if (sscanf(opt_string, "commit_time:%u%c", &val, &dummy) == 1)
  2676. sync_msec = val;
  2677. else if (!memcmp(opt_string, "meta_device:", strlen("meta_device:"))) {
  2678. if (ic->meta_dev) {
  2679. dm_put_device(ti, ic->meta_dev);
  2680. ic->meta_dev = NULL;
  2681. }
  2682. r = dm_get_device(ti, strchr(opt_string, ':') + 1, dm_table_get_mode(ti->table), &ic->meta_dev);
  2683. if (r) {
  2684. ti->error = "Device lookup failed";
  2685. goto bad;
  2686. }
  2687. } else if (sscanf(opt_string, "block_size:%u%c", &val, &dummy) == 1) {
  2688. if (val < 1 << SECTOR_SHIFT ||
  2689. val > MAX_SECTORS_PER_BLOCK << SECTOR_SHIFT ||
  2690. (val & (val -1))) {
  2691. r = -EINVAL;
  2692. ti->error = "Invalid block_size argument";
  2693. goto bad;
  2694. }
  2695. ic->sectors_per_block = val >> SECTOR_SHIFT;
  2696. } else if (!memcmp(opt_string, "internal_hash:", strlen("internal_hash:"))) {
  2697. r = get_alg_and_key(opt_string, &ic->internal_hash_alg, &ti->error,
  2698. "Invalid internal_hash argument");
  2699. if (r)
  2700. goto bad;
  2701. } else if (!memcmp(opt_string, "journal_crypt:", strlen("journal_crypt:"))) {
  2702. r = get_alg_and_key(opt_string, &ic->journal_crypt_alg, &ti->error,
  2703. "Invalid journal_crypt argument");
  2704. if (r)
  2705. goto bad;
  2706. } else if (!memcmp(opt_string, "journal_mac:", strlen("journal_mac:"))) {
  2707. r = get_alg_and_key(opt_string, &ic->journal_mac_alg, &ti->error,
  2708. "Invalid journal_mac argument");
  2709. if (r)
  2710. goto bad;
  2711. } else if (!strcmp(opt_string, "recalculate")) {
  2712. recalculate = true;
  2713. } else {
  2714. r = -EINVAL;
  2715. ti->error = "Invalid argument";
  2716. goto bad;
  2717. }
  2718. }
  2719. ic->data_device_sectors = i_size_read(ic->dev->bdev->bd_inode) >> SECTOR_SHIFT;
  2720. if (!ic->meta_dev)
  2721. ic->meta_device_sectors = ic->data_device_sectors;
  2722. else
  2723. ic->meta_device_sectors = i_size_read(ic->meta_dev->bdev->bd_inode) >> SECTOR_SHIFT;
  2724. if (!journal_sectors) {
  2725. journal_sectors = min((sector_t)DEFAULT_MAX_JOURNAL_SECTORS,
  2726. ic->data_device_sectors >> DEFAULT_JOURNAL_SIZE_FACTOR);
  2727. }
  2728. if (!buffer_sectors)
  2729. buffer_sectors = 1;
  2730. ic->log2_buffer_sectors = min((int)__fls(buffer_sectors), 31 - SECTOR_SHIFT);
  2731. r = get_mac(&ic->internal_hash, &ic->internal_hash_alg, &ti->error,
  2732. "Invalid internal hash", "Error setting internal hash key");
  2733. if (r)
  2734. goto bad;
  2735. r = get_mac(&ic->journal_mac, &ic->journal_mac_alg, &ti->error,
  2736. "Invalid journal mac", "Error setting journal mac key");
  2737. if (r)
  2738. goto bad;
  2739. if (!ic->tag_size) {
  2740. if (!ic->internal_hash) {
  2741. ti->error = "Unknown tag size";
  2742. r = -EINVAL;
  2743. goto bad;
  2744. }
  2745. ic->tag_size = crypto_shash_digestsize(ic->internal_hash);
  2746. }
  2747. if (ic->tag_size > MAX_TAG_SIZE) {
  2748. ti->error = "Too big tag size";
  2749. r = -EINVAL;
  2750. goto bad;
  2751. }
  2752. if (!(ic->tag_size & (ic->tag_size - 1)))
  2753. ic->log2_tag_size = __ffs(ic->tag_size);
  2754. else
  2755. ic->log2_tag_size = -1;
  2756. ic->autocommit_jiffies = msecs_to_jiffies(sync_msec);
  2757. ic->autocommit_msec = sync_msec;
  2758. timer_setup(&ic->autocommit_timer, autocommit_fn, 0);
  2759. ic->io = dm_io_client_create();
  2760. if (IS_ERR(ic->io)) {
  2761. r = PTR_ERR(ic->io);
  2762. ic->io = NULL;
  2763. ti->error = "Cannot allocate dm io";
  2764. goto bad;
  2765. }
  2766. r = mempool_init_slab_pool(&ic->journal_io_mempool, JOURNAL_IO_MEMPOOL, journal_io_cache);
  2767. if (r) {
  2768. ti->error = "Cannot allocate mempool";
  2769. goto bad;
  2770. }
  2771. ic->metadata_wq = alloc_workqueue("dm-integrity-metadata",
  2772. WQ_MEM_RECLAIM, METADATA_WORKQUEUE_MAX_ACTIVE);
  2773. if (!ic->metadata_wq) {
  2774. ti->error = "Cannot allocate workqueue";
  2775. r = -ENOMEM;
  2776. goto bad;
  2777. }
  2778. /*
  2779. * If this workqueue were percpu, it would cause bio reordering
  2780. * and reduced performance.
  2781. */
  2782. ic->wait_wq = alloc_workqueue("dm-integrity-wait", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  2783. if (!ic->wait_wq) {
  2784. ti->error = "Cannot allocate workqueue";
  2785. r = -ENOMEM;
  2786. goto bad;
  2787. }
  2788. ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
  2789. if (!ic->commit_wq) {
  2790. ti->error = "Cannot allocate workqueue";
  2791. r = -ENOMEM;
  2792. goto bad;
  2793. }
  2794. INIT_WORK(&ic->commit_work, integrity_commit);
  2795. if (ic->mode == 'J') {
  2796. ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
  2797. if (!ic->writer_wq) {
  2798. ti->error = "Cannot allocate workqueue";
  2799. r = -ENOMEM;
  2800. goto bad;
  2801. }
  2802. INIT_WORK(&ic->writer_work, integrity_writer);
  2803. }
  2804. ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
  2805. if (!ic->sb) {
  2806. r = -ENOMEM;
  2807. ti->error = "Cannot allocate superblock area";
  2808. goto bad;
  2809. }
  2810. r = sync_rw_sb(ic, REQ_OP_READ, 0);
  2811. if (r) {
  2812. ti->error = "Error reading superblock";
  2813. goto bad;
  2814. }
  2815. should_write_sb = false;
  2816. if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
  2817. if (ic->mode != 'R') {
  2818. if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) {
  2819. r = -EINVAL;
  2820. ti->error = "The device is not initialized";
  2821. goto bad;
  2822. }
  2823. }
  2824. r = initialize_superblock(ic, journal_sectors, interleave_sectors);
  2825. if (r) {
  2826. ti->error = "Could not initialize superblock";
  2827. goto bad;
  2828. }
  2829. if (ic->mode != 'R')
  2830. should_write_sb = true;
  2831. }
  2832. if (!ic->sb->version || ic->sb->version > SB_VERSION_2) {
  2833. r = -EINVAL;
  2834. ti->error = "Unknown version";
  2835. goto bad;
  2836. }
  2837. if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
  2838. r = -EINVAL;
  2839. ti->error = "Tag size doesn't match the information in superblock";
  2840. goto bad;
  2841. }
  2842. if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) {
  2843. r = -EINVAL;
  2844. ti->error = "Block size doesn't match the information in superblock";
  2845. goto bad;
  2846. }
  2847. if (!le32_to_cpu(ic->sb->journal_sections)) {
  2848. r = -EINVAL;
  2849. ti->error = "Corrupted superblock, journal_sections is 0";
  2850. goto bad;
  2851. }
  2852. /* make sure that ti->max_io_len doesn't overflow */
  2853. if (!ic->meta_dev) {
  2854. if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS ||
  2855. ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) {
  2856. r = -EINVAL;
  2857. ti->error = "Invalid interleave_sectors in the superblock";
  2858. goto bad;
  2859. }
  2860. } else {
  2861. if (ic->sb->log2_interleave_sectors) {
  2862. r = -EINVAL;
  2863. ti->error = "Invalid interleave_sectors in the superblock";
  2864. goto bad;
  2865. }
  2866. }
  2867. ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
  2868. if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) {
  2869. /* test for overflow */
  2870. r = -EINVAL;
  2871. ti->error = "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
  2872. goto bad;
  2873. }
  2874. if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
  2875. r = -EINVAL;
  2876. ti->error = "Journal mac mismatch";
  2877. goto bad;
  2878. }
  2879. try_smaller_buffer:
  2880. r = calculate_device_limits(ic);
  2881. if (r) {
  2882. if (ic->meta_dev) {
  2883. if (ic->log2_buffer_sectors > 3) {
  2884. ic->log2_buffer_sectors--;
  2885. goto try_smaller_buffer;
  2886. }
  2887. }
  2888. ti->error = "The device is too small";
  2889. goto bad;
  2890. }
  2891. if (!ic->meta_dev)
  2892. ic->log2_buffer_sectors = min(ic->log2_buffer_sectors, (__u8)__ffs(ic->metadata_run));
  2893. if (ti->len > ic->provided_data_sectors) {
  2894. r = -EINVAL;
  2895. ti->error = "Not enough provided sectors for requested mapping size";
  2896. goto bad;
  2897. }
  2898. threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
  2899. threshold += 50;
  2900. do_div(threshold, 100);
  2901. ic->free_sectors_threshold = threshold;
  2902. DEBUG_print("initialized:\n");
  2903. DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
  2904. DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
  2905. DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
  2906. DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
  2907. DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
  2908. DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections));
  2909. DEBUG_print(" journal_entries %u\n", ic->journal_entries);
  2910. DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
  2911. DEBUG_print(" device_sectors 0x%llx\n", (unsigned long long)ic->device_sectors);
  2912. DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
  2913. DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
  2914. DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
  2915. DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic->provided_data_sectors,
  2916. (unsigned long long)ic->provided_data_sectors);
  2917. DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
  2918. if (recalculate && !(ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))) {
  2919. ic->sb->flags |= cpu_to_le32(SB_FLAG_RECALCULATING);
  2920. ic->sb->recalc_sector = cpu_to_le64(0);
  2921. }
  2922. if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
  2923. if (!ic->internal_hash) {
  2924. r = -EINVAL;
  2925. ti->error = "Recalculate is only valid with internal hash";
  2926. goto bad;
  2927. }
  2928. ic->recalc_wq = alloc_workqueue("dm-intergrity-recalc", WQ_MEM_RECLAIM, 1);
  2929. if (!ic->recalc_wq ) {
  2930. ti->error = "Cannot allocate workqueue";
  2931. r = -ENOMEM;
  2932. goto bad;
  2933. }
  2934. INIT_WORK(&ic->recalc_work, integrity_recalc);
  2935. ic->recalc_buffer = vmalloc(RECALC_SECTORS << SECTOR_SHIFT);
  2936. if (!ic->recalc_buffer) {
  2937. ti->error = "Cannot allocate buffer for recalculating";
  2938. r = -ENOMEM;
  2939. goto bad;
  2940. }
  2941. ic->recalc_tags = kvmalloc((RECALC_SECTORS >> ic->sb->log2_sectors_per_block) * ic->tag_size, GFP_KERNEL);
  2942. if (!ic->recalc_tags) {
  2943. ti->error = "Cannot allocate tags for recalculating";
  2944. r = -ENOMEM;
  2945. goto bad;
  2946. }
  2947. }
  2948. ic->bufio = dm_bufio_client_create(ic->meta_dev ? ic->meta_dev->bdev : ic->dev->bdev,
  2949. 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors), 1, 0, NULL, NULL);
  2950. if (IS_ERR(ic->bufio)) {
  2951. r = PTR_ERR(ic->bufio);
  2952. ti->error = "Cannot initialize dm-bufio";
  2953. ic->bufio = NULL;
  2954. goto bad;
  2955. }
  2956. dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
  2957. if (ic->mode != 'R') {
  2958. r = create_journal(ic, &ti->error);
  2959. if (r)
  2960. goto bad;
  2961. }
  2962. if (should_write_sb) {
  2963. int r;
  2964. init_journal(ic, 0, ic->journal_sections, 0);
  2965. r = dm_integrity_failed(ic);
  2966. if (unlikely(r)) {
  2967. ti->error = "Error initializing journal";
  2968. goto bad;
  2969. }
  2970. r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
  2971. if (r) {
  2972. ti->error = "Error initializing superblock";
  2973. goto bad;
  2974. }
  2975. ic->just_formatted = true;
  2976. }
  2977. if (!ic->meta_dev) {
  2978. r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
  2979. if (r)
  2980. goto bad;
  2981. }
  2982. if (!ic->internal_hash)
  2983. dm_integrity_set(ti, ic);
  2984. ti->num_flush_bios = 1;
  2985. ti->flush_supported = true;
  2986. return 0;
  2987. bad:
  2988. dm_integrity_dtr(ti);
  2989. return r;
  2990. }
  2991. static void dm_integrity_dtr(struct dm_target *ti)
  2992. {
  2993. struct dm_integrity_c *ic = ti->private;
  2994. BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
  2995. BUG_ON(!list_empty(&ic->wait_list));
  2996. if (ic->metadata_wq)
  2997. destroy_workqueue(ic->metadata_wq);
  2998. if (ic->wait_wq)
  2999. destroy_workqueue(ic->wait_wq);
  3000. if (ic->commit_wq)
  3001. destroy_workqueue(ic->commit_wq);
  3002. if (ic->writer_wq)
  3003. destroy_workqueue(ic->writer_wq);
  3004. if (ic->recalc_wq)
  3005. destroy_workqueue(ic->recalc_wq);
  3006. if (ic->recalc_buffer)
  3007. vfree(ic->recalc_buffer);
  3008. if (ic->recalc_tags)
  3009. kvfree(ic->recalc_tags);
  3010. if (ic->bufio)
  3011. dm_bufio_client_destroy(ic->bufio);
  3012. mempool_exit(&ic->journal_io_mempool);
  3013. if (ic->io)
  3014. dm_io_client_destroy(ic->io);
  3015. if (ic->dev)
  3016. dm_put_device(ti, ic->dev);
  3017. if (ic->meta_dev)
  3018. dm_put_device(ti, ic->meta_dev);
  3019. dm_integrity_free_page_list(ic, ic->journal);
  3020. dm_integrity_free_page_list(ic, ic->journal_io);
  3021. dm_integrity_free_page_list(ic, ic->journal_xor);
  3022. if (ic->journal_scatterlist)
  3023. dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
  3024. if (ic->journal_io_scatterlist)
  3025. dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
  3026. if (ic->sk_requests) {
  3027. unsigned i;
  3028. for (i = 0; i < ic->journal_sections; i++) {
  3029. struct skcipher_request *req = ic->sk_requests[i];
  3030. if (req) {
  3031. kzfree(req->iv);
  3032. skcipher_request_free(req);
  3033. }
  3034. }
  3035. kvfree(ic->sk_requests);
  3036. }
  3037. kvfree(ic->journal_tree);
  3038. if (ic->sb)
  3039. free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
  3040. if (ic->internal_hash)
  3041. crypto_free_shash(ic->internal_hash);
  3042. free_alg(&ic->internal_hash_alg);
  3043. if (ic->journal_crypt)
  3044. crypto_free_skcipher(ic->journal_crypt);
  3045. free_alg(&ic->journal_crypt_alg);
  3046. if (ic->journal_mac)
  3047. crypto_free_shash(ic->journal_mac);
  3048. free_alg(&ic->journal_mac_alg);
  3049. kfree(ic);
  3050. }
  3051. static struct target_type integrity_target = {
  3052. .name = "integrity",
  3053. .version = {1, 2, 0},
  3054. .module = THIS_MODULE,
  3055. .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
  3056. .ctr = dm_integrity_ctr,
  3057. .dtr = dm_integrity_dtr,
  3058. .map = dm_integrity_map,
  3059. .postsuspend = dm_integrity_postsuspend,
  3060. .resume = dm_integrity_resume,
  3061. .status = dm_integrity_status,
  3062. .iterate_devices = dm_integrity_iterate_devices,
  3063. .io_hints = dm_integrity_io_hints,
  3064. };
  3065. int __init dm_integrity_init(void)
  3066. {
  3067. int r;
  3068. journal_io_cache = kmem_cache_create("integrity_journal_io",
  3069. sizeof(struct journal_io), 0, 0, NULL);
  3070. if (!journal_io_cache) {
  3071. DMERR("can't allocate journal io cache");
  3072. return -ENOMEM;
  3073. }
  3074. r = dm_register_target(&integrity_target);
  3075. if (r < 0)
  3076. DMERR("register failed %d", r);
  3077. return r;
  3078. }
  3079. void dm_integrity_exit(void)
  3080. {
  3081. dm_unregister_target(&integrity_target);
  3082. kmem_cache_destroy(journal_io_cache);
  3083. }
  3084. module_init(dm_integrity_init);
  3085. module_exit(dm_integrity_exit);
  3086. MODULE_AUTHOR("Milan Broz");
  3087. MODULE_AUTHOR("Mikulas Patocka");
  3088. MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
  3089. MODULE_LICENSE("GPL");