source: trunk-3.0/source/modules/vfs_prealloc.c@ 101

Last change on this file since 101 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 5.4 KB
Line 
1/*
2 * XFS preallocation support module.
3 *
4 * Copyright (c) James Peach 2006
5 *
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.
10 *
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.
15 *
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.
19 */
20
21#include "includes.h"
22
23/* Extent preallocation module.
24 *
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.
29 *
30 * Currently only implemented for XFS. This module is based on an original idea
31 * and implementation by Sebastian Brings.
32 *
33 * Tunables.
34 *
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.
38 *
39 * Example.
40 *
41 * prealloc:mpeg = 500M # Preallocate *.mpeg to 500 MiB.
42 */
43
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
50
51#define MODULE "prealloc"
52static int module_debug;
53
54static int preallocate_space(int fd, SMB_OFF_T size)
55{
56 lock_type fl = {0};
57 int err;
58
59 if (size <= 0) {
60 return 0;
61 }
62
63 fl.l_whence = SEEK_SET;
64 fl.l_start = 0;
65 fl.l_len = size;
66
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).
70 */
71
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
82
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)));
87 }
88
89 return err;
90}
91
92static int prealloc_connect(
93 struct vfs_handle_struct * handle,
94 const char * service,
95 const char * user)
96{
97 module_debug = lp_parm_int(SNUM(handle->conn),
98 MODULE, "debug", 100);
99
100 return SMB_VFS_NEXT_CONNECT(handle, service, user);
101}
102
103static int prealloc_open(vfs_handle_struct* handle,
104 const char * fname,
105 files_struct * fsp,
106 int flags,
107 mode_t mode)
108{
109 int fd;
110 off64_t size = 0;
111
112 const char * dot;
113 char fext[10];
114
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.
118 */
119 goto normal_open;
120 }
121
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);
128 }
129 }
130
131 if (*fext == '\0') {
132 goto normal_open;
133 }
134
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
140 */
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;
146 }
147
148 fd = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
149 if (fd < 0) {
150 return fd;
151 }
152
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.
156 */
157 if ((flags & O_CREAT) || (flags & O_TRUNC)) {
158 SMB_OFF_T * psize;
159
160 psize = VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T);
161 if (psize == NULL || *psize == -1) {
162 return fd;
163 }
164
165 DEBUG(module_debug,
166 ("%s: preallocating %s (fd=%d) to %lld bytes\n",
167 MODULE, fname, fd, (long long)size));
168
169 *psize = size;
170 if (preallocate_space(fd, *psize) < 0) {
171 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
172 }
173 }
174
175 return fd;
176
177normal_open:
178 /* We are not creating or replacing a file. Skip the
179 * preallocation.
180 */
181 DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
182 MODULE, fname));
183 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
184}
185
186static int prealloc_ftruncate(vfs_handle_struct * handle,
187 files_struct * fsp,
188 int fd,
189 SMB_OFF_T offset)
190{
191 SMB_OFF_T *psize;
192 int ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, fd, offset);
193
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);
197 }
198
199 return ret;
200}
201
202static 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}
207};
208
209NTSTATUS vfs_prealloc_init(void);
210NTSTATUS vfs_prealloc_init(void)
211{
212 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
213 MODULE, prealloc_op_tuples);
214}
215
Note: See TracBrowser for help on using the repository browser.