dm-snap.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  1. /*
  2. * dm-snapshot.c
  3. *
  4. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include <linux/blkdev.h>
  9. #include <linux/device-mapper.h>
  10. #include <linux/delay.h>
  11. #include <linux/fs.h>
  12. #include <linux/init.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/list.h>
  15. #include <linux/mempool.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/log2.h>
  20. #include <linux/dm-kcopyd.h>
  21. #include "dm.h"
  22. #include "dm-exception-store.h"
  23. #define DM_MSG_PREFIX "snapshots"
  24. static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
  25. #define dm_target_is_snapshot_merge(ti) \
  26. ((ti)->type->name == dm_snapshot_merge_target_name)
  27. /*
  28. * The size of the mempool used to track chunks in use.
  29. */
  30. #define MIN_IOS 256
  31. #define DM_TRACKED_CHUNK_HASH_SIZE 16
  32. #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
  33. (DM_TRACKED_CHUNK_HASH_SIZE - 1))
  34. struct dm_exception_table {
  35. uint32_t hash_mask;
  36. unsigned hash_shift;
  37. struct list_head *table;
  38. };
  39. struct dm_snapshot {
  40. struct mutex lock;
  41. struct dm_dev *origin;
  42. struct dm_dev *cow;
  43. struct dm_target *ti;
  44. /* List of snapshots per Origin */
  45. struct list_head list;
  46. /*
  47. * You can't use a snapshot if this is 0 (e.g. if full).
  48. * A snapshot-merge target never clears this.
  49. */
  50. int valid;
  51. /*
  52. * The snapshot overflowed because of a write to the snapshot device.
  53. * We don't have to invalidate the snapshot in this case, but we need
  54. * to prevent further writes.
  55. */
  56. int snapshot_overflowed;
  57. /* Origin writes don't trigger exceptions until this is set */
  58. int active;
  59. atomic_t pending_exceptions_count;
  60. /* Protected by "lock" */
  61. sector_t exception_start_sequence;
  62. /* Protected by kcopyd single-threaded callback */
  63. sector_t exception_complete_sequence;
  64. /*
  65. * A list of pending exceptions that completed out of order.
  66. * Protected by kcopyd single-threaded callback.
  67. */
  68. struct rb_root out_of_order_tree;
  69. mempool_t pending_pool;
  70. struct dm_exception_table pending;
  71. struct dm_exception_table complete;
  72. /*
  73. * pe_lock protects all pending_exception operations and access
  74. * as well as the snapshot_bios list.
  75. */
  76. spinlock_t pe_lock;
  77. /* Chunks with outstanding reads */
  78. spinlock_t tracked_chunk_lock;
  79. struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
  80. /* The on disk metadata handler */
  81. struct dm_exception_store *store;
  82. unsigned in_progress;
  83. struct wait_queue_head in_progress_wait;
  84. struct dm_kcopyd_client *kcopyd_client;
  85. /* Wait for events based on state_bits */
  86. unsigned long state_bits;
  87. /* Range of chunks currently being merged. */
  88. chunk_t first_merging_chunk;
  89. int num_merging_chunks;
  90. /*
  91. * The merge operation failed if this flag is set.
  92. * Failure modes are handled as follows:
  93. * - I/O error reading the header
  94. * => don't load the target; abort.
  95. * - Header does not have "valid" flag set
  96. * => use the origin; forget about the snapshot.
  97. * - I/O error when reading exceptions
  98. * => don't load the target; abort.
  99. * (We can't use the intermediate origin state.)
  100. * - I/O error while merging
  101. * => stop merging; set merge_failed; process I/O normally.
  102. */
  103. int merge_failed;
  104. /*
  105. * Incoming bios that overlap with chunks being merged must wait
  106. * for them to be committed.
  107. */
  108. struct bio_list bios_queued_during_merge;
  109. };
  110. /*
  111. * state_bits:
  112. * RUNNING_MERGE - Merge operation is in progress.
  113. * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
  114. * cleared afterwards.
  115. */
  116. #define RUNNING_MERGE 0
  117. #define SHUTDOWN_MERGE 1
  118. /*
  119. * Maximum number of chunks being copied on write.
  120. *
  121. * The value was decided experimentally as a trade-off between memory
  122. * consumption, stalling the kernel's workqueues and maintaining a high enough
  123. * throughput.
  124. */
  125. #define DEFAULT_COW_THRESHOLD 2048
  126. static unsigned cow_threshold = DEFAULT_COW_THRESHOLD;
  127. module_param_named(snapshot_cow_threshold, cow_threshold, uint, 0644);
  128. MODULE_PARM_DESC(snapshot_cow_threshold, "Maximum number of chunks being copied on write");
  129. DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
  130. "A percentage of time allocated for copy on write");
  131. struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
  132. {
  133. return s->origin;
  134. }
  135. EXPORT_SYMBOL(dm_snap_origin);
  136. struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
  137. {
  138. return s->cow;
  139. }
  140. EXPORT_SYMBOL(dm_snap_cow);
  141. static sector_t chunk_to_sector(struct dm_exception_store *store,
  142. chunk_t chunk)
  143. {
  144. return chunk << store->chunk_shift;
  145. }
  146. static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
  147. {
  148. /*
  149. * There is only ever one instance of a particular block
  150. * device so we can compare pointers safely.
  151. */
  152. return lhs == rhs;
  153. }
  154. struct dm_snap_pending_exception {
  155. struct dm_exception e;
  156. /*
  157. * Origin buffers waiting for this to complete are held
  158. * in a bio list
  159. */
  160. struct bio_list origin_bios;
  161. struct bio_list snapshot_bios;
  162. /* Pointer back to snapshot context */
  163. struct dm_snapshot *snap;
  164. /*
  165. * 1 indicates the exception has already been sent to
  166. * kcopyd.
  167. */
  168. int started;
  169. /* There was copying error. */
  170. int copy_error;
  171. /* A sequence number, it is used for in-order completion. */
  172. sector_t exception_sequence;
  173. struct rb_node out_of_order_node;
  174. /*
  175. * For writing a complete chunk, bypassing the copy.
  176. */
  177. struct bio *full_bio;
  178. bio_end_io_t *full_bio_end_io;
  179. };
  180. /*
  181. * Hash table mapping origin volumes to lists of snapshots and
  182. * a lock to protect it
  183. */
  184. static struct kmem_cache *exception_cache;
  185. static struct kmem_cache *pending_cache;
  186. struct dm_snap_tracked_chunk {
  187. struct hlist_node node;
  188. chunk_t chunk;
  189. };
  190. static void init_tracked_chunk(struct bio *bio)
  191. {
  192. struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
  193. INIT_HLIST_NODE(&c->node);
  194. }
  195. static bool is_bio_tracked(struct bio *bio)
  196. {
  197. struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
  198. return !hlist_unhashed(&c->node);
  199. }
  200. static void track_chunk(struct dm_snapshot *s, struct bio *bio, chunk_t chunk)
  201. {
  202. struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
  203. c->chunk = chunk;
  204. spin_lock_irq(&s->tracked_chunk_lock);
  205. hlist_add_head(&c->node,
  206. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
  207. spin_unlock_irq(&s->tracked_chunk_lock);
  208. }
  209. static void stop_tracking_chunk(struct dm_snapshot *s, struct bio *bio)
  210. {
  211. struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
  212. unsigned long flags;
  213. spin_lock_irqsave(&s->tracked_chunk_lock, flags);
  214. hlist_del(&c->node);
  215. spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
  216. }
  217. static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
  218. {
  219. struct dm_snap_tracked_chunk *c;
  220. int found = 0;
  221. spin_lock_irq(&s->tracked_chunk_lock);
  222. hlist_for_each_entry(c,
  223. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
  224. if (c->chunk == chunk) {
  225. found = 1;
  226. break;
  227. }
  228. }
  229. spin_unlock_irq(&s->tracked_chunk_lock);
  230. return found;
  231. }
  232. /*
  233. * This conflicting I/O is extremely improbable in the caller,
  234. * so msleep(1) is sufficient and there is no need for a wait queue.
  235. */
  236. static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
  237. {
  238. while (__chunk_is_tracked(s, chunk))
  239. msleep(1);
  240. }
  241. /*
  242. * One of these per registered origin, held in the snapshot_origins hash
  243. */
  244. struct origin {
  245. /* The origin device */
  246. struct block_device *bdev;
  247. struct list_head hash_list;
  248. /* List of snapshots for this origin */
  249. struct list_head snapshots;
  250. };
  251. /*
  252. * This structure is allocated for each origin target
  253. */
  254. struct dm_origin {
  255. struct dm_dev *dev;
  256. struct dm_target *ti;
  257. unsigned split_boundary;
  258. struct list_head hash_list;
  259. };
  260. /*
  261. * Size of the hash table for origin volumes. If we make this
  262. * the size of the minors list then it should be nearly perfect
  263. */
  264. #define ORIGIN_HASH_SIZE 256
  265. #define ORIGIN_MASK 0xFF
  266. static struct list_head *_origins;
  267. static struct list_head *_dm_origins;
  268. static struct rw_semaphore _origins_lock;
  269. static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
  270. static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
  271. static uint64_t _pending_exceptions_done_count;
  272. static int init_origin_hash(void)
  273. {
  274. int i;
  275. _origins = kmalloc_array(ORIGIN_HASH_SIZE, sizeof(struct list_head),
  276. GFP_KERNEL);
  277. if (!_origins) {
  278. DMERR("unable to allocate memory for _origins");
  279. return -ENOMEM;
  280. }
  281. for (i = 0; i < ORIGIN_HASH_SIZE; i++)
  282. INIT_LIST_HEAD(_origins + i);
  283. _dm_origins = kmalloc_array(ORIGIN_HASH_SIZE,
  284. sizeof(struct list_head),
  285. GFP_KERNEL);
  286. if (!_dm_origins) {
  287. DMERR("unable to allocate memory for _dm_origins");
  288. kfree(_origins);
  289. return -ENOMEM;
  290. }
  291. for (i = 0; i < ORIGIN_HASH_SIZE; i++)
  292. INIT_LIST_HEAD(_dm_origins + i);
  293. init_rwsem(&_origins_lock);
  294. return 0;
  295. }
  296. static void exit_origin_hash(void)
  297. {
  298. kfree(_origins);
  299. kfree(_dm_origins);
  300. }
  301. static unsigned origin_hash(struct block_device *bdev)
  302. {
  303. return bdev->bd_dev & ORIGIN_MASK;
  304. }
  305. static struct origin *__lookup_origin(struct block_device *origin)
  306. {
  307. struct list_head *ol;
  308. struct origin *o;
  309. ol = &_origins[origin_hash(origin)];
  310. list_for_each_entry (o, ol, hash_list)
  311. if (bdev_equal(o->bdev, origin))
  312. return o;
  313. return NULL;
  314. }
  315. static void __insert_origin(struct origin *o)
  316. {
  317. struct list_head *sl = &_origins[origin_hash(o->bdev)];
  318. list_add_tail(&o->hash_list, sl);
  319. }
  320. static struct dm_origin *__lookup_dm_origin(struct block_device *origin)
  321. {
  322. struct list_head *ol;
  323. struct dm_origin *o;
  324. ol = &_dm_origins[origin_hash(origin)];
  325. list_for_each_entry (o, ol, hash_list)
  326. if (bdev_equal(o->dev->bdev, origin))
  327. return o;
  328. return NULL;
  329. }
  330. static void __insert_dm_origin(struct dm_origin *o)
  331. {
  332. struct list_head *sl = &_dm_origins[origin_hash(o->dev->bdev)];
  333. list_add_tail(&o->hash_list, sl);
  334. }
  335. static void __remove_dm_origin(struct dm_origin *o)
  336. {
  337. list_del(&o->hash_list);
  338. }
  339. /*
  340. * _origins_lock must be held when calling this function.
  341. * Returns number of snapshots registered using the supplied cow device, plus:
  342. * snap_src - a snapshot suitable for use as a source of exception handover
  343. * snap_dest - a snapshot capable of receiving exception handover.
  344. * snap_merge - an existing snapshot-merge target linked to the same origin.
  345. * There can be at most one snapshot-merge target. The parameter is optional.
  346. *
  347. * Possible return values and states of snap_src and snap_dest.
  348. * 0: NULL, NULL - first new snapshot
  349. * 1: snap_src, NULL - normal snapshot
  350. * 2: snap_src, snap_dest - waiting for handover
  351. * 2: snap_src, NULL - handed over, waiting for old to be deleted
  352. * 1: NULL, snap_dest - source got destroyed without handover
  353. */
  354. static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
  355. struct dm_snapshot **snap_src,
  356. struct dm_snapshot **snap_dest,
  357. struct dm_snapshot **snap_merge)
  358. {
  359. struct dm_snapshot *s;
  360. struct origin *o;
  361. int count = 0;
  362. int active;
  363. o = __lookup_origin(snap->origin->bdev);
  364. if (!o)
  365. goto out;
  366. list_for_each_entry(s, &o->snapshots, list) {
  367. if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
  368. *snap_merge = s;
  369. if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
  370. continue;
  371. mutex_lock(&s->lock);
  372. active = s->active;
  373. mutex_unlock(&s->lock);
  374. if (active) {
  375. if (snap_src)
  376. *snap_src = s;
  377. } else if (snap_dest)
  378. *snap_dest = s;
  379. count++;
  380. }
  381. out:
  382. return count;
  383. }
  384. /*
  385. * On success, returns 1 if this snapshot is a handover destination,
  386. * otherwise returns 0.
  387. */
  388. static int __validate_exception_handover(struct dm_snapshot *snap)
  389. {
  390. struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
  391. struct dm_snapshot *snap_merge = NULL;
  392. /* Does snapshot need exceptions handed over to it? */
  393. if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
  394. &snap_merge) == 2) ||
  395. snap_dest) {
  396. snap->ti->error = "Snapshot cow pairing for exception "
  397. "table handover failed";
  398. return -EINVAL;
  399. }
  400. /*
  401. * If no snap_src was found, snap cannot become a handover
  402. * destination.
  403. */
  404. if (!snap_src)
  405. return 0;
  406. /*
  407. * Non-snapshot-merge handover?
  408. */
  409. if (!dm_target_is_snapshot_merge(snap->ti))
  410. return 1;
  411. /*
  412. * Do not allow more than one merging snapshot.
  413. */
  414. if (snap_merge) {
  415. snap->ti->error = "A snapshot is already merging.";
  416. return -EINVAL;
  417. }
  418. if (!snap_src->store->type->prepare_merge ||
  419. !snap_src->store->type->commit_merge) {
  420. snap->ti->error = "Snapshot exception store does not "
  421. "support snapshot-merge.";
  422. return -EINVAL;
  423. }
  424. return 1;
  425. }
  426. static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
  427. {
  428. struct dm_snapshot *l;
  429. /* Sort the list according to chunk size, largest-first smallest-last */
  430. list_for_each_entry(l, &o->snapshots, list)
  431. if (l->store->chunk_size < s->store->chunk_size)
  432. break;
  433. list_add_tail(&s->list, &l->list);
  434. }
  435. /*
  436. * Make a note of the snapshot and its origin so we can look it
  437. * up when the origin has a write on it.
  438. *
  439. * Also validate snapshot exception store handovers.
  440. * On success, returns 1 if this registration is a handover destination,
  441. * otherwise returns 0.
  442. */
  443. static int register_snapshot(struct dm_snapshot *snap)
  444. {
  445. struct origin *o, *new_o = NULL;
  446. struct block_device *bdev = snap->origin->bdev;
  447. int r = 0;
  448. new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
  449. if (!new_o)
  450. return -ENOMEM;
  451. down_write(&_origins_lock);
  452. r = __validate_exception_handover(snap);
  453. if (r < 0) {
  454. kfree(new_o);
  455. goto out;
  456. }
  457. o = __lookup_origin(bdev);
  458. if (o)
  459. kfree(new_o);
  460. else {
  461. /* New origin */
  462. o = new_o;
  463. /* Initialise the struct */
  464. INIT_LIST_HEAD(&o->snapshots);
  465. o->bdev = bdev;
  466. __insert_origin(o);
  467. }
  468. __insert_snapshot(o, snap);
  469. out:
  470. up_write(&_origins_lock);
  471. return r;
  472. }
  473. /*
  474. * Move snapshot to correct place in list according to chunk size.
  475. */
  476. static void reregister_snapshot(struct dm_snapshot *s)
  477. {
  478. struct block_device *bdev = s->origin->bdev;
  479. down_write(&_origins_lock);
  480. list_del(&s->list);
  481. __insert_snapshot(__lookup_origin(bdev), s);
  482. up_write(&_origins_lock);
  483. }
  484. static void unregister_snapshot(struct dm_snapshot *s)
  485. {
  486. struct origin *o;
  487. down_write(&_origins_lock);
  488. o = __lookup_origin(s->origin->bdev);
  489. list_del(&s->list);
  490. if (o && list_empty(&o->snapshots)) {
  491. list_del(&o->hash_list);
  492. kfree(o);
  493. }
  494. up_write(&_origins_lock);
  495. }
  496. /*
  497. * Implementation of the exception hash tables.
  498. * The lowest hash_shift bits of the chunk number are ignored, allowing
  499. * some consecutive chunks to be grouped together.
  500. */
  501. static int dm_exception_table_init(struct dm_exception_table *et,
  502. uint32_t size, unsigned hash_shift)
  503. {
  504. unsigned int i;
  505. et->hash_shift = hash_shift;
  506. et->hash_mask = size - 1;
  507. et->table = dm_vcalloc(size, sizeof(struct list_head));
  508. if (!et->table)
  509. return -ENOMEM;
  510. for (i = 0; i < size; i++)
  511. INIT_LIST_HEAD(et->table + i);
  512. return 0;
  513. }
  514. static void dm_exception_table_exit(struct dm_exception_table *et,
  515. struct kmem_cache *mem)
  516. {
  517. struct list_head *slot;
  518. struct dm_exception *ex, *next;
  519. int i, size;
  520. size = et->hash_mask + 1;
  521. for (i = 0; i < size; i++) {
  522. slot = et->table + i;
  523. list_for_each_entry_safe (ex, next, slot, hash_list)
  524. kmem_cache_free(mem, ex);
  525. }
  526. vfree(et->table);
  527. }
  528. static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
  529. {
  530. return (chunk >> et->hash_shift) & et->hash_mask;
  531. }
  532. static void dm_remove_exception(struct dm_exception *e)
  533. {
  534. list_del(&e->hash_list);
  535. }
  536. /*
  537. * Return the exception data for a sector, or NULL if not
  538. * remapped.
  539. */
  540. static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
  541. chunk_t chunk)
  542. {
  543. struct list_head *slot;
  544. struct dm_exception *e;
  545. slot = &et->table[exception_hash(et, chunk)];
  546. list_for_each_entry (e, slot, hash_list)
  547. if (chunk >= e->old_chunk &&
  548. chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
  549. return e;
  550. return NULL;
  551. }
  552. static struct dm_exception *alloc_completed_exception(gfp_t gfp)
  553. {
  554. struct dm_exception *e;
  555. e = kmem_cache_alloc(exception_cache, gfp);
  556. if (!e && gfp == GFP_NOIO)
  557. e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
  558. return e;
  559. }
  560. static void free_completed_exception(struct dm_exception *e)
  561. {
  562. kmem_cache_free(exception_cache, e);
  563. }
  564. static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
  565. {
  566. struct dm_snap_pending_exception *pe = mempool_alloc(&s->pending_pool,
  567. GFP_NOIO);
  568. atomic_inc(&s->pending_exceptions_count);
  569. pe->snap = s;
  570. return pe;
  571. }
  572. static void free_pending_exception(struct dm_snap_pending_exception *pe)
  573. {
  574. struct dm_snapshot *s = pe->snap;
  575. mempool_free(pe, &s->pending_pool);
  576. smp_mb__before_atomic();
  577. atomic_dec(&s->pending_exceptions_count);
  578. }
  579. static void dm_insert_exception(struct dm_exception_table *eh,
  580. struct dm_exception *new_e)
  581. {
  582. struct list_head *l;
  583. struct dm_exception *e = NULL;
  584. l = &eh->table[exception_hash(eh, new_e->old_chunk)];
  585. /* Add immediately if this table doesn't support consecutive chunks */
  586. if (!eh->hash_shift)
  587. goto out;
  588. /* List is ordered by old_chunk */
  589. list_for_each_entry_reverse(e, l, hash_list) {
  590. /* Insert after an existing chunk? */
  591. if (new_e->old_chunk == (e->old_chunk +
  592. dm_consecutive_chunk_count(e) + 1) &&
  593. new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
  594. dm_consecutive_chunk_count(e) + 1)) {
  595. dm_consecutive_chunk_count_inc(e);
  596. free_completed_exception(new_e);
  597. return;
  598. }
  599. /* Insert before an existing chunk? */
  600. if (new_e->old_chunk == (e->old_chunk - 1) &&
  601. new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
  602. dm_consecutive_chunk_count_inc(e);
  603. e->old_chunk--;
  604. e->new_chunk--;
  605. free_completed_exception(new_e);
  606. return;
  607. }
  608. if (new_e->old_chunk > e->old_chunk)
  609. break;
  610. }
  611. out:
  612. list_add(&new_e->hash_list, e ? &e->hash_list : l);
  613. }
  614. /*
  615. * Callback used by the exception stores to load exceptions when
  616. * initialising.
  617. */
  618. static int dm_add_exception(void *context, chunk_t old, chunk_t new)
  619. {
  620. struct dm_snapshot *s = context;
  621. struct dm_exception *e;
  622. e = alloc_completed_exception(GFP_KERNEL);
  623. if (!e)
  624. return -ENOMEM;
  625. e->old_chunk = old;
  626. /* Consecutive_count is implicitly initialised to zero */
  627. e->new_chunk = new;
  628. dm_insert_exception(&s->complete, e);
  629. return 0;
  630. }
  631. /*
  632. * Return a minimum chunk size of all snapshots that have the specified origin.
  633. * Return zero if the origin has no snapshots.
  634. */
  635. static uint32_t __minimum_chunk_size(struct origin *o)
  636. {
  637. struct dm_snapshot *snap;
  638. unsigned chunk_size = 0;
  639. if (o)
  640. list_for_each_entry(snap, &o->snapshots, list)
  641. chunk_size = min_not_zero(chunk_size,
  642. snap->store->chunk_size);
  643. return (uint32_t) chunk_size;
  644. }
  645. /*
  646. * Hard coded magic.
  647. */
  648. static int calc_max_buckets(void)
  649. {
  650. /* use a fixed size of 2MB */
  651. unsigned long mem = 2 * 1024 * 1024;
  652. mem /= sizeof(struct list_head);
  653. return mem;
  654. }
  655. /*
  656. * Allocate room for a suitable hash table.
  657. */
  658. static int init_hash_tables(struct dm_snapshot *s)
  659. {
  660. sector_t hash_size, cow_dev_size, max_buckets;
  661. /*
  662. * Calculate based on the size of the original volume or
  663. * the COW volume...
  664. */
  665. cow_dev_size = get_dev_size(s->cow->bdev);
  666. max_buckets = calc_max_buckets();
  667. hash_size = cow_dev_size >> s->store->chunk_shift;
  668. hash_size = min(hash_size, max_buckets);
  669. if (hash_size < 64)
  670. hash_size = 64;
  671. hash_size = rounddown_pow_of_two(hash_size);
  672. if (dm_exception_table_init(&s->complete, hash_size,
  673. DM_CHUNK_CONSECUTIVE_BITS))
  674. return -ENOMEM;
  675. /*
  676. * Allocate hash table for in-flight exceptions
  677. * Make this smaller than the real hash table
  678. */
  679. hash_size >>= 3;
  680. if (hash_size < 64)
  681. hash_size = 64;
  682. if (dm_exception_table_init(&s->pending, hash_size, 0)) {
  683. dm_exception_table_exit(&s->complete, exception_cache);
  684. return -ENOMEM;
  685. }
  686. return 0;
  687. }
  688. static void merge_shutdown(struct dm_snapshot *s)
  689. {
  690. clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
  691. smp_mb__after_atomic();
  692. wake_up_bit(&s->state_bits, RUNNING_MERGE);
  693. }
  694. static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
  695. {
  696. s->first_merging_chunk = 0;
  697. s->num_merging_chunks = 0;
  698. return bio_list_get(&s->bios_queued_during_merge);
  699. }
  700. /*
  701. * Remove one chunk from the index of completed exceptions.
  702. */
  703. static int __remove_single_exception_chunk(struct dm_snapshot *s,
  704. chunk_t old_chunk)
  705. {
  706. struct dm_exception *e;
  707. e = dm_lookup_exception(&s->complete, old_chunk);
  708. if (!e) {
  709. DMERR("Corruption detected: exception for block %llu is "
  710. "on disk but not in memory",
  711. (unsigned long long)old_chunk);
  712. return -EINVAL;
  713. }
  714. /*
  715. * If this is the only chunk using this exception, remove exception.
  716. */
  717. if (!dm_consecutive_chunk_count(e)) {
  718. dm_remove_exception(e);
  719. free_completed_exception(e);
  720. return 0;
  721. }
  722. /*
  723. * The chunk may be either at the beginning or the end of a
  724. * group of consecutive chunks - never in the middle. We are
  725. * removing chunks in the opposite order to that in which they
  726. * were added, so this should always be true.
  727. * Decrement the consecutive chunk counter and adjust the
  728. * starting point if necessary.
  729. */
  730. if (old_chunk == e->old_chunk) {
  731. e->old_chunk++;
  732. e->new_chunk++;
  733. } else if (old_chunk != e->old_chunk +
  734. dm_consecutive_chunk_count(e)) {
  735. DMERR("Attempt to merge block %llu from the "
  736. "middle of a chunk range [%llu - %llu]",
  737. (unsigned long long)old_chunk,
  738. (unsigned long long)e->old_chunk,
  739. (unsigned long long)
  740. e->old_chunk + dm_consecutive_chunk_count(e));
  741. return -EINVAL;
  742. }
  743. dm_consecutive_chunk_count_dec(e);
  744. return 0;
  745. }
  746. static void flush_bios(struct bio *bio);
  747. static int remove_single_exception_chunk(struct dm_snapshot *s)
  748. {
  749. struct bio *b = NULL;
  750. int r;
  751. chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
  752. mutex_lock(&s->lock);
  753. /*
  754. * Process chunks (and associated exceptions) in reverse order
  755. * so that dm_consecutive_chunk_count_dec() accounting works.
  756. */
  757. do {
  758. r = __remove_single_exception_chunk(s, old_chunk);
  759. if (r)
  760. goto out;
  761. } while (old_chunk-- > s->first_merging_chunk);
  762. b = __release_queued_bios_after_merge(s);
  763. out:
  764. mutex_unlock(&s->lock);
  765. if (b)
  766. flush_bios(b);
  767. return r;
  768. }
  769. static int origin_write_extent(struct dm_snapshot *merging_snap,
  770. sector_t sector, unsigned chunk_size);
  771. static void merge_callback(int read_err, unsigned long write_err,
  772. void *context);
  773. static uint64_t read_pending_exceptions_done_count(void)
  774. {
  775. uint64_t pending_exceptions_done;
  776. spin_lock(&_pending_exceptions_done_spinlock);
  777. pending_exceptions_done = _pending_exceptions_done_count;
  778. spin_unlock(&_pending_exceptions_done_spinlock);
  779. return pending_exceptions_done;
  780. }
  781. static void increment_pending_exceptions_done_count(void)
  782. {
  783. spin_lock(&_pending_exceptions_done_spinlock);
  784. _pending_exceptions_done_count++;
  785. spin_unlock(&_pending_exceptions_done_spinlock);
  786. wake_up_all(&_pending_exceptions_done);
  787. }
  788. static void snapshot_merge_next_chunks(struct dm_snapshot *s)
  789. {
  790. int i, linear_chunks;
  791. chunk_t old_chunk, new_chunk;
  792. struct dm_io_region src, dest;
  793. sector_t io_size;
  794. uint64_t previous_count;
  795. BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
  796. if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
  797. goto shut;
  798. /*
  799. * valid flag never changes during merge, so no lock required.
  800. */
  801. if (!s->valid) {
  802. DMERR("Snapshot is invalid: can't merge");
  803. goto shut;
  804. }
  805. linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
  806. &new_chunk);
  807. if (linear_chunks <= 0) {
  808. if (linear_chunks < 0) {
  809. DMERR("Read error in exception store: "
  810. "shutting down merge");
  811. mutex_lock(&s->lock);
  812. s->merge_failed = 1;
  813. mutex_unlock(&s->lock);
  814. }
  815. goto shut;
  816. }
  817. /* Adjust old_chunk and new_chunk to reflect start of linear region */
  818. old_chunk = old_chunk + 1 - linear_chunks;
  819. new_chunk = new_chunk + 1 - linear_chunks;
  820. /*
  821. * Use one (potentially large) I/O to copy all 'linear_chunks'
  822. * from the exception store to the origin
  823. */
  824. io_size = linear_chunks * s->store->chunk_size;
  825. dest.bdev = s->origin->bdev;
  826. dest.sector = chunk_to_sector(s->store, old_chunk);
  827. dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
  828. src.bdev = s->cow->bdev;
  829. src.sector = chunk_to_sector(s->store, new_chunk);
  830. src.count = dest.count;
  831. /*
  832. * Reallocate any exceptions needed in other snapshots then
  833. * wait for the pending exceptions to complete.
  834. * Each time any pending exception (globally on the system)
  835. * completes we are woken and repeat the process to find out
  836. * if we can proceed. While this may not seem a particularly
  837. * efficient algorithm, it is not expected to have any
  838. * significant impact on performance.
  839. */
  840. previous_count = read_pending_exceptions_done_count();
  841. while (origin_write_extent(s, dest.sector, io_size)) {
  842. wait_event(_pending_exceptions_done,
  843. (read_pending_exceptions_done_count() !=
  844. previous_count));
  845. /* Retry after the wait, until all exceptions are done. */
  846. previous_count = read_pending_exceptions_done_count();
  847. }
  848. mutex_lock(&s->lock);
  849. s->first_merging_chunk = old_chunk;
  850. s->num_merging_chunks = linear_chunks;
  851. mutex_unlock(&s->lock);
  852. /* Wait until writes to all 'linear_chunks' drain */
  853. for (i = 0; i < linear_chunks; i++)
  854. __check_for_conflicting_io(s, old_chunk + i);
  855. dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
  856. return;
  857. shut:
  858. merge_shutdown(s);
  859. }
  860. static void error_bios(struct bio *bio);
  861. static void merge_callback(int read_err, unsigned long write_err, void *context)
  862. {
  863. struct dm_snapshot *s = context;
  864. struct bio *b = NULL;
  865. if (read_err || write_err) {
  866. if (read_err)
  867. DMERR("Read error: shutting down merge.");
  868. else
  869. DMERR("Write error: shutting down merge.");
  870. goto shut;
  871. }
  872. if (s->store->type->commit_merge(s->store,
  873. s->num_merging_chunks) < 0) {
  874. DMERR("Write error in exception store: shutting down merge");
  875. goto shut;
  876. }
  877. if (remove_single_exception_chunk(s) < 0)
  878. goto shut;
  879. snapshot_merge_next_chunks(s);
  880. return;
  881. shut:
  882. mutex_lock(&s->lock);
  883. s->merge_failed = 1;
  884. b = __release_queued_bios_after_merge(s);
  885. mutex_unlock(&s->lock);
  886. error_bios(b);
  887. merge_shutdown(s);
  888. }
  889. static void start_merge(struct dm_snapshot *s)
  890. {
  891. if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
  892. snapshot_merge_next_chunks(s);
  893. }
  894. /*
  895. * Stop the merging process and wait until it finishes.
  896. */
  897. static void stop_merge(struct dm_snapshot *s)
  898. {
  899. set_bit(SHUTDOWN_MERGE, &s->state_bits);
  900. wait_on_bit(&s->state_bits, RUNNING_MERGE, TASK_UNINTERRUPTIBLE);
  901. clear_bit(SHUTDOWN_MERGE, &s->state_bits);
  902. }
  903. /*
  904. * Construct a snapshot mapping: <origin_dev> <COW-dev> <p|po|n> <chunk-size>
  905. */
  906. static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  907. {
  908. struct dm_snapshot *s;
  909. int i;
  910. int r = -EINVAL;
  911. char *origin_path, *cow_path;
  912. dev_t origin_dev, cow_dev;
  913. unsigned args_used, num_flush_bios = 1;
  914. fmode_t origin_mode = FMODE_READ;
  915. if (argc != 4) {
  916. ti->error = "requires exactly 4 arguments";
  917. r = -EINVAL;
  918. goto bad;
  919. }
  920. if (dm_target_is_snapshot_merge(ti)) {
  921. num_flush_bios = 2;
  922. origin_mode = FMODE_WRITE;
  923. }
  924. s = kzalloc(sizeof(*s), GFP_KERNEL);
  925. if (!s) {
  926. ti->error = "Cannot allocate private snapshot structure";
  927. r = -ENOMEM;
  928. goto bad;
  929. }
  930. origin_path = argv[0];
  931. argv++;
  932. argc--;
  933. r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
  934. if (r) {
  935. ti->error = "Cannot get origin device";
  936. goto bad_origin;
  937. }
  938. origin_dev = s->origin->bdev->bd_dev;
  939. cow_path = argv[0];
  940. argv++;
  941. argc--;
  942. cow_dev = dm_get_dev_t(cow_path);
  943. if (cow_dev && cow_dev == origin_dev) {
  944. ti->error = "COW device cannot be the same as origin device";
  945. r = -EINVAL;
  946. goto bad_cow;
  947. }
  948. r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
  949. if (r) {
  950. ti->error = "Cannot get COW device";
  951. goto bad_cow;
  952. }
  953. r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
  954. if (r) {
  955. ti->error = "Couldn't create exception store";
  956. r = -EINVAL;
  957. goto bad_store;
  958. }
  959. argv += args_used;
  960. argc -= args_used;
  961. s->ti = ti;
  962. s->valid = 1;
  963. s->snapshot_overflowed = 0;
  964. s->active = 0;
  965. atomic_set(&s->pending_exceptions_count, 0);
  966. s->exception_start_sequence = 0;
  967. s->exception_complete_sequence = 0;
  968. s->out_of_order_tree = RB_ROOT;
  969. mutex_init(&s->lock);
  970. INIT_LIST_HEAD(&s->list);
  971. spin_lock_init(&s->pe_lock);
  972. s->state_bits = 0;
  973. s->merge_failed = 0;
  974. s->first_merging_chunk = 0;
  975. s->num_merging_chunks = 0;
  976. bio_list_init(&s->bios_queued_during_merge);
  977. /* Allocate hash table for COW data */
  978. if (init_hash_tables(s)) {
  979. ti->error = "Unable to allocate hash table space";
  980. r = -ENOMEM;
  981. goto bad_hash_tables;
  982. }
  983. init_waitqueue_head(&s->in_progress_wait);
  984. s->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  985. if (IS_ERR(s->kcopyd_client)) {
  986. r = PTR_ERR(s->kcopyd_client);
  987. ti->error = "Could not create kcopyd client";
  988. goto bad_kcopyd;
  989. }
  990. r = mempool_init_slab_pool(&s->pending_pool, MIN_IOS, pending_cache);
  991. if (r) {
  992. ti->error = "Could not allocate mempool for pending exceptions";
  993. goto bad_pending_pool;
  994. }
  995. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  996. INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
  997. spin_lock_init(&s->tracked_chunk_lock);
  998. ti->private = s;
  999. ti->num_flush_bios = num_flush_bios;
  1000. ti->per_io_data_size = sizeof(struct dm_snap_tracked_chunk);
  1001. /* Add snapshot to the list of snapshots for this origin */
  1002. /* Exceptions aren't triggered till snapshot_resume() is called */
  1003. r = register_snapshot(s);
  1004. if (r == -ENOMEM) {
  1005. ti->error = "Snapshot origin struct allocation failed";
  1006. goto bad_load_and_register;
  1007. } else if (r < 0) {
  1008. /* invalid handover, register_snapshot has set ti->error */
  1009. goto bad_load_and_register;
  1010. }
  1011. /*
  1012. * Metadata must only be loaded into one table at once, so skip this
  1013. * if metadata will be handed over during resume.
  1014. * Chunk size will be set during the handover - set it to zero to
  1015. * ensure it's ignored.
  1016. */
  1017. if (r > 0) {
  1018. s->store->chunk_size = 0;
  1019. return 0;
  1020. }
  1021. r = s->store->type->read_metadata(s->store, dm_add_exception,
  1022. (void *)s);
  1023. if (r < 0) {
  1024. ti->error = "Failed to read snapshot metadata";
  1025. goto bad_read_metadata;
  1026. } else if (r > 0) {
  1027. s->valid = 0;
  1028. DMWARN("Snapshot is marked invalid.");
  1029. }
  1030. if (!s->store->chunk_size) {
  1031. ti->error = "Chunk size not set";
  1032. goto bad_read_metadata;
  1033. }
  1034. r = dm_set_target_max_io_len(ti, s->store->chunk_size);
  1035. if (r)
  1036. goto bad_read_metadata;
  1037. return 0;
  1038. bad_read_metadata:
  1039. unregister_snapshot(s);
  1040. bad_load_and_register:
  1041. mempool_exit(&s->pending_pool);
  1042. bad_pending_pool:
  1043. dm_kcopyd_client_destroy(s->kcopyd_client);
  1044. bad_kcopyd:
  1045. dm_exception_table_exit(&s->pending, pending_cache);
  1046. dm_exception_table_exit(&s->complete, exception_cache);
  1047. bad_hash_tables:
  1048. dm_exception_store_destroy(s->store);
  1049. bad_store:
  1050. dm_put_device(ti, s->cow);
  1051. bad_cow:
  1052. dm_put_device(ti, s->origin);
  1053. bad_origin:
  1054. kfree(s);
  1055. bad:
  1056. return r;
  1057. }
  1058. static void __free_exceptions(struct dm_snapshot *s)
  1059. {
  1060. dm_kcopyd_client_destroy(s->kcopyd_client);
  1061. s->kcopyd_client = NULL;
  1062. dm_exception_table_exit(&s->pending, pending_cache);
  1063. dm_exception_table_exit(&s->complete, exception_cache);
  1064. }
  1065. static void __handover_exceptions(struct dm_snapshot *snap_src,
  1066. struct dm_snapshot *snap_dest)
  1067. {
  1068. union {
  1069. struct dm_exception_table table_swap;
  1070. struct dm_exception_store *store_swap;
  1071. } u;
  1072. /*
  1073. * Swap all snapshot context information between the two instances.
  1074. */
  1075. u.table_swap = snap_dest->complete;
  1076. snap_dest->complete = snap_src->complete;
  1077. snap_src->complete = u.table_swap;
  1078. u.store_swap = snap_dest->store;
  1079. snap_dest->store = snap_src->store;
  1080. snap_dest->store->userspace_supports_overflow = u.store_swap->userspace_supports_overflow;
  1081. snap_src->store = u.store_swap;
  1082. snap_dest->store->snap = snap_dest;
  1083. snap_src->store->snap = snap_src;
  1084. snap_dest->ti->max_io_len = snap_dest->store->chunk_size;
  1085. snap_dest->valid = snap_src->valid;
  1086. snap_dest->snapshot_overflowed = snap_src->snapshot_overflowed;
  1087. /*
  1088. * Set source invalid to ensure it receives no further I/O.
  1089. */
  1090. snap_src->valid = 0;
  1091. }
  1092. static void snapshot_dtr(struct dm_target *ti)
  1093. {
  1094. #ifdef CONFIG_DM_DEBUG
  1095. int i;
  1096. #endif
  1097. struct dm_snapshot *s = ti->private;
  1098. struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
  1099. down_read(&_origins_lock);
  1100. /* Check whether exception handover must be cancelled */
  1101. (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
  1102. if (snap_src && snap_dest && (s == snap_src)) {
  1103. mutex_lock(&snap_dest->lock);
  1104. snap_dest->valid = 0;
  1105. mutex_unlock(&snap_dest->lock);
  1106. DMERR("Cancelling snapshot handover.");
  1107. }
  1108. up_read(&_origins_lock);
  1109. if (dm_target_is_snapshot_merge(ti))
  1110. stop_merge(s);
  1111. /* Prevent further origin writes from using this snapshot. */
  1112. /* After this returns there can be no new kcopyd jobs. */
  1113. unregister_snapshot(s);
  1114. while (atomic_read(&s->pending_exceptions_count))
  1115. msleep(1);
  1116. /*
  1117. * Ensure instructions in mempool_exit aren't reordered
  1118. * before atomic_read.
  1119. */
  1120. smp_mb();
  1121. #ifdef CONFIG_DM_DEBUG
  1122. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  1123. BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
  1124. #endif
  1125. __free_exceptions(s);
  1126. mempool_exit(&s->pending_pool);
  1127. dm_exception_store_destroy(s->store);
  1128. mutex_destroy(&s->lock);
  1129. dm_put_device(ti, s->cow);
  1130. dm_put_device(ti, s->origin);
  1131. WARN_ON(s->in_progress);
  1132. kfree(s);
  1133. }
  1134. static void account_start_copy(struct dm_snapshot *s)
  1135. {
  1136. spin_lock(&s->in_progress_wait.lock);
  1137. s->in_progress++;
  1138. spin_unlock(&s->in_progress_wait.lock);
  1139. }
  1140. static void account_end_copy(struct dm_snapshot *s)
  1141. {
  1142. spin_lock(&s->in_progress_wait.lock);
  1143. BUG_ON(!s->in_progress);
  1144. s->in_progress--;
  1145. if (likely(s->in_progress <= cow_threshold) &&
  1146. unlikely(waitqueue_active(&s->in_progress_wait)))
  1147. wake_up_locked(&s->in_progress_wait);
  1148. spin_unlock(&s->in_progress_wait.lock);
  1149. }
  1150. static bool wait_for_in_progress(struct dm_snapshot *s, bool unlock_origins)
  1151. {
  1152. if (unlikely(s->in_progress > cow_threshold)) {
  1153. spin_lock(&s->in_progress_wait.lock);
  1154. if (likely(s->in_progress > cow_threshold)) {
  1155. /*
  1156. * NOTE: this throttle doesn't account for whether
  1157. * the caller is servicing an IO that will trigger a COW
  1158. * so excess throttling may result for chunks not required
  1159. * to be COW'd. But if cow_threshold was reached, extra
  1160. * throttling is unlikely to negatively impact performance.
  1161. */
  1162. DECLARE_WAITQUEUE(wait, current);
  1163. __add_wait_queue(&s->in_progress_wait, &wait);
  1164. __set_current_state(TASK_UNINTERRUPTIBLE);
  1165. spin_unlock(&s->in_progress_wait.lock);
  1166. if (unlock_origins)
  1167. up_read(&_origins_lock);
  1168. io_schedule();
  1169. remove_wait_queue(&s->in_progress_wait, &wait);
  1170. return false;
  1171. }
  1172. spin_unlock(&s->in_progress_wait.lock);
  1173. }
  1174. return true;
  1175. }
  1176. /*
  1177. * Flush a list of buffers.
  1178. */
  1179. static void flush_bios(struct bio *bio)
  1180. {
  1181. struct bio *n;
  1182. while (bio) {
  1183. n = bio->bi_next;
  1184. bio->bi_next = NULL;
  1185. generic_make_request(bio);
  1186. bio = n;
  1187. }
  1188. }
  1189. static int do_origin(struct dm_dev *origin, struct bio *bio, bool limit);
  1190. /*
  1191. * Flush a list of buffers.
  1192. */
  1193. static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
  1194. {
  1195. struct bio *n;
  1196. int r;
  1197. while (bio) {
  1198. n = bio->bi_next;
  1199. bio->bi_next = NULL;
  1200. r = do_origin(s->origin, bio, false);
  1201. if (r == DM_MAPIO_REMAPPED)
  1202. generic_make_request(bio);
  1203. bio = n;
  1204. }
  1205. }
  1206. /*
  1207. * Error a list of buffers.
  1208. */
  1209. static void error_bios(struct bio *bio)
  1210. {
  1211. struct bio *n;
  1212. while (bio) {
  1213. n = bio->bi_next;
  1214. bio->bi_next = NULL;
  1215. bio_io_error(bio);
  1216. bio = n;
  1217. }
  1218. }
  1219. static void __invalidate_snapshot(struct dm_snapshot *s, int err)
  1220. {
  1221. if (!s->valid)
  1222. return;
  1223. if (err == -EIO)
  1224. DMERR("Invalidating snapshot: Error reading/writing.");
  1225. else if (err == -ENOMEM)
  1226. DMERR("Invalidating snapshot: Unable to allocate exception.");
  1227. if (s->store->type->drop_snapshot)
  1228. s->store->type->drop_snapshot(s->store);
  1229. s->valid = 0;
  1230. dm_table_event(s->ti->table);
  1231. }
  1232. static void pending_complete(void *context, int success)
  1233. {
  1234. struct dm_snap_pending_exception *pe = context;
  1235. struct dm_exception *e;
  1236. struct dm_snapshot *s = pe->snap;
  1237. struct bio *origin_bios = NULL;
  1238. struct bio *snapshot_bios = NULL;
  1239. struct bio *full_bio = NULL;
  1240. int error = 0;
  1241. if (!success) {
  1242. /* Read/write error - snapshot is unusable */
  1243. mutex_lock(&s->lock);
  1244. __invalidate_snapshot(s, -EIO);
  1245. error = 1;
  1246. goto out;
  1247. }
  1248. e = alloc_completed_exception(GFP_NOIO);
  1249. if (!e) {
  1250. mutex_lock(&s->lock);
  1251. __invalidate_snapshot(s, -ENOMEM);
  1252. error = 1;
  1253. goto out;
  1254. }
  1255. *e = pe->e;
  1256. mutex_lock(&s->lock);
  1257. if (!s->valid) {
  1258. free_completed_exception(e);
  1259. error = 1;
  1260. goto out;
  1261. }
  1262. /* Check for conflicting reads */
  1263. __check_for_conflicting_io(s, pe->e.old_chunk);
  1264. /*
  1265. * Add a proper exception, and remove the
  1266. * in-flight exception from the list.
  1267. */
  1268. dm_insert_exception(&s->complete, e);
  1269. out:
  1270. dm_remove_exception(&pe->e);
  1271. snapshot_bios = bio_list_get(&pe->snapshot_bios);
  1272. origin_bios = bio_list_get(&pe->origin_bios);
  1273. full_bio = pe->full_bio;
  1274. if (full_bio)
  1275. full_bio->bi_end_io = pe->full_bio_end_io;
  1276. increment_pending_exceptions_done_count();
  1277. mutex_unlock(&s->lock);
  1278. /* Submit any pending write bios */
  1279. if (error) {
  1280. if (full_bio)
  1281. bio_io_error(full_bio);
  1282. error_bios(snapshot_bios);
  1283. } else {
  1284. if (full_bio)
  1285. bio_endio(full_bio);
  1286. flush_bios(snapshot_bios);
  1287. }
  1288. retry_origin_bios(s, origin_bios);
  1289. free_pending_exception(pe);
  1290. }
  1291. static void complete_exception(struct dm_snap_pending_exception *pe)
  1292. {
  1293. struct dm_snapshot *s = pe->snap;
  1294. /* Update the metadata if we are persistent */
  1295. s->store->type->commit_exception(s->store, &pe->e, !pe->copy_error,
  1296. pending_complete, pe);
  1297. }
  1298. /*
  1299. * Called when the copy I/O has finished. kcopyd actually runs
  1300. * this code so don't block.
  1301. */
  1302. static void copy_callback(int read_err, unsigned long write_err, void *context)
  1303. {
  1304. struct dm_snap_pending_exception *pe = context;
  1305. struct dm_snapshot *s = pe->snap;
  1306. pe->copy_error = read_err || write_err;
  1307. if (pe->exception_sequence == s->exception_complete_sequence) {
  1308. struct rb_node *next;
  1309. s->exception_complete_sequence++;
  1310. complete_exception(pe);
  1311. next = rb_first(&s->out_of_order_tree);
  1312. while (next) {
  1313. pe = rb_entry(next, struct dm_snap_pending_exception,
  1314. out_of_order_node);
  1315. if (pe->exception_sequence != s->exception_complete_sequence)
  1316. break;
  1317. next = rb_next(next);
  1318. s->exception_complete_sequence++;
  1319. rb_erase(&pe->out_of_order_node, &s->out_of_order_tree);
  1320. complete_exception(pe);
  1321. cond_resched();
  1322. }
  1323. } else {
  1324. struct rb_node *parent = NULL;
  1325. struct rb_node **p = &s->out_of_order_tree.rb_node;
  1326. struct dm_snap_pending_exception *pe2;
  1327. while (*p) {
  1328. pe2 = rb_entry(*p, struct dm_snap_pending_exception, out_of_order_node);
  1329. parent = *p;
  1330. BUG_ON(pe->exception_sequence == pe2->exception_sequence);
  1331. if (pe->exception_sequence < pe2->exception_sequence)
  1332. p = &((*p)->rb_left);
  1333. else
  1334. p = &((*p)->rb_right);
  1335. }
  1336. rb_link_node(&pe->out_of_order_node, parent, p);
  1337. rb_insert_color(&pe->out_of_order_node, &s->out_of_order_tree);
  1338. }
  1339. account_end_copy(s);
  1340. }
  1341. /*
  1342. * Dispatches the copy operation to kcopyd.
  1343. */
  1344. static void start_copy(struct dm_snap_pending_exception *pe)
  1345. {
  1346. struct dm_snapshot *s = pe->snap;
  1347. struct dm_io_region src, dest;
  1348. struct block_device *bdev = s->origin->bdev;
  1349. sector_t dev_size;
  1350. dev_size = get_dev_size(bdev);
  1351. src.bdev = bdev;
  1352. src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
  1353. src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
  1354. dest.bdev = s->cow->bdev;
  1355. dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
  1356. dest.count = src.count;
  1357. /* Hand over to kcopyd */
  1358. account_start_copy(s);
  1359. dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe);
  1360. }
  1361. static void full_bio_end_io(struct bio *bio)
  1362. {
  1363. void *callback_data = bio->bi_private;
  1364. dm_kcopyd_do_callback(callback_data, 0, bio->bi_status ? 1 : 0);
  1365. }
  1366. static void start_full_bio(struct dm_snap_pending_exception *pe,
  1367. struct bio *bio)
  1368. {
  1369. struct dm_snapshot *s = pe->snap;
  1370. void *callback_data;
  1371. pe->full_bio = bio;
  1372. pe->full_bio_end_io = bio->bi_end_io;
  1373. account_start_copy(s);
  1374. callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client,
  1375. copy_callback, pe);
  1376. bio->bi_end_io = full_bio_end_io;
  1377. bio->bi_private = callback_data;
  1378. generic_make_request(bio);
  1379. }
  1380. static struct dm_snap_pending_exception *
  1381. __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
  1382. {
  1383. struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
  1384. if (!e)
  1385. return NULL;
  1386. return container_of(e, struct dm_snap_pending_exception, e);
  1387. }
  1388. /*
  1389. * Looks to see if this snapshot already has a pending exception
  1390. * for this chunk, otherwise it allocates a new one and inserts
  1391. * it into the pending table.
  1392. *
  1393. * NOTE: a write lock must be held on snap->lock before calling
  1394. * this.
  1395. */
  1396. static struct dm_snap_pending_exception *
  1397. __find_pending_exception(struct dm_snapshot *s,
  1398. struct dm_snap_pending_exception *pe, chunk_t chunk)
  1399. {
  1400. struct dm_snap_pending_exception *pe2;
  1401. pe2 = __lookup_pending_exception(s, chunk);
  1402. if (pe2) {
  1403. free_pending_exception(pe);
  1404. return pe2;
  1405. }
  1406. pe->e.old_chunk = chunk;
  1407. bio_list_init(&pe->origin_bios);
  1408. bio_list_init(&pe->snapshot_bios);
  1409. pe->started = 0;
  1410. pe->full_bio = NULL;
  1411. if (s->store->type->prepare_exception(s->store, &pe->e)) {
  1412. free_pending_exception(pe);
  1413. return NULL;
  1414. }
  1415. pe->exception_sequence = s->exception_start_sequence++;
  1416. dm_insert_exception(&s->pending, &pe->e);
  1417. return pe;
  1418. }
  1419. static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
  1420. struct bio *bio, chunk_t chunk)
  1421. {
  1422. bio_set_dev(bio, s->cow->bdev);
  1423. bio->bi_iter.bi_sector =
  1424. chunk_to_sector(s->store, dm_chunk_number(e->new_chunk) +
  1425. (chunk - e->old_chunk)) +
  1426. (bio->bi_iter.bi_sector & s->store->chunk_mask);
  1427. }
  1428. static int snapshot_map(struct dm_target *ti, struct bio *bio)
  1429. {
  1430. struct dm_exception *e;
  1431. struct dm_snapshot *s = ti->private;
  1432. int r = DM_MAPIO_REMAPPED;
  1433. chunk_t chunk;
  1434. struct dm_snap_pending_exception *pe = NULL;
  1435. init_tracked_chunk(bio);
  1436. if (bio->bi_opf & REQ_PREFLUSH) {
  1437. bio_set_dev(bio, s->cow->bdev);
  1438. return DM_MAPIO_REMAPPED;
  1439. }
  1440. chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
  1441. /* Full snapshots are not usable */
  1442. /* To get here the table must be live so s->active is always set. */
  1443. if (!s->valid)
  1444. return DM_MAPIO_KILL;
  1445. if (bio_data_dir(bio) == WRITE) {
  1446. while (unlikely(!wait_for_in_progress(s, false)))
  1447. ; /* wait_for_in_progress() has slept */
  1448. }
  1449. mutex_lock(&s->lock);
  1450. if (!s->valid || (unlikely(s->snapshot_overflowed) &&
  1451. bio_data_dir(bio) == WRITE)) {
  1452. r = DM_MAPIO_KILL;
  1453. goto out_unlock;
  1454. }
  1455. /* If the block is already remapped - use that, else remap it */
  1456. e = dm_lookup_exception(&s->complete, chunk);
  1457. if (e) {
  1458. remap_exception(s, e, bio, chunk);
  1459. goto out_unlock;
  1460. }
  1461. /*
  1462. * Write to snapshot - higher level takes care of RW/RO
  1463. * flags so we should only get this if we are
  1464. * writeable.
  1465. */
  1466. if (bio_data_dir(bio) == WRITE) {
  1467. pe = __lookup_pending_exception(s, chunk);
  1468. if (!pe) {
  1469. mutex_unlock(&s->lock);
  1470. pe = alloc_pending_exception(s);
  1471. mutex_lock(&s->lock);
  1472. if (!s->valid || s->snapshot_overflowed) {
  1473. free_pending_exception(pe);
  1474. r = DM_MAPIO_KILL;
  1475. goto out_unlock;
  1476. }
  1477. e = dm_lookup_exception(&s->complete, chunk);
  1478. if (e) {
  1479. free_pending_exception(pe);
  1480. remap_exception(s, e, bio, chunk);
  1481. goto out_unlock;
  1482. }
  1483. pe = __find_pending_exception(s, pe, chunk);
  1484. if (!pe) {
  1485. if (s->store->userspace_supports_overflow) {
  1486. s->snapshot_overflowed = 1;
  1487. DMERR("Snapshot overflowed: Unable to allocate exception.");
  1488. } else
  1489. __invalidate_snapshot(s, -ENOMEM);
  1490. r = DM_MAPIO_KILL;
  1491. goto out_unlock;
  1492. }
  1493. }
  1494. remap_exception(s, &pe->e, bio, chunk);
  1495. r = DM_MAPIO_SUBMITTED;
  1496. if (!pe->started &&
  1497. bio->bi_iter.bi_size ==
  1498. (s->store->chunk_size << SECTOR_SHIFT)) {
  1499. pe->started = 1;
  1500. mutex_unlock(&s->lock);
  1501. start_full_bio(pe, bio);
  1502. goto out;
  1503. }
  1504. bio_list_add(&pe->snapshot_bios, bio);
  1505. if (!pe->started) {
  1506. /* this is protected by snap->lock */
  1507. pe->started = 1;
  1508. mutex_unlock(&s->lock);
  1509. start_copy(pe);
  1510. goto out;
  1511. }
  1512. } else {
  1513. bio_set_dev(bio, s->origin->bdev);
  1514. track_chunk(s, bio, chunk);
  1515. }
  1516. out_unlock:
  1517. mutex_unlock(&s->lock);
  1518. out:
  1519. return r;
  1520. }
  1521. /*
  1522. * A snapshot-merge target behaves like a combination of a snapshot
  1523. * target and a snapshot-origin target. It only generates new
  1524. * exceptions in other snapshots and not in the one that is being
  1525. * merged.
  1526. *
  1527. * For each chunk, if there is an existing exception, it is used to
  1528. * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
  1529. * which in turn might generate exceptions in other snapshots.
  1530. * If merging is currently taking place on the chunk in question, the
  1531. * I/O is deferred by adding it to s->bios_queued_during_merge.
  1532. */
  1533. static int snapshot_merge_map(struct dm_target *ti, struct bio *bio)
  1534. {
  1535. struct dm_exception *e;
  1536. struct dm_snapshot *s = ti->private;
  1537. int r = DM_MAPIO_REMAPPED;
  1538. chunk_t chunk;
  1539. init_tracked_chunk(bio);
  1540. if (bio->bi_opf & REQ_PREFLUSH) {
  1541. if (!dm_bio_get_target_bio_nr(bio))
  1542. bio_set_dev(bio, s->origin->bdev);
  1543. else
  1544. bio_set_dev(bio, s->cow->bdev);
  1545. return DM_MAPIO_REMAPPED;
  1546. }
  1547. chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
  1548. mutex_lock(&s->lock);
  1549. /* Full merging snapshots are redirected to the origin */
  1550. if (!s->valid)
  1551. goto redirect_to_origin;
  1552. /* If the block is already remapped - use that */
  1553. e = dm_lookup_exception(&s->complete, chunk);
  1554. if (e) {
  1555. /* Queue writes overlapping with chunks being merged */
  1556. if (bio_data_dir(bio) == WRITE &&
  1557. chunk >= s->first_merging_chunk &&
  1558. chunk < (s->first_merging_chunk +
  1559. s->num_merging_chunks)) {
  1560. bio_set_dev(bio, s->origin->bdev);
  1561. bio_list_add(&s->bios_queued_during_merge, bio);
  1562. r = DM_MAPIO_SUBMITTED;
  1563. goto out_unlock;
  1564. }
  1565. remap_exception(s, e, bio, chunk);
  1566. if (bio_data_dir(bio) == WRITE)
  1567. track_chunk(s, bio, chunk);
  1568. goto out_unlock;
  1569. }
  1570. redirect_to_origin:
  1571. bio_set_dev(bio, s->origin->bdev);
  1572. if (bio_data_dir(bio) == WRITE) {
  1573. mutex_unlock(&s->lock);
  1574. return do_origin(s->origin, bio, false);
  1575. }
  1576. out_unlock:
  1577. mutex_unlock(&s->lock);
  1578. return r;
  1579. }
  1580. static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
  1581. blk_status_t *error)
  1582. {
  1583. struct dm_snapshot *s = ti->private;
  1584. if (is_bio_tracked(bio))
  1585. stop_tracking_chunk(s, bio);
  1586. return DM_ENDIO_DONE;
  1587. }
  1588. static void snapshot_merge_presuspend(struct dm_target *ti)
  1589. {
  1590. struct dm_snapshot *s = ti->private;
  1591. stop_merge(s);
  1592. }
  1593. static int snapshot_preresume(struct dm_target *ti)
  1594. {
  1595. int r = 0;
  1596. struct dm_snapshot *s = ti->private;
  1597. struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
  1598. down_read(&_origins_lock);
  1599. (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
  1600. if (snap_src && snap_dest) {
  1601. mutex_lock(&snap_src->lock);
  1602. if (s == snap_src) {
  1603. DMERR("Unable to resume snapshot source until "
  1604. "handover completes.");
  1605. r = -EINVAL;
  1606. } else if (!dm_suspended(snap_src->ti)) {
  1607. DMERR("Unable to perform snapshot handover until "
  1608. "source is suspended.");
  1609. r = -EINVAL;
  1610. }
  1611. mutex_unlock(&snap_src->lock);
  1612. }
  1613. up_read(&_origins_lock);
  1614. return r;
  1615. }
  1616. static void snapshot_resume(struct dm_target *ti)
  1617. {
  1618. struct dm_snapshot *s = ti->private;
  1619. struct dm_snapshot *snap_src = NULL, *snap_dest = NULL, *snap_merging = NULL;
  1620. struct dm_origin *o;
  1621. struct mapped_device *origin_md = NULL;
  1622. bool must_restart_merging = false;
  1623. down_read(&_origins_lock);
  1624. o = __lookup_dm_origin(s->origin->bdev);
  1625. if (o)
  1626. origin_md = dm_table_get_md(o->ti->table);
  1627. if (!origin_md) {
  1628. (void) __find_snapshots_sharing_cow(s, NULL, NULL, &snap_merging);
  1629. if (snap_merging)
  1630. origin_md = dm_table_get_md(snap_merging->ti->table);
  1631. }
  1632. if (origin_md == dm_table_get_md(ti->table))
  1633. origin_md = NULL;
  1634. if (origin_md) {
  1635. if (dm_hold(origin_md))
  1636. origin_md = NULL;
  1637. }
  1638. up_read(&_origins_lock);
  1639. if (origin_md) {
  1640. dm_internal_suspend_fast(origin_md);
  1641. if (snap_merging && test_bit(RUNNING_MERGE, &snap_merging->state_bits)) {
  1642. must_restart_merging = true;
  1643. stop_merge(snap_merging);
  1644. }
  1645. }
  1646. down_read(&_origins_lock);
  1647. (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
  1648. if (snap_src && snap_dest) {
  1649. mutex_lock(&snap_src->lock);
  1650. mutex_lock_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
  1651. __handover_exceptions(snap_src, snap_dest);
  1652. mutex_unlock(&snap_dest->lock);
  1653. mutex_unlock(&snap_src->lock);
  1654. }
  1655. up_read(&_origins_lock);
  1656. if (origin_md) {
  1657. if (must_restart_merging)
  1658. start_merge(snap_merging);
  1659. dm_internal_resume_fast(origin_md);
  1660. dm_put(origin_md);
  1661. }
  1662. /* Now we have correct chunk size, reregister */
  1663. reregister_snapshot(s);
  1664. mutex_lock(&s->lock);
  1665. s->active = 1;
  1666. mutex_unlock(&s->lock);
  1667. }
  1668. static uint32_t get_origin_minimum_chunksize(struct block_device *bdev)
  1669. {
  1670. uint32_t min_chunksize;
  1671. down_read(&_origins_lock);
  1672. min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
  1673. up_read(&_origins_lock);
  1674. return min_chunksize;
  1675. }
  1676. static void snapshot_merge_resume(struct dm_target *ti)
  1677. {
  1678. struct dm_snapshot *s = ti->private;
  1679. /*
  1680. * Handover exceptions from existing snapshot.
  1681. */
  1682. snapshot_resume(ti);
  1683. /*
  1684. * snapshot-merge acts as an origin, so set ti->max_io_len
  1685. */
  1686. ti->max_io_len = get_origin_minimum_chunksize(s->origin->bdev);
  1687. start_merge(s);
  1688. }
  1689. static void snapshot_status(struct dm_target *ti, status_type_t type,
  1690. unsigned status_flags, char *result, unsigned maxlen)
  1691. {
  1692. unsigned sz = 0;
  1693. struct dm_snapshot *snap = ti->private;
  1694. switch (type) {
  1695. case STATUSTYPE_INFO:
  1696. mutex_lock(&snap->lock);
  1697. if (!snap->valid)
  1698. DMEMIT("Invalid");
  1699. else if (snap->merge_failed)
  1700. DMEMIT("Merge failed");
  1701. else if (snap->snapshot_overflowed)
  1702. DMEMIT("Overflow");
  1703. else {
  1704. if (snap->store->type->usage) {
  1705. sector_t total_sectors, sectors_allocated,
  1706. metadata_sectors;
  1707. snap->store->type->usage(snap->store,
  1708. &total_sectors,
  1709. &sectors_allocated,
  1710. &metadata_sectors);
  1711. DMEMIT("%llu/%llu %llu",
  1712. (unsigned long long)sectors_allocated,
  1713. (unsigned long long)total_sectors,
  1714. (unsigned long long)metadata_sectors);
  1715. }
  1716. else
  1717. DMEMIT("Unknown");
  1718. }
  1719. mutex_unlock(&snap->lock);
  1720. break;
  1721. case STATUSTYPE_TABLE:
  1722. /*
  1723. * kdevname returns a static pointer so we need
  1724. * to make private copies if the output is to
  1725. * make sense.
  1726. */
  1727. DMEMIT("%s %s", snap->origin->name, snap->cow->name);
  1728. snap->store->type->status(snap->store, type, result + sz,
  1729. maxlen - sz);
  1730. break;
  1731. }
  1732. }
  1733. static int snapshot_iterate_devices(struct dm_target *ti,
  1734. iterate_devices_callout_fn fn, void *data)
  1735. {
  1736. struct dm_snapshot *snap = ti->private;
  1737. int r;
  1738. r = fn(ti, snap->origin, 0, ti->len, data);
  1739. if (!r)
  1740. r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
  1741. return r;
  1742. }
  1743. /*-----------------------------------------------------------------
  1744. * Origin methods
  1745. *---------------------------------------------------------------*/
  1746. /*
  1747. * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
  1748. * supplied bio was ignored. The caller may submit it immediately.
  1749. * (No remapping actually occurs as the origin is always a direct linear
  1750. * map.)
  1751. *
  1752. * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
  1753. * and any supplied bio is added to a list to be submitted once all
  1754. * the necessary exceptions exist.
  1755. */
  1756. static int __origin_write(struct list_head *snapshots, sector_t sector,
  1757. struct bio *bio)
  1758. {
  1759. int r = DM_MAPIO_REMAPPED;
  1760. struct dm_snapshot *snap;
  1761. struct dm_exception *e;
  1762. struct dm_snap_pending_exception *pe;
  1763. struct dm_snap_pending_exception *pe_to_start_now = NULL;
  1764. struct dm_snap_pending_exception *pe_to_start_last = NULL;
  1765. chunk_t chunk;
  1766. /* Do all the snapshots on this origin */
  1767. list_for_each_entry (snap, snapshots, list) {
  1768. /*
  1769. * Don't make new exceptions in a merging snapshot
  1770. * because it has effectively been deleted
  1771. */
  1772. if (dm_target_is_snapshot_merge(snap->ti))
  1773. continue;
  1774. mutex_lock(&snap->lock);
  1775. /* Only deal with valid and active snapshots */
  1776. if (!snap->valid || !snap->active)
  1777. goto next_snapshot;
  1778. /* Nothing to do if writing beyond end of snapshot */
  1779. if (sector >= dm_table_get_size(snap->ti->table))
  1780. goto next_snapshot;
  1781. /*
  1782. * Remember, different snapshots can have
  1783. * different chunk sizes.
  1784. */
  1785. chunk = sector_to_chunk(snap->store, sector);
  1786. /*
  1787. * Check exception table to see if block
  1788. * is already remapped in this snapshot
  1789. * and trigger an exception if not.
  1790. */
  1791. e = dm_lookup_exception(&snap->complete, chunk);
  1792. if (e)
  1793. goto next_snapshot;
  1794. pe = __lookup_pending_exception(snap, chunk);
  1795. if (!pe) {
  1796. mutex_unlock(&snap->lock);
  1797. pe = alloc_pending_exception(snap);
  1798. mutex_lock(&snap->lock);
  1799. if (!snap->valid) {
  1800. free_pending_exception(pe);
  1801. goto next_snapshot;
  1802. }
  1803. e = dm_lookup_exception(&snap->complete, chunk);
  1804. if (e) {
  1805. free_pending_exception(pe);
  1806. goto next_snapshot;
  1807. }
  1808. pe = __find_pending_exception(snap, pe, chunk);
  1809. if (!pe) {
  1810. __invalidate_snapshot(snap, -ENOMEM);
  1811. goto next_snapshot;
  1812. }
  1813. }
  1814. r = DM_MAPIO_SUBMITTED;
  1815. /*
  1816. * If an origin bio was supplied, queue it to wait for the
  1817. * completion of this exception, and start this one last,
  1818. * at the end of the function.
  1819. */
  1820. if (bio) {
  1821. bio_list_add(&pe->origin_bios, bio);
  1822. bio = NULL;
  1823. if (!pe->started) {
  1824. pe->started = 1;
  1825. pe_to_start_last = pe;
  1826. }
  1827. }
  1828. if (!pe->started) {
  1829. pe->started = 1;
  1830. pe_to_start_now = pe;
  1831. }
  1832. next_snapshot:
  1833. mutex_unlock(&snap->lock);
  1834. if (pe_to_start_now) {
  1835. start_copy(pe_to_start_now);
  1836. pe_to_start_now = NULL;
  1837. }
  1838. }
  1839. /*
  1840. * Submit the exception against which the bio is queued last,
  1841. * to give the other exceptions a head start.
  1842. */
  1843. if (pe_to_start_last)
  1844. start_copy(pe_to_start_last);
  1845. return r;
  1846. }
  1847. /*
  1848. * Called on a write from the origin driver.
  1849. */
  1850. static int do_origin(struct dm_dev *origin, struct bio *bio, bool limit)
  1851. {
  1852. struct origin *o;
  1853. int r = DM_MAPIO_REMAPPED;
  1854. again:
  1855. down_read(&_origins_lock);
  1856. o = __lookup_origin(origin->bdev);
  1857. if (o) {
  1858. if (limit) {
  1859. struct dm_snapshot *s;
  1860. list_for_each_entry(s, &o->snapshots, list)
  1861. if (unlikely(!wait_for_in_progress(s, true)))
  1862. goto again;
  1863. }
  1864. r = __origin_write(&o->snapshots, bio->bi_iter.bi_sector, bio);
  1865. }
  1866. up_read(&_origins_lock);
  1867. return r;
  1868. }
  1869. /*
  1870. * Trigger exceptions in all non-merging snapshots.
  1871. *
  1872. * The chunk size of the merging snapshot may be larger than the chunk
  1873. * size of some other snapshot so we may need to reallocate multiple
  1874. * chunks in other snapshots.
  1875. *
  1876. * We scan all the overlapping exceptions in the other snapshots.
  1877. * Returns 1 if anything was reallocated and must be waited for,
  1878. * otherwise returns 0.
  1879. *
  1880. * size must be a multiple of merging_snap's chunk_size.
  1881. */
  1882. static int origin_write_extent(struct dm_snapshot *merging_snap,
  1883. sector_t sector, unsigned size)
  1884. {
  1885. int must_wait = 0;
  1886. sector_t n;
  1887. struct origin *o;
  1888. /*
  1889. * The origin's __minimum_chunk_size() got stored in max_io_len
  1890. * by snapshot_merge_resume().
  1891. */
  1892. down_read(&_origins_lock);
  1893. o = __lookup_origin(merging_snap->origin->bdev);
  1894. for (n = 0; n < size; n += merging_snap->ti->max_io_len)
  1895. if (__origin_write(&o->snapshots, sector + n, NULL) ==
  1896. DM_MAPIO_SUBMITTED)
  1897. must_wait = 1;
  1898. up_read(&_origins_lock);
  1899. return must_wait;
  1900. }
  1901. /*
  1902. * Origin: maps a linear range of a device, with hooks for snapshotting.
  1903. */
  1904. /*
  1905. * Construct an origin mapping: <dev_path>
  1906. * The context for an origin is merely a 'struct dm_dev *'
  1907. * pointing to the real device.
  1908. */
  1909. static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1910. {
  1911. int r;
  1912. struct dm_origin *o;
  1913. if (argc != 1) {
  1914. ti->error = "origin: incorrect number of arguments";
  1915. return -EINVAL;
  1916. }
  1917. o = kmalloc(sizeof(struct dm_origin), GFP_KERNEL);
  1918. if (!o) {
  1919. ti->error = "Cannot allocate private origin structure";
  1920. r = -ENOMEM;
  1921. goto bad_alloc;
  1922. }
  1923. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &o->dev);
  1924. if (r) {
  1925. ti->error = "Cannot get target device";
  1926. goto bad_open;
  1927. }
  1928. o->ti = ti;
  1929. ti->private = o;
  1930. ti->num_flush_bios = 1;
  1931. return 0;
  1932. bad_open:
  1933. kfree(o);
  1934. bad_alloc:
  1935. return r;
  1936. }
  1937. static void origin_dtr(struct dm_target *ti)
  1938. {
  1939. struct dm_origin *o = ti->private;
  1940. dm_put_device(ti, o->dev);
  1941. kfree(o);
  1942. }
  1943. static int origin_map(struct dm_target *ti, struct bio *bio)
  1944. {
  1945. struct dm_origin *o = ti->private;
  1946. unsigned available_sectors;
  1947. bio_set_dev(bio, o->dev->bdev);
  1948. if (unlikely(bio->bi_opf & REQ_PREFLUSH))
  1949. return DM_MAPIO_REMAPPED;
  1950. if (bio_data_dir(bio) != WRITE)
  1951. return DM_MAPIO_REMAPPED;
  1952. available_sectors = o->split_boundary -
  1953. ((unsigned)bio->bi_iter.bi_sector & (o->split_boundary - 1));
  1954. if (bio_sectors(bio) > available_sectors)
  1955. dm_accept_partial_bio(bio, available_sectors);
  1956. /* Only tell snapshots if this is a write */
  1957. return do_origin(o->dev, bio, true);
  1958. }
  1959. static long origin_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
  1960. long nr_pages, void **kaddr, pfn_t *pfn)
  1961. {
  1962. DMWARN("device does not support dax.");
  1963. return -EIO;
  1964. }
  1965. /*
  1966. * Set the target "max_io_len" field to the minimum of all the snapshots'
  1967. * chunk sizes.
  1968. */
  1969. static void origin_resume(struct dm_target *ti)
  1970. {
  1971. struct dm_origin *o = ti->private;
  1972. o->split_boundary = get_origin_minimum_chunksize(o->dev->bdev);
  1973. down_write(&_origins_lock);
  1974. __insert_dm_origin(o);
  1975. up_write(&_origins_lock);
  1976. }
  1977. static void origin_postsuspend(struct dm_target *ti)
  1978. {
  1979. struct dm_origin *o = ti->private;
  1980. down_write(&_origins_lock);
  1981. __remove_dm_origin(o);
  1982. up_write(&_origins_lock);
  1983. }
  1984. static void origin_status(struct dm_target *ti, status_type_t type,
  1985. unsigned status_flags, char *result, unsigned maxlen)
  1986. {
  1987. struct dm_origin *o = ti->private;
  1988. switch (type) {
  1989. case STATUSTYPE_INFO:
  1990. result[0] = '\0';
  1991. break;
  1992. case STATUSTYPE_TABLE:
  1993. snprintf(result, maxlen, "%s", o->dev->name);
  1994. break;
  1995. }
  1996. }
  1997. static int origin_iterate_devices(struct dm_target *ti,
  1998. iterate_devices_callout_fn fn, void *data)
  1999. {
  2000. struct dm_origin *o = ti->private;
  2001. return fn(ti, o->dev, 0, ti->len, data);
  2002. }
  2003. static struct target_type origin_target = {
  2004. .name = "snapshot-origin",
  2005. .version = {1, 9, 0},
  2006. .module = THIS_MODULE,
  2007. .ctr = origin_ctr,
  2008. .dtr = origin_dtr,
  2009. .map = origin_map,
  2010. .resume = origin_resume,
  2011. .postsuspend = origin_postsuspend,
  2012. .status = origin_status,
  2013. .iterate_devices = origin_iterate_devices,
  2014. .direct_access = origin_dax_direct_access,
  2015. };
  2016. static struct target_type snapshot_target = {
  2017. .name = "snapshot",
  2018. .version = {1, 15, 0},
  2019. .module = THIS_MODULE,
  2020. .ctr = snapshot_ctr,
  2021. .dtr = snapshot_dtr,
  2022. .map = snapshot_map,
  2023. .end_io = snapshot_end_io,
  2024. .preresume = snapshot_preresume,
  2025. .resume = snapshot_resume,
  2026. .status = snapshot_status,
  2027. .iterate_devices = snapshot_iterate_devices,
  2028. };
  2029. static struct target_type merge_target = {
  2030. .name = dm_snapshot_merge_target_name,
  2031. .version = {1, 4, 0},
  2032. .module = THIS_MODULE,
  2033. .ctr = snapshot_ctr,
  2034. .dtr = snapshot_dtr,
  2035. .map = snapshot_merge_map,
  2036. .end_io = snapshot_end_io,
  2037. .presuspend = snapshot_merge_presuspend,
  2038. .preresume = snapshot_preresume,
  2039. .resume = snapshot_merge_resume,
  2040. .status = snapshot_status,
  2041. .iterate_devices = snapshot_iterate_devices,
  2042. };
  2043. static int __init dm_snapshot_init(void)
  2044. {
  2045. int r;
  2046. r = dm_exception_store_init();
  2047. if (r) {
  2048. DMERR("Failed to initialize exception stores");
  2049. return r;
  2050. }
  2051. r = init_origin_hash();
  2052. if (r) {
  2053. DMERR("init_origin_hash failed.");
  2054. goto bad_origin_hash;
  2055. }
  2056. exception_cache = KMEM_CACHE(dm_exception, 0);
  2057. if (!exception_cache) {
  2058. DMERR("Couldn't create exception cache.");
  2059. r = -ENOMEM;
  2060. goto bad_exception_cache;
  2061. }
  2062. pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
  2063. if (!pending_cache) {
  2064. DMERR("Couldn't create pending cache.");
  2065. r = -ENOMEM;
  2066. goto bad_pending_cache;
  2067. }
  2068. r = dm_register_target(&snapshot_target);
  2069. if (r < 0) {
  2070. DMERR("snapshot target register failed %d", r);
  2071. goto bad_register_snapshot_target;
  2072. }
  2073. r = dm_register_target(&origin_target);
  2074. if (r < 0) {
  2075. DMERR("Origin target register failed %d", r);
  2076. goto bad_register_origin_target;
  2077. }
  2078. r = dm_register_target(&merge_target);
  2079. if (r < 0) {
  2080. DMERR("Merge target register failed %d", r);
  2081. goto bad_register_merge_target;
  2082. }
  2083. return 0;
  2084. bad_register_merge_target:
  2085. dm_unregister_target(&origin_target);
  2086. bad_register_origin_target:
  2087. dm_unregister_target(&snapshot_target);
  2088. bad_register_snapshot_target:
  2089. kmem_cache_destroy(pending_cache);
  2090. bad_pending_cache:
  2091. kmem_cache_destroy(exception_cache);
  2092. bad_exception_cache:
  2093. exit_origin_hash();
  2094. bad_origin_hash:
  2095. dm_exception_store_exit();
  2096. return r;
  2097. }
  2098. static void __exit dm_snapshot_exit(void)
  2099. {
  2100. dm_unregister_target(&snapshot_target);
  2101. dm_unregister_target(&origin_target);
  2102. dm_unregister_target(&merge_target);
  2103. exit_origin_hash();
  2104. kmem_cache_destroy(pending_cache);
  2105. kmem_cache_destroy(exception_cache);
  2106. dm_exception_store_exit();
  2107. }
  2108. /* Module hooks */
  2109. module_init(dm_snapshot_init);
  2110. module_exit(dm_snapshot_exit);
  2111. MODULE_DESCRIPTION(DM_NAME " snapshot target");
  2112. MODULE_AUTHOR("Joe Thornber");
  2113. MODULE_LICENSE("GPL");
  2114. MODULE_ALIAS("dm-snapshot-origin");
  2115. MODULE_ALIAS("dm-snapshot-merge");