dm-kcopyd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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 <linux/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/delay.h>
  25. #include <linux/device-mapper.h>
  26. #include <linux/dm-kcopyd.h>
  27. #include "dm.h"
  28. #define SUB_JOB_SIZE 128
  29. #define SPLIT_COUNT 8
  30. #define MIN_JOBS 8
  31. #define RESERVE_PAGES (DIV_ROUND_UP(SUB_JOB_SIZE << SECTOR_SHIFT, PAGE_SIZE))
  32. /*-----------------------------------------------------------------
  33. * Each kcopyd client has its own little pool of preallocated
  34. * pages for kcopyd io.
  35. *---------------------------------------------------------------*/
  36. struct dm_kcopyd_client {
  37. struct page_list *pages;
  38. unsigned nr_reserved_pages;
  39. unsigned nr_free_pages;
  40. struct dm_io_client *io_client;
  41. wait_queue_head_t destroyq;
  42. atomic_t nr_jobs;
  43. mempool_t *job_pool;
  44. struct workqueue_struct *kcopyd_wq;
  45. struct work_struct kcopyd_work;
  46. struct dm_kcopyd_throttle *throttle;
  47. /*
  48. * We maintain four lists of jobs:
  49. *
  50. * i) jobs waiting for pages
  51. * ii) jobs that have pages, and are waiting for the io to be issued.
  52. * iii) jobs that don't need to do any IO and just run a callback
  53. * iv) jobs that have completed.
  54. *
  55. * All four of these are protected by job_lock.
  56. */
  57. spinlock_t job_lock;
  58. struct list_head callback_jobs;
  59. struct list_head complete_jobs;
  60. struct list_head io_jobs;
  61. struct list_head pages_jobs;
  62. };
  63. static struct page_list zero_page_list;
  64. static DEFINE_SPINLOCK(throttle_spinlock);
  65. /*
  66. * IO/IDLE accounting slowly decays after (1 << ACCOUNT_INTERVAL_SHIFT) period.
  67. * When total_period >= (1 << ACCOUNT_INTERVAL_SHIFT) the counters are divided
  68. * by 2.
  69. */
  70. #define ACCOUNT_INTERVAL_SHIFT SHIFT_HZ
  71. /*
  72. * Sleep this number of milliseconds.
  73. *
  74. * The value was decided experimentally.
  75. * Smaller values seem to cause an increased copy rate above the limit.
  76. * The reason for this is unknown but possibly due to jiffies rounding errors
  77. * or read/write cache inside the disk.
  78. */
  79. #define SLEEP_MSEC 100
  80. /*
  81. * Maximum number of sleep events. There is a theoretical livelock if more
  82. * kcopyd clients do work simultaneously which this limit avoids.
  83. */
  84. #define MAX_SLEEPS 10
  85. static void io_job_start(struct dm_kcopyd_throttle *t)
  86. {
  87. unsigned throttle, now, difference;
  88. int slept = 0, skew;
  89. if (unlikely(!t))
  90. return;
  91. try_again:
  92. spin_lock_irq(&throttle_spinlock);
  93. throttle = ACCESS_ONCE(t->throttle);
  94. if (likely(throttle >= 100))
  95. goto skip_limit;
  96. now = jiffies;
  97. difference = now - t->last_jiffies;
  98. t->last_jiffies = now;
  99. if (t->num_io_jobs)
  100. t->io_period += difference;
  101. t->total_period += difference;
  102. /*
  103. * Maintain sane values if we got a temporary overflow.
  104. */
  105. if (unlikely(t->io_period > t->total_period))
  106. t->io_period = t->total_period;
  107. if (unlikely(t->total_period >= (1 << ACCOUNT_INTERVAL_SHIFT))) {
  108. int shift = fls(t->total_period >> ACCOUNT_INTERVAL_SHIFT);
  109. t->total_period >>= shift;
  110. t->io_period >>= shift;
  111. }
  112. skew = t->io_period - throttle * t->total_period / 100;
  113. if (unlikely(skew > 0) && slept < MAX_SLEEPS) {
  114. slept++;
  115. spin_unlock_irq(&throttle_spinlock);
  116. msleep(SLEEP_MSEC);
  117. goto try_again;
  118. }
  119. skip_limit:
  120. t->num_io_jobs++;
  121. spin_unlock_irq(&throttle_spinlock);
  122. }
  123. static void io_job_finish(struct dm_kcopyd_throttle *t)
  124. {
  125. unsigned long flags;
  126. if (unlikely(!t))
  127. return;
  128. spin_lock_irqsave(&throttle_spinlock, flags);
  129. t->num_io_jobs--;
  130. if (likely(ACCESS_ONCE(t->throttle) >= 100))
  131. goto skip_limit;
  132. if (!t->num_io_jobs) {
  133. unsigned now, difference;
  134. now = jiffies;
  135. difference = now - t->last_jiffies;
  136. t->last_jiffies = now;
  137. t->io_period += difference;
  138. t->total_period += difference;
  139. /*
  140. * Maintain sane values if we got a temporary overflow.
  141. */
  142. if (unlikely(t->io_period > t->total_period))
  143. t->io_period = t->total_period;
  144. }
  145. skip_limit:
  146. spin_unlock_irqrestore(&throttle_spinlock, flags);
  147. }
  148. static void wake(struct dm_kcopyd_client *kc)
  149. {
  150. queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
  151. }
  152. /*
  153. * Obtain one page for the use of kcopyd.
  154. */
  155. static struct page_list *alloc_pl(gfp_t gfp)
  156. {
  157. struct page_list *pl;
  158. pl = kmalloc(sizeof(*pl), gfp);
  159. if (!pl)
  160. return NULL;
  161. pl->page = alloc_page(gfp);
  162. if (!pl->page) {
  163. kfree(pl);
  164. return NULL;
  165. }
  166. return pl;
  167. }
  168. static void free_pl(struct page_list *pl)
  169. {
  170. __free_page(pl->page);
  171. kfree(pl);
  172. }
  173. /*
  174. * Add the provided pages to a client's free page list, releasing
  175. * back to the system any beyond the reserved_pages limit.
  176. */
  177. static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl)
  178. {
  179. struct page_list *next;
  180. do {
  181. next = pl->next;
  182. if (kc->nr_free_pages >= kc->nr_reserved_pages)
  183. free_pl(pl);
  184. else {
  185. pl->next = kc->pages;
  186. kc->pages = pl;
  187. kc->nr_free_pages++;
  188. }
  189. pl = next;
  190. } while (pl);
  191. }
  192. static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
  193. unsigned int nr, struct page_list **pages)
  194. {
  195. struct page_list *pl;
  196. *pages = NULL;
  197. do {
  198. pl = alloc_pl(__GFP_NOWARN | __GFP_NORETRY | __GFP_KSWAPD_RECLAIM);
  199. if (unlikely(!pl)) {
  200. /* Use reserved pages */
  201. pl = kc->pages;
  202. if (unlikely(!pl))
  203. goto out_of_memory;
  204. kc->pages = pl->next;
  205. kc->nr_free_pages--;
  206. }
  207. pl->next = *pages;
  208. *pages = pl;
  209. } while (--nr);
  210. return 0;
  211. out_of_memory:
  212. if (*pages)
  213. kcopyd_put_pages(kc, *pages);
  214. return -ENOMEM;
  215. }
  216. /*
  217. * These three functions resize the page pool.
  218. */
  219. static void drop_pages(struct page_list *pl)
  220. {
  221. struct page_list *next;
  222. while (pl) {
  223. next = pl->next;
  224. free_pl(pl);
  225. pl = next;
  226. }
  227. }
  228. /*
  229. * Allocate and reserve nr_pages for the use of a specific client.
  230. */
  231. static int client_reserve_pages(struct dm_kcopyd_client *kc, unsigned nr_pages)
  232. {
  233. unsigned i;
  234. struct page_list *pl = NULL, *next;
  235. for (i = 0; i < nr_pages; i++) {
  236. next = alloc_pl(GFP_KERNEL);
  237. if (!next) {
  238. if (pl)
  239. drop_pages(pl);
  240. return -ENOMEM;
  241. }
  242. next->next = pl;
  243. pl = next;
  244. }
  245. kc->nr_reserved_pages += nr_pages;
  246. kcopyd_put_pages(kc, pl);
  247. return 0;
  248. }
  249. static void client_free_pages(struct dm_kcopyd_client *kc)
  250. {
  251. BUG_ON(kc->nr_free_pages != kc->nr_reserved_pages);
  252. drop_pages(kc->pages);
  253. kc->pages = NULL;
  254. kc->nr_free_pages = kc->nr_reserved_pages = 0;
  255. }
  256. /*-----------------------------------------------------------------
  257. * kcopyd_jobs need to be allocated by the *clients* of kcopyd,
  258. * for this reason we use a mempool to prevent the client from
  259. * ever having to do io (which could cause a deadlock).
  260. *---------------------------------------------------------------*/
  261. struct kcopyd_job {
  262. struct dm_kcopyd_client *kc;
  263. struct list_head list;
  264. unsigned long flags;
  265. /*
  266. * Error state of the job.
  267. */
  268. int read_err;
  269. unsigned long write_err;
  270. /*
  271. * Either READ or WRITE
  272. */
  273. int rw;
  274. struct dm_io_region source;
  275. /*
  276. * The destinations for the transfer.
  277. */
  278. unsigned int num_dests;
  279. struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
  280. struct page_list *pages;
  281. /*
  282. * Set this to ensure you are notified when the job has
  283. * completed. 'context' is for callback to use.
  284. */
  285. dm_kcopyd_notify_fn fn;
  286. void *context;
  287. /*
  288. * These fields are only used if the job has been split
  289. * into more manageable parts.
  290. */
  291. struct mutex lock;
  292. atomic_t sub_jobs;
  293. sector_t progress;
  294. struct kcopyd_job *master_job;
  295. };
  296. static struct kmem_cache *_job_cache;
  297. int __init dm_kcopyd_init(void)
  298. {
  299. _job_cache = kmem_cache_create("kcopyd_job",
  300. sizeof(struct kcopyd_job) * (SPLIT_COUNT + 1),
  301. __alignof__(struct kcopyd_job), 0, NULL);
  302. if (!_job_cache)
  303. return -ENOMEM;
  304. zero_page_list.next = &zero_page_list;
  305. zero_page_list.page = ZERO_PAGE(0);
  306. return 0;
  307. }
  308. void dm_kcopyd_exit(void)
  309. {
  310. kmem_cache_destroy(_job_cache);
  311. _job_cache = NULL;
  312. }
  313. /*
  314. * Functions to push and pop a job onto the head of a given job
  315. * list.
  316. */
  317. static struct kcopyd_job *pop(struct list_head *jobs,
  318. struct dm_kcopyd_client *kc)
  319. {
  320. struct kcopyd_job *job = NULL;
  321. unsigned long flags;
  322. spin_lock_irqsave(&kc->job_lock, flags);
  323. if (!list_empty(jobs)) {
  324. job = list_entry(jobs->next, struct kcopyd_job, list);
  325. list_del(&job->list);
  326. }
  327. spin_unlock_irqrestore(&kc->job_lock, flags);
  328. return job;
  329. }
  330. static void push(struct list_head *jobs, struct kcopyd_job *job)
  331. {
  332. unsigned long flags;
  333. struct dm_kcopyd_client *kc = job->kc;
  334. spin_lock_irqsave(&kc->job_lock, flags);
  335. list_add_tail(&job->list, jobs);
  336. spin_unlock_irqrestore(&kc->job_lock, flags);
  337. }
  338. static void push_head(struct list_head *jobs, struct kcopyd_job *job)
  339. {
  340. unsigned long flags;
  341. struct dm_kcopyd_client *kc = job->kc;
  342. spin_lock_irqsave(&kc->job_lock, flags);
  343. list_add(&job->list, jobs);
  344. spin_unlock_irqrestore(&kc->job_lock, flags);
  345. }
  346. /*
  347. * These three functions process 1 item from the corresponding
  348. * job list.
  349. *
  350. * They return:
  351. * < 0: error
  352. * 0: success
  353. * > 0: can't process yet.
  354. */
  355. static int run_complete_job(struct kcopyd_job *job)
  356. {
  357. void *context = job->context;
  358. int read_err = job->read_err;
  359. unsigned long write_err = job->write_err;
  360. dm_kcopyd_notify_fn fn = job->fn;
  361. struct dm_kcopyd_client *kc = job->kc;
  362. if (job->pages && job->pages != &zero_page_list)
  363. kcopyd_put_pages(kc, job->pages);
  364. /*
  365. * If this is the master job, the sub jobs have already
  366. * completed so we can free everything.
  367. */
  368. if (job->master_job == job)
  369. mempool_free(job, kc->job_pool);
  370. fn(read_err, write_err, context);
  371. if (atomic_dec_and_test(&kc->nr_jobs))
  372. wake_up(&kc->destroyq);
  373. cond_resched();
  374. return 0;
  375. }
  376. static void complete_io(unsigned long error, void *context)
  377. {
  378. struct kcopyd_job *job = (struct kcopyd_job *) context;
  379. struct dm_kcopyd_client *kc = job->kc;
  380. io_job_finish(kc->throttle);
  381. if (error) {
  382. if (job->rw & WRITE)
  383. job->write_err |= error;
  384. else
  385. job->read_err = 1;
  386. if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  387. push(&kc->complete_jobs, job);
  388. wake(kc);
  389. return;
  390. }
  391. }
  392. if (job->rw & WRITE)
  393. push(&kc->complete_jobs, job);
  394. else {
  395. job->rw = WRITE;
  396. push(&kc->io_jobs, job);
  397. }
  398. wake(kc);
  399. }
  400. /*
  401. * Request io on as many buffer heads as we can currently get for
  402. * a particular job.
  403. */
  404. static int run_io_job(struct kcopyd_job *job)
  405. {
  406. int r;
  407. struct dm_io_request io_req = {
  408. .bi_rw = job->rw,
  409. .mem.type = DM_IO_PAGE_LIST,
  410. .mem.ptr.pl = job->pages,
  411. .mem.offset = 0,
  412. .notify.fn = complete_io,
  413. .notify.context = job,
  414. .client = job->kc->io_client,
  415. };
  416. io_job_start(job->kc->throttle);
  417. if (job->rw == READ)
  418. r = dm_io(&io_req, 1, &job->source, NULL);
  419. else
  420. r = dm_io(&io_req, job->num_dests, job->dests, NULL);
  421. return r;
  422. }
  423. static int run_pages_job(struct kcopyd_job *job)
  424. {
  425. int r;
  426. unsigned nr_pages = dm_div_up(job->dests[0].count, PAGE_SIZE >> 9);
  427. r = kcopyd_get_pages(job->kc, nr_pages, &job->pages);
  428. if (!r) {
  429. /* this job is ready for io */
  430. push(&job->kc->io_jobs, job);
  431. return 0;
  432. }
  433. if (r == -ENOMEM)
  434. /* can't complete now */
  435. return 1;
  436. return r;
  437. }
  438. /*
  439. * Run through a list for as long as possible. Returns the count
  440. * of successful jobs.
  441. */
  442. static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
  443. int (*fn) (struct kcopyd_job *))
  444. {
  445. struct kcopyd_job *job;
  446. int r, count = 0;
  447. while ((job = pop(jobs, kc))) {
  448. r = fn(job);
  449. if (r < 0) {
  450. /* error this rogue job */
  451. if (job->rw & WRITE)
  452. job->write_err = (unsigned long) -1L;
  453. else
  454. job->read_err = 1;
  455. push(&kc->complete_jobs, job);
  456. break;
  457. }
  458. if (r > 0) {
  459. /*
  460. * We couldn't service this job ATM, so
  461. * push this job back onto the list.
  462. */
  463. push_head(jobs, job);
  464. break;
  465. }
  466. count++;
  467. }
  468. return count;
  469. }
  470. /*
  471. * kcopyd does this every time it's woken up.
  472. */
  473. static void do_work(struct work_struct *work)
  474. {
  475. struct dm_kcopyd_client *kc = container_of(work,
  476. struct dm_kcopyd_client, kcopyd_work);
  477. struct blk_plug plug;
  478. unsigned long flags;
  479. /*
  480. * The order that these are called is *very* important.
  481. * complete jobs can free some pages for pages jobs.
  482. * Pages jobs when successful will jump onto the io jobs
  483. * list. io jobs call wake when they complete and it all
  484. * starts again.
  485. */
  486. spin_lock_irqsave(&kc->job_lock, flags);
  487. list_splice_tail_init(&kc->callback_jobs, &kc->complete_jobs);
  488. spin_unlock_irqrestore(&kc->job_lock, flags);
  489. blk_start_plug(&plug);
  490. process_jobs(&kc->complete_jobs, kc, run_complete_job);
  491. process_jobs(&kc->pages_jobs, kc, run_pages_job);
  492. process_jobs(&kc->io_jobs, kc, run_io_job);
  493. blk_finish_plug(&plug);
  494. }
  495. /*
  496. * If we are copying a small region we just dispatch a single job
  497. * to do the copy, otherwise the io has to be split up into many
  498. * jobs.
  499. */
  500. static void dispatch_job(struct kcopyd_job *job)
  501. {
  502. struct dm_kcopyd_client *kc = job->kc;
  503. atomic_inc(&kc->nr_jobs);
  504. if (unlikely(!job->source.count))
  505. push(&kc->callback_jobs, job);
  506. else if (job->pages == &zero_page_list)
  507. push(&kc->io_jobs, job);
  508. else
  509. push(&kc->pages_jobs, job);
  510. wake(kc);
  511. }
  512. static void segment_complete(int read_err, unsigned long write_err,
  513. void *context)
  514. {
  515. /* FIXME: tidy this function */
  516. sector_t progress = 0;
  517. sector_t count = 0;
  518. struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
  519. struct kcopyd_job *job = sub_job->master_job;
  520. struct dm_kcopyd_client *kc = job->kc;
  521. mutex_lock(&job->lock);
  522. /* update the error */
  523. if (read_err)
  524. job->read_err = 1;
  525. if (write_err)
  526. job->write_err |= write_err;
  527. /*
  528. * Only dispatch more work if there hasn't been an error.
  529. */
  530. if ((!job->read_err && !job->write_err) ||
  531. test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  532. /* get the next chunk of work */
  533. progress = job->progress;
  534. count = job->source.count - progress;
  535. if (count) {
  536. if (count > SUB_JOB_SIZE)
  537. count = SUB_JOB_SIZE;
  538. job->progress += count;
  539. }
  540. }
  541. mutex_unlock(&job->lock);
  542. if (count) {
  543. int i;
  544. *sub_job = *job;
  545. sub_job->source.sector += progress;
  546. sub_job->source.count = count;
  547. for (i = 0; i < job->num_dests; i++) {
  548. sub_job->dests[i].sector += progress;
  549. sub_job->dests[i].count = count;
  550. }
  551. sub_job->fn = segment_complete;
  552. sub_job->context = sub_job;
  553. dispatch_job(sub_job);
  554. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  555. /*
  556. * Queue the completion callback to the kcopyd thread.
  557. *
  558. * Some callers assume that all the completions are called
  559. * from a single thread and don't race with each other.
  560. *
  561. * We must not call the callback directly here because this
  562. * code may not be executing in the thread.
  563. */
  564. push(&kc->complete_jobs, job);
  565. wake(kc);
  566. }
  567. }
  568. /*
  569. * Create some sub jobs to share the work between them.
  570. */
  571. static void split_job(struct kcopyd_job *master_job)
  572. {
  573. int i;
  574. atomic_inc(&master_job->kc->nr_jobs);
  575. atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
  576. for (i = 0; i < SPLIT_COUNT; i++) {
  577. master_job[i + 1].master_job = master_job;
  578. segment_complete(0, 0u, &master_job[i + 1]);
  579. }
  580. }
  581. int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
  582. unsigned int num_dests, struct dm_io_region *dests,
  583. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  584. {
  585. struct kcopyd_job *job;
  586. int i;
  587. /*
  588. * Allocate an array of jobs consisting of one master job
  589. * followed by SPLIT_COUNT sub jobs.
  590. */
  591. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  592. /*
  593. * set up for the read.
  594. */
  595. job->kc = kc;
  596. job->flags = flags;
  597. job->read_err = 0;
  598. job->write_err = 0;
  599. job->num_dests = num_dests;
  600. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  601. if (from) {
  602. job->source = *from;
  603. job->pages = NULL;
  604. job->rw = READ;
  605. } else {
  606. memset(&job->source, 0, sizeof job->source);
  607. job->source.count = job->dests[0].count;
  608. job->pages = &zero_page_list;
  609. /*
  610. * Use WRITE SAME to optimize zeroing if all dests support it.
  611. */
  612. job->rw = WRITE | REQ_WRITE_SAME;
  613. for (i = 0; i < job->num_dests; i++)
  614. if (!bdev_write_same(job->dests[i].bdev)) {
  615. job->rw = WRITE;
  616. break;
  617. }
  618. }
  619. job->fn = fn;
  620. job->context = context;
  621. job->master_job = job;
  622. if (job->source.count <= SUB_JOB_SIZE)
  623. dispatch_job(job);
  624. else {
  625. mutex_init(&job->lock);
  626. job->progress = 0;
  627. split_job(job);
  628. }
  629. return 0;
  630. }
  631. EXPORT_SYMBOL(dm_kcopyd_copy);
  632. int dm_kcopyd_zero(struct dm_kcopyd_client *kc,
  633. unsigned num_dests, struct dm_io_region *dests,
  634. unsigned flags, dm_kcopyd_notify_fn fn, void *context)
  635. {
  636. return dm_kcopyd_copy(kc, NULL, num_dests, dests, flags, fn, context);
  637. }
  638. EXPORT_SYMBOL(dm_kcopyd_zero);
  639. void *dm_kcopyd_prepare_callback(struct dm_kcopyd_client *kc,
  640. dm_kcopyd_notify_fn fn, void *context)
  641. {
  642. struct kcopyd_job *job;
  643. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  644. memset(job, 0, sizeof(struct kcopyd_job));
  645. job->kc = kc;
  646. job->fn = fn;
  647. job->context = context;
  648. job->master_job = job;
  649. atomic_inc(&kc->nr_jobs);
  650. return job;
  651. }
  652. EXPORT_SYMBOL(dm_kcopyd_prepare_callback);
  653. void dm_kcopyd_do_callback(void *j, int read_err, unsigned long write_err)
  654. {
  655. struct kcopyd_job *job = j;
  656. struct dm_kcopyd_client *kc = job->kc;
  657. job->read_err = read_err;
  658. job->write_err = write_err;
  659. push(&kc->callback_jobs, job);
  660. wake(kc);
  661. }
  662. EXPORT_SYMBOL(dm_kcopyd_do_callback);
  663. /*
  664. * Cancels a kcopyd job, eg. someone might be deactivating a
  665. * mirror.
  666. */
  667. #if 0
  668. int kcopyd_cancel(struct kcopyd_job *job, int block)
  669. {
  670. /* FIXME: finish */
  671. return -1;
  672. }
  673. #endif /* 0 */
  674. /*-----------------------------------------------------------------
  675. * Client setup
  676. *---------------------------------------------------------------*/
  677. struct dm_kcopyd_client *dm_kcopyd_client_create(struct dm_kcopyd_throttle *throttle)
  678. {
  679. int r = -ENOMEM;
  680. struct dm_kcopyd_client *kc;
  681. kc = kmalloc(sizeof(*kc), GFP_KERNEL);
  682. if (!kc)
  683. return ERR_PTR(-ENOMEM);
  684. spin_lock_init(&kc->job_lock);
  685. INIT_LIST_HEAD(&kc->callback_jobs);
  686. INIT_LIST_HEAD(&kc->complete_jobs);
  687. INIT_LIST_HEAD(&kc->io_jobs);
  688. INIT_LIST_HEAD(&kc->pages_jobs);
  689. kc->throttle = throttle;
  690. kc->job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
  691. if (!kc->job_pool)
  692. goto bad_slab;
  693. INIT_WORK(&kc->kcopyd_work, do_work);
  694. kc->kcopyd_wq = alloc_workqueue("kcopyd", WQ_MEM_RECLAIM, 0);
  695. if (!kc->kcopyd_wq)
  696. goto bad_workqueue;
  697. kc->pages = NULL;
  698. kc->nr_reserved_pages = kc->nr_free_pages = 0;
  699. r = client_reserve_pages(kc, RESERVE_PAGES);
  700. if (r)
  701. goto bad_client_pages;
  702. kc->io_client = dm_io_client_create();
  703. if (IS_ERR(kc->io_client)) {
  704. r = PTR_ERR(kc->io_client);
  705. goto bad_io_client;
  706. }
  707. init_waitqueue_head(&kc->destroyq);
  708. atomic_set(&kc->nr_jobs, 0);
  709. return kc;
  710. bad_io_client:
  711. client_free_pages(kc);
  712. bad_client_pages:
  713. destroy_workqueue(kc->kcopyd_wq);
  714. bad_workqueue:
  715. mempool_destroy(kc->job_pool);
  716. bad_slab:
  717. kfree(kc);
  718. return ERR_PTR(r);
  719. }
  720. EXPORT_SYMBOL(dm_kcopyd_client_create);
  721. void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
  722. {
  723. /* Wait for completion of all jobs submitted by this client. */
  724. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  725. BUG_ON(!list_empty(&kc->callback_jobs));
  726. BUG_ON(!list_empty(&kc->complete_jobs));
  727. BUG_ON(!list_empty(&kc->io_jobs));
  728. BUG_ON(!list_empty(&kc->pages_jobs));
  729. destroy_workqueue(kc->kcopyd_wq);
  730. dm_io_client_destroy(kc->io_client);
  731. client_free_pages(kc);
  732. mempool_destroy(kc->job_pool);
  733. kfree(kc);
  734. }
  735. EXPORT_SYMBOL(dm_kcopyd_client_destroy);