kcopyd.c 14 KB

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