debug.c 13 KB

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