dm-snap.c 53 KB

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