Skip to contentSkip to conversation

libblkid: Idea to force given cached entry to be invalidated?

DISCUSSION Qu Wenruo 3 participants 10 messages Apr 15, 2014 – Apr 18, 2014 Util-Linux package

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

Hi,

When using the blkid_get_cache(), things will be cached to reduce the probe frequency.

But filesystem which supports device management like btrfs can remove device and
if fast enough, libblkid will not invalidate the old cache and cause to get the wrong filesystem type.

The bug is describe in the following btrfs patch:
https://patchwork.kernel.org/patch/3960191/

Though the above patch try to avoid the whole cache, it's not good enough since it break the pricible not to probe the devices too frequently.

After some searching, it seems that libblkid does not provide such API, or am I missing something?

So is there any idea about forcing given cached entry invalidated?

Thanks,
Qu

#1 lore raw
Quoted 7 lines · Qu Wenruo

Though the above patch try to avoid the whole cache, it's not good enough since it break the pricible not
to probe the devices too frequently.

After some searching, it seems that libblkid does not provide such API, or am I missing something?

The blkid_verify() function is designed to make sure the information in the cache corresponds with reality. If it does not, it will update the cache to match with reality, so it will do what you want.

Using blkid_verify() will involve accessing the disk involved, but that's still better than the alternative of scanning all of the disks in the system.

Cheers,

- Ted

author
#2 lore raw

-------- Original Message --------
Subject: Re: libblkid: Idea to force given cached entry to be invalidated?
From: Theodore Ts'o <[email protected]>
To: Qu Wenruo <[email protected]>
Date: 2014年04月15日 12:27

Quoted 15 lines · Qu Wenruo
Quoted 6 lines

Though the above patch try to avoid the whole cache, it's not good enough since it break the pricible not
to probe the devices too frequently.

After some searching, it seems that libblkid does not provide such API, or am I missing something?

The blkid_verify() function is designed to make sure the information in the cache corresponds with reality. If it does not, it will update the cache to match with reality, so it will do what you want.

Using blkid_verify() will involve accessing the disk involved, but that's still better than the alternative of scanning all of the disks in the system.

Quoted 7 lines

Cheers,

- Ted
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

I hope I was wrong but according to the codes, blkid_verify() seems still using the cache:
------
blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
{
......

            (diff < BLKID_PROBE_MIN ||
                (dev->bid_flags & BLKID_BID_FL_VERIFIED &&
                 diff < BLKID_PROBE_INTERVAL)))
                return dev;

------

So it seems not working in the case.

Also in the btrfs-progs codes, it has already called blkid_verify() before operations.

Thanks,
Qu.

maintainer
#3 lore raw
In reply to Qu Wenruo
Quoted 2 lines · Qu Wenruo

When using the blkid_get_cache(), things will be cached to reduce the probe frequency.

The question is if you really want to use the cache at all :-)

It's pretty common that we have udevd that maintain all necessary information about devices, including information from libblkid. The udev db is the primary source for many system actions and tools.

All you need is to use libudev udev_enumerate API to scan for all block devices and udev_device_get_property_value(ID_FS_TYPE). Maybe you can add a new method BTRFS_SCAN_UDEV and use it as default. It would be better to keep libblkid as a fallback solution only. (For very unusual situations where there is no udev or so.)

Quoted 3 lines

But filesystem which supports device management like btrfs can remove device and if fast enough, libblkid will not invalidate the old cache and cause to get the wrong filesystem type.

Well, since commit 6c2f2b9d (year 2010, v2.18), the libblkid uses microsecond resolution to check for device modifications.

So the problem is not that btrfs is fast enough ;-)

Quoted 2 lines

The bug is describe in the following btrfs patch:
https://patchwork.kernel.org/patch/3960191/

The patch does not make sense at all, blkid without a cache scans /proc/partitions -- so it's almost the same like BTRFS_SCAN_PROC btrfs_scan_block_devices().

Quoted 1 line

So is there any idea about forcing given cached entry invalidated?

It seems you're trying workaround the *btrfs problem*. The command

btrfs device delete

does not open and modify the device! It uses btrfs ioctl and kernel does the changes to the device. The problem is that kernel does not update atime and mtime of the device. So the changes are completely invisible for libblkid (or another mtime based solutions). See my example below.

IMHO it's strange manner to modify files in kernel, but no update mtime.

