Debian Codegraph @

crt/aws-c-common/source/linux/file_direct_io.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #define _GNU_SOURCE /* NOLINT(bugprone-reserved-identifier) */
6 /* O_DIRECT is defined with _GNU_SOURCE on Linux */
7
8 #include <aws/common/environment.h>
9 #include <aws/common/file.h>
10 #include <aws/common/logging.h>
11 #include <aws/common/string.h>
12 #include <aws/common/system_info.h>
13 #include <dirent.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <inttypes.h>
17 #include <pwd.h>
18 #include <stdio.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21
22 /* O_DIRECT is not available on all platforms */
23 #ifndef O_DIRECT
24 # define O_DIRECT 0
25 #endif
26
27 /*
28 * Maximum chunk size for reading files with direct I/O.
29 * On Linux, read() (and similar system calls) will transfer at most
30 * 0x7ffff000 (2,147,479,552) bytes, returning the number of bytes
31 * actually transferred. (This is true on both 32-bit and 64-bit systems.)
32 */
33 static const size_t AWS_FILE_MAX_READ_CHUNK = 0x7ffff000;
34
35 int aws_file_path_read_from_offset_direct_io_with_chunk_size(
36 const struct aws_string *file_path,
37 uint64_t offset,
38 size_t max_read_length,
39 size_t max_chunk_size,
40 struct aws_byte_buf *output_buf,
41 size_t *out_actual_read) {
42 if (max_chunk_size > AWS_FILE_MAX_READ_CHUNK) {
43 /* Make sure it is less than the max. */
44 max_chunk_size = AWS_FILE_MAX_READ_CHUNK;
45 }
46
47 if (O_DIRECT == 0) {
48 AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "O_DIRECT is not supported on this platform");
49 return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
50 }
51 size_t available_len = aws_sub_size_saturating(output_buf->capacity, output_buf->len);
52 size_t length = aws_min_size(available_len, max_read_length);
53 if (length == 0) {
54 return AWS_OP_SUCCESS; /* Nothing to do. */
55 }
56
57 size_t page_size = aws_system_info_page_size();
58
59 /* Per open(2), O_DIRECT may impose alignment restrictions on the file offset, buffer
60 * pointer, and transfer length. Misaligned I/Os can either fail with EINVAL or silently
61 * fall back to buffered I/O depending on the filesystem and kernel version. We enforce
62 * alignment here to guarantee consistent, predictable behavior. */
63 /* Length is not forced for the case of reading last part to end of stream cannot align. */
64 if (offset % page_size != 0 || (uintptr_t)(output_buf->buffer + output_buf->len) % page_size != 0) {
65 AWS_LOGF_ERROR(
66 AWS_LS_COMMON_GENERAL,
67 "aws_file_path_read_from_offset_direct_io: offset %" PRIu64
68 ", buffer pointer %p must all be aligned to page size %zu",
69 offset,
70 (void *)(output_buf->buffer + output_buf->len),
71 page_size);
72 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
73 }
74
75 int rt_code = AWS_OP_ERR;
76 int fd = open(aws_string_c_str(file_path), O_RDONLY | O_DIRECT);
77 if (fd == -1) {
78 int errno_value = errno; /* Always cache errno before potential side-effect */
79 AWS_LOGF_ERROR(
80 AWS_LS_COMMON_GENERAL,
81 "Failed to open file %s for reading with O_DIRECT, errno: %d",
82 aws_string_c_str(file_path),
83 errno_value);
84 aws_translate_and_raise_io_error(errno_value);
85 goto cleanup;
86 }
87
88 /* seek to the right position and then read */
89 if (lseek(fd, (off_t)offset, SEEK_SET) == -1) {
90 int errno_value = errno; /* Always cache errno before potential side-effect */
91 AWS_LOGF_ERROR(
92 AWS_LS_COMMON_GENERAL,
93 "Failed to seek to position %llu in file %s, errno: %d",
94 (unsigned long long)offset,
95 aws_string_c_str(file_path),
96 errno_value);
97 aws_translate_and_raise_io_error(errno_value);
98 goto cleanup;
99 }
100
101 /* Read in chunks to handle the Linux read() limitation */
102 size_t total_bytes_read = 0;
103 size_t remaining_length = length;
104 uint8_t *current_buffer = output_buf->buffer + output_buf->len;
105
106 while (remaining_length > 0) {
107 size_t chunk_size = aws_min_size(remaining_length, max_chunk_size);
108
109 ssize_t bytes_read = read(fd, current_buffer, chunk_size);
110 if (bytes_read == -1) {
111 int errno_value = errno; /* Always cache errno before potential side-effect */
112 AWS_LOGF_ERROR(
113 AWS_LS_COMMON_GENERAL,
114 "Failed to read %zu bytes from file %s, errno: %d",
115 chunk_size,
116 aws_string_c_str(file_path),
117 errno_value);
118 aws_translate_and_raise_io_error(errno_value);
119 goto cleanup;
120 }
121
122 if (bytes_read == 0) {
123 /* End of file reached */
124 break;
125 }
126
127 total_bytes_read += (size_t)bytes_read;
128 current_buffer += bytes_read;
129 remaining_length -= (size_t)bytes_read;
130
131 /* If we read less than requested, we've reached the end of file */
132 if ((size_t)bytes_read < chunk_size) {
133 break;
134 }
135 }
136
137 *out_actual_read = total_bytes_read;
138 output_buf->len += total_bytes_read;
139 rt_code = AWS_OP_SUCCESS;
140 cleanup:
141 if (fd != -1) {
142 close(fd);
143 }
144 return rt_code;
145 }
146
147 int aws_file_path_read_from_offset_direct_io(
148 const struct aws_string *file_path,
149 uint64_t offset,
150 size_t max_read_length,
151 struct aws_byte_buf *output_buf,
152 size_t *out_actual_read) {
153
154 if (O_DIRECT == 0) {
155 AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "O_DIRECT is not supported on this platform");
156 return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
157 }
158
159 return aws_file_path_read_from_offset_direct_io_with_chunk_size(
160 file_path, offset, max_read_length, AWS_FILE_MAX_READ_CHUNK, output_buf, out_actual_read);
161 }
162
163 int aws_file_path_write_to_offset_direct_io(
164 const struct aws_string *file_path,
165 uint64_t offset,
166 struct aws_byte_cursor data) {
167
168 if (O_DIRECT == 0) {
169 AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "O_DIRECT is not supported on this platform");
170 return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
171 }
172
173 if (data.len == 0) {
174 return AWS_OP_SUCCESS;
175 }
176
177 size_t page_size = aws_system_info_page_size();
178
179 /* Per open(2), O_DIRECT may impose alignment restrictions on the file offset, buffer
180 * pointer, and transfer length. Misaligned I/Os can either fail with EINVAL or silently
181 * fall back to buffered I/O depending on the filesystem and kernel version. We enforce
182 * alignment here to guarantee consistent, predictable behavior. */
183 if (offset % page_size != 0 || (uintptr_t)data.ptr % page_size != 0 || data.len % page_size != 0) {
184 AWS_LOGF_ERROR(
185 AWS_LS_COMMON_GENERAL,
186 "aws_file_path_write_to_offset_direct_io: offset %" PRIu64
187 ", buffer pointer %p, and data.len %zu must all be aligned to page size %zu",
188 offset,
189 (void *)data.ptr,
190 data.len,
191 page_size);
192 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
193 }
194
195 int rt_code = AWS_OP_ERR;
196 int fd = open(aws_string_c_str(file_path), O_WRONLY | O_DIRECT);
197 if (fd == -1) {
198 int errno_value = errno;
199 AWS_LOGF_ERROR(
200 AWS_LS_COMMON_GENERAL,
201 "Failed to open file %s for writing with O_DIRECT, errno: %d",
202 aws_string_c_str(file_path),
203 errno_value);
204 aws_translate_and_raise_io_error(errno_value);
205 goto cleanup;
206 }
207
208 if (lseek(fd, (off_t)offset, SEEK_SET) == -1) {
209 int errno_value = errno;
210 AWS_LOGF_ERROR(
211 AWS_LS_COMMON_GENERAL,
212 "Failed to seek to position %llu in file %s, errno: %d",
213 (unsigned long long)offset,
214 aws_string_c_str(file_path),
215 errno_value);
216 aws_translate_and_raise_io_error(errno_value);
217 goto cleanup;
218 }
219
220 /* Write with O_DIRECT */
221 size_t total_written = 0;
222 while (total_written < data.len) {
223 size_t chunk_size = aws_min_size(data.len - total_written, AWS_FILE_MAX_READ_CHUNK);
224 ssize_t written = write(fd, data.ptr + total_written, chunk_size);
225 if (written == -1) {
226 int errno_value = errno;
227 AWS_LOGF_ERROR(
228 AWS_LS_COMMON_GENERAL,
229 "Failed to write %zu bytes to file %s with O_DIRECT, errno: %d",
230 chunk_size,
231 aws_string_c_str(file_path),
232 errno_value);
233 aws_translate_and_raise_io_error(errno_value);
234 goto cleanup;
235 }
236 total_written += (size_t)written;
237 }
238
239 rt_code = AWS_OP_SUCCESS;
240 cleanup:
241 if (fd != -1) {
242 close(fd);
243 }
244 return rt_code;
245 }
246
247 bool aws_file_direct_io_is_supported(void) {
248 /* O_DIRECT may be defined as 0 in some Linux build environments (e.g. glibc that doesn't
249 * expose it). When that's the case, opening with O_DIRECT is a no-op and direct I/O is
250 * not actually available. */
251 return O_DIRECT != 0;
252 }