debug.h 13 KB

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