dm-kcopyd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. mempool_t job_pool;
  43. struct workqueue_struct *kcopyd_wq;
  44. struct work_struct kcopyd_work;
  45. struct dm_kcopyd_throttle *throttle;
  46. atomic_t nr_jobs;
  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 = READ_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(READ_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. sector_t write_offset;
  295. struct kcopyd_job *master_job;
  296. };
  297. static struct kmem_cache *_job_cache;
  298. int __init dm_kcopyd_init(void)
  299. {
  300. _job_cache = kmem_cache_create("kcopyd_job",
  301. sizeof(struct kcopyd_job) * (SPLIT_COUNT + 1),
  302. __alignof__(struct kcopyd_job), 0, NULL);
  303. if (!_job_cache)
  304. return -ENOMEM;
  305. zero_page_list.next = &zero_page_list;
  306. zero_page_list.page = ZERO_PAGE(0);
  307. return 0;
  308. }
  309. void dm_kcopyd_exit(void)
  310. {
  311. kmem_cache_destroy(_job_cache);
  312. _job_cache = NULL;
  313. }
  314. /*
  315. * Functions to push and pop a job onto the head of a given job
  316. * list.
  317. */
  318. static struct kcopyd_job *pop_io_job(struct list_head *jobs,
  319. struct dm_kcopyd_client *kc)
  320. {
  321. struct kcopyd_job *job;
  322. /*
  323. * For I/O jobs, pop any read, any write without sequential write
  324. * constraint and sequential writes that are at the right position.
  325. */
  326. list_for_each_entry(job, jobs, list) {
  327. if (job->rw == READ || !test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags)) {
  328. list_del(&job->list);
  329. return job;
  330. }
  331. if (job->write_offset == job->master_job->write_offset) {
  332. job->master_job->write_offset += job->source.count;
  333. list_del(&job->list);
  334. return job;
  335. }
  336. }
  337. return NULL;
  338. }
  339. static struct kcopyd_job *pop(struct list_head *jobs,
  340. struct dm_kcopyd_client *kc)
  341. {
  342. struct kcopyd_job *job = NULL;
  343. unsigned long flags;
  344. spin_lock_irqsave(&kc->job_lock, flags);
  345. if (!list_empty(jobs)) {
  346. if (jobs == &kc->io_jobs)
  347. job = pop_io_job(jobs, kc);
  348. else {
  349. job = list_entry(jobs->next, struct kcopyd_job, list);
  350. list_del(&job->list);
  351. }
  352. }
  353. spin_unlock_irqrestore(&kc->job_lock, flags);
  354. return job;
  355. }
  356. static void push(struct list_head *jobs, struct kcopyd_job *job)
  357. {
  358. unsigned long flags;
  359. struct dm_kcopyd_client *kc = job->kc;
  360. spin_lock_irqsave(&kc->job_lock, flags);
  361. list_add_tail(&job->list, jobs);
  362. spin_unlock_irqrestore(&kc->job_lock, flags);
  363. }
  364. static void push_head(struct list_head *jobs, struct kcopyd_job *job)
  365. {
  366. unsigned long flags;
  367. struct dm_kcopyd_client *kc = job->kc;
  368. spin_lock_irqsave(&kc->job_lock, flags);
  369. list_add(&job->list, jobs);
  370. spin_unlock_irqrestore(&kc->job_lock, flags);
  371. }
  372. /*
  373. * These three functions process 1 item from the corresponding
  374. * job list.
  375. *
  376. * They return:
  377. * < 0: error
  378. * 0: success
  379. * > 0: can't process yet.
  380. */
  381. static int run_complete_job(struct kcopyd_job *job)
  382. {
  383. void *context = job->context;
  384. int read_err = job->read_err;
  385. unsigned long write_err = job->write_err;
  386. dm_kcopyd_notify_fn fn = job->fn;
  387. struct dm_kcopyd_client *kc = job->kc;
  388. if (job->pages && job->pages != &zero_page_list)
  389. kcopyd_put_pages(kc, job->pages);
  390. /*
  391. * If this is the master job, the sub jobs have already
  392. * completed so we can free everything.
  393. */
  394. if (job->master_job == job) {
  395. mutex_destroy(&job->lock);
  396. mempool_free(job, &kc->job_pool);
  397. }
  398. fn(read_err, write_err, context);
  399. if (atomic_dec_and_test(&kc->nr_jobs))
  400. wake_up(&kc->destroyq);
  401. cond_resched();
  402. return 0;
  403. }
  404. static void complete_io(unsigned long error, void *context)
  405. {
  406. struct kcopyd_job *job = (struct kcopyd_job *) context;
  407. struct dm_kcopyd_client *kc = job->kc;
  408. io_job_finish(kc->throttle);
  409. if (error) {
  410. if (op_is_write(job->rw))
  411. job->write_err |= error;
  412. else
  413. job->read_err = 1;
  414. if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  415. push(&kc->complete_jobs, job);
  416. wake(kc);
  417. return;
  418. }
  419. }
  420. if (op_is_write(job->rw))
  421. push(&kc->complete_jobs, job);
  422. else {
  423. job->rw = WRITE;
  424. push(&kc->io_jobs, job);
  425. }
  426. wake(kc);
  427. }
  428. /*
  429. * Request io on as many buffer heads as we can currently get for
  430. * a particular job.
  431. */
  432. static int run_io_job(struct kcopyd_job *job)
  433. {
  434. int r;
  435. struct dm_io_request io_req = {
  436. .bi_op = job->rw,
  437. .bi_op_flags = 0,
  438. .mem.type = DM_IO_PAGE_LIST,
  439. .mem.ptr.pl = job->pages,
  440. .mem.offset = 0,
  441. .notify.fn = complete_io,
  442. .notify.context = job,
  443. .client = job->kc->io_client,
  444. };
  445. /*
  446. * If we need to write sequentially and some reads or writes failed,
  447. * no point in continuing.
  448. */
  449. if (test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags) &&
  450. job->master_job->write_err) {
  451. job->write_err = job->master_job->write_err;
  452. return -EIO;
  453. }
  454. io_job_start(job->kc->throttle);
  455. if (job->rw == READ)
  456. r = dm_io(&io_req, 1, &job->source, NULL);
  457. else
  458. r = dm_io(&io_req, job->num_dests, job->dests, NULL);
  459. return r;
  460. }
  461. static int run_pages_job(struct kcopyd_job *job)
  462. {
  463. int r;
  464. unsigned nr_pages = dm_div_up(job->dests[0].count, PAGE_SIZE >> 9);
  465. r = kcopyd_get_pages(job->kc, nr_pages, &job->pages);
  466. if (!r) {
  467. /* this job is ready for io */
  468. push(&job->kc->io_jobs, job);
  469. return 0;
  470. }
  471. if (r == -ENOMEM)
  472. /* can't complete now */
  473. return 1;
  474. return r;
  475. }
  476. /*
  477. * Run through a list for as long as possible. Returns the count
  478. * of successful jobs.
  479. */
  480. static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
  481. int (*fn) (struct kcopyd_job *))
  482. {
  483. struct kcopyd_job *job;
  484. int r, count = 0;
  485. while ((job = pop(jobs, kc))) {
  486. r = fn(job);
  487. if (r < 0) {
  488. /* error this rogue job */
  489. if (op_is_write(job->rw))
  490. job->write_err = (unsigned long) -1L;
  491. else
  492. job->read_err = 1;
  493. push(&kc->complete_jobs, job);
  494. wake(kc);
  495. break;
  496. }
  497. if (r > 0) {
  498. /*
  499. * We couldn't service this job ATM, so
  500. * push this job back onto the list.
  501. */
  502. push_head(jobs, job);
  503. break;
  504. }
  505. count++;
  506. }
  507. return count;
  508. }
  509. /*
  510. * kcopyd does this every time it's woken up.
  511. */
  512. static void do_work(struct work_struct *work)
  513. {
  514. struct dm_kcopyd_client *kc = container_of(work,
  515. struct dm_kcopyd_client, kcopyd_work);
  516. struct blk_plug plug;
  517. unsigned long flags;
  518. /*
  519. * The order that these are called is *very* important.
  520. * complete jobs can free some pages for pages jobs.
  521. * Pages jobs when successful will jump onto the io jobs
  522. * list. io jobs call wake when they complete and it all
  523. * starts again.
  524. */
  525. spin_lock_irqsave(&kc->job_lock, flags);
  526. list_splice_tail_init(&kc->callback_jobs, &kc->complete_jobs);
  527. spin_unlock_irqrestore(&kc->job_lock, flags);
  528. blk_start_plug(&plug);
  529. process_jobs(&kc->complete_jobs, kc, run_complete_job);
  530. process_jobs(&kc->pages_jobs, kc, run_pages_job);
  531. process_jobs(&kc->io_jobs, kc, run_io_job);
  532. blk_finish_plug(&plug);
  533. }
  534. /*
  535. * If we are copying a small region we just dispatch a single job
  536. * to do the copy, otherwise the io has to be split up into many
  537. * jobs.
  538. */
  539. static void dispatch_job(struct kcopyd_job *job)
  540. {
  541. struct dm_kcopyd_client *kc = job->kc;
  542. atomic_inc(&kc->nr_jobs);
  543. if (unlikely(!job->source.count))
  544. push(&kc->callback_jobs, job);
  545. else if (job->pages == &zero_page_list)
  546. push(&kc->io_jobs, job);
  547. else
  548. push(&kc->pages_jobs, job);
  549. wake(kc);
  550. }
  551. static void segment_complete(int read_err, unsigned long write_err,
  552. void *context)
  553. {
  554. /* FIXME: tidy this function */
  555. sector_t progress = 0;
  556. sector_t count = 0;
  557. struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
  558. struct kcopyd_job *job = sub_job->master_job;
  559. struct dm_kcopyd_client *kc = job->kc;
  560. mutex_lock(&job->lock);
  561. /* update the error */
  562. if (read_err)
  563. job->read_err = 1;
  564. if (write_err)
  565. job->write_err |= write_err;
  566. /*
  567. * Only dispatch more work if there hasn't been an error.
  568. */
  569. if ((!job->read_err && !job->write_err) ||
  570. test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  571. /* get the next chunk of work */
  572. progress = job->progress;
  573. count = job->source.count - progress;
  574. if (count) {
  575. if (count > SUB_JOB_SIZE)
  576. count = SUB_JOB_SIZE;
  577. job->progress += count;
  578. }
  579. }
  580. mutex_unlock(&job->lock);
  581. if (count) {
  582. int i;
  583. *sub_job = *job;
  584. sub_job->write_offset = progress;
  585. sub_job->source.sector += progress;
  586. sub_job->source.count = count;
  587. for (i = 0; i < job->num_dests; i++) {
  588. sub_job->dests[i].sector += progress;
  589. sub_job->dests[i].count = count;
  590. }
  591. sub_job->fn = segment_complete;
  592. sub_job->context = sub_job;
  593. dispatch_job(sub_job);
  594. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  595. /*
  596. * Queue the completion callback to the kcopyd thread.
  597. *
  598. * Some callers assume that all the completions are called
  599. * from a single thread and don't race with each other.
  600. *
  601. * We must not call the callback directly here because this
  602. * code may not be executing in the thread.
  603. */
  604. push(&kc->complete_jobs, job);
  605. wake(kc);
  606. }
  607. }
  608. /*
  609. * Create some sub jobs to share the work between them.
  610. */
  611. static void split_job(struct kcopyd_job *master_job)
  612. {
  613. int i;
  614. atomic_inc(&master_job->kc->nr_jobs);
  615. atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
  616. for (i = 0; i < SPLIT_COUNT; i++) {
  617. master_job[i + 1].master_job = master_job;
  618. segment_complete(0, 0u, &master_job[i + 1]);
  619. }
  620. }
  621. void dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
  622. unsigned int num_dests, struct dm_io_region *dests,
  623. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  624. {
  625. struct kcopyd_job *job;
  626. int i;
  627. /*
  628. * Allocate an array of jobs consisting of one master job
  629. * followed by SPLIT_COUNT sub jobs.
  630. */
  631. job = mempool_alloc(&kc->job_pool, GFP_NOIO);
  632. mutex_init(&job->lock);
  633. /*
  634. * set up for the read.
  635. */
  636. job->kc = kc;
  637. job->flags = flags;
  638. job->read_err = 0;
  639. job->write_err = 0;
  640. job->num_dests = num_dests;
  641. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  642. /*
  643. * If one of the destination is a host-managed zoned block device,
  644. * we need to write sequentially. If one of the destination is a
  645. * host-aware device, then leave it to the caller to choose what to do.
  646. */
  647. if (!test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags)) {
  648. for (i = 0; i < job->num_dests; i++) {
  649. if (bdev_zoned_model(dests[i].bdev) == BLK_ZONED_HM) {
  650. set_bit(DM_KCOPYD_WRITE_SEQ, &job->flags);
  651. break;
  652. }
  653. }
  654. }
  655. /*
  656. * If we need to write sequentially, errors cannot be ignored.
  657. */
  658. if (test_bit(DM_KCOPYD_WRITE_SEQ, &job->flags) &&
  659. test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags))
  660. clear_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags);
  661. if (from) {
  662. job->source = *from;
  663. job->pages = NULL;
  664. job->rw = READ;
  665. } else {
  666. memset(&job->source, 0, sizeof job->source);
  667. job->source.count = job->dests[0].count;
  668. job->pages = &zero_page_list;
  669. /*
  670. * Use WRITE ZEROES to optimize zeroing if all dests support it.
  671. */
  672. job->rw = REQ_OP_WRITE_ZEROES;
  673. for (i = 0; i < job->num_dests; i++)
  674. if (!bdev_write_zeroes_sectors(job->dests[i].bdev)) {
  675. job->rw = WRITE;
  676. break;
  677. }
  678. }
  679. job->fn = fn;
  680. job->context = context;
  681. job->master_job = job;
  682. job->write_offset = 0;
  683. if (job->source.count <= SUB_JOB_SIZE)
  684. dispatch_job(job);
  685. else {
  686. job->progress = 0;
  687. split_job(job);
  688. }
  689. }
  690. EXPORT_SYMBOL(dm_kcopyd_copy);
  691. void dm_kcopyd_zero(struct dm_kcopyd_client *kc,
  692. unsigned num_dests, struct dm_io_region *dests,
  693. unsigned flags, dm_kcopyd_notify_fn fn, void *context)
  694. {
  695. dm_kcopyd_copy(kc, NULL, num_dests, dests, flags, fn, context);
  696. }
  697. EXPORT_SYMBOL(dm_kcopyd_zero);
  698. void *dm_kcopyd_prepare_callback(struct dm_kcopyd_client *kc,
  699. dm_kcopyd_notify_fn fn, void *context)
  700. {
  701. struct kcopyd_job *job;
  702. job = mempool_alloc(&kc->job_pool, GFP_NOIO);
  703. memset(job, 0, sizeof(struct kcopyd_job));
  704. job->kc = kc;
  705. job->fn = fn;
  706. job->context = context;
  707. job->master_job = job;
  708. atomic_inc(&kc->nr_jobs);
  709. return job;
  710. }
  711. EXPORT_SYMBOL(dm_kcopyd_prepare_callback);
  712. void dm_kcopyd_do_callback(void *j, int read_err, unsigned long write_err)
  713. {
  714. struct kcopyd_job *job = j;
  715. struct dm_kcopyd_client *kc = job->kc;
  716. job->read_err = read_err;
  717. job->write_err = write_err;
  718. push(&kc->callback_jobs, job);
  719. wake(kc);
  720. }
  721. EXPORT_SYMBOL(dm_kcopyd_do_callback);
  722. /*
  723. * Cancels a kcopyd job, eg. someone might be deactivating a
  724. * mirror.
  725. */
  726. #if 0
  727. int kcopyd_cancel(struct kcopyd_job *job, int block)
  728. {
  729. /* FIXME: finish */
  730. return -1;
  731. }
  732. #endif /* 0 */
  733. /*-----------------------------------------------------------------
  734. * Client setup
  735. *---------------------------------------------------------------*/
  736. struct dm_kcopyd_client *dm_kcopyd_client_create(struct dm_kcopyd_throttle *throttle)
  737. {
  738. int r;
  739. struct dm_kcopyd_client *kc;
  740. kc = kzalloc(sizeof(*kc), GFP_KERNEL);
  741. if (!kc)
  742. return ERR_PTR(-ENOMEM);
  743. spin_lock_init(&kc->job_lock);
  744. INIT_LIST_HEAD(&kc->callback_jobs);
  745. INIT_LIST_HEAD(&kc->complete_jobs);
  746. INIT_LIST_HEAD(&kc->io_jobs);
  747. INIT_LIST_HEAD(&kc->pages_jobs);
  748. kc->throttle = throttle;
  749. r = mempool_init_slab_pool(&kc->job_pool, MIN_JOBS, _job_cache);
  750. if (r)
  751. goto bad_slab;
  752. INIT_WORK(&kc->kcopyd_work, do_work);
  753. kc->kcopyd_wq = alloc_workqueue("kcopyd", WQ_MEM_RECLAIM, 0);
  754. if (!kc->kcopyd_wq) {
  755. r = -ENOMEM;
  756. goto bad_workqueue;
  757. }
  758. kc->pages = NULL;
  759. kc->nr_reserved_pages = kc->nr_free_pages = 0;
  760. r = client_reserve_pages(kc, RESERVE_PAGES);
  761. if (r)
  762. goto bad_client_pages;
  763. kc->io_client = dm_io_client_create();
  764. if (IS_ERR(kc->io_client)) {
  765. r = PTR_ERR(kc->io_client);
  766. goto bad_io_client;
  767. }
  768. init_waitqueue_head(&kc->destroyq);
  769. atomic_set(&kc->nr_jobs, 0);
  770. return kc;
  771. bad_io_client:
  772. client_free_pages(kc);
  773. bad_client_pages:
  774. destroy_workqueue(kc->kcopyd_wq);
  775. bad_workqueue:
  776. mempool_exit(&kc->job_pool);
  777. bad_slab:
  778. kfree(kc);
  779. return ERR_PTR(r);
  780. }
  781. EXPORT_SYMBOL(dm_kcopyd_client_create);
  782. void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
  783. {
  784. /* Wait for completion of all jobs submitted by this client. */
  785. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  786. BUG_ON(!list_empty(&kc->callback_jobs));
  787. BUG_ON(!list_empty(&kc->complete_jobs));
  788. BUG_ON(!list_empty(&kc->io_jobs));
  789. BUG_ON(!list_empty(&kc->pages_jobs));
  790. destroy_workqueue(kc->kcopyd_wq);
  791. dm_io_client_destroy(kc->io_client);
  792. client_free_pages(kc);
  793. mempool_exit(&kc->job_pool);
  794. kfree(kc);
  795. }
  796. EXPORT_SYMBOL(dm_kcopyd_client_destroy);