]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/ocfs2/cluster/heartbeat.c
[PATCH] sanitize blkdev_get() and friends
[linux-2.6-omap-h63xx.git] / fs / ocfs2 / cluster / heartbeat.c
index 979113479c664a2a8ea01bab4fa20ff3e8e0dadb..6ebaa58e2c03a1b4b237ceb1179b3c26607e7c4d 100644 (file)
@@ -216,8 +216,7 @@ static void o2hb_wait_on_io(struct o2hb_region *reg,
        wait_for_completion(&wc->wc_io_complete);
 }
 
-static int o2hb_bio_end_io(struct bio *bio,
-                          unsigned int bytes_done,
+static void o2hb_bio_end_io(struct bio *bio,
                           int error)
 {
        struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
@@ -227,12 +226,8 @@ static int o2hb_bio_end_io(struct bio *bio,
                wc->wc_error = error;
        }
 
-       if (bio->bi_size)
-               return 1;
-
        o2hb_bio_wait_dec(wc, 1);
        bio_put(bio);
-       return 0;
 }
 
 /* Setup a Bio to cover I/O against num_slots slots starting at
@@ -272,7 +267,7 @@ static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
                current_page = cs / spp;
                page = reg->hr_slot_data[current_page];
 
-               vec_len = min(PAGE_CACHE_SIZE,
+               vec_len = min(PAGE_CACHE_SIZE - vec_start,
                              (max_slots-cs) * (PAGE_CACHE_SIZE/spp) );
 
                mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
@@ -981,7 +976,7 @@ static void o2hb_region_release(struct config_item *item)
        }
 
        if (reg->hr_bdev)
-               blkdev_put(reg->hr_bdev);
+               blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
 
        if (reg->hr_slots)
                kfree(reg->hr_slots);
@@ -1273,7 +1268,7 @@ static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
                goto out;
 
        reg->hr_bdev = I_BDEV(filp->f_mapping->host);
-       ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ, 0);
+       ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ);
        if (ret) {
                reg->hr_bdev = NULL;
                goto out;
@@ -1335,6 +1330,7 @@ static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
        ret = wait_event_interruptible(o2hb_steady_queue,
                                atomic_read(&reg->hr_steady_iterations) == 0);
        if (ret) {
+               /* We got interrupted (hello ptrace!).  Clean up */
                spin_lock(&o2hb_live_lock);
                hb_task = reg->hr_task;
                reg->hr_task = NULL;
@@ -1345,7 +1341,16 @@ static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
                goto out;
        }
 
-       ret = count;
+       /* Ok, we were woken.  Make sure it wasn't by drop_item() */
+       spin_lock(&o2hb_live_lock);
+       hb_task = reg->hr_task;
+       spin_unlock(&o2hb_live_lock);
+
+       if (hb_task)
+               ret = count;
+       else
+               ret = -EIO;
+
 out:
        if (filp)
                fput(filp);
@@ -1353,7 +1358,7 @@ out:
                iput(inode);
        if (ret < 0) {
                if (reg->hr_bdev) {
-                       blkdev_put(reg->hr_bdev);
+                       blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
                        reg->hr_bdev = NULL;
                }
        }
@@ -1367,7 +1372,7 @@ static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
 
        spin_lock(&o2hb_live_lock);
        if (reg->hr_task)
-               pid = reg->hr_task->pid;
+               pid = task_pid_nr(reg->hr_task);
        spin_unlock(&o2hb_live_lock);
 
        if (!pid)
