dm-kcopyd.c 22 KB

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