11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2801c135cSArtem B. Bityutskiy /*
3801c135cSArtem B. Bityutskiy * Copyright (c) International Business Machines Corp., 2006
4801c135cSArtem B. Bityutskiy *
5801c135cSArtem B. Bityutskiy * Author: Artem Bityutskiy (Битюцкий Артём)
6801c135cSArtem B. Bityutskiy */
7801c135cSArtem B. Bityutskiy
8801c135cSArtem B. Bityutskiy #include "ubi.h"
92a734bb8SArtem Bityutskiy #include <linux/debugfs.h>
102a734bb8SArtem Bityutskiy #include <linux/uaccess.h>
11b342efd4SArtem Bityutskiy #include <linux/module.h>
127bccd12dSBen Shelton #include <linux/seq_file.h>
136931fb44SZhaoLong Wang #include <linux/fault-inject.h>
14801c135cSArtem B. Bityutskiy
156931fb44SZhaoLong Wang #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
167cd8d1f8SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_eccerr_attr);
176931fb44SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_bitflips_attr);
187cd8d1f8SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_read_failure_attr);
19e30948f7SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_write_failure_attr);
20e30948f7SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_erase_failure_attr);
216931fb44SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_power_cut_attr);
227cd8d1f8SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_io_ff_attr);
237cd8d1f8SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_io_ff_bitflips_attr);
247cd8d1f8SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_bad_hdr_attr);
257cd8d1f8SZhaoLong Wang static DECLARE_FAULT_ATTR(fault_bad_hdr_ebadmsg_attr);
266931fb44SZhaoLong Wang
276931fb44SZhaoLong Wang #define FAIL_ACTION(name, fault_attr) \
286931fb44SZhaoLong Wang bool should_fail_##name(void) \
296931fb44SZhaoLong Wang { \
306931fb44SZhaoLong Wang return should_fail(&fault_attr, 1); \
316931fb44SZhaoLong Wang }
326931fb44SZhaoLong Wang
FAIL_ACTION(eccerr,fault_eccerr_attr)337cd8d1f8SZhaoLong Wang FAIL_ACTION(eccerr, fault_eccerr_attr)
346931fb44SZhaoLong Wang FAIL_ACTION(bitflips, fault_bitflips_attr)
357cd8d1f8SZhaoLong Wang FAIL_ACTION(read_failure, fault_read_failure_attr)
36e30948f7SZhaoLong Wang FAIL_ACTION(write_failure, fault_write_failure_attr)
37e30948f7SZhaoLong Wang FAIL_ACTION(erase_failure, fault_erase_failure_attr)
386931fb44SZhaoLong Wang FAIL_ACTION(power_cut, fault_power_cut_attr)
397cd8d1f8SZhaoLong Wang FAIL_ACTION(io_ff, fault_io_ff_attr)
407cd8d1f8SZhaoLong Wang FAIL_ACTION(io_ff_bitflips, fault_io_ff_bitflips_attr)
417cd8d1f8SZhaoLong Wang FAIL_ACTION(bad_hdr, fault_bad_hdr_attr)
427cd8d1f8SZhaoLong Wang FAIL_ACTION(bad_hdr_ebadmsg, fault_bad_hdr_ebadmsg_attr)
436931fb44SZhaoLong Wang #endif
44ef7088e7SArtem Bityutskiy
45ef7088e7SArtem Bityutskiy /**
46ef7088e7SArtem Bityutskiy * ubi_dump_flash - dump a region of flash.
47ef7088e7SArtem Bityutskiy * @ubi: UBI device description object
48ef7088e7SArtem Bityutskiy * @pnum: the physical eraseblock number to dump
49ef7088e7SArtem Bityutskiy * @offset: the starting offset within the physical eraseblock to dump
50ef7088e7SArtem Bityutskiy * @len: the length of the region to dump
51ef7088e7SArtem Bityutskiy */
52ef7088e7SArtem Bityutskiy void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
53ef7088e7SArtem Bityutskiy {
54ef7088e7SArtem Bityutskiy int err;
55ef7088e7SArtem Bityutskiy size_t read;
56ef7088e7SArtem Bityutskiy void *buf;
57ef7088e7SArtem Bityutskiy loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
58ef7088e7SArtem Bityutskiy
59ef7088e7SArtem Bityutskiy buf = vmalloc(len);
60ef7088e7SArtem Bityutskiy if (!buf)
61ef7088e7SArtem Bityutskiy return;
62ef7088e7SArtem Bityutskiy err = mtd_read(ubi->mtd, addr, len, &read, buf);
63ef7088e7SArtem Bityutskiy if (err && err != -EUCLEAN) {
6432608703STanya Brokhman ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
65049333ceSArtem Bityutskiy err, len, pnum, offset, read);
66ef7088e7SArtem Bityutskiy goto out;
67ef7088e7SArtem Bityutskiy }
68ef7088e7SArtem Bityutskiy
6932608703STanya Brokhman ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
70ef7088e7SArtem Bityutskiy len, pnum, offset);
71ef7088e7SArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
72ef7088e7SArtem Bityutskiy out:
73ef7088e7SArtem Bityutskiy vfree(buf);
74ef7088e7SArtem Bityutskiy return;
75ef7088e7SArtem Bityutskiy }
76ef7088e7SArtem Bityutskiy
77801c135cSArtem B. Bityutskiy /**
78a904e3f1SArtem Bityutskiy * ubi_dump_ec_hdr - dump an erase counter header.
79801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to dump
80801c135cSArtem B. Bityutskiy */
ubi_dump_ec_hdr(const struct ubi_ec_hdr * ec_hdr)81a904e3f1SArtem Bityutskiy void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
82801c135cSArtem B. Bityutskiy {
83e28453bbSArtem Bityutskiy pr_err("Erase counter header dump:\n");
84e28453bbSArtem Bityutskiy pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic));
85e28453bbSArtem Bityutskiy pr_err("\tversion %d\n", (int)ec_hdr->version);
86e28453bbSArtem Bityutskiy pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
87e28453bbSArtem Bityutskiy pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
88e28453bbSArtem Bityutskiy pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset));
89e28453bbSArtem Bityutskiy pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq));
90e28453bbSArtem Bityutskiy pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
91e28453bbSArtem Bityutskiy pr_err("erase counter header hexdump:\n");
926986646bSArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
936986646bSArtem Bityutskiy ec_hdr, UBI_EC_HDR_SIZE, 1);
94801c135cSArtem B. Bityutskiy }
95801c135cSArtem B. Bityutskiy
96801c135cSArtem B. Bityutskiy /**
97a904e3f1SArtem Bityutskiy * ubi_dump_vid_hdr - dump a volume identifier header.
98801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to dump
99801c135cSArtem B. Bityutskiy */
ubi_dump_vid_hdr(const struct ubi_vid_hdr * vid_hdr)100a904e3f1SArtem Bityutskiy void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
101801c135cSArtem B. Bityutskiy {
102e28453bbSArtem Bityutskiy pr_err("Volume identifier header dump:\n");
103e28453bbSArtem Bityutskiy pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
104e28453bbSArtem Bityutskiy pr_err("\tversion %d\n", (int)vid_hdr->version);
105e28453bbSArtem Bityutskiy pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type);
106e28453bbSArtem Bityutskiy pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
107e28453bbSArtem Bityutskiy pr_err("\tcompat %d\n", (int)vid_hdr->compat);
108e28453bbSArtem Bityutskiy pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
109e28453bbSArtem Bityutskiy pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
110e28453bbSArtem Bityutskiy pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
111e28453bbSArtem Bityutskiy pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
112e28453bbSArtem Bityutskiy pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
113e28453bbSArtem Bityutskiy pr_err("\tsqnum %llu\n",
1143261ebd7SChristoph Hellwig (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
115e28453bbSArtem Bityutskiy pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
116e28453bbSArtem Bityutskiy pr_err("Volume identifier header hexdump:\n");
117c8566350SArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
118c8566350SArtem Bityutskiy vid_hdr, UBI_VID_HDR_SIZE, 1);
119801c135cSArtem B. Bityutskiy }
120801c135cSArtem B. Bityutskiy
121801c135cSArtem B. Bityutskiy /**
122766381f0SArtem Bityutskiy * ubi_dump_vol_info - dump volume information.
123801c135cSArtem B. Bityutskiy * @vol: UBI volume description object
124801c135cSArtem B. Bityutskiy */
ubi_dump_vol_info(const struct ubi_volume * vol)125766381f0SArtem Bityutskiy void ubi_dump_vol_info(const struct ubi_volume *vol)
126801c135cSArtem B. Bityutskiy {
127e28453bbSArtem Bityutskiy pr_err("Volume information dump:\n");
128e28453bbSArtem Bityutskiy pr_err("\tvol_id %d\n", vol->vol_id);
129e28453bbSArtem Bityutskiy pr_err("\treserved_pebs %d\n", vol->reserved_pebs);
130e28453bbSArtem Bityutskiy pr_err("\talignment %d\n", vol->alignment);
131e28453bbSArtem Bityutskiy pr_err("\tdata_pad %d\n", vol->data_pad);
132e28453bbSArtem Bityutskiy pr_err("\tvol_type %d\n", vol->vol_type);
133e28453bbSArtem Bityutskiy pr_err("\tname_len %d\n", vol->name_len);
134e28453bbSArtem Bityutskiy pr_err("\tusable_leb_size %d\n", vol->usable_leb_size);
135e28453bbSArtem Bityutskiy pr_err("\tused_ebs %d\n", vol->used_ebs);
136e28453bbSArtem Bityutskiy pr_err("\tused_bytes %lld\n", vol->used_bytes);
137e28453bbSArtem Bityutskiy pr_err("\tlast_eb_bytes %d\n", vol->last_eb_bytes);
138e28453bbSArtem Bityutskiy pr_err("\tcorrupted %d\n", vol->corrupted);
139e28453bbSArtem Bityutskiy pr_err("\tupd_marker %d\n", vol->upd_marker);
140188945e9SStefan Roese pr_err("\tskip_check %d\n", vol->skip_check);
141801c135cSArtem B. Bityutskiy
142801c135cSArtem B. Bityutskiy if (vol->name_len <= UBI_VOL_NAME_MAX &&
143801c135cSArtem B. Bityutskiy strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
144e28453bbSArtem Bityutskiy pr_err("\tname %s\n", vol->name);
145801c135cSArtem B. Bityutskiy } else {
146e28453bbSArtem Bityutskiy pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
147801c135cSArtem B. Bityutskiy vol->name[0], vol->name[1], vol->name[2],
148801c135cSArtem B. Bityutskiy vol->name[3], vol->name[4]);
149801c135cSArtem B. Bityutskiy }
150801c135cSArtem B. Bityutskiy }
151801c135cSArtem B. Bityutskiy
152801c135cSArtem B. Bityutskiy /**
1531f021e1dSArtem Bityutskiy * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
154801c135cSArtem B. Bityutskiy * @r: the object to dump
155801c135cSArtem B. Bityutskiy * @idx: volume table index
156801c135cSArtem B. Bityutskiy */
ubi_dump_vtbl_record(const struct ubi_vtbl_record * r,int idx)1571f021e1dSArtem Bityutskiy void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
158801c135cSArtem B. Bityutskiy {
1593261ebd7SChristoph Hellwig int name_len = be16_to_cpu(r->name_len);
160801c135cSArtem B. Bityutskiy
161e28453bbSArtem Bityutskiy pr_err("Volume table record %d dump:\n", idx);
162e28453bbSArtem Bityutskiy pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs));
163e28453bbSArtem Bityutskiy pr_err("\talignment %d\n", be32_to_cpu(r->alignment));
164e28453bbSArtem Bityutskiy pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad));
165e28453bbSArtem Bityutskiy pr_err("\tvol_type %d\n", (int)r->vol_type);
166e28453bbSArtem Bityutskiy pr_err("\tupd_marker %d\n", (int)r->upd_marker);
167e28453bbSArtem Bityutskiy pr_err("\tname_len %d\n", name_len);
168801c135cSArtem B. Bityutskiy
169801c135cSArtem B. Bityutskiy if (r->name[0] == '\0') {
170e28453bbSArtem Bityutskiy pr_err("\tname NULL\n");
171801c135cSArtem B. Bityutskiy return;
172801c135cSArtem B. Bityutskiy }
173801c135cSArtem B. Bityutskiy
174801c135cSArtem B. Bityutskiy if (name_len <= UBI_VOL_NAME_MAX &&
175801c135cSArtem B. Bityutskiy strnlen(&r->name[0], name_len + 1) == name_len) {
176e28453bbSArtem Bityutskiy pr_err("\tname %s\n", &r->name[0]);
177801c135cSArtem B. Bityutskiy } else {
178e28453bbSArtem Bityutskiy pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
179801c135cSArtem B. Bityutskiy r->name[0], r->name[1], r->name[2], r->name[3],
180801c135cSArtem B. Bityutskiy r->name[4]);
181801c135cSArtem B. Bityutskiy }
182e28453bbSArtem Bityutskiy pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
183801c135cSArtem B. Bityutskiy }
184801c135cSArtem B. Bityutskiy
185801c135cSArtem B. Bityutskiy /**
186517af48cSArtem Bityutskiy * ubi_dump_av - dump a &struct ubi_ainf_volume object.
187517af48cSArtem Bityutskiy * @av: the object to dump
188801c135cSArtem B. Bityutskiy */
ubi_dump_av(const struct ubi_ainf_volume * av)189517af48cSArtem Bityutskiy void ubi_dump_av(const struct ubi_ainf_volume *av)
190801c135cSArtem B. Bityutskiy {
191e28453bbSArtem Bityutskiy pr_err("Volume attaching information dump:\n");
192e28453bbSArtem Bityutskiy pr_err("\tvol_id %d\n", av->vol_id);
193e28453bbSArtem Bityutskiy pr_err("\thighest_lnum %d\n", av->highest_lnum);
194e28453bbSArtem Bityutskiy pr_err("\tleb_count %d\n", av->leb_count);
195e28453bbSArtem Bityutskiy pr_err("\tcompat %d\n", av->compat);
196e28453bbSArtem Bityutskiy pr_err("\tvol_type %d\n", av->vol_type);
197e28453bbSArtem Bityutskiy pr_err("\tused_ebs %d\n", av->used_ebs);
198e28453bbSArtem Bityutskiy pr_err("\tlast_data_size %d\n", av->last_data_size);
199e28453bbSArtem Bityutskiy pr_err("\tdata_pad %d\n", av->data_pad);
200801c135cSArtem B. Bityutskiy }
201801c135cSArtem B. Bityutskiy
202801c135cSArtem B. Bityutskiy /**
2032c5ec5ceSArtem Bityutskiy * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
2042c5ec5ceSArtem Bityutskiy * @aeb: the object to dump
205801c135cSArtem B. Bityutskiy * @type: object type: 0 - not corrupted, 1 - corrupted
206801c135cSArtem B. Bityutskiy */
ubi_dump_aeb(const struct ubi_ainf_peb * aeb,int type)2072c5ec5ceSArtem Bityutskiy void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
208801c135cSArtem B. Bityutskiy {
209e28453bbSArtem Bityutskiy pr_err("eraseblock attaching information dump:\n");
210e28453bbSArtem Bityutskiy pr_err("\tec %d\n", aeb->ec);
211e28453bbSArtem Bityutskiy pr_err("\tpnum %d\n", aeb->pnum);
212801c135cSArtem B. Bityutskiy if (type == 0) {
213e28453bbSArtem Bityutskiy pr_err("\tlnum %d\n", aeb->lnum);
214e28453bbSArtem Bityutskiy pr_err("\tscrub %d\n", aeb->scrub);
215e28453bbSArtem Bityutskiy pr_err("\tsqnum %llu\n", aeb->sqnum);
216801c135cSArtem B. Bityutskiy }
217801c135cSArtem B. Bityutskiy }
218801c135cSArtem B. Bityutskiy
219801c135cSArtem B. Bityutskiy /**
220718c00bbSArtem Bityutskiy * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
221801c135cSArtem B. Bityutskiy * @req: the object to dump
222801c135cSArtem B. Bityutskiy */
ubi_dump_mkvol_req(const struct ubi_mkvol_req * req)223718c00bbSArtem Bityutskiy void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
224801c135cSArtem B. Bityutskiy {
225801c135cSArtem B. Bityutskiy char nm[17];
226801c135cSArtem B. Bityutskiy
227e28453bbSArtem Bityutskiy pr_err("Volume creation request dump:\n");
228e28453bbSArtem Bityutskiy pr_err("\tvol_id %d\n", req->vol_id);
229e28453bbSArtem Bityutskiy pr_err("\talignment %d\n", req->alignment);
230e28453bbSArtem Bityutskiy pr_err("\tbytes %lld\n", (long long)req->bytes);
231e28453bbSArtem Bityutskiy pr_err("\tvol_type %d\n", req->vol_type);
232e28453bbSArtem Bityutskiy pr_err("\tname_len %d\n", req->name_len);
233801c135cSArtem B. Bityutskiy
234801c135cSArtem B. Bityutskiy memcpy(nm, req->name, 16);
235801c135cSArtem B. Bityutskiy nm[16] = 0;
236e28453bbSArtem Bityutskiy pr_err("\t1st 16 characters of name: %s\n", nm);
237801c135cSArtem B. Bityutskiy }
238801c135cSArtem B. Bityutskiy
2392a734bb8SArtem Bityutskiy /*
2402a734bb8SArtem Bityutskiy * Root directory for UBI stuff in debugfs. Contains sub-directories which
2412a734bb8SArtem Bityutskiy * contain the stuff specific to particular UBI devices.
2422a734bb8SArtem Bityutskiy */
2432a734bb8SArtem Bityutskiy static struct dentry *dfs_rootdir;
2442a734bb8SArtem Bityutskiy
2456931fb44SZhaoLong Wang #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
dfs_create_fault_entry(struct dentry * parent)2466931fb44SZhaoLong Wang static void dfs_create_fault_entry(struct dentry *parent)
2476931fb44SZhaoLong Wang {
2486931fb44SZhaoLong Wang struct dentry *dir;
2496931fb44SZhaoLong Wang
2506931fb44SZhaoLong Wang dir = debugfs_create_dir("fault_inject", parent);
2516931fb44SZhaoLong Wang if (IS_ERR_OR_NULL(dir)) {
2526931fb44SZhaoLong Wang int err = dir ? PTR_ERR(dir) : -ENODEV;
2536931fb44SZhaoLong Wang
2546931fb44SZhaoLong Wang pr_warn("UBI error: cannot create \"fault_inject\" debugfs directory, error %d\n",
2556931fb44SZhaoLong Wang err);
2566931fb44SZhaoLong Wang return;
2576931fb44SZhaoLong Wang }
2586931fb44SZhaoLong Wang
2597cd8d1f8SZhaoLong Wang fault_create_debugfs_attr("emulate_eccerr", dir,
2607cd8d1f8SZhaoLong Wang &fault_eccerr_attr);
2617cd8d1f8SZhaoLong Wang
2627cd8d1f8SZhaoLong Wang fault_create_debugfs_attr("emulate_read_failure", dir,
2637cd8d1f8SZhaoLong Wang &fault_read_failure_attr);
2647cd8d1f8SZhaoLong Wang
2656931fb44SZhaoLong Wang fault_create_debugfs_attr("emulate_bitflips", dir,
2666931fb44SZhaoLong Wang &fault_bitflips_attr);
2676931fb44SZhaoLong Wang
268e30948f7SZhaoLong Wang fault_create_debugfs_attr("emulate_write_failure", dir,
269e30948f7SZhaoLong Wang &fault_write_failure_attr);
270e30948f7SZhaoLong Wang
271e30948f7SZhaoLong Wang fault_create_debugfs_attr("emulate_erase_failure", dir,
272e30948f7SZhaoLong Wang &fault_erase_failure_attr);
2736931fb44SZhaoLong Wang
2746931fb44SZhaoLong Wang fault_create_debugfs_attr("emulate_power_cut", dir,
2756931fb44SZhaoLong Wang &fault_power_cut_attr);
2767cd8d1f8SZhaoLong Wang
2777cd8d1f8SZhaoLong Wang fault_create_debugfs_attr("emulate_io_ff", dir,
2787cd8d1f8SZhaoLong Wang &fault_io_ff_attr);
2797cd8d1f8SZhaoLong Wang
2807cd8d1f8SZhaoLong Wang fault_create_debugfs_attr("emulate_io_ff_bitflips", dir,
2817cd8d1f8SZhaoLong Wang &fault_io_ff_bitflips_attr);
2827cd8d1f8SZhaoLong Wang
2837cd8d1f8SZhaoLong Wang fault_create_debugfs_attr("emulate_bad_hdr", dir,
2847cd8d1f8SZhaoLong Wang &fault_bad_hdr_attr);
2857cd8d1f8SZhaoLong Wang
2867cd8d1f8SZhaoLong Wang fault_create_debugfs_attr("emulate_bad_hdr_ebadmsg", dir,
2877cd8d1f8SZhaoLong Wang &fault_bad_hdr_ebadmsg_attr);
2886931fb44SZhaoLong Wang }
2896931fb44SZhaoLong Wang #endif
2906931fb44SZhaoLong Wang
2912a734bb8SArtem Bityutskiy /**
2922a734bb8SArtem Bityutskiy * ubi_debugfs_init - create UBI debugfs directory.
2932a734bb8SArtem Bityutskiy *
2942a734bb8SArtem Bityutskiy * Create UBI debugfs directory. Returns zero in case of success and a negative
2952a734bb8SArtem Bityutskiy * error code in case of failure.
2962a734bb8SArtem Bityutskiy */
ubi_debugfs_init(void)2972a734bb8SArtem Bityutskiy int ubi_debugfs_init(void)
2982a734bb8SArtem Bityutskiy {
299903e0e4eSBrian Norris if (!IS_ENABLED(CONFIG_DEBUG_FS))
300e9b4cf20SArtem Bityutskiy return 0;
301e9b4cf20SArtem Bityutskiy
3022a734bb8SArtem Bityutskiy dfs_rootdir = debugfs_create_dir("ubi", NULL);
3032a734bb8SArtem Bityutskiy if (IS_ERR_OR_NULL(dfs_rootdir)) {
30497cb69ddSSudip Mukherjee int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
3052a734bb8SArtem Bityutskiy
30632608703STanya Brokhman pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
3072a734bb8SArtem Bityutskiy err);
3082a734bb8SArtem Bityutskiy return err;
3092a734bb8SArtem Bityutskiy }
3102a734bb8SArtem Bityutskiy
3116931fb44SZhaoLong Wang #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
3126931fb44SZhaoLong Wang dfs_create_fault_entry(dfs_rootdir);
3136931fb44SZhaoLong Wang #endif
3146931fb44SZhaoLong Wang
3152a734bb8SArtem Bityutskiy return 0;
3162a734bb8SArtem Bityutskiy }
3172a734bb8SArtem Bityutskiy
3182a734bb8SArtem Bityutskiy /**
3192a734bb8SArtem Bityutskiy * ubi_debugfs_exit - remove UBI debugfs directory.
3202a734bb8SArtem Bityutskiy */
ubi_debugfs_exit(void)3212a734bb8SArtem Bityutskiy void ubi_debugfs_exit(void)
3222a734bb8SArtem Bityutskiy {
323903e0e4eSBrian Norris if (IS_ENABLED(CONFIG_DEBUG_FS))
3242a734bb8SArtem Bityutskiy debugfs_remove(dfs_rootdir);
3252a734bb8SArtem Bityutskiy }
3262a734bb8SArtem Bityutskiy
3272a734bb8SArtem Bityutskiy /* Read an UBI debugfs file */
dfs_file_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3282a734bb8SArtem Bityutskiy static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
3292a734bb8SArtem Bityutskiy size_t count, loff_t *ppos)
3302a734bb8SArtem Bityutskiy {
3312a734bb8SArtem Bityutskiy unsigned long ubi_num = (unsigned long)file->private_data;
3322a734bb8SArtem Bityutskiy struct dentry *dent = file->f_path.dentry;
3332a734bb8SArtem Bityutskiy struct ubi_device *ubi;
3342a734bb8SArtem Bityutskiy struct ubi_debug_info *d;
3354d0deb38SZhaoLong Wang char buf[16];
3362a734bb8SArtem Bityutskiy int val;
3372a734bb8SArtem Bityutskiy
3382a734bb8SArtem Bityutskiy ubi = ubi_get_device(ubi_num);
3392a734bb8SArtem Bityutskiy if (!ubi)
3402a734bb8SArtem Bityutskiy return -ENODEV;
341eab73772SEzequiel Garcia d = &ubi->dbg;
3422a734bb8SArtem Bityutskiy
3432a734bb8SArtem Bityutskiy if (dent == d->dfs_chk_gen)
3442a734bb8SArtem Bityutskiy val = d->chk_gen;
3452a734bb8SArtem Bityutskiy else if (dent == d->dfs_chk_io)
3462a734bb8SArtem Bityutskiy val = d->chk_io;
3475fa7fa5dSRichard Weinberger else if (dent == d->dfs_chk_fastmap)
3485fa7fa5dSRichard Weinberger val = d->chk_fastmap;
349cd6d8567SArtem Bityutskiy else if (dent == d->dfs_disable_bgt)
350cd6d8567SArtem Bityutskiy val = d->disable_bgt;
351cd6d8567SArtem Bityutskiy else if (dent == d->dfs_emulate_bitflips)
352cd6d8567SArtem Bityutskiy val = d->emulate_bitflips;
353cd6d8567SArtem Bityutskiy else if (dent == d->dfs_emulate_io_failures)
354cd6d8567SArtem Bityutskiy val = d->emulate_io_failures;
3556931fb44SZhaoLong Wang else if (dent == d->dfs_emulate_failures) {
3566931fb44SZhaoLong Wang snprintf(buf, sizeof(buf), "0x%04x\n", d->emulate_failures);
3576931fb44SZhaoLong Wang count = simple_read_from_buffer(user_buf, count, ppos,
3586931fb44SZhaoLong Wang buf, strlen(buf));
3596931fb44SZhaoLong Wang goto out;
3606931fb44SZhaoLong Wang } else if (dent == d->dfs_emulate_power_cut) {
36150269067S[email protected] snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
36250269067S[email protected] count = simple_read_from_buffer(user_buf, count, ppos,
36350269067S[email protected] buf, strlen(buf));
36450269067S[email protected] goto out;
36550269067S[email protected] } else if (dent == d->dfs_power_cut_min) {
36650269067S[email protected] snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
36750269067S[email protected] count = simple_read_from_buffer(user_buf, count, ppos,
36850269067S[email protected] buf, strlen(buf));
36950269067S[email protected] goto out;
37050269067S[email protected] } else if (dent == d->dfs_power_cut_max) {
37150269067S[email protected] snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
37250269067S[email protected] count = simple_read_from_buffer(user_buf, count, ppos,
37350269067S[email protected] buf, strlen(buf));
37450269067S[email protected] goto out;
3756931fb44SZhaoLong Wang } else {
3762a734bb8SArtem Bityutskiy count = -EINVAL;
3772a734bb8SArtem Bityutskiy goto out;
3782a734bb8SArtem Bityutskiy }
3792a734bb8SArtem Bityutskiy
3802a734bb8SArtem Bityutskiy if (val)
3812a734bb8SArtem Bityutskiy buf[0] = '1';
3822a734bb8SArtem Bityutskiy else
3832a734bb8SArtem Bityutskiy buf[0] = '0';
3842a734bb8SArtem Bityutskiy buf[1] = '\n';
3852a734bb8SArtem Bityutskiy buf[2] = 0x00;
3862a734bb8SArtem Bityutskiy
3872a734bb8SArtem Bityutskiy count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3882a734bb8SArtem Bityutskiy
3892a734bb8SArtem Bityutskiy out:
3902a734bb8SArtem Bityutskiy ubi_put_device(ubi);
3912a734bb8SArtem Bityutskiy return count;
3922a734bb8SArtem Bityutskiy }
3932a734bb8SArtem Bityutskiy
3942a734bb8SArtem Bityutskiy /* Write an UBI debugfs file */
dfs_file_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)3952a734bb8SArtem Bityutskiy static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
3962a734bb8SArtem Bityutskiy size_t count, loff_t *ppos)
3972a734bb8SArtem Bityutskiy {
3982a734bb8SArtem Bityutskiy unsigned long ubi_num = (unsigned long)file->private_data;
3992a734bb8SArtem Bityutskiy struct dentry *dent = file->f_path.dentry;
4002a734bb8SArtem Bityutskiy struct ubi_device *ubi;
4012a734bb8SArtem Bityutskiy struct ubi_debug_info *d;
4022a734bb8SArtem Bityutskiy size_t buf_size;
4034d0deb38SZhaoLong Wang char buf[16] = {0};
4042a734bb8SArtem Bityutskiy int val;
4052a734bb8SArtem Bityutskiy
4062a734bb8SArtem Bityutskiy ubi = ubi_get_device(ubi_num);
4072a734bb8SArtem Bityutskiy if (!ubi)
4082a734bb8SArtem Bityutskiy return -ENODEV;
409eab73772SEzequiel Garcia d = &ubi->dbg;
4102a734bb8SArtem Bityutskiy
4112a734bb8SArtem Bityutskiy buf_size = min_t(size_t, count, (sizeof(buf) - 1));
4122a734bb8SArtem Bityutskiy if (copy_from_user(buf, user_buf, buf_size)) {
4132a734bb8SArtem Bityutskiy count = -EFAULT;
4142a734bb8SArtem Bityutskiy goto out;
4152a734bb8SArtem Bityutskiy }
4162a734bb8SArtem Bityutskiy
4176931fb44SZhaoLong Wang if (dent == d->dfs_emulate_failures) {
4186931fb44SZhaoLong Wang if (kstrtouint(buf, 0, &d->emulate_failures) != 0)
4196931fb44SZhaoLong Wang count = -EINVAL;
4206931fb44SZhaoLong Wang goto out;
4216931fb44SZhaoLong Wang } else if (dent == d->dfs_power_cut_min) {
42250269067S[email protected] if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
42350269067S[email protected] count = -EINVAL;
42450269067S[email protected] goto out;
42550269067S[email protected] } else if (dent == d->dfs_power_cut_max) {
42650269067S[email protected] if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
42750269067S[email protected] count = -EINVAL;
42850269067S[email protected] goto out;
42950269067S[email protected] } else if (dent == d->dfs_emulate_power_cut) {
43050269067S[email protected] if (kstrtoint(buf, 0, &val) != 0)
43150269067S[email protected] count = -EINVAL;
43224663e72SDan Carpenter else
43350269067S[email protected] d->emulate_power_cut = val;
43450269067S[email protected] goto out;
43550269067S[email protected] }
43650269067S[email protected]
4372a734bb8SArtem Bityutskiy if (buf[0] == '1')
4382a734bb8SArtem Bityutskiy val = 1;
4392a734bb8SArtem Bityutskiy else if (buf[0] == '0')
4402a734bb8SArtem Bityutskiy val = 0;
4412a734bb8SArtem Bityutskiy else {
4422a734bb8SArtem Bityutskiy count = -EINVAL;
4432a734bb8SArtem Bityutskiy goto out;
4442a734bb8SArtem Bityutskiy }
4452a734bb8SArtem Bityutskiy
4462a734bb8SArtem Bityutskiy if (dent == d->dfs_chk_gen)
4472a734bb8SArtem Bityutskiy d->chk_gen = val;
4482a734bb8SArtem Bityutskiy else if (dent == d->dfs_chk_io)
4492a734bb8SArtem Bityutskiy d->chk_io = val;
4505fa7fa5dSRichard Weinberger else if (dent == d->dfs_chk_fastmap)
4515fa7fa5dSRichard Weinberger d->chk_fastmap = val;
452cd6d8567SArtem Bityutskiy else if (dent == d->dfs_disable_bgt)
453cd6d8567SArtem Bityutskiy d->disable_bgt = val;
454cd6d8567SArtem Bityutskiy else if (dent == d->dfs_emulate_bitflips)
455cd6d8567SArtem Bityutskiy d->emulate_bitflips = val;
456cd6d8567SArtem Bityutskiy else if (dent == d->dfs_emulate_io_failures)
457cd6d8567SArtem Bityutskiy d->emulate_io_failures = val;
4582a734bb8SArtem Bityutskiy else
4592a734bb8SArtem Bityutskiy count = -EINVAL;
4602a734bb8SArtem Bityutskiy
4612a734bb8SArtem Bityutskiy out:
4622a734bb8SArtem Bityutskiy ubi_put_device(ubi);
4632a734bb8SArtem Bityutskiy return count;
4642a734bb8SArtem Bityutskiy }
4652a734bb8SArtem Bityutskiy
4667bccd12dSBen Shelton /* File operations for all UBI debugfs files except
4677bccd12dSBen Shelton * detailed_erase_block_info
4687bccd12dSBen Shelton */
4692a734bb8SArtem Bityutskiy static const struct file_operations dfs_fops = {
4702a734bb8SArtem Bityutskiy .read = dfs_file_read,
4712a734bb8SArtem Bityutskiy .write = dfs_file_write,
472234e3405SStephen Boyd .open = simple_open,
4732a734bb8SArtem Bityutskiy .owner = THIS_MODULE,
4742a734bb8SArtem Bityutskiy };
4752a734bb8SArtem Bityutskiy
4767bccd12dSBen Shelton /* As long as the position is less then that total number of erase blocks,
4777bccd12dSBen Shelton * we still have more to print.
4787bccd12dSBen Shelton */
eraseblk_count_seq_start(struct seq_file * s,loff_t * pos)4797bccd12dSBen Shelton static void *eraseblk_count_seq_start(struct seq_file *s, loff_t *pos)
4807bccd12dSBen Shelton {
4817bccd12dSBen Shelton struct ubi_device *ubi = s->private;
4827bccd12dSBen Shelton
4837bccd12dSBen Shelton if (*pos < ubi->peb_count)
4847bccd12dSBen Shelton return pos;
4857bccd12dSBen Shelton
4867bccd12dSBen Shelton return NULL;
4877bccd12dSBen Shelton }
4887bccd12dSBen Shelton
4897bccd12dSBen Shelton /* Since we are using the position as the iterator, we just need to check if we
4907bccd12dSBen Shelton * are done and increment the position.
4917bccd12dSBen Shelton */
eraseblk_count_seq_next(struct seq_file * s,void * v,loff_t * pos)4927bccd12dSBen Shelton static void *eraseblk_count_seq_next(struct seq_file *s, void *v, loff_t *pos)
4937bccd12dSBen Shelton {
4947bccd12dSBen Shelton struct ubi_device *ubi = s->private;
4957bccd12dSBen Shelton
4967bccd12dSBen Shelton (*pos)++;
4977bccd12dSBen Shelton
4987bccd12dSBen Shelton if (*pos < ubi->peb_count)
4997bccd12dSBen Shelton return pos;
5007bccd12dSBen Shelton
5017bccd12dSBen Shelton return NULL;
5027bccd12dSBen Shelton }
5037bccd12dSBen Shelton
eraseblk_count_seq_stop(struct seq_file * s,void * v)5047bccd12dSBen Shelton static void eraseblk_count_seq_stop(struct seq_file *s, void *v)
5057bccd12dSBen Shelton {
5067bccd12dSBen Shelton }
5077bccd12dSBen Shelton
eraseblk_count_seq_show(struct seq_file * s,void * iter)5087bccd12dSBen Shelton static int eraseblk_count_seq_show(struct seq_file *s, void *iter)
5097bccd12dSBen Shelton {
5107bccd12dSBen Shelton struct ubi_device *ubi = s->private;
5117bccd12dSBen Shelton struct ubi_wl_entry *wl;
5127bccd12dSBen Shelton int *block_number = iter;
5137bccd12dSBen Shelton int erase_count = -1;
5147bccd12dSBen Shelton int err;
5157bccd12dSBen Shelton
5167bccd12dSBen Shelton /* If this is the start, print a header */
5170e7572cfSRichard Weinberger if (*block_number == 0)
5180e7572cfSRichard Weinberger seq_puts(s, "physical_block_number\terase_count\n");
5197bccd12dSBen Shelton
5207bccd12dSBen Shelton err = ubi_io_is_bad(ubi, *block_number);
5217bccd12dSBen Shelton if (err)
5227bccd12dSBen Shelton return err;
5237bccd12dSBen Shelton
5247bccd12dSBen Shelton spin_lock(&ubi->wl_lock);
5257bccd12dSBen Shelton
5267bccd12dSBen Shelton wl = ubi->lookuptbl[*block_number];
5277bccd12dSBen Shelton if (wl)
5287bccd12dSBen Shelton erase_count = wl->ec;
5297bccd12dSBen Shelton
5307bccd12dSBen Shelton spin_unlock(&ubi->wl_lock);
5317bccd12dSBen Shelton
5327bccd12dSBen Shelton if (erase_count < 0)
5337bccd12dSBen Shelton return 0;
5347bccd12dSBen Shelton
5357bccd12dSBen Shelton seq_printf(s, "%-22d\t%-11d\n", *block_number, erase_count);
5367bccd12dSBen Shelton
5377bccd12dSBen Shelton return 0;
5387bccd12dSBen Shelton }
5397bccd12dSBen Shelton
5407bccd12dSBen Shelton static const struct seq_operations eraseblk_count_seq_ops = {
5417bccd12dSBen Shelton .start = eraseblk_count_seq_start,
5427bccd12dSBen Shelton .next = eraseblk_count_seq_next,
5437bccd12dSBen Shelton .stop = eraseblk_count_seq_stop,
5447bccd12dSBen Shelton .show = eraseblk_count_seq_show
5457bccd12dSBen Shelton };
5467bccd12dSBen Shelton
eraseblk_count_open(struct inode * inode,struct file * f)5477bccd12dSBen Shelton static int eraseblk_count_open(struct inode *inode, struct file *f)
5487bccd12dSBen Shelton {
5497bccd12dSBen Shelton struct seq_file *s;
5507bccd12dSBen Shelton int err;
5517bccd12dSBen Shelton
5527bccd12dSBen Shelton err = seq_open(f, &eraseblk_count_seq_ops);
5537bccd12dSBen Shelton if (err)
5547bccd12dSBen Shelton return err;
5557bccd12dSBen Shelton
5567bccd12dSBen Shelton s = f->private_data;
5577bccd12dSBen Shelton s->private = ubi_get_device((unsigned long)inode->i_private);
5587bccd12dSBen Shelton
5597bccd12dSBen Shelton if (!s->private)
5607bccd12dSBen Shelton return -ENODEV;
5617bccd12dSBen Shelton else
5627bccd12dSBen Shelton return 0;
5637bccd12dSBen Shelton }
5647bccd12dSBen Shelton
eraseblk_count_release(struct inode * inode,struct file * f)5657bccd12dSBen Shelton static int eraseblk_count_release(struct inode *inode, struct file *f)
5667bccd12dSBen Shelton {
5677bccd12dSBen Shelton struct seq_file *s = f->private_data;
5687bccd12dSBen Shelton struct ubi_device *ubi = s->private;
5697bccd12dSBen Shelton
5707bccd12dSBen Shelton ubi_put_device(ubi);
5717bccd12dSBen Shelton
5727bccd12dSBen Shelton return seq_release(inode, f);
5737bccd12dSBen Shelton }
5747bccd12dSBen Shelton
5757bccd12dSBen Shelton static const struct file_operations eraseblk_count_fops = {
5767bccd12dSBen Shelton .owner = THIS_MODULE,
5777bccd12dSBen Shelton .open = eraseblk_count_open,
5787bccd12dSBen Shelton .read = seq_read,
5797bccd12dSBen Shelton .llseek = seq_lseek,
5807bccd12dSBen Shelton .release = eraseblk_count_release,
5817bccd12dSBen Shelton };
5827bccd12dSBen Shelton
5832a734bb8SArtem Bityutskiy /**
5842a734bb8SArtem Bityutskiy * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
5852a734bb8SArtem Bityutskiy * @ubi: UBI device description object
5862a734bb8SArtem Bityutskiy *
5872a734bb8SArtem Bityutskiy * This function creates all debugfs files for UBI device @ubi. Returns zero in
5882a734bb8SArtem Bityutskiy * case of success and a negative error code in case of failure.
5892a734bb8SArtem Bityutskiy */
ubi_debugfs_init_dev(struct ubi_device * ubi)5902a734bb8SArtem Bityutskiy int ubi_debugfs_init_dev(struct ubi_device *ubi)
5912a734bb8SArtem Bityutskiy {
5922a734bb8SArtem Bityutskiy unsigned long ubi_num = ubi->ubi_num;
593eab73772SEzequiel Garcia struct ubi_debug_info *d = &ubi->dbg;
5942de203d8SZhaoLong Wang umode_t mode = S_IRUSR | S_IWUSR;
595c2d73ba8SGreg Kroah-Hartman int n;
5962a734bb8SArtem Bityutskiy
597903e0e4eSBrian Norris if (!IS_ENABLED(CONFIG_DEBUG_FS))
598e9b4cf20SArtem Bityutskiy return 0;
599e9b4cf20SArtem Bityutskiy
600*7037c96dSZhaoLong Wang n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN, UBI_DFS_DIR_NAME,
6012a734bb8SArtem Bityutskiy ubi->ubi_num);
602*7037c96dSZhaoLong Wang if (n >= UBI_DFS_DIR_LEN) {
6032a734bb8SArtem Bityutskiy /* The array size is too small */
604c2d73ba8SGreg Kroah-Hartman return -EINVAL;
6052a734bb8SArtem Bityutskiy }
6062a734bb8SArtem Bityutskiy
607c2d73ba8SGreg Kroah-Hartman d->dfs_dir = debugfs_create_dir(d->dfs_dir_name, dfs_rootdir);
6082a734bb8SArtem Bityutskiy
6092de203d8SZhaoLong Wang d->dfs_chk_gen = debugfs_create_file("chk_gen", mode, d->dfs_dir,
610c2d73ba8SGreg Kroah-Hartman (void *)ubi_num, &dfs_fops);
611c2d73ba8SGreg Kroah-Hartman
6122de203d8SZhaoLong Wang d->dfs_chk_io = debugfs_create_file("chk_io", mode, d->dfs_dir,
613c2d73ba8SGreg Kroah-Hartman (void *)ubi_num, &dfs_fops);
614c2d73ba8SGreg Kroah-Hartman
6152de203d8SZhaoLong Wang d->dfs_chk_fastmap = debugfs_create_file("chk_fastmap", mode,
616c2d73ba8SGreg Kroah-Hartman d->dfs_dir, (void *)ubi_num,
6172a734bb8SArtem Bityutskiy &dfs_fops);
6182a734bb8SArtem Bityutskiy
6192de203d8SZhaoLong Wang d->dfs_disable_bgt = debugfs_create_file("tst_disable_bgt", mode,
620c2d73ba8SGreg Kroah-Hartman d->dfs_dir, (void *)ubi_num,
6212a734bb8SArtem Bityutskiy &dfs_fops);
6222a734bb8SArtem Bityutskiy
623c2d73ba8SGreg Kroah-Hartman d->dfs_emulate_bitflips = debugfs_create_file("tst_emulate_bitflips",
6242de203d8SZhaoLong Wang mode, d->dfs_dir,
625c2d73ba8SGreg Kroah-Hartman (void *)ubi_num,
6265fa7fa5dSRichard Weinberger &dfs_fops);
6275fa7fa5dSRichard Weinberger
628c2d73ba8SGreg Kroah-Hartman d->dfs_emulate_io_failures = debugfs_create_file("tst_emulate_io_failures",
6292de203d8SZhaoLong Wang mode, d->dfs_dir,
630c2d73ba8SGreg Kroah-Hartman (void *)ubi_num,
631cd6d8567SArtem Bityutskiy &dfs_fops);
632cd6d8567SArtem Bityutskiy
633c2d73ba8SGreg Kroah-Hartman d->dfs_emulate_power_cut = debugfs_create_file("tst_emulate_power_cut",
6342de203d8SZhaoLong Wang mode, d->dfs_dir,
635c2d73ba8SGreg Kroah-Hartman (void *)ubi_num,
636cd6d8567SArtem Bityutskiy &dfs_fops);
637cd6d8567SArtem Bityutskiy
638c2d73ba8SGreg Kroah-Hartman d->dfs_power_cut_min = debugfs_create_file("tst_emulate_power_cut_min",
6392de203d8SZhaoLong Wang mode, d->dfs_dir,
640c2d73ba8SGreg Kroah-Hartman (void *)ubi_num, &dfs_fops);
641cd6d8567SArtem Bityutskiy
642c2d73ba8SGreg Kroah-Hartman d->dfs_power_cut_max = debugfs_create_file("tst_emulate_power_cut_max",
6432de203d8SZhaoLong Wang mode, d->dfs_dir,
644c2d73ba8SGreg Kroah-Hartman (void *)ubi_num, &dfs_fops);
64550269067S[email protected]
646c2d73ba8SGreg Kroah-Hartman debugfs_create_file("detailed_erase_block_info", S_IRUSR, d->dfs_dir,
647c2d73ba8SGreg Kroah-Hartman (void *)ubi_num, &eraseblk_count_fops);
6487bccd12dSBen Shelton
6496931fb44SZhaoLong Wang #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
6506931fb44SZhaoLong Wang d->dfs_emulate_failures = debugfs_create_file("emulate_failures",
6516931fb44SZhaoLong Wang mode, d->dfs_dir,
6526931fb44SZhaoLong Wang (void *)ubi_num,
6536931fb44SZhaoLong Wang &dfs_fops);
6546931fb44SZhaoLong Wang #endif
6552a734bb8SArtem Bityutskiy return 0;
6562a734bb8SArtem Bityutskiy }
6572a734bb8SArtem Bityutskiy
6582a734bb8SArtem Bityutskiy /**
659bc7849e2SKai Song * ubi_debugfs_exit_dev - free all debugfs files corresponding to device @ubi
6602a734bb8SArtem Bityutskiy * @ubi: UBI device description object
6612a734bb8SArtem Bityutskiy */
ubi_debugfs_exit_dev(struct ubi_device * ubi)6622a734bb8SArtem Bityutskiy void ubi_debugfs_exit_dev(struct ubi_device *ubi)
6632a734bb8SArtem Bityutskiy {
664903e0e4eSBrian Norris if (IS_ENABLED(CONFIG_DEBUG_FS))
665eab73772SEzequiel Garcia debugfs_remove_recursive(ubi->dbg.dfs_dir);
6662a734bb8SArtem Bityutskiy }
66750269067S[email protected]
66850269067S[email protected] /**
66950269067S[email protected] * ubi_dbg_power_cut - emulate a power cut if it is time to do so
67050269067S[email protected] * @ubi: UBI device description object
67150269067S[email protected] * @caller: Flags set to indicate from where the function is being called
67250269067S[email protected] *
67350269067S[email protected] * Returns non-zero if a power cut was emulated, zero if not.
67450269067S[email protected] */
ubi_dbg_power_cut(struct ubi_device * ubi,int caller)67550269067S[email protected] int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
67650269067S[email protected] {
67750269067S[email protected] unsigned int range;
67850269067S[email protected]
67950269067S[email protected] if ((ubi->dbg.emulate_power_cut & caller) == 0)
68050269067S[email protected] return 0;
68150269067S[email protected]
68250269067S[email protected] if (ubi->dbg.power_cut_counter == 0) {
68350269067S[email protected] ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
68450269067S[email protected]
68550269067S[email protected] if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
68650269067S[email protected] range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
6878032bf12SJason A. Donenfeld ubi->dbg.power_cut_counter += get_random_u32_below(range);
68850269067S[email protected] }
68950269067S[email protected] return 0;
69050269067S[email protected] }
69150269067S[email protected]
69250269067S[email protected] ubi->dbg.power_cut_counter--;
69350269067S[email protected] if (ubi->dbg.power_cut_counter)
69450269067S[email protected] return 0;
69550269067S[email protected]
69650269067S[email protected] return 1;
69750269067S[email protected] }
698