dm-snap.c 53 KB

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