dm-snap.c 58 KB

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