0

Let my filesystem be ext4 mounted with data=journal. I don't fully understand what happens during power loss with filesystem in this case. So I have the following questions:

  • Under which circumstances the data that already stored on the hard drive (no matter SSD or HDD) may be damaged (changed) at power loss?
  • Under which circumstances the data being written at power loss will be inconsistent? (I mean something will be written but not equal to initial data)
  • What happens when power loss occurs during journal replaying at fschk?
0

I believe this question is not specific to Linux or ext4, but to any filesystem in general.

A powerloss will not destroy any data unless there is a file being written to the disk.

This basically mean, when a program opens a file, it can do so in many different ways.

It can do so to just read from it, it can do so to write to it from the start (overwrite everything) and to append to the file (write at the end of the file).

Now, the trick is that a file has 2 segments. These are:

  • The table of content,
  • The data.

At the start of the harddisk, a table is written which holds the location(s) of a file on the harddisk. At the specified location(s) the data is written as 0's and 1's on the harddisk.

Reading a file back is obviously first getting its location, then from the actual location, get the amount of 0's and 1's to form the actual file in memory.

When the harddisk is writing to the file, it does so by first writing the data to disk, see what the new start and finish points are (the start usually is the same, but in case the data was fragmented and a 2nd block was used, its start and finish points are recorded) and those are written to the partition table.

If the power fails during writing to the file, the index is obviously not consistent with the data. This creates the phenomena of reaching a read error when trying to access the file. You see its there, but still you cannot access it.

Since reading to a file does not actually alter the content of the disk, a powerloss won't have any affect here.

  • AFAIU, When journaling filesystems are used, operating system firstly write to the filesystem journal (write-ahead log) something like the following: "Transaction begin" "I am going to write first segment X", "I am going to write second segment Y", "Transaction end", and secondly do actual writing. Then if power loss occurred during journal writting, then incomplete transaction can be fully dropped. If power loss occurred at file writting, we can replay journal and write missed parts from the journal. Am I right? In this case we either have full file or don't have a file at all. – 0x2207 Jan 10 '16 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.