debug.c 19 KB

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