blob: de7799a0a9d1d968f2b846ce5376d7eab889da65 [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;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +030059 unsigned int deletion:1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030060 unsigned long long sqnum;
Artem Bityutskiydebf12d2011-05-15 12:05:54 +030061 struct list_head list;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030062 union ubifs_key key;
63 union {
64 struct qstr nm;
65 struct {
66 loff_t old_size;
67 loff_t new_size;
68 };
Artem Bityutskiy1e517642008-07-14 19:08:37 +030069 };
70};
71
72/**
73 * struct bud_entry - entry in the list of buds to replay.
74 * @list: next bud in the list
75 * @bud: bud description object
Artem Bityutskiy1e517642008-07-14 19:08:37 +030076 * @sqnum: reference node sequence number
Artem Bityutskiyaf1dd412011-05-15 11:14:57 +030077 * @free: free bytes in the bud
78 * @dirty: dirty bytes in the bud
Artem Bityutskiy1e517642008-07-14 19:08:37 +030079 */
80struct bud_entry {
81 struct list_head list;
82 struct ubifs_bud *bud;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030083 unsigned long long sqnum;
Artem Bityutskiyaf1dd412011-05-15 11:14:57 +030084 int free;
85 int dirty;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030086};
87
88/**
89 * set_bud_lprops - set free and dirty space used by a bud.
90 * @c: UBIFS file-system description object
Artem Bityutskiy074bcb92011-05-15 11:37:17 +030091 * @b: bud entry which describes the bud
92 *
93 * This function makes sure the LEB properties of bud @b are set correctly
94 * after the replay. Returns zero in case of success and a negative error code
95 * in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030096 */
Artem Bityutskiy074bcb92011-05-15 11:37:17 +030097static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030098{
99 const struct ubifs_lprops *lp;
100 int err = 0, dirty;
101
102 ubifs_get_lprops(c);
103
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300104 lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300105 if (IS_ERR(lp)) {
106 err = PTR_ERR(lp);
107 goto out;
108 }
109
110 dirty = lp->dirty;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300111 if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300112 /*
113 * The LEB was added to the journal with a starting offset of
114 * zero which means the LEB must have been empty. The LEB
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300115 * property values should be @lp->free == @c->leb_size and
116 * @lp->dirty == 0, but that is not the case. The reason is that
Artem Bityutskiy7a9c3e32011-05-13 13:02:00 +0300117 * the LEB had been garbage collected before it became the bud,
118 * and there was not commit inbetween. The garbage collector
119 * resets the free and dirty space without recording it
120 * anywhere except lprops, so if there was no commit then
121 * lprops does not have that information.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300122 *
123 * We do not need to adjust free space because the scan has told
124 * us the exact value which is recorded in the replay entry as
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300125 * @b->free.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300126 *
127 * However we do need to subtract from the dirty space the
128 * amount of space that the garbage collector reclaimed, which
129 * is the whole LEB minus the amount of space that was free.
130 */
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300131 dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300132 lp->free, lp->dirty);
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300133 dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300134 lp->free, lp->dirty);
135 dirty -= c->leb_size - lp->free;
136 /*
137 * If the replay order was perfect the dirty space would now be
Artem Bityutskiy7d4e9cc2009-03-20 19:11:12 +0200138 * zero. The order is not perfect because the journal heads
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +0200139 * race with each other. This is not a problem but is does mean
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300140 * that the dirty space may temporarily exceed c->leb_size
141 * during the replay.
142 */
143 if (dirty != 0)
Artem Bityutskiy3668b702012-08-27 16:56:58 +0300144 dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300145 b->bud->lnum, lp->free, lp->dirty, b->free,
146 b->dirty);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300147 }
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300148 lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300149 lp->flags | LPROPS_TAKEN, 0);
150 if (IS_ERR(lp)) {
151 err = PTR_ERR(lp);
152 goto out;
153 }
Artem Bityutskiy52c6e6f2011-04-25 18:46:31 +0300154
155 /* Make sure the journal head points to the latest bud */
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300156 err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200157 b->bud->lnum, c->leb_size - b->free);
Artem Bityutskiy52c6e6f2011-04-25 18:46:31 +0300158
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300159out:
160 ubifs_release_lprops(c);
161 return err;
162}
163
164/**
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300165 * set_buds_lprops - set free and dirty space for all replayed buds.
166 * @c: UBIFS file-system description object
167 *
168 * This function sets LEB properties for all replayed buds. Returns zero in
169 * case of success and a negative error code in case of failure.
170 */
171static int set_buds_lprops(struct ubifs_info *c)
172{
173 struct bud_entry *b;
174 int err;
175
176 list_for_each_entry(b, &c->replay_buds, list) {
177 err = set_bud_lprops(c, b);
178 if (err)
179 return err;
180 }
181
182 return 0;
183}
184
185/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300186 * trun_remove_range - apply a replay entry for a truncation to the TNC.
187 * @c: UBIFS file-system description object
188 * @r: replay entry of truncation
189 */
190static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
191{
192 unsigned min_blk, max_blk;
193 union ubifs_key min_key, max_key;
194 ino_t ino;
195
196 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
197 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
198 min_blk += 1;
199
200 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
201 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
202 max_blk -= 1;
203
204 ino = key_inum(c, &r->key);
205
206 data_key_init(c, &min_key, ino, min_blk);
207 data_key_init(c, &max_key, ino, max_blk);
208
209 return ubifs_tnc_remove_range(c, &min_key, &max_key);
210}
211
212/**
Richard Weinberger3926df12018-12-26 13:32:11 +0100213 * inode_still_linked - check whether inode in question will be re-linked.
214 * @c: UBIFS file-system description object
215 * @rino: replay entry to test
216 *
217 * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1.
218 * This case needs special care, otherwise all references to the inode will
219 * be removed upon the first replay entry of an inode with link count 0
220 * is found.
221 */
222static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
223{
224 struct replay_entry *r;
225
226 ubifs_assert(rino->deletion);
227 ubifs_assert(key_type(c, &rino->key) == UBIFS_INO_KEY);
228
229 /*
230 * Find the most recent entry for the inode behind @rino and check
231 * whether it is a deletion.
232 */
233 list_for_each_entry_reverse(r, &c->replay_list, list) {
234 ubifs_assert(r->sqnum >= rino->sqnum);
235 if (key_inum(c, &r->key) == key_inum(c, &rino->key))
236 return r->deletion == 0;
237
238 }
239
240 ubifs_assert(0);
241 return false;
242}
243
244/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300245 * apply_replay_entry - apply a replay entry to the TNC.
246 * @c: UBIFS file-system description object
247 * @r: replay entry to apply
248 *
249 * Apply a replay entry to the TNC.
250 */
251static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
252{
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300253 int err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300254
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200255 dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
256 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300257
258 /* Set c->replay_sqnum to help deal with dangling branches. */
259 c->replay_sqnum = r->sqnum;
260
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300261 if (is_hash_key(c, &r->key)) {
262 if (r->deletion)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300263 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
264 else
265 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
266 r->len, &r->nm);
267 } else {
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300268 if (r->deletion)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300269 switch (key_type(c, &r->key)) {
270 case UBIFS_INO_KEY:
271 {
272 ino_t inum = key_inum(c, &r->key);
273
Richard Weinberger3926df12018-12-26 13:32:11 +0100274 if (inode_still_linked(c, r)) {
275 err = 0;
276 break;
277 }
278
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300279 err = ubifs_tnc_remove_ino(c, inum);
280 break;
281 }
282 case UBIFS_TRUN_KEY:
283 err = trun_remove_range(c, r);
284 break;
285 default:
286 err = ubifs_tnc_remove(c, &r->key);
287 break;
288 }
289 else
290 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
291 r->len);
292 if (err)
293 return err;
294
295 if (c->need_recovery)
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300296 err = ubifs_recover_size_accum(c, &r->key, r->deletion,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300297 r->new_size);
298 }
299
300 return err;
301}
302
303/**
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300304 * replay_entries_cmp - compare 2 replay entries.
305 * @priv: UBIFS file-system description object
306 * @a: first replay entry
Julia Lawallec037df2016-10-01 22:53:11 +0200307 * @b: second replay entry
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300308 *
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300309 * This is a comparios function for 'list_sort()' which compares 2 replay
310 * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
311 * greater sequence number and %-1 otherwise.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300312 */
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300313static int replay_entries_cmp(void *priv, struct list_head *a,
314 struct list_head *b)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300315{
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300316 struct replay_entry *ra, *rb;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300317
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300318 cond_resched();
319 if (a == b)
320 return 0;
321
322 ra = list_entry(a, struct replay_entry, list);
323 rb = list_entry(b, struct replay_entry, list);
324 ubifs_assert(ra->sqnum != rb->sqnum);
325 if (ra->sqnum > rb->sqnum)
326 return 1;
327 return -1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300328}
329
330/**
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300331 * apply_replay_list - apply the replay list to the TNC.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300332 * @c: UBIFS file-system description object
333 *
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300334 * Apply all entries in the replay list to the TNC. Returns zero in case of
335 * success and a negative error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300336 */
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300337static int apply_replay_list(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300338{
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300339 struct replay_entry *r;
340 int err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300341
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300342 list_sort(c, &c->replay_list, &replay_entries_cmp);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300343
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300344 list_for_each_entry(r, &c->replay_list, list) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300345 cond_resched();
346
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300347 err = apply_replay_entry(c, r);
348 if (err)
349 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300350 }
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300351
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300352 return 0;
353}
354
355/**
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300356 * destroy_replay_list - destroy the replay.
357 * @c: UBIFS file-system description object
358 *
359 * Destroy the replay list.
360 */
361static void destroy_replay_list(struct ubifs_info *c)
362{
363 struct replay_entry *r, *tmp;
364
365 list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
366 if (is_hash_key(c, &r->key))
367 kfree(r->nm.name);
368 list_del(&r->list);
369 kfree(r);
370 }
371}
372
373/**
374 * insert_node - insert a node to the replay list
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300375 * @c: UBIFS file-system description object
376 * @lnum: node logical eraseblock number
377 * @offs: node offset
378 * @len: node length
379 * @key: node key
380 * @sqnum: sequence number
381 * @deletion: non-zero if this is a deletion
382 * @used: number of bytes in use in a LEB
383 * @old_size: truncation old size
384 * @new_size: truncation new size
385 *
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300386 * This function inserts a scanned non-direntry node to the replay list. The
387 * replay list contains @struct replay_entry elements, and we sort this list in
388 * sequence number order before applying it. The replay list is applied at the
389 * very end of the replay process. Since the list is sorted in sequence number
390 * order, the older modifications are applied first. This function returns zero
391 * in case of success and a negative error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300392 */
393static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
394 union ubifs_key *key, unsigned long long sqnum,
395 int deletion, int *used, loff_t old_size,
396 loff_t new_size)
397{
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300398 struct replay_entry *r;
399
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200400 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300401
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300402 if (key_inum(c, key) >= c->highest_inum)
403 c->highest_inum = key_inum(c, key);
404
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300405 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
406 if (!r)
407 return -ENOMEM;
408
409 if (!deletion)
410 *used += ALIGN(len, 8);
411 r->lnum = lnum;
412 r->offs = offs;
413 r->len = len;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300414 r->deletion = !!deletion;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300415 r->sqnum = sqnum;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300416 key_copy(c, key, &r->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300417 r->old_size = old_size;
418 r->new_size = new_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300419
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300420 list_add_tail(&r->list, &c->replay_list);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300421 return 0;
422}
423
424/**
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300425 * insert_dent - insert a directory entry node into the replay list.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300426 * @c: UBIFS file-system description object
427 * @lnum: node logical eraseblock number
428 * @offs: node offset
429 * @len: node length
430 * @key: node key
431 * @name: directory entry name
432 * @nlen: directory entry name length
433 * @sqnum: sequence number
434 * @deletion: non-zero if this is a deletion
435 * @used: number of bytes in use in a LEB
436 *
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300437 * This function inserts a scanned directory entry node or an extended
438 * attribute entry to the replay list. Returns zero in case of success and a
439 * negative error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300440 */
441static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
442 union ubifs_key *key, const char *name, int nlen,
443 unsigned long long sqnum, int deletion, int *used)
444{
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300445 struct replay_entry *r;
446 char *nbuf;
447
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200448 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300449 if (key_inum(c, key) >= c->highest_inum)
450 c->highest_inum = key_inum(c, key);
451
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300452 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
453 if (!r)
454 return -ENOMEM;
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300455
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300456 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
457 if (!nbuf) {
458 kfree(r);
459 return -ENOMEM;
460 }
461
462 if (!deletion)
463 *used += ALIGN(len, 8);
464 r->lnum = lnum;
465 r->offs = offs;
466 r->len = len;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300467 r->deletion = !!deletion;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300468 r->sqnum = sqnum;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300469 key_copy(c, key, &r->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300470 r->nm.len = nlen;
471 memcpy(nbuf, name, nlen);
472 nbuf[nlen] = '\0';
473 r->nm.name = nbuf;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300474
Artem Bityutskiydebf12d2011-05-15 12:05:54 +0300475 list_add_tail(&r->list, &c->replay_list);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300476 return 0;
477}
478
479/**
480 * ubifs_validate_entry - validate directory or extended attribute entry node.
481 * @c: UBIFS file-system description object
482 * @dent: the node to validate
483 *
484 * This function validates directory or extended attribute entry node @dent.
485 * Returns zero if the node is all right and a %-EINVAL if not.
486 */
487int ubifs_validate_entry(struct ubifs_info *c,
488 const struct ubifs_dent_node *dent)
489{
490 int key_type = key_type_flash(c, dent->key);
491 int nlen = le16_to_cpu(dent->nlen);
492
493 if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
494 dent->type >= UBIFS_ITYPES_CNT ||
495 nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
496 strnlen(dent->name, nlen) != nlen ||
497 le64_to_cpu(dent->inum) > MAX_INUM) {
Sheng Yong235c3622015-03-20 10:39:42 +0000498 ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300499 "directory entry" : "extended attribute entry");
500 return -EINVAL;
501 }
502
503 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
Sheng Yong235c3622015-03-20 10:39:42 +0000504 ubifs_err(c, "bad key type %d", key_type);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300505 return -EINVAL;
506 }
507
508 return 0;
509}
510
511/**
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300512 * is_last_bud - check if the bud is the last in the journal head.
513 * @c: UBIFS file-system description object
514 * @bud: bud description object
515 *
516 * This function checks if bud @bud is the last bud in its journal head. This
517 * information is then used by 'replay_bud()' to decide whether the bud can
518 * have corruptions or not. Indeed, only last buds can be corrupted by power
519 * cuts. Returns %1 if this is the last bud, and %0 if not.
520 */
521static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
522{
523 struct ubifs_jhead *jh = &c->jheads[bud->jhead];
524 struct ubifs_bud *next;
525 uint32_t data;
526 int err;
527
528 if (list_is_last(&bud->list, &jh->buds_list))
529 return 1;
530
531 /*
532 * The following is a quirk to make sure we work correctly with UBIFS
533 * images used with older UBIFS.
534 *
535 * Normally, the last bud will be the last in the journal head's list
536 * of bud. However, there is one exception if the UBIFS image belongs
537 * to older UBIFS. This is fairly unlikely: one would need to use old
538 * UBIFS, then have a power cut exactly at the right point, and then
539 * try to mount this image with new UBIFS.
540 *
541 * The exception is: it is possible to have 2 buds A and B, A goes
542 * before B, and B is the last, bud B is contains no data, and bud A is
543 * corrupted at the end. The reason is that in older versions when the
544 * journal code switched the next bud (from A to B), it first added a
545 * log reference node for the new bud (B), and only after this it
546 * synchronized the write-buffer of current bud (A). But later this was
547 * changed and UBIFS started to always synchronize the write-buffer of
548 * the bud (A) before writing the log reference for the new bud (B).
549 *
550 * But because older UBIFS always synchronized A's write-buffer before
551 * writing to B, we can recognize this exceptional situation but
552 * checking the contents of bud B - if it is empty, then A can be
553 * treated as the last and we can recover it.
554 *
555 * TODO: remove this piece of code in a couple of years (today it is
556 * 16.05.2011).
557 */
558 next = list_entry(bud->list.next, struct ubifs_bud, list);
559 if (!list_is_last(&next->list, &jh->buds_list))
560 return 0;
561
Artem Bityutskiyd3048202011-06-03 14:03:25 +0300562 err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300563 if (err)
564 return 0;
565
566 return data == 0xFFFFFFFF;
567}
568
569/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300570 * replay_bud - replay a bud logical eraseblock.
571 * @c: UBIFS file-system description object
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300572 * @b: bud entry which describes the bud
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300573 *
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300574 * This function replays bud @bud, recovers it if needed, and adds all nodes
575 * from this bud to the replay list. Returns zero in case of success and a
576 * negative error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300577 */
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300578static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300579{
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300580 int is_last = is_last_bud(c, b->bud);
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300581 int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300582 struct ubifs_scan_leb *sleb;
583 struct ubifs_scan_node *snod;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300584
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300585 dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
586 lnum, b->bud->jhead, offs, is_last);
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300587
Artem Bityutskiy91c66082011-05-15 13:11:00 +0300588 if (c->need_recovery && is_last)
589 /*
590 * Recover only last LEBs in the journal heads, because power
591 * cuts may cause corruptions only in these LEBs, because only
592 * these LEBs could possibly be written to at the power cut
593 * time.
594 */
Artem Bityutskiyefcfde52011-05-26 08:36:52 +0300595 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300596 else
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300597 sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300598 if (IS_ERR(sleb))
599 return PTR_ERR(sleb);
600
601 /*
602 * The bud does not have to start from offset zero - the beginning of
603 * the 'lnum' LEB may contain previously committed data. One of the
604 * things we have to do in replay is to correctly update lprops with
605 * newer information about this LEB.
606 *
607 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
608 * bytes of free space because it only contain information about
609 * committed data.
610 *
611 * But we know that real amount of free space is 'c->leb_size -
612 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
613 * 'sleb->endpt' is used by bud data. We have to correctly calculate
614 * how much of these data are dirty and update lprops with this
615 * information.
616 *
617 * The dirt in that LEB region is comprised of padding nodes, deletion
618 * nodes, truncation nodes and nodes which are obsoleted by subsequent
619 * nodes in this LEB. So instead of calculating clean space, we
620 * calculate used space ('used' variable).
621 */
622
623 list_for_each_entry(snod, &sleb->nodes, list) {
624 int deletion = 0;
625
626 cond_resched();
627
628 if (snod->sqnum >= SQNUM_WATERMARK) {
Sheng Yong235c3622015-03-20 10:39:42 +0000629 ubifs_err(c, "file system's life ended");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300630 goto out_dump;
631 }
632
633 if (snod->sqnum > c->max_sqnum)
634 c->max_sqnum = snod->sqnum;
635
636 switch (snod->type) {
637 case UBIFS_INO_NODE:
638 {
639 struct ubifs_ino_node *ino = snod->node;
640 loff_t new_size = le64_to_cpu(ino->size);
641
642 if (le32_to_cpu(ino->nlink) == 0)
643 deletion = 1;
644 err = insert_node(c, lnum, snod->offs, snod->len,
645 &snod->key, snod->sqnum, deletion,
646 &used, 0, new_size);
647 break;
648 }
649 case UBIFS_DATA_NODE:
650 {
651 struct ubifs_data_node *dn = snod->node;
652 loff_t new_size = le32_to_cpu(dn->size) +
653 key_block(c, &snod->key) *
654 UBIFS_BLOCK_SIZE;
655
656 err = insert_node(c, lnum, snod->offs, snod->len,
657 &snod->key, snod->sqnum, deletion,
658 &used, 0, new_size);
659 break;
660 }
661 case UBIFS_DENT_NODE:
662 case UBIFS_XENT_NODE:
663 {
664 struct ubifs_dent_node *dent = snod->node;
665
666 err = ubifs_validate_entry(c, dent);
667 if (err)
668 goto out_dump;
669
670 err = insert_dent(c, lnum, snod->offs, snod->len,
671 &snod->key, dent->name,
672 le16_to_cpu(dent->nlen), snod->sqnum,
673 !le64_to_cpu(dent->inum), &used);
674 break;
675 }
676 case UBIFS_TRUN_NODE:
677 {
678 struct ubifs_trun_node *trun = snod->node;
679 loff_t old_size = le64_to_cpu(trun->old_size);
680 loff_t new_size = le64_to_cpu(trun->new_size);
681 union ubifs_key key;
682
683 /* Validate truncation node */
684 if (old_size < 0 || old_size > c->max_inode_sz ||
685 new_size < 0 || new_size > c->max_inode_sz ||
686 old_size <= new_size) {
Sheng Yong235c3622015-03-20 10:39:42 +0000687 ubifs_err(c, "bad truncation node");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300688 goto out_dump;
689 }
690
691 /*
692 * Create a fake truncation key just to use the same
693 * functions which expect nodes to have keys.
694 */
695 trun_key_init(c, &key, le32_to_cpu(trun->inum));
696 err = insert_node(c, lnum, snod->offs, snod->len,
697 &key, snod->sqnum, 1, &used,
698 old_size, new_size);
699 break;
700 }
701 default:
Sheng Yong235c3622015-03-20 10:39:42 +0000702 ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300703 snod->type, lnum, snod->offs);
704 err = -EINVAL;
705 goto out_dump;
706 }
707 if (err)
708 goto out;
709 }
710
Artem Bityutskiyc49139d2011-05-15 12:40:46 +0300711 ubifs_assert(ubifs_search_bud(c, lnum));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300712 ubifs_assert(sleb->endpt - offs >= used);
713 ubifs_assert(sleb->endpt % c->min_io_size == 0);
714
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300715 b->dirty = sleb->endpt - offs - used;
716 b->free = c->leb_size - sleb->endpt;
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300717 dbg_mnt("bud LEB %d replied: dirty %d, free %d",
718 lnum, b->dirty, b->free);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300719
720out:
721 ubifs_scan_destroy(sleb);
722 return err;
723
724out_dump:
Sheng Yong235c3622015-03-20 10:39:42 +0000725 ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300726 ubifs_dump_node(c, snod->node);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300727 ubifs_scan_destroy(sleb);
728 return -EINVAL;
729}
730
731/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300732 * replay_buds - replay all buds.
733 * @c: UBIFS file-system description object
734 *
735 * This function returns zero in case of success and a negative error code in
736 * case of failure.
737 */
738static int replay_buds(struct ubifs_info *c)
739{
740 struct bud_entry *b;
Artem Bityutskiy074bcb92011-05-15 11:37:17 +0300741 int err;
Artem Bityutskiy7703f092011-05-13 16:02:19 +0300742 unsigned long long prev_sqnum = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300743
744 list_for_each_entry(b, &c->replay_buds, list) {
Artem Bityutskiye76a4522011-05-15 12:34:29 +0300745 err = replay_bud(c, b);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300746 if (err)
747 return err;
Artem Bityutskiy7703f092011-05-13 16:02:19 +0300748
749 ubifs_assert(b->sqnum > prev_sqnum);
750 prev_sqnum = b->sqnum;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300751 }
752
753 return 0;
754}
755
756/**
757 * destroy_bud_list - destroy the list of buds to replay.
758 * @c: UBIFS file-system description object
759 */
760static void destroy_bud_list(struct ubifs_info *c)
761{
762 struct bud_entry *b;
763
764 while (!list_empty(&c->replay_buds)) {
765 b = list_entry(c->replay_buds.next, struct bud_entry, list);
766 list_del(&b->list);
767 kfree(b);
768 }
769}
770
771/**
772 * add_replay_bud - add a bud to the list of buds to replay.
773 * @c: UBIFS file-system description object
774 * @lnum: bud logical eraseblock number to replay
775 * @offs: bud start offset
776 * @jhead: journal head to which this bud belongs
777 * @sqnum: reference node sequence number
778 *
779 * This function returns zero in case of success and a negative error code in
780 * case of failure.
781 */
782static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
783 unsigned long long sqnum)
784{
785 struct ubifs_bud *bud;
786 struct bud_entry *b;
787
788 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
789
790 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
791 if (!bud)
792 return -ENOMEM;
793
794 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
795 if (!b) {
796 kfree(bud);
797 return -ENOMEM;
798 }
799
800 bud->lnum = lnum;
801 bud->start = offs;
802 bud->jhead = jhead;
803 ubifs_add_bud(c, bud);
804
805 b->bud = bud;
806 b->sqnum = sqnum;
807 list_add_tail(&b->list, &c->replay_buds);
808
809 return 0;
810}
811
812/**
813 * validate_ref - validate a reference node.
814 * @c: UBIFS file-system description object
815 * @ref: the reference node to validate
816 * @ref_lnum: LEB number of the reference node
817 * @ref_offs: reference node offset
818 *
819 * This function returns %1 if a bud reference already exists for the LEB. %0 is
820 * returned if the reference node is new, otherwise %-EINVAL is returned if
821 * validation failed.
822 */
823static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
824{
825 struct ubifs_bud *bud;
826 int lnum = le32_to_cpu(ref->lnum);
827 unsigned int offs = le32_to_cpu(ref->offs);
828 unsigned int jhead = le32_to_cpu(ref->jhead);
829
830 /*
831 * ref->offs may point to the end of LEB when the journal head points
832 * to the end of LEB and we write reference node for it during commit.
833 * So this is why we require 'offs > c->leb_size'.
834 */
835 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
836 lnum < c->main_first || offs > c->leb_size ||
837 offs & (c->min_io_size - 1))
838 return -EINVAL;
839
840 /* Make sure we have not already looked at this bud */
841 bud = ubifs_search_bud(c, lnum);
842 if (bud) {
843 if (bud->jhead == jhead && bud->start <= offs)
844 return 1;
Sheng Yong235c3622015-03-20 10:39:42 +0000845 ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300846 return -EINVAL;
847 }
848
849 return 0;
850}
851
852/**
853 * replay_log_leb - replay a log logical eraseblock.
854 * @c: UBIFS file-system description object
855 * @lnum: log logical eraseblock to replay
856 * @offs: offset to start replaying from
857 * @sbuf: scan buffer
858 *
859 * This function replays a log LEB and returns zero in case of success, %1 if
860 * this is the last LEB in the log, and a negative error code in case of
861 * failure.
862 */
863static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
864{
865 int err;
866 struct ubifs_scan_leb *sleb;
867 struct ubifs_scan_node *snod;
868 const struct ubifs_cs_node *node;
869
870 dbg_mnt("replay log LEB %d:%d", lnum, offs);
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300871 sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
872 if (IS_ERR(sleb)) {
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300873 if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
874 return PTR_ERR(sleb);
Artem Bityutskiy7d08ae32010-10-17 15:50:19 +0300875 /*
876 * Note, the below function will recover this log LEB only if
877 * it is the last, because unclean reboots can possibly corrupt
878 * only the tail of the log.
879 */
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300880 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300881 if (IS_ERR(sleb))
882 return PTR_ERR(sleb);
883 }
884
885 if (sleb->nodes_cnt == 0) {
886 err = 1;
887 goto out;
888 }
889
890 node = sleb->buf;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300891 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
892 if (c->cs_sqnum == 0) {
893 /*
894 * This is the first log LEB we are looking at, make sure that
895 * the first node is a commit start node. Also record its
896 * sequence number so that UBIFS can determine where the log
897 * ends, because all nodes which were have higher sequence
898 * numbers.
899 */
900 if (snod->type != UBIFS_CS_NODE) {
Sheng Yong235c3622015-03-20 10:39:42 +0000901 ubifs_err(c, "first log node at LEB %d:%d is not CS node",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300902 lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300903 goto out_dump;
904 }
905 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
Sheng Yong235c3622015-03-20 10:39:42 +0000906 ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300907 lnum, offs,
908 (unsigned long long)le64_to_cpu(node->cmt_no),
909 c->cmt_no);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300910 goto out_dump;
911 }
912
913 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
914 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
915 }
916
917 if (snod->sqnum < c->cs_sqnum) {
918 /*
919 * This means that we reached end of log and now
920 * look to the older log data, which was already
921 * committed but the eraseblock was not erased (UBIFS
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +0200922 * only un-maps it). So this basically means we have to
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300923 * exit with "end of log" code.
924 */
925 err = 1;
926 goto out;
927 }
928
929 /* Make sure the first node sits at offset zero of the LEB */
930 if (snod->offs != 0) {
Sheng Yong235c3622015-03-20 10:39:42 +0000931 ubifs_err(c, "first node is not at zero offset");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300932 goto out_dump;
933 }
934
935 list_for_each_entry(snod, &sleb->nodes, list) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300936 cond_resched();
937
938 if (snod->sqnum >= SQNUM_WATERMARK) {
Sheng Yong235c3622015-03-20 10:39:42 +0000939 ubifs_err(c, "file system's life ended");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300940 goto out_dump;
941 }
942
943 if (snod->sqnum < c->cs_sqnum) {
Sheng Yong235c3622015-03-20 10:39:42 +0000944 ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300945 snod->sqnum, c->cs_sqnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300946 goto out_dump;
947 }
948
949 if (snod->sqnum > c->max_sqnum)
950 c->max_sqnum = snod->sqnum;
951
952 switch (snod->type) {
953 case UBIFS_REF_NODE: {
954 const struct ubifs_ref_node *ref = snod->node;
955
956 err = validate_ref(c, ref);
957 if (err == 1)
958 break; /* Already have this bud */
959 if (err)
960 goto out_dump;
961
962 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
963 le32_to_cpu(ref->offs),
964 le32_to_cpu(ref->jhead),
965 snod->sqnum);
966 if (err)
967 goto out;
968
969 break;
970 }
971 case UBIFS_CS_NODE:
972 /* Make sure it sits at the beginning of LEB */
973 if (snod->offs != 0) {
Sheng Yong235c3622015-03-20 10:39:42 +0000974 ubifs_err(c, "unexpected node in log");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300975 goto out_dump;
976 }
977 break;
978 default:
Sheng Yong235c3622015-03-20 10:39:42 +0000979 ubifs_err(c, "unexpected node in log");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300980 goto out_dump;
981 }
982 }
983
984 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
985 c->lhead_lnum = lnum;
986 c->lhead_offs = sleb->endpt;
987 }
988
989 err = !sleb->endpt;
990out:
991 ubifs_scan_destroy(sleb);
992 return err;
993
994out_dump:
Sheng Yong235c3622015-03-20 10:39:42 +0000995 ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300996 lnum, offs + snod->offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300997 ubifs_dump_node(c, snod->node);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300998 ubifs_scan_destroy(sleb);
999 return -EINVAL;
1000}
1001
1002/**
1003 * take_ihead - update the status of the index head in lprops to 'taken'.
1004 * @c: UBIFS file-system description object
1005 *
1006 * This function returns the amount of free space in the index head LEB or a
1007 * negative error code.
1008 */
1009static int take_ihead(struct ubifs_info *c)
1010{
1011 const struct ubifs_lprops *lp;
1012 int err, free;
1013
1014 ubifs_get_lprops(c);
1015
1016 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
1017 if (IS_ERR(lp)) {
1018 err = PTR_ERR(lp);
1019 goto out;
1020 }
1021
1022 free = lp->free;
1023
1024 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
1025 lp->flags | LPROPS_TAKEN, 0);
1026 if (IS_ERR(lp)) {
1027 err = PTR_ERR(lp);
1028 goto out;
1029 }
1030
1031 err = free;
1032out:
1033 ubifs_release_lprops(c);
1034 return err;
1035}
1036
1037/**
1038 * ubifs_replay_journal - replay journal.
1039 * @c: UBIFS file-system description object
1040 *
1041 * This function scans the journal, replays and cleans it up. It makes sure all
1042 * memory data structures related to uncommitted journal are built (dirty TNC
1043 * tree, tree of buds, modified lprops, etc).
1044 */
1045int ubifs_replay_journal(struct ubifs_info *c)
1046{
Artem Bityutskiyd51f17ea2012-07-14 20:52:58 +03001047 int err, lnum, free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001048
1049 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1050
1051 /* Update the status of the index head in lprops to 'taken' */
1052 free = take_ihead(c);
1053 if (free < 0)
1054 return free; /* Error code */
1055
1056 if (c->ihead_offs != c->leb_size - free) {
Sheng Yong235c3622015-03-20 10:39:42 +00001057 ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001058 c->ihead_offs);
1059 return -EINVAL;
1060 }
1061
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001062 dbg_mnt("start replaying the journal");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001063 c->replaying = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001064 lnum = c->ltail_lnum = c->lhead_lnum;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001065
Artem Bityutskiyd51f17ea2012-07-14 20:52:58 +03001066 do {
1067 err = replay_log_leb(c, lnum, 0, c->sbuf);
hujianyang88cff0f2015-02-10 11:28:57 +08001068 if (err == 1) {
1069 if (lnum != c->lhead_lnum)
1070 /* We hit the end of the log */
1071 break;
1072
1073 /*
1074 * The head of the log must always start with the
1075 * "commit start" node on a properly formatted UBIFS.
1076 * But we found no nodes at all, which means that
1077 * someting went wrong and we cannot proceed mounting
1078 * the file-system.
1079 */
Sheng Yong235c3622015-03-20 10:39:42 +00001080 ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
hujianyang88cff0f2015-02-10 11:28:57 +08001081 lnum, 0);
1082 err = -EINVAL;
1083 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001084 if (err)
1085 goto out;
Artem Bityutskiyd51f17ea2012-07-14 20:52:58 +03001086 lnum = ubifs_next_log_lnum(c, lnum);
Artem Bityutskiyc212f402012-08-21 13:45:35 +03001087 } while (lnum != c->ltail_lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001088
1089 err = replay_buds(c);
1090 if (err)
1091 goto out;
1092
Artem Bityutskiydebf12d2011-05-15 12:05:54 +03001093 err = apply_replay_list(c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001094 if (err)
1095 goto out;
1096
Artem Bityutskiy074bcb92011-05-15 11:37:17 +03001097 err = set_buds_lprops(c);
1098 if (err)
1099 goto out;
1100
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +02001101 /*
Artem Bityutskiyb1375452011-03-29 18:04:05 +03001102 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1103 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +02001104 * depend on it. This means we have to initialize it to make sure
1105 * budgeting works properly.
1106 */
Artem Bityutskiyb1375452011-03-29 18:04:05 +03001107 c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1108 c->bi.uncommitted_idx *= c->max_idx_node_sz;
Artem Bityutskiy6edbfaf2008-12-30 20:06:49 +02001109
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001110 ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
Artem Bityutskiy79fda512012-08-27 13:34:09 +03001111 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1112 c->lhead_lnum, c->lhead_offs, c->max_sqnum,
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001113 (unsigned long)c->highest_inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001114out:
Artem Bityutskiydebf12d2011-05-15 12:05:54 +03001115 destroy_replay_list(c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001116 destroy_bud_list(c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001117 c->replaying = 0;
1118 return err;
1119}