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
21 #include <linux/err.h>
25 static int dbg_check_bud_bytes(struct ubifs_info *c);
28 * ubifs_search_bud - search bud LEB.
29 * @c: UBIFS file-system description object
30 * @lnum: logical eraseblock number to search
32 * This function searches bud LEB @lnum. Returns bud description object in case
33 * of success and %NULL if there is no bud with this LEB number.
35 struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
38 struct ubifs_bud *bud;
40 spin_lock(&c->buds_lock);
43 bud = rb_entry(p, struct ubifs_bud, rb);
46 else if (lnum > bud->lnum)
49 spin_unlock(&c->buds_lock);
53 spin_unlock(&c->buds_lock);
58 * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
59 * @c: UBIFS file-system description object
60 * @lnum: logical eraseblock number to search
62 * This functions returns the wbuf for @lnum or %NULL if there is not one.
64 struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
67 struct ubifs_bud *bud;
73 spin_lock(&c->buds_lock);
76 bud = rb_entry(p, struct ubifs_bud, rb);
79 else if (lnum > bud->lnum)
83 spin_unlock(&c->buds_lock);
84 return &c->jheads[jhead].wbuf;
87 spin_unlock(&c->buds_lock);
92 * empty_log_bytes - calculate amount of empty space in the log.
93 * @c: UBIFS file-system description object
95 static inline long long empty_log_bytes(const struct ubifs_info *c)
99 h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
100 t = (long long)c->ltail_lnum * c->leb_size;
103 return c->log_bytes - h + t;
109 * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
110 * @c: UBIFS file-system description object
111 * @bud: the bud to add
113 void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
115 struct rb_node **p, *parent = NULL;
117 struct ubifs_jhead *jhead;
119 spin_lock(&c->buds_lock);
120 p = &c->buds.rb_node;
123 b = rb_entry(parent, struct ubifs_bud, rb);
124 ubifs_assert(bud->lnum != b->lnum);
125 if (bud->lnum < b->lnum)
131 rb_link_node(&bud->rb, parent, p);
132 rb_insert_color(&bud->rb, &c->buds);
134 jhead = &c->jheads[bud->jhead];
135 list_add_tail(&bud->list, &jhead->buds_list);
137 ubifs_assert(c->replaying && c->ro_mount);
140 * Note, although this is a new bud, we anyway account this space now,
141 * before any data has been written to it, because this is about to
142 * guarantee fixed mount time, and this bud will anyway be read and
145 c->bud_bytes += c->leb_size - bud->start;
147 dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
148 bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
149 spin_unlock(&c->buds_lock);
153 * ubifs_add_bud_to_log - add a new bud to the log.
154 * @c: UBIFS file-system description object
155 * @jhead: journal head the bud belongs to
156 * @lnum: LEB number of the bud
157 * @offs: starting offset of the bud
159 * This function writes reference node for the new bud LEB @lnum it to the log,
160 * and adds it to the buds tress. It also makes sure that log size does not
161 * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
162 * %-EAGAIN if commit is required, and a negative error codes in case of
165 int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
168 struct ubifs_bud *bud;
169 struct ubifs_ref_node *ref;
171 bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
174 ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
180 mutex_lock(&c->log_mutex);
181 ubifs_assert(!c->ro_media && !c->ro_mount);
187 /* Make sure we have enough space in the log */
188 if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
189 dbg_log("not enough log space - %lld, required %d",
190 empty_log_bytes(c), c->min_log_bytes);
191 ubifs_commit_required(c);
197 * Make sure the amount of space in buds will not exceed the
198 * 'c->max_bud_bytes' limit, because we want to guarantee mount time
201 * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
202 * because we are holding @c->log_mutex. All @c->bud_bytes take place
203 * when both @c->log_mutex and @c->bud_bytes are locked.
205 if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
206 dbg_log("bud bytes %lld (%lld max), require commit",
207 c->bud_bytes, c->max_bud_bytes);
208 ubifs_commit_required(c);
214 * If the journal is full enough - start background commit. Note, it is
215 * OK to read 'c->cmt_state' without spinlock because integer reads
216 * are atomic in the kernel.
218 if (c->bud_bytes >= c->bg_bud_bytes &&
219 c->cmt_state == COMMIT_RESTING) {
220 dbg_log("bud bytes %lld (%lld max), initiate BG commit",
221 c->bud_bytes, c->max_bud_bytes);
222 ubifs_request_bg_commit(c);
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);
239 if (c->lhead_offs == 0) {
240 /* Must ensure next log LEB has been unmapped */
241 err = ubifs_leb_unmap(c, c->lhead_lnum);
246 if (bud->start == 0) {
248 * Before writing the LEB reference which refers an empty LEB
249 * to the log, we have to make sure it is mapped, because
250 * otherwise we'd risk to refer an LEB with garbage in case of
251 * an unclean reboot, because the target LEB might have been
252 * unmapped, but not yet physically erased.
254 err = ubifs_leb_map(c, bud->lnum);
259 dbg_log("write ref LEB %d:%d",
260 c->lhead_lnum, c->lhead_offs);
261 err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
266 c->lhead_offs += c->ref_node_alsz;
268 ubifs_add_bud(c, bud);
270 mutex_unlock(&c->log_mutex);
275 mutex_unlock(&c->log_mutex);
282 * remove_buds - remove used buds.
283 * @c: UBIFS file-system description object
285 * This function removes use buds from the buds tree. It does not remove the
286 * buds which are pointed to by journal heads.
288 static void remove_buds(struct ubifs_info *c)
292 ubifs_assert(list_empty(&c->old_buds));
293 c->cmt_bud_bytes = 0;
294 spin_lock(&c->buds_lock);
295 p = rb_first(&c->buds);
297 struct rb_node *p1 = p;
298 struct ubifs_bud *bud;
299 struct ubifs_wbuf *wbuf;
302 bud = rb_entry(p1, struct ubifs_bud, rb);
303 wbuf = &c->jheads[bud->jhead].wbuf;
305 if (wbuf->lnum == bud->lnum) {
307 * Do not remove buds which are pointed to by journal
308 * heads (non-closed buds).
310 c->cmt_bud_bytes += wbuf->offs - bud->start;
311 dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
312 bud->lnum, bud->start, dbg_jhead(bud->jhead),
313 wbuf->offs - bud->start, c->cmt_bud_bytes);
314 bud->start = wbuf->offs;
316 c->cmt_bud_bytes += c->leb_size - bud->start;
317 dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
318 bud->lnum, bud->start, dbg_jhead(bud->jhead),
319 c->leb_size - bud->start, c->cmt_bud_bytes);
320 rb_erase(p1, &c->buds);
322 * If the commit does not finish, the recovery will need
323 * to replay the journal, in which case the old buds
324 * must be unchanged. Do not release them until post
325 * commit i.e. do not allow them to be garbage
328 list_move(&bud->list, &c->old_buds);
331 spin_unlock(&c->buds_lock);
335 * ubifs_log_start_commit - start commit.
336 * @c: UBIFS file-system description object
337 * @ltail_lnum: return new log tail LEB number
339 * The commit operation starts with writing "commit start" node to the log and
340 * reference nodes for all journal heads which will define new journal after
341 * the commit has been finished. The commit start and reference nodes are
342 * written in one go to the nearest empty log LEB (hence, when commit is
343 * finished UBIFS may safely unmap all the previous log LEBs). This function
344 * returns zero in case of success and a negative error code in case of
347 int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
350 struct ubifs_cs_node *cs;
351 struct ubifs_ref_node *ref;
352 int err, i, max_len, len;
354 err = dbg_check_bud_bytes(c);
358 max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
359 max_len = ALIGN(max_len, c->min_io_size);
360 buf = cs = kmalloc(max_len, GFP_NOFS);
364 cs->ch.node_type = UBIFS_CS_NODE;
365 cs->cmt_no = cpu_to_le64(c->cmt_no);
366 ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
369 * Note, we do not lock 'c->log_mutex' because this is the commit start
370 * phase and we are exclusively using the log. And we do not lock
371 * write-buffer because nobody can write to the file-system at this
375 len = UBIFS_CS_NODE_SZ;
376 for (i = 0; i < c->jhead_cnt; i++) {
377 int lnum = c->jheads[i].wbuf.lnum;
378 int offs = c->jheads[i].wbuf.offs;
380 if (lnum == -1 || offs == c->leb_size)
383 dbg_log("add ref to LEB %d:%d for jhead %s",
384 lnum, offs, dbg_jhead(i));
386 ref->ch.node_type = UBIFS_REF_NODE;
387 ref->lnum = cpu_to_le32(lnum);
388 ref->offs = cpu_to_le32(offs);
389 ref->jhead = cpu_to_le32(i);
391 ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
392 len += UBIFS_REF_NODE_SZ;
395 ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
397 /* Switch to the next log LEB */
399 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
403 if (c->lhead_offs == 0) {
404 /* Must ensure next LEB has been unmapped */
405 err = ubifs_leb_unmap(c, c->lhead_lnum);
410 len = ALIGN(len, c->min_io_size);
411 dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
412 err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
416 *ltail_lnum = c->lhead_lnum;
418 c->lhead_offs += len;
419 if (c->lhead_offs == c->leb_size) {
420 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
427 * We have started the commit and now users may use the rest of the log
430 c->min_log_bytes = 0;
438 * ubifs_log_end_commit - end commit.
439 * @c: UBIFS file-system description object
440 * @ltail_lnum: new log tail LEB number
442 * This function is called on when the commit operation was finished. It
443 * moves log tail to new position and unmaps LEBs which contain obsolete data.
444 * Returns zero in case of success and a negative error code in case of
447 int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
452 * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
453 * writes during commit. Its only short "commit" start phase when
454 * writers are blocked.
456 mutex_lock(&c->log_mutex);
458 dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
459 c->ltail_lnum, ltail_lnum);
461 c->ltail_lnum = ltail_lnum;
463 * The commit is finished and from now on it must be guaranteed that
464 * there is always enough space for the next commit.
466 c->min_log_bytes = c->leb_size;
468 spin_lock(&c->buds_lock);
469 c->bud_bytes -= c->cmt_bud_bytes;
470 spin_unlock(&c->buds_lock);
472 err = dbg_check_bud_bytes(c);
474 mutex_unlock(&c->log_mutex);
479 * ubifs_log_post_commit - things to do after commit is completed.
480 * @c: UBIFS file-system description object
481 * @old_ltail_lnum: old log tail LEB number
483 * Release buds only after commit is completed, because they must be unchanged
484 * if recovery is needed.
486 * Unmap log LEBs only after commit is completed, because they may be needed for
489 * This function returns %0 on success and a negative error code on failure.
491 int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
495 while (!list_empty(&c->old_buds)) {
496 struct ubifs_bud *bud;
498 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
499 err = ubifs_return_leb(c, bud->lnum);
502 list_del(&bud->list);
505 mutex_lock(&c->log_mutex);
506 for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
507 lnum = ubifs_next_log_lnum(c, lnum)) {
508 dbg_log("unmap log LEB %d", lnum);
509 err = ubifs_leb_unmap(c, lnum);
514 mutex_unlock(&c->log_mutex);
519 * struct done_ref - references that have been done.
529 * done_already - determine if a reference has been done already.
530 * @done_tree: rb-tree to store references that have been done
531 * @lnum: LEB number of reference
533 * This function returns %1 if the reference has been done, %0 if not, otherwise
534 * a negative error code is returned.
536 static int done_already(struct rb_root *done_tree, int lnum)
538 struct rb_node **p = &done_tree->rb_node, *parent = NULL;
543 dr = rb_entry(parent, struct done_ref, rb);
546 else if (lnum > dr->lnum)
552 dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
558 rb_link_node(&dr->rb, parent, p);
559 rb_insert_color(&dr->rb, done_tree);
565 * destroy_done_tree - destroy the done tree.
566 * @done_tree: done tree to destroy
568 static void destroy_done_tree(struct rb_root *done_tree)
570 struct done_ref *dr, *n;
572 rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
577 * add_node - add a node to the consolidated log.
578 * @c: UBIFS file-system description object
579 * @buf: buffer to which to add
580 * @lnum: LEB number to which to write is passed and returned here
581 * @offs: offset to where to write is passed and returned here
584 * This function returns %0 on success and a negative error code on failure.
586 static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
589 struct ubifs_ch *ch = node;
590 int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
593 int sz = ALIGN(*offs, c->min_io_size), err;
595 ubifs_pad(c, buf + *offs, sz - *offs);
596 err = ubifs_leb_change(c, *lnum, buf, sz);
599 *lnum = ubifs_next_log_lnum(c, *lnum);
602 memcpy(buf + *offs, node, len);
603 *offs += ALIGN(len, 8);
608 * ubifs_consolidate_log - consolidate the log.
609 * @c: UBIFS file-system description object
611 * Repeated failed commits could cause the log to be full, but at least 1 LEB is
612 * needed for commit. This function rewrites the reference nodes in the log
613 * omitting duplicates, and failed CS nodes, and leaving no gaps.
615 * This function returns %0 on success and a negative error code on failure.
617 int ubifs_consolidate_log(struct ubifs_info *c)
619 struct ubifs_scan_leb *sleb;
620 struct ubifs_scan_node *snod;
621 struct rb_root done_tree = RB_ROOT;
622 int lnum, err, first = 1, write_lnum, offs = 0;
625 dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
627 buf = vmalloc(c->leb_size);
630 lnum = c->ltail_lnum;
633 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
638 list_for_each_entry(snod, &sleb->nodes, list) {
639 switch (snod->type) {
640 case UBIFS_REF_NODE: {
641 struct ubifs_ref_node *ref = snod->node;
642 int ref_lnum = le32_to_cpu(ref->lnum);
644 err = done_already(&done_tree, ref_lnum);
648 err = add_node(c, buf, &write_lnum,
658 err = add_node(c, buf, &write_lnum, &offs,
666 ubifs_scan_destroy(sleb);
667 if (lnum == c->lhead_lnum)
669 lnum = ubifs_next_log_lnum(c, lnum);
672 int sz = ALIGN(offs, c->min_io_size);
674 ubifs_pad(c, buf + offs, sz - offs);
675 err = ubifs_leb_change(c, write_lnum, buf, sz);
678 offs = ALIGN(offs, c->min_io_size);
680 destroy_done_tree(&done_tree);
682 if (write_lnum == c->lhead_lnum) {
683 ubifs_err("log is too full");
686 /* Unmap remaining LEBs */
689 lnum = ubifs_next_log_lnum(c, lnum);
690 err = ubifs_leb_unmap(c, lnum);
693 } while (lnum != c->lhead_lnum);
694 c->lhead_lnum = write_lnum;
695 c->lhead_offs = offs;
696 dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
700 ubifs_scan_destroy(sleb);
702 destroy_done_tree(&done_tree);
708 * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
709 * @c: UBIFS file-system description object
711 * This function makes sure the amount of flash space used by closed buds
712 * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
715 static int dbg_check_bud_bytes(struct ubifs_info *c)
718 struct ubifs_bud *bud;
719 long long bud_bytes = 0;
721 if (!dbg_is_chk_gen(c))
724 spin_lock(&c->buds_lock);
725 for (i = 0; i < c->jhead_cnt; i++)
726 list_for_each_entry(bud, &c->jheads[i].buds_list, list)
727 bud_bytes += c->leb_size - bud->start;
729 if (c->bud_bytes != bud_bytes) {
730 ubifs_err("bad bud_bytes %lld, calculated %lld",
731 c->bud_bytes, bud_bytes);
734 spin_unlock(&c->buds_lock);