dm-snap.c 52 KB

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