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