Linux Audio

Check our new training course

Loading...
v6.8
  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#include <linux/fault-inject.h>
 14
 15#ifdef CONFIG_MTD_UBI_FAULT_INJECTION
 16static DECLARE_FAULT_ATTR(fault_eccerr_attr);
 17static DECLARE_FAULT_ATTR(fault_bitflips_attr);
 18static DECLARE_FAULT_ATTR(fault_read_failure_attr);
 19static DECLARE_FAULT_ATTR(fault_write_failure_attr);
 20static DECLARE_FAULT_ATTR(fault_erase_failure_attr);
 21static DECLARE_FAULT_ATTR(fault_power_cut_attr);
 22static DECLARE_FAULT_ATTR(fault_io_ff_attr);
 23static DECLARE_FAULT_ATTR(fault_io_ff_bitflips_attr);
 24static DECLARE_FAULT_ATTR(fault_bad_hdr_attr);
 25static DECLARE_FAULT_ATTR(fault_bad_hdr_ebadmsg_attr);
 26
 27#define FAIL_ACTION(name, fault_attr)			\
 28bool should_fail_##name(void)				\
 29{							\
 30	return should_fail(&fault_attr, 1);		\
 31}
 32
 33FAIL_ACTION(eccerr,		fault_eccerr_attr)
 34FAIL_ACTION(bitflips,		fault_bitflips_attr)
 35FAIL_ACTION(read_failure,	fault_read_failure_attr)
 36FAIL_ACTION(write_failure,	fault_write_failure_attr)
 37FAIL_ACTION(erase_failure,	fault_erase_failure_attr)
 38FAIL_ACTION(power_cut,		fault_power_cut_attr)
 39FAIL_ACTION(io_ff,		fault_io_ff_attr)
 40FAIL_ACTION(io_ff_bitflips,	fault_io_ff_bitflips_attr)
 41FAIL_ACTION(bad_hdr,		fault_bad_hdr_attr)
 42FAIL_ACTION(bad_hdr_ebadmsg,	fault_bad_hdr_ebadmsg_attr)
 43#endif
 44
 45/**
 46 * ubi_dump_flash - dump a region of flash.
 47 * @ubi: UBI device description object
 48 * @pnum: the physical eraseblock number to dump
 49 * @offset: the starting offset within the physical eraseblock to dump
 50 * @len: the length of the region to dump
 51 */
 52void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
 53{
 54	int err;
 55	size_t read;
 56	void *buf;
 57	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
 58
 59	buf = vmalloc(len);
 60	if (!buf)
 61		return;
 62	err = mtd_read(ubi->mtd, addr, len, &read, buf);
 63	if (err && err != -EUCLEAN) {
 64		ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
 65			err, len, pnum, offset, read);
 66		goto out;
 67	}
 68
 69	ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
 70		len, pnum, offset);
 71	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
 72out:
 73	vfree(buf);
 74	return;
 75}
 76
 77/**
 78 * ubi_dump_ec_hdr - dump an erase counter header.
 79 * @ec_hdr: the erase counter header to dump
 80 */
 81void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
 82{
 83	pr_err("Erase counter header dump:\n");
 84	pr_err("\tmagic          %#08x\n", be32_to_cpu(ec_hdr->magic));
 85	pr_err("\tversion        %d\n", (int)ec_hdr->version);
 86	pr_err("\tec             %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
 87	pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
 88	pr_err("\tdata_offset    %d\n", be32_to_cpu(ec_hdr->data_offset));
 89	pr_err("\timage_seq      %d\n", be32_to_cpu(ec_hdr->image_seq));
 90	pr_err("\thdr_crc        %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
 91	pr_err("erase counter header hexdump:\n");
 92	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
 93		       ec_hdr, UBI_EC_HDR_SIZE, 1);
 94}
 95
 96/**
 97 * ubi_dump_vid_hdr - dump a volume identifier header.
 98 * @vid_hdr: the volume identifier header to dump
 99 */
100void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
101{
102	pr_err("Volume identifier header dump:\n");
103	pr_err("\tmagic     %08x\n", be32_to_cpu(vid_hdr->magic));
104	pr_err("\tversion   %d\n",  (int)vid_hdr->version);
105	pr_err("\tvol_type  %d\n",  (int)vid_hdr->vol_type);
106	pr_err("\tcopy_flag %d\n",  (int)vid_hdr->copy_flag);
107	pr_err("\tcompat    %d\n",  (int)vid_hdr->compat);
108	pr_err("\tvol_id    %d\n",  be32_to_cpu(vid_hdr->vol_id));
109	pr_err("\tlnum      %d\n",  be32_to_cpu(vid_hdr->lnum));
110	pr_err("\tdata_size %d\n",  be32_to_cpu(vid_hdr->data_size));
111	pr_err("\tused_ebs  %d\n",  be32_to_cpu(vid_hdr->used_ebs));
112	pr_err("\tdata_pad  %d\n",  be32_to_cpu(vid_hdr->data_pad));
113	pr_err("\tsqnum     %llu\n",
114		(unsigned long long)be64_to_cpu(vid_hdr->sqnum));
115	pr_err("\thdr_crc   %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
116	pr_err("Volume identifier header hexdump:\n");
117	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
118		       vid_hdr, UBI_VID_HDR_SIZE, 1);
119}
120
121/**
122 * ubi_dump_vol_info - dump volume information.
123 * @vol: UBI volume description object
124 */
125void ubi_dump_vol_info(const struct ubi_volume *vol)
126{
127	pr_err("Volume information dump:\n");
128	pr_err("\tvol_id          %d\n", vol->vol_id);
129	pr_err("\treserved_pebs   %d\n", vol->reserved_pebs);
130	pr_err("\talignment       %d\n", vol->alignment);
131	pr_err("\tdata_pad        %d\n", vol->data_pad);
132	pr_err("\tvol_type        %d\n", vol->vol_type);
133	pr_err("\tname_len        %d\n", vol->name_len);
134	pr_err("\tusable_leb_size %d\n", vol->usable_leb_size);
135	pr_err("\tused_ebs        %d\n", vol->used_ebs);
136	pr_err("\tused_bytes      %lld\n", vol->used_bytes);
137	pr_err("\tlast_eb_bytes   %d\n", vol->last_eb_bytes);
138	pr_err("\tcorrupted       %d\n", vol->corrupted);
139	pr_err("\tupd_marker      %d\n", vol->upd_marker);
140	pr_err("\tskip_check      %d\n", vol->skip_check);
141
142	if (vol->name_len <= UBI_VOL_NAME_MAX &&
143	    strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
144		pr_err("\tname            %s\n", vol->name);
145	} else {
146		pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
147		       vol->name[0], vol->name[1], vol->name[2],
148		       vol->name[3], vol->name[4]);
149	}
150}
151
152/**
153 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
154 * @r: the object to dump
155 * @idx: volume table index
156 */
157void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
158{
159	int name_len = be16_to_cpu(r->name_len);
160
161	pr_err("Volume table record %d dump:\n", idx);
162	pr_err("\treserved_pebs   %d\n", be32_to_cpu(r->reserved_pebs));
163	pr_err("\talignment       %d\n", be32_to_cpu(r->alignment));
164	pr_err("\tdata_pad        %d\n", be32_to_cpu(r->data_pad));
165	pr_err("\tvol_type        %d\n", (int)r->vol_type);
166	pr_err("\tupd_marker      %d\n", (int)r->upd_marker);
167	pr_err("\tname_len        %d\n", name_len);
168
169	if (r->name[0] == '\0') {
170		pr_err("\tname            NULL\n");
171		return;
172	}
173
174	if (name_len <= UBI_VOL_NAME_MAX &&
175	    strnlen(&r->name[0], name_len + 1) == name_len) {
176		pr_err("\tname            %s\n", &r->name[0]);
177	} else {
178		pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
179			r->name[0], r->name[1], r->name[2], r->name[3],
180			r->name[4]);
181	}
182	pr_err("\tcrc             %#08x\n", be32_to_cpu(r->crc));
183}
184
185/**
186 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
187 * @av: the object to dump
188 */
189void ubi_dump_av(const struct ubi_ainf_volume *av)
190{
191	pr_err("Volume attaching information dump:\n");
192	pr_err("\tvol_id         %d\n", av->vol_id);
193	pr_err("\thighest_lnum   %d\n", av->highest_lnum);
194	pr_err("\tleb_count      %d\n", av->leb_count);
195	pr_err("\tcompat         %d\n", av->compat);
196	pr_err("\tvol_type       %d\n", av->vol_type);
197	pr_err("\tused_ebs       %d\n", av->used_ebs);
198	pr_err("\tlast_data_size %d\n", av->last_data_size);
199	pr_err("\tdata_pad       %d\n", av->data_pad);
200}
201
202/**
203 * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
204 * @aeb: the object to dump
205 * @type: object type: 0 - not corrupted, 1 - corrupted
206 */
207void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
208{
209	pr_err("eraseblock attaching information dump:\n");
210	pr_err("\tec       %d\n", aeb->ec);
211	pr_err("\tpnum     %d\n", aeb->pnum);
212	if (type == 0) {
213		pr_err("\tlnum     %d\n", aeb->lnum);
214		pr_err("\tscrub    %d\n", aeb->scrub);
215		pr_err("\tsqnum    %llu\n", aeb->sqnum);
216	}
217}
218
219/**
220 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
221 * @req: the object to dump
222 */
223void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
224{
225	char nm[17];
226
227	pr_err("Volume creation request dump:\n");
228	pr_err("\tvol_id    %d\n",   req->vol_id);
229	pr_err("\talignment %d\n",   req->alignment);
230	pr_err("\tbytes     %lld\n", (long long)req->bytes);
231	pr_err("\tvol_type  %d\n",   req->vol_type);
232	pr_err("\tname_len  %d\n",   req->name_len);
233
234	memcpy(nm, req->name, 16);
235	nm[16] = 0;
236	pr_err("\t1st 16 characters of name: %s\n", nm);
237}
238
239/*
240 * Root directory for UBI stuff in debugfs. Contains sub-directories which
241 * contain the stuff specific to particular UBI devices.
242 */
243static struct dentry *dfs_rootdir;
244
245#ifdef CONFIG_MTD_UBI_FAULT_INJECTION
246static void dfs_create_fault_entry(struct dentry *parent)
247{
248	struct dentry *dir;
249
250	dir = debugfs_create_dir("fault_inject", parent);
251	if (IS_ERR_OR_NULL(dir)) {
252		int err = dir ? PTR_ERR(dir) : -ENODEV;
253
254		pr_warn("UBI error: cannot create \"fault_inject\" debugfs directory, error %d\n",
255			 err);
256		return;
257	}
258
259	fault_create_debugfs_attr("emulate_eccerr", dir,
260				  &fault_eccerr_attr);
261
262	fault_create_debugfs_attr("emulate_read_failure", dir,
263				  &fault_read_failure_attr);
264
265	fault_create_debugfs_attr("emulate_bitflips", dir,
266				  &fault_bitflips_attr);
267
268	fault_create_debugfs_attr("emulate_write_failure", dir,
269				  &fault_write_failure_attr);
270
271	fault_create_debugfs_attr("emulate_erase_failure", dir,
272				  &fault_erase_failure_attr);
273
274	fault_create_debugfs_attr("emulate_power_cut", dir,
275				  &fault_power_cut_attr);
276
277	fault_create_debugfs_attr("emulate_io_ff", dir,
278				  &fault_io_ff_attr);
279
280	fault_create_debugfs_attr("emulate_io_ff_bitflips", dir,
281				  &fault_io_ff_bitflips_attr);
282
283	fault_create_debugfs_attr("emulate_bad_hdr", dir,
284				  &fault_bad_hdr_attr);
285
286	fault_create_debugfs_attr("emulate_bad_hdr_ebadmsg", dir,
287				  &fault_bad_hdr_ebadmsg_attr);
288}
289#endif
290
291/**
292 * ubi_debugfs_init - create UBI debugfs directory.
293 *
294 * Create UBI debugfs directory. Returns zero in case of success and a negative
295 * error code in case of failure.
296 */
297int ubi_debugfs_init(void)
298{
299	if (!IS_ENABLED(CONFIG_DEBUG_FS))
300		return 0;
301
302	dfs_rootdir = debugfs_create_dir("ubi", NULL);
303	if (IS_ERR_OR_NULL(dfs_rootdir)) {
304		int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
305
306		pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
307		       err);
308		return err;
309	}
310
311#ifdef CONFIG_MTD_UBI_FAULT_INJECTION
312	dfs_create_fault_entry(dfs_rootdir);
313#endif
314
315	return 0;
316}
317
318/**
319 * ubi_debugfs_exit - remove UBI debugfs directory.
320 */
321void ubi_debugfs_exit(void)
322{
323	if (IS_ENABLED(CONFIG_DEBUG_FS))
324		debugfs_remove(dfs_rootdir);
325}
326
327/* Read an UBI debugfs file */
328static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
329			     size_t count, loff_t *ppos)
330{
331	unsigned long ubi_num = (unsigned long)file->private_data;
332	struct dentry *dent = file->f_path.dentry;
333	struct ubi_device *ubi;
334	struct ubi_debug_info *d;
335	char buf[16];
336	int val;
337
338	ubi = ubi_get_device(ubi_num);
339	if (!ubi)
340		return -ENODEV;
341	d = &ubi->dbg;
342
343	if (dent == d->dfs_chk_gen)
344		val = d->chk_gen;
345	else if (dent == d->dfs_chk_io)
346		val = d->chk_io;
347	else if (dent == d->dfs_chk_fastmap)
348		val = d->chk_fastmap;
349	else if (dent == d->dfs_disable_bgt)
350		val = d->disable_bgt;
351	else if (dent == d->dfs_emulate_bitflips)
352		val = d->emulate_bitflips;
353	else if (dent == d->dfs_emulate_io_failures)
354		val = d->emulate_io_failures;
355	else if (dent == d->dfs_emulate_failures) {
356		snprintf(buf, sizeof(buf), "0x%04x\n", d->emulate_failures);
357		count = simple_read_from_buffer(user_buf, count, ppos,
358						buf, strlen(buf));
359		goto out;
360	} else if (dent == d->dfs_emulate_power_cut) {
361		snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
362		count = simple_read_from_buffer(user_buf, count, ppos,
363						buf, strlen(buf));
364		goto out;
365	} else if (dent == d->dfs_power_cut_min) {
366		snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
367		count = simple_read_from_buffer(user_buf, count, ppos,
368						buf, strlen(buf));
369		goto out;
370	} else if (dent == d->dfs_power_cut_max) {
371		snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
372		count = simple_read_from_buffer(user_buf, count, ppos,
373						buf, strlen(buf));
374		goto out;
375	} else {
 
376		count = -EINVAL;
377		goto out;
378	}
379
380	if (val)
381		buf[0] = '1';
382	else
383		buf[0] = '0';
384	buf[1] = '\n';
385	buf[2] = 0x00;
386
387	count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
388
389out:
390	ubi_put_device(ubi);
391	return count;
392}
393
394/* Write an UBI debugfs file */
395static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
396			      size_t count, loff_t *ppos)
397{
398	unsigned long ubi_num = (unsigned long)file->private_data;
399	struct dentry *dent = file->f_path.dentry;
400	struct ubi_device *ubi;
401	struct ubi_debug_info *d;
402	size_t buf_size;
403	char buf[16] = {0};
404	int val;
405
406	ubi = ubi_get_device(ubi_num);
407	if (!ubi)
408		return -ENODEV;
409	d = &ubi->dbg;
410
411	buf_size = min_t(size_t, count, (sizeof(buf) - 1));
412	if (copy_from_user(buf, user_buf, buf_size)) {
413		count = -EFAULT;
414		goto out;
415	}
416
417	if (dent == d->dfs_emulate_failures) {
418		if (kstrtouint(buf, 0, &d->emulate_failures) != 0)
419			count = -EINVAL;
420		goto out;
421	} else if (dent == d->dfs_power_cut_min) {
422		if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
423			count = -EINVAL;
424		goto out;
425	} else if (dent == d->dfs_power_cut_max) {
426		if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
427			count = -EINVAL;
428		goto out;
429	} else if (dent == d->dfs_emulate_power_cut) {
430		if (kstrtoint(buf, 0, &val) != 0)
431			count = -EINVAL;
432		else
433			d->emulate_power_cut = val;
434		goto out;
435	}
436
437	if (buf[0] == '1')
438		val = 1;
439	else if (buf[0] == '0')
440		val = 0;
441	else {
442		count = -EINVAL;
443		goto out;
444	}
445
446	if (dent == d->dfs_chk_gen)
447		d->chk_gen = val;
448	else if (dent == d->dfs_chk_io)
449		d->chk_io = val;
450	else if (dent == d->dfs_chk_fastmap)
451		d->chk_fastmap = val;
452	else if (dent == d->dfs_disable_bgt)
453		d->disable_bgt = val;
454	else if (dent == d->dfs_emulate_bitflips)
455		d->emulate_bitflips = val;
456	else if (dent == d->dfs_emulate_io_failures)
457		d->emulate_io_failures = val;
458	else
459		count = -EINVAL;
460
461out:
462	ubi_put_device(ubi);
463	return count;
464}
465
466/* File operations for all UBI debugfs files except
467 * detailed_erase_block_info
468 */
469static const struct file_operations dfs_fops = {
470	.read   = dfs_file_read,
471	.write  = dfs_file_write,
472	.open	= simple_open,
473	.llseek = no_llseek,
474	.owner  = THIS_MODULE,
475};
476
477/* As long as the position is less then that total number of erase blocks,
478 * we still have more to print.
479 */
480static void *eraseblk_count_seq_start(struct seq_file *s, loff_t *pos)
481{
482	struct ubi_device *ubi = s->private;
483
484	if (*pos < ubi->peb_count)
485		return pos;
486
487	return NULL;
488}
489
490/* Since we are using the position as the iterator, we just need to check if we
491 * are done and increment the position.
492 */
493static void *eraseblk_count_seq_next(struct seq_file *s, void *v, loff_t *pos)
494{
495	struct ubi_device *ubi = s->private;
496
497	(*pos)++;
498
499	if (*pos < ubi->peb_count)
500		return pos;
501
502	return NULL;
503}
504
505static void eraseblk_count_seq_stop(struct seq_file *s, void *v)
506{
507}
508
509static int eraseblk_count_seq_show(struct seq_file *s, void *iter)
510{
511	struct ubi_device *ubi = s->private;
512	struct ubi_wl_entry *wl;
513	int *block_number = iter;
514	int erase_count = -1;
515	int err;
516
517	/* If this is the start, print a header */
518	if (*block_number == 0)
519		seq_puts(s, "physical_block_number\terase_count\n");
520
521	err = ubi_io_is_bad(ubi, *block_number);
522	if (err)
523		return err;
524
525	spin_lock(&ubi->wl_lock);
526
527	wl = ubi->lookuptbl[*block_number];
528	if (wl)
529		erase_count = wl->ec;
530
531	spin_unlock(&ubi->wl_lock);
532
533	if (erase_count < 0)
534		return 0;
535
536	seq_printf(s, "%-22d\t%-11d\n", *block_number, erase_count);
537
538	return 0;
539}
540
541static const struct seq_operations eraseblk_count_seq_ops = {
542	.start = eraseblk_count_seq_start,
543	.next = eraseblk_count_seq_next,
544	.stop = eraseblk_count_seq_stop,
545	.show = eraseblk_count_seq_show
546};
547
548static int eraseblk_count_open(struct inode *inode, struct file *f)
549{
550	struct seq_file *s;
551	int err;
552
553	err = seq_open(f, &eraseblk_count_seq_ops);
554	if (err)
555		return err;
556
557	s = f->private_data;
558	s->private = ubi_get_device((unsigned long)inode->i_private);
559
560	if (!s->private)
561		return -ENODEV;
562	else
563		return 0;
564}
565
566static int eraseblk_count_release(struct inode *inode, struct file *f)
567{
568	struct seq_file *s = f->private_data;
569	struct ubi_device *ubi = s->private;
570
571	ubi_put_device(ubi);
572
573	return seq_release(inode, f);
574}
575
576static const struct file_operations eraseblk_count_fops = {
577	.owner = THIS_MODULE,
578	.open = eraseblk_count_open,
579	.read = seq_read,
580	.llseek = seq_lseek,
581	.release = eraseblk_count_release,
582};
583
584/**
585 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
586 * @ubi: UBI device description object
587 *
588 * This function creates all debugfs files for UBI device @ubi. Returns zero in
589 * case of success and a negative error code in case of failure.
590 */
591int ubi_debugfs_init_dev(struct ubi_device *ubi)
592{
 
593	unsigned long ubi_num = ubi->ubi_num;
 
 
594	struct ubi_debug_info *d = &ubi->dbg;
595	umode_t mode = S_IRUSR | S_IWUSR;
596	int n;
597
598	if (!IS_ENABLED(CONFIG_DEBUG_FS))
599		return 0;
600
601	n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
602		     ubi->ubi_num);
603	if (n > UBI_DFS_DIR_LEN) {
604		/* The array size is too small */
605		return -EINVAL;
 
 
606	}
607
608	d->dfs_dir = debugfs_create_dir(d->dfs_dir_name, dfs_rootdir);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609
610	d->dfs_chk_gen = debugfs_create_file("chk_gen", mode, d->dfs_dir,
611					     (void *)ubi_num, &dfs_fops);
612
613	d->dfs_chk_io = debugfs_create_file("chk_io", mode, d->dfs_dir,
614					    (void *)ubi_num, &dfs_fops);
615
616	d->dfs_chk_fastmap = debugfs_create_file("chk_fastmap", mode,
617						 d->dfs_dir, (void *)ubi_num,
618						 &dfs_fops);
619
620	d->dfs_disable_bgt = debugfs_create_file("tst_disable_bgt", mode,
621						 d->dfs_dir, (void *)ubi_num,
622						 &dfs_fops);
623
624	d->dfs_emulate_bitflips = debugfs_create_file("tst_emulate_bitflips",
625						      mode, d->dfs_dir,
626						      (void *)ubi_num,
627						      &dfs_fops);
628
629	d->dfs_emulate_io_failures = debugfs_create_file("tst_emulate_io_failures",
630							 mode, d->dfs_dir,
631							 (void *)ubi_num,
632							 &dfs_fops);
633
634	d->dfs_emulate_power_cut = debugfs_create_file("tst_emulate_power_cut",
635						       mode, d->dfs_dir,
636						       (void *)ubi_num,
637						       &dfs_fops);
638
639	d->dfs_power_cut_min = debugfs_create_file("tst_emulate_power_cut_min",
640						   mode, d->dfs_dir,
641						   (void *)ubi_num, &dfs_fops);
642
643	d->dfs_power_cut_max = debugfs_create_file("tst_emulate_power_cut_max",
644						   mode, d->dfs_dir,
645						   (void *)ubi_num, &dfs_fops);
646
647	debugfs_create_file("detailed_erase_block_info", S_IRUSR, d->dfs_dir,
648			    (void *)ubi_num, &eraseblk_count_fops);
649
650#ifdef CONFIG_MTD_UBI_FAULT_INJECTION
651	d->dfs_emulate_failures = debugfs_create_file("emulate_failures",
652						       mode, d->dfs_dir,
653						       (void *)ubi_num,
654						       &dfs_fops);
655#endif
656	return 0;
 
 
 
 
 
 
 
 
657}
658
659/**
660 * ubi_debugfs_exit_dev - free all debugfs files corresponding to device @ubi
661 * @ubi: UBI device description object
662 */
663void ubi_debugfs_exit_dev(struct ubi_device *ubi)
664{
665	if (IS_ENABLED(CONFIG_DEBUG_FS))
666		debugfs_remove_recursive(ubi->dbg.dfs_dir);
667}
668
669/**
670 * ubi_dbg_power_cut - emulate a power cut if it is time to do so
671 * @ubi: UBI device description object
672 * @caller: Flags set to indicate from where the function is being called
673 *
674 * Returns non-zero if a power cut was emulated, zero if not.
675 */
676int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
677{
678	unsigned int range;
679
680	if ((ubi->dbg.emulate_power_cut & caller) == 0)
681		return 0;
682
683	if (ubi->dbg.power_cut_counter == 0) {
684		ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
685
686		if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
687			range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
688			ubi->dbg.power_cut_counter += get_random_u32_below(range);
689		}
690		return 0;
691	}
692
693	ubi->dbg.power_cut_counter--;
694	if (ubi->dbg.power_cut_counter)
695		return 0;
696
 
 
697	return 1;
698}
v4.6
 
  1/*
  2 * Copyright (c) International Business Machines Corp., 2006
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License as published by
  6 * the Free Software Foundation; either version 2 of the License, or
  7 * (at your option) any later version.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 12 * the GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program; if not, write to the Free Software
 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 17 *
 18 * Author: Artem Bityutskiy (Битюцкий Артём)
 19 */
 20
 21#include "ubi.h"
 22#include <linux/debugfs.h>
 23#include <linux/uaccess.h>
 24#include <linux/module.h>
 
 
 25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 26
 27/**
 28 * ubi_dump_flash - dump a region of flash.
 29 * @ubi: UBI device description object
 30 * @pnum: the physical eraseblock number to dump
 31 * @offset: the starting offset within the physical eraseblock to dump
 32 * @len: the length of the region to dump
 33 */
 34void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
 35{
 36	int err;
 37	size_t read;
 38	void *buf;
 39	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
 40
 41	buf = vmalloc(len);
 42	if (!buf)
 43		return;
 44	err = mtd_read(ubi->mtd, addr, len, &read, buf);
 45	if (err && err != -EUCLEAN) {
 46		ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
 47			err, len, pnum, offset, read);
 48		goto out;
 49	}
 50
 51	ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
 52		len, pnum, offset);
 53	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
 54out:
 55	vfree(buf);
 56	return;
 57}
 58
 59/**
 60 * ubi_dump_ec_hdr - dump an erase counter header.
 61 * @ec_hdr: the erase counter header to dump
 62 */
 63void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
 64{
 65	pr_err("Erase counter header dump:\n");
 66	pr_err("\tmagic          %#08x\n", be32_to_cpu(ec_hdr->magic));
 67	pr_err("\tversion        %d\n", (int)ec_hdr->version);
 68	pr_err("\tec             %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
 69	pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
 70	pr_err("\tdata_offset    %d\n", be32_to_cpu(ec_hdr->data_offset));
 71	pr_err("\timage_seq      %d\n", be32_to_cpu(ec_hdr->image_seq));
 72	pr_err("\thdr_crc        %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
 73	pr_err("erase counter header hexdump:\n");
 74	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
 75		       ec_hdr, UBI_EC_HDR_SIZE, 1);
 76}
 77
 78/**
 79 * ubi_dump_vid_hdr - dump a volume identifier header.
 80 * @vid_hdr: the volume identifier header to dump
 81 */
 82void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
 83{
 84	pr_err("Volume identifier header dump:\n");
 85	pr_err("\tmagic     %08x\n", be32_to_cpu(vid_hdr->magic));
 86	pr_err("\tversion   %d\n",  (int)vid_hdr->version);
 87	pr_err("\tvol_type  %d\n",  (int)vid_hdr->vol_type);
 88	pr_err("\tcopy_flag %d\n",  (int)vid_hdr->copy_flag);
 89	pr_err("\tcompat    %d\n",  (int)vid_hdr->compat);
 90	pr_err("\tvol_id    %d\n",  be32_to_cpu(vid_hdr->vol_id));
 91	pr_err("\tlnum      %d\n",  be32_to_cpu(vid_hdr->lnum));
 92	pr_err("\tdata_size %d\n",  be32_to_cpu(vid_hdr->data_size));
 93	pr_err("\tused_ebs  %d\n",  be32_to_cpu(vid_hdr->used_ebs));
 94	pr_err("\tdata_pad  %d\n",  be32_to_cpu(vid_hdr->data_pad));
 95	pr_err("\tsqnum     %llu\n",
 96		(unsigned long long)be64_to_cpu(vid_hdr->sqnum));
 97	pr_err("\thdr_crc   %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
 98	pr_err("Volume identifier header hexdump:\n");
 99	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
100		       vid_hdr, UBI_VID_HDR_SIZE, 1);
101}
102
103/**
104 * ubi_dump_vol_info - dump volume information.
105 * @vol: UBI volume description object
106 */
107void ubi_dump_vol_info(const struct ubi_volume *vol)
108{
109	pr_err("Volume information dump:\n");
110	pr_err("\tvol_id          %d\n", vol->vol_id);
111	pr_err("\treserved_pebs   %d\n", vol->reserved_pebs);
112	pr_err("\talignment       %d\n", vol->alignment);
113	pr_err("\tdata_pad        %d\n", vol->data_pad);
114	pr_err("\tvol_type        %d\n", vol->vol_type);
115	pr_err("\tname_len        %d\n", vol->name_len);
116	pr_err("\tusable_leb_size %d\n", vol->usable_leb_size);
117	pr_err("\tused_ebs        %d\n", vol->used_ebs);
118	pr_err("\tused_bytes      %lld\n", vol->used_bytes);
119	pr_err("\tlast_eb_bytes   %d\n", vol->last_eb_bytes);
120	pr_err("\tcorrupted       %d\n", vol->corrupted);
121	pr_err("\tupd_marker      %d\n", vol->upd_marker);
 
122
123	if (vol->name_len <= UBI_VOL_NAME_MAX &&
124	    strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
125		pr_err("\tname            %s\n", vol->name);
126	} else {
127		pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
128		       vol->name[0], vol->name[1], vol->name[2],
129		       vol->name[3], vol->name[4]);
130	}
131}
132
133/**
134 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
135 * @r: the object to dump
136 * @idx: volume table index
137 */
138void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
139{
140	int name_len = be16_to_cpu(r->name_len);
141
142	pr_err("Volume table record %d dump:\n", idx);
143	pr_err("\treserved_pebs   %d\n", be32_to_cpu(r->reserved_pebs));
144	pr_err("\talignment       %d\n", be32_to_cpu(r->alignment));
145	pr_err("\tdata_pad        %d\n", be32_to_cpu(r->data_pad));
146	pr_err("\tvol_type        %d\n", (int)r->vol_type);
147	pr_err("\tupd_marker      %d\n", (int)r->upd_marker);
148	pr_err("\tname_len        %d\n", name_len);
149
150	if (r->name[0] == '\0') {
151		pr_err("\tname            NULL\n");
152		return;
153	}
154
155	if (name_len <= UBI_VOL_NAME_MAX &&
156	    strnlen(&r->name[0], name_len + 1) == name_len) {
157		pr_err("\tname            %s\n", &r->name[0]);
158	} else {
159		pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
160			r->name[0], r->name[1], r->name[2], r->name[3],
161			r->name[4]);
162	}
163	pr_err("\tcrc             %#08x\n", be32_to_cpu(r->crc));
164}
165
166/**
167 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
168 * @av: the object to dump
169 */
170void ubi_dump_av(const struct ubi_ainf_volume *av)
171{
172	pr_err("Volume attaching information dump:\n");
173	pr_err("\tvol_id         %d\n", av->vol_id);
174	pr_err("\thighest_lnum   %d\n", av->highest_lnum);
175	pr_err("\tleb_count      %d\n", av->leb_count);
176	pr_err("\tcompat         %d\n", av->compat);
177	pr_err("\tvol_type       %d\n", av->vol_type);
178	pr_err("\tused_ebs       %d\n", av->used_ebs);
179	pr_err("\tlast_data_size %d\n", av->last_data_size);
180	pr_err("\tdata_pad       %d\n", av->data_pad);
181}
182
183/**
184 * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
185 * @aeb: the object to dump
186 * @type: object type: 0 - not corrupted, 1 - corrupted
187 */
188void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
189{
190	pr_err("eraseblock attaching information dump:\n");
191	pr_err("\tec       %d\n", aeb->ec);
192	pr_err("\tpnum     %d\n", aeb->pnum);
193	if (type == 0) {
194		pr_err("\tlnum     %d\n", aeb->lnum);
195		pr_err("\tscrub    %d\n", aeb->scrub);
196		pr_err("\tsqnum    %llu\n", aeb->sqnum);
197	}
198}
199
200/**
201 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
202 * @req: the object to dump
203 */
204void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
205{
206	char nm[17];
207
208	pr_err("Volume creation request dump:\n");
209	pr_err("\tvol_id    %d\n",   req->vol_id);
210	pr_err("\talignment %d\n",   req->alignment);
211	pr_err("\tbytes     %lld\n", (long long)req->bytes);
212	pr_err("\tvol_type  %d\n",   req->vol_type);
213	pr_err("\tname_len  %d\n",   req->name_len);
214
215	memcpy(nm, req->name, 16);
216	nm[16] = 0;
217	pr_err("\t1st 16 characters of name: %s\n", nm);
218}
219
220/*
221 * Root directory for UBI stuff in debugfs. Contains sub-directories which
222 * contain the stuff specific to particular UBI devices.
223 */
224static struct dentry *dfs_rootdir;
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226/**
227 * ubi_debugfs_init - create UBI debugfs directory.
228 *
229 * Create UBI debugfs directory. Returns zero in case of success and a negative
230 * error code in case of failure.
231 */
232int ubi_debugfs_init(void)
233{
234	if (!IS_ENABLED(CONFIG_DEBUG_FS))
235		return 0;
236
237	dfs_rootdir = debugfs_create_dir("ubi", NULL);
238	if (IS_ERR_OR_NULL(dfs_rootdir)) {
239		int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
240
241		pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
242		       err);
243		return err;
244	}
245
 
 
 
 
246	return 0;
247}
248
249/**
250 * ubi_debugfs_exit - remove UBI debugfs directory.
251 */
252void ubi_debugfs_exit(void)
253{
254	if (IS_ENABLED(CONFIG_DEBUG_FS))
255		debugfs_remove(dfs_rootdir);
256}
257
258/* Read an UBI debugfs file */
259static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
260			     size_t count, loff_t *ppos)
261{
262	unsigned long ubi_num = (unsigned long)file->private_data;
263	struct dentry *dent = file->f_path.dentry;
264	struct ubi_device *ubi;
265	struct ubi_debug_info *d;
266	char buf[8];
267	int val;
268
269	ubi = ubi_get_device(ubi_num);
270	if (!ubi)
271		return -ENODEV;
272	d = &ubi->dbg;
273
274	if (dent == d->dfs_chk_gen)
275		val = d->chk_gen;
276	else if (dent == d->dfs_chk_io)
277		val = d->chk_io;
278	else if (dent == d->dfs_chk_fastmap)
279		val = d->chk_fastmap;
280	else if (dent == d->dfs_disable_bgt)
281		val = d->disable_bgt;
282	else if (dent == d->dfs_emulate_bitflips)
283		val = d->emulate_bitflips;
284	else if (dent == d->dfs_emulate_io_failures)
285		val = d->emulate_io_failures;
286	else if (dent == d->dfs_emulate_power_cut) {
 
 
 
 
 
287		snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
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_min) {
292		snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
293		count = simple_read_from_buffer(user_buf, count, ppos,
294						buf, strlen(buf));
295		goto out;
296	} else if (dent == d->dfs_power_cut_max) {
297		snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
298		count = simple_read_from_buffer(user_buf, count, ppos,
299						buf, strlen(buf));
300		goto out;
301	}
302	else {
303		count = -EINVAL;
304		goto out;
305	}
306
307	if (val)
308		buf[0] = '1';
309	else
310		buf[0] = '0';
311	buf[1] = '\n';
312	buf[2] = 0x00;
313
314	count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
315
316out:
317	ubi_put_device(ubi);
318	return count;
319}
320
321/* Write an UBI debugfs file */
322static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
323			      size_t count, loff_t *ppos)
324{
325	unsigned long ubi_num = (unsigned long)file->private_data;
326	struct dentry *dent = file->f_path.dentry;
327	struct ubi_device *ubi;
328	struct ubi_debug_info *d;
329	size_t buf_size;
330	char buf[8] = {0};
331	int val;
332
333	ubi = ubi_get_device(ubi_num);
334	if (!ubi)
335		return -ENODEV;
336	d = &ubi->dbg;
337
338	buf_size = min_t(size_t, count, (sizeof(buf) - 1));
339	if (copy_from_user(buf, user_buf, buf_size)) {
340		count = -EFAULT;
341		goto out;
342	}
343
344	if (dent == d->dfs_power_cut_min) {
 
 
 
 
345		if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
346			count = -EINVAL;
347		goto out;
348	} else if (dent == d->dfs_power_cut_max) {
349		if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
350			count = -EINVAL;
351		goto out;
352	} else if (dent == d->dfs_emulate_power_cut) {
353		if (kstrtoint(buf, 0, &val) != 0)
354			count = -EINVAL;
355		d->emulate_power_cut = val;
 
356		goto out;
357	}
358
359	if (buf[0] == '1')
360		val = 1;
361	else if (buf[0] == '0')
362		val = 0;
363	else {
364		count = -EINVAL;
365		goto out;
366	}
367
368	if (dent == d->dfs_chk_gen)
369		d->chk_gen = val;
370	else if (dent == d->dfs_chk_io)
371		d->chk_io = val;
372	else if (dent == d->dfs_chk_fastmap)
373		d->chk_fastmap = val;
374	else if (dent == d->dfs_disable_bgt)
375		d->disable_bgt = val;
376	else if (dent == d->dfs_emulate_bitflips)
377		d->emulate_bitflips = val;
378	else if (dent == d->dfs_emulate_io_failures)
379		d->emulate_io_failures = val;
380	else
381		count = -EINVAL;
382
383out:
384	ubi_put_device(ubi);
385	return count;
386}
387
388/* File operations for all UBI debugfs files */
 
 
389static const struct file_operations dfs_fops = {
390	.read   = dfs_file_read,
391	.write  = dfs_file_write,
392	.open	= simple_open,
393	.llseek = no_llseek,
394	.owner  = THIS_MODULE,
395};
396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397/**
398 * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
399 * @ubi: UBI device description object
400 *
401 * This function creates all debugfs files for UBI device @ubi. Returns zero in
402 * case of success and a negative error code in case of failure.
403 */
404int ubi_debugfs_init_dev(struct ubi_device *ubi)
405{
406	int err, n;
407	unsigned long ubi_num = ubi->ubi_num;
408	const char *fname;
409	struct dentry *dent;
410	struct ubi_debug_info *d = &ubi->dbg;
 
 
411
412	if (!IS_ENABLED(CONFIG_DEBUG_FS))
413		return 0;
414
415	n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
416		     ubi->ubi_num);
417	if (n == UBI_DFS_DIR_LEN) {
418		/* The array size is too small */
419		fname = UBI_DFS_DIR_NAME;
420		dent = ERR_PTR(-EINVAL);
421		goto out;
422	}
423
424	fname = d->dfs_dir_name;
425	dent = debugfs_create_dir(fname, dfs_rootdir);
426	if (IS_ERR_OR_NULL(dent))
427		goto out;
428	d->dfs_dir = dent;
429
430	fname = "chk_gen";
431	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
432				   &dfs_fops);
433	if (IS_ERR_OR_NULL(dent))
434		goto out_remove;
435	d->dfs_chk_gen = dent;
436
437	fname = "chk_io";
438	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
439				   &dfs_fops);
440	if (IS_ERR_OR_NULL(dent))
441		goto out_remove;
442	d->dfs_chk_io = dent;
443
444	fname = "chk_fastmap";
445	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
446				   &dfs_fops);
447	if (IS_ERR_OR_NULL(dent))
448		goto out_remove;
449	d->dfs_chk_fastmap = dent;
450
451	fname = "tst_disable_bgt";
452	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
453				   &dfs_fops);
454	if (IS_ERR_OR_NULL(dent))
455		goto out_remove;
456	d->dfs_disable_bgt = dent;
457
458	fname = "tst_emulate_bitflips";
459	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
460				   &dfs_fops);
461	if (IS_ERR_OR_NULL(dent))
462		goto out_remove;
463	d->dfs_emulate_bitflips = dent;
464
465	fname = "tst_emulate_io_failures";
466	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
467				   &dfs_fops);
468	if (IS_ERR_OR_NULL(dent))
469		goto out_remove;
470	d->dfs_emulate_io_failures = dent;
471
472	fname = "tst_emulate_power_cut";
473	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
474				   &dfs_fops);
475	if (IS_ERR_OR_NULL(dent))
476		goto out_remove;
477	d->dfs_emulate_power_cut = dent;
478
479	fname = "tst_emulate_power_cut_min";
480	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
481				   &dfs_fops);
482	if (IS_ERR_OR_NULL(dent))
483		goto out_remove;
484	d->dfs_power_cut_min = dent;
485
486	fname = "tst_emulate_power_cut_max";
487	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
488				   &dfs_fops);
489	if (IS_ERR_OR_NULL(dent))
490		goto out_remove;
491	d->dfs_power_cut_max = dent;
492
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
493	return 0;
494
495out_remove:
496	debugfs_remove_recursive(d->dfs_dir);
497out:
498	err = dent ? PTR_ERR(dent) : -ENODEV;
499	ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
500		fname, err);
501	return err;
502}
503
504/**
505 * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
506 * @ubi: UBI device description object
507 */
508void ubi_debugfs_exit_dev(struct ubi_device *ubi)
509{
510	if (IS_ENABLED(CONFIG_DEBUG_FS))
511		debugfs_remove_recursive(ubi->dbg.dfs_dir);
512}
513
514/**
515 * ubi_dbg_power_cut - emulate a power cut if it is time to do so
516 * @ubi: UBI device description object
517 * @caller: Flags set to indicate from where the function is being called
518 *
519 * Returns non-zero if a power cut was emulated, zero if not.
520 */
521int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
522{
523	unsigned int range;
524
525	if ((ubi->dbg.emulate_power_cut & caller) == 0)
526		return 0;
527
528	if (ubi->dbg.power_cut_counter == 0) {
529		ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
530
531		if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
532			range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
533			ubi->dbg.power_cut_counter += prandom_u32() % range;
534		}
535		return 0;
536	}
537
538	ubi->dbg.power_cut_counter--;
539	if (ubi->dbg.power_cut_counter)
540		return 0;
541
542	ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
543	ubi_ro_mode(ubi);
544	return 1;
545}