dm-snap.c 28 KB

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