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