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. #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. sector_t offset;
  192. unsigned int nr_pages;
  193. struct page_list *pages;
  194. /*
  195. * Set this to ensure you are notified when the job has
  196. * completed. 'context' is for callback to use.
  197. */
  198. dm_kcopyd_notify_fn fn;
  199. void *context;
  200. /*
  201. * These fields are only used if the job has been split
  202. * into more manageable parts.
  203. */
  204. struct mutex lock;
  205. atomic_t sub_jobs;
  206. sector_t progress;
  207. struct kcopyd_job *master_job;
  208. };
  209. static struct kmem_cache *_job_cache;
  210. int __init dm_kcopyd_init(void)
  211. {
  212. _job_cache = kmem_cache_create("kcopyd_job",
  213. sizeof(struct kcopyd_job) * (SPLIT_COUNT + 1),
  214. __alignof__(struct kcopyd_job), 0, NULL);
  215. if (!_job_cache)
  216. return -ENOMEM;
  217. return 0;
  218. }
  219. void dm_kcopyd_exit(void)
  220. {
  221. kmem_cache_destroy(_job_cache);
  222. _job_cache = NULL;
  223. }
  224. /*
  225. * Functions to push and pop a job onto the head of a given job
  226. * list.
  227. */
  228. static struct kcopyd_job *pop(struct list_head *jobs,
  229. struct dm_kcopyd_client *kc)
  230. {
  231. struct kcopyd_job *job = NULL;
  232. unsigned long flags;
  233. spin_lock_irqsave(&kc->job_lock, flags);
  234. if (!list_empty(jobs)) {
  235. job = list_entry(jobs->next, struct kcopyd_job, list);
  236. list_del(&job->list);
  237. }
  238. spin_unlock_irqrestore(&kc->job_lock, flags);
  239. return job;
  240. }
  241. static void push(struct list_head *jobs, struct kcopyd_job *job)
  242. {
  243. unsigned long flags;
  244. struct dm_kcopyd_client *kc = job->kc;
  245. spin_lock_irqsave(&kc->job_lock, flags);
  246. list_add_tail(&job->list, jobs);
  247. spin_unlock_irqrestore(&kc->job_lock, flags);
  248. }
  249. static void push_head(struct list_head *jobs, struct kcopyd_job *job)
  250. {
  251. unsigned long flags;
  252. struct dm_kcopyd_client *kc = job->kc;
  253. spin_lock_irqsave(&kc->job_lock, flags);
  254. list_add(&job->list, jobs);
  255. spin_unlock_irqrestore(&kc->job_lock, flags);
  256. }
  257. /*
  258. * These three functions process 1 item from the corresponding
  259. * job list.
  260. *
  261. * They return:
  262. * < 0: error
  263. * 0: success
  264. * > 0: can't process yet.
  265. */
  266. static int run_complete_job(struct kcopyd_job *job)
  267. {
  268. void *context = job->context;
  269. int read_err = job->read_err;
  270. unsigned long write_err = job->write_err;
  271. dm_kcopyd_notify_fn fn = job->fn;
  272. struct dm_kcopyd_client *kc = job->kc;
  273. if (job->pages)
  274. kcopyd_put_pages(kc, job->pages);
  275. /*
  276. * If this is the master job, the sub jobs have already
  277. * completed so we can free everything.
  278. */
  279. if (job->master_job == job)
  280. mempool_free(job, kc->job_pool);
  281. fn(read_err, write_err, context);
  282. if (atomic_dec_and_test(&kc->nr_jobs))
  283. wake_up(&kc->destroyq);
  284. return 0;
  285. }
  286. static void complete_io(unsigned long error, void *context)
  287. {
  288. struct kcopyd_job *job = (struct kcopyd_job *) context;
  289. struct dm_kcopyd_client *kc = job->kc;
  290. if (error) {
  291. if (job->rw == WRITE)
  292. job->write_err |= error;
  293. else
  294. job->read_err = 1;
  295. if (!test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  296. push(&kc->complete_jobs, job);
  297. wake(kc);
  298. return;
  299. }
  300. }
  301. if (job->rw == WRITE)
  302. push(&kc->complete_jobs, job);
  303. else {
  304. job->rw = WRITE;
  305. push(&kc->io_jobs, job);
  306. }
  307. wake(kc);
  308. }
  309. /*
  310. * Request io on as many buffer heads as we can currently get for
  311. * a particular job.
  312. */
  313. static int run_io_job(struct kcopyd_job *job)
  314. {
  315. int r;
  316. struct dm_io_request io_req = {
  317. .bi_rw = job->rw,
  318. .mem.type = DM_IO_PAGE_LIST,
  319. .mem.ptr.pl = job->pages,
  320. .mem.offset = job->offset,
  321. .notify.fn = complete_io,
  322. .notify.context = job,
  323. .client = job->kc->io_client,
  324. };
  325. if (job->rw == READ)
  326. r = dm_io(&io_req, 1, &job->source, NULL);
  327. else
  328. r = dm_io(&io_req, job->num_dests, job->dests, NULL);
  329. return r;
  330. }
  331. static int run_pages_job(struct kcopyd_job *job)
  332. {
  333. int r;
  334. job->nr_pages = dm_div_up(job->dests[0].count + job->offset,
  335. PAGE_SIZE >> 9);
  336. r = kcopyd_get_pages(job->kc, job->nr_pages, &job->pages);
  337. if (!r) {
  338. /* this job is ready for io */
  339. push(&job->kc->io_jobs, job);
  340. return 0;
  341. }
  342. if (r == -ENOMEM)
  343. /* can't complete now */
  344. return 1;
  345. return r;
  346. }
  347. /*
  348. * Run through a list for as long as possible. Returns the count
  349. * of successful jobs.
  350. */
  351. static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
  352. int (*fn) (struct kcopyd_job *))
  353. {
  354. struct kcopyd_job *job;
  355. int r, count = 0;
  356. while ((job = pop(jobs, kc))) {
  357. r = fn(job);
  358. if (r < 0) {
  359. /* error this rogue job */
  360. if (job->rw == WRITE)
  361. job->write_err = (unsigned long) -1L;
  362. else
  363. job->read_err = 1;
  364. push(&kc->complete_jobs, job);
  365. break;
  366. }
  367. if (r > 0) {
  368. /*
  369. * We couldn't service this job ATM, so
  370. * push this job back onto the list.
  371. */
  372. push_head(jobs, job);
  373. break;
  374. }
  375. count++;
  376. }
  377. return count;
  378. }
  379. /*
  380. * kcopyd does this every time it's woken up.
  381. */
  382. static void do_work(struct work_struct *work)
  383. {
  384. struct dm_kcopyd_client *kc = container_of(work,
  385. struct dm_kcopyd_client, kcopyd_work);
  386. struct blk_plug plug;
  387. /*
  388. * The order that these are called is *very* important.
  389. * complete jobs can free some pages for pages jobs.
  390. * Pages jobs when successful will jump onto the io jobs
  391. * list. io jobs call wake when they complete and it all
  392. * starts again.
  393. */
  394. blk_start_plug(&plug);
  395. process_jobs(&kc->complete_jobs, kc, run_complete_job);
  396. process_jobs(&kc->pages_jobs, kc, run_pages_job);
  397. process_jobs(&kc->io_jobs, kc, run_io_job);
  398. blk_finish_plug(&plug);
  399. }
  400. /*
  401. * If we are copying a small region we just dispatch a single job
  402. * to do the copy, otherwise the io has to be split up into many
  403. * jobs.
  404. */
  405. static void dispatch_job(struct kcopyd_job *job)
  406. {
  407. struct dm_kcopyd_client *kc = job->kc;
  408. atomic_inc(&kc->nr_jobs);
  409. if (unlikely(!job->source.count))
  410. push(&kc->complete_jobs, job);
  411. else
  412. push(&kc->pages_jobs, job);
  413. wake(kc);
  414. }
  415. static void segment_complete(int read_err, unsigned long write_err,
  416. void *context)
  417. {
  418. /* FIXME: tidy this function */
  419. sector_t progress = 0;
  420. sector_t count = 0;
  421. struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
  422. struct kcopyd_job *job = sub_job->master_job;
  423. struct dm_kcopyd_client *kc = job->kc;
  424. mutex_lock(&job->lock);
  425. /* update the error */
  426. if (read_err)
  427. job->read_err = 1;
  428. if (write_err)
  429. job->write_err |= write_err;
  430. /*
  431. * Only dispatch more work if there hasn't been an error.
  432. */
  433. if ((!job->read_err && !job->write_err) ||
  434. test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  435. /* get the next chunk of work */
  436. progress = job->progress;
  437. count = job->source.count - progress;
  438. if (count) {
  439. if (count > SUB_JOB_SIZE)
  440. count = SUB_JOB_SIZE;
  441. job->progress += count;
  442. }
  443. }
  444. mutex_unlock(&job->lock);
  445. if (count) {
  446. int i;
  447. *sub_job = *job;
  448. sub_job->source.sector += progress;
  449. sub_job->source.count = count;
  450. for (i = 0; i < job->num_dests; i++) {
  451. sub_job->dests[i].sector += progress;
  452. sub_job->dests[i].count = count;
  453. }
  454. sub_job->fn = segment_complete;
  455. sub_job->context = sub_job;
  456. dispatch_job(sub_job);
  457. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  458. /*
  459. * Queue the completion callback to the kcopyd thread.
  460. *
  461. * Some callers assume that all the completions are called
  462. * from a single thread and don't race with each other.
  463. *
  464. * We must not call the callback directly here because this
  465. * code may not be executing in the thread.
  466. */
  467. push(&kc->complete_jobs, job);
  468. wake(kc);
  469. }
  470. }
  471. /*
  472. * Create some sub jobs to share the work between them.
  473. */
  474. static void split_job(struct kcopyd_job *master_job)
  475. {
  476. int i;
  477. atomic_inc(&master_job->kc->nr_jobs);
  478. atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
  479. for (i = 0; i < SPLIT_COUNT; i++) {
  480. master_job[i + 1].master_job = master_job;
  481. segment_complete(0, 0u, &master_job[i + 1]);
  482. }
  483. }
  484. int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
  485. unsigned int num_dests, struct dm_io_region *dests,
  486. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  487. {
  488. struct kcopyd_job *job;
  489. /*
  490. * Allocate an array of jobs consisting of one master job
  491. * followed by SPLIT_COUNT sub jobs.
  492. */
  493. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  494. /*
  495. * set up for the read.
  496. */
  497. job->kc = kc;
  498. job->flags = flags;
  499. job->read_err = 0;
  500. job->write_err = 0;
  501. job->rw = READ;
  502. job->source = *from;
  503. job->num_dests = num_dests;
  504. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  505. job->offset = 0;
  506. job->nr_pages = 0;
  507. job->pages = NULL;
  508. job->fn = fn;
  509. job->context = context;
  510. job->master_job = job;
  511. if (job->source.count <= SUB_JOB_SIZE)
  512. dispatch_job(job);
  513. else {
  514. mutex_init(&job->lock);
  515. job->progress = 0;
  516. split_job(job);
  517. }
  518. return 0;
  519. }
  520. EXPORT_SYMBOL(dm_kcopyd_copy);
  521. /*
  522. * Cancels a kcopyd job, eg. someone might be deactivating a
  523. * mirror.
  524. */
  525. #if 0
  526. int kcopyd_cancel(struct kcopyd_job *job, int block)
  527. {
  528. /* FIXME: finish */
  529. return -1;
  530. }
  531. #endif /* 0 */
  532. /*-----------------------------------------------------------------
  533. * Client setup
  534. *---------------------------------------------------------------*/
  535. struct dm_kcopyd_client *dm_kcopyd_client_create(void)
  536. {
  537. int r = -ENOMEM;
  538. struct dm_kcopyd_client *kc;
  539. kc = kmalloc(sizeof(*kc), GFP_KERNEL);
  540. if (!kc)
  541. return ERR_PTR(-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, RESERVE_PAGES);
  557. if (r)
  558. goto bad_client_pages;
  559. kc->io_client = dm_io_client_create();
  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. return kc;
  567. bad_io_client:
  568. client_free_pages(kc);
  569. bad_client_pages:
  570. destroy_workqueue(kc->kcopyd_wq);
  571. bad_workqueue:
  572. mempool_destroy(kc->job_pool);
  573. bad_slab:
  574. kfree(kc);
  575. return ERR_PTR(r);
  576. }
  577. EXPORT_SYMBOL(dm_kcopyd_client_create);
  578. void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
  579. {
  580. /* Wait for completion of all jobs submitted by this client. */
  581. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  582. BUG_ON(!list_empty(&kc->complete_jobs));
  583. BUG_ON(!list_empty(&kc->io_jobs));
  584. BUG_ON(!list_empty(&kc->pages_jobs));
  585. destroy_workqueue(kc->kcopyd_wq);
  586. dm_io_client_destroy(kc->io_client);
  587. client_free_pages(kc);
  588. mempool_destroy(kc->job_pool);
  589. kfree(kc);
  590. }
  591. EXPORT_SYMBOL(dm_kcopyd_client_destroy);