dm-kcopyd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. * Copyright (C) 2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2006 Red Hat GmbH
  4. *
  5. * This file is released under the GPL.
  6. *
  7. * Kcopyd provides a simple interface for copying an area of one
  8. * block-device to one or more other block-devices, with an asynchronous
  9. * completion notification.
  10. */
  11. #include <linux/types.h>
  12. #include <asm/atomic.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/fs.h>
  15. #include <linux/init.h>
  16. #include <linux/list.h>
  17. #include <linux/mempool.h>
  18. #include <linux/module.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/slab.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/mutex.h>
  24. #include <linux/device-mapper.h>
  25. #include <linux/dm-kcopyd.h>
  26. #include "dm.h"
  27. /*-----------------------------------------------------------------
  28. * Each kcopyd client has its own little pool of preallocated
  29. * pages for kcopyd io.
  30. *---------------------------------------------------------------*/
  31. struct dm_kcopyd_client {
  32. spinlock_t lock;
  33. struct page_list *pages;
  34. unsigned int nr_pages;
  35. unsigned int nr_free_pages;
  36. struct dm_io_client *io_client;
  37. wait_queue_head_t destroyq;
  38. atomic_t nr_jobs;
  39. mempool_t *job_pool;
  40. struct workqueue_struct *kcopyd_wq;
  41. struct work_struct kcopyd_work;
  42. /*
  43. * We maintain three lists of jobs:
  44. *
  45. * i) jobs waiting for pages
  46. * ii) jobs that have pages, and are waiting for the io to be issued.
  47. * iii) jobs that have completed.
  48. *
  49. * All three of these are protected by job_lock.
  50. */
  51. spinlock_t job_lock;
  52. struct list_head complete_jobs;
  53. struct list_head io_jobs;
  54. struct list_head pages_jobs;
  55. };
  56. static void wake(struct dm_kcopyd_client *kc)
  57. {
  58. queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
  59. }
  60. static struct page_list *alloc_pl(void)
  61. {
  62. struct page_list *pl;
  63. pl = kmalloc(sizeof(*pl), GFP_KERNEL);
  64. if (!pl)
  65. return NULL;
  66. pl->page = alloc_page(GFP_KERNEL);
  67. if (!pl->page) {
  68. kfree(pl);
  69. return NULL;
  70. }
  71. return pl;
  72. }
  73. static void free_pl(struct page_list *pl)
  74. {
  75. __free_page(pl->page);
  76. kfree(pl);
  77. }
  78. static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
  79. unsigned int nr, struct page_list **pages)
  80. {
  81. struct page_list *pl;
  82. spin_lock(&kc->lock);
  83. if (kc->nr_free_pages < nr) {
  84. spin_unlock(&kc->lock);
  85. return -ENOMEM;
  86. }
  87. kc->nr_free_pages -= nr;
  88. for (*pages = pl = kc->pages; --nr; pl = pl->next)
  89. ;
  90. kc->pages = pl->next;
  91. pl->next = NULL;
  92. spin_unlock(&kc->lock);
  93. return 0;
  94. }
  95. static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl)
  96. {
  97. struct page_list *cursor;
  98. spin_lock(&kc->lock);
  99. for (cursor = pl; cursor->next; cursor = cursor->next)
  100. kc->nr_free_pages++;
  101. kc->nr_free_pages++;
  102. cursor->next = kc->pages;
  103. kc->pages = pl;
  104. spin_unlock(&kc->lock);
  105. }
  106. /*
  107. * These three functions resize the page pool.
  108. */
  109. static void drop_pages(struct page_list *pl)
  110. {
  111. struct page_list *next;
  112. while (pl) {
  113. next = pl->next;
  114. free_pl(pl);
  115. pl = next;
  116. }
  117. }
  118. static int client_alloc_pages(struct dm_kcopyd_client *kc, unsigned int nr)
  119. {
  120. unsigned int i;
  121. struct page_list *pl = NULL, *next;
  122. for (i = 0; i < nr; i++) {
  123. next = alloc_pl();
  124. if (!next) {
  125. if (pl)
  126. drop_pages(pl);
  127. return -ENOMEM;
  128. }
  129. next->next = pl;
  130. pl = next;
  131. }
  132. kcopyd_put_pages(kc, pl);
  133. kc->nr_pages += nr;
  134. return 0;
  135. }
  136. static void client_free_pages(struct dm_kcopyd_client *kc)
  137. {
  138. BUG_ON(kc->nr_free_pages != kc->nr_pages);
  139. drop_pages(kc->pages);
  140. kc->pages = NULL;
  141. kc->nr_free_pages = kc->nr_pages = 0;
  142. }
  143. /*-----------------------------------------------------------------
  144. * kcopyd_jobs need to be allocated by the *clients* of kcopyd,
  145. * for this reason we use a mempool to prevent the client from
  146. * ever having to do io (which could cause a deadlock).
  147. *---------------------------------------------------------------*/
  148. struct kcopyd_job {
  149. struct dm_kcopyd_client *kc;
  150. struct list_head list;
  151. unsigned long flags;
  152. /*
  153. * Error state of the job.
  154. */
  155. int read_err;
  156. unsigned long write_err;
  157. /*
  158. * Either READ or WRITE
  159. */
  160. int rw;
  161. struct dm_io_region source;
  162. /*
  163. * The destinations for the transfer.
  164. */
  165. unsigned int num_dests;
  166. struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
  167. sector_t offset;
  168. unsigned int nr_pages;
  169. struct page_list *pages;
  170. /*
  171. * Set this to ensure you are notified when the job has
  172. * completed. 'context' is for callback to use.
  173. */
  174. dm_kcopyd_notify_fn fn;
  175. void *context;
  176. /*
  177. * These fields are only used if the job has been split
  178. * into more manageable parts.
  179. */
  180. struct mutex lock;
  181. atomic_t sub_jobs;
  182. sector_t progress;
  183. };
  184. /* FIXME: this should scale with the number of pages */
  185. #define MIN_JOBS 512
  186. static struct kmem_cache *_job_cache;
  187. int __init dm_kcopyd_init(void)
  188. {
  189. _job_cache = KMEM_CACHE(kcopyd_job, 0);
  190. if (!_job_cache)
  191. return -ENOMEM;
  192. return 0;
  193. }
  194. void dm_kcopyd_exit(void)
  195. {
  196. kmem_cache_destroy(_job_cache);
  197. _job_cache = NULL;
  198. }
  199. /*
  200. * Functions to push and pop a job onto the head of a given job
  201. * list.
  202. */
  203. static struct kcopyd_job *pop(struct list_head *jobs,
  204. struct dm_kcopyd_client *kc)
  205. {
  206. struct kcopyd_job *job = NULL;
  207. unsigned long flags;
  208. spin_lock_irqsave(&kc->job_lock, flags);
  209. if (!list_empty(jobs)) {
  210. job = list_entry(jobs->next, struct kcopyd_job, list);
  211. list_del(&job->list);
  212. }
  213. spin_unlock_irqrestore(&kc->job_lock, flags);
  214. return job;
  215. }
  216. static void push(struct list_head *jobs, struct kcopyd_job *job)
  217. {
  218. unsigned long flags;
  219. struct dm_kcopyd_client *kc = job->kc;
  220. spin_lock_irqsave(&kc->job_lock, flags);
  221. list_add_tail(&job->list, jobs);
  222. spin_unlock_irqrestore(&kc->job_lock, flags);
  223. }
  224. static void push_head(struct list_head *jobs, struct kcopyd_job *job)
  225. {
  226. unsigned long flags;
  227. struct dm_kcopyd_client *kc = job->kc;
  228. spin_lock_irqsave(&kc->job_lock, flags);
  229. list_add(&job->list, jobs);
  230. spin_unlock_irqrestore(&kc->job_lock, flags);
  231. }
  232. /*
  233. * These three functions process 1 item from the corresponding
  234. * job list.
  235. *
  236. * They return:
  237. * < 0: error
  238. * 0: success
  239. * > 0: can't process yet.
  240. */
  241. static int run_complete_job(struct kcopyd_job *job)
  242. {
  243. void *context = job->context;
  244. int read_err = job->read_err;
  245. unsigned long write_err = job->write_err;
  246. dm_kcopyd_notify_fn fn = job->fn;
  247. struct dm_kcopyd_client *kc = job->kc;
  248. kcopyd_put_pages(kc, job->pages);
  249. mempool_free(job, kc->job_pool);
  250. fn(read_err, write_err, context);
  251. if (atomic_dec_and_test(&kc->nr_jobs))
  252. wake_up(&kc->destroyq);
  253. return 0;
  254. }
  255. static void complete_io(unsigned long error, void *context)
  256. {
  257. struct kcopyd_job *job = (struct kcopyd_job *) context;
  258. struct dm_kcopyd_client *kc = job->kc;
  259. if (error) {
  260. if (job->rw == WRITE)
  261. job->write_err |= error;
  262. else
  263. job->read_err = 1;
  264. if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  265. push(&kc->complete_jobs, job);
  266. wake(kc);
  267. return;
  268. }
  269. }
  270. if (job->rw == WRITE)
  271. push(&kc->complete_jobs, job);
  272. else {
  273. job->rw = WRITE;
  274. push(&kc->io_jobs, job);
  275. }
  276. wake(kc);
  277. }
  278. /*
  279. * Request io on as many buffer heads as we can currently get for
  280. * a particular job.
  281. */
  282. static int run_io_job(struct kcopyd_job *job)
  283. {
  284. int r;
  285. struct dm_io_request io_req = {
  286. .bi_rw = job->rw | (1 << BIO_RW_SYNC),
  287. .mem.type = DM_IO_PAGE_LIST,
  288. .mem.ptr.pl = job->pages,
  289. .mem.offset = job->offset,
  290. .notify.fn = complete_io,
  291. .notify.context = job,
  292. .client = job->kc->io_client,
  293. };
  294. if (job->rw == READ)
  295. r = dm_io(&io_req, 1, &job->source, NULL);
  296. else
  297. r = dm_io(&io_req, job->num_dests, job->dests, NULL);
  298. return r;
  299. }
  300. static int run_pages_job(struct kcopyd_job *job)
  301. {
  302. int r;
  303. job->nr_pages = dm_div_up(job->dests[0].count + job->offset,
  304. PAGE_SIZE >> 9);
  305. r = kcopyd_get_pages(job->kc, job->nr_pages, &job->pages);
  306. if (!r) {
  307. /* this job is ready for io */
  308. push(&job->kc->io_jobs, job);
  309. return 0;
  310. }
  311. if (r == -ENOMEM)
  312. /* can't complete now */
  313. return 1;
  314. return r;
  315. }
  316. /*
  317. * Run through a list for as long as possible. Returns the count
  318. * of successful jobs.
  319. */
  320. static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
  321. int (*fn) (struct kcopyd_job *))
  322. {
  323. struct kcopyd_job *job;
  324. int r, count = 0;
  325. while ((job = pop(jobs, kc))) {
  326. r = fn(job);
  327. if (r < 0) {
  328. /* error this rogue job */
  329. if (job->rw == WRITE)
  330. job->write_err = (unsigned long) -1L;
  331. else
  332. job->read_err = 1;
  333. push(&kc->complete_jobs, job);
  334. break;
  335. }
  336. if (r > 0) {
  337. /*
  338. * We couldn't service this job ATM, so
  339. * push this job back onto the list.
  340. */
  341. push_head(jobs, job);
  342. break;
  343. }
  344. count++;
  345. }
  346. return count;
  347. }
  348. /*
  349. * kcopyd does this every time it's woken up.
  350. */
  351. static void do_work(struct work_struct *work)
  352. {
  353. struct dm_kcopyd_client *kc = container_of(work,
  354. struct dm_kcopyd_client, kcopyd_work);
  355. /*
  356. * The order that these are called is *very* important.
  357. * complete jobs can free some pages for pages jobs.
  358. * Pages jobs when successful will jump onto the io jobs
  359. * list. io jobs call wake when they complete and it all
  360. * starts again.
  361. */
  362. process_jobs(&kc->complete_jobs, kc, run_complete_job);
  363. process_jobs(&kc->pages_jobs, kc, run_pages_job);
  364. process_jobs(&kc->io_jobs, kc, run_io_job);
  365. }
  366. /*
  367. * If we are copying a small region we just dispatch a single job
  368. * to do the copy, otherwise the io has to be split up into many
  369. * jobs.
  370. */
  371. static void dispatch_job(struct kcopyd_job *job)
  372. {
  373. struct dm_kcopyd_client *kc = job->kc;
  374. atomic_inc(&kc->nr_jobs);
  375. push(&kc->pages_jobs, job);
  376. wake(kc);
  377. }
  378. #define SUB_JOB_SIZE 128
  379. static void segment_complete(int read_err, unsigned long write_err,
  380. void *context)
  381. {
  382. /* FIXME: tidy this function */
  383. sector_t progress = 0;
  384. sector_t count = 0;
  385. struct kcopyd_job *job = (struct kcopyd_job *) context;
  386. mutex_lock(&job->lock);
  387. /* update the error */
  388. if (read_err)
  389. job->read_err = 1;
  390. if (write_err)
  391. job->write_err |= write_err;
  392. /*
  393. * Only dispatch more work if there hasn't been an error.
  394. */
  395. if ((!job->read_err && !job->write_err) ||
  396. test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  397. /* get the next chunk of work */
  398. progress = job->progress;
  399. count = job->source.count - progress;
  400. if (count) {
  401. if (count > SUB_JOB_SIZE)
  402. count = SUB_JOB_SIZE;
  403. job->progress += count;
  404. }
  405. }
  406. mutex_unlock(&job->lock);
  407. if (count) {
  408. int i;
  409. struct kcopyd_job *sub_job = mempool_alloc(job->kc->job_pool,
  410. GFP_NOIO);
  411. *sub_job = *job;
  412. sub_job->source.sector += progress;
  413. sub_job->source.count = count;
  414. for (i = 0; i < job->num_dests; i++) {
  415. sub_job->dests[i].sector += progress;
  416. sub_job->dests[i].count = count;
  417. }
  418. sub_job->fn = segment_complete;
  419. sub_job->context = job;
  420. dispatch_job(sub_job);
  421. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  422. /*
  423. * To avoid a race we must keep the job around
  424. * until after the notify function has completed.
  425. * Otherwise the client may try and stop the job
  426. * after we've completed.
  427. */
  428. job->fn(read_err, write_err, job->context);
  429. mempool_free(job, job->kc->job_pool);
  430. }
  431. }
  432. /*
  433. * Create some little jobs that will do the move between
  434. * them.
  435. */
  436. #define SPLIT_COUNT 8
  437. static void split_job(struct kcopyd_job *job)
  438. {
  439. int i;
  440. atomic_set(&job->sub_jobs, SPLIT_COUNT);
  441. for (i = 0; i < SPLIT_COUNT; i++)
  442. segment_complete(0, 0u, job);
  443. }
  444. int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
  445. unsigned int num_dests, struct dm_io_region *dests,
  446. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  447. {
  448. struct kcopyd_job *job;
  449. /*
  450. * Allocate a new job.
  451. */
  452. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  453. /*
  454. * set up for the read.
  455. */
  456. job->kc = kc;
  457. job->flags = flags;
  458. job->read_err = 0;
  459. job->write_err = 0;
  460. job->rw = READ;
  461. job->source = *from;
  462. job->num_dests = num_dests;
  463. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  464. job->offset = 0;
  465. job->nr_pages = 0;
  466. job->pages = NULL;
  467. job->fn = fn;
  468. job->context = context;
  469. if (job->source.count < SUB_JOB_SIZE)
  470. dispatch_job(job);
  471. else {
  472. mutex_init(&job->lock);
  473. job->progress = 0;
  474. split_job(job);
  475. }
  476. return 0;
  477. }
  478. EXPORT_SYMBOL(dm_kcopyd_copy);
  479. /*
  480. * Cancels a kcopyd job, eg. someone might be deactivating a
  481. * mirror.
  482. */
  483. #if 0
  484. int kcopyd_cancel(struct kcopyd_job *job, int block)
  485. {
  486. /* FIXME: finish */
  487. return -1;
  488. }
  489. #endif /* 0 */
  490. /*-----------------------------------------------------------------
  491. * Client setup
  492. *---------------------------------------------------------------*/
  493. int dm_kcopyd_client_create(unsigned int nr_pages,
  494. struct dm_kcopyd_client **result)
  495. {
  496. int r = -ENOMEM;
  497. struct dm_kcopyd_client *kc;
  498. kc = kmalloc(sizeof(*kc), GFP_KERNEL);
  499. if (!kc)
  500. return -ENOMEM;
  501. spin_lock_init(&kc->lock);
  502. spin_lock_init(&kc->job_lock);
  503. INIT_LIST_HEAD(&kc->complete_jobs);
  504. INIT_LIST_HEAD(&kc->io_jobs);
  505. INIT_LIST_HEAD(&kc->pages_jobs);
  506. kc->job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
  507. if (!kc->job_pool)
  508. goto bad_slab;
  509. INIT_WORK(&kc->kcopyd_work, do_work);
  510. kc->kcopyd_wq = create_singlethread_workqueue("kcopyd");
  511. if (!kc->kcopyd_wq)
  512. goto bad_workqueue;
  513. kc->pages = NULL;
  514. kc->nr_pages = kc->nr_free_pages = 0;
  515. r = client_alloc_pages(kc, nr_pages);
  516. if (r)
  517. goto bad_client_pages;
  518. kc->io_client = dm_io_client_create(nr_pages);
  519. if (IS_ERR(kc->io_client)) {
  520. r = PTR_ERR(kc->io_client);
  521. goto bad_io_client;
  522. }
  523. init_waitqueue_head(&kc->destroyq);
  524. atomic_set(&kc->nr_jobs, 0);
  525. *result = kc;
  526. return 0;
  527. bad_io_client:
  528. client_free_pages(kc);
  529. bad_client_pages:
  530. destroy_workqueue(kc->kcopyd_wq);
  531. bad_workqueue:
  532. mempool_destroy(kc->job_pool);
  533. bad_slab:
  534. kfree(kc);
  535. return r;
  536. }
  537. EXPORT_SYMBOL(dm_kcopyd_client_create);
  538. void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
  539. {
  540. /* Wait for completion of all jobs submitted by this client. */
  541. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  542. BUG_ON(!list_empty(&kc->complete_jobs));
  543. BUG_ON(!list_empty(&kc->io_jobs));
  544. BUG_ON(!list_empty(&kc->pages_jobs));
  545. destroy_workqueue(kc->kcopyd_wq);
  546. dm_io_client_destroy(kc->io_client);
  547. client_free_pages(kc);
  548. mempool_destroy(kc->job_pool);
  549. kfree(kc);
  550. }
  551. EXPORT_SYMBOL(dm_kcopyd_client_destroy);