s3-talloc Change TALLOC_REALLOC_ARRAY() to talloc_realloc()
[samba.git] / source3 / modules / vfs_preopen.c
1 /*
2  * Force a readahead of files by opening them and reading the first bytes
3  *
4  * Copyright (C) Volker Lendecke 2008
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24
25 struct preopen_state;
26
27 struct preopen_helper {
28         struct preopen_state *state;
29         struct fd_event *fde;
30         pid_t pid;
31         int fd;
32         bool busy;
33 };
34
35 struct preopen_state {
36         int num_helpers;
37         struct preopen_helper *helpers;
38
39         size_t to_read;         /* How many bytes to read in children? */
40         int queue_max;
41
42         char *template_fname;   /* Filename to be sent to children */
43         size_t number_start;    /* start offset into "template_fname" */
44         int num_digits;         /* How many digits is the number long? */
45
46         int fnum_sent;          /* last fname sent to children */
47
48         int fnum_queue_end;     /* last fname to be sent, based on
49                                  * last open call + preopen:queuelen
50                                  */
51
52         name_compare_entry *preopen_names;
53 };
54
55 static void preopen_helper_destroy(struct preopen_helper *c)
56 {
57         int status;
58         close(c->fd);
59         c->fd = -1;
60         kill(c->pid, SIGKILL);
61         waitpid(c->pid, &status, 0);
62         c->busy = true;
63 }
64
65 static void preopen_queue_run(struct preopen_state *state)
66 {
67         char *pdelimiter;
68         char delimiter;
69
70         pdelimiter = state->template_fname + state->number_start
71                 + state->num_digits;
72         delimiter = *pdelimiter;
73
74         while (state->fnum_sent < state->fnum_queue_end) {
75
76                 ssize_t written;
77                 size_t to_write;
78                 int helper;
79
80                 for (helper=0; helper<state->num_helpers; helper++) {
81                         if (state->helpers[helper].busy) {
82                                 continue;
83                         }
84                         break;
85                 }
86                 if (helper == state->num_helpers) {
87                         /* everyone is busy */
88                         return;
89                 }
90
91                 snprintf(state->template_fname + state->number_start,
92                          state->num_digits + 1,
93                          "%.*lu", state->num_digits,
94                          (long unsigned int)(state->fnum_sent + 1));
95                 *pdelimiter = delimiter;
96
97                 to_write = talloc_get_size(state->template_fname);
98                 written = write_data(state->helpers[helper].fd,
99                                      state->template_fname, to_write);
100                 state->helpers[helper].busy = true;
101
102                 if (written != to_write) {
103                         preopen_helper_destroy(&state->helpers[helper]);
104                 }
105                 state->fnum_sent += 1;
106         }
107 }
108
109 static void preopen_helper_readable(struct event_context *ev,
110                                     struct fd_event *fde, uint16_t flags,
111                                     void *priv)
112 {
113         struct preopen_helper *helper = (struct preopen_helper *)priv;
114         struct preopen_state *state = helper->state;
115         ssize_t nread;
116         char c;
117
118         if ((flags & EVENT_FD_READ) == 0) {
119                 return;
120         }
121
122         nread = read(helper->fd, &c, 1);
123         if (nread <= 0) {
124                 preopen_helper_destroy(helper);
125                 return;
126         }
127
128         helper->busy = false;
129
130         preopen_queue_run(state);
131 }
132
133 static int preopen_helpers_destructor(struct preopen_state *c)
134 {
135         int i;
136
137         for (i=0; i<c->num_helpers; i++) {
138                 if (c->helpers[i].fd == -1) {
139                         continue;
140                 }
141                 preopen_helper_destroy(&c->helpers[i]);
142         }
143
144         return 0;
145 }
146
147 static bool preopen_helper_open_one(int sock_fd, char **pnamebuf,
148                                     size_t to_read, void *filebuf)
149 {
150         char *namebuf = *pnamebuf;
151         ssize_t nwritten, nread;
152         char c = 0;
153         int fd;
154
155         nread = 0;
156
157         while ((nread == 0) || (namebuf[nread-1] != '\0')) {
158                 ssize_t thistime;
159
160                 thistime = read(sock_fd, namebuf + nread,
161                                 talloc_get_size(namebuf) - nread);
162                 if (thistime <= 0) {
163                         return false;
164                 }
165
166                 nread += thistime;
167
168                 if (nread == talloc_get_size(namebuf)) {
169                         namebuf = talloc_realloc(
170                                 NULL, namebuf, char,
171                                 talloc_get_size(namebuf) * 2);
172                         if (namebuf == NULL) {
173                                 return false;
174                         }
175                         *pnamebuf = namebuf;
176                 }
177         }
178
179         fd = open(namebuf, O_RDONLY);
180         if (fd == -1) {
181                 goto done;
182         }
183         nread = read(fd, filebuf, to_read);
184         close(fd);
185
186  done:
187         nwritten = write(sock_fd, &c, 1);
188         return true;
189 }
190
191 static bool preopen_helper(int fd, size_t to_read)
192 {
193         char *namebuf;
194         void *readbuf;
195
196         namebuf = TALLOC_ARRAY(NULL, char, 1024);
197         if (namebuf == NULL) {
198                 return false;
199         }
200
201         readbuf = talloc_size(NULL, to_read);
202         if (readbuf == NULL) {
203                 TALLOC_FREE(namebuf);
204                 return false;
205         }
206
207         while (preopen_helper_open_one(fd, &namebuf, to_read, readbuf)) {
208                 ;
209         }
210
211         TALLOC_FREE(readbuf);
212         TALLOC_FREE(namebuf);
213         return false;
214 }
215
216 static NTSTATUS preopen_init_helper(struct preopen_helper *h)
217 {
218         int fdpair[2];
219         NTSTATUS status;
220
221         if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
222                 status = map_nt_error_from_unix(errno);
223                 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
224                 return status;
225         }
226
227         h->pid = sys_fork();
228
229         if (h->pid == -1) {
230                 return map_nt_error_from_unix(errno);
231         }
232
233         if (h->pid == 0) {
234                 close(fdpair[0]);
235                 preopen_helper(fdpair[1], h->state->to_read);
236                 exit(0);
237         }
238         close(fdpair[1]);
239         h->fd = fdpair[0];
240         h->fde = event_add_fd(server_event_context(), h->state, h->fd,
241                               EVENT_FD_READ, preopen_helper_readable, h);
242         if (h->fde == NULL) {
243                 close(h->fd);
244                 h->fd = -1;
245                 return NT_STATUS_NO_MEMORY;
246         }
247         h->busy = false;
248         return NT_STATUS_OK;
249 }
250
251 static NTSTATUS preopen_init_helpers(TALLOC_CTX *mem_ctx, size_t to_read,
252                                      int num_helpers, int queue_max,
253                                      struct preopen_state **presult)
254 {
255         struct preopen_state *result;
256         int i;
257
258         result = talloc(mem_ctx, struct preopen_state);
259         if (result == NULL) {
260                 return NT_STATUS_NO_MEMORY;
261         }
262
263         result->num_helpers = num_helpers;
264         result->helpers = TALLOC_ARRAY(result, struct preopen_helper,
265                                        num_helpers);
266         if (result->helpers == NULL) {
267                 TALLOC_FREE(result);
268                 return NT_STATUS_NO_MEMORY;
269         }
270
271         result->to_read = to_read;
272         result->queue_max = queue_max;
273         result->template_fname = NULL;
274         result->fnum_sent = 0;
275
276         for (i=0; i<num_helpers; i++) {
277                 result->helpers[i].state = result;
278                 result->helpers[i].fd = -1;
279         }
280
281         talloc_set_destructor(result, preopen_helpers_destructor);
282
283         for (i=0; i<num_helpers; i++) {
284                 preopen_init_helper(&result->helpers[i]);
285         }
286
287         *presult = result;
288         return NT_STATUS_OK;
289 }
290
291 static void preopen_free_helpers(void **ptr)
292 {
293         TALLOC_FREE(*ptr);
294 }
295
296 static struct preopen_state *preopen_state_get(vfs_handle_struct *handle)
297 {
298         struct preopen_state *state;
299         NTSTATUS status;
300         const char *namelist;
301
302         if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
303                 SMB_VFS_HANDLE_GET_DATA(handle, state, struct preopen_state,
304                                         return NULL);
305                 return state;
306         }
307
308         namelist = lp_parm_const_string(SNUM(handle->conn), "preopen", "names",
309                                         NULL);
310
311         if (namelist == NULL) {
312                 return NULL;
313         }
314
315         status = preopen_init_helpers(
316                 NULL,
317                 lp_parm_int(SNUM(handle->conn), "preopen", "num_bytes", 1),
318                 lp_parm_int(SNUM(handle->conn), "preopen", "helpers", 1),
319                 lp_parm_int(SNUM(handle->conn), "preopen", "queuelen", 10),
320                 &state);
321         if (!NT_STATUS_IS_OK(status)) {
322                 return NULL;
323         }
324
325         set_namearray(&state->preopen_names, namelist);
326
327         if (state->preopen_names == NULL) {
328                 TALLOC_FREE(state);
329                 return NULL;
330         }
331
332         if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
333                 SMB_VFS_HANDLE_SET_DATA(handle, state, preopen_free_helpers,
334                                         struct preopen_state, return NULL);
335         }
336
337         return state;
338 }
339
340 static bool preopen_parse_fname(const char *fname, unsigned long *pnum,
341                                 size_t *pstart_idx, int *pnum_digits)
342 {
343         const char *p, *q;
344         unsigned long num;
345
346         p = strrchr_m(fname, '/');
347         if (p == NULL) {
348                 p = fname;
349         }
350
351         p += 1;
352         while (p[0] != '\0') {
353                 if (isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])) {
354                         break;
355                 }
356                 p += 1;
357         }
358         if (*p == '\0') {
359                 /* no digits around */
360                 return false;
361         }
362
363         num = strtoul(p, (char **)&q, 10);
364
365         if (num+1 < num) {
366                 /* overflow */
367                 return false;
368         }
369
370         *pnum = num;
371         *pstart_idx = (p - fname);
372         *pnum_digits = (q - p);
373         return true;
374 }
375
376 static int preopen_open(vfs_handle_struct *handle,
377                         struct smb_filename *smb_fname, files_struct *fsp,
378                         int flags, mode_t mode)
379 {
380         struct preopen_state *state;
381         int res;
382         unsigned long num;
383
384         DEBUG(10, ("preopen_open called on %s\n", smb_fname_str_dbg(smb_fname)));
385
386         state = preopen_state_get(handle);
387         if (state == NULL) {
388                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
389         }
390
391         res = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
392         if (res == -1) {
393                 return -1;
394         }
395
396         if (flags != O_RDONLY) {
397                 return res;
398         }
399
400         if (!is_in_path(smb_fname->base_name, state->preopen_names, true)) {
401                 DEBUG(10, ("%s does not match the preopen:names list\n",
402                            smb_fname_str_dbg(smb_fname)));
403                 return res;
404         }
405
406         TALLOC_FREE(state->template_fname);
407         state->template_fname = talloc_asprintf(
408                 state, "%s/%s", fsp->conn->connectpath, smb_fname->base_name);
409
410         if (state->template_fname == NULL) {
411                 return res;
412         }
413
414         if (!preopen_parse_fname(state->template_fname, &num,
415                                  &state->number_start, &state->num_digits)) {
416                 TALLOC_FREE(state->template_fname);
417                 return res;
418         }
419
420         if (num > state->fnum_sent) {
421                 /*
422                  * Helpers were too slow, there's no point in reading
423                  * files in helpers that we already read in the
424                  * parent.
425                  */
426                 state->fnum_sent = num;
427         }
428
429         if ((state->fnum_queue_end != 0) /* Something was started earlier */
430             && (num < (state->fnum_queue_end - state->queue_max))) {
431                 /*
432                  * "num" is before the queue we announced. This means
433                  * a new run is started.
434                  */
435                 state->fnum_sent = num;
436         }
437
438         state->fnum_queue_end = num + state->queue_max;
439
440         preopen_queue_run(state);
441
442         return res;
443 }
444
445 static struct vfs_fn_pointers vfs_preopen_fns = {
446         .open_fn = preopen_open
447 };
448
449 NTSTATUS vfs_preopen_init(void);
450 NTSTATUS vfs_preopen_init(void)
451 {
452         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
453                                 "preopen", &vfs_preopen_fns);
454 }