From: Sage Weil Date: Fri, 10 Apr 2015 23:55:05 +0000 (-0700) Subject: os/fs: add zero / hole punch support X-Git-Tag: v9.1.0~324^2~14 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5cde57b6d319c7c49303bb3d36e7a4c180e32bca;p=ceph.git os/fs: add zero / hole punch support Signed-off-by: Sage Weil --- diff --git a/src/os/fs/FS.cc b/src/os/fs/FS.cc index e64e324155a3..03c9a11835e0 100644 --- a/src/os/fs/FS.cc +++ b/src/os/fs/FS.cc @@ -18,6 +18,11 @@ #include #include +// from include/linux/falloc.h: +#ifndef FALLOC_FL_PUNCH_HOLE +# define FALLOC_FL_PUNCH_HOLE 0x2 +#endif + #include "FS.h" #include "XFS.h" @@ -103,3 +108,38 @@ int FS::copy_file_range(int to_fd, uint64_t to_offset, { assert(0 == "write me"); } + +int FS::zero(int fd, uint64_t offset, uint64_t length) +{ + int r; + +#ifdef CEPH_HAVE_FALLOCATE +# if !defined(DARWIN) && !defined(__FreeBSD__) + // first try fallocate + r = fallocate(fd, FALLOC_FL_PUNCH_HOLE, offset, length); + if (r < 0) { + r = -errno; + } + if (r != -EOPNOTSUPP) { + goto out; // a real error + } +# endif +#endif + + { + // fall back to writing zeros + bufferlist bl; + bufferptr bp(length); + bp.zero(); + bl.append(bp); + int r = ::lseek64(fd, offset, SEEK_SET); + if (r < 0) { + r = -errno; + goto out; + } + r = bl.write_fd(fd); + } + + out: + return r; +} diff --git a/src/os/fs/FS.h b/src/os/fs/FS.h index 6a02b2721142..a9d8100fafcc 100644 --- a/src/os/fs/FS.h +++ b/src/os/fs/FS.h @@ -38,6 +38,7 @@ public: virtual int copy_file_range(int to_fd, uint64_t to_offset, int from_fd, uint64_t from_offset, uint64_t from_len); + virtual int zero(int fd, uint64_t offset, uint64_t length); }; #endif