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 "system/filesys.h"
22 #include "smbd/smbd.h"
24 /* Extent preallocation module.
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
31 * Currently only implemented for XFS. This module is based on an original idea
32 * and implementation by Sebastian Brings.
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.
42 * prealloc:mpeg = 500M # Preallocate *.mpeg to 500 MiB.
45 #ifdef HAVE_XFS_LIBXFS_H
46 #include <xfs/libxfs.h>
47 #define lock_type xfs_flock64_t
49 #define lock_type struct flock64
56 #define MODULE "prealloc"
57 static int module_debug
;
59 static int preallocate_space(int fd
, off_t size
)
69 fl
.l_whence
= SEEK_SET
;
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).
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
);
88 #else /* GPFS uses completely different interface */
89 err
= gpfs_prealloc(fd
, (gpfs_off64_t
)0, (gpfs_off64_t
)size
);
94 ("%s: preallocate failed on fd=%d size=%lld: %s\n",
95 MODULE
, fd
, (long long)size
, strerror(errno
)));
101 static int prealloc_connect(
102 struct vfs_handle_struct
* handle
,
103 const char * service
,
106 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
112 module_debug
= lp_parm_int(SNUM(handle
->conn
),
113 MODULE
, "debug", 100);
118 static int prealloc_open(vfs_handle_struct
* handle
,
119 struct smb_filename
*smb_fname
,
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.
138 dot
= strrchr(smb_fname
->base_name
, '.');
140 if (strlen(dot
) < sizeof(fext
)) {
141 strncpy(fext
, dot
, sizeof(fext
));
142 if (!strnorm(fext
, CASE_LOWER
)) {
152 /* Syntax for specifying preallocation size is:
153 * MODULE: <extension> = <size>
155 * <extension> is the file extension in lower case
156 * <size> is a size like 10, 10K, 10M
158 size
= conv_str_size(lp_parm_const_string(SNUM(handle
->conn
), MODULE
,
161 /* No need to preallocate this file. */
165 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
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.
174 if ((flags
& O_CREAT
) || (flags
& O_TRUNC
)) {
177 psize
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, off_t
, NULL
);
178 if (psize
== NULL
|| *psize
== -1) {
183 ("%s: preallocating %s (fd=%d) to %lld bytes\n",
184 MODULE
, smb_fname_str_dbg(smb_fname
), fd
,
188 if (preallocate_space(fd
, *psize
) < 0) {
189 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
196 /* We are not creating or replacing a file. Skip the
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
);
204 static int prealloc_ftruncate(vfs_handle_struct
* handle
,
209 int ret
= SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
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
);
219 static struct vfs_fn_pointers prealloc_fns
= {
220 .open_fn
= prealloc_open
,
221 .ftruncate_fn
= prealloc_ftruncate
,
222 .connect_fn
= prealloc_connect
,
225 NTSTATUS
vfs_prealloc_init(void);
226 NTSTATUS
vfs_prealloc_init(void)
228 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
229 MODULE
, &prealloc_fns
);