1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) International Business Machines Corp., 2006
5 * Author: Artem Bityutskiy (Битюцкий Артём)
9 #include <linux/debugfs.h>
10 #include <linux/uaccess.h>
11 #include <linux/module.h>
12 #include <linux/seq_file.h>
13 #include <linux/fault-inject.h>
15 #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
16 static DECLARE_FAULT_ATTR(fault_eccerr_attr
);
17 static DECLARE_FAULT_ATTR(fault_bitflips_attr
);
18 static DECLARE_FAULT_ATTR(fault_read_failure_attr
);
19 static DECLARE_FAULT_ATTR(fault_write_failure_attr
);
20 static DECLARE_FAULT_ATTR(fault_erase_failure_attr
);
21 static DECLARE_FAULT_ATTR(fault_power_cut_attr
);
22 static DECLARE_FAULT_ATTR(fault_io_ff_attr
);
23 static DECLARE_FAULT_ATTR(fault_io_ff_bitflips_attr
);
24 static DECLARE_FAULT_ATTR(fault_bad_hdr_attr
);
25 static DECLARE_FAULT_ATTR(fault_bad_hdr_ebadmsg_attr
);
27 #define FAIL_ACTION(name, fault_attr) \
28 bool should_fail_##name(void) \
30 return should_fail(&fault_attr, 1); \
33 FAIL_ACTION(eccerr
, fault_eccerr_attr
)
34 FAIL_ACTION(bitflips
, fault_bitflips_attr
)
35 FAIL_ACTION(read_failure
, fault_read_failure_attr
)
36 FAIL_ACTION(write_failure
, fault_write_failure_attr
)
37 FAIL_ACTION(erase_failure
, fault_erase_failure_attr
)
38 FAIL_ACTION(power_cut
, fault_power_cut_attr
)
39 FAIL_ACTION(io_ff
, fault_io_ff_attr
)
40 FAIL_ACTION(io_ff_bitflips
, fault_io_ff_bitflips_attr
)
41 FAIL_ACTION(bad_hdr
, fault_bad_hdr_attr
)
42 FAIL_ACTION(bad_hdr_ebadmsg
, fault_bad_hdr_ebadmsg_attr
)
46 * ubi_dump_flash - dump a region of flash.
47 * @ubi: UBI device description object
48 * @pnum: the physical eraseblock number to dump
49 * @offset: the starting offset within the physical eraseblock to dump
50 * @len: the length of the region to dump
52 void ubi_dump_flash(struct ubi_device
*ubi
, int pnum
, int offset
, int len
)
57 loff_t addr
= (loff_t
)pnum
* ubi
->peb_size
+ offset
;
62 err
= mtd_read(ubi
->mtd
, addr
, len
, &read
, buf
);
63 if (err
&& err
!= -EUCLEAN
) {
64 ubi_err(ubi
, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
65 err
, len
, pnum
, offset
, read
);
69 ubi_msg(ubi
, "dumping %d bytes of data from PEB %d, offset %d",
71 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_OFFSET
, 32, 1, buf
, len
, 1);
78 * ubi_dump_ec_hdr - dump an erase counter header.
79 * @ec_hdr: the erase counter header to dump
81 void ubi_dump_ec_hdr(const struct ubi_ec_hdr
*ec_hdr
)
83 pr_err("Erase counter header dump:\n");
84 pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr
->magic
));
85 pr_err("\tversion %d\n", (int)ec_hdr
->version
);
86 pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr
->ec
));
87 pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr
->vid_hdr_offset
));
88 pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr
->data_offset
));
89 pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr
->image_seq
));
90 pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr
->hdr_crc
));
91 pr_err("erase counter header hexdump:\n");
92 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_OFFSET
, 32, 1,
93 ec_hdr
, UBI_EC_HDR_SIZE
, 1);
97 * ubi_dump_vid_hdr - dump a volume identifier header.
98 * @vid_hdr: the volume identifier header to dump
100 void ubi_dump_vid_hdr(const struct ubi_vid_hdr
*vid_hdr
)
102 pr_err("Volume identifier header dump:\n");
103 pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr
->magic
));
104 pr_err("\tversion %d\n", (int)vid_hdr
->version
);
105 pr_err("\tvol_type %d\n", (int)vid_hdr
->vol_type
);
106 pr_err("\tcopy_flag %d\n", (int)vid_hdr
->copy_flag
);
107 pr_err("\tcompat %d\n", (int)vid_hdr
->compat
);
108 pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr
->vol_id
));
109 pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr
->lnum
));
110 pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr
->data_size
));
111 pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr
->used_ebs
));
112 pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr
->data_pad
));
113 pr_err("\tsqnum %llu\n",
114 (unsigned long long)be64_to_cpu(vid_hdr
->sqnum
));
115 pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr
->hdr_crc
));
116 pr_err("Volume identifier header hexdump:\n");
117 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_OFFSET
, 32, 1,
118 vid_hdr
, UBI_VID_HDR_SIZE
, 1);
122 * ubi_dump_vol_info - dump volume information.
123 * @vol: UBI volume description object
125 void ubi_dump_vol_info(const struct ubi_volume
*vol
)
127 pr_err("Volume information dump:\n");
128 pr_err("\tvol_id %d\n", vol
->vol_id
);
129 pr_err("\treserved_pebs %d\n", vol
->reserved_pebs
);
130 pr_err("\talignment %d\n", vol
->alignment
);
131 pr_err("\tdata_pad %d\n", vol
->data_pad
);
132 pr_err("\tvol_type %d\n", vol
->vol_type
);
133 pr_err("\tname_len %d\n", vol
->name_len
);
134 pr_err("\tusable_leb_size %d\n", vol
->usable_leb_size
);
135 pr_err("\tused_ebs %d\n", vol
->used_ebs
);
136 pr_err("\tused_bytes %lld\n", vol
->used_bytes
);
137 pr_err("\tlast_eb_bytes %d\n", vol
->last_eb_bytes
);
138 pr_err("\tcorrupted %d\n", vol
->corrupted
);
139 pr_err("\tupd_marker %d\n", vol
->upd_marker
);
140 pr_err("\tskip_check %d\n", vol
->skip_check
);
142 if (vol
->name_len
<= UBI_VOL_NAME_MAX
&&
143 strnlen(vol
->name
, vol
->name_len
+ 1) == vol
->name_len
) {
144 pr_err("\tname %s\n", vol
->name
);
146 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
147 vol
->name
[0], vol
->name
[1], vol
->name
[2],
148 vol
->name
[3], vol
->name
[4]);
153 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
154 * @r: the object to dump
155 * @idx: volume table index
157 void ubi_dump_vtbl_record(const struct ubi_vtbl_record
*r
, int idx
)
159 int name_len
= be16_to_cpu(r
->name_len
);
161 pr_err("Volume table record %d dump:\n", idx
);
162 pr_err("\treserved_pebs %d\n", be32_to_cpu(r
->reserved_pebs
));
163 pr_err("\talignment %d\n", be32_to_cpu(r
->alignment
));
164 pr_err("\tdata_pad %d\n", be32_to_cpu(r
->data_pad
));
165 pr_err("\tvol_type %d\n", (int)r
->vol_type
);
166 pr_err("\tupd_marker %d\n", (int)r
->upd_marker
);
167 pr_err("\tname_len %d\n", name_len
);
169 if (r
->name
[0] == '\0') {
170 pr_err("\tname NULL\n");
174 if (name_len
<= UBI_VOL_NAME_MAX
&&
175 strnlen(&r
->name
[0], name_len
+ 1) == name_len
) {
176 pr_err("\tname %s\n", &r
->name
[0]);
178 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
179 r
->name
[0], r
->name
[1], r
->name
[2], r
->name
[3],
182 pr_err("\tcrc %#08x\n", be32_to_cpu(r
->crc
));
186 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
187 * @av: the object to dump
189 void ubi_dump_av(const struct ubi_ainf_volume
*av
)
191 pr_err("Volume attaching information dump:\n");
192 pr_err("\tvol_id %d\n", av
->vol_id
);
193 pr_err("\thighest_lnum %d\n", av
->highest_lnum
);
194 pr_err("\tleb_count %d\n", av
->leb_count
);
195 pr_err("\tcompat %d\n", av
->compat
);
196 pr_err("\tvol_type %d\n", av
->vol_type
);
197 pr_err("\tused_ebs %d\n", av
->used_ebs
);
198 pr_err("\tlast_data_size %d\n", av
->last_data_size
);
199 pr_err("\tdata_pad %d\n", av
->data_pad
);
203 * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
204 * @aeb: the object to dump
205 * @type: object type: 0 - not corrupted, 1 - corrupted
207 void ubi_dump_aeb(const struct ubi_ainf_peb
*aeb
, int type
)
209 pr_err("eraseblock attaching information dump:\n");
210 pr_err("\tec %d\n", aeb
->ec
);
211 pr_err("\tpnum %d\n", aeb
->pnum
);
213 pr_err("\tlnum %d\n", aeb
->lnum
);
214 pr_err("\tscrub %d\n", aeb
->scrub
);
215 pr_err("\tsqnum %llu\n", aeb
->sqnum
);
220 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
221 * @req: the object to dump
223 void ubi_dump_mkvol_req(const struct ubi_mkvol_req
*req
)
227 pr_err("Volume creation request dump:\n");
228 pr_err("\tvol_id %d\n", req
->vol_id
);
229 pr_err("\talignment %d\n", req
->alignment
);
230 pr_err("\tbytes %lld\n", (long long)req
->bytes
);
231 pr_err("\tvol_type %d\n", req
->vol_type
);
232 pr_err("\tname_len %d\n", req
->name_len
);
234 memcpy(nm
, req
->name
, 16);
236 pr_err("\t1st 16 characters of name: %s\n", nm
);
240 * Root directory for UBI stuff in debugfs. Contains sub-directories which
241 * contain the stuff specific to particular UBI devices.
243 static struct dentry
*dfs_rootdir
;
245 #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
246 static void dfs_create_fault_entry(struct dentry
*parent
)
250 dir
= debugfs_create_dir("fault_inject", parent
);
251 if (IS_ERR_OR_NULL(dir
)) {
252 int err
= dir
? PTR_ERR(dir
) : -ENODEV
;
254 pr_warn("UBI error: cannot create \"fault_inject\" debugfs directory, error %d\n",
259 fault_create_debugfs_attr("emulate_eccerr", dir
,
262 fault_create_debugfs_attr("emulate_read_failure", dir
,
263 &fault_read_failure_attr
);
265 fault_create_debugfs_attr("emulate_bitflips", dir
,
266 &fault_bitflips_attr
);
268 fault_create_debugfs_attr("emulate_write_failure", dir
,
269 &fault_write_failure_attr
);
271 fault_create_debugfs_attr("emulate_erase_failure", dir
,
272 &fault_erase_failure_attr
);
274 fault_create_debugfs_attr("emulate_power_cut", dir
,
275 &fault_power_cut_attr
);
277 fault_create_debugfs_attr("emulate_io_ff", dir
,
280 fault_create_debugfs_attr("emulate_io_ff_bitflips", dir
,
281 &fault_io_ff_bitflips_attr
);
283 fault_create_debugfs_attr("emulate_bad_hdr", dir
,
284 &fault_bad_hdr_attr
);
286 fault_create_debugfs_attr("emulate_bad_hdr_ebadmsg", dir
,
287 &fault_bad_hdr_ebadmsg_attr
);
292 * ubi_debugfs_init - create UBI debugfs directory.
294 * Create UBI debugfs directory. Returns zero in case of success and a negative
295 * error code in case of failure.
297 int ubi_debugfs_init(void)
299 if (!IS_ENABLED(CONFIG_DEBUG_FS
))
302 dfs_rootdir
= debugfs_create_dir("ubi", NULL
);
303 if (IS_ERR_OR_NULL(dfs_rootdir
)) {
304 int err
= dfs_rootdir
? PTR_ERR(dfs_rootdir
) : -ENODEV
;
306 pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
311 #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
312 dfs_create_fault_entry(dfs_rootdir
);
319 * ubi_debugfs_exit - remove UBI debugfs directory.
321 void ubi_debugfs_exit(void)
323 if (IS_ENABLED(CONFIG_DEBUG_FS
))
324 debugfs_remove(dfs_rootdir
);
327 /* Read an UBI debugfs file */
328 static ssize_t
dfs_file_read(struct file
*file
, char __user
*user_buf
,
329 size_t count
, loff_t
*ppos
)
331 unsigned long ubi_num
= (unsigned long)file
->private_data
;
332 struct dentry
*dent
= file
->f_path
.dentry
;
333 struct ubi_device
*ubi
;
334 struct ubi_debug_info
*d
;
338 ubi
= ubi_get_device(ubi_num
);
343 if (dent
== d
->dfs_chk_gen
)
345 else if (dent
== d
->dfs_chk_io
)
347 else if (dent
== d
->dfs_chk_fastmap
)
348 val
= d
->chk_fastmap
;
349 else if (dent
== d
->dfs_disable_bgt
)
350 val
= d
->disable_bgt
;
351 else if (dent
== d
->dfs_emulate_bitflips
)
352 val
= d
->emulate_bitflips
;
353 else if (dent
== d
->dfs_emulate_io_failures
)
354 val
= d
->emulate_io_failures
;
355 else if (dent
== d
->dfs_emulate_failures
) {
356 snprintf(buf
, sizeof(buf
), "0x%04x\n", d
->emulate_failures
);
357 count
= simple_read_from_buffer(user_buf
, count
, ppos
,
360 } else if (dent
== d
->dfs_emulate_power_cut
) {
361 snprintf(buf
, sizeof(buf
), "%u\n", d
->emulate_power_cut
);
362 count
= simple_read_from_buffer(user_buf
, count
, ppos
,
365 } else if (dent
== d
->dfs_power_cut_min
) {
366 snprintf(buf
, sizeof(buf
), "%u\n", d
->power_cut_min
);
367 count
= simple_read_from_buffer(user_buf
, count
, ppos
,
370 } else if (dent
== d
->dfs_power_cut_max
) {
371 snprintf(buf
, sizeof(buf
), "%u\n", d
->power_cut_max
);
372 count
= simple_read_from_buffer(user_buf
, count
, ppos
,
387 count
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
394 /* Write an UBI debugfs file */
395 static ssize_t
dfs_file_write(struct file
*file
, const char __user
*user_buf
,
396 size_t count
, loff_t
*ppos
)
398 unsigned long ubi_num
= (unsigned long)file
->private_data
;
399 struct dentry
*dent
= file
->f_path
.dentry
;
400 struct ubi_device
*ubi
;
401 struct ubi_debug_info
*d
;
406 ubi
= ubi_get_device(ubi_num
);
411 buf_size
= min_t(size_t, count
, (sizeof(buf
) - 1));
412 if (copy_from_user(buf
, user_buf
, buf_size
)) {
417 if (dent
== d
->dfs_emulate_failures
) {
418 if (kstrtouint(buf
, 0, &d
->emulate_failures
) != 0)
421 } else if (dent
== d
->dfs_power_cut_min
) {
422 if (kstrtouint(buf
, 0, &d
->power_cut_min
) != 0)
425 } else if (dent
== d
->dfs_power_cut_max
) {
426 if (kstrtouint(buf
, 0, &d
->power_cut_max
) != 0)
429 } else if (dent
== d
->dfs_emulate_power_cut
) {
430 if (kstrtoint(buf
, 0, &val
) != 0)
433 d
->emulate_power_cut
= val
;
439 else if (buf
[0] == '0')
446 if (dent
== d
->dfs_chk_gen
)
448 else if (dent
== d
->dfs_chk_io
)
450 else if (dent
== d
->dfs_chk_fastmap
)
451 d
->chk_fastmap
= val
;
452 else if (dent
== d
->dfs_disable_bgt
)
453 d
->disable_bgt
= val
;
454 else if (dent
== d
->dfs_emulate_bitflips
)
455 d
->emulate_bitflips
= val
;
456 else if (dent
== d
->dfs_emulate_io_failures
)
457 d
->emulate_io_failures
= val
;
466 /* File operations for all UBI debugfs files except
467 * detailed_erase_block_info
469 static const struct file_operations dfs_fops
= {
470 .read
= dfs_file_read
,
471 .write
= dfs_file_write
,
473 .owner
= THIS_MODULE
,
476 /* As long as the position is less then that total number of erase blocks,
477 * we still have more to print.
479 static void *eraseblk_count_seq_start(struct seq_file
*s
, loff_t
*pos
)
481 struct ubi_device
*ubi
= s
->private;
483 if (*pos
< ubi
->peb_count
)
489 /* Since we are using the position as the iterator, we just need to check if we
490 * are done and increment the position.
492 static void *eraseblk_count_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
494 struct ubi_device
*ubi
= s
->private;
498 if (*pos
< ubi
->peb_count
)
504 static void eraseblk_count_seq_stop(struct seq_file
*s
, void *v
)
508 static int eraseblk_count_seq_show(struct seq_file
*s
, void *iter
)
510 struct ubi_device
*ubi
= s
->private;
511 struct ubi_wl_entry
*wl
;
512 int *block_number
= iter
;
513 int erase_count
= -1;
516 /* If this is the start, print a header */
517 if (*block_number
== 0)
518 seq_puts(s
, "physical_block_number\terase_count\n");
520 err
= ubi_io_is_bad(ubi
, *block_number
);
524 spin_lock(&ubi
->wl_lock
);
526 wl
= ubi
->lookuptbl
[*block_number
];
528 erase_count
= wl
->ec
;
530 spin_unlock(&ubi
->wl_lock
);
535 seq_printf(s
, "%-22d\t%-11d\n", *block_number
, erase_count
);
540 static const struct seq_operations eraseblk_count_seq_ops
= {
541 .start
= eraseblk_count_seq_start
,
542 .next
= eraseblk_count_seq_next
,
543 .stop
= eraseblk_count_seq_stop
,
544 .show
= eraseblk_count_seq_show
547 static int eraseblk_count_open(struct inode
*inode
, struct file
*f
)
552 err
= seq_open(f
, &eraseblk_count_seq_ops
);
557 s
->private = ubi_get_device((unsigned long)inode
->i_private
);
565 static int eraseblk_count_release(struct inode
*inode
, struct file
*f
)
567 struct seq_file
*s
= f
->private_data
;
568 struct ubi_device
*ubi
= s
->private;
572 return seq_release(inode
, f
);
575 static const struct file_operations eraseblk_count_fops
= {
576 .owner
= THIS_MODULE
,
577 .open
= eraseblk_count_open
,
580 .release
= eraseblk_count_release
,
584 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
585 * @ubi: UBI device description object
587 * This function creates all debugfs files for UBI device @ubi. Returns zero in
588 * case of success and a negative error code in case of failure.
590 int ubi_debugfs_init_dev(struct ubi_device
*ubi
)
592 unsigned long ubi_num
= ubi
->ubi_num
;
593 struct ubi_debug_info
*d
= &ubi
->dbg
;
594 umode_t mode
= S_IRUSR
| S_IWUSR
;
597 if (!IS_ENABLED(CONFIG_DEBUG_FS
))
600 n
= snprintf(d
->dfs_dir_name
, UBI_DFS_DIR_LEN
, UBI_DFS_DIR_NAME
,
602 if (n
>= UBI_DFS_DIR_LEN
) {
603 /* The array size is too small */
607 d
->dfs_dir
= debugfs_create_dir(d
->dfs_dir_name
, dfs_rootdir
);
609 d
->dfs_chk_gen
= debugfs_create_file("chk_gen", mode
, d
->dfs_dir
,
610 (void *)ubi_num
, &dfs_fops
);
612 d
->dfs_chk_io
= debugfs_create_file("chk_io", mode
, d
->dfs_dir
,
613 (void *)ubi_num
, &dfs_fops
);
615 d
->dfs_chk_fastmap
= debugfs_create_file("chk_fastmap", mode
,
616 d
->dfs_dir
, (void *)ubi_num
,
619 d
->dfs_disable_bgt
= debugfs_create_file("tst_disable_bgt", mode
,
620 d
->dfs_dir
, (void *)ubi_num
,
623 d
->dfs_emulate_bitflips
= debugfs_create_file("tst_emulate_bitflips",
628 d
->dfs_emulate_io_failures
= debugfs_create_file("tst_emulate_io_failures",
633 d
->dfs_emulate_power_cut
= debugfs_create_file("tst_emulate_power_cut",
638 d
->dfs_power_cut_min
= debugfs_create_file("tst_emulate_power_cut_min",
640 (void *)ubi_num
, &dfs_fops
);
642 d
->dfs_power_cut_max
= debugfs_create_file("tst_emulate_power_cut_max",
644 (void *)ubi_num
, &dfs_fops
);
646 debugfs_create_file("detailed_erase_block_info", S_IRUSR
, d
->dfs_dir
,
647 (void *)ubi_num
, &eraseblk_count_fops
);
649 #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
650 d
->dfs_emulate_failures
= debugfs_create_file("emulate_failures",
659 * ubi_debugfs_exit_dev - free all debugfs files corresponding to device @ubi
660 * @ubi: UBI device description object
662 void ubi_debugfs_exit_dev(struct ubi_device
*ubi
)
664 if (IS_ENABLED(CONFIG_DEBUG_FS
))
665 debugfs_remove_recursive(ubi
->dbg
.dfs_dir
);
669 * ubi_dbg_power_cut - emulate a power cut if it is time to do so
670 * @ubi: UBI device description object
671 * @caller: Flags set to indicate from where the function is being called
673 * Returns non-zero if a power cut was emulated, zero if not.
675 int ubi_dbg_power_cut(struct ubi_device
*ubi
, int caller
)
679 if ((ubi
->dbg
.emulate_power_cut
& caller
) == 0)
682 if (ubi
->dbg
.power_cut_counter
== 0) {
683 ubi
->dbg
.power_cut_counter
= ubi
->dbg
.power_cut_min
;
685 if (ubi
->dbg
.power_cut_max
> ubi
->dbg
.power_cut_min
) {
686 range
= ubi
->dbg
.power_cut_max
- ubi
->dbg
.power_cut_min
;
687 ubi
->dbg
.power_cut_counter
+= get_random_u32_below(range
);
692 ubi
->dbg
.power_cut_counter
--;
693 if (ubi
->dbg
.power_cut_counter
)