dm-snap.c 28 KB

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