debug.c 14 KB

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