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