2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Artem Bityutskiy (Битюцкий Артём)
12 #ifndef __UBIFS_DEBUG_H__
13 #define __UBIFS_DEBUG_H__
16 /* Checking helper functions */
17 typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
18 struct ubifs_zbranch *zbr, void *priv);
19 typedef int (*dbg_znode_callback)(struct ubifs_info *c,
20 struct ubifs_znode *znode, void *priv);
23 * The UBIFS debugfs directory name pattern and maximum name length (3 for "ubi"
24 * + 1 for "_" and plus 2x2 for 2 UBI numbers and 1 for the trailing zero byte.
26 #define UBIFS_DFS_DIR_NAME "ubi%d_%d"
27 #define UBIFS_DFS_DIR_LEN (3 + 1 + 2*2 + 1)
30 * ubifs_debug_info - per-FS debugging information.
31 * @old_zroot: old index root - used by 'dbg_check_old_index()'
32 * @old_zroot_level: old index root level - used by 'dbg_check_old_index()'
33 * @old_zroot_sqnum: old index root sqnum - used by 'dbg_check_old_index()'
35 * @pc_happened: non-zero if an emulated power cut happened
36 * @pc_delay: 0=>don't delay, 1=>delay a time, 2=>delay a number of calls
37 * @pc_timeout: time in jiffies when delay of failure mode expires
38 * @pc_cnt: current number of calls to failure mode I/O functions
39 * @pc_cnt_max: number of calls by which to delay failure mode
41 * @chk_lpt_sz: used by LPT tree size checker
42 * @chk_lpt_sz2: used by LPT tree size checker
43 * @chk_lpt_wastage: used by LPT tree size checker
44 * @chk_lpt_lebs: used by LPT tree size checker
45 * @new_nhead_offs: used by LPT tree size checker
46 * @new_ihead_lnum: used by debugging to check @c->ihead_lnum
47 * @new_ihead_offs: used by debugging to check @c->ihead_offs
49 * @saved_lst: saved lprops statistics (used by 'dbg_save_space_info()')
50 * @saved_bi: saved budgeting information
51 * @saved_free: saved amount of free space
52 * @saved_idx_gc_cnt: saved value of @c->idx_gc_cnt
54 * @chk_gen: if general extra checks are enabled
55 * @chk_index: if index xtra checks are enabled
56 * @chk_orph: if orphans extra checks are enabled
57 * @chk_lprops: if lprops extra checks are enabled
58 * @chk_fs: if UBIFS contents extra checks are enabled
59 * @tst_rcvry: if UBIFS recovery testing mode enabled
61 * @dfs_dir_name: name of debugfs directory containing this file-system's files
62 * @dfs_dir: direntry object of the file-system debugfs directory
63 * @dfs_dump_lprops: "dump lprops" debugfs knob
64 * @dfs_dump_budg: "dump budgeting information" debugfs knob
65 * @dfs_dump_tnc: "dump TNC" debugfs knob
66 * @dfs_chk_gen: debugfs knob to enable UBIFS general extra checks
67 * @dfs_chk_index: debugfs knob to enable UBIFS index extra checks
68 * @dfs_chk_orph: debugfs knob to enable UBIFS orphans extra checks
69 * @dfs_chk_lprops: debugfs knob to enable UBIFS LEP properties extra checks
70 * @dfs_chk_fs: debugfs knob to enable UBIFS contents extra checks
71 * @dfs_tst_rcvry: debugfs knob to enable UBIFS recovery testing
72 * @dfs_ro_error: debugfs knob to switch UBIFS to R/O mode (different to
73 * re-mounting to R/O mode because it does not flush any buffers
74 * and UBIFS just starts returning -EROFS on all write
77 struct ubifs_debug_info {
78 struct ubifs_zbranch old_zroot;
80 unsigned long long old_zroot_sqnum;
84 unsigned long pc_timeout;
86 unsigned int pc_cnt_max;
89 long long chk_lpt_sz2;
90 long long chk_lpt_wastage;
96 struct ubifs_lp_stats saved_lst;
97 struct ubifs_budg_info saved_bi;
101 unsigned int chk_gen:1;
102 unsigned int chk_index:1;
103 unsigned int chk_orph:1;
104 unsigned int chk_lprops:1;
105 unsigned int chk_fs:1;
106 unsigned int tst_rcvry:1;
108 char dfs_dir_name[UBIFS_DFS_DIR_LEN + 1];
109 struct dentry *dfs_dir;
110 struct dentry *dfs_dump_lprops;
111 struct dentry *dfs_dump_budg;
112 struct dentry *dfs_dump_tnc;
113 struct dentry *dfs_chk_gen;
114 struct dentry *dfs_chk_index;
115 struct dentry *dfs_chk_orph;
116 struct dentry *dfs_chk_lprops;
117 struct dentry *dfs_chk_fs;
118 struct dentry *dfs_tst_rcvry;
119 struct dentry *dfs_ro_error;
123 * ubifs_global_debug_info - global (not per-FS) UBIFS debugging information.
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
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;
142 #define ubifs_assert(expr) do { \
143 if (unlikely(!(expr))) { \
144 pr_crit("UBIFS assert failed in %s at %u (pid %d)\n", \
145 __func__, __LINE__, current->pid); \
150 #define ubifs_assert_cmt_locked(c) do { \
151 if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
152 up_write(&(c)->commit_sem); \
153 pr_crit("commit lock is not locked!\n"); \
158 #define ubifs_dbg_msg(type, fmt, ...) \
159 pr_debug("UBIFS DBG " type " (pid %d): " fmt "\n", current->pid, \
162 #define DBG_KEY_BUF_LEN 48
163 #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \
164 char __tmp_key_buf[DBG_KEY_BUF_LEN]; \
165 pr_debug("UBIFS DBG " type " (pid %d): " fmt "%s\n", current->pid, \
167 dbg_snprintf_key(c, key, __tmp_key_buf, DBG_KEY_BUF_LEN)); \
170 #define ubifs_assert(expr) do { \
171 if (unlikely(!(expr))) { \
172 pr_crit("UBIFS assert failed in %s at %u\n", \
173 __func__, __LINE__); \
178 #define ubifs_assert_cmt_locked(c) do { \
179 if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
180 up_write(&(c)->commit_sem); \
181 pr_crit("commit lock is not locked!\n"); \
186 #define ubifs_dbg_msg(type, fmt, ...) \
187 pr_debug("UBIFS DBG " type ": " fmt "\n", \
190 #define DBG_KEY_BUF_LEN 48
191 #if defined CONFIG_MTD_DEBUG
192 #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \
193 char __tmp_key_buf[DBG_KEY_BUF_LEN]; \
194 pr_debug("UBIFS DBG " type ": " fmt "%s\n", \
196 dbg_snprintf_key(c, key, __tmp_key_buf, DBG_KEY_BUF_LEN)); \
199 #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \
200 pr_debug("UBIFS DBG\n"); \
207 /* General messages */
208 #define dbg_gen(fmt, ...) ubifs_dbg_msg("gen", fmt, ##__VA_ARGS__)
209 /* Additional journal messages */
210 #define dbg_jnl(fmt, ...) ubifs_dbg_msg("jnl", fmt, ##__VA_ARGS__)
211 #define dbg_jnlk(key, fmt, ...) \
212 ubifs_dbg_msg_key("jnl", key, fmt, ##__VA_ARGS__)
213 /* Additional TNC messages */
214 #define dbg_tnc(fmt, ...) ubifs_dbg_msg("tnc", fmt, ##__VA_ARGS__)
215 #define dbg_tnck(key, fmt, ...) \
216 ubifs_dbg_msg_key("tnc", key, fmt, ##__VA_ARGS__)
217 /* Additional lprops messages */
218 #define dbg_lp(fmt, ...) ubifs_dbg_msg("lp", fmt, ##__VA_ARGS__)
219 /* Additional LEB find messages */
220 #define dbg_find(fmt, ...) ubifs_dbg_msg("find", fmt, ##__VA_ARGS__)
221 /* Additional mount messages */
222 #define dbg_mnt(fmt, ...) ubifs_dbg_msg("mnt", fmt, ##__VA_ARGS__)
223 #define dbg_mntk(key, fmt, ...) \
224 ubifs_dbg_msg_key("mnt", key, fmt, ##__VA_ARGS__)
225 /* Additional I/O messages */
226 #define dbg_io(fmt, ...) ubifs_dbg_msg("io", fmt, ##__VA_ARGS__)
227 /* Additional commit messages */
228 #define dbg_cmt(fmt, ...) ubifs_dbg_msg("cmt", fmt, ##__VA_ARGS__)
229 /* Additional budgeting messages */
230 #define dbg_budg(fmt, ...) ubifs_dbg_msg("budg", fmt, ##__VA_ARGS__)
231 /* Additional log messages */
232 #define dbg_log(fmt, ...) ubifs_dbg_msg("log", fmt, ##__VA_ARGS__)
233 /* Additional gc messages */
234 #define dbg_gc(fmt, ...) ubifs_dbg_msg("gc", fmt, ##__VA_ARGS__)
235 /* Additional scan messages */
236 #define dbg_scan(fmt, ...) ubifs_dbg_msg("scan", fmt, ##__VA_ARGS__)
237 /* Additional recovery messages */
238 #define dbg_rcvry(fmt, ...) ubifs_dbg_msg("rcvry", fmt, ##__VA_ARGS__)
241 extern struct ubifs_global_debug_info ubifs_dbg;
243 static inline int dbg_is_chk_gen(const struct ubifs_info *c)
245 return !!(ubifs_dbg.chk_gen || c->dbg->chk_gen);
247 static inline int dbg_is_chk_index(const struct ubifs_info *c)
249 return !!(ubifs_dbg.chk_index || c->dbg->chk_index);
251 static inline int dbg_is_chk_orph(const struct ubifs_info *c)
253 return !!(ubifs_dbg.chk_orph || c->dbg->chk_orph);
255 static inline int dbg_is_chk_lprops(const struct ubifs_info *c)
257 return !!(ubifs_dbg.chk_lprops || c->dbg->chk_lprops);
259 static inline int dbg_is_chk_fs(const struct ubifs_info *c)
261 return !!(ubifs_dbg.chk_fs || c->dbg->chk_fs);
263 static inline int dbg_is_tst_rcvry(const struct ubifs_info *c)
265 return !!(ubifs_dbg.tst_rcvry || c->dbg->tst_rcvry);
267 static inline int dbg_is_power_cut(const struct ubifs_info *c)
269 return !!c->dbg->pc_happened;
272 int ubifs_debugging_init(struct ubifs_info *c);
273 void ubifs_debugging_exit(struct ubifs_info *c);
275 static inline int dbg_is_chk_gen(const struct ubifs_info *c)
279 static inline int dbg_is_chk_index(const struct ubifs_info *c)
283 static inline int dbg_is_chk_orph(const struct ubifs_info *c)
287 static inline int dbg_is_chk_lprops(const struct ubifs_info *c)
291 static inline int dbg_is_chk_fs(const struct ubifs_info *c)
295 static inline int dbg_is_tst_rcvry(const struct ubifs_info *c)
299 static inline int dbg_is_power_cut(const struct ubifs_info *c)
304 int ubifs_debugging_init(struct ubifs_info *c);
305 void ubifs_debugging_exit(struct ubifs_info *c);
310 const char *dbg_ntype(int type);
311 const char *dbg_cstate(int cmt_state);
312 const char *dbg_jhead(int jhead);
313 const char *dbg_get_key_dump(const struct ubifs_info *c,
314 const union ubifs_key *key);
315 const char *dbg_snprintf_key(const struct ubifs_info *c,
316 const union ubifs_key *key, char *buffer, int len);
317 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode);
318 void ubifs_dump_node(const struct ubifs_info *c, const void *node);
319 void ubifs_dump_budget_req(const struct ubifs_budget_req *req);
320 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst);
321 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi);
322 void ubifs_dump_lprop(const struct ubifs_info *c,
323 const struct ubifs_lprops *lp);
324 void ubifs_dump_lprops(struct ubifs_info *c);
325 void ubifs_dump_lpt_info(struct ubifs_info *c);
326 void ubifs_dump_leb(const struct ubifs_info *c, int lnum);
327 void ubifs_dump_sleb(const struct ubifs_info *c,
328 const struct ubifs_scan_leb *sleb, int offs);
329 void ubifs_dump_znode(const struct ubifs_info *c,
330 const struct ubifs_znode *znode);
331 void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
333 void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
334 struct ubifs_nnode *parent, int iip);
335 void ubifs_dump_tnc(struct ubifs_info *c);
336 void ubifs_dump_index(struct ubifs_info *c);
337 void ubifs_dump_lpt_lebs(const struct ubifs_info *c);
339 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
340 dbg_znode_callback znode_cb, void *priv);
342 /* Checking functions */
343 void dbg_save_space_info(struct ubifs_info *c);
344 int dbg_check_space_info(struct ubifs_info *c);
345 int dbg_check_lprops(struct ubifs_info *c);
346 int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
347 int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
348 int dbg_check_cats(struct ubifs_info *c);
349 int dbg_check_ltab(struct ubifs_info *c);
350 int dbg_chk_lpt_free_spc(struct ubifs_info *c);
351 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len);
352 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode);
353 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir);
354 int dbg_check_tnc(struct ubifs_info *c, int extra);
355 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
356 int dbg_check_filesystem(struct ubifs_info *c);
357 void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
359 int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
361 int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode,
363 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head);
364 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head);
366 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
368 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len);
369 int dbg_leb_unmap(struct ubifs_info *c, int lnum);
370 int dbg_leb_map(struct ubifs_info *c, int lnum);
372 /* Debugfs-related stuff */
373 int dbg_debugfs_init(void);
374 void dbg_debugfs_exit(void);
375 int dbg_debugfs_init_fs(struct ubifs_info *c);
376 void dbg_debugfs_exit_fs(struct ubifs_info *c);
378 #endif /* !__UBIFS_DEBUG_H__ */