Fortunately kernel at least sends event to udev.

Maybe possible workaround is to add utime() call to "btrfs device delete". Something like:

cmds-device.c +4 -1
index 309b49e..5c438bf 100644
@@ -20,6 +20,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
+#include <sys/types.h>
+#include <utime.h>
 #include <errno.h>
 #include <sys/stat.h>
 #include <getopt.h>
@@ -179,7 +181,8 @@ static int cmd_rm_dev(int argc, char **argv)
 				"ERROR: error removing the device '%s' - %s\n",
 				argv[i], strerror(e));
 			ret++;
-		}
+		} else
+			utime(argv[i], NULL);
 	}
 
 	close_file_or_dir(fdmnt, dirstream);

BTW, it would be nice to have --verbose option for btrfs-scan, now it seems like black box.

Karel

See mtime:

# stat /dev/sdb2
  File: ‘/dev/sdb2’
  Size: 0               Blocks: 0          IO Block: 4096   block special file
Device: 5h/5d   Inode: 392701      Links: 1     Device type: 8,12
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/    disk)
Access: 2014-04-15 11:58:58.338758155 +0200
Modify: 2014-04-15 11:58:58.338758155 +0200
Change: 2014-04-15 11:58:58.338758155 +0200
 Birth: -
# btrfs device add /dev/sdb2 /mnt/test
SMALL VOLUME: forcing mixed metadata/data groups
# stat /dev/sdb2
  File: ‘/dev/sdb2’
  Size: 0               Blocks: 0          IO Block: 4096   block special file
Device: 5h/5d   Inode: 392701      Links: 1     Device type: 8,12
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/    disk)
Access: 2014-04-15 11:59:34.930143021 +0200
Modify: 2014-04-15 11:59:34.930143021 +0200
Change: 2014-04-15 11:59:34.930143021 +0200
                            ^^^^^^^^
# btrfs device delete /dev/sdb2 /mnt/test
# stat /dev/sdb2
  File: ‘/dev/sdb2’
  Size: 0               Blocks: 0          IO Block: 4096   block special file
Device: 5h/5d   Inode: 392701      Links: 1     Device type: 8,12
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/    disk)
Access: 2014-04-15 11:59:34.930143021 +0200
Modify: 2014-04-15 11:59:34.930143021 +0200
Change: 2014-04-15 11:59:34.930143021 +0200
                            ^^^^^^^^^
Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
author
#4 lore raw

-------- Original Message --------
Subject: Re: libblkid: Idea to force given cached entry to be invalidated?
From: Karel Zak <[email protected]>
To: Qu Wenruo <[email protected]>
Date: 2014=E5=B9=B404=E6=9C=8815=E6=97=A5 19:21

Quoted 2 lines · Qu Wenruo
Quoted 1 line

When using the blkid_get_cache(), things will be cached to reduce the pr=

obe

Quoted 7 lines
Quoted 1 line

frequency.

The question is if you really want to use the cache at all :-)

It's pretty common that we have udevd that maintain all necessary information about devices, including information from libblkid. The udev db is the primary source for many system actions and tools.

Quoted 5 lines

All you need is to use libudev udev_enumerate API to scan for all block devices and udev_device_get_property_value(ID_FS_TYPE). Maybe you can add a new method BTRFS_SCAN_UDEV and use it as default. It would be better to keep libblkid as a fallback solution only. (For very unusual situations where there is no udev or so.)

Thanks for the suggestion to use libudev.
It sounds solid and I'll try to use it instead.

Quoted 2 lines
Quoted 2 lines

But filesystem which supports device management like btrfs can remove de=

vice

Quoted 1 line
Quoted 1 line

and if fast enough, libblkid will not invalidate the old cache and cause=

to get

Quoted 5 lines
Quoted 1 line

the wrong filesystem type.

Well, since commit 6c2f2b9d (year 2010, v2.18), the libblkid uses microsecond resolution to check for device modifications.

So the problem is not that btrfs is fast enough ;-)

According to your explaination, it seems that if there is any=20 modification to the block device, libblkid will rescan it and not to use the old cache, do I understand it right?

Quoted 11 lines
Quoted 3 lines

The bug is describe in the following btrfs patch:
https://patchwork.kernel.org/patch/3960191/

The patch does not make sense at all, blkid without a cache scans /proc/partitions -- so it's almost the same like BTRFS_SCAN_PROC btrfs_scan_block_devices().

