blob: 412ea9f6f5a6b3fbe6505c5ec4b0928ba8452ad4 [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
Kyungmin Parkc880c532008-11-19 16:25:44 +01008#include <ubi_uboot.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +02009#include "ubi.h"
Heiko Schocherf5895d12014-06-24 10:10:04 +020010#ifndef __UBOOT__
11#include <linux/debugfs.h>
12#include <linux/uaccess.h>
13#include <linux/module.h>
14#endif
15
16/**
17 * ubi_dump_flash - dump a region of flash.
18 * @ubi: UBI device description object
19 * @pnum: the physical eraseblock number to dump
20 * @offset: the starting offset within the physical eraseblock to dump
21 * @len: the length of the region to dump
22 */
23void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
24{
25 int err;
26 size_t read;
27 void *buf;
28 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
Kyungmin Parkc880c532008-11-19 16:25:44 +010029
Heiko Schocherf5895d12014-06-24 10:10:04 +020030 buf = vmalloc(len);
31 if (!buf)
32 return;
33 err = mtd_read(ubi->mtd, addr, len, &read, buf);
34 if (err && err != -EUCLEAN) {
Heiko Schocher94b66de2015-10-22 06:19:21 +020035 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +020036 err, len, pnum, offset, read);
37 goto out;
38 }
Kyungmin Parkc880c532008-11-19 16:25:44 +010039
Heiko Schocher94b66de2015-10-22 06:19:21 +020040 ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +020041 len, pnum, offset);
42 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
43out:
44 vfree(buf);
45 return;
46}
Kyungmin Parkc880c532008-11-19 16:25:44 +010047
48/**
Heiko Schocherf5895d12014-06-24 10:10:04 +020049 * ubi_dump_ec_hdr - dump an erase counter header.
Kyungmin Parkc880c532008-11-19 16:25:44 +010050 * @ec_hdr: the erase counter header to dump
51 */
Heiko Schocherf5895d12014-06-24 10:10:04 +020052void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
Kyungmin Parkc880c532008-11-19 16:25:44 +010053{
Heiko Schocherf5895d12014-06-24 10:10:04 +020054 pr_err("Erase counter header dump:\n");
55 pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic));
56 pr_err("\tversion %d\n", (int)ec_hdr->version);
57 pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
58 pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
59 pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset));
60 pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq));
61 pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
62 pr_err("erase counter header hexdump:\n");
Kyungmin Parkc880c532008-11-19 16:25:44 +010063 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
64 ec_hdr, UBI_EC_HDR_SIZE, 1);
65}
66
67/**
Heiko Schocherf5895d12014-06-24 10:10:04 +020068 * ubi_dump_vid_hdr - dump a volume identifier header.
Kyungmin Parkc880c532008-11-19 16:25:44 +010069 * @vid_hdr: the volume identifier header to dump
70 */
Heiko Schocherf5895d12014-06-24 10:10:04 +020071void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
Kyungmin Parkc880c532008-11-19 16:25:44 +010072{
Heiko Schocherf5895d12014-06-24 10:10:04 +020073 pr_err("Volume identifier header dump:\n");
74 pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
75 pr_err("\tversion %d\n", (int)vid_hdr->version);
76 pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type);
77 pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
78 pr_err("\tcompat %d\n", (int)vid_hdr->compat);
79 pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
80 pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
81 pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
82 pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
83 pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
84 pr_err("\tsqnum %llu\n",
Kyungmin Parkc880c532008-11-19 16:25:44 +010085 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
Heiko Schocherf5895d12014-06-24 10:10:04 +020086 pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
87 pr_err("Volume identifier header hexdump:\n");
88 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
89 vid_hdr, UBI_VID_HDR_SIZE, 1);
Kyungmin Parkc880c532008-11-19 16:25:44 +010090}
91
92/**
Heiko Schocherf5895d12014-06-24 10:10:04 +020093 * ubi_dump_vol_info - dump volume information.
Kyungmin Parkc880c532008-11-19 16:25:44 +010094 * @vol: UBI volume description object
95 */
Heiko Schocherf5895d12014-06-24 10:10:04 +020096void ubi_dump_vol_info(const struct ubi_volume *vol)
Kyungmin Parkc880c532008-11-19 16:25:44 +010097{
Heiko Schocherf5895d12014-06-24 10:10:04 +020098 printf("Volume information dump:\n");
99 printf("\tvol_id %d\n", vol->vol_id);
100 printf("\treserved_pebs %d\n", vol->reserved_pebs);
101 printf("\talignment %d\n", vol->alignment);
102 printf("\tdata_pad %d\n", vol->data_pad);
103 printf("\tvol_type %d\n", vol->vol_type);
104 printf("\tname_len %d\n", vol->name_len);
105 printf("\tusable_leb_size %d\n", vol->usable_leb_size);
106 printf("\tused_ebs %d\n", vol->used_ebs);
107 printf("\tused_bytes %lld\n", vol->used_bytes);
108 printf("\tlast_eb_bytes %d\n", vol->last_eb_bytes);
109 printf("\tcorrupted %d\n", vol->corrupted);
110 printf("\tupd_marker %d\n", vol->upd_marker);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100111
112 if (vol->name_len <= UBI_VOL_NAME_MAX &&
113 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200114 printf("\tname %s\n", vol->name);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100115 } else {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200116 printf("\t1st 5 characters of name: %c%c%c%c%c\n",
117 vol->name[0], vol->name[1], vol->name[2],
118 vol->name[3], vol->name[4]);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100119 }
120}
121
122/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200123 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
Kyungmin Parkc880c532008-11-19 16:25:44 +0100124 * @r: the object to dump
125 * @idx: volume table index
126 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200127void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
Kyungmin Parkc880c532008-11-19 16:25:44 +0100128{
129 int name_len = be16_to_cpu(r->name_len);
130
Heiko Schocherf5895d12014-06-24 10:10:04 +0200131 pr_err("Volume table record %d dump:\n", idx);
132 pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs));
133 pr_err("\talignment %d\n", be32_to_cpu(r->alignment));
134 pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad));
135 pr_err("\tvol_type %d\n", (int)r->vol_type);
136 pr_err("\tupd_marker %d\n", (int)r->upd_marker);
137 pr_err("\tname_len %d\n", name_len);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100138
139 if (r->name[0] == '\0') {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200140 pr_err("\tname NULL\n");
Kyungmin Parkc880c532008-11-19 16:25:44 +0100141 return;
142 }
143
144 if (name_len <= UBI_VOL_NAME_MAX &&
145 strnlen(&r->name[0], name_len + 1) == name_len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200146 pr_err("\tname %s\n", &r->name[0]);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100147 } else {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200148 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
Kyungmin Parkc880c532008-11-19 16:25:44 +0100149 r->name[0], r->name[1], r->name[2], r->name[3],
150 r->name[4]);
151 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200152 pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
Kyungmin Parkc880c532008-11-19 16:25:44 +0100153}
154
155/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200156 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
157 * @av: the object to dump
Kyungmin Parkc880c532008-11-19 16:25:44 +0100158 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200159void ubi_dump_av(const struct ubi_ainf_volume *av)
Kyungmin Parkc880c532008-11-19 16:25:44 +0100160{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200161 pr_err("Volume attaching information dump:\n");
162 pr_err("\tvol_id %d\n", av->vol_id);
163 pr_err("\thighest_lnum %d\n", av->highest_lnum);
164 pr_err("\tleb_count %d\n", av->leb_count);
165 pr_err("\tcompat %d\n", av->compat);
166 pr_err("\tvol_type %d\n", av->vol_type);
167 pr_err("\tused_ebs %d\n", av->used_ebs);
168 pr_err("\tlast_data_size %d\n", av->last_data_size);
169 pr_err("\tdata_pad %d\n", av->data_pad);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100170}
171
172/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200173 * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
174 * @aeb: the object to dump
Kyungmin Parkc880c532008-11-19 16:25:44 +0100175 * @type: object type: 0 - not corrupted, 1 - corrupted
176 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200177void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
Kyungmin Parkc880c532008-11-19 16:25:44 +0100178{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200179 pr_err("eraseblock attaching information dump:\n");
180 pr_err("\tec %d\n", aeb->ec);
181 pr_err("\tpnum %d\n", aeb->pnum);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100182 if (type == 0) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200183 pr_err("\tlnum %d\n", aeb->lnum);
184 pr_err("\tscrub %d\n", aeb->scrub);
185 pr_err("\tsqnum %llu\n", aeb->sqnum);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100186 }
187}
188
189/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200190 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
Kyungmin Parkc880c532008-11-19 16:25:44 +0100191 * @req: the object to dump
192 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200193void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
Kyungmin Parkc880c532008-11-19 16:25:44 +0100194{
195 char nm[17];
196
Heiko Schocherf5895d12014-06-24 10:10:04 +0200197 pr_err("Volume creation request dump:\n");
198 pr_err("\tvol_id %d\n", req->vol_id);
199 pr_err("\talignment %d\n", req->alignment);
200 pr_err("\tbytes %lld\n", (long long)req->bytes);
201 pr_err("\tvol_type %d\n", req->vol_type);
202 pr_err("\tname_len %d\n", req->name_len);
Kyungmin Parkc880c532008-11-19 16:25:44 +0100203
204 memcpy(nm, req->name, 16);
205 nm[16] = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200206 pr_err("\t1st 16 characters of name: %s\n", nm);
207}
208
209#ifndef __UBOOT__
210/*
211 * Root directory for UBI stuff in debugfs. Contains sub-directories which
212 * contain the stuff specific to particular UBI devices.
213 */
214static struct dentry *dfs_rootdir;
215
216/**
217 * ubi_debugfs_init - create UBI debugfs directory.
218 *
219 * Create UBI debugfs directory. Returns zero in case of success and a negative
220 * error code in case of failure.
221 */
222int ubi_debugfs_init(void)
223{
224 if (!IS_ENABLED(CONFIG_DEBUG_FS))
225 return 0;
226
227 dfs_rootdir = debugfs_create_dir("ubi", NULL);
228 if (IS_ERR_OR_NULL(dfs_rootdir)) {
229 int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
230
Heiko Schocher94b66de2015-10-22 06:19:21 +0200231 pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
232 err);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200233 return err;
234 }
235
236 return 0;
237}
238
239/**
240 * ubi_debugfs_exit - remove UBI debugfs directory.
241 */
242void ubi_debugfs_exit(void)
243{
244 if (IS_ENABLED(CONFIG_DEBUG_FS))
245 debugfs_remove(dfs_rootdir);
246}
247
248/* Read an UBI debugfs file */
249static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
250 size_t count, loff_t *ppos)
251{
252 unsigned long ubi_num = (unsigned long)file->private_data;
253 struct dentry *dent = file->f_path.dentry;
254 struct ubi_device *ubi;
255 struct ubi_debug_info *d;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200256 char buf[8];
Heiko Schocherf5895d12014-06-24 10:10:04 +0200257 int val;
258
259 ubi = ubi_get_device(ubi_num);
260 if (!ubi)
261 return -ENODEV;
262 d = &ubi->dbg;
263
264 if (dent == d->dfs_chk_gen)
265 val = d->chk_gen;
266 else if (dent == d->dfs_chk_io)
267 val = d->chk_io;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200268 else if (dent == d->dfs_chk_fastmap)
269 val = d->chk_fastmap;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200270 else if (dent == d->dfs_disable_bgt)
271 val = d->disable_bgt;
272 else if (dent == d->dfs_emulate_bitflips)
273 val = d->emulate_bitflips;
274 else if (dent == d->dfs_emulate_io_failures)
275 val = d->emulate_io_failures;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200276 else if (dent == d->dfs_emulate_power_cut) {
277 snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
278 count = simple_read_from_buffer(user_buf, count, ppos,
279 buf, strlen(buf));
280 goto out;
281 } else if (dent == d->dfs_power_cut_min) {
282 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
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_max) {
287 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
288 count = simple_read_from_buffer(user_buf, count, ppos,
289 buf, strlen(buf));
290 goto out;
291 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200292 else {
293 count = -EINVAL;
294 goto out;
295 }
296
297 if (val)
298 buf[0] = '1';
299 else
300 buf[0] = '0';
301 buf[1] = '\n';
302 buf[2] = 0x00;
303
304 count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
305
306out:
307 ubi_put_device(ubi);
308 return count;
309}
310
311/* Write an UBI debugfs file */
312static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
313 size_t count, loff_t *ppos)
314{
315 unsigned long ubi_num = (unsigned long)file->private_data;
316 struct dentry *dent = file->f_path.dentry;
317 struct ubi_device *ubi;
318 struct ubi_debug_info *d;
319 size_t buf_size;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200320 char buf[8] = {0};
Heiko Schocherf5895d12014-06-24 10:10:04 +0200321 int val;
322
323 ubi = ubi_get_device(ubi_num);
324 if (!ubi)
325 return -ENODEV;
326 d = &ubi->dbg;
327
328 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
329 if (copy_from_user(buf, user_buf, buf_size)) {
330 count = -EFAULT;
331 goto out;
332 }
333
Heiko Schocher94b66de2015-10-22 06:19:21 +0200334 if (dent == d->dfs_power_cut_min) {
335 if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
336 count = -EINVAL;
337 goto out;
338 } else if (dent == d->dfs_power_cut_max) {
339 if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
340 count = -EINVAL;
341 goto out;
342 } else if (dent == d->dfs_emulate_power_cut) {
343 if (kstrtoint(buf, 0, &val) != 0)
344 count = -EINVAL;
345 d->emulate_power_cut = val;
346 goto out;
347 }
348
Heiko Schocherf5895d12014-06-24 10:10:04 +0200349 if (buf[0] == '1')
350 val = 1;
351 else if (buf[0] == '0')
352 val = 0;
353 else {
354 count = -EINVAL;
355 goto out;
356 }
357
358 if (dent == d->dfs_chk_gen)
359 d->chk_gen = val;
360 else if (dent == d->dfs_chk_io)
361 d->chk_io = val;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200362 else if (dent == d->dfs_chk_fastmap)
363 d->chk_fastmap = val;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200364 else if (dent == d->dfs_disable_bgt)
365 d->disable_bgt = val;
366 else if (dent == d->dfs_emulate_bitflips)
367 d->emulate_bitflips = val;
368 else if (dent == d->dfs_emulate_io_failures)
369 d->emulate_io_failures = val;
370 else
371 count = -EINVAL;
372
373out:
374 ubi_put_device(ubi);
375 return count;
376}
377
378/* File operations for all UBI debugfs files */
379static const struct file_operations dfs_fops = {
380 .read = dfs_file_read,
381 .write = dfs_file_write,
382 .open = simple_open,
383 .llseek = no_llseek,
384 .owner = THIS_MODULE,
385};
386
387/**
388 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
389 * @ubi: UBI device description object
390 *
391 * This function creates all debugfs files for UBI device @ubi. Returns zero in
392 * case of success and a negative error code in case of failure.
393 */
394int ubi_debugfs_init_dev(struct ubi_device *ubi)
395{
396 int err, n;
397 unsigned long ubi_num = ubi->ubi_num;
398 const char *fname;
399 struct dentry *dent;
400 struct ubi_debug_info *d = &ubi->dbg;
401
402 if (!IS_ENABLED(CONFIG_DEBUG_FS))
403 return 0;
404
405 n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
406 ubi->ubi_num);
407 if (n == UBI_DFS_DIR_LEN) {
408 /* The array size is too small */
409 fname = UBI_DFS_DIR_NAME;
410 dent = ERR_PTR(-EINVAL);
411 goto out;
412 }
413
414 fname = d->dfs_dir_name;
415 dent = debugfs_create_dir(fname, dfs_rootdir);
416 if (IS_ERR_OR_NULL(dent))
417 goto out;
418 d->dfs_dir = dent;
419
420 fname = "chk_gen";
421 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
422 &dfs_fops);
423 if (IS_ERR_OR_NULL(dent))
424 goto out_remove;
425 d->dfs_chk_gen = dent;
426
427 fname = "chk_io";
428 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
429 &dfs_fops);
430 if (IS_ERR_OR_NULL(dent))
431 goto out_remove;
432 d->dfs_chk_io = dent;
433
Heiko Schocher94b66de2015-10-22 06:19:21 +0200434 fname = "chk_fastmap";
435 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
436 &dfs_fops);
437 if (IS_ERR_OR_NULL(dent))
438 goto out_remove;
439 d->dfs_chk_fastmap = dent;
440
Heiko Schocherf5895d12014-06-24 10:10:04 +0200441 fname = "tst_disable_bgt";
442 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
443 &dfs_fops);
444 if (IS_ERR_OR_NULL(dent))
445 goto out_remove;
446 d->dfs_disable_bgt = dent;
447
448 fname = "tst_emulate_bitflips";
449 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
450 &dfs_fops);
451 if (IS_ERR_OR_NULL(dent))
452 goto out_remove;
453 d->dfs_emulate_bitflips = dent;
454
455 fname = "tst_emulate_io_failures";
456 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
457 &dfs_fops);
458 if (IS_ERR_OR_NULL(dent))
459 goto out_remove;
460 d->dfs_emulate_io_failures = dent;
461
Heiko Schocher94b66de2015-10-22 06:19:21 +0200462 fname = "tst_emulate_power_cut";
463 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
464 &dfs_fops);
465 if (IS_ERR_OR_NULL(dent))
466 goto out_remove;
467 d->dfs_emulate_power_cut = dent;
468
469 fname = "tst_emulate_power_cut_min";
470 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
471 &dfs_fops);
472 if (IS_ERR_OR_NULL(dent))
473 goto out_remove;
474 d->dfs_power_cut_min = dent;
475
476 fname = "tst_emulate_power_cut_max";
477 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
478 &dfs_fops);
479 if (IS_ERR_OR_NULL(dent))
480 goto out_remove;
481 d->dfs_power_cut_max = dent;
482
Heiko Schocherf5895d12014-06-24 10:10:04 +0200483 return 0;
484
485out_remove:
486 debugfs_remove_recursive(d->dfs_dir);
487out:
488 err = dent ? PTR_ERR(dent) : -ENODEV;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200489 ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200490 fname, err);
491 return err;
492}
493
494/**
495 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
496 * @ubi: UBI device description object
497 */
498void ubi_debugfs_exit_dev(struct ubi_device *ubi)
499{
500 if (IS_ENABLED(CONFIG_DEBUG_FS))
501 debugfs_remove_recursive(ubi->dbg.dfs_dir);
502}
Heiko Schocher94b66de2015-10-22 06:19:21 +0200503
504/**
505 * ubi_dbg_power_cut - emulate a power cut if it is time to do so
506 * @ubi: UBI device description object
507 * @caller: Flags set to indicate from where the function is being called
508 *
509 * Returns non-zero if a power cut was emulated, zero if not.
510 */
511int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
512{
513 unsigned int range;
514
515 if ((ubi->dbg.emulate_power_cut & caller) == 0)
516 return 0;
517
518 if (ubi->dbg.power_cut_counter == 0) {
519 ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
520
521 if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
522 range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
523 ubi->dbg.power_cut_counter += prandom_u32() % range;
524 }
525 return 0;
526 }
527
528 ubi->dbg.power_cut_counter--;
529 if (ubi->dbg.power_cut_counter)
530 return 0;
531
532 ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
533 ubi_ro_mode(ubi);
534 return 1;
535}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200536#else
537int ubi_debugfs_init(void)
538{
539 return 0;
540}
541
542void ubi_debugfs_exit(void)
543{
Kyungmin Parkc880c532008-11-19 16:25:44 +0100544}
545
Heiko Schocherf5895d12014-06-24 10:10:04 +0200546int ubi_debugfs_init_dev(struct ubi_device *ubi)
547{
548 return 0;
549}
550
551void ubi_debugfs_exit_dev(struct ubi_device *ubi)
552{
Heiko Schocher94b66de2015-10-22 06:19:21 +0200553}
554
555int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
556{
557 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200558}
559#endif