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
|
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
static void nxt_job_file_open_and_read(nxt_task_t *task, void *obj, void *data);
static nxt_int_t nxt_job_file_open(nxt_job_file_t *jbf);
static nxt_int_t nxt_job_file_info(nxt_job_file_t *jbf);
static nxt_int_t nxt_job_file_mmap(nxt_job_file_t *jbf, size_t size);
static nxt_int_t nxt_job_file_read_data(nxt_job_file_t *jbf, size_t size);
static nxt_int_t nxt_job_file_read_required(nxt_job_file_t *jbf);
nxt_job_file_t *
nxt_job_file_create(nxt_mp_t *mp)
{
nxt_job_file_t *jbf;
jbf = nxt_job_create(mp, sizeof(nxt_job_file_t));
if (nxt_fast_path(jbf != NULL)) {
jbf->file.fd = NXT_FILE_INVALID;
jbf->file.accessed = NXT_FILE_ACCESSED_LONG_AGO;
jbf->read_required = nxt_job_file_read_required;
}
return jbf;
}
void
nxt_job_file_init(nxt_job_file_t *jbf)
{
nxt_job_init(&jbf->job, sizeof(nxt_job_file_t));
jbf->file.fd = NXT_FILE_INVALID;
jbf->file.accessed = NXT_FILE_ACCESSED_LONG_AGO;
jbf->read_required = nxt_job_file_read_required;
}
/*
* Must be a function but not a macro, because
* it can be used as function pointer.
*/
void
nxt_job_file_read(nxt_task_t *task, nxt_job_t *job)
{
nxt_job_start(task, job, nxt_job_file_open_and_read);
}
static void
nxt_job_file_open_and_read(nxt_task_t *task, void *obj, void *data)
{
size_t size;
nxt_int_t n;
nxt_bool_t read_ahead;
nxt_file_t *file;
nxt_job_file_t *jbf;
nxt_work_handler_t handler;
jbf = obj;
file = &jbf->file;
nxt_debug(task, "file job read: \"%FN\"", file->name);
if (file->fd != NXT_FILE_INVALID && jbf->close_before_open) {
nxt_file_close(file);
file->fd = NXT_FILE_INVALID;
}
if (file->fd == NXT_FILE_INVALID) {
switch (nxt_job_file_open(jbf)) {
case NXT_OK:
break;
case NXT_DECLINED:
handler = jbf->ready_handler;
goto done;
default: /* NXT_ERROR */
handler = jbf->error_handler;
goto done;
}
}
if (file->size > 0) {
if (jbf->buffer != NULL) {
size = nxt_buf_mem_size(&jbf->buffer->mem);
size = nxt_min(file->size, (nxt_off_t) size);
read_ahead = nxt_buf_is_mmap(jbf->buffer);
} else {
size = nxt_min(file->size, 1024 * 1024);
read_ahead = jbf->read_ahead;
}
if (read_ahead) {
nxt_file_read_ahead(&jbf->file, jbf->offset, size);
}
if (jbf->buffer != NULL) {
if (nxt_buf_is_mmap(jbf->buffer)) {
n = nxt_job_file_mmap(jbf, size);
} else {
n = nxt_job_file_read_data(jbf, size);
}
if (nxt_slow_path(n != NXT_OK)) {
handler = jbf->error_handler;
goto done;
}
}
}
if (jbf->offset == file->size) {
jbf->complete = 1;
if (jbf->close) {
nxt_file_close(file);
file->fd = NXT_FILE_INVALID;
}
}
nxt_job_return(task, &jbf->job, jbf->ready_handler);
return;
done:
if (file->fd != NXT_FILE_INVALID) {
nxt_file_close(file);
file->fd = NXT_FILE_INVALID;
}
nxt_job_return(task, &jbf->job, handler);
}
static nxt_int_t
nxt_job_file_open(nxt_job_file_t *jbf)
{
nxt_int_t n;
if (jbf->test_before_open) {
n = nxt_job_file_info(jbf);
if (n != NXT_OK) {
goto test_directory;
}
if (jbf->file.type == NXT_FILE_DIRECTORY) {
return NXT_DECLINED;
}
if (jbf->read_required(jbf) != NXT_OK) {
return NXT_DECLINED;
}
}
n = nxt_file_open(&jbf->file, NXT_FILE_RDONLY, NXT_FILE_OPEN, 0);
if (n == NXT_OK) {
n = nxt_job_file_info(jbf);
if (nxt_fast_path(n == NXT_OK)) {
if (jbf->file.type == NXT_FILE_DIRECTORY) {
return NXT_DECLINED;
}
return jbf->read_required(jbf);
}
return n;
}
test_directory:
if (jbf->directory_end != 0
&& jbf->file.error != NXT_ENOTDIR
&& jbf->file.error != NXT_ENAMETOOLONG
&& jbf->file.error != NXT_EACCES)
{
jbf->file.name[jbf->directory_end] = '\0';
return nxt_job_file_info(jbf);
}
return n;
}
static nxt_int_t
nxt_job_file_info(nxt_job_file_t *jbf)
{
nxt_int_t n;
nxt_file_t *file;
nxt_file_info_t fi;
file = &jbf->file;
n = nxt_file_info(file, &fi);
if (n != NXT_OK) {
return NXT_ERROR;
}
if (nxt_is_file(&fi)) {
file->type = NXT_FILE_REGULAR;
file->size = nxt_file_size(&fi);
file->mtime = nxt_file_mtime(&fi);
} else if (nxt_is_dir(&fi)) {
file->type = NXT_FILE_DIRECTORY;
file->size = nxt_file_size(&fi);
file->mtime = nxt_file_mtime(&fi);
}
return NXT_OK;
}
static nxt_int_t
nxt_job_file_mmap(nxt_job_file_t *jbf, size_t size)
{
u_char *p, *end;
static nxt_uint_t n;
p = nxt_mem_map(NULL, &jbf->buffer->mmap, size, NXT_MEM_MAP_READ,
(NXT_MEM_MAP_FILE | NXT_MEM_MAP_PREFAULT),
jbf->file.fd, jbf->offset);
if (nxt_fast_path(p != NXT_MEM_MAP_FAILED)) {
end = p + size;
jbf->buffer->mem.pos = p;
jbf->buffer->mem.free = end;
jbf->buffer->mem.start = p;
jbf->buffer->mem.end = end;
jbf->buffer->file_end += size;
jbf->offset += size;
/*
* The mapped pages should be already preloaded in the kernel page
* cache by nxt_file_read_ahead(). Touching them should wire the pages
* in user land memory if mmap() did not do this. Adding to the static
* variable "n" disables the loop elimination during optimization.
*/
n += *p;
for (p = nxt_align_ptr(p, nxt_pagesize); p < end; p += nxt_pagesize) {
n += *p;
}
return NXT_OK;
}
return NXT_ERROR;
}
static nxt_int_t
nxt_job_file_read_data(nxt_job_file_t *jbf, size_t size)
{
ssize_t n;
n = nxt_file_read(&jbf->file, jbf->buffer->mem.free, size, jbf->offset);
if (nxt_fast_path(n > 0)) {
jbf->buffer->mem.free += n;
jbf->offset += n;
if (nxt_buf_is_file(jbf->buffer)) {
jbf->buffer->file_end += n;
}
return NXT_OK;
}
return NXT_ERROR;
}
static nxt_int_t
nxt_job_file_read_required(nxt_job_file_t *jbf)
{
return NXT_OK;
}
|