Skip to content

Commit 2a734bb

Browse files
Artem BityutskiyArtem Bityutskiy
authored andcommitted
UBI: use debugfs for the extra checks knobs
This patch introduces debugfs support to UBI. All the UBI stuff is kept in the "ubi" debugfs directory, which contains per-UBI device "ubi/ubiX" sub-directories, containing debugging files. This file also creates "ubi/ubiX/chk_gen" and "ubi/ubiX/chk_io" knobs for switching general and I/O extra checks on and off. And it removes the 'debug_chks' UBI module parameters. Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
1 parent d99383b commit 2a734bb

9 files changed

Lines changed: 310 additions & 36 deletions

File tree

‎drivers/mtd/ubi/build.c‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,10 +953,14 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
953953
if (!ubi->peb_buf2)
954954
goto out_free;
955955

956+
err = ubi_debugging_init_dev(ubi);
957+
if (err)
958+
goto out_free;
959+
956960
err = attach_by_scanning(ubi);
957961
if (err) {
958962
dbg_err("failed to attach by scanning, error %d", err);
959-
goto out_free;
963+
goto out_debugging;
960964
}
961965

962966
if (ubi->autoresize_vol_id != -1) {
@@ -969,12 +973,16 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
969973
if (err)
970974
goto out_detach;
971975

976+
err = ubi_debugfs_init_dev(ubi);
977+
if (err)
978+
goto out_uif;
979+
972980
ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name);
973981
if (IS_ERR(ubi->bgt_thread)) {
974982
err = PTR_ERR(ubi->bgt_thread);
975983
ubi_err("cannot spawn \"%s\", error %d", ubi->bgt_name,
976984
err);
977-
goto out_uif;
985+
goto out_debugfs;
978986
}
979987

980988
ubi_msg("attached mtd%d to ubi%d", mtd->index, ubi_num);
@@ -1008,12 +1016,16 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
10081016
ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
10091017
return ubi_num;
10101018

1019+
out_debugfs:
1020+
ubi_debugfs_exit_dev(ubi);
10111021
out_uif:
10121022
uif_close(ubi);
10131023
out_detach:
10141024
ubi_wl_close(ubi);
10151025
free_internal_volumes(ubi);
10161026
vfree(ubi->vtbl);
1027+
out_debugging:
1028+
ubi_debugging_exit_dev(ubi);
10171029
out_free:
10181030
vfree(ubi->peb_buf1);
10191031
vfree(ubi->peb_buf2);
@@ -1080,11 +1092,13 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
10801092
*/
10811093
get_device(&ubi->dev);
10821094

1095+
ubi_debugfs_exit_dev(ubi);
10831096
uif_close(ubi);
10841097
ubi_wl_close(ubi);
10851098
free_internal_volumes(ubi);
10861099
vfree(ubi->vtbl);
10871100
put_mtd_device(ubi->mtd);
1101+
ubi_debugging_exit_dev(ubi);
10881102
vfree(ubi->peb_buf1);
10891103
vfree(ubi->peb_buf2);
10901104
ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num);
@@ -1199,6 +1213,11 @@ static int __init ubi_init(void)
11991213
if (!ubi_wl_entry_slab)
12001214
goto out_dev_unreg;
12011215

1216+
err = ubi_debugfs_init();
1217+
if (err)
1218+
goto out_slab;
1219+
1220+
12021221
/* Attach MTD devices */
12031222
for (i = 0; i < mtd_devs; i++) {
12041223
struct mtd_dev_param *p = &mtd_dev_param[i];
@@ -1247,6 +1266,8 @@ static int __init ubi_init(void)
12471266
ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
12481267
mutex_unlock(&ubi_devices_mutex);
12491268
}
1269+
ubi_debugfs_exit();
1270+
out_slab:
12501271
kmem_cache_destroy(ubi_wl_entry_slab);
12511272
out_dev_unreg:
12521273
misc_deregister(&ubi_ctrl_cdev);
@@ -1270,6 +1291,7 @@ static void __exit ubi_exit(void)
12701291
ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
12711292
mutex_unlock(&ubi_devices_mutex);
12721293
}
1294+
ubi_debugfs_exit();
12731295
kmem_cache_destroy(ubi_wl_entry_slab);
12741296
misc_deregister(&ubi_ctrl_cdev);
12751297
class_remove_file(ubi_class, &ubi_version);

