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