dm-snap.c 52 KB

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