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