| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2025 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn.h" |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | |
| 28 | #include "grn_ctx.h" |
| 29 | #include "grn_io.h" |
| 30 | #include "grn_plugin.h" |
| 31 | #include "grn_hash.h" |
| 32 | #include "grn_ctx_impl.h" |
| 33 | #include "grn_util.h" |
| 34 | |
| 35 | #ifdef WIN32 |
| 36 | # include <io.h> |
| 37 | # include <share.h> |
| 38 | #endif /* WIN32 */ |
| 39 | |
| 40 | #define GRN_IO_IDSTR "GROONGA:IO:00001" |
| 41 | #define GRN_IO_IDSTR_LEN (sizeof(GRN_IO_IDSTR) - 1) |
| 42 | |
| 43 | #define GRN_IO_VERSION_DEFAULT 1 |
| 44 | |
| 45 | #define GRN_IO_FILE_SIZE_V1 1073741824UL |
| 46 | |
| 47 | #ifdef WIN32 |
| 48 | # define GRN_IO_FILE_SIZE_V0 134217728L |
| 49 | #else /* WIN32 */ |
| 50 | # define GRN_IO_FILE_SIZE_V0 GRN_IO_FILE_SIZE_V1 |
| 51 | #endif /* WIN32 */ |
| 52 | |
| 53 | #ifndef WIN32 |
| 54 | # ifdef HAVE_FUTIMENS |
| 55 | # define grn_futimens(fd, time_values, time_specs) \ |
| 56 | futimens((fd), (time_specs)) |
| 57 | # define grn_futimens_name "futimens" |
| 58 | # elif defined(HAVE_FUTIMES) /* HAVE_FUTIMENS */ |
| 59 | # define grn_futimens(fd, time_values, time_specs) \ |
| 60 | futimes((fd), (time_values)) |
| 61 | # define grn_futimens_name "futimes" |
| 62 | # else /* HAVE_FUTIMENS */ |
| 63 | # error Support for either the futimens() or futimens() function is required |
| 64 | # endif /* HAVE_FUTIMENS */ |
| 65 | #endif /* WIN32 */ |
| 66 | |
| 67 | typedef struct _grn_io_fileinfo { |
| 68 | char *path; |
| 69 | #ifdef WIN32 |
| 70 | HANDLE fh; |
| 71 | HANDLE fmo; |
| 72 | grn_critical_section cs; |
| 73 | #else /* WIN32 */ |
| 74 | int fd; |
| 75 | dev_t dev; |
| 76 | ino_t inode; |
| 77 | #endif /* WIN32 */ |
| 78 | } fileinfo; |
| 79 | |
| 80 | #define IO_HEADER_SIZE 64 |
| 81 | |
| 82 | static uint32_t grn_io_version_default = GRN_IO_VERSION_DEFAULT; |
| 83 | |
| 84 | static inline grn_rc |
| 85 | grn_fileinfo_open(grn_ctx *ctx, fileinfo *fi, const char *path, int flags); |
| 86 | static inline void |
| 87 | grn_fileinfo_init(fileinfo *fis, uint32_t nfis); |
| 88 | static inline int |
| 89 | grn_fileinfo_opened(fileinfo *fi); |
| 90 | static inline grn_rc |
| 91 | grn_fileinfo_close(grn_ctx *ctx, fileinfo *fi); |
| 92 | #ifdef WIN32 |
| 93 | inline static void * |
| 94 | grn_mmap(grn_ctx *ctx, |
| 95 | grn_io *io, |
| 96 | HANDLE *fmo, |
| 97 | fileinfo *fi, |
| 98 | int64_t offset, |
| 99 | size_t length, |
| 100 | const char *file, |
| 101 | int line, |
| 102 | const char *func); |
| 103 | static inline int |
| 104 | grn_munmap(grn_ctx *ctx, |
| 105 | grn_io *io, |
| 106 | HANDLE *fmo, |
| 107 | fileinfo *fi, |
| 108 | void *start, |
| 109 | size_t length); |
| 110 | # define GRN_MMAP(ctx, io, fmo, fi, offset, length) \ |
| 111 | (grn_mmap((ctx), \ |
| 112 | (io), \ |
| 113 | (fmo), \ |
| 114 | (fi), \ |
| 115 | (offset), \ |
| 116 | (length), \ |
| 117 | __FILE__, \ |
| 118 | __LINE__, \ |
| 119 | __FUNCTION__)) |
| 120 | # define GRN_MUNMAP(ctx, io, fmo, fi, start, length) \ |
| 121 | (grn_munmap((ctx), (io), (fmo), (fi), (start), (length))) |
| 122 | #else /* WIN32 */ |
| 123 | inline static void * |
| 124 | grn_mmap(grn_ctx *ctx, |
| 125 | grn_io *io, |
| 126 | fileinfo *fi, |
| 127 | int64_t offset, |
| 128 | size_t length, |
| 129 | const char *file, |
| 130 | int line, |
| 131 | const char *func); |
| 132 | static inline int |
| 133 | grn_munmap(grn_ctx *ctx, grn_io *io, fileinfo *fi, void *start, size_t length); |
| 134 | # define GRN_MMAP(ctx, io, fmo, fi, offset, length) \ |
| 135 | (grn_mmap((ctx), \ |
| 136 | (io), \ |
| 137 | (fi), \ |
| 138 | (offset), \ |
| 139 | (length), \ |
| 140 | __FILE__, \ |
| 141 | __LINE__, \ |
| 142 | __FUNCTION__)) |
| 143 | # define GRN_MUNMAP(ctx, io, fmo, fi, start, length) \ |
| 144 | (grn_munmap((ctx), (io), (fi), (start), (length))) |
| 145 | #endif /* WIN32 */ |
| 146 | |
| 147 | inline static int |
| 148 | grn_msync(grn_ctx *ctx, fileinfo *fi, void *start, size_t length); |
| 149 | #define GRN_MSYNC(ctx, fi, start, length) \ |
| 150 | (grn_msync((ctx), (fi), (start), (length))) |
| 151 | |
| 152 | inline static grn_rc |
| 153 | grn_pread(grn_ctx *ctx, fileinfo *fi, void *buf, size_t count, off_t offset); |
| 154 | static inline grn_rc |
| 155 | grn_pwrite(grn_ctx *ctx, fileinfo *fi, void *buf, size_t count, off_t offset); |
| 156 | |
| 157 | void |
| 158 | grn_io_init_from_env(void) |
| 159 | { |
| 160 | { |
| 161 | char version_env[GRN_ENV_BUFFER_SIZE]; |
| 162 | |
| 163 | grn_getenv("GRN_IO_VERSION", version_env, GRN_ENV_BUFFER_SIZE); |
| 164 | if (version_env[0]) { |
| 165 | grn_io_version_default = (uint32_t)atoi(version_env); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static inline uint32_t |
| 171 | grn_io_compute_base(uint32_t header_size) |
| 172 | { |
| 173 | uint32_t total_header_size; |
| 174 | total_header_size = IO_HEADER_SIZE + header_size; |
| 175 | return (total_header_size + (uint32_t)grn_pagesize - 1) & |
| 176 | (uint32_t)(~(grn_pagesize - 1)); |
| 177 | } |
| 178 | |
| 179 | static inline uint32_t |
| 180 | grn_io_compute_base_segment(uint32_t base, uint32_t segment_size) |
| 181 | { |
| 182 | return (base + segment_size - 1) / segment_size; |
| 183 | } |
| 184 | |
| 185 | static uint32_t |
| 186 | grn_io_compute_max_n_files(uint32_t segment_size, |
| 187 | uint32_t max_segment, |
| 188 | unsigned int base_segument, |
| 189 | uint32_t file_size) |
| 190 | { |
| 191 | uint64_t last_segment_end; |
| 192 | last_segment_end = ((uint64_t)segment_size) * (max_segment + base_segument); |
| 193 | return (uint32_t)((last_segment_end + file_size - 1) / file_size); |
| 194 | } |
| 195 | |
| 196 | static inline uint32_t |
| 197 | grn_io_compute_file_size(uint32_t version) |
| 198 | { |
| 199 | if (version == 0) { |
| 200 | return GRN_IO_FILE_SIZE_V0; |
| 201 | } else { |
| 202 | return GRN_IO_FILE_SIZE_V1; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static inline uint32_t |
| 207 | grn_io_max_segment(grn_io *io) |
| 208 | { |
| 209 | if (io->header->segment_tail) { |
| 210 | return io->header->segment_tail; |
| 211 | } else { |
| 212 | return io->header->max_segment; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static uint32_t |
| 217 | grn_io_max_n_files(grn_io *io) |
| 218 | { |
| 219 | uint32_t file_size = grn_io_compute_file_size(io->header->version); |
| 220 | return grn_io_compute_max_n_files(io->header->segment_size, |
| 221 | grn_io_max_segment(io), |
| 222 | io->base_seg, |
| 223 | file_size); |
| 224 | } |
| 225 | |
| 226 | static inline uint32_t |
| 227 | grn_io_compute_nth_file_info(grn_io *io, uint32_t nth_segment) |
| 228 | { |
| 229 | uint32_t segment_size; |
| 230 | uint32_t file_size; |
| 231 | uint32_t n_segments_per_file; |
| 232 | uint32_t resolved_nth_segment; |
| 233 | |
| 234 | segment_size = io->header->segment_size; |
| 235 | file_size = grn_io_compute_file_size(io->header->version); |
| 236 | n_segments_per_file = file_size / segment_size; |
| 237 | resolved_nth_segment = nth_segment + io->base_seg; |
| 238 | return resolved_nth_segment / n_segments_per_file; |
| 239 | } |
| 240 | |
| 241 | static grn_io * |
| 242 | grn_io_create_tmp(grn_ctx *ctx, |
| 243 | uint32_t header_size, |
| 244 | uint32_t segment_size, |
| 245 | uint32_t max_segment, |
| 246 | grn_io_mode mode, |
| 247 | uint32_t flags) |
| 248 | { |
| 249 | grn_io *io; |
| 250 | uint32_t b; |
| 251 | struct _grn_io_header *header; |
| 252 | b = grn_io_compute_base(header_size); |
| 253 | header = (struct _grn_io_header *)GRN_MMAP(ctx, NULL, NULL, NULL, 0, b); |
| 254 | if (header) { |
| 255 | header->version = grn_io_version_default; |
| 256 | header->header_size = header_size; |
| 257 | header->segment_size = segment_size; |
| 258 | header->max_segment = max_segment; |
| 259 | header->n_arrays = 0; |
| 260 | header->flags = flags; |
| 261 | header->lock = 0; |
| 262 | grn_memcpy(header->idstr, GRN_IO_IDSTR, 16); |
| 263 | if ((io = GRN_CALLOC(sizeof(grn_io)))) { |
| 264 | grn_io_mapinfo *maps = NULL; |
| 265 | if ((maps = GRN_CALLOC(sizeof(grn_io_mapinfo) * max_segment))) { |
| 266 | io->header = header; |
| 267 | io->user_header = (((byte *)header) + IO_HEADER_SIZE); |
| 268 | io->maps = maps; |
| 269 | io->base = b; |
| 270 | io->base_seg = 0; |
| 271 | io->mode = mode; |
| 272 | io->header->curr_size = b; |
| 273 | io->fis = NULL; |
| 274 | io->ainfo = NULL; |
| 275 | io->max_map_seg = 0; |
| 276 | io->nmaps = 0; |
| 277 | io->count = 0; |
| 278 | io->flags = GRN_IO_TEMPORARY; |
| 279 | io->lock = &header->lock; |
| 280 | io->path[0] = '\0'; |
| 281 | return io; |
| 282 | } |
| 283 | GRN_FREE(io); |
| 284 | } |
| 285 | GRN_MUNMAP(ctx, NULL, NULL, NULL, header, b); |
| 286 | } |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | static void |
| 291 | grn_io_register(grn_ctx *ctx, grn_io *io) |
| 292 | { |
| 293 | if (io->fis && (io->flags & (GRN_IO_EXPIRE_GTICK | GRN_IO_EXPIRE_SEGMENT))) { |
| 294 | bool succeeded = false; |
| 295 | CRITICAL_SECTION_ENTER(grn_glock); |
| 296 | if (grn_gctx.impl && grn_gctx.impl->ios && |
| 297 | grn_hash_add(&grn_gctx, |
| 298 | grn_gctx.impl->ios, |
| 299 | io->path, |
| 300 | (unsigned int)strlen(io->path), |
| 301 | (void **)&io, |
| 302 | NULL)) { |
| 303 | succeeded = true; |
| 304 | } |
| 305 | CRITICAL_SECTION_LEAVE(grn_glock); |
| 306 | if (!succeeded) { |
| 307 | GRN_LOG(ctx, GRN_LOG_WARNING, "grn_io_register(%s) failed", io->path); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | static void |
| 313 | grn_io_unregister(grn_ctx *ctx, grn_io *io) |
| 314 | { |
| 315 | if (io->fis && (io->flags & (GRN_IO_EXPIRE_GTICK | GRN_IO_EXPIRE_SEGMENT))) { |
| 316 | bool succeeded = false; |
| 317 | CRITICAL_SECTION_ENTER(grn_glock); |
| 318 | if (grn_gctx.impl && grn_gctx.impl->ios) { |
| 319 | grn_hash_delete(&grn_gctx, |
| 320 | grn_gctx.impl->ios, |
| 321 | io->path, |
| 322 | (unsigned int)strlen(io->path), |
| 323 | NULL); |
| 324 | succeeded = true; |
| 325 | } |
| 326 | CRITICAL_SECTION_LEAVE(grn_glock); |
| 327 | if (!succeeded) { |
| 328 | GRN_LOG(ctx, GRN_LOG_WARNING, "grn_io_unregister(%s) failed", io->path); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | grn_io * |
| 334 | grn_io_create(grn_ctx *ctx, |
| 335 | const char *path, |
| 336 | uint32_t header_size, |
| 337 | uint32_t segment_size, |
| 338 | uint32_t max_segment, |
| 339 | grn_io_mode mode, |
| 340 | uint32_t flags) |
| 341 | { |
| 342 | grn_io *io; |
| 343 | fileinfo *fis; |
| 344 | size_t path_length; |
| 345 | uint32_t b, max_nfiles; |
| 346 | uint32_t bs; |
| 347 | struct _grn_io_header *header; |
| 348 | uint32_t version = grn_io_version_default; |
| 349 | uint32_t file_size; |
| 350 | |
| 351 | if (!path) { |
| 352 | return grn_io_create_tmp(ctx, |
| 353 | header_size, |
| 354 | segment_size, |
| 355 | max_segment, |
| 356 | mode, |
| 357 | flags); |
| 358 | } |
| 359 | path_length = strlen(path); |
| 360 | if (path_length == 0 || (path_length > PATH_MAX - 4)) { |
| 361 | return NULL; |
| 362 | } |
| 363 | b = grn_io_compute_base(header_size); |
| 364 | bs = grn_io_compute_base_segment(b, segment_size); |
| 365 | file_size = grn_io_compute_file_size(version); |
| 366 | max_nfiles = |
| 367 | grn_io_compute_max_n_files(segment_size, max_segment, bs, file_size); |
| 368 | if ((fis = GRN_MALLOCN(fileinfo, max_nfiles))) { |
| 369 | grn_fileinfo_init(fis, max_nfiles); |
| 370 | if (!grn_fileinfo_open(ctx, fis, path, O_RDWR | O_CREAT | O_EXCL)) { |
| 371 | header = |
| 372 | (struct _grn_io_header *)GRN_MMAP(ctx, NULL, &fis->fmo, fis, 0, b); |
| 373 | if (header) { |
| 374 | header->version = version; |
| 375 | header->header_size = header_size; |
| 376 | header->segment_size = segment_size; |
| 377 | header->max_segment = max_segment; |
| 378 | header->n_arrays = 0; |
| 379 | header->flags = flags; |
| 380 | header->lock = 0; |
| 381 | grn_memcpy(header->idstr, GRN_IO_IDSTR, 16); |
| 382 | GRN_MSYNC(ctx, &(fis[0]), header, b); |
| 383 | if ((io = GRN_CALLOC(sizeof(grn_io)))) { |
| 384 | grn_io_mapinfo *maps = NULL; |
| 385 | if ((maps = GRN_CALLOC(sizeof(grn_io_mapinfo) * max_segment))) { |
| 386 | grn_strncpy(io->path, PATH_MAX, path, path_length + 1); |
| 387 | io->header = header; |
| 388 | io->user_header = (((byte *)header) + IO_HEADER_SIZE); |
| 389 | io->maps = maps; |
| 390 | io->base = b; |
| 391 | io->base_seg = bs; |
| 392 | io->mode = mode; |
| 393 | io->header->curr_size = b; |
| 394 | io->fis = fis; |
| 395 | io->ainfo = NULL; |
| 396 | io->max_map_seg = 0; |
| 397 | io->nmaps = 0; |
| 398 | io->count = 0; |
| 399 | io->flags = flags; |
| 400 | io->lock = &header->lock; |
| 401 | grn_io_register(ctx, io); |
| 402 | return io; |
| 403 | } |
| 404 | GRN_FREE(io); |
| 405 | } |
| 406 | GRN_MUNMAP(ctx, NULL, &fis->fmo, fis, header, b); |
| 407 | } |
| 408 | grn_fileinfo_close(ctx, fis); |
| 409 | if (grn_unlink(path) == 0) { |
| 410 | GRN_LOG(ctx, |
| 411 | GRN_LOG_INFO, |
| 412 | "[io][create][error] removed path: <%s>", |
| 413 | path); |
| 414 | } else { |
| 415 | ERRNO_ERR("[io][create][error] failed to remove path: <%s>", path); |
| 416 | } |
| 417 | } |
| 418 | GRN_FREE(fis); |
| 419 | } |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | static grn_rc |
| 424 | array_init_( |
| 425 | grn_ctx *ctx, grn_io *io, uint32_t n_arrays, size_t hsize, size_t msize) |
| 426 | { |
| 427 | uint32_t i; |
| 428 | uint32_t ws; |
| 429 | byte *hp, *mp; |
| 430 | grn_io_array_spec *array_specs = (grn_io_array_spec *)io->user_header; |
| 431 | hp = io->user_header; |
| 432 | if (!(mp = GRN_CALLOC(msize))) { |
| 433 | return GRN_NO_MEMORY_AVAILABLE; |
| 434 | } |
| 435 | io->ainfo = (grn_io_array_info *)mp; |
| 436 | hp += sizeof(grn_io_array_spec) * n_arrays; |
| 437 | mp += sizeof(grn_io_array_info) * n_arrays; |
| 438 | for (ws = 0; (uint32_t)(1 << ws) < io->header->segment_size; ws++) |
| 439 | ; |
| 440 | for (i = 0; i < n_arrays; i++) { |
| 441 | uint32_t we = ws - array_specs[i].w_of_element; |
| 442 | io->ainfo[i].w_of_elm_in_a_segment = we; |
| 443 | io->ainfo[i].elm_mask_in_a_segment = (1U << we) - 1U; |
| 444 | io->ainfo[i].max_n_segments = array_specs[i].max_n_segments; |
| 445 | io->ainfo[i].element_size = 1 << array_specs[i].w_of_element; |
| 446 | io->ainfo[i].segments = (uint32_t *)hp; |
| 447 | io->ainfo[i].addrs = (void **)mp; |
| 448 | hp += sizeof(uint32_t) * array_specs[i].max_n_segments; |
| 449 | mp += sizeof(void *) * array_specs[i].max_n_segments; |
| 450 | } |
| 451 | io->user_header += hsize; |
| 452 | return GRN_SUCCESS; |
| 453 | } |
| 454 | |
| 455 | static grn_rc |
| 456 | array_init(grn_ctx *ctx, grn_io *io, uint32_t n_arrays) |
| 457 | { |
| 458 | if (n_arrays) { |
| 459 | uint32_t i; |
| 460 | grn_io_array_spec *array_specs = (grn_io_array_spec *)io->user_header; |
| 461 | size_t hsize = sizeof(grn_io_array_spec) * n_arrays; |
| 462 | size_t msize = sizeof(grn_io_array_info) * n_arrays; |
| 463 | for (i = 0; i < n_arrays; i++) { |
| 464 | hsize += sizeof(uint32_t) * array_specs[i].max_n_segments; |
| 465 | msize += sizeof(void *) * array_specs[i].max_n_segments; |
| 466 | } |
| 467 | return array_init_(ctx, io, n_arrays, hsize, msize); |
| 468 | } |
| 469 | return GRN_SUCCESS; |
| 470 | } |
| 471 | |
| 472 | grn_io * |
| 473 | grn_io_create_with_array(grn_ctx *ctx, |
| 474 | const char *path, |
| 475 | uint32_t header_size, |
| 476 | uint32_t segment_size, |
| 477 | grn_io_mode mode, |
| 478 | uint32_t n_arrays, |
| 479 | grn_io_array_spec *array_specs) |
| 480 | { |
| 481 | if (n_arrays) { |
| 482 | uint32_t i; |
| 483 | grn_io *io; |
| 484 | byte *hp; |
| 485 | uint32_t nsegs = 0; |
| 486 | size_t hsize = sizeof(grn_io_array_spec) * n_arrays; |
| 487 | size_t msize = sizeof(grn_io_array_info) * n_arrays; |
| 488 | for (i = 0; i < n_arrays; i++) { |
| 489 | nsegs += array_specs[i].max_n_segments; |
| 490 | hsize += sizeof(uint32_t) * array_specs[i].max_n_segments; |
| 491 | msize += sizeof(void *) * array_specs[i].max_n_segments; |
| 492 | } |
| 493 | if ((io = grn_io_create(ctx, |
| 494 | path, |
| 495 | (uint32_t)(header_size + hsize), |
| 496 | segment_size, |
| 497 | nsegs, |
| 498 | mode, |
| 499 | GRN_IO_EXPIRE_GTICK))) { |
| 500 | grn_rc rc; |
| 501 | hp = io->user_header; |
| 502 | grn_memcpy(hp, array_specs, sizeof(grn_io_array_spec) * n_arrays); |
| 503 | io->header->n_arrays = n_arrays; |
| 504 | io->header->segment_tail = 1; |
| 505 | rc = array_init_(ctx, io, n_arrays, hsize, msize); |
| 506 | if (rc == GRN_SUCCESS) { |
| 507 | return io; |
| 508 | } |
| 509 | ERR(GRN_NO_MEMORY_AVAILABLE, "grn_io_create_with_array failed"); |
| 510 | grn_io_close(ctx, io); |
| 511 | } |
| 512 | } |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | static inline uint32_t |
| 517 | segment_alloc(grn_ctx *ctx, grn_io *io) |
| 518 | { |
| 519 | uint32_t n, s; |
| 520 | grn_io_array_info *ai; |
| 521 | if (io->header->segment_tail) { |
| 522 | if (io->header->segment_tail > io->header->max_segment) { |
| 523 | s = 0; |
| 524 | } else { |
| 525 | s = io->header->segment_tail++; |
| 526 | } |
| 527 | } else { |
| 528 | char *used = GRN_CALLOC(io->header->max_segment + 1); |
| 529 | if (!used) { |
| 530 | return 0; |
| 531 | } |
| 532 | for (n = io->header->n_arrays, ai = io->ainfo; n; n--, ai++) { |
| 533 | for (s = 0; s < ai->max_n_segments; s++) { |
| 534 | used[ai->segments[s]] = 1; |
| 535 | } |
| 536 | } |
| 537 | for (s = 1;; s++) { |
| 538 | if (s > io->header->max_segment) { |
| 539 | io->header->segment_tail = s; |
| 540 | s = 0; |
| 541 | break; |
| 542 | } |
| 543 | if (!used[s]) { |
| 544 | io->header->segment_tail = s + 1; |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | GRN_FREE(used); |
| 549 | } |
| 550 | return s; |
| 551 | } |
| 552 | |
| 553 | void |
| 554 | grn_io_segment_alloc(grn_ctx *ctx, |
| 555 | grn_io *io, |
| 556 | grn_io_array_info *ai, |
| 557 | uint32_t lseg, |
| 558 | int *flags, |
| 559 | void **p) |
| 560 | { |
| 561 | uint32_t *sp = &ai->segments[lseg]; |
| 562 | if (!*sp) { |
| 563 | if ((*flags & GRN_TABLE_ADD)) { |
| 564 | if ((*sp = segment_alloc(ctx, io))) { |
| 565 | *flags |= GRN_TABLE_ADDED; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | if (*sp) { |
| 570 | uint32_t pseg = *sp - 1; |
| 571 | *p = grn_io_seg_ref(ctx, io, pseg); |
| 572 | if (*p) { |
| 573 | grn_io_seg_unref(ctx, io, pseg); |
| 574 | }; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | uint32_t |
| 579 | grn_io_detect_type(grn_ctx *ctx, const char *path) |
| 580 | { |
| 581 | struct _grn_io_header h; |
| 582 | uint32_t res = 0; |
| 583 | int fd; |
| 584 | grn_open(fd, path, O_RDONLY | GRN_OPEN_FLAG_BINARY); |
| 585 | if (fd != -1) { |
| 586 | struct stat s; |
| 587 | if (fstat(fd, &s) != -1 && |
| 588 | (size_t)(s.st_size) >= sizeof(struct _grn_io_header)) { |
| 589 | if (grn_read(fd, &h, sizeof(struct _grn_io_header)) == |
| 590 | sizeof(struct _grn_io_header)) { |
| 591 | if (!memcmp(h.idstr, GRN_IO_IDSTR, GRN_IO_IDSTR_LEN)) { |
| 592 | res = h.type; |
| 593 | } else { |
| 594 | ERR(GRN_INCOMPATIBLE_FILE_FORMAT, |
| 595 | "failed to detect type: format ID is different: <%s>: <%.*s>", |
| 596 | path, |
| 597 | (int)GRN_IO_IDSTR_LEN, |
| 598 | GRN_IO_IDSTR); |
| 599 | } |
| 600 | } else { |
| 601 | SERR("failed to read enough data for detecting type: <%s>", path); |
| 602 | } |
| 603 | } else { |
| 604 | ERR(GRN_INVALID_FORMAT, "grn_io_detect_type failed"); |
| 605 | } |
| 606 | grn_close(fd); |
| 607 | } else { |
| 608 | ERRNO_ERR("failed to open path for detecting type: <%s>", path); |
| 609 | } |
| 610 | return res; |
| 611 | } |
| 612 | |
| 613 | grn_io * |
| 614 | grn_io_open(grn_ctx *ctx, const char *path, grn_io_mode mode) |
| 615 | { |
| 616 | size_t max_path_len = PATH_MAX - 4; |
| 617 | size_t path_length; |
| 618 | struct stat s; |
| 619 | fileinfo fi; |
| 620 | uint32_t flags = 0; |
| 621 | uint32_t b; |
| 622 | uint32_t header_size = 0, segment_size = 0, max_segment = 0, bs; |
| 623 | if (!path || !*path) { |
| 624 | ERR(GRN_INVALID_ARGUMENT, "[io][open] path is missing"); |
| 625 | return NULL; |
| 626 | } |
| 627 | path_length = strlen(path); |
| 628 | if (path_length > max_path_len) { |
| 629 | int truncate_length = 10; |
| 630 | ERR(GRN_INVALID_ARGUMENT, |
| 631 | "[io][open] path is too long: " |
| 632 | "<%" GRN_FMT_SIZE ">(max: %" GRN_FMT_SIZE "): <%.*s...>", |
| 633 | path_length, |
| 634 | max_path_len, |
| 635 | truncate_length, |
| 636 | path); |
| 637 | return NULL; |
| 638 | } |
| 639 | { |
| 640 | struct _grn_io_header h; |
| 641 | int fd; |
| 642 | ssize_t read_bytes; |
| 643 | grn_open(fd, path, O_RDWR | GRN_OPEN_FLAG_BINARY); |
| 644 | if (fd == -1) { |
| 645 | ERRNO_ERR("[io][open] failed to open path: <%s>", path); |
| 646 | return NULL; |
| 647 | } |
| 648 | if (fstat(fd, &s) == -1) { |
| 649 | ERRNO_ERR("[io][open] failed to file status: <%s>", path); |
| 650 | grn_close(fd); |
| 651 | return NULL; |
| 652 | } |
| 653 | if ((size_t)(s.st_size) < sizeof(struct _grn_io_header)) { |
| 654 | ERR(GRN_INCOMPATIBLE_FILE_FORMAT, |
| 655 | "[io][open] file size is too small: " |
| 656 | "<%" GRN_FMT_INT64D ">(required: >= %" GRN_FMT_SIZE "): <%s>", |
| 657 | (int64_t)(s.st_size), |
| 658 | sizeof(struct _grn_io_header), |
| 659 | path); |
| 660 | grn_close(fd); |
| 661 | return NULL; |
| 662 | } |
| 663 | read_bytes = grn_read(fd, &h, sizeof(struct _grn_io_header)); |
| 664 | if (read_bytes != sizeof(struct _grn_io_header)) { |
| 665 | ERRNO_ERR("[io][open] failed to read header data: " |
| 666 | "<%" GRN_FMT_SSIZE ">(expected: %" GRN_FMT_SSIZE "): <%s>", |
| 667 | read_bytes, |
| 668 | sizeof(struct _grn_io_header), |
| 669 | path); |
| 670 | grn_close(fd); |
| 671 | return NULL; |
| 672 | } |
| 673 | if (memcmp(h.idstr, GRN_IO_IDSTR, GRN_IO_IDSTR_LEN) != 0) { |
| 674 | ERR(GRN_INCOMPATIBLE_FILE_FORMAT, |
| 675 | "[io][open] failed to open: format ID is different: <%s>: <%.*s>", |
| 676 | path, |
| 677 | (int)GRN_IO_IDSTR_LEN, |
| 678 | GRN_IO_IDSTR); |
| 679 | grn_close(fd); |
| 680 | return NULL; |
| 681 | } |
| 682 | header_size = h.header_size; |
| 683 | segment_size = h.segment_size; |
| 684 | max_segment = h.max_segment; |
| 685 | flags = h.flags; |
| 686 | grn_close(fd); |
| 687 | if (segment_size == 0) { |
| 688 | ERR(GRN_INCOMPATIBLE_FILE_FORMAT, |
| 689 | "[io][open] failed to open: segment size is 0: <%s>", |
| 690 | path); |
| 691 | return NULL; |
| 692 | } |
| 693 | } |
| 694 | b = grn_io_compute_base(header_size); |
| 695 | bs = grn_io_compute_base_segment(b, segment_size); |
| 696 | grn_fileinfo_init(&fi, 1); |
| 697 | grn_rc rc = grn_fileinfo_open(ctx, &fi, path, O_RDWR); |
| 698 | if (rc != GRN_SUCCESS) { |
| 699 | ERR(rc, "[io][open] failed to open fileinfo: <%s>", path); |
| 700 | return NULL; |
| 701 | } |
| 702 | |
| 703 | struct _grn_io_header *header; |
| 704 | header = GRN_MMAP(ctx, NULL, &(fi.fmo), &fi, 0, b); |
| 705 | if (!header) { |
| 706 | grn_fileinfo_close(ctx, &fi); |
| 707 | ERR(GRN_NO_MEMORY_AVAILABLE, "[io][open] failed to map: <%s>", path); |
| 708 | return NULL; |
| 709 | } |
| 710 | |
| 711 | uint32_t file_size = grn_io_compute_file_size(header->version); |
| 712 | uint32_t max_nfiles = |
| 713 | grn_io_compute_max_n_files(segment_size, max_segment, bs, file_size); |
| 714 | fileinfo *fis = GRN_MALLOCN(fileinfo, max_nfiles); |
| 715 | if (!fis) { |
| 716 | GRN_MUNMAP(ctx, NULL, &(fi.fmo), &fi, header, b); |
| 717 | grn_fileinfo_close(ctx, &fi); |
| 718 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 719 | "[io][open] failed to allocate fileinfo: <%s>", |
| 720 | path); |
| 721 | return NULL; |
| 722 | } |
| 723 | |
| 724 | grn_fileinfo_init(fis, max_nfiles); |
| 725 | grn_memcpy(fis, &fi, sizeof(fileinfo)); |
| 726 | grn_io *io = GRN_CALLOC(sizeof(grn_io)); |
| 727 | if (!io) { |
| 728 | GRN_FREE(fis); |
| 729 | GRN_MUNMAP(ctx, NULL, &(fi.fmo), &fi, header, b); |
| 730 | grn_fileinfo_close(ctx, &fi); |
| 731 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 732 | "[io][open] failed to allocate grn_io: <%s>", |
| 733 | path); |
| 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | grn_io_mapinfo *maps = GRN_CALLOC(sizeof(grn_io_mapinfo) * max_segment); |
| 738 | if (!maps) { |
| 739 | GRN_FREE(io); |
| 740 | GRN_FREE(fis); |
| 741 | GRN_MUNMAP(ctx, NULL, &(fi.fmo), &fi, header, b); |
| 742 | grn_fileinfo_close(ctx, &fi); |
| 743 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 744 | "[io][open] failed to allocate grn_io_mapinfo: <%s>", |
| 745 | path); |
| 746 | return NULL; |
| 747 | } |
| 748 | |
| 749 | grn_strncpy(io->path, PATH_MAX, path, path_length + 1); |
| 750 | io->header = header; |
| 751 | io->user_header = (((byte *)header) + IO_HEADER_SIZE); |
| 752 | io->maps = maps; |
| 753 | io->base = b; |
| 754 | io->base_seg = bs; |
| 755 | io->mode = mode; |
| 756 | io->fis = fis; |
| 757 | io->ainfo = NULL; |
| 758 | io->max_map_seg = 0; |
| 759 | io->nmaps = 0; |
| 760 | io->count = 0; |
| 761 | io->flags = header->flags; |
| 762 | io->lock = &header->lock; |
| 763 | rc = array_init(ctx, io, io->header->n_arrays); |
| 764 | if (rc != GRN_SUCCESS) { |
| 765 | GRN_FREE(io->maps); |
| 766 | GRN_FREE(io); |
| 767 | GRN_FREE(fis); |
| 768 | GRN_MUNMAP(ctx, NULL, &(fi.fmo), &fi, header, b); |
| 769 | grn_fileinfo_close(ctx, &fi); |
| 770 | ERR(rc, "[io][open] failed to initialize array: <%s>", path); |
| 771 | return NULL; |
| 772 | } |
| 773 | |
| 774 | grn_io_register(ctx, io); |
| 775 | return io; |
| 776 | } |
| 777 | |
| 778 | grn_rc |
| 779 | grn_io_close(grn_ctx *ctx, grn_io *io) |
| 780 | { |
| 781 | uint32_t max_nfiles; |
| 782 | |
| 783 | max_nfiles = grn_io_max_n_files(io); |
| 784 | grn_io_unregister(ctx, io); |
| 785 | if (io->ainfo) { |
| 786 | GRN_FREE(io->ainfo); |
| 787 | } |
| 788 | if (io->maps) { |
| 789 | uint32_t i; |
| 790 | uint32_t max_segment; |
| 791 | uint32_t segment_size; |
| 792 | uint32_t file_size; |
| 793 | uint32_t n_segments_per_file; |
| 794 | |
| 795 | max_segment = grn_io_max_segment(io); |
| 796 | segment_size = io->header->segment_size; |
| 797 | file_size = grn_io_compute_file_size(io->header->version); |
| 798 | n_segments_per_file = file_size / segment_size; |
| 799 | for (i = 0; i < max_segment; i++) { |
| 800 | grn_io_mapinfo *mi; |
| 801 | mi = &(io->maps[i]); |
| 802 | if (mi->map) { |
| 803 | fileinfo *fi = NULL; |
| 804 | /* if (atomic_read(mi->nref)) { return STILL_IN_USE ; } */ |
| 805 | if (io->fis) { |
| 806 | uint32_t bseg = i + io->base_seg; |
| 807 | uint32_t fno = bseg / n_segments_per_file; |
| 808 | fi = &io->fis[fno]; |
| 809 | } |
| 810 | GRN_MUNMAP(ctx, io, &mi->fmo, fi, mi->map, segment_size); |
| 811 | } |
| 812 | } |
| 813 | GRN_FREE(io->maps); |
| 814 | } |
| 815 | GRN_MUNMAP(ctx, |
| 816 | io, |
| 817 | (io->fis ? &io->fis->fmo : NULL), |
| 818 | io->fis, |
| 819 | io->header, |
| 820 | io->base); |
| 821 | if (io->fis) { |
| 822 | uint32_t i; |
| 823 | for (i = 0; i < max_nfiles; i++) { |
| 824 | fileinfo *fi = &(io->fis[i]); |
| 825 | grn_fileinfo_close(ctx, fi); |
| 826 | } |
| 827 | GRN_FREE(io->fis); |
| 828 | } |
| 829 | GRN_FREE(io); |
| 830 | return GRN_SUCCESS; |
| 831 | } |
| 832 | |
| 833 | uint32_t |
| 834 | grn_io_base_seg(grn_io *io) |
| 835 | { |
| 836 | return io->base_seg; |
| 837 | } |
| 838 | |
| 839 | const char * |
| 840 | grn_io_path(grn_io *io) |
| 841 | { |
| 842 | return io->path; |
| 843 | } |
| 844 | |
| 845 | void * |
| 846 | grn_io_header(grn_io *io) |
| 847 | { |
| 848 | return io->user_header; |
| 849 | } |
| 850 | |
| 851 | grn_rc |
| 852 | grn_io_set_type(grn_io *io, uint32_t type) |
| 853 | { |
| 854 | if (!io || !io->header) { |
| 855 | return GRN_INVALID_ARGUMENT; |
| 856 | } |
| 857 | io->header->type = type; |
| 858 | return GRN_SUCCESS; |
| 859 | } |
| 860 | |
| 861 | uint32_t |
| 862 | grn_io_get_type(grn_io *io) |
| 863 | { |
| 864 | if (!io || !io->header) { |
| 865 | return GRN_VOID; |
| 866 | } |
| 867 | return io->header->type; |
| 868 | } |
| 869 | |
| 870 | static inline void |
| 871 | gen_pathname(const char *path, char *buffer, uint32_t fno) |
| 872 | { |
| 873 | size_t len = strlen(path); |
| 874 | grn_memcpy(buffer, path, len); |
| 875 | if (fno) { |
| 876 | buffer[len] = '.'; |
| 877 | grn_itoh(fno, buffer + len + 1, 3); |
| 878 | buffer[len + 4] = '\0'; |
| 879 | } else { |
| 880 | buffer[len] = '\0'; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | static uint32_t |
| 885 | grn_io_n_files(grn_ctx *ctx, grn_io *io) |
| 886 | { |
| 887 | unsigned long file_size; |
| 888 | file_size = grn_io_compute_file_size(io->header->version); |
| 889 | return (uint32_t)((io->header->curr_size + file_size - 1) / file_size); |
| 890 | } |
| 891 | |
| 892 | grn_rc |
| 893 | grn_io_size(grn_ctx *ctx, grn_io *io, uint64_t *size) |
| 894 | { |
| 895 | uint32_t fno; |
| 896 | struct stat s; |
| 897 | uint64_t tsize = 0; |
| 898 | char buffer[PATH_MAX]; |
| 899 | uint32_t n_files; |
| 900 | |
| 901 | n_files = grn_io_n_files(ctx, io); |
| 902 | for (fno = 0; fno < n_files; fno++) { |
| 903 | gen_pathname(io->path, buffer, fno); |
| 904 | if (stat(buffer, &s)) { |
| 905 | SERR("failed to stat path to compute size: <%s>", buffer); |
| 906 | } else { |
| 907 | tsize += (uint64_t)(s.st_size); |
| 908 | } |
| 909 | } |
| 910 | *size = tsize; |
| 911 | return GRN_SUCCESS; |
| 912 | } |
| 913 | |
| 914 | grn_rc |
| 915 | grn_io_remove_raw(grn_ctx *ctx, const char *path) |
| 916 | { |
| 917 | grn_rc rc = GRN_SUCCESS; |
| 918 | uint32_t fno; |
| 919 | char buffer[PATH_MAX]; |
| 920 | |
| 921 | if (grn_unlink(path) != 0) { |
| 922 | ERRNO_ERR("[io][remove] failed to remove path: <%s>", path); |
| 923 | return ctx->rc; |
| 924 | } |
| 925 | GRN_LOG(ctx, GRN_LOG_INFO, "[io][remove] removed path: <%s>", path); |
| 926 | |
| 927 | for (fno = 1;; fno++) { |
| 928 | struct stat s; |
| 929 | gen_pathname(path, buffer, fno); |
| 930 | if (stat(buffer, &s) != 0) { |
| 931 | break; |
| 932 | } |
| 933 | if (grn_unlink(buffer) == 0) { |
| 934 | GRN_LOG(ctx, |
| 935 | GRN_LOG_INFO, |
| 936 | "[io][remove] removed numbered path: <%d>: <%s>", |
| 937 | fno, |
| 938 | buffer); |
| 939 | } else { |
| 940 | ERRNO_ERR("[io][remove] failed to remove numbered path: <%d>: <%s>", |
| 941 | fno, |
| 942 | buffer); |
| 943 | rc = ctx->rc; |
| 944 | } |
| 945 | } |
| 946 | return rc; |
| 947 | } |
| 948 | |
| 949 | grn_rc |
| 950 | grn_io_remove(grn_ctx *ctx, const char *path) |
| 951 | { |
| 952 | struct stat s; |
| 953 | |
| 954 | if (stat(path, &s) != 0) { |
| 955 | SERR("failed to stat: <%s>", path); |
| 956 | return ctx->rc; |
| 957 | } |
| 958 | |
| 959 | return grn_io_remove_raw(ctx, path); |
| 960 | } |
| 961 | |
| 962 | grn_rc |
| 963 | grn_io_remove_if_exist(grn_ctx *ctx, const char *path) |
| 964 | { |
| 965 | struct stat s; |
| 966 | if (stat(path, &s) == 0) { |
| 967 | return grn_io_remove_raw(ctx, path); |
| 968 | } |
| 969 | return GRN_SUCCESS; |
| 970 | } |
| 971 | |
| 972 | grn_rc |
| 973 | grn_io_rename(grn_ctx *ctx, const char *old_name, const char *new_name) |
| 974 | { |
| 975 | const char *tag = "[io][rename]"; |
| 976 | struct stat s; |
| 977 | if (stat(old_name, &s)) { |
| 978 | SERR("%s failed to stat path to be renamed: <%s>", tag, old_name); |
| 979 | return ctx->rc; |
| 980 | } else if (rename(old_name, new_name)) { |
| 981 | SERR("%s failed to rename path: <%s> -> <%s>", tag, old_name, new_name); |
| 982 | return ctx->rc; |
| 983 | } else { |
| 984 | uint32_t fno; |
| 985 | char old_buffer[PATH_MAX]; |
| 986 | char new_buffer[PATH_MAX]; |
| 987 | for (fno = 1;; fno++) { |
| 988 | gen_pathname(old_name, old_buffer, fno); |
| 989 | if (stat(old_buffer, &s) == 0) { |
| 990 | break; |
| 991 | } |
| 992 | gen_pathname(new_name, new_buffer, fno); |
| 993 | if (rename(old_buffer, new_buffer)) { |
| 994 | SERR("%s failed to rename path: <%s> -> <%s>", |
| 995 | tag, |
| 996 | old_buffer, |
| 997 | new_buffer); |
| 998 | } |
| 999 | } |
| 1000 | return GRN_SUCCESS; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | typedef struct { |
| 1005 | grn_io_ja_ehead head; |
| 1006 | char body[256]; |
| 1007 | } ja_element; |
| 1008 | |
| 1009 | grn_rc |
| 1010 | grn_io_read_ja(grn_io *io, |
| 1011 | grn_ctx *ctx, |
| 1012 | grn_io_ja_einfo *einfo, |
| 1013 | uint32_t epos, |
| 1014 | uint32_t key, |
| 1015 | uint32_t segment, |
| 1016 | uint32_t offset, |
| 1017 | void **value, |
| 1018 | uint32_t *value_len) |
| 1019 | { |
| 1020 | size_t rest = 0; |
| 1021 | size_t size = *value_len + sizeof(grn_io_ja_ehead); |
| 1022 | uint32_t segment_size = io->header->segment_size; |
| 1023 | uint32_t file_size = grn_io_compute_file_size(io->header->version); |
| 1024 | uint32_t n_segments_per_file = file_size / segment_size; |
| 1025 | uint32_t bseg = segment + io->base_seg; |
| 1026 | uint32_t fno = bseg / n_segments_per_file; |
| 1027 | fileinfo *fi = &io->fis[fno]; |
| 1028 | off_t base = fno ? 0 : (off_t)(io->base - segment_size * io->base_seg); |
| 1029 | off_t pos = segment_size * (bseg % n_segments_per_file) + offset + base; |
| 1030 | ja_element *v = GRN_CALLOC(size); |
| 1031 | if (!v) { |
| 1032 | *value = NULL; |
| 1033 | *value_len = 0; |
| 1034 | return GRN_NO_MEMORY_AVAILABLE; |
| 1035 | } |
| 1036 | if (pos + (off_t)size > (off_t)file_size) { |
| 1037 | rest = (size_t)(pos + (off_t)size - file_size); |
| 1038 | size = (size_t)(file_size - pos); |
| 1039 | } |
| 1040 | if (!grn_fileinfo_opened(fi)) { |
| 1041 | char path[PATH_MAX]; |
| 1042 | gen_pathname(io->path, path, fno); |
| 1043 | if (grn_fileinfo_open(ctx, fi, path, O_RDWR | O_CREAT)) { |
| 1044 | *value = NULL; |
| 1045 | *value_len = 0; |
| 1046 | GRN_FREE(v); |
| 1047 | return ctx->rc; |
| 1048 | } |
| 1049 | } |
| 1050 | if (grn_pread(ctx, fi, v, size, pos)) { |
| 1051 | *value = NULL; |
| 1052 | *value_len = 0; |
| 1053 | GRN_FREE(v); |
| 1054 | return ctx->rc; |
| 1055 | } |
| 1056 | if (einfo->pos != epos) { |
| 1057 | GRN_LOG(ctx, |
| 1058 | GRN_LOG_WARNING, |
| 1059 | "einfo pos changed %x => %x", |
| 1060 | einfo->pos, |
| 1061 | epos); |
| 1062 | *value = NULL; |
| 1063 | *value_len = 0; |
| 1064 | GRN_FREE(v); |
| 1065 | return GRN_FILE_CORRUPT; |
| 1066 | } |
| 1067 | if (einfo->size != *value_len) { |
| 1068 | GRN_LOG(ctx, |
| 1069 | GRN_LOG_WARNING, |
| 1070 | "einfo size changed %d => %d", |
| 1071 | einfo->size, |
| 1072 | *value_len); |
| 1073 | *value = NULL; |
| 1074 | *value_len = 0; |
| 1075 | GRN_FREE(v); |
| 1076 | return GRN_FILE_CORRUPT; |
| 1077 | } |
| 1078 | if (v->head.key != key) { |
| 1079 | GRN_LOG(ctx, GRN_LOG_ERROR, "ehead key unmatch %x => %x", key, v->head.key); |
| 1080 | *value = NULL; |
| 1081 | *value_len = 0; |
| 1082 | GRN_FREE(v); |
| 1083 | return GRN_INVALID_FORMAT; |
| 1084 | } |
| 1085 | if (v->head.size != *value_len) { |
| 1086 | GRN_LOG(ctx, |
| 1087 | GRN_LOG_ERROR, |
| 1088 | "ehead size unmatch %d => %d", |
| 1089 | *value_len, |
| 1090 | v->head.size); |
| 1091 | *value = NULL; |
| 1092 | *value_len = 0; |
| 1093 | GRN_FREE(v); |
| 1094 | return GRN_INVALID_FORMAT; |
| 1095 | } |
| 1096 | if (rest) { |
| 1097 | byte *vr = (byte *)v + size; |
| 1098 | do { |
| 1099 | fi = &io->fis[++fno]; |
| 1100 | if (!grn_fileinfo_opened(fi)) { |
| 1101 | char path[PATH_MAX]; |
| 1102 | gen_pathname(io->path, path, fno); |
| 1103 | if (grn_fileinfo_open(ctx, fi, path, O_RDWR | O_CREAT)) { |
| 1104 | *value = NULL; |
| 1105 | *value_len = 0; |
| 1106 | GRN_FREE(v); |
| 1107 | return ctx->rc; |
| 1108 | } |
| 1109 | } |
| 1110 | size = rest > file_size ? file_size : rest; |
| 1111 | if (grn_pread(ctx, fi, vr, size, 0)) { |
| 1112 | *value = NULL; |
| 1113 | *value_len = 0; |
| 1114 | GRN_FREE(v); |
| 1115 | return ctx->rc; |
| 1116 | } |
| 1117 | vr += size; |
| 1118 | rest -= size; |
| 1119 | } while (rest); |
| 1120 | } |
| 1121 | *value = v->body; |
| 1122 | return GRN_SUCCESS; |
| 1123 | } |
| 1124 | |
| 1125 | grn_rc |
| 1126 | grn_io_write_ja(grn_io *io, |
| 1127 | grn_ctx *ctx, |
| 1128 | uint32_t key, |
| 1129 | uint32_t segment, |
| 1130 | uint32_t offset, |
| 1131 | void *value, |
| 1132 | uint32_t value_len) |
| 1133 | { |
| 1134 | grn_rc rc; |
| 1135 | size_t rest = 0; |
| 1136 | size_t size = value_len + sizeof(grn_io_ja_ehead); |
| 1137 | uint32_t segment_size = io->header->segment_size; |
| 1138 | uint32_t file_size = grn_io_compute_file_size(io->header->version); |
| 1139 | uint32_t n_segments_per_file = file_size / segment_size; |
| 1140 | uint32_t bseg = segment + io->base_seg; |
| 1141 | uint32_t fno = bseg / n_segments_per_file; |
| 1142 | fileinfo *fi = &io->fis[fno]; |
| 1143 | off_t base = fno ? 0 : (off_t)(io->base) - segment_size * io->base_seg; |
| 1144 | off_t pos = segment_size * (bseg % n_segments_per_file) + offset + base; |
| 1145 | if (pos + (off_t)size > (off_t)file_size) { |
| 1146 | rest = (size_t)(pos + (off_t)size - file_size); |
| 1147 | size = (size_t)(file_size - pos); |
| 1148 | } |
| 1149 | if (!grn_fileinfo_opened(fi)) { |
| 1150 | char path[PATH_MAX]; |
| 1151 | gen_pathname(io->path, path, fno); |
| 1152 | if ((rc = grn_fileinfo_open(ctx, fi, path, O_RDWR | O_CREAT))) { |
| 1153 | return rc; |
| 1154 | } |
| 1155 | } |
| 1156 | if (value_len <= 256) { |
| 1157 | ja_element je; |
| 1158 | je.head.size = value_len; |
| 1159 | je.head.key = key; |
| 1160 | grn_memcpy(je.body, value, value_len); |
| 1161 | rc = grn_pwrite(ctx, fi, &je, size, pos); |
| 1162 | } else { |
| 1163 | grn_io_ja_ehead eh; |
| 1164 | eh.size = value_len; |
| 1165 | eh.key = key; |
| 1166 | if ((rc = grn_pwrite(ctx, fi, &eh, sizeof(grn_io_ja_ehead), pos))) { |
| 1167 | return rc; |
| 1168 | } |
| 1169 | pos += (off_t)sizeof(grn_io_ja_ehead); |
| 1170 | rc = grn_pwrite(ctx, fi, value, size - sizeof(grn_io_ja_ehead), pos); |
| 1171 | } |
| 1172 | if (rc) { |
| 1173 | return rc; |
| 1174 | } |
| 1175 | if (rest) { |
| 1176 | byte *vr = (byte *)value + size - sizeof(grn_io_ja_ehead); |
| 1177 | do { |
| 1178 | fi = &io->fis[++fno]; |
| 1179 | if (!grn_fileinfo_opened(fi)) { |
| 1180 | char path[PATH_MAX]; |
| 1181 | gen_pathname(io->path, path, fno); |
| 1182 | if ((rc = grn_fileinfo_open(ctx, fi, path, O_RDWR | O_CREAT))) { |
| 1183 | return rc; |
| 1184 | } |
| 1185 | } |
| 1186 | size = rest > file_size ? file_size : rest; |
| 1187 | if ((rc = grn_pwrite(ctx, fi, vr, size, 0))) { |
| 1188 | return rc; |
| 1189 | } |
| 1190 | vr += size; |
| 1191 | rest -= size; |
| 1192 | } while (rest); |
| 1193 | } |
| 1194 | return rc; |
| 1195 | } |
| 1196 | |
| 1197 | grn_rc |
| 1198 | grn_io_write_ja_ehead(grn_io *io, |
| 1199 | grn_ctx *ctx, |
| 1200 | uint32_t key, |
| 1201 | uint32_t segment, |
| 1202 | uint32_t offset, |
| 1203 | uint32_t value_len) |
| 1204 | { |
| 1205 | grn_rc rc; |
| 1206 | uint32_t segment_size = io->header->segment_size; |
| 1207 | uint32_t file_size = grn_io_compute_file_size(io->header->version); |
| 1208 | uint32_t n_segments_per_file = file_size / segment_size; |
| 1209 | uint32_t bseg = segment + io->base_seg; |
| 1210 | uint32_t fno = bseg / n_segments_per_file; |
| 1211 | fileinfo *fi = &io->fis[fno]; |
| 1212 | off_t base = fno ? 0 : (off_t)(io->base) - segment_size + io->base_seg; |
| 1213 | off_t pos = segment_size * (bseg % n_segments_per_file) + offset + base; |
| 1214 | if (!grn_fileinfo_opened(fi)) { |
| 1215 | char path[PATH_MAX]; |
| 1216 | gen_pathname(io->path, path, fno); |
| 1217 | if ((rc = grn_fileinfo_open(ctx, fi, path, O_RDWR | O_CREAT))) { |
| 1218 | return rc; |
| 1219 | } |
| 1220 | } |
| 1221 | { |
| 1222 | grn_io_ja_ehead eh; |
| 1223 | eh.size = value_len; |
| 1224 | eh.key = key; |
| 1225 | return grn_pwrite(ctx, fi, &eh, sizeof(grn_io_ja_ehead), pos); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | void * |
| 1230 | grn_io_win_map(grn_ctx *ctx, |
| 1231 | grn_io *io, |
| 1232 | grn_io_win *iw, |
| 1233 | uint32_t segment, |
| 1234 | uint32_t offset, |
| 1235 | uint32_t size, |
| 1236 | grn_io_rw_mode mode) |
| 1237 | { |
| 1238 | uint32_t nseg, segment_size = io->header->segment_size; |
| 1239 | if (offset >= segment_size) { |
| 1240 | segment += offset / segment_size; |
| 1241 | offset = offset % segment_size; |
| 1242 | } |
| 1243 | nseg = (offset + size + segment_size - 1) / segment_size; |
| 1244 | if (!size || !ctx || segment + nseg > io->header->max_segment) { |
| 1245 | return NULL; |
| 1246 | } |
| 1247 | iw->ctx = ctx; |
| 1248 | iw->diff = 0; |
| 1249 | iw->io = io; |
| 1250 | iw->mode = (uint8_t)mode; |
| 1251 | iw->tiny_p = 0; |
| 1252 | iw->segment = segment; |
| 1253 | iw->offset = offset; |
| 1254 | iw->nseg = nseg; |
| 1255 | iw->size = size; |
| 1256 | if (nseg == 1) { |
| 1257 | byte *addr = grn_io_seg_ref(ctx, io, segment); |
| 1258 | if (!addr) { |
| 1259 | return NULL; |
| 1260 | } |
| 1261 | iw->cached = 1; |
| 1262 | iw->addr = addr + offset; |
| 1263 | } else { |
| 1264 | if (!(iw->addr = GRN_CALLOC(size))) { |
| 1265 | return NULL; |
| 1266 | } |
| 1267 | iw->cached = 0; |
| 1268 | switch (mode) { |
| 1269 | case GRN_IO_RDONLY: |
| 1270 | case GRN_IO_RDWR: |
| 1271 | { |
| 1272 | byte *p, *q = NULL; |
| 1273 | uint32_t s, r; |
| 1274 | for (p = iw->addr, r = size; r; p += s, r -= s, segment++, offset = 0) { |
| 1275 | q = grn_io_seg_ref(ctx, io, segment); |
| 1276 | if (!q) { |
| 1277 | GRN_FREE(iw->addr); |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | s = (offset + r > segment_size) ? segment_size - offset : r; |
| 1281 | grn_memcpy(p, q + offset, s); |
| 1282 | grn_io_seg_unref(ctx, io, segment); |
| 1283 | } |
| 1284 | } |
| 1285 | break; |
| 1286 | case GRN_IO_WRONLY: |
| 1287 | break; |
| 1288 | default: |
| 1289 | return NULL; |
| 1290 | } |
| 1291 | } |
| 1292 | return iw->addr; |
| 1293 | } |
| 1294 | |
| 1295 | grn_rc |
| 1296 | grn_io_win_unmap(grn_ctx *ctx, grn_io_win *iw) |
| 1297 | { |
| 1298 | if (!iw || !iw->io || !iw->ctx) { |
| 1299 | return GRN_INVALID_ARGUMENT; |
| 1300 | } |
| 1301 | if (iw->cached) { |
| 1302 | if (!iw->tiny_p) { |
| 1303 | grn_io_seg_unref(ctx, iw->io, iw->segment); |
| 1304 | } |
| 1305 | return GRN_SUCCESS; |
| 1306 | } |
| 1307 | { |
| 1308 | grn_io *io = iw->io; |
| 1309 | grn_ctx *ctx = iw->ctx; |
| 1310 | switch (iw->mode) { |
| 1311 | case GRN_IO_RDONLY: |
| 1312 | if (!iw->addr) { |
| 1313 | return GRN_INVALID_ARGUMENT; |
| 1314 | } |
| 1315 | GRN_FREE(iw->addr); |
| 1316 | return GRN_SUCCESS; |
| 1317 | case GRN_IO_RDWR: |
| 1318 | case GRN_IO_WRONLY: |
| 1319 | { |
| 1320 | byte *p, *q = NULL; |
| 1321 | uint32_t segment_size = io->header->segment_size; |
| 1322 | uint32_t s, r, offset = iw->offset, segment = iw->segment; |
| 1323 | for (p = iw->addr, r = iw->size; r; |
| 1324 | p += s, r -= s, segment++, offset = 0) { |
| 1325 | q = grn_io_seg_ref(ctx, io, segment); |
| 1326 | if (!q) { |
| 1327 | return GRN_NO_MEMORY_AVAILABLE; |
| 1328 | } |
| 1329 | s = (offset + r > segment_size) ? segment_size - offset : r; |
| 1330 | grn_memcpy(q + offset, p, s); |
| 1331 | grn_io_seg_unref(ctx, io, segment); |
| 1332 | } |
| 1333 | } |
| 1334 | GRN_FREE(iw->addr); |
| 1335 | return GRN_SUCCESS; |
| 1336 | default: |
| 1337 | return GRN_INVALID_ARGUMENT; |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | #define DO_MAP(io, fmo, fi, pos, size, segno, res) \ |
| 1343 | do { \ |
| 1344 | (res) = GRN_MMAP(ctx, (io), (fmo), (fi), (pos), (size)); \ |
| 1345 | if ((res)) { \ |
| 1346 | uint32_t nmaps; \ |
| 1347 | if (io->max_map_seg < segno) { \ |
| 1348 | io->max_map_seg = segno; \ |
| 1349 | } \ |
| 1350 | GRN_ATOMIC_ADD_EX(&io->nmaps, 1, nmaps); \ |
| 1351 | { \ |
| 1352 | uint64_t tail = io->base + (uint64_t)(size) * ((segno) + 1); \ |
| 1353 | if (tail > io->header->curr_size) { \ |
| 1354 | io->header->curr_size = tail; \ |
| 1355 | } \ |
| 1356 | } \ |
| 1357 | } \ |
| 1358 | } while (0) |
| 1359 | |
| 1360 | void |
| 1361 | grn_io_seg_map_(grn_ctx *ctx, grn_io *io, uint32_t segno, grn_io_mapinfo *info) |
| 1362 | { |
| 1363 | uint32_t segment_size = io->header->segment_size; |
| 1364 | if ((io->flags & GRN_IO_TEMPORARY)) { |
| 1365 | DO_MAP(io, &info->fmo, NULL, 0, segment_size, segno, info->map); |
| 1366 | } else { |
| 1367 | uint32_t file_size = grn_io_compute_file_size(io->header->version); |
| 1368 | uint32_t n_segments_per_file = file_size / segment_size; |
| 1369 | uint32_t bseg = segno + io->base_seg; |
| 1370 | uint32_t fno = bseg / n_segments_per_file; |
| 1371 | off_t base = fno ? 0 : (off_t)(io->base) - segment_size * io->base_seg; |
| 1372 | off_t pos = segment_size * (bseg % n_segments_per_file) + base; |
| 1373 | fileinfo *fi = &io->fis[fno]; |
| 1374 | if (!grn_fileinfo_opened(fi)) { |
| 1375 | char path[PATH_MAX]; |
| 1376 | gen_pathname(io->path, path, fno); |
| 1377 | bool path_exist = grn_path_exist(path); |
| 1378 | if (!grn_fileinfo_open(ctx, fi, path, O_RDWR | O_CREAT)) { |
| 1379 | DO_MAP(io, &info->fmo, fi, pos, segment_size, segno, info->map); |
| 1380 | if (!info->map && !path_exist) { |
| 1381 | grn_fileinfo_close(ctx, fi); |
| 1382 | if (grn_unlink(path) == 0) { |
| 1383 | GRN_LOG(ctx, |
| 1384 | GRN_LOG_INFO, |
| 1385 | "[io][map][error] memory mapping is failed and then " |
| 1386 | "removed created map file: <%s>", |
| 1387 | path); |
| 1388 | } else { |
| 1389 | ERRNO_ERR("[io][map][error] memory mapping is failed and then " |
| 1390 | "failed to remove created map file: <%s>", |
| 1391 | path); |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | } else { |
| 1396 | DO_MAP(io, &info->fmo, fi, pos, segment_size, segno, info->map); |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | grn_rc |
| 1402 | grn_io_seg_expire(grn_ctx *ctx, grn_io *io, uint32_t segno, uint32_t nretry) |
| 1403 | { |
| 1404 | uint32_t retry, *pnref; |
| 1405 | grn_io_mapinfo *info; |
| 1406 | if (!io->maps || segno >= io->header->max_segment) { |
| 1407 | return GRN_INVALID_ARGUMENT; |
| 1408 | } |
| 1409 | info = &io->maps[segno]; |
| 1410 | if (!info->map) { |
| 1411 | return GRN_INVALID_ARGUMENT; |
| 1412 | } |
| 1413 | pnref = &info->nref; |
| 1414 | for (retry = 0;; retry++) { |
| 1415 | uint32_t nref; |
| 1416 | GRN_ATOMIC_ADD_EX(pnref, 1, nref); |
| 1417 | if (nref) { |
| 1418 | GRN_ATOMIC_ADD_EX(pnref, -1, nref); |
| 1419 | if (retry >= GRN_IO_MAX_RETRY) { |
| 1420 | GRN_LOG(ctx, |
| 1421 | GRN_LOG_CRIT, |
| 1422 | "deadlock detected! in grn_io_seg_expire(%p, %u, %u)", |
| 1423 | io, |
| 1424 | segno, |
| 1425 | nref); |
| 1426 | return GRN_RESOURCE_DEADLOCK_AVOIDED; |
| 1427 | } |
| 1428 | } else { |
| 1429 | GRN_ATOMIC_ADD_EX(pnref, GRN_IO_MAX_REF, nref); |
| 1430 | uint32_t reset_max_ref_by_overflow = GRN_IO_MAX_REF - 1; |
| 1431 | if (nref > 1) { |
| 1432 | GRN_ATOMIC_ADD_EX(pnref, reset_max_ref_by_overflow, nref); |
| 1433 | GRN_FUTEX_WAKE(pnref); |
| 1434 | if (retry >= GRN_IO_MAX_RETRY) { |
| 1435 | GRN_LOG(ctx, |
| 1436 | GRN_LOG_CRIT, |
| 1437 | "deadlock detected!! in grn_io_seg_expire(%p, %u, %u)", |
| 1438 | io, |
| 1439 | segno, |
| 1440 | nref); |
| 1441 | return GRN_RESOURCE_DEADLOCK_AVOIDED; |
| 1442 | } |
| 1443 | } else { |
| 1444 | uint32_t nmaps; |
| 1445 | fileinfo *fi = &(io->fis[segno]); |
| 1446 | GRN_MUNMAP(ctx, |
| 1447 | io, |
| 1448 | &info->fmo, |
| 1449 | fi, |
| 1450 | info->map, |
| 1451 | io->header->segment_size); |
| 1452 | info->map = NULL; |
| 1453 | GRN_ATOMIC_ADD_EX(pnref, reset_max_ref_by_overflow, nref); |
| 1454 | GRN_ATOMIC_ADD_EX(&io->nmaps, -1, nmaps); |
| 1455 | GRN_FUTEX_WAKE(pnref); |
| 1456 | return GRN_SUCCESS; |
| 1457 | } |
| 1458 | } |
| 1459 | if (retry >= nretry) { |
| 1460 | return GRN_RESOURCE_DEADLOCK_AVOIDED; |
| 1461 | } |
| 1462 | GRN_FUTEX_WAIT(pnref); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | uint32_t |
| 1467 | grn_io_expire(grn_ctx *ctx, grn_io *io, uint32_t count_thresh, uint32_t limit) |
| 1468 | { |
| 1469 | uint32_t m, n = 0, ln = io->nmaps; |
| 1470 | switch ((io->flags & (GRN_IO_EXPIRE_GTICK | GRN_IO_EXPIRE_SEGMENT))) { |
| 1471 | case GRN_IO_EXPIRE_GTICK: |
| 1472 | { |
| 1473 | uint32_t nref, nmaps, *pnref = &io->nref; |
| 1474 | GRN_ATOMIC_ADD_EX(pnref, 1, nref); |
| 1475 | if (!nref && grn_gtick - io->count > count_thresh) { |
| 1476 | { |
| 1477 | uint32_t i = io->header->n_arrays; |
| 1478 | grn_io_array_spec *array_specs = (grn_io_array_spec *)io->user_header; |
| 1479 | while (i--) { |
| 1480 | memset(io->ainfo[i].addrs, |
| 1481 | 0, |
| 1482 | sizeof(void *) * array_specs[i].max_n_segments); |
| 1483 | } |
| 1484 | } |
| 1485 | { |
| 1486 | uint32_t fno; |
| 1487 | for (fno = 0; fno < io->max_map_seg; fno++) { |
| 1488 | grn_io_mapinfo *info = &(io->maps[fno]); |
| 1489 | if (info->map) { |
| 1490 | fileinfo *fi = &(io->fis[fno]); |
| 1491 | GRN_MUNMAP(ctx, |
| 1492 | io, |
| 1493 | &info->fmo, |
| 1494 | fi, |
| 1495 | info->map, |
| 1496 | io->header->segment_size); |
| 1497 | info->map = NULL; |
| 1498 | info->nref = 0; |
| 1499 | info->count = grn_gtick; |
| 1500 | GRN_ATOMIC_ADD_EX(&io->nmaps, -1, nmaps); |
| 1501 | n++; |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | GRN_ATOMIC_ADD_EX(pnref, -1, nref); |
| 1507 | } |
| 1508 | break; |
| 1509 | case GRN_IO_EXPIRE_SEGMENT: |
| 1510 | for (m = io->max_map_seg; n < limit && m; m--) { |
| 1511 | if (!grn_io_seg_expire(ctx, io, m, 0)) { |
| 1512 | n++; |
| 1513 | } |
| 1514 | } |
| 1515 | break; |
| 1516 | case (GRN_IO_EXPIRE_GTICK | GRN_IO_EXPIRE_SEGMENT): |
| 1517 | { |
| 1518 | grn_io_mapinfo *info = io->maps; |
| 1519 | for (m = io->max_map_seg; n < limit && m; info++, m--) { |
| 1520 | if (info->map && (grn_gtick - info->count) > count_thresh) { |
| 1521 | uint32_t nmaps, nref, *pnref = &info->nref; |
| 1522 | GRN_ATOMIC_ADD_EX(pnref, 1, nref); |
| 1523 | if (!nref && info->map && (grn_gtick - info->count) > count_thresh) { |
| 1524 | GRN_MUNMAP(ctx, |
| 1525 | io, |
| 1526 | &info->fmo, |
| 1527 | NULL, |
| 1528 | info->map, |
| 1529 | io->header->segment_size); |
| 1530 | GRN_ATOMIC_ADD_EX(&io->nmaps, -1, nmaps); |
| 1531 | info->map = NULL; |
| 1532 | info->count = grn_gtick; |
| 1533 | n++; |
| 1534 | } |
| 1535 | GRN_ATOMIC_ADD_EX(pnref, -1, nref); |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | break; |
| 1540 | } |
| 1541 | if (n) { |
| 1542 | GRN_LOG(ctx, |
| 1543 | GRN_LOG_DEBUG, |
| 1544 | "<%p:%x> expired i=%p max=%d (%d/%d)", |
| 1545 | ctx, |
| 1546 | grn_gtick, |
| 1547 | io, |
| 1548 | io->max_map_seg, |
| 1549 | n, |
| 1550 | ln); |
| 1551 | } |
| 1552 | return n; |
| 1553 | } |
| 1554 | |
| 1555 | void * |
| 1556 | grn_io_anon_map(grn_ctx *ctx, grn_io_mapinfo *mi, size_t length) |
| 1557 | { |
| 1558 | return (mi->map = GRN_MMAP(ctx, NULL, &mi->fmo, NULL, 0, length)); |
| 1559 | } |
| 1560 | |
| 1561 | void |
| 1562 | grn_io_anon_unmap(grn_ctx *ctx, grn_io_mapinfo *mi, size_t length) |
| 1563 | { |
| 1564 | GRN_MUNMAP(ctx, NULL, &mi->fmo, NULL, mi->map, length); |
| 1565 | } |
| 1566 | |
| 1567 | grn_rc |
| 1568 | grn_io_lock(grn_ctx *ctx, grn_io *io, int timeout) |
| 1569 | { |
| 1570 | static int _ncalls = 0, _ncolls = 0; |
| 1571 | uint32_t count, count_log_border = 1000; |
| 1572 | uint32_t rc_check_interval = 1000; |
| 1573 | _ncalls++; |
| 1574 | if (!io) { |
| 1575 | return GRN_INVALID_ARGUMENT; |
| 1576 | } |
| 1577 | for (count = 0;; count++) { |
| 1578 | uint32_t lock; |
| 1579 | GRN_ATOMIC_ADD_EX(io->lock, 1, lock); |
| 1580 | if (lock) { |
| 1581 | GRN_ATOMIC_ADD_EX(io->lock, -1, lock); |
| 1582 | if (count == count_log_border) { |
| 1583 | GRN_LOG(ctx, |
| 1584 | GRN_LOG_NOTICE, |
| 1585 | "io(%s) collisions(%d/%d): lock failed %d times", |
| 1586 | io->path, |
| 1587 | _ncolls, |
| 1588 | _ncalls, |
| 1589 | count_log_border); |
| 1590 | } |
| 1591 | if (!timeout || (timeout > 0 && (uint32_t)timeout == count)) { |
| 1592 | GRN_LOG(ctx, |
| 1593 | GRN_LOG_WARNING, |
| 1594 | "[DB Locked] time out(%d): io(%s) collisions(%d/%d)", |
| 1595 | timeout, |
| 1596 | io->path, |
| 1597 | _ncolls, |
| 1598 | _ncalls); |
| 1599 | break; |
| 1600 | } |
| 1601 | if (!(++_ncolls % 1000000) && (_ncolls > _ncalls)) { |
| 1602 | if (_ncolls < 0 || _ncalls < 0) { |
| 1603 | _ncolls = 0; |
| 1604 | _ncalls = 0; |
| 1605 | } else { |
| 1606 | GRN_LOG(ctx, |
| 1607 | GRN_LOG_NOTICE, |
| 1608 | "io(%s) collisions(%d/%d)", |
| 1609 | io->path, |
| 1610 | _ncolls, |
| 1611 | _ncalls); |
| 1612 | } |
| 1613 | } |
| 1614 | if ((count % rc_check_interval) == 0) { |
| 1615 | if (ctx->rc != GRN_SUCCESS) { |
| 1616 | return ctx->rc; |
| 1617 | } |
| 1618 | } |
| 1619 | grn_nanosleep(GRN_LOCK_WAIT_TIME_NANOSECOND); |
| 1620 | continue; |
| 1621 | } |
| 1622 | return GRN_SUCCESS; |
| 1623 | } |
| 1624 | ERR(GRN_RESOURCE_DEADLOCK_AVOIDED, "grn_io_lock failed"); |
| 1625 | return ctx->rc; |
| 1626 | } |
| 1627 | |
| 1628 | void |
| 1629 | grn_io_unlock(grn_ctx *ctx, grn_io *io) |
| 1630 | { |
| 1631 | if (io) { |
| 1632 | uint32_t lock; |
| 1633 | GRN_ATOMIC_ADD_EX(io->lock, -1, lock); |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | void |
| 1638 | grn_io_clear_lock(grn_io *io) |
| 1639 | { |
| 1640 | if (io) { |
| 1641 | *io->lock = 0; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | uint32_t |
| 1646 | grn_io_is_locked(grn_io *io) |
| 1647 | { |
| 1648 | return io ? *io->lock : 0; |
| 1649 | } |
| 1650 | |
| 1651 | grn_rc |
| 1652 | grn_io_flush(grn_ctx *ctx, grn_io *io) |
| 1653 | { |
| 1654 | grn_rc rc = GRN_SUCCESS; |
| 1655 | struct _grn_io_header *header; |
| 1656 | uint32_t aligned_header_size; |
| 1657 | |
| 1658 | if (io->path[0] == '\0') { |
| 1659 | return GRN_SUCCESS; |
| 1660 | } |
| 1661 | |
| 1662 | header = io->header; |
| 1663 | aligned_header_size = grn_io_compute_base(header->header_size); |
| 1664 | |
| 1665 | if (GRN_MSYNC(ctx, &(io->fis[0]), header, aligned_header_size) != 0) { |
| 1666 | return ctx->rc; |
| 1667 | } |
| 1668 | |
| 1669 | if (io->maps) { |
| 1670 | uint32_t i; |
| 1671 | uint32_t max_mapped_segment; |
| 1672 | uint32_t segment_size; |
| 1673 | |
| 1674 | max_mapped_segment = grn_io_max_segment(io); |
| 1675 | segment_size = header->segment_size; |
| 1676 | for (i = 0; i < max_mapped_segment; i++) { |
| 1677 | grn_io_mapinfo *info = &(io->maps[i]); |
| 1678 | uint32_t nth_file_info; |
| 1679 | uint32_t *pnref; |
| 1680 | uint32_t nref; |
| 1681 | int msync_result; |
| 1682 | |
| 1683 | if (!info) { |
| 1684 | continue; |
| 1685 | } |
| 1686 | |
| 1687 | pnref = &info->nref; |
| 1688 | GRN_ATOMIC_ADD_EX(pnref, 1, nref); |
| 1689 | if (nref != 0) { |
| 1690 | GRN_ATOMIC_ADD_EX(pnref, -1, nref); |
| 1691 | continue; |
| 1692 | } |
| 1693 | |
| 1694 | if (!info->map) { |
| 1695 | GRN_ATOMIC_ADD_EX(pnref, -1, nref); |
| 1696 | GRN_FUTEX_WAKE(pnref); |
| 1697 | continue; |
| 1698 | } |
| 1699 | |
| 1700 | nth_file_info = grn_io_compute_nth_file_info(io, i); |
| 1701 | msync_result = |
| 1702 | GRN_MSYNC(ctx, &(io->fis[nth_file_info]), info->map, segment_size); |
| 1703 | GRN_ATOMIC_ADD_EX(pnref, -1, nref); |
| 1704 | GRN_FUTEX_WAKE(pnref); |
| 1705 | |
| 1706 | if (msync_result != 0) { |
| 1707 | rc = ctx->rc; |
| 1708 | break; |
| 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | return rc; |
| 1714 | } |
| 1715 | |
| 1716 | bool |
| 1717 | grn_io_is_corrupt(grn_ctx *ctx, grn_io *io) |
| 1718 | { |
| 1719 | uint32_t i; |
| 1720 | uint32_t n_files; |
| 1721 | |
| 1722 | if (!io) { |
| 1723 | return false; |
| 1724 | } |
| 1725 | |
| 1726 | n_files = grn_io_n_files(ctx, io); |
| 1727 | for (i = 0; i < n_files; i++) { |
| 1728 | char path[PATH_MAX]; |
| 1729 | struct stat s; |
| 1730 | gen_pathname(io->path, path, i); |
| 1731 | if (stat(path, &s) != 0) { |
| 1732 | SERR("[io][corrupt] used path doesn't exist: <%s>", path); |
| 1733 | return true; |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | return false; |
| 1738 | } |
| 1739 | |
| 1740 | size_t |
| 1741 | grn_io_get_disk_usage(grn_ctx *ctx, grn_io *io) |
| 1742 | { |
| 1743 | size_t usage = 0; |
| 1744 | uint32_t i; |
| 1745 | uint32_t n_files; |
| 1746 | |
| 1747 | if (!io) { |
| 1748 | return usage; |
| 1749 | } |
| 1750 | |
| 1751 | n_files = grn_io_n_files(ctx, io); |
| 1752 | for (i = 0; i < n_files; i++) { |
| 1753 | char path[PATH_MAX]; |
| 1754 | struct stat s; |
| 1755 | gen_pathname(io->path, path, i); |
| 1756 | if (stat(path, &s) != 0) { |
| 1757 | continue; |
| 1758 | } |
| 1759 | usage += (size_t)(s.st_size); |
| 1760 | } |
| 1761 | |
| 1762 | return usage; |
| 1763 | } |
| 1764 | |
| 1765 | /** mmap abstraction **/ |
| 1766 | |
| 1767 | static size_t mmap_size = 0; |
| 1768 | |
| 1769 | size_t |
| 1770 | grn_get_memory_map_size(void) |
| 1771 | { |
| 1772 | return mmap_size; |
| 1773 | } |
| 1774 | |
| 1775 | #ifdef WIN32 |
| 1776 | |
| 1777 | inline static grn_rc |
| 1778 | grn_fileinfo_open_v1(grn_ctx *ctx, fileinfo *fi, const char *path, int flags) |
| 1779 | { |
| 1780 | CRITICAL_SECTION_INIT(fi->cs); |
| 1781 | return GRN_SUCCESS; |
| 1782 | } |
| 1783 | |
| 1784 | static inline void * |
| 1785 | grn_mmap_v1( |
| 1786 | grn_ctx *ctx, HANDLE *fmo, fileinfo *fi, int64_t offset, size_t length) |
| 1787 | { |
| 1788 | void *res; |
| 1789 | if (!fi) { |
| 1790 | if (fmo) { |
| 1791 | *fmo = NULL; |
| 1792 | } |
| 1793 | /* TODO: Try to support VirtualAlloc() as anonymous mmap in POSIX. |
| 1794 | * If VirtualAlloc() provides better performance rather than malloc(), |
| 1795 | * we'll use it. |
| 1796 | */ |
| 1797 | return GRN_CALLOC(length); |
| 1798 | } |
| 1799 | /* CRITICAL_SECTION_ENTER(fi->cs); */ |
| 1800 | /* try to create fmo */ |
| 1801 | /* TODO: Add support for 32bit over offset by using dwMaximumSizeHigh. */ |
| 1802 | *fmo = CreateFileMapping(fi->fh, |
| 1803 | NULL, |
| 1804 | PAGE_READWRITE, |
| 1805 | 0, |
| 1806 | (DWORD)(offset + length), |
| 1807 | NULL); |
| 1808 | if (!*fmo) { |
| 1809 | SERR("CreateFileMapping(%lu + %" GRN_FMT_SIZE ") failed " |
| 1810 | "<%" GRN_FMT_SIZE ">", |
| 1811 | (DWORD)offset, |
| 1812 | length, |
| 1813 | mmap_size); |
| 1814 | return NULL; |
| 1815 | } |
| 1816 | res = MapViewOfFile(*fmo, FILE_MAP_WRITE, 0, (DWORD)offset, (SIZE_T)length); |
| 1817 | if (!res) { |
| 1818 | SERR("MapViewOfFile(%lu,%" GRN_FMT_SIZE ") failed <%" GRN_FMT_SIZE ">", |
| 1819 | (DWORD)offset, |
| 1820 | length, |
| 1821 | mmap_size); |
| 1822 | return NULL; |
| 1823 | } |
| 1824 | /* CRITICAL_SECTION_LEAVE(fi->cs); */ |
| 1825 | mmap_size += length; |
| 1826 | return res; |
| 1827 | } |
| 1828 | |
| 1829 | static inline int |
| 1830 | grn_munmap_v1( |
| 1831 | grn_ctx *ctx, HANDLE *fmo, fileinfo *fi, void *start, size_t length) |
| 1832 | { |
| 1833 | int r = 0; |
| 1834 | |
| 1835 | if (!fi) { |
| 1836 | GRN_FREE(start); |
| 1837 | return r; |
| 1838 | } |
| 1839 | |
| 1840 | if (!fmo) { |
| 1841 | GRN_FREE(start); |
| 1842 | return r; |
| 1843 | } |
| 1844 | |
| 1845 | if (*fmo) { |
| 1846 | if (!FlushViewOfFile(start, length)) { |
| 1847 | SERR("FlushViewOfFile(<%p>, <%" GRN_FMT_SIZE ">) failed on unmap", |
| 1848 | start, |
| 1849 | length); |
| 1850 | r = -1; |
| 1851 | } |
| 1852 | if (UnmapViewOfFile(start)) { |
| 1853 | mmap_size -= length; |
| 1854 | } else { |
| 1855 | SERR("UnmapViewOfFile(%p,%" GRN_FMT_SIZE ") failed <%" GRN_FMT_SIZE ">", |
| 1856 | start, |
| 1857 | length, |
| 1858 | mmap_size); |
| 1859 | r = -1; |
| 1860 | } |
| 1861 | if (!CloseHandle(*fmo)) { |
| 1862 | SERR("CloseHandle(%p,%" GRN_FMT_SIZE ") failed <%" GRN_FMT_SIZE ">", |
| 1863 | start, |
| 1864 | length, |
| 1865 | mmap_size); |
| 1866 | } |
| 1867 | *fmo = NULL; |
| 1868 | } else { |
| 1869 | GRN_FREE(start); |
| 1870 | } |
| 1871 | |
| 1872 | return r; |
| 1873 | } |
| 1874 | |
| 1875 | static inline grn_rc |
| 1876 | grn_fileinfo_open_v0(grn_ctx *ctx, fileinfo *fi, const char *path, int flags) |
| 1877 | { |
| 1878 | /* signature may be wrong.. */ |
| 1879 | fi->fmo = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, NULL); |
| 1880 | /* open failed */ |
| 1881 | if (fi->fmo == NULL) { |
| 1882 | // flock |
| 1883 | /* retry to open */ |
| 1884 | fi->fmo = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, NULL); |
| 1885 | /* failed again */ |
| 1886 | if (fi->fmo == NULL) { |
| 1887 | /* try to create fmo */ |
| 1888 | fi->fmo = CreateFileMapping(fi->fh, |
| 1889 | NULL, |
| 1890 | PAGE_READWRITE, |
| 1891 | 0, |
| 1892 | GRN_IO_FILE_SIZE_V0, |
| 1893 | NULL); |
| 1894 | } |
| 1895 | // funlock |
| 1896 | } |
| 1897 | if (fi->fmo != NULL) { |
| 1898 | if (GetLastError() != ERROR_ALREADY_EXISTS) { |
| 1899 | CRITICAL_SECTION_INIT(fi->cs); |
| 1900 | return GRN_SUCCESS; |
| 1901 | } else { |
| 1902 | GRN_LOG(ctx, |
| 1903 | GRN_LOG_ERROR, |
| 1904 | "fmo object already exists! handle=%p", |
| 1905 | fi->fh); |
| 1906 | CloseHandle(fi->fmo); |
| 1907 | } |
| 1908 | } else { |
| 1909 | GRN_LOG(ctx, |
| 1910 | GRN_LOG_ALERT, |
| 1911 | "failed to get FileMappingObject #%lu", |
| 1912 | GetLastError()); |
| 1913 | } |
| 1914 | CloseHandle(fi->fh); |
| 1915 | SERR("OpenFileMapping"); |
| 1916 | if (ctx->rc != GRN_SUCCESS && fi->path) { |
| 1917 | free(fi->path); |
| 1918 | fi->path = NULL; |
| 1919 | } |
| 1920 | return ctx->rc; |
| 1921 | } |
| 1922 | |
| 1923 | static inline void * |
| 1924 | grn_mmap_v0(grn_ctx *ctx, fileinfo *fi, int64_t offset, size_t length) |
| 1925 | { |
| 1926 | void *res; |
| 1927 | if (!fi) { |
| 1928 | return GRN_CALLOC(length); |
| 1929 | } |
| 1930 | /* file must be exceeded to GRN_IO_FILE_SIZE_V0 when FileMappingObject |
| 1931 | created. and, after fmo created, it's not allowed to expand the size of file. |
| 1932 | DWORD tail = (DWORD)(offset + length); |
| 1933 | DWORD filesize = GetFileSize(fi->fh, NULL); |
| 1934 | if (filesize < tail) { |
| 1935 | if (SetFilePointer(fi->fh, tail, NULL, FILE_BEGIN) != tail) { |
| 1936 | grn_log("SetFilePointer failed"); |
| 1937 | return NULL; |
| 1938 | } |
| 1939 | if (!SetEndOfFile(fi->fh)) { |
| 1940 | grn_log("SetEndOfFile failed"); |
| 1941 | return NULL; |
| 1942 | } |
| 1943 | filesize = tail; |
| 1944 | } |
| 1945 | */ |
| 1946 | /* TODO: Add support for 32bit over offset by using dwFileOffsetHigh. */ |
| 1947 | res = |
| 1948 | MapViewOfFile(fi->fmo, FILE_MAP_WRITE, 0, (DWORD)offset, (SIZE_T)length); |
| 1949 | if (!res) { |
| 1950 | MERR("MapViewOfFile failed: <%" GRN_FMT_SIZE ">: %s", |
| 1951 | mmap_size, |
| 1952 | grn_error_get_current_system_message()); |
| 1953 | return NULL; |
| 1954 | } |
| 1955 | mmap_size += length; |
| 1956 | return res; |
| 1957 | } |
| 1958 | |
| 1959 | static inline int |
| 1960 | grn_munmap_v0(grn_ctx *ctx, fileinfo *fi, void *start, size_t length) |
| 1961 | { |
| 1962 | int r = 0; |
| 1963 | |
| 1964 | if (!fi) { |
| 1965 | GRN_FREE(start); |
| 1966 | return 0; |
| 1967 | } |
| 1968 | |
| 1969 | if (!FlushViewOfFile(start, length)) { |
| 1970 | SERR("FlushViewOfFile(<%p>, <%" GRN_FMT_SIZE ">) failed on unmap", |
| 1971 | start, |
| 1972 | length); |
| 1973 | r = -1; |
| 1974 | } |
| 1975 | if (UnmapViewOfFile(start)) { |
| 1976 | mmap_size -= length; |
| 1977 | return r; |
| 1978 | } else { |
| 1979 | SERR("UnmapViewOfFile(%p,%" GRN_FMT_SIZE ") failed <%" GRN_FMT_SIZE ">", |
| 1980 | start, |
| 1981 | length, |
| 1982 | mmap_size); |
| 1983 | return -1; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | static inline bool |
| 1988 | grn_fileinfo_open_common(grn_ctx *ctx, |
| 1989 | fileinfo *fi, |
| 1990 | const char *path, |
| 1991 | int flags) |
| 1992 | { |
| 1993 | bool success = true; |
| 1994 | |
| 1995 | fi->path = grn_strdup_raw(path); |
| 1996 | |
| 1997 | /* may be wrong if flags is just only O_RDWR */ |
| 1998 | if ((flags & O_CREAT)) { |
| 1999 | bool exist = false; |
| 2000 | DWORD dwCreationDisposition; |
| 2001 | const char *flags_description; |
| 2002 | |
| 2003 | { |
| 2004 | struct stat stat_buffer; |
| 2005 | if (stat(path, &stat_buffer) == 0) { |
| 2006 | exist = true; |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | if (flags & O_EXCL) { |
| 2011 | dwCreationDisposition = CREATE_NEW; |
| 2012 | flags_description = "O_RDWR|O_CREAT|O_EXCL"; |
| 2013 | } else { |
| 2014 | dwCreationDisposition = OPEN_ALWAYS; |
| 2015 | flags_description = "O_RDWR|O_CREAT"; |
| 2016 | } |
| 2017 | fi->fh = CreateFile(path, |
| 2018 | GRN_IO_FILE_CREATE_MODE, |
| 2019 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 2020 | NULL, |
| 2021 | dwCreationDisposition, |
| 2022 | FILE_ATTRIBUTE_NORMAL, |
| 2023 | NULL); |
| 2024 | if (fi->fh == INVALID_HANDLE_VALUE) { |
| 2025 | SERR("CreateFile(<%s>, <%s>) failed", path, flags_description); |
| 2026 | success = false; |
| 2027 | if (!exist) { |
| 2028 | struct stat stat_buffer; |
| 2029 | if (stat(path, &stat_buffer) == 0) { |
| 2030 | GRN_LOG(ctx, |
| 2031 | GRN_LOG_INFO, |
| 2032 | "[io][open] " |
| 2033 | "delete a newly created file because of open error: <%s>", |
| 2034 | path); |
| 2035 | DeleteFile(path); |
| 2036 | } |
| 2037 | } |
| 2038 | goto exit; |
| 2039 | } |
| 2040 | |
| 2041 | switch (dwCreationDisposition) { |
| 2042 | case CREATE_NEW: |
| 2043 | GRN_LOG(ctx, GRN_LOG_INFO, "[io][open] create new file: <%s>", path); |
| 2044 | break; |
| 2045 | case OPEN_ALWAYS: |
| 2046 | if (GetLastError() == ERROR_ALREADY_EXISTS) { |
| 2047 | GRN_LOG(ctx, |
| 2048 | GRN_LOG_INFO, |
| 2049 | "[io][open] open existing file because it exists: <%s>", |
| 2050 | path); |
| 2051 | } else { |
| 2052 | GRN_LOG(ctx, |
| 2053 | GRN_LOG_INFO, |
| 2054 | "[io][open] create new file because it doesn't exist: <%s>", |
| 2055 | path); |
| 2056 | } |
| 2057 | break; |
| 2058 | default: |
| 2059 | break; |
| 2060 | } |
| 2061 | |
| 2062 | { |
| 2063 | FILE_SET_SPARSE_BUFFER buffer; |
| 2064 | buffer.SetSparse = TRUE; |
| 2065 | DWORD returned_bytes; |
| 2066 | if (!DeviceIoControl(fi->fh, |
| 2067 | FSCTL_SET_SPARSE, |
| 2068 | &buffer, |
| 2069 | sizeof(FILE_SET_SPARSE_BUFFER), |
| 2070 | NULL, |
| 2071 | 0, |
| 2072 | &returned_bytes, |
| 2073 | NULL)) { |
| 2074 | GRN_LOG(ctx, |
| 2075 | GRN_LOG_INFO, |
| 2076 | "Tried to make file sparse but failed: " |
| 2077 | "DeviceIoControl(FSCTL_SET_SPARSE): " |
| 2078 | "<%s>: <%s>", |
| 2079 | path, |
| 2080 | grn_error_get_current_system_message()); |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | goto exit; |
| 2085 | } |
| 2086 | |
| 2087 | if ((flags & O_TRUNC)) { |
| 2088 | CloseHandle(fi->fh); |
| 2089 | /* unable to assign OPEN_ALWAYS and TRUNCATE_EXISTING at once */ |
| 2090 | fi->fh = CreateFile(path, |
| 2091 | GRN_IO_FILE_CREATE_MODE, |
| 2092 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 2093 | NULL, |
| 2094 | TRUNCATE_EXISTING, |
| 2095 | FILE_ATTRIBUTE_NORMAL, |
| 2096 | NULL); |
| 2097 | if (fi->fh == INVALID_HANDLE_VALUE) { |
| 2098 | SERR("CreateFile(<%s>, <O_RDWR|O_TRUNC>) failed", path); |
| 2099 | success = false; |
| 2100 | goto exit; |
| 2101 | } |
| 2102 | GRN_LOG(ctx, GRN_LOG_INFO, "[io][open] truncated: <%s>", path); |
| 2103 | goto exit; |
| 2104 | } |
| 2105 | /* O_RDWR only */ |
| 2106 | fi->fh = CreateFile(path, |
| 2107 | GRN_IO_FILE_CREATE_MODE, |
| 2108 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 2109 | NULL, |
| 2110 | OPEN_EXISTING, |
| 2111 | FILE_ATTRIBUTE_NORMAL, |
| 2112 | NULL); |
| 2113 | if (fi->fh == INVALID_HANDLE_VALUE) { |
| 2114 | SERR("CreateFile(<%s>, <O_RDWR>) failed", path); |
| 2115 | success = false; |
| 2116 | goto exit; |
| 2117 | } |
| 2118 | GRN_LOG(ctx, GRN_LOG_INFO, "[io][open] open existing file: <%s>", path); |
| 2119 | |
| 2120 | exit: |
| 2121 | if (!success && fi->path) { |
| 2122 | free(fi->path); |
| 2123 | fi->path = NULL; |
| 2124 | } |
| 2125 | return success; |
| 2126 | } |
| 2127 | |
| 2128 | static inline grn_rc |
| 2129 | grn_fileinfo_open(grn_ctx *ctx, fileinfo *fi, const char *path, int flags) |
| 2130 | { |
| 2131 | struct _grn_io_header io_header; |
| 2132 | LARGE_INTEGER file_size; |
| 2133 | int version = grn_io_version_default; |
| 2134 | |
| 2135 | if (!grn_fileinfo_open_common(ctx, fi, path, flags)) { |
| 2136 | if (fi->fh != INVALID_HANDLE_VALUE) { |
| 2137 | CloseHandle(fi->fh); |
| 2138 | fi->fh = INVALID_HANDLE_VALUE; |
| 2139 | } |
| 2140 | return ctx->rc; |
| 2141 | } |
| 2142 | |
| 2143 | if (GetFileSizeEx(fi->fh, &file_size) && file_size.QuadPart > 0) { |
| 2144 | DWORD header_size; |
| 2145 | DWORD read_bytes; |
| 2146 | header_size = sizeof(struct _grn_io_header); |
| 2147 | ReadFile(fi->fh, &io_header, header_size, &read_bytes, NULL); |
| 2148 | if (read_bytes == header_size) { |
| 2149 | version = io_header.version; |
| 2150 | } |
| 2151 | SetFilePointer(fi->fh, 0, NULL, FILE_BEGIN); |
| 2152 | } |
| 2153 | |
| 2154 | if (version == 0) { |
| 2155 | return grn_fileinfo_open_v0(ctx, fi, path, flags); |
| 2156 | } else { |
| 2157 | return grn_fileinfo_open_v1(ctx, fi, path, flags); |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | static inline int |
| 2162 | grn_guess_io_version(grn_ctx *ctx, grn_io *io, fileinfo *fi) |
| 2163 | { |
| 2164 | if (io) { |
| 2165 | return io->header->version; |
| 2166 | } |
| 2167 | |
| 2168 | if (fi) { |
| 2169 | if (fi->fmo) { |
| 2170 | return 0; |
| 2171 | } else { |
| 2172 | return 1; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | return grn_io_version_default; |
| 2177 | } |
| 2178 | |
| 2179 | static inline void * |
| 2180 | grn_mmap(grn_ctx *ctx, |
| 2181 | grn_io *io, |
| 2182 | HANDLE *fmo, |
| 2183 | fileinfo *fi, |
| 2184 | int64_t offset, |
| 2185 | size_t length, |
| 2186 | const char *file, |
| 2187 | int line, |
| 2188 | const char *func) |
| 2189 | { |
| 2190 | if (grn_fail_malloc_should_fail(length, file, line, func)) { |
| 2191 | MERR("[alloc][fail][mmap] <%u>: <%" GRN_FMT_SIZE ">: <%s>: " |
| 2192 | "<%p:%" GRN_FMT_INT64D ":%" GRN_FMT_SIZE ">: " |
| 2193 | "%s:%d: %s", |
| 2194 | grn_alloc_count(), |
| 2195 | mmap_size, |
| 2196 | io ? (io->path[0] ? io->path : "(memory)") : "(null)", |
| 2197 | fi ? fi->fh : NULL, |
| 2198 | offset, |
| 2199 | length, |
| 2200 | file, |
| 2201 | line, |
| 2202 | func); |
| 2203 | return NULL; |
| 2204 | } else { |
| 2205 | int version; |
| 2206 | |
| 2207 | version = grn_guess_io_version(ctx, io, fi); |
| 2208 | |
| 2209 | if (version == 0) { |
| 2210 | return grn_mmap_v0(ctx, fi, offset, length); |
| 2211 | } else { |
| 2212 | return grn_mmap_v1(ctx, fmo, fi, offset, length); |
| 2213 | } |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | static inline int |
| 2218 | grn_munmap(grn_ctx *ctx, |
| 2219 | grn_io *io, |
| 2220 | HANDLE *fmo, |
| 2221 | fileinfo *fi, |
| 2222 | void *start, |
| 2223 | size_t length) |
| 2224 | { |
| 2225 | int version; |
| 2226 | |
| 2227 | version = grn_guess_io_version(ctx, io, fi); |
| 2228 | |
| 2229 | if (version == 0) { |
| 2230 | return grn_munmap_v0(ctx, fi, start, length); |
| 2231 | } else { |
| 2232 | return grn_munmap_v1(ctx, fmo, fi, start, length); |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | static inline grn_rc |
| 2237 | grn_fileinfo_close(grn_ctx *ctx, fileinfo *fi) |
| 2238 | { |
| 2239 | if (fi->fmo != NULL) { |
| 2240 | CloseHandle(fi->fmo); |
| 2241 | fi->fmo = NULL; |
| 2242 | } |
| 2243 | if (fi->fh != INVALID_HANDLE_VALUE) { |
| 2244 | if (grn_logger_pass(ctx, GRN_LOG_DEBUG)) { |
| 2245 | GRN_LOG(ctx, GRN_LOG_DEBUG, "[io][close] <%s>", fi->path); |
| 2246 | } |
| 2247 | CloseHandle(fi->fh); |
| 2248 | CRITICAL_SECTION_FIN(fi->cs); |
| 2249 | fi->fh = INVALID_HANDLE_VALUE; |
| 2250 | } |
| 2251 | if (fi->path) { |
| 2252 | free(fi->path); |
| 2253 | fi->path = NULL; |
| 2254 | } |
| 2255 | return GRN_SUCCESS; |
| 2256 | } |
| 2257 | |
| 2258 | static inline void |
| 2259 | grn_fileinfo_init(fileinfo *fis, uint32_t nfis) |
| 2260 | { |
| 2261 | for (; nfis--; fis++) { |
| 2262 | fis->path = NULL; |
| 2263 | fis->fh = INVALID_HANDLE_VALUE; |
| 2264 | fis->fmo = NULL; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | static inline int |
| 2269 | grn_fileinfo_opened(fileinfo *fi) |
| 2270 | { |
| 2271 | return fi->fh != INVALID_HANDLE_VALUE; |
| 2272 | } |
| 2273 | |
| 2274 | static inline int |
| 2275 | grn_msync(grn_ctx *ctx, fileinfo *fi, void *start, size_t length) |
| 2276 | { |
| 2277 | BOOL succeeded; |
| 2278 | SYSTEMTIME system_time; |
| 2279 | FILETIME file_time; |
| 2280 | |
| 2281 | succeeded = FlushViewOfFile(start, length); |
| 2282 | if (!succeeded) { |
| 2283 | SERR("FlushViewOfFile(<%p>, <%" GRN_FMT_SIZE ">) failed on sync", |
| 2284 | start, |
| 2285 | length); |
| 2286 | return -1; |
| 2287 | } |
| 2288 | |
| 2289 | if (fi->fh == INVALID_HANDLE_VALUE) { |
| 2290 | return 0; |
| 2291 | } |
| 2292 | |
| 2293 | GetSystemTime(&system_time); |
| 2294 | succeeded = SystemTimeToFileTime(&system_time, &file_time); |
| 2295 | if (!succeeded) { |
| 2296 | SERR("SystemTimeToFileTime(<%04u-%02u-%02uT%02u:%02u:%02u.%03u>) failed", |
| 2297 | system_time.wYear, |
| 2298 | system_time.wMonth, |
| 2299 | system_time.wDay, |
| 2300 | system_time.wHour, |
| 2301 | system_time.wMinute, |
| 2302 | system_time.wSecond, |
| 2303 | system_time.wMilliseconds); |
| 2304 | return -1; |
| 2305 | } |
| 2306 | |
| 2307 | succeeded = SetFileTime(fi->fh, NULL, NULL, &file_time); |
| 2308 | if (!succeeded) { |
| 2309 | SERR("SetFileTime(<%p>, <%p>, <%" GRN_FMT_SIZE ">) failed", |
| 2310 | fi->fh, |
| 2311 | start, |
| 2312 | length); |
| 2313 | return -1; |
| 2314 | } |
| 2315 | |
| 2316 | return 0; |
| 2317 | } |
| 2318 | |
| 2319 | static inline grn_rc |
| 2320 | grn_pread(grn_ctx *ctx, fileinfo *fi, void *buf, size_t count, off_t offset) |
| 2321 | { |
| 2322 | DWORD r, len; |
| 2323 | CRITICAL_SECTION_ENTER(fi->cs); |
| 2324 | r = SetFilePointer(fi->fh, offset, NULL, FILE_BEGIN); |
| 2325 | if (r == INVALID_SET_FILE_POINTER) { |
| 2326 | SERR("SetFilePointer"); |
| 2327 | } else { |
| 2328 | if (!ReadFile(fi->fh, buf, (DWORD)count, &len, NULL)) { |
| 2329 | SERR("ReadFile"); |
| 2330 | } else if (len != count) { |
| 2331 | /* todo : should retry ? */ |
| 2332 | ERR(GRN_INPUT_OUTPUT_ERROR, |
| 2333 | "ReadFile %" GRN_FMT_SIZE " != %lu", |
| 2334 | count, |
| 2335 | len); |
| 2336 | } |
| 2337 | } |
| 2338 | CRITICAL_SECTION_LEAVE(fi->cs); |
| 2339 | return ctx->rc; |
| 2340 | } |
| 2341 | |
| 2342 | static inline grn_rc |
| 2343 | grn_pwrite(grn_ctx *ctx, fileinfo *fi, void *buf, size_t count, off_t offset) |
| 2344 | { |
| 2345 | DWORD r, len; |
| 2346 | CRITICAL_SECTION_ENTER(fi->cs); |
| 2347 | r = SetFilePointer(fi->fh, offset, NULL, FILE_BEGIN); |
| 2348 | if (r == INVALID_SET_FILE_POINTER) { |
| 2349 | SERR("SetFilePointer"); |
| 2350 | } else { |
| 2351 | if (!WriteFile(fi->fh, buf, (DWORD)count, &len, NULL)) { |
| 2352 | SERR("WriteFile"); |
| 2353 | } else if (len != count) { |
| 2354 | /* todo : should retry ? */ |
| 2355 | ERR(GRN_INPUT_OUTPUT_ERROR, |
| 2356 | "WriteFile %" GRN_FMT_SIZE " != %lu", |
| 2357 | count, |
| 2358 | len); |
| 2359 | } |
| 2360 | } |
| 2361 | CRITICAL_SECTION_LEAVE(fi->cs); |
| 2362 | return ctx->rc; |
| 2363 | } |
| 2364 | |
| 2365 | #else /* WIN32 */ |
| 2366 | |
| 2367 | inline static grn_rc |
| 2368 | grn_fileinfo_open(grn_ctx *ctx, fileinfo *fi, const char *path, int flags) |
| 2369 | { |
| 2370 | struct stat st; |
| 2371 | fi->path = grn_strdup_raw(path); |
| 2372 | grn_open(fi->fd, path, flags); |
| 2373 | if (fi->fd == -1) { |
| 2374 | ERRNO_ERR("failed to open file info path: <%s>", path); |
| 2375 | free(fi->path); |
| 2376 | fi->path = NULL; |
| 2377 | return ctx->rc; |
| 2378 | } |
| 2379 | if (fstat(fi->fd, &st) == -1) { |
| 2380 | ERRNO_ERR("failed to stat file info path: <%s>", path); |
| 2381 | grn_close(fi->fd); |
| 2382 | free(fi->path); |
| 2383 | fi->path = NULL; |
| 2384 | return ctx->rc; |
| 2385 | } |
| 2386 | fi->dev = st.st_dev; |
| 2387 | fi->inode = st.st_ino; |
| 2388 | GRN_LOG(ctx, GRN_LOG_DUMP, "[io][open] <%s>", path); |
| 2389 | return GRN_SUCCESS; |
| 2390 | } |
| 2391 | |
| 2392 | static inline void |
| 2393 | grn_fileinfo_init(fileinfo *fis, uint32_t nfis) |
| 2394 | { |
| 2395 | for (; nfis--; fis++) { |
| 2396 | fis->path = NULL; |
| 2397 | fis->fd = -1; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | static inline int |
| 2402 | grn_fileinfo_opened(fileinfo *fi) |
| 2403 | { |
| 2404 | return fi->fd != -1; |
| 2405 | } |
| 2406 | |
| 2407 | static inline grn_rc |
| 2408 | grn_fileinfo_close(grn_ctx *ctx, fileinfo *fi) |
| 2409 | { |
| 2410 | if (fi->fd != -1) { |
| 2411 | if (grn_close(fi->fd) == -1) { |
| 2412 | SERR("close"); |
| 2413 | return ctx->rc; |
| 2414 | } |
| 2415 | fi->fd = -1; |
| 2416 | if (fi->path) { |
| 2417 | GRN_LOG(ctx, GRN_LOG_DUMP, "[io][close] <%s>", fi->path); |
| 2418 | } |
| 2419 | } |
| 2420 | if (fi->path) { |
| 2421 | free(fi->path); |
| 2422 | fi->path = NULL; |
| 2423 | } |
| 2424 | return GRN_SUCCESS; |
| 2425 | } |
| 2426 | |
| 2427 | # if defined(MAP_ANON) && !defined(MAP_ANONYMOUS) |
| 2428 | # define MAP_ANONYMOUS MAP_ANON |
| 2429 | # endif |
| 2430 | |
| 2431 | # include <sys/mman.h> |
| 2432 | |
| 2433 | inline static void * |
| 2434 | grn_mmap(grn_ctx *ctx, |
| 2435 | grn_io *io, |
| 2436 | fileinfo *fi, |
| 2437 | int64_t offset, |
| 2438 | size_t length, |
| 2439 | const char *file, |
| 2440 | int line, |
| 2441 | const char *func) |
| 2442 | { |
| 2443 | if (grn_fail_malloc_should_fail(length, file, line, func)) { |
| 2444 | MERR("[alloc][fail][mmap] <%u>: <%" GRN_FMT_SIZE ">: <%s>: " |
| 2445 | "<%d:%" GRN_FMT_INT64D ":%" GRN_FMT_SIZE ">: " |
| 2446 | "%s:%d: %s", |
| 2447 | grn_alloc_count(), |
| 2448 | mmap_size, |
| 2449 | io ? (io->path[0] ? io->path : "(memory)") : "(null)", |
| 2450 | fi ? fi->fd : 0, |
| 2451 | offset, |
| 2452 | length, |
| 2453 | file, |
| 2454 | line, |
| 2455 | func); |
| 2456 | return NULL; |
| 2457 | } else { |
| 2458 | void *res; |
| 2459 | int fd, flags; |
| 2460 | if (fi) { |
| 2461 | struct stat s; |
| 2462 | int64_t tail = offset + (int64_t)length; |
| 2463 | fd = fi->fd; |
| 2464 | if ((fstat(fd, &s) == -1) || |
| 2465 | (s.st_size < tail && ftruncate(fd, tail) == -1)) { |
| 2466 | SERR("fstat"); |
| 2467 | return NULL; |
| 2468 | } |
| 2469 | flags = MAP_SHARED; |
| 2470 | } else { |
| 2471 | fd = -1; |
| 2472 | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 2473 | # ifdef MAP_ALIGNED_SUPER |
| 2474 | flags |= MAP_ALIGNED_SUPER; |
| 2475 | # endif |
| 2476 | } |
| 2477 | res = mmap(NULL, length, PROT_READ | PROT_WRITE, flags, fd, offset); |
| 2478 | if (MAP_FAILED == res) { |
| 2479 | MERR("mmap(%" GRN_FMT_SIZE ",%d,%" GRN_FMT_INT64D ")=%s <%" GRN_FMT_SIZE |
| 2480 | ">", |
| 2481 | length, |
| 2482 | fd, |
| 2483 | offset, |
| 2484 | strerror(errno), |
| 2485 | mmap_size); |
| 2486 | return NULL; |
| 2487 | } |
| 2488 | mmap_size += length; |
| 2489 | return res; |
| 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | static inline int |
| 2494 | grn_msync(grn_ctx *ctx, fileinfo *fi, void *start, size_t length) |
| 2495 | { |
| 2496 | int r = msync(start, length, MS_SYNC); |
| 2497 | if (r == -1) { |
| 2498 | SERR("msync"); |
| 2499 | return r; |
| 2500 | } |
| 2501 | |
| 2502 | if (fi->fd > 0) { |
| 2503 | struct timeval time_values[2]; |
| 2504 | struct timespec time_specs[2]; |
| 2505 | gettimeofday(&(time_values[0]), NULL); |
| 2506 | time_values[1] = time_values[0]; |
| 2507 | time_specs[0].tv_sec = time_values[0].tv_sec; |
| 2508 | time_specs[0].tv_nsec = time_values[0].tv_usec * 1000; |
| 2509 | time_specs[1] = time_specs[0]; |
| 2510 | r = grn_futimens(fi->fd, time_values, time_specs); |
| 2511 | if (r == -1) { |
| 2512 | SERR("%s failed: <%d>", grn_futimens_name, fi->fd); |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | return r; |
| 2517 | } |
| 2518 | |
| 2519 | static inline int |
| 2520 | grn_munmap(grn_ctx *ctx, grn_io *io, fileinfo *fi, void *start, size_t length) |
| 2521 | { |
| 2522 | int res; |
| 2523 | res = munmap(start, length); |
| 2524 | if (res == 0) { |
| 2525 | mmap_size -= length; |
| 2526 | } else { |
| 2527 | SERR("munmap(%p,%" GRN_FMT_LLU ") failed <%" GRN_FMT_LLU ">", |
| 2528 | start, |
| 2529 | (unsigned long long int)length, |
| 2530 | (unsigned long long int)mmap_size); |
| 2531 | } |
| 2532 | return res; |
| 2533 | } |
| 2534 | |
| 2535 | static inline grn_rc |
| 2536 | grn_pread(grn_ctx *ctx, fileinfo *fi, void *buf, size_t count, off_t offset) |
| 2537 | { |
| 2538 | ssize_t r = pread(fi->fd, buf, count, offset); |
| 2539 | if (r == -1) { |
| 2540 | SERR("pread"); |
| 2541 | return ctx->rc; |
| 2542 | } |
| 2543 | if ((size_t)r != count) { |
| 2544 | /* todo : should retry ? */ |
| 2545 | ERR(GRN_INPUT_OUTPUT_ERROR, |
| 2546 | "pread returned %" GRN_FMT_SSIZE " != %" GRN_FMT_SIZE, |
| 2547 | r, |
| 2548 | count); |
| 2549 | return ctx->rc; |
| 2550 | } |
| 2551 | return GRN_SUCCESS; |
| 2552 | } |
| 2553 | |
| 2554 | static inline grn_rc |
| 2555 | grn_pwrite(grn_ctx *ctx, fileinfo *fi, void *buf, size_t count, off_t offset) |
| 2556 | { |
| 2557 | ssize_t r = pwrite(fi->fd, buf, count, offset); |
| 2558 | if (r == -1) { |
| 2559 | SERR("pwrite"); |
| 2560 | return ctx->rc; |
| 2561 | } |
| 2562 | if ((size_t)r != count) { |
| 2563 | /* todo : should retry ? */ |
| 2564 | ERR(GRN_INPUT_OUTPUT_ERROR, |
| 2565 | "pwrite returned %" GRN_FMT_SSIZE " != %" GRN_FMT_SIZE, |
| 2566 | r, |
| 2567 | count); |
| 2568 | return ctx->rc; |
| 2569 | } |
| 2570 | return GRN_SUCCESS; |
| 2571 | } |
| 2572 | |
| 2573 | #endif /* WIN32 */ |
| 2574 | |
| 2575 | bool |
| 2576 | grn_io_warm_path(grn_ctx *ctx, grn_io *io, const char *path) |
| 2577 | { |
| 2578 | FILE *input = grn_fopen(path, "rb"); |
| 2579 | if (!input) { |
| 2580 | SERR("[io][warm] failed to open a file: <%s>", path); |
| 2581 | return false; |
| 2582 | } |
| 2583 | |
| 2584 | GRN_LOG(ctx, GRN_LOG_DUMP, "[io][warm] <%s>", path); |
| 2585 | char buffer[4096]; |
| 2586 | while (fread(buffer, 1, sizeof(buffer), input) != 0) { |
| 2587 | /* Do nothing */ |
| 2588 | } |
| 2589 | fclose(input); |
| 2590 | return true; |
| 2591 | } |
| 2592 | |
| 2593 | grn_rc |
| 2594 | grn_io_warm(grn_ctx *ctx, grn_io *io) |
| 2595 | { |
| 2596 | if (io->path[0] == '\0') { |
| 2597 | return GRN_SUCCESS; |
| 2598 | } |
| 2599 | |
| 2600 | char buffer[PATH_MAX]; |
| 2601 | uint32_t i; |
| 2602 | uint32_t n_files = grn_io_n_files(ctx, io); |
| 2603 | for (i = 0; i < n_files; i++) { |
| 2604 | gen_pathname(io->path, buffer, i); |
| 2605 | if (!grn_io_warm_path(ctx, io, buffer)) { |
| 2606 | break; |
| 2607 | } |
| 2608 | } |
| 2609 | return ctx->rc; |
| 2610 | } |