dm-snap.c 58 KB

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