dm-clone-target.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2019 Arrikto, Inc. All Rights Reserved.
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/bio.h>
  7. #include <linux/err.h>
  8. #include <linux/hash.h>
  9. #include <linux/list.h>
  10. #include <linux/log2.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/wait.h>
  14. #include <linux/dm-io.h>
  15. #include <linux/mutex.h>
  16. #include <linux/atomic.h>
  17. #include <linux/bitops.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/kdev_t.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/mempool.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/blk_types.h>
  26. #include <linux/dm-kcopyd.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/device-mapper.h>
  30. #include "dm.h"
  31. #include "dm-clone-metadata.h"
  32. #define DM_MSG_PREFIX "clone"
  33. /*
  34. * Minimum and maximum allowed region sizes
  35. */
  36. #define MIN_REGION_SIZE (1 << 3) /* 4KB */
  37. #define MAX_REGION_SIZE (1 << 21) /* 1GB */
  38. #define MIN_HYDRATIONS 256 /* Size of hydration mempool */
  39. #define DEFAULT_HYDRATION_THRESHOLD 1 /* 1 region */
  40. #define DEFAULT_HYDRATION_BATCH_SIZE 1 /* Hydrate in batches of 1 region */
  41. #define COMMIT_PERIOD HZ /* 1 sec */
  42. /*
  43. * Hydration hash table size: 1 << HASH_TABLE_BITS
  44. */
  45. #define HASH_TABLE_BITS 15
  46. DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(clone_hydration_throttle,
  47. "A percentage of time allocated for hydrating regions");
  48. /* Slab cache for struct dm_clone_region_hydration */
  49. static struct kmem_cache *_hydration_cache;
  50. /* dm-clone metadata modes */
  51. enum clone_metadata_mode {
  52. CM_WRITE, /* metadata may be changed */
  53. CM_READ_ONLY, /* metadata may not be changed */
  54. CM_FAIL, /* all metadata I/O fails */
  55. };
  56. struct hash_table_bucket;
  57. struct clone {
  58. struct dm_target *ti;
  59. struct dm_dev *metadata_dev;
  60. struct dm_dev *dest_dev;
  61. struct dm_dev *source_dev;
  62. unsigned long nr_regions;
  63. sector_t region_size;
  64. unsigned int region_shift;
  65. /*
  66. * A metadata commit and the actions taken in case it fails should run
  67. * as a single atomic step.
  68. */
  69. struct mutex commit_lock;
  70. struct dm_clone_metadata *cmd;
  71. /* Region hydration hash table */
  72. struct hash_table_bucket *ht;
  73. atomic_t ios_in_flight;
  74. wait_queue_head_t hydration_stopped;
  75. mempool_t hydration_pool;
  76. unsigned long last_commit_jiffies;
  77. /*
  78. * We defer incoming WRITE bios for regions that are not hydrated,
  79. * until after these regions have been hydrated.
  80. *
  81. * Also, we defer REQ_FUA and REQ_PREFLUSH bios, until after the
  82. * metadata have been committed.
  83. */
  84. spinlock_t lock;
  85. struct bio_list deferred_bios;
  86. struct bio_list deferred_discard_bios;
  87. struct bio_list deferred_flush_bios;
  88. struct bio_list deferred_flush_completions;
  89. /* Maximum number of regions being copied during background hydration. */
  90. unsigned int hydration_threshold;
  91. /* Number of regions to batch together during background hydration. */
  92. unsigned int hydration_batch_size;
  93. /* Which region to hydrate next */
  94. unsigned long hydration_offset;
  95. atomic_t hydrations_in_flight;
  96. /*
  97. * Save a copy of the table line rather than reconstructing it for the
  98. * status.
  99. */
  100. unsigned int nr_ctr_args;
  101. const char **ctr_args;
  102. struct workqueue_struct *wq;
  103. struct work_struct worker;
  104. struct delayed_work waker;
  105. struct dm_kcopyd_client *kcopyd_client;
  106. enum clone_metadata_mode mode;
  107. unsigned long flags;
  108. };
  109. /*
  110. * dm-clone flags
  111. */
  112. #define DM_CLONE_DISCARD_PASSDOWN 0
  113. #define DM_CLONE_HYDRATION_ENABLED 1
  114. #define DM_CLONE_HYDRATION_SUSPENDED 2
  115. /*---------------------------------------------------------------------------*/
  116. /*
  117. * Metadata failure handling.
  118. */
  119. static enum clone_metadata_mode get_clone_mode(struct clone *clone)
  120. {
  121. return READ_ONCE(clone->mode);
  122. }
  123. static const char *clone_device_name(struct clone *clone)
  124. {
  125. return dm_table_device_name(clone->ti->table);
  126. }
  127. static void __set_clone_mode(struct clone *clone, enum clone_metadata_mode new_mode)
  128. {
  129. static const char * const descs[] = {
  130. "read-write",
  131. "read-only",
  132. "fail"
  133. };
  134. enum clone_metadata_mode old_mode = get_clone_mode(clone);
  135. /* Never move out of fail mode */
  136. if (old_mode == CM_FAIL)
  137. new_mode = CM_FAIL;
  138. switch (new_mode) {
  139. case CM_FAIL:
  140. case CM_READ_ONLY:
  141. dm_clone_metadata_set_read_only(clone->cmd);
  142. break;
  143. case CM_WRITE:
  144. dm_clone_metadata_set_read_write(clone->cmd);
  145. break;
  146. }
  147. WRITE_ONCE(clone->mode, new_mode);
  148. if (new_mode != old_mode) {
  149. dm_table_event(clone->ti->table);
  150. DMINFO("%s: Switching to %s mode", clone_device_name(clone),
  151. descs[(int)new_mode]);
  152. }
  153. }
  154. static void __abort_transaction(struct clone *clone)
  155. {
  156. const char *dev_name = clone_device_name(clone);
  157. if (get_clone_mode(clone) >= CM_READ_ONLY)
  158. return;
  159. DMERR("%s: Aborting current metadata transaction", dev_name);
  160. if (dm_clone_metadata_abort(clone->cmd)) {
  161. DMERR("%s: Failed to abort metadata transaction", dev_name);
  162. __set_clone_mode(clone, CM_FAIL);
  163. }
  164. }
  165. static void __reload_in_core_bitset(struct clone *clone)
  166. {
  167. const char *dev_name = clone_device_name(clone);
  168. if (get_clone_mode(clone) == CM_FAIL)
  169. return;
  170. /* Reload the on-disk bitset */
  171. DMINFO("%s: Reloading on-disk bitmap", dev_name);
  172. if (dm_clone_reload_in_core_bitset(clone->cmd)) {
  173. DMERR("%s: Failed to reload on-disk bitmap", dev_name);
  174. __set_clone_mode(clone, CM_FAIL);
  175. }
  176. }
  177. static void __metadata_operation_failed(struct clone *clone, const char *op, int r)
  178. {
  179. DMERR("%s: Metadata operation `%s' failed: error = %d",
  180. clone_device_name(clone), op, r);
  181. __abort_transaction(clone);
  182. __set_clone_mode(clone, CM_READ_ONLY);
  183. /*
  184. * dm_clone_reload_in_core_bitset() may run concurrently with either
  185. * dm_clone_set_region_hydrated() or dm_clone_cond_set_range(), but
  186. * it's safe as we have already set the metadata to read-only mode.
  187. */
  188. __reload_in_core_bitset(clone);
  189. }
  190. /*---------------------------------------------------------------------------*/
  191. /* Wake up anyone waiting for region hydrations to stop */
  192. static inline void wakeup_hydration_waiters(struct clone *clone)
  193. {
  194. wake_up_all(&clone->hydration_stopped);
  195. }
  196. static inline void wake_worker(struct clone *clone)
  197. {
  198. queue_work(clone->wq, &clone->worker);
  199. }
  200. /*---------------------------------------------------------------------------*/
  201. /*
  202. * bio helper functions.
  203. */
  204. static inline void remap_to_source(struct clone *clone, struct bio *bio)
  205. {
  206. bio_set_dev(bio, clone->source_dev->bdev);
  207. }
  208. static inline void remap_to_dest(struct clone *clone, struct bio *bio)
  209. {
  210. bio_set_dev(bio, clone->dest_dev->bdev);
  211. }
  212. static bool bio_triggers_commit(struct clone *clone, struct bio *bio)
  213. {
  214. return op_is_flush(bio->bi_opf) &&
  215. dm_clone_changed_this_transaction(clone->cmd);
  216. }
  217. /* Get the address of the region in sectors */
  218. static inline sector_t region_to_sector(struct clone *clone, unsigned long region_nr)
  219. {
  220. return ((sector_t)region_nr << clone->region_shift);
  221. }
  222. /* Get the region number of the bio */
  223. static inline unsigned long bio_to_region(struct clone *clone, struct bio *bio)
  224. {
  225. return (bio->bi_iter.bi_sector >> clone->region_shift);
  226. }
  227. /* Get the region range covered by the bio */
  228. static void bio_region_range(struct clone *clone, struct bio *bio,
  229. unsigned long *rs, unsigned long *nr_regions)
  230. {
  231. unsigned long end;
  232. *rs = dm_sector_div_up(bio->bi_iter.bi_sector, clone->region_size);
  233. end = bio_end_sector(bio) >> clone->region_shift;
  234. if (*rs >= end)
  235. *nr_regions = 0;
  236. else
  237. *nr_regions = end - *rs;
  238. }
  239. /* Check whether a bio overwrites a region */
  240. static inline bool is_overwrite_bio(struct clone *clone, struct bio *bio)
  241. {
  242. return (bio_data_dir(bio) == WRITE && bio_sectors(bio) == clone->region_size);
  243. }
  244. static void fail_bios(struct bio_list *bios, blk_status_t status)
  245. {
  246. struct bio *bio;
  247. while ((bio = bio_list_pop(bios))) {
  248. bio->bi_status = status;
  249. bio_endio(bio);
  250. }
  251. }
  252. static void submit_bios(struct bio_list *bios)
  253. {
  254. struct bio *bio;
  255. struct blk_plug plug;
  256. blk_start_plug(&plug);
  257. while ((bio = bio_list_pop(bios)))
  258. submit_bio_noacct(bio);
  259. blk_finish_plug(&plug);
  260. }
  261. /*
  262. * Submit bio to the underlying device.
  263. *
  264. * If the bio triggers a commit, delay it, until after the metadata have been
  265. * committed.
  266. *
  267. * NOTE: The bio remapping must be performed by the caller.
  268. */
  269. static void issue_bio(struct clone *clone, struct bio *bio)
  270. {
  271. if (!bio_triggers_commit(clone, bio)) {
  272. submit_bio_noacct(bio);
  273. return;
  274. }
  275. /*
  276. * If the metadata mode is RO or FAIL we won't be able to commit the
  277. * metadata, so we complete the bio with an error.
  278. */
  279. if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
  280. bio_io_error(bio);
  281. return;
  282. }
  283. /*
  284. * Batch together any bios that trigger commits and then issue a single
  285. * commit for them in process_deferred_flush_bios().
  286. */
  287. spin_lock_irq(&clone->lock);
  288. bio_list_add(&clone->deferred_flush_bios, bio);
  289. spin_unlock_irq(&clone->lock);
  290. wake_worker(clone);
  291. }
  292. /*
  293. * Remap bio to the destination device and submit it.
  294. *
  295. * If the bio triggers a commit, delay it, until after the metadata have been
  296. * committed.
  297. */
  298. static void remap_and_issue(struct clone *clone, struct bio *bio)
  299. {
  300. remap_to_dest(clone, bio);
  301. issue_bio(clone, bio);
  302. }
  303. /*
  304. * Issue bios that have been deferred until after their region has finished
  305. * hydrating.
  306. *
  307. * We delegate the bio submission to the worker thread, so this is safe to call
  308. * from interrupt context.
  309. */
  310. static void issue_deferred_bios(struct clone *clone, struct bio_list *bios)
  311. {
  312. struct bio *bio;
  313. unsigned long flags;
  314. struct bio_list flush_bios = BIO_EMPTY_LIST;
  315. struct bio_list normal_bios = BIO_EMPTY_LIST;
  316. if (bio_list_empty(bios))
  317. return;
  318. while ((bio = bio_list_pop(bios))) {
  319. if (bio_triggers_commit(clone, bio))
  320. bio_list_add(&flush_bios, bio);
  321. else
  322. bio_list_add(&normal_bios, bio);
  323. }
  324. spin_lock_irqsave(&clone->lock, flags);
  325. bio_list_merge(&clone->deferred_bios, &normal_bios);
  326. bio_list_merge(&clone->deferred_flush_bios, &flush_bios);
  327. spin_unlock_irqrestore(&clone->lock, flags);
  328. wake_worker(clone);
  329. }
  330. static void complete_overwrite_bio(struct clone *clone, struct bio *bio)
  331. {
  332. unsigned long flags;
  333. /*
  334. * If the bio has the REQ_FUA flag set we must commit the metadata
  335. * before signaling its completion.
  336. *
  337. * complete_overwrite_bio() is only called by hydration_complete(),
  338. * after having successfully updated the metadata. This means we don't
  339. * need to call dm_clone_changed_this_transaction() to check if the
  340. * metadata has changed and thus we can avoid taking the metadata spin
  341. * lock.
  342. */
  343. if (!(bio->bi_opf & REQ_FUA)) {
  344. bio_endio(bio);
  345. return;
  346. }
  347. /*
  348. * If the metadata mode is RO or FAIL we won't be able to commit the
  349. * metadata, so we complete the bio with an error.
  350. */
  351. if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
  352. bio_io_error(bio);
  353. return;
  354. }
  355. /*
  356. * Batch together any bios that trigger commits and then issue a single
  357. * commit for them in process_deferred_flush_bios().
  358. */
  359. spin_lock_irqsave(&clone->lock, flags);
  360. bio_list_add(&clone->deferred_flush_completions, bio);
  361. spin_unlock_irqrestore(&clone->lock, flags);
  362. wake_worker(clone);
  363. }
  364. static void trim_bio(struct bio *bio, sector_t sector, unsigned int len)
  365. {
  366. bio->bi_iter.bi_sector = sector;
  367. bio->bi_iter.bi_size = to_bytes(len);
  368. }
  369. static void complete_discard_bio(struct clone *clone, struct bio *bio, bool success)
  370. {
  371. unsigned long rs, nr_regions;
  372. /*
  373. * If the destination device supports discards, remap and trim the
  374. * discard bio and pass it down. Otherwise complete the bio
  375. * immediately.
  376. */
  377. if (test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags) && success) {
  378. remap_to_dest(clone, bio);
  379. bio_region_range(clone, bio, &rs, &nr_regions);
  380. trim_bio(bio, region_to_sector(clone, rs),
  381. nr_regions << clone->region_shift);
  382. submit_bio_noacct(bio);
  383. } else
  384. bio_endio(bio);
  385. }
  386. static void process_discard_bio(struct clone *clone, struct bio *bio)
  387. {
  388. unsigned long rs, nr_regions;
  389. bio_region_range(clone, bio, &rs, &nr_regions);
  390. if (!nr_regions) {
  391. bio_endio(bio);
  392. return;
  393. }
  394. if (WARN_ON(rs >= clone->nr_regions || (rs + nr_regions) < rs ||
  395. (rs + nr_regions) > clone->nr_regions)) {
  396. DMERR("%s: Invalid range (%lu + %lu, total regions %lu) for discard (%llu + %u)",
  397. clone_device_name(clone), rs, nr_regions,
  398. clone->nr_regions,
  399. (unsigned long long)bio->bi_iter.bi_sector,
  400. bio_sectors(bio));
  401. bio_endio(bio);
  402. return;
  403. }
  404. /*
  405. * The covered regions are already hydrated so we just need to pass
  406. * down the discard.
  407. */
  408. if (dm_clone_is_range_hydrated(clone->cmd, rs, nr_regions)) {
  409. complete_discard_bio(clone, bio, true);
  410. return;
  411. }
  412. /*
  413. * If the metadata mode is RO or FAIL we won't be able to update the
  414. * metadata for the regions covered by the discard so we just ignore
  415. * it.
  416. */
  417. if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
  418. bio_endio(bio);
  419. return;
  420. }
  421. /*
  422. * Defer discard processing.
  423. */
  424. spin_lock_irq(&clone->lock);
  425. bio_list_add(&clone->deferred_discard_bios, bio);
  426. spin_unlock_irq(&clone->lock);
  427. wake_worker(clone);
  428. }
  429. /*---------------------------------------------------------------------------*/
  430. /*
  431. * dm-clone region hydrations.
  432. */
  433. struct dm_clone_region_hydration {
  434. struct clone *clone;
  435. unsigned long region_nr;
  436. struct bio *overwrite_bio;
  437. bio_end_io_t *overwrite_bio_end_io;
  438. struct bio_list deferred_bios;
  439. blk_status_t status;
  440. /* Used by hydration batching */
  441. struct list_head list;
  442. /* Used by hydration hash table */
  443. struct hlist_node h;
  444. };
  445. /*
  446. * Hydration hash table implementation.
  447. *
  448. * Ideally we would like to use list_bl, which uses bit spin locks and employs
  449. * the least significant bit of the list head to lock the corresponding bucket,
  450. * reducing the memory overhead for the locks. But, currently, list_bl and bit
  451. * spin locks don't support IRQ safe versions. Since we have to take the lock
  452. * in both process and interrupt context, we must fall back to using regular
  453. * spin locks; one per hash table bucket.
  454. */
  455. struct hash_table_bucket {
  456. struct hlist_head head;
  457. /* Spinlock protecting the bucket */
  458. spinlock_t lock;
  459. };
  460. #define bucket_lock_irqsave(bucket, flags) \
  461. spin_lock_irqsave(&(bucket)->lock, flags)
  462. #define bucket_unlock_irqrestore(bucket, flags) \
  463. spin_unlock_irqrestore(&(bucket)->lock, flags)
  464. #define bucket_lock_irq(bucket) \
  465. spin_lock_irq(&(bucket)->lock)
  466. #define bucket_unlock_irq(bucket) \
  467. spin_unlock_irq(&(bucket)->lock)
  468. static int hash_table_init(struct clone *clone)
  469. {
  470. unsigned int i, sz;
  471. struct hash_table_bucket *bucket;
  472. sz = 1 << HASH_TABLE_BITS;
  473. clone->ht = kvmalloc(sz * sizeof(struct hash_table_bucket), GFP_KERNEL);
  474. if (!clone->ht)
  475. return -ENOMEM;
  476. for (i = 0; i < sz; i++) {
  477. bucket = clone->ht + i;
  478. INIT_HLIST_HEAD(&bucket->head);
  479. spin_lock_init(&bucket->lock);
  480. }
  481. return 0;
  482. }
  483. static void hash_table_exit(struct clone *clone)
  484. {
  485. kvfree(clone->ht);
  486. }
  487. static struct hash_table_bucket *get_hash_table_bucket(struct clone *clone,
  488. unsigned long region_nr)
  489. {
  490. return &clone->ht[hash_long(region_nr, HASH_TABLE_BITS)];
  491. }
  492. /*
  493. * Search hash table for a hydration with hd->region_nr == region_nr
  494. *
  495. * NOTE: Must be called with the bucket lock held
  496. */
  497. static struct dm_clone_region_hydration *__hash_find(struct hash_table_bucket *bucket,
  498. unsigned long region_nr)
  499. {
  500. struct dm_clone_region_hydration *hd;
  501. hlist_for_each_entry(hd, &bucket->head, h) {
  502. if (hd->region_nr == region_nr)
  503. return hd;
  504. }
  505. return NULL;
  506. }
  507. /*
  508. * Insert a hydration into the hash table.
  509. *
  510. * NOTE: Must be called with the bucket lock held.
  511. */
  512. static inline void __insert_region_hydration(struct hash_table_bucket *bucket,
  513. struct dm_clone_region_hydration *hd)
  514. {
  515. hlist_add_head(&hd->h, &bucket->head);
  516. }
  517. /*
  518. * This function inserts a hydration into the hash table, unless someone else
  519. * managed to insert a hydration for the same region first. In the latter case
  520. * it returns the existing hydration descriptor for this region.
  521. *
  522. * NOTE: Must be called with the hydration hash table lock held.
  523. */
  524. static struct dm_clone_region_hydration *
  525. __find_or_insert_region_hydration(struct hash_table_bucket *bucket,
  526. struct dm_clone_region_hydration *hd)
  527. {
  528. struct dm_clone_region_hydration *hd2;
  529. hd2 = __hash_find(bucket, hd->region_nr);
  530. if (hd2)
  531. return hd2;
  532. __insert_region_hydration(bucket, hd);
  533. return hd;
  534. }
  535. /*---------------------------------------------------------------------------*/
  536. /* Allocate a hydration */
  537. static struct dm_clone_region_hydration *alloc_hydration(struct clone *clone)
  538. {
  539. struct dm_clone_region_hydration *hd;
  540. /*
  541. * Allocate a hydration from the hydration mempool.
  542. * This might block but it can't fail.
  543. */
  544. hd = mempool_alloc(&clone->hydration_pool, GFP_NOIO);
  545. hd->clone = clone;
  546. return hd;
  547. }
  548. static inline void free_hydration(struct dm_clone_region_hydration *hd)
  549. {
  550. mempool_free(hd, &hd->clone->hydration_pool);
  551. }
  552. /* Initialize a hydration */
  553. static void hydration_init(struct dm_clone_region_hydration *hd, unsigned long region_nr)
  554. {
  555. hd->region_nr = region_nr;
  556. hd->overwrite_bio = NULL;
  557. bio_list_init(&hd->deferred_bios);
  558. hd->status = 0;
  559. INIT_LIST_HEAD(&hd->list);
  560. INIT_HLIST_NODE(&hd->h);
  561. }
  562. /*---------------------------------------------------------------------------*/
  563. /*
  564. * Update dm-clone's metadata after a region has finished hydrating and remove
  565. * hydration from the hash table.
  566. */
  567. static int hydration_update_metadata(struct dm_clone_region_hydration *hd)
  568. {
  569. int r = 0;
  570. unsigned long flags;
  571. struct hash_table_bucket *bucket;
  572. struct clone *clone = hd->clone;
  573. if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY))
  574. r = -EPERM;
  575. /* Update the metadata */
  576. if (likely(!r) && hd->status == BLK_STS_OK)
  577. r = dm_clone_set_region_hydrated(clone->cmd, hd->region_nr);
  578. bucket = get_hash_table_bucket(clone, hd->region_nr);
  579. /* Remove hydration from hash table */
  580. bucket_lock_irqsave(bucket, flags);
  581. hlist_del(&hd->h);
  582. bucket_unlock_irqrestore(bucket, flags);
  583. return r;
  584. }
  585. /*
  586. * Complete a region's hydration:
  587. *
  588. * 1. Update dm-clone's metadata.
  589. * 2. Remove hydration from hash table.
  590. * 3. Complete overwrite bio.
  591. * 4. Issue deferred bios.
  592. * 5. If this was the last hydration, wake up anyone waiting for
  593. * hydrations to finish.
  594. */
  595. static void hydration_complete(struct dm_clone_region_hydration *hd)
  596. {
  597. int r;
  598. blk_status_t status;
  599. struct clone *clone = hd->clone;
  600. r = hydration_update_metadata(hd);
  601. if (hd->status == BLK_STS_OK && likely(!r)) {
  602. if (hd->overwrite_bio)
  603. complete_overwrite_bio(clone, hd->overwrite_bio);
  604. issue_deferred_bios(clone, &hd->deferred_bios);
  605. } else {
  606. status = r ? BLK_STS_IOERR : hd->status;
  607. if (hd->overwrite_bio)
  608. bio_list_add(&hd->deferred_bios, hd->overwrite_bio);
  609. fail_bios(&hd->deferred_bios, status);
  610. }
  611. free_hydration(hd);
  612. if (atomic_dec_and_test(&clone->hydrations_in_flight))
  613. wakeup_hydration_waiters(clone);
  614. }
  615. static void hydration_kcopyd_callback(int read_err, unsigned long write_err, void *context)
  616. {
  617. blk_status_t status;
  618. struct dm_clone_region_hydration *tmp, *hd = context;
  619. struct clone *clone = hd->clone;
  620. LIST_HEAD(batched_hydrations);
  621. if (read_err || write_err) {
  622. DMERR_LIMIT("%s: hydration failed", clone_device_name(clone));
  623. status = BLK_STS_IOERR;
  624. } else {
  625. status = BLK_STS_OK;
  626. }
  627. list_splice_tail(&hd->list, &batched_hydrations);
  628. hd->status = status;
  629. hydration_complete(hd);
  630. /* Complete batched hydrations */
  631. list_for_each_entry_safe(hd, tmp, &batched_hydrations, list) {
  632. hd->status = status;
  633. hydration_complete(hd);
  634. }
  635. /* Continue background hydration, if there is no I/O in-flight */
  636. if (test_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags) &&
  637. !atomic_read(&clone->ios_in_flight))
  638. wake_worker(clone);
  639. }
  640. static void hydration_copy(struct dm_clone_region_hydration *hd, unsigned int nr_regions)
  641. {
  642. unsigned long region_start, region_end;
  643. sector_t tail_size, region_size, total_size;
  644. struct dm_io_region from, to;
  645. struct clone *clone = hd->clone;
  646. if (WARN_ON(!nr_regions))
  647. return;
  648. region_size = clone->region_size;
  649. region_start = hd->region_nr;
  650. region_end = region_start + nr_regions - 1;
  651. total_size = region_to_sector(clone, nr_regions - 1);
  652. if (region_end == clone->nr_regions - 1) {
  653. /*
  654. * The last region of the target might be smaller than
  655. * region_size.
  656. */
  657. tail_size = clone->ti->len & (region_size - 1);
  658. if (!tail_size)
  659. tail_size = region_size;
  660. } else {
  661. tail_size = region_size;
  662. }
  663. total_size += tail_size;
  664. from.bdev = clone->source_dev->bdev;
  665. from.sector = region_to_sector(clone, region_start);
  666. from.count = total_size;
  667. to.bdev = clone->dest_dev->bdev;
  668. to.sector = from.sector;
  669. to.count = from.count;
  670. /* Issue copy */
  671. atomic_add(nr_regions, &clone->hydrations_in_flight);
  672. dm_kcopyd_copy(clone->kcopyd_client, &from, 1, &to, 0,
  673. hydration_kcopyd_callback, hd);
  674. }
  675. static void overwrite_endio(struct bio *bio)
  676. {
  677. struct dm_clone_region_hydration *hd = bio->bi_private;
  678. bio->bi_end_io = hd->overwrite_bio_end_io;
  679. hd->status = bio->bi_status;
  680. hydration_complete(hd);
  681. }
  682. static void hydration_overwrite(struct dm_clone_region_hydration *hd, struct bio *bio)
  683. {
  684. /*
  685. * We don't need to save and restore bio->bi_private because device
  686. * mapper core generates a new bio for us to use, with clean
  687. * bi_private.
  688. */
  689. hd->overwrite_bio = bio;
  690. hd->overwrite_bio_end_io = bio->bi_end_io;
  691. bio->bi_end_io = overwrite_endio;
  692. bio->bi_private = hd;
  693. atomic_inc(&hd->clone->hydrations_in_flight);
  694. submit_bio_noacct(bio);
  695. }
  696. /*
  697. * Hydrate bio's region.
  698. *
  699. * This function starts the hydration of the bio's region and puts the bio in
  700. * the list of deferred bios for this region. In case, by the time this
  701. * function is called, the region has finished hydrating it's submitted to the
  702. * destination device.
  703. *
  704. * NOTE: The bio remapping must be performed by the caller.
  705. */
  706. static void hydrate_bio_region(struct clone *clone, struct bio *bio)
  707. {
  708. unsigned long region_nr;
  709. struct hash_table_bucket *bucket;
  710. struct dm_clone_region_hydration *hd, *hd2;
  711. region_nr = bio_to_region(clone, bio);
  712. bucket = get_hash_table_bucket(clone, region_nr);
  713. bucket_lock_irq(bucket);
  714. hd = __hash_find(bucket, region_nr);
  715. if (hd) {
  716. /* Someone else is hydrating the region */
  717. bio_list_add(&hd->deferred_bios, bio);
  718. bucket_unlock_irq(bucket);
  719. return;
  720. }
  721. if (dm_clone_is_region_hydrated(clone->cmd, region_nr)) {
  722. /* The region has been hydrated */
  723. bucket_unlock_irq(bucket);
  724. issue_bio(clone, bio);
  725. return;
  726. }
  727. /*
  728. * We must allocate a hydration descriptor and start the hydration of
  729. * the corresponding region.
  730. */
  731. bucket_unlock_irq(bucket);
  732. hd = alloc_hydration(clone);
  733. hydration_init(hd, region_nr);
  734. bucket_lock_irq(bucket);
  735. /* Check if the region has been hydrated in the meantime. */
  736. if (dm_clone_is_region_hydrated(clone->cmd, region_nr)) {
  737. bucket_unlock_irq(bucket);
  738. free_hydration(hd);
  739. issue_bio(clone, bio);
  740. return;
  741. }
  742. hd2 = __find_or_insert_region_hydration(bucket, hd);
  743. if (hd2 != hd) {
  744. /* Someone else started the region's hydration. */
  745. bio_list_add(&hd2->deferred_bios, bio);
  746. bucket_unlock_irq(bucket);
  747. free_hydration(hd);
  748. return;
  749. }
  750. /*
  751. * If the metadata mode is RO or FAIL then there is no point starting a
  752. * hydration, since we will not be able to update the metadata when the
  753. * hydration finishes.
  754. */
  755. if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
  756. hlist_del(&hd->h);
  757. bucket_unlock_irq(bucket);
  758. free_hydration(hd);
  759. bio_io_error(bio);
  760. return;
  761. }
  762. /*
  763. * Start region hydration.
  764. *
  765. * If a bio overwrites a region, i.e., its size is equal to the
  766. * region's size, then we don't need to copy the region from the source
  767. * to the destination device.
  768. */
  769. if (is_overwrite_bio(clone, bio)) {
  770. bucket_unlock_irq(bucket);
  771. hydration_overwrite(hd, bio);
  772. } else {
  773. bio_list_add(&hd->deferred_bios, bio);
  774. bucket_unlock_irq(bucket);
  775. hydration_copy(hd, 1);
  776. }
  777. }
  778. /*---------------------------------------------------------------------------*/
  779. /*
  780. * Background hydrations.
  781. */
  782. /*
  783. * Batch region hydrations.
  784. *
  785. * To better utilize device bandwidth we batch together the hydration of
  786. * adjacent regions. This allows us to use small region sizes, e.g., 4KB, which
  787. * is good for small, random write performance (because of the overwriting of
  788. * un-hydrated regions) and at the same time issue big copy requests to kcopyd
  789. * to achieve high hydration bandwidth.
  790. */
  791. struct batch_info {
  792. struct dm_clone_region_hydration *head;
  793. unsigned int nr_batched_regions;
  794. };
  795. static void __batch_hydration(struct batch_info *batch,
  796. struct dm_clone_region_hydration *hd)
  797. {
  798. struct clone *clone = hd->clone;
  799. unsigned int max_batch_size = READ_ONCE(clone->hydration_batch_size);
  800. if (batch->head) {
  801. /* Try to extend the current batch */
  802. if (batch->nr_batched_regions < max_batch_size &&
  803. (batch->head->region_nr + batch->nr_batched_regions) == hd->region_nr) {
  804. list_add_tail(&hd->list, &batch->head->list);
  805. batch->nr_batched_regions++;
  806. hd = NULL;
  807. }
  808. /* Check if we should issue the current batch */
  809. if (batch->nr_batched_regions >= max_batch_size || hd) {
  810. hydration_copy(batch->head, batch->nr_batched_regions);
  811. batch->head = NULL;
  812. batch->nr_batched_regions = 0;
  813. }
  814. }
  815. if (!hd)
  816. return;
  817. /* We treat max batch sizes of zero and one equivalently */
  818. if (max_batch_size <= 1) {
  819. hydration_copy(hd, 1);
  820. return;
  821. }
  822. /* Start a new batch */
  823. BUG_ON(!list_empty(&hd->list));
  824. batch->head = hd;
  825. batch->nr_batched_regions = 1;
  826. }
  827. static unsigned long __start_next_hydration(struct clone *clone,
  828. unsigned long offset,
  829. struct batch_info *batch)
  830. {
  831. struct hash_table_bucket *bucket;
  832. struct dm_clone_region_hydration *hd;
  833. unsigned long nr_regions = clone->nr_regions;
  834. hd = alloc_hydration(clone);
  835. /* Try to find a region to hydrate. */
  836. do {
  837. offset = dm_clone_find_next_unhydrated_region(clone->cmd, offset);
  838. if (offset == nr_regions)
  839. break;
  840. bucket = get_hash_table_bucket(clone, offset);
  841. bucket_lock_irq(bucket);
  842. if (!dm_clone_is_region_hydrated(clone->cmd, offset) &&
  843. !__hash_find(bucket, offset)) {
  844. hydration_init(hd, offset);
  845. __insert_region_hydration(bucket, hd);
  846. bucket_unlock_irq(bucket);
  847. /* Batch hydration */
  848. __batch_hydration(batch, hd);
  849. return (offset + 1);
  850. }
  851. bucket_unlock_irq(bucket);
  852. } while (++offset < nr_regions);
  853. if (hd)
  854. free_hydration(hd);
  855. return offset;
  856. }
  857. /*
  858. * This function searches for regions that still reside in the source device
  859. * and starts their hydration.
  860. */
  861. static void do_hydration(struct clone *clone)
  862. {
  863. unsigned int current_volume;
  864. unsigned long offset, nr_regions = clone->nr_regions;
  865. struct batch_info batch = {
  866. .head = NULL,
  867. .nr_batched_regions = 0,
  868. };
  869. if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY))
  870. return;
  871. if (dm_clone_is_hydration_done(clone->cmd))
  872. return;
  873. /*
  874. * Avoid race with device suspension.
  875. */
  876. atomic_inc(&clone->hydrations_in_flight);
  877. /*
  878. * Make sure atomic_inc() is ordered before test_bit(), otherwise we
  879. * might race with clone_postsuspend() and start a region hydration
  880. * after the target has been suspended.
  881. *
  882. * This is paired with the smp_mb__after_atomic() in
  883. * clone_postsuspend().
  884. */
  885. smp_mb__after_atomic();
  886. offset = clone->hydration_offset;
  887. while (likely(!test_bit(DM_CLONE_HYDRATION_SUSPENDED, &clone->flags)) &&
  888. !atomic_read(&clone->ios_in_flight) &&
  889. test_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags) &&
  890. offset < nr_regions) {
  891. current_volume = atomic_read(&clone->hydrations_in_flight);
  892. current_volume += batch.nr_batched_regions;
  893. if (current_volume > READ_ONCE(clone->hydration_threshold))
  894. break;
  895. offset = __start_next_hydration(clone, offset, &batch);
  896. }
  897. if (batch.head)
  898. hydration_copy(batch.head, batch.nr_batched_regions);
  899. if (offset >= nr_regions)
  900. offset = 0;
  901. clone->hydration_offset = offset;
  902. if (atomic_dec_and_test(&clone->hydrations_in_flight))
  903. wakeup_hydration_waiters(clone);
  904. }
  905. /*---------------------------------------------------------------------------*/
  906. static bool need_commit_due_to_time(struct clone *clone)
  907. {
  908. return !time_in_range(jiffies, clone->last_commit_jiffies,
  909. clone->last_commit_jiffies + COMMIT_PERIOD);
  910. }
  911. /*
  912. * A non-zero return indicates read-only or fail mode.
  913. */
  914. static int commit_metadata(struct clone *clone, bool *dest_dev_flushed)
  915. {
  916. int r = 0;
  917. if (dest_dev_flushed)
  918. *dest_dev_flushed = false;
  919. mutex_lock(&clone->commit_lock);
  920. if (!dm_clone_changed_this_transaction(clone->cmd))
  921. goto out;
  922. if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY)) {
  923. r = -EPERM;
  924. goto out;
  925. }
  926. r = dm_clone_metadata_pre_commit(clone->cmd);
  927. if (unlikely(r)) {
  928. __metadata_operation_failed(clone, "dm_clone_metadata_pre_commit", r);
  929. goto out;
  930. }
  931. r = blkdev_issue_flush(clone->dest_dev->bdev);
  932. if (unlikely(r)) {
  933. __metadata_operation_failed(clone, "flush destination device", r);
  934. goto out;
  935. }
  936. if (dest_dev_flushed)
  937. *dest_dev_flushed = true;
  938. r = dm_clone_metadata_commit(clone->cmd);
  939. if (unlikely(r)) {
  940. __metadata_operation_failed(clone, "dm_clone_metadata_commit", r);
  941. goto out;
  942. }
  943. if (dm_clone_is_hydration_done(clone->cmd))
  944. dm_table_event(clone->ti->table);
  945. out:
  946. mutex_unlock(&clone->commit_lock);
  947. return r;
  948. }
  949. static void process_deferred_discards(struct clone *clone)
  950. {
  951. int r = -EPERM;
  952. struct bio *bio;
  953. struct blk_plug plug;
  954. unsigned long rs, nr_regions;
  955. struct bio_list discards = BIO_EMPTY_LIST;
  956. spin_lock_irq(&clone->lock);
  957. bio_list_merge(&discards, &clone->deferred_discard_bios);
  958. bio_list_init(&clone->deferred_discard_bios);
  959. spin_unlock_irq(&clone->lock);
  960. if (bio_list_empty(&discards))
  961. return;
  962. if (unlikely(get_clone_mode(clone) >= CM_READ_ONLY))
  963. goto out;
  964. /* Update the metadata */
  965. bio_list_for_each(bio, &discards) {
  966. bio_region_range(clone, bio, &rs, &nr_regions);
  967. /*
  968. * A discard request might cover regions that have been already
  969. * hydrated. There is no need to update the metadata for these
  970. * regions.
  971. */
  972. r = dm_clone_cond_set_range(clone->cmd, rs, nr_regions);
  973. if (unlikely(r))
  974. break;
  975. }
  976. out:
  977. blk_start_plug(&plug);
  978. while ((bio = bio_list_pop(&discards)))
  979. complete_discard_bio(clone, bio, r == 0);
  980. blk_finish_plug(&plug);
  981. }
  982. static void process_deferred_bios(struct clone *clone)
  983. {
  984. struct bio_list bios = BIO_EMPTY_LIST;
  985. spin_lock_irq(&clone->lock);
  986. bio_list_merge(&bios, &clone->deferred_bios);
  987. bio_list_init(&clone->deferred_bios);
  988. spin_unlock_irq(&clone->lock);
  989. if (bio_list_empty(&bios))
  990. return;
  991. submit_bios(&bios);
  992. }
  993. static void process_deferred_flush_bios(struct clone *clone)
  994. {
  995. struct bio *bio;
  996. bool dest_dev_flushed;
  997. struct bio_list bios = BIO_EMPTY_LIST;
  998. struct bio_list bio_completions = BIO_EMPTY_LIST;
  999. /*
  1000. * If there are any deferred flush bios, we must commit the metadata
  1001. * before issuing them or signaling their completion.
  1002. */
  1003. spin_lock_irq(&clone->lock);
  1004. bio_list_merge(&bios, &clone->deferred_flush_bios);
  1005. bio_list_init(&clone->deferred_flush_bios);
  1006. bio_list_merge(&bio_completions, &clone->deferred_flush_completions);
  1007. bio_list_init(&clone->deferred_flush_completions);
  1008. spin_unlock_irq(&clone->lock);
  1009. if (bio_list_empty(&bios) && bio_list_empty(&bio_completions) &&
  1010. !(dm_clone_changed_this_transaction(clone->cmd) && need_commit_due_to_time(clone)))
  1011. return;
  1012. if (commit_metadata(clone, &dest_dev_flushed)) {
  1013. bio_list_merge(&bios, &bio_completions);
  1014. while ((bio = bio_list_pop(&bios)))
  1015. bio_io_error(bio);
  1016. return;
  1017. }
  1018. clone->last_commit_jiffies = jiffies;
  1019. while ((bio = bio_list_pop(&bio_completions)))
  1020. bio_endio(bio);
  1021. while ((bio = bio_list_pop(&bios))) {
  1022. if ((bio->bi_opf & REQ_PREFLUSH) && dest_dev_flushed) {
  1023. /* We just flushed the destination device as part of
  1024. * the metadata commit, so there is no reason to send
  1025. * another flush.
  1026. */
  1027. bio_endio(bio);
  1028. } else {
  1029. submit_bio_noacct(bio);
  1030. }
  1031. }
  1032. }
  1033. static void do_worker(struct work_struct *work)
  1034. {
  1035. struct clone *clone = container_of(work, typeof(*clone), worker);
  1036. process_deferred_bios(clone);
  1037. process_deferred_discards(clone);
  1038. /*
  1039. * process_deferred_flush_bios():
  1040. *
  1041. * - Commit metadata
  1042. *
  1043. * - Process deferred REQ_FUA completions
  1044. *
  1045. * - Process deferred REQ_PREFLUSH bios
  1046. */
  1047. process_deferred_flush_bios(clone);
  1048. /* Background hydration */
  1049. do_hydration(clone);
  1050. }
  1051. /*
  1052. * Commit periodically so that not too much unwritten data builds up.
  1053. *
  1054. * Also, restart background hydration, if it has been stopped by in-flight I/O.
  1055. */
  1056. static void do_waker(struct work_struct *work)
  1057. {
  1058. struct clone *clone = container_of(to_delayed_work(work), struct clone, waker);
  1059. wake_worker(clone);
  1060. queue_delayed_work(clone->wq, &clone->waker, COMMIT_PERIOD);
  1061. }
  1062. /*---------------------------------------------------------------------------*/
  1063. /*
  1064. * Target methods
  1065. */
  1066. static int clone_map(struct dm_target *ti, struct bio *bio)
  1067. {
  1068. struct clone *clone = ti->private;
  1069. unsigned long region_nr;
  1070. atomic_inc(&clone->ios_in_flight);
  1071. if (unlikely(get_clone_mode(clone) == CM_FAIL))
  1072. return DM_MAPIO_KILL;
  1073. /*
  1074. * REQ_PREFLUSH bios carry no data:
  1075. *
  1076. * - Commit metadata, if changed
  1077. *
  1078. * - Pass down to destination device
  1079. */
  1080. if (bio->bi_opf & REQ_PREFLUSH) {
  1081. remap_and_issue(clone, bio);
  1082. return DM_MAPIO_SUBMITTED;
  1083. }
  1084. bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
  1085. /*
  1086. * dm-clone interprets discards and performs a fast hydration of the
  1087. * discarded regions, i.e., we skip the copy from the source device and
  1088. * just mark the regions as hydrated.
  1089. */
  1090. if (bio_op(bio) == REQ_OP_DISCARD) {
  1091. process_discard_bio(clone, bio);
  1092. return DM_MAPIO_SUBMITTED;
  1093. }
  1094. /*
  1095. * If the bio's region is hydrated, redirect it to the destination
  1096. * device.
  1097. *
  1098. * If the region is not hydrated and the bio is a READ, redirect it to
  1099. * the source device.
  1100. *
  1101. * Else, defer WRITE bio until after its region has been hydrated and
  1102. * start the region's hydration immediately.
  1103. */
  1104. region_nr = bio_to_region(clone, bio);
  1105. if (dm_clone_is_region_hydrated(clone->cmd, region_nr)) {
  1106. remap_and_issue(clone, bio);
  1107. return DM_MAPIO_SUBMITTED;
  1108. } else if (bio_data_dir(bio) == READ) {
  1109. remap_to_source(clone, bio);
  1110. return DM_MAPIO_REMAPPED;
  1111. }
  1112. remap_to_dest(clone, bio);
  1113. hydrate_bio_region(clone, bio);
  1114. return DM_MAPIO_SUBMITTED;
  1115. }
  1116. static int clone_endio(struct dm_target *ti, struct bio *bio, blk_status_t *error)
  1117. {
  1118. struct clone *clone = ti->private;
  1119. atomic_dec(&clone->ios_in_flight);
  1120. return DM_ENDIO_DONE;
  1121. }
  1122. static void emit_flags(struct clone *clone, char *result, unsigned int maxlen,
  1123. ssize_t *sz_ptr)
  1124. {
  1125. ssize_t sz = *sz_ptr;
  1126. unsigned int count;
  1127. count = !test_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags);
  1128. count += !test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags);
  1129. DMEMIT("%u ", count);
  1130. if (!test_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags))
  1131. DMEMIT("no_hydration ");
  1132. if (!test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags))
  1133. DMEMIT("no_discard_passdown ");
  1134. *sz_ptr = sz;
  1135. }
  1136. static void emit_core_args(struct clone *clone, char *result,
  1137. unsigned int maxlen, ssize_t *sz_ptr)
  1138. {
  1139. ssize_t sz = *sz_ptr;
  1140. unsigned int count = 4;
  1141. DMEMIT("%u hydration_threshold %u hydration_batch_size %u ", count,
  1142. READ_ONCE(clone->hydration_threshold),
  1143. READ_ONCE(clone->hydration_batch_size));
  1144. *sz_ptr = sz;
  1145. }
  1146. /*
  1147. * Status format:
  1148. *
  1149. * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
  1150. * <clone region size> <#hydrated regions>/<#total regions> <#hydrating regions>
  1151. * <#features> <features>* <#core args> <core args>* <clone metadata mode>
  1152. */
  1153. static void clone_status(struct dm_target *ti, status_type_t type,
  1154. unsigned int status_flags, char *result,
  1155. unsigned int maxlen)
  1156. {
  1157. int r;
  1158. unsigned int i;
  1159. ssize_t sz = 0;
  1160. dm_block_t nr_free_metadata_blocks = 0;
  1161. dm_block_t nr_metadata_blocks = 0;
  1162. char buf[BDEVNAME_SIZE];
  1163. struct clone *clone = ti->private;
  1164. switch (type) {
  1165. case STATUSTYPE_INFO:
  1166. if (get_clone_mode(clone) == CM_FAIL) {
  1167. DMEMIT("Fail");
  1168. break;
  1169. }
  1170. /* Commit to ensure statistics aren't out-of-date */
  1171. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  1172. (void) commit_metadata(clone, NULL);
  1173. r = dm_clone_get_free_metadata_block_count(clone->cmd, &nr_free_metadata_blocks);
  1174. if (r) {
  1175. DMERR("%s: dm_clone_get_free_metadata_block_count returned %d",
  1176. clone_device_name(clone), r);
  1177. goto error;
  1178. }
  1179. r = dm_clone_get_metadata_dev_size(clone->cmd, &nr_metadata_blocks);
  1180. if (r) {
  1181. DMERR("%s: dm_clone_get_metadata_dev_size returned %d",
  1182. clone_device_name(clone), r);
  1183. goto error;
  1184. }
  1185. DMEMIT("%u %llu/%llu %llu %u/%lu %u ",
  1186. DM_CLONE_METADATA_BLOCK_SIZE,
  1187. (unsigned long long)(nr_metadata_blocks - nr_free_metadata_blocks),
  1188. (unsigned long long)nr_metadata_blocks,
  1189. (unsigned long long)clone->region_size,
  1190. dm_clone_nr_of_hydrated_regions(clone->cmd),
  1191. clone->nr_regions,
  1192. atomic_read(&clone->hydrations_in_flight));
  1193. emit_flags(clone, result, maxlen, &sz);
  1194. emit_core_args(clone, result, maxlen, &sz);
  1195. switch (get_clone_mode(clone)) {
  1196. case CM_WRITE:
  1197. DMEMIT("rw");
  1198. break;
  1199. case CM_READ_ONLY:
  1200. DMEMIT("ro");
  1201. break;
  1202. case CM_FAIL:
  1203. DMEMIT("Fail");
  1204. }
  1205. break;
  1206. case STATUSTYPE_TABLE:
  1207. format_dev_t(buf, clone->metadata_dev->bdev->bd_dev);
  1208. DMEMIT("%s ", buf);
  1209. format_dev_t(buf, clone->dest_dev->bdev->bd_dev);
  1210. DMEMIT("%s ", buf);
  1211. format_dev_t(buf, clone->source_dev->bdev->bd_dev);
  1212. DMEMIT("%s", buf);
  1213. for (i = 0; i < clone->nr_ctr_args; i++)
  1214. DMEMIT(" %s", clone->ctr_args[i]);
  1215. break;
  1216. case STATUSTYPE_IMA:
  1217. *result = '\0';
  1218. break;
  1219. }
  1220. return;
  1221. error:
  1222. DMEMIT("Error");
  1223. }
  1224. static sector_t get_dev_size(struct dm_dev *dev)
  1225. {
  1226. return bdev_nr_sectors(dev->bdev);
  1227. }
  1228. /*---------------------------------------------------------------------------*/
  1229. /*
  1230. * Construct a clone device mapping:
  1231. *
  1232. * clone <metadata dev> <destination dev> <source dev> <region size>
  1233. * [<#feature args> [<feature arg>]* [<#core args> [key value]*]]
  1234. *
  1235. * metadata dev: Fast device holding the persistent metadata
  1236. * destination dev: The destination device, which will become a clone of the
  1237. * source device
  1238. * source dev: The read-only source device that gets cloned
  1239. * region size: dm-clone unit size in sectors
  1240. *
  1241. * #feature args: Number of feature arguments passed
  1242. * feature args: E.g. no_hydration, no_discard_passdown
  1243. *
  1244. * #core arguments: An even number of core arguments
  1245. * core arguments: Key/value pairs for tuning the core
  1246. * E.g. 'hydration_threshold 256'
  1247. */
  1248. static int parse_feature_args(struct dm_arg_set *as, struct clone *clone)
  1249. {
  1250. int r;
  1251. unsigned int argc;
  1252. const char *arg_name;
  1253. struct dm_target *ti = clone->ti;
  1254. const struct dm_arg args = {
  1255. .min = 0,
  1256. .max = 2,
  1257. .error = "Invalid number of feature arguments"
  1258. };
  1259. /* No feature arguments supplied */
  1260. if (!as->argc)
  1261. return 0;
  1262. r = dm_read_arg_group(&args, as, &argc, &ti->error);
  1263. if (r)
  1264. return r;
  1265. while (argc) {
  1266. arg_name = dm_shift_arg(as);
  1267. argc--;
  1268. if (!strcasecmp(arg_name, "no_hydration")) {
  1269. __clear_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags);
  1270. } else if (!strcasecmp(arg_name, "no_discard_passdown")) {
  1271. __clear_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags);
  1272. } else {
  1273. ti->error = "Invalid feature argument";
  1274. return -EINVAL;
  1275. }
  1276. }
  1277. return 0;
  1278. }
  1279. static int parse_core_args(struct dm_arg_set *as, struct clone *clone)
  1280. {
  1281. int r;
  1282. unsigned int argc;
  1283. unsigned int value;
  1284. const char *arg_name;
  1285. struct dm_target *ti = clone->ti;
  1286. const struct dm_arg args = {
  1287. .min = 0,
  1288. .max = 4,
  1289. .error = "Invalid number of core arguments"
  1290. };
  1291. /* Initialize core arguments */
  1292. clone->hydration_batch_size = DEFAULT_HYDRATION_BATCH_SIZE;
  1293. clone->hydration_threshold = DEFAULT_HYDRATION_THRESHOLD;
  1294. /* No core arguments supplied */
  1295. if (!as->argc)
  1296. return 0;
  1297. r = dm_read_arg_group(&args, as, &argc, &ti->error);
  1298. if (r)
  1299. return r;
  1300. if (argc & 1) {
  1301. ti->error = "Number of core arguments must be even";
  1302. return -EINVAL;
  1303. }
  1304. while (argc) {
  1305. arg_name = dm_shift_arg(as);
  1306. argc -= 2;
  1307. if (!strcasecmp(arg_name, "hydration_threshold")) {
  1308. if (kstrtouint(dm_shift_arg(as), 10, &value)) {
  1309. ti->error = "Invalid value for argument `hydration_threshold'";
  1310. return -EINVAL;
  1311. }
  1312. clone->hydration_threshold = value;
  1313. } else if (!strcasecmp(arg_name, "hydration_batch_size")) {
  1314. if (kstrtouint(dm_shift_arg(as), 10, &value)) {
  1315. ti->error = "Invalid value for argument `hydration_batch_size'";
  1316. return -EINVAL;
  1317. }
  1318. clone->hydration_batch_size = value;
  1319. } else {
  1320. ti->error = "Invalid core argument";
  1321. return -EINVAL;
  1322. }
  1323. }
  1324. return 0;
  1325. }
  1326. static int parse_region_size(struct clone *clone, struct dm_arg_set *as, char **error)
  1327. {
  1328. int r;
  1329. unsigned int region_size;
  1330. struct dm_arg arg;
  1331. arg.min = MIN_REGION_SIZE;
  1332. arg.max = MAX_REGION_SIZE;
  1333. arg.error = "Invalid region size";
  1334. r = dm_read_arg(&arg, as, &region_size, error);
  1335. if (r)
  1336. return r;
  1337. /* Check region size is a power of 2 */
  1338. if (!is_power_of_2(region_size)) {
  1339. *error = "Region size is not a power of 2";
  1340. return -EINVAL;
  1341. }
  1342. /* Validate the region size against the device logical block size */
  1343. if (region_size % (bdev_logical_block_size(clone->source_dev->bdev) >> 9) ||
  1344. region_size % (bdev_logical_block_size(clone->dest_dev->bdev) >> 9)) {
  1345. *error = "Region size is not a multiple of device logical block size";
  1346. return -EINVAL;
  1347. }
  1348. clone->region_size = region_size;
  1349. return 0;
  1350. }
  1351. static int validate_nr_regions(unsigned long n, char **error)
  1352. {
  1353. /*
  1354. * dm_bitset restricts us to 2^32 regions. test_bit & co. restrict us
  1355. * further to 2^31 regions.
  1356. */
  1357. if (n > (1UL << 31)) {
  1358. *error = "Too many regions. Consider increasing the region size";
  1359. return -EINVAL;
  1360. }
  1361. return 0;
  1362. }
  1363. static int parse_metadata_dev(struct clone *clone, struct dm_arg_set *as, char **error)
  1364. {
  1365. int r;
  1366. sector_t metadata_dev_size;
  1367. r = dm_get_device(clone->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1368. &clone->metadata_dev);
  1369. if (r) {
  1370. *error = "Error opening metadata device";
  1371. return r;
  1372. }
  1373. metadata_dev_size = get_dev_size(clone->metadata_dev);
  1374. if (metadata_dev_size > DM_CLONE_METADATA_MAX_SECTORS_WARNING)
  1375. DMWARN("Metadata device %pg is larger than %u sectors: excess space will not be used.",
  1376. clone->metadata_dev->bdev, DM_CLONE_METADATA_MAX_SECTORS);
  1377. return 0;
  1378. }
  1379. static int parse_dest_dev(struct clone *clone, struct dm_arg_set *as, char **error)
  1380. {
  1381. int r;
  1382. sector_t dest_dev_size;
  1383. r = dm_get_device(clone->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1384. &clone->dest_dev);
  1385. if (r) {
  1386. *error = "Error opening destination device";
  1387. return r;
  1388. }
  1389. dest_dev_size = get_dev_size(clone->dest_dev);
  1390. if (dest_dev_size < clone->ti->len) {
  1391. dm_put_device(clone->ti, clone->dest_dev);
  1392. *error = "Device size larger than destination device";
  1393. return -EINVAL;
  1394. }
  1395. return 0;
  1396. }
  1397. static int parse_source_dev(struct clone *clone, struct dm_arg_set *as, char **error)
  1398. {
  1399. int r;
  1400. sector_t source_dev_size;
  1401. r = dm_get_device(clone->ti, dm_shift_arg(as), FMODE_READ,
  1402. &clone->source_dev);
  1403. if (r) {
  1404. *error = "Error opening source device";
  1405. return r;
  1406. }
  1407. source_dev_size = get_dev_size(clone->source_dev);
  1408. if (source_dev_size < clone->ti->len) {
  1409. dm_put_device(clone->ti, clone->source_dev);
  1410. *error = "Device size larger than source device";
  1411. return -EINVAL;
  1412. }
  1413. return 0;
  1414. }
  1415. static int copy_ctr_args(struct clone *clone, int argc, const char **argv, char **error)
  1416. {
  1417. unsigned int i;
  1418. const char **copy;
  1419. copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
  1420. if (!copy)
  1421. goto error;
  1422. for (i = 0; i < argc; i++) {
  1423. copy[i] = kstrdup(argv[i], GFP_KERNEL);
  1424. if (!copy[i]) {
  1425. while (i--)
  1426. kfree(copy[i]);
  1427. kfree(copy);
  1428. goto error;
  1429. }
  1430. }
  1431. clone->nr_ctr_args = argc;
  1432. clone->ctr_args = copy;
  1433. return 0;
  1434. error:
  1435. *error = "Failed to allocate memory for table line";
  1436. return -ENOMEM;
  1437. }
  1438. static int clone_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1439. {
  1440. int r;
  1441. sector_t nr_regions;
  1442. struct clone *clone;
  1443. struct dm_arg_set as;
  1444. if (argc < 4) {
  1445. ti->error = "Invalid number of arguments";
  1446. return -EINVAL;
  1447. }
  1448. as.argc = argc;
  1449. as.argv = argv;
  1450. clone = kzalloc(sizeof(*clone), GFP_KERNEL);
  1451. if (!clone) {
  1452. ti->error = "Failed to allocate clone structure";
  1453. return -ENOMEM;
  1454. }
  1455. clone->ti = ti;
  1456. /* Initialize dm-clone flags */
  1457. __set_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags);
  1458. __set_bit(DM_CLONE_HYDRATION_SUSPENDED, &clone->flags);
  1459. __set_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags);
  1460. r = parse_metadata_dev(clone, &as, &ti->error);
  1461. if (r)
  1462. goto out_with_clone;
  1463. r = parse_dest_dev(clone, &as, &ti->error);
  1464. if (r)
  1465. goto out_with_meta_dev;
  1466. r = parse_source_dev(clone, &as, &ti->error);
  1467. if (r)
  1468. goto out_with_dest_dev;
  1469. r = parse_region_size(clone, &as, &ti->error);
  1470. if (r)
  1471. goto out_with_source_dev;
  1472. clone->region_shift = __ffs(clone->region_size);
  1473. nr_regions = dm_sector_div_up(ti->len, clone->region_size);
  1474. /* Check for overflow */
  1475. if (nr_regions != (unsigned long)nr_regions) {
  1476. ti->error = "Too many regions. Consider increasing the region size";
  1477. r = -EOVERFLOW;
  1478. goto out_with_source_dev;
  1479. }
  1480. clone->nr_regions = nr_regions;
  1481. r = validate_nr_regions(clone->nr_regions, &ti->error);
  1482. if (r)
  1483. goto out_with_source_dev;
  1484. r = dm_set_target_max_io_len(ti, clone->region_size);
  1485. if (r) {
  1486. ti->error = "Failed to set max io len";
  1487. goto out_with_source_dev;
  1488. }
  1489. r = parse_feature_args(&as, clone);
  1490. if (r)
  1491. goto out_with_source_dev;
  1492. r = parse_core_args(&as, clone);
  1493. if (r)
  1494. goto out_with_source_dev;
  1495. /* Load metadata */
  1496. clone->cmd = dm_clone_metadata_open(clone->metadata_dev->bdev, ti->len,
  1497. clone->region_size);
  1498. if (IS_ERR(clone->cmd)) {
  1499. ti->error = "Failed to load metadata";
  1500. r = PTR_ERR(clone->cmd);
  1501. goto out_with_source_dev;
  1502. }
  1503. __set_clone_mode(clone, CM_WRITE);
  1504. if (get_clone_mode(clone) != CM_WRITE) {
  1505. ti->error = "Unable to get write access to metadata, please check/repair metadata";
  1506. r = -EPERM;
  1507. goto out_with_metadata;
  1508. }
  1509. clone->last_commit_jiffies = jiffies;
  1510. /* Allocate hydration hash table */
  1511. r = hash_table_init(clone);
  1512. if (r) {
  1513. ti->error = "Failed to allocate hydration hash table";
  1514. goto out_with_metadata;
  1515. }
  1516. atomic_set(&clone->ios_in_flight, 0);
  1517. init_waitqueue_head(&clone->hydration_stopped);
  1518. spin_lock_init(&clone->lock);
  1519. bio_list_init(&clone->deferred_bios);
  1520. bio_list_init(&clone->deferred_discard_bios);
  1521. bio_list_init(&clone->deferred_flush_bios);
  1522. bio_list_init(&clone->deferred_flush_completions);
  1523. clone->hydration_offset = 0;
  1524. atomic_set(&clone->hydrations_in_flight, 0);
  1525. clone->wq = alloc_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM, 0);
  1526. if (!clone->wq) {
  1527. ti->error = "Failed to allocate workqueue";
  1528. r = -ENOMEM;
  1529. goto out_with_ht;
  1530. }
  1531. INIT_WORK(&clone->worker, do_worker);
  1532. INIT_DELAYED_WORK(&clone->waker, do_waker);
  1533. clone->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  1534. if (IS_ERR(clone->kcopyd_client)) {
  1535. r = PTR_ERR(clone->kcopyd_client);
  1536. goto out_with_wq;
  1537. }
  1538. r = mempool_init_slab_pool(&clone->hydration_pool, MIN_HYDRATIONS,
  1539. _hydration_cache);
  1540. if (r) {
  1541. ti->error = "Failed to create dm_clone_region_hydration memory pool";
  1542. goto out_with_kcopyd;
  1543. }
  1544. /* Save a copy of the table line */
  1545. r = copy_ctr_args(clone, argc - 3, (const char **)argv + 3, &ti->error);
  1546. if (r)
  1547. goto out_with_mempool;
  1548. mutex_init(&clone->commit_lock);
  1549. /* Enable flushes */
  1550. ti->num_flush_bios = 1;
  1551. ti->flush_supported = true;
  1552. /* Enable discards */
  1553. ti->discards_supported = true;
  1554. ti->num_discard_bios = 1;
  1555. ti->private = clone;
  1556. return 0;
  1557. out_with_mempool:
  1558. mempool_exit(&clone->hydration_pool);
  1559. out_with_kcopyd:
  1560. dm_kcopyd_client_destroy(clone->kcopyd_client);
  1561. out_with_wq:
  1562. destroy_workqueue(clone->wq);
  1563. out_with_ht:
  1564. hash_table_exit(clone);
  1565. out_with_metadata:
  1566. dm_clone_metadata_close(clone->cmd);
  1567. out_with_source_dev:
  1568. dm_put_device(ti, clone->source_dev);
  1569. out_with_dest_dev:
  1570. dm_put_device(ti, clone->dest_dev);
  1571. out_with_meta_dev:
  1572. dm_put_device(ti, clone->metadata_dev);
  1573. out_with_clone:
  1574. kfree(clone);
  1575. return r;
  1576. }
  1577. static void clone_dtr(struct dm_target *ti)
  1578. {
  1579. unsigned int i;
  1580. struct clone *clone = ti->private;
  1581. mutex_destroy(&clone->commit_lock);
  1582. for (i = 0; i < clone->nr_ctr_args; i++)
  1583. kfree(clone->ctr_args[i]);
  1584. kfree(clone->ctr_args);
  1585. mempool_exit(&clone->hydration_pool);
  1586. dm_kcopyd_client_destroy(clone->kcopyd_client);
  1587. cancel_delayed_work_sync(&clone->waker);
  1588. destroy_workqueue(clone->wq);
  1589. hash_table_exit(clone);
  1590. dm_clone_metadata_close(clone->cmd);
  1591. dm_put_device(ti, clone->source_dev);
  1592. dm_put_device(ti, clone->dest_dev);
  1593. dm_put_device(ti, clone->metadata_dev);
  1594. kfree(clone);
  1595. }
  1596. /*---------------------------------------------------------------------------*/
  1597. static void clone_postsuspend(struct dm_target *ti)
  1598. {
  1599. struct clone *clone = ti->private;
  1600. /*
  1601. * To successfully suspend the device:
  1602. *
  1603. * - We cancel the delayed work for periodic commits and wait for
  1604. * it to finish.
  1605. *
  1606. * - We stop the background hydration, i.e. we prevent new region
  1607. * hydrations from starting.
  1608. *
  1609. * - We wait for any in-flight hydrations to finish.
  1610. *
  1611. * - We flush the workqueue.
  1612. *
  1613. * - We commit the metadata.
  1614. */
  1615. cancel_delayed_work_sync(&clone->waker);
  1616. set_bit(DM_CLONE_HYDRATION_SUSPENDED, &clone->flags);
  1617. /*
  1618. * Make sure set_bit() is ordered before atomic_read(), otherwise we
  1619. * might race with do_hydration() and miss some started region
  1620. * hydrations.
  1621. *
  1622. * This is paired with smp_mb__after_atomic() in do_hydration().
  1623. */
  1624. smp_mb__after_atomic();
  1625. wait_event(clone->hydration_stopped, !atomic_read(&clone->hydrations_in_flight));
  1626. flush_workqueue(clone->wq);
  1627. (void) commit_metadata(clone, NULL);
  1628. }
  1629. static void clone_resume(struct dm_target *ti)
  1630. {
  1631. struct clone *clone = ti->private;
  1632. clear_bit(DM_CLONE_HYDRATION_SUSPENDED, &clone->flags);
  1633. do_waker(&clone->waker.work);
  1634. }
  1635. /*
  1636. * If discard_passdown was enabled verify that the destination device supports
  1637. * discards. Disable discard_passdown if not.
  1638. */
  1639. static void disable_passdown_if_not_supported(struct clone *clone)
  1640. {
  1641. struct block_device *dest_dev = clone->dest_dev->bdev;
  1642. struct queue_limits *dest_limits = &bdev_get_queue(dest_dev)->limits;
  1643. const char *reason = NULL;
  1644. if (!test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags))
  1645. return;
  1646. if (!bdev_max_discard_sectors(dest_dev))
  1647. reason = "discard unsupported";
  1648. else if (dest_limits->max_discard_sectors < clone->region_size)
  1649. reason = "max discard sectors smaller than a region";
  1650. if (reason) {
  1651. DMWARN("Destination device (%pg) %s: Disabling discard passdown.",
  1652. dest_dev, reason);
  1653. clear_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags);
  1654. }
  1655. }
  1656. static void set_discard_limits(struct clone *clone, struct queue_limits *limits)
  1657. {
  1658. struct block_device *dest_bdev = clone->dest_dev->bdev;
  1659. struct queue_limits *dest_limits = &bdev_get_queue(dest_bdev)->limits;
  1660. if (!test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags)) {
  1661. /* No passdown is done so we set our own virtual limits */
  1662. limits->discard_granularity = clone->region_size << SECTOR_SHIFT;
  1663. limits->max_discard_sectors = round_down(UINT_MAX >> SECTOR_SHIFT, clone->region_size);
  1664. return;
  1665. }
  1666. /*
  1667. * clone_iterate_devices() is stacking both the source and destination
  1668. * device limits but discards aren't passed to the source device, so
  1669. * inherit destination's limits.
  1670. */
  1671. limits->max_discard_sectors = dest_limits->max_discard_sectors;
  1672. limits->max_hw_discard_sectors = dest_limits->max_hw_discard_sectors;
  1673. limits->discard_granularity = dest_limits->discard_granularity;
  1674. limits->discard_alignment = dest_limits->discard_alignment;
  1675. limits->discard_misaligned = dest_limits->discard_misaligned;
  1676. limits->max_discard_segments = dest_limits->max_discard_segments;
  1677. }
  1678. static void clone_io_hints(struct dm_target *ti, struct queue_limits *limits)
  1679. {
  1680. struct clone *clone = ti->private;
  1681. u64 io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  1682. /*
  1683. * If the system-determined stacked limits are compatible with
  1684. * dm-clone's region size (io_opt is a factor) do not override them.
  1685. */
  1686. if (io_opt_sectors < clone->region_size ||
  1687. do_div(io_opt_sectors, clone->region_size)) {
  1688. blk_limits_io_min(limits, clone->region_size << SECTOR_SHIFT);
  1689. blk_limits_io_opt(limits, clone->region_size << SECTOR_SHIFT);
  1690. }
  1691. disable_passdown_if_not_supported(clone);
  1692. set_discard_limits(clone, limits);
  1693. }
  1694. static int clone_iterate_devices(struct dm_target *ti,
  1695. iterate_devices_callout_fn fn, void *data)
  1696. {
  1697. int ret;
  1698. struct clone *clone = ti->private;
  1699. struct dm_dev *dest_dev = clone->dest_dev;
  1700. struct dm_dev *source_dev = clone->source_dev;
  1701. ret = fn(ti, source_dev, 0, ti->len, data);
  1702. if (!ret)
  1703. ret = fn(ti, dest_dev, 0, ti->len, data);
  1704. return ret;
  1705. }
  1706. /*
  1707. * dm-clone message functions.
  1708. */
  1709. static void set_hydration_threshold(struct clone *clone, unsigned int nr_regions)
  1710. {
  1711. WRITE_ONCE(clone->hydration_threshold, nr_regions);
  1712. /*
  1713. * If user space sets hydration_threshold to zero then the hydration
  1714. * will stop. If at a later time the hydration_threshold is increased
  1715. * we must restart the hydration process by waking up the worker.
  1716. */
  1717. wake_worker(clone);
  1718. }
  1719. static void set_hydration_batch_size(struct clone *clone, unsigned int nr_regions)
  1720. {
  1721. WRITE_ONCE(clone->hydration_batch_size, nr_regions);
  1722. }
  1723. static void enable_hydration(struct clone *clone)
  1724. {
  1725. if (!test_and_set_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags))
  1726. wake_worker(clone);
  1727. }
  1728. static void disable_hydration(struct clone *clone)
  1729. {
  1730. clear_bit(DM_CLONE_HYDRATION_ENABLED, &clone->flags);
  1731. }
  1732. static int clone_message(struct dm_target *ti, unsigned int argc, char **argv,
  1733. char *result, unsigned int maxlen)
  1734. {
  1735. struct clone *clone = ti->private;
  1736. unsigned int value;
  1737. if (!argc)
  1738. return -EINVAL;
  1739. if (!strcasecmp(argv[0], "enable_hydration")) {
  1740. enable_hydration(clone);
  1741. return 0;
  1742. }
  1743. if (!strcasecmp(argv[0], "disable_hydration")) {
  1744. disable_hydration(clone);
  1745. return 0;
  1746. }
  1747. if (argc != 2)
  1748. return -EINVAL;
  1749. if (!strcasecmp(argv[0], "hydration_threshold")) {
  1750. if (kstrtouint(argv[1], 10, &value))
  1751. return -EINVAL;
  1752. set_hydration_threshold(clone, value);
  1753. return 0;
  1754. }
  1755. if (!strcasecmp(argv[0], "hydration_batch_size")) {
  1756. if (kstrtouint(argv[1], 10, &value))
  1757. return -EINVAL;
  1758. set_hydration_batch_size(clone, value);
  1759. return 0;
  1760. }
  1761. DMERR("%s: Unsupported message `%s'", clone_device_name(clone), argv[0]);
  1762. return -EINVAL;
  1763. }
  1764. static struct target_type clone_target = {
  1765. .name = "clone",
  1766. .version = {1, 0, 0},
  1767. .module = THIS_MODULE,
  1768. .ctr = clone_ctr,
  1769. .dtr = clone_dtr,
  1770. .map = clone_map,
  1771. .end_io = clone_endio,
  1772. .postsuspend = clone_postsuspend,
  1773. .resume = clone_resume,
  1774. .status = clone_status,
  1775. .message = clone_message,
  1776. .io_hints = clone_io_hints,
  1777. .iterate_devices = clone_iterate_devices,
  1778. };
  1779. /*---------------------------------------------------------------------------*/
  1780. /* Module functions */
  1781. static int __init dm_clone_init(void)
  1782. {
  1783. int r;
  1784. _hydration_cache = KMEM_CACHE(dm_clone_region_hydration, 0);
  1785. if (!_hydration_cache)
  1786. return -ENOMEM;
  1787. r = dm_register_target(&clone_target);
  1788. if (r < 0) {
  1789. DMERR("Failed to register clone target");
  1790. kmem_cache_destroy(_hydration_cache);
  1791. return r;
  1792. }
  1793. return 0;
  1794. }
  1795. static void __exit dm_clone_exit(void)
  1796. {
  1797. dm_unregister_target(&clone_target);
  1798. kmem_cache_destroy(_hydration_cache);
  1799. _hydration_cache = NULL;
  1800. }
  1801. /* Module hooks */
  1802. module_init(dm_clone_init);
  1803. module_exit(dm_clone_exit);
  1804. MODULE_DESCRIPTION(DM_NAME " clone target");
  1805. MODULE_AUTHOR("Nikos Tsironis <[email protected]>");
  1806. MODULE_LICENSE("GPL");