2 * XFS preallocation support module.
4 * Copyright (c) James Peach 2006
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.
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.
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/>.
21 #include "smbd/smbd.h"
23 /* Extent preallocation module.
25 * The purpose of this module is to preallocate space on the filesystem when
26 * we have a good idea of how large files are supposed to be. This lets writes
27 * proceed without having to allocate new extents and results in better file
30 * Currently only implemented for XFS. This module is based on an original idea
31 * and implementation by Sebastian Brings.
35 * prealloc: <ext> Number of bytes to preallocate for a file with
36 * the matching extension.
37 * prealloc:debug Debug level at which to emit messages.
41 * prealloc:mpeg = 500M # Preallocate *.mpeg to 500 MiB.
44 #ifdef HAVE_XFS_LIBXFS_H
45 #include <xfs/libxfs.h>
46 #define lock_type xfs_flock64_t
48 #define lock_type struct flock64
55 #define MODULE "prealloc"
56 static int module_debug
;
58 static int preallocate_space(int fd
, SMB_OFF_T size
)
68 fl
.l_whence
= SEEK_SET
;
72 /* IMPORTANT: We use RESVSP because we want the extents to be
73 * allocated, but we don't want the allocation to show up in
74 * st_size or persist after the close(2).
77 #if defined(XFS_IOC_RESVSP64)
78 /* On Linux this comes in via libxfs.h. */
79 err
= xfsctl(NULL
, fd
, XFS_IOC_RESVSP64
, &fl
);
80 #elif defined(F_RESVSP64)
81 /* On IRIX, this comes from fcntl.h. */
82 err
= fcntl(fd
, F_RESVSP64
, &fl
);
87 #else /* GPFS uses completely different interface */
88 err
= gpfs_prealloc(fd
, (gpfs_off64_t
)0, (gpfs_off64_t
)size
);
93 ("%s: preallocate failed on fd=%d size=%lld: %s\n",
94 MODULE
, fd
, (long long)size
, strerror(errno
)));
100 static int prealloc_connect(
101 struct vfs_handle_struct
* handle
,
102 const char * service
,
105 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
111 module_debug
= lp_parm_int(SNUM(handle
->conn
),
112 MODULE
, "debug", 100);
117 static int prealloc_open(vfs_handle_struct
* handle
,
118 struct smb_filename
*smb_fname
,
129 if (!(flags
& (O_CREAT
|O_TRUNC
))) {
130 /* Caller is not intending to rewrite the file. Let's not mess
131 * with the allocation in this case.
137 dot
= strrchr(smb_fname
->base_name
, '.');
139 if (strlen(dot
) < sizeof(fext
)) {
140 strncpy(fext
, dot
, sizeof(fext
));
141 strnorm(fext
, CASE_LOWER
);
149 /* Syntax for specifying preallocation size is:
150 * MODULE: <extension> = <size>
152 * <extension> is the file extension in lower case
153 * <size> is a size like 10, 10K, 10M
155 size
= conv_str_size(lp_parm_const_string(SNUM(handle
->conn
), MODULE
,
158 /* No need to preallocate this file. */
162 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
167 /* Prellocate only if the file is being created or replaced. Note that
168 * Samba won't ever pass down O_TRUNC, which is why we have to handle
169 * truncate calls specially.
171 if ((flags
& O_CREAT
) || (flags
& O_TRUNC
)) {
174 psize
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, SMB_OFF_T
, NULL
);
175 if (psize
== NULL
|| *psize
== -1) {
180 ("%s: preallocating %s (fd=%d) to %lld bytes\n",
181 MODULE
, smb_fname_str_dbg(smb_fname
), fd
,
185 if (preallocate_space(fd
, *psize
) < 0) {
186 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
193 /* We are not creating or replacing a file. Skip the
196 DEBUG(module_debug
, ("%s: skipping preallocation for %s\n",
197 MODULE
, smb_fname_str_dbg(smb_fname
)));
198 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
201 static int prealloc_ftruncate(vfs_handle_struct
* handle
,
206 int ret
= SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
208 /* Maintain the allocated space even in the face of truncates. */
209 if ((psize
= VFS_FETCH_FSP_EXTENSION(handle
, fsp
))) {
210 preallocate_space(fsp
->fh
->fd
, *psize
);
216 static struct vfs_fn_pointers prealloc_fns
= {
217 .open_fn
= prealloc_open
,
218 .ftruncate
= prealloc_ftruncate
,
219 .connect_fn
= prealloc_connect
,
222 NTSTATUS
vfs_prealloc_init(void);
223 NTSTATUS
vfs_prealloc_init(void)
225 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
226 MODULE
, &prealloc_fns
);