forked from ydb-platform/nbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzero_extent.cpp
58 lines (45 loc) · 1.44 KB
/
zero_extent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "zero_extent.h"
#include <util/string/builder.h>
#include <util/system/file.h>
#include <limits>
#if defined(__linux__)
// This exists because on sandbox builds we have newer kernels with much older UAPI headers
// If kernel does not actually support this we will get a proper error in runtime
# if !defined(FALLOC_FL_ZERO_RANGE)
# define FALLOC_FL_ZERO_RANGE 0x10
# endif
# include <errno.h>
# include <fcntl.h>
# include <malloc.h>
# include <sys/types.h>
#endif
namespace NCloud::NBlockStore::NServer {
////////////////////////////////////////////////////////////////////////////////
NProto::TError ZeroFileExtent(
TFileHandle& fileHandle,
i64 offset,
i64 length) noexcept
{
#if defined(__linux__)
if (length > std::numeric_limits<off_t>::max() ||
offset > std::numeric_limits<off_t>::max())
{
return MakeError(E_ARGUMENT);
}
// On linux we have FALLOC_FL_ZERO_RANGE (or even PUNCH_HOLE, but let's not get carried away)
int res = fallocate(FHANDLE(fileHandle), FALLOC_FL_ZERO_RANGE, offset, length);
if (res == 0) {
return {};
}
if (errno != EOPNOTSUPP) {
return MakeError(E_IO, TStringBuilder()
<< "FALLOC_FL_ZERO_RANGE failed: " << errno << " "
<< strerror(errno));
}
#endif
// Not supported
NProto::TError error;
error.SetCode(E_NOT_IMPLEMENTED);
return error;
}
} // namespace NCloud::NBlockStore::NServer