s3-vfs_shadow_copy2: Also accept a sscanf result
[kai/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, 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                         if (!strnorm(fext, CASE_LOWER)) {
143                                 goto normal_open;
144                         }
145                 }
146         }
147
148         if (*fext == '\0') {
149                 goto normal_open;
150         }
151
152         /* Syntax for specifying preallocation size is:
153          *      MODULE: <extension> = <size>
154          * where
155          *      <extension> is the file extension in lower case
156          *      <size> is a size like 10, 10K, 10M
157          */
158         size = conv_str_size(lp_parm_const_string(SNUM(handle->conn), MODULE,
159                                                     fext, NULL));
160         if (size <= 0) {
161                 /* No need to preallocate this file. */
162                 goto normal_open;
163         }
164
165         fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
166         if (fd < 0) {
167                 return fd;
168         }
169
170         /* Prellocate only if the file is being created or replaced. Note that
171          * Samba won't ever pass down O_TRUNC, which is why we have to handle
172          * truncate calls specially.
173          */
174         if ((flags & O_CREAT) || (flags & O_TRUNC)) {
175                 off_t * psize;
176
177                 psize = VFS_ADD_FSP_EXTENSION(handle, fsp, off_t, NULL);
178                 if (psize == NULL || *psize == -1) {
179                         return fd;
180                 }
181
182                 DEBUG(module_debug,
183                         ("%s: preallocating %s (fd=%d) to %lld bytes\n",
184                             MODULE, smb_fname_str_dbg(smb_fname), fd,
185                             (long long)size));
186
187                 *psize = size;
188                 if (preallocate_space(fd, *psize) < 0) {
189                         VFS_REMOVE_FSP_EXTENSION(handle, fsp);
190                 }
191         }
192
193         return fd;
194
195 normal_open:
196         /* We are not creating or replacing a file. Skip the
197          * preallocation.
198          */
199         DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
200                 MODULE, smb_fname_str_dbg(smb_fname)));
201         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
202 }
203
204 static int prealloc_ftruncate(vfs_handle_struct * handle,
205                         files_struct *  fsp,
206                         off_t   offset)
207 {
208         off_t *psize;
209         int ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
210
211         /* Maintain the allocated space even in the face of truncates. */
212         if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
213                 preallocate_space(fsp->fh->fd, *psize);
214         }
215
216         return ret;
217 }
218
219 static struct vfs_fn_pointers prealloc_fns = {
220         .open_fn = prealloc_open,
221         .ftruncate_fn = prealloc_ftruncate,
222         .connect_fn = prealloc_connect,
223 };
224
225 NTSTATUS vfs_prealloc_init(void);
226 NTSTATUS vfs_prealloc_init(void)
227 {
228         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
229                                 MODULE, &prealloc_fns);
230 }