vfs: Use static_decl_vfs in all VFS modules
[nivanova/samba-autobuild/.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 #define MODULE "prealloc"
53 static int module_debug;
54
55 static int preallocate_space(int fd, off_t size)
56 {
57         int err;
58         lock_type fl = {0};
59
60         if (size <= 0) {
61                 return 0;
62         }
63
64         fl.l_whence = SEEK_SET;
65         fl.l_start = 0;
66         fl.l_len = size;
67
68         /* IMPORTANT: We use RESVSP because we want the extents to be
69          * allocated, but we don't want the allocation to show up in
70          * st_size or persist after the close(2).
71          */
72
73 #if defined(XFS_IOC_RESVSP64)
74         /* On Linux this comes in via libxfs.h. */
75         err = xfsctl(NULL, fd, XFS_IOC_RESVSP64, &fl);
76 #elif defined(F_RESVSP64)
77         /* On IRIX, this comes from fcntl.h. */
78         err = fcntl(fd, F_RESVSP64, &fl);
79 #else
80         err = -1;
81         errno = ENOSYS;
82 #endif
83         if (err) {
84                 DEBUG(module_debug,
85                         ("%s: preallocate failed on fd=%d size=%lld: %s\n",
86                         MODULE, fd, (long long)size, strerror(errno)));
87         }
88
89         return err;
90 }
91
92 static int prealloc_connect(
93                 struct vfs_handle_struct *  handle,
94                 const char *                service,
95                 const char *                user)
96 {
97         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
98
99         if (ret < 0) {
100                 return ret;
101         }
102
103         module_debug = lp_parm_int(SNUM(handle->conn),
104                                         MODULE, "debug", 100);
105
106         return 0;
107 }
108
109 static int prealloc_open(vfs_handle_struct* handle,
110                         struct smb_filename *smb_fname,
111                         files_struct *      fsp,
112                         int                 flags,
113                         mode_t              mode)
114 {
115         int fd;
116         off_t size = 0;
117
118         const char * dot;
119         char fext[10];
120
121         if (!(flags & (O_CREAT|O_TRUNC))) {
122                 /* Caller is not intending to rewrite the file. Let's not mess
123                  * with the allocation in this case.
124                  */
125                 goto normal_open;
126         }
127
128         *fext = '\0';
129         dot = strrchr(smb_fname->base_name, '.');
130         if (dot && *++dot) {
131                 if (strlen(dot) < sizeof(fext)) {
132                         strncpy(fext, dot, sizeof(fext));
133                         if (!strnorm(fext, CASE_LOWER)) {
134                                 goto normal_open;
135                         }
136                 }
137         }
138
139         if (*fext == '\0') {
140                 goto normal_open;
141         }
142
143         /* Syntax for specifying preallocation size is:
144          *      MODULE: <extension> = <size>
145          * where
146          *      <extension> is the file extension in lower case
147          *      <size> is a size like 10, 10K, 10M
148          */
149         size = conv_str_size(lp_parm_const_string(SNUM(handle->conn), MODULE,
150                                                     fext, NULL));
151         if (size <= 0) {
152                 /* No need to preallocate this file. */
153                 goto normal_open;
154         }
155
156         fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
157         if (fd < 0) {
158                 return fd;
159         }
160
161         /* Prellocate only if the file is being created or replaced. Note that
162          * Samba won't ever pass down O_TRUNC, which is why we have to handle
163          * truncate calls specially.
164          */
165         if ((flags & O_CREAT) || (flags & O_TRUNC)) {
166                 off_t * psize;
167
168                 psize = VFS_ADD_FSP_EXTENSION(handle, fsp, off_t, NULL);
169                 if (psize == NULL || *psize == -1) {
170                         return fd;
171                 }
172
173                 DEBUG(module_debug,
174                         ("%s: preallocating %s (fd=%d) to %lld bytes\n",
175                             MODULE, smb_fname_str_dbg(smb_fname), fd,
176                             (long long)size));
177
178                 *psize = size;
179                 if (preallocate_space(fd, *psize) < 0) {
180                         VFS_REMOVE_FSP_EXTENSION(handle, fsp);
181                 }
182         }
183
184         return fd;
185
186 normal_open:
187         /* We are not creating or replacing a file. Skip the
188          * preallocation.
189          */
190         DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
191                 MODULE, smb_fname_str_dbg(smb_fname)));
192         return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
193 }
194
195 static int prealloc_ftruncate(vfs_handle_struct * handle,
196                         files_struct *  fsp,
197                         off_t   offset)
198 {
199         off_t *psize;
200         int ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
201
202         /* Maintain the allocated space even in the face of truncates. */
203         if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
204                 preallocate_space(fsp->fh->fd, *psize);
205         }
206
207         return ret;
208 }
209
210 static struct vfs_fn_pointers prealloc_fns = {
211         .open_fn = prealloc_open,
212         .ftruncate_fn = prealloc_ftruncate,
213         .connect_fn = prealloc_connect,
214 };
215
216 static_decl_vfs;
217 NTSTATUS vfs_prealloc_init(TALLOC_CTX *ctx)
218 {
219         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
220                                 MODULE, &prealloc_fns);
221 }