bq2415x_charger.patch: Make sure that hook function can be called for init reported...
[kernel-power] / kernel-power-2.6.28 / debian / patches / dm-loop.diff
1 --- kernel-2.6.28.orig/drivers/md/Kconfig
2 +++ kernel-2.6.28/drivers/md/Kconfig
3 @@ -243,6 +243,15 @@
4  
5           If unsure, say N.
6  
7 +config DM_LOOP
8 +       tristate "Loop target (EXPERIMENTAL)"
9 +       depends on BLK_DEV_DM && EXPERIMENTAL
10 +       ---help---
11 +         This device-mapper target allows you to treat a regular file as
12 +         a block device.
13 +
14 +         If unsure, say N.
15 +
16  config DM_SNAPSHOT
17         tristate "Snapshot target"
18         depends on BLK_DEV_DM
19 --- kernel-2.6.28.orig/drivers/md/Makefile
20 +++ kernel-2.6.28/drivers/md/Makefile
21 @@ -32,6 +32,7 @@
22  obj-$(CONFIG_BLK_DEV_DM)       += dm-mod.o
23  obj-$(CONFIG_DM_CRYPT)         += dm-crypt.o
24  obj-$(CONFIG_DM_DELAY)         += dm-delay.o
25 +obj-$(CONFIG_DM_LOOP)          += dm-loop.o
26  obj-$(CONFIG_DM_MULTIPATH)     += dm-multipath.o dm-round-robin.o
27  obj-$(CONFIG_DM_SNAPSHOT)      += dm-snapshot.o
28  obj-$(CONFIG_DM_MIRROR)                += dm-mirror.o dm-log.o dm-region-hash.o
29 --- /dev/null
30 +++ kernel-2.6.28/drivers/md/dm-loop.c
31 @@ -0,0 +1,1039 @@
32 +/*
33 + * Copyright (C) 2006-2008 Red Hat, Inc. All rights reserved.
34 + *
35 + * This file is part of device-mapper.
36 + *
37 + * Extent mapping implementation heavily influenced by mm/swapfile.c
38 + * Bryn Reeves <breeves@redhat.com>
39 + *
40 + * File mapping and block lookup algorithms support by
41 + * Heinz Mauelshagen <hjm@redhat.com>.
42 + *
43 + * This file is released under the GPL.
44 + */
45 +
46 +#include <linux/kernel.h>
47 +#include <linux/slab.h>
48 +#include <linux/fs.h>
49 +#include <linux/module.h>
50 +#include <linux/vmalloc.h>
51 +#include <linux/syscalls.h>
52 +#include <linux/workqueue.h>
53 +#include <linux/file.h>
54 +#include <linux/bio.h>
55 +
56 +#include "dm.h"
57 +#include "dm-bio-list.h"
58 +
59 +#define DM_LOOP_DAEMON "kloopd"
60 +#define DM_MSG_PREFIX "loop"
61 +
62 +enum flags { DM_LOOP_BMAP, DM_LOOP_FSIO };
63 +
64 +/*--------------------------------------------------------------------
65 + * Loop context
66 + *--------------------------------------------------------------------*/
67 +
68 +struct loop_c {
69 +       unsigned long flags;
70 +
71 +       /* Backing store */
72 +
73 +       struct file *filp;
74 +       char *path;
75 +       loff_t offset;
76 +       struct block_device *bdev;
77 +       unsigned blkbits;               /* file system block size shift bits */
78 +
79 +       loff_t size;                    /* size of entire file in bytes */
80 +       loff_t blocks;                  /* blocks allocated to loop file */
81 +       sector_t mapped_sectors;        /* size of mapped area in sectors */
82 +
83 +       int (*map_fn)(struct dm_target *, struct bio *);
84 +       void *map_data;
85 +};
86 +
87 +/*
88 + * Block map extent
89 + */
90 +struct dm_loop_extent {
91 +       sector_t start;                 /* start sector in mapped device */
92 +       sector_t to;                    /* start sector on target device */
93 +       sector_t len;                   /* length in sectors */
94 +};
95 +
96 +/*
97 + * Temporary extent list
98 + */
99 +struct extent_list {
100 +       struct dm_loop_extent *extent;
101 +       struct list_head list;
102 +};
103 +
104 +static struct kmem_cache *dm_loop_extent_cache;
105 +
106 +/*
107 + * Block map private context
108 + */
109 +struct block_map_c {
110 +       int nr_extents;                 /* number of extents in map */
111 +       struct dm_loop_extent **map;    /* linear map of extent pointers */
112 +       struct dm_loop_extent **mru;    /* pointer to mru entry */
113 +       spinlock_t mru_lock;            /* protects mru */
114 +};
115 +
116 +/*
117 + * File map private context
118 + */
119 +struct file_map_c {
120 +       spinlock_t lock;                /* protects in */
121 +       struct bio_list in;             /* new bios for processing */
122 +       struct bio_list work;           /* bios queued for processing */
123 +       struct workqueue_struct *wq;    /* workqueue */
124 +       struct work_struct ws;          /* loop work */
125 +       struct loop_c *loop;            /* for filp & offset */
126 +};
127 +
128 +/*--------------------------------------------------------------------
129 + * Generic helpers
130 + *--------------------------------------------------------------------*/
131 +
132 +static sector_t blk2sect(struct loop_c *lc, blkcnt_t block)
133 +{
134 +       return block << (lc->blkbits - SECTOR_SHIFT);
135 +}
136 +
137 +static blkcnt_t sec2blk(struct loop_c *lc, sector_t sector)
138 +{
139 +       return sector >> (lc->blkbits - SECTOR_SHIFT);
140 +}
141 +
142 +/*--------------------------------------------------------------------
143 + * File I/O helpers
144 + *--------------------------------------------------------------------*/
145 +
146 +/*
147 + * transfer data to/from file using the read/write file_operations.
148 + */
149 +static int fs_io(int rw, struct file *filp, loff_t *pos, struct bio_vec *bv)
150 +{
151 +       ssize_t r;
152 +       void __user *ptr = (void __user __force *) kmap(bv->bv_page) +
153 +                          bv->bv_offset;
154 +       mm_segment_t old_fs = get_fs();
155 +
156 +       set_fs(get_ds());
157 +       r = (rw == READ) ? filp->f_op->read(filp, ptr, bv->bv_len, pos) :
158 +                          filp->f_op->write(filp, ptr, bv->bv_len, pos);
159 +       set_fs(old_fs);
160 +       kunmap(bv->bv_page);
161 +
162 +       return (r == bv->bv_len) ? 0 : -EIO;
163 +}
164 +
165 +/*
166 + * Handle I/O for one bio
167 + */
168 +static void do_one_bio(struct file_map_c *fc, struct bio *bio)
169 +{
170 +       int r = 0, rw = bio_data_dir(bio);
171 +       loff_t start = (bio->bi_sector << 9) + fc->loop->offset, pos = start;
172 +       struct bio_vec *bv, *bv_end = bio->bi_io_vec + bio->bi_vcnt;
173 +
174 +       for (bv = bio->bi_io_vec; bv < bv_end; bv++) {
175 +               r = fs_io(rw, fc->loop->filp, &pos, bv);
176 +               if (r) {
177 +                       DMERR("%s error %d", rw ? "write" : "read", r);
178 +                       break;
179 +               }
180 +       }
181 +
182 +       bio_endio(bio, r);
183 +}
184 +
185 +/*
186 + * Worker thread for a 'file' type loop device
187 + */
188 +static void do_loop_work(struct work_struct *ws)
189 +{
190 +       struct file_map_c *fc = container_of(ws, struct file_map_c, ws);
191 +       struct bio *bio;
192 +
193 +       /* quickly grab all new bios queued and add them to the work list */
194 +       spin_lock_irq(&fc->lock);
195 +       bio_list_merge(&fc->work, &fc->in);
196 +       bio_list_init(&fc->in);
197 +       spin_unlock_irq(&fc->lock);
198 +
199 +       /* work the list and do file I/O on all bios */
200 +       while ((bio = bio_list_pop(&fc->work)))
201 +               do_one_bio(fc, bio);
202 +}
203 +
204 +/*
205 + * Create work queue and initialize work
206 + */
207 +static int loop_work_init(struct loop_c *lc)
208 +{
209 +       struct file_map_c *fc = lc->map_data;
210 +
211 +       fc->wq = create_singlethread_workqueue(DM_LOOP_DAEMON);
212 +       if (!fc->wq)
213 +               return -ENOMEM;
214 +
215 +       return 0;
216 +}
217 +
218 +/*
219 + * Destroy work queue
220 + */
221 +static void loop_work_exit(struct loop_c *lc)
222 +{
223 +       struct file_map_c *fc = lc->map_data;
224 +
225 +       if (fc->wq)
226 +               destroy_workqueue(fc->wq);
227 +}
228 +
229 +/*
230 + * DM_LOOP_FSIO map_fn. Mapping just queues bios to the file map
231 + * context and lets the daemon deal with them.
232 + */
233 +static int loop_file_map(struct dm_target *ti, struct bio *bio)
234 +{
235 +       int wake;
236 +       struct loop_c *lc = ti->private;
237 +       struct file_map_c *fc = lc->map_data;
238 +
239 +       spin_lock_irq(&fc->lock);
240 +       wake = bio_list_empty(&fc->in);
241 +       bio_list_add(&fc->in, bio);
242 +       spin_unlock_irq(&fc->lock);
243 +
244 +       /*
245 +        * Only call queue_work() if necessary to avoid
246 +        * superfluous preempt_{disable/enable}() overhead.
247 +        */
248 +       if (wake)
249 +               queue_work(fc->wq, &fc->ws);
250 +
251 +       /* Handling bio - will submit later. */
252 +       return 0;
253 +}
254 +
255 +/*
256 + * Shutdown the workqueue and free a file mapping
257 + */
258 +static void destroy_file_map(struct loop_c *lc)
259 +{
260 +       loop_work_exit(lc);
261 +       kfree(lc->map_data);
262 +}
263 +
264 +/*
265 + * Set up a file map context and workqueue
266 + */
267 +static int setup_file_map(struct loop_c *lc)
268 +{
269 +       struct file_map_c *fc = kzalloc(sizeof(*fc), GFP_KERNEL);
270 +
271 +       if (!fc)
272 +               return -ENOMEM;
273 +
274 +       spin_lock_init(&fc->lock);
275 +       bio_list_init(&fc->in);
276 +       bio_list_init(&fc->work);
277 +       INIT_WORK(&fc->ws, do_loop_work);
278 +       fc->loop = lc;
279 +
280 +       lc->map_data = fc;
281 +       lc->map_fn = loop_file_map;
282 +
283 +       return loop_work_init(lc);
284 +}
285 +
286 +/*--------------------------------------------------------------------
287 + * Block I/O helpers
288 + *--------------------------------------------------------------------*/
289 +
290 +static int contains_sector(struct dm_loop_extent *e, sector_t s)
291 +{
292 +       if (likely(e))
293 +               return s < (e->start + (e->len)) && s >= e->start;
294 +
295 +       return 0;
296 +}
297 +
298 +/*
299 + * Walk over a linked list of extent_list structures, freeing them as
300 + * we go. Does not free el->extent.
301 + */
302 +static void destroy_extent_list(struct list_head *head)
303 +{
304 +       struct list_head *curr, *n;
305 +       struct extent_list *el;
306 +
307 +       if (list_empty(head))
308 +               return;
309 +
310 +       list_for_each_safe(curr, n, head) {
311 +               el = list_entry(curr, struct extent_list, list);
312 +               list_del(curr);
313 +               kfree(el);
314 +       }
315 +}
316 +
317 +/*
318 + * Add a new extent to the tail of the list at *head with
319 + * start/to/len parameters. Allocates from the extent cache.
320 + */
321 +static int list_add_extent(struct list_head *head, sector_t start,
322 +                          sector_t to, sector_t len)
323 +{
324 +       struct dm_loop_extent *extent;
325 +       struct extent_list *list;
326 +
327 +       extent = kmem_cache_alloc(dm_loop_extent_cache, GFP_KERNEL);
328 +       if (!extent)
329 +               goto out;
330 +
331 +       list = kmalloc(sizeof(*list), GFP_KERNEL);
332 +       if (!list)
333 +               goto out;
334 +
335 +       extent->start = start;
336 +       extent->to = to;
337 +       extent->len = len;
338 +
339 +       list->extent = extent;
340 +       list_add_tail(&list->list, head);
341 +
342 +       return 0;
343 +out:
344 +       if (extent)
345 +               kmem_cache_free(dm_loop_extent_cache, extent);
346 +       return -ENOMEM;
347 +}
348 +
349 +/*
350 + * Return an extent range (i.e. beginning and ending physical block numbers).
351 + */
352 +static int extent_range(struct inode *inode,
353 +                       blkcnt_t logical_blk, blkcnt_t last_blk,
354 +                       blkcnt_t *begin_blk, blkcnt_t *end_blk)
355 +{
356 +       sector_t dist = 0, phys_blk, probe_blk = logical_blk;
357 +
358 +       /* Find beginning physical block of extent starting at logical_blk. */
359 +       *begin_blk = phys_blk = bmap(inode, probe_blk);
360 +       if (!phys_blk)
361 +               return -ENXIO;
362 +
363 +       for (; phys_blk == *begin_blk + dist; dist++) {
364 +               *end_blk = phys_blk;
365 +               if (++probe_blk > last_blk)
366 +                       break;
367 +
368 +               phys_blk = bmap(inode, probe_blk);
369 +               if (unlikely(!phys_blk))
370 +                       return -ENXIO;
371 +       }
372 +
373 +       return 0;
374 +}
375 +
376 +/*
377 + * Create a sequential list of extents from an inode and return
378 + * it in *head. On success return the number of extents found or
379 + * -ERRNO on failure.
380 + */
381 +static int loop_extents(struct loop_c *lc, struct inode *inode,
382 +                       struct list_head *head)
383 +{
384 +       sector_t start = 0;
385 +       int r, nr_extents = 0;
386 +       blkcnt_t nr_blks = 0, begin_blk = 0, end_blk = 0;
387 +       blkcnt_t after_last_blk = sec2blk(lc,
388 +                       (lc->mapped_sectors + (lc->offset >> 9)));
389 +       blkcnt_t logical_blk = sec2blk(lc, (lc->offset >> 9));
390 +
391 +       /* for each block in the mapped region */
392 +       while (logical_blk < after_last_blk) {
393 +               r = extent_range(inode, logical_blk, after_last_blk - 1,
394 +                                &begin_blk, &end_blk);
395 +
396 +               /* sparse file fallback */
397 +               if (unlikely(r)) {
398 +                       DMWARN("%s has a hole; sparse file detected - "
399 +                              "switching to filesystem I/O", lc->path);
400 +                       clear_bit(DM_LOOP_BMAP, &lc->flags);
401 +                       set_bit(DM_LOOP_FSIO, &lc->flags);
402 +                       return r;
403 +               }
404 +
405 +               nr_blks = 1 + end_blk - begin_blk;
406 +
407 +               if (unlikely(!nr_blks))
408 +                       continue;
409 +
410 +               r = list_add_extent(head, start, blk2sect(lc, begin_blk),
411 +                                   blk2sect(lc, nr_blks));
412 +               if (unlikely(r))
413 +                       return r;
414 +
415 +               /* advance to next extent */
416 +               nr_extents++;
417 +               start += blk2sect(lc, nr_blks);
418 +               logical_blk += nr_blks;
419 +       }
420 +
421 +       return nr_extents;
422 +}
423 +
424 +/*
425 + * Walk over the extents in a block_map_c, returning them to the cache and
426 + * freeing bc->map and bc.
427 + */
428 +static void destroy_block_map(struct block_map_c *bc)
429 +{
430 +       unsigned i;
431 +
432 +       if (!bc)
433 +               return;
434 +
435 +       for (i = 0; i < bc->nr_extents; i++)
436 +               kmem_cache_free(dm_loop_extent_cache, bc->map[i]);
437 +
438 +       DMDEBUG("destroying block map of %d entries", i);
439 +
440 +       vfree(bc->map);
441 +       kfree(bc);
442 +}
443 +
444 +/*
445 + * Find an extent in *bc using binary search. Returns a pointer into the
446 + * extent map. Calculate index as (extent - bc->map).
447 + */
448 +static struct dm_loop_extent **extent_binary_lookup(struct block_map_c *bc,
449 +           struct dm_loop_extent **extent_mru, sector_t sector)
450 +{
451 +       unsigned nr_extents = bc->nr_extents;
452 +       unsigned delta, dist, prev_dist = 0;
453 +       struct dm_loop_extent **eptr;
454 +
455 +       /* Optimize lookup range based on MRU extent. */
456 +       dist = extent_mru - bc->map;
457 +       if ((*extent_mru)->start >= sector)
458 +               delta = dist = dist / 2;
459 +       else {
460 +               delta = (nr_extents - dist) / 2;
461 +               dist += delta;
462 +       }
463 +
464 +       eptr = bc->map + dist;
465 +       while (*eptr && !contains_sector(*eptr, sector)) {
466 +               if (sector >= (*eptr)->start + (*eptr)->len) {
467 +                       prev_dist = dist;
468 +                       if (delta > 1)
469 +                               delta /= 2;
470 +                       dist += delta;
471 +               } else {
472 +                       delta = (dist - prev_dist) / 2;
473 +                       if (!delta)
474 +                               delta = 1;
475 +                       dist -= delta;
476 +               }
477 +               eptr = bc->map + dist;
478 +       }
479 +
480 +       return eptr;
481 +}
482 +
483 +/*
484 + * Lookup an extent for a sector using the mru cache and binary search.
485 + */
486 +static struct dm_loop_extent *extent_lookup(struct block_map_c *bc,
487 +                                           sector_t sector)
488 +{
489 +       struct dm_loop_extent **eptr;
490 +
491 +       spin_lock_irq(&bc->mru_lock);
492 +       eptr = bc->mru;
493 +       spin_unlock_irq(&bc->mru_lock);
494 +
495 +       if (contains_sector(*eptr, sector))
496 +               return *eptr;
497 +
498 +       eptr = extent_binary_lookup(bc, eptr, sector);
499 +       if (!eptr)
500 +               return NULL;
501 +
502 +       spin_lock_irq(&bc->mru_lock);
503 +       bc->mru = eptr;
504 +       spin_unlock_irq(&bc->mru_lock);
505 +
506 +       return *eptr;
507 +}
508 +
509 +/*
510 + * DM_LOOP_BMAP map_fn. Looks up the sector in the extent map and
511 + * rewrites the bio device and bi_sector fields.
512 + */
513 +static int loop_block_map(struct dm_target *ti, struct bio *bio)
514 +{
515 +       struct loop_c *lc = ti->private;
516 +       struct dm_loop_extent *extent = extent_lookup(lc->map_data,
517 +                                                     bio->bi_sector);
518 +
519 +       if (likely(extent)) {
520 +               bio->bi_bdev = lc->bdev;
521 +               bio->bi_sector = extent->to + (bio->bi_sector - extent->start);
522 +               return 1;       /* Done with bio -> submit */
523 +       }
524 +
525 +       DMERR("no matching extent in map for sector %llu",
526 +             (unsigned long long) bio->bi_sector + ti->begin);
527 +       BUG();
528 +
529 +       return -EIO;
530 +}
531 +
532 +/*
533 + * Turn an extent_list into a linear pointer map of nr_extents + 1 entries
534 + * and set the final entry to NULL.
535 + */
536 +static struct dm_loop_extent **build_extent_map(struct list_head *head,
537 +                                               int nr_extents,
538 +                                               unsigned long *flags)
539 +{
540 +       unsigned map_size, cache_size;
541 +       struct dm_loop_extent **map, **curr;
542 +       struct list_head *pos;
543 +       struct extent_list *el;
544 +
545 +       map_size = 1 + (sizeof(*map) * nr_extents);
546 +       cache_size = kmem_cache_size(dm_loop_extent_cache) * nr_extents;
547 +
548 +       map = vmalloc(map_size);
549 +       curr = map;
550 +
551 +       DMDEBUG("allocated extent map of %u %s for %d extents (%u %s)",
552 +               (map_size < 8192) ? map_size : map_size >> 10,
553 +               (map_size < 8192) ? "bytes" : "kilobytes", nr_extents,
554 +               (cache_size < 8192) ? cache_size : cache_size >> 10,
555 +               (cache_size < 8192) ? "bytes" : "kilobytes");
556 +
557 +       list_for_each(pos, head) {
558 +               el = list_entry(pos, struct extent_list, list);
559 +               *(curr++) = el->extent;
560 +       }
561 +       *curr = NULL;
562 +
563 +       return map;
564 +}
565 +
566 +/*
567 + * Set up a block map context and extent map
568 + */
569 +static int setup_block_map(struct loop_c *lc, struct inode *inode)
570 +{
571 +       int r, nr_extents;
572 +       struct block_map_c *bc;
573 +       LIST_HEAD(head);
574 +
575 +       if (!inode || !inode->i_sb || !inode->i_sb->s_bdev)
576 +               return -ENXIO;
577 +
578 +       /* build a linked list of extents in linear order */
579 +       r = nr_extents = loop_extents(lc, inode, &head);
580 +       if (nr_extents < 1)
581 +               goto out;
582 +
583 +       r = -ENOMEM;
584 +       bc = kzalloc(sizeof(*bc), GFP_KERNEL);
585 +       if (!bc)
586 +               goto out;
587 +
588 +       /* create a linear map of pointers into the extent cache */
589 +       bc->map = build_extent_map(&head, nr_extents, &lc->flags);
590 +       destroy_extent_list(&head);
591 +
592 +       if (IS_ERR(bc->map)) {
593 +               r = PTR_ERR(bc->map);
594 +               goto out;
595 +       }
596 +
597 +       spin_lock_init(&bc->mru_lock);
598 +       bc->mru = bc->map;
599 +       bc->nr_extents = nr_extents;
600 +       lc->bdev = inode->i_sb->s_bdev;
601 +       lc->map_data = bc;
602 +       lc->map_fn = loop_block_map;
603 +
604 +       return 0;
605 +
606 +out:
607 +       return r;
608 +}
609 +
610 +/*--------------------------------------------------------------------
611 + * Generic helpers
612 + *--------------------------------------------------------------------*/
613 +
614 +/*
615 + * Invalidate all unlocked loop file pages
616 + */
617 +static int loop_invalidate_file(struct file *filp)
618 +{
619 +       int r;
620 +
621 +       /* Same as generic_file_direct_IO() */
622 +       unmap_mapping_range(filp->f_mapping, 0, ~0UL, 0);
623 +
624 +       r = filemap_write_and_wait(filp->f_mapping);
625 +       if (r)
626 +               return r;
627 +
628 +       /*
629 +        * This will remove all pages except dirty ones.
630 +        * If there are dirty pages at this point, it means that the user
631 +        * is writing to the file and the coherency is lost anyway.
632 +        * If the user was writing to the file simultaneously, this
633 +        * returns non-zero, but we ignore that.
634 +        */
635 +       invalidate_inode_pages2_range(filp->f_mapping, 0, ~0UL);
636 +
637 +       return 0;
638 +}
639 +
640 +/*
641 + * Acquire or release a "no-truncate" lock on *filp.
642 + * We overload the S_SWAPFILE flag for loop targets because
643 + * it provides the same no-truncate semantics we require, and
644 + * holding onto i_sem is no longer an option.
645 + */
646 +static void file_truncate_lock(struct file *filp)
647 +{
648 +       struct inode *inode = filp->f_mapping->host;
649 +
650 +       mutex_lock(&inode->i_mutex);
651 +       inode->i_flags |= S_SWAPFILE;
652 +       mutex_unlock(&inode->i_mutex);
653 +}
654 +
655 +static void file_truncate_unlock(struct file *filp)
656 +{
657 +       struct inode *inode = filp->f_mapping->host;
658 +
659 +       mutex_lock(&inode->i_mutex);
660 +       inode->i_flags &= ~S_SWAPFILE;
661 +       mutex_unlock(&inode->i_mutex);
662 +}
663 +
664 +/*
665 + * Fill out split_io for taget backing store
666 + */
667 +static void set_split_io(struct dm_target *ti)
668 +{
669 +       struct loop_c *lc = ti->private;
670 +
671 +       if (test_bit(DM_LOOP_BMAP, &lc->flags))
672 +               /* Split I/O at block boundaries */
673 +               ti->split_io = 1 << (lc->blkbits - SECTOR_SHIFT);
674 +       else
675 +               ti->split_io = 64;
676 +
677 +       DMDEBUG("splitting io at %llu sector boundaries",
678 +               (unsigned long long) ti->split_io);
679 +}
680 +
681 +/*
682 + * Check that the loop file is regular and available.
683 + */
684 +static int loop_check_file(struct dm_target *ti)
685 +{
686 +       struct loop_c *lc = ti->private;
687 +       struct file *filp = lc->filp;
688 +       struct inode *inode = filp->f_mapping->host;
689 +
690 +       if (!inode)
691 +               return -ENXIO;
692 +
693 +       ti->error = "backing file must be a regular file";
694 +       if (!S_ISREG(inode->i_mode))
695 +               return -EINVAL;
696 +
697 +       ti->error = "backing file is mapped into userspace for writing";
698 +       if (mapping_writably_mapped(filp->f_mapping))
699 +               return -EBUSY;
700 +
701 +       if (mapping_mapped(filp->f_mapping))
702 +               DMWARN("%s is mapped into userspace", lc->path);
703 +
704 +       if (!inode->i_sb || !inode->i_sb->s_bdev) {
705 +               DMWARN("%s has no blockdevice - switching to filesystem I/O",
706 +                      lc->path);
707 +               clear_bit(DM_LOOP_BMAP, &lc->flags);
708 +               set_bit(DM_LOOP_FSIO, &lc->flags);
709 +       }
710 +
711 +       ti->error = "backing file already in use";
712 +       if (IS_SWAPFILE(inode))
713 +               return -EBUSY;
714 +
715 +       return 0;
716 +}
717 +
718 +/*
719 + * Check loop file size and store it in the loop context
720 + */
721 +static int loop_setup_size(struct dm_target *ti)
722 +{
723 +       struct loop_c *lc = ti->private;
724 +       struct inode *inode = lc->filp->f_mapping->host;
725 +       int r = -EINVAL;
726 +
727 +       lc->size = i_size_read(inode);
728 +       lc->blkbits = inode->i_blkbits;
729 +
730 +       ti->error = "backing file is empty";
731 +       if (!lc->size)
732 +               goto out;
733 +
734 +       DMDEBUG("set backing file size to %llu", (unsigned long long) lc->size);
735 +
736 +       ti->error = "backing file cannot be less than one block in size";
737 +       if (lc->size < (blk2sect(lc, 1) << 9))
738 +               goto out;
739 +
740 +       ti->error = "loop file offset must be a multiple of fs blocksize";
741 +       if (lc->offset & ((1 << lc->blkbits) - 1))
742 +               goto out;
743 +
744 +       ti->error = "loop file offset too large";
745 +       if (lc->offset > (lc->size - (1 << 9)))
746 +               goto out;
747 +
748 +       lc->mapped_sectors = (lc->size - lc->offset) >> 9;
749 +       DMDEBUG("set mapped sectors to %llu (%llu bytes)",
750 +               (unsigned long long) lc->mapped_sectors,
751 +               (lc->size - lc->offset));
752 +
753 +       if ((lc->offset + (lc->mapped_sectors << 9)) < lc->size)
754 +               DMWARN("not using %llu bytes in incomplete block at EOF",
755 +                      lc->size - (lc->offset + (lc->mapped_sectors << 9)));
756 +
757 +       ti->error = "mapped region cannot be smaller than target size";
758 +       if (lc->size - lc->offset < (ti->len << 9))
759 +               goto out;
760 +
761 +       r = 0;
762 +
763 +out:
764 +       return r;
765 +}
766 +
767 +/*
768 + * release a loop file
769 + */
770 +static void loop_put_file(struct file *filp)
771 +{
772 +       if (!filp)
773 +               return;
774 +
775 +       file_truncate_unlock(filp);
776 +       filp_close(filp, NULL);
777 +}
778 +
779 +/*
780 + * Open loop file and perform type, availability and size checks.
781 + */
782 +static int loop_get_file(struct dm_target *ti)
783 +{
784 +       int flags = ((dm_table_get_mode(ti->table) & FMODE_WRITE) ?
785 +                    O_RDWR : O_RDONLY) | O_LARGEFILE;
786 +       struct loop_c *lc = ti->private;
787 +       struct file *filp;
788 +       int r = 0;
789 +
790 +       ti->error = "could not open backing file";
791 +       filp = filp_open(lc->path, flags, 0);
792 +       if (IS_ERR(filp))
793 +               return PTR_ERR(filp);
794 +       lc->filp = filp;
795 +       r = loop_check_file(ti);
796 +       if (r)
797 +               goto err;
798 +
799 +       r = loop_setup_size(ti);
800 +       if (r)
801 +               goto err;
802 +
803 +       file_truncate_lock(filp);
804 +       return 0;
805 +
806 +err:
807 +       fput(filp);
808 +       return r;
809 +}
810 +
811 +/*
812 + * invalidate mapped pages belonging to the loop file
813 + */
814 +static void loop_flush(struct dm_target *ti)
815 +{
816 +       struct loop_c *lc = ti->private;
817 +
818 +       loop_invalidate_file(lc->filp);
819 +}
820 +
821 +/*--------------------------------------------------------------------
822 + * Device-mapper target methods
823 + *--------------------------------------------------------------------*/
824 +
825 +/*
826 + * Generic loop map function. Re-base I/O to target begin and submit
827 + */
828 +static int loop_map(struct dm_target *ti, struct bio *bio,
829 +                   union map_info *context)
830 +{
831 +       struct loop_c *lc = ti->private;
832 +
833 +       if (unlikely(bio_barrier(bio)))
834 +               return -EOPNOTSUPP;
835 +
836 +       bio->bi_sector -= ti->begin;
837 +
838 +       if (lc->map_fn)
839 +               return lc->map_fn(ti, bio);
840 +
841 +       return -EIO;
842 +}
843 +
844 +/*
845 + * Block status helper
846 + */
847 +static ssize_t loop_file_status(struct loop_c *lc, char *result,
848 +                               unsigned maxlen)
849 +{
850 +       ssize_t sz = 0;
851 +       struct file_map_c *fc = lc->map_data;
852 +       int qlen;
853 +
854 +       spin_lock_irq(&fc->lock);
855 +       qlen = bio_list_size(&fc->work);
856 +       qlen += bio_list_size(&fc->in);
857 +       spin_unlock_irq(&fc->lock);
858 +
859 +       DMEMIT("file %d", qlen);
860 +
861 +       return sz;
862 +}
863 +
864 +/*
865 + * File status helper
866 + */
867 +static ssize_t loop_block_status(struct loop_c *lc, char *result,
868 +                                unsigned maxlen)
869 +{
870 +       ssize_t sz = 0;
871 +       struct block_map_c *bc = lc->map_data;
872 +       int mru;
873 +
874 +       spin_lock_irq(&bc->mru_lock);
875 +       mru = bc->mru - bc->map;
876 +       spin_unlock_irq(&bc->mru_lock);
877 +
878 +       DMEMIT("block %d %d", bc->nr_extents, mru);
879 +
880 +       return sz;
881 +}
882 +
883 +/*
884 + * This needs some thought on handling unlinked backing files. some parts of
885 + * the kernel return a cached name (now invalid), while others return a dcache
886 + * "/path/to/foo (deleted)" name (never was/is valid). Which is "better" is
887 + * debatable.
888 + *
889 + * On the one hand, using a cached name gives table output which is directly
890 + * usable assuming the user re-creates the unlinked image file, on the other
891 + * it is more consistent with e.g. swap to use the dcache name.
892 + *
893 +*/
894 +static int loop_status(struct dm_target *ti, status_type_t type, char *result,
895 +                      unsigned maxlen)
896 +{
897 +       struct loop_c *lc = ti->private;
898 +       ssize_t sz = 0;
899 +
900 +       switch (type) {
901 +       case STATUSTYPE_INFO:
902 +               if (test_bit(DM_LOOP_BMAP, &lc->flags))
903 +                       sz += loop_block_status(lc, result, maxlen - sz);
904 +               else if (test_bit(DM_LOOP_FSIO, &lc->flags))
905 +                       sz += loop_file_status(lc, result, maxlen - sz);
906 +               break;
907 +
908 +       case STATUSTYPE_TABLE:
909 +               DMEMIT("%s %llu", lc->path, lc->offset);
910 +               break;
911 +       }
912 +       return 0;
913 +}
914 +
915 +/*
916 + * Destroy a loopback mapping
917 + */
918 +static void loop_dtr(struct dm_target *ti)
919 +{
920 +       struct loop_c *lc = ti->private;
921 +
922 +       if ((dm_table_get_mode(ti->table) & FMODE_WRITE))
923 +               loop_invalidate_file(lc->filp);
924 +
925 +       if (test_bit(DM_LOOP_BMAP, &lc->flags) && lc->map_data)
926 +               destroy_block_map((struct block_map_c *)lc->map_data);
927 +       if (test_bit(DM_LOOP_FSIO, &lc->flags) && lc->map_data)
928 +               destroy_file_map(lc);
929 +
930 +       loop_put_file(lc->filp);
931 +       DMINFO("released file %s", lc->path);
932 +
933 +       kfree(lc);
934 +}
935 +
936 +/*
937 + * Construct a loopback mapping: <path> <offset>
938 + */
939 +static int loop_ctr(struct dm_target *ti, unsigned argc, char **argv)
940 +{
941 +       struct loop_c *lc = NULL;
942 +       int r = -EINVAL;
943 +
944 +       ti->error = "invalid argument count";
945 +       if (argc != 2)
946 +               goto err;
947 +
948 +       r = -ENOMEM;
949 +       ti->error = "cannot allocate loop context";
950 +       lc = kzalloc(sizeof(*lc), GFP_KERNEL);
951 +       if (!lc)
952 +               goto err;
953 +
954 +       /* default */
955 +       set_bit(DM_LOOP_BMAP, &lc->flags);
956 +       ti->error = "cannot allocate loop path";
957 +       lc->path = kstrdup(argv[0], GFP_KERNEL);
958 +       if (!lc->path)
959 +               goto err;
960 +
961 +       ti->private = lc;
962 +
963 +       r = -EINVAL;
964 +       ti->error = "invalid file offset";
965 +       if (sscanf(argv[1], "%lld", &lc->offset) != 1)
966 +               goto err;
967 +
968 +       if (lc->offset)
969 +               DMDEBUG("setting file offset to %lld", lc->offset);
970 +
971 +       /* open & check file and set size parameters */
972 +       r = loop_get_file(ti);
973 +
974 +       /* ti->error has been set by loop_get_file */
975 +       if (r)
976 +               goto err;
977 +
978 +       ti->error = "could not create loop mapping";
979 +       if (test_bit(DM_LOOP_BMAP, &lc->flags))
980 +               r = setup_block_map(lc, lc->filp->f_mapping->host);
981 +       if (test_bit(DM_LOOP_FSIO, &lc->flags))
982 +               r = setup_file_map(lc);
983 +
984 +       if (r)
985 +               goto err_putf;
986 +
987 +       loop_invalidate_file(lc->filp);
988 +
989 +       set_split_io(ti);
990 +       if (lc->bdev)
991 +               dm_set_device_limits(ti, lc->bdev);
992 +
993 +       DMDEBUG("constructed loop target on %s "
994 +               "(%lldk, %llu sectors)", lc->path,
995 +               (lc->size >> 10), (unsigned long long)lc->mapped_sectors);
996 +       ti->error = NULL;
997 +
998 +       return 0;
999 +
1000 +err_putf:
1001 +       loop_put_file(lc->filp);
1002 +err:
1003 +       kfree(lc);
1004 +       return r;
1005 +}
1006 +
1007 +static struct target_type loop_target = {
1008 +       .name = "loop",
1009 +       .version = {0, 0, 2},
1010 +       .module = THIS_MODULE,
1011 +       .ctr = loop_ctr,
1012 +       .dtr = loop_dtr,
1013 +       .map = loop_map,
1014 +       .presuspend = loop_flush,
1015 +       .flush = loop_flush,
1016 +       .status = loop_status,
1017 +};
1018 +
1019 +/*--------------------------------------------------------------------
1020 + * Module bits
1021 + *--------------------------------------------------------------------*/
1022 +static int __init dm_loop_init(void)
1023 +{
1024 +       int r;
1025 +
1026 +       r = dm_register_target(&loop_target);
1027 +       if (r < 0) {
1028 +               DMERR("register failed %d", r);
1029 +               goto err;
1030 +       }
1031 +
1032 +       r = -ENOMEM;
1033 +       dm_loop_extent_cache = KMEM_CACHE(dm_loop_extent, SLAB_HWCACHE_ALIGN);
1034 +       if (!dm_loop_extent_cache)
1035 +               goto err;
1036 +
1037 +       DMINFO("version %u.%u.%u loaded",
1038 +              loop_target.version[0], loop_target.version[1],
1039 +              loop_target.version[2]);
1040 +
1041 +       return 0;
1042 +
1043 +err:
1044 +       if (dm_loop_extent_cache)
1045 +               kmem_cache_destroy(dm_loop_extent_cache);
1046 +
1047 +       return r;
1048 +}
1049 +
1050 +static void __exit dm_loop_exit(void)
1051 +{
1052 +       int r;
1053 +
1054 +       r = dm_unregister_target(&loop_target);
1055 +       kmem_cache_destroy(dm_loop_extent_cache);
1056 +
1057 +       if (r < 0)
1058 +               DMERR("target unregister failed %d", r);
1059 +       else
1060 +               DMINFO("version %u.%u.%u unloaded",
1061 +                      loop_target.version[0], loop_target.version[1],
1062 +                      loop_target.version[2]);
1063 +}
1064 +
1065 +module_init(dm_loop_init);
1066 +module_exit(dm_loop_exit);
1067 +
1068 +MODULE_LICENSE("GPL");
1069 +MODULE_AUTHOR("Bryn Reeves <breeves@redhat.com>");
1070 +MODULE_DESCRIPTION("device-mapper loop target");