Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 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 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 21 | #include "ubi.h" |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 22 | #include <linux/debugfs.h> |
| 23 | #include <linux/uaccess.h> |
Artem Bityutskiy | b342efd | 2011-03-11 14:33:23 +0200 | [diff] [blame] | 24 | #include <linux/module.h> |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 25 | |
Artem Bityutskiy | ef7088e | 2012-04-24 07:10:33 +0300 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * ubi_dump_flash - dump a region of flash. |
| 29 | * @ubi: UBI device description object |
| 30 | * @pnum: the physical eraseblock number to dump |
| 31 | * @offset: the starting offset within the physical eraseblock to dump |
| 32 | * @len: the length of the region to dump |
| 33 | */ |
| 34 | void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len) |
| 35 | { |
| 36 | int err; |
| 37 | size_t read; |
| 38 | void *buf; |
| 39 | loff_t addr = (loff_t)pnum * ubi->peb_size + offset; |
| 40 | |
| 41 | buf = vmalloc(len); |
| 42 | if (!buf) |
| 43 | return; |
| 44 | err = mtd_read(ubi->mtd, addr, len, &read, buf); |
| 45 | if (err && err != -EUCLEAN) { |
Artem Bityutskiy | 049333c | 2012-08-27 14:43:54 +0300 | [diff] [blame] | 46 | ubi_err("error %d while reading %d bytes from PEB %d:%d, read %zd bytes", |
| 47 | err, len, pnum, offset, read); |
Artem Bityutskiy | ef7088e | 2012-04-24 07:10:33 +0300 | [diff] [blame] | 48 | goto out; |
| 49 | } |
| 50 | |
| 51 | ubi_msg("dumping %d bytes of data from PEB %d, offset %d", |
| 52 | len, pnum, offset); |
| 53 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1); |
| 54 | out: |
| 55 | vfree(buf); |
| 56 | return; |
| 57 | } |
| 58 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 59 | /** |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 60 | * ubi_dump_ec_hdr - dump an erase counter header. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 61 | * @ec_hdr: the erase counter header to dump |
| 62 | */ |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 63 | void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 64 | { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 65 | pr_err("Erase counter header dump:\n"); |
| 66 | pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic)); |
| 67 | pr_err("\tversion %d\n", (int)ec_hdr->version); |
| 68 | pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec)); |
| 69 | pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset)); |
| 70 | pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset)); |
| 71 | pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq)); |
| 72 | pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc)); |
| 73 | pr_err("erase counter header hexdump:\n"); |
Artem Bityutskiy | 6986646 | 2007-08-29 14:56:20 +0300 | [diff] [blame] | 74 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
| 75 | ec_hdr, UBI_EC_HDR_SIZE, 1); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 79 | * ubi_dump_vid_hdr - dump a volume identifier header. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 80 | * @vid_hdr: the volume identifier header to dump |
| 81 | */ |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 82 | void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 83 | { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 84 | pr_err("Volume identifier header dump:\n"); |
| 85 | pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic)); |
| 86 | pr_err("\tversion %d\n", (int)vid_hdr->version); |
| 87 | pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type); |
| 88 | pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag); |
| 89 | pr_err("\tcompat %d\n", (int)vid_hdr->compat); |
| 90 | pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id)); |
| 91 | pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum)); |
| 92 | pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size)); |
| 93 | pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs)); |
| 94 | pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad)); |
| 95 | pr_err("\tsqnum %llu\n", |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 96 | (unsigned long long)be64_to_cpu(vid_hdr->sqnum)); |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 97 | pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc)); |
| 98 | pr_err("Volume identifier header hexdump:\n"); |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 99 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
| 100 | vid_hdr, UBI_VID_HDR_SIZE, 1); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Artem Bityutskiy | 766381f | 2012-05-16 17:53:17 +0300 | [diff] [blame] | 104 | * ubi_dump_vol_info - dump volume information. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 105 | * @vol: UBI volume description object |
| 106 | */ |
Artem Bityutskiy | 766381f | 2012-05-16 17:53:17 +0300 | [diff] [blame] | 107 | void ubi_dump_vol_info(const struct ubi_volume *vol) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 108 | { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 109 | pr_err("Volume information dump:\n"); |
| 110 | pr_err("\tvol_id %d\n", vol->vol_id); |
| 111 | pr_err("\treserved_pebs %d\n", vol->reserved_pebs); |
| 112 | pr_err("\talignment %d\n", vol->alignment); |
| 113 | pr_err("\tdata_pad %d\n", vol->data_pad); |
| 114 | pr_err("\tvol_type %d\n", vol->vol_type); |
| 115 | pr_err("\tname_len %d\n", vol->name_len); |
| 116 | pr_err("\tusable_leb_size %d\n", vol->usable_leb_size); |
| 117 | pr_err("\tused_ebs %d\n", vol->used_ebs); |
| 118 | pr_err("\tused_bytes %lld\n", vol->used_bytes); |
| 119 | pr_err("\tlast_eb_bytes %d\n", vol->last_eb_bytes); |
| 120 | pr_err("\tcorrupted %d\n", vol->corrupted); |
| 121 | pr_err("\tupd_marker %d\n", vol->upd_marker); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 122 | |
| 123 | if (vol->name_len <= UBI_VOL_NAME_MAX && |
| 124 | strnlen(vol->name, vol->name_len + 1) == vol->name_len) { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 125 | pr_err("\tname %s\n", vol->name); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 126 | } else { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 127 | pr_err("\t1st 5 characters of name: %c%c%c%c%c\n", |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 128 | vol->name[0], vol->name[1], vol->name[2], |
| 129 | vol->name[3], vol->name[4]); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
Artem Bityutskiy | 1f021e1d | 2012-05-16 17:56:50 +0300 | [diff] [blame] | 134 | * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 135 | * @r: the object to dump |
| 136 | * @idx: volume table index |
| 137 | */ |
Artem Bityutskiy | 1f021e1d | 2012-05-16 17:56:50 +0300 | [diff] [blame] | 138 | void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 139 | { |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 140 | int name_len = be16_to_cpu(r->name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 141 | |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 142 | pr_err("Volume table record %d dump:\n", idx); |
| 143 | pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs)); |
| 144 | pr_err("\talignment %d\n", be32_to_cpu(r->alignment)); |
| 145 | pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad)); |
| 146 | pr_err("\tvol_type %d\n", (int)r->vol_type); |
| 147 | pr_err("\tupd_marker %d\n", (int)r->upd_marker); |
| 148 | pr_err("\tname_len %d\n", name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 149 | |
| 150 | if (r->name[0] == '\0') { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 151 | pr_err("\tname NULL\n"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
| 155 | if (name_len <= UBI_VOL_NAME_MAX && |
| 156 | strnlen(&r->name[0], name_len + 1) == name_len) { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 157 | pr_err("\tname %s\n", &r->name[0]); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 158 | } else { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 159 | pr_err("\t1st 5 characters of name: %c%c%c%c%c\n", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 160 | r->name[0], r->name[1], r->name[2], r->name[3], |
| 161 | r->name[4]); |
| 162 | } |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 163 | pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc)); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 167 | * ubi_dump_av - dump a &struct ubi_ainf_volume object. |
| 168 | * @av: the object to dump |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 169 | */ |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 170 | void ubi_dump_av(const struct ubi_ainf_volume *av) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 171 | { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 172 | pr_err("Volume attaching information dump:\n"); |
| 173 | pr_err("\tvol_id %d\n", av->vol_id); |
| 174 | pr_err("\thighest_lnum %d\n", av->highest_lnum); |
| 175 | pr_err("\tleb_count %d\n", av->leb_count); |
| 176 | pr_err("\tcompat %d\n", av->compat); |
| 177 | pr_err("\tvol_type %d\n", av->vol_type); |
| 178 | pr_err("\tused_ebs %d\n", av->used_ebs); |
| 179 | pr_err("\tlast_data_size %d\n", av->last_data_size); |
| 180 | pr_err("\tdata_pad %d\n", av->data_pad); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 184 | * ubi_dump_aeb - dump a &struct ubi_ainf_peb object. |
| 185 | * @aeb: the object to dump |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 186 | * @type: object type: 0 - not corrupted, 1 - corrupted |
| 187 | */ |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 188 | void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 189 | { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 190 | pr_err("eraseblock attaching information dump:\n"); |
| 191 | pr_err("\tec %d\n", aeb->ec); |
| 192 | pr_err("\tpnum %d\n", aeb->pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 193 | if (type == 0) { |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 194 | pr_err("\tlnum %d\n", aeb->lnum); |
| 195 | pr_err("\tscrub %d\n", aeb->scrub); |
| 196 | pr_err("\tsqnum %llu\n", aeb->sqnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
Artem Bityutskiy | 718c00b | 2012-05-16 18:03:32 +0300 | [diff] [blame] | 201 | * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 202 | * @req: the object to dump |
| 203 | */ |
Artem Bityutskiy | 718c00b | 2012-05-16 18:03:32 +0300 | [diff] [blame] | 204 | void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 205 | { |
| 206 | char nm[17]; |
| 207 | |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 208 | pr_err("Volume creation request dump:\n"); |
| 209 | pr_err("\tvol_id %d\n", req->vol_id); |
| 210 | pr_err("\talignment %d\n", req->alignment); |
| 211 | pr_err("\tbytes %lld\n", (long long)req->bytes); |
| 212 | pr_err("\tvol_type %d\n", req->vol_type); |
| 213 | pr_err("\tname_len %d\n", req->name_len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 214 | |
| 215 | memcpy(nm, req->name, 16); |
| 216 | nm[16] = 0; |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 217 | pr_err("\t1st 16 characters of name: %s\n", nm); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 218 | } |
| 219 | |
Artem Bityutskiy | 867996b | 2009-07-24 15:31:33 +0300 | [diff] [blame] | 220 | /** |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 221 | * ubi_debugging_init_dev - initialize debugging for an UBI device. |
| 222 | * @ubi: UBI device description object |
| 223 | * |
| 224 | * This function initializes debugging-related data for UBI device @ubi. |
| 225 | * Returns zero in case of success and a negative error code in case of |
| 226 | * failure. |
| 227 | */ |
| 228 | int ubi_debugging_init_dev(struct ubi_device *ubi) |
| 229 | { |
| 230 | ubi->dbg = kzalloc(sizeof(struct ubi_debug_info), GFP_KERNEL); |
| 231 | if (!ubi->dbg) |
| 232 | return -ENOMEM; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * ubi_debugging_exit_dev - free debugging data for an UBI device. |
| 239 | * @ubi: UBI device description object |
| 240 | */ |
| 241 | void ubi_debugging_exit_dev(struct ubi_device *ubi) |
| 242 | { |
| 243 | kfree(ubi->dbg); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Root directory for UBI stuff in debugfs. Contains sub-directories which |
| 248 | * contain the stuff specific to particular UBI devices. |
| 249 | */ |
| 250 | static struct dentry *dfs_rootdir; |
| 251 | |
| 252 | /** |
| 253 | * ubi_debugfs_init - create UBI debugfs directory. |
| 254 | * |
| 255 | * Create UBI debugfs directory. Returns zero in case of success and a negative |
| 256 | * error code in case of failure. |
| 257 | */ |
| 258 | int ubi_debugfs_init(void) |
| 259 | { |
Brian Norris | 903e0e4 | 2012-06-18 16:31:23 -0700 | [diff] [blame] | 260 | if (!IS_ENABLED(CONFIG_DEBUG_FS)) |
Artem Bityutskiy | e9b4cf2 | 2012-06-06 15:22:41 +0300 | [diff] [blame] | 261 | return 0; |
| 262 | |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 263 | dfs_rootdir = debugfs_create_dir("ubi", NULL); |
| 264 | if (IS_ERR_OR_NULL(dfs_rootdir)) { |
| 265 | int err = dfs_rootdir ? -ENODEV : PTR_ERR(dfs_rootdir); |
| 266 | |
| 267 | ubi_err("cannot create \"ubi\" debugfs directory, error %d\n", |
| 268 | err); |
| 269 | return err; |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * ubi_debugfs_exit - remove UBI debugfs directory. |
| 277 | */ |
| 278 | void ubi_debugfs_exit(void) |
| 279 | { |
Brian Norris | 903e0e4 | 2012-06-18 16:31:23 -0700 | [diff] [blame] | 280 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
Artem Bityutskiy | e9b4cf2 | 2012-06-06 15:22:41 +0300 | [diff] [blame] | 281 | debugfs_remove(dfs_rootdir); |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* Read an UBI debugfs file */ |
| 285 | static ssize_t dfs_file_read(struct file *file, char __user *user_buf, |
| 286 | size_t count, loff_t *ppos) |
| 287 | { |
| 288 | unsigned long ubi_num = (unsigned long)file->private_data; |
| 289 | struct dentry *dent = file->f_path.dentry; |
| 290 | struct ubi_device *ubi; |
| 291 | struct ubi_debug_info *d; |
| 292 | char buf[3]; |
| 293 | int val; |
| 294 | |
| 295 | ubi = ubi_get_device(ubi_num); |
| 296 | if (!ubi) |
| 297 | return -ENODEV; |
| 298 | d = ubi->dbg; |
| 299 | |
| 300 | if (dent == d->dfs_chk_gen) |
| 301 | val = d->chk_gen; |
| 302 | else if (dent == d->dfs_chk_io) |
| 303 | val = d->chk_io; |
Artem Bityutskiy | cd6d856 | 2011-05-18 16:21:43 +0300 | [diff] [blame] | 304 | else if (dent == d->dfs_disable_bgt) |
| 305 | val = d->disable_bgt; |
| 306 | else if (dent == d->dfs_emulate_bitflips) |
| 307 | val = d->emulate_bitflips; |
| 308 | else if (dent == d->dfs_emulate_io_failures) |
| 309 | val = d->emulate_io_failures; |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 310 | else { |
| 311 | count = -EINVAL; |
| 312 | goto out; |
| 313 | } |
| 314 | |
| 315 | if (val) |
| 316 | buf[0] = '1'; |
| 317 | else |
| 318 | buf[0] = '0'; |
| 319 | buf[1] = '\n'; |
| 320 | buf[2] = 0x00; |
| 321 | |
| 322 | count = simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 323 | |
| 324 | out: |
| 325 | ubi_put_device(ubi); |
| 326 | return count; |
| 327 | } |
| 328 | |
| 329 | /* Write an UBI debugfs file */ |
| 330 | static ssize_t dfs_file_write(struct file *file, const char __user *user_buf, |
| 331 | size_t count, loff_t *ppos) |
| 332 | { |
| 333 | unsigned long ubi_num = (unsigned long)file->private_data; |
| 334 | struct dentry *dent = file->f_path.dentry; |
| 335 | struct ubi_device *ubi; |
| 336 | struct ubi_debug_info *d; |
| 337 | size_t buf_size; |
| 338 | char buf[8]; |
| 339 | int val; |
| 340 | |
| 341 | ubi = ubi_get_device(ubi_num); |
| 342 | if (!ubi) |
| 343 | return -ENODEV; |
| 344 | d = ubi->dbg; |
| 345 | |
| 346 | buf_size = min_t(size_t, count, (sizeof(buf) - 1)); |
| 347 | if (copy_from_user(buf, user_buf, buf_size)) { |
| 348 | count = -EFAULT; |
| 349 | goto out; |
| 350 | } |
| 351 | |
| 352 | if (buf[0] == '1') |
| 353 | val = 1; |
| 354 | else if (buf[0] == '0') |
| 355 | val = 0; |
| 356 | else { |
| 357 | count = -EINVAL; |
| 358 | goto out; |
| 359 | } |
| 360 | |
| 361 | if (dent == d->dfs_chk_gen) |
| 362 | d->chk_gen = val; |
| 363 | else if (dent == d->dfs_chk_io) |
| 364 | d->chk_io = val; |
Artem Bityutskiy | cd6d856 | 2011-05-18 16:21:43 +0300 | [diff] [blame] | 365 | else if (dent == d->dfs_disable_bgt) |
| 366 | d->disable_bgt = val; |
| 367 | else if (dent == d->dfs_emulate_bitflips) |
| 368 | d->emulate_bitflips = val; |
| 369 | else if (dent == d->dfs_emulate_io_failures) |
| 370 | d->emulate_io_failures = val; |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 371 | else |
| 372 | count = -EINVAL; |
| 373 | |
| 374 | out: |
| 375 | ubi_put_device(ubi); |
| 376 | return count; |
| 377 | } |
| 378 | |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 379 | /* File operations for all UBI debugfs files */ |
| 380 | static const struct file_operations dfs_fops = { |
| 381 | .read = dfs_file_read, |
| 382 | .write = dfs_file_write, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 383 | .open = simple_open, |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 384 | .llseek = no_llseek, |
| 385 | .owner = THIS_MODULE, |
| 386 | }; |
| 387 | |
| 388 | /** |
| 389 | * ubi_debugfs_init_dev - initialize debugfs for an UBI device. |
| 390 | * @ubi: UBI device description object |
| 391 | * |
| 392 | * This function creates all debugfs files for UBI device @ubi. Returns zero in |
| 393 | * case of success and a negative error code in case of failure. |
| 394 | */ |
| 395 | int ubi_debugfs_init_dev(struct ubi_device *ubi) |
| 396 | { |
| 397 | int err, n; |
| 398 | unsigned long ubi_num = ubi->ubi_num; |
| 399 | const char *fname; |
| 400 | struct dentry *dent; |
| 401 | struct ubi_debug_info *d = ubi->dbg; |
| 402 | |
Brian Norris | 903e0e4 | 2012-06-18 16:31:23 -0700 | [diff] [blame] | 403 | if (!IS_ENABLED(CONFIG_DEBUG_FS)) |
Artem Bityutskiy | e9b4cf2 | 2012-06-06 15:22:41 +0300 | [diff] [blame] | 404 | return 0; |
| 405 | |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 406 | n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME, |
| 407 | ubi->ubi_num); |
| 408 | if (n == UBI_DFS_DIR_LEN) { |
| 409 | /* The array size is too small */ |
| 410 | fname = UBI_DFS_DIR_NAME; |
| 411 | dent = ERR_PTR(-EINVAL); |
| 412 | goto out; |
| 413 | } |
| 414 | |
| 415 | fname = d->dfs_dir_name; |
| 416 | dent = debugfs_create_dir(fname, dfs_rootdir); |
| 417 | if (IS_ERR_OR_NULL(dent)) |
| 418 | goto out; |
| 419 | d->dfs_dir = dent; |
| 420 | |
| 421 | fname = "chk_gen"; |
| 422 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, |
| 423 | &dfs_fops); |
| 424 | if (IS_ERR_OR_NULL(dent)) |
| 425 | goto out_remove; |
| 426 | d->dfs_chk_gen = dent; |
| 427 | |
| 428 | fname = "chk_io"; |
| 429 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, |
| 430 | &dfs_fops); |
| 431 | if (IS_ERR_OR_NULL(dent)) |
| 432 | goto out_remove; |
| 433 | d->dfs_chk_io = dent; |
| 434 | |
Artem Bityutskiy | cd6d856 | 2011-05-18 16:21:43 +0300 | [diff] [blame] | 435 | fname = "tst_disable_bgt"; |
| 436 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, |
| 437 | &dfs_fops); |
| 438 | if (IS_ERR_OR_NULL(dent)) |
| 439 | goto out_remove; |
| 440 | d->dfs_disable_bgt = dent; |
| 441 | |
| 442 | fname = "tst_emulate_bitflips"; |
| 443 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, |
| 444 | &dfs_fops); |
| 445 | if (IS_ERR_OR_NULL(dent)) |
| 446 | goto out_remove; |
| 447 | d->dfs_emulate_bitflips = dent; |
| 448 | |
| 449 | fname = "tst_emulate_io_failures"; |
| 450 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, |
| 451 | &dfs_fops); |
| 452 | if (IS_ERR_OR_NULL(dent)) |
| 453 | goto out_remove; |
| 454 | d->dfs_emulate_io_failures = dent; |
| 455 | |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 456 | return 0; |
| 457 | |
| 458 | out_remove: |
| 459 | debugfs_remove_recursive(d->dfs_dir); |
| 460 | out: |
| 461 | err = dent ? PTR_ERR(dent) : -ENODEV; |
| 462 | ubi_err("cannot create \"%s\" debugfs file or directory, error %d\n", |
| 463 | fname, err); |
| 464 | return err; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi |
| 469 | * @ubi: UBI device description object |
| 470 | */ |
| 471 | void ubi_debugfs_exit_dev(struct ubi_device *ubi) |
| 472 | { |
Brian Norris | 903e0e4 | 2012-06-18 16:31:23 -0700 | [diff] [blame] | 473 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
Artem Bityutskiy | e9b4cf2 | 2012-06-06 15:22:41 +0300 | [diff] [blame] | 474 | debugfs_remove_recursive(ubi->dbg->dfs_dir); |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 475 | } |