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