dm-kcopyd.c 21 KB

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