dm-snap.c 54 KB

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