kcopyd.c 14 KB

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