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