dm-integrity.c 90 KB

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