blob: f3d348da833b2cff1dabe23970236eaa7a8425de [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkc880c532008-11-19 16:25:44 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 *
Kyungmin Parkc880c532008-11-19 16:25:44 +01005 * Author: Artem Bityutskiy (Битюцкий Артём)
6 */
7
Alexey Brodkin2d2fa492018-06-05 17:17:57 +03008#include <hexdump.h>
Kyungmin Parkc880c532008-11-19 16:25:44 +01009#include <ubi_uboot.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020010#include "ubi.h"
Heiko Schocherf5895d12014-06-24 10:10:04 +020011#ifndef __UBOOT__
12#include <linux/debugfs.h>
13#include <linux/uaccess.h>
14#include <linux/module.h>
15#endif
16
17/**
18 * ubi_dump_flash - dump a region of flash.
19 * @ubi: UBI device description object
20 * @pnum: the physical eraseblock number to dump
21 * @offset: the starting offset within the physical eraseblock to dump
22 * @len: the length of the region to dump
23 */
24void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
25{
26 int err;
27 size_t read;
28 void *buf;
29 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
Kyungmin Parkc880c532008-11-19 16:25:44 +010030
Heiko Schocherf5895d12014-06-24 10:10:04 +020031 buf = vmalloc(len);
32 if (!buf)
33 return;
34 err = mtd_read(ubi->mtd, addr, len, &read, buf);
35 if (err && err != -EUCLEAN) {
Heiko Schocher94b66de2015-10-22 06:19:21 +020036 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +020037 err, len, pnum, offset, read);
38 goto out;
39 }
Kyungmin Parkc880c532008-11-19 16:25:44 +010040
Heiko Schocher94b66de2015-10-22 06:19:21 +020041 ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +020042 len, pnum, offset);
Alexey Brodkin2d2fa492018-06-05 17:17:57 +030043 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
Heiko Schocherf5895d12014-06-24 10:10:04 +020044out:
45 vfree(buf);
46 return;
47}
Kyungmin Parkc880c532008-11-19 16:25:44 +010048
49/**
Heiko Schocherf5895d12014-06-24 10:10:04 +020050 * ubi_dump_ec_hdr - dump an erase counter header.
Kyungmin Parkc880c532008-11-19 16:25:44 +010051 * @ec_hdr: the erase counter header to dump
52 */
Heiko Schocherf5895d12014-06-24 10:10:04 +020053void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
Kyungmin Parkc880c532008-11-19 16:25:44 +010054{
Heiko Schocherf5895d12014-06-24 10:10:04 +020055 pr_err("Erase counter header dump:\n");
56 pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic));
57 pr_err("\tversion %d\n", (int)ec_hdr->version);
58 pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
59 pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
60 pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset));
61 pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq));
62 pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
63 pr_err("erase counter header hexdump:\n");
Alexey Brodkin2d2fa492018-06-05 17:17:57 +030064 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Kyungmin Parkc880c532008-11-19 16:25:44 +010065 ec_hdr, UBI_EC_HDR_SIZE, 1);
66}
67
68/**
Heiko Schocherf5895d12014-06-24 10:10:04 +020069 * ubi_dump_vid_hdr - dump a volume identifier header.
Kyungmin Parkc880c532008-11-19 16:25:44 +010070 * @vid_hdr: the volume identifier header to dump
71 */
Heiko Schocherf5895d12014-06-24 10:10:04 +020072void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
Kyungmin Parkc880c532008-11-19 16:25:44 +010073{
Heiko Schocherf5895d12014-06-24 10:10:04 +020074 pr_err("Volume identifier header dump:\n");
75 pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
76 pr_err("\tversion %d\n", (int)vid_hdr->version);
77 pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type);
78 pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
79 pr_err("\tcompat %d\n", (int)vid_hdr->compat);
80 pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
81 pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
82 pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
83 pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
84 pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
85 pr_err("\tsqnum %llu\n",
Kyungmin Parkc880c532008-11-19 16:25:44 +010086 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
Heiko Schocherf5895d12014-06-24 10:10:04 +020087 pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
88 pr_err("Volume identifier header hexdump:\n");
Alexey Brodkin2d2fa492018-06-05 17:17:57 +030089 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Heiko Schocherf5895d12014-06-24 10:10:04 +020090 vid_hdr, UBI_VID_HDR_SIZE, 1);
Kyungmin Parkc880c532008-11-19 16:25:44 +010091}
92
93/**
Heiko Schocherf5895d12014-06-24 10:10:04 +020094 * ubi_dump_vol_info - dump volume information.
Kyungmin Parkc880c532008-11-19 16:25:44 +010095 * @vol: UBI volume description object
96 */
Heiko Schocherf5895d12014-06-24 10:10:04 +020097void ubi_dump_vol_info(const struct ubi_volume *vol)
Kyungmin Parkc880c532008-11-19 16:25:44 +010098{
Heiko Schocherf5895d12014-06-24 10:10:04 +020099 printf("Volume information dump:\n");
100 printf("\tvol_id %d\n", vol->vol_id);
101 printf("\treserved_pebs %d\n", vol->reserved_pebs);
102 printf("\talignment %d\n", vol->alignment);
103 printf("\tdata_pad %d\n", vol->data_pad);
104 printf("\tvol_type %d\n", vol->vol_type);
105 printf("\tname_len %d\n", vol->name_len);
106 printf("\tusable_leb_size %d\n", vol->usable_leb_size);
107 printf("\tused_ebs %d\n", vol->used_ebs);
108 printf("\tused_bytes %lld\n", vol->used_bytes);
109 printf("\tlast_eb_bytes %d\n", vol->last_eb_bytes);
110 printf("\tcorrupted %d\n", vol->corrupted);
111 printf("\tupd_marker %d\n", vol->upd_marker);
Stefan Roesecab6e752019-09-12 16:41:02 +0200112 printf("\tskip_check %d\n", vol->skip_check);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100113
114 if (vol->name_len <= UBI_VOL_NAME_MAX &&
115 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200116 printf("\tname %s\n", vol->name);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100117 } else {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200118 printf("\t1st 5 characters of name: %c%c%c%c%c\n",
119 vol->name[0], vol->name[1], vol->name[2],
120 vol->name[3], vol->name[4]);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100121 }
122}
123
124/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200125 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
Kyungmin Parkc880c532008-11-19 16:25:44 +0100126 * @r: the object to dump
127 * @idx: volume table index
128 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200129void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
Kyungmin Parkc880c532008-11-19 16:25:44 +0100130{
131 int name_len = be16_to_cpu(r->name_len);
132
Heiko Schocherf5895d12014-06-24 10:10:04 +0200133 pr_err("Volume table record %d dump:\n", idx);
134 pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs));
135 pr_err("\talignment %d\n", be32_to_cpu(r->alignment));
136 pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad));
137 pr_err("\tvol_type %d\n", (int)r->vol_type);
138 pr_err("\tupd_marker %d\n", (int)r->upd_marker);
139 pr_err("\tname_len %d\n", name_len);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100140
141 if (r->name[0] == '\0') {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200142 pr_err("\tname NULL\n");
Kyungmin Parkc880c532008-11-19 16:25:44 +0100143 return;
144 }
145
146 if (name_len <= UBI_VOL_NAME_MAX &&
147 strnlen(&r->name[0], name_len + 1) == name_len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200148 pr_err("\tname %s\n", &r->name[0]);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100149 } else {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200150 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
Kyungmin Parkc880c532008-11-19 16:25:44 +0100151 r->name[0], r->name[1], r->name[2], r->name[3],
152 r->name[4]);
153 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200154 pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
Kyungmin Parkc880c532008-11-19 16:25:44 +0100155}
156
157/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200158 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
159 * @av: the object to dump
Kyungmin Parkc880c532008-11-19 16:25:44 +0100160 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200161void ubi_dump_av(const struct ubi_ainf_volume *av)
Kyungmin Parkc880c532008-11-19 16:25:44 +0100162{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200163 pr_err("Volume attaching information dump:\n");
164 pr_err("\tvol_id %d\n", av->vol_id);
165 pr_err("\thighest_lnum %d\n", av->highest_lnum);
166 pr_err("\tleb_count %d\n", av->leb_count);
167 pr_err("\tcompat %d\n", av->compat);
168 pr_err("\tvol_type %d\n", av->vol_type);
169 pr_err("\tused_ebs %d\n", av->used_ebs);
170 pr_err("\tlast_data_size %d\n", av->last_data_size);
171 pr_err("\tdata_pad %d\n", av->data_pad);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100172}
173
174/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200175 * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
176 * @aeb: the object to dump
Kyungmin Parkc880c532008-11-19 16:25:44 +0100177 * @type: object type: 0 - not corrupted, 1 - corrupted
178 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200179void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
Kyungmin Parkc880c532008-11-19 16:25:44 +0100180{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200181 pr_err("eraseblock attaching information dump:\n");
182 pr_err("\tec %d\n", aeb->ec);
183 pr_err("\tpnum %d\n", aeb->pnum);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100184 if (type == 0) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200185 pr_err("\tlnum %d\n", aeb->lnum);
186 pr_err("\tscrub %d\n", aeb->scrub);
187 pr_err("\tsqnum %llu\n", aeb->sqnum);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100188 }
189}
190
191/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200192 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
Kyungmin Parkc880c532008-11-19 16:25:44 +0100193 * @req: the object to dump
194 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200195void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
Kyungmin Parkc880c532008-11-19 16:25:44 +0100196{
197 char nm[17];
198
Heiko Schocherf5895d12014-06-24 10:10:04 +0200199 pr_err("Volume creation request dump:\n");
200 pr_err("\tvol_id %d\n", req->vol_id);
201 pr_err("\talignment %d\n", req->alignment);
202 pr_err("\tbytes %lld\n", (long long)req->bytes);
203 pr_err("\tvol_type %d\n", req->vol_type);
204 pr_err("\tname_len %d\n", req->name_len);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100205
206 memcpy(nm, req->name, 16);
207 nm[16] = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200208 pr_err("\t1st 16 characters of name: %s\n", nm);
209}
210
211#ifndef __UBOOT__
212/*
213 * Root directory for UBI stuff in debugfs. Contains sub-directories which
214 * contain the stuff specific to particular UBI devices.
215 */
216static struct dentry *dfs_rootdir;
217
218/**
219 * ubi_debugfs_init - create UBI debugfs directory.
220 *
221 * Create UBI debugfs directory. Returns zero in case of success and a negative
222 * error code in case of failure.
223 */
224int ubi_debugfs_init(void)
225{
226 if (!IS_ENABLED(CONFIG_DEBUG_FS))
227 return 0;
228
229 dfs_rootdir = debugfs_create_dir("ubi", NULL);
230 if (IS_ERR_OR_NULL(dfs_rootdir)) {
231 int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
232
Heiko Schocher94b66de2015-10-22 06:19:21 +0200233 pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
234 err);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200235 return err;
236 }
237
238 return 0;
239}
240
241/**
242 * ubi_debugfs_exit - remove UBI debugfs directory.
243 */
244void ubi_debugfs_exit(void)
245{
246 if (IS_ENABLED(CONFIG_DEBUG_FS))
247 debugfs_remove(dfs_rootdir);
248}
249
250/* Read an UBI debugfs file */
251static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
252 size_t count, loff_t *ppos)
253{
254 unsigned long ubi_num = (unsigned long)file->private_data;
255 struct dentry *dent = file->f_path.dentry;
256 struct ubi_device *ubi;
257 struct ubi_debug_info *d;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200258 char buf[8];
Heiko Schocherf5895d12014-06-24 10:10:04 +0200259 int val;
260
261 ubi = ubi_get_device(ubi_num);
262 if (!ubi)
263 return -ENODEV;
264 d = &ubi->dbg;
265
266 if (dent == d->dfs_chk_gen)
267 val = d->chk_gen;
268 else if (dent == d->dfs_chk_io)
269 val = d->chk_io;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200270 else if (dent == d->dfs_chk_fastmap)
271 val = d->chk_fastmap;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200272 else if (dent == d->dfs_disable_bgt)
273 val = d->disable_bgt;
274 else if (dent == d->dfs_emulate_bitflips)
275 val = d->emulate_bitflips;
276 else if (dent == d->dfs_emulate_io_failures)
277 val = d->emulate_io_failures;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200278 else if (dent == d->dfs_emulate_power_cut) {
279 snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
280 count = simple_read_from_buffer(user_buf, count, ppos,
281 buf, strlen(buf));
282 goto out;
283 } else if (dent == d->dfs_power_cut_min) {
284 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
285 count = simple_read_from_buffer(user_buf, count, ppos,
286 buf, strlen(buf));
287 goto out;
288 } else if (dent == d->dfs_power_cut_max) {
289 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
290 count = simple_read_from_buffer(user_buf, count, ppos,
291 buf, strlen(buf));
292 goto out;
293 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200294 else {
295 count = -EINVAL;
296 goto out;
297 }
298
299 if (val)
300 buf[0] = '1';
301 else
302 buf[0] = '0';
303 buf[1] = '\n';
304 buf[2] = 0x00;
305
306 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
307
308out:
309 ubi_put_device(ubi);
310 return count;
311}
312
313/* Write an UBI debugfs file */
314static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
315 size_t count, loff_t *ppos)
316{
317 unsigned long ubi_num = (unsigned long)file->private_data;
318 struct dentry *dent = file->f_path.dentry;
319 struct ubi_device *ubi;
320 struct ubi_debug_info *d;
321 size_t buf_size;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200322 char buf[8] = {0};
Heiko Schocherf5895d12014-06-24 10:10:04 +0200323 int val;
324
325 ubi = ubi_get_device(ubi_num);
326 if (!ubi)
327 return -ENODEV;
328 d = &ubi->dbg;
329
330 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
331 if (copy_from_user(buf, user_buf, buf_size)) {
332 count = -EFAULT;
333 goto out;
334 }
335
Heiko Schocher94b66de2015-10-22 06:19:21 +0200336 if (dent == d->dfs_power_cut_min) {
337 if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
338 count = -EINVAL;
339 goto out;
340 } else if (dent == d->dfs_power_cut_max) {
341 if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
342 count = -EINVAL;
343 goto out;
344 } else if (dent == d->dfs_emulate_power_cut) {
345 if (kstrtoint(buf, 0, &val) != 0)
346 count = -EINVAL;
347 d->emulate_power_cut = val;
348 goto out;
349 }
350
Heiko Schocherf5895d12014-06-24 10:10:04 +0200351 if (buf[0] == '1')
352 val = 1;
353 else if (buf[0] == '0')
354 val = 0;
355 else {
356 count = -EINVAL;
357 goto out;
358 }
359
360 if (dent == d->dfs_chk_gen)
361 d->chk_gen = val;
362 else if (dent == d->dfs_chk_io)
363 d->chk_io = val;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200364 else if (dent == d->dfs_chk_fastmap)
365 d->chk_fastmap = val;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200366 else if (dent == d->dfs_disable_bgt)
367 d->disable_bgt = val;
368 else if (dent == d->dfs_emulate_bitflips)
369 d->emulate_bitflips = val;
370 else if (dent == d->dfs_emulate_io_failures)
371 d->emulate_io_failures = val;
372 else
373 count = -EINVAL;
374
375out:
376 ubi_put_device(ubi);
377 return count;
378}
379
380/* File operations for all UBI debugfs files */
381static const struct file_operations dfs_fops = {
382 .read = dfs_file_read,
383 .write = dfs_file_write,
384 .open = simple_open,
385 .llseek = no_llseek,
386 .owner = THIS_MODULE,
387};
388
389/**
390 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
391 * @ubi: UBI device description object
392 *
393 * This function creates all debugfs files for UBI device @ubi. Returns zero in
394 * case of success and a negative error code in case of failure.
395 */
396int ubi_debugfs_init_dev(struct ubi_device *ubi)
397{
398 int err, n;
399 unsigned long ubi_num = ubi->ubi_num;
400 const char *fname;
401 struct dentry *dent;
402 struct ubi_debug_info *d = &ubi->dbg;
403
404 if (!IS_ENABLED(CONFIG_DEBUG_FS))
405 return 0;
406
407 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
408 ubi->ubi_num);
409 if (n == UBI_DFS_DIR_LEN) {
410 /* The array size is too small */
411 fname = UBI_DFS_DIR_NAME;
412 dent = ERR_PTR(-EINVAL);
413 goto out;
414 }
415
416 fname = d->dfs_dir_name;
417 dent = debugfs_create_dir(fname, dfs_rootdir);
418 if (IS_ERR_OR_NULL(dent))
419 goto out;
420 d->dfs_dir = dent;
421
422 fname = "chk_gen";
423 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
424 &dfs_fops);
425 if (IS_ERR_OR_NULL(dent))
426 goto out_remove;
427 d->dfs_chk_gen = dent;
428
429 fname = "chk_io";
430 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
431 &dfs_fops);
432 if (IS_ERR_OR_NULL(dent))
433 goto out_remove;
434 d->dfs_chk_io = dent;
435
Heiko Schocher94b66de2015-10-22 06:19:21 +0200436 fname = "chk_fastmap";
437 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
438 &dfs_fops);
439 if (IS_ERR_OR_NULL(dent))
440 goto out_remove;
441 d->dfs_chk_fastmap = dent;
442
Heiko Schocherf5895d12014-06-24 10:10:04 +0200443 fname = "tst_disable_bgt";
444 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
445 &dfs_fops);
446 if (IS_ERR_OR_NULL(dent))
447 goto out_remove;
448 d->dfs_disable_bgt = dent;
449
450 fname = "tst_emulate_bitflips";
451 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
452 &dfs_fops);
453 if (IS_ERR_OR_NULL(dent))
454 goto out_remove;
455 d->dfs_emulate_bitflips = dent;
456
457 fname = "tst_emulate_io_failures";
458 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
459 &dfs_fops);
460 if (IS_ERR_OR_NULL(dent))
461 goto out_remove;
462 d->dfs_emulate_io_failures = dent;
463
Heiko Schocher94b66de2015-10-22 06:19:21 +0200464 fname = "tst_emulate_power_cut";
465 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
466 &dfs_fops);
467 if (IS_ERR_OR_NULL(dent))
468 goto out_remove;
469 d->dfs_emulate_power_cut = dent;
470
471 fname = "tst_emulate_power_cut_min";
472 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
473 &dfs_fops);
474 if (IS_ERR_OR_NULL(dent))
475 goto out_remove;
476 d->dfs_power_cut_min = dent;
477
478 fname = "tst_emulate_power_cut_max";
479 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
480 &dfs_fops);
481 if (IS_ERR_OR_NULL(dent))
482 goto out_remove;
483 d->dfs_power_cut_max = dent;
484
Heiko Schocherf5895d12014-06-24 10:10:04 +0200485 return 0;
486
487out_remove:
488 debugfs_remove_recursive(d->dfs_dir);
489out:
490 err = dent ? PTR_ERR(dent) : -ENODEV;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200491 ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200492 fname, err);
493 return err;
494}
495
496/**
497 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
498 * @ubi: UBI device description object
499 */
500void ubi_debugfs_exit_dev(struct ubi_device *ubi)
501{
502 if (IS_ENABLED(CONFIG_DEBUG_FS))
503 debugfs_remove_recursive(ubi->dbg.dfs_dir);
504}
Heiko Schocher94b66de2015-10-22 06:19:21 +0200505
506/**
507 * ubi_dbg_power_cut - emulate a power cut if it is time to do so
508 * @ubi: UBI device description object
509 * @caller: Flags set to indicate from where the function is being called
510 *
511 * Returns non-zero if a power cut was emulated, zero if not.
512 */
513int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
514{
515 unsigned int range;
516
517 if ((ubi->dbg.emulate_power_cut & caller) == 0)
518 return 0;
519
520 if (ubi->dbg.power_cut_counter == 0) {
521 ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
522
523 if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
524 range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
525 ubi->dbg.power_cut_counter += prandom_u32() % range;
526 }
527 return 0;
528 }
529
530 ubi->dbg.power_cut_counter--;
531 if (ubi->dbg.power_cut_counter)
532 return 0;
533
534 ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
535 ubi_ro_mode(ubi);
536 return 1;
537}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200538#else
539int ubi_debugfs_init(void)
540{
541 return 0;
542}
543
544void ubi_debugfs_exit(void)
545{
Kyungmin Parkc880c532008-11-19 16:25:44 +0100546}
547
Heiko Schocherf5895d12014-06-24 10:10:04 +0200548int ubi_debugfs_init_dev(struct ubi_device *ubi)
549{
550 return 0;
551}
552
553void ubi_debugfs_exit_dev(struct ubi_device *ubi)
554{
Heiko Schocher94b66de2015-10-22 06:19:21 +0200555}
556
557int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
558{
559 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200560}
561#endif