dm-snap.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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. 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("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 int set_chunk_size(struct dm_snapshot *s, const char *chunk_size_arg,
  317. char **error)
  318. {
  319. unsigned long chunk_size;
  320. char *value;
  321. chunk_size = simple_strtoul(chunk_size_arg, &value, 10);
  322. if (*chunk_size_arg == '\0' || *value != '\0') {
  323. *error = "Invalid chunk size";
  324. return -EINVAL;
  325. }
  326. if (!chunk_size) {
  327. s->chunk_size = s->chunk_mask = s->chunk_shift = 0;
  328. return 0;
  329. }
  330. /*
  331. * Chunk size must be multiple of page size. Silently
  332. * round up if it's not.
  333. */
  334. chunk_size = round_up(chunk_size, PAGE_SIZE >> 9);
  335. /* Check chunk_size is a power of 2 */
  336. if (chunk_size & (chunk_size - 1)) {
  337. *error = "Chunk size is not a power of 2";
  338. return -EINVAL;
  339. }
  340. /* Validate the chunk size against the device block size */
  341. if (chunk_size % (bdev_hardsect_size(s->cow->bdev) >> 9)) {
  342. *error = "Chunk size is not a multiple of device blocksize";
  343. return -EINVAL;
  344. }
  345. s->chunk_size = chunk_size;
  346. s->chunk_mask = chunk_size - 1;
  347. s->chunk_shift = ffs(chunk_size) - 1;
  348. return 0;
  349. }
  350. /*
  351. * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
  352. */
  353. static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  354. {
  355. struct dm_snapshot *s;
  356. int r = -EINVAL;
  357. char persistent;
  358. char *origin_path;
  359. char *cow_path;
  360. if (argc != 4) {
  361. ti->error = "requires exactly 4 arguments";
  362. r = -EINVAL;
  363. goto bad1;
  364. }
  365. origin_path = argv[0];
  366. cow_path = argv[1];
  367. persistent = toupper(*argv[2]);
  368. if (persistent != 'P' && persistent != 'N') {
  369. ti->error = "Persistent flag is not P or N";
  370. r = -EINVAL;
  371. goto bad1;
  372. }
  373. s = kmalloc(sizeof(*s), GFP_KERNEL);
  374. if (s == NULL) {
  375. ti->error = "Cannot allocate snapshot context private "
  376. "structure";
  377. r = -ENOMEM;
  378. goto bad1;
  379. }
  380. r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
  381. if (r) {
  382. ti->error = "Cannot get origin device";
  383. goto bad2;
  384. }
  385. r = dm_get_device(ti, cow_path, 0, 0,
  386. FMODE_READ | FMODE_WRITE, &s->cow);
  387. if (r) {
  388. dm_put_device(ti, s->origin);
  389. ti->error = "Cannot get COW device";
  390. goto bad2;
  391. }
  392. r = set_chunk_size(s, argv[3], &ti->error);
  393. if (r)
  394. goto bad3;
  395. s->type = persistent;
  396. s->valid = 1;
  397. s->active = 0;
  398. s->last_percent = 0;
  399. init_rwsem(&s->lock);
  400. s->table = ti->table;
  401. /* Allocate hash table for COW data */
  402. if (init_hash_tables(s)) {
  403. ti->error = "Unable to allocate hash table space";
  404. r = -ENOMEM;
  405. goto bad3;
  406. }
  407. s->store.snap = s;
  408. if (persistent == 'P')
  409. r = dm_create_persistent(&s->store);
  410. else
  411. r = dm_create_transient(&s->store);
  412. if (r) {
  413. ti->error = "Couldn't create exception store";
  414. r = -EINVAL;
  415. goto bad4;
  416. }
  417. r = kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
  418. if (r) {
  419. ti->error = "Could not create kcopyd client";
  420. goto bad5;
  421. }
  422. /* Metadata must only be loaded into one table at once */
  423. r = s->store.read_metadata(&s->store);
  424. if (r) {
  425. ti->error = "Failed to read snapshot metadata";
  426. goto bad6;
  427. }
  428. /* Add snapshot to the list of snapshots for this origin */
  429. /* Exceptions aren't triggered till snapshot_resume() is called */
  430. if (register_snapshot(s)) {
  431. r = -EINVAL;
  432. ti->error = "Cannot register snapshot origin";
  433. goto bad6;
  434. }
  435. ti->private = s;
  436. ti->split_io = s->chunk_size;
  437. return 0;
  438. bad6:
  439. kcopyd_client_destroy(s->kcopyd_client);
  440. bad5:
  441. s->store.destroy(&s->store);
  442. bad4:
  443. exit_exception_table(&s->pending, pending_cache);
  444. exit_exception_table(&s->complete, exception_cache);
  445. bad3:
  446. dm_put_device(ti, s->cow);
  447. dm_put_device(ti, s->origin);
  448. bad2:
  449. kfree(s);
  450. bad1:
  451. return r;
  452. }
  453. static void snapshot_dtr(struct dm_target *ti)
  454. {
  455. struct dm_snapshot *s = (struct dm_snapshot *) ti->private;
  456. /* Prevent further origin writes from using this snapshot. */
  457. /* After this returns there can be no new kcopyd jobs. */
  458. unregister_snapshot(s);
  459. kcopyd_client_destroy(s->kcopyd_client);
  460. exit_exception_table(&s->pending, pending_cache);
  461. exit_exception_table(&s->complete, exception_cache);
  462. /* Deallocate memory used */
  463. s->store.destroy(&s->store);
  464. dm_put_device(ti, s->origin);
  465. dm_put_device(ti, s->cow);
  466. kfree(s);
  467. }
  468. /*
  469. * Flush a list of buffers.
  470. */
  471. static void flush_bios(struct bio *bio)
  472. {
  473. struct bio *n;
  474. while (bio) {
  475. n = bio->bi_next;
  476. bio->bi_next = NULL;
  477. generic_make_request(bio);
  478. bio = n;
  479. }
  480. }
  481. /*
  482. * Error a list of buffers.
  483. */
  484. static void error_bios(struct bio *bio)
  485. {
  486. struct bio *n;
  487. while (bio) {
  488. n = bio->bi_next;
  489. bio->bi_next = NULL;
  490. bio_io_error(bio, bio->bi_size);
  491. bio = n;
  492. }
  493. }
  494. static inline void error_snapshot_bios(struct pending_exception *pe)
  495. {
  496. error_bios(bio_list_get(&pe->snapshot_bios));
  497. }
  498. static struct bio *__flush_bios(struct pending_exception *pe)
  499. {
  500. /*
  501. * If this pe is involved in a write to the origin and
  502. * it is the last sibling to complete then release
  503. * the bios for the original write to the origin.
  504. */
  505. if (pe->primary_pe &&
  506. atomic_dec_and_test(&pe->primary_pe->sibling_count))
  507. return bio_list_get(&pe->primary_pe->origin_bios);
  508. return NULL;
  509. }
  510. static void __invalidate_snapshot(struct dm_snapshot *s,
  511. struct pending_exception *pe, int err)
  512. {
  513. if (!s->valid)
  514. return;
  515. if (err == -EIO)
  516. DMERR("Invalidating snapshot: Error reading/writing.");
  517. else if (err == -ENOMEM)
  518. DMERR("Invalidating snapshot: Unable to allocate exception.");
  519. if (pe)
  520. remove_exception(&pe->e);
  521. if (s->store.drop_snapshot)
  522. s->store.drop_snapshot(&s->store);
  523. s->valid = 0;
  524. dm_table_event(s->table);
  525. }
  526. static void pending_complete(struct pending_exception *pe, int success)
  527. {
  528. struct exception *e;
  529. struct pending_exception *primary_pe;
  530. struct dm_snapshot *s = pe->snap;
  531. struct bio *flush = NULL;
  532. if (!success) {
  533. /* Read/write error - snapshot is unusable */
  534. down_write(&s->lock);
  535. __invalidate_snapshot(s, pe, -EIO);
  536. flush = __flush_bios(pe);
  537. up_write(&s->lock);
  538. error_snapshot_bios(pe);
  539. goto out;
  540. }
  541. e = alloc_exception();
  542. if (!e) {
  543. down_write(&s->lock);
  544. __invalidate_snapshot(s, pe, -ENOMEM);
  545. flush = __flush_bios(pe);
  546. up_write(&s->lock);
  547. error_snapshot_bios(pe);
  548. goto out;
  549. }
  550. *e = pe->e;
  551. /*
  552. * Add a proper exception, and remove the
  553. * in-flight exception from the list.
  554. */
  555. down_write(&s->lock);
  556. if (!s->valid) {
  557. flush = __flush_bios(pe);
  558. up_write(&s->lock);
  559. free_exception(e);
  560. error_snapshot_bios(pe);
  561. goto out;
  562. }
  563. insert_exception(&s->complete, e);
  564. remove_exception(&pe->e);
  565. flush = __flush_bios(pe);
  566. up_write(&s->lock);
  567. /* Submit any pending write bios */
  568. flush_bios(bio_list_get(&pe->snapshot_bios));
  569. out:
  570. primary_pe = pe->primary_pe;
  571. /*
  572. * Free the pe if it's not linked to an origin write or if
  573. * it's not itself a primary pe.
  574. */
  575. if (!primary_pe || primary_pe != pe)
  576. free_pending_exception(pe);
  577. /*
  578. * Free the primary pe if nothing references it.
  579. */
  580. if (primary_pe && !atomic_read(&primary_pe->sibling_count))
  581. free_pending_exception(primary_pe);
  582. if (flush)
  583. flush_bios(flush);
  584. }
  585. static void commit_callback(void *context, int success)
  586. {
  587. struct pending_exception *pe = (struct pending_exception *) context;
  588. pending_complete(pe, success);
  589. }
  590. /*
  591. * Called when the copy I/O has finished. kcopyd actually runs
  592. * this code so don't block.
  593. */
  594. static void copy_callback(int read_err, unsigned int write_err, void *context)
  595. {
  596. struct pending_exception *pe = (struct pending_exception *) context;
  597. struct dm_snapshot *s = pe->snap;
  598. if (read_err || write_err)
  599. pending_complete(pe, 0);
  600. else
  601. /* Update the metadata if we are persistent */
  602. s->store.commit_exception(&s->store, &pe->e, commit_callback,
  603. pe);
  604. }
  605. /*
  606. * Dispatches the copy operation to kcopyd.
  607. */
  608. static void start_copy(struct pending_exception *pe)
  609. {
  610. struct dm_snapshot *s = pe->snap;
  611. struct io_region src, dest;
  612. struct block_device *bdev = s->origin->bdev;
  613. sector_t dev_size;
  614. dev_size = get_dev_size(bdev);
  615. src.bdev = bdev;
  616. src.sector = chunk_to_sector(s, pe->e.old_chunk);
  617. src.count = min(s->chunk_size, dev_size - src.sector);
  618. dest.bdev = s->cow->bdev;
  619. dest.sector = chunk_to_sector(s, pe->e.new_chunk);
  620. dest.count = src.count;
  621. /* Hand over to kcopyd */
  622. kcopyd_copy(s->kcopyd_client,
  623. &src, 1, &dest, 0, copy_callback, pe);
  624. }
  625. /*
  626. * Looks to see if this snapshot already has a pending exception
  627. * for this chunk, otherwise it allocates a new one and inserts
  628. * it into the pending table.
  629. *
  630. * NOTE: a write lock must be held on snap->lock before calling
  631. * this.
  632. */
  633. static struct pending_exception *
  634. __find_pending_exception(struct dm_snapshot *s, struct bio *bio)
  635. {
  636. struct exception *e;
  637. struct pending_exception *pe;
  638. chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
  639. /*
  640. * Is there a pending exception for this already ?
  641. */
  642. e = lookup_exception(&s->pending, chunk);
  643. if (e) {
  644. /* cast the exception to a pending exception */
  645. pe = container_of(e, struct pending_exception, e);
  646. goto out;
  647. }
  648. /*
  649. * Create a new pending exception, we don't want
  650. * to hold the lock while we do this.
  651. */
  652. up_write(&s->lock);
  653. pe = alloc_pending_exception();
  654. down_write(&s->lock);
  655. if (!s->valid) {
  656. free_pending_exception(pe);
  657. return NULL;
  658. }
  659. e = lookup_exception(&s->pending, chunk);
  660. if (e) {
  661. free_pending_exception(pe);
  662. pe = container_of(e, struct pending_exception, e);
  663. goto out;
  664. }
  665. pe->e.old_chunk = chunk;
  666. bio_list_init(&pe->origin_bios);
  667. bio_list_init(&pe->snapshot_bios);
  668. pe->primary_pe = NULL;
  669. atomic_set(&pe->sibling_count, 1);
  670. pe->snap = s;
  671. pe->started = 0;
  672. if (s->store.prepare_exception(&s->store, &pe->e)) {
  673. free_pending_exception(pe);
  674. return NULL;
  675. }
  676. insert_exception(&s->pending, &pe->e);
  677. out:
  678. return pe;
  679. }
  680. static inline void remap_exception(struct dm_snapshot *s, struct exception *e,
  681. struct bio *bio)
  682. {
  683. bio->bi_bdev = s->cow->bdev;
  684. bio->bi_sector = chunk_to_sector(s, e->new_chunk) +
  685. (bio->bi_sector & s->chunk_mask);
  686. }
  687. static int snapshot_map(struct dm_target *ti, struct bio *bio,
  688. union map_info *map_context)
  689. {
  690. struct exception *e;
  691. struct dm_snapshot *s = (struct dm_snapshot *) ti->private;
  692. int r = 1;
  693. chunk_t chunk;
  694. struct pending_exception *pe = NULL;
  695. chunk = sector_to_chunk(s, bio->bi_sector);
  696. /* Full snapshots are not usable */
  697. /* To get here the table must be live so s->active is always set. */
  698. if (!s->valid)
  699. return -EIO;
  700. if (unlikely(bio_barrier(bio)))
  701. return -EOPNOTSUPP;
  702. /* FIXME: should only take write lock if we need
  703. * to copy an exception */
  704. down_write(&s->lock);
  705. if (!s->valid) {
  706. r = -EIO;
  707. goto out_unlock;
  708. }
  709. /* If the block is already remapped - use that, else remap it */
  710. e = lookup_exception(&s->complete, chunk);
  711. if (e) {
  712. remap_exception(s, e, bio);
  713. goto out_unlock;
  714. }
  715. /*
  716. * Write to snapshot - higher level takes care of RW/RO
  717. * flags so we should only get this if we are
  718. * writeable.
  719. */
  720. if (bio_rw(bio) == WRITE) {
  721. pe = __find_pending_exception(s, bio);
  722. if (!pe) {
  723. __invalidate_snapshot(s, pe, -ENOMEM);
  724. r = -EIO;
  725. goto out_unlock;
  726. }
  727. remap_exception(s, &pe->e, bio);
  728. bio_list_add(&pe->snapshot_bios, bio);
  729. r = 0;
  730. if (!pe->started) {
  731. /* this is protected by snap->lock */
  732. pe->started = 1;
  733. up_write(&s->lock);
  734. start_copy(pe);
  735. goto out;
  736. }
  737. } else
  738. /*
  739. * FIXME: this read path scares me because we
  740. * always use the origin when we have a pending
  741. * exception. However I can't think of a
  742. * situation where this is wrong - ejt.
  743. */
  744. bio->bi_bdev = s->origin->bdev;
  745. out_unlock:
  746. up_write(&s->lock);
  747. out:
  748. return r;
  749. }
  750. static void snapshot_resume(struct dm_target *ti)
  751. {
  752. struct dm_snapshot *s = (struct dm_snapshot *) ti->private;
  753. down_write(&s->lock);
  754. s->active = 1;
  755. up_write(&s->lock);
  756. }
  757. static int snapshot_status(struct dm_target *ti, status_type_t type,
  758. char *result, unsigned int maxlen)
  759. {
  760. struct dm_snapshot *snap = (struct dm_snapshot *) ti->private;
  761. switch (type) {
  762. case STATUSTYPE_INFO:
  763. if (!snap->valid)
  764. snprintf(result, maxlen, "Invalid");
  765. else {
  766. if (snap->store.fraction_full) {
  767. sector_t numerator, denominator;
  768. snap->store.fraction_full(&snap->store,
  769. &numerator,
  770. &denominator);
  771. snprintf(result, maxlen, "%llu/%llu",
  772. (unsigned long long)numerator,
  773. (unsigned long long)denominator);
  774. }
  775. else
  776. snprintf(result, maxlen, "Unknown");
  777. }
  778. break;
  779. case STATUSTYPE_TABLE:
  780. /*
  781. * kdevname returns a static pointer so we need
  782. * to make private copies if the output is to
  783. * make sense.
  784. */
  785. snprintf(result, maxlen, "%s %s %c %llu",
  786. snap->origin->name, snap->cow->name,
  787. snap->type,
  788. (unsigned long long)snap->chunk_size);
  789. break;
  790. }
  791. return 0;
  792. }
  793. /*-----------------------------------------------------------------
  794. * Origin methods
  795. *---------------------------------------------------------------*/
  796. static int __origin_write(struct list_head *snapshots, struct bio *bio)
  797. {
  798. int r = 1, first = 0;
  799. struct dm_snapshot *snap;
  800. struct exception *e;
  801. struct pending_exception *pe, *next_pe, *primary_pe = NULL;
  802. chunk_t chunk;
  803. LIST_HEAD(pe_queue);
  804. /* Do all the snapshots on this origin */
  805. list_for_each_entry (snap, snapshots, list) {
  806. down_write(&snap->lock);
  807. /* Only deal with valid and active snapshots */
  808. if (!snap->valid || !snap->active)
  809. goto next_snapshot;
  810. /* Nothing to do if writing beyond end of snapshot */
  811. if (bio->bi_sector >= dm_table_get_size(snap->table))
  812. goto next_snapshot;
  813. /*
  814. * Remember, different snapshots can have
  815. * different chunk sizes.
  816. */
  817. chunk = sector_to_chunk(snap, bio->bi_sector);
  818. /*
  819. * Check exception table to see if block
  820. * is already remapped in this snapshot
  821. * and trigger an exception if not.
  822. *
  823. * sibling_count is initialised to 1 so pending_complete()
  824. * won't destroy the primary_pe while we're inside this loop.
  825. */
  826. e = lookup_exception(&snap->complete, chunk);
  827. if (e)
  828. goto next_snapshot;
  829. pe = __find_pending_exception(snap, bio);
  830. if (!pe) {
  831. __invalidate_snapshot(snap, pe, -ENOMEM);
  832. goto next_snapshot;
  833. }
  834. if (!primary_pe) {
  835. /*
  836. * Either every pe here has same
  837. * primary_pe or none has one yet.
  838. */
  839. if (pe->primary_pe)
  840. primary_pe = pe->primary_pe;
  841. else {
  842. primary_pe = pe;
  843. first = 1;
  844. }
  845. bio_list_add(&primary_pe->origin_bios, bio);
  846. r = 0;
  847. }
  848. if (!pe->primary_pe) {
  849. atomic_inc(&primary_pe->sibling_count);
  850. pe->primary_pe = primary_pe;
  851. }
  852. if (!pe->started) {
  853. pe->started = 1;
  854. list_add_tail(&pe->list, &pe_queue);
  855. }
  856. next_snapshot:
  857. up_write(&snap->lock);
  858. }
  859. if (!primary_pe)
  860. goto out;
  861. /*
  862. * If this is the first time we're processing this chunk and
  863. * sibling_count is now 1 it means all the pending exceptions
  864. * got completed while we were in the loop above, so it falls to
  865. * us here to remove the primary_pe and submit any origin_bios.
  866. */
  867. if (first && atomic_dec_and_test(&primary_pe->sibling_count)) {
  868. flush_bios(bio_list_get(&primary_pe->origin_bios));
  869. free_pending_exception(primary_pe);
  870. /* If we got here, pe_queue is necessarily empty. */
  871. goto out;
  872. }
  873. /*
  874. * Now that we have a complete pe list we can start the copying.
  875. */
  876. list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
  877. start_copy(pe);
  878. out:
  879. return r;
  880. }
  881. /*
  882. * Called on a write from the origin driver.
  883. */
  884. static int do_origin(struct dm_dev *origin, struct bio *bio)
  885. {
  886. struct origin *o;
  887. int r = 1;
  888. down_read(&_origins_lock);
  889. o = __lookup_origin(origin->bdev);
  890. if (o)
  891. r = __origin_write(&o->snapshots, bio);
  892. up_read(&_origins_lock);
  893. return r;
  894. }
  895. /*
  896. * Origin: maps a linear range of a device, with hooks for snapshotting.
  897. */
  898. /*
  899. * Construct an origin mapping: <dev_path>
  900. * The context for an origin is merely a 'struct dm_dev *'
  901. * pointing to the real device.
  902. */
  903. static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  904. {
  905. int r;
  906. struct dm_dev *dev;
  907. if (argc != 1) {
  908. ti->error = "origin: incorrect number of arguments";
  909. return -EINVAL;
  910. }
  911. r = dm_get_device(ti, argv[0], 0, ti->len,
  912. dm_table_get_mode(ti->table), &dev);
  913. if (r) {
  914. ti->error = "Cannot get target device";
  915. return r;
  916. }
  917. ti->private = dev;
  918. return 0;
  919. }
  920. static void origin_dtr(struct dm_target *ti)
  921. {
  922. struct dm_dev *dev = (struct dm_dev *) ti->private;
  923. dm_put_device(ti, dev);
  924. }
  925. static int origin_map(struct dm_target *ti, struct bio *bio,
  926. union map_info *map_context)
  927. {
  928. struct dm_dev *dev = (struct dm_dev *) ti->private;
  929. bio->bi_bdev = dev->bdev;
  930. if (unlikely(bio_barrier(bio)))
  931. return -EOPNOTSUPP;
  932. /* Only tell snapshots if this is a write */
  933. return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : 1;
  934. }
  935. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  936. /*
  937. * Set the target "split_io" field to the minimum of all the snapshots'
  938. * chunk sizes.
  939. */
  940. static void origin_resume(struct dm_target *ti)
  941. {
  942. struct dm_dev *dev = (struct dm_dev *) ti->private;
  943. struct dm_snapshot *snap;
  944. struct origin *o;
  945. chunk_t chunk_size = 0;
  946. down_read(&_origins_lock);
  947. o = __lookup_origin(dev->bdev);
  948. if (o)
  949. list_for_each_entry (snap, &o->snapshots, list)
  950. chunk_size = min_not_zero(chunk_size, snap->chunk_size);
  951. up_read(&_origins_lock);
  952. ti->split_io = chunk_size;
  953. }
  954. static int origin_status(struct dm_target *ti, status_type_t type, char *result,
  955. unsigned int maxlen)
  956. {
  957. struct dm_dev *dev = (struct dm_dev *) ti->private;
  958. switch (type) {
  959. case STATUSTYPE_INFO:
  960. result[0] = '\0';
  961. break;
  962. case STATUSTYPE_TABLE:
  963. snprintf(result, maxlen, "%s", dev->name);
  964. break;
  965. }
  966. return 0;
  967. }
  968. static struct target_type origin_target = {
  969. .name = "snapshot-origin",
  970. .version = {1, 5, 0},
  971. .module = THIS_MODULE,
  972. .ctr = origin_ctr,
  973. .dtr = origin_dtr,
  974. .map = origin_map,
  975. .resume = origin_resume,
  976. .status = origin_status,
  977. };
  978. static struct target_type snapshot_target = {
  979. .name = "snapshot",
  980. .version = {1, 5, 0},
  981. .module = THIS_MODULE,
  982. .ctr = snapshot_ctr,
  983. .dtr = snapshot_dtr,
  984. .map = snapshot_map,
  985. .resume = snapshot_resume,
  986. .status = snapshot_status,
  987. };
  988. static int __init dm_snapshot_init(void)
  989. {
  990. int r;
  991. r = dm_register_target(&snapshot_target);
  992. if (r) {
  993. DMERR("snapshot target register failed %d", r);
  994. return r;
  995. }
  996. r = dm_register_target(&origin_target);
  997. if (r < 0) {
  998. DMERR("Origin target register failed %d", r);
  999. goto bad1;
  1000. }
  1001. r = init_origin_hash();
  1002. if (r) {
  1003. DMERR("init_origin_hash failed.");
  1004. goto bad2;
  1005. }
  1006. exception_cache = kmem_cache_create("dm-snapshot-ex",
  1007. sizeof(struct exception),
  1008. __alignof__(struct exception),
  1009. 0, NULL, NULL);
  1010. if (!exception_cache) {
  1011. DMERR("Couldn't create exception cache.");
  1012. r = -ENOMEM;
  1013. goto bad3;
  1014. }
  1015. pending_cache =
  1016. kmem_cache_create("dm-snapshot-in",
  1017. sizeof(struct pending_exception),
  1018. __alignof__(struct pending_exception),
  1019. 0, NULL, NULL);
  1020. if (!pending_cache) {
  1021. DMERR("Couldn't create pending cache.");
  1022. r = -ENOMEM;
  1023. goto bad4;
  1024. }
  1025. pending_pool = mempool_create_slab_pool(128, pending_cache);
  1026. if (!pending_pool) {
  1027. DMERR("Couldn't create pending pool.");
  1028. r = -ENOMEM;
  1029. goto bad5;
  1030. }
  1031. return 0;
  1032. bad5:
  1033. kmem_cache_destroy(pending_cache);
  1034. bad4:
  1035. kmem_cache_destroy(exception_cache);
  1036. bad3:
  1037. exit_origin_hash();
  1038. bad2:
  1039. dm_unregister_target(&origin_target);
  1040. bad1:
  1041. dm_unregister_target(&snapshot_target);
  1042. return r;
  1043. }
  1044. static void __exit dm_snapshot_exit(void)
  1045. {
  1046. int r;
  1047. r = dm_unregister_target(&snapshot_target);
  1048. if (r)
  1049. DMERR("snapshot unregister failed %d", r);
  1050. r = dm_unregister_target(&origin_target);
  1051. if (r)
  1052. DMERR("origin unregister failed %d", r);
  1053. exit_origin_hash();
  1054. mempool_destroy(pending_pool);
  1055. kmem_cache_destroy(pending_cache);
  1056. kmem_cache_destroy(exception_cache);
  1057. }
  1058. /* Module hooks */
  1059. module_init(dm_snapshot_init);
  1060. module_exit(dm_snapshot_exit);
  1061. MODULE_DESCRIPTION(DM_NAME " snapshot target");
  1062. MODULE_AUTHOR("Joe Thornber");
  1063. MODULE_LICENSE("GPL");