dm-snap.c 53 KB

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