dm-kcopyd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. 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 = 0,
  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, PAGE_SIZE >> 9);
  334. r = kcopyd_get_pages(job->kc, job->nr_pages, &job->pages);
  335. if (!r) {
  336. /* this job is ready for io */
  337. push(&job->kc->io_jobs, job);
  338. return 0;
  339. }
  340. if (r == -ENOMEM)
  341. /* can't complete now */
  342. return 1;
  343. return r;
  344. }
  345. /*
  346. * Run through a list for as long as possible. Returns the count
  347. * of successful jobs.
  348. */
  349. static int process_jobs(struct list_head *jobs, struct dm_kcopyd_client *kc,
  350. int (*fn) (struct kcopyd_job *))
  351. {
  352. struct kcopyd_job *job;
  353. int r, count = 0;
  354. while ((job = pop(jobs, kc))) {
  355. r = fn(job);
  356. if (r < 0) {
  357. /* error this rogue job */
  358. if (job->rw == WRITE)
  359. job->write_err = (unsigned long) -1L;
  360. else
  361. job->read_err = 1;
  362. push(&kc->complete_jobs, job);
  363. break;
  364. }
  365. if (r > 0) {
  366. /*
  367. * We couldn't service this job ATM, so
  368. * push this job back onto the list.
  369. */
  370. push_head(jobs, job);
  371. break;
  372. }
  373. count++;
  374. }
  375. return count;
  376. }
  377. /*
  378. * kcopyd does this every time it's woken up.
  379. */
  380. static void do_work(struct work_struct *work)
  381. {
  382. struct dm_kcopyd_client *kc = container_of(work,
  383. struct dm_kcopyd_client, kcopyd_work);
  384. struct blk_plug plug;
  385. /*
  386. * The order that these are called is *very* important.
  387. * complete jobs can free some pages for pages jobs.
  388. * Pages jobs when successful will jump onto the io jobs
  389. * list. io jobs call wake when they complete and it all
  390. * starts again.
  391. */
  392. blk_start_plug(&plug);
  393. process_jobs(&kc->complete_jobs, kc, run_complete_job);
  394. process_jobs(&kc->pages_jobs, kc, run_pages_job);
  395. process_jobs(&kc->io_jobs, kc, run_io_job);
  396. blk_finish_plug(&plug);
  397. }
  398. /*
  399. * If we are copying a small region we just dispatch a single job
  400. * to do the copy, otherwise the io has to be split up into many
  401. * jobs.
  402. */
  403. static void dispatch_job(struct kcopyd_job *job)
  404. {
  405. struct dm_kcopyd_client *kc = job->kc;
  406. atomic_inc(&kc->nr_jobs);
  407. if (unlikely(!job->source.count))
  408. push(&kc->complete_jobs, job);
  409. else
  410. push(&kc->pages_jobs, job);
  411. wake(kc);
  412. }
  413. static void segment_complete(int read_err, unsigned long write_err,
  414. void *context)
  415. {
  416. /* FIXME: tidy this function */
  417. sector_t progress = 0;
  418. sector_t count = 0;
  419. struct kcopyd_job *sub_job = (struct kcopyd_job *) context;
  420. struct kcopyd_job *job = sub_job->master_job;
  421. struct dm_kcopyd_client *kc = job->kc;
  422. mutex_lock(&job->lock);
  423. /* update the error */
  424. if (read_err)
  425. job->read_err = 1;
  426. if (write_err)
  427. job->write_err |= write_err;
  428. /*
  429. * Only dispatch more work if there hasn't been an error.
  430. */
  431. if ((!job->read_err && !job->write_err) ||
  432. test_bit(DM_KCOPYD_IGNORE_ERROR, &job->flags)) {
  433. /* get the next chunk of work */
  434. progress = job->progress;
  435. count = job->source.count - progress;
  436. if (count) {
  437. if (count > SUB_JOB_SIZE)
  438. count = SUB_JOB_SIZE;
  439. job->progress += count;
  440. }
  441. }
  442. mutex_unlock(&job->lock);
  443. if (count) {
  444. int i;
  445. *sub_job = *job;
  446. sub_job->source.sector += progress;
  447. sub_job->source.count = count;
  448. for (i = 0; i < job->num_dests; i++) {
  449. sub_job->dests[i].sector += progress;
  450. sub_job->dests[i].count = count;
  451. }
  452. sub_job->fn = segment_complete;
  453. sub_job->context = sub_job;
  454. dispatch_job(sub_job);
  455. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  456. /*
  457. * Queue the completion callback to the kcopyd thread.
  458. *
  459. * Some callers assume that all the completions are called
  460. * from a single thread and don't race with each other.
  461. *
  462. * We must not call the callback directly here because this
  463. * code may not be executing in the thread.
  464. */
  465. push(&kc->complete_jobs, job);
  466. wake(kc);
  467. }
  468. }
  469. /*
  470. * Create some sub jobs to share the work between them.
  471. */
  472. static void split_job(struct kcopyd_job *master_job)
  473. {
  474. int i;
  475. atomic_inc(&master_job->kc->nr_jobs);
  476. atomic_set(&master_job->sub_jobs, SPLIT_COUNT);
  477. for (i = 0; i < SPLIT_COUNT; i++) {
  478. master_job[i + 1].master_job = master_job;
  479. segment_complete(0, 0u, &master_job[i + 1]);
  480. }
  481. }
  482. int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from,
  483. unsigned int num_dests, struct dm_io_region *dests,
  484. unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
  485. {
  486. struct kcopyd_job *job;
  487. /*
  488. * Allocate an array of jobs consisting of one master job
  489. * followed by SPLIT_COUNT sub jobs.
  490. */
  491. job = mempool_alloc(kc->job_pool, GFP_NOIO);
  492. /*
  493. * set up for the read.
  494. */
  495. job->kc = kc;
  496. job->flags = flags;
  497. job->read_err = 0;
  498. job->write_err = 0;
  499. job->rw = READ;
  500. job->source = *from;
  501. job->num_dests = num_dests;
  502. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  503. job->nr_pages = 0;
  504. job->pages = NULL;
  505. job->fn = fn;
  506. job->context = context;
  507. job->master_job = job;
  508. if (job->source.count <= SUB_JOB_SIZE)
  509. dispatch_job(job);
  510. else {
  511. mutex_init(&job->lock);
  512. job->progress = 0;
  513. split_job(job);
  514. }
  515. return 0;
  516. }
  517. EXPORT_SYMBOL(dm_kcopyd_copy);
  518. /*
  519. * Cancels a kcopyd job, eg. someone might be deactivating a
  520. * mirror.
  521. */
  522. #if 0
  523. int kcopyd_cancel(struct kcopyd_job *job, int block)
  524. {
  525. /* FIXME: finish */
  526. return -1;
  527. }
  528. #endif /* 0 */
  529. /*-----------------------------------------------------------------
  530. * Client setup
  531. *---------------------------------------------------------------*/
  532. struct dm_kcopyd_client *dm_kcopyd_client_create(void)
  533. {
  534. int r = -ENOMEM;
  535. struct dm_kcopyd_client *kc;
  536. kc = kmalloc(sizeof(*kc), GFP_KERNEL);
  537. if (!kc)
  538. return ERR_PTR(-ENOMEM);
  539. spin_lock_init(&kc->job_lock);
  540. INIT_LIST_HEAD(&kc->complete_jobs);
  541. INIT_LIST_HEAD(&kc->io_jobs);
  542. INIT_LIST_HEAD(&kc->pages_jobs);
  543. kc->job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
  544. if (!kc->job_pool)
  545. goto bad_slab;
  546. INIT_WORK(&kc->kcopyd_work, do_work);
  547. kc->kcopyd_wq = alloc_workqueue("kcopyd",
  548. WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
  549. if (!kc->kcopyd_wq)
  550. goto bad_workqueue;
  551. kc->pages = NULL;
  552. kc->nr_reserved_pages = kc->nr_free_pages = 0;
  553. r = client_reserve_pages(kc, RESERVE_PAGES);
  554. if (r)
  555. goto bad_client_pages;
  556. kc->io_client = dm_io_client_create();
  557. if (IS_ERR(kc->io_client)) {
  558. r = PTR_ERR(kc->io_client);
  559. goto bad_io_client;
  560. }
  561. init_waitqueue_head(&kc->destroyq);
  562. atomic_set(&kc->nr_jobs, 0);
  563. return kc;
  564. bad_io_client:
  565. client_free_pages(kc);
  566. bad_client_pages:
  567. destroy_workqueue(kc->kcopyd_wq);
  568. bad_workqueue:
  569. mempool_destroy(kc->job_pool);
  570. bad_slab:
  571. kfree(kc);
  572. return ERR_PTR(r);
  573. }
  574. EXPORT_SYMBOL(dm_kcopyd_client_create);
  575. void dm_kcopyd_client_destroy(struct dm_kcopyd_client *kc)
  576. {
  577. /* Wait for completion of all jobs submitted by this client. */
  578. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  579. BUG_ON(!list_empty(&kc->complete_jobs));
  580. BUG_ON(!list_empty(&kc->io_jobs));
  581. BUG_ON(!list_empty(&kc->pages_jobs));
  582. destroy_workqueue(kc->kcopyd_wq);
  583. dm_io_client_destroy(kc->io_client);
  584. client_free_pages(kc);
  585. mempool_destroy(kc->job_pool);
  586. kfree(kc);
  587. }
  588. EXPORT_SYMBOL(dm_kcopyd_client_destroy);