dm-kcopyd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. if (job->pages)
  249. kcopyd_put_pages(kc, job->pages);
  250. mempool_free(job, kc->job_pool);
  251. fn(read_err, write_err, context);
  252. if (atomic_dec_and_test(&kc->nr_jobs))
  253. wake_up(&kc->destroyq);
  254. return 0;
  255. }
  256. static void complete_io(unsigned long error, void *context)
  257. {
  258. struct kcopyd_job *job = (struct kcopyd_job *) context;
  259. struct dm_kcopyd_client *kc = job->kc;
  260. if (error) {
  261. if (job->rw == WRITE)
  262. job->write_err |= error;
  263. else
  264. job->read_err = 1;
  265. if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  266. push(&kc->complete_jobs, job);
  267. wake(kc);
  268. return;
  269. }
  270. }
  271. if (job->rw == WRITE)
  272. push(&kc->complete_jobs, job);
  273. else {
  274. job->rw = WRITE;
  275. push(&kc->io_jobs, job);
  276. }
  277. wake(kc);
  278. }
  279. /*
  280. * Request io on as many buffer heads as we can currently get for
  281. * a particular job.
  282. */
  283. static int run_io_job(struct kcopyd_job *job)
  284. {
  285. int r;
  286. struct dm_io_request io_req = {
  287. .bi_rw = job->rw | (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG),
  288. .mem.type = DM_IO_PAGE_LIST,
  289. .mem.ptr.pl = job->pages,
  290. .mem.offset = job->offset,
  291. .notify.fn = complete_io,
  292. .notify.context = job,
  293. .client = job->kc->io_client,
  294. };
  295. if (job->rw == READ)
  296. r = dm_io(&io_req, 1, &job->source, NULL);
  297. else
  298. r = dm_io(&io_req, job->num_dests, job->dests, NULL);
  299. return r;
  300. }
  301. static int run_pages_job(struct kcopyd_job *job)
  302. {
  303. int r;
  304. job->nr_pages = dm_div_up(job->dests[0].count + job->offset,
  305. PAGE_SIZE >> 9);
  306. r = kcopyd_get_pages(job->kc, job->nr_pages, &job->pages);
  307. if (!r) {
  308. /* this job is ready for io */
  309. push(&job->kc->io_jobs, job);
  310. return 0;
  311. }
  312. if (r == -ENOMEM)
  313. /* can't complete now */
  314. return 1;
  315. return r;
  316. }
  317. /*
  318. * Run through a list for as long as possible. Returns the count
  319. * of successful jobs.
  320. */
  321. static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
  322. int (*fn) (struct kcopyd_job *))
  323. {
  324. struct kcopyd_job *job;
  325. int r, count = 0;
  326. while ((job = pop(jobs, kc))) {
  327. r = fn(job);
  328. if (r < 0) {
  329. /* error this rogue job */
  330. if (job->rw == WRITE)
  331. job->write_err = (unsigned long) -1L;
  332. else
  333. job->read_err = 1;
  334. push(&kc->complete_jobs, job);
  335. break;
  336. }
  337. if (r > 0) {
  338. /*
  339. * We couldn't service this job ATM, so
  340. * push this job back onto the list.
  341. */
  342. push_head(jobs, job);
  343. break;
  344. }
  345. count++;
  346. }
  347. return count;
  348. }
  349. /*
  350. * kcopyd does this every time it's woken up.
  351. */
  352. static void do_work(struct work_struct *work)
  353. {
  354. struct dm_kcopyd_client *kc = container_of(work,
  355. struct dm_kcopyd_client, kcopyd_work);
  356. /*
  357. * The order that these are called is *very* important.
  358. * complete jobs can free some pages for pages jobs.
  359. * Pages jobs when successful will jump onto the io jobs
  360. * list. io jobs call wake when they complete and it all
  361. * starts again.
  362. */
  363. process_jobs(&kc->complete_jobs, kc, run_complete_job);
  364. process_jobs(&kc->pages_jobs, kc, run_pages_job);
  365. process_jobs(&kc->io_jobs, kc, run_io_job);
  366. }
  367. /*
  368. * If we are copying a small region we just dispatch a single job
  369. * to do the copy, otherwise the io has to be split up into many
  370. * jobs.
  371. */
  372. static void dispatch_job(struct kcopyd_job *job)
  373. {
  374. struct dm_kcopyd_client *kc = job->kc;
  375. atomic_inc(&kc->nr_jobs);
  376. push(&kc->pages_jobs, job);
  377. wake(kc);
  378. }
  379. #define SUB_JOB_SIZE 128
  380. static void segment_complete(int read_err, unsigned long write_err,
  381. void *context)
  382. {
  383. /* FIXME: tidy this function */
  384. sector_t progress = 0;
  385. sector_t count = 0;
  386. struct kcopyd_job *job = (struct kcopyd_job *) context;
  387. struct dm_kcopyd_client *kc = job->kc;
  388. mutex_lock(&job->lock);
  389. /* update the error */
  390. if (read_err)
  391. job->read_err = 1;
  392. if (write_err)
  393. job->write_err |= write_err;
  394. /*
  395. * Only dispatch more work if there hasn't been an error.
  396. */
  397. if ((!job->read_err && !job->write_err) ||
  398. test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  399. /* get the next chunk of work */
  400. progress = job->progress;
  401. count = job->source.count - progress;
  402. if (count) {
  403. if (count > SUB_JOB_SIZE)
  404. count = SUB_JOB_SIZE;
  405. job->progress += count;
  406. }
  407. }
  408. mutex_unlock(&job->lock);
  409. if (count) {
  410. int i;
  411. struct kcopyd_job *sub_job = mempool_alloc(kc->job_pool,
  412. GFP_NOIO);
  413. *sub_job = *job;
  414. sub_job->source.sector += progress;
  415. sub_job->source.count = count;
  416. for (i = 0; i < job->num_dests; i++) {
  417. sub_job->dests[i].sector += progress;
  418. sub_job->dests[i].count = count;
  419. }
  420. sub_job->fn = segment_complete;
  421. sub_job->context = job;
  422. dispatch_job(sub_job);
  423. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  424. /*
  425. * Queue the completion callback to the kcopyd thread.
  426. *
  427. * Some callers assume that all the completions are called
  428. * from a single thread and don't race with each other.
  429. *
  430. * We must not call the callback directly here because this
  431. * code may not be executing in the thread.
  432. */
  433. push(&kc->complete_jobs, job);
  434. wake(kc);
  435. }
  436. }
  437. /*
  438. * Create some little jobs that will do the move between
  439. * them.
  440. */
  441. #define SPLIT_COUNT 8
  442. static void split_job(struct kcopyd_job *job)
  443. {
  444. int i;
  445. atomic_inc(&job->kc->nr_jobs);
  446. atomic_set(&job->sub_jobs, SPLIT_COUNT);
  447. for (i = 0; i < SPLIT_COUNT; i++)
  448. segment_complete(0, 0u, job);
  449. }
  450. int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
  451. unsigned int num_dests, struct dm_io_region *dests,
  452. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  453. {
  454. struct kcopyd_job *job;
  455. /*
  456. * Allocate a new job.
  457. */
  458. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  459. /*
  460. * set up for the read.
  461. */
  462. job->kc = kc;
  463. job->flags = flags;
  464. job->read_err = 0;
  465. job->write_err = 0;
  466. job->rw = READ;
  467. job->source = *from;
  468. job->num_dests = num_dests;
  469. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  470. job->offset = 0;
  471. job->nr_pages = 0;
  472. job->pages = NULL;
  473. job->fn = fn;
  474. job->context = context;
  475. if (job->source.count < SUB_JOB_SIZE)
  476. dispatch_job(job);
  477. else {
  478. mutex_init(&job->lock);
  479. job->progress = 0;
  480. split_job(job);
  481. }
  482. return 0;
  483. }
  484. EXPORT_SYMBOL(dm_kcopyd_copy);
  485. /*
  486. * Cancels a kcopyd job, eg. someone might be deactivating a
  487. * mirror.
  488. */
  489. #if 0
  490. int kcopyd_cancel(struct kcopyd_job *job, int block)
  491. {
  492. /* FIXME: finish */
  493. return -1;
  494. }
  495. #endif /* 0 */
  496. /*-----------------------------------------------------------------
  497. * Client setup
  498. *---------------------------------------------------------------*/
  499. int dm_kcopyd_client_create(unsigned int nr_pages,
  500. struct dm_kcopyd_client **result)
  501. {
  502. int r = -ENOMEM;
  503. struct dm_kcopyd_client *kc;
  504. kc = kmalloc(sizeof(*kc), GFP_KERNEL);
  505. if (!kc)
  506. return -ENOMEM;
  507. spin_lock_init(&kc->lock);
  508. spin_lock_init(&kc->job_lock);
  509. INIT_LIST_HEAD(&kc->complete_jobs);
  510. INIT_LIST_HEAD(&kc->io_jobs);
  511. INIT_LIST_HEAD(&kc->pages_jobs);
  512. kc->job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
  513. if (!kc->job_pool)
  514. goto bad_slab;
  515. INIT_WORK(&kc->kcopyd_work, do_work);
  516. kc->kcopyd_wq = create_singlethread_workqueue("kcopyd");
  517. if (!kc->kcopyd_wq)
  518. goto bad_workqueue;
  519. kc->pages = NULL;
  520. kc->nr_pages = kc->nr_free_pages = 0;
  521. r = client_alloc_pages(kc, nr_pages);
  522. if (r)
  523. goto bad_client_pages;
  524. kc->io_client = dm_io_client_create(nr_pages);
  525. if (IS_ERR(kc->io_client)) {
  526. r = PTR_ERR(kc->io_client);
  527. goto bad_io_client;
  528. }
  529. init_waitqueue_head(&kc->destroyq);
  530. atomic_set(&kc->nr_jobs, 0);
  531. *result = kc;
  532. return 0;
  533. bad_io_client:
  534. client_free_pages(kc);
  535. bad_client_pages:
  536. destroy_workqueue(kc->kcopyd_wq);
  537. bad_workqueue:
  538. mempool_destroy(kc->job_pool);
  539. bad_slab:
  540. kfree(kc);
  541. return r;
  542. }
  543. EXPORT_SYMBOL(dm_kcopyd_client_create);
  544. void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
  545. {
  546. /* Wait for completion of all jobs submitted by this client. */
  547. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  548. BUG_ON(!list_empty(&kc->complete_jobs));
  549. BUG_ON(!list_empty(&kc->io_jobs));
  550. BUG_ON(!list_empty(&kc->pages_jobs));
  551. destroy_workqueue(kc->kcopyd_wq);
  552. dm_io_client_destroy(kc->io_client);
  553. client_free_pages(kc);
  554. mempool_destroy(kc->job_pool);
  555. kfree(kc);
  556. }
  557. EXPORT_SYMBOL(dm_kcopyd_client_destroy);