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