dm-kcopyd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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/device-mapper.h>
  25. #include <linux/dm-kcopyd.h>
  26. #include "dm.h"
  27. #define SUB_JOB_SIZE 128
  28. #define SPLIT_COUNT 8
  29. #define MIN_JOBS 8
  30. #define RESERVE_PAGES (DIV_ROUND_UP(SUB_JOB_SIZE << SECTOR_SHIFT, PAGE_SIZE))
  31. /*-----------------------------------------------------------------
  32. * Each kcopyd client has its own little pool of preallocated
  33. * pages for kcopyd io.
  34. *---------------------------------------------------------------*/
  35. struct dm_kcopyd_client {
  36. struct page_list *pages;
  37. unsigned nr_reserved_pages;
  38. unsigned nr_free_pages;
  39. struct dm_io_client *io_client;
  40. wait_queue_head_t destroyq;
  41. atomic_t nr_jobs;
  42. mempool_t *job_pool;
  43. struct workqueue_struct *kcopyd_wq;
  44. struct work_struct kcopyd_work;
  45. /*
  46. * We maintain three lists of jobs:
  47. *
  48. * i) jobs waiting for pages
  49. * ii) jobs that have pages, and are waiting for the io to be issued.
  50. * iii) jobs that have completed.
  51. *
  52. * All three of these are protected by job_lock.
  53. */
  54. spinlock_t job_lock;
  55. struct list_head complete_jobs;
  56. struct list_head io_jobs;
  57. struct list_head pages_jobs;
  58. };
  59. static void wake(struct dm_kcopyd_client *kc)
  60. {
  61. queue_work(kc->kcopyd_wq, &kc->kcopyd_work);
  62. }
  63. /*
  64. * Obtain one page for the use of kcopyd.
  65. */
  66. static struct page_list *alloc_pl(gfp_t gfp)
  67. {
  68. struct page_list *pl;
  69. pl = kmalloc(sizeof(*pl), gfp);
  70. if (!pl)
  71. return NULL;
  72. pl->page = alloc_page(gfp);
  73. if (!pl->page) {
  74. kfree(pl);
  75. return NULL;
  76. }
  77. return pl;
  78. }
  79. static void free_pl(struct page_list *pl)
  80. {
  81. __free_page(pl->page);
  82. kfree(pl);
  83. }
  84. /*
  85. * Add the provided pages to a client's free page list, releasing
  86. * back to the system any beyond the reserved_pages limit.
  87. */
  88. static void kcopyd_put_pages(struct dm_kcopyd_client *kc, struct page_list *pl)
  89. {
  90. struct page_list *next;
  91. do {
  92. next = pl->next;
  93. if (kc->nr_free_pages >= kc->nr_reserved_pages)
  94. free_pl(pl);
  95. else {
  96. pl->next = kc->pages;
  97. kc->pages = pl;
  98. kc->nr_free_pages++;
  99. }
  100. pl = next;
  101. } while (pl);
  102. }
  103. static int kcopyd_get_pages(struct dm_kcopyd_client *kc,
  104. unsigned int nr, struct page_list **pages)
  105. {
  106. struct page_list *pl;
  107. *pages = NULL;
  108. do {
  109. pl = alloc_pl(__GFP_NOWARN | __GFP_NORETRY);
  110. if (unlikely(!pl)) {
  111. /* Use reserved pages */
  112. pl = kc->pages;
  113. if (unlikely(!pl))
  114. goto out_of_memory;
  115. kc->pages = pl->next;
  116. kc->nr_free_pages--;
  117. }
  118. pl->next = *pages;
  119. *pages = pl;
  120. } while (--nr);
  121. return 0;
  122. out_of_memory:
  123. if (*pages)
  124. kcopyd_put_pages(kc, *pages);
  125. return -ENOMEM;
  126. }
  127. /*
  128. * These three functions resize the page pool.
  129. */
  130. static void drop_pages(struct page_list *pl)
  131. {
  132. struct page_list *next;
  133. while (pl) {
  134. next = pl->next;
  135. free_pl(pl);
  136. pl = next;
  137. }
  138. }
  139. /*
  140. * Allocate and reserve nr_pages for the use of a specific client.
  141. */
  142. static int client_reserve_pages(struct dm_kcopyd_client *kc, unsigned nr_pages)
  143. {
  144. unsigned i;
  145. struct page_list *pl = NULL, *next;
  146. for (i = 0; i < nr_pages; i++) {
  147. next = alloc_pl(GFP_KERNEL);
  148. if (!next) {
  149. if (pl)
  150. drop_pages(pl);
  151. return -ENOMEM;
  152. }
  153. next->next = pl;
  154. pl = next;
  155. }
  156. kc->nr_reserved_pages += nr_pages;
  157. kcopyd_put_pages(kc, pl);
  158. return 0;
  159. }
  160. static void client_free_pages(struct dm_kcopyd_client *kc)
  161. {
  162. BUG_ON(kc->nr_free_pages != kc->nr_reserved_pages);
  163. drop_pages(kc->pages);
  164. kc->pages = NULL;
  165. kc->nr_free_pages = kc->nr_reserved_pages = 0;
  166. }
  167. /*-----------------------------------------------------------------
  168. * kcopyd_jobs need to be allocated by the *clients* of kcopyd,
  169. * for this reason we use a mempool to prevent the client from
  170. * ever having to do io (which could cause a deadlock).
  171. *---------------------------------------------------------------*/
  172. struct kcopyd_job {
  173. struct dm_kcopyd_client *kc;
  174. struct list_head list;
  175. unsigned long flags;
  176. /*
  177. * Error state of the job.
  178. */
  179. int read_err;
  180. unsigned long write_err;
  181. /*
  182. * Either READ or WRITE
  183. */
  184. int rw;
  185. struct dm_io_region source;
  186. /*
  187. * The destinations for the transfer.
  188. */
  189. unsigned int num_dests;
  190. struct dm_io_region dests[DM_KCOPYD_MAX_REGIONS];
  191. struct page_list *pages;
  192. /*
  193. * Set this to ensure you are notified when the job has
  194. * completed. 'context' is for callback to use.
  195. */
  196. dm_kcopyd_notify_fn fn;
  197. void *context;
  198. /*
  199. * These fields are only used if the job has been split
  200. * into more manageable parts.
  201. */
  202. struct mutex lock;
  203. atomic_t sub_jobs;
  204. sector_t progress;
  205. struct kcopyd_job *master_job;
  206. };
  207. static struct kmem_cache *_job_cache;
  208. int __init dm_kcopyd_init(void)
  209. {
  210. _job_cache = kmem_cache_create("kcopyd_job",
  211. sizeof(struct kcopyd_job) * (SPLIT_COUNT + 1),
  212. __alignof__(struct kcopyd_job), 0, NULL);
  213. if (!_job_cache)
  214. return -ENOMEM;
  215. return 0;
  216. }
  217. void dm_kcopyd_exit(void)
  218. {
  219. kmem_cache_destroy(_job_cache);
  220. _job_cache = NULL;
  221. }
  222. /*
  223. * Functions to push and pop a job onto the head of a given job
  224. * list.
  225. */
  226. static struct kcopyd_job *pop(struct list_head *jobs,
  227. struct dm_kcopyd_client *kc)
  228. {
  229. struct kcopyd_job *job = NULL;
  230. unsigned long flags;
  231. spin_lock_irqsave(&kc->job_lock, flags);
  232. if (!list_empty(jobs)) {
  233. job = list_entry(jobs->next, struct kcopyd_job, list);
  234. list_del(&job->list);
  235. }
  236. spin_unlock_irqrestore(&kc->job_lock, flags);
  237. return job;
  238. }
  239. static void push(struct list_head *jobs, struct kcopyd_job *job)
  240. {
  241. unsigned long flags;
  242. struct dm_kcopyd_client *kc = job->kc;
  243. spin_lock_irqsave(&kc->job_lock, flags);
  244. list_add_tail(&job->list, jobs);
  245. spin_unlock_irqrestore(&kc->job_lock, flags);
  246. }
  247. static void push_head(struct list_head *jobs, struct kcopyd_job *job)
  248. {
  249. unsigned long flags;
  250. struct dm_kcopyd_client *kc = job->kc;
  251. spin_lock_irqsave(&kc->job_lock, flags);
  252. list_add(&job->list, jobs);
  253. spin_unlock_irqrestore(&kc->job_lock, flags);
  254. }
  255. /*
  256. * These three functions process 1 item from the corresponding
  257. * job list.
  258. *
  259. * They return:
  260. * < 0: error
  261. * 0: success
  262. * > 0: can't process yet.
  263. */
  264. static int run_complete_job(struct kcopyd_job *job)
  265. {
  266. void *context = job->context;
  267. int read_err = job->read_err;
  268. unsigned long write_err = job->write_err;
  269. dm_kcopyd_notify_fn fn = job->fn;
  270. struct dm_kcopyd_client *kc = job->kc;
  271. if (job->pages)
  272. kcopyd_put_pages(kc, job->pages);
  273. /*
  274. * If this is the master job, the sub jobs have already
  275. * completed so we can free everything.
  276. */
  277. if (job->master_job == job)
  278. mempool_free(job, kc->job_pool);
  279. fn(read_err, write_err, context);
  280. if (atomic_dec_and_test(&kc->nr_jobs))
  281. wake_up(&kc->destroyq);
  282. return 0;
  283. }
  284. static void complete_io(unsigned long error, void *context)
  285. {
  286. struct kcopyd_job *job = (struct kcopyd_job *) context;
  287. struct dm_kcopyd_client *kc = job->kc;
  288. if (error) {
  289. if (job->rw == WRITE)
  290. job->write_err |= error;
  291. else
  292. job->read_err = 1;
  293. if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  294. push(&kc->complete_jobs, job);
  295. wake(kc);
  296. return;
  297. }
  298. }
  299. if (job->rw == WRITE)
  300. push(&kc->complete_jobs, job);
  301. else {
  302. job->rw = WRITE;
  303. push(&kc->io_jobs, job);
  304. }
  305. wake(kc);
  306. }
  307. /*
  308. * Request io on as many buffer heads as we can currently get for
  309. * a particular job.
  310. */
  311. static int run_io_job(struct kcopyd_job *job)
  312. {
  313. int r;
  314. struct dm_io_request io_req = {
  315. .bi_rw = job->rw,
  316. .mem.type = DM_IO_PAGE_LIST,
  317. .mem.ptr.pl = job->pages,
  318. .mem.offset = 0,
  319. .notify.fn = complete_io,
  320. .notify.context = job,
  321. .client = job->kc->io_client,
  322. };
  323. if (job->rw == READ)
  324. r = dm_io(&io_req, 1, &job->source, NULL);
  325. else
  326. r = dm_io(&io_req, job->num_dests, job->dests, NULL);
  327. return r;
  328. }
  329. static int run_pages_job(struct kcopyd_job *job)
  330. {
  331. int r;
  332. unsigned nr_pages = dm_div_up(job->dests[0].count, PAGE_SIZE >> 9);
  333. r = kcopyd_get_pages(job->kc, nr_pages, &job->pages);
  334. if (!r) {
  335. /* this job is ready for io */
  336. push(&job->kc->io_jobs, job);
  337. return 0;
  338. }
  339. if (r == -ENOMEM)
  340. /* can't complete now */
  341. return 1;
  342. return r;
  343. }
  344. /*
  345. * Run through a list for as long as possible. Returns the count
  346. * of successful jobs.
  347. */
  348. static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
  349. int (*fn) (struct kcopyd_job *))
  350. {
  351. struct kcopyd_job *job;
  352. int r, count = 0;
  353. while ((job = pop(jobs, kc))) {
  354. r = fn(job);
  355. if (r < 0) {
  356. /* error this rogue job */
  357. if (job->rw == WRITE)
  358. job->write_err = (unsigned long) -1L;
  359. else
  360. job->read_err = 1;
  361. push(&kc->complete_jobs, job);
  362. break;
  363. }
  364. if (r > 0) {
  365. /*
  366. * We couldn't service this job ATM, so
  367. * push this job back onto the list.
  368. */
  369. push_head(jobs, job);
  370. break;
  371. }
  372. count++;
  373. }
  374. return count;
  375. }
  376. /*
  377. * kcopyd does this every time it's woken up.
  378. */
  379. static void do_work(struct work_struct *work)
  380. {
  381. struct dm_kcopyd_client *kc = container_of(work,
  382. struct dm_kcopyd_client, kcopyd_work);
  383. struct blk_plug plug;
  384. /*
  385. * The order that these are called is *very* important.
  386. * complete jobs can free some pages for pages jobs.
  387. * Pages jobs when successful will jump onto the io jobs
  388. * list. io jobs call wake when they complete and it all
  389. * starts again.
  390. */
  391. blk_start_plug(&plug);
  392. process_jobs(&kc->complete_jobs, kc, run_complete_job);
  393. process_jobs(&kc->pages_jobs, kc, run_pages_job);
  394. process_jobs(&kc->io_jobs, kc, run_io_job);
  395. blk_finish_plug(&plug);
  396. }
  397. /*
  398. * If we are copying a small region we just dispatch a single job
  399. * to do the copy, otherwise the io has to be split up into many
  400. * jobs.
  401. */
  402. static void dispatch_job(struct kcopyd_job *job)
  403. {
  404. struct dm_kcopyd_client *kc = job->kc;
  405. atomic_inc(&kc->nr_jobs);
  406. if (unlikely(!job->source.count))
  407. push(&kc->complete_jobs, job);
  408. else
  409. push(&kc->pages_jobs, job);
  410. wake(kc);
  411. }
  412. static void segment_complete(int read_err, unsigned long write_err,
  413. void *context)
  414. {
  415. /* FIXME: tidy this function */
  416. sector_t progress = 0;
  417. sector_t count = 0;
  418. struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
  419. struct kcopyd_job *job = sub_job->master_job;
  420. struct dm_kcopyd_client *kc = job->kc;
  421. mutex_lock(&job->lock);
  422. /* update the error */
  423. if (read_err)
  424. job->read_err = 1;
  425. if (write_err)
  426. job->write_err |= write_err;
  427. /*
  428. * Only dispatch more work if there hasn't been an error.
  429. */
  430. if ((!job->read_err && !job->write_err) ||
  431. test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  432. /* get the next chunk of work */
  433. progress = job->progress;
  434. count = job->source.count - progress;
  435. if (count) {
  436. if (count > SUB_JOB_SIZE)
  437. count = SUB_JOB_SIZE;
  438. job->progress += count;
  439. }
  440. }
  441. mutex_unlock(&job->lock);
  442. if (count) {
  443. int i;
  444. *sub_job = *job;
  445. sub_job->source.sector += progress;
  446. sub_job->source.count = count;
  447. for (i = 0; i < job->num_dests; i++) {
  448. sub_job->dests[i].sector += progress;
  449. sub_job->dests[i].count = count;
  450. }
  451. sub_job->fn = segment_complete;
  452. sub_job->context = sub_job;
  453. dispatch_job(sub_job);
  454. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  455. /*
  456. * Queue the completion callback to the kcopyd thread.
  457. *
  458. * Some callers assume that all the completions are called
  459. * from a single thread and don't race with each other.
  460. *
  461. * We must not call the callback directly here because this
  462. * code may not be executing in the thread.
  463. */
  464. push(&kc->complete_jobs, job);
  465. wake(kc);
  466. }
  467. }
  468. /*
  469. * Create some sub jobs to share the work between them.
  470. */
  471. static void split_job(struct kcopyd_job *master_job)
  472. {
  473. int i;
  474. atomic_inc(&master_job->kc->nr_jobs);
  475. atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
  476. for (i = 0; i < SPLIT_COUNT; i++) {
  477. master_job[i + 1].master_job = master_job;
  478. segment_complete(0, 0u, &master_job[i + 1]);
  479. }
  480. }
  481. int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
  482. unsigned int num_dests, struct dm_io_region *dests,
  483. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  484. {
  485. struct kcopyd_job *job;
  486. /*
  487. * Allocate an array of jobs consisting of one master job
  488. * followed by SPLIT_COUNT sub jobs.
  489. */
  490. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  491. /*
  492. * set up for the read.
  493. */
  494. job->kc = kc;
  495. job->flags = flags;
  496. job->read_err = 0;
  497. job->write_err = 0;
  498. job->rw = READ;
  499. job->source = *from;
  500. job->num_dests = num_dests;
  501. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  502. job->pages = NULL;
  503. job->fn = fn;
  504. job->context = context;
  505. job->master_job = job;
  506. if (job->source.count <= SUB_JOB_SIZE)
  507. dispatch_job(job);
  508. else {
  509. mutex_init(&job->lock);
  510. job->progress = 0;
  511. split_job(job);
  512. }
  513. return 0;
  514. }
  515. EXPORT_SYMBOL(dm_kcopyd_copy);
  516. void *dm_kcopyd_prepare_callback(struct dm_kcopyd_client *kc,
  517. dm_kcopyd_notify_fn fn, void *context)
  518. {
  519. struct kcopyd_job *job;
  520. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  521. memset(job, 0, sizeof(struct kcopyd_job));
  522. job->kc = kc;
  523. job->fn = fn;
  524. job->context = context;
  525. atomic_inc(&kc->nr_jobs);
  526. return job;
  527. }
  528. EXPORT_SYMBOL(dm_kcopyd_prepare_callback);
  529. void dm_kcopyd_do_callback(void *j, int read_err, unsigned long write_err)
  530. {
  531. struct kcopyd_job *job = j;
  532. struct dm_kcopyd_client *kc = job->kc;
  533. job->read_err = read_err;
  534. job->write_err = write_err;
  535. push(&kc->complete_jobs, job);
  536. wake(kc);
  537. }
  538. EXPORT_SYMBOL(dm_kcopyd_do_callback);
  539. /*
  540. * Cancels a kcopyd job, eg. someone might be deactivating a
  541. * mirror.
  542. */
  543. #if 0
  544. int kcopyd_cancel(struct kcopyd_job *job, int block)
  545. {
  546. /* FIXME: finish */
  547. return -1;
  548. }
  549. #endif /* 0 */
  550. /*-----------------------------------------------------------------
  551. * Client setup
  552. *---------------------------------------------------------------*/
  553. struct dm_kcopyd_client *dm_kcopyd_client_create(void)
  554. {
  555. int r = -ENOMEM;
  556. struct dm_kcopyd_client *kc;
  557. kc = kmalloc(sizeof(*kc), GFP_KERNEL);
  558. if (!kc)
  559. return ERR_PTR(-ENOMEM);
  560. spin_lock_init(&kc->job_lock);
  561. INIT_LIST_HEAD(&kc->complete_jobs);
  562. INIT_LIST_HEAD(&kc->io_jobs);
  563. INIT_LIST_HEAD(&kc->pages_jobs);
  564. kc->job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
  565. if (!kc->job_pool)
  566. goto bad_slab;
  567. INIT_WORK(&kc->kcopyd_work, do_work);
  568. kc->kcopyd_wq = alloc_workqueue("kcopyd",
  569. WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
  570. if (!kc->kcopyd_wq)
  571. goto bad_workqueue;
  572. kc->pages = NULL;
  573. kc->nr_reserved_pages = kc->nr_free_pages = 0;
  574. r = client_reserve_pages(kc, RESERVE_PAGES);
  575. if (r)
  576. goto bad_client_pages;
  577. kc->io_client = dm_io_client_create();
  578. if (IS_ERR(kc->io_client)) {
  579. r = PTR_ERR(kc->io_client);
  580. goto bad_io_client;
  581. }
  582. init_waitqueue_head(&kc->destroyq);
  583. atomic_set(&kc->nr_jobs, 0);
  584. return kc;
  585. bad_io_client:
  586. client_free_pages(kc);
  587. bad_client_pages:
  588. destroy_workqueue(kc->kcopyd_wq);
  589. bad_workqueue:
  590. mempool_destroy(kc->job_pool);
  591. bad_slab:
  592. kfree(kc);
  593. return ERR_PTR(r);
  594. }
  595. EXPORT_SYMBOL(dm_kcopyd_client_create);
  596. void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
  597. {
  598. /* Wait for completion of all jobs submitted by this client. */
  599. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  600. BUG_ON(!list_empty(&kc->complete_jobs));
  601. BUG_ON(!list_empty(&kc->io_jobs));
  602. BUG_ON(!list_empty(&kc->pages_jobs));
  603. destroy_workqueue(kc->kcopyd_wq);
  604. dm_io_client_destroy(kc->io_client);
  605. client_free_pages(kc);
  606. mempool_destroy(kc->job_pool);
  607. kfree(kc);
  608. }
  609. EXPORT_SYMBOL(dm_kcopyd_client_destroy);