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