debug.c 14 KB

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