blob: 61af9bb560ab6777cd90ea5125790f38ee6139a9 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём)
19 */
20
21/*
22 * Here we keep all the UBI debugging stuff which should normally be disabled
23 * and compiled-out, but it is extremely helpful when hunting bugs or doing big
24 * changes.
25 */
26
Artem Bityutskiyc8566352008-07-16 17:40:22 +030027#ifdef CONFIG_MTD_UBI_DEBUG
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040028
29#include "ubi.h"
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +030030#include <linux/debugfs.h>
31#include <linux/uaccess.h>
Artem Bityutskiyb342efd2011-03-11 14:33:23 +020032#include <linux/module.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040033
34/**
35 * ubi_dbg_dump_ec_hdr - dump an erase counter header.
36 * @ec_hdr: the erase counter header to dump
37 */
38void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
39{
Artem Bityutskiyc8566352008-07-16 17:40:22 +030040 printk(KERN_DEBUG "Erase counter header dump:\n");
41 printk(KERN_DEBUG "\tmagic %#08x\n",
42 be32_to_cpu(ec_hdr->magic));
43 printk(KERN_DEBUG "\tversion %d\n", (int)ec_hdr->version);
44 printk(KERN_DEBUG "\tec %llu\n",
45 (long long)be64_to_cpu(ec_hdr->ec));
46 printk(KERN_DEBUG "\tvid_hdr_offset %d\n",
47 be32_to_cpu(ec_hdr->vid_hdr_offset));
48 printk(KERN_DEBUG "\tdata_offset %d\n",
49 be32_to_cpu(ec_hdr->data_offset));
Adrian Hunter0c6c7fa2009-06-26 14:58:01 +030050 printk(KERN_DEBUG "\timage_seq %d\n",
51 be32_to_cpu(ec_hdr->image_seq));
Artem Bityutskiyc8566352008-07-16 17:40:22 +030052 printk(KERN_DEBUG "\thdr_crc %#08x\n",
53 be32_to_cpu(ec_hdr->hdr_crc));
54 printk(KERN_DEBUG "erase counter header hexdump:\n");
Artem Bityutskiy69866462007-08-29 14:56:20 +030055 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
56 ec_hdr, UBI_EC_HDR_SIZE, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040057}
58
59/**
60 * ubi_dbg_dump_vid_hdr - dump a volume identifier header.
61 * @vid_hdr: the volume identifier header to dump
62 */
63void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
64{
Artem Bityutskiyc8566352008-07-16 17:40:22 +030065 printk(KERN_DEBUG "Volume identifier header dump:\n");
66 printk(KERN_DEBUG "\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +030067 printk(KERN_DEBUG "\tversion %d\n", (int)vid_hdr->version);
68 printk(KERN_DEBUG "\tvol_type %d\n", (int)vid_hdr->vol_type);
69 printk(KERN_DEBUG "\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
70 printk(KERN_DEBUG "\tcompat %d\n", (int)vid_hdr->compat);
71 printk(KERN_DEBUG "\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
72 printk(KERN_DEBUG "\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
73 printk(KERN_DEBUG "\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
74 printk(KERN_DEBUG "\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
75 printk(KERN_DEBUG "\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
Artem Bityutskiyc8566352008-07-16 17:40:22 +030076 printk(KERN_DEBUG "\tsqnum %llu\n",
Christoph Hellwig3261ebd2007-05-21 17:41:46 +030077 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
Artem Bityutskiyc8566352008-07-16 17:40:22 +030078 printk(KERN_DEBUG "\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
79 printk(KERN_DEBUG "Volume identifier header hexdump:\n");
80 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
81 vid_hdr, UBI_VID_HDR_SIZE, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040082}
83
84/**
85 * ubi_dbg_dump_vol_info- dump volume information.
86 * @vol: UBI volume description object
87 */
88void ubi_dbg_dump_vol_info(const struct ubi_volume *vol)
89{
Artem Bityutskiyc8566352008-07-16 17:40:22 +030090 printk(KERN_DEBUG "Volume information dump:\n");
91 printk(KERN_DEBUG "\tvol_id %d\n", vol->vol_id);
92 printk(KERN_DEBUG "\treserved_pebs %d\n", vol->reserved_pebs);
93 printk(KERN_DEBUG "\talignment %d\n", vol->alignment);
94 printk(KERN_DEBUG "\tdata_pad %d\n", vol->data_pad);
95 printk(KERN_DEBUG "\tvol_type %d\n", vol->vol_type);
96 printk(KERN_DEBUG "\tname_len %d\n", vol->name_len);
97 printk(KERN_DEBUG "\tusable_leb_size %d\n", vol->usable_leb_size);
98 printk(KERN_DEBUG "\tused_ebs %d\n", vol->used_ebs);
99 printk(KERN_DEBUG "\tused_bytes %lld\n", vol->used_bytes);
100 printk(KERN_DEBUG "\tlast_eb_bytes %d\n", vol->last_eb_bytes);
101 printk(KERN_DEBUG "\tcorrupted %d\n", vol->corrupted);
102 printk(KERN_DEBUG "\tupd_marker %d\n", vol->upd_marker);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400103
104 if (vol->name_len <= UBI_VOL_NAME_MAX &&
105 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300106 printk(KERN_DEBUG "\tname %s\n", vol->name);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400107 } else {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300108 printk(KERN_DEBUG "\t1st 5 characters of name: %c%c%c%c%c\n",
109 vol->name[0], vol->name[1], vol->name[2],
110 vol->name[3], vol->name[4]);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400111 }
112}
113
114/**
115 * ubi_dbg_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
116 * @r: the object to dump
117 * @idx: volume table index
118 */
119void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
120{
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300121 int name_len = be16_to_cpu(r->name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400122
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300123 printk(KERN_DEBUG "Volume table record %d dump:\n", idx);
124 printk(KERN_DEBUG "\treserved_pebs %d\n",
125 be32_to_cpu(r->reserved_pebs));
126 printk(KERN_DEBUG "\talignment %d\n", be32_to_cpu(r->alignment));
127 printk(KERN_DEBUG "\tdata_pad %d\n", be32_to_cpu(r->data_pad));
128 printk(KERN_DEBUG "\tvol_type %d\n", (int)r->vol_type);
129 printk(KERN_DEBUG "\tupd_marker %d\n", (int)r->upd_marker);
130 printk(KERN_DEBUG "\tname_len %d\n", name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400131
132 if (r->name[0] == '\0') {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300133 printk(KERN_DEBUG "\tname NULL\n");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400134 return;
135 }
136
137 if (name_len <= UBI_VOL_NAME_MAX &&
138 strnlen(&r->name[0], name_len + 1) == name_len) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300139 printk(KERN_DEBUG "\tname %s\n", &r->name[0]);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400140 } else {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300141 printk(KERN_DEBUG "\t1st 5 characters of name: %c%c%c%c%c\n",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400142 r->name[0], r->name[1], r->name[2], r->name[3],
143 r->name[4]);
144 }
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300145 printk(KERN_DEBUG "\tcrc %#08x\n", be32_to_cpu(r->crc));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400146}
147
148/**
149 * ubi_dbg_dump_sv - dump a &struct ubi_scan_volume object.
150 * @sv: the object to dump
151 */
152void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv)
153{
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300154 printk(KERN_DEBUG "Volume scanning information dump:\n");
155 printk(KERN_DEBUG "\tvol_id %d\n", sv->vol_id);
156 printk(KERN_DEBUG "\thighest_lnum %d\n", sv->highest_lnum);
157 printk(KERN_DEBUG "\tleb_count %d\n", sv->leb_count);
158 printk(KERN_DEBUG "\tcompat %d\n", sv->compat);
159 printk(KERN_DEBUG "\tvol_type %d\n", sv->vol_type);
160 printk(KERN_DEBUG "\tused_ebs %d\n", sv->used_ebs);
161 printk(KERN_DEBUG "\tlast_data_size %d\n", sv->last_data_size);
162 printk(KERN_DEBUG "\tdata_pad %d\n", sv->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400163}
164
165/**
166 * ubi_dbg_dump_seb - dump a &struct ubi_scan_leb object.
167 * @seb: the object to dump
168 * @type: object type: 0 - not corrupted, 1 - corrupted
169 */
170void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type)
171{
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300172 printk(KERN_DEBUG "eraseblock scanning information dump:\n");
173 printk(KERN_DEBUG "\tec %d\n", seb->ec);
174 printk(KERN_DEBUG "\tpnum %d\n", seb->pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400175 if (type == 0) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300176 printk(KERN_DEBUG "\tlnum %d\n", seb->lnum);
177 printk(KERN_DEBUG "\tscrub %d\n", seb->scrub);
178 printk(KERN_DEBUG "\tsqnum %llu\n", seb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400179 }
180}
181
182/**
183 * ubi_dbg_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
184 * @req: the object to dump
185 */
186void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req)
187{
188 char nm[17];
189
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300190 printk(KERN_DEBUG "Volume creation request dump:\n");
191 printk(KERN_DEBUG "\tvol_id %d\n", req->vol_id);
192 printk(KERN_DEBUG "\talignment %d\n", req->alignment);
193 printk(KERN_DEBUG "\tbytes %lld\n", (long long)req->bytes);
194 printk(KERN_DEBUG "\tvol_type %d\n", req->vol_type);
195 printk(KERN_DEBUG "\tname_len %d\n", req->name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400196
197 memcpy(nm, req->name, 16);
198 nm[16] = 0;
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300199 printk(KERN_DEBUG "\t1st 16 characters of name: %s\n", nm);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400200}
201
Artem Bityutskiy867996b2009-07-24 15:31:33 +0300202/**
203 * ubi_dbg_dump_flash - dump a region of flash.
204 * @ubi: UBI device description object
205 * @pnum: the physical eraseblock number to dump
206 * @offset: the starting offset within the physical eraseblock to dump
207 * @len: the length of the region to dump
208 */
209void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
210{
211 int err;
212 size_t read;
213 void *buf;
214 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
215
216 buf = vmalloc(len);
217 if (!buf)
218 return;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200219 err = mtd_read(ubi->mtd, addr, len, &read, buf);
Artem Bityutskiy867996b2009-07-24 15:31:33 +0300220 if (err && err != -EUCLEAN) {
221 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
222 "read %zd bytes", err, len, pnum, offset, read);
223 goto out;
224 }
225
226 dbg_msg("dumping %d bytes of data from PEB %d, offset %d",
227 len, pnum, offset);
228 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
229out:
230 vfree(buf);
231 return;
232}
233
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300234/**
235 * ubi_debugging_init_dev - initialize debugging for an UBI device.
236 * @ubi: UBI device description object
237 *
238 * This function initializes debugging-related data for UBI device @ubi.
239 * Returns zero in case of success and a negative error code in case of
240 * failure.
241 */
242int ubi_debugging_init_dev(struct ubi_device *ubi)
243{
244 ubi->dbg = kzalloc(sizeof(struct ubi_debug_info), GFP_KERNEL);
245 if (!ubi->dbg)
246 return -ENOMEM;
247
248 return 0;
249}
250
251/**
252 * ubi_debugging_exit_dev - free debugging data for an UBI device.
253 * @ubi: UBI device description object
254 */
255void ubi_debugging_exit_dev(struct ubi_device *ubi)
256{
257 kfree(ubi->dbg);
258}
259
260/*
261 * Root directory for UBI stuff in debugfs. Contains sub-directories which
262 * contain the stuff specific to particular UBI devices.
263 */
264static struct dentry *dfs_rootdir;
265
266/**
267 * ubi_debugfs_init - create UBI debugfs directory.
268 *
269 * Create UBI debugfs directory. Returns zero in case of success and a negative
270 * error code in case of failure.
271 */
272int ubi_debugfs_init(void)
273{
274 dfs_rootdir = debugfs_create_dir("ubi", NULL);
275 if (IS_ERR_OR_NULL(dfs_rootdir)) {
276 int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
277
278 ubi_err("cannot create \"ubi\" debugfs directory, error %d\n",
279 err);
280 return err;
281 }
282
283 return 0;
284}
285
286/**
287 * ubi_debugfs_exit - remove UBI debugfs directory.
288 */
289void ubi_debugfs_exit(void)
290{
291 debugfs_remove(dfs_rootdir);
292}
293
294/* Read an UBI debugfs file */
295static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
296 size_t count, loff_t *ppos)
297{
298 unsigned long ubi_num = (unsigned long)file->private_data;
299 struct dentry *dent = file->f_path.dentry;
300 struct ubi_device *ubi;
301 struct ubi_debug_info *d;
302 char buf[3];
303 int val;
304
305 ubi = ubi_get_device(ubi_num);
306 if (!ubi)
307 return -ENODEV;
308 d = ubi->dbg;
309
310 if (dent == d->dfs_chk_gen)
311 val = d->chk_gen;
312 else if (dent == d->dfs_chk_io)
313 val = d->chk_io;
Artem Bityutskiycd6d8562011-05-18 16:21:43 +0300314 else if (dent == d->dfs_disable_bgt)
315 val = d->disable_bgt;
316 else if (dent == d->dfs_emulate_bitflips)
317 val = d->emulate_bitflips;
318 else if (dent == d->dfs_emulate_io_failures)
319 val = d->emulate_io_failures;
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300320 else {
321 count = -EINVAL;
322 goto out;
323 }
324
325 if (val)
326 buf[0] = '1';
327 else
328 buf[0] = '0';
329 buf[1] = '\n';
330 buf[2] = 0x00;
331
332 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
333
334out:
335 ubi_put_device(ubi);
336 return count;
337}
338
339/* Write an UBI debugfs file */
340static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
341 size_t count, loff_t *ppos)
342{
343 unsigned long ubi_num = (unsigned long)file->private_data;
344 struct dentry *dent = file->f_path.dentry;
345 struct ubi_device *ubi;
346 struct ubi_debug_info *d;
347 size_t buf_size;
348 char buf[8];
349 int val;
350
351 ubi = ubi_get_device(ubi_num);
352 if (!ubi)
353 return -ENODEV;
354 d = ubi->dbg;
355
356 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
357 if (copy_from_user(buf, user_buf, buf_size)) {
358 count = -EFAULT;
359 goto out;
360 }
361
362 if (buf[0] == '1')
363 val = 1;
364 else if (buf[0] == '0')
365 val = 0;
366 else {
367 count = -EINVAL;
368 goto out;
369 }
370
371 if (dent == d->dfs_chk_gen)
372 d->chk_gen = val;
373 else if (dent == d->dfs_chk_io)
374 d->chk_io = val;
Artem Bityutskiycd6d8562011-05-18 16:21:43 +0300375 else if (dent == d->dfs_disable_bgt)
376 d->disable_bgt = val;
377 else if (dent == d->dfs_emulate_bitflips)
378 d->emulate_bitflips = val;
379 else if (dent == d->dfs_emulate_io_failures)
380 d->emulate_io_failures = val;
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300381 else
382 count = -EINVAL;
383
384out:
385 ubi_put_device(ubi);
386 return count;
387}
388
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300389/* File operations for all UBI debugfs files */
390static const struct file_operations dfs_fops = {
391 .read = dfs_file_read,
392 .write = dfs_file_write,
Stephen Boyd234e3402012-04-05 14:25:11 -0700393 .open = simple_open,
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300394 .llseek = no_llseek,
395 .owner = THIS_MODULE,
396};
397
398/**
399 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
400 * @ubi: UBI device description object
401 *
402 * This function creates all debugfs files for UBI device @ubi. Returns zero in
403 * case of success and a negative error code in case of failure.
404 */
405int ubi_debugfs_init_dev(struct ubi_device *ubi)
406{
407 int err, n;
408 unsigned long ubi_num = ubi->ubi_num;
409 const char *fname;
410 struct dentry *dent;
411 struct ubi_debug_info *d = ubi->dbg;
412
413 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
414 ubi->ubi_num);
415 if (n == UBI_DFS_DIR_LEN) {
416 /* The array size is too small */
417 fname = UBI_DFS_DIR_NAME;
418 dent = ERR_PTR(-EINVAL);
419 goto out;
420 }
421
422 fname = d->dfs_dir_name;
423 dent = debugfs_create_dir(fname, dfs_rootdir);
424 if (IS_ERR_OR_NULL(dent))
425 goto out;
426 d->dfs_dir = dent;
427
428 fname = "chk_gen";
429 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
430 &dfs_fops);
431 if (IS_ERR_OR_NULL(dent))
432 goto out_remove;
433 d->dfs_chk_gen = dent;
434
435 fname = "chk_io";
436 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
437 &dfs_fops);
438 if (IS_ERR_OR_NULL(dent))
439 goto out_remove;
440 d->dfs_chk_io = dent;
441
Artem Bityutskiycd6d8562011-05-18 16:21:43 +0300442 fname = "tst_disable_bgt";
443 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
444 &dfs_fops);
445 if (IS_ERR_OR_NULL(dent))
446 goto out_remove;
447 d->dfs_disable_bgt = dent;
448
449 fname = "tst_emulate_bitflips";
450 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
451 &dfs_fops);
452 if (IS_ERR_OR_NULL(dent))
453 goto out_remove;
454 d->dfs_emulate_bitflips = dent;
455
456 fname = "tst_emulate_io_failures";
457 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
458 &dfs_fops);
459 if (IS_ERR_OR_NULL(dent))
460 goto out_remove;
461 d->dfs_emulate_io_failures = dent;
462
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300463 return 0;
464
465out_remove:
466 debugfs_remove_recursive(d->dfs_dir);
467out:
468 err = dent ? PTR_ERR(dent) : -ENODEV;
469 ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n",
470 fname, err);
471 return err;
472}
473
474/**
475 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
476 * @ubi: UBI device description object
477 */
478void ubi_debugfs_exit_dev(struct ubi_device *ubi)
479{
480 debugfs_remove_recursive(ubi->dbg->dfs_dir);
481}
482
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300483#endif /* CONFIG_MTD_UBI_DEBUG */