debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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_av - dump a &struct ubi_ainf_volume object.
  161. * @av: the object to dump
  162. */
  163. void ubi_dump_av(const struct ubi_ainf_volume *av)
  164. {
  165. printk(KERN_DEBUG "Volume attaching information dump:\n");
  166. printk(KERN_DEBUG "\tvol_id %d\n", av->vol_id);
  167. printk(KERN_DEBUG "\thighest_lnum %d\n", av->highest_lnum);
  168. printk(KERN_DEBUG "\tleb_count %d\n", av->leb_count);
  169. printk(KERN_DEBUG "\tcompat %d\n", av->compat);
  170. printk(KERN_DEBUG "\tvol_type %d\n", av->vol_type);
  171. printk(KERN_DEBUG "\tused_ebs %d\n", av->used_ebs);
  172. printk(KERN_DEBUG "\tlast_data_size %d\n", av->last_data_size);
  173. printk(KERN_DEBUG "\tdata_pad %d\n", av->data_pad);
  174. }
  175. /**
  176. * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
  177. * @aeb: the object to dump
  178. * @type: object type: 0 - not corrupted, 1 - corrupted
  179. */
  180. void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
  181. {
  182. printk(KERN_DEBUG "eraseblock attaching information dump:\n");
  183. printk(KERN_DEBUG "\tec %d\n", aeb->ec);
  184. printk(KERN_DEBUG "\tpnum %d\n", aeb->pnum);
  185. if (type == 0) {
  186. printk(KERN_DEBUG "\tlnum %d\n", aeb->lnum);
  187. printk(KERN_DEBUG "\tscrub %d\n", aeb->scrub);
  188. printk(KERN_DEBUG "\tsqnum %llu\n", aeb->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. if (!IS_ENABLED(DEBUG_FS))
  245. return 0;
  246. dfs_rootdir = debugfs_create_dir("ubi", NULL);
  247. if (IS_ERR_OR_NULL(dfs_rootdir)) {
  248. int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir);
  249. ubi_err("cannot create \"ubi\" debugfs directory, error %d\n",
  250. err);
  251. return err;
  252. }
  253. return 0;
  254. }
  255. /**
  256. * ubi_debugfs_exit - remove UBI debugfs directory.
  257. */
  258. void ubi_debugfs_exit(void)
  259. {
  260. if (IS_ENABLED(DEBUG_FS))
  261. debugfs_remove(dfs_rootdir);
  262. }
  263. /* Read an UBI debugfs file */
  264. static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
  265. size_t count, loff_t *ppos)
  266. {
  267. unsigned long ubi_num = (unsigned long)file->private_data;
  268. struct dentry *dent = file->f_path.dentry;
  269. struct ubi_device *ubi;
  270. struct ubi_debug_info *d;
  271. char buf[3];
  272. int val;
  273. ubi = ubi_get_device(ubi_num);
  274. if (!ubi)
  275. return -ENODEV;
  276. d = ubi->dbg;
  277. if (dent == d->dfs_chk_gen)
  278. val = d->chk_gen;
  279. else if (dent == d->dfs_chk_io)
  280. val = d->chk_io;
  281. else if (dent == d->dfs_disable_bgt)
  282. val = d->disable_bgt;
  283. else if (dent == d->dfs_emulate_bitflips)
  284. val = d->emulate_bitflips;
  285. else if (dent == d->dfs_emulate_io_failures)
  286. val = d->emulate_io_failures;
  287. else {
  288. count = -EINVAL;
  289. goto out;
  290. }
  291. if (val)
  292. buf[0] = '1';
  293. else
  294. buf[0] = '0';
  295. buf[1] = '\n';
  296. buf[2] = 0x00;
  297. count = simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  298. out:
  299. ubi_put_device(ubi);
  300. return count;
  301. }
  302. /* Write an UBI debugfs file */
  303. static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
  304. size_t count, loff_t *ppos)
  305. {
  306. unsigned long ubi_num = (unsigned long)file->private_data;
  307. struct dentry *dent = file->f_path.dentry;
  308. struct ubi_device *ubi;
  309. struct ubi_debug_info *d;
  310. size_t buf_size;
  311. char buf[8];
  312. int val;
  313. ubi = ubi_get_device(ubi_num);
  314. if (!ubi)
  315. return -ENODEV;
  316. d = ubi->dbg;
  317. buf_size = min_t(size_t, count, (sizeof(buf) - 1));
  318. if (copy_from_user(buf, user_buf, buf_size)) {
  319. count = -EFAULT;
  320. goto out;
  321. }
  322. if (buf[0] == '1')
  323. val = 1;
  324. else if (buf[0] == '0')
  325. val = 0;
  326. else {
  327. count = -EINVAL;
  328. goto out;
  329. }
  330. if (dent == d->dfs_chk_gen)
  331. d->chk_gen = val;
  332. else if (dent == d->dfs_chk_io)
  333. d->chk_io = val;
  334. else if (dent == d->dfs_disable_bgt)
  335. d->disable_bgt = val;
  336. else if (dent == d->dfs_emulate_bitflips)
  337. d->emulate_bitflips = val;
  338. else if (dent == d->dfs_emulate_io_failures)
  339. d->emulate_io_failures = val;
  340. else
  341. count = -EINVAL;
  342. out:
  343. ubi_put_device(ubi);
  344. return count;
  345. }
  346. /* File operations for all UBI debugfs files */
  347. static const struct file_operations dfs_fops = {
  348. .read = dfs_file_read,
  349. .write = dfs_file_write,
  350. .open = simple_open,
  351. .llseek = no_llseek,
  352. .owner = THIS_MODULE,
  353. };
  354. /**
  355. * ubi_debugfs_init_dev - initialize debugfs for an UBI device.
  356. * @ubi: UBI device description object
  357. *
  358. * This function creates all debugfs files for UBI device @ubi. Returns zero in
  359. * case of success and a negative error code in case of failure.
  360. */
  361. int ubi_debugfs_init_dev(struct ubi_device *ubi)
  362. {
  363. int err, n;
  364. unsigned long ubi_num = ubi->ubi_num;
  365. const char *fname;
  366. struct dentry *dent;
  367. struct ubi_debug_info *d = ubi->dbg;
  368. if (!IS_ENABLED(DEBUG_FS))
  369. return 0;
  370. n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME,
  371. ubi->ubi_num);
  372. if (n == UBI_DFS_DIR_LEN) {
  373. /* The array size is too small */
  374. fname = UBI_DFS_DIR_NAME;
  375. dent = ERR_PTR(-EINVAL);
  376. goto out;
  377. }
  378. fname = d->dfs_dir_name;
  379. dent = debugfs_create_dir(fname, dfs_rootdir);
  380. if (IS_ERR_OR_NULL(dent))
  381. goto out;
  382. d->dfs_dir = dent;
  383. fname = "chk_gen";
  384. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  385. &dfs_fops);
  386. if (IS_ERR_OR_NULL(dent))
  387. goto out_remove;
  388. d->dfs_chk_gen = dent;
  389. fname = "chk_io";
  390. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  391. &dfs_fops);
  392. if (IS_ERR_OR_NULL(dent))
  393. goto out_remove;
  394. d->dfs_chk_io = dent;
  395. fname = "tst_disable_bgt";
  396. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  397. &dfs_fops);
  398. if (IS_ERR_OR_NULL(dent))
  399. goto out_remove;
  400. d->dfs_disable_bgt = dent;
  401. fname = "tst_emulate_bitflips";
  402. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  403. &dfs_fops);
  404. if (IS_ERR_OR_NULL(dent))
  405. goto out_remove;
  406. d->dfs_emulate_bitflips = dent;
  407. fname = "tst_emulate_io_failures";
  408. dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num,
  409. &dfs_fops);
  410. if (IS_ERR_OR_NULL(dent))
  411. goto out_remove;
  412. d->dfs_emulate_io_failures = dent;
  413. return 0;
  414. out_remove:
  415. debugfs_remove_recursive(d->dfs_dir);
  416. out:
  417. err = dent ? PTR_ERR(dent) : -ENODEV;
  418. ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n",
  419. fname, err);
  420. return err;
  421. }
  422. /**
  423. * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi
  424. * @ubi: UBI device description object
  425. */
  426. void ubi_debugfs_exit_dev(struct ubi_device *ubi)
  427. {
  428. if (IS_ENABLED(DEBUG_FS))
  429. debugfs_remove_recursive(ubi->dbg->dfs_dir);
  430. }