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