dm-snap.c 25 KB

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