debug.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #define ubifs_assert(expr) do { \
  155. if (unlikely(!(expr))) { \
  156. pr_debug("UBIFS assert failed in %s at %u\n", \
  157. __func__, __LINE__); \
  158. dump_stack(); \
  159. } \
  160. } while (0)
  161. #define ubifs_assert_cmt_locked(c) do { \
  162. if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
  163. up_write(&(c)->commit_sem); \
  164. pr_debug("commit lock is not locked!\n"); \
  165. ubifs_assert(0); \
  166. } \
  167. } while (0)
  168. #define ubifs_dbg_msg(type, fmt, ...) \
  169. pr_debug("UBIFS DBG " type ": " fmt "\n", \
  170. ##__VA_ARGS__)
  171. #define DBG_KEY_BUF_LEN 48
  172. #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \
  173. char __tmp_key_buf[DBG_KEY_BUF_LEN]; \
  174. pr_debug("UBIFS DBG " type ": " fmt "%s\n", \
  175. ##__VA_ARGS__, \
  176. dbg_snprintf_key(c, key, __tmp_key_buf, DBG_KEY_BUF_LEN)); \
  177. } while (0)
  178. #endif
  179. /* General messages */
  180. #define dbg_gen(fmt, ...) ubifs_dbg_msg("gen", fmt, ##__VA_ARGS__)
  181. /* Additional journal messages */
  182. #define dbg_jnl(fmt, ...) ubifs_dbg_msg("jnl", fmt, ##__VA_ARGS__)
  183. #define dbg_jnlk(key, fmt, ...) \
  184. ubifs_dbg_msg_key("jnl", key, fmt, ##__VA_ARGS__)
  185. /* Additional TNC messages */
  186. #define dbg_tnc(fmt, ...) ubifs_dbg_msg("tnc", fmt, ##__VA_ARGS__)
  187. #define dbg_tnck(key, fmt, ...) \
  188. ubifs_dbg_msg_key("tnc", key, fmt, ##__VA_ARGS__)
  189. /* Additional lprops messages */
  190. #define dbg_lp(fmt, ...) ubifs_dbg_msg("lp", fmt, ##__VA_ARGS__)
  191. /* Additional LEB find messages */
  192. #define dbg_find(fmt, ...) ubifs_dbg_msg("find", fmt, ##__VA_ARGS__)
  193. /* Additional mount messages */
  194. #define dbg_mnt(fmt, ...) ubifs_dbg_msg("mnt", fmt, ##__VA_ARGS__)
  195. #define dbg_mntk(key, fmt, ...) \
  196. ubifs_dbg_msg_key("mnt", key, fmt, ##__VA_ARGS__)
  197. /* Additional I/O messages */
  198. #define dbg_io(fmt, ...) ubifs_dbg_msg("io", fmt, ##__VA_ARGS__)
  199. /* Additional commit messages */
  200. #define dbg_cmt(fmt, ...) ubifs_dbg_msg("cmt", fmt, ##__VA_ARGS__)
  201. /* Additional budgeting messages */
  202. #define dbg_budg(fmt, ...) ubifs_dbg_msg("budg", fmt, ##__VA_ARGS__)
  203. /* Additional log messages */
  204. #define dbg_log(fmt, ...) ubifs_dbg_msg("log", fmt, ##__VA_ARGS__)
  205. /* Additional gc messages */
  206. #define dbg_gc(fmt, ...) ubifs_dbg_msg("gc", fmt, ##__VA_ARGS__)
  207. /* Additional scan messages */
  208. #define dbg_scan(fmt, ...) ubifs_dbg_msg("scan", fmt, ##__VA_ARGS__)
  209. /* Additional recovery messages */
  210. #define dbg_rcvry(fmt, ...) ubifs_dbg_msg("rcvry", fmt, ##__VA_ARGS__)
  211. #ifndef __UBOOT__
  212. extern struct ubifs_global_debug_info ubifs_dbg;
  213. static inline int dbg_is_chk_gen(const struct ubifs_info *c)
  214. {
  215. return !!(ubifs_dbg.chk_gen || c->dbg->chk_gen);
  216. }
  217. static inline int dbg_is_chk_index(const struct ubifs_info *c)
  218. {
  219. return !!(ubifs_dbg.chk_index || c->dbg->chk_index);
  220. }
  221. static inline int dbg_is_chk_orph(const struct ubifs_info *c)
  222. {
  223. return !!(ubifs_dbg.chk_orph || c->dbg->chk_orph);
  224. }
  225. static inline int dbg_is_chk_lprops(const struct ubifs_info *c)
  226. {
  227. return !!(ubifs_dbg.chk_lprops || c->dbg->chk_lprops);
  228. }
  229. static inline int dbg_is_chk_fs(const struct ubifs_info *c)
  230. {
  231. return !!(ubifs_dbg.chk_fs || c->dbg->chk_fs);
  232. }
  233. static inline int dbg_is_tst_rcvry(const struct ubifs_info *c)
  234. {
  235. return !!(ubifs_dbg.tst_rcvry || c->dbg->tst_rcvry);
  236. }
  237. static inline int dbg_is_power_cut(const struct ubifs_info *c)
  238. {
  239. return !!c->dbg->pc_happened;
  240. }
  241. int ubifs_debugging_init(struct ubifs_info *c);
  242. void ubifs_debugging_exit(struct ubifs_info *c);
  243. #else
  244. static inline int dbg_is_chk_gen(const struct ubifs_info *c)
  245. {
  246. return 0;
  247. }
  248. static inline int dbg_is_chk_index(const struct ubifs_info *c)
  249. {
  250. return 0;
  251. }
  252. static inline int dbg_is_chk_orph(const struct ubifs_info *c)
  253. {
  254. return 0;
  255. }
  256. static inline int dbg_is_chk_lprops(const struct ubifs_info *c)
  257. {
  258. return 0;
  259. }
  260. static inline int dbg_is_chk_fs(const struct ubifs_info *c)
  261. {
  262. return 0;
  263. }
  264. static inline int dbg_is_tst_rcvry(const struct ubifs_info *c)
  265. {
  266. return 0;
  267. }
  268. static inline int dbg_is_power_cut(const struct ubifs_info *c)
  269. {
  270. return 0;
  271. }
  272. int ubifs_debugging_init(struct ubifs_info *c);
  273. void ubifs_debugging_exit(struct ubifs_info *c);
  274. #endif
  275. /* Dump functions */
  276. const char *dbg_ntype(int type);
  277. const char *dbg_cstate(int cmt_state);
  278. const char *dbg_jhead(int jhead);
  279. const char *dbg_get_key_dump(const struct ubifs_info *c,
  280. const union ubifs_key *key);
  281. const char *dbg_snprintf_key(const struct ubifs_info *c,
  282. const union ubifs_key *key, char *buffer, int len);
  283. void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode);
  284. void ubifs_dump_node(const struct ubifs_info *c, const void *node);
  285. void ubifs_dump_budget_req(const struct ubifs_budget_req *req);
  286. void ubifs_dump_lstats(const struct ubifs_lp_stats *lst);
  287. void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi);
  288. void ubifs_dump_lprop(const struct ubifs_info *c,
  289. const struct ubifs_lprops *lp);
  290. void ubifs_dump_lprops(struct ubifs_info *c);
  291. void ubifs_dump_lpt_info(struct ubifs_info *c);
  292. void ubifs_dump_leb(const struct ubifs_info *c, int lnum);
  293. void ubifs_dump_sleb(const struct ubifs_info *c,
  294. const struct ubifs_scan_leb *sleb, int offs);
  295. void ubifs_dump_znode(const struct ubifs_info *c,
  296. const struct ubifs_znode *znode);
  297. void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
  298. int cat);
  299. void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
  300. struct ubifs_nnode *parent, int iip);
  301. void ubifs_dump_tnc(struct ubifs_info *c);
  302. void ubifs_dump_index(struct ubifs_info *c);
  303. void ubifs_dump_lpt_lebs(const struct ubifs_info *c);
  304. int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
  305. dbg_znode_callback znode_cb, void *priv);
  306. /* Checking functions */
  307. void dbg_save_space_info(struct ubifs_info *c);
  308. int dbg_check_space_info(struct ubifs_info *c);
  309. int dbg_check_lprops(struct ubifs_info *c);
  310. int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  311. int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  312. int dbg_check_cats(struct ubifs_info *c);
  313. int dbg_check_ltab(struct ubifs_info *c);
  314. int dbg_chk_lpt_free_spc(struct ubifs_info *c);
  315. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len);
  316. int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode);
  317. int dbg_check_dir(struct ubifs_info *c, const struct inode *dir);
  318. int dbg_check_tnc(struct ubifs_info *c, int extra);
  319. int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
  320. int dbg_check_filesystem(struct ubifs_info *c);
  321. void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
  322. int add_pos);
  323. int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
  324. int row, int col);
  325. int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode,
  326. loff_t size);
  327. int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head);
  328. int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head);
  329. int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
  330. int len);
  331. int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len);
  332. int dbg_leb_unmap(struct ubifs_info *c, int lnum);
  333. int dbg_leb_map(struct ubifs_info *c, int lnum);
  334. /* Debugfs-related stuff */
  335. int dbg_debugfs_init(void);
  336. void dbg_debugfs_exit(void);
  337. int dbg_debugfs_init_fs(struct ubifs_info *c);
  338. void dbg_debugfs_exit_fs(struct ubifs_info *c);
  339. #endif /* !__UBIFS_DEBUG_H__ */