debug.c 13 KB

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