blob: 33d6b95071e4589f94c17530d0d14320920c47af [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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
23#ifndef __UBIFS_DEBUG_H__
24#define __UBIFS_DEBUG_H__
25
26#ifdef CONFIG_UBIFS_FS_DEBUG
27
28#define UBIFS_DBG(op) op
29
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +030030#define ubifs_assert(expr) do { \
Artem Bityutskiy1e517642008-07-14 19:08:37 +030031 if (unlikely(!(expr))) { \
32 printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
33 __func__, __LINE__, current->pid); \
34 dbg_dump_stack(); \
35 } \
36} while (0)
37
38#define ubifs_assert_cmt_locked(c) do { \
39 if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
40 up_write(&(c)->commit_sem); \
41 printk(KERN_CRIT "commit lock is not locked!\n"); \
42 ubifs_assert(0); \
43 } \
44} while (0)
45
46#define dbg_dump_stack() do { \
47 if (!dbg_failure_mode) \
48 dump_stack(); \
49} while (0)
50
51/* Generic debugging messages */
52#define dbg_msg(fmt, ...) do { \
53 spin_lock(&dbg_lock); \
54 printk(KERN_DEBUG "UBIFS DBG (pid %d): %s: " fmt "\n", current->pid, \
55 __func__, ##__VA_ARGS__); \
56 spin_unlock(&dbg_lock); \
57} while (0)
58
59#define dbg_do_msg(typ, fmt, ...) do { \
60 if (ubifs_msg_flags & typ) \
61 dbg_msg(fmt, ##__VA_ARGS__); \
62} while (0)
63
64#define dbg_err(fmt, ...) do { \
65 spin_lock(&dbg_lock); \
66 ubifs_err(fmt, ##__VA_ARGS__); \
67 spin_unlock(&dbg_lock); \
68} while (0)
69
70const char *dbg_key_str0(const struct ubifs_info *c,
71 const union ubifs_key *key);
72const char *dbg_key_str1(const struct ubifs_info *c,
73 const union ubifs_key *key);
74
75/*
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +030076 * DBGKEY macros require @dbg_lock to be held, which it is in the dbg message
Artem Bityutskiy1e517642008-07-14 19:08:37 +030077 * macros.
78 */
79#define DBGKEY(key) dbg_key_str0(c, (key))
80#define DBGKEY1(key) dbg_key_str1(c, (key))
81
82/* General messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +030083#define dbg_gen(fmt, ...) dbg_do_msg(UBIFS_MSG_GEN, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030084
85/* Additional journal messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +030086#define dbg_jnl(fmt, ...) dbg_do_msg(UBIFS_MSG_JNL, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030087
88/* Additional TNC messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +030089#define dbg_tnc(fmt, ...) dbg_do_msg(UBIFS_MSG_TNC, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030090
91/* Additional lprops messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +030092#define dbg_lp(fmt, ...) dbg_do_msg(UBIFS_MSG_LP, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030093
94/* Additional LEB find messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +030095#define dbg_find(fmt, ...) dbg_do_msg(UBIFS_MSG_FIND, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030096
97/* Additional mount messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +030098#define dbg_mnt(fmt, ...) dbg_do_msg(UBIFS_MSG_MNT, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030099
100/* Additional I/O messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300101#define dbg_io(fmt, ...) dbg_do_msg(UBIFS_MSG_IO, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300102
103/* Additional commit messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300104#define dbg_cmt(fmt, ...) dbg_do_msg(UBIFS_MSG_CMT, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300105
106/* Additional budgeting messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300107#define dbg_budg(fmt, ...) dbg_do_msg(UBIFS_MSG_BUDG, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300108
109/* Additional log messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300110#define dbg_log(fmt, ...) dbg_do_msg(UBIFS_MSG_LOG, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300111
112/* Additional gc messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300113#define dbg_gc(fmt, ...) dbg_do_msg(UBIFS_MSG_GC, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300114
115/* Additional scan messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300116#define dbg_scan(fmt, ...) dbg_do_msg(UBIFS_MSG_SCAN, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300117
118/* Additional recovery messages */
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300119#define dbg_rcvry(fmt, ...) dbg_do_msg(UBIFS_MSG_RCVRY, fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300120
121/*
122 * Debugging message type flags (must match msg_type_names in debug.c).
123 *
124 * UBIFS_MSG_GEN: general messages
125 * UBIFS_MSG_JNL: journal messages
126 * UBIFS_MSG_MNT: mount messages
127 * UBIFS_MSG_CMT: commit messages
128 * UBIFS_MSG_FIND: LEB find messages
129 * UBIFS_MSG_BUDG: budgeting messages
130 * UBIFS_MSG_GC: garbage collection messages
131 * UBIFS_MSG_TNC: TNC messages
132 * UBIFS_MSG_LP: lprops messages
133 * UBIFS_MSG_IO: I/O messages
134 * UBIFS_MSG_LOG: log messages
135 * UBIFS_MSG_SCAN: scan messages
136 * UBIFS_MSG_RCVRY: recovery messages
137 */
138enum {
139 UBIFS_MSG_GEN = 0x1,
140 UBIFS_MSG_JNL = 0x2,
141 UBIFS_MSG_MNT = 0x4,
142 UBIFS_MSG_CMT = 0x8,
143 UBIFS_MSG_FIND = 0x10,
144 UBIFS_MSG_BUDG = 0x20,
145 UBIFS_MSG_GC = 0x40,
146 UBIFS_MSG_TNC = 0x80,
147 UBIFS_MSG_LP = 0x100,
148 UBIFS_MSG_IO = 0x200,
149 UBIFS_MSG_LOG = 0x400,
150 UBIFS_MSG_SCAN = 0x800,
151 UBIFS_MSG_RCVRY = 0x1000,
152};
153
154/* Debugging message type flags for each default debug message level */
155#define UBIFS_MSG_LVL_0 0
156#define UBIFS_MSG_LVL_1 0x1
157#define UBIFS_MSG_LVL_2 0x7f
158#define UBIFS_MSG_LVL_3 0xffff
159
160/*
161 * Debugging check flags (must match chk_names in debug.c).
162 *
163 * UBIFS_CHK_GEN: general checks
164 * UBIFS_CHK_TNC: check TNC
165 * UBIFS_CHK_IDX_SZ: check index size
166 * UBIFS_CHK_ORPH: check orphans
167 * UBIFS_CHK_OLD_IDX: check the old index
168 * UBIFS_CHK_LPROPS: check lprops
169 * UBIFS_CHK_FS: check the file-system
170 */
171enum {
172 UBIFS_CHK_GEN = 0x1,
173 UBIFS_CHK_TNC = 0x2,
174 UBIFS_CHK_IDX_SZ = 0x4,
175 UBIFS_CHK_ORPH = 0x8,
176 UBIFS_CHK_OLD_IDX = 0x10,
177 UBIFS_CHK_LPROPS = 0x20,
178 UBIFS_CHK_FS = 0x40,
179};
180
181/*
182 * Special testing flags (must match tst_names in debug.c).
183 *
184 * UBIFS_TST_FORCE_IN_THE_GAPS: force the use of in-the-gaps method
185 * UBIFS_TST_RCVRY: failure mode for recovery testing
186 */
187enum {
188 UBIFS_TST_FORCE_IN_THE_GAPS = 0x2,
189 UBIFS_TST_RCVRY = 0x4,
190};
191
192#if CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 1
193#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_1
194#elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 2
195#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_2
196#elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 3
197#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_3
198#else
199#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_0
200#endif
201
202#ifdef CONFIG_UBIFS_FS_DEBUG_CHKS
203#define UBIFS_CHK_FLAGS_DEFAULT 0xffffffff
204#else
205#define UBIFS_CHK_FLAGS_DEFAULT 0
206#endif
207
208extern spinlock_t dbg_lock;
209
210extern unsigned int ubifs_msg_flags;
211extern unsigned int ubifs_chk_flags;
212extern unsigned int ubifs_tst_flags;
213
214/* Dump functions */
215
216const char *dbg_ntype(int type);
217const char *dbg_cstate(int cmt_state);
218const char *dbg_get_key_dump(const struct ubifs_info *c,
219 const union ubifs_key *key);
220void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode);
221void dbg_dump_node(const struct ubifs_info *c, const void *node);
222void dbg_dump_budget_req(const struct ubifs_budget_req *req);
223void dbg_dump_lstats(const struct ubifs_lp_stats *lst);
224void dbg_dump_budg(struct ubifs_info *c);
225void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp);
226void dbg_dump_lprops(struct ubifs_info *c);
Adrian Hunter73944a62008-09-12 18:13:31 +0300227void dbg_dump_lpt_info(struct ubifs_info *c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300228void dbg_dump_leb(const struct ubifs_info *c, int lnum);
229void dbg_dump_znode(const struct ubifs_info *c,
230 const struct ubifs_znode *znode);
231void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat);
232void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
233 struct ubifs_nnode *parent, int iip);
234void dbg_dump_tnc(struct ubifs_info *c);
235void dbg_dump_index(struct ubifs_info *c);
236
237/* Checking helper functions */
238
239typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
240 struct ubifs_zbranch *zbr, void *priv);
241typedef int (*dbg_znode_callback)(struct ubifs_info *c,
242 struct ubifs_znode *znode, void *priv);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300243int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
244 dbg_znode_callback znode_cb, void *priv);
245
246/* Checking functions */
247
248int dbg_check_lprops(struct ubifs_info *c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300249int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
250int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300251int dbg_check_cats(struct ubifs_info *c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300252int dbg_check_ltab(struct ubifs_info *c);
Adrian Hunter73944a62008-09-12 18:13:31 +0300253int dbg_chk_lpt_free_spc(struct ubifs_info *c);
254int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300255int dbg_check_synced_i_size(struct inode *inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300256int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300257int dbg_check_tnc(struct ubifs_info *c, int extra);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300258int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300259int dbg_check_filesystem(struct ubifs_info *c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300260void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
261 int add_pos);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300262int dbg_check_lprops(struct ubifs_info *c);
263int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
264 int row, int col);
265
266/* Force the use of in-the-gaps method for testing */
267
268#define dbg_force_in_the_gaps_enabled \
269 (ubifs_tst_flags & UBIFS_TST_FORCE_IN_THE_GAPS)
270
271int dbg_force_in_the_gaps(void);
272
273/* Failure mode for recovery testing */
274
275#define dbg_failure_mode (ubifs_tst_flags & UBIFS_TST_RCVRY)
276
277void dbg_failure_mode_registration(struct ubifs_info *c);
278void dbg_failure_mode_deregistration(struct ubifs_info *c);
279
280#ifndef UBIFS_DBG_PRESERVE_UBI
281
282#define ubi_leb_read dbg_leb_read
283#define ubi_leb_write dbg_leb_write
284#define ubi_leb_change dbg_leb_change
285#define ubi_leb_erase dbg_leb_erase
286#define ubi_leb_unmap dbg_leb_unmap
287#define ubi_is_mapped dbg_is_mapped
288#define ubi_leb_map dbg_leb_map
289
290#endif
291
292int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
293 int len, int check);
294int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
295 int offset, int len, int dtype);
296int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
297 int len, int dtype);
298int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum);
299int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum);
300int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum);
301int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype);
302
303static inline int dbg_read(struct ubi_volume_desc *desc, int lnum, char *buf,
304 int offset, int len)
305{
306 return dbg_leb_read(desc, lnum, buf, offset, len, 0);
307}
308
309static inline int dbg_write(struct ubi_volume_desc *desc, int lnum,
310 const void *buf, int offset, int len)
311{
312 return dbg_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN);
313}
314
315static inline int dbg_change(struct ubi_volume_desc *desc, int lnum,
316 const void *buf, int len)
317{
318 return dbg_leb_change(desc, lnum, buf, len, UBI_UNKNOWN);
319}
320
321#else /* !CONFIG_UBIFS_FS_DEBUG */
322
323#define UBIFS_DBG(op)
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300324
325/* Use "if (0)" to make compiler check arguments even if debugging is off */
326#define ubifs_assert(expr) do { \
327 if (0 && (expr)) \
328 printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
329 __func__, __LINE__, current->pid); \
330} while (0)
331
332#define dbg_err(fmt, ...) do { \
333 if (0) \
334 ubifs_err(fmt, ##__VA_ARGS__); \
335} while (0)
336
337#define dbg_msg(fmt, ...) do { \
338 if (0) \
339 printk(KERN_DEBUG "UBIFS DBG (pid %d): %s: " fmt "\n", \
340 current->pid, __func__, ##__VA_ARGS__); \
341} while (0)
342
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300343#define dbg_dump_stack()
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300344#define ubifs_assert_cmt_locked(c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300345
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300346#define dbg_gen(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
347#define dbg_jnl(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
348#define dbg_tnc(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
349#define dbg_lp(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
350#define dbg_find(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
351#define dbg_mnt(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
352#define dbg_io(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
353#define dbg_cmt(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
354#define dbg_budg(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
355#define dbg_log(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
356#define dbg_gc(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
357#define dbg_scan(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
358#define dbg_rcvry(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300359
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300360#define DBGKEY(key) ((char *)(key))
361#define DBGKEY1(key) ((char *)(key))
362
363#define dbg_ntype(type) ""
364#define dbg_cstate(cmt_state) ""
365#define dbg_get_key_dump(c, key) ({})
366#define dbg_dump_inode(c, inode) ({})
367#define dbg_dump_node(c, node) ({})
368#define dbg_dump_budget_req(req) ({})
369#define dbg_dump_lstats(lst) ({})
370#define dbg_dump_budg(c) ({})
371#define dbg_dump_lprop(c, lp) ({})
372#define dbg_dump_lprops(c) ({})
Adrian Hunter73944a62008-09-12 18:13:31 +0300373#define dbg_dump_lpt_info(c) ({})
Artem Bityutskiy840dc6b2008-08-01 18:13:37 +0300374#define dbg_dump_leb(c, lnum) ({})
375#define dbg_dump_znode(c, znode) ({})
376#define dbg_dump_heap(c, heap, cat) ({})
377#define dbg_dump_pnode(c, pnode, parent, iip) ({})
378#define dbg_dump_tnc(c) ({})
379#define dbg_dump_index(c) ({})
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300380
381#define dbg_walk_index(c, leaf_cb, znode_cb, priv) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300382#define dbg_old_index_check_init(c, zroot) 0
383#define dbg_check_old_index(c, zroot) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300384#define dbg_check_cats(c) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300385#define dbg_check_ltab(c) 0
Adrian Hunter73944a62008-09-12 18:13:31 +0300386#define dbg_chk_lpt_free_spc(c) 0
387#define dbg_chk_lpt_sz(c, action, len) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300388#define dbg_check_synced_i_size(inode) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300389#define dbg_check_dir_size(c, dir) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300390#define dbg_check_tnc(c, x) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300391#define dbg_check_idx_size(c, idx_size) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300392#define dbg_check_filesystem(c) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300393#define dbg_check_heap(c, heap, cat, add_pos) ({})
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300394#define dbg_check_lprops(c) 0
395#define dbg_check_lpt_nodes(c, cnode, row, col) 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300396#define dbg_force_in_the_gaps_enabled 0
397#define dbg_force_in_the_gaps() 0
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300398#define dbg_failure_mode 0
399#define dbg_failure_mode_registration(c) ({})
400#define dbg_failure_mode_deregistration(c) ({})
401
402#endif /* !CONFIG_UBIFS_FS_DEBUG */
403
404#endif /* !__UBIFS_DEBUG_H__ */