dm-snap.c 53 KB

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