2008-07-15 00:08:37 +08:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of UBIFS.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2006-2008 Nokia Corporation.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License version 2 as published by
|
|
|
|
|
* the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
|
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*
|
|
|
|
|
* Authors: Artem Bityutskiy (Битюцкий Артём)
|
|
|
|
|
* Adrian Hunter
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This file is a part of UBIFS journal implementation and contains various
|
|
|
|
|
* functions which manipulate the log. The log is a fixed area on the flash
|
|
|
|
|
* which does not contain any data but refers to buds. The log is a part of the
|
|
|
|
|
* journal.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "ubifs.h"
|
|
|
|
|
|
|
|
|
|
static int dbg_check_bud_bytes(struct ubifs_info *c);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_search_bud - search bud LEB.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @lnum: logical eraseblock number to search
|
|
|
|
|
*
|
|
|
|
|
* This function searches bud LEB @lnum. Returns bud description object in case
|
|
|
|
|
* of success and %NULL if there is no bud with this LEB number.
|
|
|
|
|
*/
|
|
|
|
|
struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
|
|
|
|
|
{
|
|
|
|
|
struct rb_node *p;
|
|
|
|
|
struct ubifs_bud *bud;
|
|
|
|
|
|
|
|
|
|
spin_lock(&c->buds_lock);
|
|
|
|
|
p = c->buds.rb_node;
|
|
|
|
|
while (p) {
|
|
|
|
|
bud = rb_entry(p, struct ubifs_bud, rb);
|
|
|
|
|
if (lnum < bud->lnum)
|
|
|
|
|
p = p->rb_left;
|
|
|
|
|
else if (lnum > bud->lnum)
|
|
|
|
|
p = p->rb_right;
|
|
|
|
|
else {
|
|
|
|
|
spin_unlock(&c->buds_lock);
|
|
|
|
|
return bud;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spin_unlock(&c->buds_lock);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @lnum: logical eraseblock number to search
|
|
|
|
|
*
|
|
|
|
|
* This functions returns the wbuf for @lnum or %NULL if there is not one.
|
|
|
|
|
*/
|
|
|
|
|
struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
|
|
|
|
|
{
|
|
|
|
|
struct rb_node *p;
|
|
|
|
|
struct ubifs_bud *bud;
|
|
|
|
|
int jhead;
|
|
|
|
|
|
|
|
|
|
if (!c->jheads)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
spin_lock(&c->buds_lock);
|
|
|
|
|
p = c->buds.rb_node;
|
|
|
|
|
while (p) {
|
|
|
|
|
bud = rb_entry(p, struct ubifs_bud, rb);
|
|
|
|
|
if (lnum < bud->lnum)
|
|
|
|
|
p = p->rb_left;
|
|
|
|
|
else if (lnum > bud->lnum)
|
|
|
|
|
p = p->rb_right;
|
|
|
|
|
else {
|
|
|
|
|
jhead = bud->jhead;
|
|
|
|
|
spin_unlock(&c->buds_lock);
|
|
|
|
|
return &c->jheads[jhead].wbuf;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spin_unlock(&c->buds_lock);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* empty_log_bytes - calculate amount of empty space in the log.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*/
|
|
|
|
|
static inline long long empty_log_bytes(const struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
long long h, t;
|
|
|
|
|
|
|
|
|
|
h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
|
|
|
|
|
t = (long long)c->ltail_lnum * c->leb_size;
|
|
|
|
|
|
2014-07-16 20:22:29 +08:00
|
|
|
|
if (h > t)
|
2008-07-15 00:08:37 +08:00
|
|
|
|
return c->log_bytes - h + t;
|
2014-07-16 20:22:29 +08:00
|
|
|
|
else if (h != t)
|
2008-07-15 00:08:37 +08:00
|
|
|
|
return t - h;
|
2014-07-16 20:22:29 +08:00
|
|
|
|
else if (c->lhead_lnum != c->ltail_lnum)
|
|
|
|
|
return 0;
|
|
|
|
|
else
|
|
|
|
|
return c->log_bytes;
|
2008-07-15 00:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @bud: the bud to add
|
|
|
|
|
*/
|
|
|
|
|
void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
|
|
|
|
|
{
|
|
|
|
|
struct rb_node **p, *parent = NULL;
|
|
|
|
|
struct ubifs_bud *b;
|
|
|
|
|
struct ubifs_jhead *jhead;
|
|
|
|
|
|
|
|
|
|
spin_lock(&c->buds_lock);
|
|
|
|
|
p = &c->buds.rb_node;
|
|
|
|
|
while (*p) {
|
|
|
|
|
parent = *p;
|
|
|
|
|
b = rb_entry(parent, struct ubifs_bud, rb);
|
2018-07-12 19:01:57 +08:00
|
|
|
|
ubifs_assert(c, bud->lnum != b->lnum);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
if (bud->lnum < b->lnum)
|
|
|
|
|
p = &(*p)->rb_left;
|
|
|
|
|
else
|
|
|
|
|
p = &(*p)->rb_right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rb_link_node(&bud->rb, parent, p);
|
|
|
|
|
rb_insert_color(&bud->rb, &c->buds);
|
|
|
|
|
if (c->jheads) {
|
|
|
|
|
jhead = &c->jheads[bud->jhead];
|
|
|
|
|
list_add_tail(&bud->list, &jhead->buds_list);
|
|
|
|
|
} else
|
2018-07-12 19:01:57 +08:00
|
|
|
|
ubifs_assert(c, c->replaying && c->ro_mount);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Note, although this is a new bud, we anyway account this space now,
|
|
|
|
|
* before any data has been written to it, because this is about to
|
|
|
|
|
* guarantee fixed mount time, and this bud will anyway be read and
|
|
|
|
|
* scanned.
|
|
|
|
|
*/
|
|
|
|
|
c->bud_bytes += c->leb_size - bud->start;
|
|
|
|
|
|
2009-09-15 20:03:51 +08:00
|
|
|
|
dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
|
|
|
|
|
bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
spin_unlock(&c->buds_lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_add_bud_to_log - add a new bud to the log.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @jhead: journal head the bud belongs to
|
|
|
|
|
* @lnum: LEB number of the bud
|
|
|
|
|
* @offs: starting offset of the bud
|
|
|
|
|
*
|
2018-05-14 16:18:17 +08:00
|
|
|
|
* This function writes a reference node for the new bud LEB @lnum to the log,
|
|
|
|
|
* and adds it to the buds trees. It also makes sure that log size does not
|
2008-07-15 00:08:37 +08:00
|
|
|
|
* exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
|
2018-05-14 16:18:17 +08:00
|
|
|
|
* %-EAGAIN if commit is required, and a negative error code in case of
|
2008-07-15 00:08:37 +08:00
|
|
|
|
* failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
struct ubifs_bud *bud;
|
|
|
|
|
struct ubifs_ref_node *ref;
|
|
|
|
|
|
|
|
|
|
bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
|
|
|
|
|
if (!bud)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
|
|
|
|
|
if (!ref) {
|
|
|
|
|
kfree(bud);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mutex_lock(&c->log_mutex);
|
2018-07-12 19:01:57 +08:00
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
UBIFS: introduce new flag for RO due to errors
The R/O state may have various reasons:
1. The UBI volume is R/O
2. The FS is mounted R/O
3. The FS switched to R/O mode because of an error
However, in UBIFS we have only one variable which represents cases
1 and 3 - 'c->ro_media'. Indeed, we set this to 1 if we switch to
R/O mode due to an error, and then we test it in many places to
make sure that we stop writing as soon as the error happens.
But this is very unclean. One consequence of this, for example, is
that in 'ubifs_remount_fs()' we use 'c->ro_media' to check whether
we are in R/O mode because on an error, and we print a message
in this case. However, if we are in R/O mode because the media
is R/O, our message is bogus.
This patch introduces new flag - 'c->ro_error' which is set when
we switch to R/O mode because of an error. It also changes all
"if (c->ro_media)" checks to "if (c->ro_error)" checks, because
this is what the checks actually mean. We do not need to check
for 'c->ro_media' because if the UBI volume is in R/O mode, we
do not allow R/W mounting, and now writes can happen. This is
guaranteed by VFS. But it is good to double-check this, so this
patch also adds many "ubifs_assert(!c->ro_media)" checks.
In the 'ubifs_remount_fs()' function this patch makes a bit more
changes - it fixes the error messages as well.
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
2010-09-17 21:44:28 +08:00
|
|
|
|
if (c->ro_error) {
|
2008-07-15 00:08:37 +08:00
|
|
|
|
err = -EROFS;
|
|
|
|
|
goto out_unlock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Make sure we have enough space in the log */
|
|
|
|
|
if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
|
|
|
|
|
dbg_log("not enough log space - %lld, required %d",
|
|
|
|
|
empty_log_bytes(c), c->min_log_bytes);
|
|
|
|
|
ubifs_commit_required(c);
|
|
|
|
|
err = -EAGAIN;
|
|
|
|
|
goto out_unlock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2009-03-21 01:11:12 +08:00
|
|
|
|
* Make sure the amount of space in buds will not exceed the
|
2008-07-15 00:08:37 +08:00
|
|
|
|
* 'c->max_bud_bytes' limit, because we want to guarantee mount time
|
|
|
|
|
* limits.
|
|
|
|
|
*
|
|
|
|
|
* It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
|
|
|
|
|
* because we are holding @c->log_mutex. All @c->bud_bytes take place
|
|
|
|
|
* when both @c->log_mutex and @c->bud_bytes are locked.
|
|
|
|
|
*/
|
|
|
|
|
if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
|
|
|
|
|
dbg_log("bud bytes %lld (%lld max), require commit",
|
|
|
|
|
c->bud_bytes, c->max_bud_bytes);
|
|
|
|
|
ubifs_commit_required(c);
|
|
|
|
|
err = -EAGAIN;
|
|
|
|
|
goto out_unlock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the journal is full enough - start background commit. Note, it is
|
|
|
|
|
* OK to read 'c->cmt_state' without spinlock because integer reads
|
|
|
|
|
* are atomic in the kernel.
|
|
|
|
|
*/
|
|
|
|
|
if (c->bud_bytes >= c->bg_bud_bytes &&
|
|
|
|
|
c->cmt_state == COMMIT_RESTING) {
|
|
|
|
|
dbg_log("bud bytes %lld (%lld max), initiate BG commit",
|
|
|
|
|
c->bud_bytes, c->max_bud_bytes);
|
|
|
|
|
ubifs_request_bg_commit(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bud->lnum = lnum;
|
|
|
|
|
bud->start = offs;
|
|
|
|
|
bud->jhead = jhead;
|
ubifs: Add authentication nodes to journal
Nodes that are written to flash can only be authenticated through the
index after the next commit. When a journal replay is necessary the
nodes are not yet referenced by the index and thus can't be
authenticated.
This patch overcomes this situation by creating a hash over all nodes
beginning from the commit start node over the reference node(s) and
the buds themselves. From
time to time we insert authentication nodes. Authentication nodes
contain a HMAC from the current hash state, so that they can be
used to authenticate a journal replay up to the point where the
authentication node is. The hash is continued afterwards
so that theoretically we would only have to check the HMAC of
the last authentication node we find.
Overall we get this picture:
,,,,,,,,
,......,...........................................
,. CS , hash1.----. hash2.----.
,. | , . |hmac . |hmac
,. v , . v . v
,.REF#0,-> bud -> bud -> bud.-> auth -> bud -> bud.-> auth ...
,..|...,...........................................
, | ,
, | ,,,,,,,,,,,,,,,
. | hash3,----.
, | , |hmac
, v , v
, REF#1 -> bud -> bud,-> auth ...
,,,|,,,,,,,,,,,,,,,,,,
v
REF#2 -> ...
|
V
...
Note how hash3 covers CS, REF#0 and REF#1 so that it is not possible to
exchange or skip any reference nodes. Unlike the picture suggests the
auth nodes themselves are not hashed.
With this it is possible for an offline attacker to cut each journal
head or to drop the last reference node(s), but not to skip any journal
heads or to reorder any operations.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Richard Weinberger <richard@nod.at>
2018-09-07 20:36:36 +08:00
|
|
|
|
bud->log_hash = NULL;
|
2008-07-15 00:08:37 +08:00
|
|
|
|
|
|
|
|
|
ref->ch.node_type = UBIFS_REF_NODE;
|
|
|
|
|
ref->lnum = cpu_to_le32(bud->lnum);
|
|
|
|
|
ref->offs = cpu_to_le32(bud->start);
|
|
|
|
|
ref->jhead = cpu_to_le32(jhead);
|
|
|
|
|
|
|
|
|
|
if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
|
2011-05-06 22:52:32 +08:00
|
|
|
|
c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
|
2018-07-12 19:01:57 +08:00
|
|
|
|
ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
c->lhead_offs = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lhead_offs == 0) {
|
|
|
|
|
/* Must ensure next log LEB has been unmapped */
|
|
|
|
|
err = ubifs_leb_unmap(c, c->lhead_lnum);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out_unlock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bud->start == 0) {
|
|
|
|
|
/*
|
|
|
|
|
* Before writing the LEB reference which refers an empty LEB
|
|
|
|
|
* to the log, we have to make sure it is mapped, because
|
|
|
|
|
* otherwise we'd risk to refer an LEB with garbage in case of
|
|
|
|
|
* an unclean reboot, because the target LEB might have been
|
|
|
|
|
* unmapped, but not yet physically erased.
|
|
|
|
|
*/
|
2012-05-14 23:55:51 +08:00
|
|
|
|
err = ubifs_leb_map(c, bud->lnum);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
if (err)
|
|
|
|
|
goto out_unlock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbg_log("write ref LEB %d:%d",
|
|
|
|
|
c->lhead_lnum, c->lhead_offs);
|
|
|
|
|
err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
|
2012-05-14 23:55:51 +08:00
|
|
|
|
c->lhead_offs);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
if (err)
|
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
ubifs: Add authentication nodes to journal
Nodes that are written to flash can only be authenticated through the
index after the next commit. When a journal replay is necessary the
nodes are not yet referenced by the index and thus can't be
authenticated.
This patch overcomes this situation by creating a hash over all nodes
beginning from the commit start node over the reference node(s) and
the buds themselves. From
time to time we insert authentication nodes. Authentication nodes
contain a HMAC from the current hash state, so that they can be
used to authenticate a journal replay up to the point where the
authentication node is. The hash is continued afterwards
so that theoretically we would only have to check the HMAC of
the last authentication node we find.
Overall we get this picture:
,,,,,,,,
,......,...........................................
,. CS , hash1.----. hash2.----.
,. | , . |hmac . |hmac
,. v , . v . v
,.REF#0,-> bud -> bud -> bud.-> auth -> bud -> bud.-> auth ...
,..|...,...........................................
, | ,
, | ,,,,,,,,,,,,,,,
. | hash3,----.
, | , |hmac
, v , v
, REF#1 -> bud -> bud,-> auth ...
,,,|,,,,,,,,,,,,,,,,,,
v
REF#2 -> ...
|
V
...
Note how hash3 covers CS, REF#0 and REF#1 so that it is not possible to
exchange or skip any reference nodes. Unlike the picture suggests the
auth nodes themselves are not hashed.
With this it is possible for an offline attacker to cut each journal
head or to drop the last reference node(s), but not to skip any journal
heads or to reorder any operations.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Richard Weinberger <richard@nod.at>
2018-09-07 20:36:36 +08:00
|
|
|
|
err = ubifs_shash_update(c, c->log_hash, ref, UBIFS_REF_NODE_SZ);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
|
|
err = ubifs_shash_copy_state(c, c->log_hash, c->jheads[jhead].log_hash);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
2008-07-15 00:08:37 +08:00
|
|
|
|
c->lhead_offs += c->ref_node_alsz;
|
|
|
|
|
|
|
|
|
|
ubifs_add_bud(c, bud);
|
|
|
|
|
|
|
|
|
|
mutex_unlock(&c->log_mutex);
|
|
|
|
|
kfree(ref);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
|
mutex_unlock(&c->log_mutex);
|
|
|
|
|
kfree(ref);
|
|
|
|
|
kfree(bud);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* remove_buds - remove used buds.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*
|
|
|
|
|
* This function removes use buds from the buds tree. It does not remove the
|
|
|
|
|
* buds which are pointed to by journal heads.
|
|
|
|
|
*/
|
|
|
|
|
static void remove_buds(struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
struct rb_node *p;
|
|
|
|
|
|
2018-07-12 19:01:57 +08:00
|
|
|
|
ubifs_assert(c, list_empty(&c->old_buds));
|
2008-07-15 00:08:37 +08:00
|
|
|
|
c->cmt_bud_bytes = 0;
|
|
|
|
|
spin_lock(&c->buds_lock);
|
|
|
|
|
p = rb_first(&c->buds);
|
|
|
|
|
while (p) {
|
|
|
|
|
struct rb_node *p1 = p;
|
|
|
|
|
struct ubifs_bud *bud;
|
|
|
|
|
struct ubifs_wbuf *wbuf;
|
|
|
|
|
|
|
|
|
|
p = rb_next(p);
|
|
|
|
|
bud = rb_entry(p1, struct ubifs_bud, rb);
|
|
|
|
|
wbuf = &c->jheads[bud->jhead].wbuf;
|
|
|
|
|
|
|
|
|
|
if (wbuf->lnum == bud->lnum) {
|
|
|
|
|
/*
|
|
|
|
|
* Do not remove buds which are pointed to by journal
|
|
|
|
|
* heads (non-closed buds).
|
|
|
|
|
*/
|
|
|
|
|
c->cmt_bud_bytes += wbuf->offs - bud->start;
|
2012-08-27 18:34:09 +08:00
|
|
|
|
dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
|
|
|
|
|
bud->lnum, bud->start, dbg_jhead(bud->jhead),
|
|
|
|
|
wbuf->offs - bud->start, c->cmt_bud_bytes);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
bud->start = wbuf->offs;
|
|
|
|
|
} else {
|
|
|
|
|
c->cmt_bud_bytes += c->leb_size - bud->start;
|
2012-08-27 18:34:09 +08:00
|
|
|
|
dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
|
|
|
|
|
bud->lnum, bud->start, dbg_jhead(bud->jhead),
|
|
|
|
|
c->leb_size - bud->start, c->cmt_bud_bytes);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
rb_erase(p1, &c->buds);
|
|
|
|
|
/*
|
|
|
|
|
* If the commit does not finish, the recovery will need
|
|
|
|
|
* to replay the journal, in which case the old buds
|
|
|
|
|
* must be unchanged. Do not release them until post
|
|
|
|
|
* commit i.e. do not allow them to be garbage
|
|
|
|
|
* collected.
|
|
|
|
|
*/
|
2009-02-13 16:13:11 +08:00
|
|
|
|
list_move(&bud->list, &c->old_buds);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spin_unlock(&c->buds_lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_log_start_commit - start commit.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @ltail_lnum: return new log tail LEB number
|
|
|
|
|
*
|
|
|
|
|
* The commit operation starts with writing "commit start" node to the log and
|
|
|
|
|
* reference nodes for all journal heads which will define new journal after
|
|
|
|
|
* the commit has been finished. The commit start and reference nodes are
|
|
|
|
|
* written in one go to the nearest empty log LEB (hence, when commit is
|
|
|
|
|
* finished UBIFS may safely unmap all the previous log LEBs). This function
|
|
|
|
|
* returns zero in case of success and a negative error code in case of
|
|
|
|
|
* failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
|
|
|
|
|
{
|
|
|
|
|
void *buf;
|
|
|
|
|
struct ubifs_cs_node *cs;
|
|
|
|
|
struct ubifs_ref_node *ref;
|
|
|
|
|
int err, i, max_len, len;
|
|
|
|
|
|
|
|
|
|
err = dbg_check_bud_bytes(c);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
|
|
|
|
|
max_len = ALIGN(max_len, c->min_io_size);
|
|
|
|
|
buf = cs = kmalloc(max_len, GFP_NOFS);
|
|
|
|
|
if (!buf)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
cs->ch.node_type = UBIFS_CS_NODE;
|
2008-07-21 22:14:29 +08:00
|
|
|
|
cs->cmt_no = cpu_to_le64(c->cmt_no);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
|
|
|
|
|
|
ubifs: Add authentication nodes to journal
Nodes that are written to flash can only be authenticated through the
index after the next commit. When a journal replay is necessary the
nodes are not yet referenced by the index and thus can't be
authenticated.
This patch overcomes this situation by creating a hash over all nodes
beginning from the commit start node over the reference node(s) and
the buds themselves. From
time to time we insert authentication nodes. Authentication nodes
contain a HMAC from the current hash state, so that they can be
used to authenticate a journal replay up to the point where the
authentication node is. The hash is continued afterwards
so that theoretically we would only have to check the HMAC of
the last authentication node we find.
Overall we get this picture:
,,,,,,,,
,......,...........................................
,. CS , hash1.----. hash2.----.
,. | , . |hmac . |hmac
,. v , . v . v
,.REF#0,-> bud -> bud -> bud.-> auth -> bud -> bud.-> auth ...
,..|...,...........................................
, | ,
, | ,,,,,,,,,,,,,,,
. | hash3,----.
, | , |hmac
, v , v
, REF#1 -> bud -> bud,-> auth ...
,,,|,,,,,,,,,,,,,,,,,,
v
REF#2 -> ...
|
V
...
Note how hash3 covers CS, REF#0 and REF#1 so that it is not possible to
exchange or skip any reference nodes. Unlike the picture suggests the
auth nodes themselves are not hashed.
With this it is possible for an offline attacker to cut each journal
head or to drop the last reference node(s), but not to skip any journal
heads or to reorder any operations.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Richard Weinberger <richard@nod.at>
2018-09-07 20:36:36 +08:00
|
|
|
|
err = ubifs_shash_init(c, c->log_hash);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
err = ubifs_shash_update(c, c->log_hash, cs, UBIFS_CS_NODE_SZ);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2008-07-15 00:08:37 +08:00
|
|
|
|
/*
|
|
|
|
|
* Note, we do not lock 'c->log_mutex' because this is the commit start
|
|
|
|
|
* phase and we are exclusively using the log. And we do not lock
|
|
|
|
|
* write-buffer because nobody can write to the file-system at this
|
|
|
|
|
* phase.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
len = UBIFS_CS_NODE_SZ;
|
|
|
|
|
for (i = 0; i < c->jhead_cnt; i++) {
|
|
|
|
|
int lnum = c->jheads[i].wbuf.lnum;
|
|
|
|
|
int offs = c->jheads[i].wbuf.offs;
|
|
|
|
|
|
|
|
|
|
if (lnum == -1 || offs == c->leb_size)
|
|
|
|
|
continue;
|
|
|
|
|
|
2009-09-15 20:03:51 +08:00
|
|
|
|
dbg_log("add ref to LEB %d:%d for jhead %s",
|
|
|
|
|
lnum, offs, dbg_jhead(i));
|
2008-07-15 00:08:37 +08:00
|
|
|
|
ref = buf + len;
|
|
|
|
|
ref->ch.node_type = UBIFS_REF_NODE;
|
|
|
|
|
ref->lnum = cpu_to_le32(lnum);
|
|
|
|
|
ref->offs = cpu_to_le32(offs);
|
|
|
|
|
ref->jhead = cpu_to_le32(i);
|
|
|
|
|
|
|
|
|
|
ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
|
|
|
|
|
len += UBIFS_REF_NODE_SZ;
|
ubifs: Add authentication nodes to journal
Nodes that are written to flash can only be authenticated through the
index after the next commit. When a journal replay is necessary the
nodes are not yet referenced by the index and thus can't be
authenticated.
This patch overcomes this situation by creating a hash over all nodes
beginning from the commit start node over the reference node(s) and
the buds themselves. From
time to time we insert authentication nodes. Authentication nodes
contain a HMAC from the current hash state, so that they can be
used to authenticate a journal replay up to the point where the
authentication node is. The hash is continued afterwards
so that theoretically we would only have to check the HMAC of
the last authentication node we find.
Overall we get this picture:
,,,,,,,,
,......,...........................................
,. CS , hash1.----. hash2.----.
,. | , . |hmac . |hmac
,. v , . v . v
,.REF#0,-> bud -> bud -> bud.-> auth -> bud -> bud.-> auth ...
,..|...,...........................................
, | ,
, | ,,,,,,,,,,,,,,,
. | hash3,----.
, | , |hmac
, v , v
, REF#1 -> bud -> bud,-> auth ...
,,,|,,,,,,,,,,,,,,,,,,
v
REF#2 -> ...
|
V
...
Note how hash3 covers CS, REF#0 and REF#1 so that it is not possible to
exchange or skip any reference nodes. Unlike the picture suggests the
auth nodes themselves are not hashed.
With this it is possible for an offline attacker to cut each journal
head or to drop the last reference node(s), but not to skip any journal
heads or to reorder any operations.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Richard Weinberger <richard@nod.at>
2018-09-07 20:36:36 +08:00
|
|
|
|
|
|
|
|
|
err = ubifs_shash_update(c, c->log_hash, ref,
|
|
|
|
|
UBIFS_REF_NODE_SZ);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
ubifs_shash_copy_state(c, c->log_hash, c->jheads[i].log_hash);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
|
|
|
|
|
|
|
|
|
|
/* Switch to the next log LEB */
|
|
|
|
|
if (c->lhead_offs) {
|
2011-05-06 22:52:32 +08:00
|
|
|
|
c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
|
2018-07-12 19:01:57 +08:00
|
|
|
|
ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
c->lhead_offs = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-29 22:22:16 +08:00
|
|
|
|
/* Must ensure next LEB has been unmapped */
|
|
|
|
|
err = ubifs_leb_unmap(c, c->lhead_lnum);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
2008-07-15 00:08:37 +08:00
|
|
|
|
|
|
|
|
|
len = ALIGN(len, c->min_io_size);
|
|
|
|
|
dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
|
2012-05-14 23:55:51 +08:00
|
|
|
|
err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
*ltail_lnum = c->lhead_lnum;
|
|
|
|
|
|
|
|
|
|
c->lhead_offs += len;
|
|
|
|
|
if (c->lhead_offs == c->leb_size) {
|
2011-05-06 22:52:32 +08:00
|
|
|
|
c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
c->lhead_offs = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remove_buds(c);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We have started the commit and now users may use the rest of the log
|
|
|
|
|
* for new writes.
|
|
|
|
|
*/
|
|
|
|
|
c->min_log_bytes = 0;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
kfree(buf);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_log_end_commit - end commit.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @ltail_lnum: new log tail LEB number
|
|
|
|
|
*
|
|
|
|
|
* This function is called on when the commit operation was finished. It
|
UBIFS: fix a race condition
Hu (hujianyang@huawei.com) discovered a race condition which may lead to a
situation when UBIFS is unable to mount the file-system after an unclean
reboot. The problem is theoretical, though.
In UBIFS, we have the log, which basically a set of LEBs in a certain area. The
log has the tail and the head.
Every time user writes data to the file-system, the UBIFS journal grows, and
the log grows as well, because we append new reference nodes to the head of the
log. So the head moves forward all the time, while the log tail stays at the
same position.
At any time, the UBIFS master node points to the tail of the log. When we mount
the file-system, we scan the log, and we always start from its tail, because
this is where the master node points to. The only occasion when the tail of the
log changes is the commit operation.
The commit operation has 2 phases - "commit start" and "commit end". The former
is relatively short, and does not involve much I/O. During this phase we mostly
just build various in-memory lists of the things which have to be written to
the flash media during "commit end" phase.
During the commit start phase, what we do is we "clean" the log. Indeed, the
commit operation will index all the data in the journal, so the entire journal
"disappears", and therefore the data in the log become unneeded. So we just
move the head of the log to the next LEB, and write the CS node there. This LEB
will be the tail of the new log when the commit operation finishes.
When the "commit start" phase finishes, users may write more data to the
file-system, in parallel with the ongoing "commit end" operation. At this point
the log tail was not changed yet, it is the same as it had been before we
started the commit. The log head keeps moving forward, though.
The commit operation now needs to write the new master node, and the new master
node should point to the new log tail. After this the LEBs between the old log
tail and the new log tail can be unmapped and re-used again.
And here is the possible problem. We do 2 operations: (a) We first update the
log tail position in memory (see 'ubifs_log_end_commit()'). (b) And then we
write the master node (see the big lock of code in 'do_commit()').
But nothing prevents the log head from moving forward between (a) and (b), and
the log head may "wrap" now to the old log tail. And when the "wrap" happens,
the contends of the log tail gets erased. Now a power cut happens and we are in
trouble. We end up with the old master node pointing to the old tail, which was
erased. And replay fails because it expects the master node to point to the
correct log tail at all times.
This patch merges the abovementioned (a) and (b) operations by moving the master
node change code to the 'ubifs_log_end_commit()' function, so that it runs with
the log mutex locked, which will prevent the log from being changed benween
operations (a) and (b).
Cc: stable@vger.kernel.org # 07e19df UBIFS: remove mst_mutex
Cc: stable@vger.kernel.org
Reported-by: hujianyang <hujianyang@huawei.com>
Tested-by: hujianyang <hujianyang@huawei.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
2014-06-29 22:00:45 +08:00
|
|
|
|
* moves log tail to new position and updates the master node so that it stores
|
|
|
|
|
* the new log tail LEB number. Returns zero in case of success and a negative
|
|
|
|
|
* error code in case of failure.
|
2008-07-15 00:08:37 +08:00
|
|
|
|
*/
|
|
|
|
|
int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
|
|
|
|
|
* writes during commit. Its only short "commit" start phase when
|
|
|
|
|
* writers are blocked.
|
|
|
|
|
*/
|
|
|
|
|
mutex_lock(&c->log_mutex);
|
|
|
|
|
|
|
|
|
|
dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
|
|
|
|
|
c->ltail_lnum, ltail_lnum);
|
|
|
|
|
|
|
|
|
|
c->ltail_lnum = ltail_lnum;
|
|
|
|
|
/*
|
|
|
|
|
* The commit is finished and from now on it must be guaranteed that
|
|
|
|
|
* there is always enough space for the next commit.
|
|
|
|
|
*/
|
|
|
|
|
c->min_log_bytes = c->leb_size;
|
|
|
|
|
|
|
|
|
|
spin_lock(&c->buds_lock);
|
|
|
|
|
c->bud_bytes -= c->cmt_bud_bytes;
|
|
|
|
|
spin_unlock(&c->buds_lock);
|
|
|
|
|
|
|
|
|
|
err = dbg_check_bud_bytes(c);
|
UBIFS: fix a race condition
Hu (hujianyang@huawei.com) discovered a race condition which may lead to a
situation when UBIFS is unable to mount the file-system after an unclean
reboot. The problem is theoretical, though.
In UBIFS, we have the log, which basically a set of LEBs in a certain area. The
log has the tail and the head.
Every time user writes data to the file-system, the UBIFS journal grows, and
the log grows as well, because we append new reference nodes to the head of the
log. So the head moves forward all the time, while the log tail stays at the
same position.
At any time, the UBIFS master node points to the tail of the log. When we mount
the file-system, we scan the log, and we always start from its tail, because
this is where the master node points to. The only occasion when the tail of the
log changes is the commit operation.
The commit operation has 2 phases - "commit start" and "commit end". The former
is relatively short, and does not involve much I/O. During this phase we mostly
just build various in-memory lists of the things which have to be written to
the flash media during "commit end" phase.
During the commit start phase, what we do is we "clean" the log. Indeed, the
commit operation will index all the data in the journal, so the entire journal
"disappears", and therefore the data in the log become unneeded. So we just
move the head of the log to the next LEB, and write the CS node there. This LEB
will be the tail of the new log when the commit operation finishes.
When the "commit start" phase finishes, users may write more data to the
file-system, in parallel with the ongoing "commit end" operation. At this point
the log tail was not changed yet, it is the same as it had been before we
started the commit. The log head keeps moving forward, though.
The commit operation now needs to write the new master node, and the new master
node should point to the new log tail. After this the LEBs between the old log
tail and the new log tail can be unmapped and re-used again.
And here is the possible problem. We do 2 operations: (a) We first update the
log tail position in memory (see 'ubifs_log_end_commit()'). (b) And then we
write the master node (see the big lock of code in 'do_commit()').
But nothing prevents the log head from moving forward between (a) and (b), and
the log head may "wrap" now to the old log tail. And when the "wrap" happens,
the contends of the log tail gets erased. Now a power cut happens and we are in
trouble. We end up with the old master node pointing to the old tail, which was
erased. And replay fails because it expects the master node to point to the
correct log tail at all times.
This patch merges the abovementioned (a) and (b) operations by moving the master
node change code to the 'ubifs_log_end_commit()' function, so that it runs with
the log mutex locked, which will prevent the log from being changed benween
operations (a) and (b).
Cc: stable@vger.kernel.org # 07e19df UBIFS: remove mst_mutex
Cc: stable@vger.kernel.org
Reported-by: hujianyang <hujianyang@huawei.com>
Tested-by: hujianyang <hujianyang@huawei.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
2014-06-29 22:00:45 +08:00
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
2008-07-15 00:08:37 +08:00
|
|
|
|
|
UBIFS: fix a race condition
Hu (hujianyang@huawei.com) discovered a race condition which may lead to a
situation when UBIFS is unable to mount the file-system after an unclean
reboot. The problem is theoretical, though.
In UBIFS, we have the log, which basically a set of LEBs in a certain area. The
log has the tail and the head.
Every time user writes data to the file-system, the UBIFS journal grows, and
the log grows as well, because we append new reference nodes to the head of the
log. So the head moves forward all the time, while the log tail stays at the
same position.
At any time, the UBIFS master node points to the tail of the log. When we mount
the file-system, we scan the log, and we always start from its tail, because
this is where the master node points to. The only occasion when the tail of the
log changes is the commit operation.
The commit operation has 2 phases - "commit start" and "commit end". The former
is relatively short, and does not involve much I/O. During this phase we mostly
just build various in-memory lists of the things which have to be written to
the flash media during "commit end" phase.
During the commit start phase, what we do is we "clean" the log. Indeed, the
commit operation will index all the data in the journal, so the entire journal
"disappears", and therefore the data in the log become unneeded. So we just
move the head of the log to the next LEB, and write the CS node there. This LEB
will be the tail of the new log when the commit operation finishes.
When the "commit start" phase finishes, users may write more data to the
file-system, in parallel with the ongoing "commit end" operation. At this point
the log tail was not changed yet, it is the same as it had been before we
started the commit. The log head keeps moving forward, though.
The commit operation now needs to write the new master node, and the new master
node should point to the new log tail. After this the LEBs between the old log
tail and the new log tail can be unmapped and re-used again.
And here is the possible problem. We do 2 operations: (a) We first update the
log tail position in memory (see 'ubifs_log_end_commit()'). (b) And then we
write the master node (see the big lock of code in 'do_commit()').
But nothing prevents the log head from moving forward between (a) and (b), and
the log head may "wrap" now to the old log tail. And when the "wrap" happens,
the contends of the log tail gets erased. Now a power cut happens and we are in
trouble. We end up with the old master node pointing to the old tail, which was
erased. And replay fails because it expects the master node to point to the
correct log tail at all times.
This patch merges the abovementioned (a) and (b) operations by moving the master
node change code to the 'ubifs_log_end_commit()' function, so that it runs with
the log mutex locked, which will prevent the log from being changed benween
operations (a) and (b).
Cc: stable@vger.kernel.org # 07e19df UBIFS: remove mst_mutex
Cc: stable@vger.kernel.org
Reported-by: hujianyang <hujianyang@huawei.com>
Tested-by: hujianyang <hujianyang@huawei.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
2014-06-29 22:00:45 +08:00
|
|
|
|
err = ubifs_write_master(c);
|
|
|
|
|
|
|
|
|
|
out:
|
2008-07-15 00:08:37 +08:00
|
|
|
|
mutex_unlock(&c->log_mutex);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_log_post_commit - things to do after commit is completed.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @old_ltail_lnum: old log tail LEB number
|
|
|
|
|
*
|
|
|
|
|
* Release buds only after commit is completed, because they must be unchanged
|
|
|
|
|
* if recovery is needed.
|
|
|
|
|
*
|
|
|
|
|
* Unmap log LEBs only after commit is completed, because they may be needed for
|
|
|
|
|
* recovery.
|
|
|
|
|
*
|
|
|
|
|
* This function returns %0 on success and a negative error code on failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
|
|
|
|
|
{
|
|
|
|
|
int lnum, err = 0;
|
|
|
|
|
|
|
|
|
|
while (!list_empty(&c->old_buds)) {
|
|
|
|
|
struct ubifs_bud *bud;
|
|
|
|
|
|
|
|
|
|
bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
|
|
|
|
|
err = ubifs_return_leb(c, bud->lnum);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
list_del(&bud->list);
|
ubifs: Add authentication nodes to journal
Nodes that are written to flash can only be authenticated through the
index after the next commit. When a journal replay is necessary the
nodes are not yet referenced by the index and thus can't be
authenticated.
This patch overcomes this situation by creating a hash over all nodes
beginning from the commit start node over the reference node(s) and
the buds themselves. From
time to time we insert authentication nodes. Authentication nodes
contain a HMAC from the current hash state, so that they can be
used to authenticate a journal replay up to the point where the
authentication node is. The hash is continued afterwards
so that theoretically we would only have to check the HMAC of
the last authentication node we find.
Overall we get this picture:
,,,,,,,,
,......,...........................................
,. CS , hash1.----. hash2.----.
,. | , . |hmac . |hmac
,. v , . v . v
,.REF#0,-> bud -> bud -> bud.-> auth -> bud -> bud.-> auth ...
,..|...,...........................................
, | ,
, | ,,,,,,,,,,,,,,,
. | hash3,----.
, | , |hmac
, v , v
, REF#1 -> bud -> bud,-> auth ...
,,,|,,,,,,,,,,,,,,,,,,
v
REF#2 -> ...
|
V
...
Note how hash3 covers CS, REF#0 and REF#1 so that it is not possible to
exchange or skip any reference nodes. Unlike the picture suggests the
auth nodes themselves are not hashed.
With this it is possible for an offline attacker to cut each journal
head or to drop the last reference node(s), but not to skip any journal
heads or to reorder any operations.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Richard Weinberger <richard@nod.at>
2018-09-07 20:36:36 +08:00
|
|
|
|
kfree(bud->log_hash);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
kfree(bud);
|
|
|
|
|
}
|
|
|
|
|
mutex_lock(&c->log_mutex);
|
|
|
|
|
for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
|
2011-05-06 22:52:32 +08:00
|
|
|
|
lnum = ubifs_next_log_lnum(c, lnum)) {
|
2008-07-15 00:08:37 +08:00
|
|
|
|
dbg_log("unmap log LEB %d", lnum);
|
|
|
|
|
err = ubifs_leb_unmap(c, lnum);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
out:
|
|
|
|
|
mutex_unlock(&c->log_mutex);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* struct done_ref - references that have been done.
|
|
|
|
|
* @rb: rb-tree node
|
|
|
|
|
* @lnum: LEB number
|
|
|
|
|
*/
|
|
|
|
|
struct done_ref {
|
|
|
|
|
struct rb_node rb;
|
|
|
|
|
int lnum;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* done_already - determine if a reference has been done already.
|
|
|
|
|
* @done_tree: rb-tree to store references that have been done
|
|
|
|
|
* @lnum: LEB number of reference
|
|
|
|
|
*
|
|
|
|
|
* This function returns %1 if the reference has been done, %0 if not, otherwise
|
|
|
|
|
* a negative error code is returned.
|
|
|
|
|
*/
|
|
|
|
|
static int done_already(struct rb_root *done_tree, int lnum)
|
|
|
|
|
{
|
|
|
|
|
struct rb_node **p = &done_tree->rb_node, *parent = NULL;
|
|
|
|
|
struct done_ref *dr;
|
|
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
|
parent = *p;
|
|
|
|
|
dr = rb_entry(parent, struct done_ref, rb);
|
|
|
|
|
if (lnum < dr->lnum)
|
|
|
|
|
p = &(*p)->rb_left;
|
|
|
|
|
else if (lnum > dr->lnum)
|
|
|
|
|
p = &(*p)->rb_right;
|
|
|
|
|
else
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
|
|
|
|
|
if (!dr)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
dr->lnum = lnum;
|
|
|
|
|
|
|
|
|
|
rb_link_node(&dr->rb, parent, p);
|
|
|
|
|
rb_insert_color(&dr->rb, done_tree);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* destroy_done_tree - destroy the done tree.
|
|
|
|
|
* @done_tree: done tree to destroy
|
|
|
|
|
*/
|
|
|
|
|
static void destroy_done_tree(struct rb_root *done_tree)
|
|
|
|
|
{
|
2014-01-24 07:56:08 +08:00
|
|
|
|
struct done_ref *dr, *n;
|
2008-07-15 00:08:37 +08:00
|
|
|
|
|
2014-01-24 07:56:08 +08:00
|
|
|
|
rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
|
2008-07-15 00:08:37 +08:00
|
|
|
|
kfree(dr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* add_node - add a node to the consolidated log.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @buf: buffer to which to add
|
|
|
|
|
* @lnum: LEB number to which to write is passed and returned here
|
|
|
|
|
* @offs: offset to where to write is passed and returned here
|
|
|
|
|
* @node: node to add
|
|
|
|
|
*
|
|
|
|
|
* This function returns %0 on success and a negative error code on failure.
|
|
|
|
|
*/
|
|
|
|
|
static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
|
|
|
|
|
void *node)
|
|
|
|
|
{
|
|
|
|
|
struct ubifs_ch *ch = node;
|
|
|
|
|
int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
|
|
|
|
|
|
|
|
|
|
if (len > remains) {
|
|
|
|
|
int sz = ALIGN(*offs, c->min_io_size), err;
|
|
|
|
|
|
|
|
|
|
ubifs_pad(c, buf + *offs, sz - *offs);
|
2012-05-14 23:55:51 +08:00
|
|
|
|
err = ubifs_leb_change(c, *lnum, buf, sz);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
2011-05-06 22:52:32 +08:00
|
|
|
|
*lnum = ubifs_next_log_lnum(c, *lnum);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
*offs = 0;
|
|
|
|
|
}
|
|
|
|
|
memcpy(buf + *offs, node, len);
|
|
|
|
|
*offs += ALIGN(len, 8);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_consolidate_log - consolidate the log.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*
|
|
|
|
|
* Repeated failed commits could cause the log to be full, but at least 1 LEB is
|
|
|
|
|
* needed for commit. This function rewrites the reference nodes in the log
|
|
|
|
|
* omitting duplicates, and failed CS nodes, and leaving no gaps.
|
|
|
|
|
*
|
|
|
|
|
* This function returns %0 on success and a negative error code on failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_consolidate_log(struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
struct ubifs_scan_leb *sleb;
|
|
|
|
|
struct ubifs_scan_node *snod;
|
|
|
|
|
struct rb_root done_tree = RB_ROOT;
|
|
|
|
|
int lnum, err, first = 1, write_lnum, offs = 0;
|
|
|
|
|
void *buf;
|
|
|
|
|
|
|
|
|
|
dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
|
|
|
|
|
c->lhead_lnum);
|
|
|
|
|
buf = vmalloc(c->leb_size);
|
|
|
|
|
if (!buf)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
lnum = c->ltail_lnum;
|
|
|
|
|
write_lnum = lnum;
|
|
|
|
|
while (1) {
|
2009-08-25 20:00:55 +08:00
|
|
|
|
sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
if (IS_ERR(sleb)) {
|
|
|
|
|
err = PTR_ERR(sleb);
|
|
|
|
|
goto out_free;
|
|
|
|
|
}
|
|
|
|
|
list_for_each_entry(snod, &sleb->nodes, list) {
|
|
|
|
|
switch (snod->type) {
|
|
|
|
|
case UBIFS_REF_NODE: {
|
|
|
|
|
struct ubifs_ref_node *ref = snod->node;
|
|
|
|
|
int ref_lnum = le32_to_cpu(ref->lnum);
|
|
|
|
|
|
|
|
|
|
err = done_already(&done_tree, ref_lnum);
|
|
|
|
|
if (err < 0)
|
|
|
|
|
goto out_scan;
|
|
|
|
|
if (err != 1) {
|
|
|
|
|
err = add_node(c, buf, &write_lnum,
|
|
|
|
|
&offs, snod->node);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out_scan;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case UBIFS_CS_NODE:
|
|
|
|
|
if (!first)
|
|
|
|
|
break;
|
|
|
|
|
err = add_node(c, buf, &write_lnum, &offs,
|
|
|
|
|
snod->node);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out_scan;
|
|
|
|
|
first = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ubifs_scan_destroy(sleb);
|
|
|
|
|
if (lnum == c->lhead_lnum)
|
|
|
|
|
break;
|
2011-05-06 22:52:32 +08:00
|
|
|
|
lnum = ubifs_next_log_lnum(c, lnum);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
}
|
|
|
|
|
if (offs) {
|
|
|
|
|
int sz = ALIGN(offs, c->min_io_size);
|
|
|
|
|
|
|
|
|
|
ubifs_pad(c, buf + offs, sz - offs);
|
2012-05-14 23:55:51 +08:00
|
|
|
|
err = ubifs_leb_change(c, write_lnum, buf, sz);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
if (err)
|
|
|
|
|
goto out_free;
|
|
|
|
|
offs = ALIGN(offs, c->min_io_size);
|
|
|
|
|
}
|
|
|
|
|
destroy_done_tree(&done_tree);
|
|
|
|
|
vfree(buf);
|
|
|
|
|
if (write_lnum == c->lhead_lnum) {
|
UBIFS: extend debug/message capabilities
In the case where we have more than one volumes on different UBI
devices, it may be not that easy to tell which volume prints the
messages. Add ubi number and volume id in ubifs_msg/warn/error
to help debug. These two values are passed by struct ubifs_info.
For those where ubifs_info is not initialized yet, ubifs_* is
replaced by pr_*. For those where ubifs_info is not avaliable,
ubifs_info is passed to the calling function as a const parameter.
The output looks like,
[ 95.444879] UBIFS (ubi0:1): background thread "ubifs_bgt0_1" started, PID 696
[ 95.484688] UBIFS (ubi0:1): UBIFS: mounted UBI device 0, volume 1, name "test1"
[ 95.484694] UBIFS (ubi0:1): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.484699] UBIFS (ubi0:1): FS size: 30220288 bytes (28 MiB, 238 LEBs), journal size 1523712 bytes (1 MiB, 12 LEBs)
[ 95.484703] UBIFS (ubi0:1): reserved for root: 1427378 bytes (1393 KiB)
[ 95.484709] UBIFS (ubi0:1): media format: w4/r0 (latest is w4/r0), UUID 40DFFC0E-70BE-4193-8905-F7D6DFE60B17, small LPT model
[ 95.489875] UBIFS (ubi1:0): background thread "ubifs_bgt1_0" started, PID 699
[ 95.529713] UBIFS (ubi1:0): UBIFS: mounted UBI device 1, volume 0, name "test2"
[ 95.529718] UBIFS (ubi1:0): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.529724] UBIFS (ubi1:0): FS size: 19808256 bytes (18 MiB, 156 LEBs), journal size 1015809 bytes (0 MiB, 8 LEBs)
[ 95.529727] UBIFS (ubi1:0): reserved for root: 935592 bytes (913 KiB)
[ 95.529733] UBIFS (ubi1:0): media format: w4/r0 (latest is w4/r0), UUID EEB7779D-F419-4CA9-811B-831CAC7233D4, small LPT model
[ 954.264767] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node type (255 but expected 6)
[ 954.367030] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node at LEB 0:0, LEB mapping status 1
Signed-off-by: Sheng Yong <shengyong1@huawei.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
2015-03-20 18:39:42 +08:00
|
|
|
|
ubifs_err(c, "log is too full");
|
2008-07-15 00:08:37 +08:00
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
/* Unmap remaining LEBs */
|
|
|
|
|
lnum = write_lnum;
|
|
|
|
|
do {
|
2011-05-06 22:52:32 +08:00
|
|
|
|
lnum = ubifs_next_log_lnum(c, lnum);
|
2008-07-15 00:08:37 +08:00
|
|
|
|
err = ubifs_leb_unmap(c, lnum);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
} while (lnum != c->lhead_lnum);
|
|
|
|
|
c->lhead_lnum = write_lnum;
|
|
|
|
|
c->lhead_offs = offs;
|
|
|
|
|
dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out_scan:
|
|
|
|
|
ubifs_scan_destroy(sleb);
|
|
|
|
|
out_free:
|
|
|
|
|
destroy_done_tree(&done_tree);
|
|
|
|
|
vfree(buf);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* dbg_check_bud_bytes - make sure bud bytes calculation are all right.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*
|
|
|
|
|
* This function makes sure the amount of flash space used by closed buds
|
|
|
|
|
* ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
|
|
|
|
|
* case of failure.
|
|
|
|
|
*/
|
|
|
|
|
static int dbg_check_bud_bytes(struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
int i, err = 0;
|
|
|
|
|
struct ubifs_bud *bud;
|
|
|
|
|
long long bud_bytes = 0;
|
|
|
|
|
|
2011-06-03 13:31:29 +08:00
|
|
|
|
if (!dbg_is_chk_gen(c))
|
2008-07-15 00:08:37 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
spin_lock(&c->buds_lock);
|
|
|
|
|
for (i = 0; i < c->jhead_cnt; i++)
|
|
|
|
|
list_for_each_entry(bud, &c->jheads[i].buds_list, list)
|
|
|
|
|
bud_bytes += c->leb_size - bud->start;
|
|
|
|
|
|
|
|
|
|
if (c->bud_bytes != bud_bytes) {
|
UBIFS: extend debug/message capabilities
In the case where we have more than one volumes on different UBI
devices, it may be not that easy to tell which volume prints the
messages. Add ubi number and volume id in ubifs_msg/warn/error
to help debug. These two values are passed by struct ubifs_info.
For those where ubifs_info is not initialized yet, ubifs_* is
replaced by pr_*. For those where ubifs_info is not avaliable,
ubifs_info is passed to the calling function as a const parameter.
The output looks like,
[ 95.444879] UBIFS (ubi0:1): background thread "ubifs_bgt0_1" started, PID 696
[ 95.484688] UBIFS (ubi0:1): UBIFS: mounted UBI device 0, volume 1, name "test1"
[ 95.484694] UBIFS (ubi0:1): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.484699] UBIFS (ubi0:1): FS size: 30220288 bytes (28 MiB, 238 LEBs), journal size 1523712 bytes (1 MiB, 12 LEBs)
[ 95.484703] UBIFS (ubi0:1): reserved for root: 1427378 bytes (1393 KiB)
[ 95.484709] UBIFS (ubi0:1): media format: w4/r0 (latest is w4/r0), UUID 40DFFC0E-70BE-4193-8905-F7D6DFE60B17, small LPT model
[ 95.489875] UBIFS (ubi1:0): background thread "ubifs_bgt1_0" started, PID 699
[ 95.529713] UBIFS (ubi1:0): UBIFS: mounted UBI device 1, volume 0, name "test2"
[ 95.529718] UBIFS (ubi1:0): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.529724] UBIFS (ubi1:0): FS size: 19808256 bytes (18 MiB, 156 LEBs), journal size 1015809 bytes (0 MiB, 8 LEBs)
[ 95.529727] UBIFS (ubi1:0): reserved for root: 935592 bytes (913 KiB)
[ 95.529733] UBIFS (ubi1:0): media format: w4/r0 (latest is w4/r0), UUID EEB7779D-F419-4CA9-811B-831CAC7233D4, small LPT model
[ 954.264767] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node type (255 but expected 6)
[ 954.367030] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node at LEB 0:0, LEB mapping status 1
Signed-off-by: Sheng Yong <shengyong1@huawei.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
2015-03-20 18:39:42 +08:00
|
|
|
|
ubifs_err(c, "bad bud_bytes %lld, calculated %lld",
|
2008-07-15 00:08:37 +08:00
|
|
|
|
c->bud_bytes, bud_bytes);
|
|
|
|
|
err = -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
spin_unlock(&c->buds_lock);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|