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