Quoted 1 line

So is there any idea about forcing given cached entry invalidated?

It seems you're trying workaround the *btrfs problem*. The command

Quoted 7 lines

btrfs device delete

does not open and modify the device! It uses btrfs ioctl and kernel does the changes to the device. The problem is that kernel does not update atime and mtime of the device. So the changes are completely invisible for libblkid (or another mtime based solutions). See my example below.

Thanks for pointing out the problem a lot !

Quoted 8 lines

IMHO it's strange manner to modify files in kernel, but no update mtime.

Fortunately kernel at least sends event to udev.

Maybe possible workaround is to add utime() call to "btrfs device delete". Something like:

This sounds good, much better than my poor patch. I will also try to fix it in kernel codes if possible.

Quoted 26 lines
cmds-device.c +4 -1
index 309b49e..5c438bf 100644
@@ -20,6 +20,8 @@
  #include <unistd.h>
  #include <fcntl.h>
  #include <sys/ioctl.h>
+#include <sys/types.h>
+#include <utime.h>
  #include <errno.h>
  #include <sys/stat.h>
  #include <getopt.h>
@@ -179,7 +181,8 @@ static int cmd_rm_dev(int argc, char **argv)
  				"ERROR: error removing the device '%s' - %s\n",
  				argv[i], strerror(e));
  			ret++;
-		}
+		} else
+			utime(argv[i], NULL);
  	}
 =20
  	close_file_or_dir(fdmnt, dirstream);
Quoted 2 lines

BTW, it would be nice to have --verbose option for btrfs-scan, now it seems like black box.

I'd like to add it, but as you know, 'btrfs dev scan' just calls a btrfs=20 ioctl, so even '--verbose' is added,
nothing useful will be printed out. :(

Quoted 10 lines

Karel

See mtime:

# stat /dev/sdb2
   File: =E2=80=98/dev/sdb2=E2=80=99
   Size: 0               Blocks: 0          IO Block: 4096   block specia=

l file

Quoted 7 lines

Device: 5h/5d Inode: 392701 Links: 1 Device type: 8,12
Access: (0660/brw-rw----) Uid: ( 0/ root) Gid: ( 6/ disk)
Access: 2014-04-15 11:58:58.338758155 +0200
Modify: 2014-04-15 11:58:58.338758155 +0200
Change: 2014-04-15 11:58:58.338758155 +0200
Birth: -

Quoted 6 lines
# btrfs device add /dev/sdb2 /mnt/test
SMALL VOLUME: forcing mixed metadata/data groups
# stat /dev/sdb2
   File: =E2=80=98/dev/sdb2=E2=80=99
   Size: 0               Blocks: 0          IO Block: 4096   block specia=

l file

Quoted 5 lines

Device: 5h/5d Inode: 392701 Links: 1 Device type: 8,12
Access: (0660/brw-rw----) Uid: ( 0/ root) Gid: ( 6/ disk)
Access: 2014-04-15 11:59:34.930143021 +0200
Modify: 2014-04-15 11:59:34.930143021 +0200
Change: 2014-04-15 11:59:34.930143021 +0200

Quoted 7 lines
                             ^^^^^^^^
# btrfs device delete /dev/sdb2 /mnt/test
# stat /dev/sdb2
   File: =E2=80=98/dev/sdb2=E2=80=99
   Size: 0               Blocks: 0          IO Block: 4096   block specia=

l file

Quoted 8 lines

Device: 5h/5d Inode: 392701 Links: 1 Device type: 8,12
Access: (0660/brw-rw----) Uid: ( 0/ root) Gid: ( 6/ disk)
Access: 2014-04-15 11:59:34.930143021 +0200
Modify: 2014-04-15 11:59:34.930143021 +0200
Change: 2014-04-15 11:59:34.930143021 +0200

                             ^^^^^^^^^

Great thanks for all your advise! It helps a lot!!

Thanks,
Qu

maintainer
#5 lore raw
Quoted 4 lines · Qu Wenruo
Quoted 1 line

So the problem is not that btrfs is fast enough ;-)

According to your explaination, it seems that if there is any modification to the block device, libblkid will rescan it and not
to use the old cache, do I understand it right?

