selftest/Samba4: make use of get_cmd_env_vars() to setup all relevant env variables
[samba.git] / source3 / modules / vfs_readahead.c
index 5b08f004c44d89953bb6e1cf0c9e01449e68840d..bb31b578b95c8ec878d00036bf043e6dfa9e89d6 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * 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.
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "smbd/smbd.h"
 
-#if !defined(HAVE_LINUX_READAHEAD) && !defined(HAVE_POSIX_FADVISE)
-static BOOL didmsg;
+#if defined(HAVE_LINUX_READAHEAD) && ! defined(HAVE_READAHEAD_DECL)
+ssize_t readahead(int fd, off_t offset, size_t count);
 #endif
 
+struct readahead_data {
+       off_t off_bound;
+       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,
+                                       files_struct *fromfsp,
                                        const DATA_BLOB *header,
-                                       SMB_OFF_T offset,
+                                       off_t offset,
                                        size_t count)
 {
-       unsigned long off_bound = lp_parm_ulong(SNUM(handle->conn), "readahead", "offset", 0x80000);
-       if ( offset % off_bound == 0) {
-               unsigned long len = lp_parm_ulong(SNUM(handle->conn), "readahead", "length", off_bound);
+       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)len);
+               int err = readahead(fsp_get_io_fd(fromfsp), offset, (size_t)rhd->len);
                DEBUG(10,("readahead_sendfile: readahead on fd %u, offset %llu, len %u returned %d\n",
-                       (unsigned int)fromfd,
+                       (unsigned int)fsp_get_io_fd(fromfsp),
                        (unsigned long long)offset,
-                       (unsigned int)len,
+                       (unsigned int)rhd->len,
                        err ));
 #elif defined(HAVE_POSIX_FADVISE)
-               int err = posix_fadvise(fromfd, offset, (off_t)len, POSIX_FADV_WILLNEED);
+               int err = posix_fadvise(fsp_get_io_fd(fromfsp), 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 int)fsp_get_io_fd(fromfsp),
                        (unsigned long long)offset,
-                       (unsigned int)len,
+                       (unsigned int)rhd->len,
                        err ));
 #else
-               if (!didmsg) {
+               if (!rhd->didmsg) {
                        DEBUG(0,("readahead_sendfile: no readahead on this platform\n"));
-                       didmsg = True;
+                       rhd->didmsg = True;
                }
 #endif
        }
        return SMB_VFS_NEXT_SENDFILE(handle,
                                        tofd,
-                                       fsp,
-                                       fromfd,
+                                       fromfsp,
                                        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)
+                               off_t offset)
 {
-       unsigned long off_bound = lp_parm_ulong(SNUM(handle->conn), "readahead", "offset", 0x80000);
-       if ( offset % off_bound == 0) {
-               unsigned long len = lp_parm_ulong(SNUM(handle->conn), "readahead", "length", off_bound);
+       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)len);
+               int err = readahead(fsp_get_io_fd(fsp), offset, (size_t)rhd->len);
                DEBUG(10,("readahead_pread: readahead on fd %u, offset %llu, len %u returned %d\n",
-                       (unsigned int)fd,
+                       (unsigned int)fsp_get_io_fd(fsp),
                        (unsigned long long)offset,
-                       (unsigned int)len,
+                       (unsigned int)rhd->len,
                        err ));
 #elif defined(HAVE_POSIX_FADVISE)
-               int err = posix_fadvise(fromfd, offset, (off_t)len, POSIX_FADV_WILLNEED);
+               int err = posix_fadvise(fsp_get_io_fd(fsp), 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 int)fsp_get_io_fd(fsp),
                        (unsigned long long)offset,
-                       (unsigned int)len,
-                       (err ));
+                       (unsigned int)rhd->len,
+                       err ));
 #else
-               if (!didmsg) {
+               if (!rhd->didmsg) {
                        DEBUG(0,("readahead_pread: no readahead on this platform\n"));
-                       didmsg = True;
+                       rhd->didmsg = True;
                }
 #endif
         }
-        return SMB_VFS_NEXT_PREAD(handle, fsp, fd, data, count, offset);
+        return SMB_VFS_NEXT_PREAD(handle, fsp, data, count, offset);
+}
+
+/*******************************************************************
+ Directly called from main smbd when freeing handle.
+*******************************************************************/
+
+static void free_readahead_data(void **pptr)
+{
+       SAFE_FREE(*pptr);
 }
 
-static vfs_op_tuple readahead_ops [] =
+/*******************************************************************
+ 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)
 {
-       {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(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
+       struct readahead_data *rhd;
+       int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
+
+       if (ret < 0) {
+               return ret;
+       }
+       rhd = SMB_MALLOC_P(struct readahead_data);
+       if (!rhd) {
+               SMB_VFS_NEXT_DISCONNECT(handle);
+               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;
+}
+
+static struct vfs_fn_pointers vfs_readahead_fns = {
+       .sendfile_fn = readahead_sendfile,
+       .pread_fn = readahead_pread,
+       .connect_fn = readahead_connect
 };
 
-NTSTATUS vfs_readahead_init(void);
-NTSTATUS vfs_readahead_init(void)
+/*******************************************************************
+ Module initialization boilerplate.
+*******************************************************************/
+
+static_decl_vfs;
+NTSTATUS vfs_readahead_init(TALLOC_CTX *ctx)
 {
-       return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "readahead", readahead_ops);
+       return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "readahead",
+                               &vfs_readahead_fns);
 }