s4-dsdb/objectclass: remove duplicated declaration for objectclass_do_add
[Samba.git] / source3 / modules / vfs_cacheprime.c
blobe90e09a17faa63405e5cec9363f6a17b55746b2e
1 /*
2 * Copyright (c) James Peach 2005-2006
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.
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.
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/>.
18 #include "includes.h"
19 #include "smbd/smbd.h"
20 #include "lib/sys_rw.h"
22 /* Cache priming module.
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.
30 * Tunables.
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.
37 #define READAHEAD_MIN (128 * 1024) /* min is 128 KiB */
38 #define READAHEAD_MAX (100 * 1024 * 1024) /* max is 100 MiB */
40 #define MODULE "cacheprime"
42 static int module_debug;
43 static ssize_t g_readsz = 0;
44 static void * g_readbuf = NULL;
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.
50 static bool prime_cache(
51 struct vfs_handle_struct * handle,
52 files_struct * fsp,
53 off_t offset,
54 size_t count)
56 off_t * last;
57 ssize_t nread;
59 last = (off_t *)VFS_ADD_FSP_EXTENSION(handle, fsp, off_t, NULL);
60 if (!last) {
61 return False;
64 if (*last == -1) {
65 /* Readahead disabled. */
66 return False;
69 if ((*last + g_readsz) > (offset + count)) {
70 /* Skip readahead ... we've already been here. */
71 return False;
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)));
79 nread = sys_pread(fsp->fh->fd, g_readbuf, g_readsz, *last);
80 if (nread < 0) {
81 *last = -1;
82 return False;
85 *last += nread;
86 return True;
89 static int cprime_connect(
90 struct vfs_handle_struct * handle,
91 const char * service,
92 const char * user)
94 int ret;
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.
102 return SMB_VFS_NEXT_CONNECT(handle, service, user);
105 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
106 if (ret < 0) {
107 return ret;
110 g_readsz = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
111 MODULE, "rsize", NULL));
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;
125 if ((g_readbuf = SMB_MALLOC(g_readsz)) == NULL) {
126 /* Turn off readahead if we can't get a buffer. */
127 g_readsz = 0;
130 return 0;
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)
141 if (g_readbuf && offset == 0) {
142 prime_cache(handle, fromfsp, offset, count);
145 return SMB_VFS_NEXT_SENDFILE(handle, tofd, fromfsp,
146 header, offset, count);
149 static ssize_t cprime_read(
150 vfs_handle_struct * handle,
151 files_struct * fsp,
152 void * data,
153 size_t count)
155 off_t offset;
157 offset = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
158 if (offset >= 0 && g_readbuf) {
159 prime_cache(handle, fsp, offset, count);
160 SMB_VFS_LSEEK(fsp, offset, SEEK_SET);
163 return SMB_VFS_NEXT_READ(handle, fsp, data, count);
166 static ssize_t cprime_pread(
167 vfs_handle_struct * handle,
168 files_struct * fsp,
169 void * data,
170 size_t count,
171 off_t offset)
173 if (g_readbuf) {
174 prime_cache(handle, fsp, offset, count);
177 return SMB_VFS_NEXT_PREAD(handle, fsp, data, count, offset);
180 static struct vfs_fn_pointers vfs_cacheprime_fns = {
181 .sendfile_fn = cprime_sendfile,
182 .pread_fn = cprime_pread,
183 .read_fn = cprime_read,
184 .connect_fn = cprime_connect,
187 /* -------------------------------------------------------------------------
188 * Samba module initialisation entry point.
189 * -------------------------------------------------------------------------
192 NTSTATUS vfs_cacheprime_init(void);
193 NTSTATUS vfs_cacheprime_init(void)
195 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE,
196 &vfs_cacheprime_fns);
199 /* vim: set sw=4 ts=4 tw=79 et: */