dm-snap.c 57 KB

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