dm-snap.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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. s->last_percent = 0;
  486. init_rwsem(&s->lock);
  487. spin_lock_init(&s->pe_lock);
  488. s->ti = ti;
  489. /* Allocate hash table for COW data */
  490. if (init_hash_tables(s)) {
  491. ti->error = "Unable to allocate hash table space";
  492. r = -ENOMEM;
  493. goto bad3;
  494. }
  495. s->store.snap = s;
  496. if (persistent == 'P')
  497. r = dm_create_persistent(&s->store);
  498. else
  499. r = dm_create_transient(&s->store);
  500. if (r) {
  501. ti->error = "Couldn't create exception store";
  502. r = -EINVAL;
  503. goto bad4;
  504. }
  505. r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
  506. if (r) {
  507. ti->error = "Could not create kcopyd client";
  508. goto bad5;
  509. }
  510. s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
  511. if (!s->pending_pool) {
  512. ti->error = "Could not allocate mempool for pending exceptions";
  513. goto bad6;
  514. }
  515. s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
  516. tracked_chunk_cache);
  517. if (!s->tracked_chunk_pool) {
  518. ti->error = "Could not allocate tracked_chunk mempool for "
  519. "tracking reads";
  520. goto bad_tracked_chunk_pool;
  521. }
  522. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  523. INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
  524. spin_lock_init(&s->tracked_chunk_lock);
  525. /* Metadata must only be loaded into one table at once */
  526. r = s->store.read_metadata(&s->store);
  527. if (r < 0) {
  528. ti->error = "Failed to read snapshot metadata";
  529. goto bad_load_and_register;
  530. } else if (r > 0) {
  531. s->valid = 0;
  532. DMWARN("Snapshot is marked invalid.");
  533. }
  534. bio_list_init(&s->queued_bios);
  535. INIT_WORK(&s->queued_bios_work, flush_queued_bios);
  536. /* Add snapshot to the list of snapshots for this origin */
  537. /* Exceptions aren't triggered till snapshot_resume() is called */
  538. if (register_snapshot(s)) {
  539. r = -EINVAL;
  540. ti->error = "Cannot register snapshot origin";
  541. goto bad_load_and_register;
  542. }
  543. ti->private = s;
  544. ti->split_io = s->chunk_size;
  545. return 0;
  546. bad_load_and_register:
  547. mempool_destroy(s->tracked_chunk_pool);
  548. bad_tracked_chunk_pool:
  549. mempool_destroy(s->pending_pool);
  550. bad6:
  551. dm_kcopyd_client_destroy(s->kcopyd_client);
  552. bad5:
  553. s->store.destroy(&s->store);
  554. bad4:
  555. exit_exception_table(&s->pending, pending_cache);
  556. exit_exception_table(&s->complete, exception_cache);
  557. bad3:
  558. dm_put_device(ti, s->cow);
  559. dm_put_device(ti, s->origin);
  560. bad2:
  561. kfree(s);
  562. bad1:
  563. return r;
  564. }
  565. static void __free_exceptions(struct dm_snapshot *s)
  566. {
  567. dm_kcopyd_client_destroy(s->kcopyd_client);
  568. s->kcopyd_client = NULL;
  569. exit_exception_table(&s->pending, pending_cache);
  570. exit_exception_table(&s->complete, exception_cache);
  571. s->store.destroy(&s->store);
  572. }
  573. static void snapshot_dtr(struct dm_target *ti)
  574. {
  575. #ifdef CONFIG_DM_DEBUG
  576. int i;
  577. #endif
  578. struct dm_snapshot *s = ti->private;
  579. flush_workqueue(ksnapd);
  580. /* Prevent further origin writes from using this snapshot. */
  581. /* After this returns there can be no new kcopyd jobs. */
  582. unregister_snapshot(s);
  583. #ifdef CONFIG_DM_DEBUG
  584. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  585. BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
  586. #endif
  587. mempool_destroy(s->tracked_chunk_pool);
  588. __free_exceptions(s);
  589. mempool_destroy(s->pending_pool);
  590. dm_put_device(ti, s->origin);
  591. dm_put_device(ti, s->cow);
  592. kfree(s);
  593. }
  594. /*
  595. * Flush a list of buffers.
  596. */
  597. static void flush_bios(struct bio *bio)
  598. {
  599. struct bio *n;
  600. while (bio) {
  601. n = bio->bi_next;
  602. bio->bi_next = NULL;
  603. generic_make_request(bio);
  604. bio = n;
  605. }
  606. }
  607. static void flush_queued_bios(struct work_struct *work)
  608. {
  609. struct dm_snapshot *s =
  610. container_of(work, struct dm_snapshot, queued_bios_work);
  611. struct bio *queued_bios;
  612. unsigned long flags;
  613. spin_lock_irqsave(&s->pe_lock, flags);
  614. queued_bios = bio_list_get(&s->queued_bios);
  615. spin_unlock_irqrestore(&s->pe_lock, flags);
  616. flush_bios(queued_bios);
  617. }
  618. /*
  619. * Error a list of buffers.
  620. */
  621. static void error_bios(struct bio *bio)
  622. {
  623. struct bio *n;
  624. while (bio) {
  625. n = bio->bi_next;
  626. bio->bi_next = NULL;
  627. bio_io_error(bio);
  628. bio = n;
  629. }
  630. }
  631. static void __invalidate_snapshot(struct dm_snapshot *s, int err)
  632. {
  633. if (!s->valid)
  634. return;
  635. if (err == -EIO)
  636. DMERR("Invalidating snapshot: Error reading/writing.");
  637. else if (err == -ENOMEM)
  638. DMERR("Invalidating snapshot: Unable to allocate exception.");
  639. if (s->store.drop_snapshot)
  640. s->store.drop_snapshot(&s->store);
  641. s->valid = 0;
  642. dm_table_event(s->ti->table);
  643. }
  644. static void get_pending_exception(struct dm_snap_pending_exception *pe)
  645. {
  646. atomic_inc(&pe->ref_count);
  647. }
  648. static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
  649. {
  650. struct dm_snap_pending_exception *primary_pe;
  651. struct bio *origin_bios = NULL;
  652. primary_pe = pe->primary_pe;
  653. /*
  654. * If this pe is involved in a write to the origin and
  655. * it is the last sibling to complete then release
  656. * the bios for the original write to the origin.
  657. */
  658. if (primary_pe &&
  659. atomic_dec_and_test(&primary_pe->ref_count)) {
  660. origin_bios = bio_list_get(&primary_pe->origin_bios);
  661. free_pending_exception(primary_pe);
  662. }
  663. /*
  664. * Free the pe if it's not linked to an origin write or if
  665. * it's not itself a primary pe.
  666. */
  667. if (!primary_pe || primary_pe != pe)
  668. free_pending_exception(pe);
  669. return origin_bios;
  670. }
  671. static void pending_complete(struct dm_snap_pending_exception *pe, int success)
  672. {
  673. struct dm_snap_exception *e;
  674. struct dm_snapshot *s = pe->snap;
  675. struct bio *origin_bios = NULL;
  676. struct bio *snapshot_bios = NULL;
  677. int error = 0;
  678. if (!success) {
  679. /* Read/write error - snapshot is unusable */
  680. down_write(&s->lock);
  681. __invalidate_snapshot(s, -EIO);
  682. error = 1;
  683. goto out;
  684. }
  685. e = alloc_exception();
  686. if (!e) {
  687. down_write(&s->lock);
  688. __invalidate_snapshot(s, -ENOMEM);
  689. error = 1;
  690. goto out;
  691. }
  692. *e = pe->e;
  693. down_write(&s->lock);
  694. if (!s->valid) {
  695. free_exception(e);
  696. error = 1;
  697. goto out;
  698. }
  699. /*
  700. * Check for conflicting reads. This is extremely improbable,
  701. * so yield() is sufficient and there is no need for a wait queue.
  702. */
  703. while (__chunk_is_tracked(s, pe->e.old_chunk))
  704. yield();
  705. /*
  706. * Add a proper exception, and remove the
  707. * in-flight exception from the list.
  708. */
  709. insert_completed_exception(s, e);
  710. out:
  711. remove_exception(&pe->e);
  712. snapshot_bios = bio_list_get(&pe->snapshot_bios);
  713. origin_bios = put_pending_exception(pe);
  714. up_write(&s->lock);
  715. /* Submit any pending write bios */
  716. if (error)
  717. error_bios(snapshot_bios);
  718. else
  719. flush_bios(snapshot_bios);
  720. flush_bios(origin_bios);
  721. }
  722. static void commit_callback(void *context, int success)
  723. {
  724. struct dm_snap_pending_exception *pe = context;
  725. pending_complete(pe, success);
  726. }
  727. /*
  728. * Called when the copy I/O has finished. kcopyd actually runs
  729. * this code so don't block.
  730. */
  731. static void copy_callback(int read_err, unsigned long write_err, void *context)
  732. {
  733. struct dm_snap_pending_exception *pe = context;
  734. struct dm_snapshot *s = pe->snap;
  735. if (read_err || write_err)
  736. pending_complete(pe, 0);
  737. else
  738. /* Update the metadata if we are persistent */
  739. s->store.commit_exception(&s->store, &pe->e, commit_callback,
  740. pe);
  741. }
  742. /*
  743. * Dispatches the copy operation to kcopyd.
  744. */
  745. static void start_copy(struct dm_snap_pending_exception *pe)
  746. {
  747. struct dm_snapshot *s = pe->snap;
  748. struct dm_io_region src, dest;
  749. struct block_device *bdev = s->origin->bdev;
  750. sector_t dev_size;
  751. dev_size = get_dev_size(bdev);
  752. src.bdev = bdev;
  753. src.sector = chunk_to_sector(s, pe->e.old_chunk);
  754. src.count = min(s->chunk_size, dev_size - src.sector);
  755. dest.bdev = s->cow->bdev;
  756. dest.sector = chunk_to_sector(s, pe->e.new_chunk);
  757. dest.count = src.count;
  758. /* Hand over to kcopyd */
  759. dm_kcopyd_copy(s->kcopyd_client,
  760. &src, 1, &dest, 0, copy_callback, pe);
  761. }
  762. /*
  763. * Looks to see if this snapshot already has a pending exception
  764. * for this chunk, otherwise it allocates a new one and inserts
  765. * it into the pending table.
  766. *
  767. * NOTE: a write lock must be held on snap->lock before calling
  768. * this.
  769. */
  770. static struct dm_snap_pending_exception *
  771. __find_pending_exception(struct dm_snapshot *s, struct bio *bio)
  772. {
  773. struct dm_snap_exception *e;
  774. struct dm_snap_pending_exception *pe;
  775. chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
  776. /*
  777. * Is there a pending exception for this already ?
  778. */
  779. e = lookup_exception(&s->pending, chunk);
  780. if (e) {
  781. /* cast the exception to a pending exception */
  782. pe = container_of(e, struct dm_snap_pending_exception, e);
  783. goto out;
  784. }
  785. /*
  786. * Create a new pending exception, we don't want
  787. * to hold the lock while we do this.
  788. */
  789. up_write(&s->lock);
  790. pe = alloc_pending_exception(s);
  791. down_write(&s->lock);
  792. if (!s->valid) {
  793. free_pending_exception(pe);
  794. return NULL;
  795. }
  796. e = lookup_exception(&s->pending, chunk);
  797. if (e) {
  798. free_pending_exception(pe);
  799. pe = container_of(e, struct dm_snap_pending_exception, e);
  800. goto out;
  801. }
  802. pe->e.old_chunk = chunk;
  803. bio_list_init(&pe->origin_bios);
  804. bio_list_init(&pe->snapshot_bios);
  805. pe->primary_pe = NULL;
  806. atomic_set(&pe->ref_count, 0);
  807. pe->started = 0;
  808. if (s->store.prepare_exception(&s->store, &pe->e)) {
  809. free_pending_exception(pe);
  810. return NULL;
  811. }
  812. get_pending_exception(pe);
  813. insert_exception(&s->pending, &pe->e);
  814. out:
  815. return pe;
  816. }
  817. static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
  818. struct bio *bio, chunk_t chunk)
  819. {
  820. bio->bi_bdev = s->cow->bdev;
  821. bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
  822. (chunk - e->old_chunk)) +
  823. (bio->bi_sector & s->chunk_mask);
  824. }
  825. static int snapshot_map(struct dm_target *ti, struct bio *bio,
  826. union map_info *map_context)
  827. {
  828. struct dm_snap_exception *e;
  829. struct dm_snapshot *s = ti->private;
  830. int r = DM_MAPIO_REMAPPED;
  831. chunk_t chunk;
  832. struct dm_snap_pending_exception *pe = NULL;
  833. chunk = sector_to_chunk(s, bio->bi_sector);
  834. /* Full snapshots are not usable */
  835. /* To get here the table must be live so s->active is always set. */
  836. if (!s->valid)
  837. return -EIO;
  838. /* FIXME: should only take write lock if we need
  839. * to copy an exception */
  840. down_write(&s->lock);
  841. if (!s->valid) {
  842. r = -EIO;
  843. goto out_unlock;
  844. }
  845. /* If the block is already remapped - use that, else remap it */
  846. e = lookup_exception(&s->complete, chunk);
  847. if (e) {
  848. remap_exception(s, e, bio, chunk);
  849. goto out_unlock;
  850. }
  851. /*
  852. * Write to snapshot - higher level takes care of RW/RO
  853. * flags so we should only get this if we are
  854. * writeable.
  855. */
  856. if (bio_rw(bio) == WRITE) {
  857. pe = __find_pending_exception(s, bio);
  858. if (!pe) {
  859. __invalidate_snapshot(s, -ENOMEM);
  860. r = -EIO;
  861. goto out_unlock;
  862. }
  863. remap_exception(s, &pe->e, bio, chunk);
  864. bio_list_add(&pe->snapshot_bios, bio);
  865. r = DM_MAPIO_SUBMITTED;
  866. if (!pe->started) {
  867. /* this is protected by snap->lock */
  868. pe->started = 1;
  869. up_write(&s->lock);
  870. start_copy(pe);
  871. goto out;
  872. }
  873. } else {
  874. bio->bi_bdev = s->origin->bdev;
  875. map_context->ptr = track_chunk(s, chunk);
  876. }
  877. out_unlock:
  878. up_write(&s->lock);
  879. out:
  880. return r;
  881. }
  882. static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
  883. int error, union map_info *map_context)
  884. {
  885. struct dm_snapshot *s = ti->private;
  886. struct dm_snap_tracked_chunk *c = map_context->ptr;
  887. if (c)
  888. stop_tracking_chunk(s, c);
  889. return 0;
  890. }
  891. static void snapshot_resume(struct dm_target *ti)
  892. {
  893. struct dm_snapshot *s = ti->private;
  894. down_write(&s->lock);
  895. s->active = 1;
  896. up_write(&s->lock);
  897. }
  898. static int snapshot_status(struct dm_target *ti, status_type_t type,
  899. char *result, unsigned int maxlen)
  900. {
  901. struct dm_snapshot *snap = ti->private;
  902. switch (type) {
  903. case STATUSTYPE_INFO:
  904. if (!snap->valid)
  905. snprintf(result, maxlen, "Invalid");
  906. else {
  907. if (snap->store.fraction_full) {
  908. sector_t numerator, denominator;
  909. snap->store.fraction_full(&snap->store,
  910. &numerator,
  911. &denominator);
  912. snprintf(result, maxlen, "%llu/%llu",
  913. (unsigned long long)numerator,
  914. (unsigned long long)denominator);
  915. }
  916. else
  917. snprintf(result, maxlen, "Unknown");
  918. }
  919. break;
  920. case STATUSTYPE_TABLE:
  921. /*
  922. * kdevname returns a static pointer so we need
  923. * to make private copies if the output is to
  924. * make sense.
  925. */
  926. snprintf(result, maxlen, "%s %s %c %llu",
  927. snap->origin->name, snap->cow->name,
  928. snap->type,
  929. (unsigned long long)snap->chunk_size);
  930. break;
  931. }
  932. return 0;
  933. }
  934. /*-----------------------------------------------------------------
  935. * Origin methods
  936. *---------------------------------------------------------------*/
  937. static int __origin_write(struct list_head *snapshots, struct bio *bio)
  938. {
  939. int r = DM_MAPIO_REMAPPED, first = 0;
  940. struct dm_snapshot *snap;
  941. struct dm_snap_exception *e;
  942. struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
  943. chunk_t chunk;
  944. LIST_HEAD(pe_queue);
  945. /* Do all the snapshots on this origin */
  946. list_for_each_entry (snap, snapshots, list) {
  947. down_write(&snap->lock);
  948. /* Only deal with valid and active snapshots */
  949. if (!snap->valid || !snap->active)
  950. goto next_snapshot;
  951. /* Nothing to do if writing beyond end of snapshot */
  952. if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
  953. goto next_snapshot;
  954. /*
  955. * Remember, different snapshots can have
  956. * different chunk sizes.
  957. */
  958. chunk = sector_to_chunk(snap, bio->bi_sector);
  959. /*
  960. * Check exception table to see if block
  961. * is already remapped in this snapshot
  962. * and trigger an exception if not.
  963. *
  964. * ref_count is initialised to 1 so pending_complete()
  965. * won't destroy the primary_pe while we're inside this loop.
  966. */
  967. e = lookup_exception(&snap->complete, chunk);
  968. if (e)
  969. goto next_snapshot;
  970. pe = __find_pending_exception(snap, bio);
  971. if (!pe) {
  972. __invalidate_snapshot(snap, -ENOMEM);
  973. goto next_snapshot;
  974. }
  975. if (!primary_pe) {
  976. /*
  977. * Either every pe here has same
  978. * primary_pe or none has one yet.
  979. */
  980. if (pe->primary_pe)
  981. primary_pe = pe->primary_pe;
  982. else {
  983. primary_pe = pe;
  984. first = 1;
  985. }
  986. bio_list_add(&primary_pe->origin_bios, bio);
  987. r = DM_MAPIO_SUBMITTED;
  988. }
  989. if (!pe->primary_pe) {
  990. pe->primary_pe = primary_pe;
  991. get_pending_exception(primary_pe);
  992. }
  993. if (!pe->started) {
  994. pe->started = 1;
  995. list_add_tail(&pe->list, &pe_queue);
  996. }
  997. next_snapshot:
  998. up_write(&snap->lock);
  999. }
  1000. if (!primary_pe)
  1001. return r;
  1002. /*
  1003. * If this is the first time we're processing this chunk and
  1004. * ref_count is now 1 it means all the pending exceptions
  1005. * got completed while we were in the loop above, so it falls to
  1006. * us here to remove the primary_pe and submit any origin_bios.
  1007. */
  1008. if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
  1009. flush_bios(bio_list_get(&primary_pe->origin_bios));
  1010. free_pending_exception(primary_pe);
  1011. /* If we got here, pe_queue is necessarily empty. */
  1012. return r;
  1013. }
  1014. /*
  1015. * Now that we have a complete pe list we can start the copying.
  1016. */
  1017. list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
  1018. start_copy(pe);
  1019. return r;
  1020. }
  1021. /*
  1022. * Called on a write from the origin driver.
  1023. */
  1024. static int do_origin(struct dm_dev *origin, struct bio *bio)
  1025. {
  1026. struct origin *o;
  1027. int r = DM_MAPIO_REMAPPED;
  1028. down_read(&_origins_lock);
  1029. o = __lookup_origin(origin->bdev);
  1030. if (o)
  1031. r = __origin_write(&o->snapshots, bio);
  1032. up_read(&_origins_lock);
  1033. return r;
  1034. }
  1035. /*
  1036. * Origin: maps a linear range of a device, with hooks for snapshotting.
  1037. */
  1038. /*
  1039. * Construct an origin mapping: <dev_path>
  1040. * The context for an origin is merely a 'struct dm_dev *'
  1041. * pointing to the real device.
  1042. */
  1043. static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1044. {
  1045. int r;
  1046. struct dm_dev *dev;
  1047. if (argc != 1) {
  1048. ti->error = "origin: incorrect number of arguments";
  1049. return -EINVAL;
  1050. }
  1051. r = dm_get_device(ti, argv[0], 0, ti->len,
  1052. dm_table_get_mode(ti->table), &dev);
  1053. if (r) {
  1054. ti->error = "Cannot get target device";
  1055. return r;
  1056. }
  1057. ti->private = dev;
  1058. return 0;
  1059. }
  1060. static void origin_dtr(struct dm_target *ti)
  1061. {
  1062. struct dm_dev *dev = ti->private;
  1063. dm_put_device(ti, dev);
  1064. }
  1065. static int origin_map(struct dm_target *ti, struct bio *bio,
  1066. union map_info *map_context)
  1067. {
  1068. struct dm_dev *dev = ti->private;
  1069. bio->bi_bdev = dev->bdev;
  1070. /* Only tell snapshots if this is a write */
  1071. return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
  1072. }
  1073. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  1074. /*
  1075. * Set the target "split_io" field to the minimum of all the snapshots'
  1076. * chunk sizes.
  1077. */
  1078. static void origin_resume(struct dm_target *ti)
  1079. {
  1080. struct dm_dev *dev = ti->private;
  1081. struct dm_snapshot *snap;
  1082. struct origin *o;
  1083. chunk_t chunk_size = 0;
  1084. down_read(&_origins_lock);
  1085. o = __lookup_origin(dev->bdev);
  1086. if (o)
  1087. list_for_each_entry (snap, &o->snapshots, list)
  1088. chunk_size = min_not_zero(chunk_size, snap->chunk_size);
  1089. up_read(&_origins_lock);
  1090. ti->split_io = chunk_size;
  1091. }
  1092. static int origin_status(struct dm_target *ti, status_type_t type, char *result,
  1093. unsigned int maxlen)
  1094. {
  1095. struct dm_dev *dev = ti->private;
  1096. switch (type) {
  1097. case STATUSTYPE_INFO:
  1098. result[0] = '\0';
  1099. break;
  1100. case STATUSTYPE_TABLE:
  1101. snprintf(result, maxlen, "%s", dev->name);
  1102. break;
  1103. }
  1104. return 0;
  1105. }
  1106. static struct target_type origin_target = {
  1107. .name = "snapshot-origin",
  1108. .version = {1, 6, 0},
  1109. .module = THIS_MODULE,
  1110. .ctr = origin_ctr,
  1111. .dtr = origin_dtr,
  1112. .map = origin_map,
  1113. .resume = origin_resume,
  1114. .status = origin_status,
  1115. };
  1116. static struct target_type snapshot_target = {
  1117. .name = "snapshot",
  1118. .version = {1, 6, 0},
  1119. .module = THIS_MODULE,
  1120. .ctr = snapshot_ctr,
  1121. .dtr = snapshot_dtr,
  1122. .map = snapshot_map,
  1123. .end_io = snapshot_end_io,
  1124. .resume = snapshot_resume,
  1125. .status = snapshot_status,
  1126. };
  1127. static int __init dm_snapshot_init(void)
  1128. {
  1129. int r;
  1130. r = dm_register_target(&snapshot_target);
  1131. if (r) {
  1132. DMERR("snapshot target register failed %d", r);
  1133. return r;
  1134. }
  1135. r = dm_register_target(&origin_target);
  1136. if (r < 0) {
  1137. DMERR("Origin target register failed %d", r);
  1138. goto bad1;
  1139. }
  1140. r = init_origin_hash();
  1141. if (r) {
  1142. DMERR("init_origin_hash failed.");
  1143. goto bad2;
  1144. }
  1145. exception_cache = KMEM_CACHE(dm_snap_exception, 0);
  1146. if (!exception_cache) {
  1147. DMERR("Couldn't create exception cache.");
  1148. r = -ENOMEM;
  1149. goto bad3;
  1150. }
  1151. pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
  1152. if (!pending_cache) {
  1153. DMERR("Couldn't create pending cache.");
  1154. r = -ENOMEM;
  1155. goto bad4;
  1156. }
  1157. tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
  1158. if (!tracked_chunk_cache) {
  1159. DMERR("Couldn't create cache to track chunks in use.");
  1160. r = -ENOMEM;
  1161. goto bad5;
  1162. }
  1163. ksnapd = create_singlethread_workqueue("ksnapd");
  1164. if (!ksnapd) {
  1165. DMERR("Failed to create ksnapd workqueue.");
  1166. r = -ENOMEM;
  1167. goto bad_pending_pool;
  1168. }
  1169. return 0;
  1170. bad_pending_pool:
  1171. kmem_cache_destroy(tracked_chunk_cache);
  1172. bad5:
  1173. kmem_cache_destroy(pending_cache);
  1174. bad4:
  1175. kmem_cache_destroy(exception_cache);
  1176. bad3:
  1177. exit_origin_hash();
  1178. bad2:
  1179. dm_unregister_target(&origin_target);
  1180. bad1:
  1181. dm_unregister_target(&snapshot_target);
  1182. return r;
  1183. }
  1184. static void __exit dm_snapshot_exit(void)
  1185. {
  1186. int r;
  1187. destroy_workqueue(ksnapd);
  1188. r = dm_unregister_target(&snapshot_target);
  1189. if (r)
  1190. DMERR("snapshot unregister failed %d", r);
  1191. r = dm_unregister_target(&origin_target);
  1192. if (r)
  1193. DMERR("origin unregister failed %d", r);
  1194. exit_origin_hash();
  1195. kmem_cache_destroy(pending_cache);
  1196. kmem_cache_destroy(exception_cache);
  1197. kmem_cache_destroy(tracked_chunk_cache);
  1198. }
  1199. /* Module hooks */
  1200. module_init(dm_snapshot_init);
  1201. module_exit(dm_snapshot_exit);
  1202. MODULE_DESCRIPTION(DM_NAME " snapshot target");
  1203. MODULE_AUTHOR("Joe Thornber");
  1204. MODULE_LICENSE("GPL");