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
|
/*-
* Copyright (c) 2014-2015 MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __open_directory --
* Open up a file handle to a directory.
*/
static int
__open_directory(WT_SESSION_IMPL *session, char *path, int *fd)
{
WT_DECL_RET;
WT_SYSCALL_RETRY(((*fd =
open(path, O_RDONLY, 0444)) == -1 ? 1 : 0), ret);
if (ret != 0)
WT_RET_MSG(session, ret, "%s: open_directory", path);
return (ret);
}
/*
* __wt_open --
* Open a file handle.
*/
int
__wt_open(WT_SESSION_IMPL *session,
const char *name, bool ok_create, bool exclusive, int dio_type, WT_FH **fhp)
{
WT_CONNECTION_IMPL *conn;
WT_DECL_RET;
WT_FH *fh, *tfh;
mode_t mode;
uint64_t bucket, hash;
int direct_io, f, fd, matched;
char *path;
conn = S2C(session);
direct_io = 0;
fh = NULL;
fd = -1;
path = NULL;
WT_RET(__wt_verbose(session, WT_VERB_FILEOPS, "%s: open", name));
/* Increment the reference count if we already have the file open. */
matched = 0;
hash = __wt_hash_city64(name, strlen(name));
bucket = hash % WT_HASH_ARRAY_SIZE;
__wt_spin_lock(session, &conn->fh_lock);
TAILQ_FOREACH(tfh, &conn->fhhash[bucket], hashq) {
if (strcmp(name, tfh->name) == 0) {
++tfh->ref;
*fhp = tfh;
matched = 1;
break;
}
}
__wt_spin_unlock(session, &conn->fh_lock);
if (matched)
return (0);
WT_RET(__wt_filename(session, name, &path));
if (dio_type == WT_FILE_TYPE_DIRECTORY) {
WT_ERR(__open_directory(session, path, &fd));
goto setupfh;
}
f = O_RDWR;
#ifdef O_BINARY
/* Windows clones: we always want to treat the file as a binary. */
f |= O_BINARY;
#endif
#ifdef O_CLOEXEC
/*
* Security:
* The application may spawn a new process, and we don't want another
* process to have access to our file handles.
*/
f |= O_CLOEXEC;
#endif
#ifdef O_NOATIME
/* Avoid updating metadata for read-only workloads. */
if (dio_type == WT_FILE_TYPE_DATA ||
dio_type == WT_FILE_TYPE_CHECKPOINT)
f |= O_NOATIME;
#endif
if (ok_create) {
f |= O_CREAT;
if (exclusive)
f |= O_EXCL;
mode = 0666;
} else
mode = 0;
#ifdef O_DIRECT
if (dio_type && FLD_ISSET(conn->direct_io, dio_type)) {
f |= O_DIRECT;
direct_io = 1;
}
#endif
if (dio_type == WT_FILE_TYPE_LOG &&
FLD_ISSET(conn->txn_logsync, WT_LOG_DSYNC))
#ifdef O_DSYNC
f |= O_DSYNC;
#elif defined(O_SYNC)
f |= O_SYNC;
#else
WT_ERR_MSG(session, ENOTSUP,
"Unsupported log sync mode requested");
#endif
WT_SYSCALL_RETRY(((fd = open(path, f, mode)) == -1 ? 1 : 0), ret);
if (ret != 0)
WT_ERR_MSG(session, ret,
direct_io ?
"%s: open failed with direct I/O configured, some "
"filesystem types do not support direct I/O" : "%s", path);
setupfh:
#if defined(HAVE_FCNTL) && defined(FD_CLOEXEC) && !defined(O_CLOEXEC)
/*
* Security:
* The application may spawn a new process, and we don't want another
* process to have access to our file handles. There's an obvious
* race here, so we prefer the flag to open if available.
*/
if ((f = fcntl(fd, F_GETFD)) == -1 ||
fcntl(fd, F_SETFD, f | FD_CLOEXEC) == -1)
WT_ERR_MSG(session, __wt_errno(), "%s: fcntl", name);
#endif
#if defined(HAVE_POSIX_FADVISE)
/* Disable read-ahead on trees: it slows down random read workloads. */
if (dio_type == WT_FILE_TYPE_DATA ||
dio_type == WT_FILE_TYPE_CHECKPOINT)
WT_ERR(posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM));
#endif
WT_ERR(__wt_calloc_one(session, &fh));
WT_ERR(__wt_strdup(session, name, &fh->name));
fh->name_hash = hash;
fh->fd = fd;
fh->ref = 1;
fh->direct_io = direct_io;
/* Set the file's size. */
WT_ERR(__wt_filesize(session, fh, &fh->size));
/* Configure file extension. */
if (dio_type == WT_FILE_TYPE_DATA ||
dio_type == WT_FILE_TYPE_CHECKPOINT)
fh->extend_len = conn->data_extend_len;
/* Configure fallocate/posix_fallocate calls. */
__wt_fallocate_config(session, fh);
/*
* Repeat the check for a match, but then link onto the database's list
* of files.
*/
matched = 0;
__wt_spin_lock(session, &conn->fh_lock);
TAILQ_FOREACH(tfh, &conn->fhhash[bucket], hashq) {
if (strcmp(name, tfh->name) == 0) {
++tfh->ref;
*fhp = tfh;
matched = 1;
break;
}
}
if (!matched) {
WT_CONN_FILE_INSERT(conn, fh, bucket);
(void)__wt_atomic_add32(&conn->open_file_count, 1);
*fhp = fh;
}
__wt_spin_unlock(session, &conn->fh_lock);
if (matched) {
err: if (fh != NULL) {
__wt_free(session, fh->name);
__wt_free(session, fh);
}
if (fd != -1)
(void)close(fd);
}
__wt_free(session, path);
return (ret);
}
/*
* __wt_close --
* Close a file handle.
*/
int
__wt_close(WT_SESSION_IMPL *session, WT_FH **fhp)
{
WT_CONNECTION_IMPL *conn;
WT_DECL_RET;
WT_FH *fh;
uint64_t bucket;
conn = S2C(session);
if (*fhp == NULL)
return (0);
fh = *fhp;
*fhp = NULL;
__wt_spin_lock(session, &conn->fh_lock);
if (fh == NULL || fh->ref == 0 || --fh->ref > 0) {
__wt_spin_unlock(session, &conn->fh_lock);
return (0);
}
/* Remove from the list. */
bucket = fh->name_hash % WT_HASH_ARRAY_SIZE;
WT_CONN_FILE_REMOVE(conn, fh, bucket);
(void)__wt_atomic_sub32(&conn->open_file_count, 1);
__wt_spin_unlock(session, &conn->fh_lock);
/* Discard the memory. */
if (close(fh->fd) != 0) {
ret = __wt_errno();
__wt_err(session, ret, "close: %s", fh->name);
}
__wt_free(session, fh->name);
__wt_free(session, fh);
return (ret);
}
|