dm-snap.c 61 KB

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