debug.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. /**
  26. * ubifs_debug_info - per-FS debugging information.
  27. * @buf: a buffer of LEB size, used for various purposes
  28. * @old_zroot: old index root - used by 'dbg_check_old_index()'
  29. * @old_zroot_level: old index root level - used by 'dbg_check_old_index()'
  30. * @old_zroot_sqnum: old index root sqnum - used by 'dbg_check_old_index()'
  31. * @failure_mode: failure mode for recovery testing
  32. * @fail_delay: 0=>don't delay, 1=>delay a time, 2=>delay a number of calls
  33. * @fail_timeout: time in jiffies when delay of failure mode expires
  34. * @fail_cnt: current number of calls to failure mode I/O functions
  35. * @fail_cnt_max: number of calls by which to delay failure mode
  36. * @chk_lpt_sz: used by LPT tree size checker
  37. * @chk_lpt_sz2: used by LPT tree size checker
  38. * @chk_lpt_wastage: used by LPT tree size checker
  39. * @chk_lpt_lebs: used by LPT tree size checker
  40. * @new_nhead_offs: used by LPT tree size checker
  41. * @new_ihead_lnum: used by debugging to check ihead_lnum
  42. * @new_ihead_offs: used by debugging to check ihead_offs
  43. *
  44. * debugfs_dir_name: name of debugfs directory containing this file-system's
  45. * files
  46. * debugfs_dir: direntry object of the file-system debugfs directory
  47. * dump_lprops: "dump lprops" debugfs knob
  48. * dump_budg: "dump budgeting information" debugfs knob
  49. * dump_tnc: "dump TNC" debugfs knob
  50. */
  51. struct ubifs_debug_info {
  52. void *buf;
  53. struct ubifs_zbranch old_zroot;
  54. int old_zroot_level;
  55. unsigned long long old_zroot_sqnum;
  56. int failure_mode;
  57. int fail_delay;
  58. unsigned long fail_timeout;
  59. unsigned int fail_cnt;
  60. unsigned int fail_cnt_max;
  61. long long chk_lpt_sz;
  62. long long chk_lpt_sz2;
  63. long long chk_lpt_wastage;
  64. int chk_lpt_lebs;
  65. int new_nhead_offs;
  66. int new_ihead_lnum;
  67. int new_ihead_offs;
  68. char debugfs_dir_name[100];
  69. struct dentry *debugfs_dir;
  70. struct dentry *dump_lprops;
  71. struct dentry *dump_budg;
  72. struct dentry *dump_tnc;
  73. };
  74. #define ubifs_assert(expr) do { \
  75. if (unlikely(!(expr))) { \
  76. printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
  77. __func__, __LINE__, current->pid); \
  78. dbg_dump_stack(); \
  79. } \
  80. } while (0)
  81. #define ubifs_assert_cmt_locked(c) do { \
  82. if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
  83. up_write(&(c)->commit_sem); \
  84. printk(KERN_CRIT "commit lock is not locked!\n"); \
  85. ubifs_assert(0); \
  86. } \
  87. } while (0)
  88. #define dbg_dump_stack() do { \
  89. if (!dbg_failure_mode) \
  90. dump_stack(); \
  91. } while (0)
  92. /* Generic debugging messages */
  93. #define dbg_msg(fmt, ...) do { \
  94. spin_lock(&dbg_lock); \
  95. printk(KERN_DEBUG "UBIFS DBG (pid %d): %s: " fmt "\n", current->pid, \
  96. __func__, ##__VA_ARGS__); \
  97. spin_unlock(&dbg_lock); \
  98. } while (0)
  99. #define dbg_do_msg(typ, fmt, ...) do { \
  100. if (ubifs_msg_flags & typ) \
  101. dbg_msg(fmt, ##__VA_ARGS__); \
  102. } while (0)
  103. #define dbg_err(fmt, ...) do { \
  104. spin_lock(&dbg_lock); \
  105. ubifs_err(fmt, ##__VA_ARGS__); \
  106. spin_unlock(&dbg_lock); \
  107. } while (0)
  108. const char *dbg_key_str0(const struct ubifs_info *c,
  109. const union ubifs_key *key);
  110. const char *dbg_key_str1(const struct ubifs_info *c,
  111. const union ubifs_key *key);
  112. /*
  113. * DBGKEY macros require @dbg_lock to be held, which it is in the dbg message
  114. * macros.
  115. */
  116. #define DBGKEY(key) dbg_key_str0(c, (key))
  117. #define DBGKEY1(key) dbg_key_str1(c, (key))
  118. /* General messages */
  119. #define dbg_gen(fmt, ...) dbg_do_msg(UBIFS_MSG_GEN, fmt, ##__VA_ARGS__)
  120. /* Additional journal messages */
  121. #define dbg_jnl(fmt, ...) dbg_do_msg(UBIFS_MSG_JNL, fmt, ##__VA_ARGS__)
  122. /* Additional TNC messages */
  123. #define dbg_tnc(fmt, ...) dbg_do_msg(UBIFS_MSG_TNC, fmt, ##__VA_ARGS__)
  124. /* Additional lprops messages */
  125. #define dbg_lp(fmt, ...) dbg_do_msg(UBIFS_MSG_LP, fmt, ##__VA_ARGS__)
  126. /* Additional LEB find messages */
  127. #define dbg_find(fmt, ...) dbg_do_msg(UBIFS_MSG_FIND, fmt, ##__VA_ARGS__)
  128. /* Additional mount messages */
  129. #define dbg_mnt(fmt, ...) dbg_do_msg(UBIFS_MSG_MNT, fmt, ##__VA_ARGS__)
  130. /* Additional I/O messages */
  131. #define dbg_io(fmt, ...) dbg_do_msg(UBIFS_MSG_IO, fmt, ##__VA_ARGS__)
  132. /* Additional commit messages */
  133. #define dbg_cmt(fmt, ...) dbg_do_msg(UBIFS_MSG_CMT, fmt, ##__VA_ARGS__)
  134. /* Additional budgeting messages */
  135. #define dbg_budg(fmt, ...) dbg_do_msg(UBIFS_MSG_BUDG, fmt, ##__VA_ARGS__)
  136. /* Additional log messages */
  137. #define dbg_log(fmt, ...) dbg_do_msg(UBIFS_MSG_LOG, fmt, ##__VA_ARGS__)
  138. /* Additional gc messages */
  139. #define dbg_gc(fmt, ...) dbg_do_msg(UBIFS_MSG_GC, fmt, ##__VA_ARGS__)
  140. /* Additional scan messages */
  141. #define dbg_scan(fmt, ...) dbg_do_msg(UBIFS_MSG_SCAN, fmt, ##__VA_ARGS__)
  142. /* Additional recovery messages */
  143. #define dbg_rcvry(fmt, ...) dbg_do_msg(UBIFS_MSG_RCVRY, fmt, ##__VA_ARGS__)
  144. /*
  145. * Debugging message type flags (must match msg_type_names in debug.c).
  146. *
  147. * UBIFS_MSG_GEN: general messages
  148. * UBIFS_MSG_JNL: journal messages
  149. * UBIFS_MSG_MNT: mount messages
  150. * UBIFS_MSG_CMT: commit messages
  151. * UBIFS_MSG_FIND: LEB find messages
  152. * UBIFS_MSG_BUDG: budgeting messages
  153. * UBIFS_MSG_GC: garbage collection messages
  154. * UBIFS_MSG_TNC: TNC messages
  155. * UBIFS_MSG_LP: lprops messages
  156. * UBIFS_MSG_IO: I/O messages
  157. * UBIFS_MSG_LOG: log messages
  158. * UBIFS_MSG_SCAN: scan messages
  159. * UBIFS_MSG_RCVRY: recovery messages
  160. */
  161. enum {
  162. UBIFS_MSG_GEN = 0x1,
  163. UBIFS_MSG_JNL = 0x2,
  164. UBIFS_MSG_MNT = 0x4,
  165. UBIFS_MSG_CMT = 0x8,
  166. UBIFS_MSG_FIND = 0x10,
  167. UBIFS_MSG_BUDG = 0x20,
  168. UBIFS_MSG_GC = 0x40,
  169. UBIFS_MSG_TNC = 0x80,
  170. UBIFS_MSG_LP = 0x100,
  171. UBIFS_MSG_IO = 0x200,
  172. UBIFS_MSG_LOG = 0x400,
  173. UBIFS_MSG_SCAN = 0x800,
  174. UBIFS_MSG_RCVRY = 0x1000,
  175. };
  176. /* Debugging message type flags for each default debug message level */
  177. #define UBIFS_MSG_LVL_0 0
  178. #define UBIFS_MSG_LVL_1 0x1
  179. #define UBIFS_MSG_LVL_2 0x7f
  180. #define UBIFS_MSG_LVL_3 0xffff
  181. /*
  182. * Debugging check flags (must match chk_names in debug.c).
  183. *
  184. * UBIFS_CHK_GEN: general checks
  185. * UBIFS_CHK_TNC: check TNC
  186. * UBIFS_CHK_IDX_SZ: check index size
  187. * UBIFS_CHK_ORPH: check orphans
  188. * UBIFS_CHK_OLD_IDX: check the old index
  189. * UBIFS_CHK_LPROPS: check lprops
  190. * UBIFS_CHK_FS: check the file-system
  191. */
  192. enum {
  193. UBIFS_CHK_GEN = 0x1,
  194. UBIFS_CHK_TNC = 0x2,
  195. UBIFS_CHK_IDX_SZ = 0x4,
  196. UBIFS_CHK_ORPH = 0x8,
  197. UBIFS_CHK_OLD_IDX = 0x10,
  198. UBIFS_CHK_LPROPS = 0x20,
  199. UBIFS_CHK_FS = 0x40,
  200. };
  201. /*
  202. * Special testing flags (must match tst_names in debug.c).
  203. *
  204. * UBIFS_TST_FORCE_IN_THE_GAPS: force the use of in-the-gaps method
  205. * UBIFS_TST_RCVRY: failure mode for recovery testing
  206. */
  207. enum {
  208. UBIFS_TST_FORCE_IN_THE_GAPS = 0x2,
  209. UBIFS_TST_RCVRY = 0x4,
  210. };
  211. #if CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 1
  212. #define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_1
  213. #elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 2
  214. #define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_2
  215. #elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 3
  216. #define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_3
  217. #else
  218. #define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_0
  219. #endif
  220. #ifdef CONFIG_UBIFS_FS_DEBUG_CHKS
  221. #define UBIFS_CHK_FLAGS_DEFAULT 0xffffffff
  222. #else
  223. #define UBIFS_CHK_FLAGS_DEFAULT 0
  224. #endif
  225. extern spinlock_t dbg_lock;
  226. extern unsigned int ubifs_msg_flags;
  227. extern unsigned int ubifs_chk_flags;
  228. extern unsigned int ubifs_tst_flags;
  229. int ubifs_debugging_init(struct ubifs_info *c);
  230. void ubifs_debugging_exit(struct ubifs_info *c);
  231. /* Dump functions */
  232. const char *dbg_ntype(int type);
  233. const char *dbg_cstate(int cmt_state);
  234. const char *dbg_get_key_dump(const struct ubifs_info *c,
  235. const union ubifs_key *key);
  236. void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode);
  237. void dbg_dump_node(const struct ubifs_info *c, const void *node);
  238. void dbg_dump_lpt_node(const struct ubifs_info *c, void *node, int lnum,
  239. int offs);
  240. void dbg_dump_budget_req(const struct ubifs_budget_req *req);
  241. void dbg_dump_lstats(const struct ubifs_lp_stats *lst);
  242. void dbg_dump_budg(struct ubifs_info *c);
  243. void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp);
  244. void dbg_dump_lprops(struct ubifs_info *c);
  245. void dbg_dump_lpt_info(struct ubifs_info *c);
  246. void dbg_dump_leb(const struct ubifs_info *c, int lnum);
  247. void dbg_dump_znode(const struct ubifs_info *c,
  248. const struct ubifs_znode *znode);
  249. void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat);
  250. void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
  251. struct ubifs_nnode *parent, int iip);
  252. void dbg_dump_tnc(struct ubifs_info *c);
  253. void dbg_dump_index(struct ubifs_info *c);
  254. void dbg_dump_lpt_lebs(const struct ubifs_info *c);
  255. /* Checking helper functions */
  256. typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
  257. struct ubifs_zbranch *zbr, void *priv);
  258. typedef int (*dbg_znode_callback)(struct ubifs_info *c,
  259. struct ubifs_znode *znode, void *priv);
  260. int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
  261. dbg_znode_callback znode_cb, void *priv);
  262. /* Checking functions */
  263. int dbg_check_lprops(struct ubifs_info *c);
  264. int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  265. int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  266. int dbg_check_cats(struct ubifs_info *c);
  267. int dbg_check_ltab(struct ubifs_info *c);
  268. int dbg_chk_lpt_free_spc(struct ubifs_info *c);
  269. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len);
  270. int dbg_check_synced_i_size(struct inode *inode);
  271. int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir);
  272. int dbg_check_tnc(struct ubifs_info *c, int extra);
  273. int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
  274. int dbg_check_filesystem(struct ubifs_info *c);
  275. void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
  276. int add_pos);
  277. int dbg_check_lprops(struct ubifs_info *c);
  278. int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
  279. int row, int col);
  280. /* Force the use of in-the-gaps method for testing */
  281. #define dbg_force_in_the_gaps_enabled \
  282. (ubifs_tst_flags & UBIFS_TST_FORCE_IN_THE_GAPS)
  283. int dbg_force_in_the_gaps(void);
  284. /* Failure mode for recovery testing */
  285. #define dbg_failure_mode (ubifs_tst_flags & UBIFS_TST_RCVRY)
  286. #ifndef UBIFS_DBG_PRESERVE_UBI
  287. #define ubi_leb_read dbg_leb_read
  288. #define ubi_leb_write dbg_leb_write
  289. #define ubi_leb_change dbg_leb_change
  290. #define ubi_leb_erase dbg_leb_erase
  291. #define ubi_leb_unmap dbg_leb_unmap
  292. #define ubi_is_mapped dbg_is_mapped
  293. #define ubi_leb_map dbg_leb_map
  294. #endif
  295. int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  296. int len, int check);
  297. int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  298. int offset, int len, int dtype);
  299. int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  300. int len, int dtype);
  301. int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum);
  302. int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum);
  303. int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum);
  304. int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype);
  305. static inline int dbg_read(struct ubi_volume_desc *desc, int lnum, char *buf,
  306. int offset, int len)
  307. {
  308. return dbg_leb_read(desc, lnum, buf, offset, len, 0);
  309. }
  310. static inline int dbg_write(struct ubi_volume_desc *desc, int lnum,
  311. const void *buf, int offset, int len)
  312. {
  313. return dbg_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN);
  314. }
  315. static inline int dbg_change(struct ubi_volume_desc *desc, int lnum,
  316. const void *buf, int len)
  317. {
  318. return dbg_leb_change(desc, lnum, buf, len, UBI_UNKNOWN);
  319. }
  320. /* Debugfs-related stuff */
  321. int dbg_debugfs_init(void);
  322. void dbg_debugfs_exit(void);
  323. int dbg_debugfs_init_fs(struct ubifs_info *c);
  324. void dbg_debugfs_exit_fs(struct ubifs_info *c);
  325. #else /* !CONFIG_UBIFS_FS_DEBUG */
  326. /* Use "if (0)" to make compiler check arguments even if debugging is off */
  327. #define ubifs_assert(expr) do { \
  328. if (0 && (expr)) \
  329. printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
  330. __func__, __LINE__, current->pid); \
  331. } while (0)
  332. #define dbg_err(fmt, ...) do { \
  333. if (0) \
  334. ubifs_err(fmt, ##__VA_ARGS__); \
  335. } while (0)
  336. #define dbg_msg(fmt, ...) do { \
  337. if (0) \
  338. printk(KERN_DEBUG "UBIFS DBG (pid %d): %s: " fmt "\n", \
  339. current->pid, __func__, ##__VA_ARGS__); \
  340. } while (0)
  341. #define dbg_dump_stack()
  342. #define ubifs_assert_cmt_locked(c)
  343. #define dbg_gen(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  344. #define dbg_jnl(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  345. #define dbg_tnc(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  346. #define dbg_lp(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  347. #define dbg_find(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  348. #define dbg_mnt(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  349. #define dbg_io(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  350. #define dbg_cmt(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  351. #define dbg_budg(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  352. #define dbg_log(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  353. #define dbg_gc(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  354. #define dbg_scan(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  355. #define dbg_rcvry(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
  356. #define DBGKEY(key) ((char *)(key))
  357. #define DBGKEY1(key) ((char *)(key))
  358. #define ubifs_debugging_init(c) 0
  359. #define ubifs_debugging_exit(c) ({})
  360. #define dbg_ntype(type) ""
  361. #define dbg_cstate(cmt_state) ""
  362. #define dbg_get_key_dump(c, key) ({})
  363. #define dbg_dump_inode(c, inode) ({})
  364. #define dbg_dump_node(c, node) ({})
  365. #define dbg_dump_lpt_node(c, node, lnum, offs) ({})
  366. #define dbg_dump_budget_req(req) ({})
  367. #define dbg_dump_lstats(lst) ({})
  368. #define dbg_dump_budg(c) ({})
  369. #define dbg_dump_lprop(c, lp) ({})
  370. #define dbg_dump_lprops(c) ({})
  371. #define dbg_dump_lpt_info(c) ({})
  372. #define dbg_dump_leb(c, lnum) ({})
  373. #define dbg_dump_znode(c, znode) ({})
  374. #define dbg_dump_heap(c, heap, cat) ({})
  375. #define dbg_dump_pnode(c, pnode, parent, iip) ({})
  376. #define dbg_dump_tnc(c) ({})
  377. #define dbg_dump_index(c) ({})
  378. #define dbg_dump_lpt_lebs(c) ({})
  379. #define dbg_walk_index(c, leaf_cb, znode_cb, priv) 0
  380. #define dbg_old_index_check_init(c, zroot) 0
  381. #define dbg_check_old_index(c, zroot) 0
  382. #define dbg_check_cats(c) 0
  383. #define dbg_check_ltab(c) 0
  384. #define dbg_chk_lpt_free_spc(c) 0
  385. #define dbg_chk_lpt_sz(c, action, len) 0
  386. #define dbg_check_synced_i_size(inode) 0
  387. #define dbg_check_dir_size(c, dir) 0
  388. #define dbg_check_tnc(c, x) 0
  389. #define dbg_check_idx_size(c, idx_size) 0
  390. #define dbg_check_filesystem(c) 0
  391. #define dbg_check_heap(c, heap, cat, add_pos) ({})
  392. #define dbg_check_lprops(c) 0
  393. #define dbg_check_lpt_nodes(c, cnode, row, col) 0
  394. #define dbg_force_in_the_gaps_enabled 0
  395. #define dbg_force_in_the_gaps() 0
  396. #define dbg_failure_mode 0
  397. #define dbg_debugfs_init() 0
  398. #define dbg_debugfs_exit()
  399. #define dbg_debugfs_init_fs(c) 0
  400. #define dbg_debugfs_exit_fs(c) 0
  401. #endif /* !CONFIG_UBIFS_FS_DEBUG */
  402. #endif /* !__UBIFS_DEBUG_H__ */