debug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. #include "ubi.h"
  21. #include <linux/debugfs.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/module.h>
  24. /**
  25. * ubi_dump_flash - dump a region of flash.
  26. * @ubi: UBI device description object
  27. * @pnum: the physical eraseblock number to dump
  28. * @offset: the starting offset within the physical eraseblock to dump
  29. * @len: the length of the region to dump
  30. */
  31. void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
  32. {
  33. int err;
  34. size_t read;
  35. void *buf;
  36. loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
  37. buf = vmalloc(len);
  38. if (!buf)
  39. return;
  40. err = mtd_read(ubi->mtd, addr, len, &read, buf);
  41. if (err && err != -EUCLEAN) {
  42. ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
  43. err, len, pnum, offset, read);
  44. goto out;
  45. }
  46. ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
  47. len, pnum, offset);
  48. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
  49. out:
  50. vfree(buf);
  51. return;
  52. }
  53. /**
  54. * ubi_dump_ec_hdr - dump an erase counter header.
  55. * @ec_hdr: the erase counter header to dump
  56. */
  57. void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
  58. {
  59. pr_err("Erase counter header dump:\n");
  60. pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic));
  61. pr_err("\tversion %d\n", (int)ec_hdr->version);
  62. pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec));
  63. pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset));
  64. pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset));
  65. pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq));
  66. pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
  67. pr_err("erase counter header hexdump:\n");
  68. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  69. ec_hdr, UBI_EC_HDR_SIZE, 1);
  70. }
  71. /**
  72. * ubi_dump_vid_hdr - dump a volume identifier header.
  73. * @vid_hdr: the volume identifier header to dump
  74. */
  75. void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
  76. {
  77. pr_err("Volume identifier header dump:\n");
  78. pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
  79. pr_err("\tversion %d\n", (int)vid_hdr->version);
  80. pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type);
  81. pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
  82. pr_err("\tcompat %d\n", (int)vid_hdr->compat);
  83. pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
  84. pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
  85. pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
  86. pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
  87. pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
  88. pr_err("\tsqnum %llu\n",
  89. (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
  90. pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
  91. pr_err("Volume identifier header hexdump:\n");
  92. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
  93. vid_hdr, UBI_VID_HDR_SIZE, 1);
  94. }
  95. /**
  96. * ubi_dump_vol_info - dump volume information.
  97. * @vol: UBI volume description object
  98. */
  99. void ubi_dump_vol_info(const struct ubi_volume *vol)
  100. {
  101. pr_err("Volume information dump:\n");
  102. pr_err("\tvol_id %d\n", vol->vol_id);
  103. pr_err("\treserved_pebs %d\n", vol->reserved_pebs);
  104. pr_err("\talignment %d\n", vol->alignment);
  105. pr_err("\tdata_pad %d\n", vol->data_pad);
  106. pr_err("\tvol_type %d\n", vol->vol_type);
  107. pr_err("\tname_len %d\n", vol->name_len);
  108. pr_err("\tusable_leb_size %d\n", vol->usable_leb_size);
  109. pr_err("\tused_ebs %d\n", vol->used_ebs);
  110. pr_err("\tused_bytes %lld\n", vol->used_bytes);
  111. pr_err("\tlast_eb_bytes %d\n", vol->last_eb_bytes);
  112. pr_err("\tcorrupted %d\n", vol->corrupted);
  113. pr_err("\tupd_marker %d\n", vol->upd_marker);
  114. if (vol->name_len <= UBI_VOL_NAME_MAX &&
  115. strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
  116. pr_err("\tname %s\n", vol->name);
  117. } else {
  118. pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
  119. vol->name[0], vol->name[1], vol->name[2],
  120. vol->name[3], vol->name[4]);
  121. }
  122. }
  123. /**
  124. * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
  125. * @r: the object to dump
  126. * @idx: volume table index
  127. */
  128. void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
  129. {
  130. int name_len = be16_to_cpu(r->name_len);
  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. if (r->name[0] == '\0') {
  139. pr_err("\tname NULL\n");
  140. return;
  141. }
  142. if (name_len <= UBI_VOL_NAME_MAX &&
  143. strnlen(&r->name[0], name_len + 1) == name_len) {
  144. pr_err("\tname %s\n", &r->name[0]);
  145. } else {
  146. pr_err("\t1st 5 characters of name: %c%c%c%c%c\n",
  147. r->name[0], r->name[1], r->name[2], r->name[3],
  148. r->name[4]);
  149. }
  150. pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc));
  151. }
  152. /**
  153. * ubi_dump_av - dump a &struct ubi_ainf_volume object.
  154. * @av: the object to dump
  155. */
  156. void ubi_dump_av(const struct ubi_ainf_volume *av)
  157. {
  158. pr_err("Volume attaching information dump:\n");
  159. pr_err("\tvol_id %d\n", av->vol_id);
  160. pr_err("\thighest_lnum %d\n", av->highest_lnum);
  161. pr_err("\tleb_count %d\n", av->leb_count);
  162. pr_err("\tcompat %d\n", av->compat);
  163. pr_err("\tvol_type %d\n", av->vol_type);
  164. pr_err("\tused_ebs %d\n", av->used_ebs);
  165. pr_err("\tlast_data_size %d\n", av->last_data_size);
  166. pr_err("\tdata_pad %d\n", av->data_pad);
  167. }
  168. /**
  169. * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
  170. * @aeb: the object to dump
  171. * @type: object type: 0 - not corrupted, 1 - corrupted
  172. */
  173. void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
  174. {
  175. pr_err("eraseblock attaching information dump:\n");
  176. pr_err("\tec %d\n", aeb->ec);
  177. pr_err("\tpnum %d\n", aeb->pnum);
  178. if (type == 0) {
  179. pr_err("\tlnum %d\n", aeb->lnum);
  180. pr_err("\tscrub %d\n", aeb->scrub);
  181. pr_err("\tsqnum %llu\n", aeb->sqnum);
  182. }
  183. }
  184. /**
  185. * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
  186. * @req: the object to dump
  187. */
  188. void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
  189. {
  190. char nm[17];
  191. pr_err("Volume creation request dump:\n");
  192. pr_err("\tvol_id %d\n", req->vol_id);
  193. pr_err("\talignment %d\n", req->alignment);
  194. pr_err("\tbytes %lld\n", (long long)req->bytes);
  195. pr_err("\tvol_type %d\n", req->vol_type);
  196. pr_err("\tname_len %d\n", req->name_len);
  197. memcpy(nm, req->name, 16);
  198. nm[16] = 0;
  199. pr_err("\t1st 16 characters of name: %s\n", nm);
  200. }
  201. /*
  202. * Root directory for UBI stuff in debugfs. Contains sub-directories which
  203. * contain the stuff specific to particular UBI devices.
  204. */
  205. static struct dentry *dfs_rootdir;
  206. /**
  207. * ubi_debugfs_init - create UBI debugfs directory.
  208. *
  209. * Create UBI debugfs directory. Returns zero in case of success and a negative
  210. * error code in case of failure.
  211. */
  212. int ubi_debugfs_init(void)
  213. {
  214. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  215. return 0;
  216. dfs_rootdir = debugfs_create_dir("ubi", NULL);
  217. if (IS_ERR_OR_NULL(dfs_rootdir)) {
  218. int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
  219. pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n",
  220. err);
  221. return err;
  222. }
  223. return 0;
  224. }
  225. /**
  226. * ubi_debugfs_exit - remove UBI debugfs directory.
  227. */
  228. void ubi_debugfs_exit(void)
  229. {
  230. if (IS_ENABLED(CONFIG_DEBUG_FS))
  231. debugfs_remove(dfs_rootdir);
  232. }
  233. /* Read an UBI debugfs file */
  234. static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
  235. size_t count, loff_t *ppos)
  236. {
  237. unsigned long ubi_num = (unsigned long)file->private_data;
  238. struct dentry *dent = file->f_path.dentry;
  239. struct ubi_device *ubi;
  240. struct ubi_debug_info *d;
  241. char buf[8];
  242. int val;
  243. ubi = ubi_get_device(ubi_num);
  244. if (!ubi)
  245. return -ENODEV;
  246. d = &ubi->dbg;
  247. if (dent == d->dfs_chk_gen)
  248. val = d->chk_gen;
  249. else if (dent == d->dfs_chk_io)
  250. val = d->chk_io;
  251. else if (dent == d->dfs_chk_fastmap)
  252. val = d->chk_fastmap;
  253. else if (dent == d->dfs_disable_bgt)
  254. val = d->disable_bgt;
  255. else if (dent == d->dfs_emulate_bitflips)
  256. val = d->emulate_bitflips;
  257. else if (dent == d->dfs_emulate_io_failures)
  258. val = d->emulate_io_failures;
  259. else if (dent == d->dfs_emulate_power_cut) {
  260. snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut);
  261. count = simple_read_from_buffer(user_buf, count, ppos,
  262. buf, strlen(buf));
  263. goto out;
  264. } else if (dent == d->dfs_power_cut_min) {
  265. snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min);
  266. count = simple_read_from_buffer(user_buf, count, ppos,
  267. buf, strlen(buf));
  268. goto out;
  269. } else if (dent == d->dfs_power_cut_max) {
  270. snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max);
  271. count = simple_read_from_buffer(user_buf, count, ppos,
  272. buf, strlen(buf));
  273. goto out;
  274. }
  275. else {
  276. count = -EINVAL;
  277. goto out;
  278. }
  279. if (val)
  280. buf[0] = '1';
  281. else
  282. buf[0] = '0';
  283. buf[1] = '\n';
  284. buf[2] = 0x00;
  285. count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  286. out:
  287. ubi_put_device(ubi);
  288. return count;
  289. }
  290. /* Write an UBI debugfs file */
  291. static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
  292. size_t count, loff_t *ppos)
  293. {
  294. unsigned long ubi_num = (unsigned long)file->private_data;
  295. struct dentry *dent = file->f_path.dentry;
  296. struct ubi_device *ubi;
  297. struct ubi_debug_info *d;
  298. size_t buf_size;
  299. char buf[8] = {0};
  300. int val;
  301. ubi = ubi_get_device(ubi_num);
  302. if (!ubi)
  303. return -ENODEV;
  304. d = &ubi->dbg;
  305. buf_size = min_t(size_t, count, (sizeof(buf) - 1));
  306. if (copy_from_user(buf, user_buf, buf_size)) {
  307. count = -EFAULT;
  308. goto out;
  309. }
  310. if (dent == d->dfs_power_cut_min) {
  311. if (kstrtouint(buf, 0, &d->power_cut_min) != 0)
  312. count = -EINVAL;
  313. goto out;
  314. } else if (dent == d->dfs_power_cut_max) {
  315. if (kstrtouint(buf, 0, &d->power_cut_max) != 0)
  316. count = -EINVAL;
  317. goto out;
  318. } else if (dent == d->dfs_emulate_power_cut) {
  319. if (kstrtoint(buf, 0, &val) != 0)
  320. count = -EINVAL;
  321. d->emulate_power_cut = val;
  322. goto out;
  323. }
  324. if (buf[0] == '1')
  325. val = 1;
  326. else if (buf[0] == '0')
  327. val = 0;
  328. else {
  329. count = -EINVAL;
  330. goto out;
  331. }
  332. if (dent == d->dfs_chk_gen)
  333. d->chk_gen = val;
  334. else if (dent == d->dfs_chk_io)
  335. d->chk_io = val;
  336. else if (dent == d->dfs_chk_fastmap)
  337. d->chk_fastmap = val;
  338. else if (dent == d->dfs_disable_bgt)
  339. d->disable_bgt = val;
  340. else if (dent == d->dfs_emulate_bitflips)
  341. d->emulate_bitflips = val;
  342. else if (dent == d->dfs_emulate_io_failures)
  343. d->emulate_io_failures = val;
  344. else
  345. count = -EINVAL;
  346. out:
  347. ubi_put_device(ubi);
  348. return count;
  349. }
  350. /* File operations for all UBI debugfs files */
  351. static const struct file_operations dfs_fops = {
  352. .read = dfs_file_read,
  353. .write = dfs_file_write,
  354. .open = simple_open,
  355. .llseek = no_llseek,
  356. .owner = THIS_MODULE,
  357. };
  358. /**
  359. * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
  360. * @ubi: UBI device description object
  361. *
  362. * This function creates all debugfs files for UBI device @ubi. Returns zero in
  363. * case of success and a negative error code in case of failure.
  364. */
  365. int ubi_debugfs_init_dev(struct ubi_device *ubi)
  366. {
  367. int err, n;
  368. unsigned long ubi_num = ubi->ubi_num;
  369. const char *fname;
  370. struct dentry *dent;
  371. struct ubi_debug_info *d = &ubi->dbg;
  372. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  373. return 0;
  374. n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
  375. ubi->ubi_num);
  376. if (n == UBI_DFS_DIR_LEN) {
  377. /* The array size is too small */
  378. fname = UBI_DFS_DIR_NAME;
  379. dent = ERR_PTR(-EINVAL);
  380. goto out;
  381. }
  382. fname = d->dfs_dir_name;
  383. dent = debugfs_create_dir(fname, dfs_rootdir);
  384. if (IS_ERR_OR_NULL(dent))
  385. goto out;
  386. d->dfs_dir = dent;
  387. fname = "chk_gen";
  388. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  389. &dfs_fops);
  390. if (IS_ERR_OR_NULL(dent))
  391. goto out_remove;
  392. d->dfs_chk_gen = dent;
  393. fname = "chk_io";
  394. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  395. &dfs_fops);
  396. if (IS_ERR_OR_NULL(dent))
  397. goto out_remove;
  398. d->dfs_chk_io = dent;
  399. fname = "chk_fastmap";
  400. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  401. &dfs_fops);
  402. if (IS_ERR_OR_NULL(dent))
  403. goto out_remove;
  404. d->dfs_chk_fastmap = dent;
  405. fname = "tst_disable_bgt";
  406. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  407. &dfs_fops);
  408. if (IS_ERR_OR_NULL(dent))
  409. goto out_remove;
  410. d->dfs_disable_bgt = dent;
  411. fname = "tst_emulate_bitflips";
  412. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  413. &dfs_fops);
  414. if (IS_ERR_OR_NULL(dent))
  415. goto out_remove;
  416. d->dfs_emulate_bitflips = dent;
  417. fname = "tst_emulate_io_failures";
  418. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  419. &dfs_fops);
  420. if (IS_ERR_OR_NULL(dent))
  421. goto out_remove;
  422. d->dfs_emulate_io_failures = dent;
  423. fname = "tst_emulate_power_cut";
  424. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  425. &dfs_fops);
  426. if (IS_ERR_OR_NULL(dent))
  427. goto out_remove;
  428. d->dfs_emulate_power_cut = dent;
  429. fname = "tst_emulate_power_cut_min";
  430. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  431. &dfs_fops);
  432. if (IS_ERR_OR_NULL(dent))
  433. goto out_remove;
  434. d->dfs_power_cut_min = dent;
  435. fname = "tst_emulate_power_cut_max";
  436. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  437. &dfs_fops);
  438. if (IS_ERR_OR_NULL(dent))
  439. goto out_remove;
  440. d->dfs_power_cut_max = dent;
  441. return 0;
  442. out_remove:
  443. debugfs_remove_recursive(d->dfs_dir);
  444. out:
  445. err = dent ? PTR_ERR(dent) : -ENODEV;
  446. ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n",
  447. fname, err);
  448. return err;
  449. }
  450. /**
  451. * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
  452. * @ubi: UBI device description object
  453. */
  454. void ubi_debugfs_exit_dev(struct ubi_device *ubi)
  455. {
  456. if (IS_ENABLED(CONFIG_DEBUG_FS))
  457. debugfs_remove_recursive(ubi->dbg.dfs_dir);
  458. }
  459. /**
  460. * ubi_dbg_power_cut - emulate a power cut if it is time to do so
  461. * @ubi: UBI device description object
  462. * @caller: Flags set to indicate from where the function is being called
  463. *
  464. * Returns non-zero if a power cut was emulated, zero if not.
  465. */
  466. int ubi_dbg_power_cut(struct ubi_device *ubi, int caller)
  467. {
  468. unsigned int range;
  469. if ((ubi->dbg.emulate_power_cut & caller) == 0)
  470. return 0;
  471. if (ubi->dbg.power_cut_counter == 0) {
  472. ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min;
  473. if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) {
  474. range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min;
  475. ubi->dbg.power_cut_counter += prandom_u32() % range;
  476. }
  477. return 0;
  478. }
  479. ubi->dbg.power_cut_counter--;
  480. if (ubi->dbg.power_cut_counter)
  481. return 0;
  482. ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX");
  483. ubi_ro_mode(ubi);
  484. return 1;
  485. }