blob: 6afb8835a58e480e92f977bda8725dfd77154a77 [file] [log] [blame]
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001/*
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/*
24 * This file implements most of the debugging stuff which is compiled in only
25 * when it is enabled. But some debugging check functions are implemented in
26 * corresponding subsystem, just because they are closely related and utilize
27 * various local functions of those subsystems.
28 */
29
30#define UBIFS_DBG_PRESERVE_UBI
31
32#include "ubifs.h"
33
34#ifdef CONFIG_UBIFS_FS_DEBUG
35
36DEFINE_SPINLOCK(dbg_lock);
37
38static char dbg_key_buf0[128];
39static char dbg_key_buf1[128];
40
41unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
42unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
43unsigned int ubifs_tst_flags;
44
45module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
46module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
47module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
48
49MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
50MODULE_PARM_DESC(debug_chks, "Debug check flags");
51MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
52
53static const char *get_key_type(int type)
54{
55 switch (type) {
56 case UBIFS_INO_KEY:
57 return "inode";
58 case UBIFS_DENT_KEY:
59 return "direntry";
60 case UBIFS_XENT_KEY:
61 return "xentry";
62 case UBIFS_DATA_KEY:
63 return "data";
64 case UBIFS_TRUN_KEY:
65 return "truncate";
66 default:
67 return "unknown/invalid key";
68 }
69}
70
71static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
72 char *buffer)
73{
74 char *p = buffer;
75 int type = key_type(c, key);
76
77 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
78 switch (type) {
79 case UBIFS_INO_KEY:
80 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
81 get_key_type(type));
82 break;
83 case UBIFS_DENT_KEY:
84 case UBIFS_XENT_KEY:
85 sprintf(p, "(%lu, %s, %#08x)",
86 (unsigned long)key_inum(c, key),
87 get_key_type(type), key_hash(c, key));
88 break;
89 case UBIFS_DATA_KEY:
90 sprintf(p, "(%lu, %s, %u)",
91 (unsigned long)key_inum(c, key),
92 get_key_type(type), key_block(c, key));
93 break;
94 case UBIFS_TRUN_KEY:
95 sprintf(p, "(%lu, %s)",
96 (unsigned long)key_inum(c, key),
97 get_key_type(type));
98 break;
99 default:
100 sprintf(p, "(bad key type: %#08x, %#08x)",
101 key->u32[0], key->u32[1]);
102 }
103 } else
104 sprintf(p, "bad key format %d", c->key_fmt);
105}
106
107const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
108{
109 /* dbg_lock must be held */
110 sprintf_key(c, key, dbg_key_buf0);
111 return dbg_key_buf0;
112}
113
114const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
115{
116 /* dbg_lock must be held */
117 sprintf_key(c, key, dbg_key_buf1);
118 return dbg_key_buf1;
119}
120
121/**
122 * ubifs_debugging_init - initialize UBIFS debugging.
123 * @c: UBIFS file-system description object
124 *
125 * This function initializes debugging-related data for the file system.
126 * Returns zero in case of success and a negative error code in case of
127 * failure.
128 */
129int ubifs_debugging_init(struct ubifs_info *c)
130{
131 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
132 if (!c->dbg)
133 return -ENOMEM;
134
135 c->dbg->buf = vmalloc(c->leb_size);
136 if (!c->dbg->buf)
137 goto out;
138
139 return 0;
140
141out:
142 kfree(c->dbg);
143 return -ENOMEM;
144}
145
146/**
147 * ubifs_debugging_exit - free debugging data.
148 * @c: UBIFS file-system description object
149 */
150void ubifs_debugging_exit(struct ubifs_info *c)
151{
152 vfree(c->dbg->buf);
153 kfree(c->dbg);
154}
155
156#endif /* CONFIG_UBIFS_FS_DEBUG */