From: Artem Bityutskiy <dedekind1@gmail.com>
To: "Albrecht Dreß" <albrecht.dress@arcor.de>
Cc: Sven <svenjaborek@gmx.de>, linux-mtd@lists.infradead.org
Subject: Re: JFFS2: truncated files after power loss scenario
Date: Fri, 11 Feb 2011 16:00:41 +0200 [thread overview]
Message-ID: <1297432841.2760.23.camel@localhost> (raw)
In-Reply-To: <1297017610.1854.3@antares>
Hi,
sorry for delay, I was very busy.
On Sun, 2011-02-06 at 19:40 +0100, Albrecht Dreß wrote:
> Unfortunately, they are more more about ubi & ext4, but I am in
> particular interested in JFFS2's behaviour, so please excuse my
> questions, but it's a really critical point...
OK, but you do realize that proper portable applications should not rely
on JFFS2-specific behavior.
Also, if this is crucial for you, you should not generally rely on the
answer of a random mailing list guy like me, of course :-)
> Currently, I use the following sequence to write my configs to jffs2
> (both NOR flash and mtd-ram):
>
> open(scratch, O_RDWR | O_CREAT | O_TRUNC, perms);
OK, if scratch existed, at this point you truncate it. The change goes
to the JFFS2 write buffer. And the write-buffer is flushed by time-out,
or if some other app wrote anything else to the file-system, of if
someone issued sync.
So if power cut happens, you may either end up with intact "scratch" or
with "scratch" truncated to zero.
If you do not want it, you have to first copy "scratch" to
"scratch_saved" and 'fsync()' "scratch_saved".
> write();
OK, you write to "scratch". What is your flash? NAND, NOR? In case of
typical 2KiB per page NAND, you can lose the last 0-8KiB of written data
in the worst case. Not sure how much, depends, and I do not remember the
details well enough.
> fsync();
OK, if you get a power cut now, you are safe, you'll have modified
"scratch".
> close();
Not necessary to do, but does not hurt. 'fsync()' was enough.
> sync(); // #1
The same, not needed. You fsync()'ed "scratch" already.
> rename(scratch, realname);
Now if you have a power cut, you'll either have "scratch" or "realname"
with correct data.
> sync(); // #2
You can just do "fsync(realname)" instead. In case of JFFS2 there is not
big difference, but if you ever migrate to another FS, there will be big
difference.
> As far as I understand the information you cited, this should be safe
> under all circumstances for jffs2. Is that assumption correct?
Not sure what is your definition of "safe". Step 1 does truncation, I've
explained possible outcome of this above.
> As calling sync() might be a lot more expensive than fsync() if other
> file systems are used simultaneously (I have a vfat fs on a CF card
> open), my question is now if I can safely omit either sync() #1 or #2
> or both?
You do not need 'sync()' #1 at all. And 'sync()' #2 may be turned into
'fsync(realname)'.
> When reading, I first try realname, and if that fails scratch, as to
> cover the case that rename() was killed in mid-air so realname has
> already been removed from the directory, but scratch is still there.
> Can this case happen at all for jffs2?
I think this can happen, AFAIR, rename will just do the change and put
it to the write-buffer, or you can have a power cut while doing re-name.
But you are not safe at the open(O_TRUNC) and write() stages.
I explained the steps of the reliable file (e.g., config file) update
here:
http://www.linux-mtd.infradead.org/faq/ubifs.html#L_atomic_change
You can read about UBIFS write-buffer here (JFFS2 write-buffer is almost
the same, but there is only one write-buffer in JFFS2):
http://www.linux-mtd.infradead.org/doc/ubifs.html#L_writebuffer
I mean, the concept is the same, we borrowed it from JFFS2.
--
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
prev parent reply other threads:[~2011-02-11 14:01 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-06 14:59 JFFS2: truncated files after power loss scenario Sven
2011-02-06 15:25 ` Artem Bityutskiy
2011-02-06 15:47 ` Sven
2011-02-06 16:03 ` Artem Bityutskiy
2011-02-06 17:12 ` Albrecht Dreß
2011-02-06 17:39 ` Artem Bityutskiy
2011-02-06 18:40 ` Albrecht Dreß
2011-02-11 14:00 ` Artem Bityutskiy [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1297432841.2760.23.camel@localhost \
--to=dedekind1@gmail.com \
--cc=albrecht.dress@arcor.de \
--cc=linux-mtd@lists.infradead.org \
--cc=svenjaborek@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox