1 /*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23 /*
24 * This file implements most of the debugging stuff which is compiled in only
25 * when it is enabled. But some debugging check functions are implemented in
26 * corresponding subsystem, just because they are closely related and utilize
27 * various local functions of those subsystems.
28 */
29
30 #include <linux/module.h>
31 #include <linux/debugfs.h>
32 #include <linux/math64.h>
33 #include <linux/uaccess.h>
34 #include <linux/random.h>
35 #include <linux/ctype.h>
36 #include "ubifs.h"
37
38 static DEFINE_SPINLOCK(dbg_lock);
39
get_key_fmt(int fmt)40 static const char *get_key_fmt(int fmt)
41 {
42 switch (fmt) {
43 case UBIFS_SIMPLE_KEY_FMT:
44 return "simple";
45 default:
46 return "unknown/invalid format";
47 }
48 }
49
get_key_hash(int hash)50 static const char *get_key_hash(int hash)
51 {
52 switch (hash) {
53 case UBIFS_KEY_HASH_R5:
54 return "R5";
55 case UBIFS_KEY_HASH_TEST:
56 return "test";
57 default:
58 return "unknown/invalid name hash";
59 }
60 }
61
get_key_type(int type)62 static const char *get_key_type(int type)
63 {
64 switch (type) {
65 case UBIFS_INO_KEY:
66 return "inode";
67 case UBIFS_DENT_KEY:
68 return "direntry";
69 case UBIFS_XENT_KEY:
70 return "xentry";
71 case UBIFS_DATA_KEY:
72 return "data";
73 case UBIFS_TRUN_KEY:
74 return "truncate";
75 default:
76 return "unknown/invalid key";
77 }
78 }
79
get_dent_type(int type)80 static const char *get_dent_type(int type)
81 {
82 switch (type) {
83 case UBIFS_ITYPE_REG:
84 return "file";
85 case UBIFS_ITYPE_DIR:
86 return "dir";
87 case UBIFS_ITYPE_LNK:
88 return "symlink";
89 case UBIFS_ITYPE_BLK:
90 return "blkdev";
91 case UBIFS_ITYPE_CHR:
92 return "char dev";
93 case UBIFS_ITYPE_FIFO:
94 return "fifo";
95 case UBIFS_ITYPE_SOCK:
96 return "socket";
97 default:
98 return "unknown/invalid type";
99 }
100 }
101
dbg_snprintf_key(const struct ubifs_info * c,const union ubifs_key * key,char * buffer,int len)102 const char *dbg_snprintf_key(const struct ubifs_info *c,
103 const union ubifs_key *key, char *buffer, int len)
104 {
105 char *p = buffer;
106 int type = key_type(c, key);
107
108 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
109 switch (type) {
110 case UBIFS_INO_KEY:
111 len -= snprintf(p, len, "(%lu, %s)",
112 (unsigned long)key_inum(c, key),
113 get_key_type(type));
114 break;
115 case UBIFS_DENT_KEY:
116 case UBIFS_XENT_KEY:
117 len -= snprintf(p, len, "(%lu, %s, %#08x)",
118 (unsigned long)key_inum(c, key),
119 get_key_type(type), key_hash(c, key));
120 break;
121 case UBIFS_DATA_KEY:
122 len -= snprintf(p, len, "(%lu, %s, %u)",
123 (unsigned long)key_inum(c, key),
124 get_key_type(type), key_block(c, key));
125 break;
126 case UBIFS_TRUN_KEY:
127 len -= snprintf(p, len, "(%lu, %s)",
128 (unsigned long)key_inum(c, key),
129 get_key_type(type));
130 break;
131 default:
132 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
133 key->u32[0], key->u32[1]);
134 }
135 } else
136 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
137 ubifs_assert(c, len > 0);
138 return p;
139 }
140
dbg_ntype(int type)141 const char *dbg_ntype(int type)
142 {
143 switch (type) {
144 case UBIFS_PAD_NODE:
145 return "padding node";
146 case UBIFS_SB_NODE:
147 return "superblock node";
148 case UBIFS_MST_NODE:
149 return "master node";
150 case UBIFS_REF_NODE:
151 return "reference node";
152 case UBIFS_INO_NODE:
153 return "inode node";
154 case UBIFS_DENT_NODE:
155 return "direntry node";
156 case UBIFS_XENT_NODE:
157 return "xentry node";
158 case UBIFS_DATA_NODE:
159 return "data node";
160 case UBIFS_TRUN_NODE:
161 return "truncate node";
162 case UBIFS_IDX_NODE:
163 return "indexing node";
164 case UBIFS_CS_NODE:
165 return "commit start node";
166 case UBIFS_ORPH_NODE:
167 return "orphan node";
168 default:
169 return "unknown node";
170 }
171 }
172
dbg_gtype(int type)173 static const char *dbg_gtype(int type)
174 {
175 switch (type) {
176 case UBIFS_NO_NODE_GROUP:
177 return "no node group";
178 case UBIFS_IN_NODE_GROUP:
179 return "in node group";
180 case UBIFS_LAST_OF_NODE_GROUP:
181 return "last of node group";
182 default:
183 return "unknown";
184 }
185 }
186
dbg_cstate(int cmt_state)187 const char *dbg_cstate(int cmt_state)
188 {
189 switch (cmt_state) {
190 case COMMIT_RESTING:
191 return "commit resting";
192 case COMMIT_BACKGROUND:
193 return "background commit requested";
194 case COMMIT_REQUIRED:
195 return "commit required";
196 case COMMIT_RUNNING_BACKGROUND:
197 return "BACKGROUND commit running";
198 case COMMIT_RUNNING_REQUIRED:
199 return "commit running and required";
200 case COMMIT_BROKEN:
201 return "broken commit";
202 default:
203 return "unknown commit state";
204 }
205 }
206
dbg_jhead(int jhead)207 const char *dbg_jhead(int jhead)
208 {
209 switch (jhead) {
210 case GCHD:
211 return "0 (GC)";
212 case BASEHD:
213 return "1 (base)";
214 case DATAHD:
215 return "2 (data)";
216 default:
217 return "unknown journal head";
218 }
219 }
220
dump_ch(const struct ubifs_ch * ch)221 static void dump_ch(const struct ubifs_ch *ch)
222 {
223 pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic));
224 pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc));
225 pr_err("\tnode_type %d (%s)\n", ch->node_type,
226 dbg_ntype(ch->node_type));
227 pr_err("\tgroup_type %d (%s)\n", ch->group_type,
228 dbg_gtype(ch->group_type));
229 pr_err("\tsqnum %llu\n",
230 (unsigned long long)le64_to_cpu(ch->sqnum));
231 pr_err("\tlen %u\n", le32_to_cpu(ch->len));
232 }
233
ubifs_dump_inode(struct ubifs_info * c,const struct inode * inode)234 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
235 {
236 const struct ubifs_inode *ui = ubifs_inode(inode);
237 struct fscrypt_name nm = {0};
238 union ubifs_key key;
239 struct ubifs_dent_node *dent, *pdent = NULL;
240 int count = 2;
241
242 pr_err("Dump in-memory inode:");
243 pr_err("\tinode %lu\n", inode->i_ino);
244 pr_err("\tsize %llu\n",
245 (unsigned long long)i_size_read(inode));
246 pr_err("\tnlink %u\n", inode->i_nlink);
247 pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode));
248 pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode));
249 pr_err("\tatime %u.%u\n",
250 (unsigned int)inode->i_atime.tv_sec,
251 (unsigned int)inode->i_atime.tv_nsec);
252 pr_err("\tmtime %u.%u\n",
253 (unsigned int)inode->i_mtime.tv_sec,
254 (unsigned int)inode->i_mtime.tv_nsec);
255 pr_err("\tctime %u.%u\n",
256 (unsigned int)inode->i_ctime.tv_sec,
257 (unsigned int)inode->i_ctime.tv_nsec);
258 pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum);
259 pr_err("\txattr_size %u\n", ui->xattr_size);
260 pr_err("\txattr_cnt %u\n", ui->xattr_cnt);
261 pr_err("\txattr_names %u\n", ui->xattr_names);
262 pr_err("\tdirty %u\n", ui->dirty);
263 pr_err("\txattr %u\n", ui->xattr);
264 pr_err("\tbulk_read %u\n", ui->bulk_read);
265 pr_err("\tsynced_i_size %llu\n",
266 (unsigned long long)ui->synced_i_size);
267 pr_err("\tui_size %llu\n",
268 (unsigned long long)ui->ui_size);
269 pr_err("\tflags %d\n", ui->flags);
270 pr_err("\tcompr_type %d\n", ui->compr_type);
271 pr_err("\tlast_page_read %lu\n", ui->last_page_read);
272 pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row);
273 pr_err("\tdata_len %d\n", ui->data_len);
274
275 if (!S_ISDIR(inode->i_mode))
276 return;
277
278 pr_err("List of directory entries:\n");
279 ubifs_assert(c, !mutex_is_locked(&c->tnc_mutex));
280
281 lowest_dent_key(c, &key, inode->i_ino);
282 while (1) {
283 dent = ubifs_tnc_next_ent(c, &key, &nm);
284 if (IS_ERR(dent)) {
285 if (PTR_ERR(dent) != -ENOENT)
286 pr_err("error %ld\n", PTR_ERR(dent));
287 break;
288 }
289
290 pr_err("\t%d: inode %llu, type %s, len %d\n",
291 count++, (unsigned long long) le64_to_cpu(dent->inum),
292 get_dent_type(dent->type),
293 le16_to_cpu(dent->nlen));
294
295 fname_name(&nm) = dent->name;
296 fname_len(&nm) = le16_to_cpu(dent->nlen);
297 kfree(pdent);
298 pdent = dent;
299 key_read(c, &dent->key, &key);
300 }
301 kfree(pdent);
302 }
303
ubifs_dump_node(const struct ubifs_info * c,const void * node)304 void ubifs_dump_node(const struct ubifs_info *c, const void *node)
305 {
306 int i, n;
307 union ubifs_key key;
308 const struct ubifs_ch *ch = node;
309 char key_buf[DBG_KEY_BUF_LEN];
310
311 /* If the magic is incorrect, just hexdump the first bytes */
312 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
313 pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
314 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,
315 (void *)node, UBIFS_CH_SZ, 1);
316 return;
317 }
318
319 spin_lock(&dbg_lock);
320 dump_ch(node);
321
322 switch (ch->node_type) {
323 case UBIFS_PAD_NODE:
324 {
325 const struct ubifs_pad_node *pad = node;
326
327 pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len));
328 break;
329 }
330 case UBIFS_SB_NODE:
331 {
332 const struct ubifs_sb_node *sup = node;
333 unsigned int sup_flags = le32_to_cpu(sup->flags);
334
335 pr_err("\tkey_hash %d (%s)\n",
336 (int)sup->key_hash, get_key_hash(sup->key_hash));
337 pr_err("\tkey_fmt %d (%s)\n",
338 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
339 pr_err("\tflags %#x\n", sup_flags);
340 pr_err("\tbig_lpt %u\n",
341 !!(sup_flags & UBIFS_FLG_BIGLPT));
342 pr_err("\tspace_fixup %u\n",
343 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
344 pr_err("\tmin_io_size %u\n", le32_to_cpu(sup->min_io_size));
345 pr_err("\tleb_size %u\n", le32_to_cpu(sup->leb_size));
346 pr_err("\tleb_cnt %u\n", le32_to_cpu(sup->leb_cnt));
347 pr_err("\tmax_leb_cnt %u\n", le32_to_cpu(sup->max_leb_cnt));
348 pr_err("\tmax_bud_bytes %llu\n",
349 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
350 pr_err("\tlog_lebs %u\n", le32_to_cpu(sup->log_lebs));
351 pr_err("\tlpt_lebs %u\n", le32_to_cpu(sup->lpt_lebs));
352 pr_err("\torph_lebs %u\n", le32_to_cpu(sup->orph_lebs));
353 pr_err("\tjhead_cnt %u\n", le32_to_cpu(sup->jhead_cnt));
354 pr_err("\tfanout %u\n", le32_to_cpu(sup->fanout));
355 pr_err("\tlsave_cnt %u\n", le32_to_cpu(sup->lsave_cnt));
356 pr_err("\tdefault_compr %u\n",
357 (int)le16_to_cpu(sup->default_compr));
358 pr_err("\trp_size %llu\n",
359 (unsigned long long)le64_to_cpu(sup->rp_size));
360 pr_err("\trp_uid %u\n", le32_to_cpu(sup->rp_uid));
361 pr_err("\trp_gid %u\n", le32_to_cpu(sup->rp_gid));
362 pr_err("\tfmt_version %u\n", le32_to_cpu(sup->fmt_version));
363 pr_err("\ttime_gran %u\n", le32_to_cpu(sup->time_gran));
364 pr_err("\tUUID %pUB\n", sup->uuid);
365 break;
366 }
367 case UBIFS_MST_NODE:
368 {
369 const struct ubifs_mst_node *mst = node;
370
371 pr_err("\thighest_inum %llu\n",
372 (unsigned long long)le64_to_cpu(mst->highest_inum));
373 pr_err("\tcommit number %llu\n",
374 (unsigned long long)le64_to_cpu(mst->cmt_no));
375 pr_err("\tflags %#x\n", le32_to_cpu(mst->flags));
376 pr_err("\tlog_lnum %u\n", le32_to_cpu(mst->log_lnum));
377 pr_err("\troot_lnum %u\n", le32_to_cpu(mst->root_lnum));
378 pr_err("\troot_offs %u\n", le32_to_cpu(mst->root_offs));
379 pr_err("\troot_len %u\n", le32_to_cpu(mst->root_len));
380 pr_err("\tgc_lnum %u\n", le32_to_cpu(mst->gc_lnum));
381 pr_err("\tihead_lnum %u\n", le32_to_cpu(mst->ihead_lnum));
382 pr_err("\tihead_offs %u\n", le32_to_cpu(mst->ihead_offs));
383 pr_err("\tindex_size %llu\n",
384 (unsigned long long)le64_to_cpu(mst->index_size));
385 pr_err("\tlpt_lnum %u\n", le32_to_cpu(mst->lpt_lnum));
386 pr_err("\tlpt_offs %u\n", le32_to_cpu(mst->lpt_offs));
387 pr_err("\tnhead_lnum %u\n", le32_to_cpu(mst->nhead_lnum));
388 pr_err("\tnhead_offs %u\n", le32_to_cpu(mst->nhead_offs));
389 pr_err("\tltab_lnum %u\n", le32_to_cpu(mst->ltab_lnum));
390 pr_err("\tltab_offs %u\n", le32_to_cpu(mst->ltab_offs));
391 pr_err("\tlsave_lnum %u\n", le32_to_cpu(mst->lsave_lnum));
392 pr_err("\tlsave_offs %u\n", le32_to_cpu(mst->lsave_offs));
393 pr_err("\tlscan_lnum %u\n", le32_to_cpu(mst->lscan_lnum));
394 pr_err("\tleb_cnt %u\n", le32_to_cpu(mst->leb_cnt));
395 pr_err("\tempty_lebs %u\n", le32_to_cpu(mst->empty_lebs));
396 pr_err("\tidx_lebs %u\n", le32_to_cpu(mst->idx_lebs));
397 pr_err("\ttotal_free %llu\n",
398 (unsigned long long)le64_to_cpu(mst->total_free));
399 pr_err("\ttotal_dirty %llu\n",
400 (unsigned long long)le64_to_cpu(mst->total_dirty));
401 pr_err("\ttotal_used %llu\n",
402 (unsigned long long)le64_to_cpu(mst->total_used));
403 pr_err("\ttotal_dead %llu\n",
404 (unsigned long long)le64_to_cpu(mst->total_dead));
405 pr_err("\ttotal_dark %llu\n",
406 (unsigned long long)le64_to_cpu(mst->total_dark));
407 break;
408 }
409 case UBIFS_REF_NODE:
410 {
411 const struct ubifs_ref_node *ref = node;
412
413 pr_err("\tlnum %u\n", le32_to_cpu(ref->lnum));
414 pr_err("\toffs %u\n", le32_to_cpu(ref->offs));
415 pr_err("\tjhead %u\n", le32_to_cpu(ref->jhead));
416 break;
417 }
418 case UBIFS_INO_NODE:
419 {
420 const struct ubifs_ino_node *ino = node;
421
422 key_read(c, &ino->key, &key);
423 pr_err("\tkey %s\n",
424 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
425 pr_err("\tcreat_sqnum %llu\n",
426 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
427 pr_err("\tsize %llu\n",
428 (unsigned long long)le64_to_cpu(ino->size));
429 pr_err("\tnlink %u\n", le32_to_cpu(ino->nlink));
430 pr_err("\tatime %lld.%u\n",
431 (long long)le64_to_cpu(ino->atime_sec),
432 le32_to_cpu(ino->atime_nsec));
433 pr_err("\tmtime %lld.%u\n",
434 (long long)le64_to_cpu(ino->mtime_sec),
435 le32_to_cpu(ino->mtime_nsec));
436 pr_err("\tctime %lld.%u\n",
437 (long long)le64_to_cpu(ino->ctime_sec),
438 le32_to_cpu(ino->ctime_nsec));
439 pr_err("\tuid %u\n", le32_to_cpu(ino->uid));
440 pr_err("\tgid %u\n", le32_to_cpu(ino->gid));
441 pr_err("\tmode %u\n", le32_to_cpu(ino->mode));
442 pr_err("\tflags %#x\n", le32_to_cpu(ino->flags));
443 pr_err("\txattr_cnt %u\n", le32_to_cpu(ino->xattr_cnt));
444 pr_err("\txattr_size %u\n", le32_to_cpu(ino->xattr_size));
445 pr_err("\txattr_names %u\n", le32_to_cpu(ino->xattr_names));
446 pr_err("\tcompr_type %#x\n",
447 (int)le16_to_cpu(ino->compr_type));
448 pr_err("\tdata len %u\n", le32_to_cpu(ino->data_len));
449 break;
450 }
451 case UBIFS_DENT_NODE:
452 case UBIFS_XENT_NODE:
453 {
454 const struct ubifs_dent_node *dent = node;
455 int nlen = le16_to_cpu(dent->nlen);
456
457 key_read(c, &dent->key, &key);
458 pr_err("\tkey %s\n",
459 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
460 pr_err("\tinum %llu\n",
461 (unsigned long long)le64_to_cpu(dent->inum));
462 pr_err("\ttype %d\n", (int)dent->type);
463 pr_err("\tnlen %d\n", nlen);
464 pr_err("\tname ");
465
466 if (nlen > UBIFS_MAX_NLEN)
467 pr_err("(bad name length, not printing, bad or corrupted node)");
468 else {
469 for (i = 0; i < nlen && dent->name[i]; i++)
470 pr_cont("%c", isprint(dent->name[i]) ?
471 dent->name[i] : '?');
472 }
473 pr_cont("\n");
474
475 break;
476 }
477 case UBIFS_DATA_NODE:
478 {
479 const struct ubifs_data_node *dn = node;
480 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
481
482 key_read(c, &dn->key, &key);
483 pr_err("\tkey %s\n",
484 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
485 pr_err("\tsize %u\n", le32_to_cpu(dn->size));
486 pr_err("\tcompr_typ %d\n",
487 (int)le16_to_cpu(dn->compr_type));
488 pr_err("\tdata size %d\n", dlen);
489 pr_err("\tdata:\n");
490 print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
491 (void *)&dn->data, dlen, 0);
492 break;
493 }
494 case UBIFS_TRUN_NODE:
495 {
496 const struct ubifs_trun_node *trun = node;
497
498 pr_err("\tinum %u\n", le32_to_cpu(trun->inum));
499 pr_err("\told_size %llu\n",
500 (unsigned long long)le64_to_cpu(trun->old_size));
501 pr_err("\tnew_size %llu\n",
502 (unsigned long long)le64_to_cpu(trun->new_size));
503 break;
504 }
505 case UBIFS_IDX_NODE:
506 {
507 const struct ubifs_idx_node *idx = node;
508
509 n = le16_to_cpu(idx->child_cnt);
510 pr_err("\tchild_cnt %d\n", n);
511 pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level));
512 pr_err("\tBranches:\n");
513
514 for (i = 0; i < n && i < c->fanout - 1; i++) {
515 const struct ubifs_branch *br;
516
517 br = ubifs_idx_branch(c, idx, i);
518 key_read(c, &br->key, &key);
519 pr_err("\t%d: LEB %d:%d len %d key %s\n",
520 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
521 le32_to_cpu(br->len),
522 dbg_snprintf_key(c, &key, key_buf,
523 DBG_KEY_BUF_LEN));
524 }
525 break;
526 }
527 case UBIFS_CS_NODE:
528 break;
529 case UBIFS_ORPH_NODE:
530 {
531 const struct ubifs_orph_node *orph = node;
532
533 pr_err("\tcommit number %llu\n",
534 (unsigned long long)
535 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
536 pr_err("\tlast node flag %llu\n",
537 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
538 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
539 pr_err("\t%d orphan inode numbers:\n", n);
540 for (i = 0; i < n; i++)
541 pr_err("\t ino %llu\n",
542 (unsigned long long)le64_to_cpu(orph->inos[i]));
543 break;
544 }
545 default:
546 pr_err("node type %d was not recognized\n",
547 (int)ch->node_type);
548 }
549 spin_unlock(&dbg_lock);
550 }
551
ubifs_dump_budget_req(const struct ubifs_budget_req * req)552 void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
553 {
554 spin_lock(&dbg_lock);
555 pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
556 req->new_ino, req->dirtied_ino);
557 pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n",
558 req->new_ino_d, req->dirtied_ino_d);
559 pr_err("\tnew_page %d, dirtied_page %d\n",
560 req->new_page, req->dirtied_page);
561 pr_err("\tnew_dent %d, mod_dent %d\n",
562 req->new_dent, req->mod_dent);
563 pr_err("\tidx_growth %d\n", req->idx_growth);
564 pr_err("\tdata_growth %d dd_growth %d\n",
565 req->data_growth, req->dd_growth);
566 spin_unlock(&dbg_lock);
567 }
568
ubifs_dump_lstats(const struct ubifs_lp_stats * lst)569 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
570 {
571 spin_lock(&dbg_lock);
572 pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n",
573 current->pid, lst->empty_lebs, lst->idx_lebs);
574 pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
575 lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
576 pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
577 lst->total_used, lst->total_dark, lst->total_dead);
578 spin_unlock(&dbg_lock);
579 }
580
ubifs_dump_budg(struct ubifs_info * c,const struct ubifs_budg_info * bi)581 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
582 {
583 int i;
584 struct rb_node *rb;
585 struct ubifs_bud *bud;
586 struct ubifs_gced_idx_leb *idx_gc;
587 long long available, outstanding, free;
588
589 spin_lock(&c->space_lock);
590 spin_lock(&dbg_lock);
591 pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
592 current->pid, bi->data_growth + bi->dd_growth,
593 bi->data_growth + bi->dd_growth + bi->idx_growth);
594 pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
595 bi->data_growth, bi->dd_growth, bi->idx_growth);
596 pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
597 bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
598 pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
599 bi->page_budget, bi->inode_budget, bi->dent_budget);
600 pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
601 pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
602 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
603
604 if (bi != &c->bi)
605 /*
606 * If we are dumping saved budgeting data, do not print
607 * additional information which is about the current state, not
608 * the old one which corresponded to the saved budgeting data.
609 */
610 goto out_unlock;
611
612 pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
613 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
614 pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
615 atomic_long_read(&c->dirty_pg_cnt),
616 atomic_long_read(&c->dirty_zn_cnt),
617 atomic_long_read(&c->clean_zn_cnt));
618 pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
619
620 /* If we are in R/O mode, journal heads do not exist */
621 if (c->jheads)
622 for (i = 0; i < c->jhead_cnt; i++)
623 pr_err("\tjhead %s\t LEB %d\n",
624 dbg_jhead(c->jheads[i].wbuf.jhead),
625 c->jheads[i].wbuf.lnum);
626 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
627 bud = rb_entry(rb, struct ubifs_bud, rb);
628 pr_err("\tbud LEB %d\n", bud->lnum);
629 }
630 list_for_each_entry(bud, &c->old_buds, list)
631 pr_err("\told bud LEB %d\n", bud->lnum);
632 list_for_each_entry(idx_gc, &c->idx_gc, list)
633 pr_err("\tGC'ed idx LEB %d unmap %d\n",
634 idx_gc->lnum, idx_gc->unmap);
635 pr_err("\tcommit state %d\n", c->cmt_state);
636
637 /* Print budgeting predictions */
638 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
639 outstanding = c->bi.data_growth + c->bi.dd_growth;
640 free = ubifs_get_free_space_nolock(c);
641 pr_err("Budgeting predictions:\n");
642 pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
643 available, outstanding, free);
644 out_unlock:
645 spin_unlock(&dbg_lock);
646 spin_unlock(&c->space_lock);
647 }
648
ubifs_dump_lprop(const struct ubifs_info * c,const struct ubifs_lprops * lp)649 void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
650 {
651 int i, spc, dark = 0, dead = 0;
652 struct rb_node *rb;
653 struct ubifs_bud *bud;
654
655 spc = lp->free + lp->dirty;
656 if (spc < c->dead_wm)
657 dead = spc;
658 else
659 dark = ubifs_calc_dark(c, spc);
660
661 if (lp->flags & LPROPS_INDEX)
662 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
663 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
664 lp->flags);
665 else
666 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
667 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
668 dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
669
670 if (lp->flags & LPROPS_TAKEN) {
671 if (lp->flags & LPROPS_INDEX)
672 pr_cont("index, taken");
673 else
674 pr_cont("taken");
675 } else {
676 const char *s;
677
678 if (lp->flags & LPROPS_INDEX) {
679 switch (lp->flags & LPROPS_CAT_MASK) {
680 case LPROPS_DIRTY_IDX:
681 s = "dirty index";
682 break;
683 case LPROPS_FRDI_IDX:
684 s = "freeable index";
685 break;
686 default:
687 s = "index";
688 }
689 } else {
690 switch (lp->flags & LPROPS_CAT_MASK) {
691 case LPROPS_UNCAT:
692 s = "not categorized";
693 break;
694 case LPROPS_DIRTY:
695 s = "dirty";
696 break;
697 case LPROPS_FREE:
698 s = "free";
699 break;
700 case LPROPS_EMPTY:
701 s = "empty";
702 break;
703 case LPROPS_FREEABLE:
704 s = "freeable";
705 break;
706 default:
707 s = NULL;
708 break;
709 }
710 }
711 pr_cont("%s", s);
712 }
713
714 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
715 bud = rb_entry(rb, struct ubifs_bud, rb);
716 if (bud->lnum == lp->lnum) {
717 int head = 0;
718 for (i = 0; i < c->jhead_cnt; i++) {
719 /*
720 * Note, if we are in R/O mode or in the middle
721 * of mounting/re-mounting, the write-buffers do
722 * not exist.
723 */
724 if (c->jheads &&
725 lp->lnum == c->jheads[i].wbuf.lnum) {
726 pr_cont(", jhead %s", dbg_jhead(i));
727 head = 1;
728 }
729 }
730 if (!head)
731 pr_cont(", bud of jhead %s",
732 dbg_jhead(bud->jhead));
733 }
734 }
735 if (lp->lnum == c->gc_lnum)
736 pr_cont(", GC LEB");
737 pr_cont(")\n");
738 }
739
ubifs_dump_lprops(struct ubifs_info * c)740 void ubifs_dump_lprops(struct ubifs_info *c)
741 {
742 int lnum, err;
743 struct ubifs_lprops lp;
744 struct ubifs_lp_stats lst;
745
746 pr_err("(pid %d) start dumping LEB properties\n", current->pid);
747 ubifs_get_lp_stats(c, &lst);
748 ubifs_dump_lstats(&lst);
749
750 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
751 err = ubifs_read_one_lp(c, lnum, &lp);
752 if (err) {
753 ubifs_err(c, "cannot read lprops for LEB %d", lnum);
754 continue;
755 }
756
757 ubifs_dump_lprop(c, &lp);
758 }
759 pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
760 }
761
ubifs_dump_lpt_info(struct ubifs_info * c)762 void ubifs_dump_lpt_info(struct ubifs_info *c)
763 {
764 int i;
765
766 spin_lock(&dbg_lock);
767 pr_err("(pid %d) dumping LPT information\n", current->pid);
768 pr_err("\tlpt_sz: %lld\n", c->lpt_sz);
769 pr_err("\tpnode_sz: %d\n", c->pnode_sz);
770 pr_err("\tnnode_sz: %d\n", c->nnode_sz);
771 pr_err("\tltab_sz: %d\n", c->ltab_sz);
772 pr_err("\tlsave_sz: %d\n", c->lsave_sz);
773 pr_err("\tbig_lpt: %d\n", c->big_lpt);
774 pr_err("\tlpt_hght: %d\n", c->lpt_hght);
775 pr_err("\tpnode_cnt: %d\n", c->pnode_cnt);
776 pr_err("\tnnode_cnt: %d\n", c->nnode_cnt);
777 pr_err("\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
778 pr_err("\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
779 pr_err("\tlsave_cnt: %d\n", c->lsave_cnt);
780 pr_err("\tspace_bits: %d\n", c->space_bits);
781 pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
782 pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
783 pr_err("\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
784 pr_err("\tpcnt_bits: %d\n", c->pcnt_bits);
785 pr_err("\tlnum_bits: %d\n", c->lnum_bits);
786 pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
787 pr_err("\tLPT head is at %d:%d\n",
788 c->nhead_lnum, c->nhead_offs);
789 pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
790 if (c->big_lpt)
791 pr_err("\tLPT lsave is at %d:%d\n",
792 c->lsave_lnum, c->lsave_offs);
793 for (i = 0; i < c->lpt_lebs; i++)
794 pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
795 i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
796 c->ltab[i].tgc, c->ltab[i].cmt);
797 spin_unlock(&dbg_lock);
798 }
799
ubifs_dump_sleb(const struct ubifs_info * c,const struct ubifs_scan_leb * sleb,int offs)800 void ubifs_dump_sleb(const struct ubifs_info *c,
801 const struct ubifs_scan_leb *sleb, int offs)
802 {
803 struct ubifs_scan_node *snod;
804
805 pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
806 current->pid, sleb->lnum, offs);
807
808 list_for_each_entry(snod, &sleb->nodes, list) {
809 cond_resched();
810 pr_err("Dumping node at LEB %d:%d len %d\n",
811 sleb->lnum, snod->offs, snod->len);
812 ubifs_dump_node(c, snod->node);
813 }
814 }
815
ubifs_dump_leb(const struct ubifs_info * c,int lnum)816 void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
817 {
818 struct ubifs_scan_leb *sleb;
819 struct ubifs_scan_node *snod;
820 void *buf;
821
822 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
823
824 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
825 if (!buf) {
826 ubifs_err(c, "cannot allocate memory for dumping LEB %d", lnum);
827 return;
828 }
829
830 sleb = ubifs_scan(c, lnum, 0, buf, 0);
831 if (IS_ERR(sleb)) {
832 ubifs_err(c, "scan error %d", (int)PTR_ERR(sleb));
833 goto out;
834 }
835
836 pr_err("LEB %d has %d nodes ending at %d\n", lnum,
837 sleb->nodes_cnt, sleb->endpt);
838
839 list_for_each_entry(snod, &sleb->nodes, list) {
840 cond_resched();
841 pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
842 snod->offs, snod->len);
843 ubifs_dump_node(c, snod->node);
844 }
845
846 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
847 ubifs_scan_destroy(sleb);
848
849 out:
850 vfree(buf);
851 return;
852 }
853
ubifs_dump_znode(const struct ubifs_info * c,const struct ubifs_znode * znode)854 void ubifs_dump_znode(const struct ubifs_info *c,
855 const struct ubifs_znode *znode)
856 {
857 int n;
858 const struct ubifs_zbranch *zbr;
859 char key_buf[DBG_KEY_BUF_LEN];
860
861 spin_lock(&dbg_lock);
862 if (znode->parent)
863 zbr = &znode->parent->zbranch[znode->iip];
864 else
865 zbr = &c->zroot;
866
867 pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
868 znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
869 znode->level, znode->child_cnt, znode->flags);
870
871 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
872 spin_unlock(&dbg_lock);
873 return;
874 }
875
876 pr_err("zbranches:\n");
877 for (n = 0; n < znode->child_cnt; n++) {
878 zbr = &znode->zbranch[n];
879 if (znode->level > 0)
880 pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
881 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
882 dbg_snprintf_key(c, &zbr->key, key_buf,
883 DBG_KEY_BUF_LEN));
884 else
885 pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n",
886 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
887 dbg_snprintf_key(c, &zbr->key, key_buf,
888 DBG_KEY_BUF_LEN));
889 }
890 spin_unlock(&dbg_lock);
891 }
892
ubifs_dump_heap(struct ubifs_info * c,struct ubifs_lpt_heap * heap,int cat)893 void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
894 {
895 int i;
896
897 pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
898 current->pid, cat, heap->cnt);
899 for (i = 0; i < heap->cnt; i++) {
900 struct ubifs_lprops *lprops = heap->arr[i];
901
902 pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
903 i, lprops->lnum, lprops->hpos, lprops->free,
904 lprops->dirty, lprops->flags);
905 }
906 pr_err("(pid %d) finish dumping heap\n", current->pid);
907 }
908
ubifs_dump_pnode(struct ubifs_info * c,struct ubifs_pnode * pnode,struct ubifs_nnode * parent,int iip)909 void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
910 struct ubifs_nnode *parent, int iip)
911 {
912 int i;
913
914 pr_err("(pid %d) dumping pnode:\n", current->pid);
915 pr_err("\taddress %zx parent %zx cnext %zx\n",
916 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
917 pr_err("\tflags %lu iip %d level %d num %d\n",
918 pnode->flags, iip, pnode->level, pnode->num);
919 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
920 struct ubifs_lprops *lp = &pnode->lprops[i];
921
922 pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
923 i, lp->free, lp->dirty, lp->flags, lp->lnum);
924 }
925 }
926
ubifs_dump_tnc(struct ubifs_info * c)927 void ubifs_dump_tnc(struct ubifs_info *c)
928 {
929 struct ubifs_znode *znode;
930 int level;
931
932 pr_err("\n");
933 pr_err("(pid %d) start dumping TNC tree\n", current->pid);
934 znode = ubifs_tnc_levelorder_next(c, c->zroot.znode, NULL);
935 level = znode->level;
936 pr_err("== Level %d ==\n", level);
937 while (znode) {
938 if (level != znode->level) {
939 level = znode->level;
940 pr_err("== Level %d ==\n", level);
941 }
942 ubifs_dump_znode(c, znode);
943 znode = ubifs_tnc_levelorder_next(c, c->zroot.znode, znode);
944 }
945 pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
946 }
947
dump_znode(struct ubifs_info * c,struct ubifs_znode * znode,void * priv)948 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
949 void *priv)
950 {
951 ubifs_dump_znode(c, znode);
952 return 0;
953 }
954
955 /**
956 * ubifs_dump_index - dump the on-flash index.
957 * @c: UBIFS file-system description object
958 *
959 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
960 * which dumps only in-memory znodes and does not read znodes which from flash.
961 */
ubifs_dump_index(struct ubifs_info * c)962 void ubifs_dump_index(struct ubifs_info *c)
963 {
964 dbg_walk_index(c, NULL, dump_znode, NULL);
965 }
966
967 /**
968 * dbg_save_space_info - save information about flash space.
969 * @c: UBIFS file-system description object
970 *
971 * This function saves information about UBIFS free space, dirty space, etc, in
972 * order to check it later.
973 */
dbg_save_space_info(struct ubifs_info * c)974 void dbg_save_space_info(struct ubifs_info *c)
975 {
976 struct ubifs_debug_info *d = c->dbg;
977 int freeable_cnt;
978
979 spin_lock(&c->space_lock);
980 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
981 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
982 d->saved_idx_gc_cnt = c->idx_gc_cnt;
983
984 /*
985 * We use a dirty hack here and zero out @c->freeable_cnt, because it
986 * affects the free space calculations, and UBIFS might not know about
987 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
988 * only when we read their lprops, and we do this only lazily, upon the
989 * need. So at any given point of time @c->freeable_cnt might be not
990 * exactly accurate.
991 *
992 * Just one example about the issue we hit when we did not zero
993 * @c->freeable_cnt.
994 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
995 * amount of free space in @d->saved_free
996 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
997 * information from flash, where we cache LEBs from various
998 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
999 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1000 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1001 * -> 'ubifs_add_to_cat()').
1002 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1003 * becomes %1.
1004 * 4. We calculate the amount of free space when the re-mount is
1005 * finished in 'dbg_check_space_info()' and it does not match
1006 * @d->saved_free.
1007 */
1008 freeable_cnt = c->freeable_cnt;
1009 c->freeable_cnt = 0;
1010 d->saved_free = ubifs_get_free_space_nolock(c);
1011 c->freeable_cnt = freeable_cnt;
1012 spin_unlock(&c->space_lock);
1013 }
1014
1015 /**
1016 * dbg_check_space_info - check flash space information.
1017 * @c: UBIFS file-system description object
1018 *
1019 * This function compares current flash space information with the information
1020 * which was saved when the 'dbg_save_space_info()' function was called.
1021 * Returns zero if the information has not changed, and %-EINVAL it it has
1022 * changed.
1023 */
dbg_check_space_info(struct ubifs_info * c)1024 int dbg_check_space_info(struct ubifs_info *c)
1025 {
1026 struct ubifs_debug_info *d = c->dbg;
1027 struct ubifs_lp_stats lst;
1028 long long free;
1029 int freeable_cnt;
1030
1031 spin_lock(&c->space_lock);
1032 freeable_cnt = c->freeable_cnt;
1033 c->freeable_cnt = 0;
1034 free = ubifs_get_free_space_nolock(c);
1035 c->freeable_cnt = freeable_cnt;
1036 spin_unlock(&c->space_lock);
1037
1038 if (free != d->saved_free) {
1039 ubifs_err(c, "free space changed from %lld to %lld",
1040 d->saved_free, free);
1041 goto out;
1042 }
1043
1044 return 0;
1045
1046 out:
1047 ubifs_msg(c, "saved lprops statistics dump");
1048 ubifs_dump_lstats(&d->saved_lst);
1049 ubifs_msg(c, "saved budgeting info dump");
1050 ubifs_dump_budg(c, &d->saved_bi);
1051 ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1052 ubifs_msg(c, "current lprops statistics dump");
1053 ubifs_get_lp_stats(c, &lst);
1054 ubifs_dump_lstats(&lst);
1055 ubifs_msg(c, "current budgeting info dump");
1056 ubifs_dump_budg(c, &c->bi);
1057 dump_stack();
1058 return -EINVAL;
1059 }
1060
1061 /**
1062 * dbg_check_synced_i_size - check synchronized inode size.
1063 * @c: UBIFS file-system description object
1064 * @inode: inode to check
1065 *
1066 * If inode is clean, synchronized inode size has to be equivalent to current
1067 * inode size. This function has to be called only for locked inodes (@i_mutex
1068 * has to be locked). Returns %0 if synchronized inode size if correct, and
1069 * %-EINVAL if not.
1070 */
dbg_check_synced_i_size(const struct ubifs_info * c,struct inode * inode)1071 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1072 {
1073 int err = 0;
1074 struct ubifs_inode *ui = ubifs_inode(inode);
1075
1076 if (!dbg_is_chk_gen(c))
1077 return 0;
1078 if (!S_ISREG(inode->i_mode))
1079 return 0;
1080
1081 mutex_lock(&ui->ui_mutex);
1082 spin_lock(&ui->ui_lock);
1083 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1084 ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean",
1085 ui->ui_size, ui->synced_i_size);
1086 ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1087 inode->i_mode, i_size_read(inode));
1088 dump_stack();
1089 err = -EINVAL;
1090 }
1091 spin_unlock(&ui->ui_lock);
1092 mutex_unlock(&ui->ui_mutex);
1093 return err;
1094 }
1095
1096 /*
1097 * dbg_check_dir - check directory inode size and link count.
1098 * @c: UBIFS file-system description object
1099 * @dir: the directory to calculate size for
1100 * @size: the result is returned here
1101 *
1102 * This function makes sure that directory size and link count are correct.
1103 * Returns zero in case of success and a negative error code in case of
1104 * failure.
1105 *
1106 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1107 * calling this function.
1108 */
dbg_check_dir(struct ubifs_info * c,const struct inode * dir)1109 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1110 {
1111 unsigned int nlink = 2;
1112 union ubifs_key key;
1113 struct ubifs_dent_node *dent, *pdent = NULL;
1114 struct fscrypt_name nm = {0};
1115 loff_t size = UBIFS_INO_NODE_SZ;
1116
1117 if (!dbg_is_chk_gen(c))
1118 return 0;
1119
1120 if (!S_ISDIR(dir->i_mode))
1121 return 0;
1122
1123 lowest_dent_key(c, &key, dir->i_ino);
1124 while (1) {
1125 int err;
1126
1127 dent = ubifs_tnc_next_ent(c, &key, &nm);
1128 if (IS_ERR(dent)) {
1129 err = PTR_ERR(dent);
1130 if (err == -ENOENT)
1131 break;
1132 kfree(pdent);
1133 return err;
1134 }
1135
1136 fname_name(&nm) = dent->name;
1137 fname_len(&nm) = le16_to_cpu(dent->nlen);
1138 size += CALC_DENT_SIZE(fname_len(&nm));
1139 if (dent->type == UBIFS_ITYPE_DIR)
1140 nlink += 1;
1141 kfree(pdent);
1142 pdent = dent;
1143 key_read(c, &dent->key, &key);
1144 }
1145 kfree(pdent);
1146
1147 if (i_size_read(dir) != size) {
1148 ubifs_err(c, "directory inode %lu has size %llu, but calculated size is %llu",
1149 dir->i_ino, (unsigned long long)i_size_read(dir),
1150 (unsigned long long)size);
1151 ubifs_dump_inode(c, dir);
1152 dump_stack();
1153 return -EINVAL;
1154 }
1155 if (dir->i_nlink != nlink) {
1156 ubifs_err(c, "directory inode %lu has nlink %u, but calculated nlink is %u",
1157 dir->i_ino, dir->i_nlink, nlink);
1158 ubifs_dump_inode(c, dir);
1159 dump_stack();
1160 return -EINVAL;
1161 }
1162
1163 return 0;
1164 }
1165
1166 /**
1167 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1168 * @c: UBIFS file-system description object
1169 * @zbr1: first zbranch
1170 * @zbr2: following zbranch
1171 *
1172 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1173 * names of the direntries/xentries which are referred by the keys. This
1174 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1175 * sure the name of direntry/xentry referred by @zbr1 is less than
1176 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1177 * and a negative error code in case of failure.
1178 */
dbg_check_key_order(struct ubifs_info * c,struct ubifs_zbranch * zbr1,struct ubifs_zbranch * zbr2)1179 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1180 struct ubifs_zbranch *zbr2)
1181 {
1182 int err, nlen1, nlen2, cmp;
1183 struct ubifs_dent_node *dent1, *dent2;
1184 union ubifs_key key;
1185 char key_buf[DBG_KEY_BUF_LEN];
1186
1187 ubifs_assert(c, !keys_cmp(c, &zbr1->key, &zbr2->key));
1188 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1189 if (!dent1)
1190 return -ENOMEM;
1191 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1192 if (!dent2) {
1193 err = -ENOMEM;
1194 goto out_free;
1195 }
1196
1197 err = ubifs_tnc_read_node(c, zbr1, dent1);
1198 if (err)
1199 goto out_free;
1200 err = ubifs_validate_entry(c, dent1);
1201 if (err)
1202 goto out_free;
1203
1204 err = ubifs_tnc_read_node(c, zbr2, dent2);
1205 if (err)
1206 goto out_free;
1207 err = ubifs_validate_entry(c, dent2);
1208 if (err)
1209 goto out_free;
1210
1211 /* Make sure node keys are the same as in zbranch */
1212 err = 1;
1213 key_read(c, &dent1->key, &key);
1214 if (keys_cmp(c, &zbr1->key, &key)) {
1215 ubifs_err(c, "1st entry at %d:%d has key %s", zbr1->lnum,
1216 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1217 DBG_KEY_BUF_LEN));
1218 ubifs_err(c, "but it should have key %s according to tnc",
1219 dbg_snprintf_key(c, &zbr1->key, key_buf,
1220 DBG_KEY_BUF_LEN));
1221 ubifs_dump_node(c, dent1);
1222 goto out_free;
1223 }
1224
1225 key_read(c, &dent2->key, &key);
1226 if (keys_cmp(c, &zbr2->key, &key)) {
1227 ubifs_err(c, "2nd entry at %d:%d has key %s", zbr1->lnum,
1228 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1229 DBG_KEY_BUF_LEN));
1230 ubifs_err(c, "but it should have key %s according to tnc",
1231 dbg_snprintf_key(c, &zbr2->key, key_buf,
1232 DBG_KEY_BUF_LEN));
1233 ubifs_dump_node(c, dent2);
1234 goto out_free;
1235 }
1236
1237 nlen1 = le16_to_cpu(dent1->nlen);
1238 nlen2 = le16_to_cpu(dent2->nlen);
1239
1240 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1241 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1242 err = 0;
1243 goto out_free;
1244 }
1245 if (cmp == 0 && nlen1 == nlen2)
1246 ubifs_err(c, "2 xent/dent nodes with the same name");
1247 else
1248 ubifs_err(c, "bad order of colliding key %s",
1249 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1250
1251 ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1252 ubifs_dump_node(c, dent1);
1253 ubifs_msg(c, "second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1254 ubifs_dump_node(c, dent2);
1255
1256 out_free:
1257 kfree(dent2);
1258 kfree(dent1);
1259 return err;
1260 }
1261
1262 /**
1263 * dbg_check_znode - check if znode is all right.
1264 * @c: UBIFS file-system description object
1265 * @zbr: zbranch which points to this znode
1266 *
1267 * This function makes sure that znode referred to by @zbr is all right.
1268 * Returns zero if it is, and %-EINVAL if it is not.
1269 */
dbg_check_znode(struct ubifs_info * c,struct ubifs_zbranch * zbr)1270 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1271 {
1272 struct ubifs_znode *znode = zbr->znode;
1273 struct ubifs_znode *zp = znode->parent;
1274 int n, err, cmp;
1275
1276 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1277 err = 1;
1278 goto out;
1279 }
1280 if (znode->level < 0) {
1281 err = 2;
1282 goto out;
1283 }
1284 if (znode->iip < 0 || znode->iip >= c->fanout) {
1285 err = 3;
1286 goto out;
1287 }
1288
1289 if (zbr->len == 0)
1290 /* Only dirty zbranch may have no on-flash nodes */
1291 if (!ubifs_zn_dirty(znode)) {
1292 err = 4;
1293 goto out;
1294 }
1295
1296 if (ubifs_zn_dirty(znode)) {
1297 /*
1298 * If znode is dirty, its parent has to be dirty as well. The
1299 * order of the operation is important, so we have to have
1300 * memory barriers.
1301 */
1302 smp_mb();
1303 if (zp && !ubifs_zn_dirty(zp)) {
1304 /*
1305 * The dirty flag is atomic and is cleared outside the
1306 * TNC mutex, so znode's dirty flag may now have
1307 * been cleared. The child is always cleared before the
1308 * parent, so we just need to check again.
1309 */
1310 smp_mb();
1311 if (ubifs_zn_dirty(znode)) {
1312 err = 5;
1313 goto out;
1314 }
1315 }
1316 }
1317
1318 if (zp) {
1319 const union ubifs_key *min, *max;
1320
1321 if (znode->level != zp->level - 1) {
1322 err = 6;
1323 goto out;
1324 }
1325
1326 /* Make sure the 'parent' pointer in our znode is correct */
1327 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1328 if (!err) {
1329 /* This zbranch does not exist in the parent */
1330 err = 7;
1331 goto out;
1332 }
1333
1334 if (znode->iip >= zp->child_cnt) {
1335 err = 8;
1336 goto out;
1337 }
1338
1339 if (znode->iip != n) {
1340 /* This may happen only in case of collisions */
1341 if (keys_cmp(c, &zp->zbranch[n].key,
1342 &zp->zbranch[znode->iip].key)) {
1343 err = 9;
1344 goto out;
1345 }
1346 n = znode->iip;
1347 }
1348
1349 /*
1350 * Make sure that the first key in our znode is greater than or
1351 * equal to the key in the pointing zbranch.
1352 */
1353 min = &zbr->key;
1354 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1355 if (cmp == 1) {
1356 err = 10;
1357 goto out;
1358 }
1359
1360 if (n + 1 < zp->child_cnt) {
1361 max = &zp->zbranch[n + 1].key;
1362
1363 /*
1364 * Make sure the last key in our znode is less or
1365 * equivalent than the key in the zbranch which goes
1366 * after our pointing zbranch.
1367 */
1368 cmp = keys_cmp(c, max,
1369 &znode->zbranch[znode->child_cnt - 1].key);
1370 if (cmp == -1) {
1371 err = 11;
1372 goto out;
1373 }
1374 }
1375 } else {
1376 /* This may only be root znode */
1377 if (zbr != &c->zroot) {
1378 err = 12;
1379 goto out;
1380 }
1381 }
1382
1383 /*
1384 * Make sure that next key is greater or equivalent then the previous
1385 * one.
1386 */
1387 for (n = 1; n < znode->child_cnt; n++) {
1388 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1389 &znode->zbranch[n].key);
1390 if (cmp > 0) {
1391 err = 13;
1392 goto out;
1393 }
1394 if (cmp == 0) {
1395 /* This can only be keys with colliding hash */
1396 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1397 err = 14;
1398 goto out;
1399 }
1400
1401 if (znode->level != 0 || c->replaying)
1402 continue;
1403
1404 /*
1405 * Colliding keys should follow binary order of
1406 * corresponding xentry/dentry names.
1407 */
1408 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1409 &znode->zbranch[n]);
1410 if (err < 0)
1411 return err;
1412 if (err) {
1413 err = 15;
1414 goto out;
1415 }
1416 }
1417 }
1418
1419 for (n = 0; n < znode->child_cnt; n++) {
1420 if (!znode->zbranch[n].znode &&
1421 (znode->zbranch[n].lnum == 0 ||
1422 znode->zbranch[n].len == 0)) {
1423 err = 16;
1424 goto out;
1425 }
1426
1427 if (znode->zbranch[n].lnum != 0 &&
1428 znode->zbranch[n].len == 0) {
1429 err = 17;
1430 goto out;
1431 }
1432
1433 if (znode->zbranch[n].lnum == 0 &&
1434 znode->zbranch[n].len != 0) {
1435 err = 18;
1436 goto out;
1437 }
1438
1439 if (znode->zbranch[n].lnum == 0 &&
1440 znode->zbranch[n].offs != 0) {
1441 err = 19;
1442 goto out;
1443 }
1444
1445 if (znode->level != 0 && znode->zbranch[n].znode)
1446 if (znode->zbranch[n].znode->parent != znode) {
1447 err = 20;
1448 goto out;
1449 }
1450 }
1451
1452 return 0;
1453
1454 out:
1455 ubifs_err(c, "failed, error %d", err);
1456 ubifs_msg(c, "dump of the znode");
1457 ubifs_dump_znode(c, znode);
1458 if (zp) {
1459 ubifs_msg(c, "dump of the parent znode");
1460 ubifs_dump_znode(c, zp);
1461 }
1462 dump_stack();
1463 return -EINVAL;
1464 }
1465
1466 /**
1467 * dbg_check_tnc - check TNC tree.
1468 * @c: UBIFS file-system description object
1469 * @extra: do extra checks that are possible at start commit
1470 *
1471 * This function traverses whole TNC tree and checks every znode. Returns zero
1472 * if everything is all right and %-EINVAL if something is wrong with TNC.
1473 */
dbg_check_tnc(struct ubifs_info * c,int extra)1474 int dbg_check_tnc(struct ubifs_info *c, int extra)
1475 {
1476 struct ubifs_znode *znode;
1477 long clean_cnt = 0, dirty_cnt = 0;
1478 int err, last;
1479
1480 if (!dbg_is_chk_index(c))
1481 return 0;
1482
1483 ubifs_assert(c, mutex_is_locked(&c->tnc_mutex));
1484 if (!c->zroot.znode)
1485 return 0;
1486
1487 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1488 while (1) {
1489 struct ubifs_znode *prev;
1490 struct ubifs_zbranch *zbr;
1491
1492 if (!znode->parent)
1493 zbr = &c->zroot;
1494 else
1495 zbr = &znode->parent->zbranch[znode->iip];
1496
1497 err = dbg_check_znode(c, zbr);
1498 if (err)
1499 return err;
1500
1501 if (extra) {
1502 if (ubifs_zn_dirty(znode))
1503 dirty_cnt += 1;
1504 else
1505 clean_cnt += 1;
1506 }
1507
1508 prev = znode;
1509 znode = ubifs_tnc_postorder_next(c, znode);
1510 if (!znode)
1511 break;
1512
1513 /*
1514 * If the last key of this znode is equivalent to the first key
1515 * of the next znode (collision), then check order of the keys.
1516 */
1517 last = prev->child_cnt - 1;
1518 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1519 !keys_cmp(c, &prev->zbranch[last].key,
1520 &znode->zbranch[0].key)) {
1521 err = dbg_check_key_order(c, &prev->zbranch[last],
1522 &znode->zbranch[0]);
1523 if (err < 0)
1524 return err;
1525 if (err) {
1526 ubifs_msg(c, "first znode");
1527 ubifs_dump_znode(c, prev);
1528 ubifs_msg(c, "second znode");
1529 ubifs_dump_znode(c, znode);
1530 return -EINVAL;
1531 }
1532 }
1533 }
1534
1535 if (extra) {
1536 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1537 ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld",
1538 atomic_long_read(&c->clean_zn_cnt),
1539 clean_cnt);
1540 return -EINVAL;
1541 }
1542 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1543 ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld",
1544 atomic_long_read(&c->dirty_zn_cnt),
1545 dirty_cnt);
1546 return -EINVAL;
1547 }
1548 }
1549
1550 return 0;
1551 }
1552
1553 /**
1554 * dbg_walk_index - walk the on-flash index.
1555 * @c: UBIFS file-system description object
1556 * @leaf_cb: called for each leaf node
1557 * @znode_cb: called for each indexing node
1558 * @priv: private data which is passed to callbacks
1559 *
1560 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1561 * node and @znode_cb for each indexing node. Returns zero in case of success
1562 * and a negative error code in case of failure.
1563 *
1564 * It would be better if this function removed every znode it pulled to into
1565 * the TNC, so that the behavior more closely matched the non-debugging
1566 * behavior.
1567 */
dbg_walk_index(struct ubifs_info * c,dbg_leaf_callback leaf_cb,dbg_znode_callback znode_cb,void * priv)1568 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1569 dbg_znode_callback znode_cb, void *priv)
1570 {
1571 int err;
1572 struct ubifs_zbranch *zbr;
1573 struct ubifs_znode *znode, *child;
1574
1575 mutex_lock(&c->tnc_mutex);
1576 /* If the root indexing node is not in TNC - pull it */
1577 if (!c->zroot.znode) {
1578 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1579 if (IS_ERR(c->zroot.znode)) {
1580 err = PTR_ERR(c->zroot.znode);
1581 c->zroot.znode = NULL;
1582 goto out_unlock;
1583 }
1584 }
1585
1586 /*
1587 * We are going to traverse the indexing tree in the postorder manner.
1588 * Go down and find the leftmost indexing node where we are going to
1589 * start from.
1590 */
1591 znode = c->zroot.znode;
1592 while (znode->level > 0) {
1593 zbr = &znode->zbranch[0];
1594 child = zbr->znode;
1595 if (!child) {
1596 child = ubifs_load_znode(c, zbr, znode, 0);
1597 if (IS_ERR(child)) {
1598 err = PTR_ERR(child);
1599 goto out_unlock;
1600 }
1601 zbr->znode = child;
1602 }
1603
1604 znode = child;
1605 }
1606
1607 /* Iterate over all indexing nodes */
1608 while (1) {
1609 int idx;
1610
1611 cond_resched();
1612
1613 if (znode_cb) {
1614 err = znode_cb(c, znode, priv);
1615 if (err) {
1616 ubifs_err(c, "znode checking function returned error %d",
1617 err);
1618 ubifs_dump_znode(c, znode);
1619 goto out_dump;
1620 }
1621 }
1622 if (leaf_cb && znode->level == 0) {
1623 for (idx = 0; idx < znode->child_cnt; idx++) {
1624 zbr = &znode->zbranch[idx];
1625 err = leaf_cb(c, zbr, priv);
1626 if (err) {
1627 ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d",
1628 err, zbr->lnum, zbr->offs);
1629 goto out_dump;
1630 }
1631 }
1632 }
1633
1634 if (!znode->parent)
1635 break;
1636
1637 idx = znode->iip + 1;
1638 znode = znode->parent;
1639 if (idx < znode->child_cnt) {
1640 /* Switch to the next index in the parent */
1641 zbr = &znode->zbranch[idx];
1642 child = zbr->znode;
1643 if (!child) {
1644 child = ubifs_load_znode(c, zbr, znode, idx);
1645 if (IS_ERR(child)) {
1646 err = PTR_ERR(child);
1647 goto out_unlock;
1648 }
1649 zbr->znode = child;
1650 }
1651 znode = child;
1652 } else
1653 /*
1654 * This is the last child, switch to the parent and
1655 * continue.
1656 */
1657 continue;
1658
1659 /* Go to the lowest leftmost znode in the new sub-tree */
1660 while (znode->level > 0) {
1661 zbr = &znode->zbranch[0];
1662 child = zbr->znode;
1663 if (!child) {
1664 child = ubifs_load_znode(c, zbr, znode, 0);
1665 if (IS_ERR(child)) {
1666 err = PTR_ERR(child);
1667 goto out_unlock;
1668 }
1669 zbr->znode = child;
1670 }
1671 znode = child;
1672 }
1673 }
1674
1675 mutex_unlock(&c->tnc_mutex);
1676 return 0;
1677
1678 out_dump:
1679 if (znode->parent)
1680 zbr = &znode->parent->zbranch[znode->iip];
1681 else
1682 zbr = &c->zroot;
1683 ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1684 ubifs_dump_znode(c, znode);
1685 out_unlock:
1686 mutex_unlock(&c->tnc_mutex);
1687 return err;
1688 }
1689
1690 /**
1691 * add_size - add znode size to partially calculated index size.
1692 * @c: UBIFS file-system description object
1693 * @znode: znode to add size for
1694 * @priv: partially calculated index size
1695 *
1696 * This is a helper function for 'dbg_check_idx_size()' which is called for
1697 * every indexing node and adds its size to the 'long long' variable pointed to
1698 * by @priv.
1699 */
add_size(struct ubifs_info * c,struct ubifs_znode * znode,void * priv)1700 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1701 {
1702 long long *idx_size = priv;
1703 int add;
1704
1705 add = ubifs_idx_node_sz(c, znode->child_cnt);
1706 add = ALIGN(add, 8);
1707 *idx_size += add;
1708 return 0;
1709 }
1710
1711 /**
1712 * dbg_check_idx_size - check index size.
1713 * @c: UBIFS file-system description object
1714 * @idx_size: size to check
1715 *
1716 * This function walks the UBIFS index, calculates its size and checks that the
1717 * size is equivalent to @idx_size. Returns zero in case of success and a
1718 * negative error code in case of failure.
1719 */
dbg_check_idx_size(struct ubifs_info * c,long long idx_size)1720 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1721 {
1722 int err;
1723 long long calc = 0;
1724
1725 if (!dbg_is_chk_index(c))
1726 return 0;
1727
1728 err = dbg_walk_index(c, NULL, add_size, &calc);
1729 if (err) {
1730 ubifs_err(c, "error %d while walking the index", err);
1731 return err;
1732 }
1733
1734 if (calc != idx_size) {
1735 ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
1736 calc, idx_size);
1737 dump_stack();
1738 return -EINVAL;
1739 }
1740
1741 return 0;
1742 }
1743
1744 /**
1745 * struct fsck_inode - information about an inode used when checking the file-system.
1746 * @rb: link in the RB-tree of inodes
1747 * @inum: inode number
1748 * @mode: inode type, permissions, etc
1749 * @nlink: inode link count
1750 * @xattr_cnt: count of extended attributes
1751 * @references: how many directory/xattr entries refer this inode (calculated
1752 * while walking the index)
1753 * @calc_cnt: for directory inode count of child directories
1754 * @size: inode size (read from on-flash inode)
1755 * @xattr_sz: summary size of all extended attributes (read from on-flash
1756 * inode)
1757 * @calc_sz: for directories calculated directory size
1758 * @calc_xcnt: count of extended attributes
1759 * @calc_xsz: calculated summary size of all extended attributes
1760 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1761 * inode (read from on-flash inode)
1762 * @calc_xnms: calculated sum of lengths of all extended attribute names
1763 */
1764 struct fsck_inode {
1765 struct rb_node rb;
1766 ino_t inum;
1767 umode_t mode;
1768 unsigned int nlink;
1769 unsigned int xattr_cnt;
1770 int references;
1771 int calc_cnt;
1772 long long size;
1773 unsigned int xattr_sz;
1774 long long calc_sz;
1775 long long calc_xcnt;
1776 long long calc_xsz;
1777 unsigned int xattr_nms;
1778 long long calc_xnms;
1779 };
1780
1781 /**
1782 * struct fsck_data - private FS checking information.
1783 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1784 */
1785 struct fsck_data {
1786 struct rb_root inodes;
1787 };
1788
1789 /**
1790 * add_inode - add inode information to RB-tree of inodes.
1791 * @c: UBIFS file-system description object
1792 * @fsckd: FS checking information
1793 * @ino: raw UBIFS inode to add
1794 *
1795 * This is a helper function for 'check_leaf()' which adds information about
1796 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1797 * case of success and a negative error code in case of failure.
1798 */
add_inode(struct ubifs_info * c,struct fsck_data * fsckd,struct ubifs_ino_node * ino)1799 static struct fsck_inode *add_inode(struct ubifs_info *c,
1800 struct fsck_data *fsckd,
1801 struct ubifs_ino_node *ino)
1802 {
1803 struct rb_node **p, *parent = NULL;
1804 struct fsck_inode *fscki;
1805 ino_t inum = key_inum_flash(c, &ino->key);
1806 struct inode *inode;
1807 struct ubifs_inode *ui;
1808
1809 p = &fsckd->inodes.rb_node;
1810 while (*p) {
1811 parent = *p;
1812 fscki = rb_entry(parent, struct fsck_inode, rb);
1813 if (inum < fscki->inum)
1814 p = &(*p)->rb_left;
1815 else if (inum > fscki->inum)
1816 p = &(*p)->rb_right;
1817 else
1818 return fscki;
1819 }
1820
1821 if (inum > c->highest_inum) {
1822 ubifs_err(c, "too high inode number, max. is %lu",
1823 (unsigned long)c->highest_inum);
1824 return ERR_PTR(-EINVAL);
1825 }
1826
1827 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1828 if (!fscki)
1829 return ERR_PTR(-ENOMEM);
1830
1831 inode = ilookup(c->vfs_sb, inum);
1832
1833 fscki->inum = inum;
1834 /*
1835 * If the inode is present in the VFS inode cache, use it instead of
1836 * the on-flash inode which might be out-of-date. E.g., the size might
1837 * be out-of-date. If we do not do this, the following may happen, for
1838 * example:
1839 * 1. A power cut happens
1840 * 2. We mount the file-system R/O, the replay process fixes up the
1841 * inode size in the VFS cache, but on on-flash.
1842 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1843 * size.
1844 */
1845 if (!inode) {
1846 fscki->nlink = le32_to_cpu(ino->nlink);
1847 fscki->size = le64_to_cpu(ino->size);
1848 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1849 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1850 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1851 fscki->mode = le32_to_cpu(ino->mode);
1852 } else {
1853 ui = ubifs_inode(inode);
1854 fscki->nlink = inode->i_nlink;
1855 fscki->size = inode->i_size;
1856 fscki->xattr_cnt = ui->xattr_cnt;
1857 fscki->xattr_sz = ui->xattr_size;
1858 fscki->xattr_nms = ui->xattr_names;
1859 fscki->mode = inode->i_mode;
1860 iput(inode);
1861 }
1862
1863 if (S_ISDIR(fscki->mode)) {
1864 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1865 fscki->calc_cnt = 2;
1866 }
1867
1868 rb_link_node(&fscki->rb, parent, p);
1869 rb_insert_color(&fscki->rb, &fsckd->inodes);
1870
1871 return fscki;
1872 }
1873
1874 /**
1875 * search_inode - search inode in the RB-tree of inodes.
1876 * @fsckd: FS checking information
1877 * @inum: inode number to search
1878 *
1879 * This is a helper function for 'check_leaf()' which searches inode @inum in
1880 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1881 * the inode was not found.
1882 */
search_inode(struct fsck_data * fsckd,ino_t inum)1883 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1884 {
1885 struct rb_node *p;
1886 struct fsck_inode *fscki;
1887
1888 p = fsckd->inodes.rb_node;
1889 while (p) {
1890 fscki = rb_entry(p, struct fsck_inode, rb);
1891 if (inum < fscki->inum)
1892 p = p->rb_left;
1893 else if (inum > fscki->inum)
1894 p = p->rb_right;
1895 else
1896 return fscki;
1897 }
1898 return NULL;
1899 }
1900
1901 /**
1902 * read_add_inode - read inode node and add it to RB-tree of inodes.
1903 * @c: UBIFS file-system description object
1904 * @fsckd: FS checking information
1905 * @inum: inode number to read
1906 *
1907 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1908 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1909 * information pointer in case of success and a negative error code in case of
1910 * failure.
1911 */
read_add_inode(struct ubifs_info * c,struct fsck_data * fsckd,ino_t inum)1912 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1913 struct fsck_data *fsckd, ino_t inum)
1914 {
1915 int n, err;
1916 union ubifs_key key;
1917 struct ubifs_znode *znode;
1918 struct ubifs_zbranch *zbr;
1919 struct ubifs_ino_node *ino;
1920 struct fsck_inode *fscki;
1921
1922 fscki = search_inode(fsckd, inum);
1923 if (fscki)
1924 return fscki;
1925
1926 ino_key_init(c, &key, inum);
1927 err = ubifs_lookup_level0(c, &key, &znode, &n);
1928 if (!err) {
1929 ubifs_err(c, "inode %lu not found in index", (unsigned long)inum);
1930 return ERR_PTR(-ENOENT);
1931 } else if (err < 0) {
1932 ubifs_err(c, "error %d while looking up inode %lu",
1933 err, (unsigned long)inum);
1934 return ERR_PTR(err);
1935 }
1936
1937 zbr = &znode->zbranch[n];
1938 if (zbr->len < UBIFS_INO_NODE_SZ) {
1939 ubifs_err(c, "bad node %lu node length %d",
1940 (unsigned long)inum, zbr->len);
1941 return ERR_PTR(-EINVAL);
1942 }
1943
1944 ino = kmalloc(zbr->len, GFP_NOFS);
1945 if (!ino)
1946 return ERR_PTR(-ENOMEM);
1947
1948 err = ubifs_tnc_read_node(c, zbr, ino);
1949 if (err) {
1950 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
1951 zbr->lnum, zbr->offs, err);
1952 kfree(ino);
1953 return ERR_PTR(err);
1954 }
1955
1956 fscki = add_inode(c, fsckd, ino);
1957 kfree(ino);
1958 if (IS_ERR(fscki)) {
1959 ubifs_err(c, "error %ld while adding inode %lu node",
1960 PTR_ERR(fscki), (unsigned long)inum);
1961 return fscki;
1962 }
1963
1964 return fscki;
1965 }
1966
1967 /**
1968 * check_leaf - check leaf node.
1969 * @c: UBIFS file-system description object
1970 * @zbr: zbranch of the leaf node to check
1971 * @priv: FS checking information
1972 *
1973 * This is a helper function for 'dbg_check_filesystem()' which is called for
1974 * every single leaf node while walking the indexing tree. It checks that the
1975 * leaf node referred from the indexing tree exists, has correct CRC, and does
1976 * some other basic validation. This function is also responsible for building
1977 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1978 * calculates reference count, size, etc for each inode in order to later
1979 * compare them to the information stored inside the inodes and detect possible
1980 * inconsistencies. Returns zero in case of success and a negative error code
1981 * in case of failure.
1982 */
check_leaf(struct ubifs_info * c,struct ubifs_zbranch * zbr,void * priv)1983 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1984 void *priv)
1985 {
1986 ino_t inum;
1987 void *node;
1988 struct ubifs_ch *ch;
1989 int err, type = key_type(c, &zbr->key);
1990 struct fsck_inode *fscki;
1991
1992 if (zbr->len < UBIFS_CH_SZ) {
1993 ubifs_err(c, "bad leaf length %d (LEB %d:%d)",
1994 zbr->len, zbr->lnum, zbr->offs);
1995 return -EINVAL;
1996 }
1997
1998 node = kmalloc(zbr->len, GFP_NOFS);
1999 if (!node)
2000 return -ENOMEM;
2001
2002 err = ubifs_tnc_read_node(c, zbr, node);
2003 if (err) {
2004 ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d",
2005 zbr->lnum, zbr->offs, err);
2006 goto out_free;
2007 }
2008
2009 /* If this is an inode node, add it to RB-tree of inodes */
2010 if (type == UBIFS_INO_KEY) {
2011 fscki = add_inode(c, priv, node);
2012 if (IS_ERR(fscki)) {
2013 err = PTR_ERR(fscki);
2014 ubifs_err(c, "error %d while adding inode node", err);
2015 goto out_dump;
2016 }
2017 goto out;
2018 }
2019
2020 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2021 type != UBIFS_DATA_KEY) {
2022 ubifs_err(c, "unexpected node type %d at LEB %d:%d",
2023 type, zbr->lnum, zbr->offs);
2024 err = -EINVAL;
2025 goto out_free;
2026 }
2027
2028 ch = node;
2029 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2030 ubifs_err(c, "too high sequence number, max. is %llu",
2031 c->max_sqnum);
2032 err = -EINVAL;
2033 goto out_dump;
2034 }
2035
2036 if (type == UBIFS_DATA_KEY) {
2037 long long blk_offs;
2038 struct ubifs_data_node *dn = node;
2039
2040 ubifs_assert(c, zbr->len >= UBIFS_DATA_NODE_SZ);
2041
2042 /*
2043 * Search the inode node this data node belongs to and insert
2044 * it to the RB-tree of inodes.
2045 */
2046 inum = key_inum_flash(c, &dn->key);
2047 fscki = read_add_inode(c, priv, inum);
2048 if (IS_ERR(fscki)) {
2049 err = PTR_ERR(fscki);
2050 ubifs_err(c, "error %d while processing data node and trying to find inode node %lu",
2051 err, (unsigned long)inum);
2052 goto out_dump;
2053 }
2054
2055 /* Make sure the data node is within inode size */
2056 blk_offs = key_block_flash(c, &dn->key);
2057 blk_offs <<= UBIFS_BLOCK_SHIFT;
2058 blk_offs += le32_to_cpu(dn->size);
2059 if (blk_offs > fscki->size) {
2060 ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld",
2061 zbr->lnum, zbr->offs, fscki->size);
2062 err = -EINVAL;
2063 goto out_dump;
2064 }
2065 } else {
2066 int nlen;
2067 struct ubifs_dent_node *dent = node;
2068 struct fsck_inode *fscki1;
2069
2070 ubifs_assert(c, zbr->len >= UBIFS_DENT_NODE_SZ);
2071
2072 err = ubifs_validate_entry(c, dent);
2073 if (err)
2074 goto out_dump;
2075
2076 /*
2077 * Search the inode node this entry refers to and the parent
2078 * inode node and insert them to the RB-tree of inodes.
2079 */
2080 inum = le64_to_cpu(dent->inum);
2081 fscki = read_add_inode(c, priv, inum);
2082 if (IS_ERR(fscki)) {
2083 err = PTR_ERR(fscki);
2084 ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu",
2085 err, (unsigned long)inum);
2086 goto out_dump;
2087 }
2088
2089 /* Count how many direntries or xentries refers this inode */
2090 fscki->references += 1;
2091
2092 inum = key_inum_flash(c, &dent->key);
2093 fscki1 = read_add_inode(c, priv, inum);
2094 if (IS_ERR(fscki1)) {
2095 err = PTR_ERR(fscki1);
2096 ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu",
2097 err, (unsigned long)inum);
2098 goto out_dump;
2099 }
2100
2101 nlen = le16_to_cpu(dent->nlen);
2102 if (type == UBIFS_XENT_KEY) {
2103 fscki1->calc_xcnt += 1;
2104 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2105 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2106 fscki1->calc_xnms += nlen;
2107 } else {
2108 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2109 if (dent->type == UBIFS_ITYPE_DIR)
2110 fscki1->calc_cnt += 1;
2111 }
2112 }
2113
2114 out:
2115 kfree(node);
2116 return 0;
2117
2118 out_dump:
2119 ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2120 ubifs_dump_node(c, node);
2121 out_free:
2122 kfree(node);
2123 return err;
2124 }
2125
2126 /**
2127 * free_inodes - free RB-tree of inodes.
2128 * @fsckd: FS checking information
2129 */
free_inodes(struct fsck_data * fsckd)2130 static void free_inodes(struct fsck_data *fsckd)
2131 {
2132 struct fsck_inode *fscki, *n;
2133
2134 rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2135 kfree(fscki);
2136 }
2137
2138 /**
2139 * check_inodes - checks all inodes.
2140 * @c: UBIFS file-system description object
2141 * @fsckd: FS checking information
2142 *
2143 * This is a helper function for 'dbg_check_filesystem()' which walks the
2144 * RB-tree of inodes after the index scan has been finished, and checks that
2145 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2146 * %-EINVAL if not, and a negative error code in case of failure.
2147 */
check_inodes(struct ubifs_info * c,struct fsck_data * fsckd)2148 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2149 {
2150 int n, err;
2151 union ubifs_key key;
2152 struct ubifs_znode *znode;
2153 struct ubifs_zbranch *zbr;
2154 struct ubifs_ino_node *ino;
2155 struct fsck_inode *fscki;
2156 struct rb_node *this = rb_first(&fsckd->inodes);
2157
2158 while (this) {
2159 fscki = rb_entry(this, struct fsck_inode, rb);
2160 this = rb_next(this);
2161
2162 if (S_ISDIR(fscki->mode)) {
2163 /*
2164 * Directories have to have exactly one reference (they
2165 * cannot have hardlinks), although root inode is an
2166 * exception.
2167 */
2168 if (fscki->inum != UBIFS_ROOT_INO &&
2169 fscki->references != 1) {
2170 ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1",
2171 (unsigned long)fscki->inum,
2172 fscki->references);
2173 goto out_dump;
2174 }
2175 if (fscki->inum == UBIFS_ROOT_INO &&
2176 fscki->references != 0) {
2177 ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it",
2178 (unsigned long)fscki->inum,
2179 fscki->references);
2180 goto out_dump;
2181 }
2182 if (fscki->calc_sz != fscki->size) {
2183 ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld",
2184 (unsigned long)fscki->inum,
2185 fscki->size, fscki->calc_sz);
2186 goto out_dump;
2187 }
2188 if (fscki->calc_cnt != fscki->nlink) {
2189 ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d",
2190 (unsigned long)fscki->inum,
2191 fscki->nlink, fscki->calc_cnt);
2192 goto out_dump;
2193 }
2194 } else {
2195 if (fscki->references != fscki->nlink) {
2196 ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d",
2197 (unsigned long)fscki->inum,
2198 fscki->nlink, fscki->references);
2199 goto out_dump;
2200 }
2201 }
2202 if (fscki->xattr_sz != fscki->calc_xsz) {
2203 ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld",
2204 (unsigned long)fscki->inum, fscki->xattr_sz,
2205 fscki->calc_xsz);
2206 goto out_dump;
2207 }
2208 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2209 ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld",
2210 (unsigned long)fscki->inum,
2211 fscki->xattr_cnt, fscki->calc_xcnt);
2212 goto out_dump;
2213 }
2214 if (fscki->xattr_nms != fscki->calc_xnms) {
2215 ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld",
2216 (unsigned long)fscki->inum, fscki->xattr_nms,
2217 fscki->calc_xnms);
2218 goto out_dump;
2219 }
2220 }
2221
2222 return 0;
2223
2224 out_dump:
2225 /* Read the bad inode and dump it */
2226 ino_key_init(c, &key, fscki->inum);
2227 err = ubifs_lookup_level0(c, &key, &znode, &n);
2228 if (!err) {
2229 ubifs_err(c, "inode %lu not found in index",
2230 (unsigned long)fscki->inum);
2231 return -ENOENT;
2232 } else if (err < 0) {
2233 ubifs_err(c, "error %d while looking up inode %lu",
2234 err, (unsigned long)fscki->inum);
2235 return err;
2236 }
2237
2238 zbr = &znode->zbranch[n];
2239 ino = kmalloc(zbr->len, GFP_NOFS);
2240 if (!ino)
2241 return -ENOMEM;
2242
2243 err = ubifs_tnc_read_node(c, zbr, ino);
2244 if (err) {
2245 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
2246 zbr->lnum, zbr->offs, err);
2247 kfree(ino);
2248 return err;
2249 }
2250
2251 ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d",
2252 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2253 ubifs_dump_node(c, ino);
2254 kfree(ino);
2255 return -EINVAL;
2256 }
2257
2258 /**
2259 * dbg_check_filesystem - check the file-system.
2260 * @c: UBIFS file-system description object
2261 *
2262 * This function checks the file system, namely:
2263 * o makes sure that all leaf nodes exist and their CRCs are correct;
2264 * o makes sure inode nlink, size, xattr size/count are correct (for all
2265 * inodes).
2266 *
2267 * The function reads whole indexing tree and all nodes, so it is pretty
2268 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2269 * not, and a negative error code in case of failure.
2270 */
dbg_check_filesystem(struct ubifs_info * c)2271 int dbg_check_filesystem(struct ubifs_info *c)
2272 {
2273 int err;
2274 struct fsck_data fsckd;
2275
2276 if (!dbg_is_chk_fs(c))
2277 return 0;
2278
2279 fsckd.inodes = RB_ROOT;
2280 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2281 if (err)
2282 goto out_free;
2283
2284 err = check_inodes(c, &fsckd);
2285 if (err)
2286 goto out_free;
2287
2288 free_inodes(&fsckd);
2289 return 0;
2290
2291 out_free:
2292 ubifs_err(c, "file-system check failed with error %d", err);
2293 dump_stack();
2294 free_inodes(&fsckd);
2295 return err;
2296 }
2297
2298 /**
2299 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2300 * @c: UBIFS file-system description object
2301 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2302 *
2303 * This function returns zero if the list of data nodes is sorted correctly,
2304 * and %-EINVAL if not.
2305 */
dbg_check_data_nodes_order(struct ubifs_info * c,struct list_head * head)2306 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2307 {
2308 struct list_head *cur;
2309 struct ubifs_scan_node *sa, *sb;
2310
2311 if (!dbg_is_chk_gen(c))
2312 return 0;
2313
2314 for (cur = head->next; cur->next != head; cur = cur->next) {
2315 ino_t inuma, inumb;
2316 uint32_t blka, blkb;
2317
2318 cond_resched();
2319 sa = container_of(cur, struct ubifs_scan_node, list);
2320 sb = container_of(cur->next, struct ubifs_scan_node, list);
2321
2322 if (sa->type != UBIFS_DATA_NODE) {
2323 ubifs_err(c, "bad node type %d", sa->type);
2324 ubifs_dump_node(c, sa->node);
2325 return -EINVAL;
2326 }
2327 if (sb->type != UBIFS_DATA_NODE) {
2328 ubifs_err(c, "bad node type %d", sb->type);
2329 ubifs_dump_node(c, sb->node);
2330 return -EINVAL;
2331 }
2332
2333 inuma = key_inum(c, &sa->key);
2334 inumb = key_inum(c, &sb->key);
2335
2336 if (inuma < inumb)
2337 continue;
2338 if (inuma > inumb) {
2339 ubifs_err(c, "larger inum %lu goes before inum %lu",
2340 (unsigned long)inuma, (unsigned long)inumb);
2341 goto error_dump;
2342 }
2343
2344 blka = key_block(c, &sa->key);
2345 blkb = key_block(c, &sb->key);
2346
2347 if (blka > blkb) {
2348 ubifs_err(c, "larger block %u goes before %u", blka, blkb);
2349 goto error_dump;
2350 }
2351 if (blka == blkb) {
2352 ubifs_err(c, "two data nodes for the same block");
2353 goto error_dump;
2354 }
2355 }
2356
2357 return 0;
2358
2359 error_dump:
2360 ubifs_dump_node(c, sa->node);
2361 ubifs_dump_node(c, sb->node);
2362 return -EINVAL;
2363 }
2364
2365 /**
2366 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2367 * @c: UBIFS file-system description object
2368 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2369 *
2370 * This function returns zero if the list of non-data nodes is sorted correctly,
2371 * and %-EINVAL if not.
2372 */
dbg_check_nondata_nodes_order(struct ubifs_info * c,struct list_head * head)2373 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2374 {
2375 struct list_head *cur;
2376 struct ubifs_scan_node *sa, *sb;
2377
2378 if (!dbg_is_chk_gen(c))
2379 return 0;
2380
2381 for (cur = head->next; cur->next != head; cur = cur->next) {
2382 ino_t inuma, inumb;
2383 uint32_t hasha, hashb;
2384
2385 cond_resched();
2386 sa = container_of(cur, struct ubifs_scan_node, list);
2387 sb = container_of(cur->next, struct ubifs_scan_node, list);
2388
2389 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2390 sa->type != UBIFS_XENT_NODE) {
2391 ubifs_err(c, "bad node type %d", sa->type);
2392 ubifs_dump_node(c, sa->node);
2393 return -EINVAL;
2394 }
2395 if (sb->type != UBIFS_INO_NODE && sb->type != UBIFS_DENT_NODE &&
2396 sb->type != UBIFS_XENT_NODE) {
2397 ubifs_err(c, "bad node type %d", sb->type);
2398 ubifs_dump_node(c, sb->node);
2399 return -EINVAL;
2400 }
2401
2402 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2403 ubifs_err(c, "non-inode node goes before inode node");
2404 goto error_dump;
2405 }
2406
2407 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2408 continue;
2409
2410 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2411 /* Inode nodes are sorted in descending size order */
2412 if (sa->len < sb->len) {
2413 ubifs_err(c, "smaller inode node goes first");
2414 goto error_dump;
2415 }
2416 continue;
2417 }
2418
2419 /*
2420 * This is either a dentry or xentry, which should be sorted in
2421 * ascending (parent ino, hash) order.
2422 */
2423 inuma = key_inum(c, &sa->key);
2424 inumb = key_inum(c, &sb->key);
2425
2426 if (inuma < inumb)
2427 continue;
2428 if (inuma > inumb) {
2429 ubifs_err(c, "larger inum %lu goes before inum %lu",
2430 (unsigned long)inuma, (unsigned long)inumb);
2431 goto error_dump;
2432 }
2433
2434 hasha = key_block(c, &sa->key);
2435 hashb = key_block(c, &sb->key);
2436
2437 if (hasha > hashb) {
2438 ubifs_err(c, "larger hash %u goes before %u",
2439 hasha, hashb);
2440 goto error_dump;
2441 }
2442 }
2443
2444 return 0;
2445
2446 error_dump:
2447 ubifs_msg(c, "dumping first node");
2448 ubifs_dump_node(c, sa->node);
2449 ubifs_msg(c, "dumping second node");
2450 ubifs_dump_node(c, sb->node);
2451 return -EINVAL;
2452 return 0;
2453 }
2454
chance(unsigned int n,unsigned int out_of)2455 static inline int chance(unsigned int n, unsigned int out_of)
2456 {
2457 return !!((prandom_u32() % out_of) + 1 <= n);
2458
2459 }
2460
power_cut_emulated(struct ubifs_info * c,int lnum,int write)2461 static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2462 {
2463 struct ubifs_debug_info *d = c->dbg;
2464
2465 ubifs_assert(c, dbg_is_tst_rcvry(c));
2466
2467 if (!d->pc_cnt) {
2468 /* First call - decide delay to the power cut */
2469 if (chance(1, 2)) {
2470 unsigned long delay;
2471
2472 if (chance(1, 2)) {
2473 d->pc_delay = 1;
2474 /* Fail within 1 minute */
2475 delay = prandom_u32() % 60000;
2476 d->pc_timeout = jiffies;
2477 d->pc_timeout += msecs_to_jiffies(delay);
2478 ubifs_warn(c, "failing after %lums", delay);
2479 } else {
2480 d->pc_delay = 2;
2481 delay = prandom_u32() % 10000;
2482 /* Fail within 10000 operations */
2483 d->pc_cnt_max = delay;
2484 ubifs_warn(c, "failing after %lu calls", delay);
2485 }
2486 }
2487
2488 d->pc_cnt += 1;
2489 }
2490
2491 /* Determine if failure delay has expired */
2492 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2493 return 0;
2494 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2495 return 0;
2496
2497 if (lnum == UBIFS_SB_LNUM) {
2498 if (write && chance(1, 2))
2499 return 0;
2500 if (chance(19, 20))
2501 return 0;
2502 ubifs_warn(c, "failing in super block LEB %d", lnum);
2503 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2504 if (chance(19, 20))
2505 return 0;
2506 ubifs_warn(c, "failing in master LEB %d", lnum);
2507 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2508 if (write && chance(99, 100))
2509 return 0;
2510 if (chance(399, 400))
2511 return 0;
2512 ubifs_warn(c, "failing in log LEB %d", lnum);
2513 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2514 if (write && chance(7, 8))
2515 return 0;
2516 if (chance(19, 20))
2517 return 0;
2518 ubifs_warn(c, "failing in LPT LEB %d", lnum);
2519 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2520 if (write && chance(1, 2))
2521 return 0;
2522 if (chance(9, 10))
2523 return 0;
2524 ubifs_warn(c, "failing in orphan LEB %d", lnum);
2525 } else if (lnum == c->ihead_lnum) {
2526 if (chance(99, 100))
2527 return 0;
2528 ubifs_warn(c, "failing in index head LEB %d", lnum);
2529 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2530 if (chance(9, 10))
2531 return 0;
2532 ubifs_warn(c, "failing in GC head LEB %d", lnum);
2533 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2534 !ubifs_search_bud(c, lnum)) {
2535 if (chance(19, 20))
2536 return 0;
2537 ubifs_warn(c, "failing in non-bud LEB %d", lnum);
2538 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2539 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2540 if (chance(999, 1000))
2541 return 0;
2542 ubifs_warn(c, "failing in bud LEB %d commit running", lnum);
2543 } else {
2544 if (chance(9999, 10000))
2545 return 0;
2546 ubifs_warn(c, "failing in bud LEB %d commit not running", lnum);
2547 }
2548
2549 d->pc_happened = 1;
2550 ubifs_warn(c, "========== Power cut emulated ==========");
2551 dump_stack();
2552 return 1;
2553 }
2554
corrupt_data(const struct ubifs_info * c,const void * buf,unsigned int len)2555 static int corrupt_data(const struct ubifs_info *c, const void *buf,
2556 unsigned int len)
2557 {
2558 unsigned int from, to, ffs = chance(1, 2);
2559 unsigned char *p = (void *)buf;
2560
2561 from = prandom_u32() % len;
2562 /* Corruption span max to end of write unit */
2563 to = min(len, ALIGN(from + 1, c->max_write_size));
2564
2565 ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1,
2566 ffs ? "0xFFs" : "random data");
2567
2568 if (ffs)
2569 memset(p + from, 0xFF, to - from);
2570 else
2571 prandom_bytes(p + from, to - from);
2572
2573 return to;
2574 }
2575
dbg_leb_write(struct ubifs_info * c,int lnum,const void * buf,int offs,int len)2576 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2577 int offs, int len)
2578 {
2579 int err, failing;
2580
2581 if (dbg_is_power_cut(c))
2582 return -EROFS;
2583
2584 failing = power_cut_emulated(c, lnum, 1);
2585 if (failing) {
2586 len = corrupt_data(c, buf, len);
2587 ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2588 len, lnum, offs);
2589 }
2590 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2591 if (err)
2592 return err;
2593 if (failing)
2594 return -EROFS;
2595 return 0;
2596 }
2597
dbg_leb_change(struct ubifs_info * c,int lnum,const void * buf,int len)2598 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2599 int len)
2600 {
2601 int err;
2602
2603 if (dbg_is_power_cut(c))
2604 return -EROFS;
2605 if (power_cut_emulated(c, lnum, 1))
2606 return -EROFS;
2607 err = ubi_leb_change(c->ubi, lnum, buf, len);
2608 if (err)
2609 return err;
2610 if (power_cut_emulated(c, lnum, 1))
2611 return -EROFS;
2612 return 0;
2613 }
2614
dbg_leb_unmap(struct ubifs_info * c,int lnum)2615 int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2616 {
2617 int err;
2618
2619 if (dbg_is_power_cut(c))
2620 return -EROFS;
2621 if (power_cut_emulated(c, lnum, 0))
2622 return -EROFS;
2623 err = ubi_leb_unmap(c->ubi, lnum);
2624 if (err)
2625 return err;
2626 if (power_cut_emulated(c, lnum, 0))
2627 return -EROFS;
2628 return 0;
2629 }
2630
dbg_leb_map(struct ubifs_info * c,int lnum)2631 int dbg_leb_map(struct ubifs_info *c, int lnum)
2632 {
2633 int err;
2634
2635 if (dbg_is_power_cut(c))
2636 return -EROFS;
2637 if (power_cut_emulated(c, lnum, 0))
2638 return -EROFS;
2639 err = ubi_leb_map(c->ubi, lnum);
2640 if (err)
2641 return err;
2642 if (power_cut_emulated(c, lnum, 0))
2643 return -EROFS;
2644 return 0;
2645 }
2646
2647 /*
2648 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2649 * contain the stuff specific to particular file-system mounts.
2650 */
2651 static struct dentry *dfs_rootdir;
2652
dfs_file_open(struct inode * inode,struct file * file)2653 static int dfs_file_open(struct inode *inode, struct file *file)
2654 {
2655 file->private_data = inode->i_private;
2656 return nonseekable_open(inode, file);
2657 }
2658
2659 /**
2660 * provide_user_output - provide output to the user reading a debugfs file.
2661 * @val: boolean value for the answer
2662 * @u: the buffer to store the answer at
2663 * @count: size of the buffer
2664 * @ppos: position in the @u output buffer
2665 *
2666 * This is a simple helper function which stores @val boolean value in the user
2667 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2668 * bytes written to @u in case of success and a negative error code in case of
2669 * failure.
2670 */
provide_user_output(int val,char __user * u,size_t count,loff_t * ppos)2671 static int provide_user_output(int val, char __user *u, size_t count,
2672 loff_t *ppos)
2673 {
2674 char buf[3];
2675
2676 if (val)
2677 buf[0] = '1';
2678 else
2679 buf[0] = '0';
2680 buf[1] = '\n';
2681 buf[2] = 0x00;
2682
2683 return simple_read_from_buffer(u, count, ppos, buf, 2);
2684 }
2685
dfs_file_read(struct file * file,char __user * u,size_t count,loff_t * ppos)2686 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2687 loff_t *ppos)
2688 {
2689 struct dentry *dent = file->f_path.dentry;
2690 struct ubifs_info *c = file->private_data;
2691 struct ubifs_debug_info *d = c->dbg;
2692 int val;
2693
2694 if (dent == d->dfs_chk_gen)
2695 val = d->chk_gen;
2696 else if (dent == d->dfs_chk_index)
2697 val = d->chk_index;
2698 else if (dent == d->dfs_chk_orph)
2699 val = d->chk_orph;
2700 else if (dent == d->dfs_chk_lprops)
2701 val = d->chk_lprops;
2702 else if (dent == d->dfs_chk_fs)
2703 val = d->chk_fs;
2704 else if (dent == d->dfs_tst_rcvry)
2705 val = d->tst_rcvry;
2706 else if (dent == d->dfs_ro_error)
2707 val = c->ro_error;
2708 else
2709 return -EINVAL;
2710
2711 return provide_user_output(val, u, count, ppos);
2712 }
2713
2714 /**
2715 * interpret_user_input - interpret user debugfs file input.
2716 * @u: user-provided buffer with the input
2717 * @count: buffer size
2718 *
2719 * This is a helper function which interpret user input to a boolean UBIFS
2720 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2721 * in case of failure.
2722 */
interpret_user_input(const char __user * u,size_t count)2723 static int interpret_user_input(const char __user *u, size_t count)
2724 {
2725 size_t buf_size;
2726 char buf[8];
2727
2728 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2729 if (copy_from_user(buf, u, buf_size))
2730 return -EFAULT;
2731
2732 if (buf[0] == '1')
2733 return 1;
2734 else if (buf[0] == '0')
2735 return 0;
2736
2737 return -EINVAL;
2738 }
2739
dfs_file_write(struct file * file,const char __user * u,size_t count,loff_t * ppos)2740 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2741 size_t count, loff_t *ppos)
2742 {
2743 struct ubifs_info *c = file->private_data;
2744 struct ubifs_debug_info *d = c->dbg;
2745 struct dentry *dent = file->f_path.dentry;
2746 int val;
2747
2748 /*
2749 * TODO: this is racy - the file-system might have already been
2750 * unmounted and we'd oops in this case. The plan is to fix it with
2751 * help of 'iterate_supers_type()' which we should have in v3.0: when
2752 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2753 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2754 * superblocks and fine the one with the same UUID, and take the
2755 * locking right.
2756 *
2757 * The other way to go suggested by Al Viro is to create a separate
2758 * 'ubifs-debug' file-system instead.
2759 */
2760 if (file->f_path.dentry == d->dfs_dump_lprops) {
2761 ubifs_dump_lprops(c);
2762 return count;
2763 }
2764 if (file->f_path.dentry == d->dfs_dump_budg) {
2765 ubifs_dump_budg(c, &c->bi);
2766 return count;
2767 }
2768 if (file->f_path.dentry == d->dfs_dump_tnc) {
2769 mutex_lock(&c->tnc_mutex);
2770 ubifs_dump_tnc(c);
2771 mutex_unlock(&c->tnc_mutex);
2772 return count;
2773 }
2774
2775 val = interpret_user_input(u, count);
2776 if (val < 0)
2777 return val;
2778
2779 if (dent == d->dfs_chk_gen)
2780 d->chk_gen = val;
2781 else if (dent == d->dfs_chk_index)
2782 d->chk_index = val;
2783 else if (dent == d->dfs_chk_orph)
2784 d->chk_orph = val;
2785 else if (dent == d->dfs_chk_lprops)
2786 d->chk_lprops = val;
2787 else if (dent == d->dfs_chk_fs)
2788 d->chk_fs = val;
2789 else if (dent == d->dfs_tst_rcvry)
2790 d->tst_rcvry = val;
2791 else if (dent == d->dfs_ro_error)
2792 c->ro_error = !!val;
2793 else
2794 return -EINVAL;
2795
2796 return count;
2797 }
2798
2799 static const struct file_operations dfs_fops = {
2800 .open = dfs_file_open,
2801 .read = dfs_file_read,
2802 .write = dfs_file_write,
2803 .owner = THIS_MODULE,
2804 .llseek = no_llseek,
2805 };
2806
2807 /**
2808 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2809 * @c: UBIFS file-system description object
2810 *
2811 * This function creates all debugfs files for this instance of UBIFS. Returns
2812 * zero in case of success and a negative error code in case of failure.
2813 *
2814 * Note, the only reason we have not merged this function with the
2815 * 'ubifs_debugging_init()' function is because it is better to initialize
2816 * debugfs interfaces at the very end of the mount process, and remove them at
2817 * the very beginning of the mount process.
2818 */
dbg_debugfs_init_fs(struct ubifs_info * c)2819 int dbg_debugfs_init_fs(struct ubifs_info *c)
2820 {
2821 int err, n;
2822 const char *fname;
2823 struct dentry *dent;
2824 struct ubifs_debug_info *d = c->dbg;
2825
2826 if (!IS_ENABLED(CONFIG_DEBUG_FS))
2827 return 0;
2828
2829 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2830 c->vi.ubi_num, c->vi.vol_id);
2831 if (n == UBIFS_DFS_DIR_LEN) {
2832 /* The array size is too small */
2833 fname = UBIFS_DFS_DIR_NAME;
2834 dent = ERR_PTR(-EINVAL);
2835 goto out;
2836 }
2837
2838 fname = d->dfs_dir_name;
2839 dent = debugfs_create_dir(fname, dfs_rootdir);
2840 if (IS_ERR_OR_NULL(dent))
2841 goto out;
2842 d->dfs_dir = dent;
2843
2844 fname = "dump_lprops";
2845 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2846 if (IS_ERR_OR_NULL(dent))
2847 goto out_remove;
2848 d->dfs_dump_lprops = dent;
2849
2850 fname = "dump_budg";
2851 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2852 if (IS_ERR_OR_NULL(dent))
2853 goto out_remove;
2854 d->dfs_dump_budg = dent;
2855
2856 fname = "dump_tnc";
2857 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2858 if (IS_ERR_OR_NULL(dent))
2859 goto out_remove;
2860 d->dfs_dump_tnc = dent;
2861
2862 fname = "chk_general";
2863 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2864 &dfs_fops);
2865 if (IS_ERR_OR_NULL(dent))
2866 goto out_remove;
2867 d->dfs_chk_gen = dent;
2868
2869 fname = "chk_index";
2870 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2871 &dfs_fops);
2872 if (IS_ERR_OR_NULL(dent))
2873 goto out_remove;
2874 d->dfs_chk_index = dent;
2875
2876 fname = "chk_orphans";
2877 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2878 &dfs_fops);
2879 if (IS_ERR_OR_NULL(dent))
2880 goto out_remove;
2881 d->dfs_chk_orph = dent;
2882
2883 fname = "chk_lprops";
2884 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2885 &dfs_fops);
2886 if (IS_ERR_OR_NULL(dent))
2887 goto out_remove;
2888 d->dfs_chk_lprops = dent;
2889
2890 fname = "chk_fs";
2891 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2892 &dfs_fops);
2893 if (IS_ERR_OR_NULL(dent))
2894 goto out_remove;
2895 d->dfs_chk_fs = dent;
2896
2897 fname = "tst_recovery";
2898 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2899 &dfs_fops);
2900 if (IS_ERR_OR_NULL(dent))
2901 goto out_remove;
2902 d->dfs_tst_rcvry = dent;
2903
2904 fname = "ro_error";
2905 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2906 &dfs_fops);
2907 if (IS_ERR_OR_NULL(dent))
2908 goto out_remove;
2909 d->dfs_ro_error = dent;
2910
2911 return 0;
2912
2913 out_remove:
2914 debugfs_remove_recursive(d->dfs_dir);
2915 out:
2916 err = dent ? PTR_ERR(dent) : -ENODEV;
2917 ubifs_err(c, "cannot create \"%s\" debugfs file or directory, error %d\n",
2918 fname, err);
2919 return err;
2920 }
2921
2922 /**
2923 * dbg_debugfs_exit_fs - remove all debugfs files.
2924 * @c: UBIFS file-system description object
2925 */
dbg_debugfs_exit_fs(struct ubifs_info * c)2926 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2927 {
2928 if (IS_ENABLED(CONFIG_DEBUG_FS))
2929 debugfs_remove_recursive(c->dbg->dfs_dir);
2930 }
2931
2932 struct ubifs_global_debug_info ubifs_dbg;
2933
2934 static struct dentry *dfs_chk_gen;
2935 static struct dentry *dfs_chk_index;
2936 static struct dentry *dfs_chk_orph;
2937 static struct dentry *dfs_chk_lprops;
2938 static struct dentry *dfs_chk_fs;
2939 static struct dentry *dfs_tst_rcvry;
2940
dfs_global_file_read(struct file * file,char __user * u,size_t count,loff_t * ppos)2941 static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2942 size_t count, loff_t *ppos)
2943 {
2944 struct dentry *dent = file->f_path.dentry;
2945 int val;
2946
2947 if (dent == dfs_chk_gen)
2948 val = ubifs_dbg.chk_gen;
2949 else if (dent == dfs_chk_index)
2950 val = ubifs_dbg.chk_index;
2951 else if (dent == dfs_chk_orph)
2952 val = ubifs_dbg.chk_orph;
2953 else if (dent == dfs_chk_lprops)
2954 val = ubifs_dbg.chk_lprops;
2955 else if (dent == dfs_chk_fs)
2956 val = ubifs_dbg.chk_fs;
2957 else if (dent == dfs_tst_rcvry)
2958 val = ubifs_dbg.tst_rcvry;
2959 else
2960 return -EINVAL;
2961
2962 return provide_user_output(val, u, count, ppos);
2963 }
2964
dfs_global_file_write(struct file * file,const char __user * u,size_t count,loff_t * ppos)2965 static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
2966 size_t count, loff_t *ppos)
2967 {
2968 struct dentry *dent = file->f_path.dentry;
2969 int val;
2970
2971 val = interpret_user_input(u, count);
2972 if (val < 0)
2973 return val;
2974
2975 if (dent == dfs_chk_gen)
2976 ubifs_dbg.chk_gen = val;
2977 else if (dent == dfs_chk_index)
2978 ubifs_dbg.chk_index = val;
2979 else if (dent == dfs_chk_orph)
2980 ubifs_dbg.chk_orph = val;
2981 else if (dent == dfs_chk_lprops)
2982 ubifs_dbg.chk_lprops = val;
2983 else if (dent == dfs_chk_fs)
2984 ubifs_dbg.chk_fs = val;
2985 else if (dent == dfs_tst_rcvry)
2986 ubifs_dbg.tst_rcvry = val;
2987 else
2988 return -EINVAL;
2989
2990 return count;
2991 }
2992
2993 static const struct file_operations dfs_global_fops = {
2994 .read = dfs_global_file_read,
2995 .write = dfs_global_file_write,
2996 .owner = THIS_MODULE,
2997 .llseek = no_llseek,
2998 };
2999
3000 /**
3001 * dbg_debugfs_init - initialize debugfs file-system.
3002 *
3003 * UBIFS uses debugfs file-system to expose various debugging knobs to
3004 * user-space. This function creates "ubifs" directory in the debugfs
3005 * file-system. Returns zero in case of success and a negative error code in
3006 * case of failure.
3007 */
dbg_debugfs_init(void)3008 int dbg_debugfs_init(void)
3009 {
3010 int err;
3011 const char *fname;
3012 struct dentry *dent;
3013
3014 if (!IS_ENABLED(CONFIG_DEBUG_FS))
3015 return 0;
3016
3017 fname = "ubifs";
3018 dent = debugfs_create_dir(fname, NULL);
3019 if (IS_ERR_OR_NULL(dent))
3020 goto out;
3021 dfs_rootdir = dent;
3022
3023 fname = "chk_general";
3024 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3025 &dfs_global_fops);
3026 if (IS_ERR_OR_NULL(dent))
3027 goto out_remove;
3028 dfs_chk_gen = dent;
3029
3030 fname = "chk_index";
3031 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3032 &dfs_global_fops);
3033 if (IS_ERR_OR_NULL(dent))
3034 goto out_remove;
3035 dfs_chk_index = dent;
3036
3037 fname = "chk_orphans";
3038 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3039 &dfs_global_fops);
3040 if (IS_ERR_OR_NULL(dent))
3041 goto out_remove;
3042 dfs_chk_orph = dent;
3043
3044 fname = "chk_lprops";
3045 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3046 &dfs_global_fops);
3047 if (IS_ERR_OR_NULL(dent))
3048 goto out_remove;
3049 dfs_chk_lprops = dent;
3050
3051 fname = "chk_fs";
3052 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3053 &dfs_global_fops);
3054 if (IS_ERR_OR_NULL(dent))
3055 goto out_remove;
3056 dfs_chk_fs = dent;
3057
3058 fname = "tst_recovery";
3059 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3060 &dfs_global_fops);
3061 if (IS_ERR_OR_NULL(dent))
3062 goto out_remove;
3063 dfs_tst_rcvry = dent;
3064
3065 return 0;
3066
3067 out_remove:
3068 debugfs_remove_recursive(dfs_rootdir);
3069 out:
3070 err = dent ? PTR_ERR(dent) : -ENODEV;
3071 pr_err("UBIFS error (pid %d): cannot create \"%s\" debugfs file or directory, error %d\n",
3072 current->pid, fname, err);
3073 return err;
3074 }
3075
3076 /**
3077 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3078 */
dbg_debugfs_exit(void)3079 void dbg_debugfs_exit(void)
3080 {
3081 if (IS_ENABLED(CONFIG_DEBUG_FS))
3082 debugfs_remove_recursive(dfs_rootdir);
3083 }
3084
ubifs_assert_failed(struct ubifs_info * c,const char * expr,const char * file,int line)3085 void ubifs_assert_failed(struct ubifs_info *c, const char *expr,
3086 const char *file, int line)
3087 {
3088 ubifs_err(c, "UBIFS assert failed: %s, in %s:%u", expr, file, line);
3089
3090 switch (c->assert_action) {
3091 case ASSACT_PANIC:
3092 BUG();
3093 break;
3094
3095 case ASSACT_RO:
3096 ubifs_ro_mode(c, -EINVAL);
3097 break;
3098
3099 case ASSACT_REPORT:
3100 default:
3101 dump_stack();
3102 break;
3103
3104 }
3105 }
3106
3107 /**
3108 * ubifs_debugging_init - initialize UBIFS debugging.
3109 * @c: UBIFS file-system description object
3110 *
3111 * This function initializes debugging-related data for the file system.
3112 * Returns zero in case of success and a negative error code in case of
3113 * failure.
3114 */
ubifs_debugging_init(struct ubifs_info * c)3115 int ubifs_debugging_init(struct ubifs_info *c)
3116 {
3117 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3118 if (!c->dbg)
3119 return -ENOMEM;
3120
3121 return 0;
3122 }
3123
3124 /**
3125 * ubifs_debugging_exit - free debugging data.
3126 * @c: UBIFS file-system description object
3127 */
ubifs_debugging_exit(struct ubifs_info * c)3128 void ubifs_debugging_exit(struct ubifs_info *c)
3129 {
3130 kfree(c->dbg);
3131 }
3132