dm-snap.c 52 KB

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