fix setattr error handling in sysfs, configfs
[safe/jmp/linux-2.6] / fs / dlm / user.c
index ebce994..b627285 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2009 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2006-2010 Red Hat, Inc.  All rights reserved.
  *
  * This copyrighted material is made available to anyone wishing to use,
  * modify, copy, or redistribute it subject to the terms and conditions
@@ -17,6 +17,7 @@
 #include <linux/spinlock.h>
 #include <linux/dlm.h>
 #include <linux/dlm_device.h>
+#include <linux/slab.h>
 
 #include "dlm_internal.h"
 #include "lockspace.h"
@@ -173,7 +174,7 @@ static int lkb_is_endoflife(struct dlm_lkb *lkb, int sb_status, int type)
 /* we could possibly check if the cancel of an orphan has resulted in the lkb
    being removed and then remove that lkb from the orphans list and free it */
 
-void dlm_user_add_ast(struct dlm_lkb *lkb, int type, int bastmode)
+void dlm_user_add_ast(struct dlm_lkb *lkb, int type, int mode)
 {
        struct dlm_ls *ls;
        struct dlm_user_args *ua;
@@ -206,12 +207,15 @@ void dlm_user_add_ast(struct dlm_lkb *lkb, int type, int bastmode)
 
        ast_type = lkb->lkb_ast_type;
        lkb->lkb_ast_type |= type;
-       if (bastmode)
-               lkb->lkb_bastmode = bastmode;
+       if (type == AST_BAST)
+               lkb->lkb_bastmode = mode;
+       else
+               lkb->lkb_castmode = mode;
 
        if (!ast_type) {
                kref_get(&lkb->lkb_ref);
                list_add_tail(&lkb->lkb_astqueue, &proc->asts);
+               lkb->lkb_ast_first = type;
                wake_up_interruptible(&proc->wait);
        }
        if (type == AST_COMP && (ast_type & AST_COMP))
@@ -220,7 +224,6 @@ void dlm_user_add_ast(struct dlm_lkb *lkb, int type, int bastmode)
 
        eol = lkb_is_endoflife(lkb, ua->lksb.sb_status, type);
        if (eol) {
-               lkb->lkb_ast_type &= ~AST_BAST;
                lkb->lkb_flags |= DLM_IFL_ENDOFLIFE;
        }
 
@@ -267,7 +270,7 @@ static int device_user_lock(struct dlm_user_proc *proc,
                goto out;
        }
 
-       ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
+       ua = kzalloc(sizeof(struct dlm_user_args), GFP_NOFS);
        if (!ua)
                goto out;
        ua->proc = proc;
@@ -307,7 +310,7 @@ static int device_user_unlock(struct dlm_user_proc *proc,
        if (!ls)
                return -ENOENT;
 
-       ua = kzalloc(sizeof(struct dlm_user_args), GFP_KERNEL);
+       ua = kzalloc(sizeof(struct dlm_user_args), GFP_NOFS);
        if (!ua)
                goto out;
        ua->proc = proc;
@@ -352,7 +355,7 @@ static int dlm_device_register(struct dlm_ls *ls, char *name)
 
        error = -ENOMEM;
        len = strlen(name) + strlen(name_prefix) + 2;
-       ls->ls_device.name = kzalloc(len, GFP_KERNEL);
+       ls->ls_device.name = kzalloc(len, GFP_NOFS);
        if (!ls->ls_device.name)
                goto fail;
 
@@ -520,7 +523,7 @@ static ssize_t device_write(struct file *file, const char __user *buf,
 #endif
                return -EINVAL;
 
-       kbuf = kzalloc(count + 1, GFP_KERNEL);
+       kbuf = kzalloc(count + 1, GFP_NOFS);
        if (!kbuf)
                return -ENOMEM;
 
@@ -546,7 +549,7 @@ static ssize_t device_write(struct file *file, const char __user *buf,
 
                /* add 1 after namelen so that the name string is terminated */
                kbuf = kzalloc(sizeof(struct dlm_write_request) + namelen + 1,
-                              GFP_KERNEL);
+                              GFP_NOFS);
                if (!kbuf) {
                        kfree(k32buf);
                        return -ENOMEM;
@@ -648,7 +651,7 @@ static int device_open(struct inode *inode, struct file *file)
        if (!ls)
                return -ENOENT;
 
-       proc = kzalloc(sizeof(struct dlm_user_proc), GFP_KERNEL);
+       proc = kzalloc(sizeof(struct dlm_user_proc), GFP_NOFS);
        if (!proc) {
                dlm_put_lockspace(ls);
                return -ENOMEM;
@@ -703,7 +706,7 @@ static int device_close(struct inode *inode, struct file *file)
 }
 
 static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
-                              int bmode, char __user *buf, size_t count)
+                              int mode, char __user *buf, size_t count)
 {
 #ifdef CONFIG_COMPAT
        struct dlm_lock_result32 result32;
@@ -730,7 +733,7 @@ static int copy_result_to_user(struct dlm_user_args *ua, int compat, int type,
        if (type == AST_BAST) {
                result.user_astaddr = ua->bastaddr;
                result.user_astparam = ua->bastparam;
-               result.bast_mode = bmode;
+               result.bast_mode = mode;
        } else {
                result.user_astaddr = ua->castaddr;
                result.user_astparam = ua->castparam;
@@ -798,7 +801,9 @@ static ssize_t device_read(struct file *file, char __user *buf, size_t count,
        struct dlm_user_proc *proc = file->private_data;
        struct dlm_lkb *lkb;
        DECLARE_WAITQUEUE(wait, current);
-       int error, type=0, bmode=0, removed = 0;
+       int error = 0, removed;
+       int ret_type, ret_mode;
+       int bastmode, castmode, do_bast, do_cast;
 
        if (count == sizeof(struct dlm_device_version)) {
                error = copy_version_to_user(buf, count);
@@ -817,6 +822,8 @@ static ssize_t device_read(struct file *file, char __user *buf, size_t count,
 #endif
                return -EINVAL;
 
+ try_another:
+
        /* do we really need this? can a read happen after a close? */
        if (test_bit(DLM_PROC_FLAGS_CLOSING, &proc->flags))
                return -EINVAL;
@@ -852,13 +859,55 @@ static ssize_t device_read(struct file *file, char __user *buf, size_t count,
 
        lkb = list_entry(proc->asts.next, struct dlm_lkb, lkb_astqueue);
 
-       if (lkb->lkb_ast_type & AST_COMP) {
-               lkb->lkb_ast_type &= ~AST_COMP;
-               type = AST_COMP;
-       } else if (lkb->lkb_ast_type & AST_BAST) {
-               lkb->lkb_ast_type &= ~AST_BAST;
-               type = AST_BAST;
-               bmode = lkb->lkb_bastmode;
+       removed = 0;
+       ret_type = 0;
+       ret_mode = 0;
+       do_bast = lkb->lkb_ast_type & AST_BAST;
+       do_cast = lkb->lkb_ast_type & AST_COMP;
+       bastmode = lkb->lkb_bastmode;
+       castmode = lkb->lkb_castmode;
+
+       /* when both are queued figure out which to do first and
+          switch first so the other goes in the next read */
+
+       if (do_cast && do_bast) {
+               if (lkb->lkb_ast_first == AST_COMP) {
+                       ret_type = AST_COMP;
+                       ret_mode = castmode;
+                       lkb->lkb_ast_type &= ~AST_COMP;
+                       lkb->lkb_ast_first = AST_BAST;
+               } else {
+                       ret_type = AST_BAST;
+                       ret_mode = bastmode;
+                       lkb->lkb_ast_type &= ~AST_BAST;
+                       lkb->lkb_ast_first = AST_COMP;
+               }
+       } else {
+               ret_type = lkb->lkb_ast_first;
+               ret_mode = (ret_type == AST_COMP) ? castmode : bastmode;
+               lkb->lkb_ast_type &= ~ret_type;
+               lkb->lkb_ast_first = 0;
+       }
+
+       /* if we're doing a bast but the bast is unnecessary, then
+          switch to do nothing or do a cast if that was needed next */
+
+       if ((ret_type == AST_BAST) &&
+           dlm_modes_compat(bastmode, lkb->lkb_castmode_done)) {
+               ret_type = 0;
+               ret_mode = 0;
+
+               if (do_cast) {
+                       ret_type = AST_COMP;
+                       ret_mode = castmode;
+                       lkb->lkb_ast_type &= ~AST_COMP;
+                       lkb->lkb_ast_first = 0;
+               }
+       }
+
+       if (lkb->lkb_ast_first != lkb->lkb_ast_type) {
+               log_print("device_read %x ast_first %x ast_type %x",
+                         lkb->lkb_id, lkb->lkb_ast_first, lkb->lkb_ast_type);
        }
 
        if (!lkb->lkb_ast_type) {
@@ -867,15 +916,29 @@ static ssize_t device_read(struct file *file, char __user *buf, size_t count,
        }
        spin_unlock(&proc->asts_spin);
 
-       error = copy_result_to_user(lkb->lkb_ua,
-                               test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
-                               type, bmode, buf, count);
+       if (ret_type) {
+               error = copy_result_to_user(lkb->lkb_ua,
+                               test_bit(DLM_PROC_FLAGS_COMPAT, &proc->flags),
+                               ret_type, ret_mode, buf, count);
+
+               if (ret_type == AST_COMP)
+                       lkb->lkb_castmode_done = castmode;
+               if (ret_type == AST_BAST)
+                       lkb->lkb_bastmode_done = bastmode;
+       }
 
        /* removes reference for the proc->asts lists added by
           dlm_user_add_ast() and may result in the lkb being freed */
+
        if (removed)
                dlm_put_lkb(lkb);
 
+       /* the bast that was queued was eliminated (see unnecessary above),
+          leaving nothing to return */
+
+       if (!ret_type)
+               goto try_another;
+
        return error;
 }