dm-snap.c 53 KB

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