Add quick utils flush_device helper

Add a quick helper that just calls cache flushing ioctls on different
kinds of files.

Signed-off-by: Zach Brown <zab@versity.com>
This commit is contained in:
Zach Brown
2023-01-18 10:27:47 -08:00
parent 1b0e9c45f4
commit ddb5cce2a5
2 changed files with 43 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <errno.h>
@@ -103,3 +104,44 @@ char *size_str(u64 nr, unsigned size)
return suffixes[i];
}
/*
* Try to flush the local read cache for a device. This is only a best
* effort as these interfaces don't block waiting to fully purge the
* cache. This is OK because it's used by cached readers that are known
* to be racy anyway.
*/
int flush_device(int fd)
{
struct stat st;
int ret;
ret = fstat(fd, &st);
if (ret < 0) {
ret = -errno;
fprintf(stderr, "fstat failed: %s (%d)\n", strerror(errno), errno);
goto out;
}
if (S_ISREG(st.st_mode)) {
ret = posix_fadvise(fd, 0, st.st_size, POSIX_FADV_DONTNEED);
if (ret < 0) {
ret = -errno;
fprintf(stderr, "POSIX_FADV_DONTNEED failed: %s (%d)\n",
strerror(errno), errno);
goto out;
}
} else if (S_ISBLK(st.st_mode)) {
ret = ioctl(fd, BLKFLSBUF, 0);
if (ret < 0) {
ret = -errno;
fprintf(stderr, "BLKFLSBUF, failed: %s (%d)\n", strerror(errno), errno);
goto out;
}
}
ret = 0;
out:
return ret;
}

View File

@@ -14,5 +14,6 @@ int device_size(char *path, int fd,
char *use_type, u64 *size_ret);
float size_flt(u64 nr, unsigned size);
char *size_str(u64 nr, unsigned size);
int flush_device(int fd);
#endif