Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
801c135c AB |
2 | /* |
3 | * Copyright (c) International Business Machines Corp., 2006 | |
4 | * | |
801c135c AB |
5 | * Author: Artem Bityutskiy (Битюцкий Артём) |
6 | */ | |
7 | ||
801c135c | 8 | #include "ubi.h" |
2a734bb8 AB |
9 | #include <linux/debugfs.h> |
10 | #include <linux/uaccess.h> | |
b342efd4 | 11 | #include <linux/module.h> |
7bccd12d | 12 | #include <linux/seq_file.h> |
801c135c | 13 | |
ef7088e7 AB |
14 | |
15 | /** | |
16 | * ubi_dump_flash - dump a region of flash. | |
17 | * @ubi: UBI device description object | |
18 | * @pnum: the physical eraseblock number to dump | |
19 | * @offset: the starting offset within the physical eraseblock to dump | |
20 | * @len: the length of the region to dump | |
21 | */ | |
22 | void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len) | |
23 | { | |
24 | int err; | |
25 | size_t read; | |
26 | void *buf; | |
27 | loff_t addr = (loff_t)pnum * ubi->peb_size + offset; | |
28 | ||
29 | buf = vmalloc(len); | |
30 | if (!buf) | |
31 | return; | |
32 | err = mtd_read(ubi->mtd, addr, len, &read, buf); | |
33 | if (err && err != -EUCLEAN) { | |
32608703 | 34 | ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes", |
049333ce | 35 | err, len, pnum, offset, read); |
ef7088e7 AB |
36 | goto out; |
37 | } | |
38 | ||
32608703 | 39 | ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d", |
ef7088e7 AB |
40 | len, pnum, offset); |
41 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1); | |
42 | out: | |
43 | vfree(buf); | |
44 | return; | |
45 | } | |
46 | ||
801c135c | 47 | /** |
a904e3f1 | 48 | * ubi_dump_ec_hdr - dump an erase counter header. |
801c135c AB |
49 | * @ec_hdr: the erase counter header to dump |
50 | */ | |
a904e3f1 | 51 | void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) |
801c135c | 52 | { |
e28453bb AB |
53 | pr_err("Erase counter header dump:\n"); |
54 | pr_err("\tmagic %#08x\n", be32_to_cpu(ec_hdr->magic)); | |
55 | pr_err("\tversion %d\n", (int)ec_hdr->version); | |
56 | pr_err("\tec %llu\n", (long long)be64_to_cpu(ec_hdr->ec)); | |
57 | pr_err("\tvid_hdr_offset %d\n", be32_to_cpu(ec_hdr->vid_hdr_offset)); | |
58 | pr_err("\tdata_offset %d\n", be32_to_cpu(ec_hdr->data_offset)); | |
59 | pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq)); | |
60 | pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc)); | |
61 | pr_err("erase counter header hexdump:\n"); | |
6986646b AB |
62 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
63 | ec_hdr, UBI_EC_HDR_SIZE, 1); | |
801c135c AB |
64 | } |
65 | ||
66 | /** | |
a904e3f1 | 67 | * ubi_dump_vid_hdr - dump a volume identifier header. |
801c135c AB |
68 | * @vid_hdr: the volume identifier header to dump |
69 | */ | |
a904e3f1 | 70 | void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) |
801c135c | 71 | { |
e28453bb AB |
72 | pr_err("Volume identifier header dump:\n"); |
73 | pr_err("\tmagic %08x\n", be32_to_cpu(vid_hdr->magic)); | |
74 | pr_err("\tversion %d\n", (int)vid_hdr->version); | |
75 | pr_err("\tvol_type %d\n", (int)vid_hdr->vol_type); | |
76 | pr_err("\tcopy_flag %d\n", (int)vid_hdr->copy_flag); | |
77 | pr_err("\tcompat %d\n", (int)vid_hdr->compat); | |
78 | pr_err("\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id)); | |
79 | pr_err("\tlnum %d\n", be32_to_cpu(vid_hdr->lnum)); | |
80 | pr_err("\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size)); | |
81 | pr_err("\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs)); | |
82 | pr_err("\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad)); | |
83 | pr_err("\tsqnum %llu\n", | |
3261ebd7 | 84 | (unsigned long long)be64_to_cpu(vid_hdr->sqnum)); |
e28453bb AB |
85 | pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc)); |
86 | pr_err("Volume identifier header hexdump:\n"); | |
c8566350 AB |
87 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
88 | vid_hdr, UBI_VID_HDR_SIZE, 1); | |
801c135c AB |
89 | } |
90 | ||
91 | /** | |
766381f0 | 92 | * ubi_dump_vol_info - dump volume information. |
801c135c AB |
93 | * @vol: UBI volume description object |
94 | */ | |
766381f0 | 95 | void ubi_dump_vol_info(const struct ubi_volume *vol) |
801c135c | 96 | { |
e28453bb AB |
97 | pr_err("Volume information dump:\n"); |
98 | pr_err("\tvol_id %d\n", vol->vol_id); | |
99 | pr_err("\treserved_pebs %d\n", vol->reserved_pebs); | |
100 | pr_err("\talignment %d\n", vol->alignment); | |
101 | pr_err("\tdata_pad %d\n", vol->data_pad); | |
102 | pr_err("\tvol_type %d\n", vol->vol_type); | |
103 | pr_err("\tname_len %d\n", vol->name_len); | |
104 | pr_err("\tusable_leb_size %d\n", vol->usable_leb_size); | |
105 | pr_err("\tused_ebs %d\n", vol->used_ebs); | |
106 | pr_err("\tused_bytes %lld\n", vol->used_bytes); | |
107 | pr_err("\tlast_eb_bytes %d\n", vol->last_eb_bytes); | |
108 | pr_err("\tcorrupted %d\n", vol->corrupted); | |
109 | pr_err("\tupd_marker %d\n", vol->upd_marker); | |
801c135c AB |
110 | |
111 | if (vol->name_len <= UBI_VOL_NAME_MAX && | |
112 | strnlen(vol->name, vol->name_len + 1) == vol->name_len) { | |
e28453bb | 113 | pr_err("\tname %s\n", vol->name); |
801c135c | 114 | } else { |
e28453bb | 115 | pr_err("\t1st 5 characters of name: %c%c%c%c%c\n", |
c8566350 AB |
116 | vol->name[0], vol->name[1], vol->name[2], |
117 | vol->name[3], vol->name[4]); | |
801c135c AB |
118 | } |
119 | } | |
120 | ||
121 | /** | |
1f021e1d | 122 | * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object. |
801c135c AB |
123 | * @r: the object to dump |
124 | * @idx: volume table index | |
125 | */ | |
1f021e1d | 126 | void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) |
801c135c | 127 | { |
3261ebd7 | 128 | int name_len = be16_to_cpu(r->name_len); |
801c135c | 129 | |
e28453bb AB |
130 | pr_err("Volume table record %d dump:\n", idx); |
131 | pr_err("\treserved_pebs %d\n", be32_to_cpu(r->reserved_pebs)); | |
132 | pr_err("\talignment %d\n", be32_to_cpu(r->alignment)); | |
133 | pr_err("\tdata_pad %d\n", be32_to_cpu(r->data_pad)); | |
134 | pr_err("\tvol_type %d\n", (int)r->vol_type); | |
135 | pr_err("\tupd_marker %d\n", (int)r->upd_marker); | |
136 | pr_err("\tname_len %d\n", name_len); | |
801c135c AB |
137 | |
138 | if (r->name[0] == '\0') { | |
e28453bb | 139 | pr_err("\tname NULL\n"); |
801c135c AB |
140 | return; |
141 | } | |
142 | ||
143 | if (name_len <= UBI_VOL_NAME_MAX && | |
144 | strnlen(&r->name[0], name_len + 1) == name_len) { | |
e28453bb | 145 | pr_err("\tname %s\n", &r->name[0]); |
801c135c | 146 | } else { |
e28453bb | 147 | pr_err("\t1st 5 characters of name: %c%c%c%c%c\n", |
801c135c AB |
148 | r->name[0], r->name[1], r->name[2], r->name[3], |
149 | r->name[4]); | |
150 | } | |
e28453bb | 151 | pr_err("\tcrc %#08x\n", be32_to_cpu(r->crc)); |
801c135c AB |
152 | } |
153 | ||
154 | /** | |
517af48c AB |
155 | * ubi_dump_av - dump a &struct ubi_ainf_volume object. |
156 | * @av: the object to dump | |
801c135c | 157 | */ |
517af48c | 158 | void ubi_dump_av(const struct ubi_ainf_volume *av) |
801c135c | 159 | { |
e28453bb AB |
160 | pr_err("Volume attaching information dump:\n"); |
161 | pr_err("\tvol_id %d\n", av->vol_id); | |
162 | pr_err("\thighest_lnum %d\n", av->highest_lnum); | |
163 | pr_err("\tleb_count %d\n", av->leb_count); | |
164 | pr_err("\tcompat %d\n", av->compat); | |
165 | pr_err("\tvol_type %d\n", av->vol_type); | |
166 | pr_err("\tused_ebs %d\n", av->used_ebs); | |
167 | pr_err("\tlast_data_size %d\n", av->last_data_size); | |
168 | pr_err("\tdata_pad %d\n", av->data_pad); | |
801c135c AB |
169 | } |
170 | ||
171 | /** | |
2c5ec5ce AB |
172 | * ubi_dump_aeb - dump a &struct ubi_ainf_peb object. |
173 | * @aeb: the object to dump | |
801c135c AB |
174 | * @type: object type: 0 - not corrupted, 1 - corrupted |
175 | */ | |
2c5ec5ce | 176 | void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type) |
801c135c | 177 | { |
e28453bb AB |
178 | pr_err("eraseblock attaching information dump:\n"); |
179 | pr_err("\tec %d\n", aeb->ec); | |
180 | pr_err("\tpnum %d\n", aeb->pnum); | |
801c135c | 181 | if (type == 0) { |
e28453bb AB |
182 | pr_err("\tlnum %d\n", aeb->lnum); |
183 | pr_err("\tscrub %d\n", aeb->scrub); | |
184 | pr_err("\tsqnum %llu\n", aeb->sqnum); | |
801c135c AB |
185 | } |
186 | } | |
187 | ||
188 | /** | |
718c00bb | 189 | * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object. |
801c135c AB |
190 | * @req: the object to dump |
191 | */ | |
718c00bb | 192 | void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req) |
801c135c AB |
193 | { |
194 | char nm[17]; | |
195 | ||
e28453bb AB |
196 | pr_err("Volume creation request dump:\n"); |
197 | pr_err("\tvol_id %d\n", req->vol_id); | |
198 | pr_err("\talignment %d\n", req->alignment); | |
199 | pr_err("\tbytes %lld\n", (long long)req->bytes); | |
200 | pr_err("\tvol_type %d\n", req->vol_type); | |
201 | pr_err("\tname_len %d\n", req->name_len); | |
801c135c AB |
202 | |
203 | memcpy(nm, req->name, 16); | |
204 | nm[16] = 0; | |
e28453bb | 205 | pr_err("\t1st 16 characters of name: %s\n", nm); |
801c135c AB |
206 | } |
207 | ||
2a734bb8 AB |
208 | /* |
209 | * Root directory for UBI stuff in debugfs. Contains sub-directories which | |
210 | * contain the stuff specific to particular UBI devices. | |
211 | */ | |
212 | static struct dentry *dfs_rootdir; | |
213 | ||
214 | /** | |
215 | * ubi_debugfs_init - create UBI debugfs directory. | |
216 | * | |
217 | * Create UBI debugfs directory. Returns zero in case of success and a negative | |
218 | * error code in case of failure. | |
219 | */ | |
220 | int ubi_debugfs_init(void) | |
221 | { | |
903e0e4e | 222 | if (!IS_ENABLED(CONFIG_DEBUG_FS)) |
e9b4cf20 AB |
223 | return 0; |
224 | ||
2a734bb8 AB |
225 | dfs_rootdir = debugfs_create_dir("ubi", NULL); |
226 | if (IS_ERR_OR_NULL(dfs_rootdir)) { | |
97cb69dd | 227 | int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV; |
2a734bb8 | 228 | |
32608703 TB |
229 | pr_err("UBI error: cannot create \"ubi\" debugfs directory, error %d\n", |
230 | err); | |
2a734bb8 AB |
231 | return err; |
232 | } | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | /** | |
238 | * ubi_debugfs_exit - remove UBI debugfs directory. | |
239 | */ | |
240 | void ubi_debugfs_exit(void) | |
241 | { | |
903e0e4e | 242 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
e9b4cf20 | 243 | debugfs_remove(dfs_rootdir); |
2a734bb8 AB |
244 | } |
245 | ||
246 | /* Read an UBI debugfs file */ | |
247 | static ssize_t dfs_file_read(struct file *file, char __user *user_buf, | |
248 | size_t count, loff_t *ppos) | |
249 | { | |
250 | unsigned long ubi_num = (unsigned long)file->private_data; | |
251 | struct dentry *dent = file->f_path.dentry; | |
252 | struct ubi_device *ubi; | |
253 | struct ubi_debug_info *d; | |
50269067 | 254 | char buf[8]; |
2a734bb8 AB |
255 | int val; |
256 | ||
257 | ubi = ubi_get_device(ubi_num); | |
258 | if (!ubi) | |
259 | return -ENODEV; | |
eab73772 | 260 | d = &ubi->dbg; |
2a734bb8 AB |
261 | |
262 | if (dent == d->dfs_chk_gen) | |
263 | val = d->chk_gen; | |
264 | else if (dent == d->dfs_chk_io) | |
265 | val = d->chk_io; | |
5fa7fa5d RW |
266 | else if (dent == d->dfs_chk_fastmap) |
267 | val = d->chk_fastmap; | |
cd6d8567 AB |
268 | else if (dent == d->dfs_disable_bgt) |
269 | val = d->disable_bgt; | |
270 | else if (dent == d->dfs_emulate_bitflips) | |
271 | val = d->emulate_bitflips; | |
272 | else if (dent == d->dfs_emulate_io_failures) | |
273 | val = d->emulate_io_failures; | |
50269067 | 274 | else if (dent == d->dfs_emulate_power_cut) { |
275 | snprintf(buf, sizeof(buf), "%u\n", d->emulate_power_cut); | |
276 | count = simple_read_from_buffer(user_buf, count, ppos, | |
277 | buf, strlen(buf)); | |
278 | goto out; | |
279 | } else if (dent == d->dfs_power_cut_min) { | |
280 | snprintf(buf, sizeof(buf), "%u\n", d->power_cut_min); | |
281 | count = simple_read_from_buffer(user_buf, count, ppos, | |
282 | buf, strlen(buf)); | |
283 | goto out; | |
284 | } else if (dent == d->dfs_power_cut_max) { | |
285 | snprintf(buf, sizeof(buf), "%u\n", d->power_cut_max); | |
286 | count = simple_read_from_buffer(user_buf, count, ppos, | |
287 | buf, strlen(buf)); | |
288 | goto out; | |
289 | } | |
2a734bb8 AB |
290 | else { |
291 | count = -EINVAL; | |
292 | goto out; | |
293 | } | |
294 | ||
295 | if (val) | |
296 | buf[0] = '1'; | |
297 | else | |
298 | buf[0] = '0'; | |
299 | buf[1] = '\n'; | |
300 | buf[2] = 0x00; | |
301 | ||
302 | count = simple_read_from_buffer(user_buf, count, ppos, buf, 2); | |
303 | ||
304 | out: | |
305 | ubi_put_device(ubi); | |
306 | return count; | |
307 | } | |
308 | ||
309 | /* Write an UBI debugfs file */ | |
310 | static ssize_t dfs_file_write(struct file *file, const char __user *user_buf, | |
311 | size_t count, loff_t *ppos) | |
312 | { | |
313 | unsigned long ubi_num = (unsigned long)file->private_data; | |
314 | struct dentry *dent = file->f_path.dentry; | |
315 | struct ubi_device *ubi; | |
316 | struct ubi_debug_info *d; | |
317 | size_t buf_size; | |
50269067 | 318 | char buf[8] = {0}; |
2a734bb8 AB |
319 | int val; |
320 | ||
321 | ubi = ubi_get_device(ubi_num); | |
322 | if (!ubi) | |
323 | return -ENODEV; | |
eab73772 | 324 | d = &ubi->dbg; |
2a734bb8 AB |
325 | |
326 | buf_size = min_t(size_t, count, (sizeof(buf) - 1)); | |
327 | if (copy_from_user(buf, user_buf, buf_size)) { | |
328 | count = -EFAULT; | |
329 | goto out; | |
330 | } | |
331 | ||
50269067 | 332 | if (dent == d->dfs_power_cut_min) { |
333 | if (kstrtouint(buf, 0, &d->power_cut_min) != 0) | |
334 | count = -EINVAL; | |
335 | goto out; | |
336 | } else if (dent == d->dfs_power_cut_max) { | |
337 | if (kstrtouint(buf, 0, &d->power_cut_max) != 0) | |
338 | count = -EINVAL; | |
339 | goto out; | |
340 | } else if (dent == d->dfs_emulate_power_cut) { | |
341 | if (kstrtoint(buf, 0, &val) != 0) | |
342 | count = -EINVAL; | |
24663e72 DC |
343 | else |
344 | d->emulate_power_cut = val; | |
50269067 | 345 | goto out; |
346 | } | |
347 | ||
2a734bb8 AB |
348 | if (buf[0] == '1') |
349 | val = 1; | |
350 | else if (buf[0] == '0') | |
351 | val = 0; | |
352 | else { | |
353 | count = -EINVAL; | |
354 | goto out; | |
355 | } | |
356 | ||
357 | if (dent == d->dfs_chk_gen) | |
358 | d->chk_gen = val; | |
359 | else if (dent == d->dfs_chk_io) | |
360 | d->chk_io = val; | |
5fa7fa5d RW |
361 | else if (dent == d->dfs_chk_fastmap) |
362 | d->chk_fastmap = val; | |
cd6d8567 AB |
363 | else if (dent == d->dfs_disable_bgt) |
364 | d->disable_bgt = val; | |
365 | else if (dent == d->dfs_emulate_bitflips) | |
366 | d->emulate_bitflips = val; | |
367 | else if (dent == d->dfs_emulate_io_failures) | |
368 | d->emulate_io_failures = val; | |
2a734bb8 AB |
369 | else |
370 | count = -EINVAL; | |
371 | ||
372 | out: | |
373 | ubi_put_device(ubi); | |
374 | return count; | |
375 | } | |
376 | ||
7bccd12d BS |
377 | /* File operations for all UBI debugfs files except |
378 | * detailed_erase_block_info | |
379 | */ | |
2a734bb8 AB |
380 | static const struct file_operations dfs_fops = { |
381 | .read = dfs_file_read, | |
382 | .write = dfs_file_write, | |
234e3405 | 383 | .open = simple_open, |
2a734bb8 AB |
384 | .llseek = no_llseek, |
385 | .owner = THIS_MODULE, | |
386 | }; | |
387 | ||
7bccd12d BS |
388 | /* As long as the position is less then that total number of erase blocks, |
389 | * we still have more to print. | |
390 | */ | |
391 | static void *eraseblk_count_seq_start(struct seq_file *s, loff_t *pos) | |
392 | { | |
393 | struct ubi_device *ubi = s->private; | |
394 | ||
395 | if (*pos == 0) | |
396 | return SEQ_START_TOKEN; | |
397 | ||
398 | if (*pos < ubi->peb_count) | |
399 | return pos; | |
400 | ||
401 | return NULL; | |
402 | } | |
403 | ||
404 | /* Since we are using the position as the iterator, we just need to check if we | |
405 | * are done and increment the position. | |
406 | */ | |
407 | static void *eraseblk_count_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
408 | { | |
409 | struct ubi_device *ubi = s->private; | |
410 | ||
411 | if (v == SEQ_START_TOKEN) | |
412 | return pos; | |
413 | (*pos)++; | |
414 | ||
415 | if (*pos < ubi->peb_count) | |
416 | return pos; | |
417 | ||
418 | return NULL; | |
419 | } | |
420 | ||
421 | static void eraseblk_count_seq_stop(struct seq_file *s, void *v) | |
422 | { | |
423 | } | |
424 | ||
425 | static int eraseblk_count_seq_show(struct seq_file *s, void *iter) | |
426 | { | |
427 | struct ubi_device *ubi = s->private; | |
428 | struct ubi_wl_entry *wl; | |
429 | int *block_number = iter; | |
430 | int erase_count = -1; | |
431 | int err; | |
432 | ||
433 | /* If this is the start, print a header */ | |
434 | if (iter == SEQ_START_TOKEN) { | |
435 | seq_puts(s, | |
436 | "physical_block_number\terase_count\tblock_status\tread_status\n"); | |
437 | return 0; | |
438 | } | |
439 | ||
440 | err = ubi_io_is_bad(ubi, *block_number); | |
441 | if (err) | |
442 | return err; | |
443 | ||
444 | spin_lock(&ubi->wl_lock); | |
445 | ||
446 | wl = ubi->lookuptbl[*block_number]; | |
447 | if (wl) | |
448 | erase_count = wl->ec; | |
449 | ||
450 | spin_unlock(&ubi->wl_lock); | |
451 | ||
452 | if (erase_count < 0) | |
453 | return 0; | |
454 | ||
455 | seq_printf(s, "%-22d\t%-11d\n", *block_number, erase_count); | |
456 | ||
457 | return 0; | |
458 | } | |
459 | ||
460 | static const struct seq_operations eraseblk_count_seq_ops = { | |
461 | .start = eraseblk_count_seq_start, | |
462 | .next = eraseblk_count_seq_next, | |
463 | .stop = eraseblk_count_seq_stop, | |
464 | .show = eraseblk_count_seq_show | |
465 | }; | |
466 | ||
467 | static int eraseblk_count_open(struct inode *inode, struct file *f) | |
468 | { | |
469 | struct seq_file *s; | |
470 | int err; | |
471 | ||
472 | err = seq_open(f, &eraseblk_count_seq_ops); | |
473 | if (err) | |
474 | return err; | |
475 | ||
476 | s = f->private_data; | |
477 | s->private = ubi_get_device((unsigned long)inode->i_private); | |
478 | ||
479 | if (!s->private) | |
480 | return -ENODEV; | |
481 | else | |
482 | return 0; | |
483 | } | |
484 | ||
485 | static int eraseblk_count_release(struct inode *inode, struct file *f) | |
486 | { | |
487 | struct seq_file *s = f->private_data; | |
488 | struct ubi_device *ubi = s->private; | |
489 | ||
490 | ubi_put_device(ubi); | |
491 | ||
492 | return seq_release(inode, f); | |
493 | } | |
494 | ||
495 | static const struct file_operations eraseblk_count_fops = { | |
496 | .owner = THIS_MODULE, | |
497 | .open = eraseblk_count_open, | |
498 | .read = seq_read, | |
499 | .llseek = seq_lseek, | |
500 | .release = eraseblk_count_release, | |
501 | }; | |
502 | ||
2a734bb8 AB |
503 | /** |
504 | * ubi_debugfs_init_dev - initialize debugfs for an UBI device. | |
505 | * @ubi: UBI device description object | |
506 | * | |
507 | * This function creates all debugfs files for UBI device @ubi. Returns zero in | |
508 | * case of success and a negative error code in case of failure. | |
509 | */ | |
510 | int ubi_debugfs_init_dev(struct ubi_device *ubi) | |
511 | { | |
512 | int err, n; | |
513 | unsigned long ubi_num = ubi->ubi_num; | |
514 | const char *fname; | |
515 | struct dentry *dent; | |
eab73772 | 516 | struct ubi_debug_info *d = &ubi->dbg; |
2a734bb8 | 517 | |
903e0e4e | 518 | if (!IS_ENABLED(CONFIG_DEBUG_FS)) |
e9b4cf20 AB |
519 | return 0; |
520 | ||
2a734bb8 AB |
521 | n = snprintf(d->dfs_dir_name, UBI_DFS_DIR_LEN + 1, UBI_DFS_DIR_NAME, |
522 | ubi->ubi_num); | |
523 | if (n == UBI_DFS_DIR_LEN) { | |
524 | /* The array size is too small */ | |
525 | fname = UBI_DFS_DIR_NAME; | |
526 | dent = ERR_PTR(-EINVAL); | |
527 | goto out; | |
528 | } | |
529 | ||
530 | fname = d->dfs_dir_name; | |
531 | dent = debugfs_create_dir(fname, dfs_rootdir); | |
532 | if (IS_ERR_OR_NULL(dent)) | |
533 | goto out; | |
534 | d->dfs_dir = dent; | |
535 | ||
536 | fname = "chk_gen"; | |
537 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
538 | &dfs_fops); | |
539 | if (IS_ERR_OR_NULL(dent)) | |
540 | goto out_remove; | |
541 | d->dfs_chk_gen = dent; | |
542 | ||
543 | fname = "chk_io"; | |
544 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
545 | &dfs_fops); | |
546 | if (IS_ERR_OR_NULL(dent)) | |
547 | goto out_remove; | |
548 | d->dfs_chk_io = dent; | |
549 | ||
5fa7fa5d RW |
550 | fname = "chk_fastmap"; |
551 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
552 | &dfs_fops); | |
553 | if (IS_ERR_OR_NULL(dent)) | |
554 | goto out_remove; | |
555 | d->dfs_chk_fastmap = dent; | |
556 | ||
cd6d8567 AB |
557 | fname = "tst_disable_bgt"; |
558 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
559 | &dfs_fops); | |
560 | if (IS_ERR_OR_NULL(dent)) | |
561 | goto out_remove; | |
562 | d->dfs_disable_bgt = dent; | |
563 | ||
564 | fname = "tst_emulate_bitflips"; | |
565 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
566 | &dfs_fops); | |
567 | if (IS_ERR_OR_NULL(dent)) | |
568 | goto out_remove; | |
569 | d->dfs_emulate_bitflips = dent; | |
570 | ||
571 | fname = "tst_emulate_io_failures"; | |
572 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
573 | &dfs_fops); | |
574 | if (IS_ERR_OR_NULL(dent)) | |
575 | goto out_remove; | |
576 | d->dfs_emulate_io_failures = dent; | |
577 | ||
50269067 | 578 | fname = "tst_emulate_power_cut"; |
579 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
580 | &dfs_fops); | |
581 | if (IS_ERR_OR_NULL(dent)) | |
582 | goto out_remove; | |
583 | d->dfs_emulate_power_cut = dent; | |
584 | ||
585 | fname = "tst_emulate_power_cut_min"; | |
586 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
587 | &dfs_fops); | |
588 | if (IS_ERR_OR_NULL(dent)) | |
589 | goto out_remove; | |
590 | d->dfs_power_cut_min = dent; | |
591 | ||
592 | fname = "tst_emulate_power_cut_max"; | |
593 | dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, (void *)ubi_num, | |
594 | &dfs_fops); | |
595 | if (IS_ERR_OR_NULL(dent)) | |
596 | goto out_remove; | |
597 | d->dfs_power_cut_max = dent; | |
598 | ||
7bccd12d BS |
599 | fname = "detailed_erase_block_info"; |
600 | dent = debugfs_create_file(fname, S_IRUSR, d->dfs_dir, (void *)ubi_num, | |
601 | &eraseblk_count_fops); | |
602 | if (IS_ERR_OR_NULL(dent)) | |
603 | goto out_remove; | |
604 | ||
2a734bb8 AB |
605 | return 0; |
606 | ||
607 | out_remove: | |
608 | debugfs_remove_recursive(d->dfs_dir); | |
609 | out: | |
610 | err = dent ? PTR_ERR(dent) : -ENODEV; | |
32608703 | 611 | ubi_err(ubi, "cannot create \"%s\" debugfs file or directory, error %d\n", |
2a734bb8 AB |
612 | fname, err); |
613 | return err; | |
614 | } | |
615 | ||
616 | /** | |
617 | * dbg_debug_exit_dev - free all debugfs files corresponding to device @ubi | |
618 | * @ubi: UBI device description object | |
619 | */ | |
620 | void ubi_debugfs_exit_dev(struct ubi_device *ubi) | |
621 | { | |
903e0e4e | 622 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
eab73772 | 623 | debugfs_remove_recursive(ubi->dbg.dfs_dir); |
2a734bb8 | 624 | } |
50269067 | 625 | |
626 | /** | |
627 | * ubi_dbg_power_cut - emulate a power cut if it is time to do so | |
628 | * @ubi: UBI device description object | |
629 | * @caller: Flags set to indicate from where the function is being called | |
630 | * | |
631 | * Returns non-zero if a power cut was emulated, zero if not. | |
632 | */ | |
633 | int ubi_dbg_power_cut(struct ubi_device *ubi, int caller) | |
634 | { | |
635 | unsigned int range; | |
636 | ||
637 | if ((ubi->dbg.emulate_power_cut & caller) == 0) | |
638 | return 0; | |
639 | ||
640 | if (ubi->dbg.power_cut_counter == 0) { | |
641 | ubi->dbg.power_cut_counter = ubi->dbg.power_cut_min; | |
642 | ||
643 | if (ubi->dbg.power_cut_max > ubi->dbg.power_cut_min) { | |
644 | range = ubi->dbg.power_cut_max - ubi->dbg.power_cut_min; | |
645 | ubi->dbg.power_cut_counter += prandom_u32() % range; | |
646 | } | |
647 | return 0; | |
648 | } | |
649 | ||
650 | ubi->dbg.power_cut_counter--; | |
651 | if (ubi->dbg.power_cut_counter) | |
652 | return 0; | |
653 | ||
654 | ubi_msg(ubi, "XXXXXXXXXXXXXXX emulating a power cut XXXXXXXXXXXXXXXX"); | |
655 | ubi_ro_mode(ubi); | |
656 | return 1; | |
657 | } |