Motr  M0
finject_init.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #ifdef ENABLE_FAULT_INJECTION
24 
25 #include <linux/version.h> /* LINUX_VERSION_CODE */
26 #include <linux/kernel.h> /* UINT_MAX */
27 #include <linux/random.h> /* random32 */
28 #include <linux/sched.h> /* current */
29 
30 #include "lib/mutex.h" /* m0_mutex */
31 #include "lib/time.h" /* m0_time_now */
32 #include "lib/finject.h"
33 #include "lib/finject_internal.h"
34 
35 enum {
38 };
39 
40 M0_INTERNAL int m0_fi_init(void)
41 {
44  return 0;
45 }
46 
47 M0_INTERNAL void m0_fi_fini(void)
48 {
51 }
52 
56 M0_INTERNAL uint32_t fi_random(void)
57 {
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
59  u32 rnd = prandom_u32();
60 #else
61  u32 rnd = random32();
62 #endif
63  u32 roundup = rnd % FI_RAND_SCALE_UNIT ? 1 : 0;
64 
65  return rnd / FI_RAND_SCALE_UNIT + roundup;
66 }
67 
68 #else /* ENABLE_FAULT_INJECTION */
69 
70 M0_INTERNAL int m0_fi_init(void)
71 {
72  return 0;
73 }
74 
75 M0_INTERNAL void m0_fi_fini(void)
76 {
77 }
78 
79 #endif /* ENABLE_FAULT_INJECTION */
80 
81 /*
82  * Local variables:
83  * c-indentation-style: "K&R"
84  * c-basic-offset: 8
85  * tab-width: 8
86  * fill-column: 80
87  * scroll-step: 1
88  * End:
89  */
M0_INTERNAL int m0_fi_init(void)
Definition: finject_init.c:40
M0_INTERNAL void fi_states_init(void)
Definition: finject.c:199
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
M0_INTERNAL uint32_t fi_random(void)
Definition: finject_init.c:56
M0_INTERNAL void fi_states_fini(void)
Definition: finject.c:204
struct m0_mutex fi_states_mutex
Definition: finject.c:50
M0_INTERNAL void m0_fi_fini(void)
Definition: finject_init.c:47