kcopyd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 inline 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 semaphore 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_create("kcopyd-jobs",
  189. sizeof(struct kcopyd_job),
  190. __alignof__(struct kcopyd_job),
  191. 0, NULL, NULL);
  192. if (!_job_cache)
  193. return -ENOMEM;
  194. _job_pool = mempool_create_slab_pool(MIN_JOBS, _job_cache);
  195. if (!_job_pool) {
  196. kmem_cache_destroy(_job_cache);
  197. return -ENOMEM;
  198. }
  199. return 0;
  200. }
  201. static void jobs_exit(void)
  202. {
  203. BUG_ON(!list_empty(&_complete_jobs));
  204. BUG_ON(!list_empty(&_io_jobs));
  205. BUG_ON(!list_empty(&_pages_jobs));
  206. mempool_destroy(_job_pool);
  207. kmem_cache_destroy(_job_cache);
  208. _job_pool = NULL;
  209. _job_cache = NULL;
  210. }
  211. /*
  212. * Functions to push and pop a job onto the head of a given job
  213. * list.
  214. */
  215. static inline struct kcopyd_job *pop(struct list_head *jobs)
  216. {
  217. struct kcopyd_job *job = NULL;
  218. unsigned long flags;
  219. spin_lock_irqsave(&_job_lock, flags);
  220. if (!list_empty(jobs)) {
  221. job = list_entry(jobs->next, struct kcopyd_job, list);
  222. list_del(&job->list);
  223. }
  224. spin_unlock_irqrestore(&_job_lock, flags);
  225. return job;
  226. }
  227. static inline void push(struct list_head *jobs, struct kcopyd_job *job)
  228. {
  229. unsigned long flags;
  230. spin_lock_irqsave(&_job_lock, flags);
  231. list_add_tail(&job->list, jobs);
  232. spin_unlock_irqrestore(&_job_lock, flags);
  233. }
  234. /*
  235. * These three functions process 1 item from the corresponding
  236. * job list.
  237. *
  238. * They return:
  239. * < 0: error
  240. * 0: success
  241. * > 0: can't process yet.
  242. */
  243. static int run_complete_job(struct kcopyd_job *job)
  244. {
  245. void *context = job->context;
  246. int read_err = job->read_err;
  247. unsigned int write_err = job->write_err;
  248. kcopyd_notify_fn fn = job->fn;
  249. struct kcopyd_client *kc = job->kc;
  250. kcopyd_put_pages(kc, job->pages);
  251. mempool_free(job, _job_pool);
  252. fn(read_err, write_err, context);
  253. if (atomic_dec_and_test(&kc->nr_jobs))
  254. wake_up(&kc->destroyq);
  255. return 0;
  256. }
  257. static void complete_io(unsigned long error, void *context)
  258. {
  259. struct kcopyd_job *job = (struct kcopyd_job *) context;
  260. if (error) {
  261. if (job->rw == WRITE)
  262. job->write_err |= error;
  263. else
  264. job->read_err = 1;
  265. if (!test_bit(KCOPYD_IGNORE_ERROR, &job->flags)) {
  266. push(&_complete_jobs, job);
  267. wake();
  268. return;
  269. }
  270. }
  271. if (job->rw == WRITE)
  272. push(&_complete_jobs, job);
  273. else {
  274. job->rw = WRITE;
  275. push(&_io_jobs, job);
  276. }
  277. wake();
  278. }
  279. /*
  280. * Request io on as many buffer heads as we can currently get for
  281. * a particular job.
  282. */
  283. static int run_io_job(struct kcopyd_job *job)
  284. {
  285. int r;
  286. struct dm_io_request io_req = {
  287. .bi_rw = job->rw,
  288. .mem.type = DM_IO_PAGE_LIST,
  289. .mem.ptr.pl = job->pages,
  290. .mem.offset = job->offset,
  291. .notify.fn = complete_io,
  292. .notify.context = job,
  293. .client = job->kc->io_client,
  294. };
  295. if (job->rw == READ)
  296. r = dm_io(&io_req, 1, &job->source, NULL);
  297. else
  298. r = dm_io(&io_req, job->num_dests, job->dests, NULL);
  299. return r;
  300. }
  301. static int run_pages_job(struct kcopyd_job *job)
  302. {
  303. int r;
  304. job->nr_pages = dm_div_up(job->dests[0].count + job->offset,
  305. PAGE_SIZE >> 9);
  306. r = kcopyd_get_pages(job->kc, job->nr_pages, &job->pages);
  307. if (!r) {
  308. /* this job is ready for io */
  309. push(&_io_jobs, job);
  310. return 0;
  311. }
  312. if (r == -ENOMEM)
  313. /* can't complete now */
  314. return 1;
  315. return r;
  316. }
  317. /*
  318. * Run through a list for as long as possible. Returns the count
  319. * of successful jobs.
  320. */
  321. static int process_jobs(struct list_head *jobs, int (*fn) (struct kcopyd_job *))
  322. {
  323. struct kcopyd_job *job;
  324. int r, count = 0;
  325. while ((job = pop(jobs))) {
  326. r = fn(job);
  327. if (r < 0) {
  328. /* error this rogue job */
  329. if (job->rw == WRITE)
  330. job->write_err = (unsigned int) -1;
  331. else
  332. job->read_err = 1;
  333. push(&_complete_jobs, job);
  334. break;
  335. }
  336. if (r > 0) {
  337. /*
  338. * We couldn't service this job ATM, so
  339. * push this job back onto the list.
  340. */
  341. push(jobs, job);
  342. break;
  343. }
  344. count++;
  345. }
  346. return count;
  347. }
  348. /*
  349. * kcopyd does this every time it's woken up.
  350. */
  351. static void do_work(struct work_struct *ignored)
  352. {
  353. /*
  354. * The order that these are called is *very* important.
  355. * complete jobs can free some pages for pages jobs.
  356. * Pages jobs when successful will jump onto the io jobs
  357. * list. io jobs call wake when they complete and it all
  358. * starts again.
  359. */
  360. process_jobs(&_complete_jobs, run_complete_job);
  361. process_jobs(&_pages_jobs, run_pages_job);
  362. process_jobs(&_io_jobs, run_io_job);
  363. }
  364. /*
  365. * If we are copying a small region we just dispatch a single job
  366. * to do the copy, otherwise the io has to be split up into many
  367. * jobs.
  368. */
  369. static void dispatch_job(struct kcopyd_job *job)
  370. {
  371. atomic_inc(&job->kc->nr_jobs);
  372. push(&_pages_jobs, job);
  373. wake();
  374. }
  375. #define SUB_JOB_SIZE 128
  376. static void segment_complete(int read_err,
  377. unsigned int write_err, void *context)
  378. {
  379. /* FIXME: tidy this function */
  380. sector_t progress = 0;
  381. sector_t count = 0;
  382. struct kcopyd_job *job = (struct kcopyd_job *) context;
  383. down(&job->lock);
  384. /* update the error */
  385. if (read_err)
  386. job->read_err = 1;
  387. if (write_err)
  388. job->write_err |= write_err;
  389. /*
  390. * Only dispatch more work if there hasn't been an error.
  391. */
  392. if ((!job->read_err && !job->write_err) ||
  393. test_bit(KCOPYD_IGNORE_ERROR, &job->flags)) {
  394. /* get the next chunk of work */
  395. progress = job->progress;
  396. count = job->source.count - progress;
  397. if (count) {
  398. if (count > SUB_JOB_SIZE)
  399. count = SUB_JOB_SIZE;
  400. job->progress += count;
  401. }
  402. }
  403. up(&job->lock);
  404. if (count) {
  405. int i;
  406. struct kcopyd_job *sub_job = mempool_alloc(_job_pool, GFP_NOIO);
  407. *sub_job = *job;
  408. sub_job->source.sector += progress;
  409. sub_job->source.count = count;
  410. for (i = 0; i < job->num_dests; i++) {
  411. sub_job->dests[i].sector += progress;
  412. sub_job->dests[i].count = count;
  413. }
  414. sub_job->fn = segment_complete;
  415. sub_job->context = job;
  416. dispatch_job(sub_job);
  417. } else if (atomic_dec_and_test(&job->sub_jobs)) {
  418. /*
  419. * To avoid a race we must keep the job around
  420. * until after the notify function has completed.
  421. * Otherwise the client may try and stop the job
  422. * after we've completed.
  423. */
  424. job->fn(read_err, write_err, job->context);
  425. mempool_free(job, _job_pool);
  426. }
  427. }
  428. /*
  429. * Create some little jobs that will do the move between
  430. * them.
  431. */
  432. #define SPLIT_COUNT 8
  433. static void split_job(struct kcopyd_job *job)
  434. {
  435. int i;
  436. atomic_set(&job->sub_jobs, SPLIT_COUNT);
  437. for (i = 0; i < SPLIT_COUNT; i++)
  438. segment_complete(0, 0u, job);
  439. }
  440. int kcopyd_copy(struct kcopyd_client *kc, struct io_region *from,
  441. unsigned int num_dests, struct io_region *dests,
  442. unsigned int flags, kcopyd_notify_fn fn, void *context)
  443. {
  444. struct kcopyd_job *job;
  445. /*
  446. * Allocate a new job.
  447. */
  448. job = mempool_alloc(_job_pool, GFP_NOIO);
  449. /*
  450. * set up for the read.
  451. */
  452. job->kc = kc;
  453. job->flags = flags;
  454. job->read_err = 0;
  455. job->write_err = 0;
  456. job->rw = READ;
  457. job->source = *from;
  458. job->num_dests = num_dests;
  459. memcpy(&job->dests, dests, sizeof(*dests) * num_dests);
  460. job->offset = 0;
  461. job->nr_pages = 0;
  462. job->pages = NULL;
  463. job->fn = fn;
  464. job->context = context;
  465. if (job->source.count < SUB_JOB_SIZE)
  466. dispatch_job(job);
  467. else {
  468. init_MUTEX(&job->lock);
  469. job->progress = 0;
  470. split_job(job);
  471. }
  472. return 0;
  473. }
  474. /*
  475. * Cancels a kcopyd job, eg. someone might be deactivating a
  476. * mirror.
  477. */
  478. #if 0
  479. int kcopyd_cancel(struct kcopyd_job *job, int block)
  480. {
  481. /* FIXME: finish */
  482. return -1;
  483. }
  484. #endif /* 0 */
  485. /*-----------------------------------------------------------------
  486. * Unit setup
  487. *---------------------------------------------------------------*/
  488. static DEFINE_MUTEX(_client_lock);
  489. static LIST_HEAD(_clients);
  490. static void client_add(struct kcopyd_client *kc)
  491. {
  492. mutex_lock(&_client_lock);
  493. list_add(&kc->list, &_clients);
  494. mutex_unlock(&_client_lock);
  495. }
  496. static void client_del(struct kcopyd_client *kc)
  497. {
  498. mutex_lock(&_client_lock);
  499. list_del(&kc->list);
  500. mutex_unlock(&_client_lock);
  501. }
  502. static DEFINE_MUTEX(kcopyd_init_lock);
  503. static int kcopyd_clients = 0;
  504. static int kcopyd_init(void)
  505. {
  506. int r;
  507. mutex_lock(&kcopyd_init_lock);
  508. if (kcopyd_clients) {
  509. /* Already initialized. */
  510. kcopyd_clients++;
  511. mutex_unlock(&kcopyd_init_lock);
  512. return 0;
  513. }
  514. r = jobs_init();
  515. if (r) {
  516. mutex_unlock(&kcopyd_init_lock);
  517. return r;
  518. }
  519. _kcopyd_wq = create_singlethread_workqueue("kcopyd");
  520. if (!_kcopyd_wq) {
  521. jobs_exit();
  522. mutex_unlock(&kcopyd_init_lock);
  523. return -ENOMEM;
  524. }
  525. kcopyd_clients++;
  526. INIT_WORK(&_kcopyd_work, do_work);
  527. mutex_unlock(&kcopyd_init_lock);
  528. return 0;
  529. }
  530. static void kcopyd_exit(void)
  531. {
  532. mutex_lock(&kcopyd_init_lock);
  533. kcopyd_clients--;
  534. if (!kcopyd_clients) {
  535. jobs_exit();
  536. destroy_workqueue(_kcopyd_wq);
  537. _kcopyd_wq = NULL;
  538. }
  539. mutex_unlock(&kcopyd_init_lock);
  540. }
  541. int kcopyd_client_create(unsigned int nr_pages, struct kcopyd_client **result)
  542. {
  543. int r = 0;
  544. struct kcopyd_client *kc;
  545. r = kcopyd_init();
  546. if (r)
  547. return r;
  548. kc = kmalloc(sizeof(*kc), GFP_KERNEL);
  549. if (!kc) {
  550. kcopyd_exit();
  551. return -ENOMEM;
  552. }
  553. spin_lock_init(&kc->lock);
  554. kc->pages = NULL;
  555. kc->nr_pages = kc->nr_free_pages = 0;
  556. r = client_alloc_pages(kc, nr_pages);
  557. if (r) {
  558. kfree(kc);
  559. kcopyd_exit();
  560. return r;
  561. }
  562. kc->io_client = dm_io_client_create(nr_pages);
  563. if (IS_ERR(kc->io_client)) {
  564. r = PTR_ERR(kc->io_client);
  565. client_free_pages(kc);
  566. kfree(kc);
  567. kcopyd_exit();
  568. return r;
  569. }
  570. init_waitqueue_head(&kc->destroyq);
  571. atomic_set(&kc->nr_jobs, 0);
  572. client_add(kc);
  573. *result = kc;
  574. return 0;
  575. }
  576. void kcopyd_client_destroy(struct kcopyd_client *kc)
  577. {
  578. /* Wait for completion of all jobs submitted by this client. */
  579. wait_event(kc->destroyq, !atomic_read(&kc->nr_jobs));
  580. dm_io_client_destroy(kc->io_client);
  581. client_free_pages(kc);
  582. client_del(kc);
  583. kfree(kc);
  584. kcopyd_exit();
  585. }
  586. EXPORT_SYMBOL(kcopyd_client_create);
  587. EXPORT_SYMBOL(kcopyd_client_destroy);
  588. EXPORT_SYMBOL(kcopyd_copy);