blob: 1c6ceb6265aa389cd8c78d957a7c64f8036aa124 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file contains journal replay code. It runs when the file-system is being
25 * mounted and requires no locking.
26 *
27 * The larger is the journal, the longer it takes to scan it, so the longer it
28 * takes to mount UBIFS. This is why the journal has limited size which may be
29 * changed depending on the system requirements. But a larger journal gives
30 * faster I/O speed because it writes the index less frequently. So this is a
31 * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
32 * larger is the journal, the more memory its index may consume.
33 */
34
35#include "ubifs.h"
Artem Bityutskiydebf12d2011-05-15 12:05:54 +030036#include <linux/list_sort.h>
Artem Bityutskiy1e517642008-07-14 19:08:37 +030037
Artem Bityutskiy1e517642008-07-14 19:08:37 +030038/**
Artem Bityutskiydebf12d2011-05-15 12:05:54 +030039 * struct replay_entry - replay list entry.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030040 * @lnum: logical eraseblock number of the node
41 * @offs: node offset
42 * @len: node length
Artem Bityutskiy074bcb92011-05-15 11:37:17 +030043 * @deletion: non-zero if this entry corresponds to a node deletion
Artem Bityutskiy1e517642008-07-14 19:08:37 +030044 * @sqnum: node sequence number
Artem Bityutskiydebf12d2011-05-15 12:05:54 +030045 * @list: links the replay list
Artem Bityutskiy1e517642008-07-14 19:08:37 +030046 * @key: node key
47 * @nm: directory entry name
48 * @old_size: truncation old size
49 * @new_size: truncation new size
Artem Bityutskiy1e517642008-07-14 19:08:37 +030050 *
Artem Bityutskiydebf12d2011-05-15 12:05:54 +030051 * The replay process first scans all buds and builds the replay list, then
52 * sorts the replay list in nodes sequence number order, and then inserts all
53 * the replay entries to the TNC.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030054 */
55struct replay_entry {
56 int lnum;
57 int offs;
58 int len;
Sascha Hauer16a26b22018-09-07 14:36:35 +020059 u8 hash[UBIFS_HASH_ARR_SZ];
Artem Bityutskiy074bcb92011-05-15 11:37:17 +030060 unsigned int deletion:1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030061 unsigned long long sqnum;
Artem Bityutskiydebf12d2011-05-15 12:05:54 +030062 struct list_head list;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030063 union ubifs_key key;
64 union {
Richard Weinbergerf4f61d22016-11-11 22:50:29 +010065 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030066 struct {
67 loff_t old_size;
68 loff_t new_size;
69 };
Artem Bityutskiy1e517642008-07-14 19:08:37 +030070 };
71};
72
73/**
74 * struct bud_entry - entry in the list of buds to replay.
75 * @list: next bud in the list
76 * @bud: bud description object
Artem Bityutskiy1e517642008-07-14 19:08:37 +030077 * @sqnum: reference node sequence number
Artem Bityutskiyaf1dd412011-05-15 11:14:57 +030078 * @free: free bytes in the bud
79 * @dirty: dirty bytes in the bud
Artem Bityutskiy1e517642008-07-14 19:08:37 +030080 */
81struct bud_entry {
82 struct list_head list;
83 struct ubifs_bud *bud;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030084 unsigned long long sqnum;
Artem Bityutskiyaf1dd412011-05-15 11:14:57 +030085 int free;
86 int dirty;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030087};
88
89/**
90 * set_bud_lprops - set free and dirty space used by a bud.
91 * @c: UBIFS file-system description object
Artem Bityutskiy074bcb92011-05-15 11:37:17 +030092 * @b: bud entry which describes the bud
93 *
94 * This function makes sure the LEB properties of bud @b are set correctly
95 * after the replay. Returns zero in case of success and a negative error code
96 * in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030097 */
Artem Bityutskiy074bcb92011-05-15 11:37:17 +030098static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030099{
100 const struct ubifs_lprops *lp;
101 int err = 0, dirty;
102
103 ubifs_get_lprops(c);
104
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300105 lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300106 if (IS_ERR(lp)) {
107 err = PTR_ERR(lp);
108 goto out;
109 }
110
111 dirty = lp->dirty;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300112 if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300113 /*
114 * The LEB was added to the journal with a starting offset of
115 * zero which means the LEB must have been empty. The LEB
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300116 * property values should be @lp->free == @c->leb_size and
117 * @lp->dirty == 0, but that is not the case. The reason is that
Artem Bityutskiy7a9c3e32011-05-13 13:02:00 +0300118 * the LEB had been garbage collected before it became the bud,
119 * and there was not commit inbetween. The garbage collector
120 * resets the free and dirty space without recording it
121 * anywhere except lprops, so if there was no commit then
122 * lprops does not have that information.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300123 *
124 * We do not need to adjust free space because the scan has told
125 * us the exact value which is recorded in the replay entry as
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300126 * @b->free.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300127 *
128 * However we do need to subtract from the dirty space the
129 * amount of space that the garbage collector reclaimed, which
130 * is the whole LEB minus the amount of space that was free.
131 */
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300132 dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300133 lp->free, lp->dirty);
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300134 dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300135 lp->free, lp->dirty);
136 dirty -= c->leb_size - lp->free;
137 /*
138 * If the replay order was perfect the dirty space would now be
Artem Bityutskiy7d4e9cc2009-03-20 19:11:12 +0200139 * zero. The order is not perfect because the journal heads
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +0200140 * race with each other. This is not a problem but is does mean
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300141 * that the dirty space may temporarily exceed c->leb_size
142 * during the replay.
143 */
144 if (dirty != 0)
Artem Bityutskiy3668b702012-08-27 16:56:58 +0300145 dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300146 b->bud->lnum, lp->free, lp->dirty, b->free,
147 b->dirty);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300148 }
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300149 lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300150 lp->flags | LPROPS_TAKEN, 0);
151 if (IS_ERR(lp)) {
152 err = PTR_ERR(lp);
153 goto out;
154 }
Artem Bityutskiy52c6e6f2011-04-25 18:46:31 +0300155
156 /* Make sure the journal head points to the latest bud */
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300157 err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200158 b->bud->lnum, c->leb_size - b->free);
Artem Bityutskiy52c6e6f2011-04-25 18:46:31 +0300159
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300160out:
161 ubifs_release_lprops(c);
162 return err;
163}
164
165/**
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300166 * set_buds_lprops - set free and dirty space for all replayed buds.
167 * @c: UBIFS file-system description object
168 *
169 * This function sets LEB properties for all replayed buds. Returns zero in
170 * case of success and a negative error code in case of failure.
171 */
172static int set_buds_lprops(struct ubifs_info *c)
173{
174 struct bud_entry *b;
175 int err;
176
177 list_for_each_entry(b, &c->replay_buds, list) {
178 err = set_bud_lprops(c, b);
179 if (err)
180 return err;
181 }
182
183 return 0;
184}
185
186/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300187 * trun_remove_range - apply a replay entry for a truncation to the TNC.
188 * @c: UBIFS file-system description object
189 * @r: replay entry of truncation
190 */
191static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
192{
193 unsigned min_blk, max_blk;
194 union ubifs_key min_key, max_key;
195 ino_t ino;
196
197 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
198 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
199 min_blk += 1;
200
201 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
202 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
203 max_blk -= 1;
204
205 ino = key_inum(c, &r->key);
206
207 data_key_init(c, &min_key, ino, min_blk);
208 data_key_init(c, &max_key, ino, max_blk);
209
210 return ubifs_tnc_remove_range(c, &min_key, &max_key);
211}
212
213/**
214 * apply_replay_entry - apply a replay entry to the TNC.
215 * @c: UBIFS file-system description object
216 * @r: replay entry to apply
217 *
218 * Apply a replay entry to the TNC.
219 */
220static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
221{
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300222 int err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300223
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200224 dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
225 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300226
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300227 if (is_hash_key(c, &r->key)) {
228 if (r->deletion)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300229 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
230 else
231 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
Sascha Hauer16a26b22018-09-07 14:36:35 +0200232 r->len, r->hash, &r->nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300233 } else {
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300234 if (r->deletion)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300235 switch (key_type(c, &r->key)) {
236 case UBIFS_INO_KEY:
237 {
238 ino_t inum = key_inum(c, &r->key);
239
240 err = ubifs_tnc_remove_ino(c, inum);
241 break;
242 }
243 case UBIFS_TRUN_KEY:
244 err = trun_remove_range(c, r);
245 break;
246 default:
247 err = ubifs_tnc_remove(c, &r->key);
248 break;
249 }
250 else
251 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
Sascha Hauer16a26b22018-09-07 14:36:35 +0200252 r->len, r->hash);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300253 if (err)
254 return err;
255
256 if (c->need_recovery)
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300257 err = ubifs_recover_size_accum(c, &r->key, r->deletion,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300258 r->new_size);
259 }
260
261 return err;
262}
263
264/**
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300265 * replay_entries_cmp - compare 2 replay entries.
266 * @priv: UBIFS file-system description object
267 * @a: first replay entry
Julia Lawallec037df2016-10-01 22:53:11 +0200268 * @b: second replay entry
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300269 *
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300270 * This is a comparios function for 'list_sort()' which compares 2 replay
271 * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
272 * greater sequence number and %-1 otherwise.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300273 */
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300274static int replay_entries_cmp(void *priv, struct list_head *a,
275 struct list_head *b)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300276{
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200277 struct ubifs_info *c = priv;
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300278 struct replay_entry *ra, *rb;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300279
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300280 cond_resched();
281 if (a == b)
282 return 0;
283
284 ra = list_entry(a, struct replay_entry, list);
285 rb = list_entry(b, struct replay_entry, list);
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200286 ubifs_assert(c, ra->sqnum != rb->sqnum);
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300287 if (ra->sqnum > rb->sqnum)
288 return 1;
289 return -1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300290}
291
292/**
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300293 * apply_replay_list - apply the replay list to the TNC.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300294 * @c: UBIFS file-system description object
295 *
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300296 * Apply all entries in the replay list to the TNC. Returns zero in case of
297 * success and a negative error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300298 */
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300299static int apply_replay_list(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300300{
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300301 struct replay_entry *r;
302 int err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300303
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300304 list_sort(c, &c->replay_list, &replay_entries_cmp);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300305
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300306 list_for_each_entry(r, &c->replay_list, list) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300307 cond_resched();
308
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300309 err = apply_replay_entry(c, r);
310 if (err)
311 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300312 }
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300313
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300314 return 0;
315}
316
317/**
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300318 * destroy_replay_list - destroy the replay.
319 * @c: UBIFS file-system description object
320 *
321 * Destroy the replay list.
322 */
323static void destroy_replay_list(struct ubifs_info *c)
324{
325 struct replay_entry *r, *tmp;
326
327 list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
328 if (is_hash_key(c, &r->key))
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100329 kfree(fname_name(&r->nm));
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300330 list_del(&r->list);
331 kfree(r);
332 }
333}
334
335/**
336 * insert_node - insert a node to the replay list
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300337 * @c: UBIFS file-system description object
338 * @lnum: node logical eraseblock number
339 * @offs: node offset
340 * @len: node length
341 * @key: node key
342 * @sqnum: sequence number
343 * @deletion: non-zero if this is a deletion
344 * @used: number of bytes in use in a LEB
345 * @old_size: truncation old size
346 * @new_size: truncation new size
347 *
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300348 * This function inserts a scanned non-direntry node to the replay list. The
349 * replay list contains @struct replay_entry elements, and we sort this list in
350 * sequence number order before applying it. The replay list is applied at the
351 * very end of the replay process. Since the list is sorted in sequence number
352 * order, the older modifications are applied first. This function returns zero
353 * in case of success and a negative error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300354 */
355static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
Sascha Hauer16a26b22018-09-07 14:36:35 +0200356 const u8 *hash, union ubifs_key *key,
357 unsigned long long sqnum, int deletion, int *used,
358 loff_t old_size, loff_t new_size)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300359{
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300360 struct replay_entry *r;
361
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200362 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300363
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300364 if (key_inum(c, key) >= c->highest_inum)
365 c->highest_inum = key_inum(c, key);
366
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300367 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
368 if (!r)
369 return -ENOMEM;
370
371 if (!deletion)
372 *used += ALIGN(len, 8);
373 r->lnum = lnum;
374 r->offs = offs;
375 r->len = len;
Sascha Hauer16a26b22018-09-07 14:36:35 +0200376 ubifs_copy_hash(c, hash, r->hash);
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300377 r->deletion = !!deletion;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300378 r->sqnum = sqnum;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300379 key_copy(c, key, &r->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300380 r->old_size = old_size;
381 r->new_size = new_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300382
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300383 list_add_tail(&r->list, &c->replay_list);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300384 return 0;
385}
386
387/**
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300388 * insert_dent - insert a directory entry node into the replay list.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300389 * @c: UBIFS file-system description object
390 * @lnum: node logical eraseblock number
391 * @offs: node offset
392 * @len: node length
393 * @key: node key
394 * @name: directory entry name
395 * @nlen: directory entry name length
396 * @sqnum: sequence number
397 * @deletion: non-zero if this is a deletion
398 * @used: number of bytes in use in a LEB
399 *
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300400 * This function inserts a scanned directory entry node or an extended
401 * attribute entry to the replay list. Returns zero in case of success and a
402 * negative error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300403 */
404static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
Sascha Hauer16a26b22018-09-07 14:36:35 +0200405 const u8 *hash, union ubifs_key *key,
406 const char *name, int nlen, unsigned long long sqnum,
407 int deletion, int *used)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300408{
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300409 struct replay_entry *r;
410 char *nbuf;
411
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200412 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300413 if (key_inum(c, key) >= c->highest_inum)
414 c->highest_inum = key_inum(c, key);
415
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300416 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
417 if (!r)
418 return -ENOMEM;
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300419
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300420 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
421 if (!nbuf) {
422 kfree(r);
423 return -ENOMEM;
424 }
425
426 if (!deletion)
427 *used += ALIGN(len, 8);
428 r->lnum = lnum;
429 r->offs = offs;
430 r->len = len;
Sascha Hauer16a26b22018-09-07 14:36:35 +0200431 ubifs_copy_hash(c, hash, r->hash);
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300432 r->deletion = !!deletion;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300433 r->sqnum = sqnum;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300434 key_copy(c, key, &r->key);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100435 fname_len(&r->nm) = nlen;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300436 memcpy(nbuf, name, nlen);
437 nbuf[nlen] = '\0';
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100438 fname_name(&r->nm) = nbuf;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300439
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300440 list_add_tail(&r->list, &c->replay_list);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300441 return 0;
442}
443
444/**
445 * ubifs_validate_entry - validate directory or extended attribute entry node.
446 * @c: UBIFS file-system description object
447 * @dent: the node to validate
448 *
449 * This function validates directory or extended attribute entry node @dent.
450 * Returns zero if the node is all right and a %-EINVAL if not.
451 */
452int ubifs_validate_entry(struct ubifs_info *c,
453 const struct ubifs_dent_node *dent)
454{
455 int key_type = key_type_flash(c, dent->key);
456 int nlen = le16_to_cpu(dent->nlen);
457
458 if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
459 dent->type >= UBIFS_ITYPES_CNT ||
460 nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
Richard Weinberger304790c2016-10-04 22:38:33 +0200461 (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300462 le64_to_cpu(dent->inum) > MAX_INUM) {
Sheng Yong235c3622015-03-20 10:39:42 +0000463 ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300464 "directory entry" : "extended attribute entry");
465 return -EINVAL;
466 }
467
468 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
Sheng Yong235c3622015-03-20 10:39:42 +0000469 ubifs_err(c, "bad key type %d", key_type);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300470 return -EINVAL;
471 }
472
473 return 0;
474}
475
476/**
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300477 * is_last_bud - check if the bud is the last in the journal head.
478 * @c: UBIFS file-system description object
479 * @bud: bud description object
480 *
481 * This function checks if bud @bud is the last bud in its journal head. This
482 * information is then used by 'replay_bud()' to decide whether the bud can
483 * have corruptions or not. Indeed, only last buds can be corrupted by power
484 * cuts. Returns %1 if this is the last bud, and %0 if not.
485 */
486static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
487{
488 struct ubifs_jhead *jh = &c->jheads[bud->jhead];
489 struct ubifs_bud *next;
490 uint32_t data;
491 int err;
492
493 if (list_is_last(&bud->list, &jh->buds_list))
494 return 1;
495
496 /*
497 * The following is a quirk to make sure we work correctly with UBIFS
498 * images used with older UBIFS.
499 *
500 * Normally, the last bud will be the last in the journal head's list
501 * of bud. However, there is one exception if the UBIFS image belongs
502 * to older UBIFS. This is fairly unlikely: one would need to use old
503 * UBIFS, then have a power cut exactly at the right point, and then
504 * try to mount this image with new UBIFS.
505 *
506 * The exception is: it is possible to have 2 buds A and B, A goes
507 * before B, and B is the last, bud B is contains no data, and bud A is
508 * corrupted at the end. The reason is that in older versions when the
509 * journal code switched the next bud (from A to B), it first added a
510 * log reference node for the new bud (B), and only after this it
511 * synchronized the write-buffer of current bud (A). But later this was
512 * changed and UBIFS started to always synchronize the write-buffer of
513 * the bud (A) before writing the log reference for the new bud (B).
514 *
515 * But because older UBIFS always synchronized A's write-buffer before
516 * writing to B, we can recognize this exceptional situation but
517 * checking the contents of bud B - if it is empty, then A can be
518 * treated as the last and we can recover it.
519 *
520 * TODO: remove this piece of code in a couple of years (today it is
521 * 16.05.2011).
522 */
523 next = list_entry(bud->list.next, struct ubifs_bud, list);
524 if (!list_is_last(&next->list, &jh->buds_list))
525 return 0;
526
Artem Bityutskiyd3048202011-06-03 14:03:25 +0300527 err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300528 if (err)
529 return 0;
530
531 return data == 0xFFFFFFFF;
532}
533
534/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300535 * replay_bud - replay a bud logical eraseblock.
536 * @c: UBIFS file-system description object
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300537 * @b: bud entry which describes the bud
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300538 *
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300539 * This function replays bud @bud, recovers it if needed, and adds all nodes
540 * from this bud to the replay list. Returns zero in case of success and a
541 * negative error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300542 */
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300543static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300544{
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300545 int is_last = is_last_bud(c, b->bud);
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300546 int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300547 struct ubifs_scan_leb *sleb;
548 struct ubifs_scan_node *snod;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300549
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300550 dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
551 lnum, b->bud->jhead, offs, is_last);
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300552
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300553 if (c->need_recovery && is_last)
554 /*
555 * Recover only last LEBs in the journal heads, because power
556 * cuts may cause corruptions only in these LEBs, because only
557 * these LEBs could possibly be written to at the power cut
558 * time.
559 */
Artem Bityutskiyefcfde52011-05-26 08:36:52 +0300560 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300561 else
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300562 sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300563 if (IS_ERR(sleb))
564 return PTR_ERR(sleb);
565
566 /*
567 * The bud does not have to start from offset zero - the beginning of
568 * the 'lnum' LEB may contain previously committed data. One of the
569 * things we have to do in replay is to correctly update lprops with
570 * newer information about this LEB.
571 *
572 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
573 * bytes of free space because it only contain information about
574 * committed data.
575 *
576 * But we know that real amount of free space is 'c->leb_size -
577 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
578 * 'sleb->endpt' is used by bud data. We have to correctly calculate
579 * how much of these data are dirty and update lprops with this
580 * information.
581 *
582 * The dirt in that LEB region is comprised of padding nodes, deletion
583 * nodes, truncation nodes and nodes which are obsoleted by subsequent
584 * nodes in this LEB. So instead of calculating clean space, we
585 * calculate used space ('used' variable).
586 */
587
588 list_for_each_entry(snod, &sleb->nodes, list) {
Sascha Hauer16a26b22018-09-07 14:36:35 +0200589 u8 hash[UBIFS_HASH_ARR_SZ];
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300590 int deletion = 0;
591
592 cond_resched();
593
594 if (snod->sqnum >= SQNUM_WATERMARK) {
Sheng Yong235c3622015-03-20 10:39:42 +0000595 ubifs_err(c, "file system's life ended");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300596 goto out_dump;
597 }
598
Sascha Hauer16a26b22018-09-07 14:36:35 +0200599 ubifs_node_calc_hash(c, snod->node, hash);
600
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300601 if (snod->sqnum > c->max_sqnum)
602 c->max_sqnum = snod->sqnum;
603
604 switch (snod->type) {
605 case UBIFS_INO_NODE:
606 {
607 struct ubifs_ino_node *ino = snod->node;
608 loff_t new_size = le64_to_cpu(ino->size);
609
610 if (le32_to_cpu(ino->nlink) == 0)
611 deletion = 1;
Sascha Hauer16a26b22018-09-07 14:36:35 +0200612 err = insert_node(c, lnum, snod->offs, snod->len, hash,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300613 &snod->key, snod->sqnum, deletion,
614 &used, 0, new_size);
615 break;
616 }
617 case UBIFS_DATA_NODE:
618 {
619 struct ubifs_data_node *dn = snod->node;
620 loff_t new_size = le32_to_cpu(dn->size) +
621 key_block(c, &snod->key) *
622 UBIFS_BLOCK_SIZE;
623
Sascha Hauer16a26b22018-09-07 14:36:35 +0200624 err = insert_node(c, lnum, snod->offs, snod->len, hash,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300625 &snod->key, snod->sqnum, deletion,
626 &used, 0, new_size);
627 break;
628 }
629 case UBIFS_DENT_NODE:
630 case UBIFS_XENT_NODE:
631 {
632 struct ubifs_dent_node *dent = snod->node;
633
634 err = ubifs_validate_entry(c, dent);
635 if (err)
636 goto out_dump;
637
Sascha Hauer16a26b22018-09-07 14:36:35 +0200638 err = insert_dent(c, lnum, snod->offs, snod->len, hash,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300639 &snod->key, dent->name,
640 le16_to_cpu(dent->nlen), snod->sqnum,
641 !le64_to_cpu(dent->inum), &used);
642 break;
643 }
644 case UBIFS_TRUN_NODE:
645 {
646 struct ubifs_trun_node *trun = snod->node;
647 loff_t old_size = le64_to_cpu(trun->old_size);
648 loff_t new_size = le64_to_cpu(trun->new_size);
649 union ubifs_key key;
650
651 /* Validate truncation node */
652 if (old_size < 0 || old_size > c->max_inode_sz ||
653 new_size < 0 || new_size > c->max_inode_sz ||
654 old_size <= new_size) {
Sheng Yong235c3622015-03-20 10:39:42 +0000655 ubifs_err(c, "bad truncation node");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300656 goto out_dump;
657 }
658
659 /*
660 * Create a fake truncation key just to use the same
661 * functions which expect nodes to have keys.
662 */
663 trun_key_init(c, &key, le32_to_cpu(trun->inum));
Sascha Hauer16a26b22018-09-07 14:36:35 +0200664 err = insert_node(c, lnum, snod->offs, snod->len, hash,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300665 &key, snod->sqnum, 1, &used,
666 old_size, new_size);
667 break;
668 }
669 default:
Sheng Yong235c3622015-03-20 10:39:42 +0000670 ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300671 snod->type, lnum, snod->offs);
672 err = -EINVAL;
673 goto out_dump;
674 }
675 if (err)
676 goto out;
677 }
678
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200679 ubifs_assert(c, ubifs_search_bud(c, lnum));
680 ubifs_assert(c, sleb->endpt - offs >= used);
681 ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300682
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300683 b->dirty = sleb->endpt - offs - used;
684 b->free = c->leb_size - sleb->endpt;
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300685 dbg_mnt("bud LEB %d replied: dirty %d, free %d",
686 lnum, b->dirty, b->free);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300687
688out:
689 ubifs_scan_destroy(sleb);
690 return err;
691
692out_dump:
Sheng Yong235c3622015-03-20 10:39:42 +0000693 ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300694 ubifs_dump_node(c, snod->node);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300695 ubifs_scan_destroy(sleb);
696 return -EINVAL;
697}
698
699/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300700 * replay_buds - replay all buds.
701 * @c: UBIFS file-system description object
702 *
703 * This function returns zero in case of success and a negative error code in
704 * case of failure.
705 */
706static int replay_buds(struct ubifs_info *c)
707{
708 struct bud_entry *b;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300709 int err;
Artem Bityutskiy7703f092011-05-13 16:02:19 +0300710 unsigned long long prev_sqnum = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300711
712 list_for_each_entry(b, &c->replay_buds, list) {
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300713 err = replay_bud(c, b);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300714 if (err)
715 return err;
Artem Bityutskiy7703f092011-05-13 16:02:19 +0300716
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200717 ubifs_assert(c, b->sqnum > prev_sqnum);
Artem Bityutskiy7703f092011-05-13 16:02:19 +0300718 prev_sqnum = b->sqnum;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300719 }
720
721 return 0;
722}
723
724/**
725 * destroy_bud_list - destroy the list of buds to replay.
726 * @c: UBIFS file-system description object
727 */
728static void destroy_bud_list(struct ubifs_info *c)
729{
730 struct bud_entry *b;
731
732 while (!list_empty(&c->replay_buds)) {
733 b = list_entry(c->replay_buds.next, struct bud_entry, list);
734 list_del(&b->list);
735 kfree(b);
736 }
737}
738
739/**
740 * add_replay_bud - add a bud to the list of buds to replay.
741 * @c: UBIFS file-system description object
742 * @lnum: bud logical eraseblock number to replay
743 * @offs: bud start offset
744 * @jhead: journal head to which this bud belongs
745 * @sqnum: reference node sequence number
746 *
747 * This function returns zero in case of success and a negative error code in
748 * case of failure.
749 */
750static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
751 unsigned long long sqnum)
752{
753 struct ubifs_bud *bud;
754 struct bud_entry *b;
755
756 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
757
758 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
759 if (!bud)
760 return -ENOMEM;
761
762 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
763 if (!b) {
764 kfree(bud);
765 return -ENOMEM;
766 }
767
768 bud->lnum = lnum;
769 bud->start = offs;
770 bud->jhead = jhead;
771 ubifs_add_bud(c, bud);
772
773 b->bud = bud;
774 b->sqnum = sqnum;
775 list_add_tail(&b->list, &c->replay_buds);
776
777 return 0;
778}
779
780/**
781 * validate_ref - validate a reference node.
782 * @c: UBIFS file-system description object
783 * @ref: the reference node to validate
784 * @ref_lnum: LEB number of the reference node
785 * @ref_offs: reference node offset
786 *
787 * This function returns %1 if a bud reference already exists for the LEB. %0 is
788 * returned if the reference node is new, otherwise %-EINVAL is returned if
789 * validation failed.
790 */
791static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
792{
793 struct ubifs_bud *bud;
794 int lnum = le32_to_cpu(ref->lnum);
795 unsigned int offs = le32_to_cpu(ref->offs);
796 unsigned int jhead = le32_to_cpu(ref->jhead);
797
798 /*
799 * ref->offs may point to the end of LEB when the journal head points
800 * to the end of LEB and we write reference node for it during commit.
801 * So this is why we require 'offs > c->leb_size'.
802 */
803 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
804 lnum < c->main_first || offs > c->leb_size ||
805 offs & (c->min_io_size - 1))
806 return -EINVAL;
807
808 /* Make sure we have not already looked at this bud */
809 bud = ubifs_search_bud(c, lnum);
810 if (bud) {
811 if (bud->jhead == jhead && bud->start <= offs)
812 return 1;
Sheng Yong235c3622015-03-20 10:39:42 +0000813 ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300814 return -EINVAL;
815 }
816
817 return 0;
818}
819
820/**
821 * replay_log_leb - replay a log logical eraseblock.
822 * @c: UBIFS file-system description object
823 * @lnum: log logical eraseblock to replay
824 * @offs: offset to start replaying from
825 * @sbuf: scan buffer
826 *
827 * This function replays a log LEB and returns zero in case of success, %1 if
828 * this is the last LEB in the log, and a negative error code in case of
829 * failure.
830 */
831static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
832{
833 int err;
834 struct ubifs_scan_leb *sleb;
835 struct ubifs_scan_node *snod;
836 const struct ubifs_cs_node *node;
837
838 dbg_mnt("replay log LEB %d:%d", lnum, offs);
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300839 sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
840 if (IS_ERR(sleb)) {
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300841 if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
842 return PTR_ERR(sleb);
Artem Bityutskiy7d08ae32010-10-17 15:50:19 +0300843 /*
844 * Note, the below function will recover this log LEB only if
845 * it is the last, because unclean reboots can possibly corrupt
846 * only the tail of the log.
847 */
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300848 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300849 if (IS_ERR(sleb))
850 return PTR_ERR(sleb);
851 }
852
853 if (sleb->nodes_cnt == 0) {
854 err = 1;
855 goto out;
856 }
857
858 node = sleb->buf;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300859 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
860 if (c->cs_sqnum == 0) {
861 /*
862 * This is the first log LEB we are looking at, make sure that
863 * the first node is a commit start node. Also record its
864 * sequence number so that UBIFS can determine where the log
865 * ends, because all nodes which were have higher sequence
866 * numbers.
867 */
868 if (snod->type != UBIFS_CS_NODE) {
Sheng Yong235c3622015-03-20 10:39:42 +0000869 ubifs_err(c, "first log node at LEB %d:%d is not CS node",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300870 lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300871 goto out_dump;
872 }
873 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
Sheng Yong235c3622015-03-20 10:39:42 +0000874 ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300875 lnum, offs,
876 (unsigned long long)le64_to_cpu(node->cmt_no),
877 c->cmt_no);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300878 goto out_dump;
879 }
880
881 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
882 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
883 }
884
885 if (snod->sqnum < c->cs_sqnum) {
886 /*
887 * This means that we reached end of log and now
888 * look to the older log data, which was already
889 * committed but the eraseblock was not erased (UBIFS
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +0200890 * only un-maps it). So this basically means we have to
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300891 * exit with "end of log" code.
892 */
893 err = 1;
894 goto out;
895 }
896
897 /* Make sure the first node sits at offset zero of the LEB */
898 if (snod->offs != 0) {
Sheng Yong235c3622015-03-20 10:39:42 +0000899 ubifs_err(c, "first node is not at zero offset");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300900 goto out_dump;
901 }
902
903 list_for_each_entry(snod, &sleb->nodes, list) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300904 cond_resched();
905
906 if (snod->sqnum >= SQNUM_WATERMARK) {
Sheng Yong235c3622015-03-20 10:39:42 +0000907 ubifs_err(c, "file system's life ended");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300908 goto out_dump;
909 }
910
911 if (snod->sqnum < c->cs_sqnum) {
Sheng Yong235c3622015-03-20 10:39:42 +0000912 ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300913 snod->sqnum, c->cs_sqnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300914 goto out_dump;
915 }
916
917 if (snod->sqnum > c->max_sqnum)
918 c->max_sqnum = snod->sqnum;
919
920 switch (snod->type) {
921 case UBIFS_REF_NODE: {
922 const struct ubifs_ref_node *ref = snod->node;
923
924 err = validate_ref(c, ref);
925 if (err == 1)
926 break; /* Already have this bud */
927 if (err)
928 goto out_dump;
929
930 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
931 le32_to_cpu(ref->offs),
932 le32_to_cpu(ref->jhead),
933 snod->sqnum);
934 if (err)
935 goto out;
936
937 break;
938 }
939 case UBIFS_CS_NODE:
940 /* Make sure it sits at the beginning of LEB */
941 if (snod->offs != 0) {
Sheng Yong235c3622015-03-20 10:39:42 +0000942 ubifs_err(c, "unexpected node in log");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300943 goto out_dump;
944 }
945 break;
946 default:
Sheng Yong235c3622015-03-20 10:39:42 +0000947 ubifs_err(c, "unexpected node in log");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300948 goto out_dump;
949 }
950 }
951
952 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
953 c->lhead_lnum = lnum;
954 c->lhead_offs = sleb->endpt;
955 }
956
957 err = !sleb->endpt;
958out:
959 ubifs_scan_destroy(sleb);
960 return err;
961
962out_dump:
Sheng Yong235c3622015-03-20 10:39:42 +0000963 ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300964 lnum, offs + snod->offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300965 ubifs_dump_node(c, snod->node);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300966 ubifs_scan_destroy(sleb);
967 return -EINVAL;
968}
969
970/**
971 * take_ihead - update the status of the index head in lprops to 'taken'.
972 * @c: UBIFS file-system description object
973 *
974 * This function returns the amount of free space in the index head LEB or a
975 * negative error code.
976 */
977static int take_ihead(struct ubifs_info *c)
978{
979 const struct ubifs_lprops *lp;
980 int err, free;
981
982 ubifs_get_lprops(c);
983
984 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
985 if (IS_ERR(lp)) {
986 err = PTR_ERR(lp);
987 goto out;
988 }
989
990 free = lp->free;
991
992 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
993 lp->flags | LPROPS_TAKEN, 0);
994 if (IS_ERR(lp)) {
995 err = PTR_ERR(lp);
996 goto out;
997 }
998
999 err = free;
1000out:
1001 ubifs_release_lprops(c);
1002 return err;
1003}
1004
1005/**
1006 * ubifs_replay_journal - replay journal.
1007 * @c: UBIFS file-system description object
1008 *
1009 * This function scans the journal, replays and cleans it up. It makes sure all
1010 * memory data structures related to uncommitted journal are built (dirty TNC
1011 * tree, tree of buds, modified lprops, etc).
1012 */
1013int ubifs_replay_journal(struct ubifs_info *c)
1014{
Artem Bityutskiyd51f17ea2012-07-14 20:52:58 +03001015 int err, lnum, free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001016
1017 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1018
1019 /* Update the status of the index head in lprops to 'taken' */
1020 free = take_ihead(c);
1021 if (free < 0)
1022 return free; /* Error code */
1023
1024 if (c->ihead_offs != c->leb_size - free) {
Sheng Yong235c3622015-03-20 10:39:42 +00001025 ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001026 c->ihead_offs);
1027 return -EINVAL;
1028 }
1029
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001030 dbg_mnt("start replaying the journal");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001031 c->replaying = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001032 lnum = c->ltail_lnum = c->lhead_lnum;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001033
Artem Bityutskiyd51f17ea2012-07-14 20:52:58 +03001034 do {
1035 err = replay_log_leb(c, lnum, 0, c->sbuf);
hujianyang88cff0f2015-02-10 11:28:57 +08001036 if (err == 1) {
1037 if (lnum != c->lhead_lnum)
1038 /* We hit the end of the log */
1039 break;
1040
1041 /*
1042 * The head of the log must always start with the
1043 * "commit start" node on a properly formatted UBIFS.
1044 * But we found no nodes at all, which means that
Sascha Hauerc7e593b2018-05-14 10:18:16 +02001045 * something went wrong and we cannot proceed mounting
hujianyang88cff0f2015-02-10 11:28:57 +08001046 * the file-system.
1047 */
Sheng Yong235c3622015-03-20 10:39:42 +00001048 ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
hujianyang88cff0f2015-02-10 11:28:57 +08001049 lnum, 0);
1050 err = -EINVAL;
1051 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001052 if (err)
1053 goto out;
Artem Bityutskiyd51f17ea2012-07-14 20:52:58 +03001054 lnum = ubifs_next_log_lnum(c, lnum);
Artem Bityutskiyc212f402012-08-21 13:45:35 +03001055 } while (lnum != c->ltail_lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001056
1057 err = replay_buds(c);
1058 if (err)
1059 goto out;
1060
Artem Bityutskiydebf12d2011-05-15 12:05:54 +03001061 err = apply_replay_list(c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001062 if (err)
1063 goto out;
1064
Artem Bityutskiy074bcb92011-05-15 11:37:17 +03001065 err = set_buds_lprops(c);
1066 if (err)
1067 goto out;
1068
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +02001069 /*
Artem Bityutskiyb1375452011-03-29 18:04:05 +03001070 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1071 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +02001072 * depend on it. This means we have to initialize it to make sure
1073 * budgeting works properly.
1074 */
Artem Bityutskiyb1375452011-03-29 18:04:05 +03001075 c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1076 c->bi.uncommitted_idx *= c->max_idx_node_sz;
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +02001077
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001078 ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
Artem Bityutskiy79fda512012-08-27 13:34:09 +03001079 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1080 c->lhead_lnum, c->lhead_offs, c->max_sqnum,
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001081 (unsigned long)c->highest_inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001082out:
Artem Bityutskiydebf12d2011-05-15 12:05:54 +03001083 destroy_replay_list(c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001084 destroy_bud_list(c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001085 c->replaying = 0;
1086 return err;
1087}