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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
/*-
* Copyright (c) 2014-2016 MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __win_fs_exist --
* Return if the file exists.
*/
static int
__win_fs_exist(WT_FILE_SYSTEM *file_system,
WT_SESSION *wt_session, const char *name, bool *existp)
{
WT_DECL_ITEM(name_wide);
WT_SESSION_IMPL *session;
WT_UNUSED(file_system);
session = (WT_SESSION_IMPL *)wt_session;
*existp = false;
WT_RET(__wt_to_utf16_string(session, name, &name_wide));
if (GetFileAttributesW(name_wide->data) != INVALID_FILE_ATTRIBUTES)
*existp = true;
__wt_scr_free(session, &name_wide);
return (0);
}
/*
* __win_fs_remove --
* Remove a file.
*/
static int
__win_fs_remove(WT_FILE_SYSTEM *file_system,
WT_SESSION *wt_session, const char *name, uint32_t flags)
{
DWORD windows_error;
WT_DECL_ITEM(name_wide);
WT_DECL_RET;
WT_SESSION_IMPL *session;
WT_UNUSED(file_system);
WT_UNUSED(flags);
session = (WT_SESSION_IMPL *)wt_session;
WT_RET(__wt_to_utf16_string(session, name, &name_wide));
if (DeleteFileW(name_wide->data) == FALSE) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: file-remove: DeleteFileW: %s",
name, __wt_formatmessage(session, windows_error));
WT_ERR(__wt_map_windows_error(windows_error));
}
err: __wt_scr_free(session, &name_wide);
return (ret);
}
/*
* __win_fs_rename --
* Rename a file.
*/
static int
__win_fs_rename(WT_FILE_SYSTEM *file_system,
WT_SESSION *wt_session, const char *from, const char *to, uint32_t flags)
{
DWORD windows_error;
WT_DECL_RET;
WT_DECL_ITEM(from_wide);
WT_DECL_ITEM(to_wide);
WT_SESSION_IMPL *session;
WT_UNUSED(file_system);
WT_UNUSED(flags);
session = (WT_SESSION_IMPL *)wt_session;
WT_ERR(__wt_to_utf16_string(session, from, &from_wide));
WT_ERR(__wt_to_utf16_string(session, to, &to_wide));
/*
* Check if file exists since Windows does not override the file if
* it exists.
*/
if (GetFileAttributesW(to_wide->data) != INVALID_FILE_ATTRIBUTES)
if (DeleteFileW(to_wide->data) == FALSE) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: file-rename: DeleteFileW: %s",
to, __wt_formatmessage(session, windows_error));
WT_ERR(__wt_map_windows_error(windows_error));
}
if (MoveFileW(from_wide->data, to_wide->data) == FALSE) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s to %s: file-rename: MoveFileW: %s",
from, to, __wt_formatmessage(session, windows_error));
WT_ERR(__wt_map_windows_error(windows_error));
}
err: __wt_scr_free(session, &from_wide);
__wt_scr_free(session, &to_wide);
return (ret);
}
/*
* __wt_win_fs_size --
* Get the size of a file in bytes, by file name.
*/
int
__wt_win_fs_size(WT_FILE_SYSTEM *file_system,
WT_SESSION *wt_session, const char *name, wt_off_t *sizep)
{
DWORD windows_error;
WT_DECL_RET;
WIN32_FILE_ATTRIBUTE_DATA data;
WT_DECL_ITEM(name_wide);
WT_SESSION_IMPL *session;
WT_UNUSED(file_system);
session = (WT_SESSION_IMPL *)wt_session;
WT_RET(__wt_to_utf16_string(session, name, &name_wide));
if (GetFileAttributesExW(
name_wide->data, GetFileExInfoStandard, &data) == 0) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: file-size: GetFileAttributesEx: %s",
name, __wt_formatmessage(session, windows_error));
WT_ERR(__wt_map_windows_error(windows_error));
}
*sizep = ((int64_t)data.nFileSizeHigh << 32) | data.nFileSizeLow;
err: __wt_scr_free(session, &name_wide);
return (ret);
}
/*
* __win_file_close --
* ANSI C close.
*/
static int
__win_file_close(WT_FILE_HANDLE *file_handle, WT_SESSION *wt_session)
{
DWORD windows_error;
WT_DECL_RET;
WT_FILE_HANDLE_WIN *win_fh;
WT_SESSION_IMPL *session;
win_fh = (WT_FILE_HANDLE_WIN *)file_handle;
session = (WT_SESSION_IMPL *)wt_session;
/*
* Close the primary and secondary handles.
*
* We don't open Windows system handles when opening directories for
* flushing, as it's not necessary (or possible) to flush a directory
* on Windows. Confirm the file handle is open before closing it.
*/
if (win_fh->filehandle != INVALID_HANDLE_VALUE &&
CloseHandle(win_fh->filehandle) == 0) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-close: CloseHandle: %s",
file_handle->name,
__wt_formatmessage(session, windows_error));
ret = __wt_map_windows_error(windows_error);
}
if (win_fh->filehandle_secondary != INVALID_HANDLE_VALUE &&
CloseHandle(win_fh->filehandle_secondary) == 0) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-close: secondary: CloseHandle: %s",
file_handle->name,
__wt_formatmessage(session, windows_error));
ret = __wt_map_windows_error(windows_error);
}
__wt_free(session, file_handle->name);
__wt_free(session, win_fh);
return (ret);
}
/*
* __win_file_lock --
* Lock/unlock a file.
*/
static int
__win_file_lock(
WT_FILE_HANDLE *file_handle, WT_SESSION *wt_session, bool lock)
{
DWORD windows_error;
WT_FILE_HANDLE_WIN *win_fh;
WT_SESSION_IMPL *session;
win_fh = (WT_FILE_HANDLE_WIN *)file_handle;
session = (WT_SESSION_IMPL *)wt_session;
/*
* WiredTiger requires this function be able to acquire locks past
* the end of file.
*
* http://msdn.microsoft.com/
* en-us/library/windows/desktop/aa365202%28v=vs.85%29.aspx
*
* You can lock bytes that are beyond the end of the current file.
* This is useful to coordinate adding records to the end of a file.
*/
if (lock) {
if (LockFile(win_fh->filehandle, 0, 0, 1, 0) == FALSE) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-lock: LockFile: %s",
file_handle->name,
__wt_formatmessage(session, windows_error));
return (__wt_map_windows_error(windows_error));
}
} else
if (UnlockFile(win_fh->filehandle, 0, 0, 1, 0) == FALSE) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-lock: UnlockFile: %s",
file_handle->name,
__wt_formatmessage(session, windows_error));
return (__wt_map_windows_error(windows_error));
}
return (0);
}
/*
* __win_file_read --
* Read a chunk.
*/
static int
__win_file_read(WT_FILE_HANDLE *file_handle,
WT_SESSION *wt_session, wt_off_t offset, size_t len, void *buf)
{
DWORD chunk, nr, windows_error;
uint8_t *addr;
OVERLAPPED overlapped = { 0 };
WT_FILE_HANDLE_WIN *win_fh;
WT_SESSION_IMPL *session;
win_fh = (WT_FILE_HANDLE_WIN *)file_handle;
session = (WT_SESSION_IMPL *)wt_session;
nr = 0;
/* Assert direct I/O is aligned and a multiple of the alignment. */
WT_ASSERT(session,
!win_fh->direct_io ||
S2C(session)->buffer_alignment == 0 ||
(!((uintptr_t)buf &
(uintptr_t)(S2C(session)->buffer_alignment - 1)) &&
len >= S2C(session)->buffer_alignment &&
len % S2C(session)->buffer_alignment == 0));
/* Break reads larger than 1GB into 1GB chunks. */
for (addr = buf; len > 0; addr += nr, len -= (size_t)nr, offset += nr) {
chunk = (DWORD)WT_MIN(len, WT_GIGABYTE);
overlapped.Offset = UINT32_MAX & offset;
overlapped.OffsetHigh = UINT32_MAX & (offset >> 32);
if (!ReadFile(
win_fh->filehandle, addr, chunk, &nr, &overlapped)) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-read: ReadFile: failed to read %lu "
"bytes at offset %" PRIuMAX ": %s",
file_handle->name, chunk, (uintmax_t)offset,
__wt_formatmessage(session, windows_error));
return (__wt_map_windows_error(windows_error));
}
}
return (0);
}
/*
* __win_file_size --
* Get the size of a file in bytes, by file handle.
*/
static int
__win_file_size(
WT_FILE_HANDLE *file_handle, WT_SESSION *wt_session, wt_off_t *sizep)
{
DWORD windows_error;
WT_FILE_HANDLE_WIN *win_fh;
WT_SESSION_IMPL *session;
LARGE_INTEGER size;
win_fh = (WT_FILE_HANDLE_WIN *)file_handle;
session = (WT_SESSION_IMPL *)wt_session;
if (GetFileSizeEx(win_fh->filehandle, &size) != 0) {
*sizep = size.QuadPart;
return (0);
}
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-size: GetFileSizeEx: %s",
file_handle->name, __wt_formatmessage(session, windows_error));
return (__wt_map_windows_error(windows_error));
}
/*
* __win_file_sync --
* MSVC fsync.
*/
static int
__win_file_sync(WT_FILE_HANDLE *file_handle, WT_SESSION *wt_session)
{
DWORD windows_error;
WT_FILE_HANDLE_WIN *win_fh;
WT_SESSION_IMPL *session;
win_fh = (WT_FILE_HANDLE_WIN *)file_handle;
session = (WT_SESSION_IMPL *)wt_session;
/*
* We don't open Windows system handles when opening directories
* for flushing, as it is not necessary (or possible) to flush
* a directory on Windows. Confirm the file handle is set before
* attempting to sync it.
*/
if (win_fh->filehandle == INVALID_HANDLE_VALUE)
return (0);
if (FlushFileBuffers(win_fh->filehandle) == FALSE) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s handle-sync: FlushFileBuffers: %s",
file_handle->name,
__wt_formatmessage(session, windows_error));
return (__wt_map_windows_error(windows_error));
}
return (0);
}
/*
* __win_file_set_end --
* Truncate or extend a file.
*/
static int
__win_file_set_end(
WT_FILE_HANDLE *file_handle, WT_SESSION *wt_session, wt_off_t len)
{
DWORD windows_error;
WT_FILE_HANDLE_WIN *win_fh;
WT_SESSION_IMPL *session;
LARGE_INTEGER largeint;
win_fh = (WT_FILE_HANDLE_WIN *)file_handle;
session = (WT_SESSION_IMPL *)wt_session;
largeint.QuadPart = len;
if (win_fh->filehandle_secondary == INVALID_HANDLE_VALUE)
WT_RET_MSG(session, EINVAL,
"%s: handle-set-end: no secondary handle",
file_handle->name);
if (SetFilePointerEx(win_fh->filehandle_secondary,
largeint, NULL, FILE_BEGIN) == FALSE) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-set-end: SetFilePointerEx: %s",
file_handle->name,
__wt_formatmessage(session, windows_error));
return (__wt_map_windows_error(windows_error));
}
if (SetEndOfFile(win_fh->filehandle_secondary) == FALSE) {
if (GetLastError() == ERROR_USER_MAPPED_FILE)
return (EBUSY);
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-set-end: SetEndOfFile: %s",
file_handle->name,
__wt_formatmessage(session, windows_error));
return (__wt_map_windows_error(windows_error));
}
return (0);
}
/*
* __win_file_write --
* Write a chunk.
*/
static int
__win_file_write(WT_FILE_HANDLE *file_handle,
WT_SESSION *wt_session, wt_off_t offset, size_t len, const void *buf)
{
DWORD chunk, nw, windows_error;
const uint8_t *addr;
OVERLAPPED overlapped = { 0 };
WT_FILE_HANDLE_WIN *win_fh;
WT_SESSION_IMPL *session;
win_fh = (WT_FILE_HANDLE_WIN *)file_handle;
session = (WT_SESSION_IMPL *)wt_session;
nw = 0;
/* Assert direct I/O is aligned and a multiple of the alignment. */
WT_ASSERT(session,
!win_fh->direct_io ||
S2C(session)->buffer_alignment == 0 ||
(!((uintptr_t)buf &
(uintptr_t)(S2C(session)->buffer_alignment - 1)) &&
len >= S2C(session)->buffer_alignment &&
len % S2C(session)->buffer_alignment == 0));
/* Break writes larger than 1GB into 1GB chunks. */
for (addr = buf; len > 0; addr += nw, len -= (size_t)nw, offset += nw) {
chunk = (DWORD)WT_MIN(len, WT_GIGABYTE);
overlapped.Offset = UINT32_MAX & offset;
overlapped.OffsetHigh = UINT32_MAX & (offset >> 32);
if (!WriteFile(
win_fh->filehandle, addr, chunk, &nw, &overlapped)) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-write: WriteFile: failed to write %lu "
"bytes at offset %" PRIuMAX ": %s",
file_handle->name, chunk, (uintmax_t)offset,
__wt_formatmessage(session, windows_error));
return (__wt_map_windows_error(windows_error));
}
}
return (0);
}
/*
* __win_open_file --
* Open a file handle.
*/
static int
__win_open_file(WT_FILE_SYSTEM *file_system, WT_SESSION *wt_session,
const char *name, WT_FS_OPEN_FILE_TYPE file_type, uint32_t flags,
WT_FILE_HANDLE **file_handlep)
{
DWORD dwCreationDisposition, windows_error;
WT_CONNECTION_IMPL *conn;
WT_DECL_RET;
WT_DECL_ITEM(name_wide);
WT_FILE_HANDLE *file_handle;
WT_FILE_HANDLE_WIN *win_fh;
WT_SESSION_IMPL *session;
int desired_access, f;
WT_UNUSED(file_system);
session = (WT_SESSION_IMPL *)wt_session;
conn = S2C(session);
*file_handlep = NULL;
WT_RET(__wt_calloc_one(session, &win_fh));
win_fh->direct_io = false;
/* Set up error handling. */
win_fh->filehandle =
win_fh->filehandle_secondary = INVALID_HANDLE_VALUE;
WT_ERR(__wt_to_utf16_string(session, name, &name_wide));
/*
* Opening a file handle on a directory is only to support filesystems
* that require a directory sync for durability, and Windows doesn't
* require that functionality: create an empty WT_FH structure with
* invalid handles.
*/
if (file_type == WT_FS_OPEN_FILE_TYPE_DIRECTORY)
goto directory_open;
desired_access = GENERIC_READ;
if (!LF_ISSET(WT_FS_OPEN_READONLY))
desired_access |= GENERIC_WRITE;
/*
* Security:
* The application may spawn a new process, and we don't want another
* process to have access to our file handles.
*
* TODO: Set tighter file permissions but set bInheritHandle to false
* to prevent inheritance
*/
f = FILE_ATTRIBUTE_NORMAL;
dwCreationDisposition = 0;
if (LF_ISSET(WT_FS_OPEN_CREATE)) {
dwCreationDisposition = CREATE_NEW;
if (LF_ISSET(WT_FS_OPEN_EXCLUSIVE))
dwCreationDisposition = CREATE_ALWAYS;
} else
dwCreationDisposition = OPEN_EXISTING;
/* Direct I/O. */
if (LF_ISSET(WT_FS_OPEN_DIRECTIO)) {
f |= FILE_FLAG_NO_BUFFERING;
win_fh->direct_io = true;
}
/* FILE_FLAG_WRITE_THROUGH does not require aligned buffers */
if (FLD_ISSET(conn->write_through, file_type))
f |= FILE_FLAG_WRITE_THROUGH;
if (file_type == WT_FS_OPEN_FILE_TYPE_LOG &&
FLD_ISSET(conn->txn_logsync, WT_LOG_DSYNC))
f |= FILE_FLAG_WRITE_THROUGH;
/* If the user indicated a random workload, disable read-ahead. */
if (file_type == WT_FS_OPEN_FILE_TYPE_DATA &&
LF_ISSET(WT_FS_OPEN_ACCESS_RAND))
f |= FILE_FLAG_RANDOM_ACCESS;
/* If the user indicated a sequential workload, set that. */
if (file_type == WT_FS_OPEN_FILE_TYPE_DATA &&
LF_ISSET(WT_FS_OPEN_ACCESS_SEQ))
f |= FILE_FLAG_SEQUENTIAL_SCAN;
win_fh->filehandle = CreateFileW(name_wide->data, desired_access,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, dwCreationDisposition, f, NULL);
if (win_fh->filehandle == INVALID_HANDLE_VALUE) {
if (LF_ISSET(WT_FS_OPEN_CREATE) &&
GetLastError() == ERROR_FILE_EXISTS)
win_fh->filehandle = CreateFileW(name_wide->data,
desired_access, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, f, NULL);
if (win_fh->filehandle == INVALID_HANDLE_VALUE) {
windows_error = __wt_getlasterror();
__wt_errx(session,
win_fh->direct_io ?
"%s: handle-open: CreateFileW: failed with direct "
"I/O configured, some filesystem types do not "
"support direct I/O: %s" :
"%s: handle-open: CreateFileW: %s",
name, __wt_formatmessage(session, windows_error));
WT_ERR(__wt_map_windows_error(windows_error));
}
}
/*
* Open a second handle to file to support file extension/truncation
* concurrently with reads on the file. Writes would also move the
* file pointer.
*/
if (!LF_ISSET(WT_FS_OPEN_READONLY)) {
win_fh->filehandle_secondary = CreateFileW(name_wide->data,
desired_access, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, f, NULL);
if (win_fh->filehandle_secondary == INVALID_HANDLE_VALUE) {
windows_error = __wt_getlasterror();
__wt_errx(session,
"%s: handle-open: Creatively: secondary: %s",
name, __wt_formatmessage(session, windows_error));
WT_ERR(__wt_map_windows_error(windows_error));
}
}
directory_open:
/* Initialize public information. */
file_handle = (WT_FILE_HANDLE *)win_fh;
WT_ERR(__wt_strdup(session, name, &file_handle->name));
file_handle->close = __win_file_close;
file_handle->fh_lock = __win_file_lock;
#ifdef WORDS_BIGENDIAN
/*
* The underlying objects are little-endian, mapping objects isn't
* currently supported on big-endian systems.
*/
#else
file_handle->fh_map = __wt_win_map;
file_handle->fh_unmap = __wt_win_unmap;
#endif
file_handle->fh_read = __win_file_read;
file_handle->fh_size = __win_file_size;
file_handle->fh_sync = __win_file_sync;
/* Extend and truncate share the same implementation. */
file_handle->fh_extend = __win_file_set_end;
file_handle->fh_truncate = __win_file_set_end;
file_handle->fh_write = __win_file_write;
*file_handlep = file_handle;
__wt_scr_free(session, &name_wide);
return (0);
err: __wt_scr_free(session, &name_wide);
WT_TRET(__win_file_close((WT_FILE_HANDLE *)win_fh, wt_session));
return (ret);
}
/*
* __win_terminate --
* Discard a Windows configuration.
*/
static int
__win_terminate(WT_FILE_SYSTEM *file_system, WT_SESSION *wt_session)
{
WT_SESSION_IMPL *session;
session = (WT_SESSION_IMPL *)wt_session;
__wt_free(session, file_system);
return (0);
}
/*
* __wt_os_win --
* Initialize a MSVC configuration.
*/
int
__wt_os_win(WT_SESSION_IMPL *session)
{
WT_CONNECTION_IMPL *conn;
WT_FILE_SYSTEM *file_system;
conn = S2C(session);
WT_RET(__wt_calloc_one(session, &file_system));
/* Initialize the Windows jump table. */
file_system->fs_directory_list = __wt_win_directory_list;
file_system->fs_directory_list_free = __wt_win_directory_list_free;
file_system->fs_exist = __win_fs_exist;
file_system->fs_open_file = __win_open_file;
file_system->fs_remove = __win_fs_remove;
file_system->fs_rename = __win_fs_rename;
file_system->fs_size = __wt_win_fs_size;
file_system->terminate = __win_terminate;
/* Switch it into place. */
conn->file_system = file_system;
return (0);
}
|