aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--src/bindfs.c75
-rw-r--r--tests/Makefile.am4
-rw-r--r--tests/odirect_read.c59
-rw-r--r--tests/odirect_write.c78
-rwxr-xr-xtests/test_bindfs.rb22
6 files changed, 218 insertions, 22 deletions
diff --git a/.gitignore b/.gitignore
index d81bad5..2c3ea73 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,6 +35,8 @@ Makefile
src/bindfs
tests/fcntl_locker
+tests/odirect_read
+tests/odirect_write
tests/readdir_inode
tests/test_dir_rewind
tests/utimens_nofollow
diff --git a/src/bindfs.c b/src/bindfs.c
index fff3a16..3c430a1 100644
--- a/src/bindfs.c
+++ b/src/bindfs.c
@@ -72,11 +72,14 @@
#include <sys/xattr.h>
#endif
+#ifdef __linux__
#include <sys/mman.h>
#include <fcntl.h>
-
-#ifdef __LINUX__
#include <linux/fs.h> // For BLKGETSIZE64
+
+#ifndef O_DIRECT
+#define O_DIRECT 00040000 /* direct disk access hint */
+#endif
#endif
#include <fuse.h>
@@ -99,10 +102,6 @@
#define XATTR_APPLE_PREFIX "com.apple."
#endif
-#ifndef O_DIRECT
-#define O_DIRECT 00040000 /* direct disk access hint */
-#endif
-
/* We pessimistically assume signed uid_t and gid_t in our overflow checks,
mostly because supporting both cases would require a bunch more code. */
static const uid_t UID_T_MAX = ((1LL << (sizeof(uid_t)*8-1)) - 1);
@@ -194,10 +193,14 @@ static struct Settings {
int enable_lock_forwarding;
int enable_ioctl;
-
+
uid_t uid_offset;
gid_t gid_offset;
+#ifdef __linux__
+ size_t linux_page_size;
+#endif
+
} settings;
@@ -227,6 +230,10 @@ static int apply_gid_offset(gid_t *gid);
static int unapply_uid_offset(uid_t *uid);
static int unapply_gid_offset(gid_t *gid);
+#ifdef __linux__
+static size_t round_up_buffer_size_for_direct_io(size_t size);
+#endif
+
/* FUSE callbacks */
static void *bindfs_init();
static void bindfs_destroy(void *private_data);
@@ -388,15 +395,15 @@ static int getattr_common(const char *procpath, struct stat *stbuf)
/* Block files as regular files. */
if (settings.block_devices_as_files && S_ISBLK(stbuf->st_mode)) {
stbuf->st_mode ^= S_IFBLK | S_IFREG; // Flip both bits
-#ifdef __LINUX__
+ int fd = open(procpath, O_RDONLY);
+#ifdef __linux__
uint64_t size;
- ioctl(file, BLKGETSIZE64, &size);
+ ioctl(fd, BLKGETSIZE64, &size);
stbuf->st_size = (off_t)size;
if (stbuf->st_size < 0) { // Underflow
return -EOVERFLOW;
}
#else
- int fd = open(procpath, O_RDONLY);
if (fd == -1) {
return -errno;
}
@@ -592,6 +599,21 @@ static int unapply_gid_offset(gid_t *gid) {
return 1;
}
+#ifdef __linux__
+static size_t round_up_buffer_size_for_direct_io(size_t size)
+{
+ // `man 2 open` says this should be block-size aligned and that there's no
+ // general way to determine this. Empirically the page size should be good enough.
+ // See also: Pull Request #74
+ size_t page_size = settings.linux_page_size;
+ size_t rem = size % page_size;
+ if (rem == 0) {
+ return size;
+ }
+ return size - rem + page_size;
+}
+#endif
+
static void *bindfs_init()
{
@@ -1105,29 +1127,33 @@ static int bindfs_read(const char *path, char *buf, size_t size, off_t offset,
int res;
(void) path;
- char * target_buf = buf;
+ char *target_buf = buf;
if (settings.read_limiter) {
rate_limiter_wait(settings.read_limiter, size);
}
- unsigned int page_size = sysconf(_SC_PAGESIZE);
+#ifdef __linux__
+ size_t mmap_size = 0;
if (fi->flags & O_DIRECT) {
- // allocate 512 bytes aligned buffer for direct io to work
- target_buf = mmap(NULL, ((page_size - 1 + size) / page_size) * page_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+ mmap_size = round_up_buffer_size_for_direct_io(size);
+ target_buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
if (target_buf == MAP_FAILED) {
return -ENOMEM;
}
}
+#endif
res = pread(fi->fh, target_buf, size, offset);
if (res == -1)
res = -errno;
+#ifdef __linux__
if (target_buf != buf) {
memcpy(buf, target_buf, size);
- munmap(target_buf, ((page_size - 1 + size) / page_size) * page_size);
+ munmap(target_buf, mmap_size);
}
+#endif
return res;
}
@@ -1137,29 +1163,33 @@ static int bindfs_write(const char *path, const char *buf, size_t size,
{
int res;
(void) path;
- char * source_buf = buf;
+ char *source_buf = (char*)buf;
if (settings.write_limiter) {
rate_limiter_wait(settings.write_limiter, size);
}
- unsigned int page_size = sysconf(_SC_PAGESIZE);
+#ifdef __linux__
+ size_t mmap_size = 0;
if (fi->flags & O_DIRECT) {
- // allocate 512 bytes aligned buffer for direct io to work
- source_buf = mmap(NULL, ((page_size - 1 + size) / page_size) * page_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+ mmap_size = round_up_buffer_size_for_direct_io(size);
+ source_buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
if (source_buf == MAP_FAILED) {
return -ENOMEM;
}
memcpy(source_buf, buf, size);
}
+#endif
res = pwrite(fi->fh, source_buf, size, offset);
if (res == -1)
res = -errno;
- if (source_buf != buf) {
- munmap(source_buf, ((page_size - 1 + size) / page_size) * page_size);
+#ifdef __linux__
+ if (source_buf != buf) {
+ munmap(source_buf, mmap_size);
}
+#endif
return res;
}
@@ -2076,6 +2106,9 @@ int main(int argc, char *argv[])
settings.enable_ioctl = 0;
settings.uid_offset = 0;
settings.gid_offset = 0;
+#ifdef __linux__
+ settings.linux_page_size = sysconf(_SC_PAGESIZE);
+#endif
atexit(&atexit_func);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0a52108..742899b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -4,10 +4,12 @@ UNAME_S := $(shell uname -s)
AM_CPPFLAGS = ${my_CPPFLAGS}
AM_CFLAGS = ${my_CFLAGS}
-noinst_PROGRAMS = readdir_inode utimens_nofollow fcntl_locker test_dir_rewind
+noinst_PROGRAMS = readdir_inode utimens_nofollow fcntl_locker odirect_read odirect_write test_dir_rewind
readdir_inode_SOURCES = readdir_inode.c
utimens_nofollow_SOURCES = utimens_nofollow.c
fcntl_locker_SOURCES = fcntl_locker.c
+odirect_read_SOURCES = odirect_read.c
+odirect_write_SOURCES = odirect_write.c
test_dir_rewind_SOURCES = test_dir_rewind.c
TESTS = test_bindfs.rb
diff --git a/tests/odirect_read.c b/tests/odirect_read.c
new file mode 100644
index 0000000..8fc746b
--- /dev/null
+++ b/tests/odirect_read.c
@@ -0,0 +1,59 @@
+#ifdef __linux__
+
+#include <stdio.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#ifndef O_DIRECT
+#define O_DIRECT 00040000 /* direct disk access hint */
+#endif
+
+int main(int argc, char** argv) {
+ if (argc != 2) {
+ fprintf(stderr, "Expected 1 argument: the file to read.\n");
+ return 1;
+ }
+
+ int fd = open(argv[1], O_RDONLY | O_DIRECT);
+ if (fd == -1) {
+ perror("failed to open file");
+ return 1;
+ }
+
+ const size_t buf_size = 4096;
+ unsigned char* buf = mmap(NULL, buf_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+ if (buf == MAP_FAILED) {
+ perror("mmap failed");
+ return 1;
+ }
+
+ while (1) {
+ ssize_t amt_read = read(fd, buf, buf_size);
+ if (amt_read == 0) {
+ break;
+ }
+ if (amt_read == -1) {
+ perror("failed to read file");
+ return 1;
+ }
+ fwrite(buf, 1, amt_read, stdout);
+ fflush(stdout);
+ }
+
+ return 0;
+}
+
+#else // __linux__
+
+#include <stdio.h>
+
+int main() {
+ fprintf(stderr, "Not supported on this platform.\n");
+ return 1;
+}
+
+#endif // __linux__
diff --git a/tests/odirect_write.c b/tests/odirect_write.c
new file mode 100644
index 0000000..1376a51
--- /dev/null
+++ b/tests/odirect_write.c
@@ -0,0 +1,78 @@
+#ifdef __linux__
+
+#include <stdio.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#ifndef O_DIRECT
+#define O_DIRECT 00040000 /* direct disk access hint */
+#endif
+
+int main(int argc, char** argv) {
+ if (argc != 2) {
+ fprintf(stderr, "Expected 1 argument: the file to read.\n");
+ return 1;
+ }
+
+ int fd = open(argv[1], O_WRONLY | O_CREAT | O_DIRECT, 0644);
+ if (fd == -1) {
+ perror("failed to open file");
+ return 1;
+ }
+
+ const size_t buf_size = 4096;
+ unsigned char* buf = mmap(NULL, buf_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
+ if (buf == MAP_FAILED) {
+ perror("mmap failed");
+ return 1;
+ }
+
+ size_t total_size = 0;
+ while (1) {
+ if (feof(stdin)) {
+ break;
+ }
+
+ memset(buf, 0, buf_size);
+ size_t amt_read = fread(buf, 1, buf_size, stdin);
+ if (ferror(stdin)) {
+ perror("failed to read stdin");
+ return 1;
+ }
+ if (amt_read == 0) {
+ continue;
+ }
+
+ total_size += amt_read;
+
+ ssize_t res = write(fd, buf, buf_size);
+ if (res == -1) {
+ perror("failed to write");
+ return 1;
+ }
+ if (res != buf_size) {
+ // Too lazy to write a loop here unless it turns out to be necessary.
+ fprintf(stderr, "Failed to write exactly %ld bytes", amt_read);
+ }
+ }
+
+ munmap(buf, buf_size);
+
+ return 0;
+}
+
+#else // __linux__
+
+#include <stdio.h>
+
+int main() {
+ fprintf(stderr, "Not supported on this platform.\n");
+ return 1;
+}
+
+#endif // __linux__
diff --git a/tests/test_bindfs.rb b/tests/test_bindfs.rb
index c5d41f6..9e5d381 100755
--- a/tests/test_bindfs.rb
+++ b/tests/test_bindfs.rb
@@ -775,6 +775,28 @@ if `uname`.strip == 'Linux' &&
end
end
+# Pull Request #74
+if `uname`.strip == 'Linux'
+ def odirect_data
+ ('abc' * 10000)[0...8192]
+ end
+
+ testenv("", :title => "O_DIRECT reads") do
+ File.write("src/f", odirect_data)
+ read_data = `#{$tests_dir}/odirect_read mnt/f`
+ assert { $?.success? }
+ assert { read_data == odirect_data }
+ end
+
+ testenv("", :title => "O_DIRECT writes") do
+ IO.popen("#{$tests_dir}/odirect_write mnt/f", "w") do |pipe|
+ pipe.write(odirect_data)
+ end
+ assert { $?.success? }
+ assert { File.read("src/f") == odirect_data }
+ end
+end
+
if `uname`.strip != 'FreeBSD' # -o dev is not supported on FreeBSD
root_testenv("-odev") do
system("mknod mnt/zero c 1 5")