dm-snap.c 55 KB

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