dm-snap.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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/ctype.h>
  10. #include <linux/device-mapper.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-snap.h"
  22. #include "dm-bio-list.h"
  23. #define DM_MSG_PREFIX "snapshots"
  24. /*
  25. * The percentage increment we will wake up users at
  26. */
  27. #define WAKE_UP_PERCENT 5
  28. /*
  29. * kcopyd priority of snapshot operations
  30. */
  31. #define SNAPSHOT_COPY_PRIORITY 2
  32. /*
  33. * Reserve 1MB for each snapshot initially (with minimum of 1 page).
  34. */
  35. #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
  36. /*
  37. * The size of the mempool used to track chunks in use.
  38. */
  39. #define MIN_IOS 256
  40. static struct workqueue_struct *ksnapd;
  41. static void flush_queued_bios(struct work_struct *work);
  42. struct dm_snap_pending_exception {
  43. struct dm_snap_exception e;
  44. /*
  45. * Origin buffers waiting for this to complete are held
  46. * in a bio list
  47. */
  48. struct bio_list origin_bios;
  49. struct bio_list snapshot_bios;
  50. /*
  51. * Short-term queue of pending exceptions prior to submission.
  52. */
  53. struct list_head list;
  54. /*
  55. * The primary pending_exception is the one that holds
  56. * the ref_count and the list of origin_bios for a
  57. * group of pending_exceptions. It is always last to get freed.
  58. * These fields get set up when writing to the origin.
  59. */
  60. struct dm_snap_pending_exception *primary_pe;
  61. /*
  62. * Number of pending_exceptions processing this chunk.
  63. * When this drops to zero we must complete the origin bios.
  64. * If incrementing or decrementing this, hold pe->snap->lock for
  65. * the sibling concerned and not pe->primary_pe->snap->lock unless
  66. * they are the same.
  67. */
  68. atomic_t ref_count;
  69. /* Pointer back to snapshot context */
  70. struct dm_snapshot *snap;
  71. /*
  72. * 1 indicates the exception has already been sent to
  73. * kcopyd.
  74. */
  75. int started;
  76. };
  77. /*
  78. * Hash table mapping origin volumes to lists of snapshots and
  79. * a lock to protect it
  80. */
  81. static struct kmem_cache *exception_cache;
  82. static struct kmem_cache *pending_cache;
  83. struct dm_snap_tracked_chunk {
  84. struct hlist_node node;
  85. chunk_t chunk;
  86. };
  87. static struct kmem_cache *tracked_chunk_cache;
  88. static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
  89. chunk_t chunk)
  90. {
  91. struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
  92. GFP_NOIO);
  93. unsigned long flags;
  94. c->chunk = chunk;
  95. spin_lock_irqsave(&s->tracked_chunk_lock, flags);
  96. hlist_add_head(&c->node,
  97. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
  98. spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
  99. return c;
  100. }
  101. static void stop_tracking_chunk(struct dm_snapshot *s,
  102. struct dm_snap_tracked_chunk *c)
  103. {
  104. unsigned long flags;
  105. spin_lock_irqsave(&s->tracked_chunk_lock, flags);
  106. hlist_del(&c->node);
  107. spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
  108. mempool_free(c, s->tracked_chunk_pool);
  109. }
  110. static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
  111. {
  112. struct dm_snap_tracked_chunk *c;
  113. struct hlist_node *hn;
  114. int found = 0;
  115. spin_lock_irq(&s->tracked_chunk_lock);
  116. hlist_for_each_entry(c, hn,
  117. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
  118. if (c->chunk == chunk) {
  119. found = 1;
  120. break;
  121. }
  122. }
  123. spin_unlock_irq(&s->tracked_chunk_lock);
  124. return found;
  125. }
  126. /*
  127. * One of these per registered origin, held in the snapshot_origins hash
  128. */
  129. struct origin {
  130. /* The origin device */
  131. struct block_device *bdev;
  132. struct list_head hash_list;
  133. /* List of snapshots for this origin */
  134. struct list_head snapshots;
  135. };
  136. /*
  137. * Size of the hash table for origin volumes. If we make this
  138. * the size of the minors list then it should be nearly perfect
  139. */
  140. #define ORIGIN_HASH_SIZE 256
  141. #define ORIGIN_MASK 0xFF
  142. static struct list_head *_origins;
  143. static struct rw_semaphore _origins_lock;
  144. static int init_origin_hash(void)
  145. {
  146. int i;
  147. _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
  148. GFP_KERNEL);
  149. if (!_origins) {
  150. DMERR("unable to allocate memory");
  151. return -ENOMEM;
  152. }
  153. for (i = 0; i < ORIGIN_HASH_SIZE; i++)
  154. INIT_LIST_HEAD(_origins + i);
  155. init_rwsem(&_origins_lock);
  156. return 0;
  157. }
  158. static void exit_origin_hash(void)
  159. {
  160. kfree(_origins);
  161. }
  162. static unsigned origin_hash(struct block_device *bdev)
  163. {
  164. return bdev->bd_dev & ORIGIN_MASK;
  165. }
  166. static struct origin *__lookup_origin(struct block_device *origin)
  167. {
  168. struct list_head *ol;
  169. struct origin *o;
  170. ol = &_origins[origin_hash(origin)];
  171. list_for_each_entry (o, ol, hash_list)
  172. if (bdev_equal(o->bdev, origin))
  173. return o;
  174. return NULL;
  175. }
  176. static void __insert_origin(struct origin *o)
  177. {
  178. struct list_head *sl = &_origins[origin_hash(o->bdev)];
  179. list_add_tail(&o->hash_list, sl);
  180. }
  181. /*
  182. * Make a note of the snapshot and its origin so we can look it
  183. * up when the origin has a write on it.
  184. */
  185. static int register_snapshot(struct dm_snapshot *snap)
  186. {
  187. struct origin *o;
  188. struct block_device *bdev = snap->origin->bdev;
  189. down_write(&_origins_lock);
  190. o = __lookup_origin(bdev);
  191. if (!o) {
  192. /* New origin */
  193. o = kmalloc(sizeof(*o), GFP_KERNEL);
  194. if (!o) {
  195. up_write(&_origins_lock);
  196. return -ENOMEM;
  197. }
  198. /* Initialise the struct */
  199. INIT_LIST_HEAD(&o->snapshots);
  200. o->bdev = bdev;
  201. __insert_origin(o);
  202. }
  203. list_add_tail(&snap->list, &o->snapshots);
  204. up_write(&_origins_lock);
  205. return 0;
  206. }
  207. static void unregister_snapshot(struct dm_snapshot *s)
  208. {
  209. struct origin *o;
  210. down_write(&_origins_lock);
  211. o = __lookup_origin(s->origin->bdev);
  212. list_del(&s->list);
  213. if (list_empty(&o->snapshots)) {
  214. list_del(&o->hash_list);
  215. kfree(o);
  216. }
  217. up_write(&_origins_lock);
  218. }
  219. /*
  220. * Implementation of the exception hash tables.
  221. * The lowest hash_shift bits of the chunk number are ignored, allowing
  222. * some consecutive chunks to be grouped together.
  223. */
  224. static int init_exception_table(struct exception_table *et, uint32_t size,
  225. unsigned hash_shift)
  226. {
  227. unsigned int i;
  228. et->hash_shift = hash_shift;
  229. et->hash_mask = size - 1;
  230. et->table = dm_vcalloc(size, sizeof(struct list_head));
  231. if (!et->table)
  232. return -ENOMEM;
  233. for (i = 0; i < size; i++)
  234. INIT_LIST_HEAD(et->table + i);
  235. return 0;
  236. }
  237. static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
  238. {
  239. struct list_head *slot;
  240. struct dm_snap_exception *ex, *next;
  241. int i, size;
  242. size = et->hash_mask + 1;
  243. for (i = 0; i < size; i++) {
  244. slot = et->table + i;
  245. list_for_each_entry_safe (ex, next, slot, hash_list)
  246. kmem_cache_free(mem, ex);
  247. }
  248. vfree(et->table);
  249. }
  250. static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
  251. {
  252. return (chunk >> et->hash_shift) & et->hash_mask;
  253. }
  254. static void insert_exception(struct exception_table *eh,
  255. struct dm_snap_exception *e)
  256. {
  257. struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
  258. list_add(&e->hash_list, l);
  259. }
  260. static void remove_exception(struct dm_snap_exception *e)
  261. {
  262. list_del(&e->hash_list);
  263. }
  264. /*
  265. * Return the exception data for a sector, or NULL if not
  266. * remapped.
  267. */
  268. static struct dm_snap_exception *lookup_exception(struct exception_table *et,
  269. chunk_t chunk)
  270. {
  271. struct list_head *slot;
  272. struct dm_snap_exception *e;
  273. slot = &et->table[exception_hash(et, chunk)];
  274. list_for_each_entry (e, slot, hash_list)
  275. if (chunk >= e->old_chunk &&
  276. chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
  277. return e;
  278. return NULL;
  279. }
  280. static struct dm_snap_exception *alloc_exception(void)
  281. {
  282. struct dm_snap_exception *e;
  283. e = kmem_cache_alloc(exception_cache, GFP_NOIO);
  284. if (!e)
  285. e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
  286. return e;
  287. }
  288. static void free_exception(struct dm_snap_exception *e)
  289. {
  290. kmem_cache_free(exception_cache, e);
  291. }
  292. static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
  293. {
  294. struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
  295. GFP_NOIO);
  296. pe->snap = s;
  297. return pe;
  298. }
  299. static void free_pending_exception(struct dm_snap_pending_exception *pe)
  300. {
  301. mempool_free(pe, pe->snap->pending_pool);
  302. }
  303. static void insert_completed_exception(struct dm_snapshot *s,
  304. struct dm_snap_exception *new_e)
  305. {
  306. struct exception_table *eh = &s->complete;
  307. struct list_head *l;
  308. struct dm_snap_exception *e = NULL;
  309. l = &eh->table[exception_hash(eh, new_e->old_chunk)];
  310. /* Add immediately if this table doesn't support consecutive chunks */
  311. if (!eh->hash_shift)
  312. goto out;
  313. /* List is ordered by old_chunk */
  314. list_for_each_entry_reverse(e, l, hash_list) {
  315. /* Insert after an existing chunk? */
  316. if (new_e->old_chunk == (e->old_chunk +
  317. dm_consecutive_chunk_count(e) + 1) &&
  318. new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
  319. dm_consecutive_chunk_count(e) + 1)) {
  320. dm_consecutive_chunk_count_inc(e);
  321. free_exception(new_e);
  322. return;
  323. }
  324. /* Insert before an existing chunk? */
  325. if (new_e->old_chunk == (e->old_chunk - 1) &&
  326. new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
  327. dm_consecutive_chunk_count_inc(e);
  328. e->old_chunk--;
  329. e->new_chunk--;
  330. free_exception(new_e);
  331. return;
  332. }
  333. if (new_e->old_chunk > e->old_chunk)
  334. break;
  335. }
  336. out:
  337. list_add(&new_e->hash_list, e ? &e->hash_list : l);
  338. }
  339. int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
  340. {
  341. struct dm_snap_exception *e;
  342. e = alloc_exception();
  343. if (!e)
  344. return -ENOMEM;
  345. e->old_chunk = old;
  346. /* Consecutive_count is implicitly initialised to zero */
  347. e->new_chunk = new;
  348. insert_completed_exception(s, e);
  349. return 0;
  350. }
  351. /*
  352. * Hard coded magic.
  353. */
  354. static int calc_max_buckets(void)
  355. {
  356. /* use a fixed size of 2MB */
  357. unsigned long mem = 2 * 1024 * 1024;
  358. mem /= sizeof(struct list_head);
  359. return mem;
  360. }
  361. /*
  362. * Allocate room for a suitable hash table.
  363. */
  364. static int init_hash_tables(struct dm_snapshot *s)
  365. {
  366. sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
  367. /*
  368. * Calculate based on the size of the original volume or
  369. * the COW volume...
  370. */
  371. cow_dev_size = get_dev_size(s->cow->bdev);
  372. origin_dev_size = get_dev_size(s->origin->bdev);
  373. max_buckets = calc_max_buckets();
  374. hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
  375. hash_size = min(hash_size, max_buckets);
  376. hash_size = rounddown_pow_of_two(hash_size);
  377. if (init_exception_table(&s->complete, hash_size,
  378. DM_CHUNK_CONSECUTIVE_BITS))
  379. return -ENOMEM;
  380. /*
  381. * Allocate hash table for in-flight exceptions
  382. * Make this smaller than the real hash table
  383. */
  384. hash_size >>= 3;
  385. if (hash_size < 64)
  386. hash_size = 64;
  387. if (init_exception_table(&s->pending, hash_size, 0)) {
  388. exit_exception_table(&s->complete, exception_cache);
  389. return -ENOMEM;
  390. }
  391. return 0;
  392. }
  393. /*
  394. * Round a number up to the nearest 'size' boundary. size must
  395. * be a power of 2.
  396. */
  397. static ulong round_up(ulong n, ulong size)
  398. {
  399. size--;
  400. return (n + size) & ~size;
  401. }
  402. static int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
  403. char **error)
  404. {
  405. unsigned long chunk_size;
  406. char *value;
  407. chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
  408. if (*chunk_size_arg == '\0' || *value != '\0') {
  409. *error = "Invalid chunk size";
  410. return -EINVAL;
  411. }
  412. if (!chunk_size) {
  413. s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
  414. return 0;
  415. }
  416. /*
  417. * Chunk size must be multiple of page size. Silently
  418. * round up if it's not.
  419. */
  420. chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
  421. /* Check chunk_size is a power of 2 */
  422. if (!is_power_of_2(chunk_size)) {
  423. *error = "Chunk size is not a power of 2";
  424. return -EINVAL;
  425. }
  426. /* Validate the chunk size against the device block size */
  427. if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
  428. *error = "Chunk size is not a multiple of device blocksize";
  429. return -EINVAL;
  430. }
  431. s->chunk_size = chunk_size;
  432. s->chunk_mask = chunk_size - 1;
  433. s->chunk_shift = ffs(chunk_size) - 1;
  434. return 0;
  435. }
  436. /*
  437. * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
  438. */
  439. static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  440. {
  441. struct dm_snapshot *s;
  442. int i;
  443. int r = -EINVAL;
  444. char persistent;
  445. char *origin_path;
  446. char *cow_path;
  447. if (argc != 4) {
  448. ti->error = "requires exactly 4 arguments";
  449. r = -EINVAL;
  450. goto bad1;
  451. }
  452. origin_path = argv[0];
  453. cow_path = argv[1];
  454. persistent = toupper(*argv[2]);
  455. if (persistent != 'P' && persistent != 'N') {
  456. ti->error = "Persistent flag is not P or N";
  457. r = -EINVAL;
  458. goto bad1;
  459. }
  460. s = kmalloc(sizeof(*s), GFP_KERNEL);
  461. if (s == NULL) {
  462. ti->error = "Cannot allocate snapshot context private "
  463. "structure";
  464. r = -ENOMEM;
  465. goto bad1;
  466. }
  467. r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
  468. if (r) {
  469. ti->error = "Cannot get origin device";
  470. goto bad2;
  471. }
  472. r = dm_get_device(ti, cow_path, 0, 0,
  473. FMODE_READ | FMODE_WRITE, &s->cow);
  474. if (r) {
  475. dm_put_device(ti, s->origin);
  476. ti->error = "Cannot get COW device";
  477. goto bad2;
  478. }
  479. r = set_chunk_size(s, argv[3], &ti->error);
  480. if (r)
  481. goto bad3;
  482. s->type = persistent;
  483. s->valid = 1;
  484. s->active = 0;
  485. init_rwsem(&s->lock);
  486. spin_lock_init(&s->pe_lock);
  487. s->ti = ti;
  488. /* Allocate hash table for COW data */
  489. if (init_hash_tables(s)) {
  490. ti->error = "Unable to allocate hash table space";
  491. r = -ENOMEM;
  492. goto bad3;
  493. }
  494. s->store.snap = s;
  495. if (persistent == 'P')
  496. r = dm_create_persistent(&s->store);
  497. else
  498. r = dm_create_transient(&s->store);
  499. if (r) {
  500. ti->error = "Couldn't create exception store";
  501. r = -EINVAL;
  502. goto bad4;
  503. }
  504. r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
  505. if (r) {
  506. ti->error = "Could not create kcopyd client";
  507. goto bad5;
  508. }
  509. s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
  510. if (!s->pending_pool) {
  511. ti->error = "Could not allocate mempool for pending exceptions";
  512. goto bad6;
  513. }
  514. s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
  515. tracked_chunk_cache);
  516. if (!s->tracked_chunk_pool) {
  517. ti->error = "Could not allocate tracked_chunk mempool for "
  518. "tracking reads";
  519. goto bad_tracked_chunk_pool;
  520. }
  521. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  522. INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
  523. spin_lock_init(&s->tracked_chunk_lock);
  524. /* Metadata must only be loaded into one table at once */
  525. r = s->store.read_metadata(&s->store);
  526. if (r < 0) {
  527. ti->error = "Failed to read snapshot metadata";
  528. goto bad_load_and_register;
  529. } else if (r > 0) {
  530. s->valid = 0;
  531. DMWARN("Snapshot is marked invalid.");
  532. }
  533. bio_list_init(&s->queued_bios);
  534. INIT_WORK(&s->queued_bios_work, flush_queued_bios);
  535. /* Add snapshot to the list of snapshots for this origin */
  536. /* Exceptions aren't triggered till snapshot_resume() is called */
  537. if (register_snapshot(s)) {
  538. r = -EINVAL;
  539. ti->error = "Cannot register snapshot origin";
  540. goto bad_load_and_register;
  541. }
  542. ti->private = s;
  543. ti->split_io = s->chunk_size;
  544. return 0;
  545. bad_load_and_register:
  546. mempool_destroy(s->tracked_chunk_pool);
  547. bad_tracked_chunk_pool:
  548. mempool_destroy(s->pending_pool);
  549. bad6:
  550. dm_kcopyd_client_destroy(s->kcopyd_client);
  551. bad5:
  552. s->store.destroy(&s->store);
  553. bad4:
  554. exit_exception_table(&s->pending, pending_cache);
  555. exit_exception_table(&s->complete, exception_cache);
  556. bad3:
  557. dm_put_device(ti, s->cow);
  558. dm_put_device(ti, s->origin);
  559. bad2:
  560. kfree(s);
  561. bad1:
  562. return r;
  563. }
  564. static void __free_exceptions(struct dm_snapshot *s)
  565. {
  566. dm_kcopyd_client_destroy(s->kcopyd_client);
  567. s->kcopyd_client = NULL;
  568. exit_exception_table(&s->pending, pending_cache);
  569. exit_exception_table(&s->complete, exception_cache);
  570. s->store.destroy(&s->store);
  571. }
  572. static void snapshot_dtr(struct dm_target *ti)
  573. {
  574. #ifdef CONFIG_DM_DEBUG
  575. int i;
  576. #endif
  577. struct dm_snapshot *s = ti->private;
  578. flush_workqueue(ksnapd);
  579. /* Prevent further origin writes from using this snapshot. */
  580. /* After this returns there can be no new kcopyd jobs. */
  581. unregister_snapshot(s);
  582. #ifdef CONFIG_DM_DEBUG
  583. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  584. BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
  585. #endif
  586. mempool_destroy(s->tracked_chunk_pool);
  587. __free_exceptions(s);
  588. mempool_destroy(s->pending_pool);
  589. dm_put_device(ti, s->origin);
  590. dm_put_device(ti, s->cow);
  591. kfree(s);
  592. }
  593. /*
  594. * Flush a list of buffers.
  595. */
  596. static void flush_bios(struct bio *bio)
  597. {
  598. struct bio *n;
  599. while (bio) {
  600. n = bio->bi_next;
  601. bio->bi_next = NULL;
  602. generic_make_request(bio);
  603. bio = n;
  604. }
  605. }
  606. static void flush_queued_bios(struct work_struct *work)
  607. {
  608. struct dm_snapshot *s =
  609. container_of(work, struct dm_snapshot, queued_bios_work);
  610. struct bio *queued_bios;
  611. unsigned long flags;
  612. spin_lock_irqsave(&s->pe_lock, flags);
  613. queued_bios = bio_list_get(&s->queued_bios);
  614. spin_unlock_irqrestore(&s->pe_lock, flags);
  615. flush_bios(queued_bios);
  616. }
  617. /*
  618. * Error a list of buffers.
  619. */
  620. static void error_bios(struct bio *bio)
  621. {
  622. struct bio *n;
  623. while (bio) {
  624. n = bio->bi_next;
  625. bio->bi_next = NULL;
  626. bio_io_error(bio);
  627. bio = n;
  628. }
  629. }
  630. static void __invalidate_snapshot(struct dm_snapshot *s, int err)
  631. {
  632. if (!s->valid)
  633. return;
  634. if (err == -EIO)
  635. DMERR("Invalidating snapshot: Error reading/writing.");
  636. else if (err == -ENOMEM)
  637. DMERR("Invalidating snapshot: Unable to allocate exception.");
  638. if (s->store.drop_snapshot)
  639. s->store.drop_snapshot(&s->store);
  640. s->valid = 0;
  641. dm_table_event(s->ti->table);
  642. }
  643. static void get_pending_exception(struct dm_snap_pending_exception *pe)
  644. {
  645. atomic_inc(&pe->ref_count);
  646. }
  647. static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
  648. {
  649. struct dm_snap_pending_exception *primary_pe;
  650. struct bio *origin_bios = NULL;
  651. primary_pe = pe->primary_pe;
  652. /*
  653. * If this pe is involved in a write to the origin and
  654. * it is the last sibling to complete then release
  655. * the bios for the original write to the origin.
  656. */
  657. if (primary_pe &&
  658. atomic_dec_and_test(&primary_pe->ref_count)) {
  659. origin_bios = bio_list_get(&primary_pe->origin_bios);
  660. free_pending_exception(primary_pe);
  661. }
  662. /*
  663. * Free the pe if it's not linked to an origin write or if
  664. * it's not itself a primary pe.
  665. */
  666. if (!primary_pe || primary_pe != pe)
  667. free_pending_exception(pe);
  668. return origin_bios;
  669. }
  670. static void pending_complete(struct dm_snap_pending_exception *pe, int success)
  671. {
  672. struct dm_snap_exception *e;
  673. struct dm_snapshot *s = pe->snap;
  674. struct bio *origin_bios = NULL;
  675. struct bio *snapshot_bios = NULL;
  676. int error = 0;
  677. if (!success) {
  678. /* Read/write error - snapshot is unusable */
  679. down_write(&s->lock);
  680. __invalidate_snapshot(s, -EIO);
  681. error = 1;
  682. goto out;
  683. }
  684. e = alloc_exception();
  685. if (!e) {
  686. down_write(&s->lock);
  687. __invalidate_snapshot(s, -ENOMEM);
  688. error = 1;
  689. goto out;
  690. }
  691. *e = pe->e;
  692. down_write(&s->lock);
  693. if (!s->valid) {
  694. free_exception(e);
  695. error = 1;
  696. goto out;
  697. }
  698. /*
  699. * Check for conflicting reads. This is extremely improbable,
  700. * so yield() is sufficient and there is no need for a wait queue.
  701. */
  702. while (__chunk_is_tracked(s, pe->e.old_chunk))
  703. yield();
  704. /*
  705. * Add a proper exception, and remove the
  706. * in-flight exception from the list.
  707. */
  708. insert_completed_exception(s, e);
  709. out:
  710. remove_exception(&pe->e);
  711. snapshot_bios = bio_list_get(&pe->snapshot_bios);
  712. origin_bios = put_pending_exception(pe);
  713. up_write(&s->lock);
  714. /* Submit any pending write bios */
  715. if (error)
  716. error_bios(snapshot_bios);
  717. else
  718. flush_bios(snapshot_bios);
  719. flush_bios(origin_bios);
  720. }
  721. static void commit_callback(void *context, int success)
  722. {
  723. struct dm_snap_pending_exception *pe = context;
  724. pending_complete(pe, success);
  725. }
  726. /*
  727. * Called when the copy I/O has finished. kcopyd actually runs
  728. * this code so don't block.
  729. */
  730. static void copy_callback(int read_err, unsigned long write_err, void *context)
  731. {
  732. struct dm_snap_pending_exception *pe = context;
  733. struct dm_snapshot *s = pe->snap;
  734. if (read_err || write_err)
  735. pending_complete(pe, 0);
  736. else
  737. /* Update the metadata if we are persistent */
  738. s->store.commit_exception(&s->store, &pe->e, commit_callback,
  739. pe);
  740. }
  741. /*
  742. * Dispatches the copy operation to kcopyd.
  743. */
  744. static void start_copy(struct dm_snap_pending_exception *pe)
  745. {
  746. struct dm_snapshot *s = pe->snap;
  747. struct dm_io_region src, dest;
  748. struct block_device *bdev = s->origin->bdev;
  749. sector_t dev_size;
  750. dev_size = get_dev_size(bdev);
  751. src.bdev = bdev;
  752. src.sector = chunk_to_sector(s, pe->e.old_chunk);
  753. src.count = min(s->chunk_size, dev_size - src.sector);
  754. dest.bdev = s->cow->bdev;
  755. dest.sector = chunk_to_sector(s, pe->e.new_chunk);
  756. dest.count = src.count;
  757. /* Hand over to kcopyd */
  758. dm_kcopyd_copy(s->kcopyd_client,
  759. &src, 1, &dest, 0, copy_callback, pe);
  760. }
  761. /*
  762. * Looks to see if this snapshot already has a pending exception
  763. * for this chunk, otherwise it allocates a new one and inserts
  764. * it into the pending table.
  765. *
  766. * NOTE: a write lock must be held on snap->lock before calling
  767. * this.
  768. */
  769. static struct dm_snap_pending_exception *
  770. __find_pending_exception(struct dm_snapshot *s, struct bio *bio)
  771. {
  772. struct dm_snap_exception *e;
  773. struct dm_snap_pending_exception *pe;
  774. chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
  775. /*
  776. * Is there a pending exception for this already ?
  777. */
  778. e = lookup_exception(&s->pending, chunk);
  779. if (e) {
  780. /* cast the exception to a pending exception */
  781. pe = container_of(e, struct dm_snap_pending_exception, e);
  782. goto out;
  783. }
  784. /*
  785. * Create a new pending exception, we don't want
  786. * to hold the lock while we do this.
  787. */
  788. up_write(&s->lock);
  789. pe = alloc_pending_exception(s);
  790. down_write(&s->lock);
  791. if (!s->valid) {
  792. free_pending_exception(pe);
  793. return NULL;
  794. }
  795. e = lookup_exception(&s->pending, chunk);
  796. if (e) {
  797. free_pending_exception(pe);
  798. pe = container_of(e, struct dm_snap_pending_exception, e);
  799. goto out;
  800. }
  801. pe->e.old_chunk = chunk;
  802. bio_list_init(&pe->origin_bios);
  803. bio_list_init(&pe->snapshot_bios);
  804. pe->primary_pe = NULL;
  805. atomic_set(&pe->ref_count, 0);
  806. pe->started = 0;
  807. if (s->store.prepare_exception(&s->store, &pe->e)) {
  808. free_pending_exception(pe);
  809. return NULL;
  810. }
  811. get_pending_exception(pe);
  812. insert_exception(&s->pending, &pe->e);
  813. out:
  814. return pe;
  815. }
  816. static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
  817. struct bio *bio, chunk_t chunk)
  818. {
  819. bio->bi_bdev = s->cow->bdev;
  820. bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
  821. (chunk - e->old_chunk)) +
  822. (bio->bi_sector & s->chunk_mask);
  823. }
  824. static int snapshot_map(struct dm_target *ti, struct bio *bio,
  825. union map_info *map_context)
  826. {
  827. struct dm_snap_exception *e;
  828. struct dm_snapshot *s = ti->private;
  829. int r = DM_MAPIO_REMAPPED;
  830. chunk_t chunk;
  831. struct dm_snap_pending_exception *pe = NULL;
  832. chunk = sector_to_chunk(s, bio->bi_sector);
  833. /* Full snapshots are not usable */
  834. /* To get here the table must be live so s->active is always set. */
  835. if (!s->valid)
  836. return -EIO;
  837. /* FIXME: should only take write lock if we need
  838. * to copy an exception */
  839. down_write(&s->lock);
  840. if (!s->valid) {
  841. r = -EIO;
  842. goto out_unlock;
  843. }
  844. /* If the block is already remapped - use that, else remap it */
  845. e = lookup_exception(&s->complete, chunk);
  846. if (e) {
  847. remap_exception(s, e, bio, chunk);
  848. goto out_unlock;
  849. }
  850. /*
  851. * Write to snapshot - higher level takes care of RW/RO
  852. * flags so we should only get this if we are
  853. * writeable.
  854. */
  855. if (bio_rw(bio) == WRITE) {
  856. pe = __find_pending_exception(s, bio);
  857. if (!pe) {
  858. __invalidate_snapshot(s, -ENOMEM);
  859. r = -EIO;
  860. goto out_unlock;
  861. }
  862. remap_exception(s, &pe->e, bio, chunk);
  863. bio_list_add(&pe->snapshot_bios, bio);
  864. r = DM_MAPIO_SUBMITTED;
  865. if (!pe->started) {
  866. /* this is protected by snap->lock */
  867. pe->started = 1;
  868. up_write(&s->lock);
  869. start_copy(pe);
  870. goto out;
  871. }
  872. } else {
  873. bio->bi_bdev = s->origin->bdev;
  874. map_context->ptr = track_chunk(s, chunk);
  875. }
  876. out_unlock:
  877. up_write(&s->lock);
  878. out:
  879. return r;
  880. }
  881. static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
  882. int error, union map_info *map_context)
  883. {
  884. struct dm_snapshot *s = ti->private;
  885. struct dm_snap_tracked_chunk *c = map_context->ptr;
  886. if (c)
  887. stop_tracking_chunk(s, c);
  888. return 0;
  889. }
  890. static void snapshot_resume(struct dm_target *ti)
  891. {
  892. struct dm_snapshot *s = ti->private;
  893. down_write(&s->lock);
  894. s->active = 1;
  895. up_write(&s->lock);
  896. }
  897. static int snapshot_status(struct dm_target *ti, status_type_t type,
  898. char *result, unsigned int maxlen)
  899. {
  900. struct dm_snapshot *snap = ti->private;
  901. switch (type) {
  902. case STATUSTYPE_INFO:
  903. if (!snap->valid)
  904. snprintf(result, maxlen, "Invalid");
  905. else {
  906. if (snap->store.fraction_full) {
  907. sector_t numerator, denominator;
  908. snap->store.fraction_full(&snap->store,
  909. &numerator,
  910. &denominator);
  911. snprintf(result, maxlen, "%llu/%llu",
  912. (unsigned long long)numerator,
  913. (unsigned long long)denominator);
  914. }
  915. else
  916. snprintf(result, maxlen, "Unknown");
  917. }
  918. break;
  919. case STATUSTYPE_TABLE:
  920. /*
  921. * kdevname returns a static pointer so we need
  922. * to make private copies if the output is to
  923. * make sense.
  924. */
  925. snprintf(result, maxlen, "%s %s %c %llu",
  926. snap->origin->name, snap->cow->name,
  927. snap->type,
  928. (unsigned long long)snap->chunk_size);
  929. break;
  930. }
  931. return 0;
  932. }
  933. /*-----------------------------------------------------------------
  934. * Origin methods
  935. *---------------------------------------------------------------*/
  936. static int __origin_write(struct list_head *snapshots, struct bio *bio)
  937. {
  938. int r = DM_MAPIO_REMAPPED, first = 0;
  939. struct dm_snapshot *snap;
  940. struct dm_snap_exception *e;
  941. struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
  942. chunk_t chunk;
  943. LIST_HEAD(pe_queue);
  944. /* Do all the snapshots on this origin */
  945. list_for_each_entry (snap, snapshots, list) {
  946. down_write(&snap->lock);
  947. /* Only deal with valid and active snapshots */
  948. if (!snap->valid || !snap->active)
  949. goto next_snapshot;
  950. /* Nothing to do if writing beyond end of snapshot */
  951. if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
  952. goto next_snapshot;
  953. /*
  954. * Remember, different snapshots can have
  955. * different chunk sizes.
  956. */
  957. chunk = sector_to_chunk(snap, bio->bi_sector);
  958. /*
  959. * Check exception table to see if block
  960. * is already remapped in this snapshot
  961. * and trigger an exception if not.
  962. *
  963. * ref_count is initialised to 1 so pending_complete()
  964. * won't destroy the primary_pe while we're inside this loop.
  965. */
  966. e = lookup_exception(&snap->complete, chunk);
  967. if (e)
  968. goto next_snapshot;
  969. pe = __find_pending_exception(snap, bio);
  970. if (!pe) {
  971. __invalidate_snapshot(snap, -ENOMEM);
  972. goto next_snapshot;
  973. }
  974. if (!primary_pe) {
  975. /*
  976. * Either every pe here has same
  977. * primary_pe or none has one yet.
  978. */
  979. if (pe->primary_pe)
  980. primary_pe = pe->primary_pe;
  981. else {
  982. primary_pe = pe;
  983. first = 1;
  984. }
  985. bio_list_add(&primary_pe->origin_bios, bio);
  986. r = DM_MAPIO_SUBMITTED;
  987. }
  988. if (!pe->primary_pe) {
  989. pe->primary_pe = primary_pe;
  990. get_pending_exception(primary_pe);
  991. }
  992. if (!pe->started) {
  993. pe->started = 1;
  994. list_add_tail(&pe->list, &pe_queue);
  995. }
  996. next_snapshot:
  997. up_write(&snap->lock);
  998. }
  999. if (!primary_pe)
  1000. return r;
  1001. /*
  1002. * If this is the first time we're processing this chunk and
  1003. * ref_count is now 1 it means all the pending exceptions
  1004. * got completed while we were in the loop above, so it falls to
  1005. * us here to remove the primary_pe and submit any origin_bios.
  1006. */
  1007. if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
  1008. flush_bios(bio_list_get(&primary_pe->origin_bios));
  1009. free_pending_exception(primary_pe);
  1010. /* If we got here, pe_queue is necessarily empty. */
  1011. return r;
  1012. }
  1013. /*
  1014. * Now that we have a complete pe list we can start the copying.
  1015. */
  1016. list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
  1017. start_copy(pe);
  1018. return r;
  1019. }
  1020. /*
  1021. * Called on a write from the origin driver.
  1022. */
  1023. static int do_origin(struct dm_dev *origin, struct bio *bio)
  1024. {
  1025. struct origin *o;
  1026. int r = DM_MAPIO_REMAPPED;
  1027. down_read(&_origins_lock);
  1028. o = __lookup_origin(origin->bdev);
  1029. if (o)
  1030. r = __origin_write(&o->snapshots, bio);
  1031. up_read(&_origins_lock);
  1032. return r;
  1033. }
  1034. /*
  1035. * Origin: maps a linear range of a device, with hooks for snapshotting.
  1036. */
  1037. /*
  1038. * Construct an origin mapping: <dev_path>
  1039. * The context for an origin is merely a 'struct dm_dev *'
  1040. * pointing to the real device.
  1041. */
  1042. static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1043. {
  1044. int r;
  1045. struct dm_dev *dev;
  1046. if (argc != 1) {
  1047. ti->error = "origin: incorrect number of arguments";
  1048. return -EINVAL;
  1049. }
  1050. r = dm_get_device(ti, argv[0], 0, ti->len,
  1051. dm_table_get_mode(ti->table), &dev);
  1052. if (r) {
  1053. ti->error = "Cannot get target device";
  1054. return r;
  1055. }
  1056. ti->private = dev;
  1057. return 0;
  1058. }
  1059. static void origin_dtr(struct dm_target *ti)
  1060. {
  1061. struct dm_dev *dev = ti->private;
  1062. dm_put_device(ti, dev);
  1063. }
  1064. static int origin_map(struct dm_target *ti, struct bio *bio,
  1065. union map_info *map_context)
  1066. {
  1067. struct dm_dev *dev = ti->private;
  1068. bio->bi_bdev = dev->bdev;
  1069. /* Only tell snapshots if this is a write */
  1070. return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
  1071. }
  1072. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  1073. /*
  1074. * Set the target "split_io" field to the minimum of all the snapshots'
  1075. * chunk sizes.
  1076. */
  1077. static void origin_resume(struct dm_target *ti)
  1078. {
  1079. struct dm_dev *dev = ti->private;
  1080. struct dm_snapshot *snap;
  1081. struct origin *o;
  1082. chunk_t chunk_size = 0;
  1083. down_read(&_origins_lock);
  1084. o = __lookup_origin(dev->bdev);
  1085. if (o)
  1086. list_for_each_entry (snap, &o->snapshots, list)
  1087. chunk_size = min_not_zero(chunk_size, snap->chunk_size);
  1088. up_read(&_origins_lock);
  1089. ti->split_io = chunk_size;
  1090. }
  1091. static int origin_status(struct dm_target *ti, status_type_t type, char *result,
  1092. unsigned int maxlen)
  1093. {
  1094. struct dm_dev *dev = ti->private;
  1095. switch (type) {
  1096. case STATUSTYPE_INFO:
  1097. result[0] = '\0';
  1098. break;
  1099. case STATUSTYPE_TABLE:
  1100. snprintf(result, maxlen, "%s", dev->name);
  1101. break;
  1102. }
  1103. return 0;
  1104. }
  1105. static struct target_type origin_target = {
  1106. .name = "snapshot-origin",
  1107. .version = {1, 6, 0},
  1108. .module = THIS_MODULE,
  1109. .ctr = origin_ctr,
  1110. .dtr = origin_dtr,
  1111. .map = origin_map,
  1112. .resume = origin_resume,
  1113. .status = origin_status,
  1114. };
  1115. static struct target_type snapshot_target = {
  1116. .name = "snapshot",
  1117. .version = {1, 6, 0},
  1118. .module = THIS_MODULE,
  1119. .ctr = snapshot_ctr,
  1120. .dtr = snapshot_dtr,
  1121. .map = snapshot_map,
  1122. .end_io = snapshot_end_io,
  1123. .resume = snapshot_resume,
  1124. .status = snapshot_status,
  1125. };
  1126. static int __init dm_snapshot_init(void)
  1127. {
  1128. int r;
  1129. r = dm_register_target(&snapshot_target);
  1130. if (r) {
  1131. DMERR("snapshot target register failed %d", r);
  1132. return r;
  1133. }
  1134. r = dm_register_target(&origin_target);
  1135. if (r < 0) {
  1136. DMERR("Origin target register failed %d", r);
  1137. goto bad1;
  1138. }
  1139. r = init_origin_hash();
  1140. if (r) {
  1141. DMERR("init_origin_hash failed.");
  1142. goto bad2;
  1143. }
  1144. exception_cache = KMEM_CACHE(dm_snap_exception, 0);
  1145. if (!exception_cache) {
  1146. DMERR("Couldn't create exception cache.");
  1147. r = -ENOMEM;
  1148. goto bad3;
  1149. }
  1150. pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
  1151. if (!pending_cache) {
  1152. DMERR("Couldn't create pending cache.");
  1153. r = -ENOMEM;
  1154. goto bad4;
  1155. }
  1156. tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
  1157. if (!tracked_chunk_cache) {
  1158. DMERR("Couldn't create cache to track chunks in use.");
  1159. r = -ENOMEM;
  1160. goto bad5;
  1161. }
  1162. ksnapd = create_singlethread_workqueue("ksnapd");
  1163. if (!ksnapd) {
  1164. DMERR("Failed to create ksnapd workqueue.");
  1165. r = -ENOMEM;
  1166. goto bad_pending_pool;
  1167. }
  1168. return 0;
  1169. bad_pending_pool:
  1170. kmem_cache_destroy(tracked_chunk_cache);
  1171. bad5:
  1172. kmem_cache_destroy(pending_cache);
  1173. bad4:
  1174. kmem_cache_destroy(exception_cache);
  1175. bad3:
  1176. exit_origin_hash();
  1177. bad2:
  1178. dm_unregister_target(&origin_target);
  1179. bad1:
  1180. dm_unregister_target(&snapshot_target);
  1181. return r;
  1182. }
  1183. static void __exit dm_snapshot_exit(void)
  1184. {
  1185. int r;
  1186. destroy_workqueue(ksnapd);
  1187. r = dm_unregister_target(&snapshot_target);
  1188. if (r)
  1189. DMERR("snapshot unregister failed %d", r);
  1190. r = dm_unregister_target(&origin_target);
  1191. if (r)
  1192. DMERR("origin unregister failed %d", r);
  1193. exit_origin_hash();
  1194. kmem_cache_destroy(pending_cache);
  1195. kmem_cache_destroy(exception_cache);
  1196. kmem_cache_destroy(tracked_chunk_cache);
  1197. }
  1198. /* Module hooks */
  1199. module_init(dm_snapshot_init);
  1200. module_exit(dm_snapshot_exit);
  1201. MODULE_DESCRIPTION(DM_NAME " snapshot target");
  1202. MODULE_AUTHOR("Joe Thornber");
  1203. MODULE_LICENSE("GPL");