| 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 | SMB_OFF_T                           offset, | 
|---|
| 52 | size_t                              count) | 
|---|
| 53 | { | 
|---|
| 54 | SMB_OFF_T * last; | 
|---|
| 55 | ssize_t nread; | 
|---|
| 56 |  | 
|---|
| 57 | last = (SMB_OFF_T *)VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T, NULL); | 
|---|
| 58 | if (!last) { | 
|---|
| 59 | return False; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | if (*last == -1) { | 
|---|
| 63 | /* Readahead disabled. */ | 
|---|
| 64 | return False; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | if ((*last + g_readsz) > (offset + count)) { | 
|---|
| 68 | /* Skip readahead ... we've already been here. */ | 
|---|
| 69 | return False; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | DEBUG(module_debug, | 
|---|
| 73 | ("%s: doing readahead of %lld bytes at %lld for %s\n", | 
|---|
| 74 | MODULE, (long long)g_readsz, (long long)*last, | 
|---|
| 75 | fsp_str_dbg(fsp))); | 
|---|
| 76 |  | 
|---|
| 77 | nread = sys_pread(fsp->fh->fd, g_readbuf, g_readsz, *last); | 
|---|
| 78 | if (nread < 0) { | 
|---|
| 79 | *last = -1; | 
|---|
| 80 | return False; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | *last += nread; | 
|---|
| 84 | return True; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | static int cprime_connect( | 
|---|
| 88 | struct vfs_handle_struct *  handle, | 
|---|
| 89 | const char *                service, | 
|---|
| 90 | const char *                user) | 
|---|
| 91 | { | 
|---|
| 92 | int ret; | 
|---|
| 93 |  | 
|---|
| 94 | module_debug = lp_parm_int(SNUM(handle->conn), MODULE, "debug", 100); | 
|---|
| 95 | if (g_readbuf) { | 
|---|
| 96 | /* Only allocate g_readbuf once. If the config changes and | 
|---|
| 97 | * another client multiplexes onto this smbd, we don't want | 
|---|
| 98 | * to risk memory corruption. | 
|---|
| 99 | */ | 
|---|
| 100 | return SMB_VFS_NEXT_CONNECT(handle, service, user); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | ret = SMB_VFS_NEXT_CONNECT(handle, service, user); | 
|---|
| 104 | if (ret < 0) { | 
|---|
| 105 | return ret; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | g_readsz = conv_str_size(lp_parm_const_string(SNUM(handle->conn), | 
|---|
| 109 | MODULE, "rsize", NULL)); | 
|---|
| 110 |  | 
|---|
| 111 | if (g_readsz < READAHEAD_MIN) { | 
|---|
| 112 | DEBUG(module_debug, ("%s: %ld bytes of readahead " | 
|---|
| 113 | "requested, using minimum of %u\n", | 
|---|
| 114 | MODULE, (long)g_readsz, READAHEAD_MIN)); | 
|---|
| 115 | g_readsz = READAHEAD_MIN; | 
|---|
| 116 | } else if (g_readsz > READAHEAD_MAX) { | 
|---|
| 117 | DEBUG(module_debug, ("%s: %ld bytes of readahead " | 
|---|
| 118 | "requested, using maximum of %u\n", | 
|---|
| 119 | MODULE, (long)g_readsz, READAHEAD_MAX)); | 
|---|
| 120 | g_readsz = READAHEAD_MAX; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | if ((g_readbuf = SMB_MALLOC(g_readsz)) == NULL) { | 
|---|
| 124 | /* Turn off readahead if we can't get a buffer. */ | 
|---|
| 125 | g_readsz = 0; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | return 0; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | static ssize_t cprime_sendfile( | 
|---|
| 132 | struct vfs_handle_struct *  handle, | 
|---|
| 133 | int                         tofd, | 
|---|
| 134 | files_struct *              fromfsp, | 
|---|
| 135 | const DATA_BLOB *           header, | 
|---|
| 136 | SMB_OFF_T                   offset, | 
|---|
| 137 | size_t                      count) | 
|---|
| 138 | { | 
|---|
| 139 | if (g_readbuf && offset == 0) { | 
|---|
| 140 | prime_cache(handle, fromfsp, offset, count); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | return SMB_VFS_NEXT_SENDFILE(handle, tofd, fromfsp, | 
|---|
| 144 | header, offset, count); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | static ssize_t cprime_read( | 
|---|
| 148 | vfs_handle_struct * handle, | 
|---|
| 149 | files_struct *      fsp, | 
|---|
| 150 | void *              data, | 
|---|
| 151 | size_t              count) | 
|---|
| 152 | { | 
|---|
| 153 | SMB_OFF_T offset; | 
|---|
| 154 |  | 
|---|
| 155 | offset = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR); | 
|---|
| 156 | if (offset >= 0 && g_readbuf)  { | 
|---|
| 157 | prime_cache(handle, fsp, offset, count); | 
|---|
| 158 | SMB_VFS_LSEEK(fsp, offset, SEEK_SET); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | return SMB_VFS_NEXT_READ(handle, fsp, data, count); | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | static ssize_t cprime_pread( | 
|---|
| 165 | vfs_handle_struct * handle, | 
|---|
| 166 | files_struct *      fsp, | 
|---|
| 167 | void *              data, | 
|---|
| 168 | size_t              count, | 
|---|
| 169 | SMB_OFF_T           offset) | 
|---|
| 170 | { | 
|---|
| 171 | if (g_readbuf) { | 
|---|
| 172 | prime_cache(handle, fsp, offset, count); | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | return SMB_VFS_NEXT_PREAD(handle, fsp, data, count, offset); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | static struct vfs_fn_pointers vfs_cacheprime_fns = { | 
|---|
| 179 | .sendfile = cprime_sendfile, | 
|---|
| 180 | .pread = cprime_pread, | 
|---|
| 181 | .vfs_read = cprime_read, | 
|---|
| 182 | .connect_fn = cprime_connect, | 
|---|
| 183 | }; | 
|---|
| 184 |  | 
|---|
| 185 | /* ------------------------------------------------------------------------- | 
|---|
| 186 | * Samba module initialisation entry point. | 
|---|
| 187 | * ------------------------------------------------------------------------- | 
|---|
| 188 | */ | 
|---|
| 189 |  | 
|---|
| 190 | NTSTATUS vfs_cacheprime_init(void); | 
|---|
| 191 | NTSTATUS vfs_cacheprime_init(void) | 
|---|
| 192 | { | 
|---|
| 193 | return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE, | 
|---|
| 194 | &vfs_cacheprime_fns); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | /* vim: set sw=4 ts=4 tw=79 et: */ | 
|---|