dm-kcopyd.c 15 KB

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