blob: 9bd4aafd5c6f21e56683ba3c45eb48681494418d [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: 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
Artem Bityutskiy1e517642008-07-14 19:08:37 +030032static int dbg_check_bud_bytes(struct ubifs_info *c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030033
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
98/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +030099 * 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
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300144 ubifs_assert(c->replaying && c->ro_mount);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300145
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
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300154 dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
155 bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300156 spin_unlock(&c->buds_lock);
157}
158
159/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300160 * 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);
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300188 ubifs_assert(!c->ro_media && !c->ro_mount);
Artem Bityutskiy2680d722010-09-17 16:44:28 +0300189 if (c->ro_error) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300190 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 /*
Artem Bityutskiy7d4e9cc2009-03-20 19:11:12 +0200204 * Make sure the amount of space in buds will not exceed the
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300205 * '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) {
Artem Bityutskiye11602e2011-05-06 17:52:32 +0300242 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
hujianyang25601a32014-07-30 10:37:53 +0800243 ubifs_assert(c->lhead_lnum != c->ltail_lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300244 c->lhead_offs = 0;
245 }
246
247 if (c->lhead_offs == 0) {
248 /* Must ensure next log LEB has been unmapped */
249 err = ubifs_leb_unmap(c, c->lhead_lnum);
250 if (err)
251 goto out_unlock;
252 }
253
254 if (bud->start == 0) {
255 /*
256 * Before writing the LEB reference which refers an empty LEB
257 * to the log, we have to make sure it is mapped, because
258 * otherwise we'd risk to refer an LEB with garbage in case of
259 * an unclean reboot, because the target LEB might have been
260 * unmapped, but not yet physically erased.
261 */
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200262 err = ubifs_leb_map(c, bud->lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300263 if (err)
264 goto out_unlock;
265 }
266
267 dbg_log("write ref LEB %d:%d",
268 c->lhead_lnum, c->lhead_offs);
269 err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200270 c->lhead_offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300271 if (err)
272 goto out_unlock;
273
274 c->lhead_offs += c->ref_node_alsz;
275
276 ubifs_add_bud(c, bud);
277
278 mutex_unlock(&c->log_mutex);
279 kfree(ref);
280 return 0;
281
282out_unlock:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300283 mutex_unlock(&c->log_mutex);
284 kfree(ref);
285 kfree(bud);
286 return err;
287}
288
289/**
290 * remove_buds - remove used buds.
291 * @c: UBIFS file-system description object
292 *
293 * This function removes use buds from the buds tree. It does not remove the
294 * buds which are pointed to by journal heads.
295 */
296static void remove_buds(struct ubifs_info *c)
297{
298 struct rb_node *p;
299
300 ubifs_assert(list_empty(&c->old_buds));
301 c->cmt_bud_bytes = 0;
302 spin_lock(&c->buds_lock);
303 p = rb_first(&c->buds);
304 while (p) {
305 struct rb_node *p1 = p;
306 struct ubifs_bud *bud;
307 struct ubifs_wbuf *wbuf;
308
309 p = rb_next(p);
310 bud = rb_entry(p1, struct ubifs_bud, rb);
311 wbuf = &c->jheads[bud->jhead].wbuf;
312
313 if (wbuf->lnum == bud->lnum) {
314 /*
315 * Do not remove buds which are pointed to by journal
316 * heads (non-closed buds).
317 */
318 c->cmt_bud_bytes += wbuf->offs - bud->start;
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300319 dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
320 bud->lnum, bud->start, dbg_jhead(bud->jhead),
321 wbuf->offs - bud->start, c->cmt_bud_bytes);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300322 bud->start = wbuf->offs;
323 } else {
324 c->cmt_bud_bytes += c->leb_size - bud->start;
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300325 dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
326 bud->lnum, bud->start, dbg_jhead(bud->jhead),
327 c->leb_size - bud->start, c->cmt_bud_bytes);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300328 rb_erase(p1, &c->buds);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300329 /*
330 * If the commit does not finish, the recovery will need
331 * to replay the journal, in which case the old buds
332 * must be unchanged. Do not release them until post
333 * commit i.e. do not allow them to be garbage
334 * collected.
335 */
Eric Sesterhennec328162009-02-13 09:13:11 +0100336 list_move(&bud->list, &c->old_buds);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300337 }
338 }
339 spin_unlock(&c->buds_lock);
340}
341
342/**
343 * ubifs_log_start_commit - start commit.
344 * @c: UBIFS file-system description object
345 * @ltail_lnum: return new log tail LEB number
346 *
347 * The commit operation starts with writing "commit start" node to the log and
348 * reference nodes for all journal heads which will define new journal after
349 * the commit has been finished. The commit start and reference nodes are
350 * written in one go to the nearest empty log LEB (hence, when commit is
351 * finished UBIFS may safely unmap all the previous log LEBs). This function
352 * returns zero in case of success and a negative error code in case of
353 * failure.
354 */
355int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
356{
357 void *buf;
358 struct ubifs_cs_node *cs;
359 struct ubifs_ref_node *ref;
360 int err, i, max_len, len;
361
362 err = dbg_check_bud_bytes(c);
363 if (err)
364 return err;
365
366 max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
367 max_len = ALIGN(max_len, c->min_io_size);
368 buf = cs = kmalloc(max_len, GFP_NOFS);
369 if (!buf)
370 return -ENOMEM;
371
372 cs->ch.node_type = UBIFS_CS_NODE;
Artem Bityutskiy014eb042008-07-21 17:14:29 +0300373 cs->cmt_no = cpu_to_le64(c->cmt_no);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300374 ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
375
376 /*
377 * Note, we do not lock 'c->log_mutex' because this is the commit start
378 * phase and we are exclusively using the log. And we do not lock
379 * write-buffer because nobody can write to the file-system at this
380 * phase.
381 */
382
383 len = UBIFS_CS_NODE_SZ;
384 for (i = 0; i < c->jhead_cnt; i++) {
385 int lnum = c->jheads[i].wbuf.lnum;
386 int offs = c->jheads[i].wbuf.offs;
387
388 if (lnum == -1 || offs == c->leb_size)
389 continue;
390
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300391 dbg_log("add ref to LEB %d:%d for jhead %s",
392 lnum, offs, dbg_jhead(i));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300393 ref = buf + len;
394 ref->ch.node_type = UBIFS_REF_NODE;
395 ref->lnum = cpu_to_le32(lnum);
396 ref->offs = cpu_to_le32(offs);
397 ref->jhead = cpu_to_le32(i);
398
399 ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
400 len += UBIFS_REF_NODE_SZ;
401 }
402
403 ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
404
405 /* Switch to the next log LEB */
406 if (c->lhead_offs) {
Artem Bityutskiye11602e2011-05-06 17:52:32 +0300407 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
hujianyang25601a32014-07-30 10:37:53 +0800408 ubifs_assert(c->lhead_lnum != c->ltail_lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300409 c->lhead_offs = 0;
410 }
411
Artem Bityutskiyf1cb7052014-06-29 17:22:16 +0300412 /* Must ensure next LEB has been unmapped */
413 err = ubifs_leb_unmap(c, c->lhead_lnum);
414 if (err)
415 goto out;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300416
417 len = ALIGN(len, c->min_io_size);
418 dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200419 err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300420 if (err)
421 goto out;
422
423 *ltail_lnum = c->lhead_lnum;
424
425 c->lhead_offs += len;
426 if (c->lhead_offs == c->leb_size) {
Artem Bityutskiye11602e2011-05-06 17:52:32 +0300427 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300428 c->lhead_offs = 0;
429 }
430
431 remove_buds(c);
432
433 /*
434 * We have started the commit and now users may use the rest of the log
435 * for new writes.
436 */
437 c->min_log_bytes = 0;
438
439out:
440 kfree(buf);
441 return err;
442}
443
444/**
445 * ubifs_log_end_commit - end commit.
446 * @c: UBIFS file-system description object
447 * @ltail_lnum: new log tail LEB number
448 *
449 * This function is called on when the commit operation was finished. It
Artem Bityutskiy052c2802014-06-29 17:00:45 +0300450 * moves log tail to new position and updates the master node so that it stores
451 * the new log tail LEB number. Returns zero in case of success and a negative
452 * error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300453 */
454int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
455{
456 int err;
457
458 /*
459 * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
460 * writes during commit. Its only short "commit" start phase when
461 * writers are blocked.
462 */
463 mutex_lock(&c->log_mutex);
464
465 dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
466 c->ltail_lnum, ltail_lnum);
467
468 c->ltail_lnum = ltail_lnum;
469 /*
470 * The commit is finished and from now on it must be guaranteed that
471 * there is always enough space for the next commit.
472 */
473 c->min_log_bytes = c->leb_size;
474
475 spin_lock(&c->buds_lock);
476 c->bud_bytes -= c->cmt_bud_bytes;
477 spin_unlock(&c->buds_lock);
478
479 err = dbg_check_bud_bytes(c);
Artem Bityutskiy052c2802014-06-29 17:00:45 +0300480 if (err)
481 goto out;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300482
Artem Bityutskiy052c2802014-06-29 17:00:45 +0300483 err = ubifs_write_master(c);
484
485out:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300486 mutex_unlock(&c->log_mutex);
487 return err;
488}
489
490/**
491 * ubifs_log_post_commit - things to do after commit is completed.
492 * @c: UBIFS file-system description object
493 * @old_ltail_lnum: old log tail LEB number
494 *
495 * Release buds only after commit is completed, because they must be unchanged
496 * if recovery is needed.
497 *
498 * Unmap log LEBs only after commit is completed, because they may be needed for
499 * recovery.
500 *
501 * This function returns %0 on success and a negative error code on failure.
502 */
503int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
504{
505 int lnum, err = 0;
506
507 while (!list_empty(&c->old_buds)) {
508 struct ubifs_bud *bud;
509
510 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
511 err = ubifs_return_leb(c, bud->lnum);
512 if (err)
513 return err;
514 list_del(&bud->list);
515 kfree(bud);
516 }
517 mutex_lock(&c->log_mutex);
518 for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
Artem Bityutskiye11602e2011-05-06 17:52:32 +0300519 lnum = ubifs_next_log_lnum(c, lnum)) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300520 dbg_log("unmap log LEB %d", lnum);
521 err = ubifs_leb_unmap(c, lnum);
522 if (err)
523 goto out;
524 }
525out:
526 mutex_unlock(&c->log_mutex);
527 return err;
528}
529
530/**
531 * struct done_ref - references that have been done.
532 * @rb: rb-tree node
533 * @lnum: LEB number
534 */
535struct done_ref {
536 struct rb_node rb;
537 int lnum;
538};
539
540/**
541 * done_already - determine if a reference has been done already.
542 * @done_tree: rb-tree to store references that have been done
543 * @lnum: LEB number of reference
544 *
545 * This function returns %1 if the reference has been done, %0 if not, otherwise
546 * a negative error code is returned.
547 */
548static int done_already(struct rb_root *done_tree, int lnum)
549{
550 struct rb_node **p = &done_tree->rb_node, *parent = NULL;
551 struct done_ref *dr;
552
553 while (*p) {
554 parent = *p;
555 dr = rb_entry(parent, struct done_ref, rb);
556 if (lnum < dr->lnum)
557 p = &(*p)->rb_left;
558 else if (lnum > dr->lnum)
559 p = &(*p)->rb_right;
560 else
561 return 1;
562 }
563
564 dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
565 if (!dr)
566 return -ENOMEM;
567
568 dr->lnum = lnum;
569
570 rb_link_node(&dr->rb, parent, p);
571 rb_insert_color(&dr->rb, done_tree);
572
573 return 0;
574}
575
576/**
577 * destroy_done_tree - destroy the done tree.
578 * @done_tree: done tree to destroy
579 */
580static void destroy_done_tree(struct rb_root *done_tree)
581{
Cody P Schaferbb25e492014-01-23 15:56:08 -0800582 struct done_ref *dr, *n;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300583
Cody P Schaferbb25e492014-01-23 15:56:08 -0800584 rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300585 kfree(dr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300586}
587
588/**
589 * add_node - add a node to the consolidated log.
590 * @c: UBIFS file-system description object
591 * @buf: buffer to which to add
592 * @lnum: LEB number to which to write is passed and returned here
593 * @offs: offset to where to write is passed and returned here
594 * @node: node to add
595 *
596 * This function returns %0 on success and a negative error code on failure.
597 */
598static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
599 void *node)
600{
601 struct ubifs_ch *ch = node;
602 int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
603
604 if (len > remains) {
605 int sz = ALIGN(*offs, c->min_io_size), err;
606
607 ubifs_pad(c, buf + *offs, sz - *offs);
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200608 err = ubifs_leb_change(c, *lnum, buf, sz);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300609 if (err)
610 return err;
Artem Bityutskiye11602e2011-05-06 17:52:32 +0300611 *lnum = ubifs_next_log_lnum(c, *lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300612 *offs = 0;
613 }
614 memcpy(buf + *offs, node, len);
615 *offs += ALIGN(len, 8);
616 return 0;
617}
618
619/**
620 * ubifs_consolidate_log - consolidate the log.
621 * @c: UBIFS file-system description object
622 *
623 * Repeated failed commits could cause the log to be full, but at least 1 LEB is
624 * needed for commit. This function rewrites the reference nodes in the log
625 * omitting duplicates, and failed CS nodes, and leaving no gaps.
626 *
627 * This function returns %0 on success and a negative error code on failure.
628 */
629int ubifs_consolidate_log(struct ubifs_info *c)
630{
631 struct ubifs_scan_leb *sleb;
632 struct ubifs_scan_node *snod;
633 struct rb_root done_tree = RB_ROOT;
634 int lnum, err, first = 1, write_lnum, offs = 0;
635 void *buf;
636
637 dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
638 c->lhead_lnum);
639 buf = vmalloc(c->leb_size);
640 if (!buf)
641 return -ENOMEM;
642 lnum = c->ltail_lnum;
643 write_lnum = lnum;
644 while (1) {
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300645 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300646 if (IS_ERR(sleb)) {
647 err = PTR_ERR(sleb);
648 goto out_free;
649 }
650 list_for_each_entry(snod, &sleb->nodes, list) {
651 switch (snod->type) {
652 case UBIFS_REF_NODE: {
653 struct ubifs_ref_node *ref = snod->node;
654 int ref_lnum = le32_to_cpu(ref->lnum);
655
656 err = done_already(&done_tree, ref_lnum);
657 if (err < 0)
658 goto out_scan;
659 if (err != 1) {
660 err = add_node(c, buf, &write_lnum,
661 &offs, snod->node);
662 if (err)
663 goto out_scan;
664 }
665 break;
666 }
667 case UBIFS_CS_NODE:
668 if (!first)
669 break;
670 err = add_node(c, buf, &write_lnum, &offs,
671 snod->node);
672 if (err)
673 goto out_scan;
674 first = 0;
675 break;
676 }
677 }
678 ubifs_scan_destroy(sleb);
679 if (lnum == c->lhead_lnum)
680 break;
Artem Bityutskiye11602e2011-05-06 17:52:32 +0300681 lnum = ubifs_next_log_lnum(c, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300682 }
683 if (offs) {
684 int sz = ALIGN(offs, c->min_io_size);
685
686 ubifs_pad(c, buf + offs, sz - offs);
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200687 err = ubifs_leb_change(c, write_lnum, buf, sz);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300688 if (err)
689 goto out_free;
690 offs = ALIGN(offs, c->min_io_size);
691 }
692 destroy_done_tree(&done_tree);
693 vfree(buf);
694 if (write_lnum == c->lhead_lnum) {
695 ubifs_err("log is too full");
696 return -EINVAL;
697 }
698 /* Unmap remaining LEBs */
699 lnum = write_lnum;
700 do {
Artem Bityutskiye11602e2011-05-06 17:52:32 +0300701 lnum = ubifs_next_log_lnum(c, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300702 err = ubifs_leb_unmap(c, lnum);
703 if (err)
704 return err;
705 } while (lnum != c->lhead_lnum);
706 c->lhead_lnum = write_lnum;
707 c->lhead_offs = offs;
708 dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
709 return 0;
710
711out_scan:
712 ubifs_scan_destroy(sleb);
713out_free:
714 destroy_done_tree(&done_tree);
715 vfree(buf);
716 return err;
717}
718
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300719/**
720 * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
721 * @c: UBIFS file-system description object
722 *
723 * This function makes sure the amount of flash space used by closed buds
724 * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
725 * case of failure.
726 */
727static int dbg_check_bud_bytes(struct ubifs_info *c)
728{
729 int i, err = 0;
730 struct ubifs_bud *bud;
731 long long bud_bytes = 0;
732
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +0300733 if (!dbg_is_chk_gen(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300734 return 0;
735
736 spin_lock(&c->buds_lock);
737 for (i = 0; i < c->jhead_cnt; i++)
738 list_for_each_entry(bud, &c->jheads[i].buds_list, list)
739 bud_bytes += c->leb_size - bud->start;
740
741 if (c->bud_bytes != bud_bytes) {
742 ubifs_err("bad bud_bytes %lld, calculated %lld",
743 c->bud_bytes, bud_bytes);
744 err = -EINVAL;
745 }
746 spin_unlock(&c->buds_lock);
747
748 return err;
749}