debug.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. */
  22. #ifndef __UBIFS_DEBUG_H__
  23. #define __UBIFS_DEBUG_H__
  24. #ifdef CONFIG_UBIFS_FS_DEBUG
  25. #define UBIFS_DBG(op) op
  26. #define ubifs_assert(expr) do { \
  27. if (unlikely(!(expr))) { \
  28. printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
  29. __func__, __LINE__, current->pid); \
  30. dbg_dump_stack(); \
  31. } \
  32. } while (0)
  33. #define ubifs_assert_cmt_locked(c) do { \
  34. if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
  35. up_write(&(c)->commit_sem); \
  36. printk(KERN_CRIT "commit lock is not locked!\n"); \
  37. ubifs_assert(0); \
  38. } \
  39. } while (0)
  40. #define dbg_dump_stack() do { \
  41. if (!dbg_failure_mode) \
  42. dump_stack(); \
  43. } while (0)
  44. /* Generic debugging messages */
  45. #define dbg_msg(fmt, ...) do { \
  46. spin_lock(&dbg_lock); \
  47. printk(KERN_DEBUG "UBIFS DBG (pid %d): %s: " fmt "\n", current->pid, \
  48. __func__, ##__VA_ARGS__); \
  49. spin_unlock(&dbg_lock); \
  50. } while (0)
  51. #define dbg_do_msg(typ, fmt, ...) do { \
  52. if (ubifs_msg_flags & typ) \
  53. dbg_msg(fmt, ##__VA_ARGS__); \
  54. } while (0)
  55. #define dbg_err(fmt, ...) do { \
  56. spin_lock(&dbg_lock); \
  57. ubifs_err(fmt, ##__VA_ARGS__); \
  58. spin_unlock(&dbg_lock); \
  59. } while (0)
  60. const char *dbg_key_str0(const struct ubifs_info *c,
  61. const union ubifs_key *key);
  62. const char *dbg_key_str1(const struct ubifs_info *c,
  63. const union ubifs_key *key);
  64. /*
  65. * DBGKEY macros require @dbg_lock to be held, which it is in the dbg message
  66. * macros.
  67. */
  68. #define DBGKEY(key) dbg_key_str0(c, (key))
  69. #define DBGKEY1(key) dbg_key_str1(c, (key))
  70. /* General messages */
  71. #define dbg_gen(fmt, ...) dbg_do_msg(UBIFS_MSG_GEN, fmt, ##__VA_ARGS__)
  72. /* Additional journal messages */
  73. #define dbg_jnl(fmt, ...) dbg_do_msg(UBIFS_MSG_JNL, fmt, ##__VA_ARGS__)
  74. /* Additional TNC messages */
  75. #define dbg_tnc(fmt, ...) dbg_do_msg(UBIFS_MSG_TNC, fmt, ##__VA_ARGS__)
  76. /* Additional lprops messages */
  77. #define dbg_lp(fmt, ...) dbg_do_msg(UBIFS_MSG_LP, fmt, ##__VA_ARGS__)
  78. /* Additional LEB find messages */
  79. #define dbg_find(fmt, ...) dbg_do_msg(UBIFS_MSG_FIND, fmt, ##__VA_ARGS__)
  80. /* Additional mount messages */
  81. #define dbg_mnt(fmt, ...) dbg_do_msg(UBIFS_MSG_MNT, fmt, ##__VA_ARGS__)
  82. /* Additional I/O messages */
  83. #define dbg_io(fmt, ...) dbg_do_msg(UBIFS_MSG_IO, fmt, ##__VA_ARGS__)
  84. /* Additional commit messages */
  85. #define dbg_cmt(fmt, ...) dbg_do_msg(UBIFS_MSG_CMT, fmt, ##__VA_ARGS__)
  86. /* Additional budgeting messages */
  87. #define dbg_budg(fmt, ...) dbg_do_msg(UBIFS_MSG_BUDG, fmt, ##__VA_ARGS__)
  88. /* Additional log messages */
  89. #define dbg_log(fmt, ...) dbg_do_msg(UBIFS_MSG_LOG, fmt, ##__VA_ARGS__)
  90. /* Additional gc messages */
  91. #define dbg_gc(fmt, ...) dbg_do_msg(UBIFS_MSG_GC, fmt, ##__VA_ARGS__)
  92. /* Additional scan messages */
  93. #define dbg_scan(fmt, ...) dbg_do_msg(UBIFS_MSG_SCAN, fmt, ##__VA_ARGS__)
  94. /* Additional recovery messages */
  95. #define dbg_rcvry(fmt, ...) dbg_do_msg(UBIFS_MSG_RCVRY, fmt, ##__VA_ARGS__)
  96. /*
  97. * Debugging message type flags (must match msg_type_names in debug.c).
  98. *
  99. * UBIFS_MSG_GEN: general messages
  100. * UBIFS_MSG_JNL: journal messages
  101. * UBIFS_MSG_MNT: mount messages
  102. * UBIFS_MSG_CMT: commit messages
  103. * UBIFS_MSG_FIND: LEB find messages
  104. * UBIFS_MSG_BUDG: budgeting messages
  105. * UBIFS_MSG_GC: garbage collection messages
  106. * UBIFS_MSG_TNC: TNC messages
  107. * UBIFS_MSG_LP: lprops messages
  108. * UBIFS_MSG_IO: I/O messages
  109. * UBIFS_MSG_LOG: log messages
  110. * UBIFS_MSG_SCAN: scan messages
  111. * UBIFS_MSG_RCVRY: recovery messages
  112. */
  113. enum {
  114. UBIFS_MSG_GEN = 0x1,
  115. UBIFS_MSG_JNL = 0x2,
  116. UBIFS_MSG_MNT = 0x4,
  117. UBIFS_MSG_CMT = 0x8,
  118. UBIFS_MSG_FIND = 0x10,
  119. UBIFS_MSG_BUDG = 0x20,
  120. UBIFS_MSG_GC = 0x40,
  121. UBIFS_MSG_TNC = 0x80,
  122. UBIFS_MSG_LP = 0x100,
  123. UBIFS_MSG_IO = 0x200,
  124. UBIFS_MSG_LOG = 0x400,
  125. UBIFS_MSG_SCAN = 0x800,
  126. UBIFS_MSG_RCVRY = 0x1000,
  127. };
  128. /* Debugging message type flags for each default debug message level */
  129. #define UBIFS_MSG_LVL_0 0
  130. #define UBIFS_MSG_LVL_1 0x1
  131. #define UBIFS_MSG_LVL_2 0x7f
  132. #define UBIFS_MSG_LVL_3 0xffff
  133. /*
  134. * Debugging check flags (must match chk_names in debug.c).
  135. *
  136. * UBIFS_CHK_GEN: general checks
  137. * UBIFS_CHK_TNC: check TNC
  138. * UBIFS_CHK_IDX_SZ: check index size
  139. * UBIFS_CHK_ORPH: check orphans
  140. * UBIFS_CHK_OLD_IDX: check the old index
  141. * UBIFS_CHK_LPROPS: check lprops
  142. * UBIFS_CHK_FS: check the file-system
  143. */
  144. enum {
  145. UBIFS_CHK_GEN = 0x1,
  146. UBIFS_CHK_TNC = 0x2,
  147. UBIFS_CHK_IDX_SZ = 0x4,
  148. UBIFS_CHK_ORPH = 0x8,
  149. UBIFS_CHK_OLD_IDX = 0x10,
  150. UBIFS_CHK_LPROPS = 0x20,
  151. UBIFS_CHK_FS = 0x40,
  152. };
  153. /*
  154. * Special testing flags (must match tst_names in debug.c).
  155. *
  156. * UBIFS_TST_FORCE_IN_THE_GAPS: force the use of in-the-gaps method
  157. * UBIFS_TST_RCVRY: failure mode for recovery testing
  158. */
  159. enum {
  160. UBIFS_TST_FORCE_IN_THE_GAPS = 0x2,
  161. UBIFS_TST_RCVRY = 0x4,
  162. };
  163. #if CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 1
  164. #define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_1
  165. #elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 2
  166. #define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_2
  167. #elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 3
  168. #define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_3
  169. #else
  170. #define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_0
  171. #endif
  172. #ifdef CONFIG_UBIFS_FS_DEBUG_CHKS
  173. #define UBIFS_CHK_FLAGS_DEFAULT 0xffffffff
  174. #else
  175. #define UBIFS_CHK_FLAGS_DEFAULT 0
  176. #endif
  177. extern spinlock_t dbg_lock;
  178. extern unsigned int ubifs_msg_flags;
  179. extern unsigned int ubifs_chk_flags;
  180. extern unsigned int ubifs_tst_flags;
  181. /* Dump functions */
  182. const char *dbg_ntype(int type);
  183. const char *dbg_cstate(int cmt_state);
  184. const char *dbg_get_key_dump(const struct ubifs_info *c,
  185. const union ubifs_key *key);
  186. void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode);
  187. void dbg_dump_node(const struct ubifs_info *c, const void *node);
  188. void dbg_dump_budget_req(const struct ubifs_budget_req *req);
  189. void dbg_dump_lstats(const struct ubifs_lp_stats *lst);
  190. void dbg_dump_budg(struct ubifs_info *c);
  191. void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp);
  192. void dbg_dump_lprops(struct ubifs_info *c);
  193. void dbg_dump_lpt_info(struct ubifs_info *c);
  194. void dbg_dump_leb(const struct ubifs_info *c, int lnum);
  195. void dbg_dump_znode(const struct ubifs_info *c,
  196. const struct ubifs_znode *znode);
  197. void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat);
  198. void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
  199. struct ubifs_nnode *parent, int iip);
  200. void dbg_dump_tnc(struct ubifs_info *c);
  201. void dbg_dump_index(struct ubifs_info *c);
  202. /* Checking helper functions */
  203. typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
  204. struct ubifs_zbranch *zbr, void *priv);
  205. typedef int (*dbg_znode_callback)(struct ubifs_info *c,
  206. struct ubifs_znode *znode, void *priv);
  207. int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
  208. dbg_znode_callback znode_cb, void *priv);
  209. /* Checking functions */
  210. int dbg_check_lprops(struct ubifs_info *c);
  211. int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  212. int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  213. int dbg_check_cats(struct ubifs_info *c);
  214. int dbg_check_ltab(struct ubifs_info *c);
  215. int dbg_chk_lpt_free_spc(struct ubifs_info *c);
  216. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len);
  217. int dbg_check_synced_i_size(struct inode *inode);
  218. int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir);
  219. int dbg_check_tnc(struct ubifs_info *c, int extra);
  220. int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
  221. int dbg_check_filesystem(struct ubifs_info *c);
  222. void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
  223. int add_pos);
  224. int dbg_check_lprops(struct ubifs_info *c);
  225. int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
  226. int row, int col);
  227. /* Force the use of in-the-gaps method for testing */
  228. #define dbg_force_in_the_gaps_enabled \
  229. (ubifs_tst_flags & UBIFS_TST_FORCE_IN_THE_GAPS)
  230. int dbg_force_in_the_gaps(void);
  231. /* Failure mode for recovery testing */
  232. #define dbg_failure_mode (ubifs_tst_flags & UBIFS_TST_RCVRY)
  233. void dbg_failure_mode_registration(struct ubifs_info *c);
  234. void dbg_failure_mode_deregistration(struct ubifs_info *c);
  235. #ifndef UBIFS_DBG_PRESERVE_UBI
  236. #define ubi_leb_read dbg_leb_read
  237. #define ubi_leb_write dbg_leb_write
  238. #define ubi_leb_change dbg_leb_change
  239. #define ubi_leb_erase dbg_leb_erase
  240. #define ubi_leb_unmap dbg_leb_unmap
  241. #define ubi_is_mapped dbg_is_mapped
  242. #define ubi_leb_map dbg_leb_map
  243. #endif
  244. int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  245. int len, int check);
  246. int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  247. int offset, int len, int dtype);
  248. int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  249. int len, int dtype);
  250. int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum);
  251. int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum);
  252. int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum);
  253. int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype);
  254. static inline int dbg_read(struct ubi_volume_desc *desc, int lnum, char *buf,
  255. int offset, int len)
  256. {
  257. return dbg_leb_read(desc, lnum, buf, offset, len, 0);
  258. }
  259. static inline int dbg_write(struct ubi_volume_desc *desc, int lnum,
  260. const void *buf, int offset, int len)
  261. {
  262. return dbg_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN);
  263. }
  264. static inline int dbg_change(struct ubi_volume_desc *desc, int lnum,
  265. const void *buf, int len)
  266. {
  267. return dbg_leb_change(desc, lnum, buf, len, UBI_UNKNOWN);
  268. }
  269. #else /* !CONFIG_UBIFS_FS_DEBUG */
  270. #define UBIFS_DBG(op)
  271. /* Use "if (0)" to make compiler check arguments even if debugging is off */
  272. #define ubifs_assert(expr) do { \
  273. if (0 && (expr)) \
  274. printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
  275. __func__, __LINE__, current->pid); \
  276. } while (0)
  277. #define dbg_err(fmt, ...) do { \
  278. if (0) \
  279. ubifs_err(fmt, ##__VA_ARGS__); \
  280. } while (0)
  281. #define dbg_msg(fmt, ...) do { \
  282. if (0) \
  283. printk(KERN_DEBUG "UBIFS DBG (pid %d): %s: " fmt "\n", \
  284. current->pid, __func__, ##__VA_ARGS__); \
  285. } while (0)
  286. #define dbg_dump_stack()
  287. #define ubifs_assert_cmt_locked(c)
  288. #define dbg_gen(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  289. #define dbg_jnl(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  290. #define dbg_tnc(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  291. #define dbg_lp(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  292. #define dbg_find(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  293. #define dbg_mnt(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  294. #define dbg_io(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  295. #define dbg_cmt(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  296. #define dbg_budg(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  297. #define dbg_log(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  298. #define dbg_gc(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  299. #define dbg_scan(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  300. #define dbg_rcvry(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  301. #define DBGKEY(key) ((char *)(key))
  302. #define DBGKEY1(key) ((char *)(key))
  303. #define dbg_ntype(type) ""
  304. #define dbg_cstate(cmt_state) ""
  305. #define dbg_get_key_dump(c, key) ({})
  306. #define dbg_dump_inode(c, inode) ({})
  307. #define dbg_dump_node(c, node) ({})
  308. #define dbg_dump_budget_req(req) ({})
  309. #define dbg_dump_lstats(lst) ({})
  310. #define dbg_dump_budg(c) ({})
  311. #define dbg_dump_lprop(c, lp) ({})
  312. #define dbg_dump_lprops(c) ({})
  313. #define dbg_dump_lpt_info(c) ({})
  314. #define dbg_dump_leb(c, lnum) ({})
  315. #define dbg_dump_znode(c, znode) ({})
  316. #define dbg_dump_heap(c, heap, cat) ({})
  317. #define dbg_dump_pnode(c, pnode, parent, iip) ({})
  318. #define dbg_dump_tnc(c) ({})
  319. #define dbg_dump_index(c) ({})
  320. #define dbg_walk_index(c, leaf_cb, znode_cb, priv) 0
  321. #define dbg_old_index_check_init(c, zroot) 0
  322. #define dbg_check_old_index(c, zroot) 0
  323. #define dbg_check_cats(c) 0
  324. #define dbg_check_ltab(c) 0
  325. #define dbg_chk_lpt_free_spc(c) 0
  326. #define dbg_chk_lpt_sz(c, action, len) 0
  327. #define dbg_check_synced_i_size(inode) 0
  328. #define dbg_check_dir_size(c, dir) 0
  329. #define dbg_check_tnc(c, x) 0
  330. #define dbg_check_idx_size(c, idx_size) 0
  331. #define dbg_check_filesystem(c) 0
  332. #define dbg_check_heap(c, heap, cat, add_pos) ({})
  333. #define dbg_check_lprops(c) 0
  334. #define dbg_check_lpt_nodes(c, cnode, row, col) 0
  335. #define dbg_force_in_the_gaps_enabled 0
  336. #define dbg_force_in_the_gaps() 0
  337. #define dbg_failure_mode 0
  338. #define dbg_failure_mode_registration(c) ({})
  339. #define dbg_failure_mode_deregistration(c) ({})
  340. #endif /* !CONFIG_UBIFS_FS_DEBUG */
  341. #endif /* !__UBIFS_DEBUG_H__ */