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"
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 #include "lib/util_matching.h"
28 #include "lib/global_contexts.h"
30 static int vfs_preopen_debug_level = DBGC_VFS;
33 #define DBGC_CLASS vfs_preopen_debug_level
37 struct preopen_helper {
38 struct preopen_state *state;
39 struct tevent_fd *fde;
45 struct preopen_state {
47 struct preopen_helper *helpers;
49 size_t to_read; /* How many bytes to read in children? */
52 char *template_fname; /* Filename to be sent to children */
53 size_t number_start; /* start offset into "template_fname" */
54 int num_digits; /* How many digits is the number long? */
56 int fnum_sent; /* last fname sent to children */
58 int fnum_queue_end; /* last fname to be sent, based on
59 * last open call + preopen:queuelen
62 struct samba_path_matching *preopen_names;
65 static void preopen_helper_destroy(struct preopen_helper *c)
71 kill(c->pid, SIGKILL);
72 waitpid(c->pid, &status, 0);
76 static void preopen_queue_run(struct preopen_state *state)
81 pdelimiter = state->template_fname + state->number_start
83 delimiter = *pdelimiter;
85 while (state->fnum_sent < state->fnum_queue_end) {
91 for (helper=0; helper<state->num_helpers; helper++) {
92 if (state->helpers[helper].busy) {
97 if (helper == state->num_helpers) {
98 /* everyone is busy */
102 snprintf(state->template_fname + state->number_start,
103 state->num_digits + 1,
104 "%.*lu", state->num_digits,
105 (long unsigned int)(state->fnum_sent + 1));
106 *pdelimiter = delimiter;
108 to_write = talloc_get_size(state->template_fname);
109 written = write_data(state->helpers[helper].fd,
110 state->template_fname, to_write);
111 state->helpers[helper].busy = true;
113 if (written != to_write) {
114 preopen_helper_destroy(&state->helpers[helper]);
116 state->fnum_sent += 1;
120 static void preopen_helper_readable(struct tevent_context *ev,
121 struct tevent_fd *fde, uint16_t flags,
124 struct preopen_helper *helper = (struct preopen_helper *)priv;
125 struct preopen_state *state = helper->state;
129 if ((flags & TEVENT_FD_READ) == 0) {
133 nread = read(helper->fd, &c, 1);
135 preopen_helper_destroy(helper);
139 helper->busy = false;
141 preopen_queue_run(state);
144 static int preopen_helpers_destructor(struct preopen_state *c)
148 for (i=0; i<c->num_helpers; i++) {
149 if (c->helpers[i].fd == -1) {
152 preopen_helper_destroy(&c->helpers[i]);
158 static bool preopen_helper_open_one(int sock_fd, char **pnamebuf,
159 size_t to_read, void *filebuf)
161 char *namebuf = *pnamebuf;
171 thistime = read(sock_fd, namebuf + nread,
172 talloc_get_size(namebuf) - nread);
179 if (nread == talloc_get_size(namebuf)) {
180 namebuf = talloc_realloc(
182 talloc_get_size(namebuf) * 2);
183 if (namebuf == NULL) {
188 } while (namebuf[nread - 1] != '\0');
190 fd = open(namebuf, O_RDONLY);
194 nread = read(fd, filebuf, to_read);
198 sys_write_v(sock_fd, &c, 1);
202 static bool preopen_helper(int fd, size_t to_read)
207 namebuf = talloc_array(NULL, char, 1024);
208 if (namebuf == NULL) {
212 readbuf = talloc_size(NULL, to_read);
213 if (readbuf == NULL) {
214 TALLOC_FREE(namebuf);
218 while (preopen_helper_open_one(fd, &namebuf, to_read, readbuf)) {
222 TALLOC_FREE(readbuf);
223 TALLOC_FREE(namebuf);
227 static NTSTATUS preopen_init_helper(struct preopen_helper *h)
232 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) == -1) {
233 status = map_nt_error_from_unix(errno);
234 DEBUG(10, ("socketpair() failed: %s\n", strerror(errno)));
241 return map_nt_error_from_unix(errno);
246 preopen_helper(fdpair[1], h->state->to_read);
251 h->fde = tevent_add_fd(global_event_context(), h->state, h->fd,
252 TEVENT_FD_READ, preopen_helper_readable, h);
253 if (h->fde == NULL) {
256 return NT_STATUS_NO_MEMORY;
262 static NTSTATUS preopen_init_helpers(TALLOC_CTX *mem_ctx, size_t to_read,
263 int num_helpers, int queue_max,
264 struct preopen_state **presult)
266 struct preopen_state *result;
269 result = talloc(mem_ctx, struct preopen_state);
270 if (result == NULL) {
271 return NT_STATUS_NO_MEMORY;
274 result->num_helpers = num_helpers;
275 result->helpers = talloc_array(result, struct preopen_helper,
277 if (result->helpers == NULL) {
279 return NT_STATUS_NO_MEMORY;
282 result->to_read = to_read;
283 result->queue_max = queue_max;
284 result->template_fname = NULL;
285 result->fnum_sent = 0;
286 result->fnum_queue_end = 0;
288 for (i=0; i<num_helpers; i++) {
289 result->helpers[i].state = result;
290 result->helpers[i].fd = -1;
293 talloc_set_destructor(result, preopen_helpers_destructor);
295 for (i=0; i<num_helpers; i++) {
296 preopen_init_helper(&result->helpers[i]);
303 static void preopen_free_helpers(void **ptr)
308 static struct preopen_state *preopen_state_get(vfs_handle_struct *handle)
310 struct preopen_state *state;
312 const char *namelist;
314 if (SMB_VFS_HANDLE_TEST_DATA(handle)) {
315 SMB_VFS_HANDLE_GET_DATA(handle, state, struct preopen_state,
320 namelist = lp_parm_const_string(SNUM(handle->conn), "preopen", "names",
323 if (namelist == NULL) {
327 status = preopen_init_helpers(
329 lp_parm_int(SNUM(handle->conn), "preopen", "num_bytes", 1),
330 lp_parm_int(SNUM(handle->conn), "preopen", "helpers", 1),
331 lp_parm_int(SNUM(handle->conn), "preopen", "queuelen", 10),
333 if (!NT_STATUS_IS_OK(status)) {
337 status = samba_path_matching_mswild_create(state,
338 true, /* case_sensitive */
340 &state->preopen_names);
341 if (!NT_STATUS_IS_OK(status)) {
346 if (!SMB_VFS_HANDLE_TEST_DATA(handle)) {
347 SMB_VFS_HANDLE_SET_DATA(handle, state, preopen_free_helpers,
348 struct preopen_state, return NULL);
354 static bool preopen_parse_fname(const char *fname, unsigned long *pnum,
355 size_t *pstart_idx, int *pnum_digits)
362 p = strrchr_m(fname, '/');
368 while (p[0] != '\0') {
369 if (isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])) {
375 /* no digits around */
379 num = smb_strtoul(p, (char **)&q, 10, &error, SMB_STR_STANDARD);
390 *pstart_idx = (p - fname);
391 *pnum_digits = (q - p);
395 static int preopen_openat(struct vfs_handle_struct *handle,
396 const struct files_struct *dirfsp,
397 const struct smb_filename *smb_fname,
398 struct files_struct *fsp,
402 const char *dirname = dirfsp->fsp_name->base_name;
403 struct preopen_state *state;
407 char *new_template = NULL;
408 size_t new_start = 0;
410 ssize_t match_idx = -1;
412 DEBUG(10, ("preopen_open called on %s\n", smb_fname_str_dbg(smb_fname)));
414 state = preopen_state_get(handle);
416 return SMB_VFS_NEXT_OPENAT(handle,
424 res = SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, flags, mode);
429 if ((flags & O_ACCMODE) != O_RDONLY) {
434 * Make sure we can later contruct an absolute pathname
436 if (dirname[0] != '/') {
440 * There's no point in preopen the directory itself.
442 if (ISDOT(smb_fname->base_name)) {
446 * If we got an absolute path in
447 * smb_fname it's most likely the
448 * reopen via /proc/self/fd/$fd
450 if (smb_fname->base_name[0] == '/') {
454 status = samba_path_matching_check_last_component(state->preopen_names,
455 smb_fname->base_name,
457 NULL, /* replace_start */
458 NULL);/* replace_end */
459 if (!NT_STATUS_IS_OK(status)) {
463 DEBUG(10, ("%s does not match the preopen:names list\n",
464 smb_fname_str_dbg(smb_fname)));
468 new_template = talloc_asprintf(
470 dirname, smb_fname->base_name);
471 if (new_template == NULL) {
475 if (!preopen_parse_fname(new_template, &num,
476 &new_start, &new_digits)) {
477 TALLOC_FREE(new_template);
481 TALLOC_FREE(state->template_fname);
482 state->template_fname = new_template;
483 state->number_start = new_start;
484 state->num_digits = new_digits;
486 if (num > state->fnum_sent) {
488 * Helpers were too slow, there's no point in reading
489 * files in helpers that we already read in the
492 state->fnum_sent = num;
495 if ((state->fnum_queue_end != 0) /* Something was started earlier */
496 && (num < (state->fnum_queue_end - state->queue_max))) {
498 * "num" is before the queue we announced. This means
499 * a new run is started.
501 state->fnum_sent = num;
504 state->fnum_queue_end = num + state->queue_max;
506 preopen_queue_run(state);
511 static struct vfs_fn_pointers vfs_preopen_fns = {
512 .openat_fn = preopen_openat,
516 NTSTATUS vfs_preopen_init(TALLOC_CTX *ctx)
520 status = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
523 if (!NT_STATUS_IS_OK(status)) {
527 vfs_preopen_debug_level = debug_add_class("preopen");
528 if (vfs_preopen_debug_level == -1) {
529 vfs_preopen_debug_level = DBGC_VFS;
530 DBG_ERR("Couldn't register custom debugging class!\n");
532 DBG_DEBUG("Debug class number of 'preopen': %d\n",
533 vfs_preopen_debug_level);