debug.c 14 KB

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