dm-snap.c 55 KB

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