dm-snap.c 52 KB

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