dm-integrity.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299
  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/module.h>
  9. #include <linux/device-mapper.h>
  10. #include <linux/dm-io.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/sort.h>
  13. #include <linux/rbtree.h>
  14. #include <linux/delay.h>
  15. #include <linux/random.h>
  16. #include <crypto/hash.h>
  17. #include <crypto/skcipher.h>
  18. #include <linux/async_tx.h>
  19. #include "dm-bufio.h"
  20. #define DM_MSG_PREFIX "integrity"
  21. #define DEFAULT_INTERLEAVE_SECTORS 32768
  22. #define DEFAULT_JOURNAL_SIZE_FACTOR 7
  23. #define DEFAULT_BUFFER_SECTORS 128
  24. #define DEFAULT_JOURNAL_WATERMARK 50
  25. #define DEFAULT_SYNC_MSEC 10000
  26. #define DEFAULT_MAX_JOURNAL_SECTORS 131072
  27. #define MIN_LOG2_INTERLEAVE_SECTORS 3
  28. #define MAX_LOG2_INTERLEAVE_SECTORS 31
  29. #define METADATA_WORKQUEUE_MAX_ACTIVE 16
  30. /*
  31. * Warning - DEBUG_PRINT prints security-sensitive data to the log,
  32. * so it should not be enabled in the official kernel
  33. */
  34. //#define DEBUG_PRINT
  35. //#define INTERNAL_VERIFY
  36. /*
  37. * On disk structures
  38. */
  39. #define SB_MAGIC "integrt"
  40. #define SB_VERSION 1
  41. #define SB_SECTORS 8
  42. #define MAX_SECTORS_PER_BLOCK 8
  43. struct superblock {
  44. __u8 magic[8];
  45. __u8 version;
  46. __u8 log2_interleave_sectors;
  47. __u16 integrity_tag_size;
  48. __u32 journal_sections;
  49. __u64 provided_data_sectors; /* userspace uses this value */
  50. __u32 flags;
  51. __u8 log2_sectors_per_block;
  52. };
  53. #define SB_FLAG_HAVE_JOURNAL_MAC 0x1
  54. #define JOURNAL_ENTRY_ROUNDUP 8
  55. typedef __u64 commit_id_t;
  56. #define JOURNAL_MAC_PER_SECTOR 8
  57. struct journal_entry {
  58. union {
  59. struct {
  60. __u32 sector_lo;
  61. __u32 sector_hi;
  62. } s;
  63. __u64 sector;
  64. } u;
  65. commit_id_t last_bytes[0];
  66. /* __u8 tag[0]; */
  67. };
  68. #define journal_entry_tag(ic, je) ((__u8 *)&(je)->last_bytes[(ic)->sectors_per_block])
  69. #if BITS_PER_LONG == 64
  70. #define journal_entry_set_sector(je, x) do { smp_wmb(); ACCESS_ONCE((je)->u.sector) = cpu_to_le64(x); } while (0)
  71. #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
  72. #elif defined(CONFIG_LBDAF)
  73. #define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); ACCESS_ONCE((je)->u.s.sector_hi) = cpu_to_le32((x) >> 32); } while (0)
  74. #define journal_entry_get_sector(je) le64_to_cpu((je)->u.sector)
  75. #else
  76. #define journal_entry_set_sector(je, x) do { (je)->u.s.sector_lo = cpu_to_le32(x); smp_wmb(); ACCESS_ONCE((je)->u.s.sector_hi) = cpu_to_le32(0); } while (0)
  77. #define journal_entry_get_sector(je) le32_to_cpu((je)->u.s.sector_lo)
  78. #endif
  79. #define journal_entry_is_unused(je) ((je)->u.s.sector_hi == cpu_to_le32(-1))
  80. #define journal_entry_set_unused(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-1)); } while (0)
  81. #define journal_entry_is_inprogress(je) ((je)->u.s.sector_hi == cpu_to_le32(-2))
  82. #define journal_entry_set_inprogress(je) do { ((je)->u.s.sector_hi = cpu_to_le32(-2)); } while (0)
  83. #define JOURNAL_BLOCK_SECTORS 8
  84. #define JOURNAL_SECTOR_DATA ((1 << SECTOR_SHIFT) - sizeof(commit_id_t))
  85. #define JOURNAL_MAC_SIZE (JOURNAL_MAC_PER_SECTOR * JOURNAL_BLOCK_SECTORS)
  86. struct journal_sector {
  87. __u8 entries[JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR];
  88. __u8 mac[JOURNAL_MAC_PER_SECTOR];
  89. commit_id_t commit_id;
  90. };
  91. #define MAX_TAG_SIZE (JOURNAL_SECTOR_DATA - JOURNAL_MAC_PER_SECTOR - offsetof(struct journal_entry, last_bytes[MAX_SECTORS_PER_BLOCK]))
  92. #define METADATA_PADDING_SECTORS 8
  93. #define N_COMMIT_IDS 4
  94. static unsigned char prev_commit_seq(unsigned char seq)
  95. {
  96. return (seq + N_COMMIT_IDS - 1) % N_COMMIT_IDS;
  97. }
  98. static unsigned char next_commit_seq(unsigned char seq)
  99. {
  100. return (seq + 1) % N_COMMIT_IDS;
  101. }
  102. /*
  103. * In-memory structures
  104. */
  105. struct journal_node {
  106. struct rb_node node;
  107. sector_t sector;
  108. };
  109. struct alg_spec {
  110. char *alg_string;
  111. char *key_string;
  112. __u8 *key;
  113. unsigned key_size;
  114. };
  115. struct dm_integrity_c {
  116. struct dm_dev *dev;
  117. unsigned tag_size;
  118. __s8 log2_tag_size;
  119. sector_t start;
  120. mempool_t *journal_io_mempool;
  121. struct dm_io_client *io;
  122. struct dm_bufio_client *bufio;
  123. struct workqueue_struct *metadata_wq;
  124. struct superblock *sb;
  125. unsigned journal_pages;
  126. struct page_list *journal;
  127. struct page_list *journal_io;
  128. struct page_list *journal_xor;
  129. struct crypto_skcipher *journal_crypt;
  130. struct scatterlist **journal_scatterlist;
  131. struct scatterlist **journal_io_scatterlist;
  132. struct skcipher_request **sk_requests;
  133. struct crypto_shash *journal_mac;
  134. struct journal_node *journal_tree;
  135. struct rb_root journal_tree_root;
  136. sector_t provided_data_sectors;
  137. unsigned short journal_entry_size;
  138. unsigned char journal_entries_per_sector;
  139. unsigned char journal_section_entries;
  140. unsigned short journal_section_sectors;
  141. unsigned journal_sections;
  142. unsigned journal_entries;
  143. sector_t device_sectors;
  144. unsigned initial_sectors;
  145. unsigned metadata_run;
  146. __s8 log2_metadata_run;
  147. __u8 log2_buffer_sectors;
  148. __u8 sectors_per_block;
  149. unsigned char mode;
  150. int suspending;
  151. int failed;
  152. struct crypto_shash *internal_hash;
  153. /* these variables are locked with endio_wait.lock */
  154. struct rb_root in_progress;
  155. wait_queue_head_t endio_wait;
  156. struct workqueue_struct *wait_wq;
  157. struct workqueue_struct *offload_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 ACCESS_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 = 0;
  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,
  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(unsigned long data)
  899. {
  900. struct dm_integrity_c *ic = (struct dm_integrity_c *)data;
  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->offload_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_LIMIT("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_LIMIT("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 (ACCESS_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->offload_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 (ACCESS_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 (ACCESS_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. WRITE_ONCE(ic->suspending, 1);
  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. WRITE_ONCE(ic->suspending, 0);
  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. kvfree(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 (!strncmp(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 (!strncmp(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 (!strncmp(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. setup_timer(&ic->autocommit_timer, autocommit_fn, (unsigned long)ic);
  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->offload_wq = alloc_workqueue("dm-integrity-offload", WQ_MEM_RECLAIM,
  2533. METADATA_WORKQUEUE_MAX_ACTIVE);
  2534. if (!ic->offload_wq) {
  2535. ti->error = "Cannot allocate workqueue";
  2536. r = -ENOMEM;
  2537. goto bad;
  2538. }
  2539. ic->commit_wq = alloc_workqueue("dm-integrity-commit", WQ_MEM_RECLAIM, 1);
  2540. if (!ic->commit_wq) {
  2541. ti->error = "Cannot allocate workqueue";
  2542. r = -ENOMEM;
  2543. goto bad;
  2544. }
  2545. INIT_WORK(&ic->commit_work, integrity_commit);
  2546. if (ic->mode == 'J') {
  2547. ic->writer_wq = alloc_workqueue("dm-integrity-writer", WQ_MEM_RECLAIM, 1);
  2548. if (!ic->writer_wq) {
  2549. ti->error = "Cannot allocate workqueue";
  2550. r = -ENOMEM;
  2551. goto bad;
  2552. }
  2553. INIT_WORK(&ic->writer_work, integrity_writer);
  2554. }
  2555. ic->sb = alloc_pages_exact(SB_SECTORS << SECTOR_SHIFT, GFP_KERNEL);
  2556. if (!ic->sb) {
  2557. r = -ENOMEM;
  2558. ti->error = "Cannot allocate superblock area";
  2559. goto bad;
  2560. }
  2561. r = sync_rw_sb(ic, REQ_OP_READ, 0);
  2562. if (r) {
  2563. ti->error = "Error reading superblock";
  2564. goto bad;
  2565. }
  2566. should_write_sb = false;
  2567. if (memcmp(ic->sb->magic, SB_MAGIC, 8)) {
  2568. if (ic->mode != 'R') {
  2569. if (memchr_inv(ic->sb, 0, SB_SECTORS << SECTOR_SHIFT)) {
  2570. r = -EINVAL;
  2571. ti->error = "The device is not initialized";
  2572. goto bad;
  2573. }
  2574. }
  2575. r = initialize_superblock(ic, journal_sectors, interleave_sectors);
  2576. if (r) {
  2577. ti->error = "Could not initialize superblock";
  2578. goto bad;
  2579. }
  2580. if (ic->mode != 'R')
  2581. should_write_sb = true;
  2582. }
  2583. if (ic->sb->version != SB_VERSION) {
  2584. r = -EINVAL;
  2585. ti->error = "Unknown version";
  2586. goto bad;
  2587. }
  2588. if (le16_to_cpu(ic->sb->integrity_tag_size) != ic->tag_size) {
  2589. r = -EINVAL;
  2590. ti->error = "Tag size doesn't match the information in superblock";
  2591. goto bad;
  2592. }
  2593. if (ic->sb->log2_sectors_per_block != __ffs(ic->sectors_per_block)) {
  2594. r = -EINVAL;
  2595. ti->error = "Block size doesn't match the information in superblock";
  2596. goto bad;
  2597. }
  2598. if (!le32_to_cpu(ic->sb->journal_sections)) {
  2599. r = -EINVAL;
  2600. ti->error = "Corrupted superblock, journal_sections is 0";
  2601. goto bad;
  2602. }
  2603. /* make sure that ti->max_io_len doesn't overflow */
  2604. if (ic->sb->log2_interleave_sectors < MIN_LOG2_INTERLEAVE_SECTORS ||
  2605. ic->sb->log2_interleave_sectors > MAX_LOG2_INTERLEAVE_SECTORS) {
  2606. r = -EINVAL;
  2607. ti->error = "Invalid interleave_sectors in the superblock";
  2608. goto bad;
  2609. }
  2610. ic->provided_data_sectors = le64_to_cpu(ic->sb->provided_data_sectors);
  2611. if (ic->provided_data_sectors != le64_to_cpu(ic->sb->provided_data_sectors)) {
  2612. /* test for overflow */
  2613. r = -EINVAL;
  2614. ti->error = "The superblock has 64-bit device size, but the kernel was compiled with 32-bit sectors";
  2615. goto bad;
  2616. }
  2617. if (!!(ic->sb->flags & cpu_to_le32(SB_FLAG_HAVE_JOURNAL_MAC)) != !!ic->journal_mac_alg.alg_string) {
  2618. r = -EINVAL;
  2619. ti->error = "Journal mac mismatch";
  2620. goto bad;
  2621. }
  2622. r = calculate_device_limits(ic);
  2623. if (r) {
  2624. ti->error = "The device is too small";
  2625. goto bad;
  2626. }
  2627. if (ti->len > ic->provided_data_sectors) {
  2628. r = -EINVAL;
  2629. ti->error = "Not enough provided sectors for requested mapping size";
  2630. goto bad;
  2631. }
  2632. if (!buffer_sectors)
  2633. buffer_sectors = 1;
  2634. ic->log2_buffer_sectors = min3((int)__fls(buffer_sectors), (int)__ffs(ic->metadata_run), 31 - SECTOR_SHIFT);
  2635. threshold = (__u64)ic->journal_entries * (100 - journal_watermark);
  2636. threshold += 50;
  2637. do_div(threshold, 100);
  2638. ic->free_sectors_threshold = threshold;
  2639. DEBUG_print("initialized:\n");
  2640. DEBUG_print(" integrity_tag_size %u\n", le16_to_cpu(ic->sb->integrity_tag_size));
  2641. DEBUG_print(" journal_entry_size %u\n", ic->journal_entry_size);
  2642. DEBUG_print(" journal_entries_per_sector %u\n", ic->journal_entries_per_sector);
  2643. DEBUG_print(" journal_section_entries %u\n", ic->journal_section_entries);
  2644. DEBUG_print(" journal_section_sectors %u\n", ic->journal_section_sectors);
  2645. DEBUG_print(" journal_sections %u\n", (unsigned)le32_to_cpu(ic->sb->journal_sections));
  2646. DEBUG_print(" journal_entries %u\n", ic->journal_entries);
  2647. DEBUG_print(" log2_interleave_sectors %d\n", ic->sb->log2_interleave_sectors);
  2648. DEBUG_print(" device_sectors 0x%llx\n", (unsigned long long)ic->device_sectors);
  2649. DEBUG_print(" initial_sectors 0x%x\n", ic->initial_sectors);
  2650. DEBUG_print(" metadata_run 0x%x\n", ic->metadata_run);
  2651. DEBUG_print(" log2_metadata_run %d\n", ic->log2_metadata_run);
  2652. DEBUG_print(" provided_data_sectors 0x%llx (%llu)\n", (unsigned long long)ic->provided_data_sectors,
  2653. (unsigned long long)ic->provided_data_sectors);
  2654. DEBUG_print(" log2_buffer_sectors %u\n", ic->log2_buffer_sectors);
  2655. ic->bufio = dm_bufio_client_create(ic->dev->bdev, 1U << (SECTOR_SHIFT + ic->log2_buffer_sectors),
  2656. 1, 0, NULL, NULL);
  2657. if (IS_ERR(ic->bufio)) {
  2658. r = PTR_ERR(ic->bufio);
  2659. ti->error = "Cannot initialize dm-bufio";
  2660. ic->bufio = NULL;
  2661. goto bad;
  2662. }
  2663. dm_bufio_set_sector_offset(ic->bufio, ic->start + ic->initial_sectors);
  2664. if (ic->mode != 'R') {
  2665. r = create_journal(ic, &ti->error);
  2666. if (r)
  2667. goto bad;
  2668. }
  2669. if (should_write_sb) {
  2670. int r;
  2671. init_journal(ic, 0, ic->journal_sections, 0);
  2672. r = dm_integrity_failed(ic);
  2673. if (unlikely(r)) {
  2674. ti->error = "Error initializing journal";
  2675. goto bad;
  2676. }
  2677. r = sync_rw_sb(ic, REQ_OP_WRITE, REQ_FUA);
  2678. if (r) {
  2679. ti->error = "Error initializing superblock";
  2680. goto bad;
  2681. }
  2682. ic->just_formatted = true;
  2683. }
  2684. r = dm_set_target_max_io_len(ti, 1U << ic->sb->log2_interleave_sectors);
  2685. if (r)
  2686. goto bad;
  2687. if (!ic->internal_hash)
  2688. dm_integrity_set(ti, ic);
  2689. ti->num_flush_bios = 1;
  2690. ti->flush_supported = true;
  2691. return 0;
  2692. bad:
  2693. dm_integrity_dtr(ti);
  2694. return r;
  2695. }
  2696. static void dm_integrity_dtr(struct dm_target *ti)
  2697. {
  2698. struct dm_integrity_c *ic = ti->private;
  2699. BUG_ON(!RB_EMPTY_ROOT(&ic->in_progress));
  2700. if (ic->metadata_wq)
  2701. destroy_workqueue(ic->metadata_wq);
  2702. if (ic->wait_wq)
  2703. destroy_workqueue(ic->wait_wq);
  2704. if (ic->offload_wq)
  2705. destroy_workqueue(ic->offload_wq);
  2706. if (ic->commit_wq)
  2707. destroy_workqueue(ic->commit_wq);
  2708. if (ic->writer_wq)
  2709. destroy_workqueue(ic->writer_wq);
  2710. if (ic->bufio)
  2711. dm_bufio_client_destroy(ic->bufio);
  2712. mempool_destroy(ic->journal_io_mempool);
  2713. if (ic->io)
  2714. dm_io_client_destroy(ic->io);
  2715. if (ic->dev)
  2716. dm_put_device(ti, ic->dev);
  2717. dm_integrity_free_page_list(ic, ic->journal);
  2718. dm_integrity_free_page_list(ic, ic->journal_io);
  2719. dm_integrity_free_page_list(ic, ic->journal_xor);
  2720. if (ic->journal_scatterlist)
  2721. dm_integrity_free_journal_scatterlist(ic, ic->journal_scatterlist);
  2722. if (ic->journal_io_scatterlist)
  2723. dm_integrity_free_journal_scatterlist(ic, ic->journal_io_scatterlist);
  2724. if (ic->sk_requests) {
  2725. unsigned i;
  2726. for (i = 0; i < ic->journal_sections; i++) {
  2727. struct skcipher_request *req = ic->sk_requests[i];
  2728. if (req) {
  2729. kzfree(req->iv);
  2730. skcipher_request_free(req);
  2731. }
  2732. }
  2733. kvfree(ic->sk_requests);
  2734. }
  2735. kvfree(ic->journal_tree);
  2736. if (ic->sb)
  2737. free_pages_exact(ic->sb, SB_SECTORS << SECTOR_SHIFT);
  2738. if (ic->internal_hash)
  2739. crypto_free_shash(ic->internal_hash);
  2740. free_alg(&ic->internal_hash_alg);
  2741. if (ic->journal_crypt)
  2742. crypto_free_skcipher(ic->journal_crypt);
  2743. free_alg(&ic->journal_crypt_alg);
  2744. if (ic->journal_mac)
  2745. crypto_free_shash(ic->journal_mac);
  2746. free_alg(&ic->journal_mac_alg);
  2747. kfree(ic);
  2748. }
  2749. static struct target_type integrity_target = {
  2750. .name = "integrity",
  2751. .version = {1, 1, 0},
  2752. .module = THIS_MODULE,
  2753. .features = DM_TARGET_SINGLETON | DM_TARGET_INTEGRITY,
  2754. .ctr = dm_integrity_ctr,
  2755. .dtr = dm_integrity_dtr,
  2756. .map = dm_integrity_map,
  2757. .postsuspend = dm_integrity_postsuspend,
  2758. .resume = dm_integrity_resume,
  2759. .status = dm_integrity_status,
  2760. .iterate_devices = dm_integrity_iterate_devices,
  2761. .io_hints = dm_integrity_io_hints,
  2762. };
  2763. int __init dm_integrity_init(void)
  2764. {
  2765. int r;
  2766. journal_io_cache = kmem_cache_create("integrity_journal_io",
  2767. sizeof(struct journal_io), 0, 0, NULL);
  2768. if (!journal_io_cache) {
  2769. DMERR("can't allocate journal io cache");
  2770. return -ENOMEM;
  2771. }
  2772. r = dm_register_target(&integrity_target);
  2773. if (r < 0)
  2774. DMERR("register failed %d", r);
  2775. return r;
  2776. }
  2777. void dm_integrity_exit(void)
  2778. {
  2779. dm_unregister_target(&integrity_target);
  2780. kmem_cache_destroy(journal_io_cache);
  2781. }
  2782. module_init(dm_integrity_init);
  2783. module_exit(dm_integrity_exit);
  2784. MODULE_AUTHOR("Milan Broz");
  2785. MODULE_AUTHOR("Mikulas Patocka");
  2786. MODULE_DESCRIPTION(DM_NAME " target for integrity tags extension");
  2787. MODULE_LICENSE("GPL");