dm-snap.c 57 KB

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