‎drivers/mtd/ubi/debug.c‎

Lines changed: 227 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@
2727
#ifdef CONFIG_MTD_UBI_DEBUG
2828

2929
#include "ubi.h"
30+
#include <linux/debugfs.h>
31+
#include <linux/uaccess.h>
3032
#include <linux/module.h>
3133
#include <linux/moduleparam.h>
3234

33-
unsigned int ubi_chk_flags;
3435
unsigned int ubi_tst_flags;
3536

36-
module_param_named(debug_chks, ubi_chk_flags, uint, S_IRUGO | S_IWUSR);
37-
module_param_named(debug_tsts, ubi_chk_flags, uint, S_IRUGO | S_IWUSR);
37+
module_param_named(debug_tsts, ubi_tst_flags, uint, S_IRUGO | S_IWUSR);
3838

39-
MODULE_PARM_DESC(debug_chks, "Debug check flags");
4039
MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
4140

4241
/**
@@ -239,4 +238,228 @@ void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
239238
return;
240239
}
241240

241+
/**
242+
* ubi_debugging_init_dev - initialize debugging for an UBI device.
243+
* @ubi: UBI device description object
244+
*
245+
* This function initializes debugging-related data for UBI device @ubi.
246+
* Returns zero in case of success and a negative error code in case of
247+
* failure.
248+
*/
249+
int ubi_debugging_init_dev(struct ubi_device *ubi)
250+
{
251+
ubi->dbg = kzalloc(sizeof(struct ubi_debug_info), GFP_KERNEL);
252+
if (!ubi->dbg)
253+
return -ENOMEM;
254+
255+
return 0;
256+
}
257+
258+
/**
259+
* ubi_debugging_exit_dev - free debugging data for an UBI device.
260+
* @ubi: UBI device description object
261+
*/
262+
void ubi_debugging_exit_dev(struct ubi_device *ubi)
263+
{
264+
kfree(ubi->dbg);
265+
}
266+
267+
/*
268+
* Root directory for UBI stuff in debugfs. Contains sub-directories which
269+
* contain the stuff specific to particular UBI devices.
270+
*/
271+
static struct dentry *dfs_rootdir;
272+
273+
/**
274+
* ubi_debugfs_init - create UBI debugfs directory.
275+
*
276+
* Create UBI debugfs directory. Returns zero in case of success and a negative
277+
* error code in case of failure.
278+
*/
279+
int ubi_debugfs_init(void)
280+
{
281+
dfs_rootdir = debugfs_create_dir("ubi", NULL);
282+
if (IS_ERR_OR_NULL(dfs_rootdir)) {
283+
int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
284+
285+
ubi_err("cannot create \"ubi\" debugfs directory, error %d\n",
286+
err);
287+
return err;
288+
}
289+
290+
return 0;
291+
}
292+
293+
/**
294+
* ubi_debugfs_exit - remove UBI debugfs directory.
295+
*/
296+
void ubi_debugfs_exit(void)
297+
{
298+
debugfs_remove(dfs_rootdir);
299+
}
300+
301+
/* Read an UBI debugfs file */
302+
static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
303+
size_t count, loff_t *ppos)
304+
{
305+
unsigned long ubi_num = (unsigned long)file->private_data;
306+
struct dentry *dent = file->f_path.dentry;
307+
struct ubi_device *ubi;
308+
struct ubi_debug_info *d;
309+
char buf[3];
310+
int val;
311+
312+
ubi = ubi_get_device(ubi_num);
313+
if (!ubi)
314+
return -ENODEV;
315+
d = ubi->dbg;
316+
317+
if (dent == d->dfs_chk_gen)
318+
val = d->chk_gen;
319+
else if (dent == d->dfs_chk_io)
320+
val = d->chk_io;
321+
else {
322+
count = -EINVAL;
323+
goto out;
324+
}
325+
326+
if (val)
327+
buf[0] = '1';
328+
else
329+
buf[0] = '0';
330+
buf[1] = '\n';
331+
buf[2] = 0x00;
332+
333+
count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
334+
335+
out:
336+
ubi_put_device(ubi);
337+
return count;
338+
}
339+
340+
/* Write an UBI debugfs file */
341+
static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
342+
size_t count, loff_t *ppos)
343+
{
344+
unsigned long ubi_num = (unsigned long)file->private_data;
345+
struct dentry *dent = file->f_path.dentry;
346+
struct ubi_device *ubi;
347+
struct ubi_debug_info *d;
348+
size_t buf_size;
349+
char buf[8];
350+
int val;
351+
352+
ubi = ubi_get_device(ubi_num);
353+
if (!ubi)
354+
return -ENODEV;
355+
d = ubi->dbg;
356+
357+
buf_size = min_t(size_t, count, (sizeof(buf) - 1));
358+
if (copy_from_user(buf, user_buf, buf_size)) {
359+
count = -EFAULT;
360+
goto out;
361+
}
362+
363+
if (buf[0] == '1')
364+
val = 1;
365+
else if (buf[0] == '0')
366+
val = 0;
367+
else {
368+
count = -EINVAL;
369+
goto out;
370+
}
371+
372+
if (dent == d->dfs_chk_gen)
373+
d->chk_gen = val;
374+
else if (dent == d->dfs_chk_io)
375+
d->chk_io = val;
376+
else
377+
count = -EINVAL;
378+
379+
out:
380+
ubi_put_device(ubi);
381+
return count;
382+
}
383+
384+
static int default_open(struct inode *inode, struct file *file)
385+
{
386+
if (inode->i_private)
387+
file->private_data = inode->i_private;
388+
389+
return 0;
390+
}
391+
392+
/* File operations for all UBI debugfs files */
393+
static const struct file_operations dfs_fops = {
394+
.read = dfs_file_read,
395+
.write = dfs_file_write,
396+
.open = default_open,
397+
.llseek = no_llseek,
398+
.owner = THIS_MODULE,
399+
};
400+
401+
/**
402+
* ubi_debugfs_init_dev - initialize debugfs for an UBI device.
403+
* @ubi: UBI device description object
404+
*
405+
* This function creates all debugfs files for UBI device @ubi. Returns zero in
406+
* case of success and a negative error code in case of failure.
407+
*/
408+
int ubi_debugfs_init_dev(struct ubi_device *ubi)
409+
{
410+
int err, n;
411+
unsigned long ubi_num = ubi->ubi_num;
412+
const char *fname;
413+
struct dentry *dent;
414+
struct ubi_debug_info *d = ubi->dbg;
415+
416+
n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
417+
ubi->ubi_num);
418+
if (n == UBI_DFS_DIR_LEN) {
419+
/* The array size is too small */
420+
fname = UBI_DFS_DIR_NAME;
421+
dent = ERR_PTR(-EINVAL);
422+
goto out;
423+
}
424+
425+
fname = d->dfs_dir_name;
426+
dent = debugfs_create_dir(fname, dfs_rootdir);
427+
if (IS_ERR_OR_NULL(dent))
428+
goto out;
429+
d->dfs_dir = dent;
430+
431+
fname = "chk_gen";
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_gen = dent;
437+
438+
fname = "chk_io";
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_io = dent;
444+
445+
return 0;
446+
447+
out_remove:
448+
debugfs_remove_recursive(d->dfs_dir);
449+
out:
450+
err = dent ? PTR_ERR(dent) : -ENODEV;
451+
ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n",
452+
fname, err);
453+
return err;
454+
}
455+
456+
/**
457+
* dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
458+
* @ubi: UBI device description object
459+
*/
460+
void ubi_debugfs_exit_dev(struct ubi_device *ubi)
461+
{
462+
debugfs_remove_recursive(ubi->dbg->dfs_dir);
463+
}
464+
242465
#endif /* CONFIG_MTD_UBI_DEBUG */

0 commit comments

Comments
 (0)