import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ubifs / log.c
CommitLineData
1e51764a
AB
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file is a part of UBIFS journal implementation and contains various
25 * functions which manipulate the log. The log is a fixed area on the flash
26 * which does not contain any data but refers to buds. The log is a part of the
27 * journal.
28 */
29
30#include "ubifs.h"
31
1e51764a 32static int dbg_check_bud_bytes(struct ubifs_info *c);
1e51764a
AB
33
34/**
35 * ubifs_search_bud - search bud LEB.
36 * @c: UBIFS file-system description object
37 * @lnum: logical eraseblock number to search
38 *
39 * This function searches bud LEB @lnum. Returns bud description object in case
40 * of success and %NULL if there is no bud with this LEB number.
41 */
42struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
43{
44 struct rb_node *p;
45 struct ubifs_bud *bud;
46
47 spin_lock(&c->buds_lock);
48 p = c->buds.rb_node;
49 while (p) {
50 bud = rb_entry(p, struct ubifs_bud, rb);
51 if (lnum < bud->lnum)
52 p = p->rb_left;
53 else if (lnum > bud->lnum)
54 p = p->rb_right;
55 else {
56 spin_unlock(&c->buds_lock);
57 return bud;
58 }
59 }
60 spin_unlock(&c->buds_lock);
61 return NULL;
62}
63
64/**
65 * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
66 * @c: UBIFS file-system description object
67 * @lnum: logical eraseblock number to search
68 *
69 * This functions returns the wbuf for @lnum or %NULL if there is not one.
70 */
71struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
72{
73 struct rb_node *p;
74 struct ubifs_bud *bud;
75 int jhead;
76
77 if (!c->jheads)
78 return NULL;
79
80 spin_lock(&c->buds_lock);
81 p = c->buds.rb_node;
82 while (p) {
83 bud = rb_entry(p, struct ubifs_bud, rb);
84 if (lnum < bud->lnum)
85 p = p->rb_left;
86 else if (lnum > bud->lnum)
87 p = p->rb_right;
88 else {
89 jhead = bud->jhead;
90 spin_unlock(&c->buds_lock);
91 return &c->jheads[jhead].wbuf;
92 }
93 }
94 spin_unlock(&c->buds_lock);
95 return NULL;
96}
97
1e51764a
AB
98/**
99 * empty_log_bytes - calculate amount of empty space in the log.
100 * @c: UBIFS file-system description object
101 */
102static inline long long empty_log_bytes(const struct ubifs_info *c)
103{
104 long long h, t;
105
106 h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
107 t = (long long)c->ltail_lnum * c->leb_size;
108
6fa3eb70 109 if (h > t)
1e51764a 110 return c->log_bytes - h + t;
6fa3eb70 111 else if (h != t)
1e51764a 112 return t - h;
6fa3eb70
S
113 else if (c->lhead_lnum != c->ltail_lnum)
114 return 0;
115 else
116 return c->log_bytes;
1e51764a
AB
117}
118
119/**
120 * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
121 * @c: UBIFS file-system description object
122 * @bud: the bud to add
123 */
124void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
125{
126 struct rb_node **p, *parent = NULL;
127 struct ubifs_bud *b;
128 struct ubifs_jhead *jhead;
129
130 spin_lock(&c->buds_lock);
131 p = &c->buds.rb_node;
132 while (*p) {
133 parent = *p;
134 b = rb_entry(parent, struct ubifs_bud, rb);
135 ubifs_assert(bud->lnum != b->lnum);
136 if (bud->lnum < b->lnum)
137 p = &(*p)->rb_left;
138 else
139 p = &(*p)->rb_right;
140 }
141
142 rb_link_node(&bud->rb, parent, p);
143 rb_insert_color(&bud->rb, &c->buds);
144 if (c->jheads) {
145 jhead = &c->jheads[bud->jhead];
146 list_add_tail(&bud->list, &jhead->buds_list);
147 } else
2ef13294 148 ubifs_assert(c->replaying && c->ro_mount);
1e51764a
AB
149
150 /*
151 * Note, although this is a new bud, we anyway account this space now,
152 * before any data has been written to it, because this is about to
153 * guarantee fixed mount time, and this bud will anyway be read and
154 * scanned.
155 */
156 c->bud_bytes += c->leb_size - bud->start;
157
77a7ae58
AB
158 dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
159 bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
1e51764a
AB
160 spin_unlock(&c->buds_lock);
161}
162
1e51764a
AB
163/**
164 * ubifs_add_bud_to_log - add a new bud to the log.
165 * @c: UBIFS file-system description object
166 * @jhead: journal head the bud belongs to
167 * @lnum: LEB number of the bud
168 * @offs: starting offset of the bud
169 *
170 * This function writes reference node for the new bud LEB @lnum it to the log,
171 * and adds it to the buds tress. It also makes sure that log size does not
172 * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
173 * %-EAGAIN if commit is required, and a negative error codes in case of
174 * failure.
175 */
176int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
177{
178 int err;
179 struct ubifs_bud *bud;
180 struct ubifs_ref_node *ref;
181
182 bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
183 if (!bud)
184 return -ENOMEM;
185 ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
186 if (!ref) {
187 kfree(bud);
188 return -ENOMEM;
189 }
190
191 mutex_lock(&c->log_mutex);
2ef13294 192 ubifs_assert(!c->ro_media && !c->ro_mount);
2680d722 193 if (c->ro_error) {
1e51764a
AB
194 err = -EROFS;
195 goto out_unlock;
196 }
197
198 /* Make sure we have enough space in the log */
199 if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
200 dbg_log("not enough log space - %lld, required %d",
201 empty_log_bytes(c), c->min_log_bytes);
202 ubifs_commit_required(c);
203 err = -EAGAIN;
204 goto out_unlock;
205 }
206
207 /*
7d4e9ccb 208 * Make sure the amount of space in buds will not exceed the
1e51764a
AB
209 * 'c->max_bud_bytes' limit, because we want to guarantee mount time
210 * limits.
211 *
212 * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
213 * because we are holding @c->log_mutex. All @c->bud_bytes take place
214 * when both @c->log_mutex and @c->bud_bytes are locked.
215 */
216 if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
217 dbg_log("bud bytes %lld (%lld max), require commit",
218 c->bud_bytes, c->max_bud_bytes);
219 ubifs_commit_required(c);
220 err = -EAGAIN;
221 goto out_unlock;
222 }
223
224 /*
225 * If the journal is full enough - start background commit. Note, it is
226 * OK to read 'c->cmt_state' without spinlock because integer reads
227 * are atomic in the kernel.
228 */
229 if (c->bud_bytes >= c->bg_bud_bytes &&
230 c->cmt_state == COMMIT_RESTING) {
231 dbg_log("bud bytes %lld (%lld max), initiate BG commit",
232 c->bud_bytes, c->max_bud_bytes);
233 ubifs_request_bg_commit(c);
234 }
235
236 bud->lnum = lnum;
237 bud->start = offs;
238 bud->jhead = jhead;
239
240 ref->ch.node_type = UBIFS_REF_NODE;
241 ref->lnum = cpu_to_le32(bud->lnum);
242 ref->offs = cpu_to_le32(bud->start);
243 ref->jhead = cpu_to_le32(jhead);
244
245 if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
e11602ea 246 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
1e51764a
AB
247 c->lhead_offs = 0;
248 }
249
250 if (c->lhead_offs == 0) {
251 /* Must ensure next log LEB has been unmapped */
252 err = ubifs_leb_unmap(c, c->lhead_lnum);
253 if (err)
254 goto out_unlock;
255 }
256
257 if (bud->start == 0) {
258 /*
259 * Before writing the LEB reference which refers an empty LEB
260 * to the log, we have to make sure it is mapped, because
261 * otherwise we'd risk to refer an LEB with garbage in case of
262 * an unclean reboot, because the target LEB might have been
263 * unmapped, but not yet physically erased.
264 */
b36a261e 265 err = ubifs_leb_map(c, bud->lnum);
1e51764a
AB
266 if (err)
267 goto out_unlock;
268 }
269
270 dbg_log("write ref LEB %d:%d",
271 c->lhead_lnum, c->lhead_offs);
272 err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
b36a261e 273 c->lhead_offs);
1e51764a
AB
274 if (err)
275 goto out_unlock;
276
277 c->lhead_offs += c->ref_node_alsz;
278
279 ubifs_add_bud(c, bud);
280
281 mutex_unlock(&c->log_mutex);
282 kfree(ref);
283 return 0;
284
285out_unlock:
286 mutex_unlock(&c->log_mutex);
287 kfree(ref);
288 kfree(bud);
289 return err;
290}
291
292/**
293 * remove_buds - remove used buds.
294 * @c: UBIFS file-system description object
295 *
296 * This function removes use buds from the buds tree. It does not remove the
297 * buds which are pointed to by journal heads.
298 */
299static void remove_buds(struct ubifs_info *c)
300{
301 struct rb_node *p;
302
303 ubifs_assert(list_empty(&c->old_buds));
304 c->cmt_bud_bytes = 0;
305 spin_lock(&c->buds_lock);
306 p = rb_first(&c->buds);
307 while (p) {
308 struct rb_node *p1 = p;
309 struct ubifs_bud *bud;
310 struct ubifs_wbuf *wbuf;
311
312 p = rb_next(p);
313 bud = rb_entry(p1, struct ubifs_bud, rb);
314 wbuf = &c->jheads[bud->jhead].wbuf;
315
316 if (wbuf->lnum == bud->lnum) {
317 /*
318 * Do not remove buds which are pointed to by journal
319 * heads (non-closed buds).
320 */
321 c->cmt_bud_bytes += wbuf->offs - bud->start;
79fda517
AB
322 dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
323 bud->lnum, bud->start, dbg_jhead(bud->jhead),
324 wbuf->offs - bud->start, c->cmt_bud_bytes);
1e51764a
AB
325 bud->start = wbuf->offs;
326 } else {
327 c->cmt_bud_bytes += c->leb_size - bud->start;
79fda517
AB
328 dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
329 bud->lnum, bud->start, dbg_jhead(bud->jhead),
330 c->leb_size - bud->start, c->cmt_bud_bytes);
1e51764a 331 rb_erase(p1, &c->buds);
1e51764a
AB
332 /*
333 * If the commit does not finish, the recovery will need
334 * to replay the journal, in which case the old buds
335 * must be unchanged. Do not release them until post
336 * commit i.e. do not allow them to be garbage
337 * collected.
338 */
ec32816f 339 list_move(&bud->list, &c->old_buds);
1e51764a
AB
340 }
341 }
342 spin_unlock(&c->buds_lock);
343}
344
345/**
346 * ubifs_log_start_commit - start commit.
347 * @c: UBIFS file-system description object
348 * @ltail_lnum: return new log tail LEB number
349 *
350 * The commit operation starts with writing "commit start" node to the log and
351 * reference nodes for all journal heads which will define new journal after
352 * the commit has been finished. The commit start and reference nodes are
353 * written in one go to the nearest empty log LEB (hence, when commit is
354 * finished UBIFS may safely unmap all the previous log LEBs). This function
355 * returns zero in case of success and a negative error code in case of
356 * failure.
357 */
358int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
359{
360 void *buf;
361 struct ubifs_cs_node *cs;
362 struct ubifs_ref_node *ref;
363 int err, i, max_len, len;
364
365 err = dbg_check_bud_bytes(c);
366 if (err)
367 return err;
368
369 max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
370 max_len = ALIGN(max_len, c->min_io_size);
371 buf = cs = kmalloc(max_len, GFP_NOFS);
372 if (!buf)
373 return -ENOMEM;
374
375 cs->ch.node_type = UBIFS_CS_NODE;
014eb04b 376 cs->cmt_no = cpu_to_le64(c->cmt_no);
1e51764a
AB
377 ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
378
379 /*
380 * Note, we do not lock 'c->log_mutex' because this is the commit start
381 * phase and we are exclusively using the log. And we do not lock
382 * write-buffer because nobody can write to the file-system at this
383 * phase.
384 */
385
386 len = UBIFS_CS_NODE_SZ;
387 for (i = 0; i < c->jhead_cnt; i++) {
388 int lnum = c->jheads[i].wbuf.lnum;
389 int offs = c->jheads[i].wbuf.offs;
390
391 if (lnum == -1 || offs == c->leb_size)
392 continue;
393
77a7ae58
AB
394 dbg_log("add ref to LEB %d:%d for jhead %s",
395 lnum, offs, dbg_jhead(i));
1e51764a
AB
396 ref = buf + len;
397 ref->ch.node_type = UBIFS_REF_NODE;
398 ref->lnum = cpu_to_le32(lnum);
399 ref->offs = cpu_to_le32(offs);
400 ref->jhead = cpu_to_le32(i);
401
402 ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
403 len += UBIFS_REF_NODE_SZ;
404 }
405
406 ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
407
6fa3eb70
S
408#ifdef CONFIG_UBIFS_FS_FULL_USE_LOG
409 /* Not Switch to next log LEB, programming next available page in the same log LEB continuously*/
410
411 /* if available page is in the end of the LEB, switch to next LEB*/
412 if(c->lhead_offs >= (c->leb_size - (c->min_io_size * 4)) )
413 {
414 int old_lnum = c->lhead_lnum;
415 int old_offs = c->lhead_offs;
416 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
417 c->lhead_offs = 0;
418 ubifs_msg("switch log LEB %d:%d to %d:%d\n", old_lnum, old_offs, c->lhead_lnum, c->lhead_offs);
419 }
420#else
1e51764a
AB
421 /* Switch to the next log LEB */
422 if (c->lhead_offs) {
6fa3eb70
S
423 int old_lnum = c->lhead_lnum;
424 int old_offs = c->lhead_offs;
e11602ea 425 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
1e51764a 426 c->lhead_offs = 0;
6fa3eb70 427 ubifs_msg("switch log LEB %d:%d to %d:%d\n", old_lnum, old_offs, c->lhead_lnum, c->lhead_offs);
1e51764a 428 }
6fa3eb70 429#endif
1e51764a
AB
430
431 if (c->lhead_offs == 0) {
432 /* Must ensure next LEB has been unmapped */
433 err = ubifs_leb_unmap(c, c->lhead_lnum);
434 if (err)
435 goto out;
436 }
437
438 len = ALIGN(len, c->min_io_size);
439 dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
6fa3eb70 440 err = ubifs_leb_write(c, c->lhead_lnum, cs, c->lhead_offs, len); //MTK, modify offset 0 -> c->lhead_offs
1e51764a
AB
441 if (err)
442 goto out;
443
444 *ltail_lnum = c->lhead_lnum;
445
446 c->lhead_offs += len;
447 if (c->lhead_offs == c->leb_size) {
e11602ea 448 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
1e51764a
AB
449 c->lhead_offs = 0;
450 }
451
452 remove_buds(c);
453
454 /*
455 * We have started the commit and now users may use the rest of the log
456 * for new writes.
457 */
458 c->min_log_bytes = 0;
459
460out:
461 kfree(buf);
462 return err;
463}
464
465/**
466 * ubifs_log_end_commit - end commit.
467 * @c: UBIFS file-system description object
468 * @ltail_lnum: new log tail LEB number
469 *
470 * This function is called on when the commit operation was finished. It
6fa3eb70
S
471 * moves log tail to new position and updates the master node so that it stores
472 * the new log tail LEB number. Returns zero in case of success and a negative
473 * error code in case of failure.
1e51764a
AB
474 */
475int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
476{
477 int err;
478
479 /*
480 * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
481 * writes during commit. Its only short "commit" start phase when
482 * writers are blocked.
483 */
484 mutex_lock(&c->log_mutex);
485
486 dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
487 c->ltail_lnum, ltail_lnum);
488
489 c->ltail_lnum = ltail_lnum;
490 /*
491 * The commit is finished and from now on it must be guaranteed that
492 * there is always enough space for the next commit.
493 */
494 c->min_log_bytes = c->leb_size;
495
496 spin_lock(&c->buds_lock);
497 c->bud_bytes -= c->cmt_bud_bytes;
498 spin_unlock(&c->buds_lock);
499
500 err = dbg_check_bud_bytes(c);
6fa3eb70
S
501 if (err)
502 goto out;
1e51764a 503
6fa3eb70
S
504 err = ubifs_write_master(c);
505
506out:
1e51764a
AB
507 mutex_unlock(&c->log_mutex);
508 return err;
509}
510
511/**
512 * ubifs_log_post_commit - things to do after commit is completed.
513 * @c: UBIFS file-system description object
514 * @old_ltail_lnum: old log tail LEB number
515 *
516 * Release buds only after commit is completed, because they must be unchanged
517 * if recovery is needed.
518 *
519 * Unmap log LEBs only after commit is completed, because they may be needed for
520 * recovery.
521 *
522 * This function returns %0 on success and a negative error code on failure.
523 */
524int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
525{
526 int lnum, err = 0;
527
528 while (!list_empty(&c->old_buds)) {
529 struct ubifs_bud *bud;
530
531 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
532 err = ubifs_return_leb(c, bud->lnum);
533 if (err)
534 return err;
535 list_del(&bud->list);
536 kfree(bud);
537 }
538 mutex_lock(&c->log_mutex);
539 for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
e11602ea 540 lnum = ubifs_next_log_lnum(c, lnum)) {
1e51764a
AB
541 dbg_log("unmap log LEB %d", lnum);
542 err = ubifs_leb_unmap(c, lnum);
543 if (err)
544 goto out;
545 }
546out:
547 mutex_unlock(&c->log_mutex);
548 return err;
549}
550
551/**
552 * struct done_ref - references that have been done.
553 * @rb: rb-tree node
554 * @lnum: LEB number
555 */
556struct done_ref {
557 struct rb_node rb;
558 int lnum;
559};
560
561/**
562 * done_already - determine if a reference has been done already.
563 * @done_tree: rb-tree to store references that have been done
564 * @lnum: LEB number of reference
565 *
566 * This function returns %1 if the reference has been done, %0 if not, otherwise
567 * a negative error code is returned.
568 */
569static int done_already(struct rb_root *done_tree, int lnum)
570{
571 struct rb_node **p = &done_tree->rb_node, *parent = NULL;
572 struct done_ref *dr;
573
574 while (*p) {
575 parent = *p;
576 dr = rb_entry(parent, struct done_ref, rb);
577 if (lnum < dr->lnum)
578 p = &(*p)->rb_left;
579 else if (lnum > dr->lnum)
580 p = &(*p)->rb_right;
581 else
582 return 1;
583 }
584
585 dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
586 if (!dr)
587 return -ENOMEM;
588
589 dr->lnum = lnum;
590
591 rb_link_node(&dr->rb, parent, p);
592 rb_insert_color(&dr->rb, done_tree);
593
594 return 0;
595}
596
597/**
598 * destroy_done_tree - destroy the done tree.
599 * @done_tree: done tree to destroy
600 */
601static void destroy_done_tree(struct rb_root *done_tree)
602{
603 struct rb_node *this = done_tree->rb_node;
604 struct done_ref *dr;
605
606 while (this) {
607 if (this->rb_left) {
608 this = this->rb_left;
609 continue;
610 } else if (this->rb_right) {
611 this = this->rb_right;
612 continue;
613 }
614 dr = rb_entry(this, struct done_ref, rb);
615 this = rb_parent(this);
616 if (this) {
617 if (this->rb_left == &dr->rb)
618 this->rb_left = NULL;
619 else
620 this->rb_right = NULL;
621 }
622 kfree(dr);
623 }
624}
625
626/**
627 * add_node - add a node to the consolidated log.
628 * @c: UBIFS file-system description object
629 * @buf: buffer to which to add
630 * @lnum: LEB number to which to write is passed and returned here
631 * @offs: offset to where to write is passed and returned here
632 * @node: node to add
633 *
634 * This function returns %0 on success and a negative error code on failure.
635 */
636static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
637 void *node)
638{
639 struct ubifs_ch *ch = node;
640 int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
641
642 if (len > remains) {
643 int sz = ALIGN(*offs, c->min_io_size), err;
644
645 ubifs_pad(c, buf + *offs, sz - *offs);
b36a261e 646 err = ubifs_leb_change(c, *lnum, buf, sz);
1e51764a
AB
647 if (err)
648 return err;
e11602ea 649 *lnum = ubifs_next_log_lnum(c, *lnum);
1e51764a
AB
650 *offs = 0;
651 }
652 memcpy(buf + *offs, node, len);
653 *offs += ALIGN(len, 8);
654 return 0;
655}
656
657/**
658 * ubifs_consolidate_log - consolidate the log.
659 * @c: UBIFS file-system description object
660 *
661 * Repeated failed commits could cause the log to be full, but at least 1 LEB is
662 * needed for commit. This function rewrites the reference nodes in the log
663 * omitting duplicates, and failed CS nodes, and leaving no gaps.
664 *
665 * This function returns %0 on success and a negative error code on failure.
666 */
667int ubifs_consolidate_log(struct ubifs_info *c)
668{
669 struct ubifs_scan_leb *sleb;
670 struct ubifs_scan_node *snod;
671 struct rb_root done_tree = RB_ROOT;
672 int lnum, err, first = 1, write_lnum, offs = 0;
673 void *buf;
6fa3eb70
S
674#if defined(CONFIG_UBIFS_FS_FULL_USE_LOG)
675 const struct ubifs_cs_node *node;
676 unsigned long long cs_sqnum = 0;
677#endif
1e51764a
AB
678
679 dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
680 c->lhead_lnum);
6fa3eb70 681 buf = kmalloc(c->leb_size, GFP_KERNEL);
1e51764a
AB
682 if (!buf)
683 return -ENOMEM;
684 lnum = c->ltail_lnum;
685 write_lnum = lnum;
686 while (1) {
348709ba 687 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
1e51764a
AB
688 if (IS_ERR(sleb)) {
689 err = PTR_ERR(sleb);
690 goto out_free;
691 }
6fa3eb70
S
692#if defined(CONFIG_UBIFS_FS_FULL_USE_LOG)
693 /* Search for the last cs node */
694 if(lnum == c->ltail_lnum)
695 {
696 list_for_each_entry_reverse(snod, &sleb->nodes, list) {
697 if(snod->type == UBIFS_CS_NODE)
698 break;
699 }
700 node = snod->node;
701 cs_sqnum = le64_to_cpu(node->ch.sqnum);
702 }
703#endif
704
1e51764a 705 list_for_each_entry(snod, &sleb->nodes, list) {
6fa3eb70
S
706#if defined(CONFIG_UBIFS_FS_FULL_USE_LOG)
707 if(lnum == c->ltail_lnum)
708 {
709 /* Skip those nodes which locate in front of the last cs node*/
710 if(cs_sqnum > snod->sqnum)
711 continue;
712 }
713#endif
1e51764a
AB
714 switch (snod->type) {
715 case UBIFS_REF_NODE: {
716 struct ubifs_ref_node *ref = snod->node;
717 int ref_lnum = le32_to_cpu(ref->lnum);
718
719 err = done_already(&done_tree, ref_lnum);
720 if (err < 0)
721 goto out_scan;
722 if (err != 1) {
723 err = add_node(c, buf, &write_lnum,
724 &offs, snod->node);
725 if (err)
726 goto out_scan;
727 }
728 break;
729 }
730 case UBIFS_CS_NODE:
731 if (!first)
732 break;
733 err = add_node(c, buf, &write_lnum, &offs,
734 snod->node);
735 if (err)
736 goto out_scan;
737 first = 0;
738 break;
739 }
740 }
741 ubifs_scan_destroy(sleb);
742 if (lnum == c->lhead_lnum)
743 break;
e11602ea 744 lnum = ubifs_next_log_lnum(c, lnum);
1e51764a
AB
745 }
746 if (offs) {
747 int sz = ALIGN(offs, c->min_io_size);
748
749 ubifs_pad(c, buf + offs, sz - offs);
b36a261e 750 err = ubifs_leb_change(c, write_lnum, buf, sz);
1e51764a
AB
751 if (err)
752 goto out_free;
753 offs = ALIGN(offs, c->min_io_size);
754 }
755 destroy_done_tree(&done_tree);
6fa3eb70 756 kfree(buf);
1e51764a
AB
757 if (write_lnum == c->lhead_lnum) {
758 ubifs_err("log is too full");
759 return -EINVAL;
760 }
761 /* Unmap remaining LEBs */
762 lnum = write_lnum;
763 do {
e11602ea 764 lnum = ubifs_next_log_lnum(c, lnum);
1e51764a
AB
765 err = ubifs_leb_unmap(c, lnum);
766 if (err)
767 return err;
768 } while (lnum != c->lhead_lnum);
769 c->lhead_lnum = write_lnum;
770 c->lhead_offs = offs;
771 dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
772 return 0;
773
774out_scan:
775 ubifs_scan_destroy(sleb);
776out_free:
777 destroy_done_tree(&done_tree);
6fa3eb70 778 kfree(buf);
1e51764a
AB
779 return err;
780}
781
1e51764a
AB
782/**
783 * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
784 * @c: UBIFS file-system description object
785 *
786 * This function makes sure the amount of flash space used by closed buds
787 * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
788 * case of failure.
789 */
790static int dbg_check_bud_bytes(struct ubifs_info *c)
791{
792 int i, err = 0;
793 struct ubifs_bud *bud;
794 long long bud_bytes = 0;
795
2b1844a8 796 if (!dbg_is_chk_gen(c))
1e51764a
AB
797 return 0;
798
799 spin_lock(&c->buds_lock);
800 for (i = 0; i < c->jhead_cnt; i++)
801 list_for_each_entry(bud, &c->jheads[i].buds_list, list)
802 bud_bytes += c->leb_size - bud->start;
803
804 if (c->bud_bytes != bud_bytes) {
805 ubifs_err("bad bud_bytes %lld, calculated %lld",
806 c->bud_bytes, bud_bytes);
807 err = -EINVAL;
808 }
809 spin_unlock(&c->buds_lock);
810
811 return err;
812}