]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/mtd/ubi/debug.c
Merge tag 'wireless-drivers-2020-02-08' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / mtd / ubi / debug.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2006
4  *
5  * Author: Artem Bityutskiy (Битюцкий Артём)
6  */
7
8 #include "ubi.h"
9 #include <linux/debugfs.h>
10 #include <linux/uaccess.h>
11 #include <linux/module.h>
12 #include <linux/seq_file.h>
13
14
15 /**
16  * ubi_dump_flash - dump a region of flash.
17  * @ubi: UBI device description object
18  * @pnum: the physical eraseblock number to dump
19  * @offset: the starting offset within the physical eraseblock to dump
20  * @len: the length of the region to dump
21  */
22 void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
23 {
24         int err;
25         size_t read;
26         void *buf;
27         loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
28
29         buf = vmalloc(len);
30         if (!buf)
31                 return;
32         err = mtd_read(ubi->mtd, addr, len, &read, buf);
33         if (err && err != -EUCLEAN) {
34                 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
35                         err, len, pnum, offset, read);
36                 goto out;
37         }
38
39         ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
40                 len, pnum, offset);
41         print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
42 out:
43         vfree(buf);
44         return;
45 }
46
47 /**
48  * ubi_dump_ec_hdr - dump an erase counter header.
49  * @ec_hdr: the erase counter header to dump
50  */
51 void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
52 {
53         pr_err("Erase counter header dump:\n");
54         pr_err("\tmagic          %#08x\n", be32_to_cpu(ec_hdr->magic));
55         pr_err("\tversion        %d\n", (int)ec_hdr->version);
56         pr_err("\tec             %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
57         pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
58         pr_err("\tdata_offset    %d\n", be32_to_cpu(ec_hdr->data_offset));
59         pr_err("\timage_seq      %d\n", be32_to_cpu(ec_hdr->image_seq));
60         pr_err("\thdr_crc        %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
61         pr_err("erase counter header hexdump:\n");
62         print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
63                        ec_hdr, UBI_EC_HDR_SIZE, 1);
64 }
65
66 /**
67  * ubi_dump_vid_hdr - dump a volume identifier header.
68  * @vid_hdr: the volume identifier header to dump
69  */
70 void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
71 {
72         pr_err("Volume identifier header dump:\n");
73         pr_err("\tmagic     %08x\n", be32_to_cpu(vid_hdr->magic));
74         pr_err("\tversion   %d\n",  (int)vid_hdr->version);
75         pr_err("\tvol_type  %d\n",  (int)vid_hdr->vol_type);
76         pr_err("\tcopy_flag %d\n",  (int)vid_hdr->copy_flag);
77         pr_err("\tcompat    %d\n",  (int)vid_hdr->compat);
78         pr_err("\tvol_id    %d\n",  be32_to_cpu(vid_hdr->vol_id));
79         pr_err("\tlnum      %d\n",  be32_to_cpu(vid_hdr->lnum));
80         pr_err("\tdata_size %d\n",  be32_to_cpu(vid_hdr->data_size));
81         pr_err("\tused_ebs  %d\n",  be32_to_cpu(vid_hdr->used_ebs));
82         pr_err("\tdata_pad  %d\n",  be32_to_cpu(vid_hdr->data_pad));
83         pr_err("\tsqnum     %llu\n",
84                 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
85         pr_err("\thdr_crc   %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
86         pr_err("Volume identifier header hexdump:\n");
87         print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
88                        vid_hdr, UBI_VID_HDR_SIZE, 1);
89 }
90
91 /**
92  * ubi_dump_vol_info - dump volume information.
93  * @vol: UBI volume description object
94  */
95 void ubi_dump_vol_info(const struct ubi_volume *vol)
96 {
97         pr_err("Volume information dump:\n");
98         pr_err("\tvol_id          %d\n", vol->vol_id);
99         pr_err("\treserved_pebs   %d\n", vol->reserved_pebs);
100         pr_err("\talignment       %d\n", vol->alignment);
101         pr_err("\tdata_pad        %d\n", vol->data_pad);
102         pr_err("\tvol_type        %d\n", vol->vol_type);
103         pr_err("\tname_len        %d\n", vol->name_len);
104         pr_err("\tusable_leb_size %d\n", vol->usable_leb_size);
105         pr_err("\tused_ebs        %d\n", vol->used_ebs);
106         pr_err("\tused_bytes      %lld\n", vol->used_bytes);
107         pr_err("\tlast_eb_bytes   %d\n", vol->last_eb_bytes);
108         pr_err("\tcorrupted       %d\n", vol->corrupted);
109         pr_err("\tupd_marker      %d\n", vol->upd_marker);
110         pr_err("\tskip_check      %d\n", vol->skip_check);
111
112         if (vol->name_len <= UBI_VOL_NAME_MAX &&
113             strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
114                 pr_err("\tname            %s\n", vol->name);
115         } else {
116                 pr_err("\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]);
119         }
120 }
121
122 /**
123  * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
124  * @r: the object to dump
125  * @idx: volume table index
126  */
127 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
128 {
129         int name_len = be16_to_cpu(r->name_len);
130
131         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);
138
139         if (r->name[0] == '\0') {
140                 pr_err("\tname            NULL\n");
141                 return;
142         }
143
144         if (name_len <= UBI_VOL_NAME_MAX &&
145             strnlen(&r->name[0], name_len + 1) == name_len) {
146                 pr_err("\tname            %s\n", &r->name[0]);
147         } else {
148                 pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
149                         r->name[0], r->name[1], r->name[2], r->name[3],
150                         r->name[4]);
151         }
152         pr_err("\tcrc             %#08x\n", be32_to_cpu(r->crc));
153 }
154
155 /**
156  * ubi_dump_av - dump a &struct ubi_ainf_volume object.
157  * @av: the object to dump
158  */
159 void ubi_dump_av(const struct ubi_ainf_volume *av)
160 {
161         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);
170 }
171
172 /**
173  * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
174  * @aeb: the object to dump
175  * @type: object type: 0 - not corrupted, 1 - corrupted
176  */
177 void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
178 {
179         pr_err("eraseblock attaching information dump:\n");
180         pr_err("\tec       %d\n", aeb->ec);
181         pr_err("\tpnum     %d\n", aeb->pnum);
182         if (type == 0) {
183                 pr_err("\tlnum     %d\n", aeb->lnum);
184                 pr_err("\tscrub    %d\n", aeb->scrub);
185                 pr_err("\tsqnum    %llu\n", aeb->sqnum);
186         }
187 }
188
189 /**
190  * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
191  * @req: the object to dump
192  */
193 void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
194 {
195         char nm[17];
196
197         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);
203
204         memcpy(nm, req->name, 16);
205         nm[16] = 0;
206         pr_err("\t1st 16 characters of name: %s\n", nm);
207 }
208
209 /*
210  * Root directory for UBI stuff in debugfs. Contains sub-directories which
211  * contain the stuff specific to particular UBI devices.
212  */
213 static struct dentry *dfs_rootdir;
214
215 /**
216  * ubi_debugfs_init - create UBI debugfs directory.
217  *
218  * Create UBI debugfs directory. Returns zero in case of success and a negative
219  * error code in case of failure.
220  */
221 int ubi_debugfs_init(void)
222 {
223         if (!IS_ENABLED(CONFIG_DEBUG_FS))
224                 return 0;
225
226         dfs_rootdir = debugfs_create_dir("ubi", NULL);
227         if (IS_ERR_OR_NULL(dfs_rootdir)) {
228                 int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
229
230                 pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
231                        err);
232                 return err;
233         }
234
235         return 0;
236 }
237
238 /**
239  * ubi_debugfs_exit - remove UBI debugfs directory.
240  */
241 void ubi_debugfs_exit(void)
242 {
243         if (IS_ENABLED(CONFIG_DEBUG_FS))
244                 debugfs_remove(dfs_rootdir);
245 }
246
247 /* Read an UBI debugfs file */
248 static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
249                              size_t count, loff_t *ppos)
250 {
251         unsigned long ubi_num = (unsigned long)file->private_data;
252         struct dentry *dent = file->f_path.dentry;
253         struct ubi_device *ubi;
254         struct ubi_debug_info *d;
255         char buf[8];
256         int val;
257
258         ubi = ubi_get_device(ubi_num);
259         if (!ubi)
260                 return -ENODEV;
261         d = &ubi->dbg;
262
263         if (dent == d->dfs_chk_gen)
264                 val = d->chk_gen;
265         else if (dent == d->dfs_chk_io)
266                 val = d->chk_io;
267         else if (dent == d->dfs_chk_fastmap)
268                 val = d->chk_fastmap;
269         else if (dent == d->dfs_disable_bgt)
270                 val = d->disable_bgt;
271         else if (dent == d->dfs_emulate_bitflips)
272                 val = d->emulate_bitflips;
273         else if (dent == d->dfs_emulate_io_failures)
274                 val = d->emulate_io_failures;
275         else if (dent == d->dfs_emulate_power_cut) {
276                 snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
277                 count = simple_read_from_buffer(user_buf, count, ppos,
278                                                 buf, strlen(buf));
279                 goto out;
280         } else if (dent == d->dfs_power_cut_min) {
281                 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
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_max) {
286                 snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
287                 count = simple_read_from_buffer(user_buf, count, ppos,
288                                                 buf, strlen(buf));
289                 goto out;
290         }
291         else {
292                 count = -EINVAL;
293                 goto out;
294         }
295
296         if (val)
297                 buf[0] = '1';
298         else
299                 buf[0] = '0';
300         buf[1] = '\n';
301         buf[2] = 0x00;
302
303         count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
304
305 out:
306         ubi_put_device(ubi);
307         return count;
308 }
309
310 /* Write an UBI debugfs file */
311 static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
312                               size_t count, loff_t *ppos)
313 {
314         unsigned long ubi_num = (unsigned long)file->private_data;
315         struct dentry *dent = file->f_path.dentry;
316         struct ubi_device *ubi;
317         struct ubi_debug_info *d;
318         size_t buf_size;
319         char buf[8] = {0};
320         int val;
321
322         ubi = ubi_get_device(ubi_num);
323         if (!ubi)
324                 return -ENODEV;
325         d = &ubi->dbg;
326
327         buf_size = min_t(size_t, count, (sizeof(buf) - 1));
328         if (copy_from_user(buf, user_buf, buf_size)) {
329                 count = -EFAULT;
330                 goto out;
331         }
332
333         if (dent == d->dfs_power_cut_min) {
334                 if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
335                         count = -EINVAL;
336                 goto out;
337         } else if (dent == d->dfs_power_cut_max) {
338                 if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
339                         count = -EINVAL;
340                 goto out;
341         } else if (dent == d->dfs_emulate_power_cut) {
342                 if (kstrtoint(buf, 0, &val) != 0)
343                         count = -EINVAL;
344                 else
345                         d->emulate_power_cut = val;
346                 goto out;
347         }
348
349         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;
362         else if (dent == d->dfs_chk_fastmap)
363                 d->chk_fastmap = val;
364         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
373 out:
374         ubi_put_device(ubi);
375         return count;
376 }
377
378 /* File operations for all UBI debugfs files except
379  * detailed_erase_block_info
380  */
381 static 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 /* As long as the position is less then that total number of erase blocks,
390  * we still have more to print.
391  */
392 static void *eraseblk_count_seq_start(struct seq_file *s, loff_t *pos)
393 {
394         struct ubi_device *ubi = s->private;
395
396         if (*pos == 0)
397                 return SEQ_START_TOKEN;
398
399         if (*pos < ubi->peb_count)
400                 return pos;
401
402         return NULL;
403 }
404
405 /* Since we are using the position as the iterator, we just need to check if we
406  * are done and increment the position.
407  */
408 static void *eraseblk_count_seq_next(struct seq_file *s, void *v, loff_t *pos)
409 {
410         struct ubi_device *ubi = s->private;
411
412         if (v == SEQ_START_TOKEN)
413                 return pos;
414         (*pos)++;
415
416         if (*pos < ubi->peb_count)
417                 return pos;
418
419         return NULL;
420 }
421
422 static void eraseblk_count_seq_stop(struct seq_file *s, void *v)
423 {
424 }
425
426 static int eraseblk_count_seq_show(struct seq_file *s, void *iter)
427 {
428         struct ubi_device *ubi = s->private;
429         struct ubi_wl_entry *wl;
430         int *block_number = iter;
431         int erase_count = -1;
432         int err;
433
434         /* If this is the start, print a header */
435         if (iter == SEQ_START_TOKEN) {
436                 seq_puts(s,
437                          "physical_block_number\terase_count\tblock_status\tread_status\n");
438                 return 0;
439         }
440
441         err = ubi_io_is_bad(ubi, *block_number);
442         if (err)
443                 return err;
444
445         spin_lock(&ubi->wl_lock);
446
447         wl = ubi->lookuptbl[*block_number];
448         if (wl)
449                 erase_count = wl->ec;
450
451         spin_unlock(&ubi->wl_lock);
452
453         if (erase_count < 0)
454                 return 0;
455
456         seq_printf(s, "%-22d\t%-11d\n", *block_number, erase_count);
457
458         return 0;
459 }
460
461 static const struct seq_operations eraseblk_count_seq_ops = {
462         .start = eraseblk_count_seq_start,
463         .next = eraseblk_count_seq_next,
464         .stop = eraseblk_count_seq_stop,
465         .show = eraseblk_count_seq_show
466 };
467
468 static int eraseblk_count_open(struct inode *inode, struct file *f)
469 {
470         struct seq_file *s;
471         int err;
472
473         err = seq_open(f, &eraseblk_count_seq_ops);
474         if (err)
475                 return err;
476
477         s = f->private_data;
478         s->private = ubi_get_device((unsigned long)inode->i_private);
479
480         if (!s->private)
481                 return -ENODEV;
482         else
483                 return 0;
484 }
485
486 static int eraseblk_count_release(struct inode *inode, struct file *f)
487 {
488         struct seq_file *s = f->private_data;
489         struct ubi_device *ubi = s->private;
490
491         ubi_put_device(ubi);
492
493         return seq_release(inode, f);
494 }
495
496 static const struct file_operations eraseblk_count_fops = {
497         .owner = THIS_MODULE,
498         .open = eraseblk_count_open,
499         .read = seq_read,
500         .llseek = seq_lseek,
501         .release = eraseblk_count_release,
502 };
503
504 /**
505  * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
506  * @ubi: UBI device description object
507  *
508  * This function creates all debugfs files for UBI device @ubi. Returns zero in
509  * case of success and a negative error code in case of failure.
510  */
511 int ubi_debugfs_init_dev(struct ubi_device *ubi)
512 {
513         unsigned long ubi_num = ubi->ubi_num;
514         struct ubi_debug_info *d = &ubi->dbg;
515         int n;
516
517         if (!IS_ENABLED(CONFIG_DEBUG_FS))
518                 return 0;
519
520         n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
521                      ubi->ubi_num);
522         if (n == UBI_DFS_DIR_LEN) {
523                 /* The array size is too small */
524                 return -EINVAL;
525         }
526
527         d->dfs_dir = debugfs_create_dir(d->dfs_dir_name, dfs_rootdir);
528
529         d->dfs_chk_gen = debugfs_create_file("chk_gen", S_IWUSR, d->dfs_dir,
530                                              (void *)ubi_num, &dfs_fops);
531
532         d->dfs_chk_io = debugfs_create_file("chk_io", S_IWUSR, d->dfs_dir,
533                                             (void *)ubi_num, &dfs_fops);
534
535         d->dfs_chk_fastmap = debugfs_create_file("chk_fastmap", S_IWUSR,
536                                                  d->dfs_dir, (void *)ubi_num,
537                                                  &dfs_fops);
538
539         d->dfs_disable_bgt = debugfs_create_file("tst_disable_bgt", S_IWUSR,
540                                                  d->dfs_dir, (void *)ubi_num,
541                                                  &dfs_fops);
542
543         d->dfs_emulate_bitflips = debugfs_create_file("tst_emulate_bitflips",
544                                                       S_IWUSR, d->dfs_dir,
545                                                       (void *)ubi_num,
546                                                       &dfs_fops);
547
548         d->dfs_emulate_io_failures = debugfs_create_file("tst_emulate_io_failures",
549                                                          S_IWUSR, d->dfs_dir,
550                                                          (void *)ubi_num,
551                                                          &dfs_fops);
552
553         d->dfs_emulate_power_cut = debugfs_create_file("tst_emulate_power_cut",
554                                                        S_IWUSR, d->dfs_dir,
555                                                        (void *)ubi_num,
556                                                        &dfs_fops);
557
558         d->dfs_power_cut_min = debugfs_create_file("tst_emulate_power_cut_min",
559                                                    S_IWUSR, d->dfs_dir,
560                                                    (void *)ubi_num, &dfs_fops);
561
562         d->dfs_power_cut_max = debugfs_create_file("tst_emulate_power_cut_max",
563                                                    S_IWUSR, d->dfs_dir,
564                                                    (void *)ubi_num, &dfs_fops);
565
566         debugfs_create_file("detailed_erase_block_info", S_IRUSR, d->dfs_dir,
567                             (void *)ubi_num, &eraseblk_count_fops);
568
569         return 0;
570 }
571
572 /**
573  * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
574  * @ubi: UBI device description object
575  */
576 void ubi_debugfs_exit_dev(struct ubi_device *ubi)
577 {
578         if (IS_ENABLED(CONFIG_DEBUG_FS))
579                 debugfs_remove_recursive(ubi->dbg.dfs_dir);
580 }
581
582 /**
583  * ubi_dbg_power_cut - emulate a power cut if it is time to do so
584  * @ubi: UBI device description object
585  * @caller: Flags set to indicate from where the function is being called
586  *
587  * Returns non-zero if a power cut was emulated, zero if not.
588  */
589 int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
590 {
591         unsigned int range;
592
593         if ((ubi->dbg.emulate_power_cut & caller) == 0)
594                 return 0;
595
596         if (ubi->dbg.power_cut_counter == 0) {
597                 ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
598
599                 if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
600                         range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
601                         ubi->dbg.power_cut_counter += prandom_u32() % range;
602                 }
603                 return 0;
604         }
605
606         ubi->dbg.power_cut_counter--;
607         if (ubi->dbg.power_cut_counter)
608                 return 0;
609
610         ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
611         ubi_ro_mode(ubi);
612         return 1;
613 }