blob: d2b7ca5e33fd0b42aa1649396a40c120f3cdedf3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Park961df832008-11-19 16:25:44 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 *
Kyungmin Park961df832008-11-19 16:25:44 +01005 * Author: Artem Bityutskiy (Битюцкий Артём)
6 */
7
Alexey Brodkinf8c987f2018-06-05 17:17:57 +03008#include <hexdump.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <malloc.h>
Kyungmin Park961df832008-11-19 16:25:44 +010010#include <ubi_uboot.h>
Kyungmin Park961df832008-11-19 16:25:44 +010011#include "ubi.h"
Heiko Schocherff94bc42014-06-24 10:10:04 +020012#ifndef __UBOOT__
13#include <linux/debugfs.h>
Simon Glass61b29b82020-02-03 07:36:15 -070014#include <linux/err.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020015#include <linux/uaccess.h>
16#include <linux/module.h>
17#endif
Kyungmin Park961df832008-11-19 16:25:44 +010018
19/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020020 * ubi_dump_flash - dump a region of flash.
21 * @ubi: UBI device description object
22 * @pnum: the physical eraseblock number to dump
23 * @offset: the starting offset within the physical eraseblock to dump
24 * @len: the length of the region to dump
25 */
26void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
27{
28 int err;
29 size_t read;
30 void *buf;
31 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
32
33 buf = vmalloc(len);
34 if (!buf)
35 return;
36 err = mtd_read(ubi->mtd, addr, len, &read, buf);
37 if (err && err != -EUCLEAN) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +020038 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
Heiko Schocherff94bc42014-06-24 10:10:04 +020039 err, len, pnum, offset, read);
40 goto out;
41 }
42
Heiko Schocher0195a7b2015-10-22 06:19:21 +020043 ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +020044 len, pnum, offset);
Alexey Brodkinf8c987f2018-06-05 17:17:57 +030045 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
Heiko Schocherff94bc42014-06-24 10:10:04 +020046out:
47 vfree(buf);
48 return;
49}
50
51/**
52 * ubi_dump_ec_hdr - dump an erase counter header.
Kyungmin Park961df832008-11-19 16:25:44 +010053 * @ec_hdr: the erase counter header to dump
54 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020055void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
Kyungmin Park961df832008-11-19 16:25:44 +010056{
Heiko Schocherff94bc42014-06-24 10:10:04 +020057 pr_err("Erase counter header dump:\n");
58 pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic));
59 pr_err("\tversion %d\n", (int)ec_hdr->version);
60 pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
61 pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
62 pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset));
63 pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq));
64 pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
65 pr_err("erase counter header hexdump:\n");
Alexey Brodkinf8c987f2018-06-05 17:17:57 +030066 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Kyungmin Park961df832008-11-19 16:25:44 +010067 ec_hdr, UBI_EC_HDR_SIZE, 1);
68}
69
70/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020071 * ubi_dump_vid_hdr - dump a volume identifier header.
Kyungmin Park961df832008-11-19 16:25:44 +010072 * @vid_hdr: the volume identifier header to dump
73 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020074void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
Kyungmin Park961df832008-11-19 16:25:44 +010075{
Heiko Schocherff94bc42014-06-24 10:10:04 +020076 pr_err("Volume identifier header dump:\n");
77 pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
78 pr_err("\tversion %d\n", (int)vid_hdr->version);
79 pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type);
80 pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
81 pr_err("\tcompat %d\n", (int)vid_hdr->compat);
82 pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
83 pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
84 pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
85 pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
86 pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
87 pr_err("\tsqnum %llu\n",
Kyungmin Park961df832008-11-19 16:25:44 +010088 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
Heiko Schocherff94bc42014-06-24 10:10:04 +020089 pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
90 pr_err("Volume identifier header hexdump:\n");
Alexey Brodkinf8c987f2018-06-05 17:17:57 +030091 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Heiko Schocherff94bc42014-06-24 10:10:04 +020092 vid_hdr, UBI_VID_HDR_SIZE, 1);
Kyungmin Park961df832008-11-19 16:25:44 +010093}
94
95/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020096 * ubi_dump_vol_info - dump volume information.
Kyungmin Park961df832008-11-19 16:25:44 +010097 * @vol: UBI volume description object
98 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020099void ubi_dump_vol_info(const struct ubi_volume *vol)
Kyungmin Park961df832008-11-19 16:25:44 +0100100{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200101 printf("Volume information dump:\n");
102 printf("\tvol_id %d\n", vol->vol_id);
103 printf("\treserved_pebs %d\n", vol->reserved_pebs);
104 printf("\talignment %d\n", vol->alignment);
105 printf("\tdata_pad %d\n", vol->data_pad);
106 printf("\tvol_type %d\n", vol->vol_type);
107 printf("\tname_len %d\n", vol->name_len);
108 printf("\tusable_leb_size %d\n", vol->usable_leb_size);
109 printf("\tused_ebs %d\n", vol->used_ebs);
110 printf("\tused_bytes %lld\n", vol->used_bytes);
111 printf("\tlast_eb_bytes %d\n", vol->last_eb_bytes);
112 printf("\tcorrupted %d\n", vol->corrupted);
113 printf("\tupd_marker %d\n", vol->upd_marker);
Stefan Roese524acec2019-09-12 16:41:02 +0200114 printf("\tskip_check %d\n", vol->skip_check);
Kyungmin Park961df832008-11-19 16:25:44 +0100115
116 if (vol->name_len <= UBI_VOL_NAME_MAX &&
117 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200118 printf("\tname %s\n", vol->name);
Kyungmin Park961df832008-11-19 16:25:44 +0100119 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200120 printf("\t1st 5 characters of name: %c%c%c%c%c\n",
121 vol->name[0], vol->name[1], vol->name[2],
122 vol->name[3], vol->name[4]);
Kyungmin Park961df832008-11-19 16:25:44 +0100123 }
124}
125
126/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200127 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
Kyungmin Park961df832008-11-19 16:25:44 +0100128 * @r: the object to dump
129 * @idx: volume table index
130 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200131void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
Kyungmin Park961df832008-11-19 16:25:44 +0100132{
133 int name_len = be16_to_cpu(r->name_len);
134
Heiko Schocherff94bc42014-06-24 10:10:04 +0200135 pr_err("Volume table record %d dump:\n", idx);
136 pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs));
137 pr_err("\talignment %d\n", be32_to_cpu(r->alignment));
138 pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad));
139 pr_err("\tvol_type %d\n", (int)r->vol_type);
140 pr_err("\tupd_marker %d\n", (int)r->upd_marker);
141 pr_err("\tname_len %d\n", name_len);
Kyungmin Park961df832008-11-19 16:25:44 +0100142
143 if (r->name[0] == '\0') {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200144 pr_err("\tname NULL\n");
Kyungmin Park961df832008-11-19 16:25:44 +0100145 return;
146 }
147
148 if (name_len <= UBI_VOL_NAME_MAX &&
149 strnlen(&r->name[0], name_len + 1) == name_len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200150 pr_err("\tname %s\n", &r->name[0]);
Kyungmin Park961df832008-11-19 16:25:44 +0100151 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200152 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
Kyungmin Park961df832008-11-19 16:25:44 +0100153 r->name[0], r->name[1], r->name[2], r->name[3],
154 r->name[4]);
155 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200156 pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
Kyungmin Park961df832008-11-19 16:25:44 +0100157}
158
159/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200160 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
161 * @av: the object to dump
Kyungmin Park961df832008-11-19 16:25:44 +0100162 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200163void ubi_dump_av(const struct ubi_ainf_volume *av)
Kyungmin Park961df832008-11-19 16:25:44 +0100164{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200165 pr_err("Volume attaching information dump:\n");
166 pr_err("\tvol_id %d\n", av->vol_id);
167 pr_err("\thighest_lnum %d\n", av->highest_lnum);
168 pr_err("\tleb_count %d\n", av->leb_count);
169 pr_err("\tcompat %d\n", av->compat);
170 pr_err("\tvol_type %d\n", av->vol_type);
171 pr_err("\tused_ebs %d\n", av->used_ebs);
172 pr_err("\tlast_data_size %d\n", av->last_data_size);
173 pr_err("\tdata_pad %d\n", av->data_pad);
Kyungmin Park961df832008-11-19 16:25:44 +0100174}
175
176/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200177 * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
178 * @aeb: the object to dump
Kyungmin Park961df832008-11-19 16:25:44 +0100179 * @type: object type: 0 - not corrupted, 1 - corrupted
180 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200181void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
Kyungmin Park961df832008-11-19 16:25:44 +0100182{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200183 pr_err("eraseblock attaching information dump:\n");
184 pr_err("\tec %d\n", aeb->ec);
185 pr_err("\tpnum %d\n", aeb->pnum);
Kyungmin Park961df832008-11-19 16:25:44 +0100186 if (type == 0) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200187 pr_err("\tlnum %d\n", aeb->lnum);
188 pr_err("\tscrub %d\n", aeb->scrub);
189 pr_err("\tsqnum %llu\n", aeb->sqnum);
Kyungmin Park961df832008-11-19 16:25:44 +0100190 }
191}
192
193/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200194 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
Kyungmin Park961df832008-11-19 16:25:44 +0100195 * @req: the object to dump
196 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200197void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
Kyungmin Park961df832008-11-19 16:25:44 +0100198{
199 char nm[17];
200
Heiko Schocherff94bc42014-06-24 10:10:04 +0200201 pr_err("Volume creation request dump:\n");
202 pr_err("\tvol_id %d\n", req->vol_id);
203 pr_err("\talignment %d\n", req->alignment);
204 pr_err("\tbytes %lld\n", (long long)req->bytes);
205 pr_err("\tvol_type %d\n", req->vol_type);
206 pr_err("\tname_len %d\n", req->name_len);
Kyungmin Park961df832008-11-19 16:25:44 +0100207
208 memcpy(nm, req->name, 16);
209 nm[16] = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200210 pr_err("\t1st 16 characters of name: %s\n", nm);
Kyungmin Park961df832008-11-19 16:25:44 +0100211}
212
Heiko Schocherff94bc42014-06-24 10:10:04 +0200213#ifndef __UBOOT__
214/*
215 * Root directory for UBI stuff in debugfs. Contains sub-directories which
216 * contain the stuff specific to particular UBI devices.
217 */
218static struct dentry *dfs_rootdir;
219
220/**
221 * ubi_debugfs_init - create UBI debugfs directory.
222 *
223 * Create UBI debugfs directory. Returns zero in case of success and a negative
224 * error code in case of failure.
225 */
226int ubi_debugfs_init(void)
227{
228 if (!IS_ENABLED(CONFIG_DEBUG_FS))
229 return 0;
230
231 dfs_rootdir = debugfs_create_dir("ubi", NULL);
232 if (IS_ERR_OR_NULL(dfs_rootdir)) {
233 int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
234
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200235 pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
236 err);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200237 return err;
238 }
239
240 return 0;
241}
242
243/**
244 * ubi_debugfs_exit - remove UBI debugfs directory.
245 */
246void ubi_debugfs_exit(void)
247{
248 if (IS_ENABLED(CONFIG_DEBUG_FS))
249 debugfs_remove(dfs_rootdir);
250}
251
252/* Read an UBI debugfs file */
253static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
254 size_t count, loff_t *ppos)
255{
256 unsigned long ubi_num = (unsigned long)file->private_data;
257 struct dentry *dent = file->f_path.dentry;
258 struct ubi_device *ubi;
259 struct ubi_debug_info *d;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200260 char buf[8];
Heiko Schocherff94bc42014-06-24 10:10:04 +0200261 int val;
262
263 ubi = ubi_get_device(ubi_num);
264 if (!ubi)
265 return -ENODEV;
266 d = &ubi->dbg;
267
268 if (dent == d->dfs_chk_gen)
269 val = d->chk_gen;
270 else if (dent == d->dfs_chk_io)
271 val = d->chk_io;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200272 else if (dent == d->dfs_chk_fastmap)
273 val = d->chk_fastmap;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200274 else if (dent == d->dfs_disable_bgt)
275 val = d->disable_bgt;
276 else if (dent == d->dfs_emulate_bitflips)
277 val = d->emulate_bitflips;
278 else if (dent == d->dfs_emulate_io_failures)
279 val = d->emulate_io_failures;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200280 else if (dent == d->dfs_emulate_power_cut) {
281 snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
282 count = simple_read_from_buffer(user_buf, count, ppos,
283 buf, strlen(buf));
284 goto out;
285 } else if (dent == d->dfs_power_cut_min) {
286 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
287 count = simple_read_from_buffer(user_buf, count, ppos,
288 buf, strlen(buf));
289 goto out;
290 } else if (dent == d->dfs_power_cut_max) {
291 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
292 count = simple_read_from_buffer(user_buf, count, ppos,
293 buf, strlen(buf));
294 goto out;
295 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200296 else {
297 count = -EINVAL;
298 goto out;
299 }
300
301 if (val)
302 buf[0] = '1';
303 else
304 buf[0] = '0';
305 buf[1] = '\n';
306 buf[2] = 0x00;
307
308 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
309
310out:
311 ubi_put_device(ubi);
312 return count;
313}
314
315/* Write an UBI debugfs file */
316static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
317 size_t count, loff_t *ppos)
318{
319 unsigned long ubi_num = (unsigned long)file->private_data;
320 struct dentry *dent = file->f_path.dentry;
321 struct ubi_device *ubi;
322 struct ubi_debug_info *d;
323 size_t buf_size;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200324 char buf[8] = {0};
Heiko Schocherff94bc42014-06-24 10:10:04 +0200325 int val;
326
327 ubi = ubi_get_device(ubi_num);
328 if (!ubi)
329 return -ENODEV;
330 d = &ubi->dbg;
331
332 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
333 if (copy_from_user(buf, user_buf, buf_size)) {
334 count = -EFAULT;
335 goto out;
336 }
337
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200338 if (dent == d->dfs_power_cut_min) {
339 if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
340 count = -EINVAL;
341 goto out;
342 } else if (dent == d->dfs_power_cut_max) {
343 if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
344 count = -EINVAL;
345 goto out;
346 } else if (dent == d->dfs_emulate_power_cut) {
347 if (kstrtoint(buf, 0, &val) != 0)
348 count = -EINVAL;
349 d->emulate_power_cut = val;
350 goto out;
351 }
352
Heiko Schocherff94bc42014-06-24 10:10:04 +0200353 if (buf[0] == '1')
354 val = 1;
355 else if (buf[0] == '0')
356 val = 0;
357 else {
358 count = -EINVAL;
359 goto out;
360 }
361
362 if (dent == d->dfs_chk_gen)
363 d->chk_gen = val;
364 else if (dent == d->dfs_chk_io)
365 d->chk_io = val;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200366 else if (dent == d->dfs_chk_fastmap)
367 d->chk_fastmap = val;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200368 else if (dent == d->dfs_disable_bgt)
369 d->disable_bgt = val;
370 else if (dent == d->dfs_emulate_bitflips)
371 d->emulate_bitflips = val;
372 else if (dent == d->dfs_emulate_io_failures)
373 d->emulate_io_failures = val;
374 else
375 count = -EINVAL;
376
377out:
378 ubi_put_device(ubi);
379 return count;
380}
381
382/* File operations for all UBI debugfs files */
383static const struct file_operations dfs_fops = {
384 .read = dfs_file_read,
385 .write = dfs_file_write,
386 .open = simple_open,
387 .llseek = no_llseek,
388 .owner = THIS_MODULE,
389};
390
391/**
392 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
393 * @ubi: UBI device description object
394 *
395 * This function creates all debugfs files for UBI device @ubi. Returns zero in
396 * case of success and a negative error code in case of failure.
397 */
398int ubi_debugfs_init_dev(struct ubi_device *ubi)
399{
400 int err, n;
401 unsigned long ubi_num = ubi->ubi_num;
402 const char *fname;
403 struct dentry *dent;
404 struct ubi_debug_info *d = &ubi->dbg;
405
406 if (!IS_ENABLED(CONFIG_DEBUG_FS))
407 return 0;
408
409 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
410 ubi->ubi_num);
411 if (n == UBI_DFS_DIR_LEN) {
412 /* The array size is too small */
413 fname = UBI_DFS_DIR_NAME;
414 dent = ERR_PTR(-EINVAL);
415 goto out;
416 }
417
418 fname = d->dfs_dir_name;
419 dent = debugfs_create_dir(fname, dfs_rootdir);
420 if (IS_ERR_OR_NULL(dent))
421 goto out;
422 d->dfs_dir = dent;
423
424 fname = "chk_gen";
425 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
426 &dfs_fops);
427 if (IS_ERR_OR_NULL(dent))
428 goto out_remove;
429 d->dfs_chk_gen = dent;
430
431 fname = "chk_io";
432 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
433 &dfs_fops);
434 if (IS_ERR_OR_NULL(dent))
435 goto out_remove;
436 d->dfs_chk_io = dent;
437
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200438 fname = "chk_fastmap";
439 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
440 &dfs_fops);
441 if (IS_ERR_OR_NULL(dent))
442 goto out_remove;
443 d->dfs_chk_fastmap = dent;
444
Heiko Schocherff94bc42014-06-24 10:10:04 +0200445 fname = "tst_disable_bgt";
446 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
447 &dfs_fops);
448 if (IS_ERR_OR_NULL(dent))
449 goto out_remove;
450 d->dfs_disable_bgt = dent;
451
452 fname = "tst_emulate_bitflips";
453 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
454 &dfs_fops);
455 if (IS_ERR_OR_NULL(dent))
456 goto out_remove;
457 d->dfs_emulate_bitflips = dent;
458
459 fname = "tst_emulate_io_failures";
460 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
461 &dfs_fops);
462 if (IS_ERR_OR_NULL(dent))
463 goto out_remove;
464 d->dfs_emulate_io_failures = dent;
465
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200466 fname = "tst_emulate_power_cut";
467 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
468 &dfs_fops);
469 if (IS_ERR_OR_NULL(dent))
470 goto out_remove;
471 d->dfs_emulate_power_cut = dent;
472
473 fname = "tst_emulate_power_cut_min";
474 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
475 &dfs_fops);
476 if (IS_ERR_OR_NULL(dent))
477 goto out_remove;
478 d->dfs_power_cut_min = dent;
479
480 fname = "tst_emulate_power_cut_max";
481 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
482 &dfs_fops);
483 if (IS_ERR_OR_NULL(dent))
484 goto out_remove;
485 d->dfs_power_cut_max = dent;
486
Heiko Schocherff94bc42014-06-24 10:10:04 +0200487 return 0;
488
489out_remove:
490 debugfs_remove_recursive(d->dfs_dir);
491out:
492 err = dent ? PTR_ERR(dent) : -ENODEV;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200493 ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200494 fname, err);
495 return err;
496}
497
498/**
499 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
500 * @ubi: UBI device description object
501 */
502void ubi_debugfs_exit_dev(struct ubi_device *ubi)
503{
504 if (IS_ENABLED(CONFIG_DEBUG_FS))
505 debugfs_remove_recursive(ubi->dbg.dfs_dir);
506}
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200507
508/**
509 * ubi_dbg_power_cut - emulate a power cut if it is time to do so
510 * @ubi: UBI device description object
511 * @caller: Flags set to indicate from where the function is being called
512 *
513 * Returns non-zero if a power cut was emulated, zero if not.
514 */
515int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
516{
517 unsigned int range;
518
519 if ((ubi->dbg.emulate_power_cut & caller) == 0)
520 return 0;
521
522 if (ubi->dbg.power_cut_counter == 0) {
523 ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
524
525 if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
526 range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
527 ubi->dbg.power_cut_counter += prandom_u32() % range;
528 }
529 return 0;
530 }
531
532 ubi->dbg.power_cut_counter--;
533 if (ubi->dbg.power_cut_counter)
534 return 0;
535
536 ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
537 ubi_ro_mode(ubi);
538 return 1;
539}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200540#else
541int ubi_debugfs_init(void)
542{
543 return 0;
544}
545
546void ubi_debugfs_exit(void)
547{
548}
549
550int ubi_debugfs_init_dev(struct ubi_device *ubi)
551{
552 return 0;
553}
554
555void ubi_debugfs_exit_dev(struct ubi_device *ubi)
556{
557}
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200558
559int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
560{
561 return 0;
562}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200563#endif