netfilter: ebt_ulog: fix checkentry return value
[linux-2.6/mini2440.git] / drivers / md / dm-io.c
blobe73aabd61cd78abdbc63996704c177e6aea399d1
1 /*
2 * Copyright (C) 2003 Sistina Software
3 * Copyright (C) 2006 Red Hat GmbH
5 * This file is released under the GPL.
6 */
8 #include <linux/device-mapper.h>
10 #include <linux/bio.h>
11 #include <linux/mempool.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/dm-io.h>
17 struct dm_io_client {
18 mempool_t *pool;
19 struct bio_set *bios;
22 /* FIXME: can we shrink this ? */
23 struct io {
24 unsigned long error_bits;
25 atomic_t count;
26 struct task_struct *sleeper;
27 struct dm_io_client *client;
28 io_notify_fn callback;
29 void *context;
33 * io contexts are only dynamically allocated for asynchronous
34 * io. Since async io is likely to be the majority of io we'll
35 * have the same number of io contexts as bios! (FIXME: must reduce this).
38 static unsigned int pages_to_ios(unsigned int pages)
40 return 4 * pages; /* too many ? */
44 * Create a client with mempool and bioset.
46 struct dm_io_client *dm_io_client_create(unsigned num_pages)
48 unsigned ios = pages_to_ios(num_pages);
49 struct dm_io_client *client;
51 client = kmalloc(sizeof(*client), GFP_KERNEL);
52 if (!client)
53 return ERR_PTR(-ENOMEM);
55 client->pool = mempool_create_kmalloc_pool(ios, sizeof(struct io));
56 if (!client->pool)
57 goto bad;
59 client->bios = bioset_create(16, 0);
60 if (!client->bios)
61 goto bad;
63 return client;
65 bad:
66 if (client->pool)
67 mempool_destroy(client->pool);
68 kfree(client);
69 return ERR_PTR(-ENOMEM);
71 EXPORT_SYMBOL(dm_io_client_create);
73 int dm_io_client_resize(unsigned num_pages, struct dm_io_client *client)
75 return mempool_resize(client->pool, pages_to_ios(num_pages),
76 GFP_KERNEL);
78 EXPORT_SYMBOL(dm_io_client_resize);
80 void dm_io_client_destroy(struct dm_io_client *client)
82 mempool_destroy(client->pool);
83 bioset_free(client->bios);
84 kfree(client);
86 EXPORT_SYMBOL(dm_io_client_destroy);
88 /*-----------------------------------------------------------------
89 * We need to keep track of which region a bio is doing io for.
90 * In order to save a memory allocation we store this the last
91 * bvec which we know is unused (blech).
92 * XXX This is ugly and can OOPS with some configs... find another way.
93 *---------------------------------------------------------------*/
94 static inline void bio_set_region(struct bio *bio, unsigned region)
96 bio->bi_io_vec[bio->bi_max_vecs].bv_len = region;
99 static inline unsigned bio_get_region(struct bio *bio)
101 return bio->bi_io_vec[bio->bi_max_vecs].bv_len;
104 /*-----------------------------------------------------------------
105 * We need an io object to keep track of the number of bios that
106 * have been dispatched for a particular io.
107 *---------------------------------------------------------------*/
108 static void dec_count(struct io *io, unsigned int region, int error)
110 if (error)
111 set_bit(region, &io->error_bits);
113 if (atomic_dec_and_test(&io->count)) {
114 if (io->sleeper)
115 wake_up_process(io->sleeper);
117 else {
118 unsigned long r = io->error_bits;
119 io_notify_fn fn = io->callback;
120 void *context = io->context;
122 mempool_free(io, io->client->pool);
123 fn(r, context);
128 static void endio(struct bio *bio, int error)
130 struct io *io;
131 unsigned region;
133 if (error && bio_data_dir(bio) == READ)
134 zero_fill_bio(bio);
137 * The bio destructor in bio_put() may use the io object.
139 io = bio->bi_private;
140 region = bio_get_region(bio);
142 bio->bi_max_vecs++;
143 bio_put(bio);
145 dec_count(io, region, error);
148 /*-----------------------------------------------------------------
149 * These little objects provide an abstraction for getting a new
150 * destination page for io.
151 *---------------------------------------------------------------*/
152 struct dpages {
153 void (*get_page)(struct dpages *dp,
154 struct page **p, unsigned long *len, unsigned *offset);
155 void (*next_page)(struct dpages *dp);
157 unsigned context_u;
158 void *context_ptr;
162 * Functions for getting the pages from a list.
164 static void list_get_page(struct dpages *dp,
165 struct page **p, unsigned long *len, unsigned *offset)
167 unsigned o = dp->context_u;
168 struct page_list *pl = (struct page_list *) dp->context_ptr;
170 *p = pl->page;
171 *len = PAGE_SIZE - o;
172 *offset = o;
175 static void list_next_page(struct dpages *dp)
177 struct page_list *pl = (struct page_list *) dp->context_ptr;
178 dp->context_ptr = pl->next;
179 dp->context_u = 0;
182 static void list_dp_init(struct dpages *dp, struct page_list *pl, unsigned offset)
184 dp->get_page = list_get_page;
185 dp->next_page = list_next_page;
186 dp->context_u = offset;
187 dp->context_ptr = pl;
191 * Functions for getting the pages from a bvec.
193 static void bvec_get_page(struct dpages *dp,
194 struct page **p, unsigned long *len, unsigned *offset)
196 struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
197 *p = bvec->bv_page;
198 *len = bvec->bv_len;
199 *offset = bvec->bv_offset;
202 static void bvec_next_page(struct dpages *dp)
204 struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
205 dp->context_ptr = bvec + 1;
208 static void bvec_dp_init(struct dpages *dp, struct bio_vec *bvec)
210 dp->get_page = bvec_get_page;
211 dp->next_page = bvec_next_page;
212 dp->context_ptr = bvec;
216 * Functions for getting the pages from a VMA.
218 static void vm_get_page(struct dpages *dp,
219 struct page **p, unsigned long *len, unsigned *offset)
221 *p = vmalloc_to_page(dp->context_ptr);
222 *offset = dp->context_u;
223 *len = PAGE_SIZE - dp->context_u;
226 static void vm_next_page(struct dpages *dp)
228 dp->context_ptr += PAGE_SIZE - dp->context_u;
229 dp->context_u = 0;
232 static void vm_dp_init(struct dpages *dp, void *data)
234 dp->get_page = vm_get_page;
235 dp->next_page = vm_next_page;
236 dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
237 dp->context_ptr = data;
240 static void dm_bio_destructor(struct bio *bio)
242 struct io *io = bio->bi_private;
244 bio_free(bio, io->client->bios);
248 * Functions for getting the pages from kernel memory.
250 static void km_get_page(struct dpages *dp, struct page **p, unsigned long *len,
251 unsigned *offset)
253 *p = virt_to_page(dp->context_ptr);
254 *offset = dp->context_u;
255 *len = PAGE_SIZE - dp->context_u;
258 static void km_next_page(struct dpages *dp)
260 dp->context_ptr += PAGE_SIZE - dp->context_u;
261 dp->context_u = 0;
264 static void km_dp_init(struct dpages *dp, void *data)
266 dp->get_page = km_get_page;
267 dp->next_page = km_next_page;
268 dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
269 dp->context_ptr = data;
272 /*-----------------------------------------------------------------
273 * IO routines that accept a list of pages.
274 *---------------------------------------------------------------*/
275 static void do_region(int rw, unsigned region, struct dm_io_region *where,
276 struct dpages *dp, struct io *io)
278 struct bio *bio;
279 struct page *page;
280 unsigned long len;
281 unsigned offset;
282 unsigned num_bvecs;
283 sector_t remaining = where->count;
285 while (remaining) {
287 * Allocate a suitably sized-bio: we add an extra
288 * bvec for bio_get/set_region() and decrement bi_max_vecs
289 * to hide it from bio_add_page().
291 num_bvecs = dm_sector_div_up(remaining,
292 (PAGE_SIZE >> SECTOR_SHIFT));
293 num_bvecs = 1 + min_t(int, bio_get_nr_vecs(where->bdev),
294 num_bvecs);
295 if (unlikely(num_bvecs > BIO_MAX_PAGES))
296 num_bvecs = BIO_MAX_PAGES;
297 bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, io->client->bios);
298 bio->bi_sector = where->sector + (where->count - remaining);
299 bio->bi_bdev = where->bdev;
300 bio->bi_end_io = endio;
301 bio->bi_private = io;
302 bio->bi_destructor = dm_bio_destructor;
303 bio->bi_max_vecs--;
304 bio_set_region(bio, region);
307 * Try and add as many pages as possible.
309 while (remaining) {
310 dp->get_page(dp, &page, &len, &offset);
311 len = min(len, to_bytes(remaining));
312 if (!bio_add_page(bio, page, len, offset))
313 break;
315 offset = 0;
316 remaining -= to_sector(len);
317 dp->next_page(dp);
320 atomic_inc(&io->count);
321 submit_bio(rw, bio);
325 static void dispatch_io(int rw, unsigned int num_regions,
326 struct dm_io_region *where, struct dpages *dp,
327 struct io *io, int sync)
329 int i;
330 struct dpages old_pages = *dp;
332 if (sync)
333 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
336 * For multiple regions we need to be careful to rewind
337 * the dp object for each call to do_region.
339 for (i = 0; i < num_regions; i++) {
340 *dp = old_pages;
341 if (where[i].count)
342 do_region(rw, i, where + i, dp, io);
346 * Drop the extra reference that we were holding to avoid
347 * the io being completed too early.
349 dec_count(io, 0, 0);
352 static int sync_io(struct dm_io_client *client, unsigned int num_regions,
353 struct dm_io_region *where, int rw, struct dpages *dp,
354 unsigned long *error_bits)
356 struct io io;
358 if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
359 WARN_ON(1);
360 return -EIO;
363 io.error_bits = 0;
364 atomic_set(&io.count, 1); /* see dispatch_io() */
365 io.sleeper = current;
366 io.client = client;
368 dispatch_io(rw, num_regions, where, dp, &io, 1);
370 while (1) {
371 set_current_state(TASK_UNINTERRUPTIBLE);
373 if (!atomic_read(&io.count))
374 break;
376 io_schedule();
378 set_current_state(TASK_RUNNING);
380 if (error_bits)
381 *error_bits = io.error_bits;
383 return io.error_bits ? -EIO : 0;
386 static int async_io(struct dm_io_client *client, unsigned int num_regions,
387 struct dm_io_region *where, int rw, struct dpages *dp,
388 io_notify_fn fn, void *context)
390 struct io *io;
392 if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
393 WARN_ON(1);
394 fn(1, context);
395 return -EIO;
398 io = mempool_alloc(client->pool, GFP_NOIO);
399 io->error_bits = 0;
400 atomic_set(&io->count, 1); /* see dispatch_io() */
401 io->sleeper = NULL;
402 io->client = client;
403 io->callback = fn;
404 io->context = context;
406 dispatch_io(rw, num_regions, where, dp, io, 0);
407 return 0;
410 static int dp_init(struct dm_io_request *io_req, struct dpages *dp)
412 /* Set up dpages based on memory type */
413 switch (io_req->mem.type) {
414 case DM_IO_PAGE_LIST:
415 list_dp_init(dp, io_req->mem.ptr.pl, io_req->mem.offset);
416 break;
418 case DM_IO_BVEC:
419 bvec_dp_init(dp, io_req->mem.ptr.bvec);
420 break;
422 case DM_IO_VMA:
423 vm_dp_init(dp, io_req->mem.ptr.vma);
424 break;
426 case DM_IO_KMEM:
427 km_dp_init(dp, io_req->mem.ptr.addr);
428 break;
430 default:
431 return -EINVAL;
434 return 0;
438 * New collapsed (a)synchronous interface.
440 * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug
441 * the queue with blk_unplug() some time later or set the BIO_RW_SYNC bit in
442 * io_req->bi_rw. If you fail to do one of these, the IO will be submitted to
443 * the disk after q->unplug_delay, which defaults to 3ms in blk-settings.c.
445 int dm_io(struct dm_io_request *io_req, unsigned num_regions,
446 struct dm_io_region *where, unsigned long *sync_error_bits)
448 int r;
449 struct dpages dp;
451 r = dp_init(io_req, &dp);
452 if (r)
453 return r;
455 if (!io_req->notify.fn)
456 return sync_io(io_req->client, num_regions, where,
457 io_req->bi_rw, &dp, sync_error_bits);
459 return async_io(io_req->client, num_regions, where, io_req->bi_rw,
460 &dp, io_req->notify.fn, io_req->notify.context);
462 EXPORT_SYMBOL(dm_io);