blob: b119cb6e9c05dd3c5243a019c407dedba4176504 [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>
Simon Glass1e94b462023-09-14 18:21:46 -060011#include <linux/printk.h>
Kyungmin Park961df832008-11-19 16:25:44 +010012#include "ubi.h"
Heiko Schocherff94bc42014-06-24 10:10:04 +020013#ifndef __UBOOT__
14#include <linux/debugfs.h>
Simon Glass61b29b82020-02-03 07:36:15 -070015#include <linux/err.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020016#include <linux/uaccess.h>
17#include <linux/module.h>
18#endif
Kyungmin Park961df832008-11-19 16:25:44 +010019
20/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020021 * ubi_dump_flash - dump a region of flash.
22 * @ubi: UBI device description object
23 * @pnum: the physical eraseblock number to dump
24 * @offset: the starting offset within the physical eraseblock to dump
25 * @len: the length of the region to dump
26 */
27void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
28{
29 int err;
30 size_t read;
31 void *buf;
32 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
33
34 buf = vmalloc(len);
35 if (!buf)
36 return;
37 err = mtd_read(ubi->mtd, addr, len, &read, buf);
38 if (err && err != -EUCLEAN) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +020039 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
Heiko Schocherff94bc42014-06-24 10:10:04 +020040 err, len, pnum, offset, read);
41 goto out;
42 }
43
Heiko Schocher0195a7b2015-10-22 06:19:21 +020044 ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +020045 len, pnum, offset);
Alexey Brodkinf8c987f2018-06-05 17:17:57 +030046 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
Heiko Schocherff94bc42014-06-24 10:10:04 +020047out:
48 vfree(buf);
49 return;
50}
51
52/**
53 * ubi_dump_ec_hdr - dump an erase counter header.
Kyungmin Park961df832008-11-19 16:25:44 +010054 * @ec_hdr: the erase counter header to dump
55 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020056void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
Kyungmin Park961df832008-11-19 16:25:44 +010057{
Heiko Schocherff94bc42014-06-24 10:10:04 +020058 pr_err("Erase counter header dump:\n");
59 pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic));
60 pr_err("\tversion %d\n", (int)ec_hdr->version);
61 pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
62 pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
63 pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset));
64 pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq));
65 pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
66 pr_err("erase counter header hexdump:\n");
Alexey Brodkinf8c987f2018-06-05 17:17:57 +030067 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Kyungmin Park961df832008-11-19 16:25:44 +010068 ec_hdr, UBI_EC_HDR_SIZE, 1);
69}
70
71/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020072 * ubi_dump_vid_hdr - dump a volume identifier header.
Kyungmin Park961df832008-11-19 16:25:44 +010073 * @vid_hdr: the volume identifier header to dump
74 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020075void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
Kyungmin Park961df832008-11-19 16:25:44 +010076{
Heiko Schocherff94bc42014-06-24 10:10:04 +020077 pr_err("Volume identifier header dump:\n");
78 pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
79 pr_err("\tversion %d\n", (int)vid_hdr->version);
80 pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type);
81 pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
82 pr_err("\tcompat %d\n", (int)vid_hdr->compat);
83 pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
84 pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
85 pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
86 pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
87 pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
88 pr_err("\tsqnum %llu\n",
Kyungmin Park961df832008-11-19 16:25:44 +010089 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
Heiko Schocherff94bc42014-06-24 10:10:04 +020090 pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
91 pr_err("Volume identifier header hexdump:\n");
Alexey Brodkinf8c987f2018-06-05 17:17:57 +030092 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Heiko Schocherff94bc42014-06-24 10:10:04 +020093 vid_hdr, UBI_VID_HDR_SIZE, 1);
Kyungmin Park961df832008-11-19 16:25:44 +010094}
95
96/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020097 * ubi_dump_vol_info - dump volume information.
Kyungmin Park961df832008-11-19 16:25:44 +010098 * @vol: UBI volume description object
99 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200100void ubi_dump_vol_info(const struct ubi_volume *vol)
Kyungmin Park961df832008-11-19 16:25:44 +0100101{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200102 printf("Volume information dump:\n");
103 printf("\tvol_id %d\n", vol->vol_id);
104 printf("\treserved_pebs %d\n", vol->reserved_pebs);
105 printf("\talignment %d\n", vol->alignment);
106 printf("\tdata_pad %d\n", vol->data_pad);
107 printf("\tvol_type %d\n", vol->vol_type);
108 printf("\tname_len %d\n", vol->name_len);
109 printf("\tusable_leb_size %d\n", vol->usable_leb_size);
110 printf("\tused_ebs %d\n", vol->used_ebs);
111 printf("\tused_bytes %lld\n", vol->used_bytes);
112 printf("\tlast_eb_bytes %d\n", vol->last_eb_bytes);
113 printf("\tcorrupted %d\n", vol->corrupted);
114 printf("\tupd_marker %d\n", vol->upd_marker);
Stefan Roese524acec2019-09-12 16:41:02 +0200115 printf("\tskip_check %d\n", vol->skip_check);
Kyungmin Park961df832008-11-19 16:25:44 +0100116
117 if (vol->name_len <= UBI_VOL_NAME_MAX &&
118 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200119 printf("\tname %s\n", vol->name);
Kyungmin Park961df832008-11-19 16:25:44 +0100120 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200121 printf("\t1st 5 characters of name: %c%c%c%c%c\n",
122 vol->name[0], vol->name[1], vol->name[2],
123 vol->name[3], vol->name[4]);
Kyungmin Park961df832008-11-19 16:25:44 +0100124 }
125}
126
127/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200128 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
Kyungmin Park961df832008-11-19 16:25:44 +0100129 * @r: the object to dump
130 * @idx: volume table index
131 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200132void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
Kyungmin Park961df832008-11-19 16:25:44 +0100133{
134 int name_len = be16_to_cpu(r->name_len);
135
Heiko Schocherff94bc42014-06-24 10:10:04 +0200136 pr_err("Volume table record %d dump:\n", idx);
137 pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs));
138 pr_err("\talignment %d\n", be32_to_cpu(r->alignment));
139 pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad));
140 pr_err("\tvol_type %d\n", (int)r->vol_type);
141 pr_err("\tupd_marker %d\n", (int)r->upd_marker);
142 pr_err("\tname_len %d\n", name_len);
Kyungmin Park961df832008-11-19 16:25:44 +0100143
144 if (r->name[0] == '\0') {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200145 pr_err("\tname NULL\n");
Kyungmin Park961df832008-11-19 16:25:44 +0100146 return;
147 }
148
149 if (name_len <= UBI_VOL_NAME_MAX &&
150 strnlen(&r->name[0], name_len + 1) == name_len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200151 pr_err("\tname %s\n", &r->name[0]);
Kyungmin Park961df832008-11-19 16:25:44 +0100152 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200153 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
Kyungmin Park961df832008-11-19 16:25:44 +0100154 r->name[0], r->name[1], r->name[2], r->name[3],
155 r->name[4]);
156 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200157 pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
Kyungmin Park961df832008-11-19 16:25:44 +0100158}
159
160/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200161 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
162 * @av: the object to dump
Kyungmin Park961df832008-11-19 16:25:44 +0100163 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200164void ubi_dump_av(const struct ubi_ainf_volume *av)
Kyungmin Park961df832008-11-19 16:25:44 +0100165{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200166 pr_err("Volume attaching information dump:\n");
167 pr_err("\tvol_id %d\n", av->vol_id);
168 pr_err("\thighest_lnum %d\n", av->highest_lnum);
169 pr_err("\tleb_count %d\n", av->leb_count);
170 pr_err("\tcompat %d\n", av->compat);
171 pr_err("\tvol_type %d\n", av->vol_type);
172 pr_err("\tused_ebs %d\n", av->used_ebs);
173 pr_err("\tlast_data_size %d\n", av->last_data_size);
174 pr_err("\tdata_pad %d\n", av->data_pad);
Kyungmin Park961df832008-11-19 16:25:44 +0100175}
176
177/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200178 * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
179 * @aeb: the object to dump
Kyungmin Park961df832008-11-19 16:25:44 +0100180 * @type: object type: 0 - not corrupted, 1 - corrupted
181 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200182void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
Kyungmin Park961df832008-11-19 16:25:44 +0100183{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200184 pr_err("eraseblock attaching information dump:\n");
185 pr_err("\tec %d\n", aeb->ec);
186 pr_err("\tpnum %d\n", aeb->pnum);
Kyungmin Park961df832008-11-19 16:25:44 +0100187 if (type == 0) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200188 pr_err("\tlnum %d\n", aeb->lnum);
189 pr_err("\tscrub %d\n", aeb->scrub);
190 pr_err("\tsqnum %llu\n", aeb->sqnum);
Kyungmin Park961df832008-11-19 16:25:44 +0100191 }
192}
193
194/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200195 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
Kyungmin Park961df832008-11-19 16:25:44 +0100196 * @req: the object to dump
197 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200198void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
Kyungmin Park961df832008-11-19 16:25:44 +0100199{
200 char nm[17];
201
Heiko Schocherff94bc42014-06-24 10:10:04 +0200202 pr_err("Volume creation request dump:\n");
203 pr_err("\tvol_id %d\n", req->vol_id);
204 pr_err("\talignment %d\n", req->alignment);
205 pr_err("\tbytes %lld\n", (long long)req->bytes);
206 pr_err("\tvol_type %d\n", req->vol_type);
207 pr_err("\tname_len %d\n", req->name_len);
Kyungmin Park961df832008-11-19 16:25:44 +0100208
209 memcpy(nm, req->name, 16);
210 nm[16] = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200211 pr_err("\t1st 16 characters of name: %s\n", nm);
Kyungmin Park961df832008-11-19 16:25:44 +0100212}
213
Heiko Schocherff94bc42014-06-24 10:10:04 +0200214#ifndef __UBOOT__
215/*
216 * Root directory for UBI stuff in debugfs. Contains sub-directories which
217 * contain the stuff specific to particular UBI devices.
218 */
219static struct dentry *dfs_rootdir;
220
221/**
222 * ubi_debugfs_init - create UBI debugfs directory.
223 *
224 * Create UBI debugfs directory. Returns zero in case of success and a negative
225 * error code in case of failure.
226 */
227int ubi_debugfs_init(void)
228{
229 if (!IS_ENABLED(CONFIG_DEBUG_FS))
230 return 0;
231
232 dfs_rootdir = debugfs_create_dir("ubi", NULL);
233 if (IS_ERR_OR_NULL(dfs_rootdir)) {
234 int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
235
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200236 pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
237 err);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200238 return err;
239 }
240
241 return 0;
242}
243
244/**
245 * ubi_debugfs_exit - remove UBI debugfs directory.
246 */
247void ubi_debugfs_exit(void)
248{
249 if (IS_ENABLED(CONFIG_DEBUG_FS))
250 debugfs_remove(dfs_rootdir);
251}
252
253/* Read an UBI debugfs file */
254static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
255 size_t count, loff_t *ppos)
256{
257 unsigned long ubi_num = (unsigned long)file->private_data;
258 struct dentry *dent = file->f_path.dentry;
259 struct ubi_device *ubi;
260 struct ubi_debug_info *d;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200261 char buf[8];
Heiko Schocherff94bc42014-06-24 10:10:04 +0200262 int val;
263
264 ubi = ubi_get_device(ubi_num);
265 if (!ubi)
266 return -ENODEV;
267 d = &ubi->dbg;
268
269 if (dent == d->dfs_chk_gen)
270 val = d->chk_gen;
271 else if (dent == d->dfs_chk_io)
272 val = d->chk_io;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200273 else if (dent == d->dfs_chk_fastmap)
274 val = d->chk_fastmap;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200275 else if (dent == d->dfs_disable_bgt)
276 val = d->disable_bgt;
277 else if (dent == d->dfs_emulate_bitflips)
278 val = d->emulate_bitflips;
279 else if (dent == d->dfs_emulate_io_failures)
280 val = d->emulate_io_failures;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200281 else if (dent == d->dfs_emulate_power_cut) {
282 snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
283 count = simple_read_from_buffer(user_buf, count, ppos,
284 buf, strlen(buf));
285 goto out;
286 } else if (dent == d->dfs_power_cut_min) {
287 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
288 count = simple_read_from_buffer(user_buf, count, ppos,
289 buf, strlen(buf));
290 goto out;
291 } else if (dent == d->dfs_power_cut_max) {
292 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
293 count = simple_read_from_buffer(user_buf, count, ppos,
294 buf, strlen(buf));
295 goto out;
296 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200297 else {
298 count = -EINVAL;
299 goto out;
300 }
301
302 if (val)
303 buf[0] = '1';
304 else
305 buf[0] = '0';
306 buf[1] = '\n';
307 buf[2] = 0x00;
308
309 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
310
311out:
312 ubi_put_device(ubi);
313 return count;
314}
315
316/* Write an UBI debugfs file */
317static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
318 size_t count, loff_t *ppos)
319{
320 unsigned long ubi_num = (unsigned long)file->private_data;
321 struct dentry *dent = file->f_path.dentry;
322 struct ubi_device *ubi;
323 struct ubi_debug_info *d;
324 size_t buf_size;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200325 char buf[8] = {0};
Heiko Schocherff94bc42014-06-24 10:10:04 +0200326 int val;
327
328 ubi = ubi_get_device(ubi_num);
329 if (!ubi)
330 return -ENODEV;
331 d = &ubi->dbg;
332
333 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
334 if (copy_from_user(buf, user_buf, buf_size)) {
335 count = -EFAULT;
336 goto out;
337 }
338
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200339 if (dent == d->dfs_power_cut_min) {
340 if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
341 count = -EINVAL;
342 goto out;
343 } else if (dent == d->dfs_power_cut_max) {
344 if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
345 count = -EINVAL;
346 goto out;
347 } else if (dent == d->dfs_emulate_power_cut) {
348 if (kstrtoint(buf, 0, &val) != 0)
349 count = -EINVAL;
350 d->emulate_power_cut = val;
351 goto out;
352 }
353
Heiko Schocherff94bc42014-06-24 10:10:04 +0200354 if (buf[0] == '1')
355 val = 1;
356 else if (buf[0] == '0')
357 val = 0;
358 else {
359 count = -EINVAL;
360 goto out;
361 }
362
363 if (dent == d->dfs_chk_gen)
364 d->chk_gen = val;
365 else if (dent == d->dfs_chk_io)
366 d->chk_io = val;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200367 else if (dent == d->dfs_chk_fastmap)
368 d->chk_fastmap = val;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200369 else if (dent == d->dfs_disable_bgt)
370 d->disable_bgt = val;
371 else if (dent == d->dfs_emulate_bitflips)
372 d->emulate_bitflips = val;
373 else if (dent == d->dfs_emulate_io_failures)
374 d->emulate_io_failures = val;
375 else
376 count = -EINVAL;
377
378out:
379 ubi_put_device(ubi);
380 return count;
381}
382
383/* File operations for all UBI debugfs files */
384static const struct file_operations dfs_fops = {
385 .read = dfs_file_read,
386 .write = dfs_file_write,
387 .open = simple_open,
388 .llseek = no_llseek,
389 .owner = THIS_MODULE,
390};
391
392/**
393 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
394 * @ubi: UBI device description object
395 *
396 * This function creates all debugfs files for UBI device @ubi. Returns zero in
397 * case of success and a negative error code in case of failure.
398 */
399int ubi_debugfs_init_dev(struct ubi_device *ubi)
400{
401 int err, n;
402 unsigned long ubi_num = ubi->ubi_num;
403 const char *fname;
404 struct dentry *dent;
405 struct ubi_debug_info *d = &ubi->dbg;
406
407 if (!IS_ENABLED(CONFIG_DEBUG_FS))
408 return 0;
409
410 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
411 ubi->ubi_num);
412 if (n == UBI_DFS_DIR_LEN) {
413 /* The array size is too small */
414 fname = UBI_DFS_DIR_NAME;
415 dent = ERR_PTR(-EINVAL);
416 goto out;
417 }
418
419 fname = d->dfs_dir_name;
420 dent = debugfs_create_dir(fname, dfs_rootdir);
421 if (IS_ERR_OR_NULL(dent))
422 goto out;
423 d->dfs_dir = dent;
424
425 fname = "chk_gen";
426 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
427 &dfs_fops);
428 if (IS_ERR_OR_NULL(dent))
429 goto out_remove;
430 d->dfs_chk_gen = dent;
431
432 fname = "chk_io";
433 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
434 &dfs_fops);
435 if (IS_ERR_OR_NULL(dent))
436 goto out_remove;
437 d->dfs_chk_io = dent;
438
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200439 fname = "chk_fastmap";
440 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
441 &dfs_fops);
442 if (IS_ERR_OR_NULL(dent))
443 goto out_remove;
444 d->dfs_chk_fastmap = dent;
445
Heiko Schocherff94bc42014-06-24 10:10:04 +0200446 fname = "tst_disable_bgt";
447 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
448 &dfs_fops);
449 if (IS_ERR_OR_NULL(dent))
450 goto out_remove;
451 d->dfs_disable_bgt = dent;
452
453 fname = "tst_emulate_bitflips";
454 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
455 &dfs_fops);
456 if (IS_ERR_OR_NULL(dent))
457 goto out_remove;
458 d->dfs_emulate_bitflips = dent;
459
460 fname = "tst_emulate_io_failures";
461 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
462 &dfs_fops);
463 if (IS_ERR_OR_NULL(dent))
464 goto out_remove;
465 d->dfs_emulate_io_failures = dent;
466
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200467 fname = "tst_emulate_power_cut";
468 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
469 &dfs_fops);
470 if (IS_ERR_OR_NULL(dent))
471 goto out_remove;
472 d->dfs_emulate_power_cut = dent;
473
474 fname = "tst_emulate_power_cut_min";
475 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
476 &dfs_fops);
477 if (IS_ERR_OR_NULL(dent))
478 goto out_remove;
479 d->dfs_power_cut_min = dent;
480
481 fname = "tst_emulate_power_cut_max";
482 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
483 &dfs_fops);
484 if (IS_ERR_OR_NULL(dent))
485 goto out_remove;
486 d->dfs_power_cut_max = dent;
487
Heiko Schocherff94bc42014-06-24 10:10:04 +0200488 return 0;
489
490out_remove:
491 debugfs_remove_recursive(d->dfs_dir);
492out:
493 err = dent ? PTR_ERR(dent) : -ENODEV;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200494 ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200495 fname, err);
496 return err;
497}
498
499/**
500 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
501 * @ubi: UBI device description object
502 */
503void ubi_debugfs_exit_dev(struct ubi_device *ubi)
504{
505 if (IS_ENABLED(CONFIG_DEBUG_FS))
506 debugfs_remove_recursive(ubi->dbg.dfs_dir);
507}
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200508
509/**
510 * ubi_dbg_power_cut - emulate a power cut if it is time to do so
511 * @ubi: UBI device description object
512 * @caller: Flags set to indicate from where the function is being called
513 *
514 * Returns non-zero if a power cut was emulated, zero if not.
515 */
516int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
517{
518 unsigned int range;
519
520 if ((ubi->dbg.emulate_power_cut & caller) == 0)
521 return 0;
522
523 if (ubi->dbg.power_cut_counter == 0) {
524 ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
525
526 if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
527 range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
528 ubi->dbg.power_cut_counter += prandom_u32() % range;
529 }
530 return 0;
531 }
532
533 ubi->dbg.power_cut_counter--;
534 if (ubi->dbg.power_cut_counter)
535 return 0;
536
537 ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
538 ubi_ro_mode(ubi);
539 return 1;
540}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200541#else
542int ubi_debugfs_init(void)
543{
544 return 0;
545}
546
547void ubi_debugfs_exit(void)
548{
549}
550
551int ubi_debugfs_init_dev(struct ubi_device *ubi)
552{
553 return 0;
554}
555
556void ubi_debugfs_exit_dev(struct ubi_device *ubi)
557{
558}
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200559
560int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
561{
562 return 0;
563}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200564#endif