debug.h 17 KB

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