dm-snap.c 53 KB

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