Add a missing include file to two VFS modules
[samba.git] / source3 / modules / vfs_prealloc.c
1 /*
2  * XFS preallocation support module.
3  *
4  * Copyright (c) James Peach 2006
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24
25 /* Extent preallocation module.
26  *
27  * The purpose of this module is to preallocate space on the filesystem when
28  * we have a good idea of how large files are supposed to be. This lets writes
29  * proceed without having to allocate new extents and results in better file
30  * layouts on disk.
31  *
32  * Currently only implemented for XFS. This module is based on an original idea
33  * and implementation by Sebastian Brings.
34  *
35  * Tunables.
36  *
37  *      prealloc: <ext>     Number of bytes to preallocate for a file with
38  *                          the matching extension.
39  *      prealloc:debug      Debug level at which to emit messages.
40  *
41  * Example.
42  *
43  *      prealloc:mpeg = 500M  # Preallocate *.mpeg to 500 MiB.
44  */
45
46 #ifdef HAVE_XFS_LIBXFS_H
47 #include <xfs/libxfs.h>
48 #define lock_type xfs_flock64_t
49 #else
50 #define lock_type struct flock64
51 #endif
52
53 #ifdef HAVE_GPFS
54 #include "gpfs_gpl.h"
55 #endif
56
57 #define MODULE "prealloc"
58 static int module_debug;
59
60 static int preallocate_space(int fd, SMB_OFF_T size)
61 {
62         int err;
63 #ifndef HAVE_GPFS
64         lock_type fl = {0};
65
66         if (size <= 0) {
67                 return 0;
68         }
69
70         fl.l_whence = SEEK_SET;
71         fl.l_start = 0;
72         fl.l_len = size;
73
74         /* IMPORTANT: We use RESVSP because we want the extents to be
75          * allocated, but we don't want the allocation to show up in
76          * st_size or persist after the close(2).
77          */
78
79 #if defined(XFS_IOC_RESVSP64)
80         /* On Linux this comes in via libxfs.h. */
81         err = xfsctl(NULL, fd, XFS_IOC_RESVSP64, &fl);
82 #elif defined(F_RESVSP64)
83         /* On IRIX, this comes from fcntl.h. */
84         err = fcntl(fd, F_RESVSP64, &fl);
85 #else
86         err = -1;
87         errno = ENOSYS;
88 #endif
89 #else /* GPFS uses completely different interface */
90        err = gpfs_prealloc(fd, (gpfs_off64_t)0, (gpfs_off64_t)size);
91 #endif
92
93         if (err) {
94                 DEBUG(module_debug,
95                         ("%s: preallocate failed on fd=%d size=%lld: %s\n",
96                         MODULE, fd, (long long)size, strerror(errno)));
97         }
98
99         return err;
100 }
101
102 static int prealloc_connect(
103                 struct vfs_handle_struct *  handle,
104                 const char *                service,
105                 const char *                user)
106 {
107         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
108
109         if (ret < 0) {
110                 return ret;
111         }
112
113         module_debug = lp_parm_int(SNUM(handle->conn),
114                                         MODULE, "debug", 100);
115
116         return 0;
117 }
118
119 static int prealloc_open(vfs_handle_struct* handle,
120                         struct smb_filename *smb_fname,
121                         files_struct *      fsp,
122                         int                 flags,
123                         mode_t              mode)
124 {
125         int fd;
126         off64_t size = 0;
127
128         const char * dot;
129         char fext[10];
130
131         if (!(flags & (O_CREAT|O_TRUNC))) {
132                 /* Caller is not intending to rewrite the file. Let's not mess
133                  * with the allocation in this case.
134                  */
135                 goto normal_open;
136         }
137
138         *fext = '\0';
139         dot = strrchr(smb_fname->base_name, '.');
140         if (dot && *++dot) {
141                 if (strlen(dot) < sizeof(fext)) {
142                         strncpy(fext, dot, sizeof(fext));
143                         strnorm(fext, CASE_LOWER);
144                 }
145         }
146
147         if (*fext == '\0') {
148                 goto normal_open;
149         }
150
151         /* Syntax for specifying preallocation size is:
152          *      MODULE: <extension> = <size>
153          * where
154          *      <extension> is the file extension in lower case
155          *      <size> is a size like 10, 10K, 10M
156          */
157         size = conv_str_size(lp_parm_const_string(SNUM(handle->conn), MODULE,
158                                                     fext, NULL));
159         if (size <= 0) {
160                 /* No need to preallocate this file. */
161                 goto normal_open;
162         }
163
164         fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
165         if (fd < 0) {
166                 return fd;
167         }
168
169         /* Prellocate only if the file is being created or replaced. Note that
170          * Samba won't ever pass down O_TRUNC, which is why we have to handle
171          * truncate calls specially.
172          */
173         if ((flags & O_CREAT) || (flags & O_TRUNC)) {
174                 SMB_OFF_T * psize;
175
176                 psize = VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T, NULL);
177                 if (psize == NULL || *psize == -1) {
178                         return fd;
179                 }
180
181                 DEBUG(module_debug,
182                         ("%s: preallocating %s (fd=%d) to %lld bytes\n",
183                             MODULE, smb_fname_str_dbg(smb_fname), fd,
184                             (long long)size));
185
186                 *psize = size;
187                 if (preallocate_space(fd, *psize) < 0) {
188                         VFS_REMOVE_FSP_EXTENSION(handle, fsp);
189                 }
190         }
191
192         return fd;
193
194 normal_open:
195         /* We are not creating or replacing a file. Skip the
196          * preallocation.
197          */
198         DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
199                 MODULE, smb_fname_str_dbg(smb_fname)));
200         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
201 }
202
203 static int prealloc_ftruncate(vfs_handle_struct * handle,
204                         files_struct *  fsp,
205                         SMB_OFF_T       offset)
206 {
207         SMB_OFF_T *psize;
208         int ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
209
210         /* Maintain the allocated space even in the face of truncates. */
211         if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
212                 preallocate_space(fsp->fh->fd, *psize);
213         }
214
215         return ret;
216 }
217
218 static struct vfs_fn_pointers prealloc_fns = {
219         .open_fn = prealloc_open,
220         .ftruncate = prealloc_ftruncate,
221         .connect_fn = prealloc_connect,
222 };
223
224 NTSTATUS vfs_prealloc_init(void);
225 NTSTATUS vfs_prealloc_init(void)
226 {
227         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
228                                 MODULE, &prealloc_fns);
229 }