@@ -53,56 +53,153 @@ int ubi_debugfs_init_dev(struct ubi_device *ubi);
5353void ubi_debugfs_exit_dev (struct ubi_device * ubi );
5454
5555/**
56- * ubi_dbg_is_bgt_disabled - if the background thread is disabled.
56+ * The following function is a legacy implementation of UBI fault-injection
57+ * hook. When using more powerful fault injection capabilities, the legacy
58+ * fault injection interface should be retained.
59+ */
60+ int ubi_dbg_power_cut (struct ubi_device * ubi , int caller );
61+
62+ static inline int ubi_dbg_bitflip (const struct ubi_device * ubi )
63+ {
64+ if (ubi -> dbg .emulate_bitflips )
65+ return !get_random_u32_below (200 );
66+ return 0 ;
67+ }
68+
69+ static inline int ubi_dbg_write_failure (const struct ubi_device * ubi )
70+ {
71+ if (ubi -> dbg .emulate_io_failures )
72+ return !get_random_u32_below (500 );
73+ return 0 ;
74+ }
75+
76+ static inline int ubi_dbg_erase_failure (const struct ubi_device * ubi )
77+ {
78+ if (ubi -> dbg .emulate_io_failures )
79+ return !get_random_u32_below (400 );
80+ return 0 ;
81+ }
82+
83+ /**
84+ * MASK_XXX: Mask for emulate_failures in ubi_debug_info.The mask is used to
85+ * precisely control the type and process of fault injection.
86+ */
87+ /* Emulate a power cut when writing EC/VID header */
88+ #define MASK_POWER_CUT_EC (1 << 0)
89+ #define MASK_POWER_CUT_VID (1 << 1)
90+
91+ #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
92+ /* Emulate bit-flips */
93+ #define MASK_BITFLIPS (1 << 2)
94+ /* Emulates -EIO during write/erase */
95+ #define MASK_IO_FAILURE (1 << 3)
96+
97+ extern bool should_fail_bitflips (void );
98+ extern bool should_fail_io_failures (void );
99+ extern bool should_fail_power_cut (void );
100+
101+ static inline bool ubi_dbg_fail_bitflip (const struct ubi_device * ubi )
102+ {
103+ if (ubi -> dbg .emulate_failures & MASK_BITFLIPS )
104+ return should_fail_bitflips ();
105+ return false;
106+ }
107+
108+ static inline bool ubi_dbg_fail_write (const struct ubi_device * ubi )
109+ {
110+ if (ubi -> dbg .emulate_failures & MASK_IO_FAILURE )
111+ return should_fail_io_failures ();
112+ return false;
113+ }
114+
115+ static inline bool ubi_dbg_fail_erase (const struct ubi_device * ubi )
116+ {
117+ if (ubi -> dbg .emulate_failures & MASK_IO_FAILURE )
118+ return should_fail_io_failures ();
119+ return false;
120+ }
121+
122+ static inline bool ubi_dbg_fail_power_cut (const struct ubi_device * ubi ,
123+ unsigned int caller )
124+ {
125+ if (ubi -> dbg .emulate_failures & caller )
126+ return should_fail_power_cut ();
127+ return false;
128+ }
129+
130+ #else /* CONFIG_MTD_UBI_FAULT_INJECTION */
131+
132+ #define ubi_dbg_fail_bitflip (u ) false
133+ #define ubi_dbg_fail_write (u ) false
134+ #define ubi_dbg_fail_erase (u ) false
135+ #define ubi_dbg_fail_power_cut (u , c ) false
136+ #endif
137+
138+ /**
139+ * ubi_dbg_is_power_cut - if it is time to emulate power cut.
57140 * @ubi: UBI device description object
58141 *
59- * Returns non-zero if the UBI background thread is disabled for testing
60- * purposes.
142+ * Returns true if power cut should be emulated, otherwise returns false.
61143 */
62- static inline int ubi_dbg_is_bgt_disabled (const struct ubi_device * ubi )
144+ static inline bool ubi_dbg_is_power_cut (struct ubi_device * ubi ,
145+ unsigned int caller )
63146{
64- return ubi -> dbg .disable_bgt ;
147+ if (ubi_dbg_power_cut (ubi , caller ))
148+ return true;
149+ return ubi_dbg_fail_power_cut (ubi , caller );
65150}
66151
67152/**
68153 * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip.
69154 * @ubi: UBI device description object
70155 *
71- * Returns non-zero if a bit-flip should be emulated, otherwise returns zero .
156+ * Returns true if a bit-flip should be emulated, otherwise returns false .
72157 */
73- static inline int ubi_dbg_is_bitflip (const struct ubi_device * ubi )
158+ static inline bool ubi_dbg_is_bitflip (const struct ubi_device * ubi )
74159{
75- if (ubi -> dbg . emulate_bitflips )
76- return ! get_random_u32_below ( 200 ) ;
77- return 0 ;
160+ if (ubi_dbg_bitflip ( ubi ) )
161+ return true ;
162+ return ubi_dbg_fail_bitflip ( ubi ) ;
78163}
79164
80165/**
81166 * ubi_dbg_is_write_failure - if it is time to emulate a write failure.
82167 * @ubi: UBI device description object
83168 *
84- * Returns non-zero if a write failure should be emulated, otherwise returns
85- * zero .
169+ * Returns true if a write failure should be emulated, otherwise returns
170+ * false .
86171 */
87- static inline int ubi_dbg_is_write_failure (const struct ubi_device * ubi )
172+ static inline bool ubi_dbg_is_write_failure (const struct ubi_device * ubi )
88173{
89- if (ubi -> dbg . emulate_io_failures )
90- return ! get_random_u32_below ( 500 ) ;
91- return 0 ;
174+ if (ubi_dbg_write_failure ( ubi ) )
175+ return true ;
176+ return ubi_dbg_fail_write ( ubi ) ;
92177}
93178
94179/**
95180 * ubi_dbg_is_erase_failure - if its time to emulate an erase failure.
96181 * @ubi: UBI device description object
97182 *
98- * Returns non-zero if an erase failure should be emulated, otherwise returns
99- * zero .
183+ * Returns true if an erase failure should be emulated, otherwise returns
184+ * false .
100185 */
101- static inline int ubi_dbg_is_erase_failure (const struct ubi_device * ubi )
186+ static inline bool ubi_dbg_is_erase_failure (const struct ubi_device * ubi )
102187{
103- if (ubi -> dbg .emulate_io_failures )
104- return !get_random_u32_below (400 );
105- return 0 ;
188+ if (ubi_dbg_erase_failure (ubi ))
189+ return true;
190+ return ubi_dbg_fail_erase (ubi );
191+ }
192+
193+ /**
194+ * ubi_dbg_is_bgt_disabled - if the background thread is disabled.
195+ * @ubi: UBI device description object
196+ *
197+ * Returns non-zero if the UBI background thread is disabled for testing
198+ * purposes.
199+ */
200+ static inline int ubi_dbg_is_bgt_disabled (const struct ubi_device * ubi )
201+ {
202+ return ubi -> dbg .disable_bgt ;
106203}
107204
108205static inline int ubi_dbg_chk_io (const struct ubi_device * ubi )
@@ -125,5 +222,4 @@ static inline void ubi_enable_dbg_chk_fastmap(struct ubi_device *ubi)
125222 ubi -> dbg .chk_fastmap = 1 ;
126223}
127224
128- int ubi_dbg_power_cut (struct ubi_device * ubi , int caller );
129225#endif /* !__UBI_DEBUG_H__ */
0 commit comments