Skip to contentSkip to conversation

Why the alignemt to 4 bytes is necessary in jffs2_commit_write function

DISCUSSION akorolev 2 participants 3 messages Jul 11, 2007 Memory technology devices (MTD)

lore
Share & tools
lore

akorolev

author
#0 lore raw

Hi All

I'm currently working on JFFS2 improving. I faced some issues with understanding why we need to align start offset to 4 bytes in jffs2_commit_write function? Here is the part of this code:
=========================

unsigned aligned_start = start & ~3;
.........................................
/* Adjust writtenlen for the padding we did, so we don't confuse our
caller */
if (writtenlen < (start&3))

       writtenlen = 0;
   else
       writtenlen -= (start&3);

===========================
What does the padding mean here?
If I properly understand the aligned_start is not related to offset of the fragment it is just offset within the file. (Frag offset is aligned) File offset is not necessary to keep aligned - so why not to kick off it. (I tried and didn't find anything wrong in tests).

Thanks,
Alexey

#1 lore raw
Quoted 4 lines · akorolev

If I properly understand the aligned_start is not related to offset of the fragment it is just offset within the file. (Frag offset is aligned) File offset is not necessary to keep aligned - so why not to kick off it. (I tried and didn't find anything wrong in tests).

Consider:

	while (1)
		write(fd, "x", 1);

This is fairly much worst-case behaviour for JFFS2, and you end up with a _large_ number of data nodes. One thing we've done to improve behaviour in this situation is rewrite the entire page when we write the last byte in a page -- but that doesn't fix _all_ cases.

Since data nodes _are_ aligned to 4 bytes, it doesn't hurt us at all to write a full 4 bytes instead of only 1 byte. So you'll have a maximum of 1024 data nodes for a given 4KiB page even if you don't manage to trigger the code which rewrites the page.

Signature · 1 line
dwmw2
author
#2 lore raw

Aghh I see.
Thank you very much!

Quoted 23 lines · akorolev
Quoted 6 lines

If I properly understand the aligned_start is not related to offset of the fragment it is just offset within the file. (Frag offset is aligned) File offset is not necessary to keep aligned - so why not to kick off it. (I tried and didn't find anything wrong in tests).

Consider:

	while (1)
		write(fd, "x", 1);

This is fairly much worst-case behaviour for JFFS2, and you end up with a _large_ number of data nodes. One thing we've done to improve behaviour in this situation is rewrite the entire page when we write the last byte in a page -- but that doesn't fix _all_ cases.

Since data nodes _are_ aligned to 4 bytes, it doesn't hurt us at all to write a full 4 bytes instead of only 1 byte. So you'll have a maximum of 1024 data nodes for a given 4KiB page even if you don't manage to trigger the code which rewrites the page.