1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/*
* Copyright (c) Jeremy Allison 2007.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#if !defined(HAVE_LINUX_READAHEAD) && !defined(HAVE_POSIX_FADVISE)
static BOOL didmsg;
#endif
struct readahead_data {
SMB_OFF_T off_bound;
SMB_OFF_T len;
BOOL didmsg;
};
/*
* This module copes with Vista AIO read requests on Linux
* by detecting the initial 0x80000 boundary reads and causing
* the buffer cache to be filled in advance.
*/
/*******************************************************************
sendfile wrapper that does readahead/posix_fadvise.
*******************************************************************/
static ssize_t readahead_sendfile(struct vfs_handle_struct *handle,
int tofd,
files_struct *fsp,
int fromfd,
const DATA_BLOB *header,
SMB_OFF_T offset,
size_t count)
{
struct readahead_data *rhd = (struct readahead_data *)handle->data;
if ( offset % rhd->off_bound == 0) {
#if defined(HAVE_LINUX_READAHEAD)
int err = readahead(fromfd, offset, (size_t)rhd->len);
DEBUG(10,("readahead_sendfile: readahead on fd %u, offset %llu, len %u returned %d\n",
(unsigned int)fromfd,
(unsigned long long)offset,
(unsigned int)rhd->len,
err ));
#elif defined(HAVE_POSIX_FADVISE)
int err = posix_fadvise(fromfd, offset, (off_t)rhd->len, POSIX_FADV_WILLNEED);
DEBUG(10,("readahead_sendfile: posix_fadvise on fd %u, offset %llu, len %u returned %d\n",
(unsigned int)fromfd,
(unsigned long long)offset,
(unsigned int)rhd->len,
err ));
#else
if (!rhd->didmsg) {
DEBUG(0,("readahead_sendfile: no readahead on this platform\n"));
rhd->didmsg = True;
}
#endif
}
return SMB_VFS_NEXT_SENDFILE(handle,
tofd,
fsp,
fromfd,
header,
offset,
count);
}
/*******************************************************************
pread wrapper that does readahead/posix_fadvise.
*******************************************************************/
static ssize_t readahead_pread(vfs_handle_struct *handle,
files_struct *fsp,
int fd,
void *data,
size_t count,
SMB_OFF_T offset)
{
struct readahead_data *rhd = (struct readahead_data *)handle->data;
if ( offset % rhd->off_bound == 0) {
#if defined(HAVE_LINUX_READAHEAD)
int err = readahead(fd, offset, (size_t)rhd->len);
DEBUG(10,("readahead_pread: readahead on fd %u, offset %llu, len %u returned %d\n",
(unsigned int)fd,
(unsigned long long)offset,
(unsigned int)rhd->len,
err ));
#elif defined(HAVE_POSIX_FADVISE)
int err = posix_fadvise(fromfd, offset, (off_t)rhd->len, POSIX_FADV_WILLNEED);
DEBUG(10,("readahead_pread: posix_fadvise on fd %u, offset %llu, len %u returned %d\n",
(unsigned int)fd,
(unsigned long long)offset,
(unsigned int)rhd->len,
(err ));
#else
if (!rhd->didmsg) {
DEBUG(0,("readahead_pread: no readahead on this platform\n"));
rhd->didmsg = True;
}
#endif
}
return SMB_VFS_NEXT_PREAD(handle, fsp, fd, data, count, offset);
}
/*******************************************************************
Directly called from main smbd when freeing handle.
*******************************************************************/
static void free_readahead_data(void **pptr)
{
SAFE_FREE(*pptr);
}
/*******************************************************************
Allocate the handle specific data so we don't call the expensive
conv_str_size function for each sendfile/pread.
*******************************************************************/
static int readahead_connect(struct vfs_handle_struct *handle,
const char *service,
const char *user)
{
struct readahead_data *rhd = SMB_MALLOC_P(struct readahead_data);
if (!rhd) {
DEBUG(0,("readahead_connect: out of memory\n"));
return -1;
}
ZERO_STRUCTP(rhd);
rhd->didmsg = False;
rhd->off_bound = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
"readahead",
"offset",
NULL));
if (rhd->off_bound == 0) {
rhd->off_bound = 0x80000;
}
rhd->len = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
"readahead",
"length",
NULL));
if (rhd->len == 0) {
rhd->len = rhd->off_bound;
}
handle->data = (void *)rhd;
handle->free_data = free_readahead_data;
return 0;
}
/*******************************************************************
Functions we're replacing.
We don't replace read as it isn't used from smbd to read file
data.
*******************************************************************/
static vfs_op_tuple readahead_ops [] =
{
{SMB_VFS_OP(readahead_sendfile), SMB_VFS_OP_SENDFILE, SMB_VFS_LAYER_TRANSPARENT},
{SMB_VFS_OP(readahead_pread), SMB_VFS_OP_PREAD, SMB_VFS_LAYER_TRANSPARENT},
{SMB_VFS_OP(readahead_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_TRANSPARENT},
{SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
};
/*******************************************************************
Module initialization boilerplate.
*******************************************************************/
NTSTATUS vfs_readahead_init(void);
NTSTATUS vfs_readahead_init(void)
{
return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "readahead", readahead_ops);
}
|