debug.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author: Artem Bityutskiy (Битюцкий Артём)
  19. */
  20. #ifndef __UBI_DEBUG_H__
  21. #define __UBI_DEBUG_H__
  22. struct ubi_ec_hdr;
  23. struct ubi_vid_hdr;
  24. struct ubi_volume;
  25. struct ubi_vtbl_record;
  26. struct ubi_scan_volume;
  27. struct ubi_scan_leb;
  28. struct ubi_mkvol_req;
  29. #ifdef CONFIG_MTD_UBI_DEBUG
  30. #include <linux/random.h>
  31. #define ubi_assert(expr) do { \
  32. if (unlikely(!(expr))) { \
  33. printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
  34. __func__, __LINE__, current->pid); \
  35. ubi_dbg_dump_stack(); \
  36. } \
  37. } while (0)
  38. #define dbg_err(fmt, ...) ubi_err(fmt, ##__VA_ARGS__)
  39. #define ubi_dbg_dump_stack() dump_stack()
  40. #define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) \
  41. print_hex_dump(l, ps, pt, r, g, b, len, a)
  42. #define ubi_dbg_msg(type, fmt, ...) \
  43. pr_debug("UBI DBG " type ": " fmt "\n", ##__VA_ARGS__)
  44. /* Just a debugging messages not related to any specific UBI subsystem */
  45. #define dbg_msg(fmt, ...) ubi_dbg_msg("msg", fmt, ##__VA_ARGS__)
  46. /* General debugging messages */
  47. #define dbg_gen(fmt, ...) ubi_dbg_msg("gen", fmt, ##__VA_ARGS__)
  48. /* Messages from the eraseblock association sub-system */
  49. #define dbg_eba(fmt, ...) ubi_dbg_msg("eba", fmt, ##__VA_ARGS__)
  50. /* Messages from the wear-leveling sub-system */
  51. #define dbg_wl(fmt, ...) ubi_dbg_msg("wl", fmt, ##__VA_ARGS__)
  52. /* Messages from the input/output sub-system */
  53. #define dbg_io(fmt, ...) ubi_dbg_msg("io", fmt, ##__VA_ARGS__)
  54. /* Initialization and build messages */
  55. #define dbg_bld(fmt, ...) ubi_dbg_msg("bld", fmt, ##__VA_ARGS__)
  56. void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr);
  57. void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
  58. void ubi_dbg_dump_vol_info(const struct ubi_volume *vol);
  59. void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx);
  60. void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv);
  61. void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type);
  62. void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req);
  63. void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
  64. int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len);
  65. int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
  66. int offset, int len);
  67. int ubi_debugging_init_dev(struct ubi_device *ubi);
  68. void ubi_debugging_exit_dev(struct ubi_device *ubi);
  69. int ubi_debugfs_init(void);
  70. void ubi_debugfs_exit(void);
  71. int ubi_debugfs_init_dev(struct ubi_device *ubi);
  72. void ubi_debugfs_exit_dev(struct ubi_device *ubi);
  73. /*
  74. * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
  75. * + 2 for the number plus 1 for the trailing zero byte.
  76. */
  77. #define UBI_DFS_DIR_NAME "ubi%d"
  78. #define UBI_DFS_DIR_LEN (3 + 2 + 1)
  79. /**
  80. * struct ubi_debug_info - debugging information for an UBI device.
  81. *
  82. * @chk_gen: if UBI general extra checks are enabled
  83. * @chk_io: if UBI I/O extra checks are enabled
  84. * @dfs_dir_name: name of debugfs directory containing files of this UBI device
  85. * @dfs_dir: direntry object of the UBI device debugfs directory
  86. * @dfs_chk_gen: debugfs knob to enable UBI general extra checks
  87. * @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
  88. */
  89. struct ubi_debug_info {
  90. unsigned int chk_gen:1;
  91. unsigned int chk_io:1;
  92. char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
  93. struct dentry *dfs_dir;
  94. struct dentry *dfs_chk_gen;
  95. struct dentry *dfs_chk_io;
  96. };
  97. extern unsigned int ubi_tst_flags;
  98. /*
  99. * Special testing flags.
  100. *
  101. * UBIFS_TST_DISABLE_BGT: disable the background thread
  102. * UBI_TST_EMULATE_BITFLIPS: emulate bit-flips
  103. * UBI_TST_EMULATE_WRITE_FAILURES: emulate write failures
  104. * UBI_TST_EMULATE_ERASE_FAILURES: emulate erase failures
  105. */
  106. enum {
  107. UBI_TST_DISABLE_BGT = 0x1,
  108. UBI_TST_EMULATE_BITFLIPS = 0x2,
  109. UBI_TST_EMULATE_WRITE_FAILURES = 0x4,
  110. UBI_TST_EMULATE_ERASE_FAILURES = 0x8,
  111. };
  112. /**
  113. * ubi_dbg_is_bgt_disabled - if the background thread is disabled.
  114. *
  115. * Returns non-zero if the UBI background thread is disabled for testing
  116. * purposes.
  117. */
  118. static inline int ubi_dbg_is_bgt_disabled(void)
  119. {
  120. return ubi_tst_flags & UBI_TST_DISABLE_BGT;
  121. }
  122. /**
  123. * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip.
  124. *
  125. * Returns non-zero if a bit-flip should be emulated, otherwise returns zero.
  126. */
  127. static inline int ubi_dbg_is_bitflip(void)
  128. {
  129. if (ubi_tst_flags & UBI_TST_EMULATE_BITFLIPS)
  130. return !(random32() % 200);
  131. return 0;
  132. }
  133. /**
  134. * ubi_dbg_is_write_failure - if it is time to emulate a write failure.
  135. *
  136. * Returns non-zero if a write failure should be emulated, otherwise returns
  137. * zero.
  138. */
  139. static inline int ubi_dbg_is_write_failure(void)
  140. {
  141. if (ubi_tst_flags & UBI_TST_EMULATE_WRITE_FAILURES)
  142. return !(random32() % 500);
  143. return 0;
  144. }
  145. /**
  146. * ubi_dbg_is_erase_failure - if its time to emulate an erase failure.
  147. *
  148. * Returns non-zero if an erase failure should be emulated, otherwise returns
  149. * zero.
  150. */
  151. static inline int ubi_dbg_is_erase_failure(void)
  152. {
  153. if (ubi_tst_flags & UBI_TST_EMULATE_ERASE_FAILURES)
  154. return !(random32() % 400);
  155. return 0;
  156. }
  157. #else
  158. /* Use "if (0)" to make compiler check arguments even if debugging is off */
  159. #define ubi_assert(expr) do { \
  160. if (0) { \
  161. printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
  162. __func__, __LINE__, current->pid); \
  163. } \
  164. } while (0)
  165. #define dbg_err(fmt, ...) do { \
  166. if (0) \
  167. ubi_err(fmt, ##__VA_ARGS__); \
  168. } while (0)
  169. #define ubi_dbg_msg(fmt, ...) do { \
  170. if (0) \
  171. pr_debug(fmt "\n", ##__VA_ARGS__); \
  172. } while (0)
  173. #define dbg_msg(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  174. #define dbg_gen(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  175. #define dbg_eba(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  176. #define dbg_wl(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  177. #define dbg_io(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  178. #define dbg_bld(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  179. static inline void ubi_dbg_dump_stack(void) { return; }
  180. static inline void
  181. ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) { return; }
  182. static inline void
  183. ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) { return; }
  184. static inline void
  185. ubi_dbg_dump_vol_info(const struct ubi_volume *vol) { return; }
  186. static inline void
  187. ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) { return; }
  188. static inline void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv) { return; }
  189. static inline void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb,
  190. int type) { return; }
  191. static inline void
  192. ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req) { return; }
  193. static inline void ubi_dbg_dump_flash(struct ubi_device *ubi,
  194. int pnum, int offset, int len) { return; }
  195. static inline void
  196. ubi_dbg_print_hex_dump(const char *l, const char *ps, int pt, int r,
  197. int g, const void *b, size_t len, bool a) { return; }
  198. static inline int ubi_dbg_check_all_ff(struct ubi_device *ubi,
  199. int pnum, int offset,
  200. int len) { return 0; }
  201. static inline int ubi_dbg_check_write(struct ubi_device *ubi,
  202. const void *buf, int pnum,
  203. int offset, int len) { return 0; }
  204. static inline int ubi_debugging_init_dev(struct ubi_device *ubi) { return 0; }
  205. static inline void ubi_debugging_exit_dev(struct ubi_device *ubi) { return; }
  206. static inline int ubi_debugfs_init(void) { return 0; }
  207. static inline void ubi_debugfs_exit(void) { return; }
  208. static inline int ubi_debugfs_init_dev(struct ubi_device *ubi) { return 0; }
  209. static inline void ubi_debugfs_exit_dev(struct ubi_device *ubi) { return; }
  210. static inline int ubi_dbg_is_bgt_disabled(void) { return 0; }
  211. static inline int ubi_dbg_is_bitflip(void) { return 0; }
  212. static inline int ubi_dbg_is_write_failure(void) { return 0; }
  213. static inline int ubi_dbg_is_erase_failure(void) { return 0; }
  214. #endif /* !CONFIG_MTD_UBI_DEBUG */
  215. #endif /* !__UBI_DEBUG_H__ */