Yes, blkid_verify():

	if (now >= dev->bid_time &&
#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
	    (st.st_mtime < dev->bid_time ||
	        (st.st_mtime == dev->bid_time &&
		 st.st_mtim.tv_nsec / 1000 <= dev->bid_utime)) &&
#else
	    st.st_mtime <= dev->bid_time &&
#endif
	    (diff < BLKID_PROBE_MIN ||
		(dev->bid_flags & BLKID_BID_FL_VERIFIED &&
		 diff < BLKID_PROBE_INTERVAL)))
		return dev;

It checks two basic things:

a) compare device st_mtime with cache entry time (bid_time and bid_utime), the valid cache entry has not to be older than device mtime.

b) valid cache entry has not to be older than BLKID_PROBE_MIN (2 sec) and previous blkid_verify() validation remaining in memory has not to be older than BLKID_PROBE_INTERVAL (200s).

Quoted 5 lines
Quoted 2 lines

BTW, it would be nice to have --verbose option for btrfs-scan, now it seems like black box.

I'd like to add it, but as you know, 'btrfs dev scan' just calls a btrfs ioctl, so even '--verbose' is added,
nothing useful will be printed out. :(

Well, I mean at least info about for what devices it calls the ioctls.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
author
#6 lore raw

-------- Original Message --------
Subject: Re: libblkid: Idea to force given cached entry to be invalidated?
From: Karel Zak <[email protected]>
To: Qu Wenruo <[email protected]>
Date: 2014年04月16日 17:03

Quoted 30 lines · Qu Wenruo
Quoted 4 lines
Quoted 1 line

So the problem is not that btrfs is fast enough ;-)

According to your explaination, it seems that if there is any modification to the block device, libblkid will rescan it and not
to use the old cache, do I understand it right?

Yes, blkid_verify():

	if (now >= dev->bid_time &&
#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
	    (st.st_mtime < dev->bid_time ||
	        (st.st_mtime == dev->bid_time &&
		 st.st_mtim.tv_nsec / 1000 <= dev->bid_utime)) &&
#else
	    st.st_mtime <= dev->bid_time &&
#endif
	    (diff < BLKID_PROBE_MIN ||
		(dev->bid_flags & BLKID_BID_FL_VERIFIED &&
		 diff < BLKID_PROBE_INTERVAL)))
		return dev;

It checks two basic things:

a) compare device st_mtime with cache entry time (bid_time and bid_utime), the valid cache entry has not to be older than device mtime.

b) valid cache entry has not to be older than BLKID_PROBE_MIN (2 sec) and previous blkid_verify() validation remaining in memory has not to be older than BLKID_PROBE_INTERVAL (200s).

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

BTW, it would be nice to have --verbose option for btrfs-scan, now it seems like black box.

I'd like to add it, but as you know, 'btrfs dev scan' just calls a btrfs ioctl, so even '--verbose' is added,
nothing useful will be printed out. :(

Well, I mean at least info about for what devices it calls the ioctls.

Karel

Thanks for all your explaination.
Now I think I am somewhat clear of the libblkid cache now.

But after some tests, it seems that the ctime/mtime based cache seems not perfect due to the fact that multiple device file can be created in different fs with same major/minor number.
especially each of them has individual ctime/mtime timestamp.

So if someone (maybe insane) use mknod to create as block device file and use 'btrfs dev del' on the newly created block device file, libblkid will still be unable to detect the change since the ctime/mtime of block device file in /dev/ does not change.

Will libblkid be able to deal with it in later versions? (Although not related to my original problem now)

Thanks,
Qu

maintainer
#7 lore raw
Quoted 2 lines · Qu Wenruo

But after some tests, it seems that the ctime/mtime based cache seems not perfect due to the fact that multiple

Yes, the cache is not perfect -- it was originally introduced by Ted in time when things was less complicated :-) The current goal is to avoid the cache usage, so we have low-level API in libblkid to bypass the cache at all, and in userspace we use this API or udev db. I guess that many users have the cache empty, because it's unnecessary for fsck, mount, lsblk, findmnt, systemd etc.

Quoted 8 lines

device file can be created in different fs with same major/minor number. especially each of them has individual ctime/mtime timestamp.

So if someone (maybe insane) use mknod to create as block device file and use 'btrfs dev del' on the newly created
block device file, libblkid will still be unable to detect the change since the ctime/mtime of block device file in /dev/
does not change.

Not sure if I understand, it would be nice to have complete example.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
author
#8 lore raw

