debug.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  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 2 for UBI device numbers and 3 for volume number and 1 for
  20. * the trailing zero byte.
  21. */
  22. #define UBIFS_DFS_DIR_NAME "ubi%d_%d"
  23. #define UBIFS_DFS_DIR_LEN (3 + 1 + 2 + 3 + 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];
  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. void ubifs_assert_failed(struct ubifs_info *c, const char *expr,
  130. const char *file, int line);
  131. #define ubifs_assert(c, expr) do { \
  132. if (unlikely(!(expr))) { \
  133. ubifs_assert_failed((struct ubifs_info *)c, #expr, __FILE__, \
  134. __LINE__); \
  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. ubifs_err(c, "commit lock is not locked!\n"); \
  141. ubifs_assert(c, 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. /* General messages */
  155. #define dbg_gen(fmt, ...) ubifs_dbg_msg("gen", fmt, ##__VA_ARGS__)
  156. /* Additional journal messages */
  157. #define dbg_jnl(fmt, ...) ubifs_dbg_msg("jnl", fmt, ##__VA_ARGS__)
  158. #define dbg_jnlk(key, fmt, ...) \
  159. ubifs_dbg_msg_key("jnl", key, fmt, ##__VA_ARGS__)
  160. /* Additional TNC messages */
  161. #define dbg_tnc(fmt, ...) ubifs_dbg_msg("tnc", fmt, ##__VA_ARGS__)
  162. #define dbg_tnck(key, fmt, ...) \
  163. ubifs_dbg_msg_key("tnc", key, fmt, ##__VA_ARGS__)
  164. /* Additional lprops messages */
  165. #define dbg_lp(fmt, ...) ubifs_dbg_msg("lp", fmt, ##__VA_ARGS__)
  166. /* Additional LEB find messages */
  167. #define dbg_find(fmt, ...) ubifs_dbg_msg("find", fmt, ##__VA_ARGS__)
  168. /* Additional mount messages */
  169. #define dbg_mnt(fmt, ...) ubifs_dbg_msg("mnt", fmt, ##__VA_ARGS__)
  170. #define dbg_mntk(key, fmt, ...) \
  171. ubifs_dbg_msg_key("mnt", key, fmt, ##__VA_ARGS__)
  172. /* Additional I/O messages */
  173. #define dbg_io(fmt, ...) ubifs_dbg_msg("io", fmt, ##__VA_ARGS__)
  174. /* Additional commit messages */
  175. #define dbg_cmt(fmt, ...) ubifs_dbg_msg("cmt", fmt, ##__VA_ARGS__)
  176. /* Additional budgeting messages */
  177. #define dbg_budg(fmt, ...) ubifs_dbg_msg("budg", fmt, ##__VA_ARGS__)
  178. /* Additional log messages */
  179. #define dbg_log(fmt, ...) ubifs_dbg_msg("log", fmt, ##__VA_ARGS__)
  180. /* Additional gc messages */
  181. #define dbg_gc(fmt, ...) ubifs_dbg_msg("gc", fmt, ##__VA_ARGS__)
  182. /* Additional scan messages */
  183. #define dbg_scan(fmt, ...) ubifs_dbg_msg("scan", fmt, ##__VA_ARGS__)
  184. /* Additional recovery messages */
  185. #define dbg_rcvry(fmt, ...) ubifs_dbg_msg("rcvry", fmt, ##__VA_ARGS__)
  186. extern struct ubifs_global_debug_info ubifs_dbg;
  187. static inline int dbg_is_chk_gen(const struct ubifs_info *c)
  188. {
  189. return !!(ubifs_dbg.chk_gen || c->dbg->chk_gen);
  190. }
  191. static inline int dbg_is_chk_index(const struct ubifs_info *c)
  192. {
  193. return !!(ubifs_dbg.chk_index || c->dbg->chk_index);
  194. }
  195. static inline int dbg_is_chk_orph(const struct ubifs_info *c)
  196. {
  197. return !!(ubifs_dbg.chk_orph || c->dbg->chk_orph);
  198. }
  199. static inline int dbg_is_chk_lprops(const struct ubifs_info *c)
  200. {
  201. return !!(ubifs_dbg.chk_lprops || c->dbg->chk_lprops);
  202. }
  203. static inline int dbg_is_chk_fs(const struct ubifs_info *c)
  204. {
  205. return !!(ubifs_dbg.chk_fs || c->dbg->chk_fs);
  206. }
  207. static inline int dbg_is_tst_rcvry(const struct ubifs_info *c)
  208. {
  209. return !!(ubifs_dbg.tst_rcvry || c->dbg->tst_rcvry);
  210. }
  211. static inline int dbg_is_power_cut(const struct ubifs_info *c)
  212. {
  213. return !!c->dbg->pc_happened;
  214. }
  215. int ubifs_debugging_init(struct ubifs_info *c);
  216. void ubifs_debugging_exit(struct ubifs_info *c);
  217. /* Dump functions */
  218. const char *dbg_ntype(int type);
  219. const char *dbg_cstate(int cmt_state);
  220. const char *dbg_jhead(int jhead);
  221. const char *dbg_get_key_dump(const struct ubifs_info *c,
  222. const union ubifs_key *key);
  223. const char *dbg_snprintf_key(const struct ubifs_info *c,
  224. const union ubifs_key *key, char *buffer, int len);
  225. void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode);
  226. void ubifs_dump_node(const struct ubifs_info *c, const void *node,
  227. int node_len);
  228. void ubifs_dump_budget_req(const struct ubifs_budget_req *req);
  229. void ubifs_dump_lstats(const struct ubifs_lp_stats *lst);
  230. void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi);
  231. void ubifs_dump_lprop(const struct ubifs_info *c,
  232. const struct ubifs_lprops *lp);
  233. void ubifs_dump_lprops(struct ubifs_info *c);
  234. void ubifs_dump_lpt_info(struct ubifs_info *c);
  235. void ubifs_dump_leb(const struct ubifs_info *c, int lnum);
  236. void ubifs_dump_znode(const struct ubifs_info *c,
  237. const struct ubifs_znode *znode);
  238. void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
  239. int cat);
  240. void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
  241. struct ubifs_nnode *parent, int iip);
  242. void ubifs_dump_tnc(struct ubifs_info *c);
  243. void ubifs_dump_index(struct ubifs_info *c);
  244. void ubifs_dump_lpt_lebs(const struct ubifs_info *c);
  245. int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
  246. dbg_znode_callback znode_cb, void *priv);
  247. /* Checking functions */
  248. void dbg_save_space_info(struct ubifs_info *c);
  249. int dbg_check_space_info(struct ubifs_info *c);
  250. int dbg_check_lprops(struct ubifs_info *c);
  251. int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  252. int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
  253. int dbg_check_cats(struct ubifs_info *c);
  254. int dbg_check_ltab(struct ubifs_info *c);
  255. int dbg_chk_lpt_free_spc(struct ubifs_info *c);
  256. int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len);
  257. int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode);
  258. int dbg_check_dir(struct ubifs_info *c, const struct inode *dir);
  259. int dbg_check_tnc(struct ubifs_info *c, int extra);
  260. int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
  261. int dbg_check_filesystem(struct ubifs_info *c);
  262. void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
  263. int add_pos);
  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. int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
  271. int len);
  272. int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len);
  273. int dbg_leb_unmap(struct ubifs_info *c, int lnum);
  274. int dbg_leb_map(struct ubifs_info *c, int lnum);
  275. /* Debugfs-related stuff */
  276. void dbg_debugfs_init(void);
  277. void dbg_debugfs_exit(void);
  278. void dbg_debugfs_init_fs(struct ubifs_info *c);
  279. void dbg_debugfs_exit_fs(struct ubifs_info *c);
  280. #endif /* !__UBIFS_DEBUG_H__ */