1 // SPDX-License-Identifier: GPL-2.0+
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 #include <dm/devres.h>
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;
106 else if (c->lhead_lnum != c->ltail_lnum)
113 * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
114 * @c: UBIFS file-system description object
115 * @bud: the bud to add
117 void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
119 struct rb_node **p, *parent = NULL;
121 struct ubifs_jhead *jhead;
123 spin_lock(&c->buds_lock);
124 p = &c->buds.rb_node;
127 b = rb_entry(parent, struct ubifs_bud, rb);
128 ubifs_assert(bud->lnum != b->lnum);
129 if (bud->lnum < b->lnum)
135 rb_link_node(&bud->rb, parent, p);
136 rb_insert_color(&bud->rb, &c->buds);
138 jhead = &c->jheads[bud->jhead];
139 list_add_tail(&bud->list, &jhead->buds_list);
141 ubifs_assert(c->replaying && c->ro_mount);
144 * Note, although this is a new bud, we anyway account this space now,
145 * before any data has been written to it, because this is about to
146 * guarantee fixed mount time, and this bud will anyway be read and
149 c->bud_bytes += c->leb_size - bud->start;
151 dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
152 bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
153 spin_unlock(&c->buds_lock);
157 * ubifs_add_bud_to_log - add a new bud to the log.
158 * @c: UBIFS file-system description object
159 * @jhead: journal head the bud belongs to
160 * @lnum: LEB number of the bud
161 * @offs: starting offset of the bud
163 * This function writes reference node for the new bud LEB @lnum it to the log,
164 * and adds it to the buds tress. It also makes sure that log size does not
165 * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
166 * %-EAGAIN if commit is required, and a negative error codes in case of
169 int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
172 struct ubifs_bud *bud;
173 struct ubifs_ref_node *ref;
175 bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
178 ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
184 mutex_lock(&c->log_mutex);
185 ubifs_assert(!c->ro_media && !c->ro_mount);
191 /* Make sure we have enough space in the log */
192 if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
193 dbg_log("not enough log space - %lld, required %d",
194 empty_log_bytes(c), c->min_log_bytes);
195 ubifs_commit_required(c);
201 * Make sure the amount of space in buds will not exceed the
202 * 'c->max_bud_bytes' limit, because we want to guarantee mount time
205 * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
206 * because we are holding @c->log_mutex. All @c->bud_bytes take place
207 * when both @c->log_mutex and @c->bud_bytes are locked.
209 if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
210 dbg_log("bud bytes %lld (%lld max), require commit",
211 c->bud_bytes, c->max_bud_bytes);
212 ubifs_commit_required(c);
218 * If the journal is full enough - start background commit. Note, it is
219 * OK to read 'c->cmt_state' without spinlock because integer reads
220 * are atomic in the kernel.
222 if (c->bud_bytes >= c->bg_bud_bytes &&
223 c->cmt_state == COMMIT_RESTING) {
224 dbg_log("bud bytes %lld (%lld max), initiate BG commit",
225 c->bud_bytes, c->max_bud_bytes);
226 ubifs_request_bg_commit(c);
233 ref->ch.node_type = UBIFS_REF_NODE;
234 ref->lnum = cpu_to_le32(bud->lnum);
235 ref->offs = cpu_to_le32(bud->start);
236 ref->jhead = cpu_to_le32(jhead);
238 if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
239 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
240 ubifs_assert(c->lhead_lnum != c->ltail_lnum);
244 if (c->lhead_offs == 0) {
245 /* Must ensure next log LEB has been unmapped */
246 err = ubifs_leb_unmap(c, c->lhead_lnum);
251 if (bud->start == 0) {
253 * Before writing the LEB reference which refers an empty LEB
254 * to the log, we have to make sure it is mapped, because
255 * otherwise we'd risk to refer an LEB with garbage in case of
256 * an unclean reboot, because the target LEB might have been
257 * unmapped, but not yet physically erased.
259 err = ubifs_leb_map(c, bud->lnum);
264 dbg_log("write ref LEB %d:%d",
265 c->lhead_lnum, c->lhead_offs);
266 err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
271 c->lhead_offs += c->ref_node_alsz;
273 ubifs_add_bud(c, bud);
275 mutex_unlock(&c->log_mutex);
280 mutex_unlock(&c->log_mutex);
287 * remove_buds - remove used buds.
288 * @c: UBIFS file-system description object
290 * This function removes use buds from the buds tree. It does not remove the
291 * buds which are pointed to by journal heads.
293 static void remove_buds(struct ubifs_info *c)
297 ubifs_assert(list_empty(&c->old_buds));
298 c->cmt_bud_bytes = 0;
299 spin_lock(&c->buds_lock);
300 p = rb_first(&c->buds);
302 struct rb_node *p1 = p;
303 struct ubifs_bud *bud;
304 struct ubifs_wbuf *wbuf;
307 bud = rb_entry(p1, struct ubifs_bud, rb);
308 wbuf = &c->jheads[bud->jhead].wbuf;
310 if (wbuf->lnum == bud->lnum) {
312 * Do not remove buds which are pointed to by journal
313 * heads (non-closed buds).
315 c->cmt_bud_bytes += wbuf->offs - bud->start;
316 dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
317 bud->lnum, bud->start, dbg_jhead(bud->jhead),
318 wbuf->offs - bud->start, c->cmt_bud_bytes);
319 bud->start = wbuf->offs;
321 c->cmt_bud_bytes += c->leb_size - bud->start;
322 dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
323 bud->lnum, bud->start, dbg_jhead(bud->jhead),
324 c->leb_size - bud->start, c->cmt_bud_bytes);
325 rb_erase(p1, &c->buds);
327 * If the commit does not finish, the recovery will need
328 * to replay the journal, in which case the old buds
329 * must be unchanged. Do not release them until post
330 * commit i.e. do not allow them to be garbage
333 list_move(&bud->list, &c->old_buds);
336 spin_unlock(&c->buds_lock);
340 * ubifs_log_start_commit - start commit.
341 * @c: UBIFS file-system description object
342 * @ltail_lnum: return new log tail LEB number
344 * The commit operation starts with writing "commit start" node to the log and
345 * reference nodes for all journal heads which will define new journal after
346 * the commit has been finished. The commit start and reference nodes are
347 * written in one go to the nearest empty log LEB (hence, when commit is
348 * finished UBIFS may safely unmap all the previous log LEBs). This function
349 * returns zero in case of success and a negative error code in case of
352 int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
355 struct ubifs_cs_node *cs;
356 struct ubifs_ref_node *ref;
357 int err, i, max_len, len;
359 err = dbg_check_bud_bytes(c);
363 max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
364 max_len = ALIGN(max_len, c->min_io_size);
365 buf = cs = kmalloc(max_len, GFP_NOFS);
369 cs->ch.node_type = UBIFS_CS_NODE;
370 cs->cmt_no = cpu_to_le64(c->cmt_no);
371 ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
374 * Note, we do not lock 'c->log_mutex' because this is the commit start
375 * phase and we are exclusively using the log. And we do not lock
376 * write-buffer because nobody can write to the file-system at this
380 len = UBIFS_CS_NODE_SZ;
381 for (i = 0; i < c->jhead_cnt; i++) {
382 int lnum = c->jheads[i].wbuf.lnum;
383 int offs = c->jheads[i].wbuf.offs;
385 if (lnum == -1 || offs == c->leb_size)
388 dbg_log("add ref to LEB %d:%d for jhead %s",
389 lnum, offs, dbg_jhead(i));
391 ref->ch.node_type = UBIFS_REF_NODE;
392 ref->lnum = cpu_to_le32(lnum);
393 ref->offs = cpu_to_le32(offs);
394 ref->jhead = cpu_to_le32(i);
396 ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
397 len += UBIFS_REF_NODE_SZ;
400 ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
402 /* Switch to the next log LEB */
404 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
405 ubifs_assert(c->lhead_lnum != c->ltail_lnum);
409 /* Must ensure next LEB has been unmapped */
410 err = ubifs_leb_unmap(c, c->lhead_lnum);
414 len = ALIGN(len, c->min_io_size);
415 dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
416 err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
420 *ltail_lnum = c->lhead_lnum;
422 c->lhead_offs += len;
423 if (c->lhead_offs == c->leb_size) {
424 c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
431 * We have started the commit and now users may use the rest of the log
434 c->min_log_bytes = 0;
442 * ubifs_log_end_commit - end commit.
443 * @c: UBIFS file-system description object
444 * @ltail_lnum: new log tail LEB number
446 * This function is called on when the commit operation was finished. It
447 * moves log tail to new position and updates the master node so that it stores
448 * the new log tail LEB number. Returns zero in case of success and a negative
449 * error code in case of failure.
451 int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
456 * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
457 * writes during commit. Its only short "commit" start phase when
458 * writers are blocked.
460 mutex_lock(&c->log_mutex);
462 dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
463 c->ltail_lnum, ltail_lnum);
465 c->ltail_lnum = ltail_lnum;
467 * The commit is finished and from now on it must be guaranteed that
468 * there is always enough space for the next commit.
470 c->min_log_bytes = c->leb_size;
472 spin_lock(&c->buds_lock);
473 c->bud_bytes -= c->cmt_bud_bytes;
474 spin_unlock(&c->buds_lock);
476 err = dbg_check_bud_bytes(c);
480 err = ubifs_write_master(c);
483 mutex_unlock(&c->log_mutex);
488 * ubifs_log_post_commit - things to do after commit is completed.
489 * @c: UBIFS file-system description object
490 * @old_ltail_lnum: old log tail LEB number
492 * Release buds only after commit is completed, because they must be unchanged
493 * if recovery is needed.
495 * Unmap log LEBs only after commit is completed, because they may be needed for
498 * This function returns %0 on success and a negative error code on failure.
500 int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
504 while (!list_empty(&c->old_buds)) {
505 struct ubifs_bud *bud;
507 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
508 err = ubifs_return_leb(c, bud->lnum);
511 list_del(&bud->list);
514 mutex_lock(&c->log_mutex);
515 for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
516 lnum = ubifs_next_log_lnum(c, lnum)) {
517 dbg_log("unmap log LEB %d", lnum);
518 err = ubifs_leb_unmap(c, lnum);
523 mutex_unlock(&c->log_mutex);
528 * struct done_ref - references that have been done.
538 * done_already - determine if a reference has been done already.
539 * @done_tree: rb-tree to store references that have been done
540 * @lnum: LEB number of reference
542 * This function returns %1 if the reference has been done, %0 if not, otherwise
543 * a negative error code is returned.
545 static int done_already(struct rb_root *done_tree, int lnum)
547 struct rb_node **p = &done_tree->rb_node, *parent = NULL;
552 dr = rb_entry(parent, struct done_ref, rb);
555 else if (lnum > dr->lnum)
561 dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
567 rb_link_node(&dr->rb, parent, p);
568 rb_insert_color(&dr->rb, done_tree);
574 * destroy_done_tree - destroy the done tree.
575 * @done_tree: done tree to destroy
577 static void destroy_done_tree(struct rb_root *done_tree)
579 struct done_ref *dr, *n;
581 rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
586 * add_node - add a node to the consolidated log.
587 * @c: UBIFS file-system description object
588 * @buf: buffer to which to add
589 * @lnum: LEB number to which to write is passed and returned here
590 * @offs: offset to where to write is passed and returned here
593 * This function returns %0 on success and a negative error code on failure.
595 static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
598 struct ubifs_ch *ch = node;
599 int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
602 int sz = ALIGN(*offs, c->min_io_size), err;
604 ubifs_pad(c, buf + *offs, sz - *offs);
605 err = ubifs_leb_change(c, *lnum, buf, sz);
608 *lnum = ubifs_next_log_lnum(c, *lnum);
611 memcpy(buf + *offs, node, len);
612 *offs += ALIGN(len, 8);
617 * ubifs_consolidate_log - consolidate the log.
618 * @c: UBIFS file-system description object
620 * Repeated failed commits could cause the log to be full, but at least 1 LEB is
621 * needed for commit. This function rewrites the reference nodes in the log
622 * omitting duplicates, and failed CS nodes, and leaving no gaps.
624 * This function returns %0 on success and a negative error code on failure.
626 int ubifs_consolidate_log(struct ubifs_info *c)
628 struct ubifs_scan_leb *sleb;
629 struct ubifs_scan_node *snod;
630 struct rb_root done_tree = RB_ROOT;
631 int lnum, err, first = 1, write_lnum, offs = 0;
634 dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
636 buf = vmalloc(c->leb_size);
639 lnum = c->ltail_lnum;
642 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
647 list_for_each_entry(snod, &sleb->nodes, list) {
648 switch (snod->type) {
649 case UBIFS_REF_NODE: {
650 struct ubifs_ref_node *ref = snod->node;
651 int ref_lnum = le32_to_cpu(ref->lnum);
653 err = done_already(&done_tree, ref_lnum);
657 err = add_node(c, buf, &write_lnum,
667 err = add_node(c, buf, &write_lnum, &offs,
675 ubifs_scan_destroy(sleb);
676 if (lnum == c->lhead_lnum)
678 lnum = ubifs_next_log_lnum(c, lnum);
681 int sz = ALIGN(offs, c->min_io_size);
683 ubifs_pad(c, buf + offs, sz - offs);
684 err = ubifs_leb_change(c, write_lnum, buf, sz);
687 offs = ALIGN(offs, c->min_io_size);
689 destroy_done_tree(&done_tree);
691 if (write_lnum == c->lhead_lnum) {
692 ubifs_err(c, "log is too full");
695 /* Unmap remaining LEBs */
698 lnum = ubifs_next_log_lnum(c, lnum);
699 err = ubifs_leb_unmap(c, lnum);
702 } while (lnum != c->lhead_lnum);
703 c->lhead_lnum = write_lnum;
704 c->lhead_offs = offs;
705 dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
709 ubifs_scan_destroy(sleb);
711 destroy_done_tree(&done_tree);
717 * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
718 * @c: UBIFS file-system description object
720 * This function makes sure the amount of flash space used by closed buds
721 * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
724 static int dbg_check_bud_bytes(struct ubifs_info *c)
727 struct ubifs_bud *bud;
728 long long bud_bytes = 0;
730 if (!dbg_is_chk_gen(c))
733 spin_lock(&c->buds_lock);
734 for (i = 0; i < c->jhead_cnt; i++)
735 list_for_each_entry(bud, &c->jheads[i].buds_list, list)
736 bud_bytes += c->leb_size - bud->start;
738 if (c->bud_bytes != bud_bytes) {
739 ubifs_err(c, "bad bud_bytes %lld, calculated %lld",
740 c->bud_bytes, bud_bytes);
743 spin_unlock(&c->buds_lock);