debug.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #ifdef CONFIG_MTD_UBI_DEBUG
  23. #include <linux/random.h>
  24. #define ubi_assert(expr) do { \
  25. if (unlikely(!(expr))) { \
  26. printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
  27. __func__, __LINE__, current->pid); \
  28. ubi_dbg_dump_stack(); \
  29. } \
  30. } while (0)
  31. #define dbg_err(fmt, ...) ubi_err(fmt, ##__VA_ARGS__)
  32. #define ubi_dbg_dump_stack() dump_stack()
  33. #define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) \
  34. print_hex_dump(l, ps, pt, r, g, b, len, a)
  35. #define ubi_dbg_msg(type, fmt, ...) \
  36. pr_debug("UBI DBG " type ": " fmt "\n", ##__VA_ARGS__)
  37. /* Just a debugging messages not related to any specific UBI subsystem */
  38. #define dbg_msg(fmt, ...) \
  39. printk(KERN_DEBUG "UBI DBG (pid %d): %s: " fmt "\n", \
  40. current->pid, __func__, ##__VA_ARGS__)
  41. /* General debugging messages */
  42. #define dbg_gen(fmt, ...) ubi_dbg_msg("gen", fmt, ##__VA_ARGS__)
  43. /* Messages from the eraseblock association sub-system */
  44. #define dbg_eba(fmt, ...) ubi_dbg_msg("eba", fmt, ##__VA_ARGS__)
  45. /* Messages from the wear-leveling sub-system */
  46. #define dbg_wl(fmt, ...) ubi_dbg_msg("wl", fmt, ##__VA_ARGS__)
  47. /* Messages from the input/output sub-system */
  48. #define dbg_io(fmt, ...) ubi_dbg_msg("io", fmt, ##__VA_ARGS__)
  49. /* Initialization and build messages */
  50. #define dbg_bld(fmt, ...) ubi_dbg_msg("bld", fmt, ##__VA_ARGS__)
  51. void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr);
  52. void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
  53. void ubi_dbg_dump_vol_info(const struct ubi_volume *vol);
  54. void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx);
  55. void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv);
  56. void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type);
  57. void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req);
  58. void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
  59. int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len);
  60. int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
  61. int offset, int len);
  62. int ubi_debugging_init_dev(struct ubi_device *ubi);
  63. void ubi_debugging_exit_dev(struct ubi_device *ubi);
  64. int ubi_debugfs_init(void);
  65. void ubi_debugfs_exit(void);
  66. int ubi_debugfs_init_dev(struct ubi_device *ubi);
  67. void ubi_debugfs_exit_dev(struct ubi_device *ubi);
  68. /*
  69. * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
  70. * + 2 for the number plus 1 for the trailing zero byte.
  71. */
  72. #define UBI_DFS_DIR_NAME "ubi%d"
  73. #define UBI_DFS_DIR_LEN (3 + 2 + 1)
  74. /**
  75. * struct ubi_debug_info - debugging information for an UBI device.
  76. *
  77. * @chk_gen: if UBI general extra checks are enabled
  78. * @chk_io: if UBI I/O extra checks are enabled
  79. * @disable_bgt: disable the background task for testing purposes
  80. * @emulate_bitflips: emulate bit-flips for testing purposes
  81. * @emulate_io_failures: emulate write/erase failures for testing purposes
  82. * @dfs_dir_name: name of debugfs directory containing files of this UBI device
  83. * @dfs_dir: direntry object of the UBI device debugfs directory
  84. * @dfs_chk_gen: debugfs knob to enable UBI general extra checks
  85. * @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
  86. * @dfs_disable_bgt: debugfs knob to disable the background task
  87. * @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
  88. * @dfs_emulate_io_failures: debugfs knob to emulate write/erase failures
  89. */
  90. struct ubi_debug_info {
  91. unsigned int chk_gen:1;
  92. unsigned int chk_io:1;
  93. unsigned int disable_bgt:1;
  94. unsigned int emulate_bitflips:1;
  95. unsigned int emulate_io_failures:1;
  96. char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
  97. struct dentry *dfs_dir;
  98. struct dentry *dfs_chk_gen;
  99. struct dentry *dfs_chk_io;
  100. struct dentry *dfs_disable_bgt;
  101. struct dentry *dfs_emulate_bitflips;
  102. struct dentry *dfs_emulate_io_failures;
  103. };
  104. /**
  105. * ubi_dbg_is_bgt_disabled - if the background thread is disabled.
  106. * @ubi: UBI device description object
  107. *
  108. * Returns non-zero if the UBI background thread is disabled for testing
  109. * purposes.
  110. */
  111. static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi)
  112. {
  113. return ubi->dbg->disable_bgt;
  114. }
  115. /**
  116. * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip.
  117. * @ubi: UBI device description object
  118. *
  119. * Returns non-zero if a bit-flip should be emulated, otherwise returns zero.
  120. */
  121. static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi)
  122. {
  123. if (ubi->dbg->emulate_bitflips)
  124. return !(random32() % 200);
  125. return 0;
  126. }
  127. /**
  128. * ubi_dbg_is_write_failure - if it is time to emulate a write failure.
  129. * @ubi: UBI device description object
  130. *
  131. * Returns non-zero if a write failure should be emulated, otherwise returns
  132. * zero.
  133. */
  134. static inline int ubi_dbg_is_write_failure(const struct ubi_device *ubi)
  135. {
  136. if (ubi->dbg->emulate_io_failures)
  137. return !(random32() % 500);
  138. return 0;
  139. }
  140. /**
  141. * ubi_dbg_is_erase_failure - if its time to emulate an erase failure.
  142. * @ubi: UBI device description object
  143. *
  144. * Returns non-zero if an erase failure should be emulated, otherwise returns
  145. * zero.
  146. */
  147. static inline int ubi_dbg_is_erase_failure(const struct ubi_device *ubi)
  148. {
  149. if (ubi->dbg->emulate_io_failures)
  150. return !(random32() % 400);
  151. return 0;
  152. }
  153. #else
  154. /* Use "if (0)" to make compiler check arguments even if debugging is off */
  155. #define ubi_assert(expr) do { \
  156. if (0) { \
  157. printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
  158. __func__, __LINE__, current->pid); \
  159. } \
  160. } while (0)
  161. #define dbg_err(fmt, ...) do { \
  162. if (0) \
  163. ubi_err(fmt, ##__VA_ARGS__); \
  164. } while (0)
  165. #define ubi_dbg_msg(fmt, ...) do { \
  166. if (0) \
  167. printk(KERN_DEBUG fmt "\n", ##__VA_ARGS__); \
  168. } while (0)
  169. #define dbg_msg(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  170. #define dbg_gen(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  171. #define dbg_eba(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  172. #define dbg_wl(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  173. #define dbg_io(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  174. #define dbg_bld(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
  175. static inline void ubi_dbg_dump_stack(void) { return; }
  176. static inline void
  177. ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) { return; }
  178. static inline void
  179. ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) { return; }
  180. static inline void
  181. ubi_dbg_dump_vol_info(const struct ubi_volume *vol) { return; }
  182. static inline void
  183. ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) { return; }
  184. static inline void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv) { return; }
  185. static inline void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb,
  186. int type) { return; }
  187. static inline void
  188. ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req) { return; }
  189. static inline void ubi_dbg_dump_flash(struct ubi_device *ubi,
  190. int pnum, int offset, int len) { return; }
  191. static inline void
  192. ubi_dbg_print_hex_dump(const char *l, const char *ps, int pt, int r,
  193. int g, const void *b, size_t len, bool a) { return; }
  194. static inline int ubi_dbg_check_all_ff(struct ubi_device *ubi,
  195. int pnum, int offset,
  196. int len) { return 0; }
  197. static inline int ubi_dbg_check_write(struct ubi_device *ubi,
  198. const void *buf, int pnum,
  199. int offset, int len) { return 0; }
  200. static inline int ubi_debugging_init_dev(struct ubi_device *ubi) { return 0; }
  201. static inline void ubi_debugging_exit_dev(struct ubi_device *ubi) { return; }
  202. static inline int ubi_debugfs_init(void) { return 0; }
  203. static inline void ubi_debugfs_exit(void) { return; }
  204. static inline int ubi_debugfs_init_dev(struct ubi_device *ubi) { return 0; }
  205. static inline void ubi_debugfs_exit_dev(struct ubi_device *ubi) { return; }
  206. static inline int
  207. ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) { return 0; }
  208. static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) { return 0; }
  209. static inline int
  210. ubi_dbg_is_write_failure(const struct ubi_device *ubi) { return 0; }
  211. static inline int
  212. ubi_dbg_is_erase_failure(const struct ubi_device *ubi) { return 0; }
  213. #endif /* !CONFIG_MTD_UBI_DEBUG */
  214. #endif /* !__UBI_DEBUG_H__ */