Whamcloud - gitweb
LU-5409 obd: add CFS_FAULT_CHECK()
[fs/lustre-release.git] / libcfs / libcfs / fail.c
index e11caff..13a7539 100644 (file)
  * Lustre is a trademark of Oracle Corporation, Inc.
  */
 
-#ifndef __KERNEL__
-#include <liblustre.h>
-#else
 #include <libcfs/libcfs.h>
-#endif
 
 unsigned long cfs_fail_loc = 0;
 unsigned int cfs_fail_val = 0;
+int cfs_fail_err;
 wait_queue_head_t cfs_race_waitq;
 int cfs_race_state;
 
 EXPORT_SYMBOL(cfs_fail_loc);
 EXPORT_SYMBOL(cfs_fail_val);
+EXPORT_SYMBOL(cfs_fail_err);
 EXPORT_SYMBOL(cfs_race_waitq);
 EXPORT_SYMBOL(cfs_race_state);
 
 int __cfs_fail_check_set(__u32 id, __u32 value, int set)
 {
-        static cfs_atomic_t cfs_fail_count = CFS_ATOMIC_INIT(0);
-
-        LASSERT(!(id & CFS_FAIL_ONCE));
-
-        if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
-            (CFS_FAILED | CFS_FAIL_ONCE)) {
-                cfs_atomic_set(&cfs_fail_count, 0); /* paranoia */
-                return 0;
-        }
-
-        /* Fail 1/cfs_fail_val times */
-        if (cfs_fail_loc & CFS_FAIL_RAND) {
-                if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
-                        return 0;
-        }
-
-        /* Skip the first cfs_fail_val, then fail */
-        if (cfs_fail_loc & CFS_FAIL_SKIP) {
-                if (cfs_atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
-                        return 0;
-        }
-
-        /* check cfs_fail_val... */
-        if (set == CFS_FAIL_LOC_VALUE) {
-                if (cfs_fail_val != -1 && cfs_fail_val != value)
-                        return 0;
-        }
-
-        /* Fail cfs_fail_val times, overridden by FAIL_ONCE */
-        if (cfs_fail_loc & CFS_FAIL_SOME &&
-            (!(cfs_fail_loc & CFS_FAIL_ONCE) || cfs_fail_val <= 1)) {
-                int count = cfs_atomic_inc_return(&cfs_fail_count);
-
-                if (count >= cfs_fail_val) {
+       static atomic_t cfs_fail_count = ATOMIC_INIT(0);
+
+       LASSERT(!(id & CFS_FAIL_ONCE));
+
+       if ((cfs_fail_loc & (CFS_FAILED | CFS_FAIL_ONCE)) ==
+           (CFS_FAILED | CFS_FAIL_ONCE)) {
+               atomic_set(&cfs_fail_count, 0); /* paranoia */
+               return 0;
+       }
+
+       /* Fail 1/cfs_fail_val times */
+       if (cfs_fail_loc & CFS_FAIL_RAND) {
+               if (cfs_fail_val < 2 || cfs_rand() % cfs_fail_val > 0)
+                       return 0;
+       }
+
+       /* Skip the first cfs_fail_val, then fail */
+       if (cfs_fail_loc & CFS_FAIL_SKIP) {
+               if (atomic_inc_return(&cfs_fail_count) <= cfs_fail_val)
+                       return 0;
+       }
+
+       /* check cfs_fail_val... */
+       if (set == CFS_FAIL_LOC_VALUE) {
+               if (cfs_fail_val != -1 && cfs_fail_val != value)
+                       return 0;
+       }
+
+       /* Fail cfs_fail_val times, overridden by FAIL_ONCE */
+       if (cfs_fail_loc & CFS_FAIL_SOME &&
+           (!(cfs_fail_loc & CFS_FAIL_ONCE) || cfs_fail_val <= 1)) {
+               int count = atomic_inc_return(&cfs_fail_count);
+
+               if (count >= cfs_fail_val) {
                        set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
-                        cfs_atomic_set(&cfs_fail_count, 0);
-                        /* we are lost race to increase  */
-                        if (count > cfs_fail_val)
-                                return 0;
-                }
-        }
-
-        if ((set == CFS_FAIL_LOC_ORSET || set == CFS_FAIL_LOC_RESET) &&
-            (value & CFS_FAIL_ONCE))
+                       atomic_set(&cfs_fail_count, 0);
+                       /* we are lost race to increase  */
+                       if (count > cfs_fail_val)
+                               return 0;
+               }
+       }
+
+       /* Take into account the current call for FAIL_ONCE for ORSET only,
+        * as RESET is a new fail_loc, it does not change the current call */
+       if ((set == CFS_FAIL_LOC_ORSET) && (value & CFS_FAIL_ONCE))
                set_bit(CFS_FAIL_ONCE_BIT, &cfs_fail_loc);
-        /* Lost race to set CFS_FAILED_BIT. */
+       /* Lost race to set CFS_FAILED_BIT. */
        if (test_and_set_bit(CFS_FAILED_BIT, &cfs_fail_loc)) {
-                /* If CFS_FAIL_ONCE is valid, only one process can fail,
-                 * otherwise multi-process can fail at the same time. */
-                if (cfs_fail_loc & CFS_FAIL_ONCE)
-                        return 0;
-        }
-
-        switch (set) {
-                case CFS_FAIL_LOC_NOSET:
-                case CFS_FAIL_LOC_VALUE:
-                        break;
-                case CFS_FAIL_LOC_ORSET:
-                        cfs_fail_loc |= value & ~(CFS_FAILED | CFS_FAIL_ONCE);
-                        break;
-                case CFS_FAIL_LOC_RESET:
-                        cfs_fail_loc = value;
-                        break;
-                default:
-                        LASSERTF(0, "called with bad set %u\n", set);
-                        break;
-        }
-
-        return 1;
+               /* If CFS_FAIL_ONCE is valid, only one process can fail,
+                * otherwise multi-process can fail at the same time. */
+               if (cfs_fail_loc & CFS_FAIL_ONCE)
+                       return 0;
+       }
+
+       switch (set) {
+               case CFS_FAIL_LOC_NOSET:
+               case CFS_FAIL_LOC_VALUE:
+                       break;
+               case CFS_FAIL_LOC_ORSET:
+                       cfs_fail_loc |= value & ~(CFS_FAILED | CFS_FAIL_ONCE);
+                       break;
+               case CFS_FAIL_LOC_RESET:
+                       cfs_fail_loc = value;
+                       break;
+               default:
+                       LASSERTF(0, "called with bad set %u\n", set);
+                       break;
+       }
+
+       return 1;
 }
 EXPORT_SYMBOL(__cfs_fail_check_set);