spitfire88
spitfire88

Reputation: 1624

Difference between reading from /dev/block/mmcblk0 and /dev/block/mmcblk0p1

Reading from /dev/block/mmcblk0 returned old data while reading from /dev/block/mmcblk0p1 gave the latest data. My question is does Linux maintain a backup if data is written to /dev/block/mmcblk0? This is because I was able to read old contents of the SD card by reading through that node.

Upvotes: 14

Views: 52124

Answers (2)

Tino
Tino

Reputation: 10489

Linux does not maintain a backup. It maintains a cache. This has nothing to do with the particular block device or driver (/dev/mmcblk* in your case), instead it has something to do how the block cache works.

Hence your observation is normal, but dangerous. (Normally this is not a problem, because only root can find this "feature".)

In Linux each block device is handled independently in the kernel when it comes to caching. As the raw device (in your case /dev/mmcblk0) and the partition (dev/mmcblk0p1) are different block devices, both have independent caches!

If then the partition (/dev/mmcblk0p1) gets updated, the cache of the partition is updated, of course, but the cache of the raw device is not updated at all, hence it becomes stale (from the mapped data, the cache instance still is considered fresh).

If you then re-access the raw device again, the still (stale) cached data may be returned, as long as the cache is not flushed.

This is true in the opposite direction, like when updating the partition data through the raw device. This latter usually kills the filesystem on the partition!

If you want to get rid of the cache, you need to flush all caches before re-accessing the drive. This has two effects:

  • Flushing also syncs the data on the disk, so if forces out dirty data in the cache to the filesystem (aka. partition).

  • Flushing also gets rid of old cached data on the raw block device.

Flushing is done with:

echo 3 >/proc/sys/vm/drop_caches

But beware. Active partitions still may change quickly afterwards. And you cannot read dirty data of the partition cache from the raw device and vice versa.

Upvotes: 2

TheCodeArtist
TheCodeArtist

Reputation: 22497

The mmc sub-system in the Linux kernel registers device nodes of the format mmcblkXpY.

  • Each mmc device registered within the Linux kernel receives it own mmc device number X.
  • Each partition on a particular device receives it own number Y

Normal file I/O can be performed after mounting a device node pointing to a partition.

Also note that unless a valid partition table is present on the /dev/mmcblkX device, there will be no subsequent /dev/mmcblkXpY nodes on the system.

Upvotes: 19

Related Questions