[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / modules / vfs_prealloc.c
blob2e0b8b18b49380f03a215d29d0efc0e68f9fd8c3
1 /*
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.
21 #include "includes.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
28 * layouts on disk.
30 * Currently only implemented for XFS. This module is based on an original idea
31 * and implementation by Sebastian Brings.
33 * Tunables.
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.
39 * Example.
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
47 #else
48 #define lock_type struct flock64
49 #endif
51 #define MODULE "prealloc"
52 static int module_debug;
54 static int preallocate_space(int fd, SMB_OFF_T size)
56 lock_type fl = {0};
57 int err;
59 if (size <= 0) {
60 return 0;
63 fl.l_whence = SEEK_SET;
64 fl.l_start = 0;
65 fl.l_len = size;
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);
78 #else
79 err = -1;
80 errno = ENOSYS;
81 #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)));
89 return err;
92 static int prealloc_connect(
93 struct vfs_handle_struct * handle,
94 const char * service,
95 const char * user)
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,
104 const char * fname,
105 files_struct * fsp,
106 int flags,
107 mode_t mode)
109 int fd;
110 off64_t size = 0;
112 const char * dot;
113 char fext[10];
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.
119 goto normal_open;
122 *fext = '\0';
123 dot = strrchr(fname, '.');
124 if (dot && *++dot) {
125 if (strlen(dot) < sizeof(fext)) {
126 strncpy(fext, dot, sizeof(fext));
127 strnorm(fext, CASE_LOWER);
131 if (*fext == '\0') {
132 goto normal_open;
135 /* Syntax for specifying preallocation size is:
136 * MODULE: <extension> = <size>
137 * where
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,
142 fext, NULL));
143 if (size <= 0) {
144 /* No need to preallocate this file. */
145 goto normal_open;
148 fd = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
149 if (fd < 0) {
150 return fd;
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)) {
158 SMB_OFF_T * psize;
160 psize = VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T);
161 if (psize == NULL || *psize == -1) {
162 return fd;
165 DEBUG(module_debug,
166 ("%s: preallocating %s (fd=%d) to %lld bytes\n",
167 MODULE, fname, fd, (long long)size));
169 *psize = size;
170 if (preallocate_space(fd, *psize) < 0) {
171 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
175 return fd;
177 normal_open:
178 /* We are not creating or replacing a file. Skip the
179 * preallocation.
181 DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
182 MODULE, fname));
183 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
186 static int prealloc_ftruncate(vfs_handle_struct * handle,
187 files_struct * fsp,
188 int fd,
189 SMB_OFF_T offset)
191 SMB_OFF_T *psize;
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);
199 return ret;
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);