dm-clone-target.c 56 KB

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