Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (C) 2024, Huawei Technologies Co, Ltd.
4 : *
5 : * Authors: Zhihao Cheng <chengzhihao1@huawei.com>
6 : */
7 :
8 : #include <stdio.h>
9 : #include <stdlib.h>
10 : #include <getopt.h>
11 : #include <sys/stat.h>
12 :
13 : #include "linux_err.h"
14 : #include "bitops.h"
15 : #include "kmem.h"
16 : #include "crc32.h"
17 : #include "ubifs.h"
18 : #include "defs.h"
19 : #include "debug.h"
20 : #include "key.h"
21 : #include "misc.h"
22 : #include "fsck.ubifs.h"
23 :
24 : static void parse_node_header(int lnum, int offs, int len,
25 : unsigned long long sqnum,
26 : struct scanned_node *header)
27 : {
28 2734381987 : header->exist = true;
29 2734381987 : header->lnum = lnum;
30 2734381987 : header->offs = offs;
31 2734381987 : header->len = len;
32 2734381987 : header->sqnum = sqnum;
33 : }
34 :
35 : static inline bool inode_can_be_encrypted(struct ubifs_info *c,
36 : struct scanned_ino_node *ino_node)
37 : {
38 69007396 : if (!c->encrypted)
39 : return false;
40 :
41 69007396 : if (ino_node->is_xattr)
42 : return false;
43 :
44 : /* Only regular files, directories, and symlinks can be encrypted. */
45 69007396 : if (S_ISREG(ino_node->mode) || S_ISDIR(ino_node->mode) ||
46 : S_ISLNK(ino_node->mode))
47 : return true;
48 :
49 : return false;
50 : }
51 :
52 : /**
53 : * parse_ino_node - parse inode node and check it's validity.
54 : * @c: UBIFS file-system description object
55 : * @lnum: logical eraseblock number
56 : * @offs: the offset in LEB of the raw inode node
57 : * @node: raw node
58 : * @key: key of node scanned (if it has one)
59 : * @ino_node: node used to store raw inode information
60 : *
61 : * This function checks the raw inode information, and stores inode
62 : * information into @ino_node. Returns %true if the inode is valid,
63 : * otherwise %false is returned.
64 : */
65 245416385 : bool parse_ino_node(struct ubifs_info *c, int lnum, int offs, void *node,
66 : union ubifs_key *key, struct scanned_ino_node *ino_node)
67 : {
68 245416385 : bool valid = false;
69 : int data_len, node_len;
70 : unsigned int flags;
71 : unsigned long long sqnum;
72 245416385 : struct ubifs_ch *ch = (struct ubifs_ch *)node;
73 245416385 : struct ubifs_ino_node *ino = (struct ubifs_ino_node *)node;
74 490832770 : ino_t inum = key_inum(c, key);
75 :
76 245416385 : if (!inum || inum > INUM_WATERMARK) {
77 0 : if (FSCK(c)->mode == REBUILD_MODE)
78 0 : dbg_fsck("bad inode node(bad inum %lu) at %d:%d, in %s",
79 : inum, lnum, offs, c->dev_name);
80 : else
81 0 : log_out(c, "bad inode node(bad inum %lu) at %d:%d",
82 : inum, lnum, offs);
83 : goto out;
84 : }
85 :
86 490832770 : if (ch->node_type != key_type(c, key)) {
87 0 : if (FSCK(c)->mode == REBUILD_MODE)
88 0 : dbg_fsck("bad inode node %lu(inconsistent node type %d vs key_type %d) at %d:%d, in %s",
89 : inum, ch->node_type, key_type(c, key),
90 : lnum, offs, c->dev_name);
91 : else
92 0 : log_out(c, "bad inode node %lu(inconsistent node type %d vs key_type %d) at %d:%d",
93 : inum, ch->node_type, key_type(c, key),
94 : lnum, offs);
95 : goto out;
96 : }
97 :
98 245416385 : node_len = le32_to_cpu(ch->len);
99 245416385 : sqnum = le64_to_cpu(ch->sqnum);
100 490832770 : key_copy(c, key, &ino_node->key);
101 245416385 : flags = le32_to_cpu(ino->flags);
102 245416385 : data_len = le32_to_cpu(ino->data_len);
103 245416385 : ino_node->is_xattr = !!(flags & UBIFS_XATTR_FL) ? 1 : 0;
104 245416385 : ino_node->is_encrypted = !!(flags & UBIFS_CRYPT_FL) ? 1 : 0;
105 245416385 : ino_node->mode = le32_to_cpu(ino->mode);
106 245416385 : ino_node->nlink = le32_to_cpu(ino->nlink);
107 245416385 : ino_node->xcnt = le32_to_cpu(ino->xattr_cnt);
108 245416385 : ino_node->xsz = le32_to_cpu(ino->xattr_size);
109 245416385 : ino_node->xnms = le32_to_cpu(ino->xattr_names);
110 245416385 : ino_node->size = le64_to_cpu(ino->size);
111 :
112 245416385 : if (inum == UBIFS_ROOT_INO && !S_ISDIR(ino_node->mode)) {
113 0 : if (FSCK(c)->mode == REBUILD_MODE)
114 0 : dbg_fsck("bad inode node %lu(root inode is not dir, tyoe %u) at %d:%d, in %s",
115 : inum, ino_node->mode & S_IFMT, lnum, offs,
116 : c->dev_name);
117 : else
118 0 : log_out(c, "bad inode node %lu(root inode is not dir, tyoe %u) at %d:%d",
119 : inum, ino_node->mode & S_IFMT, lnum, offs);
120 : goto out;
121 : }
122 :
123 245416385 : if (ino_node->size > c->max_inode_sz) {
124 2 : if (FSCK(c)->mode == REBUILD_MODE)
125 0 : dbg_fsck("bad inode node %lu(size %llu is too large) at %d:%d, in %s",
126 : inum, ino_node->size, lnum, offs, c->dev_name);
127 : else
128 2 : log_out(c, "bad inode node %lu(size %llu is too large) at %d:%d",
129 : inum, ino_node->size, lnum, offs);
130 : goto out;
131 : }
132 :
133 245416383 : if (le16_to_cpu(ino->compr_type) >= UBIFS_COMPR_TYPES_CNT) {
134 0 : if (FSCK(c)->mode == REBUILD_MODE)
135 0 : dbg_fsck("bad inode node %lu(unknown compression type %d) at %d:%d, in %s",
136 : inum, le16_to_cpu(ino->compr_type), lnum, offs,
137 : c->dev_name);
138 : else
139 0 : log_out(c, "bad inode node %lu(unknown compression type %d) at %d:%d",
140 : inum, le16_to_cpu(ino->compr_type), lnum, offs);
141 : goto out;
142 : }
143 :
144 245416383 : if (ino_node->xnms + ino_node->xcnt > XATTR_LIST_MAX) {
145 0 : if (FSCK(c)->mode == REBUILD_MODE)
146 0 : dbg_fsck("bad inode node %lu(too big xnames %u xcount %u) at %d:%d, in %s",
147 : inum, ino_node->xnms, ino_node->xcnt,
148 : lnum, offs, c->dev_name);
149 : else
150 0 : log_out(c, "bad inode node %lu(too big xnames %u xcount %u) at %d:%d",
151 : inum, ino_node->xnms, ino_node->xcnt,
152 : lnum, offs);
153 : goto out;
154 : }
155 :
156 245416383 : if (data_len < 0 || data_len > UBIFS_MAX_INO_DATA) {
157 0 : if (FSCK(c)->mode == REBUILD_MODE)
158 0 : dbg_fsck("bad inode node %lu(invalid data len %d) at %d:%d, in %s",
159 : inum, data_len, lnum, offs, c->dev_name);
160 : else
161 0 : log_out(c, "bad inode node %lu(invalid data len %d) at %d:%d",
162 : inum, data_len, lnum, offs);
163 : goto out;
164 : }
165 :
166 245416383 : if (UBIFS_INO_NODE_SZ + data_len != node_len) {
167 403774 : if (FSCK(c)->mode == REBUILD_MODE)
168 403705 : dbg_fsck("bad inode node %lu(inconsistent data len %d vs node len %d) at %d:%d, in %s",
169 : inum, data_len, node_len, lnum, offs, c->dev_name);
170 : else
171 69 : log_out(c, "bad inode node %lu(inconsistent data len %d vs node len %d) at %d:%d",
172 : inum, data_len, node_len, lnum, offs);
173 : goto out;
174 : }
175 :
176 245012609 : if (ino_node->is_xattr) {
177 77421602 : if (!S_ISREG(ino_node->mode)) {
178 6 : if (FSCK(c)->mode == REBUILD_MODE)
179 0 : dbg_fsck("bad inode node %lu(bad type %u for xattr) at %d:%d, in %s",
180 : inum, ino_node->mode & S_IFMT,
181 : lnum, offs, c->dev_name);
182 : else
183 6 : log_out(c, "bad inode node %lu(bad type %u for xattr) at %d:%d",
184 : inum, ino_node->mode & S_IFMT,
185 : lnum, offs);
186 : goto out;
187 : }
188 77421596 : if (data_len != ino_node->size) {
189 0 : if (FSCK(c)->mode == REBUILD_MODE)
190 0 : dbg_fsck("bad inode node %lu(inconsistent data_len %d vs size %llu for xattr) at %d:%d, in %s",
191 : inum, data_len, ino_node->size,
192 : lnum, offs, c->dev_name);
193 : else
194 0 : log_out(c, "bad inode node %lu(inconsistent data_len %d vs size %llu for xattr) at %d:%d",
195 : inum, data_len, ino_node->size,
196 : lnum, offs);
197 : goto out;
198 : }
199 77421596 : if (ino_node->xcnt || ino_node->xsz || ino_node->xnms) {
200 0 : if (FSCK(c)->mode == REBUILD_MODE)
201 0 : dbg_fsck("bad inode node %lu(non zero xattr count %u xattr size %u xattr names %u for xattr) at %d:%d, in %s",
202 : inum, ino_node->xcnt, ino_node->xsz,
203 : ino_node->xnms, lnum, offs, c->dev_name);
204 : else
205 0 : log_out(c, "bad inode node %lu(non zero xattr count %u xattr size %u xattr names %u for xattr) at %d:%d",
206 : inum, ino_node->xcnt, ino_node->xsz,
207 : ino_node->xnms, lnum, offs);
208 : goto out;
209 : }
210 : }
211 :
212 245012603 : switch (ino_node->mode & S_IFMT) {
213 134209266 : case S_IFREG:
214 134209266 : if (!ino_node->is_xattr && data_len != 0) {
215 6 : if (FSCK(c)->mode == REBUILD_MODE)
216 0 : dbg_fsck("bad inode node %lu(bad data len %d for reg file) at %d:%d, in %s",
217 : inum, data_len, lnum, offs, c->dev_name);
218 : else
219 6 : log_out(c, "bad inode node %lu(bad data len %d for reg file) at %d:%d",
220 : inum, data_len, lnum, offs);
221 : goto out;
222 : }
223 : break;
224 35542103 : case S_IFDIR:
225 35542103 : if (data_len != 0) {
226 0 : if (FSCK(c)->mode == REBUILD_MODE)
227 0 : dbg_fsck("bad inode node %lu(bad data len %d for dir file) at %d:%d, in %s",
228 : inum, data_len, lnum, offs, c->dev_name);
229 : else
230 0 : log_out(c, "bad inode node %lu(bad data len %d for dir file) at %d:%d",
231 : inum, data_len, lnum, offs);
232 : goto out;
233 : }
234 : break;
235 25312407 : case S_IFLNK:
236 25312407 : if (data_len == 0) {
237 : /*
238 : * For encryption enabled or selinux enabled situation,
239 : * uninitialized inode with xattrs could be written
240 : * before ubifs_jnl_update(). If the dent node is
241 : * written successfully but the initialized inode is
242 : * not written, ubifs_iget() will get bad symlink inode
243 : * with 'ui->data_len = 0'. Similar phenomenon can also
244 : * occur for block/char dev creation.
245 : * Just drop the inode node when above class of
246 : * exceptions are found.
247 : */
248 345593 : if (FSCK(c)->mode == REBUILD_MODE)
249 345593 : dbg_fsck("bad symlink inode node %lu(bad data len %d) at %d:%d, in %s",
250 : inum, data_len, lnum, offs, c->dev_name);
251 : else
252 0 : log_out(c, "bad symlink inode node %lu(bad data len %d) at %d:%d",
253 : inum, data_len, lnum, offs);
254 : goto out;
255 : }
256 : break;
257 49948803 : case S_IFBLK:
258 : fallthrough;
259 : case S_IFCHR:
260 : {
261 49948803 : union ubifs_dev_desc *dev = (union ubifs_dev_desc *)ino->data;
262 49948803 : int sz_new = sizeof(dev->new), sz_huge = sizeof(dev->huge);
263 :
264 49948803 : if (data_len != sz_new && data_len != sz_huge) {
265 0 : if (FSCK(c)->mode == REBUILD_MODE)
266 0 : dbg_fsck("bad inode node %lu(bad data len %d for char/block file, expect %d or %d) at %d:%d, in %s",
267 : inum, data_len, sz_new, sz_huge, lnum,
268 : offs, c->dev_name);
269 : else
270 0 : log_out(c, "bad inode node %lu(bad data len %d for char/block file, expect %d or %d) at %d:%d",
271 : inum, data_len, sz_new, sz_huge, lnum,
272 : offs);
273 : goto out;
274 : }
275 : break;
276 : }
277 12 : case S_IFSOCK:
278 : fallthrough;
279 : case S_IFIFO:
280 12 : if (data_len != 0) {
281 0 : if (FSCK(c)->mode == REBUILD_MODE)
282 0 : dbg_fsck("bad inode node %lu(bad data len %d for fifo/sock file) at %d:%d, in %s",
283 : inum, data_len, lnum, offs, c->dev_name);
284 : else
285 0 : log_out(c, "bad inode node %lu(bad data len %d for fifo/sock file) at %d:%d",
286 : inum, data_len, lnum, offs);
287 : goto out;
288 : }
289 : break;
290 12 : default:
291 : /* invalid file type. */
292 12 : if (FSCK(c)->mode == REBUILD_MODE)
293 0 : dbg_fsck("bad inode node %lu(unknown type %u) at %d:%d, in %s",
294 : inum, ino_node->mode & S_IFMT, lnum, offs, c->dev_name);
295 : else
296 12 : log_out(c, "bad inode node %lu(unknown type %u) at %d:%d",
297 : inum, ino_node->mode & S_IFMT, lnum, offs);
298 : goto out;
299 : }
300 :
301 244666992 : if (ino_node->is_encrypted && !inode_can_be_encrypted(c, ino_node)) {
302 0 : if (FSCK(c)->mode == REBUILD_MODE)
303 0 : dbg_fsck("bad inode node %lu(encrypted but cannot be encrypted, type %u, is_xattr %d, fs_encrypted %d) at %d:%d, in %s",
304 : inum, ino_node->mode & S_IFMT, ino_node->is_xattr,
305 : c->encrypted, lnum, offs, c->dev_name);
306 : else
307 0 : log_out(c, "bad inode node %lu(encrypted but cannot be encrypted, type %u, is_xattr %d, fs_encrypted %d) at %d:%d",
308 : inum, ino_node->mode & S_IFMT, ino_node->is_xattr,
309 : c->encrypted, lnum, offs);
310 : goto out;
311 : }
312 :
313 244666992 : valid = true;
314 244666992 : parse_node_header(lnum, offs, node_len, sqnum, &ino_node->header);
315 :
316 245416385 : out:
317 245416385 : return valid;
318 : }
319 :
320 : /**
321 : * parse_dent_node - parse dentry node and check it's validity.
322 : * @c: UBIFS file-system description object
323 : * @lnum: logical eraseblock number
324 : * @offs: the offset in LEB of the raw inode node
325 : * @node: raw node
326 : * @key: key of node scanned (if it has one)
327 : * @dent_node: node used to store raw dentry information
328 : *
329 : * This function checks the raw dentry/(xattr entry) information, and
330 : * stores dentry/(xattr entry) information into @dent_node. Returns
331 : * %true if the entry is valid, otherwise %false is returned.
332 : */
333 239414977 : bool parse_dent_node(struct ubifs_info *c, int lnum, int offs, void *node,
334 : union ubifs_key *key, struct scanned_dent_node *dent_node)
335 : {
336 239414977 : bool valid = false;
337 : int node_len, nlen;
338 : unsigned long long sqnum;
339 239414977 : struct ubifs_ch *ch = (struct ubifs_ch *)node;
340 239414977 : struct ubifs_dent_node *dent = (struct ubifs_dent_node *)node;
341 478829954 : int key_type = key_type_flash(c, dent->key);
342 : ino_t inum;
343 :
344 239414977 : nlen = le16_to_cpu(dent->nlen);
345 239414977 : node_len = le32_to_cpu(ch->len);
346 239414977 : sqnum = le64_to_cpu(ch->sqnum);
347 239414977 : inum = le64_to_cpu(dent->inum);
348 :
349 478829930 : if (node_len != nlen + UBIFS_DENT_NODE_SZ + 1 ||
350 478829894 : dent->type >= UBIFS_ITYPES_CNT ||
351 239414941 : nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
352 77412741 : (key_type == UBIFS_XENT_KEY &&
353 316827682 : strnlen((const char *)dent->name, nlen) != nlen) ||
354 239414941 : inum > INUM_WATERMARK || key_type != ch->node_type) {
355 36 : if (FSCK(c)->mode == REBUILD_MODE)
356 0 : dbg_fsck("bad %s node(len %d nlen %d type %d inum %lu key_type %d node_type %d) at %d:%d, in %s",
357 : ch->node_type == UBIFS_XENT_NODE ? "xattr entry" : "directory entry",
358 : node_len, nlen, dent->type, inum, key_type,
359 : ch->node_type, lnum, offs, c->dev_name);
360 : else
361 36 : log_out(c, "bad %s node(len %d nlen %d type %d inum %lu key_type %d node_type %d) at %d:%d",
362 : ch->node_type == UBIFS_XENT_NODE ? "xattr entry" : "directory entry",
363 : node_len, nlen, dent->type, inum, key_type,
364 : ch->node_type, lnum, offs);
365 : goto out;
366 : }
367 :
368 478829882 : key_copy(c, key, &dent_node->key);
369 239414941 : dent_node->can_be_found = false;
370 239414941 : dent_node->type = dent->type;
371 239414941 : dent_node->nlen = nlen;
372 239414941 : memcpy(dent_node->name, dent->name, nlen);
373 239414941 : dent_node->name[nlen] = '\0';
374 239414941 : dent_node->inum = inum;
375 :
376 239414941 : valid = true;
377 239414941 : parse_node_header(lnum, offs, node_len, sqnum, &dent_node->header);
378 :
379 239414977 : out:
380 239414977 : return valid;
381 : }
382 :
383 : /**
384 : * parse_data_node - parse data node and check it's validity.
385 : * @c: UBIFS file-system description object
386 : * @lnum: logical eraseblock number
387 : * @offs: the offset in LEB of the raw data node
388 : * @node: raw node
389 : * @key: key of node scanned (if it has one)
390 : * @ino_node: node used to store raw data information
391 : *
392 : * This function checks the raw data node information, and stores
393 : * data node information into @data_node. Returns %true if the data
394 : * node is valid, otherwise %false is returned.
395 : */
396 2249972397 : bool parse_data_node(struct ubifs_info *c, int lnum, int offs, void *node,
397 : union ubifs_key *key, struct scanned_data_node *data_node)
398 : {
399 2249972397 : bool valid = false;
400 : int node_len;
401 : unsigned long long sqnum;
402 2249972397 : struct ubifs_ch *ch = (struct ubifs_ch *)node;
403 2249972397 : struct ubifs_data_node *dn = (struct ubifs_data_node *)node;
404 4499944794 : ino_t inum = key_inum(c, key);
405 :
406 4499944794 : if (ch->node_type != key_type(c, key)) {
407 0 : if (FSCK(c)->mode == REBUILD_MODE)
408 0 : dbg_fsck("bad data node(inconsistent node type %d vs key_type %d) at %d:%d, in %s",
409 : ch->node_type, key_type(c, key),
410 : lnum, offs, c->dev_name);
411 : else
412 0 : log_out(c, "bad data node(inconsistent node type %d vs key_type %d) at %d:%d",
413 : ch->node_type, key_type(c, key), lnum, offs);
414 : goto out;
415 : }
416 :
417 2249972397 : if (!inum || inum > INUM_WATERMARK) {
418 0 : if (FSCK(c)->mode == REBUILD_MODE)
419 0 : dbg_fsck("bad data node(bad inum %lu) at %d:%d, in %s",
420 : inum, lnum, offs, c->dev_name);
421 : else
422 0 : log_out(c, "bad data node(bad inum %lu) at %d:%d",
423 : inum, lnum, offs);
424 : goto out;
425 : }
426 :
427 2249972397 : node_len = le32_to_cpu(ch->len);
428 2249972397 : sqnum = le64_to_cpu(ch->sqnum);
429 4499944794 : key_copy(c, key, &data_node->key);
430 2249972397 : data_node->size = le32_to_cpu(dn->size);
431 :
432 2249972397 : if (!data_node->size || data_node->size > UBIFS_BLOCK_SIZE) {
433 6 : if (FSCK(c)->mode == REBUILD_MODE)
434 0 : dbg_fsck("bad data node(invalid size %u) at %d:%d, in %s",
435 : data_node->size, lnum, offs, c->dev_name);
436 : else
437 6 : log_out(c, "bad data node(invalid size %u) at %d:%d",
438 : data_node->size, lnum, offs);
439 : goto out;
440 : }
441 :
442 2249972391 : if (le16_to_cpu(dn->compr_type) >= UBIFS_COMPR_TYPES_CNT) {
443 0 : if (FSCK(c)->mode == REBUILD_MODE)
444 0 : dbg_fsck("bad data node(invalid compression type %d) at %d:%d, in %s",
445 : le16_to_cpu(dn->compr_type), lnum, offs, c->dev_name);
446 : else
447 0 : log_out(c, "bad data node(invalid compression type %d) at %d:%d",
448 : le16_to_cpu(dn->compr_type), lnum, offs);
449 : goto out;
450 : }
451 :
452 2249972391 : valid = true;
453 2249972391 : parse_node_header(lnum, offs, node_len, sqnum, &data_node->header);
454 :
455 2249972397 : out:
456 2249972397 : return valid;
457 : }
458 :
459 : /**
460 : * parse_trun_node - parse truncation node and check it's validity.
461 : * @c: UBIFS file-system description object
462 : * @lnum: logical eraseblock number
463 : * @offs: the offset in LEB of the raw truncation node
464 : * @node: raw node
465 : * @key: key of node scanned (if it has one)
466 : * @trun_node: node used to store raw truncation information
467 : *
468 : * This function checks the raw truncation information, and stores
469 : * truncation information into @trun_node. Returns %true if the
470 : * truncation is valid, otherwise %false is returned.
471 : */
472 327663 : bool parse_trun_node(struct ubifs_info *c, int lnum, int offs, void *node,
473 : union ubifs_key *key, struct scanned_trun_node *trun_node)
474 : {
475 327663 : bool valid = false;
476 : int node_len;
477 : unsigned long long sqnum;
478 327663 : struct ubifs_ch *ch = (struct ubifs_ch *)node;
479 327663 : struct ubifs_trun_node *trun = (struct ubifs_trun_node *)node;
480 327663 : loff_t old_size = le64_to_cpu(trun->old_size);
481 327663 : loff_t new_size = le64_to_cpu(trun->new_size);
482 327663 : ino_t inum = le32_to_cpu(trun->inum);
483 :
484 327663 : if (!inum || inum > INUM_WATERMARK) {
485 0 : dbg_fsck("bad truncation node(bad inum %lu) at %d:%d, in %s",
486 : inum, lnum, offs, c->dev_name);
487 : goto out;
488 : }
489 :
490 327663 : node_len = le32_to_cpu(ch->len);
491 327663 : sqnum = le64_to_cpu(ch->sqnum);
492 327663 : trun_node->new_size = new_size;
493 :
494 327663 : if (old_size < 0 || old_size > c->max_inode_sz ||
495 327663 : new_size < 0 || new_size > c->max_inode_sz ||
496 : old_size <= new_size) {
497 0 : dbg_fsck("bad truncation node(new size %ld old size %ld inum %lu) at %d:%d, in %s",
498 : new_size, old_size, inum, lnum, offs, c->dev_name);
499 : goto out;
500 : }
501 :
502 655326 : trun_key_init(c, key, inum);
503 327663 : valid = true;
504 327663 : parse_node_header(lnum, offs, node_len, sqnum, &trun_node->header);
505 :
506 327663 : out:
507 327663 : return valid;
508 : }
509 :
510 : /**
511 : * insert_file_dentry - insert dentry according to scanned dent node.
512 : * @file: file object
513 : * @n_dent: scanned dent node
514 : *
515 : * Insert file dentry information. Returns zero in case of success, a
516 : * negative error code in case of failure.
517 : */
518 233719400 : static int insert_file_dentry(struct scanned_file *file,
519 : struct scanned_dent_node *n_dent)
520 : {
521 : struct scanned_dent_node *dent;
522 233719400 : struct rb_node **p, *parent = NULL;
523 :
524 233719400 : p = &file->dent_nodes.rb_node;
525 480041780 : while (*p) {
526 12602980 : parent = *p;
527 12602980 : dent = rb_entry(parent, struct scanned_dent_node, rb);
528 12602980 : if (n_dent->header.sqnum < dent->header.sqnum)
529 4168531 : p = &(*p)->rb_left;
530 : else
531 8434449 : p = &(*p)->rb_right;
532 : }
533 :
534 233719400 : dent = kmalloc(sizeof(struct scanned_dent_node), GFP_KERNEL);
535 233719400 : if (!dent)
536 : return -ENOMEM;
537 :
538 233719400 : *dent = *n_dent;
539 467438800 : rb_link_node(&dent->rb, parent, p);
540 233719400 : rb_insert_color(&dent->rb, &file->dent_nodes);
541 :
542 233719400 : return 0;
543 : }
544 :
545 : /**
546 : * update_file_data - insert/update data according to scanned data node.
547 : * @c: UBIFS file-system description object
548 : * @file: file object
549 : * @n_dn: scanned data node
550 : *
551 : * Insert or update file data information. Returns zero in case of success,
552 : * a negative error code in case of failure.
553 : */
554 2249972391 : static int update_file_data(struct ubifs_info *c, struct scanned_file *file,
555 : struct scanned_data_node *n_dn)
556 : {
557 : int cmp;
558 2249972391 : struct scanned_data_node *dn, *o_dn = NULL;
559 2249972391 : struct rb_node **p, *parent = NULL;
560 :
561 2249972391 : p = &file->data_nodes.rb_node;
562 19831542287 : while (*p) {
563 17588294092 : parent = *p;
564 17588294092 : dn = rb_entry(parent, struct scanned_data_node, rb);
565 34825930611 : cmp = keys_cmp(c, &n_dn->key, &dn->key);
566 : if (cmp < 0) {
567 343933377 : p = &(*p)->rb_left;
568 17244360715 : } else if (cmp > 0) {
569 17237636519 : p = &(*p)->rb_right;
570 : } else {
571 : o_dn = dn;
572 : break;
573 : }
574 : }
575 :
576 2249972391 : if (o_dn) {
577 : /* found data node with same block no. */
578 6724196 : if (o_dn->header.sqnum < n_dn->header.sqnum) {
579 4005519 : o_dn->header = n_dn->header;
580 4005519 : o_dn->size = n_dn->size;
581 : }
582 :
583 : return 0;
584 : }
585 :
586 2243248195 : dn = kmalloc(sizeof(struct scanned_data_node), GFP_KERNEL);
587 2243248195 : if (!dn)
588 : return -ENOMEM;
589 :
590 2243248195 : *dn = *n_dn;
591 4486496390 : INIT_LIST_HEAD(&dn->list);
592 4486496390 : rb_link_node(&dn->rb, parent, p);
593 2243248195 : rb_insert_color(&dn->rb, &file->data_nodes);
594 :
595 : return 0;
596 : }
597 :
598 : /**
599 : * update_file - update file information.
600 : * @c: UBIFS file-system description object
601 : * @file: file object
602 : * @sn: scanned node
603 : * @key_type: type of @sn
604 : *
605 : * Update inode/dent/truncation/data node information of @file. Returns
606 : * zero in case of success, a negative error code in case of failure.
607 : */
608 2706636835 : static int update_file(struct ubifs_info *c, struct scanned_file *file,
609 : struct scanned_node *sn, int key_type)
610 : {
611 2706636835 : int err = 0;
612 :
613 2706636835 : switch (key_type) {
614 222617381 : case UBIFS_INO_KEY:
615 : {
616 : struct scanned_ino_node *o_ino, *n_ino;
617 :
618 222617381 : o_ino = &file->ino;
619 222617381 : n_ino = (struct scanned_ino_node *)sn;
620 222617381 : if (o_ino->header.exist && o_ino->header.sqnum > sn->sqnum)
621 : goto out;
622 :
623 222617381 : *o_ino = *n_ino;
624 222617381 : break;
625 : }
626 233719400 : case UBIFS_DENT_KEY:
627 : case UBIFS_XENT_KEY:
628 : {
629 233719400 : struct scanned_dent_node *dent = (struct scanned_dent_node *)sn;
630 :
631 233719400 : dent->file = file;
632 233719400 : err = insert_file_dentry(file, dent);
633 233719400 : break;
634 : }
635 2249972391 : case UBIFS_DATA_KEY:
636 : {
637 2249972391 : struct scanned_data_node *dn = (struct scanned_data_node *)sn;
638 :
639 2249972391 : err = update_file_data(c, file, dn);
640 2249972391 : break;
641 : }
642 327663 : case UBIFS_TRUN_KEY:
643 : {
644 : struct scanned_trun_node *o_trun, *n_trun;
645 :
646 327663 : o_trun = &file->trun;
647 327663 : n_trun = (struct scanned_trun_node *)sn;
648 327663 : if (o_trun->header.exist && o_trun->header.sqnum > sn->sqnum)
649 : goto out;
650 :
651 301694 : *o_trun = *n_trun;
652 301694 : break;
653 : }
654 0 : default:
655 0 : err = -EINVAL;
656 0 : log_err(c, 0, "unknown key type %d", key_type);
657 : }
658 :
659 2706662804 : out:
660 2706636835 : return err;
661 : }
662 :
663 : /**
664 : * insert_or_update_file - insert or update file according to scanned node.
665 : * @c: UBIFS file-system description object
666 : * @file_tree: tree of all scanned files
667 : * @sn: scanned node
668 : * @key_type: key type of @sn
669 : * @inum: inode number
670 : *
671 : * According to @sn, this function inserts file into the tree, or updates
672 : * file information if it already exists in the tree. Returns zero in case
673 : * of success, a negative error code in case of failure.
674 : */
675 2706636835 : int insert_or_update_file(struct ubifs_info *c, struct rb_root *file_tree,
676 : struct scanned_node *sn, int key_type, ino_t inum)
677 : {
678 : int err;
679 2706636835 : struct scanned_file *file, *old_file = NULL;
680 2706636835 : struct rb_node **p, *parent = NULL;
681 :
682 2706636835 : p = &file_tree->rb_node;
683 60797206147 : while (*p) {
684 57867640735 : parent = *p;
685 57867640735 : file = rb_entry(parent, struct scanned_file, rb);
686 57867640735 : if (inum < file->inum) {
687 16347345921 : p = &(*p)->rb_left;
688 41520294814 : } else if (inum > file->inum) {
689 39036586556 : p = &(*p)->rb_right;
690 : } else {
691 : old_file = file;
692 : break;
693 : }
694 : }
695 2706636835 : if (old_file)
696 2483708258 : return update_file(c, old_file, sn, key_type);
697 :
698 222928577 : file = kzalloc(sizeof(struct scanned_file), GFP_KERNEL);
699 222928577 : if (!file)
700 : return -ENOMEM;
701 :
702 222928577 : file->inum = inum;
703 222928577 : file->dent_nodes = RB_ROOT;
704 222928577 : file->data_nodes = RB_ROOT;
705 222928577 : file->xattr_files = RB_ROOT;
706 445857154 : INIT_LIST_HEAD(&file->list);
707 222928577 : err = update_file(c, file, sn, key_type);
708 222928577 : if (err) {
709 0 : kfree(file);
710 0 : return err;
711 : }
712 445857154 : rb_link_node(&file->rb, parent, p);
713 222928577 : rb_insert_color(&file->rb, file_tree);
714 :
715 222928577 : return 0;
716 : }
717 :
718 : /**
719 : * destroy_file_content - destroy scanned data/dentry nodes in give file.
720 : * @c: UBIFS file-system description object
721 : * @file: file object
722 : *
723 : * Destroy all data/dentry nodes and xattrs attached to @file.
724 : */
725 222831510 : void destroy_file_content(struct ubifs_info *c, struct scanned_file *file)
726 : {
727 : struct scanned_data_node *data_node;
728 : struct scanned_dent_node *dent_node;
729 : struct scanned_file *xattr_file;
730 : struct rb_node *this;
731 :
732 222831510 : this = rb_first(&file->data_nodes);
733 2674653121 : while (this) {
734 2228990101 : data_node = rb_entry(this, struct scanned_data_node, rb);
735 2228990101 : this = rb_next(this);
736 :
737 2228990101 : rb_erase(&data_node->rb, &file->data_nodes);
738 : kfree(data_node);
739 : }
740 :
741 222831510 : this = rb_first(&file->dent_nodes);
742 677064563 : while (this) {
743 231401543 : dent_node = rb_entry(this, struct scanned_dent_node, rb);
744 231401543 : this = rb_next(this);
745 :
746 231401543 : rb_erase(&dent_node->rb, &file->dent_nodes);
747 : kfree(dent_node);
748 : }
749 :
750 222831510 : this = rb_first(&file->xattr_files);
751 522840118 : while (this) {
752 77177098 : xattr_file = rb_entry(this, struct scanned_file, rb);
753 77177098 : this = rb_next(this);
754 :
755 77177098 : ubifs_assert(c, !rb_first(&xattr_file->xattr_files));
756 77177098 : destroy_file_content(c, xattr_file);
757 77177098 : rb_erase(&xattr_file->rb, &file->xattr_files);
758 : kfree(xattr_file);
759 : }
760 222831510 : }
761 :
762 : /**
763 : * destroy_file_tree - destroy files from a given tree.
764 : * @c: UBIFS file-system description object
765 : * @file_tree: tree of all scanned files
766 : *
767 : * Destroy scanned files from a given tree.
768 : */
769 2431 : void destroy_file_tree(struct ubifs_info *c, struct rb_root *file_tree)
770 : {
771 : struct scanned_file *file;
772 : struct rb_node *this;
773 :
774 2431 : this = rb_first(file_tree);
775 143155330 : while (this) {
776 143150468 : file = rb_entry(this, struct scanned_file, rb);
777 143150468 : this = rb_next(this);
778 :
779 143150468 : destroy_file_content(c, file);
780 :
781 143150468 : rb_erase(&file->rb, file_tree);
782 : kfree(file);
783 : }
784 2431 : }
785 :
786 : /**
787 : * destroy_file_list - destroy files from a given list head.
788 : * @c: UBIFS file-system description object
789 : * @file_list: list of the scanned files
790 : *
791 : * Destroy scanned files from a given list.
792 : */
793 35 : void destroy_file_list(struct ubifs_info *c, struct list_head *file_list)
794 : {
795 : struct scanned_file *file;
796 :
797 18820 : while (!list_empty(file_list)) {
798 18750 : file = list_entry(file_list->next, struct scanned_file, list);
799 :
800 18750 : destroy_file_content(c, file);
801 37500 : list_del(&file->list);
802 : kfree(file);
803 : }
804 35 : }
805 :
806 : /**
807 : * lookup_file - lookup file according to inode number.
808 : * @file_tree: tree of all scanned files
809 : * @inum: inode number
810 : *
811 : * This function lookups target file from @file_tree according to @inum.
812 : */
813 0 : struct scanned_file *lookup_file(struct rb_root *file_tree, ino_t inum)
814 : {
815 : struct scanned_file *file;
816 : struct rb_node *p;
817 :
818 532466528 : p = file_tree->rb_node;
819 9609549859 : while (p) {
820 9609401342 : file = rb_entry(p, struct scanned_file, rb);
821 :
822 9609401342 : if (inum < file->inum)
823 4239466060 : p = p->rb_left;
824 5369935282 : else if (inum > file->inum)
825 4837617271 : p = p->rb_right;
826 : else
827 : return file;
828 : }
829 :
830 : return NULL;
831 : }
832 :
833 : static void handle_invalid_file(struct ubifs_info *c, int problem_type,
834 : struct scanned_file *file, void *priv)
835 : {
836 5120391 : struct invalid_file_problem ifp = {
837 : .file = file,
838 : .priv = priv,
839 : };
840 :
841 5120391 : if (FSCK(c)->mode == REBUILD_MODE)
842 4991984 : return;
843 :
844 128407 : fix_problem(c, problem_type, &ifp);
845 : }
846 :
847 224225 : static int delete_node(struct ubifs_info *c, const union ubifs_key *key,
848 : int lnum, int offs)
849 : {
850 : int err;
851 :
852 224225 : err = ubifs_tnc_remove_node(c, key, lnum, offs);
853 224225 : if (err) {
854 : /* TNC traversing is finished, any TNC path is accessible */
855 0 : ubifs_assert(c, !get_failure_reason_callback(c));
856 : }
857 :
858 224225 : return err;
859 : }
860 :
861 68293 : static int delete_dent_nodes(struct ubifs_info *c, struct scanned_file *file,
862 : int err)
863 : {
864 68293 : int ret = 0;
865 68293 : struct rb_node *this = rb_first(&file->dent_nodes);
866 : struct scanned_dent_node *dent_node;
867 :
868 161454 : while (this) {
869 24868 : dent_node = rb_entry(this, struct scanned_dent_node, rb);
870 24868 : this = rb_next(this);
871 :
872 24868 : if (!err) {
873 24868 : err = delete_node(c, &dent_node->key,
874 : dent_node->header.lnum, dent_node->header.offs);
875 24868 : if (err)
876 0 : ret = ret ? ret : err;
877 : }
878 :
879 24868 : rb_erase(&dent_node->rb, &file->dent_nodes);
880 : kfree(dent_node);
881 : }
882 :
883 68293 : return ret;
884 : }
885 :
886 68286 : int delete_file(struct ubifs_info *c, struct scanned_file *file)
887 : {
888 68286 : int err = 0, ret = 0;
889 : struct rb_node *this;
890 : struct scanned_file *xattr_file;
891 : struct scanned_data_node *data_node;
892 :
893 68286 : if (file->ino.header.exist) {
894 67031 : err = delete_node(c, &file->ino.key, file->ino.header.lnum,
895 : file->ino.header.offs);
896 67031 : if (err)
897 0 : ret = ret ? ret : err;
898 : }
899 :
900 68286 : this = rb_first(&file->data_nodes);
901 206770 : while (this) {
902 70198 : data_node = rb_entry(this, struct scanned_data_node, rb);
903 70198 : this = rb_next(this);
904 :
905 70198 : if (!err) {
906 70198 : err = delete_node(c, &data_node->key,
907 : data_node->header.lnum, data_node->header.offs);
908 70198 : if (err)
909 0 : ret = ret ? ret : err;
910 : }
911 :
912 70198 : rb_erase(&data_node->rb, &file->data_nodes);
913 : kfree(data_node);
914 : }
915 :
916 68286 : err = delete_dent_nodes(c, file, err);
917 68286 : if (err)
918 0 : ret = ret ? : err;
919 :
920 68286 : this = rb_first(&file->xattr_files);
921 158144 : while (this) {
922 21572 : xattr_file = rb_entry(this, struct scanned_file, rb);
923 21572 : this = rb_next(this);
924 :
925 21572 : ubifs_assert(c, !rb_first(&xattr_file->xattr_files));
926 21572 : err = delete_file(c, xattr_file);
927 21572 : if (err)
928 0 : ret = ret ? ret : err;
929 21572 : rb_erase(&xattr_file->rb, &file->xattr_files);
930 : kfree(xattr_file);
931 : }
932 :
933 68286 : return ret;
934 : }
935 :
936 : /**
937 : * insert_xattr_file - insert xattr file into file's subtree.
938 : * @c: UBIFS file-system description object
939 : * @xattr_file: xattr file
940 : * @host_file: host file
941 : *
942 : * This inserts xattr file into its' host file's subtree.
943 : */
944 77200230 : static void insert_xattr_file(struct ubifs_info *c,
945 : struct scanned_file *xattr_file,
946 : struct scanned_file *host_file)
947 : {
948 : struct scanned_file *tmp_xattr_file;
949 77200230 : struct rb_node **p, *parent = NULL;
950 :
951 77200230 : p = &host_file->xattr_files.rb_node;
952 168584905 : while (*p) {
953 14184445 : parent = *p;
954 14184445 : tmp_xattr_file = rb_entry(parent, struct scanned_file, rb);
955 14184445 : if (xattr_file->inum < tmp_xattr_file->inum) {
956 14184445 : p = &(*p)->rb_left;
957 0 : } else if (xattr_file->inum > tmp_xattr_file->inum) {
958 0 : p = &(*p)->rb_right;
959 : } else {
960 : /* Impossible: Same xattr file is inserted twice. */
961 0 : ubifs_assert(c, 0);
962 : }
963 : }
964 :
965 154400460 : rb_link_node(&xattr_file->rb, parent, p);
966 77200230 : rb_insert_color(&xattr_file->rb, &host_file->xattr_files);
967 77200230 : }
968 :
969 : /**
970 : * file_is_valid - check whether the file is valid.
971 : * @c: UBIFS file-system description object
972 : * @file: file object
973 : * @file_tree: tree of all scanned files
974 : * @is_diconnected: reason of invalid file, whether the @file is disconnected
975 : *
976 : * This function checks whether given @file is valid, following checks will
977 : * be performed:
978 : * 1. All files have none-zero nlink inode, otherwise they are invalid.
979 : * 2. The file type comes from inode and dentries should be consistent,
980 : * inconsistent dentries will be deleted.
981 : * 3. Directory type or xattr type files only have one dentry. Superfluous
982 : * dentries with lower sequence number will be deleted.
983 : * 4. Non-regular file doesn't have data nodes. Data nodes are deleted for
984 : * non-regular file.
985 : * 5. All files must have at least one dentries, except '/', '/' doesn't
986 : * have dentries. Non '/' file is invalid if it doesn't have dentries.
987 : * 6. Xattr files should have host inode, and host inode cannot be a xattr,
988 : * otherwise they are invalid.
989 : * 7. Encrypted files should have corresponding xattrs, otherwise they are
990 : * invalid.
991 : * Xattr file will be inserted into corresponding host file's subtree.
992 : *
993 : * Returns %1 is @file is valid, %0 if @file is invalid, otherwise a negative
994 : * error code in case of failure.
995 : * Notice: All xattr files should be traversed before non-xattr files, because
996 : * checking item 7 depends on it.
997 : */
998 222771340 : int file_is_valid(struct ubifs_info *c, struct scanned_file *file,
999 : struct rb_root *file_tree, int *is_diconnected)
1000 : {
1001 : int type;
1002 : struct rb_node *node;
1003 222771340 : struct scanned_file *parent_file = NULL;
1004 : struct scanned_dent_node *dent_node;
1005 : struct scanned_data_node *data_node;
1006 222771340 : LIST_HEAD(drop_list);
1007 :
1008 222771340 : dbg_fsck("check validation of file %lu, in %s", file->inum, c->dev_name);
1009 :
1010 222771340 : if (!file->ino.header.exist) {
1011 : handle_invalid_file(c, FILE_HAS_NO_INODE, file, NULL);
1012 : return 0;
1013 : }
1014 :
1015 222569317 : if (!file->ino.nlink) {
1016 : handle_invalid_file(c, FILE_HAS_0_NLINK_INODE, file, NULL);
1017 : return 0;
1018 : }
1019 :
1020 222569314 : type = ubifs_get_dent_type(file->ino.mode);
1021 :
1022 : /* Drop dentry nodes with inconsistent type. */
1023 456097127 : for (node = rb_first(&file->dent_nodes); node; node = rb_next(node)) {
1024 233527813 : int is_xattr = 0;
1025 :
1026 233527813 : dent_node = rb_entry(node, struct scanned_dent_node, rb);
1027 :
1028 467055626 : if (key_type(c, &dent_node->key) == UBIFS_XENT_KEY)
1029 77238205 : is_xattr = 1;
1030 233527813 : if (is_xattr != file->ino.is_xattr || type != dent_node->type)
1031 18 : list_add(&dent_node->list, &drop_list);
1032 : }
1033 :
1034 222569326 : while (!list_empty(&drop_list)) {
1035 15 : dent_node = list_entry(drop_list.next, struct scanned_dent_node,
1036 : list);
1037 :
1038 12 : handle_invalid_file(c, FILE_HAS_INCONSIST_TYPE, file, dent_node);
1039 12 : if (FSCK(c)->mode != REBUILD_MODE) {
1040 12 : int err = delete_node(c, &dent_node->key,
1041 : dent_node->header.lnum, dent_node->header.offs);
1042 12 : if (err)
1043 : return err;
1044 : }
1045 :
1046 24 : list_del(&dent_node->list);
1047 12 : rb_erase(&dent_node->rb, &file->dent_nodes);
1048 : kfree(dent_node);
1049 : }
1050 :
1051 222569311 : if (type != UBIFS_ITYPE_DIR && !file->ino.is_xattr)
1052 : goto check_data_nodes;
1053 :
1054 : /* Make sure that directory/xattr type files only have one dentry. */
1055 102156355 : node = rb_first(&file->dent_nodes);
1056 204341676 : while (node) {
1057 102094233 : dent_node = rb_entry(node, struct scanned_dent_node, rb);
1058 102094233 : node = rb_next(node);
1059 102094233 : if (!node)
1060 : break;
1061 :
1062 28966 : handle_invalid_file(c, FILE_HAS_TOO_MANY_DENT, file, dent_node);
1063 28966 : if (FSCK(c)->mode != REBUILD_MODE) {
1064 3 : int err = delete_node(c, &dent_node->key,
1065 : dent_node->header.lnum, dent_node->header.offs);
1066 3 : if (err)
1067 : return err;
1068 : }
1069 :
1070 28966 : rb_erase(&dent_node->rb, &file->dent_nodes);
1071 : kfree(dent_node);
1072 : }
1073 :
1074 222569308 : check_data_nodes:
1075 222569308 : if (type == UBIFS_ITYPE_REG && !file->ino.is_xattr)
1076 : goto check_dent_node;
1077 :
1078 : /* Make sure that non regular type files not have data/trun nodes. */
1079 174501873 : file->trun.header.exist = 0;
1080 174501873 : node = rb_first(&file->data_nodes);
1081 349003752 : while (node) {
1082 6 : data_node = rb_entry(node, struct scanned_data_node, rb);
1083 6 : node = rb_next(node);
1084 :
1085 6 : handle_invalid_file(c, FILE_SHOULDNT_HAVE_DATA, file, data_node);
1086 6 : if (FSCK(c)->mode != REBUILD_MODE) {
1087 6 : int err = delete_node(c, &data_node->key,
1088 : data_node->header.lnum, data_node->header.offs);
1089 6 : if (err)
1090 : return err;
1091 : }
1092 :
1093 6 : rb_erase(&data_node->rb, &file->data_nodes);
1094 : kfree(data_node);
1095 : }
1096 :
1097 222569308 : check_dent_node:
1098 222569308 : if (rb_first(&file->dent_nodes)) {
1099 222274568 : if (file->inum == UBIFS_ROOT_INO) {
1100 : /* '/' has no dentries. */
1101 : handle_invalid_file(c, FILE_ROOT_HAS_DENT, file,
1102 0 : rb_entry(rb_first(&file->dent_nodes),
1103 : struct scanned_dent_node, rb));
1104 : return 0;
1105 : }
1106 :
1107 222274568 : node = rb_first(&file->dent_nodes);
1108 222274568 : dent_node = rb_entry(node, struct scanned_dent_node, rb);
1109 444549136 : parent_file = lookup_file(file_tree, key_inum(c, &dent_node->key));
1110 : } else {
1111 : /* Non-root files must have dentries. */
1112 294740 : if (file->inum != UBIFS_ROOT_INO) {
1113 292759 : if (type == UBIFS_ITYPE_REG && !file->ino.is_xattr) {
1114 22385 : handle_invalid_file(c, FILE_IS_DISCONNECTED,
1115 : file, NULL);
1116 22385 : if (is_diconnected)
1117 1701 : *is_diconnected = 1;
1118 : } else {
1119 : handle_invalid_file(c, FILE_HAS_NO_DENT,
1120 : file, NULL);
1121 : }
1122 : return 0;
1123 : }
1124 : }
1125 :
1126 222276549 : if (file->ino.is_xattr) {
1127 77238205 : if (!parent_file) {
1128 : /* Host inode is not found. */
1129 : handle_invalid_file(c, XATTR_HAS_NO_HOST, file, NULL);
1130 : return 0;
1131 : }
1132 77200236 : if (parent_file->ino.is_xattr) {
1133 : /* Host cannot be a xattr file. */
1134 : handle_invalid_file(c, XATTR_HAS_WRONG_HOST, file, parent_file);
1135 : return 0;
1136 : }
1137 :
1138 77200230 : insert_xattr_file(c, file, parent_file);
1139 77200230 : if (parent_file->ino.is_encrypted) {
1140 69490300 : int nlen = min(dent_node->nlen,
1141 : strlen(UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT));
1142 :
1143 69490300 : if (!strncmp(dent_node->name,
1144 : UBIFS_XATTR_NAME_ENCRYPTION_CONTEXT, nlen))
1145 58052778 : parent_file->has_encrypted_info = true;
1146 : }
1147 : } else {
1148 145038344 : if (parent_file && !S_ISDIR(parent_file->ino.mode)) {
1149 : /* Parent file should be directory. */
1150 27792 : if (type == UBIFS_ITYPE_REG) {
1151 8272 : handle_invalid_file(c, FILE_IS_DISCONNECTED,
1152 : file, NULL);
1153 8272 : if (FSCK(c)->mode != REBUILD_MODE) {
1154 : /* Delete dentries for the disconnected file. */
1155 7 : int err = delete_dent_nodes(c, file, 0);
1156 7 : if (err)
1157 : return err;
1158 : }
1159 8272 : if (is_diconnected)
1160 7 : *is_diconnected = 1;
1161 : }
1162 : return 0;
1163 : }
1164 :
1165 : /*
1166 : * Since xattr files are checked in first round, so all
1167 : * non-xattr files's @has_encrypted_info fields have been
1168 : * initialized.
1169 : */
1170 145010552 : if (file->ino.is_encrypted && !file->has_encrypted_info) {
1171 : handle_invalid_file(c, FILE_HAS_NO_ENCRYPT, file, NULL);
1172 : return 0;
1173 : }
1174 : }
1175 :
1176 : return 1;
1177 : }
1178 :
1179 156065409 : static bool dentry_is_reachable(struct ubifs_info *c,
1180 : struct scanned_dent_node *dent_node,
1181 : struct list_head *path_list,
1182 : struct rb_root *file_tree)
1183 : {
1184 310274481 : struct scanned_file *parent_file = NULL;
1185 : struct scanned_dent_node *dn, *parent_dent;
1186 : struct rb_node *p;
1187 :
1188 : /* Check whether the path is cyclical. */
1189 510806604 : list_for_each_entry(dn, path_list, list) {
1190 200532128 : if (dn == dent_node)
1191 : return false;
1192 : }
1193 :
1194 : /* Quick path, dentry has already been checked as reachable. */
1195 310274476 : if (dent_node->can_be_found)
1196 : return true;
1197 :
1198 156215635 : dent_node->can_be_found = true;
1199 312431270 : list_add(&dent_node->list, path_list);
1200 :
1201 468646905 : parent_file = lookup_file(file_tree, key_inum(c, &dent_node->key));
1202 : /* Parent dentry is not found, unreachable. */
1203 156215635 : if (!parent_file)
1204 : return false;
1205 :
1206 : /* Parent dentry is '/', reachable. */
1207 156105961 : if (parent_file->inum == UBIFS_ROOT_INO)
1208 : return true;
1209 :
1210 156087827 : p = rb_first(&parent_file->dent_nodes);
1211 156087827 : if (!p)
1212 : return false;
1213 154209072 : parent_dent = rb_entry(p, struct scanned_dent_node, rb);
1214 :
1215 154209072 : return dentry_is_reachable(c, parent_dent, path_list, file_tree);
1216 : }
1217 :
1218 : /**
1219 : * file_is_reachable - whether the file can be found from '/'.
1220 : * @c: UBIFS file-system description object
1221 : * @file: file object
1222 : * @file_tree: tree of all scanned files
1223 : *
1224 : * This function iterates all directory entries in given @file and checks
1225 : * whether each dentry is reachable. All unreachable directory entries will
1226 : * be removed.
1227 : */
1228 144996797 : bool file_is_reachable(struct ubifs_info *c, struct scanned_file *file,
1229 : struct rb_root *file_tree)
1230 : {
1231 : struct rb_node *node;
1232 : struct scanned_dent_node *dent_node;
1233 :
1234 144996797 : if (file->inum == UBIFS_ROOT_INO)
1235 : goto reachable;
1236 :
1237 144994830 : retry:
1238 602120478 : for (node = rb_first(&file->dent_nodes); node; node = rb_next(node)) {
1239 156065409 : LIST_HEAD(path_list);
1240 :
1241 156065409 : dent_node = rb_entry(node, struct scanned_dent_node, rb);
1242 :
1243 156065409 : if (dentry_is_reachable(c, dent_node, &path_list, file_tree))
1244 154076975 : continue;
1245 :
1246 4227744 : while (!list_empty(&path_list)) {
1247 2239310 : dent_node = list_entry(path_list.next,
1248 : struct scanned_dent_node, list);
1249 :
1250 4478620 : handle_invalid_file(c, DENTRY_IS_UNREACHABLE,
1251 : dent_node->file, dent_node);
1252 2239310 : if (FSCK(c)->mode != REBUILD_MODE) {
1253 62053 : int err = delete_node(c, &dent_node->key,
1254 : dent_node->header.lnum,
1255 : dent_node->header.offs);
1256 62053 : if (err)
1257 0 : return err;
1258 : }
1259 2239310 : dbg_fsck("remove unreachable dentry %s, in %s",
1260 : c->encrypted && !file->ino.is_xattr ?
1261 : "<encrypted>" : dent_node->name, c->dev_name);
1262 4478620 : list_del(&dent_node->list);
1263 2239310 : rb_erase(&dent_node->rb, &dent_node->file->dent_nodes);
1264 : kfree(dent_node);
1265 : }
1266 :
1267 : /* Since dentry node is removed from rb-tree, rescan rb-tree. */
1268 1988434 : goto retry;
1269 : }
1270 :
1271 144994830 : if (!rb_first(&file->dent_nodes)) {
1272 1975846 : if (S_ISREG(file->ino.mode))
1273 : handle_invalid_file(c, FILE_IS_DISCONNECTED, file, NULL);
1274 : else
1275 : handle_invalid_file(c, FILE_HAS_NO_DENT, file, NULL);
1276 1975846 : dbg_fsck("file %lu is unreachable, in %s", file->inum, c->dev_name);
1277 : return false;
1278 : }
1279 :
1280 143020951 : reachable:
1281 143020951 : dbg_fsck("file %lu is reachable, in %s", file->inum, c->dev_name);
1282 : return true;
1283 : }
1284 :
1285 : /**
1286 : * calculate_file_info - calculate the information of file
1287 : * @c: UBIFS file-system description object
1288 : * @file: file object
1289 : * @file_tree: tree of all scanned files
1290 : *
1291 : * This function calculates file information according to dentry nodes,
1292 : * data nodes and truncation node. The calculated informaion will be used
1293 : * to correct inode node.
1294 : */
1295 143039281 : static int calculate_file_info(struct ubifs_info *c, struct scanned_file *file,
1296 : struct rb_root *file_tree)
1297 : {
1298 143039281 : int nlink = 0;
1299 143039281 : bool corrupted_truncation = false;
1300 143039281 : unsigned long long ino_sqnum, trun_size = 0, new_size = 0, trun_sqnum = 0;
1301 : struct rb_node *node;
1302 : struct scanned_file *parent_file, *xattr_file;
1303 : struct scanned_dent_node *dent_node;
1304 : struct scanned_data_node *data_node;
1305 143039281 : LIST_HEAD(drop_list);
1306 :
1307 219021714 : for (node = rb_first(&file->xattr_files); node; node = rb_next(node)) {
1308 75982433 : xattr_file = rb_entry(node, struct scanned_file, rb);
1309 75982433 : dent_node = rb_entry(rb_first(&xattr_file->dent_nodes),
1310 : struct scanned_dent_node, rb);
1311 :
1312 75982433 : ubifs_assert(c, xattr_file->ino.is_xattr);
1313 75982433 : ubifs_assert(c, !rb_first(&xattr_file->xattr_files));
1314 75982433 : xattr_file->calc_nlink = 1;
1315 75982433 : xattr_file->calc_size = xattr_file->ino.size;
1316 :
1317 75982433 : file->calc_xcnt += 1;
1318 75982433 : file->calc_xsz += CALC_DENT_SIZE(dent_node->nlen);
1319 75982433 : file->calc_xsz += CALC_XATTR_BYTES(xattr_file->ino.size);
1320 75982433 : file->calc_xnms += dent_node->nlen;
1321 : }
1322 :
1323 143039281 : if (file->inum == UBIFS_ROOT_INO) {
1324 1967 : file->calc_nlink += 2;
1325 1967 : file->calc_size += UBIFS_INO_NODE_SZ;
1326 1967 : return 0;
1327 : }
1328 :
1329 143037314 : if (S_ISDIR(file->ino.mode)) {
1330 24471095 : file->calc_nlink += 2;
1331 24471095 : file->calc_size += UBIFS_INO_NODE_SZ;
1332 :
1333 24471095 : dent_node = rb_entry(rb_first(&file->dent_nodes),
1334 : struct scanned_dent_node, rb);
1335 73413285 : parent_file = lookup_file(file_tree, key_inum(c, &dent_node->key));
1336 24471095 : if (!parent_file) {
1337 0 : ubifs_assert(c, 0);
1338 0 : return 0;
1339 : }
1340 24471095 : parent_file->calc_nlink += 1;
1341 24471095 : parent_file->calc_size += CALC_DENT_SIZE(dent_node->nlen);
1342 24471095 : return 0;
1343 : }
1344 :
1345 248071449 : for (node = rb_first(&file->dent_nodes); node; node = rb_next(node)) {
1346 129505230 : nlink++;
1347 :
1348 129505230 : dent_node = rb_entry(node, struct scanned_dent_node, rb);
1349 :
1350 388515690 : parent_file = lookup_file(file_tree, key_inum(c, &dent_node->key));
1351 129505230 : if (!parent_file) {
1352 0 : ubifs_assert(c, 0);
1353 0 : return 0;
1354 : }
1355 129505230 : parent_file->calc_size += CALC_DENT_SIZE(dent_node->nlen);
1356 : }
1357 118566219 : file->calc_nlink = nlink;
1358 :
1359 118566219 : if (!S_ISREG(file->ino.mode)) {
1360 : /* No need to verify i_size for symlink/sock/block/char/fifo. */
1361 71171032 : file->calc_size = file->ino.size;
1362 71171032 : return 0;
1363 : }
1364 :
1365 : /*
1366 : * Process i_size and data content, following situations should
1367 : * be considered:
1368 : * 1. Sequential writing or overwriting, i_size should be
1369 : * max(i_size, data node size), pick larger sqnum one from
1370 : * data nodes with same block index.
1371 : * 2. Mixed truncation and writing, i_size depends on the latest
1372 : * truncation node or inode node or last data node, pick data
1373 : * nodes which are not truncated.
1374 : * 3. Setting bigger i_size attr, pick inode size or biggest
1375 : * i_size calculated by data nodes.
1376 : */
1377 47395187 : if (file->trun.header.exist) {
1378 200385 : trun_size = file->trun.new_size;
1379 200385 : trun_sqnum = file->trun.header.sqnum;
1380 : }
1381 47395187 : ino_sqnum = file->ino.header.sqnum;
1382 2249213419 : for (node = rb_first(&file->data_nodes); node; node = rb_next(node)) {
1383 : unsigned long long d_sz, d_sqnum;
1384 : unsigned int block_no;
1385 :
1386 2201818232 : data_node = rb_entry(node, struct scanned_data_node, rb);
1387 :
1388 2201818232 : d_sqnum = data_node->header.sqnum;
1389 4403636464 : block_no = key_block(c, &data_node->key);
1390 2201818232 : d_sz = data_node->size + block_no * UBIFS_BLOCK_SIZE;
1391 2201818232 : if ((trun_sqnum > d_sqnum && trun_size < d_sz) ||
1392 1992269851 : (ino_sqnum > d_sqnum && file->ino.size < d_sz)) {
1393 : /*
1394 : * The truncated data nodes are not gced after
1395 : * truncating, just remove them.
1396 : */
1397 12732043 : list_add(&data_node->list, &drop_list);
1398 : } else {
1399 2189086189 : new_size = max_t(unsigned long long, new_size, d_sz);
1400 : }
1401 : }
1402 : /*
1403 : * Truncation node is written successful, but inode node is not. It
1404 : * won't happen because inode node is written before truncation node
1405 : * according to ubifs_jnl_truncate(), unless only inode is corrupted.
1406 : * In this case, data nodes could have been removed in history mounting
1407 : * recovery, so i_size needs to be updated.
1408 : */
1409 47395187 : if (trun_sqnum > ino_sqnum && trun_size < file->ino.size) {
1410 26 : if (trun_size < new_size) {
1411 : corrupted_truncation = true;
1412 : /*
1413 : * Appendant writing after truncation and newest inode
1414 : * is not fell on disk.
1415 : */
1416 : goto update_isize;
1417 : }
1418 :
1419 : /*
1420 : * Overwriting happens after truncation and newest inode is
1421 : * not fell on disk.
1422 : */
1423 0 : file->calc_size = trun_size;
1424 0 : goto drop_data;
1425 : }
1426 47395161 : update_isize:
1427 : /*
1428 : * The file cannot use 'new_size' directly when the file may have ever
1429 : * been set i_size. For example:
1430 : * 1. echo 123 > file # i_size = 4
1431 : * 2. truncate -s 100 file # i_size = 100
1432 : * After scanning, new_size is 4. Apperantly the size of 'file' should
1433 : * be 100. So, the calculated new_size according to data nodes should
1434 : * only be used for extending i_size, like ubifs_recover_size() does.
1435 : */
1436 47395187 : if (new_size > file->ino.size || corrupted_truncation)
1437 1712 : file->calc_size = new_size;
1438 : else
1439 47393475 : file->calc_size = file->ino.size;
1440 :
1441 : drop_data:
1442 60127230 : while (!list_empty(&drop_list)) {
1443 12732043 : data_node = list_entry(drop_list.next, struct scanned_data_node,
1444 : list);
1445 :
1446 12732043 : if (FSCK(c)->mode != REBUILD_MODE) {
1447 : /*
1448 : * Don't ask, inconsistent file correcting will be
1449 : * asked in function correct_file_info().
1450 : */
1451 54 : int err = delete_node(c, &data_node->key,
1452 : data_node->header.lnum, data_node->header.offs);
1453 54 : if (err)
1454 : return err;
1455 : }
1456 25464086 : list_del(&data_node->list);
1457 12732043 : rb_erase(&data_node->rb, &file->data_nodes);
1458 : kfree(data_node);
1459 : }
1460 :
1461 : return 0;
1462 : }
1463 :
1464 : /**
1465 : * correct_file_info - correct the information of file
1466 : * @c: UBIFS file-system description object
1467 : * @file: file object
1468 : *
1469 : * This function corrects file information according to calculated fields,
1470 : * eg. 'calc_nlink', 'calc_xcnt', 'calc_xsz', 'calc_xnms' and 'calc_size'.
1471 : * Corrected inode node will be re-written.
1472 : */
1473 218151565 : static int correct_file_info(struct ubifs_info *c, struct scanned_file *file)
1474 : {
1475 : uint32_t crc;
1476 : int err, lnum, len;
1477 : struct rb_node *node;
1478 : struct ubifs_ino_node *ino;
1479 : struct scanned_file *xattr_file;
1480 :
1481 293799683 : for (node = rb_first(&file->xattr_files); node; node = rb_next(node)) {
1482 75648118 : xattr_file = rb_entry(node, struct scanned_file, rb);
1483 :
1484 75648118 : err = correct_file_info(c, xattr_file);
1485 75648118 : if (err)
1486 : return err;
1487 : }
1488 :
1489 218151565 : if (file->calc_nlink == file->ino.nlink &&
1490 : file->calc_xcnt == file->ino.xcnt &&
1491 217956949 : file->calc_xsz == file->ino.xsz &&
1492 217956846 : file->calc_xnms == file->ino.xnms &&
1493 217956846 : file->calc_size == file->ino.size)
1494 : return 0;
1495 :
1496 321564 : handle_invalid_file(c, FILE_IS_INCONSISTENT, file, NULL);
1497 321564 : lnum = file->ino.header.lnum;
1498 321564 : dbg_fsck("correct file(inum:%lu type:%s), nlink %u->%u, xattr cnt %u->%u, xattr size %u->%u, xattr names %u->%u, size %llu->%llu, at %d:%d, in %s",
1499 : file->inum, file->ino.is_xattr ? "xattr" :
1500 : ubifs_get_type_name(ubifs_get_dent_type(file->ino.mode)),
1501 : file->ino.nlink, file->calc_nlink,
1502 : file->ino.xcnt, file->calc_xcnt,
1503 : file->ino.xsz, file->calc_xsz,
1504 : file->ino.xnms, file->calc_xnms,
1505 : file->ino.size, file->calc_size,
1506 : lnum, file->ino.header.offs, c->dev_name);
1507 :
1508 321564 : err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 0);
1509 321564 : if (err && err != -EBADMSG)
1510 : return err;
1511 :
1512 321564 : ino = c->sbuf + file->ino.header.offs;
1513 321564 : ino->nlink = cpu_to_le32(file->calc_nlink);
1514 321564 : ino->xattr_cnt = cpu_to_le32(file->calc_xcnt);
1515 321564 : ino->xattr_size = cpu_to_le32(file->calc_xsz);
1516 321564 : ino->xattr_names = cpu_to_le32(file->calc_xnms);
1517 321564 : ino->size = cpu_to_le64(file->calc_size);
1518 321564 : len = le32_to_cpu(ino->ch.len);
1519 643128 : crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1520 321564 : ino->ch.crc = cpu_to_le32(crc);
1521 :
1522 : /* Atomically write the fixed LEB back again */
1523 321564 : return ubifs_leb_change(c, lnum, c->sbuf, c->leb_size);
1524 : }
1525 :
1526 : /**
1527 : * check_and_correct_files - check and correct information of files.
1528 : * @c: UBIFS file-system description object
1529 : *
1530 : * This function does similar things with dbg_check_filesystem(), besides,
1531 : * it also corrects file information if the calculated information is not
1532 : * consistent with information from flash.
1533 : */
1534 1979 : int check_and_correct_files(struct ubifs_info *c)
1535 : {
1536 : int err;
1537 : struct rb_node *node;
1538 : struct scanned_file *file;
1539 1979 : struct rb_root *tree = &FSCK(c)->scanned_files;
1540 :
1541 143022930 : for (node = rb_first(tree); node; node = rb_next(node)) {
1542 143020951 : file = rb_entry(node, struct scanned_file, rb);
1543 :
1544 143020951 : err = calculate_file_info(c, file, tree);
1545 143020951 : if (err)
1546 : return err;
1547 : }
1548 :
1549 142487036 : for (node = rb_first(tree); node; node = rb_next(node)) {
1550 142485117 : file = rb_entry(node, struct scanned_file, rb);
1551 :
1552 142485117 : err = correct_file_info(c, file);
1553 142485117 : if (err)
1554 : return err;
1555 : }
1556 :
1557 3838 : if (list_empty(&FSCK(c)->disconnected_files))
1558 : return 0;
1559 :
1560 65 : ubifs_assert(c, FSCK(c)->mode != REBUILD_MODE);
1561 18395 : list_for_each_entry(file, &FSCK(c)->disconnected_files, list) {
1562 18330 : err = calculate_file_info(c, file, tree);
1563 18330 : if (err)
1564 : return err;
1565 :
1566 : /* Reset disconnected file's nlink as one. */
1567 18330 : file->calc_nlink = 1;
1568 18330 : err = correct_file_info(c, file);
1569 18330 : if (err)
1570 : return err;
1571 : }
1572 :
1573 : return 0;
1574 : }
|