libnet keytab: add parameter ingnore_kvno to libnet_keytab_remove_entries()
[Samba.git] / source / modules / vfs_prealloc.c
blob2a06e3d81bd5b7ae84a6520efeb3eb3d6e6dc6e8
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 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/>.
20 #include "includes.h"
22 /* Extent preallocation module.
24 * The purpose of this module is to preallocate space on the filesystem when
25 * we have a good idea of how large files are supposed to be. This lets writes
26 * proceed without having to allocate new extents and results in better file
27 * layouts on disk.
29 * Currently only implemented for XFS. This module is based on an original idea
30 * and implementation by Sebastian Brings.
32 * Tunables.
34 * prealloc: <ext> Number of bytes to preallocate for a file with
35 * the matching extension.
36 * prealloc:debug Debug level at which to emit messages.
38 * Example.
40 * prealloc:mpeg = 500M # Preallocate *.mpeg to 500 MiB.
43 #ifdef HAVE_XFS_LIBXFS_H
44 #include <xfs/libxfs.h>
45 #define lock_type xfs_flock64_t
46 #else
47 #define lock_type struct flock64
48 #endif
50 #ifdef HAVE_GPFS
51 #include "gpfs_gpl.h"
52 #endif
54 #define MODULE "prealloc"
55 static int module_debug;
57 static int preallocate_space(int fd, SMB_OFF_T size)
59 #ifndef HAVE_GPFS
60 lock_type fl = {0};
61 int err;
63 if (size <= 0) {
64 return 0;
67 fl.l_whence = SEEK_SET;
68 fl.l_start = 0;
69 fl.l_len = size;
71 /* IMPORTANT: We use RESVSP because we want the extents to be
72 * allocated, but we don't want the allocation to show up in
73 * st_size or persist after the close(2).
76 #if defined(XFS_IOC_RESVSP64)
77 /* On Linux this comes in via libxfs.h. */
78 err = xfsctl(NULL, fd, XFS_IOC_RESVSP64, &fl);
79 #elif defined(F_RESVSP64)
80 /* On IRIX, this comes from fcntl.h. */
81 err = fcntl(fd, F_RESVSP64, &fl);
82 #else
83 err = -1;
84 errno = ENOSYS;
85 #endif
86 #else /* GPFS uses completely different interface */
87 err = gpfs_prealloc(fd, (gpfs_off64_t)0, (gpfs_off64_t)size);
88 #endif
90 if (err) {
91 DEBUG(module_debug,
92 ("%s: preallocate failed on fd=%d size=%lld: %s\n",
93 MODULE, fd, (long long)size, strerror(errno)));
96 return err;
99 static int prealloc_connect(
100 struct vfs_handle_struct * handle,
101 const char * service,
102 const char * user)
104 module_debug = lp_parm_int(SNUM(handle->conn),
105 MODULE, "debug", 100);
107 return SMB_VFS_NEXT_CONNECT(handle, service, user);
110 static int prealloc_open(vfs_handle_struct* handle,
111 const char * fname,
112 files_struct * fsp,
113 int flags,
114 mode_t mode)
116 int fd;
117 off64_t size = 0;
119 const char * dot;
120 char fext[10];
122 if (!(flags & (O_CREAT|O_TRUNC))) {
123 /* Caller is not intending to rewrite the file. Let's not mess
124 * with the allocation in this case.
126 goto normal_open;
129 *fext = '\0';
130 dot = strrchr(fname, '.');
131 if (dot && *++dot) {
132 if (strlen(dot) < sizeof(fext)) {
133 strncpy(fext, dot, sizeof(fext));
134 strnorm(fext, CASE_LOWER);
138 if (*fext == '\0') {
139 goto normal_open;
142 /* Syntax for specifying preallocation size is:
143 * MODULE: <extension> = <size>
144 * where
145 * <extension> is the file extension in lower case
146 * <size> is a size like 10, 10K, 10M
148 size = conv_str_size(lp_parm_const_string(SNUM(handle->conn), MODULE,
149 fext, NULL));
150 if (size <= 0) {
151 /* No need to preallocate this file. */
152 goto normal_open;
155 fd = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
156 if (fd < 0) {
157 return fd;
160 /* Prellocate only if the file is being created or replaced. Note that
161 * Samba won't ever pass down O_TRUNC, which is why we have to handle
162 * truncate calls specially.
164 if ((flags & O_CREAT) || (flags & O_TRUNC)) {
165 SMB_OFF_T * psize;
167 psize = VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T);
168 if (psize == NULL || *psize == -1) {
169 return fd;
172 DEBUG(module_debug,
173 ("%s: preallocating %s (fd=%d) to %lld bytes\n",
174 MODULE, fname, fd, (long long)size));
176 *psize = size;
177 if (preallocate_space(fd, *psize) < 0) {
178 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
182 return fd;
184 normal_open:
185 /* We are not creating or replacing a file. Skip the
186 * preallocation.
188 DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
189 MODULE, fname));
190 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
193 static int prealloc_ftruncate(vfs_handle_struct * handle,
194 files_struct * fsp,
195 SMB_OFF_T offset)
197 SMB_OFF_T *psize;
198 int ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
200 /* Maintain the allocated space even in the face of truncates. */
201 if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
202 preallocate_space(fsp->fh->fd, *psize);
205 return ret;
208 static vfs_op_tuple prealloc_op_tuples[] = {
209 {SMB_VFS_OP(prealloc_open), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
210 {SMB_VFS_OP(prealloc_ftruncate), SMB_VFS_OP_FTRUNCATE, SMB_VFS_LAYER_TRANSPARENT},
211 {SMB_VFS_OP(prealloc_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_TRANSPARENT},
212 {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
215 NTSTATUS vfs_prealloc_init(void);
216 NTSTATUS vfs_prealloc_init(void)
218 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
219 MODULE, prealloc_op_tuples);