blob: 30aa9c440834eef4282363f6876211d32c390a52 [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
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040021#include "ubi.h"
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +030022#include <linux/debugfs.h>
23#include <linux/uaccess.h>
Artem Bityutskiyb342efd2011-03-11 14:33:23 +020024#include <linux/module.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040025
Artem Bityutskiyef7088e2012-04-24 07:10:33 +030026
27/**
28 * ubi_dump_flash - dump a region of flash.
29 * @ubi: UBI device description object
30 * @pnum: the physical eraseblock number to dump
31 * @offset: the starting offset within the physical eraseblock to dump
32 * @len: the length of the region to dump
33 */
34void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
35{
36 int err;
37 size_t read;
38 void *buf;
39 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
40
41 buf = vmalloc(len);
42 if (!buf)
43 return;
44 err = mtd_read(ubi->mtd, addr, len, &read, buf);
45 if (err && err != -EUCLEAN) {
46 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
47 "read %zd bytes", err, len, pnum, offset, read);
48 goto out;
49 }
50
51 ubi_msg("dumping %d bytes of data from PEB %d, offset %d",
52 len, pnum, offset);
53 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
54out:
55 vfree(buf);
56 return;
57}
58
59#ifdef CONFIG_MTD_UBI_DEBUG
60
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040061/**
62 * ubi_dbg_dump_ec_hdr - dump an erase counter header.
63 * @ec_hdr: the erase counter header to dump
64 */
65void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
66{
Artem Bityutskiyc8566352008-07-16 17:40:22 +030067 printk(KERN_DEBUG "Erase counter header dump:\n");
68 printk(KERN_DEBUG "\tmagic %#08x\n",
69 be32_to_cpu(ec_hdr->magic));
70 printk(KERN_DEBUG "\tversion %d\n", (int)ec_hdr->version);
71 printk(KERN_DEBUG "\tec %llu\n",
72 (long long)be64_to_cpu(ec_hdr->ec));
73 printk(KERN_DEBUG "\tvid_hdr_offset %d\n",
74 be32_to_cpu(ec_hdr->vid_hdr_offset));
75 printk(KERN_DEBUG "\tdata_offset %d\n",
76 be32_to_cpu(ec_hdr->data_offset));
Adrian Hunter0c6c7fa2009-06-26 14:58:01 +030077 printk(KERN_DEBUG "\timage_seq %d\n",
78 be32_to_cpu(ec_hdr->image_seq));
Artem Bityutskiyc8566352008-07-16 17:40:22 +030079 printk(KERN_DEBUG "\thdr_crc %#08x\n",
80 be32_to_cpu(ec_hdr->hdr_crc));
81 printk(KERN_DEBUG "erase counter header hexdump:\n");
Artem Bityutskiy69866462007-08-29 14:56:20 +030082 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
83 ec_hdr, UBI_EC_HDR_SIZE, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040084}
85
86/**
87 * ubi_dbg_dump_vid_hdr - dump a volume identifier header.
88 * @vid_hdr: the volume identifier header to dump
89 */
90void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
91{
Artem Bityutskiyc8566352008-07-16 17:40:22 +030092 printk(KERN_DEBUG "Volume identifier header dump:\n");
93 printk(KERN_DEBUG "\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +030094 printk(KERN_DEBUG "\tversion %d\n", (int)vid_hdr->version);
95 printk(KERN_DEBUG "\tvol_type %d\n", (int)vid_hdr->vol_type);
96 printk(KERN_DEBUG "\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
97 printk(KERN_DEBUG "\tcompat %d\n", (int)vid_hdr->compat);
98 printk(KERN_DEBUG "\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
99 printk(KERN_DEBUG "\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
100 printk(KERN_DEBUG "\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
101 printk(KERN_DEBUG "\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
102 printk(KERN_DEBUG "\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300103 printk(KERN_DEBUG "\tsqnum %llu\n",
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300104 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300105 printk(KERN_DEBUG "\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
106 printk(KERN_DEBUG "Volume identifier header hexdump:\n");
107 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
108 vid_hdr, UBI_VID_HDR_SIZE, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400109}
110
111/**
112 * ubi_dbg_dump_vol_info- dump volume information.
113 * @vol: UBI volume description object
114 */
115void ubi_dbg_dump_vol_info(const struct ubi_volume *vol)
116{
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300117 printk(KERN_DEBUG "Volume information dump:\n");
118 printk(KERN_DEBUG "\tvol_id %d\n", vol->vol_id);
119 printk(KERN_DEBUG "\treserved_pebs %d\n", vol->reserved_pebs);
120 printk(KERN_DEBUG "\talignment %d\n", vol->alignment);
121 printk(KERN_DEBUG "\tdata_pad %d\n", vol->data_pad);
122 printk(KERN_DEBUG "\tvol_type %d\n", vol->vol_type);
123 printk(KERN_DEBUG "\tname_len %d\n", vol->name_len);
124 printk(KERN_DEBUG "\tusable_leb_size %d\n", vol->usable_leb_size);
125 printk(KERN_DEBUG "\tused_ebs %d\n", vol->used_ebs);
126 printk(KERN_DEBUG "\tused_bytes %lld\n", vol->used_bytes);
127 printk(KERN_DEBUG "\tlast_eb_bytes %d\n", vol->last_eb_bytes);
128 printk(KERN_DEBUG "\tcorrupted %d\n", vol->corrupted);
129 printk(KERN_DEBUG "\tupd_marker %d\n", vol->upd_marker);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400130
131 if (vol->name_len <= UBI_VOL_NAME_MAX &&
132 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300133 printk(KERN_DEBUG "\tname %s\n", vol->name);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400134 } else {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300135 printk(KERN_DEBUG "\t1st 5 characters of name: %c%c%c%c%c\n",
136 vol->name[0], vol->name[1], vol->name[2],
137 vol->name[3], vol->name[4]);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400138 }
139}
140
141/**
142 * ubi_dbg_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
143 * @r: the object to dump
144 * @idx: volume table index
145 */
146void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
147{
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300148 int name_len = be16_to_cpu(r->name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400149
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300150 printk(KERN_DEBUG "Volume table record %d dump:\n", idx);
151 printk(KERN_DEBUG "\treserved_pebs %d\n",
152 be32_to_cpu(r->reserved_pebs));
153 printk(KERN_DEBUG "\talignment %d\n", be32_to_cpu(r->alignment));
154 printk(KERN_DEBUG "\tdata_pad %d\n", be32_to_cpu(r->data_pad));
155 printk(KERN_DEBUG "\tvol_type %d\n", (int)r->vol_type);
156 printk(KERN_DEBUG "\tupd_marker %d\n", (int)r->upd_marker);
157 printk(KERN_DEBUG "\tname_len %d\n", name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400158
159 if (r->name[0] == '\0') {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300160 printk(KERN_DEBUG "\tname NULL\n");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400161 return;
162 }
163
164 if (name_len <= UBI_VOL_NAME_MAX &&
165 strnlen(&r->name[0], name_len + 1) == name_len) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300166 printk(KERN_DEBUG "\tname %s\n", &r->name[0]);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400167 } else {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300168 printk(KERN_DEBUG "\t1st 5 characters of name: %c%c%c%c%c\n",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400169 r->name[0], r->name[1], r->name[2], r->name[3],
170 r->name[4]);
171 }
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300172 printk(KERN_DEBUG "\tcrc %#08x\n", be32_to_cpu(r->crc));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400173}
174
175/**
176 * ubi_dbg_dump_sv - dump a &struct ubi_scan_volume object.
177 * @sv: the object to dump
178 */
179void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv)
180{
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300181 printk(KERN_DEBUG "Volume scanning information dump:\n");
182 printk(KERN_DEBUG "\tvol_id %d\n", sv->vol_id);
183 printk(KERN_DEBUG "\thighest_lnum %d\n", sv->highest_lnum);
184 printk(KERN_DEBUG "\tleb_count %d\n", sv->leb_count);
185 printk(KERN_DEBUG "\tcompat %d\n", sv->compat);
186 printk(KERN_DEBUG "\tvol_type %d\n", sv->vol_type);
187 printk(KERN_DEBUG "\tused_ebs %d\n", sv->used_ebs);
188 printk(KERN_DEBUG "\tlast_data_size %d\n", sv->last_data_size);
189 printk(KERN_DEBUG "\tdata_pad %d\n", sv->data_pad);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400190}
191
192/**
193 * ubi_dbg_dump_seb - dump a &struct ubi_scan_leb object.
194 * @seb: the object to dump
195 * @type: object type: 0 - not corrupted, 1 - corrupted
196 */
197void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type)
198{
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300199 printk(KERN_DEBUG "eraseblock scanning information dump:\n");
200 printk(KERN_DEBUG "\tec %d\n", seb->ec);
201 printk(KERN_DEBUG "\tpnum %d\n", seb->pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400202 if (type == 0) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300203 printk(KERN_DEBUG "\tlnum %d\n", seb->lnum);
204 printk(KERN_DEBUG "\tscrub %d\n", seb->scrub);
205 printk(KERN_DEBUG "\tsqnum %llu\n", seb->sqnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400206 }
207}
208
209/**
210 * ubi_dbg_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
211 * @req: the object to dump
212 */
213void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req)
214{
215 char nm[17];
216
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300217 printk(KERN_DEBUG "Volume creation request dump:\n");
218 printk(KERN_DEBUG "\tvol_id %d\n", req->vol_id);
219 printk(KERN_DEBUG "\talignment %d\n", req->alignment);
220 printk(KERN_DEBUG "\tbytes %lld\n", (long long)req->bytes);
221 printk(KERN_DEBUG "\tvol_type %d\n", req->vol_type);
222 printk(KERN_DEBUG "\tname_len %d\n", req->name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400223
224 memcpy(nm, req->name, 16);
225 nm[16] = 0;
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300226 printk(KERN_DEBUG "\t1st 16 characters of name: %s\n", nm);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400227}
228
Artem Bityutskiy867996b2009-07-24 15:31:33 +0300229/**
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300230 * ubi_debugging_init_dev - initialize debugging for an UBI device.
231 * @ubi: UBI device description object
232 *
233 * This function initializes debugging-related data for UBI device @ubi.
234 * Returns zero in case of success and a negative error code in case of
235 * failure.
236 */
237int ubi_debugging_init_dev(struct ubi_device *ubi)
238{
239 ubi->dbg = kzalloc(sizeof(struct ubi_debug_info), GFP_KERNEL);
240 if (!ubi->dbg)
241 return -ENOMEM;
242
243 return 0;
244}
245
246/**
247 * ubi_debugging_exit_dev - free debugging data for an UBI device.
248 * @ubi: UBI device description object
249 */
250void ubi_debugging_exit_dev(struct ubi_device *ubi)
251{
252 kfree(ubi->dbg);
253}
254
255/*
256 * Root directory for UBI stuff in debugfs. Contains sub-directories which
257 * contain the stuff specific to particular UBI devices.
258 */
259static struct dentry *dfs_rootdir;
260
261/**
262 * ubi_debugfs_init - create UBI debugfs directory.
263 *
264 * Create UBI debugfs directory. Returns zero in case of success and a negative
265 * error code in case of failure.
266 */
267int ubi_debugfs_init(void)
268{
269 dfs_rootdir = debugfs_create_dir("ubi", NULL);
270 if (IS_ERR_OR_NULL(dfs_rootdir)) {
271 int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
272
273 ubi_err("cannot create \"ubi\" debugfs directory, error %d\n",
274 err);
275 return err;
276 }
277
278 return 0;
279}
280
281/**
282 * ubi_debugfs_exit - remove UBI debugfs directory.
283 */
284void ubi_debugfs_exit(void)
285{
286 debugfs_remove(dfs_rootdir);
287}
288
289/* Read an UBI debugfs file */
290static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
291 size_t count, loff_t *ppos)
292{
293 unsigned long ubi_num = (unsigned long)file->private_data;
294 struct dentry *dent = file->f_path.dentry;
295 struct ubi_device *ubi;
296 struct ubi_debug_info *d;
297 char buf[3];
298 int val;
299
300 ubi = ubi_get_device(ubi_num);
301 if (!ubi)
302 return -ENODEV;
303 d = ubi->dbg;
304
305 if (dent == d->dfs_chk_gen)
306 val = d->chk_gen;
307 else if (dent == d->dfs_chk_io)
308 val = d->chk_io;
Artem Bityutskiycd6d8562011-05-18 16:21:43 +0300309 else if (dent == d->dfs_disable_bgt)
310 val = d->disable_bgt;
311 else if (dent == d->dfs_emulate_bitflips)
312 val = d->emulate_bitflips;
313 else if (dent == d->dfs_emulate_io_failures)
314 val = d->emulate_io_failures;
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300315 else {
316 count = -EINVAL;
317 goto out;
318 }
319
320 if (val)
321 buf[0] = '1';
322 else
323 buf[0] = '0';
324 buf[1] = '\n';
325 buf[2] = 0x00;
326
327 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
328
329out:
330 ubi_put_device(ubi);
331 return count;
332}
333
334/* Write an UBI debugfs file */
335static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
336 size_t count, loff_t *ppos)
337{
338 unsigned long ubi_num = (unsigned long)file->private_data;
339 struct dentry *dent = file->f_path.dentry;
340 struct ubi_device *ubi;
341 struct ubi_debug_info *d;
342 size_t buf_size;
343 char buf[8];
344 int val;
345
346 ubi = ubi_get_device(ubi_num);
347 if (!ubi)
348 return -ENODEV;
349 d = ubi->dbg;
350
351 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
352 if (copy_from_user(buf, user_buf, buf_size)) {
353 count = -EFAULT;
354 goto out;
355 }
356
357 if (buf[0] == '1')
358 val = 1;
359 else if (buf[0] == '0')
360 val = 0;
361 else {
362 count = -EINVAL;
363 goto out;
364 }
365
366 if (dent == d->dfs_chk_gen)
367 d->chk_gen = val;
368 else if (dent == d->dfs_chk_io)
369 d->chk_io = val;
Artem Bityutskiycd6d8562011-05-18 16:21:43 +0300370 else if (dent == d->dfs_disable_bgt)
371 d->disable_bgt = val;
372 else if (dent == d->dfs_emulate_bitflips)
373 d->emulate_bitflips = val;
374 else if (dent == d->dfs_emulate_io_failures)
375 d->emulate_io_failures = val;
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300376 else
377 count = -EINVAL;
378
379out:
380 ubi_put_device(ubi);
381 return count;
382}
383
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300384/* File operations for all UBI debugfs files */
385static const struct file_operations dfs_fops = {
386 .read = dfs_file_read,
387 .write = dfs_file_write,
Stephen Boyd234e3402012-04-05 14:25:11 -0700388 .open = simple_open,
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300389 .llseek = no_llseek,
390 .owner = THIS_MODULE,
391};
392
393/**
394 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
395 * @ubi: UBI device description object
396 *
397 * This function creates all debugfs files for UBI device @ubi. Returns zero in
398 * case of success and a negative error code in case of failure.
399 */
400int ubi_debugfs_init_dev(struct ubi_device *ubi)
401{
402 int err, n;
403 unsigned long ubi_num = ubi->ubi_num;
404 const char *fname;
405 struct dentry *dent;
406 struct ubi_debug_info *d = ubi->dbg;
407
408 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
409 ubi->ubi_num);
410 if (n == UBI_DFS_DIR_LEN) {
411 /* The array size is too small */
412 fname = UBI_DFS_DIR_NAME;
413 dent = ERR_PTR(-EINVAL);
414 goto out;
415 }
416
417 fname = d->dfs_dir_name;
418 dent = debugfs_create_dir(fname, dfs_rootdir);
419 if (IS_ERR_OR_NULL(dent))
420 goto out;
421 d->dfs_dir = dent;
422
423 fname = "chk_gen";
424 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
425 &dfs_fops);
426 if (IS_ERR_OR_NULL(dent))
427 goto out_remove;
428 d->dfs_chk_gen = dent;
429
430 fname = "chk_io";
431 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
432 &dfs_fops);
433 if (IS_ERR_OR_NULL(dent))
434 goto out_remove;
435 d->dfs_chk_io = dent;
436
Artem Bityutskiycd6d8562011-05-18 16:21:43 +0300437 fname = "tst_disable_bgt";
438 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
439 &dfs_fops);
440 if (IS_ERR_OR_NULL(dent))
441 goto out_remove;
442 d->dfs_disable_bgt = dent;
443
444 fname = "tst_emulate_bitflips";
445 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
446 &dfs_fops);
447 if (IS_ERR_OR_NULL(dent))
448 goto out_remove;
449 d->dfs_emulate_bitflips = dent;
450
451 fname = "tst_emulate_io_failures";
452 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
453 &dfs_fops);
454 if (IS_ERR_OR_NULL(dent))
455 goto out_remove;
456 d->dfs_emulate_io_failures = dent;
457
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +0300458 return 0;
459
460out_remove:
461 debugfs_remove_recursive(d->dfs_dir);
462out:
463 err = dent ? PTR_ERR(dent) : -ENODEV;
464 ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n",
465 fname, err);
466 return err;
467}
468
469/**
470 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
471 * @ubi: UBI device description object
472 */
473void ubi_debugfs_exit_dev(struct ubi_device *ubi)
474{
475 debugfs_remove_recursive(ubi->dbg->dfs_dir);
476}
477
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300478#endif /* CONFIG_MTD_UBI_DEBUG */