dm-kcopyd.c 14 KB

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