@@ -1488,24 +1493,18 @@ static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *g
                                                          const char *name)
 {
        struct o2hb_region *reg = NULL;
-       struct config_item *ret = NULL;
 
        reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
        if (reg == NULL)
-               goto out; /* ENOMEM */
+               return ERR_PTR(-ENOMEM);
 
        config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
 
-       ret = &reg->hr_item;
-
        spin_lock(&o2hb_live_lock);
        list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
        spin_unlock(&o2hb_live_lock);
-out:
-       if (ret == NULL)
-               kfree(reg);
 
-       return ret;
+       return &reg->hr_item;
 }
 
 static void o2hb_heartbeat_group_drop_item(struct config_group *group,
@@ -1523,6 +1522,15 @@ static void o2hb_heartbeat_group_drop_item(struct config_group *group,
        if (hb_task)
                kthread_stop(hb_task);
 
+       /*
+        * If we're racing a dev_write(), we need to wake them.  They will
+        * check reg->hr_task
+        */
+       if (atomic_read(&reg->hr_steady_iterations) != 0) {
+               atomic_set(&reg->hr_steady_iterations, 0);
+               wake_up(&o2hb_steady_queue);
+       }
+
        config_item_put(item);
 }
 
@@ -1665,7 +1673,67 @@ void o2hb_setup_callback(struct o2hb_callback_func *hc,
 }
 EXPORT_SYMBOL_GPL(o2hb_setup_callback);
 
-int o2hb_register_callback(struct o2hb_callback_func *hc)
+static struct o2hb_region *o2hb_find_region(const char *region_uuid)
+{
+       struct o2hb_region *p, *reg = NULL;
+
+       assert_spin_locked(&o2hb_live_lock);
+
+       list_for_each_entry(p, &o2hb_all_regions, hr_all_item) {
+               if (!strcmp(region_uuid, config_item_name(&p->hr_item))) {
+                       reg = p;
+                       break;
+               }
+       }
+
+       return reg;
+}
+
+static int o2hb_region_get(const char *region_uuid)
+{
+       int ret = 0;
+       struct o2hb_region *reg;
+
+       spin_lock(&o2hb_live_lock);
+
+       reg = o2hb_find_region(region_uuid);
+       if (!reg)
+               ret = -ENOENT;
+       spin_unlock(&o2hb_live_lock);
+
+       if (ret)
+               goto out;
+
+       ret = o2nm_depend_this_node();
+       if (ret)
+               goto out;
+
+       ret = o2nm_depend_item(&reg->hr_item);
+       if (ret)
+               o2nm_undepend_this_node();
+
+out:
+       return ret;
+}
+
+static void o2hb_region_put(const char *region_uuid)
+{
+       struct o2hb_region *reg;
+
+       spin_lock(&o2hb_live_lock);
+
+       reg = o2hb_find_region(region_uuid);
+
+       spin_unlock(&o2hb_live_lock);
+
+       if (reg) {
+               o2nm_undepend_item(&reg->hr_item);
+               o2nm_undepend_this_node();
+       }
+}
+
+int o2hb_register_callback(const char *region_uuid,
+                          struct o2hb_callback_func *hc)
 {
        struct o2hb_callback_func *tmp;
        struct list_head *iter;
@@ -1681,6 +1749,12 @@ int o2hb_register_callback(struct o2hb_callback_func *hc)
                goto out;
        }
 
+       if (region_uuid) {
+               ret = o2hb_region_get(region_uuid);
+               if (ret)
+                       goto out;
+       }
+
        down_write(&o2hb_callback_sem);
 
        list_for_each(iter, &hbcall->list) {
@@ -1702,16 +1776,21 @@ out:
 }
 EXPORT_SYMBOL_GPL(o2hb_register_callback);
 
-void o2hb_unregister_callback(struct o2hb_callback_func *hc)
+void o2hb_unregister_callback(const char *region_uuid,
+                             struct o2hb_callback_func *hc)
 {
        BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
 
        mlog(ML_HEARTBEAT, "on behalf of %p for funcs %p\n",
             __builtin_return_address(0), hc);
 
+       /* XXX Can this happen _with_ a region reference? */
        if (list_empty(&hc->hc_item))
                return;
 
+       if (region_uuid)
+               o2hb_region_put(region_uuid);
+
        down_write(&o2hb_callback_sem);
 
        list_del_init(&hc->hc_item);