kernel-ark/fs/ubifs
Artem Bityutskiy 052c28073f UBIFS: fix a race condition
Hu (hujianyang@huawei.com) discovered a race condition which may lead to a
situation when UBIFS is unable to mount the file-system after an unclean
reboot. The problem is theoretical, though.

In UBIFS, we have the log, which basically a set of LEBs in a certain area. The
log has the tail and the head.

Every time user writes data to the file-system, the UBIFS journal grows, and
the log grows as well, because we append new reference nodes to the head of the
log. So the head moves forward all the time, while the log tail stays at the
same position.

At any time, the UBIFS master node points to the tail of the log. When we mount
the file-system, we scan the log, and we always start from its tail, because
this is where the master node points to. The only occasion when the tail of the
log changes is the commit operation.

The commit operation has 2 phases - "commit start" and "commit end". The former
is relatively short, and does not involve much I/O. During this phase we mostly
just build various in-memory lists of the things which have to be written to
the flash media during "commit end" phase.

During the commit start phase, what we do is we "clean" the log. Indeed, the
commit operation will index all the data in the journal, so the entire journal
"disappears", and therefore the data in the log become unneeded. So we just
move the head of the log to the next LEB, and write the CS node there. This LEB
will be the tail of the new log when the commit operation finishes.

When the "commit start" phase finishes, users may write more data to the
file-system, in parallel with the ongoing "commit end" operation. At this point
the log tail was not changed yet, it is the same as it had been before we
started the commit. The log head keeps moving forward, though.

The commit operation now needs to write the new master node, and the new master
node should point to the new log tail. After this the LEBs between the old log
tail and the new log tail can be unmapped and re-used again.

And here is the possible problem. We do 2 operations: (a) We first update the
log tail position in memory (see 'ubifs_log_end_commit()'). (b) And then we
write the master node (see the big lock of code in 'do_commit()').

But nothing prevents the log head from moving forward between (a) and (b), and
the log head may "wrap" now to the old log tail. And when the "wrap" happens,
the contends of the log tail gets erased. Now a power cut happens and we are in
trouble. We end up with the old master node pointing to the old tail, which was
erased. And replay fails because it expects the master node to point to the
correct log tail at all times.

This patch merges the abovementioned (a) and (b) operations by moving the master
node change code to the 'ubifs_log_end_commit()' function, so that it runs with
the log mutex locked, which will prevent the log from being changed benween
operations (a) and (b).

Cc: stable@vger.kernel.org # 07e19df UBIFS: remove mst_mutex
Cc: stable@vger.kernel.org
Reported-by: hujianyang <hujianyang@huawei.com>
Tested-by: hujianyang <hujianyang@huawei.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
2014-09-08 15:55:02 +03:00
..
budget.c UBIFS: Remove unused variables in ubifs_budget_space 2014-05-13 13:45:16 +03:00
commit.c UBIFS: fix a race condition 2014-09-08 15:55:02 +03:00
compress.c UBIFS: comply with coding style 2012-08-31 17:32:57 +03:00
debug.c UBIFS: Fix dump messages in ubifs_dump_lprops 2014-05-27 15:05:23 +03:00
debug.h UBIFS: print less 2012-08-31 17:32:58 +03:00
dir.c ubifs: switch to %pd 2013-10-24 23:34:51 -04:00
file.c Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs 2014-06-12 10:30:18 -07:00
find.c UBIFS: fix mounting problems after power cuts 2012-10-26 16:26:44 +03:00
gc.c UBIFS: remove unnecessary code in ubifs_garbage_collect 2013-10-22 13:34:27 +01:00
io.c UBIFS: kernel-doc warning fix 2014-07-19 09:53:52 +03:00
ioctl.c new helper: file_inode(file) 2013-02-22 23:31:31 -05:00
journal.c ubifs: switch to %pd 2013-10-24 23:34:51 -04:00
Kconfig UBIFS: remove Kconfig debugging option 2012-05-16 19:53:46 +03:00
key.h UBIFS: mark unused key objects as invalid 2010-08-30 10:19:08 +03:00
log.c UBIFS: fix a race condition 2014-09-08 15:55:02 +03:00
lprops.c UBIFS: introduce categorized lprops counter 2012-10-26 16:00:26 +03:00
lpt_commit.c UBIFS: remove useless statements 2014-07-19 09:53:51 +03:00
lpt.c UBIFS: remove useless statements 2014-07-19 09:53:51 +03:00
Makefile UBIFS: remove Kconfig debugging option 2012-05-16 19:53:46 +03:00
master.c UBIFS: remove mst_mutex 2014-07-19 09:53:52 +03:00
misc.h Revert "UBIFS: add a log overlap assertion" 2014-07-28 19:15:19 +03:00
orphan.c UBIFS: remove useless statements 2014-07-19 09:53:51 +03:00
recovery.c UBIFS: kernel-doc warning fix 2014-07-19 09:53:52 +03:00
replay.c UBIFS: print less 2012-08-31 17:32:58 +03:00
sb.c UBIFS: fix error path in create_default_filesystem() 2014-07-19 09:53:52 +03:00
scan.c UBIFS: fix spelling of "scanned" 2014-07-19 09:53:51 +03:00
shrinker.c UBIFS: Remove incorrect assertion in shrink_tnc() 2014-06-02 11:28:24 +03:00
super.c UBIFS: remove mst_mutex 2014-07-19 09:53:52 +03:00
tnc_commit.c UBIFS: remove useless statements 2014-07-19 09:53:51 +03:00
tnc_misc.c UBIFS: print less 2012-08-31 17:32:58 +03:00
tnc.c UBIFS: remove useless statements 2014-07-19 09:53:51 +03:00
ubifs-media.h UBIFS: add a superblock flag for free space fix-up 2011-05-16 14:12:14 +03:00
ubifs.h UBIFS: remove mst_mutex 2014-07-19 09:53:52 +03:00
xattr.c ubifs: switch to %pd 2013-10-24 23:34:51 -04:00