dm-snap.c 53 KB

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