2 * Force a readahead of files by opening them and reading the first bytes
4 * Copyright (C) Volker Lendecke 2008
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.
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.
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.
22 #include "system/filesys.h"
26 struct preopen_helper {
27 struct preopen_state *state;
34 struct preopen_state {
36 struct preopen_helper *helpers;
38 size_t to_read; /* How many bytes to read in children? */
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? */
45 int fnum_sent; /* last fname sent to children */
47 int fnum_queue_end; /* last fname to be sent, based on
48 * last open call + preopen:queuelen
51 name_compare_entry *preopen_names;
54 static void preopen_helper_destroy(struct preopen_helper *c)
59 kill(c->pid, SIGKILL);
60 waitpid(c->pid, &status, 0);
64 static void preopen_queue_run(struct preopen_state *state)
69 pdelimiter = state->template_fname + state->number_start
71 delimiter = *pdelimiter;
73 while (state->fnum_sent < state->fnum_queue_end) {
79 for (helper=0; helper<state->num_helpers; helper++) {
80 if (state->helpers[helper].busy) {
85 if (helper == state->num_helpers) {
86 /* everyone is busy */
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;
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;
101 if (written != to_write) {
102 preopen_helper_destroy(&state->helpers[helper]);
104 state->fnum_sent += 1;
108 static void preopen_helper_readable(struct event_context *ev,
109 struct fd_event *fde, uint16_t flags,
112 struct preopen_helper *helper = (struct preopen_helper *)priv;
113 struct preopen_state *state = helper->state;
117 if ((flags & EVENT_FD_READ) == 0) {
121 nread = read(helper->fd, &c, 1);
123 preopen_helper_destroy(helper);
127 helper->busy = false;
129 preopen_queue_run(state);
132 static int preopen_helpers_destructor(struct preopen_state *c)
136 for (i=0; i<c->num_helpers; i++) {
137 if (c->helpers[i].fd == -1) {
140 preopen_helper_destroy(&c->helpers[i]);
146 static bool preopen_helper_open_one(int sock_fd, char **pnamebuf,
147 size_t to_read, void *filebuf)
149 char *namebuf = *pnamebuf;
150 ssize_t nwritten, nread;
156 while ((nread == 0) || (namebuf[nread-1] != '\0')) {
159 thistime = read(sock_fd, namebuf + nread,
160 talloc_get_size(namebuf) - nread);
167 if (nread == talloc_get_size(namebuf)) {
168 namebuf = TALLOC_REALLOC_ARRAY(
170 talloc_get_size(namebuf) * 2);
171 if (namebuf == NULL) {
178 fd = open(namebuf, O_RDONLY);
182 nread = read(fd, filebuf, to_read);
186 nwritten = write(sock_fd, &c, 1);
190 static bool preopen_helper(int fd, size_t to_read)
195 namebuf = TALLOC_ARRAY(NULL, char, 1024);
196 if (namebuf == NULL) {
200 readbuf = talloc_size(NULL, to_read);
201 if (readbuf == NULL) {
202 TALLOC_FREE(namebuf);
206 while (preopen_helper_open_one(fd, &namebuf, to_read, readbuf)) {
210 TALLOC_FREE(readbuf);
211 TALLOC_FREE(namebuf);
215 static NTSTATUS preopen_init_helper(struct preopen_helper *h)
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)));
229 return map_nt_error_from_unix(errno);
234 preopen_helper(fdpair[1], h->state->to_read);
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) {
244 return NT_STATUS_NO_MEMORY;
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)
254 struct preopen_state *result;
257 result = talloc(mem_ctx, struct preopen_state);
258 if (result == NULL) {
259 return NT_STATUS_NO_MEMORY;
262 result->num_helpers = num_helpers;
263 result->helpers = TALLOC_ARRAY(result, struct preopen_helper,
265 if (result->helpers == NULL) {
267 return NT_STATUS_NO_MEMORY;
270 result->to_read = to_read;
271 result->queue_max = queue_max;
272 result->template_fname = NULL;
273 result->fnum_sent = 0;
275 for (i=0; i<num_helpers; i++) {
276 result->helpers[i].state = result;
277 result->helpers[i].fd = -1;
280 talloc_set_destructor(result, preopen_helpers_destructor);
282 for (i=0; i<num_helpers; i++) {
283 preopen_init_helper(&result->helpers[i]);
290 static void preopen_free_helpers(void **ptr)
295 static struct preopen_state *preopen_state_get(vfs_handle_struct *handle)
297 struct preopen_state *state;
299 const char *namelist;
301 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
302 SMB_VFS_HANDLE_GET_DATA(handle, state, struct preopen_state,
307 namelist = lp_parm_const_string(SNUM(handle->conn), "preopen", "names",
310 if (namelist == NULL) {
314 status = preopen_init_helpers(
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),
320 if (!NT_STATUS_IS_OK(status)) {
324 set_namearray(&state->preopen_names, (char *)namelist);
326 if (state->preopen_names == NULL) {
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);
339 static bool preopen_parse_fname(const char *fname, unsigned long *pnum,
340 size_t *pstart_idx, int *pnum_digits)
345 p = strrchr_m(fname, '/');
351 while (p[0] != '\0') {
352 if (isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])) {
358 /* no digits around */
362 num = strtoul(p, (char **)&q, 10);
370 *pstart_idx = (p - fname);
371 *pnum_digits = (q - p);
375 static int preopen_open(vfs_handle_struct *handle,
376 struct smb_filename *smb_fname, files_struct *fsp,
377 int flags, mode_t mode)
379 struct preopen_state *state;
383 DEBUG(10, ("preopen_open called on %s\n", smb_fname_str_dbg(smb_fname)));
385 state = preopen_state_get(handle);
387 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
390 res = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
395 if (flags != O_RDONLY) {
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)));
405 TALLOC_FREE(state->template_fname);
406 state->template_fname = talloc_asprintf(
407 state, "%s/%s", fsp->conn->connectpath, smb_fname->base_name);
409 if (state->template_fname == NULL) {
413 if (!preopen_parse_fname(state->template_fname, &num,
414 &state->number_start, &state->num_digits)) {
415 TALLOC_FREE(state->template_fname);
419 if (num > state->fnum_sent) {
421 * Helpers were too slow, there's no point in reading
422 * files in helpers that we already read in the
425 state->fnum_sent = num;
428 if ((state->fnum_queue_end != 0) /* Something was started earlier */
429 && (num < (state->fnum_queue_end - state->queue_max))) {
431 * "num" is before the queue we announced. This means
432 * a new run is started.
434 state->fnum_sent = num;
437 state->fnum_queue_end = num + state->queue_max;
439 preopen_queue_run(state);
444 static struct vfs_fn_pointers vfs_preopen_fns = {
448 NTSTATUS vfs_preopen_init(void);
449 NTSTATUS vfs_preopen_init(void)
451 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
452 "preopen", &vfs_preopen_fns);