kcopyd.c 14 KB

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