loop: fix NULL dereference if mount fails
[safe/jmp/linux-2.6] / fs / ocfs2 / stack_user.c
index cd12001..ff4c798 100644 (file)
@@ -28,6 +28,7 @@
 #include "ocfs2.h"  /* For struct ocfs2_lock_res */
 #include "stackglue.h"
 
+#include <linux/dlm_plock.h>
 
 /*
  * The control protocol starts with a handshake.  Until the handshake
@@ -62,7 +63,7 @@
  * negotiated by the client.  The client negotiates based on the maximum
  * version advertised in /sys/fs/ocfs2/max_locking_protocol.  The major
  * number from the "SETV" message must match
- * user_stack.sp_proto->lp_max_version.pv_major, and the minor number
+ * ocfs2_user_plugin.sp_proto->lp_max_version.pv_major, and the minor number
  * must be less than or equal to ...->lp_max_version.pv_minor.
  *
  * Once this information has been set, mounts will be allowed.  From this
@@ -154,7 +155,7 @@ union ocfs2_control_message {
        struct ocfs2_control_message_down       u_down;
 };
 
-static struct ocfs2_stack_plugin user_stack;
+static struct ocfs2_stack_plugin ocfs2_user_plugin;
 
 static atomic_t ocfs2_control_opened;
 static int ocfs2_control_this_node = -1;
@@ -400,7 +401,7 @@ static int ocfs2_control_do_setversion_msg(struct file *file,
        char *ptr = NULL;
        struct ocfs2_control_private *p = file->private_data;
        struct ocfs2_protocol_version *max =
-               &user_stack.sp_proto->lp_max_version;
+               &ocfs2_user_plugin.sp_proto->lp_max_version;
 
        if (ocfs2_control_get_handshake_state(file) !=
            OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
@@ -550,26 +551,17 @@ static ssize_t ocfs2_control_read(struct file *file,
                                  size_t count,
                                  loff_t *ppos)
 {
-       char *proto_string = OCFS2_CONTROL_PROTO;
-       size_t to_write = 0;
-
-       if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
-               return 0;
-
-       to_write = OCFS2_CONTROL_PROTO_LEN - *ppos;
-       if (to_write > count)
-               to_write = count;
-       if (copy_to_user(buf, proto_string + *ppos, to_write))
-               return -EFAULT;
+       ssize_t ret;
 
-       *ppos += to_write;
+       ret = simple_read_from_buffer(buf, count, ppos,
+                       OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN);
 
        /* Have we read the whole protocol list? */
-       if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
+       if (ret > 0 && *ppos >= OCFS2_CONTROL_PROTO_LEN)
                ocfs2_control_set_handshake_state(file,
                                                  OCFS2_CONTROL_HANDSHAKE_READ);
 
-       return to_write;
+       return ret;
 }
 
 static int ocfs2_control_release(struct inode *inode, struct file *file)
@@ -683,7 +675,7 @@ static void fsdlm_lock_ast_wrapper(void *astarg)
        struct dlm_lksb *lksb = fsdlm_astarg_to_lksb(astarg);
        int status = lksb->sb_status;
 
-       BUG_ON(user_stack.sp_proto == NULL);
+       BUG_ON(ocfs2_user_plugin.sp_proto == NULL);
 
        /*
         * For now we're punting on the issue of other non-standard errors
@@ -696,16 +688,16 @@ static void fsdlm_lock_ast_wrapper(void *astarg)
         */
 
        if (status == -DLM_EUNLOCK || status == -DLM_ECANCEL)
-               user_stack.sp_proto->lp_unlock_ast(astarg, 0);
+               ocfs2_user_plugin.sp_proto->lp_unlock_ast(astarg, 0);
        else
-               user_stack.sp_proto->lp_lock_ast(astarg);
+               ocfs2_user_plugin.sp_proto->lp_lock_ast(astarg);
 }
 
 static void fsdlm_blocking_ast_wrapper(void *astarg, int level)
 {
-       BUG_ON(user_stack.sp_proto == NULL);
+       BUG_ON(ocfs2_user_plugin.sp_proto == NULL);
 
-       user_stack.sp_proto->lp_blocking_ast(astarg, level);
+       ocfs2_user_plugin.sp_proto->lp_blocking_ast(astarg, level);
 }
 
 static int user_dlm_lock(struct ocfs2_cluster_connection *conn,
@@ -746,8 +738,18 @@ static int user_dlm_lock_status(union ocfs2_dlm_lksb *lksb)
        return lksb->lksb_fsdlm.sb_status;
 }
 
