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 2 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
51 #define MODULE "prealloc"
52 static int module_debug
;
54 static int preallocate_space(int fd
, SMB_OFF_T size
)
63 fl
.l_whence
= SEEK_SET
;
67 /* IMPORTANT: We use RESVSP because we want the extents to be
68 * allocated, but we don't want the allocation to show up in
69 * st_size or persist after the close(2).
72 #if defined(XFS_IOC_RESVSP64)
73 /* On Linux this comes in via libxfs.h. */
74 err
= xfsctl(NULL
, fd
, XFS_IOC_RESVSP64
, &fl
);
75 #elif defined(F_RESVSP64)
76 /* On IRIX, this comes from fcntl.h. */
77 err
= fcntl(fd
, F_RESVSP64
, &fl
);
85 ("%s: preallocate failed on fd=%d size=%lld: %s\n",
86 MODULE
, fd
, (long long)size
, strerror(errno
)));
92 static int prealloc_connect(
93 struct vfs_handle_struct
* handle
,
97 module_debug
= lp_parm_int(SNUM(handle
->conn
),
98 MODULE
, "debug", 100);
100 return SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
103 static int prealloc_open(vfs_handle_struct
* handle
,
115 if (!(flags
& (O_CREAT
|O_TRUNC
))) {
116 /* Caller is not intending to rewrite the file. Let's not mess
117 * with the allocation in this case.
123 dot
= strrchr(fname
, '.');
125 if (strlen(dot
) < sizeof(fext
)) {
126 strncpy(fext
, dot
, sizeof(fext
));
127 strnorm(fext
, CASE_LOWER
);
135 /* Syntax for specifying preallocation size is:
136 * MODULE: <extension> = <size>
138 * <extension> is the file extension in lower case
139 * <size> is a size like 10, 10K, 10M
141 size
= conv_str_size(lp_parm_const_string(SNUM(handle
->conn
), MODULE
,
144 /* No need to preallocate this file. */
148 fd
= SMB_VFS_NEXT_OPEN(handle
, fname
, fsp
, flags
, mode
);
153 /* Prellocate only if the file is being created or replaced. Note that
154 * Samba won't ever pass down O_TRUNC, which is why we have to handle
155 * truncate calls specially.
157 if ((flags
& O_CREAT
) || (flags
& O_TRUNC
)) {
160 psize
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, SMB_OFF_T
);
161 if (psize
== NULL
|| *psize
== -1) {
166 ("%s: preallocating %s (fd=%d) to %lld bytes\n",
167 MODULE
, fname
, fd
, (long long)size
));
170 if (preallocate_space(fd
, *psize
) < 0) {
171 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
178 /* We are not creating or replacing a file. Skip the
181 DEBUG(module_debug
, ("%s: skipping preallocation for %s\n",
183 return SMB_VFS_NEXT_OPEN(handle
, fname
, fsp
, flags
, mode
);
186 static int prealloc_ftruncate(vfs_handle_struct
* handle
,
192 int ret
= SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, fd
, offset
);
194 /* Maintain the allocated space even in the face of truncates. */
195 if ((psize
= VFS_FETCH_FSP_EXTENSION(handle
, fsp
))) {
196 preallocate_space(fd
, *psize
);
202 static vfs_op_tuple prealloc_op_tuples
[] = {
203 {SMB_VFS_OP(prealloc_open
), SMB_VFS_OP_OPEN
, SMB_VFS_LAYER_TRANSPARENT
},
204 {SMB_VFS_OP(prealloc_ftruncate
), SMB_VFS_OP_FTRUNCATE
, SMB_VFS_LAYER_TRANSPARENT
},
205 {SMB_VFS_OP(prealloc_connect
), SMB_VFS_OP_CONNECT
, SMB_VFS_LAYER_TRANSPARENT
},
206 {NULL
, SMB_VFS_OP_NOOP
, SMB_VFS_LAYER_NOOP
}
209 NTSTATUS
vfs_prealloc_init(void);
210 NTSTATUS
vfs_prealloc_init(void)
212 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
213 MODULE
, prealloc_op_tuples
);