Remove unused variable 'didmsg'.
[samba.git] / source3 / modules / vfs_readahead.c
1 /*
2  * Copyright (c) Jeremy Allison 2007.
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 struct readahead_data {
21         SMB_OFF_T off_bound;
22         SMB_OFF_T len;
23         bool didmsg;
24 };
25
26 /* 
27  * This module copes with Vista AIO read requests on Linux
28  * by detecting the initial 0x80000 boundary reads and causing
29  * the buffer cache to be filled in advance.
30  */
31
32 /*******************************************************************
33  sendfile wrapper that does readahead/posix_fadvise.
34 *******************************************************************/
35
36 static ssize_t readahead_sendfile(struct vfs_handle_struct *handle,
37                                         int tofd,
38                                         files_struct *fsp,
39                                         int fromfd,
40                                         const DATA_BLOB *header,
41                                         SMB_OFF_T offset,
42                                         size_t count)
43 {
44         struct readahead_data *rhd = (struct readahead_data *)handle->data;
45
46         if ( offset % rhd->off_bound == 0) {
47 #if defined(HAVE_LINUX_READAHEAD)
48                 int err = readahead(fromfd, offset, (size_t)rhd->len);
49                 DEBUG(10,("readahead_sendfile: readahead on fd %u, offset %llu, len %u returned %d\n",
50                         (unsigned int)fromfd,
51                         (unsigned long long)offset,
52                         (unsigned int)rhd->len,
53                         err ));
54 #elif defined(HAVE_POSIX_FADVISE)
55                 int err = posix_fadvise(fromfd, offset, (off_t)rhd->len, POSIX_FADV_WILLNEED);
56                 DEBUG(10,("readahead_sendfile: posix_fadvise on fd %u, offset %llu, len %u returned %d\n",
57                         (unsigned int)fromfd,
58                         (unsigned long long)offset,
59                         (unsigned int)rhd->len,
60                         err ));
61 #else
62                 if (!rhd->didmsg) {
63                         DEBUG(0,("readahead_sendfile: no readahead on this platform\n"));
64                         rhd->didmsg = True;
65                 }
66 #endif
67         }
68         return SMB_VFS_NEXT_SENDFILE(handle,
69                                         tofd,
70                                         fsp,
71                                         fromfd,
72                                         header,
73                                         offset,
74                                         count);
75 }
76
77 /*******************************************************************
78  pread wrapper that does readahead/posix_fadvise.
79 *******************************************************************/
80
81 static ssize_t readahead_pread(vfs_handle_struct *handle,
82                                 files_struct *fsp,
83                                 int fd,
84                                 void *data,
85                                 size_t count,
86                                 SMB_OFF_T offset)
87 {
88         struct readahead_data *rhd = (struct readahead_data *)handle->data;
89
90         if ( offset % rhd->off_bound == 0) {
91 #if defined(HAVE_LINUX_READAHEAD)
92                 int err = readahead(fd, offset, (size_t)rhd->len);
93                 DEBUG(10,("readahead_pread: readahead on fd %u, offset %llu, len %u returned %d\n",
94                         (unsigned int)fd,
95                         (unsigned long long)offset,
96                         (unsigned int)rhd->len,
97                         err ));
98 #elif defined(HAVE_POSIX_FADVISE)
99                 int err = posix_fadvise(fd, offset, (off_t)rhd->len, POSIX_FADV_WILLNEED);
100                 DEBUG(10,("readahead_pread: posix_fadvise on fd %u, offset %llu, len %u returned %d\n",
101                         (unsigned int)fd,
102                         (unsigned long long)offset,
103                         (unsigned int)rhd->len,
104                         err ));
105 #else
106                 if (!rhd->didmsg) {
107                         DEBUG(0,("readahead_pread: no readahead on this platform\n"));
108                         rhd->didmsg = True;
109                 }
110 #endif
111         }
112         return SMB_VFS_NEXT_PREAD(handle, fsp, fd, data, count, offset);
113 }
114
115 /*******************************************************************
116  Directly called from main smbd when freeing handle.
117 *******************************************************************/
118
119 static void free_readahead_data(void **pptr)
120 {
121         SAFE_FREE(*pptr);
122 }
123
124 /*******************************************************************
125  Allocate the handle specific data so we don't call the expensive
126  conv_str_size function for each sendfile/pread.
127 *******************************************************************/
128
129 static int readahead_connect(struct vfs_handle_struct *handle,
130                                 const char *service,
131                                 const char *user)
132 {
133         struct readahead_data *rhd = SMB_MALLOC_P(struct readahead_data);
134         if (!rhd) {
135                 DEBUG(0,("readahead_connect: out of memory\n"));
136                 return -1;
137         }
138         ZERO_STRUCTP(rhd);
139
140         rhd->didmsg = False;
141         rhd->off_bound = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
142                                                 "readahead",
143                                                 "offset",
144                                                 NULL));
145         if (rhd->off_bound == 0) {
146                 rhd->off_bound = 0x80000;
147         }
148         rhd->len = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
149                                                 "readahead",
150                                                 "length",
151                                                 NULL));
152         if (rhd->len == 0) {
153                 rhd->len = rhd->off_bound;
154         }
155
156         handle->data = (void *)rhd;
157         handle->free_data = free_readahead_data;
158         return SMB_VFS_NEXT_CONNECT(handle, service, user);
159 }
160
161 /*******************************************************************
162  Functions we're replacing.
163  We don't replace read as it isn't used from smbd to read file
164  data.
165 *******************************************************************/
166
167 static vfs_op_tuple readahead_ops [] =
168 {
169         {SMB_VFS_OP(readahead_sendfile), SMB_VFS_OP_SENDFILE, SMB_VFS_LAYER_TRANSPARENT},
170         {SMB_VFS_OP(readahead_pread), SMB_VFS_OP_PREAD, SMB_VFS_LAYER_TRANSPARENT},
171         {SMB_VFS_OP(readahead_connect), SMB_VFS_OP_CONNECT,  SMB_VFS_LAYER_TRANSPARENT},
172         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
173 };
174
175 /*******************************************************************
176  Module initialization boilerplate.
177 *******************************************************************/
178
179 NTSTATUS vfs_readahead_init(void);
180 NTSTATUS vfs_readahead_init(void)
181 {
182         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "readahead", readahead_ops);
183 }