mirror of
https://github.com/versity/scoutfs.git
synced 2026-01-05 19:54:53 +00:00
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:
@@ -3,6 +3,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@@ -103,3 +104,44 @@ char *size_str(u64 nr, unsigned size)
|
|||||||
|
|
||||||
return suffixes[i];
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,5 +14,6 @@ int device_size(char *path, int fd,
|
|||||||
char *use_type, u64 *size_ret);
|
char *use_type, u64 *size_ret);
|
||||||
float size_flt(u64 nr, unsigned size);
|
float size_flt(u64 nr, unsigned size);
|
||||||
char *size_str(u64 nr, unsigned size);
|
char *size_str(u64 nr, unsigned size);
|
||||||
|
int flush_device(int fd);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user