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