]> nv-tegra.nvidia Code Review - linux-3.10.git/blobdiff - drivers/md/dm.c
Driver core: convert block from raw kobjects to core devices
[linux-3.10.git] / drivers / md / dm.c
index b5e56af8f85ac039a296445027d404f80f3dd9ba..f2d24eb3208c97e79bdf33b9d8c396f855f55566 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "dm.h"
 #include "dm-bio-list.h"
+#include "dm-uevent.h"
 
 #include <linux/init.h>
 #include <linux/module.h>
@@ -80,7 +81,7 @@ struct mapped_device {
 
        unsigned long flags;
 
-       request_queue_t *queue;
+       struct request_queue *queue;
        struct gendisk *disk;
        char name[16];
 
@@ -112,6 +113,9 @@ struct mapped_device {
         */
        atomic_t event_nr;
        wait_queue_head_t eventq;
+       atomic_t uevent_seq;
+       struct list_head uevent_list;
+       spinlock_t uevent_lock; /* Protect access to uevent_list */
 
        /*
         * freeze/thaw support require holding onto a super block
@@ -143,11 +147,19 @@ static int __init local_init(void)
                return -ENOMEM;
        }
 
+       r = dm_uevent_init();
+       if (r) {
+               kmem_cache_destroy(_tio_cache);
+               kmem_cache_destroy(_io_cache);
+               return r;
+       }
+
        _major = major;
        r = register_blkdev(_major, _name);
        if (r < 0) {
                kmem_cache_destroy(_tio_cache);
                kmem_cache_destroy(_io_cache);
+               dm_uevent_exit();
                return r;
        }
 
@@ -161,9 +173,8 @@ static void local_exit(void)
 {
        kmem_cache_destroy(_tio_cache);
        kmem_cache_destroy(_io_cache);
-
-       if (unregister_blkdev(_major, _name) < 0)
-               DMERR("unregister_blkdev failed");
+       unregister_blkdev(_major, _name);
+       dm_uevent_exit();
 
        _major = 0;
 
@@ -486,23 +497,20 @@ static void dec_pending(struct dm_io *io, int error)
                        blk_add_trace_bio(io->md->queue, io->bio,
                                          BLK_TA_COMPLETE);
 
-                       bio_endio(io->bio, io->bio->bi_size, io->error);
+                       bio_endio(io->bio, io->error);
                }
 
                free_io(io->md, io);
        }
 }
 
-static int clone_endio(struct bio *bio, unsigned int done, int error)
+static void clone_endio(struct bio *bio, int error)
 {
        int r = 0;
        struct dm_target_io *tio = bio->bi_private;
        struct mapped_device *md = tio->io->md;
        dm_endio_fn endio = tio->ti->type->end_io;
 
-       if (bio->bi_size)
-               return 1;
-
        if (!bio_flagged(bio, BIO_UPTODATE) && !error)
                error = -EIO;
 
@@ -516,7 +524,7 @@ static int clone_endio(struct bio *bio, unsigned int done, int error)
                        error = r;
                else if (r == DM_ENDIO_INCOMPLETE)
                        /* The target will handle the io */
-                       return 1;
+                       return;
                else if (r) {
                        DMWARN("unimplemented target endio return value: %d", r);
                        BUG();
@@ -532,7 +540,6 @@ static int clone_endio(struct bio *bio, unsigned int done, int error)
 
        bio_put(bio);
        free_tio(md, tio);
-       return r;
 }
 
 static sector_t max_io_len(struct mapped_device *md,
@@ -582,8 +589,8 @@ static void __map_bio(struct dm_target *ti, struct bio *clone,
                /* the bio has been remapped so dispatch it */
 
                blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
-                                   tio->io->bio->bi_bdev->bd_dev, sector,
-                                   clone->bi_sector);
+                                   tio->io->bio->bi_bdev->bd_dev,
+                                   clone->bi_sector, sector);
 
                generic_make_request(clone);
        } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
@@ -665,13 +672,19 @@ static struct bio *clone_bio(struct bio *bio, sector_t sector,
        return clone;
 }
 
-static void __clone_and_map(struct clone_info *ci)
+static int __clone_and_map(struct clone_info *ci)
 {
        struct bio *clone, *bio = ci->bio;
-       struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
-       sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
+       struct dm_target *ti;
+       sector_t len = 0, max;
        struct dm_target_io *tio;
 
+       ti = dm_table_find_target(ci->map, ci->sector);
+       if (!dm_target_is_valid(ti))
+               return -EIO;
+
+       max = max_io_len(ci->md, ci->sector, ti);
+
        /*
         * Allocate a target io object.
         */
@@ -729,6 +742,9 @@ static void __clone_and_map(struct clone_info *ci)
                do {
                        if (offset) {
                                ti = dm_table_find_target(ci->map, ci->sector);
+                               if (!dm_target_is_valid(ti))
+                                       return -EIO;
+
                                max = max_io_len(ci->md, ci->sector, ti);
 
                                tio = alloc_tio(ci->md);
@@ -752,20 +768,21 @@ static void __clone_and_map(struct clone_info *ci)
 
                ci->idx++;
        }
+
+       return 0;
 }
 
 /*
  * Split the bio into several clones.
  */
-static void __split_bio(struct mapped_device *md, struct bio *bio)
+static int __split_bio(struct mapped_device *md, struct bio *bio)
 {
        struct clone_info ci;
+       int error = 0;
 
        ci.map = dm_get_table(md);
-       if (!ci.map) {
-               bio_io_error(bio, bio->bi_size);
-               return;
-       }
+       if (unlikely(!ci.map))
+               return -EIO;
 
        ci.md = md;
        ci.bio = bio;
@@ -779,12 +796,14 @@ static void __split_bio(struct mapped_device *md, struct bio *bio)
        ci.idx = bio->bi_idx;
 
        start_io_acct(ci.io);
-       while (ci.sector_count)
-               __clone_and_map(&ci);
+       while (ci.sector_count && !error)
+               error = __clone_and_map(&ci);
 
        /* drop the extra reference count */
-       dec_pending(ci.io, 0);
+       dec_pending(ci.io, error);
        dm_table_put(ci.map);
+
+       return 0;
 }
 /*-----------------------------------------------------------------
  * CRUD END
@@ -794,12 +813,21 @@ static void __split_bio(struct mapped_device *md, struct bio *bio)
  * The request function that just remaps the bio built up by
  * dm_merge_bvec.
  */
-static int dm_request(request_queue_t *q, struct bio *bio)
+static int dm_request(struct request_queue *q, struct bio *bio)
 {
-       int r;
+       int r = -EIO;
        int rw = bio_data_dir(bio);
        struct mapped_device *md = q->queuedata;
 
+       /*
+        * There is no use in forwarding any barrier request since we can't
+        * guarantee it is (or can be) handled by the targets correctly.
+        */
+       if (unlikely(bio_barrier(bio))) {
+               bio_endio(bio, -EOPNOTSUPP);
+               return 0;
+       }
+
        down_read(&md->io_lock);
 
        disk_stat_inc(dm_disk(md), ios[rw]);
@@ -812,18 +840,11 @@ static int dm_request(request_queue_t *q, struct bio *bio)
        while (test_bit(DMF_BLOCK_IO, &md->flags)) {
                up_read(&md->io_lock);
 
-               if (bio_rw(bio) == READA) {
-                       bio_io_error(bio, bio->bi_size);
-                       return 0;
-               }
-
-               r = queue_io(md, bio);
-               if (r < 0) {
-                       bio_io_error(bio, bio->bi_size);
-                       return 0;
+               if (bio_rw(bio) != READA)
+                       r = queue_io(md, bio);
 
-               } else if (r == 0)
-                       return 0;       /* deferred successfully */
+               if (r <= 0)
+                       goto out_req;
 
                /*
                 * We're in a while loop, because someone could suspend
@@ -832,27 +853,17 @@ static int dm_request(request_queue_t *q, struct bio *bio)
                down_read(&md->io_lock);
        }
 
-       __split_bio(md, bio);
+       r = __split_bio(md, bio);
        up_read(&md->io_lock);
-       return 0;
-}
 
-static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
-                       sector_t *error_sector)
-{
-       struct mapped_device *md = q->queuedata;
-       struct dm_table *map = dm_get_table(md);
-       int ret = -ENXIO;
-
-       if (map) {
-               ret = dm_table_flush_all(map);
-               dm_table_put(map);
-       }
+out_req:
+       if (r < 0)
+               bio_io_error(bio);
 
-       return ret;
+       return 0;
 }
 
-static void dm_unplug_all(request_queue_t *q)
+static void dm_unplug_all(struct request_queue *q)
 {
        struct mapped_device *md = q->queuedata;
        struct dm_table *map = dm_get_table(md);
@@ -989,6 +1000,9 @@ static struct mapped_device *alloc_dev(int minor)
        atomic_set(&md->holders, 1);
        atomic_set(&md->open_count, 0);
        atomic_set(&md->event_nr, 0);
+       atomic_set(&md->uevent_seq, 0);
+       INIT_LIST_HEAD(&md->uevent_list);
+       spin_lock_init(&md->uevent_lock);
 
        md->queue = blk_alloc_queue(GFP_KERNEL);
        if (!md->queue)
@@ -1000,7 +1014,6 @@ static struct mapped_device *alloc_dev(int minor)
        blk_queue_make_request(md->queue, dm_request);
        blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
        md->queue->unplug_fn = dm_unplug_all;
-       md->queue->issue_flush_fn = dm_flush_all;
 
        md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
        if (!md->io_pool)
@@ -1057,12 +1070,14 @@ static struct mapped_device *alloc_dev(int minor)
        return NULL;
 }
 
+static void unlock_fs(struct mapped_device *md);
+
 static void free_dev(struct mapped_device *md)
 {
        int minor = md->disk->first_minor;
 
        if (md->suspended_bdev) {
-               thaw_bdev(md->suspended_bdev, NULL);
+               unlock_fs(md);
                bdput(md->suspended_bdev);
        }
        mempool_destroy(md->tio_pool);
@@ -1086,8 +1101,16 @@ static void free_dev(struct mapped_device *md)
  */
 static void event_callback(void *context)
 {
+       unsigned long flags;
+       LIST_HEAD(uevents);
        struct mapped_device *md = (struct mapped_device *) context;
 
+       spin_lock_irqsave(&md->uevent_lock, flags);
+       list_splice_init(&md->uevent_list, &uevents);
+       spin_unlock_irqrestore(&md->uevent_lock, flags);
+
+       dm_send_uevents(&uevents, &md->disk->dev.kobj);
+
        atomic_inc(&md->event_nr);
        wake_up(&md->eventq);
 }
@@ -1103,7 +1126,7 @@ static void __set_size(struct mapped_device *md, sector_t size)
 
 static int __bind(struct mapped_device *md, struct dm_table *t)
 {
-       request_queue_t *q = md->queue;
+       struct request_queue *q = md->queue;
        sector_t size;
 
        size = dm_table_get_size(t);
@@ -1246,7 +1269,8 @@ static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
        while (c) {
                n = c->bi_next;
                c->bi_next = NULL;
-               __split_bio(md, c);
+               if (__split_bio(md, c))
+                       bio_io_error(c);
                c = n;
        }
 }
@@ -1490,7 +1514,7 @@ int dm_resume(struct mapped_device *md)
 
        dm_table_unplug_all(map);
 
-       kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
+       dm_kobject_uevent(md);
 
        r = 0;
 
@@ -1504,6 +1528,16 @@ out:
 /*-----------------------------------------------------------------
  * Event notification.
  *---------------------------------------------------------------*/
+void dm_kobject_uevent(struct mapped_device *md)
+{
+       kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE);
+}
+
+uint32_t dm_next_uevent_seq(struct mapped_device *md)
+{
+       return atomic_add_return(1, &md->uevent_seq);
+}
+
 uint32_t dm_get_event_nr(struct mapped_device *md)
 {
        return atomic_read(&md->event_nr);
@@ -1515,6 +1549,15 @@ int dm_wait_event(struct mapped_device *md, int event_nr)
                        (event_nr != atomic_read(&md->event_nr)));
 }
 
+void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&md->uevent_lock, flags);
+       list_add(elist, &md->uevent_list);
+       spin_unlock_irqrestore(&md->uevent_lock, flags);
+}
+
 /*
  * The gendisk is only valid as long as you have a reference
  * count on 'md'.