dm-kcopyd.c 13 KB

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