debug.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * Authors: Artem Bityutskiy (Битюцкий Артём)
  9. * Adrian Hunter
  10. */
  11. #ifndef __UBIFS_DEBUG_H__
  12. #define __UBIFS_DEBUG_H__
  13. /* Checking helper functions */
  14. typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
  15. struct ubifs_zbranch *zbr, void *priv);
  16. typedef int (*dbg_znode_callback)(struct ubifs_info *c,
  17. struct ubifs_znode *znode, void *priv);
  18. /*
  19. * The UBIFS debugfs directory name pattern and maximum name length (3 for "ubi"
  20. * + 1 for "_" and plus 2x2 for 2 UBI numbers and 1 for the trailing zero byte.
  21. */
  22. #define UBIFS_DFS_DIR_NAME "ubi%d_%d"
  23. #define UBIFS_DFS_DIR_LEN (3 + 1 + 2*2 + 1)
  24. /**
  25. * ubifs_debug_info - per-FS debugging information.
  26. * @old_zroot: old index root - used by 'dbg_check_old_index()'
  27. * @old_zroot_level: old index root level - used by 'dbg_check_old_index()'
  28. * @old_zroot_sqnum: old index root sqnum - used by 'dbg_check_old_index()'
  29. *
  30. * @pc_happened: non-zero if an emulated power cut happened
  31. * @pc_delay: 0=>don't delay, 1=>delay a time, 2=>delay a number of calls
  32. * @pc_timeout: time in jiffies when delay of failure mode expires
  33. * @pc_cnt: current number of calls to failure mode I/O functions
  34. * @pc_cnt_max: number of calls by which to delay failure mode
  35. *
  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 @c->ihead_lnum
  42. * @new_ihead_offs: used by debugging to check @c->ihead_offs
  43. *
  44. * @saved_lst: saved lprops statistics (used by 'dbg_save_space_info()')
  45. * @saved_bi: saved budgeting information
  46. * @saved_free: saved amount of free space
  47. * @saved_idx_gc_cnt: saved value of @c->idx_gc_cnt
  48. *
  49. * @chk_gen: if general extra checks are enabled
  50. * @chk_index: if index xtra checks are enabled
  51. * @chk_orph: if orphans extra checks are enabled
  52. * @chk_lprops: if lprops extra checks are enabled
  53. * @chk_fs: if UBIFS contents extra checks are enabled
  54. * @tst_rcvry: if UBIFS recovery testing mode enabled
  55. *
  56. * @dfs_dir_name: name of debugfs directory containing this file-system's files
  57. * @dfs_dir: direntry object of the file-system debugfs directory
  58. * @dfs_dump_lprops: "dump lprops" debugfs knob
  59. * @dfs_dump_budg: "dump budgeting information" debugfs knob
  60. * @dfs_dump_tnc: "dump TNC" debugfs knob
  61. * @dfs_chk_gen: debugfs knob to enable UBIFS general extra checks
  62. * @dfs_chk_index: debugfs knob to enable UBIFS index extra checks
  63. * @dfs_chk_orph: debugfs knob to enable UBIFS orphans extra checks
  64. * @dfs_chk_lprops: debugfs knob to enable UBIFS LEP properties extra checks
  65. * @dfs_chk_fs: debugfs knob to enable UBIFS contents extra checks
  66. * @dfs_tst_rcvry: debugfs knob to enable UBIFS recovery testing
  67. * @dfs_ro_error: debugfs knob to switch UBIFS to R/O mode (different to
  68. * re-mounting to R/O mode because it does not flush any buffers
  69. * and UBIFS just starts returning -EROFS on all write
  70. * operations)
  71. */
  72. struct ubifs_debug_info {
  73. struct ubifs_zbranch old_zroot;
  74. int old_zroot_level;
  75. unsigned long long old_zroot_sqnum;
  76. int pc_happened;
  77. int pc_delay;
  78. unsigned long pc_timeout;
  79. unsigned int pc_cnt;
  80. unsigned int pc_cnt_max;
  81. long long chk_lpt_sz;
  82. long long chk_lpt_sz2;
  83. long long chk_lpt_wastage;
  84. int chk_lpt_lebs;
  85. int new_nhead_offs;
  86. int new_ihead_lnum;
  87. int new_ihead_offs;
  88. struct ubifs_lp_stats saved_lst;
  89. struct ubifs_budg_info saved_bi;
  90. long long saved_free;
  91. int saved_idx_gc_cnt;
  92. unsigned int chk_gen:1;
  93. unsigned int chk_index:1;
  94. unsigned int chk_orph:1;
  95. unsigned int chk_lprops:1;
  96. unsigned int chk_fs:1;
  97. unsigned int tst_rcvry:1;
  98. char dfs_dir_name[UBIFS_DFS_DIR_LEN + 1];
  99. struct dentry *dfs_dir;
  100. struct dentry *dfs_dump_lprops;
  101. struct dentry *dfs_dump_budg;
  102. struct dentry *dfs_dump_tnc;
  103. struct dentry *dfs_chk_gen;
  104. struct dentry *dfs_chk_index;
  105. struct dentry *dfs_chk_orph;
  106. struct dentry *dfs_chk_lprops;
  107. struct dentry *dfs_chk_fs;
  108. struct dentry *dfs_tst_rcvry;
  109. struct dentry *dfs_ro_error;
  110. };
  111. /**
  112. * ubifs_global_debug_info - global (not per-FS) UBIFS debugging information.
  113. *
  114. * @chk_gen: if general extra checks are enabled
  115. * @chk_index: if index xtra checks are enabled
  116. * @chk_orph: if orphans extra checks are enabled
  117. * @chk_lprops: if lprops extra checks are enabled
  118. * @chk_fs: if UBIFS contents extra checks are enabled
  119. * @tst_rcvry: if UBIFS recovery testing mode enabled
  120. */
  121. struct ubifs_global_debug_info {
  122. unsigned int chk_gen:1;
  123. unsigned int chk_index:1;
  124. unsigned int chk_orph:1;
  125. unsigned int chk_lprops:1;
  126. unsigned int chk_fs:1;
  127. unsigned int tst_rcvry:1;
  128. };
  129. #ifndef __UBOOT__
  130. #define ubifs_assert(expr) do { \
  131. if (unlikely(!(expr))) { \
  132. pr_crit("UBIFS assert failed in %s at %u (pid %d)\n", \
  133. __func__, __LINE__, current->pid); \
  134. dump_stack(); \
  135. } \
  136. } while (0)
  137. #define ubifs_assert_cmt_locked(c) do { \
  138. if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
  139. up_write(&(c)->commit_sem); \
  140. pr_crit("commit lock is not locked!\n"); \
  141. ubifs_assert(0); \
  142. } \
  143. } while (0)
  144. #define ubifs_dbg_msg(type, fmt, ...) \
  145. pr_debug("UBIFS DBG " type " (pid %d): " fmt "\n", current->pid, \
  146. ##__VA_ARGS__)
  147. #define DBG_KEY_BUF_LEN 48
  148. #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \
  149. char __tmp_key_buf[DBG_KEY_BUF_LEN]; \
  150. pr_debug("UBIFS DBG " type " (pid %d): " fmt "%s\n", current->pid, \
  151. ##__VA_ARGS__, \
  152. dbg_snprintf_key(c, key, __tmp_key_buf, DBG_KEY_BUF_LEN)); \
  153. } while (0)
  154. #else
  155. #define ubifs_assert(expr) do { \
  156. if (unlikely(!(expr))) { \
  157. pr_crit("UBIFS assert failed in %s at %u\n", \
  158. __func__, __LINE__); \
  159. dump_stack(); \
  160. } \
  161. } while (0)
  162. #define ubifs_assert_cmt_locked(c) do { \
  163. if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
  164. up_write(&(c)->commit_sem); \
  165. pr_crit("commit lock is not locked!\n"); \
  166. ubifs_assert(0); \
  167. } \
  168. } while (0)
  169. #define ubifs_dbg_msg(type, fmt, ...) \
  170. pr_debug("UBIFS DBG " type ": " fmt "\n", \
  171. ##__VA_ARGS__)
  172. #define DBG_KEY_BUF_LEN 48
  173. #if defined CONFIG_MTD_DEBUG
  174. #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \
  175. char __tmp_key_buf[DBG_KEY_BUF_LEN]; \
  176. pr_debug("UBIFS DBG " type ": " fmt "%s\n", \
  177. ##__VA_ARGS__, \
  178. dbg_snprintf_key(c, key, __tmp_key_buf, DBG_KEY_BUF_LEN)); \
  179. } while (0)
  180. #else
  181. #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \
  182. pr_debug("UBIFS DBG\n"); \
  183. } while (0)
  184. #endif
  185. #endif
  186. /* General messages */
  187. #define dbg_gen(fmt, ...) ubifs_dbg_msg("gen", fmt, ##__VA_ARGS__)
  188. /* Additional journal messages */
  189. #define dbg_jnl(fmt, ...) ubifs_dbg_msg("jnl", fmt, ##__VA_ARGS__)
  190. #define dbg_jnlk(key, fmt, ...) \
  191. ubifs_dbg_msg_key("jnl", key, fmt, ##__VA_ARGS__)
  192. /* Additional TNC messages */
  193. #define dbg_tnc(fmt, ...) ubifs_dbg_msg("tnc", fmt, ##__VA_ARGS__)
  194. #define dbg_tnck(key, fmt, ...) \
  195. ubifs_dbg_msg_key("tnc", key, fmt, ##__VA_ARGS__)
  196. /* Additional lprops messages */
  197. #define dbg_lp(fmt, ...) ubifs_dbg_msg("lp", fmt, ##__VA_ARGS__)
  198. /* Additional LEB find messages */
  199. #define dbg_find(fmt, ...) ubifs_dbg_msg("find", fmt, ##__VA_ARGS__)
  200. /* Additional mount messages */
  201. #define dbg_mnt(fmt, ...) ubifs_dbg_msg("mnt", fmt, ##__VA_ARGS__)
  202. #define dbg_mntk(key, fmt, ...) \
  203. ubifs_dbg_msg_key("mnt", key, fmt, ##__VA_ARGS__)
  204. /* Additional I/O messages */
  205. #define dbg_io(fmt, ...) ubifs_dbg_msg("io", fmt, ##__VA_ARGS__)
  206. /* Additional commit messages */
  207. #define dbg_cmt(fmt, ...) ubifs_dbg_msg("cmt", fmt, ##__VA_ARGS__)
  208. /* Additional budgeting messages */
  209. #define dbg_budg(fmt, ...) ubifs_dbg_msg("budg", fmt, ##__VA_ARGS__)
  210. /* Additional log messages */
  211. #define dbg_log(fmt, ...) ubifs_dbg_msg("log", fmt, ##__VA_ARGS__)
  212. /* Additional gc messages */
  213. #define dbg_gc(fmt, ...) ubifs_dbg_msg("gc", fmt, ##__VA_ARGS__)
  214. /* Additional scan messages */
  215. #define dbg_scan(fmt, ...) ubifs_dbg_msg("scan", fmt, ##__VA_ARGS__)
  216. /* Additional recovery messages */
  217. #define dbg_rcvry(fmt, ...) ubifs_dbg_msg("rcvry", fmt, ##__VA_ARGS__)
  218. #ifndef __UBOOT__
  219. extern struct ubifs_global_debug_info ubifs_dbg;
  220. static inline int dbg_is_chk_gen(const struct ubifs_info *c)
  221. {
  222. return !!(ubifs_dbg.chk_gen || c->dbg->chk_gen);
  223. }
  224. static inline int dbg_is_chk_index(const struct ubifs_info *c)
  225. {
  226. return !!(ubifs_dbg.chk_index || c->dbg->chk_index);
  227. }
  228. static inline int dbg_is_chk_orph(const struct ubifs_info *c)
  229. {
  230. return !!(ubifs_dbg.chk_orph || c->dbg->chk_orph);
  231. }
  232. static inline int dbg_is_chk_lprops(const struct ubifs_info *c)
  233. {
  234. return !!(ubifs_dbg.chk_lprops || c->dbg->chk_lprops);
  235. }
  236. static inline int dbg_is_chk_fs(const struct ubifs_info *c)
  237. {
  238. return !!(ubifs_dbg.chk_fs || c->dbg->chk_fs);
  239. }
  240. static inline int dbg_is_tst_rcvry(const struct ubifs_info *c)
  241. {
  242. return !!(ubifs_dbg.tst_rcvry || c->dbg->tst_rcvry);
  243. }
  244. static inline int dbg_is_power_cut(const struct ubifs_info *c)
  245. {
  246. return !!c->dbg->pc_happened;
  247. }
  248. int ubifs_debugging_init(struct ubifs_info *c);
  249. void ubifs_debugging_exit(struct ubifs_info *c);
  250. #else
  251. static inline int dbg_is_chk_gen(const struct ubifs_info *c)
  252. {
  253. return 0;
  254. }
  255. static inline int dbg_is_chk_index(const struct ubifs_info *c)
  256. {
  257. return 0;
  258. }
  259. static inline int dbg_is_chk_orph(const struct ubifs_info *c)
  260. {
  261. return 0;
  262. }
  263. static inline int dbg_is_chk_lprops(const struct ubifs_info *c)
  264. {
  265. return 0;
  266. }
  267. static inline int dbg_is_chk_fs(const struct ubifs_info *c)
  268. {
  269. return 0;
  270. }
  271. static inline int dbg_is_tst_rcvry(const struct ubifs_info *c)
  272. {
  273. return 0;
  274. }
  275. static inline int dbg_is_power_cut(const struct ubifs_info *c)
  276. {
  277. return 0;
  278. }
  279. int ubifs_debugging_init(struct ubifs_info *c);
  280. void ubifs_debugging_exit(struct ubifs_info *c);
  281. #endif
  282. /* Dump functions */
  283. const char *dbg_ntype(int type);
  284. const char *dbg_cstate(int cmt_state);
  285. const char *dbg_jhead(int jhead);
  286. const char *dbg_get_key_dump(const struct ubifs_info *c,
  287. const union ubifs_key *key);
  288. const char *dbg_snprintf_key(const struct ubifs_info *c,
  289. const union ubifs_key *key, char *buffer, int len);
  290. void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode);
  291. void ubifs_dump_node(const struct ubifs_info *c, const void *node);
  292. void ubifs_dump_budget_req(const struct ubifs_budget_req *req);
  293. void ubifs_dump_lstats(const struct ubifs_lp_stats *lst);
  294. void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi);
  295. void ubifs_dump_lprop(const struct ubifs_info *c,
  296. const struct ubifs_lprops *lp);
  297. void ubifs_dump_lprops(struct ubifs_info *c);
  298. void ubifs_dump_lpt_info(struct ubifs_info *c);
  299. void ubifs_dump_leb(const struct ubifs_info *c, int lnum);
  300. void ubifs_dump_sleb(const struct ubifs_info *c,
  301. const struct ubifs_scan_leb *sleb, int offs);
  302. void ubifs_dump_znode(const struct ubifs_info *c,
  303. const struct ubifs_znode *znode);
  304. void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
  305. int cat);
  306. void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
  307. struct ubifs_nnode *parent, int iip);
  308. void ubifs_dump_tnc(struct ubifs_info *c);
  309. void ubifs_dump_index(struct ubifs_info *c);
  310. void ubifs_dump_lpt_lebs(const struct ubifs_info *c);
  311. int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
  312. dbg_znode_callback znode_cb, void *priv);
  313. /* Checking functions */
  314. void dbg_save_space_info(struct ubifs_info *c);
  315. int dbg_check_space_info(struct ubifs_info *c);
  316. int dbg_check_lprops(struct ubifs_info *c);
  317. int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  318. int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  319. int dbg_check_cats(struct ubifs_info *c);
  320. int dbg_check_ltab(struct ubifs_info *c);
  321. int dbg_chk_lpt_free_spc(struct ubifs_info *c);
  322. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len);
  323. int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode);
  324. int dbg_check_dir(struct ubifs_info *c, const struct inode *dir);
  325. int dbg_check_tnc(struct ubifs_info *c, int extra);
  326. int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
  327. int dbg_check_filesystem(struct ubifs_info *c);
  328. void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
  329. int add_pos);
  330. int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
  331. int row, int col);
  332. int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode,
  333. loff_t size);
  334. int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head);
  335. int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head);
  336. int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
  337. int len);
  338. int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len);
  339. int dbg_leb_unmap(struct ubifs_info *c, int lnum);
  340. int dbg_leb_map(struct ubifs_info *c, int lnum);
  341. /* Debugfs-related stuff */
  342. int dbg_debugfs_init(void);
  343. void dbg_debugfs_exit(void);
  344. int dbg_debugfs_init_fs(struct ubifs_info *c);
  345. void dbg_debugfs_exit_fs(struct ubifs_info *c);
  346. #endif /* !__UBIFS_DEBUG_H__ */