2 * Copyright (c) Jeremy Allison 2007.
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 2 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #if !defined(HAVE_LINUX_READAHEAD) && !defined(HAVE_POSIX_FADVISE)
26 * This module copes with Vista AIO read requests on Linux
27 * by detecting the initial 0x80000 boundary reads and causing
28 * the buffer cache to be filled in advance.
31 static unsigned long get_offset_boundary(struct vfs_handle_struct *handle)
33 SMB_OFF_T off_bound = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
40 return (unsigned long)off_bound;
43 static unsigned long get_offset_length(struct vfs_handle_struct *handle, unsigned long def_val)
45 SMB_OFF_T len = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
52 return (unsigned long)len;
55 static ssize_t readahead_sendfile(struct vfs_handle_struct *handle,
59 const DATA_BLOB *header,
63 unsigned long off_bound = get_offset_boundary(handle);
64 if ( offset % off_bound == 0) {
65 unsigned long len = get_offset_length(handle, off_bound);
66 #if defined(HAVE_LINUX_READAHEAD)
67 int err = readahead(fromfd, offset, (size_t)len);
68 DEBUG(10,("readahead_sendfile: readahead on fd %u, offset %llu, len %u returned %d\n",
70 (unsigned long long)offset,
73 #elif defined(HAVE_POSIX_FADVISE)
74 int err = posix_fadvise(fromfd, offset, (off_t)len, POSIX_FADV_WILLNEED);
75 DEBUG(10,("readahead_sendfile: posix_fadvise on fd %u, offset %llu, len %u returned %d\n",
77 (unsigned long long)offset,
82 DEBUG(0,("readahead_sendfile: no readahead on this platform\n"));
87 return SMB_VFS_NEXT_SENDFILE(handle,
96 static ssize_t readahead_pread(vfs_handle_struct *handle,
103 unsigned long off_bound = get_offset_boundary(handle);
104 if ( offset % off_bound == 0) {
105 unsigned long len = get_offset_length(handle, off_bound);
106 #if defined(HAVE_LINUX_READAHEAD)
107 int err = readahead(fd, offset, (size_t)len);
108 DEBUG(10,("readahead_pread: readahead on fd %u, offset %llu, len %u returned %d\n",
110 (unsigned long long)offset,
113 #elif defined(HAVE_POSIX_FADVISE)
114 int err = posix_fadvise(fromfd, offset, (off_t)len, POSIX_FADV_WILLNEED);
115 DEBUG(10,("readahead_pread: posix_fadvise on fd %u, offset %llu, len %u returned %d\n",
117 (unsigned long long)offset,
122 DEBUG(0,("readahead_pread: no readahead on this platform\n"));
127 return SMB_VFS_NEXT_PREAD(handle, fsp, fd, data, count, offset);
130 static vfs_op_tuple readahead_ops [] =
132 {SMB_VFS_OP(readahead_sendfile), SMB_VFS_OP_SENDFILE, SMB_VFS_LAYER_TRANSPARENT},
133 {SMB_VFS_OP(readahead_pread), SMB_VFS_OP_PREAD, SMB_VFS_LAYER_TRANSPARENT},
134 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
137 NTSTATUS vfs_readahead_init(void);
138 NTSTATUS vfs_readahead_init(void)
140 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "readahead", readahead_ops);