r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[tprouty/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
20 /* Cache priming module.
21  *
22  * The purpose of this module is to do RAID stripe width reads to prime the
23  * buffer cache to do zero-copy I/O for subsequent sendfile calls. The idea is
24  * to do a single large read at the start of the file to make sure that most or
25  * all of the file is pulled into the buffer cache. Subsequent I/Os have
26  * reduced latency.
27  *
28  * Tunables.
29  *
30  *      cacheprime:rsize    Amount of readahead in bytes. This should be a
31  *                          multiple of the RAID stripe width.
32  *      cacheprime:debug    Debug level at which to emit messages.
33  */
34
35 #define READAHEAD_MIN       (128 * 1024)        /* min is 128 KiB */
36 #define READAHEAD_MAX       (100 * 1024 * 1024) /* max is 100 MiB */
37
38 #define MODULE "cacheprime"
39
40 static int module_debug;
41 static ssize_t g_readsz = 0;
42 static void * g_readbuf = NULL;
43
44 /* Prime the kernel buffer cache with data from the specified file. We use
45  * per-fsp data to make sure we only ever do this once. If pread is being
46  * emulated by seek/read/seek, when this will suck quite a lot.
47  */
48 static BOOL prime_cache(
49             struct vfs_handle_struct *  handle,
50                         files_struct *                  fsp,
51                         int                                     fd,
52                         SMB_OFF_T                           offset,
53                         size_t                              count)
54 {
55         SMB_OFF_T * last;
56         ssize_t nread;
57
58         last = VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T);
59         if (!last) {
60                 return False;
61         }
62
63         if (*last == -1) {
64             /* Readahead disabled. */
65             return False;
66         }
67
68         if ((*last + g_readsz) > (offset + count)) {
69             /* Skip readahead ... we've already been here. */
70             return False;
71         }
72
73         DEBUG(module_debug,
74             ("%s: doing readahead of %lld bytes at %lld for %s\n",
75             MODULE, (long long)g_readsz, (long long)*last,
76             fsp->fsp_name));
77
78         nread = sys_pread(fd, g_readbuf, g_readsz, *last);
79         if (nread < 0) {
80             *last = -1;
81             return False;
82         }
83
84         *last += nread;
85         return True;
86 }
87
88 static int cprime_connect(
89                 struct vfs_handle_struct *  handle,
90                 const char *                service,
91                 const char *                user)
92 {
93         module_debug = lp_parm_int(SNUM(handle->conn), MODULE, "debug", 100);
94         if (g_readbuf) {
95                 /* Only allocate g_readbuf once. If the config changes and
96                  * another client multiplexes onto this smbd, we don't want
97                  * to risk memory corruption.
98                  */
99                 return SMB_VFS_NEXT_CONNECT(handle, service, user);
100         }
101
102         g_readsz = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
103                                         MODULE, "rsize", NULL));
104
105         if (g_readsz < READAHEAD_MIN) {
106                 DEBUG(module_debug, ("%s: %ld bytes of readahead "
107                             "requested, using minimum of %u\n",
108                             MODULE, (long)g_readsz, READAHEAD_MIN));
109                 g_readsz = READAHEAD_MIN;
110         } else if (g_readsz > READAHEAD_MAX) {
111                 DEBUG(module_debug, ("%s: %ld bytes of readahead "
112                             "requested, using maximum of %u\n",
113                             MODULE, (long)g_readsz, READAHEAD_MAX));
114                 g_readsz = READAHEAD_MAX;
115         }
116
117         if ((g_readbuf = SMB_MALLOC(g_readsz)) == NULL) {
118                 /* Turn off readahead if we can't get a buffer. */
119                 g_readsz = 0;
120         }
121
122         return SMB_VFS_NEXT_CONNECT(handle, service, user);
123 }
124
125 static ssize_t cprime_sendfile(
126                 struct vfs_handle_struct *  handle,
127                 int                         tofd,
128                 files_struct *              fsp,
129                 int                         fromfd,
130                 const DATA_BLOB *           header,
131                 SMB_OFF_T                   offset,
132                 size_t                      count)
133 {
134         if (g_readbuf && offset == 0) {
135                 prime_cache(handle, fsp, fromfd, offset, count);
136         }
137
138         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, fromfd,
139                                      header, offset, count);
140 }
141
142 static ssize_t cprime_read(
143                 vfs_handle_struct * handle,
144                 files_struct *      fsp,
145                 int                 fd,
146                 void *              data,
147                 size_t              count)
148 {
149         SMB_OFF_T offset;
150
151         offset = SMB_VFS_LSEEK(fsp, fd, 0, SEEK_CUR);
152         if (offset >= 0 && g_readbuf)  {
153                 prime_cache(handle, fsp, fd, offset, count);
154                 SMB_VFS_LSEEK(fsp, fd, offset, SEEK_SET);
155         }
156
157         return SMB_VFS_NEXT_READ(handle, fsp, fd, data, count);
158 }
159
160 static ssize_t cprime_pread(
161                 vfs_handle_struct * handle,
162                 files_struct *      fsp,
163                 int                 fd,
164                 void *              data,
165                         size_t              count,
166                 SMB_OFF_T           offset)
167 {
168         if (g_readbuf) {
169                 prime_cache(handle, fsp, fd, offset, count);
170         }
171
172         return SMB_VFS_NEXT_PREAD(handle, fsp, fd, data, count, offset);
173 }
174
175 static vfs_op_tuple cprime_ops [] =
176 {
177         {SMB_VFS_OP(cprime_sendfile),
178                 SMB_VFS_OP_SENDFILE, SMB_VFS_LAYER_TRANSPARENT},
179         {SMB_VFS_OP(cprime_pread),
180                 SMB_VFS_OP_PREAD, SMB_VFS_LAYER_TRANSPARENT},
181         {SMB_VFS_OP(cprime_read),
182                 SMB_VFS_OP_READ, SMB_VFS_LAYER_TRANSPARENT},
183         {SMB_VFS_OP(cprime_connect),
184                 SMB_VFS_OP_CONNECT,  SMB_VFS_LAYER_TRANSPARENT},
185
186         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
187 };
188
189 /* -------------------------------------------------------------------------
190  * Samba module initialisation entry point.
191  * -------------------------------------------------------------------------
192  */
193
194 NTSTATUS vfs_cacheprime_init(void);
195 NTSTATUS vfs_cacheprime_init(void)
196 {
197     return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE, cprime_ops);
198 }
199
200 /* vim: set sw=4 ts=4 tw=79 et: */