dm-kcopyd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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-core.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 (op_is_write(job->rw))
  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 (op_is_write(job->rw))
  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_op = job->rw,
  409. .bi_op_flags = 0,
  410. .mem.type = DM_IO_PAGE_LIST,
  411. .mem.ptr.pl = job->pages,
  412. .mem.offset = 0,
  413. .notify.fn = complete_io,
  414. .notify.context = job,
  415. .client = job->kc->io_client,
  416. };
  417. io_job_start(job->kc->throttle);
  418. if (job->rw == READ)
  419. r = dm_io(&io_req, 1, &job->source, NULL);
  420. else
  421. r = dm_io(&io_req, job->num_dests, job->dests, NULL);
  422. return r;
  423. }
  424. static int run_pages_job(struct kcopyd_job *job)
  425. {
  426. int r;
  427. unsigned nr_pages = dm_div_up(job->dests[0].count, PAGE_SIZE >> 9);
  428. r = kcopyd_get_pages(job->kc, nr_pages, &job->pages);
  429. if (!r) {
  430. /* this job is ready for io */
  431. push(&job->kc->io_jobs, job);
  432. return 0;
  433. }
  434. if (r == -ENOMEM)
  435. /* can't complete now */
  436. return 1;
  437. return r;
  438. }
  439. /*
  440. * Run through a list for as long as possible. Returns the count
  441. * of successful jobs.
  442. */
  443. static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
  444. int (*fn) (struct kcopyd_job *))
  445. {
  446. struct kcopyd_job *job;
  447. int r, count = 0;
  448. while ((job = pop(jobs, kc))) {
  449. r = fn(job);
  450. if (r < 0) {
  451. /* error this rogue job */
  452. if (op_is_write(job->rw))
  453. job->write_err = (unsigned long) -1L;
  454. else
  455. job->read_err = 1;
  456. push(&kc->complete_jobs, job);
  457. break;
  458. }
  459. if (r > 0) {
  460. /*
  461. * We couldn't service this job ATM, so
  462. * push this job back onto the list.
  463. */
  464. push_head(jobs, job);
  465. break;
  466. }
  467. count++;
  468. }
  469. return count;
  470. }
  471. /*
  472. * kcopyd does this every time it's woken up.
  473. */
  474. static void do_work(struct work_struct *work)
  475. {
  476. struct dm_kcopyd_client *kc = container_of(work,
  477. struct dm_kcopyd_client, kcopyd_work);
  478. struct blk_plug plug;
  479. unsigned long flags;
  480. /*
  481. * The order that these are called is *very* important.
  482. * complete jobs can free some pages for pages jobs.
  483. * Pages jobs when successful will jump onto the io jobs
  484. * list. io jobs call wake when they complete and it all
  485. * starts again.
  486. */
  487. spin_lock_irqsave(&kc->job_lock, flags);
  488. list_splice_tail_init(&kc->callback_jobs, &kc->complete_jobs);
  489. spin_unlock_irqrestore(&kc->job_lock, flags);
  490. blk_start_plug(&plug);
  491. process_jobs(&kc->complete_jobs, kc, run_complete_job);
  492. process_jobs(&kc->pages_jobs, kc, run_pages_job);
  493. process_jobs(&kc->io_jobs, kc, run_io_job);
  494. blk_finish_plug(&plug);
  495. }
  496. /*
  497. * If we are copying a small region we just dispatch a single job
  498. * to do the copy, otherwise the io has to be split up into many
  499. * jobs.
  500. */
  501. static void dispatch_job(struct kcopyd_job *job)
  502. {
  503. struct dm_kcopyd_client *kc = job->kc;
  504. atomic_inc(&kc->nr_jobs);
  505. if (unlikely(!job->source.count))
  506. push(&kc->callback_jobs, job);
  507. else if (job->pages == &zero_page_list)
  508. push(&kc->io_jobs, job);
  509. else
  510. push(&kc->pages_jobs, job);
  511. wake(kc);
  512. }
  513. static void segment_complete(int read_err, unsigned long write_err,
  514. void *context)
  515. {
  516. /* FIXME: tidy this function */
  517. sector_t progress = 0;
  518. sector_t count = 0;
  519. struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
  520. struct kcopyd_job *job = sub_job->master_job;
  521. struct dm_kcopyd_client *kc = job->kc;
  522. mutex_lock(&job->lock);
  523. /* update the error */
  524. if (read_err)
  525. job->read_err = 1;
  526. if (write_err)
  527. job->write_err |= write_err;
  528. /*
  529. * Only dispatch more work if there hasn't been an error.
  530. */
  531. if ((!job->read_err && !job->write_err) ||
  532. test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  533. /* get the next chunk of work */
  534. progress = job->progress;
  535. count = job->source.count - progress;
  536. if (count) {
  537. if (count > SUB_JOB_SIZE)
  538. count = SUB_JOB_SIZE;
  539. job->progress += count;
  540. }
  541. }
  542. mutex_unlock(&job->lock);
  543. if (count) {
  544. int i;
  545. *sub_job = *job;
  546. sub_job->source.sector += progress;
  547. sub_job->source.count = count;
  548. for (i = 0; i < job->num_dests; i++) {
  549. sub_job->dests[i].sector += progress;
  550. sub_job->dests[i].count = count;
  551. }
  552. sub_job->fn = segment_complete;
  553. sub_job->context = sub_job;
  554. dispatch_job(sub_job);
  555. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  556. /*
  557. * Queue the completion callback to the kcopyd thread.
  558. *
  559. * Some callers assume that all the completions are called
  560. * from a single thread and don't race with each other.
  561. *
  562. * We must not call the callback directly here because this
  563. * code may not be executing in the thread.
  564. */
  565. push(&kc->complete_jobs, job);
  566. wake(kc);
  567. }
  568. }
  569. /*
  570. * Create some sub jobs to share the work between them.
  571. */
  572. static void split_job(struct kcopyd_job *master_job)
  573. {
  574. int i;
  575. atomic_inc(&master_job->kc->nr_jobs);
  576. atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
  577. for (i = 0; i < SPLIT_COUNT; i++) {
  578. master_job[i + 1].master_job = master_job;
  579. segment_complete(0, 0u, &master_job[i + 1]);
  580. }
  581. }
  582. int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
  583. unsigned int num_dests, struct dm_io_region *dests,
  584. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  585. {
  586. struct kcopyd_job *job;
  587. int i;
  588. /*
  589. * Allocate an array of jobs consisting of one master job
  590. * followed by SPLIT_COUNT sub jobs.
  591. */
  592. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  593. /*
  594. * set up for the read.
  595. */
  596. job->kc = kc;
  597. job->flags = flags;
  598. job->read_err = 0;
  599. job->write_err = 0;
  600. job->num_dests = num_dests;
  601. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  602. if (from) {
  603. job->source = *from;
  604. job->pages = NULL;
  605. job->rw = READ;
  606. } else {
  607. memset(&job->source, 0, sizeof job->source);
  608. job->source.count = job->dests[0].count;
  609. job->pages = &zero_page_list;
  610. /*
  611. * Use WRITE SAME to optimize zeroing if all dests support it.
  612. */
  613. job->rw = REQ_OP_WRITE_SAME;
  614. for (i = 0; i < job->num_dests; i++)
  615. if (!bdev_write_same(job->dests[i].bdev)) {
  616. job->rw = WRITE;
  617. break;
  618. }
  619. }
  620. job->fn = fn;
  621. job->context = context;
  622. job->master_job = job;
  623. if (job->source.count <= SUB_JOB_SIZE)
  624. dispatch_job(job);
  625. else {
  626. mutex_init(&job->lock);
  627. job->progress = 0;
  628. split_job(job);
  629. }
  630. return 0;
  631. }
  632. EXPORT_SYMBOL(dm_kcopyd_copy);
  633. int dm_kcopyd_zero(struct dm_kcopyd_client *kc,
  634. unsigned num_dests, struct dm_io_region *dests,
  635. unsigned flags, dm_kcopyd_notify_fn fn, void *context)
  636. {
  637. return dm_kcopyd_copy(kc, NULL, num_dests, dests, flags, fn, context);
  638. }
  639. EXPORT_SYMBOL(dm_kcopyd_zero);
  640. void *dm_kcopyd_prepare_callback(struct dm_kcopyd_client *kc,
  641. dm_kcopyd_notify_fn fn, void *context)
  642. {
  643. struct kcopyd_job *job;
  644. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  645. memset(job, 0, sizeof(struct kcopyd_job));
  646. job->kc = kc;
  647. job->fn = fn;
  648. job->context = context;
  649. job->master_job = job;
  650. atomic_inc(&kc->nr_jobs);
  651. return job;
  652. }
  653. EXPORT_SYMBOL(dm_kcopyd_prepare_callback);
  654. void dm_kcopyd_do_callback(void *j, int read_err, unsigned long write_err)
  655. {
  656. struct kcopyd_job *job = j;
  657. struct dm_kcopyd_client *kc = job->kc;
  658. job->read_err = read_err;
  659. job->write_err = write_err;
  660. push(&kc->callback_jobs, job);
  661. wake(kc);
  662. }
  663. EXPORT_SYMBOL(dm_kcopyd_do_callback);
  664. /*
  665. * Cancels a kcopyd job, eg. someone might be deactivating a
  666. * mirror.
  667. */
  668. #if 0
  669. int kcopyd_cancel(struct kcopyd_job *job, int block)
  670. {
  671. /* FIXME: finish */
  672. return -1;
  673. }
  674. #endif /* 0 */
  675. /*-----------------------------------------------------------------
  676. * Client setup
  677. *---------------------------------------------------------------*/
  678. struct dm_kcopyd_client *dm_kcopyd_client_create(struct dm_kcopyd_throttle *throttle)
  679. {
  680. int r = -ENOMEM;
  681. struct dm_kcopyd_client *kc;
  682. kc = kzalloc(sizeof(*kc), GFP_KERNEL);
  683. if (!kc)
  684. return ERR_PTR(-ENOMEM);
  685. spin_lock_init(&kc->job_lock);
  686. INIT_LIST_HEAD(&kc->callback_jobs);
  687. INIT_LIST_HEAD(&kc->complete_jobs);
  688. INIT_LIST_HEAD(&kc->io_jobs);
  689. INIT_LIST_HEAD(&kc->pages_jobs);
  690. kc->throttle = throttle;
  691. kc->job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
  692. if (!kc->job_pool)
  693. goto bad_slab;
  694. INIT_WORK(&kc->kcopyd_work, do_work);
  695. kc->kcopyd_wq = alloc_workqueue("kcopyd", WQ_MEM_RECLAIM, 0);
  696. if (!kc->kcopyd_wq)
  697. goto bad_workqueue;
  698. kc->pages = NULL;
  699. kc->nr_reserved_pages = kc->nr_free_pages = 0;
  700. r = client_reserve_pages(kc, RESERVE_PAGES);
  701. if (r)
  702. goto bad_client_pages;
  703. kc->io_client = dm_io_client_create();
  704. if (IS_ERR(kc->io_client)) {
  705. r = PTR_ERR(kc->io_client);
  706. goto bad_io_client;
  707. }
  708. init_waitqueue_head(&kc->destroyq);
  709. atomic_set(&kc->nr_jobs, 0);
  710. return kc;
  711. bad_io_client:
  712. client_free_pages(kc);
  713. bad_client_pages:
  714. destroy_workqueue(kc->kcopyd_wq);
  715. bad_workqueue:
  716. mempool_destroy(kc->job_pool);
  717. bad_slab:
  718. kfree(kc);
  719. return ERR_PTR(r);
  720. }
  721. EXPORT_SYMBOL(dm_kcopyd_client_create);
  722. void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
  723. {
  724. /* Wait for completion of all jobs submitted by this client. */
  725. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  726. BUG_ON(!list_empty(&kc->callback_jobs));
  727. BUG_ON(!list_empty(&kc->complete_jobs));
  728. BUG_ON(!list_empty(&kc->io_jobs));
  729. BUG_ON(!list_empty(&kc->pages_jobs));
  730. destroy_workqueue(kc->kcopyd_wq);
  731. dm_io_client_destroy(kc->io_client);
  732. client_free_pages(kc);
  733. mempool_destroy(kc->job_pool);
  734. kfree(kc);
  735. }
  736. EXPORT_SYMBOL(dm_kcopyd_client_destroy);