dm-snap.c 28 KB

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