2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Artem Bityutskiy (Битюцкий Артём)
13 * This file is a part of UBIFS journal implementation and contains various
14 * functions which manipulate the log. The log is a fixed area on the flash
15 * which does not contain any data but refers to buds. The log is a part of the
20 #include <linux/err.h>
24 static int dbg_check_bud_bytes(struct ubifs_info *c);
27 * ubifs_search_bud - search bud LEB.
28 * @c: UBIFS file-system description object
29 * @lnum: logical eraseblock number to search
31 * This function searches bud LEB @lnum. Returns bud description object in case
32 * of success and %NULL if there is no bud with this LEB number.
34 struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
37 struct ubifs_bud *bud;
39 spin_lock(&c->buds_lock);
42 bud = rb_entry(p, struct ubifs_bud, rb);
45 else if (lnum > bud->lnum)
48 spin_unlock(&c->buds_lock);
52 spin_unlock(&c->buds_lock);
57 * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
58 * @c: UBIFS file-system description object
59 * @lnum: logical eraseblock number to search
61 * This functions returns the wbuf for @lnum or %NULL if there is not one.
63 struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
66 struct ubifs_bud *bud;
72 spin_lock(&c->buds_lock);
75 bud = rb_entry(p, struct ubifs_bud, rb);
78 else if (lnum > bud->lnum)
82 spin_unlock(&c->buds_lock);
83 return &c->jheads[jhead].wbuf;
86 spin_unlock(&c->buds_lock);
91 * empty_log_bytes - calculate amount of empty space in the log.
92 * @c: UBIFS file-system description object
94 static inline long long empty_log_bytes(const struct ubifs_info *c)
98 h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
99 t = (long long)c->ltail_lnum * c->leb_size;
102 return c->log_bytes - h + t;
108 * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
109 * @c: UBIFS file-system description object
110 * @bud: the bud to add
112 void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
114 struct rb_node **p, *parent = NULL;
116 struct ubifs_jhead *jhead;
118 spin_lock(&c->buds_lock);
119 p = &c->buds.rb_node;
122 b = rb_entry(parent, struct ubifs_bud, rb);
123 ubifs_assert(bud->lnum != b->lnum);
124 if (bud->lnum < b->lnum)
130 rb_link_node(&bud->rb, parent, p);
131 rb_insert_color(&bud->rb, &c->buds);
133 jhead = &c->jheads[bud->jhead];
134 list_add_tail(&bud->list, &jhead->buds_list);
136 ubifs_assert(c->replaying && c->ro_mount);
139 * Note, although this is a new bud, we anyway account this space now,
140 * before any data has been written to it, because this is about to
141 * guarantee fixed mount time, and this bud will anyway be read and
144 c->bud_bytes += c->leb_size - bud->start;
146 dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
147 bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
148 spin_unlock(&c->buds_lock);
152 * ubifs_add_bud_to_log - add a new bud to the log.
153 * @c: UBIFS file-system description object
154 * @jhead: journal head the bud belongs to
155 * @lnum: LEB number of the bud
156 * @offs: starting offset of the bud
158 * This function writes reference node for the new bud LEB @lnum it to the log,
159 * and adds it to the buds tress. It also makes sure that log size does not
160 * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
161 * %-EAGAIN if commit is required, and a negative error codes in case of
164 int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
167 struct ubifs_bud *bud;
168 struct ubifs_ref_node *ref;
170 bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
173 ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
179 mutex_lock(&c->log_mutex);
180 ubifs_assert(!c->ro_media && !c->ro_mount);
186 /* Make sure we have enough space in the log */
187 if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
188 dbg_log("not enough log space - %lld, required %d",
189 empty_log_bytes(c), c->min_log_bytes);
190 ubifs_commit_required(c);
196 * Make sure the amount of space in buds will not exceed the
197 * 'c->max_bud_bytes' limit, because we want to guarantee mount time
200 * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
201 * because we are holding @c->log_mutex. All @c->bud_bytes take place
202 * when both @c->log_mutex and @c->bud_bytes are locked.
204 if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
205 dbg_log("bud bytes %lld (%lld max), require commit",
206 c->bud_bytes, c->max_bud_bytes);
207 ubifs_commit_required(c);
213 * If the journal is full enough - start background commit. Note, it is
214 * OK to read 'c->cmt_state' without spinlock because integer reads
215 * are atomic in the kernel.
217 if (c->bud_bytes >= c->bg_bud_bytes &&
218 c->cmt_state == COMMIT_RESTING) {
219 dbg_log("bud bytes %lld (%lld max), initiate BG commit",
220 c->bud_bytes, c->max_bud_bytes);
221 ubifs_request_bg_commit(c);
228 ref->ch.node_type = UBIFS_REF_NODE;
229 ref->lnum = cpu_to_le32(bud->lnum);
230 ref->offs = cpu_to_le32(bud->start);
231 ref->jhead = cpu_to_le32(jhead);
233 if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
234 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
238 if (c->lhead_offs == 0) {
239 /* Must ensure next log LEB has been unmapped */
240 err = ubifs_leb_unmap(c, c->lhead_lnum);
245 if (bud->start == 0) {
247 * Before writing the LEB reference which refers an empty LEB
248 * to the log, we have to make sure it is mapped, because
249 * otherwise we'd risk to refer an LEB with garbage in case of
250 * an unclean reboot, because the target LEB might have been
251 * unmapped, but not yet physically erased.
253 err = ubifs_leb_map(c, bud->lnum);
258 dbg_log("write ref LEB %d:%d",
259 c->lhead_lnum, c->lhead_offs);
260 err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
265 c->lhead_offs += c->ref_node_alsz;
267 ubifs_add_bud(c, bud);
269 mutex_unlock(&c->log_mutex);
274 mutex_unlock(&c->log_mutex);
281 * remove_buds - remove used buds.
282 * @c: UBIFS file-system description object
284 * This function removes use buds from the buds tree. It does not remove the
285 * buds which are pointed to by journal heads.
287 static void remove_buds(struct ubifs_info *c)
291 ubifs_assert(list_empty(&c->old_buds));
292 c->cmt_bud_bytes = 0;
293 spin_lock(&c->buds_lock);
294 p = rb_first(&c->buds);
296 struct rb_node *p1 = p;
297 struct ubifs_bud *bud;
298 struct ubifs_wbuf *wbuf;
301 bud = rb_entry(p1, struct ubifs_bud, rb);
302 wbuf = &c->jheads[bud->jhead].wbuf;
304 if (wbuf->lnum == bud->lnum) {
306 * Do not remove buds which are pointed to by journal
307 * heads (non-closed buds).
309 c->cmt_bud_bytes += wbuf->offs - bud->start;
310 dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
311 bud->lnum, bud->start, dbg_jhead(bud->jhead),
312 wbuf->offs - bud->start, c->cmt_bud_bytes);
313 bud->start = wbuf->offs;
315 c->cmt_bud_bytes += c->leb_size - bud->start;
316 dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
317 bud->lnum, bud->start, dbg_jhead(bud->jhead),
318 c->leb_size - bud->start, c->cmt_bud_bytes);
319 rb_erase(p1, &c->buds);
321 * If the commit does not finish, the recovery will need
322 * to replay the journal, in which case the old buds
323 * must be unchanged. Do not release them until post
324 * commit i.e. do not allow them to be garbage
327 list_move(&bud->list, &c->old_buds);
330 spin_unlock(&c->buds_lock);
334 * ubifs_log_start_commit - start commit.
335 * @c: UBIFS file-system description object
336 * @ltail_lnum: return new log tail LEB number
338 * The commit operation starts with writing "commit start" node to the log and
339 * reference nodes for all journal heads which will define new journal after
340 * the commit has been finished. The commit start and reference nodes are
341 * written in one go to the nearest empty log LEB (hence, when commit is
342 * finished UBIFS may safely unmap all the previous log LEBs). This function
343 * returns zero in case of success and a negative error code in case of
346 int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
349 struct ubifs_cs_node *cs;
350 struct ubifs_ref_node *ref;
351 int err, i, max_len, len;
353 err = dbg_check_bud_bytes(c);
357 max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
358 max_len = ALIGN(max_len, c->min_io_size);
359 buf = cs = kmalloc(max_len, GFP_NOFS);
363 cs->ch.node_type = UBIFS_CS_NODE;
364 cs->cmt_no = cpu_to_le64(c->cmt_no);
365 ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
368 * Note, we do not lock 'c->log_mutex' because this is the commit start
369 * phase and we are exclusively using the log. And we do not lock
370 * write-buffer because nobody can write to the file-system at this
374 len = UBIFS_CS_NODE_SZ;
375 for (i = 0; i < c->jhead_cnt; i++) {
376 int lnum = c->jheads[i].wbuf.lnum;
377 int offs = c->jheads[i].wbuf.offs;
379 if (lnum == -1 || offs == c->leb_size)
382 dbg_log("add ref to LEB %d:%d for jhead %s",
383 lnum, offs, dbg_jhead(i));
385 ref->ch.node_type = UBIFS_REF_NODE;
386 ref->lnum = cpu_to_le32(lnum);
387 ref->offs = cpu_to_le32(offs);
388 ref->jhead = cpu_to_le32(i);
390 ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
391 len += UBIFS_REF_NODE_SZ;
394 ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
396 /* Switch to the next log LEB */
398 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
402 if (c->lhead_offs == 0) {
403 /* Must ensure next LEB has been unmapped */
404 err = ubifs_leb_unmap(c, c->lhead_lnum);
409 len = ALIGN(len, c->min_io_size);
410 dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
411 err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
415 *ltail_lnum = c->lhead_lnum;
417 c->lhead_offs += len;
418 if (c->lhead_offs == c->leb_size) {
419 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
426 * We have started the commit and now users may use the rest of the log
429 c->min_log_bytes = 0;
437 * ubifs_log_end_commit - end commit.
438 * @c: UBIFS file-system description object
439 * @ltail_lnum: new log tail LEB number
441 * This function is called on when the commit operation was finished. It
442 * moves log tail to new position and unmaps LEBs which contain obsolete data.
443 * Returns zero in case of success and a negative error code in case of
446 int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
451 * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
452 * writes during commit. Its only short "commit" start phase when
453 * writers are blocked.
455 mutex_lock(&c->log_mutex);
457 dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
458 c->ltail_lnum, ltail_lnum);
460 c->ltail_lnum = ltail_lnum;
462 * The commit is finished and from now on it must be guaranteed that
463 * there is always enough space for the next commit.
465 c->min_log_bytes = c->leb_size;
467 spin_lock(&c->buds_lock);
468 c->bud_bytes -= c->cmt_bud_bytes;
469 spin_unlock(&c->buds_lock);
471 err = dbg_check_bud_bytes(c);
473 mutex_unlock(&c->log_mutex);
478 * ubifs_log_post_commit - things to do after commit is completed.
479 * @c: UBIFS file-system description object
480 * @old_ltail_lnum: old log tail LEB number
482 * Release buds only after commit is completed, because they must be unchanged
483 * if recovery is needed.
485 * Unmap log LEBs only after commit is completed, because they may be needed for
488 * This function returns %0 on success and a negative error code on failure.
490 int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
494 while (!list_empty(&c->old_buds)) {
495 struct ubifs_bud *bud;
497 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
498 err = ubifs_return_leb(c, bud->lnum);
501 list_del(&bud->list);
504 mutex_lock(&c->log_mutex);
505 for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
506 lnum = ubifs_next_log_lnum(c, lnum)) {
507 dbg_log("unmap log LEB %d", lnum);
508 err = ubifs_leb_unmap(c, lnum);
513 mutex_unlock(&c->log_mutex);
518 * struct done_ref - references that have been done.
528 * done_already - determine if a reference has been done already.
529 * @done_tree: rb-tree to store references that have been done
530 * @lnum: LEB number of reference
532 * This function returns %1 if the reference has been done, %0 if not, otherwise
533 * a negative error code is returned.
535 static int done_already(struct rb_root *done_tree, int lnum)
537 struct rb_node **p = &done_tree->rb_node, *parent = NULL;
542 dr = rb_entry(parent, struct done_ref, rb);
545 else if (lnum > dr->lnum)
551 dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
557 rb_link_node(&dr->rb, parent, p);
558 rb_insert_color(&dr->rb, done_tree);
564 * destroy_done_tree - destroy the done tree.
565 * @done_tree: done tree to destroy
567 static void destroy_done_tree(struct rb_root *done_tree)
569 struct done_ref *dr, *n;
571 rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
576 * add_node - add a node to the consolidated log.
577 * @c: UBIFS file-system description object
578 * @buf: buffer to which to add
579 * @lnum: LEB number to which to write is passed and returned here
580 * @offs: offset to where to write is passed and returned here
583 * This function returns %0 on success and a negative error code on failure.
585 static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
588 struct ubifs_ch *ch = node;
589 int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
592 int sz = ALIGN(*offs, c->min_io_size), err;
594 ubifs_pad(c, buf + *offs, sz - *offs);
595 err = ubifs_leb_change(c, *lnum, buf, sz);
598 *lnum = ubifs_next_log_lnum(c, *lnum);
601 memcpy(buf + *offs, node, len);
602 *offs += ALIGN(len, 8);
607 * ubifs_consolidate_log - consolidate the log.
608 * @c: UBIFS file-system description object
610 * Repeated failed commits could cause the log to be full, but at least 1 LEB is
611 * needed for commit. This function rewrites the reference nodes in the log
612 * omitting duplicates, and failed CS nodes, and leaving no gaps.
614 * This function returns %0 on success and a negative error code on failure.
616 int ubifs_consolidate_log(struct ubifs_info *c)
618 struct ubifs_scan_leb *sleb;
619 struct ubifs_scan_node *snod;
620 struct rb_root done_tree = RB_ROOT;
621 int lnum, err, first = 1, write_lnum, offs = 0;
624 dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
626 buf = vmalloc(c->leb_size);
629 lnum = c->ltail_lnum;
632 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
637 list_for_each_entry(snod, &sleb->nodes, list) {
638 switch (snod->type) {
639 case UBIFS_REF_NODE: {
640 struct ubifs_ref_node *ref = snod->node;
641 int ref_lnum = le32_to_cpu(ref->lnum);
643 err = done_already(&done_tree, ref_lnum);
647 err = add_node(c, buf, &write_lnum,
657 err = add_node(c, buf, &write_lnum, &offs,
665 ubifs_scan_destroy(sleb);
666 if (lnum == c->lhead_lnum)
668 lnum = ubifs_next_log_lnum(c, lnum);
671 int sz = ALIGN(offs, c->min_io_size);
673 ubifs_pad(c, buf + offs, sz - offs);
674 err = ubifs_leb_change(c, write_lnum, buf, sz);
677 offs = ALIGN(offs, c->min_io_size);
679 destroy_done_tree(&done_tree);
681 if (write_lnum == c->lhead_lnum) {
682 ubifs_err("log is too full");
685 /* Unmap remaining LEBs */
688 lnum = ubifs_next_log_lnum(c, lnum);
689 err = ubifs_leb_unmap(c, lnum);
692 } while (lnum != c->lhead_lnum);
693 c->lhead_lnum = write_lnum;
694 c->lhead_offs = offs;
695 dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
699 ubifs_scan_destroy(sleb);
701 destroy_done_tree(&done_tree);
707 * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
708 * @c: UBIFS file-system description object
710 * This function makes sure the amount of flash space used by closed buds
711 * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
714 static int dbg_check_bud_bytes(struct ubifs_info *c)
717 struct ubifs_bud *bud;
718 long long bud_bytes = 0;
720 if (!dbg_is_chk_gen(c))
723 spin_lock(&c->buds_lock);
724 for (i = 0; i < c->jhead_cnt; i++)
725 list_for_each_entry(bud, &c->jheads[i].buds_list, list)
726 bud_bytes += c->leb_size - bud->start;
728 if (c->bud_bytes != bud_bytes) {
729 ubifs_err("bad bud_bytes %lld, calculated %lld",
730 c->bud_bytes, bud_bytes);
733 spin_unlock(&c->buds_lock);