dm-kcopyd.c 14 KB

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