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 3 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, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "smbd/smbd.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 | #ifdef HAVE_GPFS
|
---|
52 | #include "gpfs_gpl.h"
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | #define MODULE "prealloc"
|
---|
56 | static int module_debug;
|
---|
57 |
|
---|
58 | static int preallocate_space(int fd, SMB_OFF_T size)
|
---|
59 | {
|
---|
60 | int err;
|
---|
61 | #ifndef HAVE_GPFS
|
---|
62 | lock_type fl = {0};
|
---|
63 |
|
---|
64 | if (size <= 0) {
|
---|
65 | return 0;
|
---|
66 | }
|
---|
67 |
|
---|
68 | fl.l_whence = SEEK_SET;
|
---|
69 | fl.l_start = 0;
|
---|
70 | fl.l_len = size;
|
---|
71 |
|
---|
72 | /* IMPORTANT: We use RESVSP because we want the extents to be
|
---|
73 | * allocated, but we don't want the allocation to show up in
|
---|
74 | * st_size or persist after the close(2).
|
---|
75 | */
|
---|
76 |
|
---|
77 | #if defined(XFS_IOC_RESVSP64)
|
---|
78 | /* On Linux this comes in via libxfs.h. */
|
---|
79 | err = xfsctl(NULL, fd, XFS_IOC_RESVSP64, &fl);
|
---|
80 | #elif defined(F_RESVSP64)
|
---|
81 | /* On IRIX, this comes from fcntl.h. */
|
---|
82 | err = fcntl(fd, F_RESVSP64, &fl);
|
---|
83 | #else
|
---|
84 | err = -1;
|
---|
85 | errno = ENOSYS;
|
---|
86 | #endif
|
---|
87 | #else /* GPFS uses completely different interface */
|
---|
88 | err = gpfs_prealloc(fd, (gpfs_off64_t)0, (gpfs_off64_t)size);
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | if (err) {
|
---|
92 | DEBUG(module_debug,
|
---|
93 | ("%s: preallocate failed on fd=%d size=%lld: %s\n",
|
---|
94 | MODULE, fd, (long long)size, strerror(errno)));
|
---|
95 | }
|
---|
96 |
|
---|
97 | return err;
|
---|
98 | }
|
---|
99 |
|
---|
100 | static int prealloc_connect(
|
---|
101 | struct vfs_handle_struct * handle,
|
---|
102 | const char * service,
|
---|
103 | const char * user)
|
---|
104 | {
|
---|
105 | int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
|
---|
106 |
|
---|
107 | if (ret < 0) {
|
---|
108 | return ret;
|
---|
109 | }
|
---|
110 |
|
---|
111 | module_debug = lp_parm_int(SNUM(handle->conn),
|
---|
112 | MODULE, "debug", 100);
|
---|
113 |
|
---|
114 | return 0;
|
---|
115 | }
|
---|
116 |
|
---|
117 | static int prealloc_open(vfs_handle_struct* handle,
|
---|
118 | struct smb_filename *smb_fname,
|
---|
119 | files_struct * fsp,
|
---|
120 | int flags,
|
---|
121 | mode_t mode)
|
---|
122 | {
|
---|
123 | int fd;
|
---|
124 | off64_t size = 0;
|
---|
125 |
|
---|
126 | const char * dot;
|
---|
127 | char fext[10];
|
---|
128 |
|
---|
129 | if (!(flags & (O_CREAT|O_TRUNC))) {
|
---|
130 | /* Caller is not intending to rewrite the file. Let's not mess
|
---|
131 | * with the allocation in this case.
|
---|
132 | */
|
---|
133 | goto normal_open;
|
---|
134 | }
|
---|
135 |
|
---|
136 | *fext = '\0';
|
---|
137 | dot = strrchr(smb_fname->base_name, '.');
|
---|
138 | if (dot && *++dot) {
|
---|
139 | if (strlen(dot) < sizeof(fext)) {
|
---|
140 | strncpy(fext, dot, sizeof(fext));
|
---|
141 | strnorm(fext, CASE_LOWER);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (*fext == '\0') {
|
---|
146 | goto normal_open;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* Syntax for specifying preallocation size is:
|
---|
150 | * MODULE: <extension> = <size>
|
---|
151 | * where
|
---|
152 | * <extension> is the file extension in lower case
|
---|
153 | * <size> is a size like 10, 10K, 10M
|
---|
154 | */
|
---|
155 | size = conv_str_size(lp_parm_const_string(SNUM(handle->conn), MODULE,
|
---|
156 | fext, NULL));
|
---|
157 | if (size <= 0) {
|
---|
158 | /* No need to preallocate this file. */
|
---|
159 | goto normal_open;
|
---|
160 | }
|
---|
161 |
|
---|
162 | fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
|
---|
163 | if (fd < 0) {
|
---|
164 | return fd;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* Prellocate only if the file is being created or replaced. Note that
|
---|
168 | * Samba won't ever pass down O_TRUNC, which is why we have to handle
|
---|
169 | * truncate calls specially.
|
---|
170 | */
|
---|
171 | if ((flags & O_CREAT) || (flags & O_TRUNC)) {
|
---|
172 | SMB_OFF_T * psize;
|
---|
173 |
|
---|
174 | psize = VFS_ADD_FSP_EXTENSION(handle, fsp, SMB_OFF_T, NULL);
|
---|
175 | if (psize == NULL || *psize == -1) {
|
---|
176 | return fd;
|
---|
177 | }
|
---|
178 |
|
---|
179 | DEBUG(module_debug,
|
---|
180 | ("%s: preallocating %s (fd=%d) to %lld bytes\n",
|
---|
181 | MODULE, smb_fname_str_dbg(smb_fname), fd,
|
---|
182 | (long long)size));
|
---|
183 |
|
---|
184 | *psize = size;
|
---|
185 | if (preallocate_space(fd, *psize) < 0) {
|
---|
186 | VFS_REMOVE_FSP_EXTENSION(handle, fsp);
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | return fd;
|
---|
191 |
|
---|
192 | normal_open:
|
---|
193 | /* We are not creating or replacing a file. Skip the
|
---|
194 | * preallocation.
|
---|
195 | */
|
---|
196 | DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
|
---|
197 | MODULE, smb_fname_str_dbg(smb_fname)));
|
---|
198 | return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
|
---|
199 | }
|
---|
200 |
|
---|
201 | static int prealloc_ftruncate(vfs_handle_struct * handle,
|
---|
202 | files_struct * fsp,
|
---|
203 | SMB_OFF_T offset)
|
---|
204 | {
|
---|
205 | SMB_OFF_T *psize;
|
---|
206 | int ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
|
---|
207 |
|
---|
208 | /* Maintain the allocated space even in the face of truncates. */
|
---|
209 | if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
|
---|
210 | preallocate_space(fsp->fh->fd, *psize);
|
---|
211 | }
|
---|
212 |
|
---|
213 | return ret;
|
---|
214 | }
|
---|
215 |
|
---|
216 | static struct vfs_fn_pointers prealloc_fns = {
|
---|
217 | .open_fn = prealloc_open,
|
---|
218 | .ftruncate = prealloc_ftruncate,
|
---|
219 | .connect_fn = prealloc_connect,
|
---|
220 | };
|
---|
221 |
|
---|
222 | NTSTATUS vfs_prealloc_init(void);
|
---|
223 | NTSTATUS vfs_prealloc_init(void)
|
---|
224 | {
|
---|
225 | return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
|
---|
226 | MODULE, &prealloc_fns);
|
---|
227 | }
|
---|