blob: 6d325af8bcc296305815536f748aed790e697df4 [file] [log] [blame]
Stefan Roese2fc10f62009-03-19 15:35:05 +01001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
Heiko Schocherf5895d12014-06-24 10:10:04 +02006 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese2fc10f62009-03-19 15:35:05 +01007 *
8 * Authors: Artem Bityutskiy (Битюцкий Артём)
9 * Adrian Hunter
10 */
11
12#ifndef __UBIFS_DEBUG_H__
13#define __UBIFS_DEBUG_H__
14
Heiko Schocherf5895d12014-06-24 10:10:04 +020015#define __UBOOT__
16/* Checking helper functions */
17typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
18 struct ubifs_zbranch *zbr, void *priv);
19typedef int (*dbg_znode_callback)(struct ubifs_info *c,
20 struct ubifs_znode *znode, void *priv);
21
22/*
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.
25 */
26#define UBIFS_DFS_DIR_NAME "ubi%d_%d"
27#define UBIFS_DFS_DIR_LEN (3 + 1 + 2*2 + 1)
Stefan Roese2fc10f62009-03-19 15:35:05 +010028
29/**
30 * ubifs_debug_info - per-FS debugging information.
Stefan Roese2fc10f62009-03-19 15:35:05 +010031 * @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()'
Heiko Schocherf5895d12014-06-24 10:10:04 +020034 *
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
40 *
Stefan Roese2fc10f62009-03-19 15:35:05 +010041 * @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
48 *
49 * @saved_lst: saved lprops statistics (used by 'dbg_save_space_info()')
Heiko Schocherf5895d12014-06-24 10:10:04 +020050 * @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
Stefan Roese2fc10f62009-03-19 15:35:05 +010053 *
Heiko Schocherf5895d12014-06-24 10:10:04 +020054 * @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
60 *
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
75 * operations)
Stefan Roese2fc10f62009-03-19 15:35:05 +010076 */
77struct ubifs_debug_info {
Stefan Roese2fc10f62009-03-19 15:35:05 +010078 struct ubifs_zbranch old_zroot;
79 int old_zroot_level;
80 unsigned long long old_zroot_sqnum;
Heiko Schocherf5895d12014-06-24 10:10:04 +020081
82 int pc_happened;
83 int pc_delay;
84 unsigned long pc_timeout;
85 unsigned int pc_cnt;
86 unsigned int pc_cnt_max;
87
Stefan Roese2fc10f62009-03-19 15:35:05 +010088 long long chk_lpt_sz;
89 long long chk_lpt_sz2;
90 long long chk_lpt_wastage;
91 int chk_lpt_lebs;
92 int new_nhead_offs;
93 int new_ihead_lnum;
94 int new_ihead_offs;
95
96 struct ubifs_lp_stats saved_lst;
Heiko Schocherf5895d12014-06-24 10:10:04 +020097 struct ubifs_budg_info saved_bi;
Stefan Roese2fc10f62009-03-19 15:35:05 +010098 long long saved_free;
Heiko Schocherf5895d12014-06-24 10:10:04 +020099 int saved_idx_gc_cnt;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100100
Heiko Schocherf5895d12014-06-24 10:10:04 +0200101 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;
107
108 char dfs_dir_name[UBIFS_DFS_DIR_LEN + 1];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100109 struct dentry *dfs_dir;
110 struct dentry *dfs_dump_lprops;
111 struct dentry *dfs_dump_budg;
112 struct dentry *dfs_dump_tnc;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200113 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;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100120};
121
Heiko Schocherf5895d12014-06-24 10:10:04 +0200122/**
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 */
132struct 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};
Stefan Roese2fc10f62009-03-19 15:35:05 +0100140
Heiko Schocherf5895d12014-06-24 10:10:04 +0200141#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100142#define ubifs_assert(expr) do { \
143 if (unlikely(!(expr))) { \
Heiko Schocherf5895d12014-06-24 10:10:04 +0200144 pr_crit("UBIFS assert failed in %s at %u (pid %d)\n", \
145 __func__, __LINE__, current->pid); \
146 dump_stack(); \
Stefan Roese2fc10f62009-03-19 15:35:05 +0100147 } \
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); \
Heiko Schocherf5895d12014-06-24 10:10:04 +0200153 pr_crit("commit lock is not locked!\n"); \
Stefan Roese2fc10f62009-03-19 15:35:05 +0100154 ubifs_assert(0); \
155 } \
156} while (0)
157
Heiko Schocherf5895d12014-06-24 10:10:04 +0200158#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#else
170#define ubifs_assert(expr) do { \
171 if (unlikely(!(expr))) { \
172 pr_crit("UBIFS assert failed in %s at %u\n", \
173 __func__, __LINE__); \
Stefan Roese2fc10f62009-03-19 15:35:05 +0100174 dump_stack(); \
Heiko Schocherf5895d12014-06-24 10:10:04 +0200175 } \
Stefan Roese2fc10f62009-03-19 15:35:05 +0100176} while (0)
177
Heiko Schocherf5895d12014-06-24 10:10:04 +0200178#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"); \
182 ubifs_assert(0); \
183 } \
Stefan Roese2fc10f62009-03-19 15:35:05 +0100184} while (0)
185
Heiko Schocherf5895d12014-06-24 10:10:04 +0200186#define ubifs_dbg_msg(type, fmt, ...) \
187 pr_debug("UBIFS DBG " type ": " fmt "\n", \
188 ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100189
Heiko Schocherf5895d12014-06-24 10:10:04 +0200190#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", \
195 ##__VA_ARGS__, \
196 dbg_snprintf_key(c, key, __tmp_key_buf, DBG_KEY_BUF_LEN)); \
197} while (0)
198#else
199#define ubifs_dbg_msg_key(type, key, fmt, ...) do { \
200 pr_debug("UBIFS DBG\n"); \
Stefan Roese2fc10f62009-03-19 15:35:05 +0100201} while (0)
202
Heiko Schocherf5895d12014-06-24 10:10:04 +0200203#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100204
Heiko Schocherf5895d12014-06-24 10:10:04 +0200205#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100206
207/* General messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200208#define dbg_gen(fmt, ...) ubifs_dbg_msg("gen", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100209/* Additional journal messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200210#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__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100213/* Additional TNC messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200214#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__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100217/* Additional lprops messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200218#define dbg_lp(fmt, ...) ubifs_dbg_msg("lp", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100219/* Additional LEB find messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200220#define dbg_find(fmt, ...) ubifs_dbg_msg("find", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100221/* Additional mount messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200222#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__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100225/* Additional I/O messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200226#define dbg_io(fmt, ...) ubifs_dbg_msg("io", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100227/* Additional commit messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200228#define dbg_cmt(fmt, ...) ubifs_dbg_msg("cmt", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100229/* Additional budgeting messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200230#define dbg_budg(fmt, ...) ubifs_dbg_msg("budg", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100231/* Additional log messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200232#define dbg_log(fmt, ...) ubifs_dbg_msg("log", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100233/* Additional gc messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200234#define dbg_gc(fmt, ...) ubifs_dbg_msg("gc", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100235/* Additional scan messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200236#define dbg_scan(fmt, ...) ubifs_dbg_msg("scan", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100237/* Additional recovery messages */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200238#define dbg_rcvry(fmt, ...) ubifs_dbg_msg("rcvry", fmt, ##__VA_ARGS__)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100239
Heiko Schocherf5895d12014-06-24 10:10:04 +0200240#ifndef __UBOOT__
241extern struct ubifs_global_debug_info ubifs_dbg;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100242
Heiko Schocherf5895d12014-06-24 10:10:04 +0200243static inline int dbg_is_chk_gen(const struct ubifs_info *c)
244{
245 return !!(ubifs_dbg.chk_gen || c->dbg->chk_gen);
246}
247static inline int dbg_is_chk_index(const struct ubifs_info *c)
248{
249 return !!(ubifs_dbg.chk_index || c->dbg->chk_index);
250}
251static inline int dbg_is_chk_orph(const struct ubifs_info *c)
252{
253 return !!(ubifs_dbg.chk_orph || c->dbg->chk_orph);
254}
255static inline int dbg_is_chk_lprops(const struct ubifs_info *c)
256{
257 return !!(ubifs_dbg.chk_lprops || c->dbg->chk_lprops);
258}
259static inline int dbg_is_chk_fs(const struct ubifs_info *c)
260{
261 return !!(ubifs_dbg.chk_fs || c->dbg->chk_fs);
262}
263static inline int dbg_is_tst_rcvry(const struct ubifs_info *c)
264{
265 return !!(ubifs_dbg.tst_rcvry || c->dbg->tst_rcvry);
266}
267static inline int dbg_is_power_cut(const struct ubifs_info *c)
268{
269 return !!c->dbg->pc_happened;
270}
Stefan Roese2fc10f62009-03-19 15:35:05 +0100271
Heiko Schocherf5895d12014-06-24 10:10:04 +0200272int ubifs_debugging_init(struct ubifs_info *c);
273void ubifs_debugging_exit(struct ubifs_info *c);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100274#else
Heiko Schocherf5895d12014-06-24 10:10:04 +0200275static inline int dbg_is_chk_gen(const struct ubifs_info *c)
276{
277 return 0;
278}
279static inline int dbg_is_chk_index(const struct ubifs_info *c)
280{
281 return 0;
282}
283static inline int dbg_is_chk_orph(const struct ubifs_info *c)
284{
285 return 0;
286}
287static inline int dbg_is_chk_lprops(const struct ubifs_info *c)
288{
289 return 0;
290}
291static inline int dbg_is_chk_fs(const struct ubifs_info *c)
292{
293 return 0;
294}
295static inline int dbg_is_tst_rcvry(const struct ubifs_info *c)
296{
297 return 0;
298}
299static inline int dbg_is_power_cut(const struct ubifs_info *c)
300{
301 return 0;
302}
Stefan Roese2fc10f62009-03-19 15:35:05 +0100303
304int ubifs_debugging_init(struct ubifs_info *c);
305void ubifs_debugging_exit(struct ubifs_info *c);
306
Heiko Schocherf5895d12014-06-24 10:10:04 +0200307#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100308
Heiko Schocherf5895d12014-06-24 10:10:04 +0200309/* Dump functions */
310const char *dbg_ntype(int type);
311const char *dbg_cstate(int cmt_state);
312const char *dbg_jhead(int jhead);
313const char *dbg_get_key_dump(const struct ubifs_info *c,
314 const union ubifs_key *key);
315const char *dbg_snprintf_key(const struct ubifs_info *c,
316 const union ubifs_key *key, char *buffer, int len);
317void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode);
318void ubifs_dump_node(const struct ubifs_info *c, const void *node);
319void ubifs_dump_budget_req(const struct ubifs_budget_req *req);
320void ubifs_dump_lstats(const struct ubifs_lp_stats *lst);
321void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi);
322void ubifs_dump_lprop(const struct ubifs_info *c,
323 const struct ubifs_lprops *lp);
324void ubifs_dump_lprops(struct ubifs_info *c);
325void ubifs_dump_lpt_info(struct ubifs_info *c);
326void ubifs_dump_leb(const struct ubifs_info *c, int lnum);
327void ubifs_dump_sleb(const struct ubifs_info *c,
328 const struct ubifs_scan_leb *sleb, int offs);
329void ubifs_dump_znode(const struct ubifs_info *c,
330 const struct ubifs_znode *znode);
331void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
332 int cat);
333void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
334 struct ubifs_nnode *parent, int iip);
335void ubifs_dump_tnc(struct ubifs_info *c);
336void ubifs_dump_index(struct ubifs_info *c);
337void ubifs_dump_lpt_lebs(const struct ubifs_info *c);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100338
Heiko Schocherf5895d12014-06-24 10:10:04 +0200339int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
340 dbg_znode_callback znode_cb, void *priv);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100341
Heiko Schocherf5895d12014-06-24 10:10:04 +0200342/* Checking functions */
343void dbg_save_space_info(struct ubifs_info *c);
344int dbg_check_space_info(struct ubifs_info *c);
345int dbg_check_lprops(struct ubifs_info *c);
346int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
347int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
348int dbg_check_cats(struct ubifs_info *c);
349int dbg_check_ltab(struct ubifs_info *c);
350int dbg_chk_lpt_free_spc(struct ubifs_info *c);
351int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len);
352int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode);
353int dbg_check_dir(struct ubifs_info *c, const struct inode *dir);
354int dbg_check_tnc(struct ubifs_info *c, int extra);
355int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
356int dbg_check_filesystem(struct ubifs_info *c);
357void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
358 int add_pos);
359int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
360 int row, int col);
361int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode,
362 loff_t size);
363int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head);
364int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100365
Heiko Schocherf5895d12014-06-24 10:10:04 +0200366int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
367 int len);
368int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len);
369int dbg_leb_unmap(struct ubifs_info *c, int lnum);
370int dbg_leb_map(struct ubifs_info *c, int lnum);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100371
Heiko Schocherf5895d12014-06-24 10:10:04 +0200372/* Debugfs-related stuff */
373int dbg_debugfs_init(void);
374void dbg_debugfs_exit(void);
375int dbg_debugfs_init_fs(struct ubifs_info *c);
376void dbg_debugfs_exit_fs(struct ubifs_info *c);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100377
378#endif /* !__UBIFS_DEBUG_H__ */