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