Revert "smbd: explain that/why we use the raw tevent_context for lease_timeout_handler()"
[samba.git] / source3 / modules / vfs_cacheprime.c
1 /*
2  * Copyright (c) James Peach 2005-2006
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "includes.h"
19 #include "smbd/smbd.h"
20 #include "lib/util/sys_rw.h"
21
22 /* Cache priming module.
23  *
24  * The purpose of this module is to do RAID stripe width reads to prime the
25  * buffer cache to do zero-copy I/O for subsequent sendfile calls. The idea is
26  * to do a single large read at the start of the file to make sure that most or
27  * all of the file is pulled into the buffer cache. Subsequent I/Os have
28  * reduced latency.
29  *
30  * Tunables.
31  *
32  *      cacheprime:rsize    Amount of readahead in bytes. This should be a
33  *                          multiple of the RAID stripe width.
34  *      cacheprime:debug    Debug level at which to emit messages.
35  */
36
37 #define READAHEAD_MIN       (128 * 1024)        /* min is 128 KiB */
38 #define READAHEAD_MAX       (100 * 1024 * 1024) /* max is 100 MiB */
39
40 #define MODULE "cacheprime"
41
42 static int module_debug;
43 static ssize_t g_readsz = 0;
44 static void * g_readbuf = NULL;
45
46 /* Prime the kernel buffer cache with data from the specified file. We use
47  * per-fsp data to make sure we only ever do this once. If pread is being
48  * emulated by seek/read/seek, when this will suck quite a lot.
49  */
50 static bool prime_cache(
51             struct vfs_handle_struct *  handle,
52                         files_struct *                  fsp,
53                         off_t                       offset,
54                         size_t                              count)
55 {
56         off_t * last;
57         ssize_t nread;
58
59         last = VFS_ADD_FSP_EXTENSION(handle, fsp, off_t, NULL);
60         if (!last) {
61                 return False;
62         }
63
64         if (*last == -1) {
65             /* Readahead disabled. */
66             return False;
67         }
68
69         if ((*last + g_readsz) > (offset + count)) {
70             /* Skip readahead ... we've already been here. */
71             return False;
72         }
73
74         DEBUG(module_debug,
75             ("%s: doing readahead of %lld bytes at %lld for %s\n",
76             MODULE, (long long)g_readsz, (long long)*last,
77             fsp_str_dbg(fsp)));
78
79         nread = sys_pread(fsp->fh->fd, g_readbuf, g_readsz, *last);
80         if (nread < 0) {
81             *last = -1;
82             return False;
83         }
84
85         *last += nread;
86         return True;
87 }
88
89 static int cprime_connect(
90                 struct vfs_handle_struct *  handle,
91                 const char *                service,
92                 const char *                user)
93 {
94         int ret;
95
96         module_debug = lp_parm_int(SNUM(handle->conn), MODULE, "debug", 100);
97         if (g_readbuf) {
98                 /* Only allocate g_readbuf once. If the config changes and
99                  * another client multiplexes onto this smbd, we don't want
100                  * to risk memory corruption.
101                  */
102                 return SMB_VFS_NEXT_CONNECT(handle, service, user);
103         }
104
105         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
106         if (ret < 0) {
107                 return ret;
108         }
109
110         g_readsz = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
111                                         MODULE, "rsize", NULL));
112
113         if (g_readsz < READAHEAD_MIN) {
114                 DEBUG(module_debug, ("%s: %ld bytes of readahead "
115                             "requested, using minimum of %u\n",
116                             MODULE, (long)g_readsz, READAHEAD_MIN));
117                 g_readsz = READAHEAD_MIN;
118         } else if (g_readsz > READAHEAD_MAX) {
119                 DEBUG(module_debug, ("%s: %ld bytes of readahead "
120                             "requested, using maximum of %u\n",
121                             MODULE, (long)g_readsz, READAHEAD_MAX));
122                 g_readsz = READAHEAD_MAX;
123         }
124
125         if ((g_readbuf = SMB_MALLOC(g_readsz)) == NULL) {
126                 /* Turn off readahead if we can't get a buffer. */
127                 g_readsz = 0;
128         }
129
130         return 0;
131 }
132
133 static ssize_t cprime_sendfile(
134                 struct vfs_handle_struct *  handle,
135                 int                         tofd,
136                 files_struct *              fromfsp,
137                 const DATA_BLOB *           header,
138                 off_t                   offset,
139                 size_t                      count)
140 {
141         if (g_readbuf && offset == 0) {
142                 prime_cache(handle, fromfsp, offset, count);
143         }
144
145         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fromfsp,
146                                      header, offset, count);
147 }
148
149 static ssize_t cprime_pread(
150                 vfs_handle_struct * handle,
151                 files_struct *      fsp,
152                 void *              data,
153                         size_t              count,
154                 off_t           offset)
155 {
156         if (g_readbuf) {
157                 prime_cache(handle, fsp, offset, count);
158         }
159
160         return SMB_VFS_NEXT_PREAD(handle, fsp, data, count, offset);
161 }
162
163 static struct vfs_fn_pointers vfs_cacheprime_fns = {
164     .sendfile_fn = cprime_sendfile,
165     .pread_fn = cprime_pread,
166     .connect_fn = cprime_connect,
167 };
168
169 /* -------------------------------------------------------------------------
170  * Samba module initialisation entry point.
171  * -------------------------------------------------------------------------
172  */
173
174 static_decl_vfs;
175 NTSTATUS vfs_cacheprime_init(TALLOC_CTX *ctx)
176 {
177         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE,
178                                 &vfs_cacheprime_fns);
179 }
180
181 /* vim: set sw=4 ts=4 tw=79 et: */