kcopyd.c 13 KB

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