vserver 1.9.5.x5
[linux-2.6.git] / drivers / md / dm-io.c
1 /*
2  * Copyright (C) 2003 Sistina Software
3  *
4  * This file is released under the GPL.
5  */
6
7 #include "dm-io.h"
8
9 #include <linux/bio.h>
10 #include <linux/mempool.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14
15 #define BIO_POOL_SIZE 256
16
17
18 /*-----------------------------------------------------------------
19  * Bio set, move this to bio.c
20  *---------------------------------------------------------------*/
21 #define BV_NAME_SIZE 16
22 struct biovec_pool {
23         int nr_vecs;
24         char name[BV_NAME_SIZE];
25         kmem_cache_t *slab;
26         mempool_t *pool;
27         atomic_t allocated;     /* FIXME: debug */
28 };
29
30 #define BIOVEC_NR_POOLS 6
31 struct bio_set {
32         char name[BV_NAME_SIZE];
33         kmem_cache_t *bio_slab;
34         mempool_t *bio_pool;
35         struct biovec_pool pools[BIOVEC_NR_POOLS];
36 };
37
38 static void bio_set_exit(struct bio_set *bs)
39 {
40         unsigned i;
41         struct biovec_pool *bp;
42
43         if (bs->bio_pool)
44                 mempool_destroy(bs->bio_pool);
45
46         if (bs->bio_slab)
47                 kmem_cache_destroy(bs->bio_slab);
48
49         for (i = 0; i < BIOVEC_NR_POOLS; i++) {
50                 bp = bs->pools + i;
51                 if (bp->pool)
52                         mempool_destroy(bp->pool);
53
54                 if (bp->slab)
55                         kmem_cache_destroy(bp->slab);
56         }
57 }
58
59 static void mk_name(char *str, size_t len, const char *prefix, unsigned count)
60 {
61         snprintf(str, len, "%s-%u", prefix, count);
62 }
63
64 static int bio_set_init(struct bio_set *bs, const char *slab_prefix,
65                          unsigned pool_entries, unsigned scale)
66 {
67         /* FIXME: this must match bvec_index(), why not go the
68          * whole hog and have a pool per power of 2 ? */
69         static unsigned _vec_lengths[BIOVEC_NR_POOLS] = {
70                 1, 4, 16, 64, 128, BIO_MAX_PAGES
71         };
72
73
74         unsigned i, size;
75         struct biovec_pool *bp;
76
77         /* zero the bs so we can tear down properly on error */
78         memset(bs, 0, sizeof(*bs));
79
80         /*
81          * Set up the bio pool.
82          */
83         snprintf(bs->name, sizeof(bs->name), "%s-bio", slab_prefix);
84
85         bs->bio_slab = kmem_cache_create(bs->name, sizeof(struct bio), 0,
86                                          SLAB_HWCACHE_ALIGN, NULL, NULL);
87         if (!bs->bio_slab) {
88                 DMWARN("can't init bio slab");
89                 goto bad;
90         }
91
92         bs->bio_pool = mempool_create(pool_entries, mempool_alloc_slab,
93                                       mempool_free_slab, bs->bio_slab);
94         if (!bs->bio_pool) {
95                 DMWARN("can't init bio pool");
96                 goto bad;
97         }
98
99         /*
100          * Set up the biovec pools.
101          */
102         for (i = 0; i < BIOVEC_NR_POOLS; i++) {
103                 bp = bs->pools + i;
104                 bp->nr_vecs = _vec_lengths[i];
105                 atomic_set(&bp->allocated, 1); /* FIXME: debug */
106
107
108                 size = bp->nr_vecs * sizeof(struct bio_vec);
109
110                 mk_name(bp->name, sizeof(bp->name), slab_prefix, i);
111                 bp->slab = kmem_cache_create(bp->name, size, 0,
112                                              SLAB_HWCACHE_ALIGN, NULL, NULL);
113                 if (!bp->slab) {
114                         DMWARN("can't init biovec slab cache");
115                         goto bad;
116                 }
117
118                 if (i >= scale)
119                         pool_entries >>= 1;
120
121                 bp->pool = mempool_create(pool_entries, mempool_alloc_slab,
122                                           mempool_free_slab, bp->slab);
123                 if (!bp->pool) {
124                         DMWARN("can't init biovec mempool");
125                         goto bad;
126                 }
127         }
128
129         return 0;
130
131  bad:
132         bio_set_exit(bs);
133         return -ENOMEM;
134 }
135
136 /* FIXME: blech */
137 static inline unsigned bvec_index(unsigned nr)
138 {
139         switch (nr) {
140         case 1:         return 0;
141         case 2 ... 4:   return 1;
142         case 5 ... 16:  return 2;
143         case 17 ... 64: return 3;
144         case 65 ... 128:return 4;
145         case 129 ... BIO_MAX_PAGES: return 5;
146         }
147
148         BUG();
149         return 0;
150 }
151
152 static unsigned _bio_count = 0;
153 struct bio *bio_set_alloc(struct bio_set *bs, int gfp_mask, int nr_iovecs)
154 {
155         struct biovec_pool *bp;
156         struct bio_vec *bv = NULL;
157         unsigned long idx;
158         struct bio *bio;
159
160         bio = mempool_alloc(bs->bio_pool, gfp_mask);
161         if (unlikely(!bio))
162                 return NULL;
163
164         bio_init(bio);
165
166         if (likely(nr_iovecs)) {
167                 idx = bvec_index(nr_iovecs);
168                 bp = bs->pools + idx;
169                 bv = mempool_alloc(bp->pool, gfp_mask);
170                 if (!bv) {
171                         mempool_free(bio, bs->bio_pool);
172                         return NULL;
173                 }
174
175                 memset(bv, 0, bp->nr_vecs * sizeof(*bv));
176                 bio->bi_flags |= idx << BIO_POOL_OFFSET;
177                 bio->bi_max_vecs = bp->nr_vecs;
178                 atomic_inc(&bp->allocated);
179         }
180
181         bio->bi_io_vec = bv;
182         return bio;
183 }
184
185 static void bio_set_free(struct bio_set *bs, struct bio *bio)
186 {
187         struct biovec_pool *bp = bs->pools + BIO_POOL_IDX(bio);
188
189         if (atomic_dec_and_test(&bp->allocated))
190                 BUG();
191
192         mempool_free(bio->bi_io_vec, bp->pool);
193         mempool_free(bio, bs->bio_pool);
194 }
195
196 /*-----------------------------------------------------------------
197  * dm-io proper
198  *---------------------------------------------------------------*/
199 static struct bio_set _bios;
200
201 /* FIXME: can we shrink this ? */
202 struct io {
203         unsigned long error;
204         atomic_t count;
205         struct task_struct *sleeper;
206         io_notify_fn callback;
207         void *context;
208 };
209
210 /*
211  * io contexts are only dynamically allocated for asynchronous
212  * io.  Since async io is likely to be the majority of io we'll
213  * have the same number of io contexts as buffer heads ! (FIXME:
214  * must reduce this).
215  */
216 static unsigned _num_ios;
217 static mempool_t *_io_pool;
218
219 static void *alloc_io(int gfp_mask, void *pool_data)
220 {
221         return kmalloc(sizeof(struct io), gfp_mask);
222 }
223
224 static void free_io(void *element, void *pool_data)
225 {
226         kfree(element);
227 }
228
229 static unsigned int pages_to_ios(unsigned int pages)
230 {
231         return 4 * pages;       /* too many ? */
232 }
233
234 static int resize_pool(unsigned int new_ios)
235 {
236         int r = 0;
237
238         if (_io_pool) {
239                 if (new_ios == 0) {
240                         /* free off the pool */
241                         mempool_destroy(_io_pool);
242                         _io_pool = NULL;
243                         bio_set_exit(&_bios);
244
245                 } else {
246                         /* resize the pool */
247                         r = mempool_resize(_io_pool, new_ios, GFP_KERNEL);
248                 }
249
250         } else {
251                 /* create new pool */
252                 _io_pool = mempool_create(new_ios, alloc_io, free_io, NULL);
253                 if (!_io_pool)
254                         return -ENOMEM;
255
256                 r = bio_set_init(&_bios, "dm-io", 512, 1);
257                 if (r) {
258                         mempool_destroy(_io_pool);
259                         _io_pool = NULL;
260                 }
261         }
262
263         if (!r)
264                 _num_ios = new_ios;
265
266         return r;
267 }
268
269 int dm_io_get(unsigned int num_pages)
270 {
271         return resize_pool(_num_ios + pages_to_ios(num_pages));
272 }
273
274 void dm_io_put(unsigned int num_pages)
275 {
276         resize_pool(_num_ios - pages_to_ios(num_pages));
277 }
278
279 /*-----------------------------------------------------------------
280  * We need to keep track of which region a bio is doing io for.
281  * In order to save a memory allocation we store this the last
282  * bvec which we know is unused (blech).
283  *---------------------------------------------------------------*/
284 static inline void bio_set_region(struct bio *bio, unsigned region)
285 {
286         bio->bi_io_vec[bio->bi_max_vecs - 1].bv_len = region;
287 }
288
289 static inline unsigned bio_get_region(struct bio *bio)
290 {
291         return bio->bi_io_vec[bio->bi_max_vecs - 1].bv_len;
292 }
293
294 /*-----------------------------------------------------------------
295  * We need an io object to keep track of the number of bios that
296  * have been dispatched for a particular io.
297  *---------------------------------------------------------------*/
298 static void dec_count(struct io *io, unsigned int region, int error)
299 {
300         if (error)
301                 set_bit(region, &io->error);
302
303         if (atomic_dec_and_test(&io->count)) {
304                 if (io->sleeper)
305                         wake_up_process(io->sleeper);
306
307                 else {
308                         int r = io->error;
309                         io_notify_fn fn = io->callback;
310                         void *context = io->context;
311
312                         mempool_free(io, _io_pool);
313                         fn(r, context);
314                 }
315         }
316 }
317
318 /* FIXME Move this to bio.h? */
319 static void zero_fill_bio(struct bio *bio)
320 {
321         unsigned long flags;
322         struct bio_vec *bv;
323         int i;
324
325         bio_for_each_segment(bv, bio, i) {
326                 char *data = bvec_kmap_irq(bv, &flags);
327                 memset(data, 0, bv->bv_len);
328                 flush_dcache_page(bv->bv_page);
329                 bvec_kunmap_irq(data, &flags);
330         }
331 }
332
333 static int endio(struct bio *bio, unsigned int done, int error)
334 {
335         struct io *io = (struct io *) bio->bi_private;
336
337         /* keep going until we've finished */
338         if (bio->bi_size)
339                 return 1;
340
341         if (error && bio_data_dir(bio) == READ)
342                 zero_fill_bio(bio);
343
344         dec_count(io, bio_get_region(bio), error);
345         bio_put(bio);
346
347         return 0;
348 }
349
350 static void bio_dtr(struct bio *bio)
351 {
352         _bio_count--;
353         bio_set_free(&_bios, bio);
354 }
355
356 /*-----------------------------------------------------------------
357  * These little objects provide an abstraction for getting a new
358  * destination page for io.
359  *---------------------------------------------------------------*/
360 struct dpages {
361         void (*get_page)(struct dpages *dp,
362                          struct page **p, unsigned long *len, unsigned *offset);
363         void (*next_page)(struct dpages *dp);
364
365         unsigned context_u;
366         void *context_ptr;
367 };
368
369 /*
370  * Functions for getting the pages from a list.
371  */
372 static void list_get_page(struct dpages *dp,
373                   struct page **p, unsigned long *len, unsigned *offset)
374 {
375         unsigned o = dp->context_u;
376         struct page_list *pl = (struct page_list *) dp->context_ptr;
377
378         *p = pl->page;
379         *len = PAGE_SIZE - o;
380         *offset = o;
381 }
382
383 static void list_next_page(struct dpages *dp)
384 {
385         struct page_list *pl = (struct page_list *) dp->context_ptr;
386         dp->context_ptr = pl->next;
387         dp->context_u = 0;
388 }
389
390 static void list_dp_init(struct dpages *dp, struct page_list *pl, unsigned offset)
391 {
392         dp->get_page = list_get_page;
393         dp->next_page = list_next_page;
394         dp->context_u = offset;
395         dp->context_ptr = pl;
396 }
397
398 /*
399  * Functions for getting the pages from a bvec.
400  */
401 static void bvec_get_page(struct dpages *dp,
402                   struct page **p, unsigned long *len, unsigned *offset)
403 {
404         struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
405         *p = bvec->bv_page;
406         *len = bvec->bv_len;
407         *offset = bvec->bv_offset;
408 }
409
410 static void bvec_next_page(struct dpages *dp)
411 {
412         struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
413         dp->context_ptr = bvec + 1;
414 }
415
416 static void bvec_dp_init(struct dpages *dp, struct bio_vec *bvec)
417 {
418         dp->get_page = bvec_get_page;
419         dp->next_page = bvec_next_page;
420         dp->context_ptr = bvec;
421 }
422
423 static void vm_get_page(struct dpages *dp,
424                  struct page **p, unsigned long *len, unsigned *offset)
425 {
426         *p = vmalloc_to_page(dp->context_ptr);
427         *offset = dp->context_u;
428         *len = PAGE_SIZE - dp->context_u;
429 }
430
431 static void vm_next_page(struct dpages *dp)
432 {
433         dp->context_ptr += PAGE_SIZE - dp->context_u;
434         dp->context_u = 0;
435 }
436
437 static void vm_dp_init(struct dpages *dp, void *data)
438 {
439         dp->get_page = vm_get_page;
440         dp->next_page = vm_next_page;
441         dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
442         dp->context_ptr = data;
443 }
444
445 /*-----------------------------------------------------------------
446  * IO routines that accept a list of pages.
447  *---------------------------------------------------------------*/
448 static void do_region(int rw, unsigned int region, struct io_region *where,
449                       struct dpages *dp, struct io *io)
450 {
451         struct bio *bio;
452         struct page *page;
453         unsigned long len;
454         unsigned offset;
455         unsigned num_bvecs;
456         sector_t remaining = where->count;
457
458         while (remaining) {
459                 /*
460                  * Allocate a suitably sized bio, we add an extra
461                  * bvec for bio_get/set_region().
462                  */
463                 num_bvecs = (remaining / (PAGE_SIZE >> 9)) + 2;
464                 _bio_count++;
465                 bio = bio_set_alloc(&_bios, GFP_NOIO, num_bvecs);
466                 bio->bi_sector = where->sector + (where->count - remaining);
467                 bio->bi_bdev = where->bdev;
468                 bio->bi_end_io = endio;
469                 bio->bi_private = io;
470                 bio->bi_destructor = bio_dtr;
471                 bio_set_region(bio, region);
472
473                 /*
474                  * Try and add as many pages as possible.
475                  */
476                 while (remaining) {
477                         dp->get_page(dp, &page, &len, &offset);
478                         len = min(len, to_bytes(remaining));
479                         if (!bio_add_page(bio, page, len, offset))
480                                 break;
481
482                         offset = 0;
483                         remaining -= to_sector(len);
484                         dp->next_page(dp);
485                 }
486
487                 atomic_inc(&io->count);
488                 submit_bio(rw, bio);
489         }
490 }
491
492 static void dispatch_io(int rw, unsigned int num_regions,
493                         struct io_region *where, struct dpages *dp,
494                         struct io *io, int sync)
495 {
496         int i;
497         struct dpages old_pages = *dp;
498
499         if (sync)
500                 rw |= (1 << BIO_RW_SYNC);
501
502         /*
503          * For multiple regions we need to be careful to rewind
504          * the dp object for each call to do_region.
505          */
506         for (i = 0; i < num_regions; i++) {
507                 *dp = old_pages;
508                 if (where[i].count)
509                         do_region(rw, i, where + i, dp, io);
510         }
511
512         /*
513          * Drop the extra refence that we were holding to avoid
514          * the io being completed too early.
515          */
516         dec_count(io, 0, 0);
517 }
518
519 static int sync_io(unsigned int num_regions, struct io_region *where,
520             int rw, struct dpages *dp, unsigned long *error_bits)
521 {
522         struct io io;
523
524         if (num_regions > 1 && rw != WRITE) {
525                 WARN_ON(1);
526                 return -EIO;
527         }
528
529         io.error = 0;
530         atomic_set(&io.count, 1); /* see dispatch_io() */
531         io.sleeper = current;
532
533         dispatch_io(rw, num_regions, where, dp, &io, 1);
534
535         while (1) {
536                 set_current_state(TASK_UNINTERRUPTIBLE);
537
538                 if (!atomic_read(&io.count) || signal_pending(current))
539                         break;
540
541                 io_schedule();
542         }
543         set_current_state(TASK_RUNNING);
544
545         if (atomic_read(&io.count))
546                 return -EINTR;
547
548         *error_bits = io.error;
549         return io.error ? -EIO : 0;
550 }
551
552 static int async_io(unsigned int num_regions, struct io_region *where, int rw,
553              struct dpages *dp, io_notify_fn fn, void *context)
554 {
555         struct io *io;
556
557         if (num_regions > 1 && rw != WRITE) {
558                 WARN_ON(1);
559                 fn(1, context);
560                 return -EIO;
561         }
562
563         io = mempool_alloc(_io_pool, GFP_NOIO);
564         io->error = 0;
565         atomic_set(&io->count, 1); /* see dispatch_io() */
566         io->sleeper = NULL;
567         io->callback = fn;
568         io->context = context;
569
570         dispatch_io(rw, num_regions, where, dp, io, 0);
571         return 0;
572 }
573
574 int dm_io_sync(unsigned int num_regions, struct io_region *where, int rw,
575                struct page_list *pl, unsigned int offset,
576                unsigned long *error_bits)
577 {
578         struct dpages dp;
579         list_dp_init(&dp, pl, offset);
580         return sync_io(num_regions, where, rw, &dp, error_bits);
581 }
582
583 int dm_io_sync_bvec(unsigned int num_regions, struct io_region *where, int rw,
584                     struct bio_vec *bvec, unsigned long *error_bits)
585 {
586         struct dpages dp;
587         bvec_dp_init(&dp, bvec);
588         return sync_io(num_regions, where, rw, &dp, error_bits);
589 }
590
591 int dm_io_sync_vm(unsigned int num_regions, struct io_region *where, int rw,
592                   void *data, unsigned long *error_bits)
593 {
594         struct dpages dp;
595         vm_dp_init(&dp, data);
596         return sync_io(num_regions, where, rw, &dp, error_bits);
597 }
598
599 int dm_io_async(unsigned int num_regions, struct io_region *where, int rw,
600                 struct page_list *pl, unsigned int offset,
601                 io_notify_fn fn, void *context)
602 {
603         struct dpages dp;
604         list_dp_init(&dp, pl, offset);
605         return async_io(num_regions, where, rw, &dp, fn, context);
606 }
607
608 int dm_io_async_bvec(unsigned int num_regions, struct io_region *where, int rw,
609                      struct bio_vec *bvec, io_notify_fn fn, void *context)
610 {
611         struct dpages dp;
612         bvec_dp_init(&dp, bvec);
613         return async_io(num_regions, where, rw, &dp, fn, context);
614 }
615
616 int dm_io_async_vm(unsigned int num_regions, struct io_region *where, int rw,
617                    void *data, io_notify_fn fn, void *context)
618 {
619         struct dpages dp;
620         vm_dp_init(&dp, data);
621         return async_io(num_regions, where, rw, &dp, fn, context);
622 }
623
624 EXPORT_SYMBOL(dm_io_get);
625 EXPORT_SYMBOL(dm_io_put);
626 EXPORT_SYMBOL(dm_io_sync);
627 EXPORT_SYMBOL(dm_io_async);
628 EXPORT_SYMBOL(dm_io_sync_bvec);
629 EXPORT_SYMBOL(dm_io_async_bvec);
630 EXPORT_SYMBOL(dm_io_sync_vm);
631 EXPORT_SYMBOL(dm_io_async_vm);