2019-05-29 23:57:50 +00:00
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-07-14 16:08:37 +00:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of UBIFS.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2006-2008 Nokia Corporation.
|
|
|
|
|
* Copyright (C) 2006, 2007 University of Szeged, Hungary
|
|
|
|
|
*
|
|
|
|
|
* Authors: Artem Bityutskiy (Битюцкий Артём)
|
|
|
|
|
* Adrian Hunter
|
|
|
|
|
* Zoltan Sogor
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This file implements UBIFS I/O subsystem which provides various I/O-related
|
|
|
|
|
* helper functions (reading/writing/checking/validating nodes) and implements
|
|
|
|
|
* write-buffering support. Write buffers help to save space which otherwise
|
|
|
|
|
* would have been wasted for padding to the nearest minimal I/O unit boundary.
|
|
|
|
|
* Instead, data first goes to the write-buffer and is flushed when the
|
|
|
|
|
* buffer is full or when it is not used for some time (by timer). This is
|
2009-01-27 14:12:31 +00:00
|
|
|
|
* similar to the mechanism is used by JFFS2.
|
2008-07-14 16:08:37 +00:00
|
|
|
|
*
|
2011-02-06 12:45:26 +00:00
|
|
|
|
* UBIFS distinguishes between minimum write size (@c->min_io_size) and maximum
|
|
|
|
|
* write size (@c->max_write_size). The latter is the maximum amount of bytes
|
|
|
|
|
* the underlying flash is able to program at a time, and writing in
|
|
|
|
|
* @c->max_write_size units should presumably be faster. Obviously,
|
|
|
|
|
* @c->min_io_size <= @c->max_write_size. Write-buffers are of
|
|
|
|
|
* @c->max_write_size bytes in size for maximum performance. However, when a
|
|
|
|
|
* write-buffer is flushed, only the portion of it (aligned to @c->min_io_size
|
|
|
|
|
* boundary) which contains data is written, not the whole write-buffer,
|
|
|
|
|
* because this is more space-efficient.
|
|
|
|
|
*
|
|
|
|
|
* This optimization adds few complications to the code. Indeed, on the one
|
|
|
|
|
* hand, we want to write in optimal @c->max_write_size bytes chunks, which
|
|
|
|
|
* also means aligning writes at the @c->max_write_size bytes offsets. On the
|
|
|
|
|
* other hand, we do not want to waste space when synchronizing the write
|
|
|
|
|
* buffer, so during synchronization we writes in smaller chunks. And this makes
|
|
|
|
|
* the next write offset to be not aligned to @c->max_write_size bytes. So the
|
|
|
|
|
* have to make sure that the write-buffer offset (@wbuf->offs) becomes aligned
|
|
|
|
|
* to @c->max_write_size bytes again. We do this by temporarily shrinking
|
|
|
|
|
* write-buffer size (@wbuf->size).
|
|
|
|
|
*
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
|
|
|
|
|
* mutexes defined inside these objects. Since sometimes upper-level code
|
|
|
|
|
* has to lock the write-buffer (e.g. journal space reservation code), many
|
|
|
|
|
* functions related to write-buffers have "nolock" suffix which means that the
|
|
|
|
|
* caller has to lock the write-buffer before calling this function.
|
|
|
|
|
*
|
|
|
|
|
* UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
|
|
|
|
|
* aligned, UBIFS starts the next node from the aligned address, and the padded
|
|
|
|
|
* bytes may contain any rubbish. In other words, UBIFS does not put padding
|
|
|
|
|
* bytes in those small gaps. Common headers of nodes store real node lengths,
|
|
|
|
|
* not aligned lengths. Indexing nodes also store real lengths in branches.
|
|
|
|
|
*
|
|
|
|
|
* UBIFS uses padding when it pads to the next min. I/O unit. In this case it
|
|
|
|
|
* uses padding nodes or padding bytes, if the padding node does not fit.
|
|
|
|
|
*
|
2011-02-06 12:45:26 +00:00
|
|
|
|
* All UBIFS nodes are protected by CRC checksums and UBIFS checks CRC when
|
|
|
|
|
* they are read from the flash media.
|
2008-07-14 16:08:37 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <linux/crc32.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
|
#include <linux/slab.h>
|
2008-07-14 16:08:37 +00:00
|
|
|
|
#include "ubifs.h"
|
|
|
|
|
|
2008-07-21 12:39:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* ubifs_ro_mode - switch UBIFS to read read-only mode.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @err: error code which is the reason of switching to R/O mode
|
|
|
|
|
*/
|
|
|
|
|
void ubifs_ro_mode(struct ubifs_info *c, int err)
|
|
|
|
|
{
|
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 13:44:28 +00:00
|
|
|
|
if (!c->ro_error) {
|
|
|
|
|
c->ro_error = 1;
|
2008-09-08 13:07:01 +00:00
|
|
|
|
c->no_chk_data_crc = 0;
|
2017-11-27 21:05:09 +00:00
|
|
|
|
c->vfs_sb->s_flags |= SB_RDONLY;
|
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 10:39:42 +00:00
|
|
|
|
ubifs_warn(c, "switched to read-only mode, error %d", err);
|
2011-06-03 10:16:08 +00:00
|
|
|
|
dump_stack();
|
2008-07-21 12:39:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-03 10:45:09 +00:00
|
|
|
|
/*
|
|
|
|
|
* Below are simple wrappers over UBI I/O functions which include some
|
|
|
|
|
* additional checks and UBIFS debugging stuff. See corresponding UBI function
|
|
|
|
|
* for more information.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
int ubifs_leb_read(const struct ubifs_info *c, int lnum, void *buf, int offs,
|
|
|
|
|
int len, int even_ebadmsg)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
err = ubi_read(c->ubi, lnum, buf, offs, len);
|
|
|
|
|
/*
|
|
|
|
|
* In case of %-EBADMSG print the error message only if the
|
|
|
|
|
* @even_ebadmsg is true.
|
|
|
|
|
*/
|
|
|
|
|
if (err && (err != -EBADMSG || even_ebadmsg)) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "reading %d bytes from LEB %d:%d failed, error %d",
|
2011-06-03 10:45:09 +00:00
|
|
|
|
len, lnum, offs, err);
|
2012-05-16 16:04:54 +00:00
|
|
|
|
dump_stack();
|
2011-06-03 10:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ubifs_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
|
2012-05-14 15:55:51 +00:00
|
|
|
|
int len)
|
2011-06-03 10:45:09 +00:00
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
if (c->ro_error)
|
|
|
|
|
return -EROFS;
|
|
|
|
|
if (!dbg_is_tst_rcvry(c))
|
2012-05-14 15:55:51 +00:00
|
|
|
|
err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
else
|
2012-05-14 15:55:51 +00:00
|
|
|
|
err = dbg_leb_write(c, lnum, buf, offs, len);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
if (err) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "writing %d bytes to LEB %d:%d failed, error %d",
|
2011-06-03 10:45:09 +00:00
|
|
|
|
len, lnum, offs, err);
|
|
|
|
|
ubifs_ro_mode(c, err);
|
2012-05-16 16:04:54 +00:00
|
|
|
|
dump_stack();
|
2011-06-03 10:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-14 15:55:51 +00:00
|
|
|
|
int ubifs_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len)
|
2011-06-03 10:45:09 +00:00
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
if (c->ro_error)
|
|
|
|
|
return -EROFS;
|
|
|
|
|
if (!dbg_is_tst_rcvry(c))
|
2012-05-14 15:55:51 +00:00
|
|
|
|
err = ubi_leb_change(c->ubi, lnum, buf, len);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
else
|
2012-05-14 15:55:51 +00:00
|
|
|
|
err = dbg_leb_change(c, lnum, buf, len);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
if (err) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "changing %d bytes in LEB %d failed, error %d",
|
2011-06-03 10:45:09 +00:00
|
|
|
|
len, lnum, err);
|
|
|
|
|
ubifs_ro_mode(c, err);
|
2012-05-16 16:04:54 +00:00
|
|
|
|
dump_stack();
|
2011-06-03 10:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ubifs_leb_unmap(struct ubifs_info *c, int lnum)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
if (c->ro_error)
|
|
|
|
|
return -EROFS;
|
|
|
|
|
if (!dbg_is_tst_rcvry(c))
|
|
|
|
|
err = ubi_leb_unmap(c->ubi, lnum);
|
|
|
|
|
else
|
2011-06-03 11:51:41 +00:00
|
|
|
|
err = dbg_leb_unmap(c, lnum);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
if (err) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "unmap LEB %d failed, error %d", lnum, err);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
ubifs_ro_mode(c, err);
|
2012-05-16 16:04:54 +00:00
|
|
|
|
dump_stack();
|
2011-06-03 10:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-14 15:55:51 +00:00
|
|
|
|
int ubifs_leb_map(struct ubifs_info *c, int lnum)
|
2011-06-03 10:45:09 +00:00
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
if (c->ro_error)
|
|
|
|
|
return -EROFS;
|
|
|
|
|
if (!dbg_is_tst_rcvry(c))
|
2012-05-14 15:55:51 +00:00
|
|
|
|
err = ubi_leb_map(c->ubi, lnum);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
else
|
2012-05-14 15:55:51 +00:00
|
|
|
|
err = dbg_leb_map(c, lnum);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
if (err) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "mapping LEB %d failed, error %d", lnum, err);
|
2011-06-03 10:45:09 +00:00
|
|
|
|
ubifs_ro_mode(c, err);
|
2012-05-16 16:04:54 +00:00
|
|
|
|
dump_stack();
|
2011-06-03 10:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ubifs_is_mapped(const struct ubifs_info *c, int lnum)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
err = ubi_is_mapped(c->ubi, lnum);
|
|
|
|
|
if (err < 0) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "ubi_is_mapped failed for LEB %d, error %d",
|
2011-06-03 10:45:09 +00:00
|
|
|
|
lnum, err);
|
2012-05-16 16:04:54 +00:00
|
|
|
|
dump_stack();
|
2011-06-03 10:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-10 04:22:39 +00:00
|
|
|
|
static void record_magic_error(struct ubifs_stats_info *stats)
|
|
|
|
|
{
|
|
|
|
|
if (stats)
|
|
|
|
|
stats->magic_errors++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void record_node_error(struct ubifs_stats_info *stats)
|
|
|
|
|
{
|
|
|
|
|
if (stats)
|
|
|
|
|
stats->node_errors++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void record_crc_error(struct ubifs_stats_info *stats)
|
|
|
|
|
{
|
|
|
|
|
if (stats)
|
|
|
|
|
stats->crc_errors++;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-14 16:08:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* ubifs_check_node - check node.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @buf: node to check
|
2020-06-16 07:11:44 +00:00
|
|
|
|
* @len: node length
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* @lnum: logical eraseblock number
|
|
|
|
|
* @offs: offset within the logical eraseblock
|
|
|
|
|
* @quiet: print no messages
|
2009-01-27 14:12:31 +00:00
|
|
|
|
* @must_chk_crc: indicates whether to always check the CRC
|
2008-07-14 16:08:37 +00:00
|
|
|
|
*
|
|
|
|
|
* This function checks node magic number and CRC checksum. This function also
|
|
|
|
|
* validates node length to prevent UBIFS from becoming crazy when an attacker
|
|
|
|
|
* feeds it a file-system image with incorrect nodes. For example, too large
|
|
|
|
|
* node length in the common header could cause UBIFS to read memory outside of
|
|
|
|
|
* allocated buffer when checking the CRC checksum.
|
|
|
|
|
*
|
2009-01-27 14:12:31 +00:00
|
|
|
|
* This function may skip data nodes CRC checking if @c->no_chk_data_crc is
|
|
|
|
|
* true, which is controlled by corresponding UBIFS mount option. However, if
|
|
|
|
|
* @must_chk_crc is true, then @c->no_chk_data_crc is ignored and CRC is
|
2011-01-17 20:27:56 +00:00
|
|
|
|
* checked. Similarly, if @c->mounting or @c->remounting_rw is true (we are
|
|
|
|
|
* mounting or re-mounting to R/W mode), @c->no_chk_data_crc is ignored and CRC
|
|
|
|
|
* is checked. This is because during mounting or re-mounting from R/O mode to
|
|
|
|
|
* R/W mode we may read journal nodes (when replying the journal or doing the
|
|
|
|
|
* recovery) and the journal nodes may potentially be corrupted, so checking is
|
|
|
|
|
* required.
|
2009-01-27 14:12:31 +00:00
|
|
|
|
*
|
|
|
|
|
* This function returns zero in case of success and %-EUCLEAN in case of bad
|
|
|
|
|
* CRC or magic.
|
2008-07-14 16:08:37 +00:00
|
|
|
|
*/
|
2020-06-16 07:11:44 +00:00
|
|
|
|
int ubifs_check_node(const struct ubifs_info *c, const void *buf, int len,
|
|
|
|
|
int lnum, int offs, int quiet, int must_chk_crc)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
{
|
2020-06-16 07:11:43 +00:00
|
|
|
|
int err = -EINVAL, type, node_len;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
uint32_t crc, node_crc, magic;
|
|
|
|
|
const struct ubifs_ch *ch = buf;
|
|
|
|
|
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
|
|
|
|
|
ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
magic = le32_to_cpu(ch->magic);
|
|
|
|
|
if (magic != UBIFS_NODE_MAGIC) {
|
|
|
|
|
if (!quiet)
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "bad magic %#08x, expected %#08x",
|
2008-07-14 16:08:37 +00:00
|
|
|
|
magic, UBIFS_NODE_MAGIC);
|
2021-10-10 04:22:39 +00:00
|
|
|
|
record_magic_error(c->stats);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
err = -EUCLEAN;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type = ch->node_type;
|
|
|
|
|
if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
|
|
|
|
|
if (!quiet)
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "bad node type %d", type);
|
2021-10-10 04:22:39 +00:00
|
|
|
|
record_node_error(c->stats);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node_len = le32_to_cpu(ch->len);
|
|
|
|
|
if (node_len + offs > c->leb_size)
|
|
|
|
|
goto out_len;
|
|
|
|
|
|
|
|
|
|
if (c->ranges[type].max_len == 0) {
|
|
|
|
|
if (node_len != c->ranges[type].len)
|
|
|
|
|
goto out_len;
|
|
|
|
|
} else if (node_len < c->ranges[type].min_len ||
|
|
|
|
|
node_len > c->ranges[type].max_len)
|
|
|
|
|
goto out_len;
|
|
|
|
|
|
2011-01-17 20:27:56 +00:00
|
|
|
|
if (!must_chk_crc && type == UBIFS_DATA_NODE && !c->mounting &&
|
|
|
|
|
!c->remounting_rw && c->no_chk_data_crc)
|
2009-01-27 14:12:31 +00:00
|
|
|
|
return 0;
|
2008-09-04 13:26:00 +00:00
|
|
|
|
|
2008-07-14 16:08:37 +00:00
|
|
|
|
crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
|
|
|
|
|
node_crc = le32_to_cpu(ch->crc);
|
|
|
|
|
if (crc != node_crc) {
|
|
|
|
|
if (!quiet)
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "bad CRC: calculated %#08x, read %#08x",
|
2008-07-14 16:08:37 +00:00
|
|
|
|
crc, node_crc);
|
2021-10-10 04:22:39 +00:00
|
|
|
|
record_crc_error(c->stats);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
err = -EUCLEAN;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out_len:
|
|
|
|
|
if (!quiet)
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "bad node length %d", node_len);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
out:
|
|
|
|
|
if (!quiet) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "bad node at LEB %d:%d", lnum, offs);
|
2020-06-16 07:11:44 +00:00
|
|
|
|
ubifs_dump_node(c, buf, len);
|
2012-05-16 16:04:54 +00:00
|
|
|
|
dump_stack();
|
2008-07-14 16:08:37 +00:00
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_pad - pad flash space.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @buf: buffer to put padding to
|
|
|
|
|
* @pad: how many bytes to pad
|
|
|
|
|
*
|
|
|
|
|
* The flash media obliges us to write only in chunks of %c->min_io_size and
|
|
|
|
|
* when we have to write less data we add padding node to the write-buffer and
|
|
|
|
|
* pad it to the next minimal I/O unit's boundary. Padding nodes help when the
|
|
|
|
|
* media is being scanned. If the amount of wasted space is not enough to fit a
|
|
|
|
|
* padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
|
|
|
|
|
* pattern (%UBIFS_PADDING_BYTE).
|
|
|
|
|
*
|
|
|
|
|
* Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
|
|
|
|
|
* used.
|
|
|
|
|
*/
|
|
|
|
|
void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
|
|
|
|
|
{
|
|
|
|
|
uint32_t crc;
|
|
|
|
|
|
2020-11-16 21:05:30 +00:00
|
|
|
|
ubifs_assert(c, pad >= 0);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
if (pad >= UBIFS_PAD_NODE_SZ) {
|
|
|
|
|
struct ubifs_ch *ch = buf;
|
|
|
|
|
struct ubifs_pad_node *pad_node = buf;
|
|
|
|
|
|
|
|
|
|
ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
|
|
|
|
|
ch->node_type = UBIFS_PAD_NODE;
|
|
|
|
|
ch->group_type = UBIFS_NO_NODE_GROUP;
|
|
|
|
|
ch->padding[0] = ch->padding[1] = 0;
|
|
|
|
|
ch->sqnum = 0;
|
|
|
|
|
ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
|
|
|
|
|
pad -= UBIFS_PAD_NODE_SZ;
|
|
|
|
|
pad_node->pad_len = cpu_to_le32(pad);
|
|
|
|
|
crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
|
|
|
|
|
ch->crc = cpu_to_le32(crc);
|
|
|
|
|
memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
|
|
|
|
|
} else if (pad > 0)
|
|
|
|
|
/* Too little space, padding node won't fit */
|
|
|
|
|
memset(buf, UBIFS_PADDING_BYTE, pad);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* next_sqnum - get next sequence number.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*/
|
|
|
|
|
static unsigned long long next_sqnum(struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
unsigned long long sqnum;
|
|
|
|
|
|
|
|
|
|
spin_lock(&c->cnt_lock);
|
|
|
|
|
sqnum = ++c->max_sqnum;
|
|
|
|
|
spin_unlock(&c->cnt_lock);
|
|
|
|
|
|
|
|
|
|
if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
|
|
|
|
|
if (sqnum >= SQNUM_WATERMARK) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "sequence number overflow %llu, end of life",
|
2008-07-14 16:08:37 +00:00
|
|
|
|
sqnum);
|
|
|
|
|
ubifs_ro_mode(c, -EINVAL);
|
|
|
|
|
}
|
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 10:39:42 +00:00
|
|
|
|
ubifs_warn(c, "running out of sequence numbers, end of life soon");
|
2008-07-14 16:08:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sqnum;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 12:36:31 +00:00
|
|
|
|
void ubifs_init_node(struct ubifs_info *c, void *node, int len, int pad)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
{
|
|
|
|
|
struct ubifs_ch *ch = node;
|
|
|
|
|
unsigned long long sqnum = next_sqnum(c);
|
|
|
|
|
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, len >= UBIFS_CH_SZ);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
|
|
|
|
|
ch->len = cpu_to_le32(len);
|
|
|
|
|
ch->group_type = UBIFS_NO_NODE_GROUP;
|
|
|
|
|
ch->sqnum = cpu_to_le64(sqnum);
|
|
|
|
|
ch->padding[0] = ch->padding[1] = 0;
|
|
|
|
|
|
|
|
|
|
if (pad) {
|
|
|
|
|
len = ALIGN(len, 8);
|
|
|
|
|
pad = ALIGN(len, c->min_io_size) - len;
|
|
|
|
|
ubifs_pad(c, node + len, pad);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 12:36:31 +00:00
|
|
|
|
void ubifs_crc_node(struct ubifs_info *c, void *node, int len)
|
|
|
|
|
{
|
|
|
|
|
struct ubifs_ch *ch = node;
|
|
|
|
|
uint32_t crc;
|
|
|
|
|
|
|
|
|
|
crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
|
|
|
|
|
ch->crc = cpu_to_le32(crc);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 12:36:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* ubifs_prepare_node_hmac - prepare node to be written to flash.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @node: the node to pad
|
|
|
|
|
* @len: node length
|
|
|
|
|
* @hmac_offs: offset of the HMAC in the node
|
|
|
|
|
* @pad: if the buffer has to be padded
|
|
|
|
|
*
|
|
|
|
|
* This function prepares node at @node to be written to the media - it
|
|
|
|
|
* calculates node CRC, fills the common header, and adds proper padding up to
|
|
|
|
|
* the next minimum I/O unit if @pad is not zero. if @hmac_offs is positive then
|
|
|
|
|
* a HMAC is inserted into the node at the given offset.
|
|
|
|
|
*
|
|
|
|
|
* This function returns 0 for success or a negative error code otherwise.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_prepare_node_hmac(struct ubifs_info *c, void *node, int len,
|
|
|
|
|
int hmac_offs, int pad)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
ubifs_init_node(c, node, len, pad);
|
|
|
|
|
|
|
|
|
|
if (hmac_offs > 0) {
|
|
|
|
|
err = ubifs_node_insert_hmac(c, node, len, hmac_offs);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ubifs_crc_node(c, node, len);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 12:36:31 +00:00
|
|
|
|
/**
|
|
|
|
|
* ubifs_prepare_node - prepare node to be written to flash.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @node: the node to pad
|
|
|
|
|
* @len: node length
|
|
|
|
|
* @pad: if the buffer has to be padded
|
|
|
|
|
*
|
|
|
|
|
* This function prepares node at @node to be written to the media - it
|
|
|
|
|
* calculates node CRC, fills the common header, and adds proper padding up to
|
|
|
|
|
* the next minimum I/O unit if @pad is not zero.
|
|
|
|
|
*/
|
|
|
|
|
void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
|
|
|
|
|
{
|
2018-09-07 12:36:33 +00:00
|
|
|
|
/*
|
|
|
|
|
* Deliberately ignore return value since this function can only fail
|
|
|
|
|
* when a hmac offset is given.
|
|
|
|
|
*/
|
|
|
|
|
ubifs_prepare_node_hmac(c, node, len, 0, pad);
|
2018-09-07 12:36:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-07-14 16:08:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* ubifs_prep_grp_node - prepare node of a group to be written to flash.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @node: the node to pad
|
|
|
|
|
* @len: node length
|
|
|
|
|
* @last: indicates the last node of the group
|
|
|
|
|
*
|
|
|
|
|
* This function prepares node at @node to be written to the media - it
|
|
|
|
|
* calculates node CRC and fills the common header.
|
|
|
|
|
*/
|
|
|
|
|
void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
|
|
|
|
|
{
|
|
|
|
|
uint32_t crc;
|
|
|
|
|
struct ubifs_ch *ch = node;
|
|
|
|
|
unsigned long long sqnum = next_sqnum(c);
|
|
|
|
|
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, len >= UBIFS_CH_SZ);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
|
|
|
|
|
ch->len = cpu_to_le32(len);
|
|
|
|
|
if (last)
|
|
|
|
|
ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
|
|
|
|
|
else
|
|
|
|
|
ch->group_type = UBIFS_IN_NODE_GROUP;
|
|
|
|
|
ch->sqnum = cpu_to_le64(sqnum);
|
|
|
|
|
ch->padding[0] = ch->padding[1] = 0;
|
|
|
|
|
crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
|
|
|
|
|
ch->crc = cpu_to_le32(crc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-17 07:03:04 +00:00
|
|
|
|
* wbuf_timer_callback_nolock - write-buffer timer callback function.
|
2014-07-15 19:33:51 +00:00
|
|
|
|
* @timer: timer data (write-buffer descriptor)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
*
|
|
|
|
|
* This function is called when the write-buffer timer expires.
|
|
|
|
|
*/
|
2009-05-28 13:24:15 +00:00
|
|
|
|
static enum hrtimer_restart wbuf_timer_callback_nolock(struct hrtimer *timer)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
{
|
2009-05-28 13:24:15 +00:00
|
|
|
|
struct ubifs_wbuf *wbuf = container_of(timer, struct ubifs_wbuf, timer);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
2009-09-15 12:03:51 +00:00
|
|
|
|
dbg_io("jhead %s", dbg_jhead(wbuf->jhead));
|
2008-07-14 16:08:37 +00:00
|
|
|
|
wbuf->need_sync = 1;
|
|
|
|
|
wbuf->c->need_wbuf_sync = 1;
|
|
|
|
|
ubifs_wake_up_bgt(wbuf->c);
|
2009-05-28 13:24:15 +00:00
|
|
|
|
return HRTIMER_NORESTART;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-17 07:03:04 +00:00
|
|
|
|
* new_wbuf_timer_nolock - start new write-buffer timer.
|
2018-07-12 11:01:57 +00:00
|
|
|
|
* @c: UBIFS file-system description object
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* @wbuf: write-buffer descriptor
|
|
|
|
|
*/
|
2018-07-12 11:01:57 +00:00
|
|
|
|
static void new_wbuf_timer_nolock(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
{
|
2016-09-20 08:36:15 +00:00
|
|
|
|
ktime_t softlimit = ms_to_ktime(dirty_writeback_interval * 10);
|
|
|
|
|
unsigned long long delta = dirty_writeback_interval;
|
2016-09-20 08:36:14 +00:00
|
|
|
|
|
2016-09-20 08:36:15 +00:00
|
|
|
|
/* centi to milli, milli to nano, then 10% */
|
|
|
|
|
delta *= 10ULL * NSEC_PER_MSEC / 10ULL;
|
2016-09-20 08:36:14 +00:00
|
|
|
|
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, !hrtimer_active(&wbuf->timer));
|
|
|
|
|
ubifs_assert(c, delta <= ULONG_MAX);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
2009-06-23 09:30:43 +00:00
|
|
|
|
if (wbuf->no_timer)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return;
|
2009-09-15 12:03:51 +00:00
|
|
|
|
dbg_io("set timer for jhead %s, %llu-%llu millisecs",
|
|
|
|
|
dbg_jhead(wbuf->jhead),
|
2016-09-20 08:36:14 +00:00
|
|
|
|
div_u64(ktime_to_ns(softlimit), USEC_PER_SEC),
|
|
|
|
|
div_u64(ktime_to_ns(softlimit) + delta, USEC_PER_SEC));
|
|
|
|
|
hrtimer_start_range_ns(&wbuf->timer, softlimit, delta,
|
2009-05-28 13:24:15 +00:00
|
|
|
|
HRTIMER_MODE_REL);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-17 07:03:04 +00:00
|
|
|
|
* cancel_wbuf_timer_nolock - cancel write-buffer timer.
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* @wbuf: write-buffer descriptor
|
|
|
|
|
*/
|
|
|
|
|
static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
|
|
|
|
|
{
|
2009-06-23 09:30:43 +00:00
|
|
|
|
if (wbuf->no_timer)
|
|
|
|
|
return;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
wbuf->need_sync = 0;
|
2009-05-28 13:24:15 +00:00
|
|
|
|
hrtimer_cancel(&wbuf->timer);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_wbuf_sync_nolock - synchronize write-buffer.
|
|
|
|
|
* @wbuf: write-buffer to synchronize
|
|
|
|
|
*
|
|
|
|
|
* This function synchronizes write-buffer @buf and returns zero in case of
|
|
|
|
|
* success or a negative error code in case of failure.
|
2011-02-06 12:45:26 +00:00
|
|
|
|
*
|
|
|
|
|
* Note, although write-buffers are of @c->max_write_size, this function does
|
|
|
|
|
* not necessarily writes all @c->max_write_size bytes to the flash. Instead,
|
|
|
|
|
* if the write-buffer is only partially filled with data, only the used part
|
|
|
|
|
* of the write-buffer (aligned on @c->min_io_size boundary) is synchronized.
|
|
|
|
|
* This way we waste less space.
|
2008-07-14 16:08:37 +00:00
|
|
|
|
*/
|
|
|
|
|
int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
|
|
|
|
|
{
|
|
|
|
|
struct ubifs_info *c = wbuf->c;
|
2011-02-06 12:45:26 +00:00
|
|
|
|
int err, dirt, sync_len;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
cancel_wbuf_timer_nolock(wbuf);
|
|
|
|
|
if (!wbuf->used || wbuf->lnum == -1)
|
|
|
|
|
/* Write-buffer is empty or not seeked */
|
|
|
|
|
return 0;
|
|
|
|
|
|
2009-09-15 12:03:51 +00:00
|
|
|
|
dbg_io("LEB %d:%d, %d bytes, jhead %s",
|
|
|
|
|
wbuf->lnum, wbuf->offs, wbuf->used, dbg_jhead(wbuf->jhead));
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, !(wbuf->avail & 7));
|
|
|
|
|
ubifs_assert(c, wbuf->offs + wbuf->size <= c->leb_size);
|
|
|
|
|
ubifs_assert(c, wbuf->size >= c->min_io_size);
|
|
|
|
|
ubifs_assert(c, wbuf->size <= c->max_write_size);
|
|
|
|
|
ubifs_assert(c, wbuf->size % c->min_io_size == 0);
|
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
2011-02-06 12:45:26 +00:00
|
|
|
|
if (c->leb_size - wbuf->offs >= c->max_write_size)
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size));
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
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 13:44:28 +00:00
|
|
|
|
if (c->ro_error)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return -EROFS;
|
|
|
|
|
|
2011-02-06 12:45:26 +00:00
|
|
|
|
/*
|
|
|
|
|
* Do not write whole write buffer but write only the minimum necessary
|
|
|
|
|
* amount of min. I/O units.
|
|
|
|
|
*/
|
|
|
|
|
sync_len = ALIGN(wbuf->used, c->min_io_size);
|
|
|
|
|
dirt = sync_len - wbuf->used;
|
|
|
|
|
if (dirt)
|
|
|
|
|
ubifs_pad(c, wbuf->buf + wbuf->used, dirt);
|
2012-05-14 15:55:51 +00:00
|
|
|
|
err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, sync_len);
|
2011-06-03 11:12:10 +00:00
|
|
|
|
if (err)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
spin_lock(&wbuf->lock);
|
2011-02-06 12:45:26 +00:00
|
|
|
|
wbuf->offs += sync_len;
|
|
|
|
|
/*
|
|
|
|
|
* Now @wbuf->offs is not necessarily aligned to @c->max_write_size.
|
|
|
|
|
* But our goal is to optimize writes and make sure we write in
|
|
|
|
|
* @c->max_write_size chunks and to @c->max_write_size-aligned offset.
|
|
|
|
|
* Thus, if @wbuf->offs is not aligned to @c->max_write_size now, make
|
|
|
|
|
* sure that @wbuf->offs + @wbuf->size is aligned to
|
|
|
|
|
* @c->max_write_size. This way we make sure that after next
|
|
|
|
|
* write-buffer flush we are again at the optimal offset (aligned to
|
|
|
|
|
* @c->max_write_size).
|
|
|
|
|
*/
|
|
|
|
|
if (c->leb_size - wbuf->offs < c->max_write_size)
|
|
|
|
|
wbuf->size = c->leb_size - wbuf->offs;
|
|
|
|
|
else if (wbuf->offs & (c->max_write_size - 1))
|
|
|
|
|
wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
|
|
|
|
|
else
|
|
|
|
|
wbuf->size = c->max_write_size;
|
|
|
|
|
wbuf->avail = wbuf->size;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
wbuf->used = 0;
|
|
|
|
|
wbuf->next_ino = 0;
|
|
|
|
|
spin_unlock(&wbuf->lock);
|
|
|
|
|
|
|
|
|
|
if (wbuf->sync_callback)
|
|
|
|
|
err = wbuf->sync_callback(c, wbuf->lnum,
|
|
|
|
|
c->leb_size - wbuf->offs, dirt);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_wbuf_seek_nolock - seek write-buffer.
|
|
|
|
|
* @wbuf: write-buffer
|
|
|
|
|
* @lnum: logical eraseblock number to seek to
|
|
|
|
|
* @offs: logical eraseblock offset to seek to
|
|
|
|
|
*
|
2009-06-23 17:30:32 +00:00
|
|
|
|
* This function targets the write-buffer to logical eraseblock @lnum:@offs.
|
2011-05-15 11:51:54 +00:00
|
|
|
|
* The write-buffer has to be empty. Returns zero in case of success and a
|
|
|
|
|
* negative error code in case of failure.
|
2008-07-14 16:08:37 +00:00
|
|
|
|
*/
|
2012-05-14 15:55:51 +00:00
|
|
|
|
int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
{
|
|
|
|
|
const struct ubifs_info *c = wbuf->c;
|
|
|
|
|
|
2009-09-15 12:03:51 +00:00
|
|
|
|
dbg_io("LEB %d:%d, jhead %s", lnum, offs, dbg_jhead(wbuf->jhead));
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt);
|
|
|
|
|
ubifs_assert(c, offs >= 0 && offs <= c->leb_size);
|
|
|
|
|
ubifs_assert(c, offs % c->min_io_size == 0 && !(offs & 7));
|
|
|
|
|
ubifs_assert(c, lnum != wbuf->lnum);
|
|
|
|
|
ubifs_assert(c, wbuf->used == 0);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
spin_lock(&wbuf->lock);
|
|
|
|
|
wbuf->lnum = lnum;
|
|
|
|
|
wbuf->offs = offs;
|
2011-02-06 12:45:26 +00:00
|
|
|
|
if (c->leb_size - wbuf->offs < c->max_write_size)
|
|
|
|
|
wbuf->size = c->leb_size - wbuf->offs;
|
|
|
|
|
else if (wbuf->offs & (c->max_write_size - 1))
|
|
|
|
|
wbuf->size = ALIGN(wbuf->offs, c->max_write_size) - wbuf->offs;
|
|
|
|
|
else
|
|
|
|
|
wbuf->size = c->max_write_size;
|
|
|
|
|
wbuf->avail = wbuf->size;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
wbuf->used = 0;
|
|
|
|
|
spin_unlock(&wbuf->lock);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_bg_wbufs_sync - synchronize write-buffers.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*
|
|
|
|
|
* This function is called by background thread to synchronize write-buffers.
|
|
|
|
|
* Returns zero in case of success and a negative error code in case of
|
|
|
|
|
* failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_bg_wbufs_sync(struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
int err, i;
|
|
|
|
|
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
if (!c->need_wbuf_sync)
|
|
|
|
|
return 0;
|
|
|
|
|
c->need_wbuf_sync = 0;
|
|
|
|
|
|
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 13:44:28 +00:00
|
|
|
|
if (c->ro_error) {
|
2008-07-14 16:08:37 +00:00
|
|
|
|
err = -EROFS;
|
|
|
|
|
goto out_timers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbg_io("synchronize");
|
|
|
|
|
for (i = 0; i < c->jhead_cnt; i++) {
|
|
|
|
|
struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
|
|
|
|
|
|
|
|
|
|
cond_resched();
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the mutex is locked then wbuf is being changed, so
|
|
|
|
|
* synchronization is not necessary.
|
|
|
|
|
*/
|
|
|
|
|
if (mutex_is_locked(&wbuf->io_mutex))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
|
|
|
|
|
if (!wbuf->need_sync) {
|
|
|
|
|
mutex_unlock(&wbuf->io_mutex);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = ubifs_wbuf_sync_nolock(wbuf);
|
|
|
|
|
mutex_unlock(&wbuf->io_mutex);
|
|
|
|
|
if (err) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "cannot sync write-buffer, error %d", err);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
ubifs_ro_mode(c, err);
|
|
|
|
|
goto out_timers;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out_timers:
|
|
|
|
|
/* Cancel all timers to prevent repeated errors */
|
|
|
|
|
for (i = 0; i < c->jhead_cnt; i++) {
|
|
|
|
|
struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
|
|
|
|
|
|
|
|
|
|
mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
|
|
|
|
|
cancel_wbuf_timer_nolock(wbuf);
|
|
|
|
|
mutex_unlock(&wbuf->io_mutex);
|
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_wbuf_write_nolock - write data to flash via write-buffer.
|
|
|
|
|
* @wbuf: write-buffer
|
|
|
|
|
* @buf: node to write
|
|
|
|
|
* @len: node length
|
|
|
|
|
*
|
|
|
|
|
* This function writes data to flash via write-buffer @wbuf. This means that
|
|
|
|
|
* the last piece of the node won't reach the flash media immediately if it
|
2011-02-06 12:45:26 +00:00
|
|
|
|
* does not take whole max. write unit (@c->max_write_size). Instead, the node
|
|
|
|
|
* will sit in RAM until the write-buffer is synchronized (e.g., by timer, or
|
|
|
|
|
* because more data are appended to the write-buffer).
|
2008-07-14 16:08:37 +00:00
|
|
|
|
*
|
|
|
|
|
* This function returns zero in case of success and a negative error code in
|
|
|
|
|
* case of failure. If the node cannot be written because there is no more
|
|
|
|
|
* space in this logical eraseblock, %-ENOSPC is returned.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
|
|
|
|
|
{
|
|
|
|
|
struct ubifs_info *c = wbuf->c;
|
2020-06-16 07:11:44 +00:00
|
|
|
|
int err, n, written = 0, aligned_len = ALIGN(len, 8);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
2009-09-15 12:03:51 +00:00
|
|
|
|
dbg_io("%d bytes (%s) to jhead %s wbuf at LEB %d:%d", len,
|
|
|
|
|
dbg_ntype(((struct ubifs_ch *)buf)->node_type),
|
|
|
|
|
dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs + wbuf->used);
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
|
|
|
|
|
ubifs_assert(c, wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
|
|
|
|
|
ubifs_assert(c, !(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
|
|
|
|
|
ubifs_assert(c, wbuf->avail > 0 && wbuf->avail <= wbuf->size);
|
|
|
|
|
ubifs_assert(c, wbuf->size >= c->min_io_size);
|
|
|
|
|
ubifs_assert(c, wbuf->size <= c->max_write_size);
|
|
|
|
|
ubifs_assert(c, wbuf->size % c->min_io_size == 0);
|
|
|
|
|
ubifs_assert(c, mutex_is_locked(&wbuf->io_mutex));
|
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
|
|
|
|
ubifs_assert(c, !c->space_fixup);
|
2011-02-06 12:45:26 +00:00
|
|
|
|
if (c->leb_size - wbuf->offs >= c->max_write_size)
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, !((wbuf->offs + wbuf->size) % c->max_write_size));
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
|
|
|
|
|
err = -ENOSPC;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel_wbuf_timer_nolock(wbuf);
|
|
|
|
|
|
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 13:44:28 +00:00
|
|
|
|
if (c->ro_error)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return -EROFS;
|
|
|
|
|
|
|
|
|
|
if (aligned_len <= wbuf->avail) {
|
|
|
|
|
/*
|
|
|
|
|
* The node is not very large and fits entirely within
|
|
|
|
|
* write-buffer.
|
|
|
|
|
*/
|
|
|
|
|
memcpy(wbuf->buf + wbuf->used, buf, len);
|
2020-11-16 21:05:30 +00:00
|
|
|
|
if (aligned_len > len) {
|
|
|
|
|
ubifs_assert(c, aligned_len - len < 8);
|
|
|
|
|
ubifs_pad(c, wbuf->buf + wbuf->used + len, aligned_len - len);
|
|
|
|
|
}
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
if (aligned_len == wbuf->avail) {
|
2009-09-15 12:03:51 +00:00
|
|
|
|
dbg_io("flush jhead %s wbuf to LEB %d:%d",
|
|
|
|
|
dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
|
2011-06-03 11:12:10 +00:00
|
|
|
|
err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf,
|
2012-05-14 15:55:51 +00:00
|
|
|
|
wbuf->offs, wbuf->size);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
spin_lock(&wbuf->lock);
|
2011-02-06 12:45:26 +00:00
|
|
|
|
wbuf->offs += wbuf->size;
|
|
|
|
|
if (c->leb_size - wbuf->offs >= c->max_write_size)
|
|
|
|
|
wbuf->size = c->max_write_size;
|
|
|
|
|
else
|
|
|
|
|
wbuf->size = c->leb_size - wbuf->offs;
|
|
|
|
|
wbuf->avail = wbuf->size;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
wbuf->used = 0;
|
|
|
|
|
wbuf->next_ino = 0;
|
|
|
|
|
spin_unlock(&wbuf->lock);
|
|
|
|
|
} else {
|
|
|
|
|
spin_lock(&wbuf->lock);
|
|
|
|
|
wbuf->avail -= aligned_len;
|
|
|
|
|
wbuf->used += aligned_len;
|
|
|
|
|
spin_unlock(&wbuf->lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-06 12:45:26 +00:00
|
|
|
|
if (wbuf->used) {
|
|
|
|
|
/*
|
|
|
|
|
* The node is large enough and does not fit entirely within
|
|
|
|
|
* current available space. We have to fill and flush
|
|
|
|
|
* write-buffer and switch to the next max. write unit.
|
|
|
|
|
*/
|
|
|
|
|
dbg_io("flush jhead %s wbuf to LEB %d:%d",
|
|
|
|
|
dbg_jhead(wbuf->jhead), wbuf->lnum, wbuf->offs);
|
|
|
|
|
memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
|
2011-06-03 11:12:10 +00:00
|
|
|
|
err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs,
|
2012-05-14 15:55:51 +00:00
|
|
|
|
wbuf->size);
|
2011-02-06 12:45:26 +00:00
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2011-05-14 14:37:47 +00:00
|
|
|
|
wbuf->offs += wbuf->size;
|
2011-02-06 12:45:26 +00:00
|
|
|
|
len -= wbuf->avail;
|
|
|
|
|
aligned_len -= wbuf->avail;
|
|
|
|
|
written += wbuf->avail;
|
|
|
|
|
} else if (wbuf->offs & (c->max_write_size - 1)) {
|
|
|
|
|
/*
|
|
|
|
|
* The write-buffer offset is not aligned to
|
|
|
|
|
* @c->max_write_size and @wbuf->size is less than
|
|
|
|
|
* @c->max_write_size. Write @wbuf->size bytes to make sure the
|
|
|
|
|
* following writes are done in optimal @c->max_write_size
|
|
|
|
|
* chunks.
|
|
|
|
|
*/
|
|
|
|
|
dbg_io("write %d bytes to LEB %d:%d",
|
|
|
|
|
wbuf->size, wbuf->lnum, wbuf->offs);
|
2011-06-03 11:12:10 +00:00
|
|
|
|
err = ubifs_leb_write(c, wbuf->lnum, buf, wbuf->offs,
|
2012-05-14 15:55:51 +00:00
|
|
|
|
wbuf->size);
|
2011-02-06 12:45:26 +00:00
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2011-05-14 14:37:47 +00:00
|
|
|
|
wbuf->offs += wbuf->size;
|
2011-02-06 12:45:26 +00:00
|
|
|
|
len -= wbuf->size;
|
|
|
|
|
aligned_len -= wbuf->size;
|
|
|
|
|
written += wbuf->size;
|
|
|
|
|
}
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
/*
|
2011-02-06 12:45:26 +00:00
|
|
|
|
* The remaining data may take more whole max. write units, so write the
|
|
|
|
|
* remains multiple to max. write unit size directly to the flash media.
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* We align node length to 8-byte boundary because we anyway flash wbuf
|
|
|
|
|
* if the remaining space is less than 8 bytes.
|
|
|
|
|
*/
|
2011-02-06 12:45:26 +00:00
|
|
|
|
n = aligned_len >> c->max_write_shift;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
if (n) {
|
ubifs: Fix read out-of-bounds in ubifs_wbuf_write_nolock()
Function ubifs_wbuf_write_nolock() may access buf out of bounds in
following process:
ubifs_wbuf_write_nolock():
aligned_len = ALIGN(len, 8); // Assume len = 4089, aligned_len = 4096
if (aligned_len <= wbuf->avail) ... // Not satisfy
if (wbuf->used) {
ubifs_leb_write() // Fill some data in avail wbuf
len -= wbuf->avail; // len is still not 8-bytes aligned
aligned_len -= wbuf->avail;
}
n = aligned_len >> c->max_write_shift;
if (n) {
n <<= c->max_write_shift;
err = ubifs_leb_write(c, wbuf->lnum, buf + written,
wbuf->offs, n);
// n > len, read out of bounds less than 8(n-len) bytes
}
, which can be catched by KASAN:
=========================================================
BUG: KASAN: slab-out-of-bounds in ecc_sw_hamming_calculate+0x1dc/0x7d0
Read of size 4 at addr ffff888105594ff8 by task kworker/u8:4/128
Workqueue: writeback wb_workfn (flush-ubifs_0_0)
Call Trace:
kasan_report.cold+0x81/0x165
nand_write_page_swecc+0xa9/0x160
ubifs_leb_write+0xf2/0x1b0 [ubifs]
ubifs_wbuf_write_nolock+0x421/0x12c0 [ubifs]
write_head+0xdc/0x1c0 [ubifs]
ubifs_jnl_write_inode+0x627/0x960 [ubifs]
wb_workfn+0x8af/0xb80
Function ubifs_wbuf_write_nolock() accepts that parameter 'len' is not 8
bytes aligned, the 'len' represents the true length of buf (which is
allocated in 'ubifs_jnl_xxx', eg. ubifs_jnl_write_inode), so
ubifs_wbuf_write_nolock() must handle the length read from 'buf' carefully
to write leb safely.
Fetch a reproducer in [Link].
Fixes: 1e51764a3c2ac0 ("UBIFS: add new flash file system")
Link: https://bugzilla.kernel.org/show_bug.cgi?id=214785
Reported-by: Chengsong Ke <kechengsong@huawei.com>
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Richard Weinberger <richard@nod.at>
2021-12-27 03:22:40 +00:00
|
|
|
|
int m = n - 1;
|
|
|
|
|
|
2011-05-14 14:37:47 +00:00
|
|
|
|
dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum,
|
|
|
|
|
wbuf->offs);
|
ubifs: Fix read out-of-bounds in ubifs_wbuf_write_nolock()
Function ubifs_wbuf_write_nolock() may access buf out of bounds in
following process:
ubifs_wbuf_write_nolock():
aligned_len = ALIGN(len, 8); // Assume len = 4089, aligned_len = 4096
if (aligned_len <= wbuf->avail) ... // Not satisfy
if (wbuf->used) {
ubifs_leb_write() // Fill some data in avail wbuf
len -= wbuf->avail; // len is still not 8-bytes aligned
aligned_len -= wbuf->avail;
}
n = aligned_len >> c->max_write_shift;
if (n) {
n <<= c->max_write_shift;
err = ubifs_leb_write(c, wbuf->lnum, buf + written,
wbuf->offs, n);
// n > len, read out of bounds less than 8(n-len) bytes
}
, which can be catched by KASAN:
=========================================================
BUG: KASAN: slab-out-of-bounds in ecc_sw_hamming_calculate+0x1dc/0x7d0
Read of size 4 at addr ffff888105594ff8 by task kworker/u8:4/128
Workqueue: writeback wb_workfn (flush-ubifs_0_0)
Call Trace:
kasan_report.cold+0x81/0x165
nand_write_page_swecc+0xa9/0x160
ubifs_leb_write+0xf2/0x1b0 [ubifs]
ubifs_wbuf_write_nolock+0x421/0x12c0 [ubifs]
write_head+0xdc/0x1c0 [ubifs]
ubifs_jnl_write_inode+0x627/0x960 [ubifs]
wb_workfn+0x8af/0xb80
Function ubifs_wbuf_write_nolock() accepts that parameter 'len' is not 8
bytes aligned, the 'len' represents the true length of buf (which is
allocated in 'ubifs_jnl_xxx', eg. ubifs_jnl_write_inode), so
ubifs_wbuf_write_nolock() must handle the length read from 'buf' carefully
to write leb safely.
Fetch a reproducer in [Link].
Fixes: 1e51764a3c2ac0 ("UBIFS: add new flash file system")
Link: https://bugzilla.kernel.org/show_bug.cgi?id=214785
Reported-by: Chengsong Ke <kechengsong@huawei.com>
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Richard Weinberger <richard@nod.at>
2021-12-27 03:22:40 +00:00
|
|
|
|
|
|
|
|
|
if (m) {
|
|
|
|
|
/* '(n-1)<<c->max_write_shift < len' is always true. */
|
|
|
|
|
m <<= c->max_write_shift;
|
|
|
|
|
err = ubifs_leb_write(c, wbuf->lnum, buf + written,
|
|
|
|
|
wbuf->offs, m);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
wbuf->offs += m;
|
|
|
|
|
aligned_len -= m;
|
|
|
|
|
len -= m;
|
|
|
|
|
written += m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The non-written len of buf may be less than 'n' because
|
|
|
|
|
* parameter 'len' is not 8 bytes aligned, so here we read
|
|
|
|
|
* min(len, n) bytes from buf.
|
|
|
|
|
*/
|
|
|
|
|
n = 1 << c->max_write_shift;
|
|
|
|
|
memcpy(wbuf->buf, buf + written, min(len, n));
|
|
|
|
|
if (n > len) {
|
|
|
|
|
ubifs_assert(c, n - len < 8);
|
|
|
|
|
ubifs_pad(c, wbuf->buf + len, n - len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = ubifs_leb_write(c, wbuf->lnum, wbuf->buf, wbuf->offs, n);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
2011-05-14 14:37:47 +00:00
|
|
|
|
wbuf->offs += n;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
aligned_len -= n;
|
ubifs: Fix read out-of-bounds in ubifs_wbuf_write_nolock()
Function ubifs_wbuf_write_nolock() may access buf out of bounds in
following process:
ubifs_wbuf_write_nolock():
aligned_len = ALIGN(len, 8); // Assume len = 4089, aligned_len = 4096
if (aligned_len <= wbuf->avail) ... // Not satisfy
if (wbuf->used) {
ubifs_leb_write() // Fill some data in avail wbuf
len -= wbuf->avail; // len is still not 8-bytes aligned
aligned_len -= wbuf->avail;
}
n = aligned_len >> c->max_write_shift;
if (n) {
n <<= c->max_write_shift;
err = ubifs_leb_write(c, wbuf->lnum, buf + written,
wbuf->offs, n);
// n > len, read out of bounds less than 8(n-len) bytes
}
, which can be catched by KASAN:
=========================================================
BUG: KASAN: slab-out-of-bounds in ecc_sw_hamming_calculate+0x1dc/0x7d0
Read of size 4 at addr ffff888105594ff8 by task kworker/u8:4/128
Workqueue: writeback wb_workfn (flush-ubifs_0_0)
Call Trace:
kasan_report.cold+0x81/0x165
nand_write_page_swecc+0xa9/0x160
ubifs_leb_write+0xf2/0x1b0 [ubifs]
ubifs_wbuf_write_nolock+0x421/0x12c0 [ubifs]
write_head+0xdc/0x1c0 [ubifs]
ubifs_jnl_write_inode+0x627/0x960 [ubifs]
wb_workfn+0x8af/0xb80
Function ubifs_wbuf_write_nolock() accepts that parameter 'len' is not 8
bytes aligned, the 'len' represents the true length of buf (which is
allocated in 'ubifs_jnl_xxx', eg. ubifs_jnl_write_inode), so
ubifs_wbuf_write_nolock() must handle the length read from 'buf' carefully
to write leb safely.
Fetch a reproducer in [Link].
Fixes: 1e51764a3c2ac0 ("UBIFS: add new flash file system")
Link: https://bugzilla.kernel.org/show_bug.cgi?id=214785
Reported-by: Chengsong Ke <kechengsong@huawei.com>
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Richard Weinberger <richard@nod.at>
2021-12-27 03:22:40 +00:00
|
|
|
|
len -= min(len, n);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
written += n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spin_lock(&wbuf->lock);
|
2020-11-16 21:05:30 +00:00
|
|
|
|
if (aligned_len) {
|
2008-07-14 16:08:37 +00:00
|
|
|
|
/*
|
|
|
|
|
* And now we have what's left and what does not take whole
|
2011-02-06 12:45:26 +00:00
|
|
|
|
* max. write unit, so write it to the write-buffer and we are
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* done.
|
|
|
|
|
*/
|
|
|
|
|
memcpy(wbuf->buf, buf + written, len);
|
2020-11-16 21:05:30 +00:00
|
|
|
|
if (aligned_len > len) {
|
|
|
|
|
ubifs_assert(c, aligned_len - len < 8);
|
|
|
|
|
ubifs_pad(c, wbuf->buf + len, aligned_len - len);
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
2011-02-06 12:45:26 +00:00
|
|
|
|
if (c->leb_size - wbuf->offs >= c->max_write_size)
|
|
|
|
|
wbuf->size = c->max_write_size;
|
|
|
|
|
else
|
|
|
|
|
wbuf->size = c->leb_size - wbuf->offs;
|
|
|
|
|
wbuf->avail = wbuf->size - aligned_len;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
wbuf->used = aligned_len;
|
|
|
|
|
wbuf->next_ino = 0;
|
|
|
|
|
spin_unlock(&wbuf->lock);
|
|
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
if (wbuf->sync_callback) {
|
|
|
|
|
int free = c->leb_size - wbuf->offs - wbuf->used;
|
|
|
|
|
|
|
|
|
|
err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (wbuf->used)
|
2018-07-12 11:01:57 +00:00
|
|
|
|
new_wbuf_timer_nolock(c, wbuf);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out:
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "cannot write %d bytes to LEB %d:%d, error %d",
|
2008-07-14 16:08:37 +00:00
|
|
|
|
len, wbuf->lnum, wbuf->offs, err);
|
2020-06-16 07:11:44 +00:00
|
|
|
|
ubifs_dump_node(c, buf, written + len);
|
2012-05-16 16:04:54 +00:00
|
|
|
|
dump_stack();
|
2012-05-16 16:15:56 +00:00
|
|
|
|
ubifs_dump_leb(c, wbuf->lnum);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-09-07 12:36:33 +00:00
|
|
|
|
* ubifs_write_node_hmac - write node to the media.
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @buf: the node to write
|
|
|
|
|
* @len: node length
|
|
|
|
|
* @lnum: logical eraseblock number
|
|
|
|
|
* @offs: offset within the logical eraseblock
|
2018-09-07 12:36:33 +00:00
|
|
|
|
* @hmac_offs: offset of the HMAC within the node
|
2008-07-14 16:08:37 +00:00
|
|
|
|
*
|
|
|
|
|
* This function automatically fills node magic number, assigns sequence
|
|
|
|
|
* number, and calculates node CRC checksum. The length of the @buf buffer has
|
|
|
|
|
* to be aligned to the minimal I/O unit size. This function automatically
|
|
|
|
|
* appends padding node and padding bytes if needed. Returns zero in case of
|
|
|
|
|
* success and a negative error code in case of failure.
|
|
|
|
|
*/
|
2018-09-07 12:36:33 +00:00
|
|
|
|
int ubifs_write_node_hmac(struct ubifs_info *c, void *buf, int len, int lnum,
|
|
|
|
|
int offs, int hmac_offs)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
{
|
|
|
|
|
int err, buf_len = ALIGN(len, c->min_io_size);
|
|
|
|
|
|
|
|
|
|
dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
|
|
|
|
|
lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
|
|
|
|
|
buf_len);
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
|
|
|
|
|
ubifs_assert(c, offs % c->min_io_size == 0 && offs < c->leb_size);
|
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
|
|
|
|
ubifs_assert(c, !c->space_fixup);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
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 13:44:28 +00:00
|
|
|
|
if (c->ro_error)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return -EROFS;
|
|
|
|
|
|
2018-09-07 12:36:33 +00:00
|
|
|
|
err = ubifs_prepare_node_hmac(c, buf, len, hmac_offs, 1);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
2012-05-14 15:55:51 +00:00
|
|
|
|
err = ubifs_leb_write(c, lnum, buf, offs, buf_len);
|
2011-06-03 11:12:10 +00:00
|
|
|
|
if (err)
|
2020-06-16 07:11:44 +00:00
|
|
|
|
ubifs_dump_node(c, buf, len);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-07 12:36:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* ubifs_write_node - write node to the media.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @buf: the node to write
|
|
|
|
|
* @len: node length
|
|
|
|
|
* @lnum: logical eraseblock number
|
|
|
|
|
* @offs: offset within the logical eraseblock
|
|
|
|
|
*
|
|
|
|
|
* This function automatically fills node magic number, assigns sequence
|
|
|
|
|
* number, and calculates node CRC checksum. The length of the @buf buffer has
|
|
|
|
|
* to be aligned to the minimal I/O unit size. This function automatically
|
|
|
|
|
* appends padding node and padding bytes if needed. Returns zero in case of
|
|
|
|
|
* success and a negative error code in case of failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
|
|
|
|
|
int offs)
|
|
|
|
|
{
|
|
|
|
|
return ubifs_write_node_hmac(c, buf, len, lnum, offs, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-14 16:08:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* ubifs_read_node_wbuf - read node from the media or write-buffer.
|
|
|
|
|
* @wbuf: wbuf to check for un-written data
|
|
|
|
|
* @buf: buffer to read to
|
|
|
|
|
* @type: node type
|
|
|
|
|
* @len: node length
|
|
|
|
|
* @lnum: logical eraseblock number
|
|
|
|
|
* @offs: offset within the logical eraseblock
|
|
|
|
|
*
|
|
|
|
|
* This function reads a node of known type and length, checks it and stores
|
|
|
|
|
* in @buf. If the node partially or fully sits in the write-buffer, this
|
|
|
|
|
* function takes data from the buffer, otherwise it reads the flash media.
|
|
|
|
|
* Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
|
|
|
|
|
* error code in case of failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
|
|
|
|
|
int lnum, int offs)
|
|
|
|
|
{
|
|
|
|
|
const struct ubifs_info *c = wbuf->c;
|
|
|
|
|
int err, rlen, overlap;
|
|
|
|
|
struct ubifs_ch *ch = buf;
|
|
|
|
|
|
2009-09-15 12:03:51 +00:00
|
|
|
|
dbg_io("LEB %d:%d, %s, length %d, jhead %s", lnum, offs,
|
|
|
|
|
dbg_ntype(type), len, dbg_jhead(wbuf->jhead));
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
|
|
|
|
|
ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
|
|
|
|
|
ubifs_assert(c, type >= 0 && type < UBIFS_NODE_TYPES_CNT);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
spin_lock(&wbuf->lock);
|
|
|
|
|
overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
|
|
|
|
|
if (!overlap) {
|
|
|
|
|
/* We may safely unlock the write-buffer and read the data */
|
|
|
|
|
spin_unlock(&wbuf->lock);
|
|
|
|
|
return ubifs_read_node(c, buf, type, len, lnum, offs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Don't read under wbuf */
|
|
|
|
|
rlen = wbuf->offs - offs;
|
|
|
|
|
if (rlen < 0)
|
|
|
|
|
rlen = 0;
|
|
|
|
|
|
|
|
|
|
/* Copy the rest from the write-buffer */
|
|
|
|
|
memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
|
|
|
|
|
spin_unlock(&wbuf->lock);
|
|
|
|
|
|
|
|
|
|
if (rlen > 0) {
|
|
|
|
|
/* Read everything that goes before write-buffer */
|
2011-06-03 11:03:25 +00:00
|
|
|
|
err = ubifs_leb_read(c, lnum, buf, offs, rlen, 0);
|
|
|
|
|
if (err && err != -EBADMSG)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type != ch->node_type) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "bad node type (%d but expected %d)",
|
2008-07-14 16:08:37 +00:00
|
|
|
|
ch->node_type, type);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 07:11:44 +00:00
|
|
|
|
err = ubifs_check_node(c, buf, len, lnum, offs, 0, 0);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
if (err) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "expected node type %d", type);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rlen = le32_to_cpu(ch->len);
|
|
|
|
|
if (rlen != len) {
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "bad node length %d, expected %d", rlen, len);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out:
|
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 10:39:42 +00:00
|
|
|
|
ubifs_err(c, "bad node at LEB %d:%d", lnum, offs);
|
2020-06-16 07:11:44 +00:00
|
|
|
|
ubifs_dump_node(c, buf, len);
|
2012-05-16 16:04:54 +00:00
|
|
|
|
dump_stack();
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_read_node - read node.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @buf: buffer to read to
|
|
|
|
|
* @type: node type
|
|
|
|
|
* @len: node length (not aligned)
|
|
|
|
|
* @lnum: logical eraseblock number
|
|
|
|
|
* @offs: offset within the logical eraseblock
|
|
|
|
|
*
|
2020-08-05 02:49:35 +00:00
|
|
|
|
* This function reads a node of known type and length, checks it and
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
|
|
|
|
|
* and a negative error code in case of failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
|
|
|
|
|
int lnum, int offs)
|
|
|
|
|
{
|
|
|
|
|
int err, l;
|
|
|
|
|
struct ubifs_ch *ch = buf;
|
|
|
|
|
|
|
|
|
|
dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
|
2018-07-12 11:01:57 +00:00
|
|
|
|
ubifs_assert(c, lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
|
|
|
|
|
ubifs_assert(c, len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
|
|
|
|
|
ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
|
|
|
|
|
ubifs_assert(c, type >= 0 && type < UBIFS_NODE_TYPES_CNT);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
|
2011-06-03 11:03:25 +00:00
|
|
|
|
err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
|
|
|
|
|
if (err && err != -EBADMSG)
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
if (type != ch->node_type) {
|
2014-06-02 13:51:10 +00:00
|
|
|
|
ubifs_errc(c, "bad node type (%d but expected %d)",
|
|
|
|
|
ch->node_type, type);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 07:11:44 +00:00
|
|
|
|
err = ubifs_check_node(c, buf, len, lnum, offs, 0, 0);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
if (err) {
|
2014-06-02 13:51:10 +00:00
|
|
|
|
ubifs_errc(c, "expected node type %d", type);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l = le32_to_cpu(ch->len);
|
|
|
|
|
if (l != len) {
|
2014-06-02 13:51:10 +00:00
|
|
|
|
ubifs_errc(c, "bad node length %d, expected %d", l, len);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out:
|
2014-06-02 13:51:10 +00:00
|
|
|
|
ubifs_errc(c, "bad node at LEB %d:%d, LEB mapping status %d", lnum,
|
|
|
|
|
offs, ubi_is_mapped(c->ubi, lnum));
|
|
|
|
|
if (!c->probing) {
|
2020-06-16 07:11:44 +00:00
|
|
|
|
ubifs_dump_node(c, buf, len);
|
2014-06-02 13:51:10 +00:00
|
|
|
|
dump_stack();
|
|
|
|
|
}
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_wbuf_init - initialize write-buffer.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @wbuf: write-buffer to initialize
|
|
|
|
|
*
|
2009-06-23 17:30:32 +00:00
|
|
|
|
* This function initializes write-buffer. Returns zero in case of success
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* %-ENOMEM in case of failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
|
|
|
|
|
{
|
|
|
|
|
size_t size;
|
|
|
|
|
|
2011-02-06 12:45:26 +00:00
|
|
|
|
wbuf->buf = kmalloc(c->max_write_size, GFP_KERNEL);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
if (!wbuf->buf)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
2011-02-06 12:45:26 +00:00
|
|
|
|
size = (c->max_write_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
|
2008-07-14 16:08:37 +00:00
|
|
|
|
wbuf->inodes = kmalloc(size, GFP_KERNEL);
|
|
|
|
|
if (!wbuf->inodes) {
|
|
|
|
|
kfree(wbuf->buf);
|
|
|
|
|
wbuf->buf = NULL;
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wbuf->used = 0;
|
|
|
|
|
wbuf->lnum = wbuf->offs = -1;
|
2011-02-06 12:45:26 +00:00
|
|
|
|
/*
|
|
|
|
|
* If the LEB starts at the max. write size aligned address, then
|
|
|
|
|
* write-buffer size has to be set to @c->max_write_size. Otherwise,
|
|
|
|
|
* set it to something smaller so that it ends at the closest max.
|
|
|
|
|
* write size boundary.
|
|
|
|
|
*/
|
|
|
|
|
size = c->max_write_size - (c->leb_start % c->max_write_size);
|
|
|
|
|
wbuf->avail = wbuf->size = size;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
wbuf->sync_callback = NULL;
|
|
|
|
|
mutex_init(&wbuf->io_mutex);
|
|
|
|
|
spin_lock_init(&wbuf->lock);
|
|
|
|
|
wbuf->c = c;
|
|
|
|
|
wbuf->next_ino = 0;
|
|
|
|
|
|
2009-05-28 13:24:15 +00:00
|
|
|
|
hrtimer_init(&wbuf->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
|
|
|
|
|
wbuf->timer.function = wbuf_timer_callback_nolock;
|
2008-07-14 16:08:37 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
|
2009-06-23 17:30:32 +00:00
|
|
|
|
* @wbuf: the write-buffer where to add
|
2008-07-14 16:08:37 +00:00
|
|
|
|
* @inum: the inode number
|
|
|
|
|
*
|
|
|
|
|
* This function adds an inode number to the inode array of the write-buffer.
|
|
|
|
|
*/
|
|
|
|
|
void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
|
|
|
|
|
{
|
|
|
|
|
if (!wbuf->buf)
|
|
|
|
|
/* NOR flash or something similar */
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
spin_lock(&wbuf->lock);
|
|
|
|
|
if (wbuf->used)
|
|
|
|
|
wbuf->inodes[wbuf->next_ino++] = inum;
|
|
|
|
|
spin_unlock(&wbuf->lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* wbuf_has_ino - returns if the wbuf contains data from the inode.
|
|
|
|
|
* @wbuf: the write-buffer
|
|
|
|
|
* @inum: the inode number
|
|
|
|
|
*
|
|
|
|
|
* This function returns with %1 if the write-buffer contains some data from the
|
|
|
|
|
* given inode otherwise it returns with %0.
|
|
|
|
|
*/
|
|
|
|
|
static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
|
|
|
|
|
{
|
|
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
|
|
spin_lock(&wbuf->lock);
|
|
|
|
|
for (i = 0; i < wbuf->next_ino; i++)
|
|
|
|
|
if (inum == wbuf->inodes[i]) {
|
|
|
|
|
ret = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
spin_unlock(&wbuf->lock);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @inode: inode to synchronize
|
|
|
|
|
*
|
|
|
|
|
* This function synchronizes write-buffers which contain nodes belonging to
|
|
|
|
|
* @inode. Returns zero in case of success and a negative error code in case of
|
|
|
|
|
* failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
|
|
|
|
|
{
|
|
|
|
|
int i, err = 0;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < c->jhead_cnt; i++) {
|
|
|
|
|
struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
|
|
|
|
|
|
|
|
|
|
if (i == GCHD)
|
|
|
|
|
/*
|
|
|
|
|
* GC head is special, do not look at it. Even if the
|
|
|
|
|
* head contains something related to this inode, it is
|
|
|
|
|
* a _copy_ of corresponding on-flash node which sits
|
|
|
|
|
* somewhere else.
|
|
|
|
|
*/
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!wbuf_has_ino(wbuf, inode->i_ino))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
|
|
|
|
|
if (wbuf_has_ino(wbuf, inode->i_ino))
|
|
|
|
|
err = ubifs_wbuf_sync_nolock(wbuf);
|
|
|
|
|
mutex_unlock(&wbuf->io_mutex);
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
ubifs_ro_mode(c, err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|