dm-snap.c 53 KB

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