kcopyd.c 15 KB

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