dm-integrity.c 92 KB

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