1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Authors: Artem Bityutskiy (Битюцкий Артём)
12 * This file is a part of UBIFS journal implementation and contains various
13 * functions which manipulate the log. The log is a fixed area on the flash
14 * which does not contain any data but refers to buds. The log is a part of the
20 static int dbg_check_bud_bytes(struct ubifs_info *c);
23 * ubifs_search_bud - search bud LEB.
24 * @c: UBIFS file-system description object
25 * @lnum: logical eraseblock number to search
27 * This function searches bud LEB @lnum. Returns bud description object in case
28 * of success and %NULL if there is no bud with this LEB number.
30 struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
33 struct ubifs_bud *bud;
35 spin_lock(&c->buds_lock);
38 bud = rb_entry(p, struct ubifs_bud, rb);
41 else if (lnum > bud->lnum)
44 spin_unlock(&c->buds_lock);
48 spin_unlock(&c->buds_lock);
53 * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
54 * @c: UBIFS file-system description object
55 * @lnum: logical eraseblock number to search
57 * This functions returns the wbuf for @lnum or %NULL if there is not one.
59 struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
62 struct ubifs_bud *bud;
68 spin_lock(&c->buds_lock);
71 bud = rb_entry(p, struct ubifs_bud, rb);
74 else if (lnum > bud->lnum)
78 spin_unlock(&c->buds_lock);
79 return &c->jheads[jhead].wbuf;
82 spin_unlock(&c->buds_lock);
87 * empty_log_bytes - calculate amount of empty space in the log.
88 * @c: UBIFS file-system description object
90 static inline long long empty_log_bytes(const struct ubifs_info *c)
94 h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
95 t = (long long)c->ltail_lnum * c->leb_size;
98 return c->log_bytes - h + t;
101 else if (c->lhead_lnum != c->ltail_lnum)
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(c, 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, 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 a reference node for the new bud LEB @lnum to the log,
159 * and adds it to the buds trees. 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 code 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, !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);
227 bud->log_hash = NULL;
229 ref->ch.node_type = UBIFS_REF_NODE;
230 ref->lnum = cpu_to_le32(bud->lnum);
231 ref->offs = cpu_to_le32(bud->start);
232 ref->jhead = cpu_to_le32(jhead);
234 if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
235 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
236 ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
240 if (c->lhead_offs == 0) {
241 /* Must ensure next log LEB has been unmapped */
242 err = ubifs_leb_unmap(c, c->lhead_lnum);
247 if (bud->start == 0) {
249 * Before writing the LEB reference which refers an empty LEB
250 * to the log, we have to make sure it is mapped, because
251 * otherwise we'd risk to refer an LEB with garbage in case of
252 * an unclean reboot, because the target LEB might have been
253 * unmapped, but not yet physically erased.
255 err = ubifs_leb_map(c, bud->lnum);
260 dbg_log("write ref LEB %d:%d",
261 c->lhead_lnum, c->lhead_offs);
262 err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
267 err = ubifs_shash_update(c, c->log_hash, ref, UBIFS_REF_NODE_SZ);
271 err = ubifs_shash_copy_state(c, c->log_hash, c->jheads[jhead].log_hash);
275 c->lhead_offs += c->ref_node_alsz;
277 ubifs_add_bud(c, bud);
279 mutex_unlock(&c->log_mutex);
284 mutex_unlock(&c->log_mutex);
291 * remove_buds - remove used buds.
292 * @c: UBIFS file-system description object
294 * This function removes use buds from the buds tree. It does not remove the
295 * buds which are pointed to by journal heads.
297 static void remove_buds(struct ubifs_info *c)
301 ubifs_assert(c, list_empty(&c->old_buds));
302 c->cmt_bud_bytes = 0;
303 spin_lock(&c->buds_lock);
304 p = rb_first(&c->buds);
306 struct rb_node *p1 = p;
307 struct ubifs_bud *bud;
308 struct ubifs_wbuf *wbuf;
311 bud = rb_entry(p1, struct ubifs_bud, rb);
312 wbuf = &c->jheads[bud->jhead].wbuf;
314 if (wbuf->lnum == bud->lnum) {
316 * Do not remove buds which are pointed to by journal
317 * heads (non-closed buds).
319 c->cmt_bud_bytes += wbuf->offs - bud->start;
320 dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
321 bud->lnum, bud->start, dbg_jhead(bud->jhead),
322 wbuf->offs - bud->start, c->cmt_bud_bytes);
323 bud->start = wbuf->offs;
325 c->cmt_bud_bytes += c->leb_size - bud->start;
326 dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
327 bud->lnum, bud->start, dbg_jhead(bud->jhead),
328 c->leb_size - bud->start, c->cmt_bud_bytes);
329 rb_erase(p1, &c->buds);
331 * If the commit does not finish, the recovery will need
332 * to replay the journal, in which case the old buds
333 * must be unchanged. Do not release them until post
334 * commit i.e. do not allow them to be garbage
337 list_move(&bud->list, &c->old_buds);
340 spin_unlock(&c->buds_lock);
344 * ubifs_log_start_commit - start commit.
345 * @c: UBIFS file-system description object
346 * @ltail_lnum: return new log tail LEB number
348 * The commit operation starts with writing "commit start" node to the log and
349 * reference nodes for all journal heads which will define new journal after
350 * the commit has been finished. The commit start and reference nodes are
351 * written in one go to the nearest empty log LEB (hence, when commit is
352 * finished UBIFS may safely unmap all the previous log LEBs). This function
353 * returns zero in case of success and a negative error code in case of
356 int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
359 struct ubifs_cs_node *cs;
360 struct ubifs_ref_node *ref;
361 int err, i, max_len, len;
363 err = dbg_check_bud_bytes(c);
367 max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
368 max_len = ALIGN(max_len, c->min_io_size);
369 buf = cs = kmalloc(max_len, GFP_NOFS);
373 cs->ch.node_type = UBIFS_CS_NODE;
374 cs->cmt_no = cpu_to_le64(c->cmt_no);
375 ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
377 err = ubifs_shash_init(c, c->log_hash);
381 err = ubifs_shash_update(c, c->log_hash, cs, UBIFS_CS_NODE_SZ);
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
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;
397 if (lnum == -1 || offs == c->leb_size)
400 dbg_log("add ref to LEB %d:%d for jhead %s",
401 lnum, offs, dbg_jhead(i));
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);
408 ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
409 len += UBIFS_REF_NODE_SZ;
411 err = ubifs_shash_update(c, c->log_hash, ref,
415 ubifs_shash_copy_state(c, c->log_hash, c->jheads[i].log_hash);
418 ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
420 /* Switch to the next log LEB */
422 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
423 ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
427 /* Must ensure next LEB has been unmapped */
428 err = ubifs_leb_unmap(c, c->lhead_lnum);
432 len = ALIGN(len, c->min_io_size);
433 dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
434 err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
438 *ltail_lnum = c->lhead_lnum;
440 c->lhead_offs += len;
441 ubifs_assert(c, c->lhead_offs < c->leb_size);
446 * We have started the commit and now users may use the rest of the log
449 c->min_log_bytes = 0;
457 * ubifs_log_end_commit - end commit.
458 * @c: UBIFS file-system description object
459 * @ltail_lnum: new log tail LEB number
461 * This function is called on when the commit operation was finished. It
462 * moves log tail to new position and updates the master node so that it stores
463 * the new log tail LEB number. Returns zero in case of success and a negative
464 * error code in case of failure.
466 int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
471 * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
472 * writes during commit. Its only short "commit" start phase when
473 * writers are blocked.
475 mutex_lock(&c->log_mutex);
477 dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
478 c->ltail_lnum, ltail_lnum);
480 c->ltail_lnum = ltail_lnum;
482 * The commit is finished and from now on it must be guaranteed that
483 * there is always enough space for the next commit.
485 c->min_log_bytes = c->leb_size;
487 spin_lock(&c->buds_lock);
488 c->bud_bytes -= c->cmt_bud_bytes;
489 spin_unlock(&c->buds_lock);
491 err = dbg_check_bud_bytes(c);
495 err = ubifs_write_master(c);
498 mutex_unlock(&c->log_mutex);
503 * ubifs_log_post_commit - things to do after commit is completed.
504 * @c: UBIFS file-system description object
505 * @old_ltail_lnum: old log tail LEB number
507 * Release buds only after commit is completed, because they must be unchanged
508 * if recovery is needed.
510 * Unmap log LEBs only after commit is completed, because they may be needed for
513 * This function returns %0 on success and a negative error code on failure.
515 int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
519 while (!list_empty(&c->old_buds)) {
520 struct ubifs_bud *bud;
522 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
523 err = ubifs_return_leb(c, bud->lnum);
526 list_del(&bud->list);
527 kfree(bud->log_hash);
530 mutex_lock(&c->log_mutex);
531 for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
532 lnum = ubifs_next_log_lnum(c, lnum)) {
533 dbg_log("unmap log LEB %d", lnum);
534 err = ubifs_leb_unmap(c, lnum);
539 mutex_unlock(&c->log_mutex);
544 * struct done_ref - references that have been done.
554 * done_already - determine if a reference has been done already.
555 * @done_tree: rb-tree to store references that have been done
556 * @lnum: LEB number of reference
558 * This function returns %1 if the reference has been done, %0 if not, otherwise
559 * a negative error code is returned.
561 static int done_already(struct rb_root *done_tree, int lnum)
563 struct rb_node **p = &done_tree->rb_node, *parent = NULL;
568 dr = rb_entry(parent, struct done_ref, rb);
571 else if (lnum > dr->lnum)
577 dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
583 rb_link_node(&dr->rb, parent, p);
584 rb_insert_color(&dr->rb, done_tree);
590 * destroy_done_tree - destroy the done tree.
591 * @done_tree: done tree to destroy
593 static void destroy_done_tree(struct rb_root *done_tree)
595 struct done_ref *dr, *n;
597 rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
602 * add_node - add a node to the consolidated log.
603 * @c: UBIFS file-system description object
604 * @buf: buffer to which to add
605 * @lnum: LEB number to which to write is passed and returned here
606 * @offs: offset to where to write is passed and returned here
609 * This function returns %0 on success and a negative error code on failure.
611 static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
614 struct ubifs_ch *ch = node;
615 int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
618 int sz = ALIGN(*offs, c->min_io_size), err;
620 ubifs_pad(c, buf + *offs, sz - *offs);
621 err = ubifs_leb_change(c, *lnum, buf, sz);
624 *lnum = ubifs_next_log_lnum(c, *lnum);
627 memcpy(buf + *offs, node, len);
628 *offs += ALIGN(len, 8);
633 * ubifs_consolidate_log - consolidate the log.
634 * @c: UBIFS file-system description object
636 * Repeated failed commits could cause the log to be full, but at least 1 LEB is
637 * needed for commit. This function rewrites the reference nodes in the log
638 * omitting duplicates, and failed CS nodes, and leaving no gaps.
640 * This function returns %0 on success and a negative error code on failure.
642 int ubifs_consolidate_log(struct ubifs_info *c)
644 struct ubifs_scan_leb *sleb;
645 struct ubifs_scan_node *snod;
646 struct rb_root done_tree = RB_ROOT;
647 int lnum, err, first = 1, write_lnum, offs = 0;
650 dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
652 buf = vmalloc(c->leb_size);
655 lnum = c->ltail_lnum;
658 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
663 list_for_each_entry(snod, &sleb->nodes, list) {
664 switch (snod->type) {
665 case UBIFS_REF_NODE: {
666 struct ubifs_ref_node *ref = snod->node;
667 int ref_lnum = le32_to_cpu(ref->lnum);
669 err = done_already(&done_tree, ref_lnum);
673 err = add_node(c, buf, &write_lnum,
683 err = add_node(c, buf, &write_lnum, &offs,
691 ubifs_scan_destroy(sleb);
692 if (lnum == c->lhead_lnum)
694 lnum = ubifs_next_log_lnum(c, lnum);
697 int sz = ALIGN(offs, c->min_io_size);
699 ubifs_pad(c, buf + offs, sz - offs);
700 err = ubifs_leb_change(c, write_lnum, buf, sz);
703 offs = ALIGN(offs, c->min_io_size);
705 destroy_done_tree(&done_tree);
707 if (write_lnum == c->lhead_lnum) {
708 ubifs_err(c, "log is too full");
711 /* Unmap remaining LEBs */
714 lnum = ubifs_next_log_lnum(c, lnum);
715 err = ubifs_leb_unmap(c, lnum);
718 } while (lnum != c->lhead_lnum);
719 c->lhead_lnum = write_lnum;
720 c->lhead_offs = offs;
721 dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
725 ubifs_scan_destroy(sleb);
727 destroy_done_tree(&done_tree);
733 * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
734 * @c: UBIFS file-system description object
736 * This function makes sure the amount of flash space used by closed buds
737 * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
740 static int dbg_check_bud_bytes(struct ubifs_info *c)
743 struct ubifs_bud *bud;
744 long long bud_bytes = 0;
746 if (!dbg_is_chk_gen(c))
749 spin_lock(&c->buds_lock);
750 for (i = 0; i < c->jhead_cnt; i++)
751 list_for_each_entry(bud, &c->jheads[i].buds_list, list)
752 bud_bytes += c->leb_size - bud->start;
754 if (c->bud_bytes != bud_bytes) {
755 ubifs_err(c, "bad bud_bytes %lld, calculated %lld",
756 c->bud_bytes, bud_bytes);
759 spin_unlock(&c->buds_lock);