hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/ubifs/tnc.c
....@@ -1,20 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * This file is part of UBIFS.
34 *
45 * 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
186 *
197 * Authors: Adrian Hunter
208 * Artem Bityutskiy (Битюцкий Артём)
....@@ -35,7 +23,7 @@
3523 #include "ubifs.h"
3624
3725 static int try_read_node(const struct ubifs_info *c, void *buf, int type,
38
- int len, int lnum, int offs);
26
+ struct ubifs_zbranch *zbr);
3927 static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
4028 struct ubifs_zbranch *zbr, void *node);
4129
....@@ -55,6 +43,33 @@
5543 NAME_GREATER = 2,
5644 NOT_ON_MEDIA = 3,
5745 };
46
+
47
+static void do_insert_old_idx(struct ubifs_info *c,
48
+ struct ubifs_old_idx *old_idx)
49
+{
50
+ struct ubifs_old_idx *o;
51
+ struct rb_node **p, *parent = NULL;
52
+
53
+ p = &c->old_idx.rb_node;
54
+ while (*p) {
55
+ parent = *p;
56
+ o = rb_entry(parent, struct ubifs_old_idx, rb);
57
+ if (old_idx->lnum < o->lnum)
58
+ p = &(*p)->rb_left;
59
+ else if (old_idx->lnum > o->lnum)
60
+ p = &(*p)->rb_right;
61
+ else if (old_idx->offs < o->offs)
62
+ p = &(*p)->rb_left;
63
+ else if (old_idx->offs > o->offs)
64
+ p = &(*p)->rb_right;
65
+ else {
66
+ ubifs_err(c, "old idx added twice!");
67
+ kfree(old_idx);
68
+ }
69
+ }
70
+ rb_link_node(&old_idx->rb, parent, p);
71
+ rb_insert_color(&old_idx->rb, &c->old_idx);
72
+}
5873
5974 /**
6075 * insert_old_idx - record an index node obsoleted since the last commit start.
....@@ -81,35 +96,15 @@
8196 */
8297 static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
8398 {
84
- struct ubifs_old_idx *old_idx, *o;
85
- struct rb_node **p, *parent = NULL;
99
+ struct ubifs_old_idx *old_idx;
86100
87101 old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
88102 if (unlikely(!old_idx))
89103 return -ENOMEM;
90104 old_idx->lnum = lnum;
91105 old_idx->offs = offs;
106
+ do_insert_old_idx(c, old_idx);
92107
93
- p = &c->old_idx.rb_node;
94
- while (*p) {
95
- parent = *p;
96
- o = rb_entry(parent, struct ubifs_old_idx, rb);
97
- if (lnum < o->lnum)
98
- p = &(*p)->rb_left;
99
- else if (lnum > o->lnum)
100
- p = &(*p)->rb_right;
101
- else if (offs < o->offs)
102
- p = &(*p)->rb_left;
103
- else if (offs > o->offs)
104
- p = &(*p)->rb_right;
105
- else {
106
- ubifs_err(c, "old idx added twice!");
107
- kfree(old_idx);
108
- return 0;
109
- }
110
- }
111
- rb_link_node(&old_idx->rb, parent, p);
112
- rb_insert_color(&old_idx->rb, &c->old_idx);
113108 return 0;
114109 }
115110
....@@ -211,23 +206,6 @@
211206 __set_bit(DIRTY_ZNODE, &zn->flags);
212207 __clear_bit(COW_ZNODE, &zn->flags);
213208
214
- ubifs_assert(c, !ubifs_zn_obsolete(znode));
215
- __set_bit(OBSOLETE_ZNODE, &znode->flags);
216
-
217
- if (znode->level != 0) {
218
- int i;
219
- const int n = zn->child_cnt;
220
-
221
- /* The children now have new parent */
222
- for (i = 0; i < n; i++) {
223
- struct ubifs_zbranch *zbr = &zn->zbranch[i];
224
-
225
- if (zbr->znode)
226
- zbr->znode->parent = zn;
227
- }
228
- }
229
-
230
- atomic_long_inc(&c->dirty_zn_cnt);
231209 return zn;
232210 }
233211
....@@ -243,6 +221,42 @@
243221 {
244222 c->calc_idx_sz -= ALIGN(dirt, 8);
245223 return ubifs_add_dirt(c, lnum, dirt);
224
+}
225
+
226
+/**
227
+ * replace_znode - replace old znode with new znode.
228
+ * @c: UBIFS file-system description object
229
+ * @new_zn: new znode
230
+ * @old_zn: old znode
231
+ * @zbr: the branch of parent znode
232
+ *
233
+ * Replace old znode with new znode in TNC.
234
+ */
235
+static void replace_znode(struct ubifs_info *c, struct ubifs_znode *new_zn,
236
+ struct ubifs_znode *old_zn, struct ubifs_zbranch *zbr)
237
+{
238
+ ubifs_assert(c, !ubifs_zn_obsolete(old_zn));
239
+ __set_bit(OBSOLETE_ZNODE, &old_zn->flags);
240
+
241
+ if (old_zn->level != 0) {
242
+ int i;
243
+ const int n = new_zn->child_cnt;
244
+
245
+ /* The children now have new parent */
246
+ for (i = 0; i < n; i++) {
247
+ struct ubifs_zbranch *child = &new_zn->zbranch[i];
248
+
249
+ if (child->znode)
250
+ child->znode->parent = new_zn;
251
+ }
252
+ }
253
+
254
+ zbr->znode = new_zn;
255
+ zbr->lnum = 0;
256
+ zbr->offs = 0;
257
+ zbr->len = 0;
258
+
259
+ atomic_long_inc(&c->dirty_zn_cnt);
246260 }
247261
248262 /**
....@@ -277,21 +291,32 @@
277291 return zn;
278292
279293 if (zbr->len) {
280
- err = insert_old_idx(c, zbr->lnum, zbr->offs);
281
- if (unlikely(err))
282
- return ERR_PTR(err);
294
+ struct ubifs_old_idx *old_idx;
295
+
296
+ old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
297
+ if (unlikely(!old_idx)) {
298
+ err = -ENOMEM;
299
+ goto out;
300
+ }
301
+ old_idx->lnum = zbr->lnum;
302
+ old_idx->offs = zbr->offs;
303
+
283304 err = add_idx_dirt(c, zbr->lnum, zbr->len);
284
- } else
285
- err = 0;
305
+ if (err) {
306
+ kfree(old_idx);
307
+ goto out;
308
+ }
286309
287
- zbr->znode = zn;
288
- zbr->lnum = 0;
289
- zbr->offs = 0;
290
- zbr->len = 0;
310
+ do_insert_old_idx(c, old_idx);
311
+ }
291312
292
- if (unlikely(err))
293
- return ERR_PTR(err);
313
+ replace_znode(c, zn, znode, zbr);
314
+
294315 return zn;
316
+
317
+out:
318
+ kfree(zn);
319
+ return ERR_PTR(err);
295320 }
296321
297322 /**
....@@ -372,7 +397,6 @@
372397 /**
373398 * lnc_free - remove a leaf node from the leaf node cache.
374399 * @zbr: zbranch of leaf node
375
- * @node: leaf node
376400 */
377401 static void lnc_free(struct ubifs_zbranch *zbr)
378402 {
....@@ -433,9 +457,7 @@
433457 * @c: UBIFS file-system description object
434458 * @buf: buffer to read to
435459 * @type: node type
436
- * @len: node length (not aligned)
437
- * @lnum: LEB number of node to read
438
- * @offs: offset of node to read
460
+ * @zbr: the zbranch describing the node to read
439461 *
440462 * This function tries to read a node of known type and length, checks it and
441463 * stores it in @buf. This function returns %1 if a node is present and %0 if
....@@ -453,8 +475,11 @@
453475 * journal nodes may potentially be corrupted, so checking is required.
454476 */
455477 static int try_read_node(const struct ubifs_info *c, void *buf, int type,
456
- int len, int lnum, int offs)
478
+ struct ubifs_zbranch *zbr)
457479 {
480
+ int len = zbr->len;
481
+ int lnum = zbr->lnum;
482
+ int offs = zbr->offs;
458483 int err, node_len;
459484 struct ubifs_ch *ch = buf;
460485 uint32_t crc, node_crc;
....@@ -478,14 +503,19 @@
478503 if (node_len != len)
479504 return 0;
480505
481
- if (type == UBIFS_DATA_NODE && c->no_chk_data_crc && !c->mounting &&
482
- !c->remounting_rw)
483
- return 1;
506
+ if (type != UBIFS_DATA_NODE || !c->no_chk_data_crc || c->mounting ||
507
+ c->remounting_rw) {
508
+ crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
509
+ node_crc = le32_to_cpu(ch->crc);
510
+ if (crc != node_crc)
511
+ return 0;
512
+ }
484513
485
- crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
486
- node_crc = le32_to_cpu(ch->crc);
487
- if (crc != node_crc)
514
+ err = ubifs_node_check_hash(c, buf, zbr->hash);
515
+ if (err) {
516
+ ubifs_bad_hash(c, buf, zbr->hash, lnum, offs);
488517 return 0;
518
+ }
489519
490520 return 1;
491521 }
....@@ -507,8 +537,7 @@
507537
508538 dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs);
509539
510
- ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
511
- zbr->offs);
540
+ ret = try_read_node(c, node, key_type(c, key), zbr);
512541 if (ret == 1) {
513542 union ubifs_key node_key;
514543 struct ubifs_dent_node *dent = node;
....@@ -899,7 +928,7 @@
899928 int adding)
900929 {
901930 struct ubifs_znode *o_znode = NULL, *znode = *zn;
902
- int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
931
+ int o_n, err, cmp, unsure = 0, nn = *n;
903932
904933 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
905934 if (unlikely(cmp < 0))
....@@ -1521,8 +1550,8 @@
15211550 */
15221551 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
15231552 {
1524
- int n, err = 0, lnum = -1, uninitialized_var(offs);
1525
- int uninitialized_var(len);
1553
+ int n, err = 0, lnum = -1, offs;
1554
+ int len;
15261555 unsigned int block = key_block(c, &bu->key);
15271556 struct ubifs_znode *znode;
15281557
....@@ -1711,6 +1740,12 @@
17111740 if (err) {
17121741 ubifs_err(c, "expected node type %d", UBIFS_DATA_NODE);
17131742 goto out;
1743
+ }
1744
+
1745
+ err = ubifs_node_check_hash(c, buf, zbr->hash);
1746
+ if (err) {
1747
+ ubifs_bad_hash(c, buf, zbr->hash, zbr->lnum, zbr->offs);
1748
+ return err;
17141749 }
17151750
17161751 len = le32_to_cpu(ch->len);
....@@ -2266,13 +2301,14 @@
22662301 * @lnum: LEB number of node
22672302 * @offs: node offset
22682303 * @len: node length
2304
+ * @hash: The hash over the node
22692305 *
22702306 * This function adds a node with key @key to TNC. The node may be new or it may
22712307 * obsolete some existing one. Returns %0 on success or negative error code on
22722308 * failure.
22732309 */
22742310 int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
2275
- int offs, int len)
2311
+ int offs, int len, const u8 *hash)
22762312 {
22772313 int found, n, err = 0;
22782314 struct ubifs_znode *znode;
....@@ -2287,6 +2323,7 @@
22872323 zbr.lnum = lnum;
22882324 zbr.offs = offs;
22892325 zbr.len = len;
2326
+ ubifs_copy_hash(c, hash, zbr.hash);
22902327 key_copy(c, key, &zbr.key);
22912328 err = tnc_insert(c, znode, &zbr, n + 1);
22922329 } else if (found == 1) {
....@@ -2297,6 +2334,7 @@
22972334 zbr->lnum = lnum;
22982335 zbr->offs = offs;
22992336 zbr->len = len;
2337
+ ubifs_copy_hash(c, hash, zbr->hash);
23002338 } else
23012339 err = found;
23022340 if (!err)
....@@ -2398,13 +2436,14 @@
23982436 * @lnum: LEB number of node
23992437 * @offs: node offset
24002438 * @len: node length
2439
+ * @hash: The hash over the node
24012440 * @nm: node name
24022441 *
24032442 * This is the same as 'ubifs_tnc_add()' but it should be used with keys which
24042443 * may have collisions, like directory entry keys.
24052444 */
24062445 int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
2407
- int lnum, int offs, int len,
2446
+ int lnum, int offs, int len, const u8 *hash,
24082447 const struct fscrypt_name *nm)
24092448 {
24102449 int found, n, err = 0;
....@@ -2447,6 +2486,7 @@
24472486 zbr->lnum = lnum;
24482487 zbr->offs = offs;
24492488 zbr->len = len;
2489
+ ubifs_copy_hash(c, hash, zbr->hash);
24502490 goto out_unlock;
24512491 }
24522492 }
....@@ -2458,6 +2498,7 @@
24582498 zbr.lnum = lnum;
24592499 zbr.offs = offs;
24602500 zbr.len = len;
2501
+ ubifs_copy_hash(c, hash, zbr.hash);
24612502 key_copy(c, key, &zbr.key);
24622503 err = tnc_insert(c, znode, &zbr, n + 1);
24632504 if (err)
....@@ -2880,6 +2921,7 @@
28802921 err = PTR_ERR(xent);
28812922 if (err == -ENOENT)
28822923 break;
2924
+ kfree(pxent);
28832925 return err;
28842926 }
28852927
....@@ -2893,6 +2935,7 @@
28932935 fname_len(&nm) = le16_to_cpu(xent->nlen);
28942936 err = ubifs_tnc_remove_nm(c, &key1, &nm);
28952937 if (err) {
2938
+ kfree(pxent);
28962939 kfree(xent);
28972940 return err;
28982941 }
....@@ -2901,6 +2944,7 @@
29012944 highest_ino_key(c, &key2, xattr_inum);
29022945 err = ubifs_tnc_remove_range(c, &key1, &key2);
29032946 if (err) {
2947
+ kfree(pxent);
29042948 kfree(xent);
29052949 return err;
29062950 }
....@@ -3046,6 +3090,21 @@
30463090 cnext = cnext->cnext;
30473091 if (ubifs_zn_obsolete(znode))
30483092 kfree(znode);
3093
+ else if (!ubifs_zn_cow(znode)) {
3094
+ /*
3095
+ * Don't forget to update clean znode count after
3096
+ * committing failed, because ubifs will check this
3097
+ * count while closing tnc. Non-obsolete znode could
3098
+ * be re-dirtied during committing process, so dirty
3099
+ * flag is untrustable. The flag 'COW_ZNODE' is set
3100
+ * for each dirty znode before committing, and it is
3101
+ * cleared as long as the znode become clean, so we
3102
+ * can statistic clean znode count according to this
3103
+ * flag.
3104
+ */
3105
+ atomic_long_inc(&c->clean_zn_cnt);
3106
+ atomic_long_inc(&ubifs_clean_zn_cnt);
3107
+ }
30493108 } while (cnext && cnext != c->cnext);
30503109 }
30513110
....@@ -3461,7 +3520,7 @@
34613520 /**
34623521 * dbg_check_inode_size - check if inode size is correct.
34633522 * @c: UBIFS file-system description object
3464
- * @inum: inode number
3523
+ * @inode: inode to check
34653524 * @size: inode size
34663525 *
34673526 * This function makes sure that the inode size (@size) is correct and it does