dm-kcopyd.c 19 KB

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