dm-snap.c 52 KB

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