+static int user_dlm_lvb_valid(union ocfs2_dlm_lksb *lksb)
+{
+       int invalid = lksb->lksb_fsdlm.sb_flags & DLM_SBF_VALNOTVALID;
+
+       return !invalid;
+}
+
 static void *user_dlm_lvb(union ocfs2_dlm_lksb *lksb)
 {
+       if (!lksb->lksb_fsdlm.sb_lvbptr)
+               lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
+                                            sizeof(struct dlm_lksb);
        return (void *)(lksb->lksb_fsdlm.sb_lvbptr);
 }
 
@@ -755,6 +757,37 @@ static void user_dlm_dump_lksb(union ocfs2_dlm_lksb *lksb)
 {
 }
 
+static int user_plock(struct ocfs2_cluster_connection *conn,
+                     u64 ino,
+                     struct file *file,
+                     int cmd,
+                     struct file_lock *fl)
+{
+       /*
+        * This more or less just demuxes the plock request into any
+        * one of three dlm calls.
+        *
+        * Internally, fs/dlm will pass these to a misc device, which
+        * a userspace daemon will read and write to.
+        *
+        * For now, cancel requests (which happen internally only),
+        * are turned into unlocks. Most of this function taken from
+        * gfs2_lock.
+        */
+
+       if (cmd == F_CANCELLK) {
+               cmd = F_SETLK;
+               fl->fl_type = F_UNLCK;
+       }
+
+       if (IS_GETLK(cmd))
+               return dlm_posix_get(conn->cc_lockspace, ino, file, fl);
+       else if (fl->fl_type == F_UNLCK)
+               return dlm_posix_unlock(conn->cc_lockspace, ino, file, fl);
+       else
+               return dlm_posix_lock(conn->cc_lockspace, ino, file, cmd, fl);
+}
+
 /*
  * Compare a requested locking protocol version against the current one.
  *
@@ -819,8 +852,7 @@ out:
        return rc;
 }
 
-static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn,
-                                  int hangup_pending)
+static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn)
 {
        dlm_release_lockspace(conn->cc_lockspace, 2);
        conn->cc_lockspace = NULL;
@@ -841,31 +873,33 @@ static int user_cluster_this_node(unsigned int *this_node)
        return 0;
 }
 
-static struct ocfs2_stack_operations user_stack_ops = {
+static struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
        .connect        = user_cluster_connect,
        .disconnect     = user_cluster_disconnect,
        .this_node      = user_cluster_this_node,
        .dlm_lock       = user_dlm_lock,
        .dlm_unlock     = user_dlm_unlock,
        .lock_status    = user_dlm_lock_status,
+       .lvb_valid      = user_dlm_lvb_valid,
        .lock_lvb       = user_dlm_lvb,
+       .plock          = user_plock,
        .dump_lksb      = user_dlm_dump_lksb,
 };
 
-static struct ocfs2_stack_plugin user_stack = {
+static struct ocfs2_stack_plugin ocfs2_user_plugin = {
        .sp_name        = "user",
-       .sp_ops         = &user_stack_ops,
+       .sp_ops         = &ocfs2_user_plugin_ops,
        .sp_owner       = THIS_MODULE,
 };
 
 
-static int __init user_stack_init(void)
+static int __init ocfs2_user_plugin_init(void)
 {
        int rc;
 
        rc = ocfs2_control_init();
        if (!rc) {
-               rc = ocfs2_stack_glue_register(&user_stack);
+               rc = ocfs2_stack_glue_register(&ocfs2_user_plugin);
                if (rc)
                        ocfs2_control_exit();
        }
@@ -873,14 +907,14 @@ static int __init user_stack_init(void)
        return rc;
 }
 
-static void __exit user_stack_exit(void)
+static void __exit ocfs2_user_plugin_exit(void)
 {
-       ocfs2_stack_glue_unregister(&user_stack);
+       ocfs2_stack_glue_unregister(&ocfs2_user_plugin);
        ocfs2_control_exit();
 }
 
 MODULE_AUTHOR("Oracle");
 MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
 MODULE_LICENSE("GPL");
-module_init(user_stack_init);
-module_exit(user_stack_exit);
+module_init(ocfs2_user_plugin_init);
+module_exit(ocfs2_user_plugin_exit);