debug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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("error %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("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. * ubi_debugging_init_dev - initialize debugging for an UBI device.
  203. * @ubi: UBI device description object
  204. *
  205. * This function initializes debugging-related data for UBI device @ubi.
  206. * Returns zero in case of success and a negative error code in case of
  207. * failure.
  208. */
  209. int ubi_debugging_init_dev(struct ubi_device *ubi)
  210. {
  211. ubi->dbg = kzalloc(sizeof(struct ubi_debug_info), GFP_KERNEL);
  212. if (!ubi->dbg)
  213. return -ENOMEM;
  214. return 0;
  215. }
  216. /**
  217. * ubi_debugging_exit_dev - free debugging data for an UBI device.
  218. * @ubi: UBI device description object
  219. */
  220. void ubi_debugging_exit_dev(struct ubi_device *ubi)
  221. {
  222. kfree(ubi->dbg);
  223. }
  224. /*
  225. * Root directory for UBI stuff in debugfs. Contains sub-directories which
  226. * contain the stuff specific to particular UBI devices.
  227. */
  228. static struct dentry *dfs_rootdir;
  229. /**
  230. * ubi_debugfs_init - create UBI debugfs directory.
  231. *
  232. * Create UBI debugfs directory. Returns zero in case of success and a negative
  233. * error code in case of failure.
  234. */
  235. int ubi_debugfs_init(void)
  236. {
  237. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  238. return 0;
  239. dfs_rootdir = debugfs_create_dir("ubi", NULL);
  240. if (IS_ERR_OR_NULL(dfs_rootdir)) {
  241. int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
  242. ubi_err("cannot create \"ubi\" debugfs directory, error %d\n",
  243. err);
  244. return err;
  245. }
  246. return 0;
  247. }
  248. /**
  249. * ubi_debugfs_exit - remove UBI debugfs directory.
  250. */
  251. void ubi_debugfs_exit(void)
  252. {
  253. if (IS_ENABLED(CONFIG_DEBUG_FS))
  254. debugfs_remove(dfs_rootdir);
  255. }
  256. /* Read an UBI debugfs file */
  257. static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
  258. size_t count, loff_t *ppos)
  259. {
  260. unsigned long ubi_num = (unsigned long)file->private_data;
  261. struct dentry *dent = file->f_path.dentry;
  262. struct ubi_device *ubi;
  263. struct ubi_debug_info *d;
  264. char buf[3];
  265. int val;
  266. ubi = ubi_get_device(ubi_num);
  267. if (!ubi)
  268. return -ENODEV;
  269. d = ubi->dbg;
  270. if (dent == d->dfs_chk_gen)
  271. val = d->chk_gen;
  272. else if (dent == d->dfs_chk_io)
  273. val = d->chk_io;
  274. else if (dent == d->dfs_disable_bgt)
  275. val = d->disable_bgt;
  276. else if (dent == d->dfs_emulate_bitflips)
  277. val = d->emulate_bitflips;
  278. else if (dent == d->dfs_emulate_io_failures)
  279. val = d->emulate_io_failures;
  280. else {
  281. count = -EINVAL;
  282. goto out;
  283. }
  284. if (val)
  285. buf[0] = '1';
  286. else
  287. buf[0] = '0';
  288. buf[1] = '\n';
  289. buf[2] = 0x00;
  290. count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  291. out:
  292. ubi_put_device(ubi);
  293. return count;
  294. }
  295. /* Write an UBI debugfs file */
  296. static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
  297. size_t count, loff_t *ppos)
  298. {
  299. unsigned long ubi_num = (unsigned long)file->private_data;
  300. struct dentry *dent = file->f_path.dentry;
  301. struct ubi_device *ubi;
  302. struct ubi_debug_info *d;
  303. size_t buf_size;
  304. char buf[8];
  305. int val;
  306. ubi = ubi_get_device(ubi_num);
  307. if (!ubi)
  308. return -ENODEV;
  309. d = ubi->dbg;
  310. buf_size = min_t(size_t, count, (sizeof(buf) - 1));
  311. if (copy_from_user(buf, user_buf, buf_size)) {
  312. count = -EFAULT;
  313. goto out;
  314. }
  315. if (buf[0] == '1')
  316. val = 1;
  317. else if (buf[0] == '0')
  318. val = 0;
  319. else {
  320. count = -EINVAL;
  321. goto out;
  322. }
  323. if (dent == d->dfs_chk_gen)
  324. d->chk_gen = val;
  325. else if (dent == d->dfs_chk_io)
  326. d->chk_io = val;
  327. else if (dent == d->dfs_disable_bgt)
  328. d->disable_bgt = val;
  329. else if (dent == d->dfs_emulate_bitflips)
  330. d->emulate_bitflips = val;
  331. else if (dent == d->dfs_emulate_io_failures)
  332. d->emulate_io_failures = val;
  333. else
  334. count = -EINVAL;
  335. out:
  336. ubi_put_device(ubi);
  337. return count;
  338. }
  339. /* File operations for all UBI debugfs files */
  340. static const struct file_operations dfs_fops = {
  341. .read = dfs_file_read,
  342. .write = dfs_file_write,
  343. .open = simple_open,
  344. .llseek = no_llseek,
  345. .owner = THIS_MODULE,
  346. };
  347. /**
  348. * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
  349. * @ubi: UBI device description object
  350. *
  351. * This function creates all debugfs files for UBI device @ubi. Returns zero in
  352. * case of success and a negative error code in case of failure.
  353. */
  354. int ubi_debugfs_init_dev(struct ubi_device *ubi)
  355. {
  356. int err, n;
  357. unsigned long ubi_num = ubi->ubi_num;
  358. const char *fname;
  359. struct dentry *dent;
  360. struct ubi_debug_info *d = ubi->dbg;
  361. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  362. return 0;
  363. n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
  364. ubi->ubi_num);
  365. if (n == UBI_DFS_DIR_LEN) {
  366. /* The array size is too small */
  367. fname = UBI_DFS_DIR_NAME;
  368. dent = ERR_PTR(-EINVAL);
  369. goto out;
  370. }
  371. fname = d->dfs_dir_name;
  372. dent = debugfs_create_dir(fname, dfs_rootdir);
  373. if (IS_ERR_OR_NULL(dent))
  374. goto out;
  375. d->dfs_dir = dent;
  376. fname = "chk_gen";
  377. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  378. &dfs_fops);
  379. if (IS_ERR_OR_NULL(dent))
  380. goto out_remove;
  381. d->dfs_chk_gen = dent;
  382. fname = "chk_io";
  383. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  384. &dfs_fops);
  385. if (IS_ERR_OR_NULL(dent))
  386. goto out_remove;
  387. d->dfs_chk_io = dent;
  388. fname = "tst_disable_bgt";
  389. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  390. &dfs_fops);
  391. if (IS_ERR_OR_NULL(dent))
  392. goto out_remove;
  393. d->dfs_disable_bgt = dent;
  394. fname = "tst_emulate_bitflips";
  395. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  396. &dfs_fops);
  397. if (IS_ERR_OR_NULL(dent))
  398. goto out_remove;
  399. d->dfs_emulate_bitflips = dent;
  400. fname = "tst_emulate_io_failures";
  401. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  402. &dfs_fops);
  403. if (IS_ERR_OR_NULL(dent))
  404. goto out_remove;
  405. d->dfs_emulate_io_failures = dent;
  406. return 0;
  407. out_remove:
  408. debugfs_remove_recursive(d->dfs_dir);
  409. out:
  410. err = dent ? PTR_ERR(dent) : -ENODEV;
  411. ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n",
  412. fname, err);
  413. return err;
  414. }
  415. /**
  416. * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
  417. * @ubi: UBI device description object
  418. */
  419. void ubi_debugfs_exit_dev(struct ubi_device *ubi)
  420. {
  421. if (IS_ENABLED(CONFIG_DEBUG_FS))
  422. debugfs_remove_recursive(ubi->dbg->dfs_dir);
  423. }