debug.h 12 KB

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