dm-kcopyd.c 15 KB

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