-------- Original Message --------
Subject: Re: libblkid: Idea to force given cached entry to be invalidated?
From: Karel Zak <[email protected]>
To: Qu Wenruo <[email protected]>
Date: 2014=E5=B9=B404=E6=9C=8817=E6=97=A5 16:21

Quoted 2 lines · Qu Wenruo
Quoted 1 line

But after some tests, it seems that the ctime/mtime based cache seems no=

t

Quoted 12 lines
Quoted 1 line

perfect due to the fact that multiple

Yes, the cache is not perfect -- it was originally introduced by Ted in time when things was less complicated :-) The current goal is to avoid the cache usage, so we have low-level API in libblkid to bypass the cache at all, and in userspace we use this API or udev db. I guess that many users have the cache empty, because it's unnecessary for fsck, mount, lsblk, findmnt, systemd etc.

Quoted 4 lines

device file can be created in different fs with same major/minor number. especially each of them has individual ctime/mtime timestamp.

So if someone (maybe insane) use mknod to create as block device file an=

d

Quoted 2 lines
Quoted 2 lines

use 'btrfs dev del' on the newly created
block device file, libblkid will still be unable to detect the change si=

nce

Quoted 6 lines
Quoted 2 lines

the ctime/mtime of block device file in /dev/
does not change.

Not sure if I understand, it would be nice to have complete example.

Karel

Sorry for my poor English.
Use /dev/sda6(major 8, minor 6) as an example.
------

# stat  /dev/sda6
  File: '/dev/sda6'
  Size: 0             Blocks: 0          IO Block: 4096   block special=20
file
Device: 5h/5d    Inode: 7289        Links: 1     Device type: 8,6
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/ disk)
Access: 2014-04-17 12:30:08.981732698 +0800
Modify: 2014-04-17 12:30:08.981732698 +0800
Change: 2014-04-17 12:30:08.981732698 +0800
                                     ^^^^^^^^^^^^^^^^^
 Birth: -
# mknod sda6 b 8 6
# mkfs.ext4 ./sda6 <<< Not /dev/sda6
......
# stat  /dev/sda6
  File: '/dev/sda6'
  Size: 0             Blocks: 0          IO Block: 4096   block special=20
file
Device: 5h/5d    Inode: 7289        Links: 1     Device type: 8,6
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/ disk)
Access: 2014-04-17 12:30:08.981732698 +0800
Modify: 2014-04-17 12:30:08.981732698 +0800
Change: 2014-04-17 12:30:08.981732698 +0800
                                       ^^^^^^^^^^^^^^^^ unchanged
 Birth: -
# stat  ./sda6
  File: './sda6'
  Size: 0             Blocks: 0          IO Block: 4096   block special=20
file
Device: 805h/2053d    Inode: 8889848     Links: 1     Device type: 8,6
Access: (0644/brw-r--r--)  Uid: (    0/    root)   Gid: (    0/ root)
Access: 2014-04-17 16:26:30.312439395 +0800
Modify: 2014-04-17 16:26:40.869106587 +0800
Change: 2014-04-17 16:26:40.869106587 +0800  <<< Only the created block=20
device file is updated
 Birth: -
------

Thanks,
Qu

maintainer
#9 lore raw
Quoted 12 lines · Qu Wenruo
Quoted 9 lines
Quoted 5 lines

So if someone (maybe insane) use mknod to create as block device file and use 'btrfs dev del' on the newly created
block device file, libblkid will still be unable to detect the change since the ctime/mtime of block device file in /dev/
does not change.

Not sure if I understand, it would be nice to have complete example.

Karel

Sorry for my poor English.
Use /dev/sda6(major 8, minor 6) as an example.
------

Quoted 12 lines
# stat  /dev/sda6
  File: '/dev/sda6'
  Size: 0             Blocks: 0          IO Block: 4096   block special file
Device: 5h/5d    Inode: 7289        Links: 1     Device type: 8,6
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/ disk)
Access: 2014-04-17 12:30:08.981732698 +0800
Modify: 2014-04-17 12:30:08.981732698 +0800
Change: 2014-04-17 12:30:08.981732698 +0800
                                     ^^^^^^^^^^^^^^^^^
 Birth: -
# mknod sda6 b 8 6
# mkfs.ext4 ./sda6 <<< Not /dev/sda6

Ah... it's too crazy to support this scenario :-)

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com